Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(client): make the browser menu's Cut work in code notes
Elian Doran4dd3dc8ce76a
diff --git a/apps/client/src/menus/note_context_menu.spec.ts b/apps/client/src/menus/note_context_menu.spec.tsindex b182620ff5..a7d58239f7 100644--- a/apps/client/src/menus/note_context_menu.spec.ts+++ b/apps/client/src/menus/note_context_menu.spec.ts@@ -235,6 +235,31 @@ describe("setupContextMenu (browser)", () => {/** Lets the listener's async tail run, so a menu it would raise has been raised by now. */const settle = () => new Promise((resolve) => setTimeout(resolve, 0));+ /** happy-dom ships no `execCommand`, so the browser's own cut is only observable as a stub. */+ function stubExecCommand() {+ const execCommand = vi.fn(() => true);+ document.execCommand = execCommand;+ return execCommand;+ }++ /** An editable standing in for CodeMirror's `.cm-content`, read-only when asked. */+ function codeEditable(readOnly = false) {+ const content = document.createElement("div");+ content.setAttribute("contenteditable", "true");+ if (readOnly) {+ content.setAttribute("aria-readonly", "true");+ }+ document.body.appendChild(content);+ return content;+ }++ /** Right-clicks `element` and returns the rows the menu went up with. */+ async function menuFor(element: Element) {+ rightClick(element);+ await vi.waitFor(() => expect(contextMenu.show).toHaveBeenCalled());+ return vi.mocked(contextMenu.show).mock.calls[0][0].items;+ }+it("leaves the browser's own menu up when nothing is selected", async () => {const div = document.createElement("div");document.body.appendChild(div);@@ -270,6 +295,46 @@ describe("setupContextMenu (browser)", () => {expect(contextMenu.show).not.toHaveBeenCalled();});+ it("cuts a text note through its editor and a code note through the browser", async () => {+ const execCommand = stubExecCommand();++ const content = codeEditable();+ tabManager.activeNote = { type: "code" }; // so no CKEditor answers for the selection+ setSelection(content, "<span>code</span>", "code");++ const codeRows = await menuFor(content);+ expect(findItem(codeRows, "electron_context_menu.cut")?.enabled).toBe(true);+ await run(codeRows, "electron_context_menu.cut");+ expect(execCommand).toHaveBeenCalledWith("cut");++ vi.clearAllMocks();++ const editorRoot = codeEditable();+ const anchor = document.createElement("span");+ editorRoot.appendChild(anchor);+ const editor = fakeEditor(editorRoot, "<p>clean</p>");+ tabManager.activeNote = { type: "text" };+ tabManager.activeContext = { getTextEditor: async () => editor };+ setSelection(anchor, "<p>clean</p>", "clean");++ await run(await menuFor(editorRoot), "electron_context_menu.cut");+ expect(copyHtml).toHaveBeenCalledWith("<p>clean</p>", "clean");+ expect(editor.execute).toHaveBeenCalledWith("delete");+ expect(execCommand).not.toHaveBeenCalled();+ });++ it("drops the cut row in a read-only code note", async () => {+ const content = codeEditable(true);+ tabManager.activeNote = { type: "code" };+ setSelection(content, "<span>code</span>", "code");++ const shown = titles(await menuFor(content));++ expect(shown).not.toContain("electron_context_menu.cut");+ expect(shown).toContain("electron_context_menu.copy");+ expect(shown).toContain("electron_context_menu.copy-as-markdown");+ });+it("takes the menu over for a selection, and reads the link under the pointer", async () => {const link = document.createElement("a");link.href = "https://example.com/";diff --git a/apps/client/src/menus/note_context_menu.ts b/apps/client/src/menus/note_context_menu.tsindex 289e57526c..566a51cee3 100644--- a/apps/client/src/menus/note_context_menu.ts+++ b/apps/client/src/menus/note_context_menu.ts@@ -91,7 +91,7 @@ export function setupContextMenu() {linkURL: link instanceof HTMLAnchorElement ? link.href : "",linkText: link?.textContent ?? "",isMedia: !!element?.closest("img, video, audio"),- isEditable: !!element?.isContentEditable,+ isEditable: acceptsTyping(element),selectionText});});@@ -276,18 +276,37 @@ async function showBrowserContextMenu(x: number, y: number, target: ContextMenuTcontextMenu.show({ x, y, items, selectMenuItemHandler: () => {} });}+/**+ * Whether `element` takes typing, which is what decides the rows that write to a note.+ *+ * `isContentEditable` alone does not answer it inside a code note. CodeMirror drives+ * `contenteditable` off its `editable` facet, which Trilium never lowers, and marks a read-only+ * note with `aria-readonly` on the same element instead — so the attribute is what tells the two+ * apart.+ */+function acceptsTyping(element: HTMLElement | null) {+ return !!element?.isContentEditable && !element.closest("[aria-readonly='true']");+}+/*** The host for a page, which has web APIs and nothing more.*- * `editor` is what the selection sits in, when it sits in a text note at all. Cut needs somewhere- * to delete from, and the editor is the only thing in a note that answers that.+ * `editor` is what the selection sits in, when it sits in a text note at all — `null` in a code+ * note, which has a CodeMirror instead, and in a plain field.*/function browserHost(editor: CKTextEditor | null): ContextMenuHost {return {- canCut: !!editor && !editor.isReadOnly,- async cut() {- await copySelection();- editor?.execute("delete");+ canCut: !editor?.isReadOnly,+ cut() {+ // A text note's editor is asked directly, so that the clipboard gets the clean+ // data-pipeline HTML `copySelection` reads rather than the editing view's markup.+ // Everywhere else the browser cuts, and CodeMirror takes that as it would any edit.+ if (!editor) {+ document.execCommand("cut");+ return;+ }++ return copySelection().then(() => editor.execute("delete"));},canCopy: true,copy: copySelection,