feat: save edited files
This commit is contained in:
parent
88b45e5098
commit
3d60777a12
@ -27,7 +27,15 @@
|
||||
<splitbox id="editorContainer" split-pos="0.8">
|
||||
<container color="#00000080">
|
||||
<container size-func="-1,30" color="#00000020">
|
||||
<image id="lockIcon" src="gui/lock" enabled="false" size="16" pos="4,6"></image>
|
||||
<image id="lockIcon" src="gui/lock" tooltip="@Read only"
|
||||
interactive="true" onclick="unlock_access()"
|
||||
color="#FFFFFF80" size="16" pos="4,6"
|
||||
hover-color="#1080FF"></image>
|
||||
<image id="saveIcon" src="gui/save" tooltip="@Save"
|
||||
enabled="false" interactive="true"
|
||||
gravity="top-right" hover-color="#1080FF"
|
||||
onclick="save_current_file()"
|
||||
color="#FFFFFF80" size="16" margin="8"></image>
|
||||
<label id="title" pos="26,8"></label>
|
||||
</container>
|
||||
<textbox
|
||||
|
||||
@ -9,6 +9,13 @@ local errors_all = {}
|
||||
local warning_id = 0
|
||||
local error_id = 0
|
||||
|
||||
local writeables = {}
|
||||
|
||||
local current_file = {
|
||||
filename = "",
|
||||
mutable = nil
|
||||
}
|
||||
|
||||
events.on("core:warning", function (wtype, text, traceback)
|
||||
local full = wtype..": "..text
|
||||
if table.has(warnings_all, full) then
|
||||
@ -45,7 +52,59 @@ events.on("core:error", function (msg, traceback)
|
||||
table.insert(errors_all, full)
|
||||
end)
|
||||
|
||||
function open_file_in_editor(filename, line)
|
||||
local function find_mutable(filename)
|
||||
local saved = writeables[file.prefix(filename)]
|
||||
if saved then
|
||||
return saved..":"..file.path(filename)
|
||||
end
|
||||
|
||||
local packid = file.prefix(filename)
|
||||
local packinfo = pack.get_info(packid)
|
||||
if not packinfo then
|
||||
return
|
||||
end
|
||||
local path = packinfo.path
|
||||
if file.is_writeable(path) then
|
||||
return file.join(path, file.path(filename))
|
||||
end
|
||||
end
|
||||
|
||||
local function refresh_file_title()
|
||||
if current_file.filename == "" then
|
||||
document.title.text = ""
|
||||
return
|
||||
end
|
||||
local edited = document.editor.edited
|
||||
current_file.modified = edited
|
||||
document.saveIcon.enabled = edited
|
||||
document.title.text = gui.str('File')..' - '..current_file.filename
|
||||
..(edited and ' *' or '')
|
||||
end
|
||||
|
||||
function unlock_access()
|
||||
if current_file.filename == "" then
|
||||
return
|
||||
end
|
||||
pack.request_writeable(file.prefix(current_file.filename),
|
||||
function(token)
|
||||
writeables[file.prefix(current_file.filename)] = token
|
||||
current_file.mutable = token..":"..file.path(current_file.filename)
|
||||
open_file_in_editor(current_file.filename, 0, current_file.mutable)
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
function save_current_file()
|
||||
if not current_file.mutable then
|
||||
return
|
||||
end
|
||||
file.write(current_file.mutable, document.editor.text)
|
||||
current_file.modified = false
|
||||
document.saveIcon.enabled = false
|
||||
document.title.text = gui.str('File')..' - '..current_file.filename
|
||||
end
|
||||
|
||||
function open_file_in_editor(filename, line, mutable)
|
||||
local editor = document.editor
|
||||
local source = file.read(filename):gsub('\t', ' ')
|
||||
editor.text = source
|
||||
@ -55,7 +114,12 @@ function open_file_in_editor(filename, line)
|
||||
editor.caret = editor:linePos(line)
|
||||
end)
|
||||
end
|
||||
document.title.text = gui.str('File')..' - '..filename
|
||||
document.title.text = gui.str('File') .. ' - ' .. filename
|
||||
current_file.filename = filename
|
||||
current_file.mutable = mutable or find_mutable(filename)
|
||||
document.lockIcon.visible = current_file.mutable == nil
|
||||
document.editor.editable = current_file.mutable ~= nil
|
||||
document.saveIcon.enabled = current_file.modified
|
||||
end
|
||||
|
||||
events.on("core:open_traceback", function(traceback_b64)
|
||||
@ -186,7 +250,6 @@ end
|
||||
function set_mode(mode)
|
||||
local show_prompt = mode == 'chat' or mode == 'console'
|
||||
|
||||
document.title.text = ""
|
||||
document.lockIcon.visible = false
|
||||
document.editorRoot.visible = mode == 'debug'
|
||||
document.editorContainer.visible = mode == 'debug'
|
||||
@ -245,6 +308,8 @@ function on_open(mode)
|
||||
filename = filename
|
||||
}))
|
||||
end
|
||||
|
||||
document.editorContainer:setInterval(200, refresh_file_title)
|
||||
elseif mode then
|
||||
modes:set(mode)
|
||||
end
|
||||
|
||||
@ -25,7 +25,8 @@
|
||||
"gui/check_mark",
|
||||
"gui/left_arrow",
|
||||
"gui/right_arrow",
|
||||
"gui/lock"
|
||||
"gui/lock",
|
||||
"gui/save"
|
||||
],
|
||||
"fonts": [
|
||||
{
|
||||
|
||||
@ -19,6 +19,9 @@ Problems=Проблемы
|
||||
Monitor=Мониторинг
|
||||
Debug=Отладка
|
||||
File=Файл
|
||||
Read only=Только для чтения
|
||||
Save=Сохранить
|
||||
Grant %{0} pack modification permission?=Выдать разрешение на модификацию пака %{0}?
|
||||
|
||||
devtools.traceback=Стек вызовов (от последнего)
|
||||
error.pack-not-found=Не удалось найти пакет
|
||||
|
||||
BIN
res/textures/gui/save.png
Normal file
BIN
res/textures/gui/save.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 123 B |
Loading…
x
Reference in New Issue
Block a user