A platform for community discussion. Free, open, simple.
FIX: Decorate composer preview oneboxes once they load (#44710)
Renato Atilio630b787ebded
diff --git a/frontend/discourse/app/components/composer-editor.gjs b/frontend/discourse/app/components/composer-editor.gjsindex bb0375a23bb..63301a4df37 100644--- a/frontend/discourse/app/components/composer-editor.gjs+++ b/frontend/discourse/app/components/composer-editor.gjs@@ -675,7 +675,9 @@ export default class ComposerEditor extends Component {this.get("composer.topic.id"),this.get("composer.category.id"),this.siteSettings.max_oneboxes_per_post,- refresh+ refresh,+ false,+ (onebox) => onebox && this.appEvents.trigger("composer:refresh-preview"));if (refresh && loaded > 0) {diff --git a/frontend/discourse/app/lib/load-oneboxes.js b/frontend/discourse/app/lib/load-oneboxes.jsindex 0c27a285302..914366ad992 100644--- a/frontend/discourse/app/lib/load-oneboxes.js+++ b/frontend/discourse/app/lib/load-oneboxes.js@@ -8,7 +8,8 @@ export function loadOneboxes(categoryId,maxOneboxes,refresh,- offline+ offline,+ onLoad) {const oneboxes = {};const inlineOneboxes = {};@@ -51,20 +52,22 @@ export function loadOneboxes(categoryId,refresh,offline,+ onLoad,});}if (Object.keys(inlineOneboxes).length > 0) {- _loadInlineOneboxes(inlineOneboxes, ajax, topicId, categoryId);+ _loadInlineOneboxes(inlineOneboxes, ajax, topicId, categoryId, onLoad);}return Object.keys(oneboxes).length + Object.keys(inlineOneboxes).length;}-function _loadInlineOneboxes(inline, ajax, topicId, categoryId) {+function _loadInlineOneboxes(inline, ajax, topicId, categoryId, onLoad) {applyInlineOneboxes(inline, ajax, {categoryId: topicId,topicId: categoryId,+ onLoad,});}@@ -75,6 +78,7 @@ function _loadOneboxes({categoryId,refresh,offline,+ onLoad,}) {Object.values(oneboxes).forEach((onebox) => {onebox.forEach((elem) => {@@ -85,6 +89,7 @@ function _loadOneboxes({topicId,refresh,offline,+ onResolve: onLoad,});});});diff --git a/frontend/discourse/app/ui-kit/d-editor.gjs b/frontend/discourse/app/ui-kit/d-editor.gjsindex 36c024ce8fd..8672e903f47 100644--- a/frontend/discourse/app/ui-kit/d-editor.gjs+++ b/frontend/discourse/app/ui-kit/d-editor.gjs@@ -439,10 +439,16 @@ export default class DEditor extends Component {this.appEvents.on("composer:replace-toolbar", replaceToolbar);this.appEvents.on("composer:reset-toolbar", this, "resetToolbar");+ this.appEvents.on("composer:refresh-preview", this, "_watchForChanges");return () => {this.appEvents.off("composer:replace-toolbar", replaceToolbar);this.appEvents.off("composer:reset-toolbar", this, "resetToolbar");+ this.appEvents.off(+ "composer:refresh-preview",+ this,+ "_watchForChanges"+ );this.appEvents.off("composer:insert-block",diff --git a/frontend/discourse/tests/acceptance/composer-onebox-test.js b/frontend/discourse/tests/acceptance/composer-onebox-test.jsindex 557a58633bb..8a53c9bb9e0 100644--- a/frontend/discourse/tests/acceptance/composer-onebox-test.js+++ b/frontend/discourse/tests/acceptance/composer-onebox-test.js@@ -1,5 +1,7 @@import { click, fillIn, visit } from "@ember/test-helpers";import { test } from "qunit";+import { withPluginApi } from "discourse/lib/plugin-api";+import pretender from "discourse/tests/helpers/create-pretender";import { acceptance } from "discourse/tests/helpers/qunit-helpers";acceptance("Composer - Onebox", function (needs) {@@ -30,12 +32,49 @@ http://www.example.com/has-title.htmlassert.dom(".d-editor-preview").exists();assert.dom(".d-editor-preview").hasHtml(`-<p><aside class=\"onebox\"><article class=\"onebox-body\"><h3><a href=\"http://www.example.com/article.html\" tabindex=\"-1\">An interesting article</a></h3></article></aside><br>-This is another test <a href=\"http://www.example.com/has-title.html\" class=\"inline-onebox\" tabindex=\"-1\">This is a great title</a></p>+<p></p><aside class=\"onebox\"><article class=\"onebox-body\"><h3><a href=\"http://www.example.com/article.html\" tabindex=\"-1\">An interesting article</a></h3></article></aside><br>+This is another test <a href=\"http://www.example.com/has-title.html\" class=\"inline-onebox\" tabindex=\"-1\">This is a great title</a><p></p><p><a href=\"http://www.example.com/no-title.html\" class=\"onebox\" target=\"_blank\" tabindex=\"-1\">http://www.example.com/no-title.html</a></p><p>This is another test <a href=\"http://www.example.com/no-title.html\" class=\"\" tabindex=\"-1\">http://www.example.com/no-title.html</a><br>This is another test <a href=\"http://www.example.com/has-title.html\" class=\"inline-onebox\" tabindex=\"-1\">This is a great title</a></p>-<p><aside class=\"onebox\"><article class=\"onebox-body\"><h3><a href=\"http://www.example.com/article.html\" tabindex=\"-1\">An interesting article</a></h3></article></aside></p>`);+<p></p><aside class=\"onebox\"><article class=\"onebox-body\"><h3><a href=\"http://www.example.com/article.html\" tabindex=\"-1\">An interesting article</a></h3></article></aside><p></p>`);+ });++ test("decorates oneboxes once they load", async function (assert) {+ withPluginApi((api) => {+ api.decorateCookedElement((element) =>+ element+ .querySelectorAll("aside.onebox")+ .forEach((onebox) => onebox.classList.add("decorated"))+ );+ });++ await visit("/t/internationalization-localization/280");+ await click("#topic-footer-buttons .btn.create");+ await fillIn(".d-editor-input", "http://www.example.com/has-title.html");++ assert.dom(".d-editor-preview aside.onebox.decorated").exists();+ });++ test("keeps inline onebox titles that load after a onebox", async function (assert) {+ pretender.get(+ "/inline-onebox",+ () => [+ 200,+ { "Content-Type": "application/html" },+ '{"inline-oneboxes":[{"url":"http://www.example.com/late-title.html","title":"A late title"}]}',+ ],+ 500+ );++ await visit("/t/internationalization-localization/280");+ await click("#topic-footer-buttons .btn.create");+ await fillIn(+ ".d-editor-input",+ "http://www.example.com/no-title.html\n\nSee http://www.example.com/late-title.html"+ );++ assert.dom(".d-editor-preview .inline-onebox").hasText("A late title");});});diff --git a/frontend/discourse/tests/unit/lib/oneboxer-test.js b/frontend/discourse/tests/unit/lib/oneboxer-test.jsindex 29dd1d6fa49..eb2dd3d4fcf 100644--- a/frontend/discourse/tests/unit/lib/oneboxer-test.js+++ b/frontend/discourse/tests/unit/lib/oneboxer-test.js@@ -1,3 +1,4 @@+import { settled } from "@ember/test-helpers";import { setupTest } from "ember-qunit";import { load } from "pretty-text/oneboxer";import { failedCache, localCache } from "pretty-text/oneboxer-cache";@@ -52,4 +53,21 @@ module("Unit | Utility | oneboxer", function (hooks) {"returns the html from the cache");});++ test("load - does not refetch a url an earlier queued entry resolved", async function (assert) {+ let requests = 0;+ const countingAjax = (...args) => {+ requests++;+ return ajax(...args);+ };++ for (let i = 0; i < 2; i++) {+ const element = document.createElement("A");+ element.setAttribute("href", "http://somegoodurl.com");+ load({ elem: element, refresh: false, ajax: countingAjax });+ }+ await settled();++ assert.strictEqual(requests, 1);+ });});diff --git a/frontend/pretty-text/addon/inline-oneboxer.js b/frontend/pretty-text/addon/inline-oneboxer.jsindex 1d59b97252a..7337b5494cf 100644--- a/frontend/pretty-text/addon/inline-oneboxer.js+++ b/frontend/pretty-text/addon/inline-oneboxer.js@@ -28,21 +28,23 @@ export async function applyInlineOneboxes(inline, ajax, opts) {topic_id: opts.topicId,},});- result["inline-oneboxes"].forEach((onebox) => {- if (onebox.title) {- _cache[onebox.url] = onebox;+ const loaded = result["inline-oneboxes"].filter((onebox) => onebox.title);+ loaded.forEach((onebox) => {+ _cache[onebox.url] = onebox;- let links = inline[onebox.url] || [];- links.forEach((link) => {- link.innerText = onebox.title;- link.classList.add("inline-onebox");- if (onebox.css_class) {- link.classList.add(onebox.css_class);- }- link.classList.remove("inline-onebox-loading");- });- }+ let links = inline[onebox.url] || [];+ links.forEach((link) => {+ link.innerText = onebox.title;+ link.classList.add("inline-onebox");+ if (onebox.css_class) {+ link.classList.add(onebox.css_class);+ }+ link.classList.remove("inline-onebox-loading");+ });});+ if (loaded.length) {+ opts.onLoad?.(loaded);+ }} catch (err) {const rateLimited = (err.jqXHR ?? err).status === TOO_MANY_REQUESTS;diff --git a/frontend/pretty-text/addon/oneboxer.js b/frontend/pretty-text/addon/oneboxer.jsindex ed7a2fca23a..a859b24df50 100644--- a/frontend/pretty-text/addon/oneboxer.js+++ b/frontend/pretty-text/addon/oneboxer.js@@ -65,6 +65,16 @@ function loadNext(ajax) {const { url, refresh, elem, categoryId, topicId, onResolve } =loadingQueue.shift();+ // an earlier entry for the same url may have resolved while this one waited+ const cached = !refresh && lookupCache(url);+ if (cached) {+ const node = domFromString(cached)[0];+ onResolve?.(cached);+ elem.replaceWith(node);+ applySquareGenericOnebox(node);+ return loadNext(ajax);+ }+// Retrieve the oneboxreturn ajax("/onebox", {dataType: "html",diff --git a/plugins/discourse-events/test/javascripts/acceptance/event-onebox-test.js b/plugins/discourse-events/test/javascripts/acceptance/event-onebox-test.jsindex 185d737f454..7b584b615df 100644--- a/plugins/discourse-events/test/javascripts/acceptance/event-onebox-test.js+++ b/plugins/discourse-events/test/javascripts/acceptance/event-onebox-test.js@@ -63,9 +63,6 @@ acceptance("Event onebox", function (needs) {await visit("/t/internationalization-localization/280");await click("#topic-footer-buttons .btn.create");await fillIn(".d-editor-input", `${url}\n\nEditable text`);- // the decorator sees the onebox only after the next preview update- await waitFor(".d-editor-preview aside.quote");- await fillIn(".d-editor-input", `${url}\n\nEditable text!`);await waitFor(".d-editor-preview .discourse-post-event-widget");assert@@ -97,7 +94,7 @@ acceptance("Event onebox", function (needs) {assert.dom(".d-editor-input").hasValue(- `${url}\n\nEditable text!`,+ `${url}\n\nEditable text`,"switching modes preserves the source URL");});