Repository navigation
fix(desktop): flush pending edits before entering source code mode - #5103
Conversation
An edit made in the same frame as the WYSIWYG -> source switch (e.g. a task-checkbox click) was missing from the CodeMirror snapshot, and the swap back out of source mode cancels the engine's scheduled op flush (marktext#4658), silently reverting the edit. Flush the rAF batch in the sync sourceCode watcher so the tab holds fresh markdown before sourceCode.vue mounts — the same guard saving (marktext#3803/marktext#4859) and tab switching (marktext#2938) already use. Fixes marktext#5102 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Thanks for the fix and the clear write-up, @richbodo! I dug into this before merging, so I'm leaving the findings here for the record. The race is real, but it depends on timing and doesn't fail every time. Measured on develop
One small correction to the root cause: exiting source mode goes through With this PR applied (same machine): the stubbed-rAF control keeps the toggle 3/3 with a fresh source snapshot, the Playwright sequence loses 0/20, and |
Fixes #5102
Problem
An edit made in the WYSIWYG editor in the same frame as switching to source code mode (deterministic repro: clicking a task-list checkbox, then immediately toggling View → Source Code Mode) is silently lost:
@muyajs/corequeues the edit in its deferred rAF op batch (JSONState).sourceCode.vuemounts and snapshotscurrentFile.markdownbefore that batch flushes, so CodeMirror opens on stale content.setContent, which cancels the still-pending batch by design (fix(muya): drop pending op batch on setContent to prevent cross-document corruption (#2938) #4658) — the edit is reverted permanently, with no undo entry.This is the same unflushed-batch class as #3803 (dropped edit on save, fixed in #4859) and the tab-switch guard (#2938); source-mode entry was the remaining reader of
currentFile.markdownwithout a flush.It also explains a deterministic e2e failure on develop on macOS:
test/e2e/task-list-autocheck.spec.ts› "with autoCheck OFF, clicking the parent changes only the parent" clicks a checkbox and immediately reads markdown through a source-mode round-trip, and fails 4/4 runs without this fix.Fix
Call
editor.value.flush()in the existingwatch(sourceCode, …, { flush: 'sync' })handler ineditorWithTabs/editor.vue— the sync watcher that already prepares the tab (JIT cursor) before thev-if-gatedsourceCode.vuemounts. The pending batch lands in the tab first, so the snapshot is always fresh.Tests
test/e2e/source-mode-flush.spec.ts: clicks a checkbox and immediately round-trips through source mode with no settle wait; asserts the toggle is present in the source view and survives the round-trip. Fails on develop, passes with the fix.task-list-autocheck.spec.ts(previously failing deterministically on macOS): now passes, including with--repeat-each=2.lintandtypecheckclean.🤖 Generated with Claude Code