Skip to content

A platform for community discussion. Free, open, simple.

DEV: Remove pretty_text import hack & still work server-side (#8266)

Mark VanLandingham11f50eee3bb3
diff --git a/app/assets/javascripts/discourse-loader.js b/app/assets/javascripts/discourse-loader.js
index 59c3f4b62be..11bdb9d2fd4 100644
--- a/app/assets/javascripts/discourse-loader.js
+++ b/app/assets/javascripts/discourse-loader.js
@@ -68,12 +68,6 @@ var define, requirejs;
inject: Ember.inject.service
}
};
- } else if (typeof __PRETTY_TEXT !== "undefined") {
- // This is a hack because our server side code includes the pretty_text bundle
- // which relies on ember now that we're using modules properly.
- // The proper fix would be to move the upload urls code out of the pretty text
- // bundle and remove this code. It should never be called;
- EMBER_MODULES["@ember/runloop"] = {};
}
var _isArray;
diff --git a/app/assets/javascripts/discourse/components/composer-title.js.es6 b/app/assets/javascripts/discourse/components/composer-title.js.es6
index 907a4569db3..556b24bda86 100644
--- a/app/assets/javascripts/discourse/components/composer-title.js.es6
+++ b/app/assets/javascripts/discourse/components/composer-title.js.es6
@@ -8,7 +8,8 @@ import {
observes
} from "ember-addons/ember-computed-decorators";
import InputValidation from "discourse/models/input-validation";
-import { load, lookupCache } from "pretty-text/oneboxer";
+import { load } from "pretty-text/oneboxer";
+import { lookupCache } from "pretty-text/oneboxer-cache";
import { ajax } from "discourse/lib/ajax";
import afterTransition from "discourse/lib/after-transition";
diff --git a/app/assets/javascripts/pretty-text-bundle.js b/app/assets/javascripts/pretty-text-bundle.js
index 3887f90031d..c964a71cdaf 100644
--- a/app/assets/javascripts/pretty-text-bundle.js
+++ b/app/assets/javascripts/pretty-text-bundle.js
@@ -10,6 +10,7 @@
//= require ./pretty-text/white-lister
//= require ./pretty-text/sanitizer
//= require ./pretty-text/oneboxer
+//= require ./pretty-text/oneboxer-cache
//= require ./pretty-text/context/inline-onebox-css-classes
//= require ./pretty-text/inline-oneboxer
//= require ./pretty-text/upload-short-url
diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown-it.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown-it.js.es6
index 116eadb316f..7bf824de7f6 100644
--- a/app/assets/javascripts/pretty-text/engines/discourse-markdown-it.js.es6
+++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown-it.js.es6
@@ -1,7 +1,8 @@
import { default as WhiteLister } from "pretty-text/white-lister";
import { sanitize } from "pretty-text/sanitizer";
import guid from "pretty-text/guid";
-import { ATTACHMENT_CSS_CLASS } from "pretty-text/upload-short-url";
+
+export const ATTACHMENT_CSS_CLASS = "attachment";
function deprecate(feature, name) {
return function() {
diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/onebox.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/onebox.js.es6
index ddd4a1b1781..3203110c946 100644
--- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/onebox.js.es6
+++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/onebox.js.es6
@@ -1,4 +1,4 @@
-import { lookupCache } from "pretty-text/oneboxer";
+import { lookupCache } from "pretty-text/oneboxer-cache";
import { cachedInlineOnebox } from "pretty-text/inline-oneboxer";
import {
diff --git a/app/assets/javascripts/pretty-text/oneboxer-cache.js.es6 b/app/assets/javascripts/pretty-text/oneboxer-cache.js.es6
new file mode 100644
index 00000000000..c9a98165ce7
--- /dev/null
+++ b/app/assets/javascripts/pretty-text/oneboxer-cache.js.es6
@@ -0,0 +1,29 @@
+export let localCache = {};
+export let failedCache = {};
+
+// Sometimes jQuery will return URLs with trailing slashes when the
+// `href` didn't have them.
+export function resetLocalCache() {
+ localCache = {};
+}
+
+export function resetFailedCache() {
+ failedCache = {};
+}
+
+export function setLocalCache(key, value) {
+ localCache[key] = value;
+}
+
+export function setFailedCache(key, value) {
+ failedCache[key] = value;
+}
+
+export function normalize(url) {
+ return url.replace(/\/$/, "");
+}
+
+export function lookupCache(url) {
+ const cached = localCache[normalize(url)];
+ return cached && cached.prop("outerHTML");
+}
diff --git a/app/assets/javascripts/pretty-text/oneboxer.js.es6 b/app/assets/javascripts/pretty-text/oneboxer.js.es6
index a500274a777..cb7d94b641f 100644
--- a/app/assets/javascripts/pretty-text/oneboxer.js.es6
+++ b/app/assets/javascripts/pretty-text/oneboxer.js.es6
@@ -1,15 +1,23 @@
import { later } from "@ember/runloop";
+import {
+ localCache,
+ failedCache,
+ setLocalCache,
+ setFailedCache,
+ resetLocalCache,
+ resetFailedCache,
+ normalize
+} from "pretty-text/oneboxer-cache";
+
let timeout;
const loadingQueue = [];
-let localCache = {};
-let failedCache = {};
export const LOADING_ONEBOX_CSS_CLASS = "loading-onebox";
export function resetCache() {
loadingQueue.clear();
- localCache = {};
- failedCache = {};
+ resetLocalCache();
+ resetFailedCache();
}
function resolveSize(img) {
@@ -68,7 +76,7 @@ function loadNext(ajax) {
.then(
html => {
let $html = $(html);
- localCache[normalize(url)] = $html;
+ setLocalCache(normalize(url), $html);
$elem.replaceWith($html);
applySquareGenericOnebox($html, normalize(url));
},
@@ -78,7 +86,7 @@ function loadNext(ajax) {
removeLoading = false;
loadingQueue.unshift({ url, refresh, $elem, categoryId, topicId });
} else {
- failedCache[normalize(url)] = true;
+ setFailedCache[normalize(url)] = true;
}
}
)
@@ -133,14 +141,3 @@ export function load({
timeout = timeout || later(() => loadNext(ajax), 150);
}
}
-
-// Sometimes jQuery will return URLs with trailing slashes when the
-// `href` didn't have them.
-function normalize(url) {
- return url.replace(/\/$/, "");
-}
-
-export function lookupCache(url) {
- const cached = localCache[normalize(url)];
- return cached && cached.prop("outerHTML");
-}
diff --git a/app/assets/javascripts/pretty-text/upload-short-url.js.es6 b/app/assets/javascripts/pretty-text/upload-short-url.js.es6
index 3a2f46805c5..916f899b6ac 100644
--- a/app/assets/javascripts/pretty-text/upload-short-url.js.es6
+++ b/app/assets/javascripts/pretty-text/upload-short-url.js.es6
@@ -1,4 +1,5 @@
import { debounce } from "@ember/runloop";
+import { ATTACHMENT_CSS_CLASS } from "./engines/discourse-markdown-it";
let _cache = {};
export function lookupCachedUploadUrl(shortUrl) {
@@ -38,8 +39,6 @@ export function resetCache() {
_cache = {};
}
-export const ATTACHMENT_CSS_CLASS = "attachment";
-
function _loadCachedShortUrls($uploads) {
$uploads.each((idx, upload) => {
const $upload = $(upload);