Fix: Prevent files from being marked as modified immediately on open #25
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.
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.