Procházet zdrojové kódy

dev: migrate "Create Shortcut" to DRY multi/single

KernelDeimos před 6 dny
rodič
revize
3dbe89f3dc
3 změnil soubory, kde provedl 53 přidání a 55 odebrání
  1. 5 1
      src/gui/src/UI/UIItem.js
  2. 13 0
      src/gui/src/UI/lib/TODO.md
  3. 35 54
      src/gui/src/UI/lib/ui_item.js

+ 5 - 1
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 truncate_filename from '../helpers/truncate_filename.js';
 import launch_app from "../helpers/launch_app.js"
 import launch_app from "../helpers/launch_app.js"
 import open_item from "../helpers/open_item.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){
 function UIItem(options){
     const matching_appendto_count = $(options.appendTo).length;
     const matching_appendto_count = $(options.appendTo).length;
@@ -753,6 +753,10 @@ function UIItem(options){
         event.preventDefault();
         event.preventDefault();
         let menu_items = [];
         let menu_items = [];
         const $selected_items = $(el_item).closest('.item-container').find('.item-selected').not(el_item).addBack();
         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
         // Multiple items selected
         if($selected_items.length > 1){
         if($selected_items.length > 1){

+ 13 - 0
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

+ 35 - 54
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 launch_app from "../../helpers/launch_app.js"
 import open_item from "../../helpers/open_item.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, {
 export const add_multiple_select_menu_items = (menu_items, {
     $selected_items,
     $selected_items,
     el_item,
     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
     // Delete
     // -------------------------------------------
     // -------------------------------------------
     if(!are_trashed){
     if(!are_trashed){
@@ -523,32 +530,6 @@ export const add_single_select_menu_items = async (menu_items, {
         menu_items.push('-')
         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
     // Delete
     // -------------------------------------------
     // -------------------------------------------
     if($(el_item).attr('data-immutable') === '0' && !is_trashed && !is_shared_with_me){
     if($(el_item).attr('data-immutable') === '0' && !is_trashed && !is_shared_with_me){