no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / toolkit / content / editMenuOverlay.js
blobb0ec197224206d628cd7e5fb9e0022c38f341cd2
1 // -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // update menu items that rely on focus or on the current selection
8 function goUpdateGlobalEditMenuItems(force) {
9   // Don't bother updating the edit commands if they aren't visible in any way
10   // (i.e. the Edit menu isn't open, nor is the context menu open, nor have the
11   // cut, copy, and paste buttons been added to the toolbars) for performance.
12   // This only works in applications/on platforms that set the gEditUIVisible
13   // flag, so we check to see if that flag is defined before using it.
14   if (!force && typeof gEditUIVisible != "undefined" && !gEditUIVisible) {
15     return;
16   }
18   goUpdateUndoEditMenuItems();
19   goUpdateCommand("cmd_cut");
20   goUpdateCommand("cmd_copy");
21   goUpdatePasteMenuItems();
22   goUpdateCommand("cmd_selectAll");
23   goUpdateCommand("cmd_delete");
24   goUpdateCommand("cmd_switchTextDirection");
27 // update menu items that relate to undo/redo
28 function goUpdateUndoEditMenuItems() {
29   goUpdateCommand("cmd_undo");
30   goUpdateCommand("cmd_redo");
33 // update menu items that depend on clipboard contents
34 function goUpdatePasteMenuItems() {
35   goUpdateCommand("cmd_paste");
36   goUpdateCommand("cmd_pasteNoFormatting");
39 // Inject the commandset here instead of relying on preprocessor to share this across documents.
40 window.addEventListener(
41   "DOMContentLoaded",
42   () => {
43     let container =
44       document.querySelector("commandset") || document.documentElement;
45     let fragment = MozXULElement.parseXULToFragment(`
46       <commandset id="editMenuCommands">
47         <commandset id="editMenuCommandSetAll" commandupdater="true" events="focus,select" />
48         <commandset id="editMenuCommandSetUndo" commandupdater="true" events="undo" />
49         <commandset id="editMenuCommandSetPaste" commandupdater="true" events="clipboard" />
50         <command id="cmd_undo" internal="true"/>
51         <command id="cmd_redo" internal="true" />
52         <command id="cmd_cut" internal="true" />
53         <command id="cmd_copy" internal="true" />
54         <command id="cmd_paste" internal="true" />
55         <command id="cmd_pasteNoFormatting" internal="true" />
56         <command id="cmd_delete" />
57         <command id="cmd_selectAll" internal="true" />
58         <command id="cmd_switchTextDirection" />
59       </commandset>
60     `);
62     let editMenuCommandSetAll = fragment.querySelector(
63       "#editMenuCommandSetAll"
64     );
65     editMenuCommandSetAll.addEventListener("commandupdate", function () {
66       goUpdateGlobalEditMenuItems();
67     });
69     let editMenuCommandSetUndo = fragment.querySelector(
70       "#editMenuCommandSetUndo"
71     );
72     editMenuCommandSetUndo.addEventListener("commandupdate", function () {
73       goUpdateUndoEditMenuItems();
74     });
76     let editMenuCommandSetPaste = fragment.querySelector(
77       "#editMenuCommandSetPaste"
78     );
79     editMenuCommandSetPaste.addEventListener("commandupdate", function () {
80       goUpdatePasteMenuItems();
81     });
83     fragment.firstElementChild.addEventListener("command", event => {
84       let commandID = event.target.id;
85       goDoCommand(commandID);
86     });
88     container.appendChild(fragment);
89   },
90   { once: true }
93 // Support context menus on html textareas in the parent process:
94 window.addEventListener("contextmenu", e => {
95   const HTML_NS = "http://www.w3.org/1999/xhtml";
96   let needsContextMenu =
97     e.composedTarget.ownerDocument == document &&
98     !e.defaultPrevented &&
99     e.composedTarget.parentNode.nodeName != "moz-input-box" &&
100     ((["textarea", "input"].includes(e.composedTarget.localName) &&
101       e.composedTarget.namespaceURI == HTML_NS) ||
102       e.composedTarget.closest("search-textbox"));
104   if (!needsContextMenu) {
105     return;
106   }
108   let popup = document.getElementById("textbox-contextmenu");
109   if (!popup) {
110     MozXULElement.insertFTLIfNeeded("toolkit/global/textActions.ftl");
111     document.documentElement.appendChild(
112       MozXULElement.parseXULToFragment(`
113       <menupopup id="textbox-contextmenu" class="textbox-contextmenu">
114         <menuitem data-l10n-id="text-action-undo" command="cmd_undo"></menuitem>
115         <menuitem data-l10n-id="text-action-redo" command="cmd_redo"></menuitem>
116         <menuseparator></menuseparator>
117         <menuitem data-l10n-id="text-action-cut" command="cmd_cut"></menuitem>
118         <menuitem data-l10n-id="text-action-copy" command="cmd_copy"></menuitem>
119         <menuitem data-l10n-id="text-action-paste" command="cmd_paste"></menuitem>
120         <menuitem data-l10n-id="text-action-delete" command="cmd_delete"></menuitem>
121         <menuitem data-l10n-id="text-action-select-all" command="cmd_selectAll"></menuitem>
122       </menupopup>
123     `)
124     );
125     popup = document.documentElement.lastElementChild;
126   }
128   goUpdateGlobalEditMenuItems(true);
129   popup.openPopupAtScreen(e.screenX, e.screenY, true, e);
130   // Don't show any other context menu at the same time. There can be a
131   // context menu from an ancestor too but we only want to show this one.
132   e.preventDefault();