2019-09-23 15:06:43 +02:00
tool
extends Control
2022-07-15 00:19:48 +02:00
enum FileMenuOptions {
FILE_MENU_OPTION_NEW = 0 ,
FILE_MENU_OPTION_OPEN = 1 ,
FILE_MENU_OPTION_CLOSE = 2 ,
FILE_MENU_OPTION_SAVE = 3 ,
FILE_MENU_OPTION_SAVE_AS = 4 ,
FILE_MENU_OPTION_DELETE = 5 ,
FILE_MENU_OPTION_SEARCH = 6 ,
FILE_MENU_OPTION_REPLACE = 7 ,
} ;
var DIRECTORY : String = " res:// "
var EXCEPTIONS : String = " addons "
var EXTENSIONS : PoolStringArray = [
" *.txt ; Plain Text File " ,
" *.rtf ; Rich Text Format File " ,
" *.log ; Log File " ,
" *.md ; MD File " ,
" *.doc ; WordPad Document " ,
" *.doc ; Microsoft Word Document " ,
" *.docm ; Word Open XML Macro-Enabled Document " ,
" *.docx ; Microsoft Word Open XML Document " ,
" *.bbs ; Bulletin Board System Text " ,
" *.dat ; Data File " ,
" *.xml ; XML File " ,
" *.sql ; SQL database file " ,
" *.json ; JavaScript Object Notation File " ,
" *.html ; HyperText Markup Language " ,
" *.csv ; Comma-separated values " ,
" *.cfg ; Configuration File " ,
" *.ini ; Initialization File (same as .cfg Configuration File) " ,
" *.csv ; Comma-separated values File " ,
" *.res ; Resource File " ,
]
2022-07-14 23:56:03 +02:00
var file_btn : MenuButton = null
var preview_btn : MenuButton = null
var settings_btn : MenuButton = null
2019-09-23 15:06:43 +02:00
2022-07-14 23:56:03 +02:00
var file_btn_popup : PopupMenu = null
var preview_btn_popup : PopupMenu = null
var settings_btn_popup : PopupMenu = null
2019-09-23 15:06:43 +02:00
2022-07-14 23:56:03 +02:00
var editor_container : HSplitContainer = null
var file_container : VBoxContainer = null
var open_file_list : ItemList = null
var split_editor_container : VBoxContainer = null
var open_file_name : LineEdit = null
var wrap_btn : OptionButton = null
var map_btn : OptionButton = null
2019-10-08 00:25:16 +02:00
2022-07-14 23:56:03 +02:00
var file_list : FileDialog = null
2019-09-24 17:03:49 +02:00
2022-07-14 23:56:03 +02:00
var new_file_dialogue : AcceptDialog = null
var new_file_dialogue_name : LineEdit = null
2019-10-08 00:25:16 +02:00
2022-07-14 23:56:03 +02:00
var confirmation_close : ConfirmationDialog = null
var select_font_dialog : FileDialog = null
2021-02-14 18:06:56 +01:00
2020-03-17 19:18:28 +01:00
var LastOpenedFiles = preload ( " res://addons/file-editor/scripts/LastOpenedFiles.gd " ) . new ( )
2022-07-14 16:24:43 +02:00
var Preview = preload ( " res://addons/file-editor/scripts/Preview.gd " )
2022-07-14 20:01:51 +02:00
var VanillaEditor = preload ( " res://addons/file-editor/scripts/VanillaEditor.gd " )
2019-10-11 21:53:20 +02:00
2019-09-23 15:06:43 +02:00
var directories = [ ]
var files = [ ]
2019-10-08 00:25:16 +02:00
var current_file_index = - 1
var current_file_path = " "
var save_as = false
2020-06-05 02:40:07 +02:00
var current_editor : Control
2020-10-12 18:13:53 +02:00
var current_font : DynamicFont
2020-03-17 19:18:28 +01:00
2021-02-14 18:06:56 +01:00
var editing_file : bool = false
2022-07-14 23:56:03 +02:00
func _init ( ) :
set_anchors_and_margins_preset ( Control . PRESET_WIDE )
size_flags_vertical = SIZE_EXPAND_FILL
size_flags_horizontal = SIZE_EXPAND_FILL
2022-07-14 10:23:53 +02:00
2022-07-14 23:56:03 +02:00
var vbc : VBoxContainer = VBoxContainer . new ( )
add_child ( vbc )
vbc . set_anchors_and_margins_preset ( Control . PRESET_WIDE )
var tob_bar : HBoxContainer = HBoxContainer . new ( )
vbc . add_child ( tob_bar )
file_btn = MenuButton . new ( )
tob_bar . add_child ( file_btn )
file_btn . text = " File "
file_btn_popup = file_btn . get_popup ( )
var hotkey : InputEventKey = InputEventKey . new ( )
2022-07-14 10:23:53 +02:00
hotkey . scancode = KEY_N
hotkey . control = true
2022-07-15 00:19:48 +02:00
file_btn_popup . add_item ( " New File " , FileMenuOptions . FILE_MENU_OPTION_NEW , hotkey . get_scancode_with_modifiers ( ) )
2022-07-14 23:56:03 +02:00
2022-07-14 10:23:53 +02:00
hotkey = InputEventKey . new ( )
hotkey . scancode = KEY_O
hotkey . control = true
2022-07-15 00:19:48 +02:00
file_btn_popup . add_item ( " Open File " , FileMenuOptions . FILE_MENU_OPTION_OPEN , hotkey . get_scancode_with_modifiers ( ) )
2022-07-14 23:56:03 +02:00
2022-07-14 10:23:53 +02:00
hotkey = InputEventKey . new ( )
2022-07-14 23:56:03 +02:00
hotkey . scancode = KEY_C
2022-07-14 10:23:53 +02:00
hotkey . control = true
2022-07-14 23:56:03 +02:00
hotkey . alt = true
2022-07-15 00:19:48 +02:00
file_btn_popup . add_item ( " Close File " , FileMenuOptions . FILE_MENU_OPTION_CLOSE , hotkey . get_scancode_with_modifiers ( ) )
2022-07-14 23:56:03 +02:00
file_btn_popup . add_separator ( )
2022-07-14 10:23:53 +02:00
hotkey = InputEventKey . new ( )
hotkey . scancode = KEY_S
hotkey . control = true
2022-07-15 00:19:48 +02:00
file_btn_popup . add_item ( " Save File " , FileMenuOptions . FILE_MENU_OPTION_SAVE , hotkey . get_scancode_with_modifiers ( ) )
2022-07-14 23:56:03 +02:00
2022-07-14 10:23:53 +02:00
hotkey = InputEventKey . new ( )
2022-07-14 23:56:03 +02:00
hotkey . scancode = KEY_S
2022-07-14 10:23:53 +02:00
hotkey . control = true
hotkey . alt = true
2022-07-15 00:19:48 +02:00
file_btn_popup . add_item ( " Save File as... " , FileMenuOptions . FILE_MENU_OPTION_SAVE_AS , hotkey . get_scancode_with_modifiers ( ) )
2022-07-14 23:56:03 +02:00
hotkey = InputEventKey . new ( )
hotkey . scancode = KEY_D
hotkey . control = true
2022-07-15 00:19:48 +02:00
file_btn_popup . add_item ( " Delete File " , FileMenuOptions . FILE_MENU_OPTION_DELETE , hotkey . get_scancode_with_modifiers ( ) )
2022-07-14 23:56:03 +02:00
file_btn_popup . add_separator ( )
2022-07-14 10:23:53 +02:00
hotkey = InputEventKey . new ( )
hotkey . scancode = KEY_F
hotkey . control = true
2022-07-15 00:19:48 +02:00
file_btn_popup . add_item ( " Search in file... " , FileMenuOptions . FILE_MENU_OPTION_SEARCH , hotkey . get_scancode_with_modifiers ( ) )
2022-07-14 23:56:03 +02:00
2022-07-14 10:23:53 +02:00
hotkey = InputEventKey . new ( )
hotkey . scancode = KEY_R
hotkey . control = true
2022-07-15 00:19:48 +02:00
file_btn_popup . add_item ( " Replace occurencies " , FileMenuOptions . FILE_MENU_OPTION_REPLACE , hotkey . get_scancode_with_modifiers ( ) )
2022-07-14 23:56:03 +02:00
#Preview
preview_btn = MenuButton . new ( )
tob_bar . add_child ( preview_btn )
preview_btn . text = " Preview "
preview_btn_popup = preview_btn . get_popup ( )
preview_btn_popup . add_item ( " BBCode Preview " )
preview_btn_popup . add_item ( " Markdown Preview " )
preview_btn_popup . add_item ( " HTML Preview " )
preview_btn_popup . add_item ( " CSV Preview " )
#Settings
settings_btn = MenuButton . new ( )
tob_bar . add_child ( settings_btn )
settings_btn . text = " Settings "
settings_btn_popup = settings_btn . get_popup ( )
settings_btn_popup . add_item ( " Change Font " )
#SplitContainer
editor_container = HSplitContainer . new ( )
vbc . add_child ( editor_container )
editor_container . split_offset = 150
editor_container . size_flags_horizontal = SIZE_EXPAND_FILL
editor_container . size_flags_vertical = SIZE_EXPAND_FILL
#Files
file_container = VBoxContainer . new ( )
editor_container . add_child ( file_container )
open_file_list = ItemList . new ( )
file_container . add_child ( open_file_list )
open_file_list . allow_reselect = true
open_file_list . size_flags_vertical = SIZE_EXPAND_FILL
file_container . add_child ( HSeparator . new ( ) )
#Editor
split_editor_container = VBoxContainer . new ( )
editor_container . add_child ( split_editor_container )
var editor_top_bar : HBoxContainer = HBoxContainer . new ( )
split_editor_container . add_child ( editor_top_bar )
var edtopbar_label : Label = Label . new ( )
editor_top_bar . add_child ( edtopbar_label )
edtopbar_label . text = " Editing file: "
open_file_name = LineEdit . new ( )
editor_top_bar . add_child ( open_file_name )
open_file_name . editable = false
open_file_name . mouse_filter = Control . MOUSE_FILTER_PASS
open_file_name . size_flags_horizontal = SIZE_EXPAND_FILL
wrap_btn = OptionButton . new ( )
editor_top_bar . add_child ( wrap_btn )
wrap_btn . add_item ( " No Wrap " )
wrap_btn . add_item ( " Soft Wrap " )
map_btn = OptionButton . new ( )
editor_top_bar . add_child ( map_btn )
map_btn . add_item ( " Hide Map " )
map_btn . add_item ( " Show Map " )
map_btn . selected = 1
#dialogs
file_list = FileDialog . new ( )
add_child ( file_list )
file_list . show_hidden_files = true
file_list . dialog_hide_on_ok = true
file_list . window_title = " Save file "
file_list . popup_exclusive = true
2022-07-15 00:29:39 +02:00
file_list . set_anchors_and_margins_preset ( Control . PRESET_WIDE )
file_list . margin_left = 222
file_list . margin_top = 132
file_list . margin_right = - 221
file_list . margin_bottom = - 131
file_list . rect_min_size = Vector2 ( 200 , 70 )
2022-07-14 23:56:03 +02:00
new_file_dialogue = AcceptDialog . new ( )
add_child ( new_file_dialogue )
new_file_dialogue . window_title = " Create new File "
var nfd_vbc : VBoxContainer = VBoxContainer . new ( )
new_file_dialogue . add_child ( nfd_vbc )
var nfd_name : Label = Label . new ( )
nfd_vbc . add_child ( nfd_name )
nfd_name . text = " Insert file name (no extension needed) "
nfd_name . align = Label . ALIGN_CENTER
nfd_name . valign = Label . VALIGN_CENTER
nfd_name . size_flags_vertical = SIZE_EXPAND_FILL
new_file_dialogue_name = LineEdit . new ( )
nfd_vbc . add_child ( new_file_dialogue_name )
new_file_dialogue_name . clear_button_enabled = true
new_file_dialogue_name . text = " example "
new_file_dialogue_name . rect_min_size = Vector2 ( 200 , 0 )
new_file_dialogue_name . size_flags_horizontal = SIZE_EXPAND | SIZE_SHRINK_CENTER
new_file_dialogue_name . size_flags_vertical = SIZE_EXPAND_FILL
confirmation_close = ConfirmationDialog . new ( )
add_child ( confirmation_close )
confirmation_close . dialog_text = " There are some unsaved changes. \n Press \" OK \" if you want to close this tab anyway, or \" cancel \" if you want to keep on editing your file. "
confirmation_close . window_title = " Unsaved changes "
confirmation_close . set_anchors_and_margins_preset ( Control . PRESET_CENTER )
select_font_dialog = FileDialog . new ( )
add_child ( select_font_dialog )
select_font_dialog . mode = FileDialog . MODE_OPEN_FILE
select_font_dialog . access = FileDialog . ACCESS_FILESYSTEM
select_font_dialog . show_hidden_files = true
select_font_dialog . window_title = " Open a File "
select_font_dialog . resizable = true
2022-07-15 00:08:19 +02:00
select_font_dialog . set_anchors_and_margins_preset ( Control . PRESET_WIDE )
select_font_dialog . margin_left = 222
select_font_dialog . margin_top = 132
select_font_dialog . margin_right = - 221
select_font_dialog . margin_bottom = - 131
select_font_dialog . rect_min_size = Vector2 ( 200 , 70 )
2022-07-14 23:56:03 +02:00
var farr : PoolStringArray = PoolStringArray ( )
farr . push_back ( " *.TTF " )
farr . push_back ( " *.ttf " )
select_font_dialog . filters = farr
2022-07-14 10:23:53 +02:00
2022-07-14 23:56:03 +02:00
func _ready ( ) :
if not Engine . is_editor_hint ( ) :
return
clean_editor ( )
connect_signals ( )
var opened_files : Array = LastOpenedFiles . load_opened_files ( )
for opened_file in opened_files :
open_file ( opened_file [ 1 ] , opened_file [ 2 ] )
file_list . set_filters ( EXTENSIONS )
2019-10-08 00:25:16 +02:00
func connect_signals ( ) :
2022-07-14 23:56:03 +02:00
file_list . connect ( " confirmed " , self , " update_list " )
file_btn_popup . connect ( " id_pressed " , self , " _on_file_btn_pressed " )
preview_btn_popup . connect ( " id_pressed " , self , " _on_preview_btn_pressed " )
settings_btn_popup . connect ( " id_pressed " , self , " _on_settings_btn_pressed " )
2022-07-14 10:23:53 +02:00
2022-07-14 23:56:03 +02:00
open_file_list . connect ( " item_selected " , self , " _on_fileitem_pressed " )
wrap_btn . connect ( " item_selected " , self , " on_wrap_button " )
map_btn . connect ( " item_selected " , self , " on_minimap_button " )
2022-07-14 10:23:53 +02:00
2022-07-14 23:56:03 +02:00
select_font_dialog . connect ( " file_selected " , self , " _on_font_selected " )
2019-10-08 00:25:16 +02:00
func create_selected_file ( ) :
2022-07-14 10:23:53 +02:00
update_list ( )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
file_list . mode = FileDialog . MODE_SAVE_FILE
file_list . set_title ( " Create a new File " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
if file_list . is_connected ( " file_selected " , self , " delete_file " ) :
file_list . disconnect ( " file_selected " , self , " delete_file " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
if file_list . is_connected ( " file_selected " , self , " open_file " ) :
file_list . disconnect ( " file_selected " , self , " open_file " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
if not file_list . is_connected ( " file_selected " , self , " create_new_file " ) :
file_list . connect ( " file_selected " , self , " create_new_file " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
open_file_list ( )
2019-10-08 00:25:16 +02:00
func open_selected_file ( ) :
2022-07-14 10:23:53 +02:00
update_list ( )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
file_list . mode = FileDialog . MODE_OPEN_FILE
file_list . set_title ( " Select a File you want to edit " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
if file_list . is_connected ( " file_selected " , self , " delete_file " ) :
file_list . disconnect ( " file_selected " , self , " delete_file " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
if file_list . is_connected ( " file_selected " , self , " create_new_file " ) :
file_list . disconnect ( " file_selected " , self , " create_new_file " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
if not file_list . is_connected ( " file_selected " , self , " open_file " ) :
file_list . connect ( " file_selected " , self , " open_file " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
open_file_list ( )
2019-10-08 00:25:16 +02:00
func delete_selected_file ( ) :
2022-07-14 10:23:53 +02:00
update_list ( )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
file_list . mode = FileDialog . MODE_OPEN_FILES
file_list . set_title ( " Select one or more Files you want to delete " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
if file_list . is_connected ( " file_selected " , self , " open_file " ) :
file_list . disconnect ( " file_selected " , self , " open_file " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
if file_list . is_connected ( " file_selected " , self , " create_new_file " ) :
file_list . disconnect ( " file_selected " , self , " create_new_file " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
if not file_list . is_connected ( " files_selected " , self , " delete_file " ) :
file_list . connect ( " files_selected " , self , " delete_file " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
open_file_list ( )
2019-10-08 00:25:16 +02:00
func save_current_file_as ( ) :
2022-07-14 10:23:53 +02:00
update_list ( )
2022-07-14 23:56:03 +02:00
file_list . mode = FileDialog . MODE_SAVE_FILE
file_list . set_title ( " Save this File as... " )
2022-07-14 20:01:51 +02:00
2022-07-14 23:56:03 +02:00
if file_list . is_connected ( " file_selected " , self , " delete_file " ) :
file_list . disconnect ( " file_selected " , self , " delete_file " )
2022-07-14 20:01:51 +02:00
2022-07-14 23:56:03 +02:00
if file_list . is_connected ( " file_selected " , self , " open_file " ) :
file_list . disconnect ( " file_selected " , self , " open_file " )
2022-07-14 20:01:51 +02:00
2022-07-14 23:56:03 +02:00
if not file_list . is_connected ( " file_selected " , self , " create_new_file " ) :
file_list . connect ( " file_selected " , self , " create_new_file " )
2022-07-14 20:01:51 +02:00
2022-07-14 23:56:03 +02:00
open_file_list ( )
2019-10-08 00:25:16 +02:00
2022-07-14 23:56:03 +02:00
func _on_file_btn_pressed ( index : int ) :
2022-07-14 10:23:53 +02:00
match index :
2022-07-15 00:19:48 +02:00
FileMenuOptions . FILE_MENU_OPTION_NEW :
2022-07-14 10:23:53 +02:00
create_selected_file ( )
2022-07-15 00:19:48 +02:00
FileMenuOptions . FILE_MENU_OPTION_OPEN :
2022-07-14 10:23:53 +02:00
open_selected_file ( )
2022-07-15 00:19:48 +02:00
FileMenuOptions . FILE_MENU_OPTION_CLOSE :
2022-07-14 10:23:53 +02:00
if current_file_index != - 1 and current_file_path != " " :
close_file ( current_file_index )
2022-07-15 00:19:48 +02:00
FileMenuOptions . FILE_MENU_OPTION_SAVE :
2022-07-14 10:23:53 +02:00
if current_file_index != - 1 and current_file_path != " " :
save_as = false
2022-07-14 20:39:11 +02:00
2022-07-14 10:23:53 +02:00
save_file ( current_file_path )
2022-07-15 00:19:48 +02:00
FileMenuOptions . FILE_MENU_OPTION_SAVE_AS :
2022-07-14 10:23:53 +02:00
if current_file_index != - 1 and current_file_path != " " :
save_as = true
save_file ( current_file_path )
save_current_file_as ( )
2022-07-15 00:19:48 +02:00
FileMenuOptions . FILE_MENU_OPTION_DELETE :
2022-07-14 10:23:53 +02:00
delete_selected_file ( )
2022-07-15 00:19:48 +02:00
FileMenuOptions . FILE_MENU_OPTION_SEARCH :
2022-07-15 00:39:28 +02:00
current_editor . open_search_box ( )
2022-07-15 00:19:48 +02:00
FileMenuOptions . FILE_MENU_OPTION_REPLACE :
2022-07-15 00:39:28 +02:00
current_editor . open_replace_box ( )
2019-10-08 00:25:16 +02:00
2022-07-14 23:56:03 +02:00
func _on_preview_btn_pressed ( id : int ) :
2022-07-14 10:23:53 +02:00
if id == 0 :
bbcode_preview ( )
elif id == 1 :
markdown_preview ( )
elif id == 2 :
html_preview ( )
elif id == 3 :
csv_preview ( )
2019-10-08 00:25:16 +02:00
2022-07-14 23:56:03 +02:00
func _on_settings_btn_pressed ( index : int ) :
2022-07-14 10:23:53 +02:00
match index :
0 :
2022-07-14 23:56:03 +02:00
select_font_dialog . popup ( )
2020-10-12 18:13:53 +02:00
func _on_font_selected ( font_path : String ) :
2022-07-14 10:23:53 +02:00
current_editor . set_font ( font_path )
LastOpenedFiles . store_editor_fonts ( current_file_path . get_file ( ) , font_path )
2019-10-08 00:25:16 +02:00
func _on_fileitem_pressed ( index : int ) :
2022-07-14 10:23:53 +02:00
current_file_index = index
2022-07-14 23:56:03 +02:00
var selected_item_metadata = open_file_list . get_item_metadata ( current_file_index )
2022-07-14 10:23:53 +02:00
var extension = selected_item_metadata [ 0 ] . current_path . get_file ( ) . get_extension ( )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
if open_file_list . get_item_text ( current_file_index ) . begins_with ( " (*) " ) :
2022-07-14 10:23:53 +02:00
editing_file = true
else :
editing_file = false
2022-07-14 11:08:58 +02:00
2022-07-14 10:23:53 +02:00
current_file_path = selected_item_metadata [ 0 ] . current_path
2022-07-14 11:08:58 +02:00
2022-07-14 10:23:53 +02:00
if current_editor . visible or current_editor == null :
2022-07-14 20:39:11 +02:00
if current_editor != null :
current_editor . hide ( )
2022-07-14 10:23:53 +02:00
current_editor = selected_item_metadata [ 0 ]
current_editor . show ( )
2022-07-14 23:56:03 +02:00
open_file_name . set_text ( current_editor . current_path )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
if wrap_btn . get_selected_id ( ) == 1 :
2022-07-14 10:23:53 +02:00
current_editor . set_wrap_enabled ( true )
else :
current_editor . set_wrap_enabled ( false )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
if map_btn . get_selected_id ( ) == 1 :
2022-07-14 10:23:53 +02:00
current_editor . draw_minimap ( true )
else :
current_editor . draw_minimap ( false )
2020-10-12 18:13:53 +02:00
func open_file ( path : String , font : String = " null " ) :
2022-07-14 10:23:53 +02:00
if current_file_path != path :
current_file_path = path
var vanilla_editor = open_in_vanillaeditor ( path )
2022-07-14 20:39:11 +02:00
2022-07-14 10:23:53 +02:00
if font != " null " and vanilla_editor . get ( " custom_fonts/font " ) != null :
vanilla_editor . set_font ( font )
2022-07-14 20:39:11 +02:00
generate_file_item ( path , vanilla_editor )
2022-07-14 10:23:53 +02:00
2022-07-14 23:56:03 +02:00
LastOpenedFiles . store_opened_files ( open_file_list )
2022-07-14 11:08:58 +02:00
2022-07-14 10:23:53 +02:00
current_editor . show ( )
2019-10-08 00:25:16 +02:00
2022-07-14 20:39:11 +02:00
func generate_file_item ( path : String , veditor : Control ) :
2022-07-14 23:56:03 +02:00
open_file_name . set_text ( path )
2022-07-15 00:41:49 +02:00
open_file_list . add_item ( path . get_file ( ) , null , true )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
current_file_index = open_file_list . get_item_count ( ) - 1
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
open_file_list . set_item_metadata ( current_file_index , [ veditor ] )
open_file_list . select ( open_file_list . get_item_count ( ) - 1 )
2019-10-18 01:22:11 +02:00
func open_in_vanillaeditor ( path : String ) - > Control :
2022-07-14 20:01:51 +02:00
var editor = VanillaEditor . new ( )
2022-07-14 23:56:03 +02:00
split_editor_container . add_child ( editor , true )
2022-07-14 10:23:53 +02:00
if current_editor and current_editor != editor :
editor . show ( )
current_editor . hide ( )
2022-07-14 11:08:58 +02:00
2022-07-14 10:23:53 +02:00
current_editor = editor
editor . connect ( " text_changed " , self , " _on_vanillaeditor_text_changed " )
var current_file : File = File . new ( )
current_file . open ( path , File . READ )
var current_content = " "
current_content = current_file . get_as_text ( )
var last_modified = OS . get_datetime_from_unix_time ( current_file . get_modified_time ( path ) )
current_file . close ( )
editor . new_file_open ( current_content , last_modified , current_file_path )
update_list ( )
2022-07-14 23:56:03 +02:00
if wrap_btn . get_selected_id ( ) == 1 :
2022-07-14 10:23:53 +02:00
current_editor . set_wrap_enabled ( true )
return editor
2019-10-08 00:25:16 +02:00
2019-10-11 21:53:20 +02:00
func close_file ( index ) :
2022-07-14 10:23:53 +02:00
if editing_file :
2022-07-14 23:56:03 +02:00
confirmation_close . popup ( )
2022-07-14 10:23:53 +02:00
else :
confirm_close ( index )
2021-02-14 18:06:56 +01:00
func confirm_close ( index ) :
2022-07-14 23:56:03 +02:00
LastOpenedFiles . remove_opened_file ( index , open_file_list )
open_file_list . remove_item ( index )
open_file_name . clear ( )
2022-07-14 10:23:53 +02:00
current_editor . queue_free ( )
2022-07-14 11:08:58 +02:00
2022-07-14 10:23:53 +02:00
if index > 0 :
2022-07-14 23:56:03 +02:00
open_file_list . select ( index - 1 )
2022-07-14 10:23:53 +02:00
_on_fileitem_pressed ( index - 1 )
2019-10-08 00:25:16 +02:00
func _on_update_file ( ) :
2022-07-14 10:23:53 +02:00
var current_file : File = File . new ( )
current_file . open ( current_file_path , File . READ )
var current_content = current_file . get_as_text ( )
var last_modified = OS . get_datetime_from_unix_time ( current_file . get_modified_time ( current_file_path ) )
current_file . close ( )
current_editor . new_file_open ( current_content , last_modified , current_file_path )
2019-10-08 00:25:16 +02:00
func delete_file ( files_selected : PoolStringArray ) :
2022-07-14 10:23:53 +02:00
var dir = Directory . new ( )
for file in files_selected :
dir . remove ( file )
update_list ( )
2019-09-23 15:06:43 +02:00
2022-07-14 23:56:03 +02:00
func open_new_file_dialogue ( ) :
new_file_dialogue . popup ( )
new_file_dialogue . set_position ( OS . get_screen_size ( ) / 2 - new_file_dialogue . get_size ( ) / 2 )
2019-09-23 15:06:43 +02:00
2022-07-14 23:56:03 +02:00
func open_file_list ( ) :
2022-07-14 10:23:53 +02:00
update_list ( )
2022-07-14 23:56:03 +02:00
file_list . popup ( )
file_list . set_position ( OS . get_screen_size ( ) / 2 - file_list . get_size ( ) / 2 )
2019-09-23 15:06:43 +02:00
2019-10-08 00:25:16 +02:00
func create_new_file ( given_path : String ) :
2022-07-14 10:23:53 +02:00
var current_file = File . new ( )
current_file . open ( given_path , File . WRITE )
if save_as :
2022-07-14 20:01:51 +02:00
current_file . store_line ( current_editor . text_editor . get_text ( ) )
2022-07-14 10:23:53 +02:00
current_file . close ( )
open_file ( given_path )
2019-09-23 15:06:43 +02:00
2019-10-08 00:25:16 +02:00
func save_file ( current_path : String ) :
2022-07-14 10:23:53 +02:00
print ( " Saving file: " , current_path )
var current_file = File . new ( )
current_file . open ( current_path , File . WRITE )
var current_content = " "
2022-07-14 20:01:51 +02:00
var lines = current_editor . text_editor . get_line_count ( )
2022-07-14 11:08:58 +02:00
2022-07-14 10:23:53 +02:00
for line in range ( 0 , lines ) :
2022-07-14 20:01:51 +02:00
if current_editor . text_editor . get_line ( line ) == " " :
2022-07-14 10:23:53 +02:00
continue
2022-07-14 11:08:58 +02:00
2022-07-14 20:01:51 +02:00
current_content = current_editor . text_editor . get_text ( )
current_file . store_line ( current_editor . text_editor . get_line ( line ) )
2022-07-14 11:08:58 +02:00
2022-07-14 10:23:53 +02:00
current_file . close ( )
current_file_path = current_path
var last_modified = OS . get_datetime_from_unix_time ( current_file . get_modified_time ( current_file_path ) )
current_editor . update_lastmodified ( last_modified , " save " )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
open_file_list . set_item_metadata ( current_file_index , [ current_editor ] )
2022-07-14 10:23:53 +02:00
2022-07-14 23:56:03 +02:00
if open_file_list . get_item_text ( current_file_index ) . begins_with ( " (*) " ) :
open_file_list . set_item_text ( current_file_index , open_file_list . get_item_text ( current_file_index ) . lstrip ( " (*) " ) )
2022-07-14 10:23:53 +02:00
editing_file = false
update_list ( )
2019-09-23 15:06:43 +02:00
2019-10-18 01:22:11 +02:00
func clean_editor ( ) - > void :
2022-07-14 10:23:53 +02:00
for vanillaeditor in get_tree ( ) . get_nodes_in_group ( " vanilla_editor " ) :
vanillaeditor . queue_free ( )
2022-07-14 11:08:58 +02:00
2022-07-14 23:56:03 +02:00
open_file_name . clear ( )
open_file_list . clear ( )
2019-10-18 01:22:11 +02:00
2019-10-08 00:25:16 +02:00
func csv_preview ( ) :
2022-07-14 16:24:43 +02:00
var preview = Preview . new ( )
2022-07-14 10:23:53 +02:00
get_parent ( ) . get_parent ( ) . get_parent ( ) . add_child ( preview )
preview . popup ( )
preview . window_title += " ( " + current_file_path . get_file ( ) + " ) "
2022-07-14 20:01:51 +02:00
var lines = current_editor . text_editor . get_line_count ( )
2022-07-14 10:23:53 +02:00
var rows = [ ]
2022-07-14 11:08:58 +02:00
2022-07-14 10:23:53 +02:00
for i in range ( 0 , lines - 1 ) :
2022-07-14 20:01:51 +02:00
rows . append ( current_editor . text_editor . get_line ( i ) . rsplit ( " , " , false ) )
2022-07-14 11:08:58 +02:00
2022-07-14 10:23:53 +02:00
preview . print_csv ( rows )
2019-10-08 00:25:16 +02:00
func bbcode_preview ( ) :
2022-07-14 16:24:43 +02:00
var preview = Preview . new ( )
2022-07-14 10:23:53 +02:00
get_parent ( ) . get_parent ( ) . get_parent ( ) . add_child ( preview )
preview . popup ( )
preview . window_title += " ( " + current_file_path . get_file ( ) + " ) "
2022-07-14 20:01:51 +02:00
preview . print_bb ( current_editor . text_editor . get_text ( ) )
2019-10-08 00:25:16 +02:00
func markdown_preview ( ) :
2022-07-14 16:24:43 +02:00
var preview = Preview . new ( )
2022-07-14 10:23:53 +02:00
get_parent ( ) . get_parent ( ) . get_parent ( ) . add_child ( preview )
preview . popup ( )
preview . window_title += " ( " + current_file_path . get_file ( ) + " ) "
2022-07-14 20:01:51 +02:00
preview . print_markdown ( current_editor . text_editor . get_text ( ) )
2019-10-08 00:25:16 +02:00
func html_preview ( ) :
2022-07-14 16:24:43 +02:00
var preview = Preview . new ( )
2022-07-14 10:23:53 +02:00
get_parent ( ) . get_parent ( ) . get_parent ( ) . add_child ( preview )
preview . popup ( )
preview . window_title += " ( " + current_file_path . get_file ( ) + " ) "
2022-07-14 20:01:51 +02:00
preview . print_html ( current_editor . text_editor . get_text ( ) )
2019-10-08 00:25:16 +02:00
2019-10-11 21:53:20 +02:00
func _on_vanillaeditor_text_changed ( ) :
2022-07-14 23:56:03 +02:00
if not open_file_list . get_item_text ( current_file_index ) . begins_with ( " (*) " ) :
open_file_list . set_item_text ( current_file_index , " (*) " + open_file_list . get_item_text ( current_file_index ) )
2022-07-14 10:23:53 +02:00
editing_file = true
2019-10-11 21:53:20 +02:00
2019-09-24 00:42:50 +02:00
func update_list ( ) :
2022-07-14 23:56:03 +02:00
file_list . invalidate ( )
2019-10-18 01:22:11 +02:00
func on_wrap_button ( index : int ) :
2022-07-14 10:23:53 +02:00
match index :
0 :
current_editor . set_wrap_enabled ( false )
1 :
current_editor . set_wrap_enabled ( true )
2019-10-18 01:22:11 +02:00
func on_minimap_button ( index : int ) :
2022-07-14 10:23:53 +02:00
match index :
0 :
current_editor . draw_minimap ( false )
1 :
current_editor . draw_minimap ( true )
2020-03-17 18:11:08 +01:00
func check_file_preview ( file : String ) :
2022-07-14 10:23:53 +02:00
# check whether the opened file has a corresponding preview session for its extension
pass
2021-02-14 18:06:56 +01:00
func _on_ConfirmationDialog_confirmed ( ) :
2022-07-14 10:23:53 +02:00
confirm_close ( current_file_index )