Repository navigation
Conversation
Fixed a bug where opening a file would immediately mark it as modified, prompting a save dialog even when no changes were made. The issue had two parts: 1. Added autoNormalizeMarkdownOnOpen preference (defaults to false) to control whether MarkText automatically normalizes line endings and detects trailing newlines when files are opened. When disabled, files are preserved as-is. 2. Added isFirstContentChange flag to ignore the initial content change event triggered by Muya editor normalizing content during file load. This prevents marking files as unsaved when they haven't been actually edited. Changes: - Added autoNormalizeMarkdownOnOpen parameter to loadMarkdownFile - Modified line ending and trailing newline detection to only run when auto-normalize is enabled - Added isFirstContentChange flag to file state to track first content change - Updated content change handler to skip marking file as unsaved on first change - Added UI toggle in preferences for the new setting
Added localization strings for the new autoNormalizeMarkdownOnOpen preference to all supported languages: - German (de.json) - Spanish (es.json) - French (fr.json) - Japanese (ja.json) - Korean (ko.json) - Portuguese (pt.json) - Simplified Chinese (zh-CN.json) - Traditional Chinese (zh-TW.json) The localization describes that this preference controls whether files are automatically normalized when opened or only when saved.
|
cc @Tkaixiang Some simple test feedback |
Tkaixiang
left a comment
There was a problem hiding this comment.
Hey, thanks for this PR, just a few comments
| } | ||
|
|
||
| // Ignore the first content change after file load (Muya normalizes content) | ||
| if (this.currentFile.isFirstContentChange) { |
There was a problem hiding this comment.
Consider not doing this since it silently suppreses file changes if there was no normalisation initially, instead, perhaps a notification could be sent if auto-normalisation of line endings/trailing new lines was carried out.
An alternative could be to modify the ground truth markdown directly instead of modifying the end lines through a change event
|
Thanks for the PR, since I have extra time right now, I will be bringing this branch in for works |
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where files were incorrectly marked as modified immediately upon opening, even when no user changes were made. The fix addresses two root causes: automatic line ending normalization on file open and Muya editor's internal content normalization.
Changes:
- Added a new preference
autoNormalizeMarkdownOnOpen(default: false) to control automatic normalization behavior when opening files - Implemented logic to ignore the first content change event after loading a file to prevent Muya's internal normalization from triggering the unsaved state
- Propagated the new preference through the file watching and loading pipeline
Reviewed changes
Copilot reviewed 16 out of 17 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/main/preferences/schema.json | Added the new preference definition with default value false |
| src/main/filesystem/markdown.js | Added conditional normalization logic based on the new preference |
| src/main/filesystem/watcher.js | Propagated the new preference parameter through file watching functions |
| src/main/windows/editor.js | Extracted and passed the new preference to loadMarkdownFile |
| src/renderer/src/prefComponents/editor/index.vue | Added UI toggle for the preference |
| src/renderer/src/store/help.js | Added isFirstContentChange flag to defaultFileState |
| src/renderer/src/store/editor.js | Implemented logic to skip the first content change after file load |
| static/locales/*.json | Added translations for the new preference in all supported languages |
| .gitignore | Added local Claude settings file to ignore list |
Comments suppressed due to low confidence (1)
src/renderer/src/store/help.js:5
- Corrected spelling of 'internel' to 'internal'.
* Default internel markdown document with editor options.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Upon further review, this PR does not fully solve the problem unfortunately, it was simply surpressing the first change which sometimes included normalisation. See my explanation in #31 |
Fixes #31
Problem
MarkText currently marks files as modified immediately upon opening them, even when no user changes have been made. This causes unnecessary "unsaved changes" warnings when switching tabs or closing files, creating a frustrating user experience.
Root Cause
The issue has two contributing factors:
Automatic line ending normalization on file open: When
loadMarkdownFile()detects CRLF line endings or mixed line endings, it automatically converts them to LF and setsadjustLineEndingOnSave = true. This marks the file as modified even though the user hasn't made any changes.Muya editor content normalization: When content is loaded into the Muya editor via
setMarkdown(), the editor performs its own internal normalization (whitespace adjustments, etc.). This triggers aLISTEN_FOR_CONTENT_CHANGEevent that marks the file as unsaved, even when the file was just opened.Solution
This PR implements a two-part fix:
1. Optional Line Ending Normalization
Added a new preference
autoNormalizeMarkdownOnOpen(default:false) that controls whether files should be automatically normalized when opened:When disabled (default): Files are opened as-is with their original line endings. The editor still converts to LF internally (required by MarkText), but preserves the original format via
adjustLineEndingOnSaveflag. The file is only marked as modified when the user actually makes changes.When enabled: Maintains the old behavior where files with CRLF or mixed line endings are normalized immediately.
Files modified:
src/main/filesystem/markdown.js- Added conditional normalization logicsrc/main/preferences/schema.json- Added preference definitionsrc/renderer/src/prefComponents/editor/index.vue- Added UI togglestatic/locales/en.json- Added localization stringsrc/main/filesystem/watcher.js- Propagated preference through file watchingsrc/main/windows/editor.js- Passed preference to loadMarkdownFile2. Ignore First Content Change from Muya
Added
isFirstContentChangeflag to the file state that ignores the first content change event after a file is loaded. This prevents Muya's internal normalization from marking files as unsaved.Files modified:
src/renderer/src/store/help.js- AddedisFirstContentChange: truetodefaultFileStatesrc/renderer/src/store/editor.js- ModifiedLISTEN_FOR_CONTENT_CHANGEto skip first changeTesting
Tested on macOS with files containing:
Result: Files are no longer marked as modified when opened. The unsaved indicator only appears after actual user edits.
Impact
This fix eliminates a significant UI annoyance where users were constantly prompted about unsaved changes when they hadn't modified anything. Files are now only marked as modified when actual changes are made by the user.