From eeb6744947832ec86b4a702d9050966bbb1ca0bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= <24468819+Yagich@users.noreply.github.com> Date: Thu, 23 Jun 2022 21:58:18 +0300 Subject: [PATCH] add optional autosave to file content --- ServerUI.gd | 27 +++++++++++++++++++++++++-- ServerUI.tscn | 5 ----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/ServerUI.gd b/ServerUI.gd index fc45f48..fb1f7de 100644 --- a/ServerUI.gd +++ b/ServerUI.gd @@ -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 diff --git a/ServerUI.tscn b/ServerUI.tscn index c78ba70..31d8177 100644 --- a/ServerUI.tscn +++ b/ServerUI.tscn @@ -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