fix: 修复打开/切换文件后误标记为已修改的问题

This commit is contained in:
thzxx
2026-05-27 17:40:52 +08:00
parent eb05e9d1b1
commit 03cd491f31
+7
View File
@@ -63,6 +63,7 @@
let viewMode = 'split';
let updateTimer = null;
let lineCountCache = 0;
let _loadingTab = false; // 加载 tab 内容期间跳过 input 事件的 isModified 标记
let currentRootPath = null;
// Search state
@@ -674,6 +675,7 @@
function loadTabState(tab) {
// 使用 execCommand('insertText') 替换内容,保留 undo/redo 历史
// setRangeText 会清空 undo 栈,所以此处必须用 execCommand
_loadingTab = true;
editor.focus();
editor.select();
const inserted = document.execCommand('insertText', false, tab.content);
@@ -699,6 +701,7 @@
// Update modified state
setModified(tab.isModified);
_loadingTab = false;
// Notify main process about active file
if (typeof window.electronAPI !== 'undefined') {
@@ -1101,9 +1104,12 @@
if (!tab || !tab.filePath) return;
const result = await window.electronAPI.reloadFile();
if (result.success) {
_loadingTab = true;
editor.value = result.content;
_loadingTab = false;
tab.content = result.content;
tab.isModified = false;
setModified(false);
lineCountCache = 0;
updateLineNumbers();
renderMarkdown(result.content);
@@ -1149,6 +1155,7 @@
// ===== Event Listeners =====
function initEventListeners() {
editor.addEventListener('input', () => {
if (_loadingTab) { scheduleUpdate(); return; } // 加载中不标记修改
const tab = getActiveTab();
if (tab) {
tab.isModified = true;