Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): keep the last saved title of a reference link whose note goes missing

Elian Dorana342d90d134b
diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts
index 17e21fb60f..fb931473a8 100644
--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts
+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts
@@ -799,7 +799,7 @@ describe("ContentEmbed with attachments", () => {
editor.execute("undo");
expect(getModelData(editor.model, { withoutSelection: true })).toBe(
- `<paragraph>Before <reference href="${LINK_HREF}"></reference> after</paragraph>`
+ `<paragraph>Before <reference href="${LINK_HREF}" storedTitle="x"></reference> after</paragraph>`
);
const relinked = editor.editing.view.getDomRoot()?.querySelector("a.reference-link");
@@ -839,8 +839,8 @@ describe("ContentEmbed with attachments", () => {
"<paragraph>Before </paragraph>" +
"<contentEmbed boxSize=\"full\" noteId=\"noteAbc\"></contentEmbed>" +
"<paragraph> after</paragraph>" +
- `<paragraph><reference href="${cardHref}"></reference></paragraph>` +
- `<paragraph><reference href="${LINK_HREF}"></reference></paragraph>`
+ `<paragraph><reference href="${cardHref}" storedTitle="card"></reference></paragraph>` +
+ `<paragraph><reference href="${LINK_HREF}" storedTitle="file"></reference></paragraph>`
);
// A link in the content of an embed is not part of the note.
diff --git a/packages/ckeditor5/src/plugins/inline_icon/inline_icon.spec.ts b/packages/ckeditor5/src/plugins/inline_icon/inline_icon.spec.ts
index 227e33de33..2f82536cdc 100644
--- a/packages/ckeditor5/src/plugins/inline_icon/inline_icon.spec.ts
+++ b/packages/ckeditor5/src/plugins/inline_icon/inline_icon.spec.ts
@@ -113,7 +113,7 @@ describe("a reference link pasted from a rendered page", () => {
+ `<span><span class="tn-icon bx bx-file"></span>Some note</span></a> for more.</p>`);
expect(getModelData(editor.model, { withoutSelection: true }))
- .toBe(`<paragraph>See <reference href="#root/abc"></reference> for more.</paragraph>`);
+ .toBe(`<paragraph>See <reference href="#root/abc" storedTitle="Some note"></reference> for more.</paragraph>`);
});
});
diff --git a/packages/ckeditor5/src/plugins/referencelink.spec.ts b/packages/ckeditor5/src/plugins/referencelink.spec.ts
index 0722a6a160..0861d15a47 100644
--- a/packages/ckeditor5/src/plugins/referencelink.spec.ts
+++ b/packages/ckeditor5/src/plugins/referencelink.spec.ts
@@ -331,7 +331,7 @@ describe("ReferenceLink", () => {
await Promise.resolve();
expect(getModelData(editor.model, { withoutSelection: true }))
- .toBe('<paragraph>a<reference href="#root/noteNew"></reference>b</paragraph>');
+ .toBe('<paragraph>a<reference href="#root/noteNew" storedTitle="Some title"></reference>b</paragraph>');
editor.execute("undo");
@@ -339,6 +339,19 @@ describe("ReferenceLink", () => {
.toBe('<paragraph>a<reference href="#root/gone" storedTitle="Gone"></reference>b</paragraph>');
});
+ it("saves the last title it saved for a link whose note goes missing while the editor is open", () => {
+ let liveTitle: string | undefined = "Renamed";
+ getReferenceLinkTitleSync.mockImplementation((_href: string, storedTitle?: string) =>
+ liveTitle ?? storedTitle ?? "[missing note]");
+ editor.setData('<p><a class="reference-link" href="#root/noteAbc">Loaded</a></p>');
+
+ expect(editor.getData()).toContain(">Renamed</a>");
+
+ liveTitle = undefined;
+
+ expect(editor.getData()).toContain(">Renamed</a>");
+ });
+
it("asks to fix a reference to a missing note when it is clicked, and not one to a note", () => {
const fixReferenceLink = vi.fn();
// The test setup makes `$` a passthrough, so the title element arrives as itself.
diff --git a/packages/ckeditor5/src/plugins/referencelink.ts b/packages/ckeditor5/src/plugins/referencelink.ts
index e5e43ec3e2..1c30ecbef1 100644
--- a/packages/ckeditor5/src/plugins/referencelink.ts
+++ b/packages/ckeditor5/src/plugins/referencelink.ts
@@ -22,14 +22,14 @@ class ReferenceLinkCommand extends Command {
const editor = this.editor;
if (replace) {
- glob.getReferenceLinkTitle(href).then(() => {
+ glob.getReferenceLinkTitle(href).then(title => {
const root = replace.root;
if (!root.is('rootElement') || root.rootName === '$graveyard') {
return;
}
editor.model.change(writer => {
- const reference = writer.createElement('reference', {href});
+ const reference = writer.createElement('reference', referenceAttributes(href, title));
writer.insert(reference, writer.createPositionBefore(replace));
writer.remove(replace);
writer.setSelection(reference, 'on');
@@ -49,7 +49,7 @@ class ReferenceLinkCommand extends Command {
const insertionPosition = ModelLivePosition.fromPosition(selectionPosition, 'toPrevious');
// make sure the referenced note is in cache before adding the reference element
- glob.getReferenceLinkTitle(href).then(() => {
+ glob.getReferenceLinkTitle(href).then(title => {
if (insertionPosition.root.rootName === '$graveyard') {
// The context the user picked in was deleted while the title loaded.
return;
@@ -60,7 +60,7 @@ class ReferenceLinkCommand extends Command {
&& (currentSelection.getFirstPosition()?.isEqual(insertionPosition) ?? false);
editor.model.change(writer => {
- const placeholder = writer.createElement('reference', {href});
+ const placeholder = writer.createElement('reference', referenceAttributes(href, title));
// ... and insert it into the document.
editor.model.insertContent(placeholder, insertionPosition);
@@ -98,6 +98,9 @@ export class ReferenceLinkEditing extends Plugin {
return 'ReferenceLinkEditing' as const;
}
+ /** The title the data downcast last wrote for each reference. */
+ private readonly _savedTitles = new WeakMap<ModelElement, string>();
+
/**
* Redraws the links and embeds of changed attachments, so they show the current state, and
* removes those of deleted ones.
@@ -225,8 +228,7 @@ export class ReferenceLinkEditing extends Plugin {
const href = viewElement.getAttribute('href');
const storedTitle = getText( viewElement );
- return modelWriter.createElement( 'reference',
- storedTitle ? { href, storedTitle } : { href } );
+ return modelWriter.createElement( 'reference', referenceAttributes( href, storedTitle ) );
}
} );
@@ -281,8 +283,11 @@ export class ReferenceLinkEditing extends Plugin {
class: 'reference-link'
} );
- const storedTitle = modelItem.getAttribute('storedTitle') as string | undefined;
+ // A note deleted while the editor is open keeps the title saved last, not the loaded one.
+ const storedTitle = this._savedTitles.get( modelItem )
+ ?? modelItem.getAttribute('storedTitle') as string | undefined;
const title = glob.getReferenceLinkTitleSync(href, storedTitle);
+ this._savedTitles.set( modelItem, title );
const innerText = viewWriter.createText(title);
viewWriter.insert(viewWriter.createPositionAt(referenceLinkView, 0), innerText);
@@ -293,6 +298,11 @@ 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 ) {
+ return storedTitle ? { href, storedTitle } : { href };
+}
+
/** The text inside `element` and its descendants. */
function getText( element: ViewElement ): string {
let text = '';