add optional autosave to file content

This commit is contained in:
Lera Elvoé 2022-06-23 21:58:18 +03:00
parent d3764d3c4a
commit eeb6744947
2 changed files with 25 additions and 7 deletions

View File

@ -7,6 +7,9 @@ const SERVER_STATUS_TEXT: Dictionary = {
const ACCEPTED_FILE_FORMATS := ["md"] # server should ignore these file types when adding endpoints
export(bool) var enable_file_autosave := true # if true, will save the active file's contents every save_file_timeout seconds.
export(float) var save_file_timeout := 4.0 # the time to save document content after the edited signal of TextEdit
enum ContextMenuOptions {
MOVE_UP,
MOVE_DOWN,
@ -24,7 +27,6 @@ onready var file_tree_context_menu := $"%FileTreeContextMenu"
onready var document_input_textedit := $"%DocInputTextEdit"
onready var content_preview_richtextlabel := $"%ContentPreviewRichTextLabel"
onready var document_input_timer := $"%EditedTimeout"
onready var server_folder_dialog := $"%ServerFolderDialog"
@ -157,7 +159,6 @@ func _generate_filestxt():
files += "%s %s %s\n" % [file.file_path, file.date, file.title]
var f := File.new()
if f.open(server_dir.plus_file("files.txt"), File.WRITE) == OK:
f.store_string(files)
f.close()
@ -236,6 +237,26 @@ func _on_DocInputTextEdit_text_changed() -> void:
current_file.content = new_text
current_file.dirty = true
if !current_file.timer:
var t := Timer.new()
t.wait_time = save_file_timeout
t.one_shot = true
current_file.timer = t
add_child(t)
t.connect("timeout", self, "_on_EditedTimeout_timeout", [t, current_file])
else:
current_file.timer.stop()
current_file.timer.start()
func _on_EditedTimeout_timeout(timer: Timer, file: FileDef) -> void:
file.timer = null
timer.queue_free()
var f := File.new()
if f.open(server_dir.plus_file(file.file_path), File.WRITE) == OK:
f.store_string(file.content)
func FileDef(
file_path: String,
@ -259,3 +280,5 @@ class FileDef:
var dirty: bool = false
var content: String
var timer: Timer

View File

@ -206,11 +206,6 @@ size_flags_vertical = 3
show_line_numbers = true
wrap_enabled = true
[node name="EditedTimeout" type="Timer" parent="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer/HSplitContainer/ContentEditContainer/DocInputTextEdit"]
unique_name_in_owner = true
wait_time = 3.0
one_shot = true
[node name="ContentPreviewContainer" type="VBoxContainer" parent="VBoxContainer/HSplitContainer/VBoxContainer/HBoxContainer/VBoxContainer/HSplitContainer"]
margin_left = 390.0
margin_right = 848.0