Browse Source

adding ability to undo create file/folder action

vineethvk11 1 năm trước cách đây
mục cha
commit
a18ec1efdb
5 tập tin đã thay đổi với 58 bổ sung1 xóa
  1. 10 0
      src/UI/UIDesktop.js
  2. 10 0
      src/UI/UIWindow.js
  3. 1 0
      src/globals.js
  4. 29 1
      src/helpers.js
  5. 8 0
      src/initgui.js

+ 10 - 0
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
                     // -------------------------------------------
                     {

+ 10 - 0
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
                         // -------------------------------------------
                         {

+ 1 - 0
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 = [];

+ 29 - 1
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);
+}

+ 8 - 0
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){