fix: avoid false "unsaved" prompt when closing file opened from search without editing#4146
Conversation
|
I tested it on my macOS and this fixed the issue reported, as claimed. |
|
@Jocs This PR has presumably resolved an issue existing on macOS and Windows, so I'd appreciate a quick merge. Thanks~ |
…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
|
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.
|
There was a problem hiding this comment.
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.
| // 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') | ||
| }, |
| if (state.currentFile.ignoreNextContentChangeForSaveStatus) { | ||
| commit('CLEAR_IGNORE_NEXT_CONTENT_CHANGE_FOR_SAVE_STATUS') | ||
| } else { |
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 achangeevent with re-serialized content that can differ from the on-disk content (e.g. trailing newlines, line endings).Solution
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 setisSaved = false.Ignore first content change after opening
For tabs created via
NEW_TAB_WITH_CONTENT(e.g. open from search or file tree), set a flagignoreNextContentChangeForSaveStatusso the firstchangeevent from Muya aftersetMarkdown()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 mutationCLEAR_IGNORE_NEXT_CONTENT_CHANGE_FOR_SAVE_STATUS; setignoreNextContentChangeForSaveStatusinNEW_TAB_WITH_CONTENT; inLISTEN_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,与磁盘内容可能不完全一致(如末尾换行、换行符等),导致被误判为未保存。修复
判断内容是否变化时,对「当前内容」和「打开时的内容」都做相同的末尾换行归一化再比较,避免仅因归一化差异被标为未保存。
对通过
NEW_TAB_WITH_CONTENT打开的标签(如从搜索/文件树打开),设置ignoreNextContentChangeForSaveStatus,使 Muya 在setMarkdown()之后触发的第一次change不视为用户编辑。每次处理 content change 后都会清除该标记,确保之后的真实编辑仍会被正确标为未保存。涉及文件
src/renderer/store/editor.js:新增 mutation、在打开带内容的 tab 时设标记、在 content change 中做归一化比较与“忽略首次”逻辑,并在每次处理完后清除标记。