diff --git a/src/edit/moz-section-finder.js b/src/edit/moz-section-finder.js index 92f58703ae..a0ade0d982 100644 --- a/src/edit/moz-section-finder.js +++ b/src/edit/moz-section-finder.js @@ -1,5 +1,5 @@ -import {deepEqual} from '@/js/util'; import {CodeMirror} from '@/cm'; +import {deepEqual} from '@/js/util'; import {trimCommentLabel} from './util'; export default function MozSectionFinder(cm) { @@ -17,6 +17,12 @@ export default function MozSectionFinder(cm) { const minPos = (a, b) => cmpPos(a, b) < 0 ? a : b; const maxPos = (a, b) => cmpPos(a, b) > 0 ? a : b; const keptAlive = new Map(); + const state = /** @namespace MozSectionCmState */ { + /** @type {Set} */ + listeners: new Set(), + /** @type {MozSection[]} */ + sections: [], + }; /** @type {CodeMirror.Pos} */ let updFrom; /** @type {CodeMirror.Pos} */ @@ -32,9 +38,7 @@ export default function MozSectionFinder(cm) { 'valueEnd', 'sticky', // added by TextMarker::find() ], - get sections() { - return getState().sections; - }, + sections: state.sections, keepAliveFor(id, ms) { let data = keptAlive.get(id); if (data) { @@ -49,7 +53,7 @@ export default function MozSectionFinder(cm) { }, on(fn) { - const {listeners} = getState(); + const {listeners} = state; const needsInit = !listeners.size; listeners.add(fn); if (needsInit) { @@ -59,7 +63,7 @@ export default function MozSectionFinder(cm) { }, off(fn) { - const {listeners, sections} = getState(); + const {listeners, sections} = state; if (listeners.size) { listeners.delete(fn); if (!listeners.size) { @@ -76,24 +80,10 @@ export default function MozSectionFinder(cm) { /** @param {MozSection} [section] */ updatePositions(section) { - (section ? [section] : getState().sections).forEach(setPositionFromMark); + (section ? [section] : state.sections).forEach(setPositionFromMark); }, }; - /** @returns {MozSectionCmState} */ - function getState() { - let state = cm.state[KEY]; - if (!state) { - state = cm.state[KEY] = /** @namespace MozSectionCmState */ { - /** @type {Set} */ - listeners: new Set(), - /** @type {MozSection[]} */ - sections: [], - }; - } - return state; - } - function onCmChanges(_cm, changes) { if (!updFrom) updFrom = {line: Infinity, ch: 0}; if (!updTo) updTo = {line: -1, ch: 0}; @@ -109,7 +99,7 @@ export default function MozSectionFinder(cm) { } function update() { - const {sections, listeners} = getState(); + const {sections, listeners} = state; // Cloning to avoid breaking the internals of CodeMirror let from = updFrom ? {line: updFrom.line, ch: updFrom.ch} : {line: 0, ch: 0}; let to = updTo ? {line: updTo.line, ch: updTo.ch} : {line: cm.doc.size, ch: 0}; diff --git a/src/edit/source-editor.js b/src/edit/source-editor.js index 94e4de594a..fc4d28800a 100644 --- a/src/edit/source-editor.js +++ b/src/edit/source-editor.js @@ -44,6 +44,9 @@ export default function SourceEditor() { editor.viewTo = si.viewTo; sectionFinder = MozSectionFinder(me); sectionWidget = MozSectionWidget(me, sectionFinder); + editor.sections = sectionFinder.sections; + prefs.subscribe('editor.toc.expanded', + (k, val) => sectionFinder.onOff(editor.updateToc, val), true); prefs.subscribe('editor.appliesToLineWidget', (k, val) => sectionWidget.toggle(val), true); Object.assign(me.curOp, si.scroll); @@ -69,12 +72,9 @@ export default function SourceEditor() { updateMeta(); // Subsribing outside of finishInit() because it uses `cm` that's still not initialized prefs.subscribe('editor.linter', updateLinterSwitch, true); - prefs.subscribe('editor.toc.expanded', - (k, val) => sectionFinder.onOff(editor.updateToc, val), true); /** @namespace Editor */ Object.assign(editor, { - sections: sectionFinder.sections, replaceStyle, updateLinterSwitch, updateLivePreview,