Skip to content

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

Open
richbodo wants to merge 1 commit into
marktext:developfrom
richbodo:fix/flush-before-source-mode
Open

fix(desktop): flush pending edits before entering source code mode#5103
richbodo wants to merge 1 commit into
marktext:developfrom
richbodo:fix/flush-before-source-mode

Conversation

@richbodo

Copy link
Copy Markdown

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 <noreply@anthropic.com>
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

1 participant