Skip to content

Build your personal knowledge base with Trilium Notes

link previews: address the valid PR review findings

Elian Dorane9d9e0cbaeaf
diff --git a/packages/ckeditor5/src/augmentation.ts b/packages/ckeditor5/src/augmentation.ts
index 263503cf4f..a8f968f37d 100644
--- a/packages/ckeditor5/src/augmentation.ts
+++ b/packages/ckeditor5/src/augmentation.ts
@@ -26,6 +26,11 @@ declare global {
loadReferenceLinkTitle($el: JQuery<HTMLElement>, href: string): Promise<void>;
createNoteForReferenceLink(title: string): Promise<string>;
loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>, boxSize?: string): void;
+ /**
+ * Reads a page's preview metadata through the host. Never rejects: any failure — network
+ * error, HTTP error, unparseable page — resolves as `{ unresolved: true }` with
+ * hostname-derived placeholders, so callers branch on `unresolved` instead of catching.
+ */
fetchLinkMetadata(url: string): Promise<LinkEmbedMetadata>;
detectEmbedType(url: string): string;
renderLinkEmbed(container: HTMLElement, metadata: LinkEmbedMetadata, editable?: boolean): void;
diff --git a/packages/ckeditor5/src/plugins/link_embed/link_embed_autodetect.spec.ts b/packages/ckeditor5/src/plugins/link_embed/link_embed_autodetect.spec.ts
index 55c817863b..69c81282c4 100644
--- a/packages/ckeditor5/src/plugins/link_embed/link_embed_autodetect.spec.ts
+++ b/packages/ckeditor5/src/plugins/link_embed/link_embed_autodetect.spec.ts
@@ -187,6 +187,32 @@ describe("AutoLinkToMention", () => {
const caretBlock = editor.model.document.selection.getFirstPosition()?.parent;
expect(caretBlock?.is("element", "paragraph")).toBe(true);
});
+
+ it("does not drag the caret back when an inline mention lands mid-typing", async () => {
+ const { resolveFetch } = useDeferredFetch(fetchLinkMetadata);
+ // Text beside the URL makes this a mention; the caret has already moved to the next line.
+ autoLinkIn(editor, `<paragraph>see ${PLAIN_URL}</paragraph><paragraph>[]</paragraph>`, PLAIN_URL);
+
+ // The user keeps typing on the second line while the metadata is still being fetched.
+ editor.model.change((writer) => {
+ const caret = editor.model.document.selection.getFirstPosition();
+ if (!caret) {
+ throw new Error("Expected a caret position.");
+ }
+ writer.insertText("hello", caret);
+ });
+
+ resolveFetch({ ...META, url: PLAIN_URL });
+ await flushFetch();
+
+ // The mention replaced the URL up on the first line...
+ expect(findElement(editor, "linkMention")?.getAttribute("url")).toBe(PLAIN_URL);
+ expect(editor.getData()).toContain("<p>hello</p>");
+
+ // ...while the caret stayed down where the user was typing.
+ const caretBlock = editor.model.document.selection.getFirstPosition()?.parent;
+ expect(caretBlock).toBe(editor.model.document.getRoot()?.getChild(1));
+ });
});
it("ignores a labeled link whose text differs from the href", async () => {
diff --git a/packages/ckeditor5/src/plugins/link_embed/link_embed_autodetect.ts b/packages/ckeditor5/src/plugins/link_embed/link_embed_autodetect.ts
index aede20ff74..62835fa20a 100644
--- a/packages/ckeditor5/src/plugins/link_embed/link_embed_autodetect.ts
+++ b/packages/ckeditor5/src/plugins/link_embed/link_embed_autodetect.ts
@@ -146,10 +146,11 @@ export default class AutoLinkToMention extends Plugin {
};
if (kind === 'mention') {
+ // The URL's own range is passed to insertContent rather than selected
+ // first: the fetch was async and the caret may be words away by now, so
+ // the replacement must not drag it back to the mention.
const urlRange = writer.createRangeOn(item.item);
- writer.setSelection(urlRange);
- editor.model.deleteContent(editor.model.document.selection);
- editor.model.insertContent(writer.createElement('linkMention', attributes));
+ editor.model.insertContent(writer.createElement('linkMention', attributes), urlRange);
return;
}
diff --git a/packages/ckeditor5/src/plugins/link_embed/link_embed_form.spec.ts b/packages/ckeditor5/src/plugins/link_embed/link_embed_form.spec.ts
index 7969e02ba0..83c26b346c 100644
--- a/packages/ckeditor5/src/plugins/link_embed/link_embed_form.spec.ts
+++ b/packages/ckeditor5/src/plugins/link_embed/link_embed_form.spec.ts
@@ -246,6 +246,22 @@ describe("LinkEmbedFormView", () => {
expect(editor.getData()).toBe(before);
});
+ it("follows the URL again after a dismissal, forgetting the earlier mode pick", () => {
+ const form = openForm();
+ typeUrl(form, "https://example.com/article");
+ form.modeDropdownView.fire("execute", { source: { _displayMode: "inline" } });
+ form.mode = "inline";
+
+ // Dismiss without inserting: the pick was for that URL only.
+ form.keystrokes.press({ keyCode: 27, preventDefault: () => {}, stopPropagation: () => {} } as unknown as KeyboardEvent);
+ expect(isInBalloon(form)).toBe(false);
+
+ // Reopened with an embeddable URL, the mode follows the URL again instead of the old pick.
+ openForm();
+ typeUrl(form, "https://youtube.com/watch?v=abc12345678");
+ expect(form.mode).toBe("embed");
+ });
+
it("closes when the user clicks away from it", () => {
const form = openForm();
expect(isInBalloon(form)).toBe(true);
diff --git a/packages/ckeditor5/src/plugins/link_embed/link_embed_ui.ts b/packages/ckeditor5/src/plugins/link_embed/link_embed_ui.ts
index 774dc52673..d63b416f36 100644
--- a/packages/ckeditor5/src/plugins/link_embed/link_embed_ui.ts
+++ b/packages/ckeditor5/src/plugins/link_embed/link_embed_ui.ts
@@ -79,6 +79,9 @@ export default class LinkEmbedUI extends Plugin {
if (!form || !balloon.hasView(form)) return;
balloon.remove(form);
+ // Whatever mode the user picked was for the URL of this session — however it ended, the
+ // next form opening follows its own URL again.
+ this._modePickedByUser = false;
this.editor.editing.view.focus();
}
@@ -145,7 +148,6 @@ export default class LinkEmbedUI extends Plugin {
form.isFetching = false;
}
- this._modePickedByUser = false;
this._hideForm();
}
}