diff --git a/src/lib/selectionMenu.js b/src/lib/selectionMenu.js index e697802e6..1329f3e90 100644 --- a/src/lib/selectionMenu.js +++ b/src/lib/selectionMenu.js @@ -2,69 +2,69 @@ import { focusEditorIfEditable } from "cm/editorReadOnly"; import appSettings from "lib/settings"; const exec = (command) => { - const { editor } = editorManager; - editor.execCommand(command); + const { editor } = editorManager; + editor.execCommand(command); - if (command === "selectall") { - editor.scrollToRow(Number.POSITIVE_INFINITY); - editor.setSelection(true); - editor.setMenu(true); - } - focusEditorIfEditable(editor); + if (command === "selectall") { + editor.scrollToRow(Number.POSITIVE_INFINITY); + editor.setSelection(true); + editor.setMenu(true); + } + focusEditorIfEditable(editor); }; const showCodeActions = async () => { - const { editor } = editorManager; - if (!editor) return; + const { editor } = editorManager; + if (!editor) return; - try { - const { showCodeActionsMenu, supportsCodeActions } = await import("cm/lsp"); - if (supportsCodeActions(editor)) { - await showCodeActionsMenu(editor); - } - } catch (error) { - console.warn("[SelectionMenu] Code actions not available:", error); - } + try { + const { showCodeActionsMenu, supportsCodeActions } = await import("cm/lsp"); + if (supportsCodeActions(editor)) { + await showCodeActionsMenu(editor); + } + } catch (error) { + console.warn("[SelectionMenu] Code actions not available:", error); + } }; const items = []; export default function selectionMenu() { - return [ - item( - () => exec("copy"), - , - "selected", - true, - ), - item(() => exec("cut"), , "selected"), - item(() => exec("paste"), , "all"), - item( - () => exec("selectall"), - , - "all", - true, - ), - appSettings.get("showShareButton") && - item( - () => exec("share"), - , - "selected", - true, - ), - item( - (color) => acode.exec("insert-color", color), - , - "all", - ), - item( - () => showCodeActions(), - , - "all", - true, - ), - ...items, - ].filter(Boolean); + return [ + item( + () => exec("copy"), + , + "selected", + true, + ), + item(() => exec("cut"), , "selected"), + item(() => exec("paste"), , "all"), + item( + () => exec("selectall"), + , + "all", + true, + ), + appSettings.get("showShareButton") && + item( + () => exec("share"), + , + "selected", + true, + ), + item( + (color) => acode.exec("insert-color", color), + , + "all", + ), + item( + () => showCodeActions(), + , + "all", + true, + ), + ...items, + ].filter(Boolean); } /** @@ -75,13 +75,20 @@ export default function selectionMenu() { * @param {boolean} readOnly whether to show the item in readOnly mode */ selectionMenu.add = (onclick, text, mode, readOnly) => { - items.push(item(onclick, text, mode, readOnly)); + const menuItem = item(onclick, text, mode, readOnly); + items.push(menuItem); + return menuItem; +}; + +selectionMenu.remove = (item) => { + const i = items.indexOf(item); + if (i >= 0) items.splice(i, 1); }; selectionMenu.exec = (command) => { - exec(command); + exec(command); }; function item(onclick, text, mode = "all", readOnly = false) { - return { onclick, text, mode, readOnly }; + return { onclick, text, mode, readOnly }; }