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(links): leave a moved caret alone when a reference lands
Elian Doran5d852d1a0ac2
diff --git a/packages/ckeditor5/src/plugins/internallink.spec.ts b/packages/ckeditor5/src/plugins/internallink.spec.tsindex 71ea629cdb..f741f7379f 100644--- a/packages/ckeditor5/src/plugins/internallink.spec.ts+++ b/packages/ckeditor5/src/plugins/internallink.spec.ts@@ -147,6 +147,21 @@ describe("the link at the selection", () => {expect(data()).toBe(original);});+ it("puts the caret after the reference, unless it moved while the title loaded", async () => {+ setModelData(editor.model, LINKED);+ await replaceLinkAtSelection(editor, "#root/abc", null);+ expect(getModelData(editor.model)).toMatch(/<\/reference>\[\] b<\/paragraph>$/);++ setModelData(editor.model, LINKED);+ const replacing = replaceLinkAtSelection(editor, "#root/abc", null);+ editor.model.change((writer) => {+ const paragraph = editor.model.document.getRoot()?.getChild(0);+ if (paragraph) writer.setSelection(paragraph, 0);+ });+ await replacing;+ expect(getModelData(editor.model)).toMatch(/^<paragraph>\[\]a <reference/);+ });+it("makes no reference once the link is gone by the time the title loads", async () => {setModelData(editor.model, LINKED);const replacing = replaceLinkAtSelection(editor, "#root/abc", null);diff --git a/packages/ckeditor5/src/plugins/internallink.ts b/packages/ckeditor5/src/plugins/internallink.tsindex 24e59a3c5e..ee7c5e86ce 100644--- a/packages/ckeditor5/src/plugins/internallink.ts+++ b/packages/ckeditor5/src/plugins/internallink.ts@@ -109,7 +109,8 @@ export function getLinkAtSelection(editor: Editor): { href: string; text: string/*** Replaces the link around the selection with `text` linking to `href`, keeping the formatting of* its start, or with a reference to the note at `href` when `text` is `null`. A reference replaces- * the link once `glob.getReferenceLinkTitle()` resolves, in one undo step with the removal.+ * the link once `glob.getReferenceLinkTitle()` resolves, in one undo step with the removal, and+ * takes the caret along only if the user has not moved it in the meantime.*/export async function replaceLinkAtSelection(editor: Editor, href: string, text: string | null) {const found = findLinkAtSelection(editor);@@ -120,6 +121,12 @@ export async function replaceLinkAtSelection(editor: Editor, href: string, text:const model = editor.model;if (text === null) {const range = ModelLiveRange.fromRange(found.range);+ const selection = model.document.selection;+ let selectionMoved = false;+ const onSelectionChange = (_event: unknown, data: { directChange: boolean }) => {+ selectionMoved = data.directChange || selectionMoved;+ };+ selection.on('change:range', onSelectionChange);try {const title = await glob.getReferenceLinkTitle(href);if (range.root.rootName === '$graveyard') {@@ -131,9 +138,12 @@ export async function replaceLinkAtSelection(editor: Editor, href: string, text:const start = range.start;writer.remove(range);writer.insert(reference, start);- writer.setSelection(reference, 'after');+ if (!selectionMoved) {+ writer.setSelection(reference, 'after');+ }});} finally {+ selection.off('change:range', onSelectionChange);range.detach();}return;