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/text editor/tables: increase test coverage, improve a comment
Adorian Doraneb15a591e273
diff --git a/apps/client/src/menus/electron_context_menu.ts b/apps/client/src/menus/electron_context_menu.tsindex b4a00aafbf..ce519ec6a8 100644--- a/apps/client/src/menus/electron_context_menu.ts+++ b/apps/client/src/menus/electron_context_menu.ts@@ -16,8 +16,8 @@ function setupContextMenu() {const api = eApi.contextMenu;api.onContextMenu(async (params) => {- // The IPC params carry window coordinates only, so the element under the pointer is read- // back from the page; the zoom factor maps them onto CSS pixels.+ // The IPC params contain only window coordinates, so `document.elementFromPoint()` finds+ // the element under the pointer. Dividing by the zoom factor converts them to CSS pixels.const zoomLevel = zoomService.getCurrentZoom();const items = await buildNoteContextMenuItems({diff --git a/apps/client/src/menus/text_editor_context_menu.ts b/apps/client/src/menus/text_editor_context_menu.tsindex 6350e90ecd..445c58004e 100644--- a/apps/client/src/menus/text_editor_context_menu.ts+++ b/apps/client/src/menus/text_editor_context_menu.ts@@ -144,11 +144,12 @@ export async function getTextEditorAtSelection(): Promise<CKTextEditor | null> {}/**- * The text editor whose editable contains `node`, or `null` when the node sits anywhere else.- * Answers a pointer target rather than the DOM selection, which a right-click does not move in- * every browser.+ * Returns the text editor whose editable contains `node`, or `null` when no text editor does.+ * Takes the right-click target instead of the DOM selection, because a right-click does not move+ * the selection in every browser.*- * The editor is looked up in the split pane holding `node`, which a right-click does not activate.+ * Looks up the editor in the split pane that contains `node`, because a right-click does not+ * activate that pane.*/export async function getTextEditorContaining(node: Node | null | undefined@@ -174,8 +175,8 @@ export async function getTextEditorContaining(}/**- * The note context of the split pane that holds `node`. Outside every pane, e.g. in a dialog, the- * active note context.+ * Returns the note context whose `ntxId` matches the `data-ntx-id` of the split pane that contains+ * `node`, or the active note context when `node` is outside every pane, e.g. in a dialog.*/function getNoteContextContaining(node: Node): NoteContext | null {const { tabManager } = appContext;diff --git a/packages/ckeditor5/src/plugins/table_context_menu.spec.ts b/packages/ckeditor5/src/plugins/table_context_menu.spec.tsindex 8bb3e15b1e..5e25bad6ae 100644--- a/packages/ckeditor5/src/plugins/table_context_menu.spec.ts+++ b/packages/ckeditor5/src/plugins/table_context_menu.spec.ts@@ -1,5 +1,5 @@import { _getModelData as getModelData, _setModelData as setModelData, Essentials, Paragraph, Table, TableSelection, TableUtils } from "ckeditor5";-import type { ClassicEditor, ModelElement } from "ckeditor5";+import type { ClassicEditor, Editor, ModelElement } from "ckeditor5";import { beforeEach, describe, expect, it } from "vitest";import { createTestEditor } from "../../test/editor-kit.js";@@ -227,6 +227,12 @@ describe("TableContextMenu", () => {const nested = domCell(editor, 0, 1).firstChild;expect(nested && plugin.syncSelectionToDomTarget(nested)).toBe(true);expect(selectionCells(editor)[0]).toBe(getCell(editor, 0, 1));++ // So does a text node.+ const text = document.createTreeWalker(domCell(editor, 1, 0), NodeFilter.SHOW_TEXT)+ .nextNode();+ expect(text && plugin.syncSelectionToDomTarget(text)).toBe(true);+ expect(selectionCells(editor)[0]).toBe(getCell(editor, 1, 0));});it("keeps a multi-cell selection that contains the target", () => {@@ -255,6 +261,18 @@ describe("TableContextMenu", () => {strayCell.remove();}});++ it("rejects a cell drawn inside another widget's raw content", async () => {+ const widgetEditor = await createTestEditor([+ Essentials, Paragraph, Table, TableSelection, TableContextMenu, RawTableWidget+ ]);+ setModelData(widgetEditor.model, "<rawTable></rawTable>");+ const plugin = widgetEditor.plugins.get(TableContextMenu);++ const rawCell = widgetEditor.editing.view.getDomRoot()?.querySelector(".raw-table td");+ expect(rawCell).toBeInstanceOf(HTMLTableCellElement);+ expect(rawCell && plugin.syncSelectionToDomTarget(rawCell)).toBe(false);+ });});describe("right-button mousedown", () => {@@ -281,6 +299,14 @@ describe("TableContextMenu", () => {// Right button with a plain in-cell selection.setModelData(editor.model, modelTable([["1[]1"]]));expect(mouseDown(domCell(editor, 0, 0), 2).defaultPrevented).toBe(false);++ // Right button outside the table.+ setModelData(editor.model, `${modelTable([["11", "12"]])}<paragraph>foo</paragraph>`);+ selectCells(editor, [0, 0], [0, 1]);+ const paragraph = editor.editing.view.getDomRoot()+ ?.querySelector<HTMLElement>(":scope > p");+ expect(paragraph).toBeInstanceOf(HTMLParagraphElement);+ expect(paragraph && mouseDown(paragraph, 2).defaultPrevented).toBe(false);});});});@@ -329,6 +355,18 @@ function selectionCells(editor: ClassicEditor): ModelElement[] {return editor.plugins.get(TableUtils).getSelectionAffectedTableCells(editor.model.document.selection);}+/** A widget whose raw view element holds a rendered table, like an included note's preview. */+function RawTableWidget(editor: Editor) {+ editor.model.schema.register("rawTable", { inheritAllFrom: "$blockObject" });+ editor.conversion.for("editingDowncast").elementToElement({+ model: "rawTable",+ view: (_element, { writer }) => writer.createRawElement("div", { class: "raw-table" },+ (domElement) => {+ domElement.innerHTML = "<table><tbody><tr><td>raw</td></tr></tbody></table>";+ })+ });+}+function mouseDown(target: HTMLElement, button: number): MouseEvent {const event = new MouseEvent("mousedown", { button, bubbles: true, cancelable: true });target.dispatchEvent(event);