Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/components/editor/CodeMirrorEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,13 @@ function moveLineThenRenumber(base: Command): Command {
// AND the change inserted or deleted a newline (the operations that shift item
// counts); pure in-line typing is left alone for performance.
const ORDERED_LINE_PROBE = /(^|\n)\s*\d+\.\s/
let renumberInFlight = false
// Per-view guard. A single module-level boolean would be shared by every
// editor instance, so in a split-pane layout one pane's in-flight renumber
// would suppress another pane's. Key the flag on the EditorView instead.
const renumberInFlight = new WeakMap<EditorView, boolean>()
const renumberOnEdit = EditorView.updateListener.of((update) => {
if (!update.docChanged) return
if (renumberInFlight) return
if (renumberInFlight.get(update.view)) return
if (update.view.composing) return

let touchedNewline = false
Expand Down Expand Up @@ -403,14 +406,14 @@ const renumberOnEdit = EditorView.updateListener.of((update) => {
if (!touchedNewline && !touchedOrderedLineStart) return
if (!ORDERED_LINE_PROBE.test(update.state.doc.toString())) return

renumberInFlight = true
renumberInFlight.set(update.view, true)
// Defer to escape the current update cycle (dispatching inside an
// updateListener is discouraged).
queueMicrotask(() => {
try {
renumberDocument(update.view)
} finally {
renumberInFlight = false
renumberInFlight.delete(update.view)
}
})
})
Expand Down