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): undo a link turned into a reference in one step
Elian Doranae7ff045077f
diff --git a/apps/client/src/widgets/type_widgets/text/CKEditor.tsx b/apps/client/src/widgets/type_widgets/text/CKEditor.tsxindex c9db80b190..272a11e695 100644--- a/apps/client/src/widgets/type_widgets/text/CKEditor.tsx+++ b/apps/client/src/widgets/type_widgets/text/CKEditor.tsx@@ -24,7 +24,7 @@ export interface CKEditorApi {getCurrentLink(): { href: string; text: string } | null;addLink(notePath: string, linkTitle: string | null, externalLink?: boolean): void;/** Rewrites the link around the caret, as {@link addLink} would write a new one. */- updateLink(notePath: string, linkTitle: string | null, externalLink?: boolean): void;+ updateLink(notePath: string, linkTitle: string | null, externalLink?: boolean): Promise<void>;addLinkToEditor(linkHref: string, linkTitle: string): void;addHtmlToEditor(html: string): void;addContentEmbed(noteId: string, boxSize?: BoxSize): void;@@ -163,12 +163,13 @@ export default function CKEditor({ containerRef: externalContainerRef, contentLaconst editor = editorRef.current;return editor ? getLinkAtSelection(editor) : null;},- updateLink(notePath, linkTitle, externalLink) {+ async updateLink(notePath, linkTitle, externalLink) {const editor = editorRef.current;if (!editor) return;- replaceLinkAtSelection(editor, externalLink ? notePath : `#${notePath}`, linkTitle);editor.editing.view.focus();+ const href = externalLink ? notePath : `#${notePath}`;+ await replaceLinkAtSelection(editor, href, linkTitle);},addLink(notePath, linkTitle, externalLink) {const editor = editorRef.current;diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsxindex ceae420bd6..0770d6339e 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx@@ -720,7 +720,7 @@ export function getAddLinkOpts(api: CKEditorApi, ready: () => Promise<unknown>):...parseLinkHref(currentLink.href),async addLink(notePath, linkTitle, externalLink) {await ready();- api.updateLink(notePath, linkTitle, externalLink);+ await api.updateLink(notePath, linkTitle, externalLink);}};}diff --git a/packages/ckeditor5/src/plugins/internallink.spec.ts b/packages/ckeditor5/src/plugins/internallink.spec.tsindex 1e2f405a7e..982cfe640a 100644--- a/packages/ckeditor5/src/plugins/internallink.spec.ts+++ b/packages/ckeditor5/src/plugins/internallink.spec.ts@@ -112,6 +112,28 @@ describe("the link at the selection", () => {await vi.waitFor(() => expect(data()).toContain("<reference href=\"#root/abc\""));expect(data()).not.toContain("foo");});++ it("makes the reference in one undo step, and none when the title fails to load", async () => {+ setModelData(editor.model, LINKED);+ const data = () => getModelData(editor.model, { withoutSelection: true });+ const original = data();+ await replaceLinkAtSelection(editor, "#root/abc", null);+ expect(data()).toContain("<reference href=\"#root/abc\"");+ editor.execute("undo");+ expect(data()).toBe(original);++ installGlobMock({+ getComponentByEl: () => ({ loadReferenceLinkTitle: async () => undefined }),+ getReferenceLinkTitle: async () => {+ throw new Error("not found");+ },+ getReferenceLinkTitleSync: () => "Target"+ });+ setModelData(editor.model, LINKED);+ const replacing = replaceLinkAtSelection(editor, "#root/abc", null);+ await expect(replacing).rejects.toThrow("not found");+ expect(data()).toBe(original);+ });});describe("the edit button of the link toolbar", () => {diff --git a/packages/ckeditor5/src/plugins/internallink.ts b/packages/ckeditor5/src/plugins/internallink.tsindex 7aa7a34b35..40a101b3d3 100644--- a/packages/ckeditor5/src/plugins/internallink.ts+++ b/packages/ckeditor5/src/plugins/internallink.ts@@ -1,7 +1,9 @@-import { ButtonView, Command, findAttributeRange, IconPencil, LinkUI, Plugin } from 'ckeditor5';+import {+ ButtonView, Command, findAttributeRange, IconPencil, LinkUI, ModelLiveRange, Plugin+} from 'ckeditor5';import type { Editor, ModelRange } from 'ckeditor5';import internalLinkIcon from '../icons/trilium.svg?raw';-import ReferenceLink from './referencelink';+import ReferenceLink, { referenceAttributes } from './referencelink';export const COMMAND_NAME = 'insertInternalLink';@@ -106,34 +108,46 @@ 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`.+ * 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.*/-export function replaceLinkAtSelection(editor: Editor, href: string, text: string | null) {+export async function replaceLinkAtSelection(editor: Editor, href: string, text: string | null) {const found = findLinkAtSelection(editor);if (!found) {return;}const model = editor.model;+ if (text === null) {+ const range = ModelLiveRange.fromRange(found.range);+ try {+ const title = await glob.getReferenceLinkTitle(href);+ if (range.root.rootName === '$graveyard') {+ return;+ }+ model.change((writer) => {+ const attributes = referenceAttributes(href, title);+ const reference = writer.createElement('reference', attributes);+ const start = range.start;+ writer.remove(range);+ writer.insert(reference, start);+ writer.setSelection(reference, 'after');+ });+ } finally {+ range.detach();+ }+ return;+ }+model.change((writer) => {const start = found.range.start;const first = start.textNode ?? start.nodeAfter;const attributes = new Map(first?.getAttributes() ?? []);writer.remove(found.range);-- if (text === null) {- writer.setSelection(start);- return;- }-attributes.set('linkHref', href);writer.insertText(text, attributes, start);writer.setSelection(start.getShiftedBy(text.length));});-- if (text === null) {- editor.execute('referenceLink', { href });- }}function findLinkAtSelection(editor: Editor): { href: string; range: ModelRange } | null {diff --git a/packages/ckeditor5/src/plugins/referencelink.ts b/packages/ckeditor5/src/plugins/referencelink.tsindex 05fcb495f6..7619cd616e 100644--- a/packages/ckeditor5/src/plugins/referencelink.ts+++ b/packages/ckeditor5/src/plugins/referencelink.ts@@ -299,7 +299,7 @@ export class ReferenceLinkEditing extends Plugin {}/** The attributes of a `reference` to `href`, with `storedTitle` when there is one to keep. */-function referenceAttributes( href: string | undefined, storedTitle: string | undefined ) {+export function referenceAttributes( href: string | undefined, storedTitle: string | undefined ) {return storedTitle ? { href, storedTitle } : { href };}