106 lines
3.3 KiB
GDScript3
106 lines
3.3 KiB
GDScript3
|
extends Control
|
||
|
|
||
|
@onready var select_folder_button: Button = %SelectFolderButton
|
||
|
@onready var start_server_button: Button = %StartServerButton
|
||
|
@onready var open_in_browser_button: Button = %OpenInBrowserButton
|
||
|
@onready var file_tree: Tree = %FileTree
|
||
|
@onready var dnd_help_label: Label = %DnDHelpLabel
|
||
|
@onready var article_name_line_edit: LineEdit = %ArticleNameLineEdit
|
||
|
@onready var article_date_line_edit: LineEdit = %ArticleDateLineEdit
|
||
|
@onready var content_text_edit: TextEdit = %ContentTextEdit
|
||
|
@onready var server_folder_dialog: FileDialog = %ServerFolderDialog
|
||
|
|
||
|
const SERVER_FILE := "res://server_files/index.html"
|
||
|
|
||
|
var working_folder: String
|
||
|
var server: HTTPServer
|
||
|
|
||
|
var server_port: int = 3001
|
||
|
|
||
|
|
||
|
func _ready() -> void:
|
||
|
select_folder_button.pressed.connect(
|
||
|
func():
|
||
|
server_folder_dialog.popup_centered()
|
||
|
)
|
||
|
|
||
|
server_folder_dialog.dir_selected.connect(on_server_folder_dialog_dir_selected)
|
||
|
|
||
|
|
||
|
func on_server_folder_dialog_dir_selected(folder: String) -> void:
|
||
|
working_folder = folder
|
||
|
var dirs := list_files_recursive(working_folder)
|
||
|
print(dirs)
|
||
|
refresh_file_tree(dirs)
|
||
|
dnd_help_label.visible = false
|
||
|
|
||
|
|
||
|
func refresh_file_tree(dirs: Array[String]) -> void:
|
||
|
file_tree.clear()
|
||
|
var root := file_tree.create_item()
|
||
|
for i in dirs:
|
||
|
prints(i, ":", i.get_slice_count("/"))
|
||
|
if i.get_slice_count("/") == 1:
|
||
|
var item := root.create_child()
|
||
|
item.set_text(0, i)
|
||
|
else:
|
||
|
var base := find_or_create_subfolder_item(i.get_base_dir())
|
||
|
var item := base.create_child()
|
||
|
item.set_text(0, i.get_file())
|
||
|
|
||
|
|
||
|
func list_files_recursive(folder: String, prefix: String = "") -> Array[String]:
|
||
|
# var res := [folder.trim_prefix(working_folder).trim_prefix("/")]
|
||
|
var res: Array[String] = []
|
||
|
var dir := DirAccess.open(folder)
|
||
|
if dir:
|
||
|
dir.list_dir_begin()
|
||
|
var file_name := dir.get_next()
|
||
|
while file_name != "":
|
||
|
if dir.current_is_dir():
|
||
|
res.append_array(list_files_recursive(folder.path_join(file_name), folder))
|
||
|
else:
|
||
|
res.append(folder.path_join(file_name).trim_prefix(working_folder + "/"))
|
||
|
file_name = dir.get_next()
|
||
|
|
||
|
res.sort_custom(
|
||
|
func(a: String, b: String):
|
||
|
return b.get_slice_count("/") == 1
|
||
|
)
|
||
|
return res
|
||
|
else:
|
||
|
return []
|
||
|
|
||
|
|
||
|
func find_or_create_subfolder_item(subfolder: String, from_item: TreeItem = file_tree.get_root()) -> TreeItem:
|
||
|
var search_res: TreeItem = null
|
||
|
for item in from_item.get_children():
|
||
|
if item.get_meta("subfolder_name", "") == subfolder:
|
||
|
search_res = item
|
||
|
return search_res
|
||
|
if item.get_child_count() > 0:
|
||
|
search_res = find_or_create_subfolder_item(subfolder, item)
|
||
|
|
||
|
if is_instance_valid(search_res):
|
||
|
return search_res
|
||
|
|
||
|
# early optimization. if there is only one slash, the item is a child of root.
|
||
|
if subfolder.get_slice_count("/") == 1:
|
||
|
var item := file_tree.get_root().create_child()
|
||
|
item.set_meta("subfolder_name", subfolder)
|
||
|
item.set_text(0, subfolder.get_slice("/", subfolder.get_slice_count("/") - 1))
|
||
|
return item
|
||
|
|
||
|
if subfolder.get_slice_count("/") > 1:
|
||
|
# need more searches to find items with the base dir(s)
|
||
|
# var base_paths := subfolder.get_base_dir().split("/", false)
|
||
|
var base: TreeItem = find_or_create_subfolder_item(subfolder.get_base_dir())
|
||
|
var item := base.create_child()
|
||
|
item.set_meta("subfolder_name", subfolder)
|
||
|
item.set_text(0, subfolder.get_slice("/", subfolder.get_slice_count("/") - 1))
|
||
|
return item
|
||
|
|
||
|
# i dont know at this point
|
||
|
print("cant find item for subfolder, ", subfolder)
|
||
|
return null
|