Skip to content

Commit 3ae036f

Browse files
authored
fix(link-preview): ensure spinner is not shown indefinetely when no preview results are available (#289)
1 parent 2e6b217 commit 3ae036f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/rich-text/plugins/link-preview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function generateRecentPreviewDecorations(
150150
if (!n.isTextOnly && n.content) {
151151
// if the url is in the cache, insert the link preview
152152
linkPreviewDecorations.push(insertLinkPreview(n.pos, n.content));
153-
} else if (!n.content) {
153+
} else if (n.content === null) {
154154
// otherwise, add the loading styles
155155
// attach the node decorations to the text's parent node
156156
const resolved = doc.resolve(n.pos);

test/rich-text/plugins/link-preview.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,39 @@ describe("link-preview", () => {
6060
expect(oneboxDom[0].textContent).toBe("https://example.com");
6161
});
6262

63+
it("should attempt to retrieve a link preview but simply show the plain link when not available", async () => {
64+
const markdown = "https://example.com/not-preview\n";
65+
66+
// store the promise so we can control when it resolves
67+
let resolver: (value: Element | PromiseLike<Element>) => void;
68+
const promise = new Promise<Element>((resolve) => {
69+
resolver = resolve;
70+
});
71+
72+
const richEditorView = richView(markdown, {
73+
domainTest: /^.+$/, // any url
74+
renderer: () => promise,
75+
});
76+
77+
// check that the loading indicator is shown
78+
let loadingIndicator = richEditorView.dom.querySelectorAll(
79+
".js-link-preview-loading"
80+
);
81+
expect(loadingIndicator).toHaveLength(1);
82+
83+
resolver(undefined); // link preview not available
84+
await sleepAsync(0); // await next tick
85+
86+
// check that the loading indicator is no longer showing
87+
loadingIndicator = richEditorView.dom.querySelectorAll(
88+
".js-link-preview-loading"
89+
);
90+
expect(loadingIndicator).toHaveLength(0);
91+
92+
const link = richEditorView.dom.querySelector("a");
93+
expect(link.textContent).toBe("https://example.com/not-preview");
94+
});
95+
6396
it("should not add rich previews to links with additional text on the same line", () => {
6497
const markdown = "here is [some link](https://example.com)\n";
6598

0 commit comments

Comments
 (0)