A platform for community discussion. Free, open, simple.
FIX: Stop re-requesting pending inline oneboxes in the rich editor
Régis Hanolee429a3c6751
diff --git a/frontend/discourse/app/static/prosemirror/extensions/onebox.js b/frontend/discourse/app/static/prosemirror/extensions/onebox.jsindex 32557aa2eca..e5e26fda793 100644--- a/frontend/discourse/app/static/prosemirror/extensions/onebox.js+++ b/frontend/discourse/app/static/prosemirror/extensions/onebox.js@@ -371,6 +371,8 @@ const extension = {!pendingUrls[spec.oneboxType].has(spec.oneboxUrl));+ const inlineUrls = new Set();+for (const dec of decorations) {if (!dec.spec.forceRetry &&@@ -381,6 +383,10 @@ const extension = {pendingUrls[dec.spec.oneboxType].add(dec.spec.oneboxUrl);+ if (dec.spec.oneboxType === "inline") {+ inlineUrls.add(dec.spec.oneboxUrl);+ }+// Full onebox, one by oneif (dec.spec.oneboxType === "full") {const { oneboxUrl, forceRetry } = dec.spec;@@ -418,8 +424,7 @@ const extension = {}// Inline oneboxes, batched- if (pendingUrls.inline.size) {- const inlineUrls = pendingUrls.inline;+ if (inlineUrls.size) {loadInlineOneboxes(inlineUrls, getContext()).then((inlineOneboxes) => {for (const url of inlineUrls) {diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/onebox-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/onebox-test.jsindex 495453f078f..f1b58ccd0e1 100644--- a/frontend/discourse/tests/integration/components/prosemirror-editor/onebox-test.js+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/onebox-test.js@@ -10,6 +10,7 @@ import { setupRichEditor } from "discourse/tests/helpers/rich-editor-helper";// Mocked by create-pretender for both /inline-onebox and /onebox.const URL = "http://www.example.com/has-title.html";const OTHER_URL = "http://www.example.com/no-title.html";+const ONEBOX_SCAN_WAIT_MS = 300;function lastParagraphStart(doc) {let pos = null;@@ -21,6 +22,10 @@ function lastParagraphStart(doc) {return pos;}+function waitForOneboxScan() {+ return new Promise((resolve) => setTimeout(resolve, ONEBOX_SCAN_WAIT_MS));+}+function moveCursorTo(view, pos) {view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, pos))@@ -155,6 +160,40 @@ module(assert.strictEqual(toasts.activeToasts.length, 1);});+ test("renders an inline onebox when the cursor moves while it loads", async function (assert) {+ const { promise: gate, resolve: release } = Promise.withResolvers();+ pretender.get("/inline-onebox", async (request) => {+ await gate;+ return [+ 200,+ { "Content-Type": "application/json" },+ {+ "inline-oneboxes": request.queryParams.urls.map((url) => ({+ url,+ title: "Loaded title",+ })),+ },+ ];+ });++ const url = "http://www.example.com/loads-slowly.html";+ const [editor] = await setupRichEditor(assert, "see x here\n\nb");+ const { view } = editor;++ const tr = view.state.tr.insertText(url, 5, 6);+ tr.setSelection(TextSelection.atEnd(tr.doc));+ view.dispatch(tr);+ await waitForOneboxScan();++ moveCursorTo(view, 2);+ await waitForOneboxScan();++ release();+ await settled();++ assert.dom("a.inline-onebox").hasText("Loaded title");+ });+test("saves an inline onebox URL unescaped", async function (assert) {const url = "http://www.example.com/~user/has-title.html";const [editor] = await setupRichEditor(assert, "see x here");