Skip to content

fix(desktop): prevent data loss when closing after Source Code mode edits - #4627

Merged
Jocs merged 1 commit into
developfrom
fix/source-mode-dirty-4455
Jun 22, 2026
Merged

Jocs merged 1 commit into
developfrom
fix/source-mode-dirty-4455

Conversation

@Jocs

@Jocs Jocs commented Jun 22, 2026

Copy link
Copy Markdown
Member

What

Source Code mode edits now mark the tab dirty, so closing the window after editing in Source Code mode shows the unsaved-changes prompt instead of silently discarding the edits.

Why (data loss)

LISTEN_FOR_CONTENT_CHANGE decides isSaved entirely from the editor history (lastEditIndex / editEntry.id vs lastSavedHistoryId). Source Code mode (sourceCode.vue) commits content without a history field, so:

  • the history branch never flips isSaved to false, and
  • the change instead falls into the else if restore branch and resets isSaved to true.

The close path (LISTEN_FOR_CLOSE) collects tabs.filter(t => !t.isSaved); with isSaved still true, it finds nothing unsaved and closes the window with no save dialog. Editing in Source Code mode and closing without switching back to WYSIWYG therefore loses the edits. (A source-mode caret move could even clear a dirty flag set earlier in WYSIWYG, via the same restore branch.)

Fix

When the content-change carries no history (the Source Code path), decide dirty state from a content comparison (markdown !== oldMarkdown). The history-based branch is unchanged for WYSIWYG, and the "restore isSaved = true" branch now only runs when a history is present, so a source-mode caret move can no longer clear a pending dirty flag.

Tests

New test/unit/specs/source-mode-dirty.spec.ts (RED→GREEN): source-mode edit → dirty; source-mode caret move (unchanged content) → stays saved; WYSIWYG history path → unchanged. Full desktop unit suite (28 files / 653 tests), lint, and vue-tsc typecheck pass.

Fixes #4455

🤖 Generated with Claude Code

…a loss (#4455)

Editing a file in Source Code mode and then closing the window without first
switching back to WYSIWYG silently skipped the unsaved-changes prompt, losing
the edits. Source-mode commits reach LISTEN_FOR_CONTENT_CHANGE without an
editor `history`, and the dirty check is entirely history-based: a missing
history left `isSaved` untouched and actually fell into the restore branch
that resets it to `true`, so the close path saw nothing unsaved.

Decide dirty state from a content comparison when the change carries no
history (the Source Code path), and only let the history branch restore
`isSaved = true` when a history is present, so a source-mode caret move can no
longer clear a pending dirty flag.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

Build artifacts for PR #4627:

Run: https://github.com/marktext/marktext/actions/runs/27929123931

Artifact Size Link
marktext-windows-arm64 275.8 MB Download
marktext-macos-x64 293.1 MB Download
marktext-linux 627.5 MB Download
marktext-windows-x64 283.9 MB Download
marktext-macos-arm64 282.8 MB Download

@Jocs
Jocs merged commit f937b70 into develop Jun 22, 2026
10 checks passed
@Jocs
Jocs deleted the fix/source-mode-dirty-4455 branch June 22, 2026 07:28
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] AFTER MODIFYING, IT DOESN'T ASK IF YOU WANT TO SAVE OR NOT!

1 participant