discourse/app/assets/javascripts/pretty-text/inline-oneboxer.js.es6.erb

35 lines
940 B
Plaintext
Raw Normal View History

let _cache = {};
export const INLINE_ONEBOX_LOADING_CSS_CLASS =
"<%= CookedPostProcessor::INLINE_ONEBOX_LOADING_CSS_CLASS %>";
export const INLINE_ONEBOX_CSS_CLASS =
"<%= CookedPostProcessor::INLINE_ONEBOX_CSS_CLASS %>";
export function applyInlineOneboxes(inline, ajax) {
Object.keys(inline).forEach(url => {
// cache a blank locally, so we never trigger a lookup
_cache[url] = {};
});
return ajax("/inline-onebox", {
2018-06-15 23:03:24 +08:00
data: { urls: Object.keys(inline) }
}).then(result => {
2018-06-15 23:03:24 +08:00
result["inline-oneboxes"].forEach(onebox => {
if (onebox.title) {
_cache[onebox.url] = onebox;
let links = inline[onebox.url] || [];
links.forEach(link => {
$(link).text(onebox.title)
.addClass(INLINE_ONEBOX_CSS_CLASS)
.removeClass(INLINE_ONEBOX_LOADING_CSS_CLASS);
});
}
});
});
2018-06-15 23:03:24 +08:00
}
export function cachedInlineOnebox(url) {
return _cache[url];
}