toolbox-context-menu.js (3005B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const Menu = require("resource://devtools/client/framework/menu.js"); 8 const MenuItem = require("resource://devtools/client/framework/menu-item.js"); 9 10 // This WeakMap will be used to know if strings have already been loaded in a given 11 // window, which will be used as key. 12 const stringsLoaded = new WeakMap(); 13 14 /** 15 * Lazily load strings for the edit menu. 16 */ 17 function loadEditMenuStrings(win) { 18 if (stringsLoaded.has(win)) { 19 return; 20 } 21 22 if (win.MozXULElement) { 23 stringsLoaded.set(win, true); 24 win.MozXULElement.insertFTLIfNeeded("toolkit/global/textActions.ftl"); 25 } 26 } 27 28 /** 29 * Return an 'edit' menu for a input field. This integrates directly 30 * with docshell commands to provide the right enabled state and editor 31 * functionality. 32 * 33 * You'll need to call menu.popup() yourself, this just returns the Menu instance. 34 * 35 * @param {Window} win parent window reference 36 * @param {string} id menu ID 37 * 38 * @returns {Menu} 39 */ 40 function createEditContextMenu(win, id) { 41 // Localized strings for the menu are loaded lazily. 42 loadEditMenuStrings(win); 43 44 const docshell = win.docShell; 45 const menu = new Menu({ id }); 46 menu.append( 47 new MenuItem({ 48 id: "editmenu-undo", 49 l10nID: "text-action-undo", 50 disabled: !docshell.isCommandEnabled("cmd_undo"), 51 click: () => { 52 docshell.doCommand("cmd_undo"); 53 }, 54 }) 55 ); 56 menu.append( 57 new MenuItem({ 58 type: "separator", 59 }) 60 ); 61 menu.append( 62 new MenuItem({ 63 id: "editmenu-cut", 64 l10nID: "text-action-cut", 65 disabled: !docshell.isCommandEnabled("cmd_cut"), 66 click: () => { 67 docshell.doCommand("cmd_cut"); 68 }, 69 }) 70 ); 71 menu.append( 72 new MenuItem({ 73 id: "editmenu-copy", 74 l10nID: "text-action-copy", 75 disabled: !docshell.isCommandEnabled("cmd_copy"), 76 click: () => { 77 docshell.doCommand("cmd_copy"); 78 }, 79 }) 80 ); 81 menu.append( 82 new MenuItem({ 83 id: "editmenu-paste", 84 l10nID: "text-action-paste", 85 disabled: !docshell.isCommandEnabled("cmd_paste"), 86 click: () => { 87 docshell.doCommand("cmd_paste"); 88 }, 89 }) 90 ); 91 menu.append( 92 new MenuItem({ 93 id: "editmenu-delete", 94 l10nID: "text-action-delete", 95 disabled: !docshell.isCommandEnabled("cmd_delete"), 96 click: () => { 97 docshell.doCommand("cmd_delete"); 98 }, 99 }) 100 ); 101 menu.append( 102 new MenuItem({ 103 type: "separator", 104 }) 105 ); 106 menu.append( 107 new MenuItem({ 108 id: "editmenu-selectAll", 109 l10nID: "text-action-select-all", 110 disabled: !docshell.isCommandEnabled("cmd_selectAll"), 111 click: () => { 112 docshell.doCommand("cmd_selectAll"); 113 }, 114 }) 115 ); 116 return menu; 117 } 118 119 module.exports.createEditContextMenu = createEditContextMenu;