FIX: preview up to 'max_oneboxes_per_post' oneboxes

We were counting all the oneboxes in the DOM instead of just the ones in the preview.

Also refactored the logic to count up to 'max_oneboxes_per_post` instead of down to 0.
That also ensured we don't load 11 oneboxes when the setting is limiting to 10.
This commit is contained in:
Régis Hanol
2019-09-17 23:25:15 +02:00
parent 3debdc8131
commit bb127b8140

View File

@@ -1026,39 +1026,37 @@ export default Ember.Component.extend({
Ember.run.debounce( Ember.run.debounce(
this, this,
() => { () => {
const inlineOneboxes = {};
const oneboxes = {}; const oneboxes = {};
const inlineOneboxes = {};
let oneboxLeft = // Oneboxes = `a.onebox` -> `a.onebox-loading` -> `aside.onebox`
this.siteSettings.max_oneboxes_per_post - // Inline Oneboxes = `a.inline-onebox-loading` -> `a.inline-onebox`
$(
`aside.onebox, a.${INLINE_ONEBOX_CSS_CLASS}, a.${LOADING_ONEBOX_CSS_CLASS}` let loadedOneboxes = $preview.find(
).length; `aside.onebox, a.${LOADING_ONEBOX_CSS_CLASS}, a.${INLINE_ONEBOX_CSS_CLASS}`
).length;
$preview $preview
.find(`a.${INLINE_ONEBOX_LOADING_CSS_CLASS}, a.onebox`) .find(`a.onebox, a.${INLINE_ONEBOX_LOADING_CSS_CLASS}`)
.each((_index, link) => { .each((_, link) => {
const $link = $(link); const $link = $(link);
const text = $link.text(); const text = $link.text();
const isInline = const isInline =
$link.attr("class") === INLINE_ONEBOX_LOADING_CSS_CLASS; $link.attr("class") === INLINE_ONEBOX_LOADING_CSS_CLASS;
const m = isInline ? inlineOneboxes : oneboxes;
const map = isInline ? inlineOneboxes : oneboxes; if (loadedOneboxes < this.siteSettings.max_oneboxes_per_post) {
if (m[text] === undefined) {
if (oneboxLeft <= 0) { m[text] = [];
if (map[text] !== undefined) { loadedOneboxes++;
map[text].push(link); }
m[text].push(link);
} else {
if (m[text] !== undefined) {
m[text].push(link);
} else if (isInline) { } else if (isInline) {
$link.removeClass(INLINE_ONEBOX_LOADING_CSS_CLASS); $link.removeClass(INLINE_ONEBOX_LOADING_CSS_CLASS);
} }
} else {
if (!map[text]) {
map[text] = [];
oneboxLeft--;
}
map[text].push(link);
} }
}); });
@@ -1072,6 +1070,7 @@ export default Ember.Component.extend({
}, },
450 450
); );
// Short upload urls need resolution // Short upload urls need resolution
resolveAllShortUrls(ajax); resolveAllShortUrls(ajax);