fix: save写入失败时恢复文件监听、窗口销毁安全检查、暗色主题跟随系统偏好
This commit is contained in:
@@ -159,15 +159,34 @@ function openFileInTab(filePath) {
|
||||
function switchActiveFile(filePath) {
|
||||
activeFilePath = filePath || null;
|
||||
startWatching(filePath);
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
if (filePath) {
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(filePath)}`);
|
||||
} else {
|
||||
mainWindow.setTitle('MarkLite');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// IPC Handlers
|
||||
|
||||
// Save file to a specific path with watcher management
|
||||
function saveToPath(filePath, content) {
|
||||
stopWatching();
|
||||
try {
|
||||
fs.writeFileSync(filePath, content, 'utf-8');
|
||||
} catch (err) {
|
||||
startWatching(activeFilePath); // Restore watcher on failure
|
||||
return { success: false, error: err.message };
|
||||
}
|
||||
activeFilePath = filePath;
|
||||
startWatching(filePath);
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(filePath)}`);
|
||||
}
|
||||
return { success: true, filePath };
|
||||
}
|
||||
|
||||
// Open single file via dialog
|
||||
ipcMain.handle('dialog:openFile', async () => {
|
||||
try {
|
||||
@@ -198,12 +217,7 @@ ipcMain.handle('file:read', async (event, filePath) => {
|
||||
ipcMain.handle('file:save', async (event, { filePath, content }) => {
|
||||
try {
|
||||
if (filePath) {
|
||||
stopWatching();
|
||||
fs.writeFileSync(filePath, content, 'utf-8');
|
||||
activeFilePath = filePath;
|
||||
startWatching(filePath);
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(filePath)}`);
|
||||
return { success: true, filePath };
|
||||
return saveToPath(filePath, content);
|
||||
} else {
|
||||
const result = await dialog.showSaveDialog(mainWindow, {
|
||||
filters: [
|
||||
@@ -211,12 +225,7 @@ ipcMain.handle('file:save', async (event, { filePath, content }) => {
|
||||
]
|
||||
});
|
||||
if (!result.canceled) {
|
||||
stopWatching();
|
||||
fs.writeFileSync(result.filePath, content, 'utf-8');
|
||||
activeFilePath = result.filePath;
|
||||
startWatching(result.filePath);
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(result.filePath)}`);
|
||||
return { success: true, filePath: result.filePath };
|
||||
return saveToPath(result.filePath, content);
|
||||
}
|
||||
return { success: false, canceled: true };
|
||||
}
|
||||
@@ -233,12 +242,7 @@ ipcMain.handle('file:saveAs', async (event, { content }) => {
|
||||
]
|
||||
});
|
||||
if (!result.canceled) {
|
||||
stopWatching();
|
||||
fs.writeFileSync(result.filePath, content, 'utf-8');
|
||||
activeFilePath = result.filePath;
|
||||
startWatching(result.filePath);
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(result.filePath)}`);
|
||||
return { success: true, filePath: result.filePath };
|
||||
return saveToPath(result.filePath, content);
|
||||
}
|
||||
return { success: false, canceled: true };
|
||||
} catch (err) {
|
||||
|
||||
@@ -85,7 +85,9 @@
|
||||
iconLight.style.display = isDark ? '' : 'none';
|
||||
}
|
||||
|
||||
apply(!!settings.darkMode);
|
||||
// Respect system preference if no saved setting
|
||||
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
apply(settings.darkMode !== undefined ? !!settings.darkMode : prefersDark);
|
||||
|
||||
btnDark.addEventListener('click', () => {
|
||||
const isDark = !document.documentElement.classList.contains('dark');
|
||||
|
||||
Reference in New Issue
Block a user