From 3dbe89f3dc317c321a5e7cb0ad0f07beb6445a83 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Mon, 12 May 2025 17:34:04 -0400 Subject: [PATCH] dev: migrate "Create Shortcut" to DRY multi/single --- src/gui/src/UI/UIItem.js | 6 ++- src/gui/src/UI/lib/TODO.md | 13 +++++ src/gui/src/UI/lib/ui_item.js | 89 ++++++++++++++--------------------- 3 files changed, 53 insertions(+), 55 deletions(-) create mode 100644 src/gui/src/UI/lib/TODO.md diff --git a/src/gui/src/UI/UIItem.js b/src/gui/src/UI/UIItem.js index 268c9f9e1..6fe6a5010 100644 --- a/src/gui/src/UI/UIItem.js +++ b/src/gui/src/UI/UIItem.js @@ -24,7 +24,7 @@ import path from "../lib/path.js" import truncate_filename from '../helpers/truncate_filename.js'; import launch_app from "../helpers/launch_app.js" import open_item from "../helpers/open_item.js" -import { add_multiple_select_menu_items, add_single_select_menu_items } from './lib/ui_item.js'; +import { add_common_select_menu_items, add_multiple_select_menu_items, add_single_select_menu_items } from './lib/ui_item.js'; function UIItem(options){ const matching_appendto_count = $(options.appendTo).length; @@ -753,6 +753,10 @@ function UIItem(options){ event.preventDefault(); let menu_items = []; const $selected_items = $(el_item).closest('.item-container').find('.item-selected').not(el_item).addBack(); + + add_common_select_menu_items(menu_items, { + $selected_items, + }); // Multiple items selected if($selected_items.length > 1){ diff --git a/src/gui/src/UI/lib/TODO.md b/src/gui/src/UI/lib/TODO.md new file mode 100644 index 000000000..b6aa17bee --- /dev/null +++ b/src/gui/src/UI/lib/TODO.md @@ -0,0 +1,13 @@ +### Extract Common Menu Items + +We can migrate these individually each time menu item is updated +or otherwise as needed. "Create Shortcut" is already migrated +to get the ball rolling. + +[x] Create Shortcut +[ ] Share With... +[ ] Download +[ ] Zip +[ ] Cut +[ ] Copy +[ ] Delete diff --git a/src/gui/src/UI/lib/ui_item.js b/src/gui/src/UI/lib/ui_item.js index f386fb80f..7296fac65 100644 --- a/src/gui/src/UI/lib/ui_item.js +++ b/src/gui/src/UI/lib/ui_item.js @@ -8,6 +8,41 @@ import path from "../../lib/path.js" import launch_app from "../../helpers/launch_app.js" import open_item from "../../helpers/open_item.js" +export const add_common_select_menu_items = (menu_items, { + $selected_items, + is_shared_with_me, +}) => { + const are_trashed = $selected_items.attr('data-path').startsWith(window.trash_path + '/'); + const plural = $selected_items.length > 1; + + if(!are_trashed && window.feature_flags.create_shortcut){ + menu_items.push({ + html: is_shared_with_me + ? i18n('create_desktop_shortcut' + (plural ? '_s' : '')) + : i18n('create_shortcut' + (plural ? '_s' : '')), + onClick: async function(){ + $selected_items.each(function() { + let base_dir = path.dirname($(this).attr('data-path')); + // Trash on Desktop is a special case + if($(this).attr('data-path') && $(this).closest('.item-container').attr('data-path') === window.desktop_path){ + base_dir = window.desktop_path; + } + if ( is_shared_with_me ) base_dir = window.desktop_path; + // create shortcut + window.create_shortcut( + path.basename($(this).attr('data-path')), + $(this).attr('data-is_dir') === '1', + base_dir, + $(this).closest('.item-container'), + $(this).attr('data-shortcut_to') === '' ? $(this).attr('data-uid') : $(this).attr('data-shortcut_to'), + $(this).attr('data-shortcut_to_path') === '' ? $(this).attr('data-path') : $(this).attr('data-shortcut_to_path'), + ); + }) + } + }); + } +}; + export const add_multiple_select_menu_items = (menu_items, { $selected_items, el_item, @@ -170,34 +205,6 @@ export const add_multiple_select_menu_items = (menu_items, { }); } // ------------------------------------------- - // Create Shortcut - // ------------------------------------------- - if(!are_trashed && window.feature_flags.create_shortcut){ - menu_items.push({ - html: i18n('create_shortcut'), - html: is_shared_with_me ? i18n('create_desktop_shortcut_s') : i18n('create_shortcut_s'), - onClick: async function(){ - $selected_items.each(function() { - let base_dir = path.dirname($(this).attr('data-path')); - // Trash on Desktop is a special case - if($(this).attr('data-path') && $(this).closest('.item-container').attr('data-path') === window.desktop_path){ - base_dir = window.desktop_path; - } - if ( is_shared_with_me ) base_dir = window.desktop_path; - // create shortcut - window.create_shortcut( - path.basename($(this).attr('data-path')), - $(this).attr('data-is_dir') === '1', - base_dir, - $(this).closest('.item-container'), - $(this).attr('data-shortcut_to') === '' ? $(this).attr('data-uid') : $(this).attr('data-shortcut_to'), - $(this).attr('data-shortcut_to_path') === '' ? $(this).attr('data-path') : $(this).attr('data-shortcut_to_path'), - ); - }) - } - }); - } - // ------------------------------------------- // Delete // ------------------------------------------- if(!are_trashed){ @@ -523,32 +530,6 @@ export const add_single_select_menu_items = async (menu_items, { menu_items.push('-') } // ------------------------------------------- - // Create Shortcut - // ------------------------------------------- - if(!is_trashed && window.feature_flags.create_shortcut){ - menu_items.push({ - html: is_shared_with_me ? i18n('create_desktop_shortcut') : i18n('create_shortcut'), - onClick: async function(){ - let base_dir = path.dirname($(el_item).attr('data-path')); - // Trash on Desktop is a special case - if($(el_item).attr('data-path') && $(el_item).closest('.item-container').attr('data-path') === window.desktop_path){ - base_dir = window.desktop_path; - } - - if ( is_shared_with_me ) base_dir = window.desktop_path; - - window.create_shortcut( - path.basename($(el_item).attr('data-path')), - options.is_dir, - base_dir, - options.appendTo, - options.shortcut_to === '' ? options.uid : options.shortcut_to, - options.shortcut_to_path === '' ? options.path : options.shortcut_to_path, - ); - } - }); - } - // ------------------------------------------- // Delete // ------------------------------------------- if($(el_item).attr('data-immutable') === '0' && !is_trashed && !is_shared_with_me){