From 293467a37ae33c5fd8fd40bbfc05bffa578b90ed Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 5 Jun 2020 17:15:14 +0100 Subject: [PATCH] REFACTOR: Remove jquery usage from resolveAllShortUrls, and fix debounce - This function now requires an explicit scope. It will never run on the entire document. - Previously debounce was being used with an anonymous function, which means it was having no effect. --- .../discourse/app/components/cook-text.js | 2 +- .../pretty-text/addon/upload-short-url.js | 68 ++++++++++--------- test/javascripts/lib/upload-short-url-test.js | 12 ++-- 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/app/assets/javascripts/discourse/app/components/cook-text.js b/app/assets/javascripts/discourse/app/components/cook-text.js index f3ad48f5492..234d0906d6b 100644 --- a/app/assets/javascripts/discourse/app/components/cook-text.js +++ b/app/assets/javascripts/discourse/app/components/cook-text.js @@ -16,7 +16,7 @@ const CookText = Component.extend({ next(() => window .requireModule("pretty-text/upload-short-url") - .resolveAllShortUrls(ajax, this.siteSettings) + .resolveAllShortUrls(ajax, this.siteSettings, this.element) ); }); } diff --git a/app/assets/javascripts/pretty-text/addon/upload-short-url.js b/app/assets/javascripts/pretty-text/addon/upload-short-url.js index f37a5d4d4cb..5ccdc41417a 100644 --- a/app/assets/javascripts/pretty-text/addon/upload-short-url.js +++ b/app/assets/javascripts/pretty-text/addon/upload-short-url.js @@ -43,12 +43,14 @@ export function resetCache() { _cache = {}; } -function retrieveCachedUrl($upload, siteSettings, dataAttribute, callback) { - const cachedUpload = lookupCachedUploadUrl($upload.data(dataAttribute)); +function retrieveCachedUrl(upload, siteSettings, dataAttribute, callback) { + const cachedUpload = lookupCachedUploadUrl( + upload.getAttribute(`data-${dataAttribute}`) + ); const url = getAttributeBasedUrl(dataAttribute, cachedUpload, siteSettings); if (url) { - $upload.removeAttr(`data-${dataAttribute}`); + upload.removeAttribute(`data-${dataAttribute}`); if (url !== MISSING) { callback(url); } @@ -77,43 +79,40 @@ function getAttributeBasedUrl(dataAttribute, cachedUpload, siteSettings) { return cachedUpload.short_path; } -function _loadCachedShortUrls($uploads, siteSettings) { - $uploads.each((_idx, upload) => { - const $upload = $(upload); +function _loadCachedShortUrls(uploadElements, siteSettings) { + uploadElements.forEach(upload => { switch (upload.tagName) { case "A": - retrieveCachedUrl($upload, siteSettings, "orig-href", url => { - $upload.attr("href", url); + retrieveCachedUrl(upload, siteSettings, "orig-href", url => { + upload.href = url; }); break; case "IMG": - retrieveCachedUrl($upload, siteSettings, "orig-src", url => { - $upload.attr("src", url); + retrieveCachedUrl(upload, siteSettings, "orig-src", url => { + upload.src = url; }); break; case "SOURCE": // video/audio tag > source tag - retrieveCachedUrl($upload, siteSettings, "orig-src", url => { - $upload.attr("src", url); - + retrieveCachedUrl(upload, siteSettings, "orig-src", url => { if (url.startsWith(`//${window.location.host}`)) { let hostRegex = new RegExp("//" + window.location.host, "g"); url = url.replace(hostRegex, ""); } let fullUrl = window.location.origin + url; - $upload.attr("src", fullUrl); + upload.src = fullUrl; // this is necessary, otherwise because of the src change the // video/audio just doesn't bother loading! - let $parent = $upload.parent(); - $parent[0].load(); + upload.parentElement.load(); // set the url and text for the tag within the