Skip to content

Commit 85f45ae

Browse files
committed
show # of sections in compact editor
+ simplify state in finder
1 parent 2e50da4 commit 85f45ae

File tree

2 files changed

+15
-25
lines changed

2 files changed

+15
-25
lines changed

src/edit/moz-section-finder.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {deepEqual} from '@/js/util';
21
import {CodeMirror} from '@/cm';
2+
import {deepEqual} from '@/js/util';
33
import {trimCommentLabel} from './util';
44

55
export default function MozSectionFinder(cm) {
@@ -17,6 +17,12 @@ export default function MozSectionFinder(cm) {
1717
const minPos = (a, b) => cmpPos(a, b) < 0 ? a : b;
1818
const maxPos = (a, b) => cmpPos(a, b) > 0 ? a : b;
1919
const keptAlive = new Map();
20+
const state = /** @namespace MozSectionCmState */ {
21+
/** @type {Set<function>} */
22+
listeners: new Set(),
23+
/** @type {MozSection[]} */
24+
sections: [],
25+
};
2026
/** @type {CodeMirror.Pos} */
2127
let updFrom;
2228
/** @type {CodeMirror.Pos} */
@@ -32,9 +38,7 @@ export default function MozSectionFinder(cm) {
3238
'valueEnd',
3339
'sticky', // added by TextMarker::find()
3440
],
35-
get sections() {
36-
return getState().sections;
37-
},
41+
sections: state.sections,
3842
keepAliveFor(id, ms) {
3943
let data = keptAlive.get(id);
4044
if (data) {
@@ -49,7 +53,7 @@ export default function MozSectionFinder(cm) {
4953
},
5054

5155
on(fn) {
52-
const {listeners} = getState();
56+
const {listeners} = state;
5357
const needsInit = !listeners.size;
5458
listeners.add(fn);
5559
if (needsInit) {
@@ -59,7 +63,7 @@ export default function MozSectionFinder(cm) {
5963
},
6064

6165
off(fn) {
62-
const {listeners, sections} = getState();
66+
const {listeners, sections} = state;
6367
if (listeners.size) {
6468
listeners.delete(fn);
6569
if (!listeners.size) {
@@ -76,24 +80,10 @@ export default function MozSectionFinder(cm) {
7680

7781
/** @param {MozSection} [section] */
7882
updatePositions(section) {
79-
(section ? [section] : getState().sections).forEach(setPositionFromMark);
83+
(section ? [section] : state.sections).forEach(setPositionFromMark);
8084
},
8185
};
8286

83-
/** @returns {MozSectionCmState} */
84-
function getState() {
85-
let state = cm.state[KEY];
86-
if (!state) {
87-
state = cm.state[KEY] = /** @namespace MozSectionCmState */ {
88-
/** @type {Set<function>} */
89-
listeners: new Set(),
90-
/** @type {MozSection[]} */
91-
sections: [],
92-
};
93-
}
94-
return state;
95-
}
96-
9787
function onCmChanges(_cm, changes) {
9888
if (!updFrom) updFrom = {line: Infinity, ch: 0};
9989
if (!updTo) updTo = {line: -1, ch: 0};
@@ -109,7 +99,7 @@ export default function MozSectionFinder(cm) {
10999
}
110100

111101
function update() {
112-
const {sections, listeners} = getState();
102+
const {sections, listeners} = state;
113103
// Cloning to avoid breaking the internals of CodeMirror
114104
let from = updFrom ? {line: updFrom.line, ch: updFrom.ch} : {line: 0, ch: 0};
115105
let to = updTo ? {line: updTo.line, ch: updTo.ch} : {line: cm.doc.size, ch: 0};

src/edit/source-editor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export default function SourceEditor() {
4444
editor.viewTo = si.viewTo;
4545
sectionFinder = MozSectionFinder(me);
4646
sectionWidget = MozSectionWidget(me, sectionFinder);
47+
editor.sections = sectionFinder.sections;
48+
prefs.subscribe('editor.toc.expanded',
49+
(k, val) => sectionFinder.onOff(editor.updateToc, val), true);
4750
prefs.subscribe('editor.appliesToLineWidget',
4851
(k, val) => sectionWidget.toggle(val), true);
4952
Object.assign(me.curOp, si.scroll);
@@ -69,12 +72,9 @@ export default function SourceEditor() {
6972
updateMeta();
7073
// Subsribing outside of finishInit() because it uses `cm` that's still not initialized
7174
prefs.subscribe('editor.linter', updateLinterSwitch, true);
72-
prefs.subscribe('editor.toc.expanded',
73-
(k, val) => sectionFinder.onOff(editor.updateToc, val), true);
7475

7576
/** @namespace Editor */
7677
Object.assign(editor, {
77-
sections: sectionFinder.sections,
7878
replaceStyle,
7979
updateLinterSwitch,
8080
updateLivePreview,

0 commit comments

Comments
 (0)