Skip to content

fix(desktop): flush pending edits before entering source code mode - #5103

Merged
Jocs merged 1 commit into
marktext:developfrom
richbodo:fix/flush-before-source-mode
Sep 11, 2026
Merged

Jocs merged 1 commit into
marktext:developfrom
richbodo:fix/flush-before-source-mode

Conversation

@richbodo

Copy link
Copy Markdown
Contributor

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:

  1. @muyajs/core queues the edit in its deferred rAF op batch (JSONState).
  2. sourceCode.vue mounts and snapshots currentFile.markdown before that batch flushes, so CodeMirror opens on stale content.
  3. Exiting source mode applies the stale snapshot back via 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.markdown without 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 existing watch(sourceCode, …, { flush: 'sync' }) handler in editorWithTabs/editor.vue — the sync watcher that already prepares the tab (JIT cursor) before the v-if-gated sourceCode.vue mounts. The pending batch lands in the tab first, so the snapshot is always fresh.

Tests

  • New regression spec 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.
  • Full suites on macOS arm64: unit 734/734, e2e 216 passed / 4 skipped, lint and typecheck clean.

🤖 Generated with Claude Code

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]>
@Jocs

Jocs commented Sep 11, 2026

Copy link
Copy Markdown
Member

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 7ab05fff (built app, macOS, 120 Hz display):

  • Real input path (webContents.sendInputEvent click, then the same menu handler Cmd+Option+S runs), toggle sent 0–200 ms after the click: 0 losses in 35 runs, plus 0 in 4 more runs with a 3000-paragraph document under 20× CPU throttling. The batch's rAF flush ran ~2–7 ms after the click; the source view mounted ~7 ms or later. Sending the toggle 2–30 ms before the click means the source view already covers the checkbox, so the click never lands. I couldn't find a window a person could hit by hand.
  • The Playwright sequence from the issue (page.click, then an immediate getMarkdownContent round-trip) is flaky: it lost the edit in 2/10, 1/20 and 0/10 runs. In the losing run the rAF slipped to 9.1 ms and the source view mounted at 8.8 ms. task-list-autocheck.spec.ts passed 3/3 on develop here, so the 4/4 failure you saw is probably machine-dependent (frame timing, e.g. a 60 Hz display, is my guess; unverified).
  • Forcing the window open reproduces it every time: stub window.requestAnimationFrame with a no-op so the batch stays pending, click the checkbox, toggle source mode. The source view is stale and the toggle is reverted after the round-trip.

One small correction to the root cause: exiting source mode goes through replaceContent (the isSourceModeHandoff branch in editor.vue), not setContent. So the revert doesn't need the pending batch to be cancelled. Writing CodeMirror's stale snapshot back is enough.

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 source-mode-flush.spec.ts plus task-list-autocheck.spec.ts pass. The fix is correct, matches the existing save (#4859) and tab-switch (#2938) guards, and removes a source of e2e flakiness. Merging.

@Jocs
Jocs merged commit 3ed697b into marktext:develop Sep 11, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] WYSIWYG edit made immediately before entering source code mode is silently reverted

2 participants