Text editor plugin, InputDialog enhancements (#4135)

This plugin mostly sets up a "Text editor>" submenu, that allows
browsing files, creating a new file, and managing a history of
previously opened file for easier re-opening.
It restore previous scroll and cursor positions on re-opening.
Additional "Check lua" syntax button is added when editing
a .lua file, and prevent saving if errors.
The text editing is mainly provided by the enhanced InputDialog.

InputDialog: added a few more options, the main one being
'save_callback', which will add a Save and Close buttons
and manage saving/discarding/exiting.
If "fullscreen" and "add_nav_bar", will add a show/hide keyboard
button to it.
Moved the preset buttons setup code in their own InputDialog
methods for clarity of the main init code.
Buttons are now enabled/disabled depending on context for feedback
(eg: Save is disabled as long as text has not been modified).

Added util.checkLuaSyntax(lua_string), might be useful elsewhere.
This commit is contained in:
poire-z
2018-08-06 21:16:30 +02:00
committed by GitHub
parent 1d18b01cf7
commit 6e35e683dd
11 changed files with 927 additions and 62 deletions

View File

@@ -28,6 +28,8 @@ local InputText = InputContainer:new{
scroll = false, -- whether to allow scrolling (will be set to true if no height provided)
focused = true,
parent = nil, -- parent dialog that will be set dirty
edit_callback = nil, -- called with true when text modified, false on init or text re-set
scroll_callback = nil, -- called with (low, high) when view is scrolled (cf ScrollTextWidget)
width = nil,
height = nil, -- when nil, will be set to original text height (possibly
@@ -209,9 +211,14 @@ function InputText:init()
-- text_type changes from "password" to "text" when we toggle password
self.is_password_type = true
end
-- Beware other cases where implicit conversion to text may be done
-- at some point, but checkTextEditability() would say "not editable".
if self.input_type == "number" and type(self.text) == "number" then
-- checkTextEditability() fails if self.text stays not a string
self.text = tostring(self.text)
end
self:initTextBox(self.text)
self:checkTextEditability()
self.is_text_edited = false
if self.readonly ~= true then
self:initKeyboard()
self:initEventListener()
@@ -314,6 +321,7 @@ function InputText:initTextBox(text, char_added)
width = self.width,
height = self.height,
dialog = self.parent,
scroll_callback = self.scroll_callback,
}
else
self.text_widget = TextBoxWidget:new{
@@ -356,6 +364,9 @@ function InputText:initTextBox(text, char_added)
UIManager:setDirty(self.parent, function()
return "ui", self.dimen
end)
if self.edit_callback then
self.edit_callback(self.is_text_edited)
end
end
function InputText:initKeyboard()
@@ -408,11 +419,16 @@ function InputText:getKeyboardDimen()
end
function InputText:addChars(chars)
if not chars then
-- VirtualKeyboard:addChar(key) gave us 'nil' once (?!)
-- which would crash table.concat()
return
end
if self.enter_callback and chars == "\n" then
UIManager:scheduleIn(0.3, function() self.enter_callback() end)
return
end
if not self:isTextEditable(true) then
if self.readonly or not self:isTextEditable(true) then
return
end
self.is_text_edited = true
@@ -422,7 +438,7 @@ function InputText:addChars(chars)
end
function InputText:delChar()
if not self:isTextEditable(true) then
if self.readonly or not self:isTextEditable(true) then
return
end
if self.charpos == 1 then return end
@@ -433,7 +449,7 @@ function InputText:delChar()
end
function InputText:delToStartOfLine()
if not self:isTextEditable(true) then
if self.readonly or not self:isTextEditable(true) then
return
end
if self.charpos == 1 then return end