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
client/code note/text toolss: fix an issue
Adorian Doranb7017cb41b0c
diff --git a/apps/client/src/menus/note_context_menu.spec.ts b/apps/client/src/menus/note_context_menu.spec.tsindex d9f24f0031..58b96e4bb1 100644--- a/apps/client/src/menus/note_context_menu.spec.ts+++ b/apps/client/src/menus/note_context_menu.spec.ts@@ -79,7 +79,8 @@ function fakeEditor(domRoot: HTMLElement, selectedHtml: string, plainText = "",* Points window.getSelection() at `anchorNode`, cloning `fallbackHtml` for the DOM-range path.* `text` is what the selection stringifies to — what the browser menu's gate reads. happy-dom lays* nothing out, so the highlighted box is stated here: it covers the origin every `rightClick`- * lands on unless that test says otherwise.+ * lands on unless that test says otherwise. The selection touches `anchorNode`, so it partly+ * contains the ancestors of that node.*/function setSelection(anchorNode: Node | null, fallbackHtml = "", text = "") {const fragment = document.createDocumentFragment();@@ -95,6 +96,9 @@ function setSelection(anchorNode: Node | null, fallbackHtml = "", text = "") {cloneContents: () => fragment.cloneNode(true),getClientRects: () => [ { left: 0, top: 0, right: 200, bottom: 100 } ]}),+ isCollapsed: !text,+ containsNode: (node: Node, allowPartialContainment = false) => !!anchorNode+ && (node === anchorNode || (allowPartialContainment && node.contains(anchorNode))),toString: () => text} as unknown as Selection);}@@ -508,6 +512,14 @@ describe("setupContextMenu (browser)", () => {return content;}+ /** A line of a code editor, as CodeMirror renders one inside its `.cm-content`. */+ function codeLine(content: HTMLElement) {+ const line = document.createElement("div");+ line.className = "cm-line";+ content.appendChild(line);+ return line;+ }+/** Right-clicks `element` and returns the rows the menu went up with. */async function menuFor(element: Element) {rightClick(element);@@ -798,6 +810,37 @@ describe("setupContextMenu (browser)", () => {}});+ it("claims a selection of blank lines or spaces on a line of a code note", async () => {+ const line = codeLine(codeEditable());+ setSelection(line, "", "\n \n");++ // Away from the highlighted box: any part of a line that the selection touches counts.+ const event = rightClick(line, { clientX: 500, clientY: 500 });+ await vi.waitFor(() => expect(contextMenu.show).toHaveBeenCalled());++ expect(event.defaultPrevented).toBe(true);+ });++ it("leaves a blank selection to the browser off its lines or when read-only", async () => {+ const content = codeEditable();+ const selectedLine = codeLine(content);+ setSelection(selectedLine, "", " ");+ const otherLine = rightClick(codeLine(content));++ const readOnlyLine = codeLine(codeEditable(true));+ setSelection(readOnlyLine, "", " ");+ const readOnly = rightClick(readOnlyLine);++ const caretLine = codeLine(codeEditable());+ setSelection(caretLine, "", "");+ const caret = rightClick(caretLine);+ await settle();++ expect([ otherLine, readOnly, caret ].map((event) => event.defaultPrevented))+ .toEqual([ false, false, false ]);+ expect(contextMenu.show).not.toHaveBeenCalled();+ });+it("drops the cut row in a read-only code note", async () => {const content = codeEditable(true);setSelection(content, "<span>code</span>", "code");diff --git a/apps/client/src/menus/note_context_menu.ts b/apps/client/src/menus/note_context_menu.tsindex 4b7cdc73be..2a27a9d9b1 100644--- a/apps/client/src/menus/note_context_menu.ts+++ b/apps/client/src/menus/note_context_menu.ts@@ -84,8 +84,9 @@ export function setupContextMenu() {// earlier one standing.const selection = window.getSelection();const selectionText = selection?.toString() ?? "";- const isOnSelection = !!selection && !!selectionText.trim()- && isPointInSelection(selection, event.clientX, event.clientY);+ const isOnSelection = !!selection && (isOnSelectedCodeLine(selection, element, isEditable)+ || (!!selectionText.trim()+ && isPointInSelection(selection, event.clientX, event.clientY)));// A table cell in the text editor is claimed even at a bare caret, for the table section.const isOnEditableTableCell = isEditable && !!element?.closest("td, th");@@ -332,6 +333,19 @@ async function showBrowserContextMenu(x: number, y: number, target: ContextMenuTcontextMenu.show({ x, y, items, selectMenuItemHandler: () => {} });}+/**+ * Whether `element` is on a line of an editable code editor that `selection` touches. The whole+ * line counts, so that the text tools are reachable for a selection of blank lines or spaces.+ */+function isOnSelectedCodeLine(+ selection: Selection,+ element: HTMLElement | null,+ isEditable: boolean+) {+ const line = isEditable ? element?.closest(".cm-line") : null;+ return !!line && !selection.isCollapsed && selection.containsNode(line, true);+}+/** Whether the point (`x`, `y`), in viewport coordinates, lands on what `selection` highlights. */function isPointInSelection(selection: Selection, x: number, y: number) {for (let i = 0; i < selection.rangeCount; i++) {