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: fix some issues
Adorian Doranb777b14d8aef
diff --git a/packages/ckeditor5/src/plugins/cuttonote.spec.ts b/packages/ckeditor5/src/plugins/cuttonote.spec.tsindex 317e01bdac..addf5e2fd7 100644--- a/packages/ckeditor5/src/plugins/cuttonote.spec.ts+++ b/packages/ckeditor5/src/plugins/cuttonote.spec.ts@@ -109,7 +109,7 @@ describe("CutToNotePlugin", () => {expect(editor.getData()).toBe("<p>aXb</p><p>cd</p>");});- it("pastes at the current selection when the captured content was removed", () => {+ it("discards the paste when the captured content was removed", () => {setModelData(editor.model, "<paragraph>a[]b</paragraph><paragraph>cd</paragraph>");const target = editor.capturePasteTarget();editor.model.change((writer) => {@@ -122,7 +122,23 @@ describe("CutToNotePlugin", () => {target.paste("", "X");- expect(editor.getData()).toBe("<p>cXd</p>");+ expect(editor.getData()).toBe("<p>cd</p>");+ });++ it("discards the paste when the editor loads other content, such as another note", () => {+ for (const data of [+ "<paragraph>a[]b</paragraph>",+ "<paragraph>a[b</paragraph><paragraph>c]d</paragraph>",+ "<paragraph>[abcd]</paragraph>"+ ]) {+ setModelData(editor.model, data);+ const target = editor.capturePasteTarget();+ editor.setData("<p>other</p>");++ target.paste("", "X");++ expect(editor.getData(), data).toBe("<p>other</p>");+ }});it("does nothing once the editor is destroyed", async () => {@@ -205,6 +221,26 @@ describe("CutToNotePlugin", () => {expect(data).not.toContain("x1");});+ it("capturePasteTarget discards a table paste when the editor loads other content", () => {+ const html = "<table><tbody><tr><td>new</td></tr></tbody></table>";+ const selections: (() => void)[] = [+ () => selectCells(tableEditor, [0, 0], [1, 1]),+ () => selectRoot(tableEditor, "on"),+ () => selectRoot(tableEditor, "in")+ ];++ for (const [index, select] of selections.entries()) {+ setModelData(tableEditor.model, modelTable([["a1", "b1"], ["a2", "b2"]]));+ select();+ const target = tableEditor.capturePasteTarget();+ tableEditor.setData("<p>other</p>");++ target.paste(html, "");++ expect(tableEditor.getData(), `selection ${index}`).toBe("<p>other</p>");+ }+ });+it("capturePasteTarget restores a cell selection before pasting into it", () => {setModelData(tableEditor.model, modelTable([["a1", "b1"], ["a2", "b2"]]));selectCells(tableEditor, [0, 0], [1, 1]);@@ -282,3 +318,20 @@ function selectCells(editor: ClassicEditor, anchor: [number, number], target: [ngetCell(target[0], target[1]));}++/** Selects the first element of the root (`"on"`) or the whole content of the root (`"in"`). */+function selectRoot(editor: ClassicEditor, placement: "on" | "in") {+ const root = editor.model.document.getRoot();+ const first = root?.getChild(0);+ if (!root || !first) {+ throw new Error("The document is empty.");+ }++ editor.model.change((writer) => {+ if (placement === "on") {+ writer.setSelection(first, "on");+ } else {+ writer.setSelection(writer.createRangeIn(root));+ }+ });+}diff --git a/packages/ckeditor5/src/plugins/cuttonote.ts b/packages/ckeditor5/src/plugins/cuttonote.tsindex a6bfed3130..cc3370e47f 100644--- a/packages/ckeditor5/src/plugins/cuttonote.ts+++ b/packages/ckeditor5/src/plugins/cuttonote.ts@@ -8,11 +8,14 @@ import {ViewDataTransfer,viewToPlainText} from 'ckeditor5';-import type { ViewDocumentFragment } from 'ckeditor5';+import type { ModelRange, ViewDocumentFragment } from 'ckeditor5';/** A selection pinned by `capturePasteTarget()` for a paste whose content arrives later. */export interface PasteTarget {- /** Pastes at the pinned selection, then releases it. */+ /**+ * Pastes at the pinned selection, then releases it. Does nothing when the pinned content is+ * gone.+ */paste(html: string, text: string): void;/** Detaches the pinned ranges without pasting. Safe to call more than once. */release(): void;@@ -111,8 +114,8 @@ export default class CutToNotePlugin extends Plugin {* Pins the current selection as live ranges, for a paste whose content arrives asynchronously,* e.g. from `navigator.clipboard.read()`. `paste()` moves the selection back onto the pinned* ranges first, so a selection changed while the content was read does not move the paste.- * Ranges whose content was removed in the meantime are dropped; with none left, the paste goes- * to the current selection.+ * Ranges whose content was removed in the meantime are dropped. With none left, for example+ * after `setData()` loaded another note into the editor, the paste is discarded.*/capturePasteTarget(): PasteTarget {const editor = this.editor;@@ -126,31 +129,33 @@ export default class CutToNotePlugin extends Plugin {return {paste: (html, text) => {- if (editor.state === "destroyed") {- release();- return;- }- this.restoreSelection(ranges);+ const targets = editor.state === "destroyed" ? [] : this.getRemainingRanges(ranges);release();- this.pasteContent(html, text);+ if (targets.length) {+ this.restoreSelection(targets);+ this.pasteContent(html, text);+ }},release};}- private restoreSelection(ranges: ModelLiveRange[]) {- const model = this.editor.model;- const targets = ranges- .filter((range) => range.root !== model.document.graveyard)+ /** The pinned ranges whose content is still in the document. */+ private getRemainingRanges(ranges: ModelLiveRange[]): ModelRange[] {+ const graveyard = this.editor.model.document.graveyard;+ return ranges+ .filter((range) => range.root !== graveyard).map((range) => range.toRange());+ }++ private restoreSelection(targets: ModelRange[]) {+ const model = this.editor.model;const current = [...model.document.selection.getRanges()];const isUnchanged = targets.length === current.length&& targets.every((range, index) => range.isEqual(current[index]));- if (!targets.length || isUnchanged) {- return;+ if (!isUnchanged) {+ model.change((writer) => writer.setSelection(targets));}-- model.change((writer) => writer.setSelection(targets));}async removeSelection() {