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(text editor): anchor reference-link insertion at the mention pick position
matt wilkie5c8ac043ce61
diff --git a/packages/ckeditor5/src/plugins/referencelink.spec.ts b/packages/ckeditor5/src/plugins/referencelink.spec.tsindex da6edbf7ad..5bcb6e40da 100644--- a/packages/ckeditor5/src/plugins/referencelink.spec.ts+++ b/packages/ckeditor5/src/plugins/referencelink.spec.ts@@ -1,4 +1,4 @@-import { _getViewData as getViewData, _setModelData as setModelData, ClassicEditor, Essentials, LinkEditing, Paragraph } from "ckeditor5";+import { _getModelData as getModelData, _getViewData as getViewData, _setModelData as setModelData, ClassicEditor, Essentials, LinkEditing, Paragraph } from "ckeditor5";import { beforeEach, describe, expect, it, vi } from "vitest";import { createTestEditor } from "../../test/editor-kit.js";@@ -61,6 +61,88 @@ describe("ReferenceLink", () => {expect(findReference(editor)).toBeUndefined();});+ describe("anchored insertion during the title fetch (#10663)", () => {+ let resolveTitle: () => void = () => {};++ beforeEach(() => {+ getReferenceLinkTitle.mockImplementation(+ () =>+ new Promise<void>((resolve) => {+ resolveTitle = resolve;+ })+ );+ });++ it("inserts at the position captured at execute time even when typing continues during the fetch", async () => {+ setModelData(editor.model, "<paragraph>foo[]</paragraph>");++ editor.execute("referenceLink", { href: "#root/noteAbc" });++ // Simulate the user continuing to type while the title fetch is still pending.+ editor.execute("insertText", { text: " BBB" });++ resolveTitle();+ await Promise.resolve();+ await Promise.resolve();++ const data = getModelData(editor.model, { withoutSelection: true });+ expect(data).toBe('<paragraph>foo<reference href="#root/noteAbc"></reference> BBB</paragraph>');++ // The caret must stay where typing left it, not get yanked back onto the reference.+ const selection = editor.model.document.selection;+ const reference = findReference(editor);+ expect(reference).toBeDefined();+ if (!reference) {+ return;+ }+ const afterReference = editor.model.createPositionAfter(reference);+ expect(selection.getFirstPosition()?.isEqual(afterReference)).toBe(false);+ });++ it("puts the caret after the link when the selection has not moved during the fetch", async () => {+ setModelData(editor.model, "<paragraph>foo[]</paragraph>");++ editor.execute("referenceLink", { href: "#root/noteAbc" });++ resolveTitle();+ await Promise.resolve();+ await Promise.resolve();++ const reference = findReference(editor);+ expect(reference).toBeDefined();+ if (!reference) {+ return;+ }++ const selection = editor.model.document.selection;+ expect(selection.isCollapsed).toBe(true);+ const afterReference = editor.model.createPositionAfter(reference);+ expect(selection.getFirstPosition()?.isEqual(afterReference)).toBe(true);+ });++ it("does not insert when the picked context was deleted during the fetch", async () => {+ setModelData(editor.model, "<paragraph>foo[]</paragraph>");++ editor.execute("referenceLink", { href: "#root/noteAbc" });++ const root = editor.model.document.getRoot();+ expect(root).toBeDefined();+ if (root) {+ editor.model.change((writer) => {+ for (const child of Array.from(root.getChildren())) {+ writer.remove(child);+ }+ });+ }++ resolveTitle();+ await Promise.resolve();+ await Promise.resolve();++ expect(findReference(editor)).toBeUndefined();+ });+ });+it("is enabled where text is allowed and disabled where it is not", () => {const command = editor.commands.get("referenceLink");diff --git a/packages/ckeditor5/src/plugins/referencelink.ts b/packages/ckeditor5/src/plugins/referencelink.tsindex 7569d2fa6b..0fb9af97c8 100644--- a/packages/ckeditor5/src/plugins/referencelink.ts+++ b/packages/ckeditor5/src/plugins/referencelink.ts@@ -1,4 +1,4 @@-import { Command, ModelElement, LinkEditing, Plugin, toWidget, viewToModelPositionOutsideModelElement, Widget } from "ckeditor5";+import { Command, ModelElement, ModelLivePosition, LinkEditing, Plugin, toWidget, viewToModelPositionOutsideModelElement, Widget } from "ckeditor5";export default class ReferenceLink extends Plugin {static get requires() {@@ -15,17 +15,41 @@ class ReferenceLinkCommand extends Command {const editor = this.editor;+ const selectionPosition = editor.model.document.selection.getFirstPosition();+ if (!selectionPosition) {+ return;+ }++ // Fetching the title can be a network round trip (froca cache miss), and the+ // user keeps typing while we wait. Anchor the insertion at the position+ // captured here rather than wherever the caret ends up at resolve time (#10663).+ const insertionPosition = ModelLivePosition.fromPosition(selectionPosition, 'toPrevious');+// make sure the referenced note is in cache before adding the reference elementglob.getReferenceLinkTitle(href).then(() => {+ if (insertionPosition.root.rootName === '$graveyard') {+ // The context the user picked in was deleted while the title loaded.+ return;+ }++ const currentSelection = editor.model.document.selection;+ const selectionUntouched = currentSelection.isCollapsed+ && (currentSelection.getFirstPosition()?.isEqual(insertionPosition) ?? false);+editor.model.change(writer => {const placeholder = writer.createElement('reference', {href});// ... and insert it into the document.- editor.model.insertContent(placeholder);+ editor.model.insertContent(placeholder, insertionPosition);- // Put the selection on the inserted element.- writer.setSelection(placeholder, 'after');+ // Put the selection on the inserted element, but only if the user hasn't+ // moved on while we were waiting for the title.+ if (selectionUntouched) {+ writer.setSelection(placeholder, 'after');+ }});+ }).finally(() => {+ insertionPosition.detach();});}