From a18ec1efdbc3e0c31748e118351d58e3a2ac2439 Mon Sep 17 00:00:00 2001 From: vineethvk11 Date: Sun, 10 Mar 2024 19:48:17 +0530 Subject: [PATCH] adding ability to undo create file/folder action --- src/UI/UIDesktop.js | 10 ++++++++++ src/UI/UIWindow.js | 10 ++++++++++ src/globals.js | 1 + src/helpers.js | 30 +++++++++++++++++++++++++++++- src/initgui.js | 8 ++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/UI/UIDesktop.js b/src/UI/UIDesktop.js index 030546e5..2926f433 100644 --- a/src/UI/UIDesktop.js +++ b/src/UI/UIDesktop.js @@ -726,6 +726,16 @@ async function UIDesktop(options){ } }, // ------------------------------------------- + // Undo + // ------------------------------------------- + { + html: "Undo", + disabled: actions_history.length > 0 ? false : true, + onClick: function(){ + undo_last_action(); + } + }, + // ------------------------------------------- // Upload Here // ------------------------------------------- { diff --git a/src/UI/UIWindow.js b/src/UI/UIWindow.js index 8d5dc509..3a90926f 100644 --- a/src/UI/UIWindow.js +++ b/src/UI/UIWindow.js @@ -1917,6 +1917,16 @@ async function UIWindow(options) { } }, // ------------------------------------------- + // Undo + // ------------------------------------------- + { + html: "Undo", + disabled: actions_history.length > 0 ? false : true, + onClick: function(){ + undo_last_action(); + } + }, + // ------------------------------------------- // Upload Here // ------------------------------------------- { diff --git a/src/globals.js b/src/globals.js index d2d336e0..f5ff998a 100644 --- a/src/globals.js +++ b/src/globals.js @@ -19,6 +19,7 @@ window.clipboard_op = ''; window.clipboard = []; +window.actions_history = []; window.window_nav_history = {}; window.window_nav_history_current_position = {}; window.progress_tracker = []; diff --git a/src/helpers.js b/src/helpers.js index 499b6d82..6d561bbb 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1434,9 +1434,15 @@ window.create_folder = async(basedir, appendto_element)=>{ overwrite: false, success: function (data){ const el_created_dir = $(appendto_element).find('.item[data-path="'+html_encode(dirname)+'/'+html_encode(data.name)+'"]'); - if(el_created_dir.length > 0) + if(el_created_dir.length > 0){ activate_item_name_editor(el_created_dir); + // Add action to actions_history for undo ability + actions_history.push({ + operation: 'create_folder', + data: el_created_dir + }); + } clearTimeout(progwin_timeout); // done @@ -1472,6 +1478,12 @@ window.create_file = async(options)=>{ const created_file = $(appendto_element).find('.item[data-path="'+html_encode(dirname)+'/'+html_encode(data.name)+'"]'); if(created_file.length > 0){ activate_item_name_editor(created_file); + + // Add action to actions_history for undo ability + actions_history.push({ + operation: 'create_file', + data: created_file + }); } } }); @@ -3313,3 +3325,19 @@ window.unzipItem = async function(itemPath) { }, Math.max(0, copy_progress_hide_delay - (Date.now() - start_ts))); }) } + +window.undo_last_action = async()=>{ + if (actions_history.length > 0) { + const last_action = actions_history.pop(); + + // Undo the create file action + if (last_action.operation === 'create_file' || last_action.operation === 'create_folder') { + const lastCreatedItem = last_action.data; + undo_create_file_or_folder(lastCreatedItem); + } + } +} + +window.undo_create_file_or_folder = async(item)=>{ + await window.delete_item(item); +} diff --git a/src/initgui.js b/src/initgui.js index 306e070f..097dd037 100644 --- a/src/initgui.js +++ b/src/initgui.js @@ -1677,6 +1677,14 @@ window.initgui = async function(){ } return false; } + //----------------------------------------------------------------------------- + // Undo + // ctrl/command + z, will undo last action + //----------------------------------------------------------------------------- + if((e.ctrlKey || e.metaKey) && e.which === 90){ + undo_last_action(); + return false; + } }); $(document).on('click', '.remove-permission-link', async function(e){