From 1ae905bd7a31bc66d23fc3551d5317aa92d016ae Mon Sep 17 00:00:00 2001 From: vineethvk11 Date: Thu, 14 Mar 2024 01:45:28 +0530 Subject: [PATCH] Adding ability to undo move and delete actions --- src/helpers.js | 172 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 169 insertions(+), 3 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 78aadcb8..ac5b015f 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -2305,7 +2305,7 @@ window.new_context_menu_item = function(dirname, append_to_element){ * @param {string} dest_path - The destination path to move the items to * @returns {Promise} */ -window.move_items = async function(el_items, dest_path){ +window.move_items = async function(el_items, dest_path, is_undo = false){ let move_op_id = operation_id++; operation_cancelled[move_op_id] = false; @@ -2339,6 +2339,9 @@ window.move_items = async function(el_items, dest_path){ progwin = await UIWindowMoveProgress({operation_id: move_op_id}); }, 2000); + // storing moved items for undo ability + const moved_items = [] + // Go through each item and try to move it for(let i=0; i { $(progwin).close(); @@ -3506,6 +3524,12 @@ window.undo_last_action = async()=>{ } else if(last_action.operation === 'copy') { const files = last_action.data; undo_copy(files); + } else if(last_action.operation === 'move') { + const items = last_action.data; + undo_move(items); + } else if(last_action.operation === 'delete') { + const items = last_action.data; + undo_delete(items); } } } @@ -3524,4 +3548,146 @@ window.undo_copy = async(files)=>{ for (const file of files) { await window.delete_item_with_path(file); } +} + +window.undo_move = async(items)=>{ + for (const item of items) { + const el = await get_html_from_options(item.options); + console.log(item.original_path) + move_items([el], path.dirname(item.original_path), true); + } +} + +window.undo_delete = async(items)=>{ + for (const item of items) { + const el = await get_html_from_options(item.options); + let metadata = $(el).attr('data-metadata') === '' ? {} : JSON.parse($(el).attr('data-metadata')) + move_items([el], path.dirname(metadata.original_path), true); + } +} + + +window.get_html_element_from_options = async function(options){ + const item_id = global_element_id++; + + options.disabled = options.disabled ?? false; + options.visible = options.visible ?? 'visible'; // one of 'visible', 'revealed', 'hidden' + options.is_dir = options.is_dir ?? false; + options.is_selected = options.is_selected ?? false; + options.is_shared = options.is_shared ?? false; + options.is_shortcut = options.is_shortcut ?? 0; + options.is_trash = options.is_trash ?? false; + options.metadata = options.metadata ?? ''; + options.multiselectable = options.multiselectable ?? true; + options.shortcut_to = options.shortcut_to ?? ''; + options.shortcut_to_path = options.shortcut_to_path ?? ''; + options.immutable = (options.immutable === false || options.immutable === 0 || options.immutable === undefined ? 0 : 1); + options.sort_container_after_append = (options.sort_container_after_append !== undefined ? options.sort_container_after_append : false); + const is_shared_with_me = (options.path !== '/'+window.user.username && !options.path.startsWith('/'+window.user.username+'/')); + + let website_url = determine_website_url(options.path); + + // do a quick check to see if the target parent has any file type restrictions + const appendto_allowed_file_types = $(options.appendTo).attr('data-allowed_file_types') + if(!window.check_fsentry_against_allowed_file_types_string({is_dir: options.is_dir, name:options.name, type:options.type}, appendto_allowed_file_types)) + options.disabled = true; + + // -------------------------------------------------------- + // HTML for Item + // -------------------------------------------------------- + let h = ''; + h += `
`; + + // spinner + h += `
`; + h += `
`; + // modified + h += `
`; + h += `${options.modified === 0 ? '-' : timeago.format(options.modified*1000)}`; + h += `
`; + // size + h += `
`; + h += `${options.size ? byte_format(options.size) : '-'}`; + h += `
`; + // type + h += `
`; + if(options.is_dir) + h += `Folder`; + else + h += `${options.type ? html_encode(options.type) : '-'}`; + h += `
`; + + + // icon + h += `
`; + h += ``; + h += `
`; + // badges + h += `
`; + // website badge + h += ``; + // link badge + h += ``; + + // shared badge + h += ``; + // owner-shared badge + h += ``; + // shortcut badge + h += ``; + + h += `
`; + + // name + h += `${html_encode(truncate_filename(options.name, TRUNCATE_LENGTH)).replaceAll(' ', ' ')}` + // name editor + h += `` + h += `
`; + + return h; } \ No newline at end of file