2019-06-11 09:13:39 +08:00
|
|
|
import {
|
|
|
|
INLINE_ONEBOX_LOADING_CSS_CLASS,
|
|
|
|
INLINE_ONEBOX_CSS_CLASS
|
|
|
|
} from "pretty-text/context/inline-onebox-css-classes";
|
2017-07-20 03:08:54 +08:00
|
|
|
|
2019-06-11 09:13:39 +08:00
|
|
|
const _cache = {};
|
2018-11-27 16:00:31 +08:00
|
|
|
|
2020-02-12 18:11:28 +08:00
|
|
|
export function applyInlineOneboxes(inline, ajax, opts) {
|
|
|
|
opts = opts || {};
|
|
|
|
|
2018-01-30 05:39:41 +08:00
|
|
|
Object.keys(inline).forEach(url => {
|
|
|
|
// cache a blank locally, so we never trigger a lookup
|
|
|
|
_cache[url] = {};
|
|
|
|
});
|
|
|
|
|
2017-07-20 03:08:54 +08:00
|
|
|
return ajax("/inline-onebox", {
|
2020-02-12 18:11:28 +08:00
|
|
|
data: {
|
|
|
|
urls: Object.keys(inline),
|
|
|
|
category_id: opts.categoryId,
|
|
|
|
topic_id: opts.topicId
|
|
|
|
}
|
2017-07-20 03:08:54 +08:00
|
|
|
}).then(result => {
|
|
|
|
result["inline-oneboxes"].forEach(onebox => {
|
2018-11-13 04:14:20 +08:00
|
|
|
if (onebox.title) {
|
|
|
|
_cache[onebox.url] = onebox;
|
|
|
|
let links = inline[onebox.url] || [];
|
|
|
|
links.forEach(link => {
|
2019-06-11 09:13:39 +08:00
|
|
|
$(link)
|
|
|
|
.text(onebox.title)
|
2018-11-27 16:00:31 +08:00
|
|
|
.addClass(INLINE_ONEBOX_CSS_CLASS)
|
|
|
|
.removeClass(INLINE_ONEBOX_LOADING_CSS_CLASS);
|
2018-11-13 04:14:20 +08:00
|
|
|
});
|
|
|
|
}
|
2017-07-20 03:08:54 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cachedInlineOnebox(url) {
|
|
|
|
return _cache[url];
|
|
|
|
}
|
2019-06-11 09:13:39 +08:00
|
|
|
|
|
|
|
export function applyCachedInlineOnebox(url, onebox) {
|
|
|
|
return (_cache[url] = onebox);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteCachedInlineOnebox(url) {
|
|
|
|
return delete _cache[url];
|
|
|
|
}
|