Skip to content

fix: avoid false "unsaved" prompt when closing file opened from search without editing#4146

Open
liyongning wants to merge 1 commit into
marktext:developfrom
liyongning:fix/unsaved-dialog-on-open-from-search
Open

fix: avoid false "unsaved" prompt when closing file opened from search without editing#4146
liyongning wants to merge 1 commit into
marktext:developfrom
liyongning:fix/unsaved-dialog-on-open-from-search

Conversation

@liyongning
Copy link
Copy Markdown

@liyongning liyongning commented Mar 15, 2026

Q A
Bug fix? yes
New feature? no
Breaking changes? no
Deprecations? no
New tests added? not needed
Fixed tickets none
License MIT

Description

Problem
After opening a folder containing files in the editor, if you open a file via the global search on the left, close it without making any edits, a prompt "Do you want to save changes?" will still pop up. The tab was incorrectly marked as unsaved because Muya's setMarkdown() triggers a change event with re-serialized content that can differ from the on-disk content (e.g. trailing newlines, line endings).

Solution

  1. Normalized comparison
    When deciding whether content changed, compare both the incoming markdown and the previous markdown using the same trailing-newline normalization (adjustTrailingNewlines), so purely normalization differences do not set isSaved = false.

  2. Ignore first content change after opening
    For tabs created via NEW_TAB_WITH_CONTENT (e.g. open from search or file tree), set a flag ignoreNextContentChangeForSaveStatus so the first change event from Muya after setMarkdown() is not treated as a user edit. The flag is always cleared after handling each content change so subsequent real edits are still detected.

Files changed

  • src/renderer/store/editor.js: added mutation CLEAR_IGNORE_NEXT_CONTENT_CHANGE_FOR_SAVE_STATUS; set ignoreNextContentChangeForSaveStatus in NEW_TAB_WITH_CONTENT; in LISTEN_FOR_CONTENT_CHANGE, compare using normalized old markdown and skip setting unsaved when the flag is set; clear the flag after every content change.

中文说明

问题
在编辑器中打开一个拥有文件的文件夹后,通过左侧全局搜索打开文件后,未做任何编辑直接关闭,仍会弹出「是否保存更改」的提示。原因是 Muya 的 setMarkdown() 会触发一次 change 事件,其内容为重新序列化后的 markdown,与磁盘内容可能不完全一致(如末尾换行、换行符等),导致被误判为未保存。

修复

  1. 归一化后再比较
    判断内容是否变化时,对「当前内容」和「打开时的内容」都做相同的末尾换行归一化再比较,避免仅因归一化差异被标为未保存。
  2. 打开后忽略第一次 content change
    对通过 NEW_TAB_WITH_CONTENT 打开的标签(如从搜索/文件树打开),设置 ignoreNextContentChangeForSaveStatus,使 Muya 在 setMarkdown() 之后触发的第一次 change 不视为用户编辑。每次处理 content change 后都会清除该标记,确保之后的真实编辑仍会被正确标为未保存。

涉及文件

  • src/renderer/store/editor.js:新增 mutation、在打开带内容的 tab 时设标记、在 content change 中做归一化比较与“忽略首次”逻辑,并在每次处理完后清除标记。

@chongchonghe
Copy link
Copy Markdown
Contributor

I tested it on my macOS and this fixed the issue reported, as claimed.

@chongchonghe
Copy link
Copy Markdown
Contributor

@Jocs This PR has presumably resolved an issue existing on macOS and Windows, so I'd appreciate a quick merge. Thanks~

UCHIHAHA103 pushed a commit to UCHIHAHA103/marktext that referenced this pull request May 14, 2026
…ll position / XSS / contextIsolation

- fix: 编辑菜单截图分割线仅 macOS 可见(Windows 不再出现双分割线)
- fix(marktext#4093): 深色主题打开窗口预设背景色,消除白闪
- feat(marktext#4134): capitalizeAccelerator - 自定义快捷键修饰符大写修复
- fix(marktext#4146): 搜索打开文件后首次 muya 序列化不触发未保存状态
- fix(marktext#4075): 外部文件变更:已保存时静默加载,未保存时才提示
- fix(marktext#4152): 切换 Tab 保留滚动位置;打开文件光标定位到开头
- fix(marktext#4206): 图片属性 HTML 转义防 XSS/RCE
- fix(marktext#4192): contextIsolation=true 防渲染进程调用 Node.js API
@Jocs
Copy link
Copy Markdown
Member

Jocs commented May 15, 2026

This project recently merged a large refactor (electron-vite migration, PR #4001) that upgraded dependencies and fixed several issues.

Please review this PR and let us know if it is still needed, or if the changes have already been addressed upstream.

Note: This PR currently has merge conflicts with the develop branch. If you decide to continue with this PR, please rebase or merge the latest develop branch to resolve the conflicts before it can be reviewed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes false “Do you want to save changes?” prompts when closing files opened via global search by preventing Muya’s initial setMarkdown()-triggered change event (and trailing-newline normalization differences) from marking a tab as unsaved.

Changes:

  • Add a per-tab flag to ignore the first Muya content-change event after opening a document with existing content.
  • Normalize prior markdown when comparing changes to avoid “unsaved” state caused only by trailing-newline normalization.
  • Clear the ignore flag after content-change handling via a new Vuex mutation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +993 to 996
// Always clear so that the flag only protects the very first change; otherwise when the
// first change had same content we would never clear and would wrongly ignore a later edit.
commit('CLEAR_IGNORE_NEXT_CONTENT_CHANGE_FOR_SAVE_STATUS')
},
Comment on lines +975 to +977
if (state.currentFile.ignoreNextContentChangeForSaveStatus) {
commit('CLEAR_IGNORE_NEXT_CONTENT_CHANGE_FOR_SAVE_STATUS')
} else {
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.

4 participants