fix: avoid false "unsaved" prompt when closing file opened from search without editing#4146
Open
liyongning wants to merge 1 commit intomarktext:developfrom
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 中做归一化比较与“忽略首次”逻辑,并在每次处理完后清除标记。