Skip to content

Commit

Permalink
show # of sections in compact editor
Browse files Browse the repository at this point in the history
+ simplify state in finder
  • Loading branch information
tophf committed Jan 11, 2025
1 parent 2e50da4 commit 85f45ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
34 changes: 12 additions & 22 deletions src/edit/moz-section-finder.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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<function>} */
listeners: new Set(),
/** @type {MozSection[]} */
sections: [],
};
/** @type {CodeMirror.Pos} */
let updFrom;
/** @type {CodeMirror.Pos} */
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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<function>} */
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};
Expand All @@ -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};
Expand Down
6 changes: 3 additions & 3 deletions src/edit/source-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down

0 comments on commit 85f45ae

Please sign in to comment.