discourse/app/assets/javascripts/pretty-text/inline-oneboxer.js.es6.erb
Guo Xiang Tan a1e77aa2ed
FEATURE: Reimplement SiteSetting.max_oneboxes_per_post. (#6668)
Previously, the site setting was only effective on the client side of
things. Once the site setting was been reached, all oneboxes are not
rendered. This commit changes it such that the site setting is respected
both on the client and server side. The first N oneboxes are rendered and
once the limit has been reached, subsequent oneboxes will not be
rendered.
2018-11-27 16:00:31 +08:00

35 lines
940 B
Plaintext

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", {
data: { urls: Object.keys(inline) }
}).then(result => {
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);
});
}
});
});
}
export function cachedInlineOnebox(url) {
return _cache[url];
}