diff --git a/edit/edit.js b/edit/edit.js index 54c1f85f..dfd83c13 100644 --- a/edit/edit.js +++ b/edit/edit.js @@ -300,16 +300,9 @@ lazyInit(); function updateToc(added = editor.sections) { const {sections} = editor; const first = sections.indexOf(added[0]); - let el = elToc.children[first]; - if (added.focus) { - const cls = 'current'; - const old = $('.' + cls, elToc); - if (old && old !== el) old.classList.remove(cls); - el.classList.add(cls); - return; - } - if (first >= 0) { - for (let i = first; i < sections.length; i++) { + const elFirst = elToc.children[first]; + if (first >= 0 && (!added.focus || !elFirst)) { + for (let el = elFirst, i = first; i < sections.length; i++) { const entry = sections[i].tocEntry; if (!deepEqual(entry, toc[i])) { if (!el) el = elToc.appendChild($create('li', {tabIndex: 0})); @@ -328,6 +321,13 @@ lazyInit(); elToc.lastElementChild.remove(); toc.length--; } + if (added.focus) { + const cls = 'current'; + const old = $('.' + cls, elToc); + const el = elFirst || elToc.children[first]; + if (old && old !== el) old.classList.remove(cls); + el.classList.add(cls); + } } })();