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

* FIX: move attachment_css_class constant out of upload-short-url for discourse-markdown-it

* Use setTimeout instead of ember later

* WIP. Not sure if this worked.

* oneboxer cache in separate file

* Reset onebox cache still

* set functions for oneboxers cache
This commit is contained in:
Mark VanLandingham 2019-10-31 11:54:46 -05:00 committed by GitHub
parent 1f88ecf6d8
commit 11f50eee3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 50 additions and 28 deletions

View File

@ -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;

View File

@ -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";

View File

@ -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

View File

@ -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() {

View File

@ -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 {

View File

@ -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");
}

View File

@ -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");
}

View File

@ -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);