Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 0 additions & 6 deletions app/assets/javascripts/discourse-loader.js
Expand Up @@ -31,12 +31,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;
Expand Down
Expand Up @@ -7,7 +7,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";

Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/pretty-text-bundle.js
Expand Up @@ -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
@@ -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() {
Expand Down
@@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions 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");
}
31 changes: 14 additions & 17 deletions 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) {
Expand Down Expand Up @@ -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));
},
Expand All @@ -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;
}
}
)
Expand Down Expand Up @@ -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");
}
3 changes: 1 addition & 2 deletions 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) {
Expand Down Expand Up @@ -38,8 +39,6 @@ export function resetCache() {
_cache = {};
}

export const ATTACHMENT_CSS_CLASS = "attachment";

function _loadCachedShortUrls($uploads) {
$uploads.each((idx, upload) => {
const $upload = $(upload);
Expand Down