Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/link: fix an issue

Adorian Doran144559215fe7
diff --git a/packages/ckeditor5/src/plugins/link_embed/link_embed_commands.spec.ts b/packages/ckeditor5/src/plugins/link_embed/link_embed_commands.spec.ts
index 1221dbd32c..bde4925b43 100644
--- a/packages/ckeditor5/src/plugins/link_embed/link_embed_commands.spec.ts
+++ b/packages/ckeditor5/src/plugins/link_embed/link_embed_commands.spec.ts
@@ -94,6 +94,43 @@ describe("LinkEmbed commands", () => {
expect(editor.model.document.selection.getSelectedElement()).toBe(card);
});
+ it("leaves the caret where the user moved it while the metadata loads", async () => {
+ const url = "https://example.com/article";
+ const { resolveFetch } = useDeferredFetch(fetchMetadata);
+ const selection = editor.model.document.selection;
+ const moveCaretToSecondParagraph = () => editor.model.change((writer) => {
+ const paragraph = editor.model.document.getRoot()?.getChild(1);
+ if (!paragraph?.is("element")) throw new Error("Expected a paragraph.");
+ writer.setSelection(paragraph, "end");
+ return paragraph;
+ });
+
+ setModelData(editor.model, "<paragraph>see []</paragraph><paragraph>elsewhere</paragraph>");
+ const inserted = editor.execute(LINK_EMBED_COMMAND, {
+ url, mode: "card"
+ }) as unknown as Promise<void>;
+ let elsewhere = moveCaretToSecondParagraph();
+ resolveFetch({ ...META, url });
+ await inserted;
+
+ expect(findElement(editor, "linkEmbed")).toBeDefined();
+ expect(selection.getFirstPosition()?.parent).toBe(elsewhere);
+ expect(selection.getFirstPosition()?.offset).toBe(9);
+
+ setModelData(editor.model, `<paragraph><$text linkHref="${url}">the []article</$text>`
+ + "</paragraph><paragraph>elsewhere</paragraph>");
+ const converted = editor.execute(CHANGE_LINK_DISPLAY_COMMAND, {
+ value: "inline"
+ }) as unknown as Promise<void>;
+ elsewhere = moveCaretToSecondParagraph();
+ resolveFetch({ ...META, url });
+ await converted;
+
+ expect(findElement(editor, "linkMention")).toBeDefined();
+ expect(selection.getFirstPosition()?.parent).toBe(elsewhere);
+ expect(selection.getFirstPosition()?.offset).toBe(9);
+ });
+
it("replaces the link the selection is in, which is the link being edited", async () => {
const url = "https://example.com/new";
setModelData(editor.model, '<paragraph>see '
diff --git a/packages/ckeditor5/src/plugins/link_embed/link_embed_commands.ts b/packages/ckeditor5/src/plugins/link_embed/link_embed_commands.ts
index aefbddcdfb..30171925a7 100644
--- a/packages/ckeditor5/src/plugins/link_embed/link_embed_commands.ts
+++ b/packages/ckeditor5/src/plugins/link_embed/link_embed_commands.ts
@@ -243,7 +243,8 @@ export class RemoveLinkEmbedCommand extends Command {
/**
* Replaces the link to `href` in `range` with a preview of the `mode` shape, once the page's
- * metadata arrives, and selects it. `title` and `description` replace the page's own.
+ * metadata arrives, and selects it unless the user has moved the selection in the meantime.
+ * `title` and `description` replace the page's own.
*
* The range is tracked as a live range while the metadata is fetched, so edits made meanwhile do
* not shift what gets replaced. Nothing is replaced when the link is no longer in the range by
@@ -255,38 +256,50 @@ async function replaceLinkWithPreview(
) {
const model = editor.model;
const liveRange = ModelLiveRange.fromRange(range);
- const metadata = await getLinkEmbedHost(editor).fetchMetadata(href);
- const currentRange = liveRange.toRange();
- liveRange.detach();
-
- model.change(writer => {
- let linkStillThere = false;
- for (const { item } of currentRange.getWalker()) {
- if (item.is('$textProxy') && item.getAttribute('linkHref') === href) {
- linkStillThere = true;
- break;
+ 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 metadata = await getLinkEmbedHost(editor).fetchMetadata(href);
+ const currentRange = liveRange.toRange();
+
+ model.change(writer => {
+ let linkStillThere = false;
+ for (const { item } of currentRange.getWalker()) {
+ if (item.is('$textProxy') && item.getAttribute('linkHref') === href) {
+ linkStillThere = true;
+ break;
+ }
}
- }
- if (!linkStillThere) return;
-
- const element = writer.createElement(mode === 'inline' ? 'linkMention' : 'linkEmbed', {
- url: metadata.url,
- // A card is an embed that declines to play: forcing "opengraph" is what tells the
- // renderer to draw the static card rather than the provider's player.
- embedType: mode === 'card' ? 'opengraph'
- : mode === 'embed' ? getLinkEmbedHost(editor).detectEmbedType(href)
- : metadata.embedType,
- title: title || metadata.title,
- description: description || metadata.description,
- favicon: metadata.favicon,
- siteName: metadata.siteName,
- image: metadata.image
- });
+ if (!linkStillThere) return;
+
+ const element = writer.createElement(mode === 'inline' ? 'linkMention' : 'linkEmbed', {
+ url: metadata.url,
+ // A card is an embed that declines to play: forcing "opengraph" is what tells the
+ // renderer to draw the static card rather than the provider's player.
+ embedType: mode === 'card' ? 'opengraph'
+ : mode === 'embed' ? getLinkEmbedHost(editor).detectEmbedType(href)
+ : metadata.embedType,
+ title: title || metadata.title,
+ description: description || metadata.description,
+ favicon: metadata.favicon,
+ siteName: metadata.siteName,
+ image: metadata.image
+ });
- model.insertContent(element, currentRange);
- // The widget toolbar, with its Display dropdown, takes over from the link balloon.
- writer.setSelection(element, 'on');
- });
+ model.insertContent(element, currentRange);
+ // The widget toolbar, with its Display dropdown, takes over from the link balloon.
+ if (!selectionMoved) {
+ writer.setSelection(element, 'on');
+ }
+ });
+ } finally {
+ selection.off('change:range', onSelectionChange);
+ liveRange.detach();
+ }
}
/**