From addf8e4dd155cfc15a67e1319d9b87b8b4e5ee22 Mon Sep 17 00:00:00 2001 From: thzxx Date: Mon, 18 May 2026 15:50:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20save=E5=86=99=E5=85=A5=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E6=97=B6=E6=81=A2=E5=A4=8D=E6=96=87=E4=BB=B6=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E3=80=81=E7=AA=97=E5=8F=A3=E9=94=80=E6=AF=81=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E3=80=81=E6=9A=97=E8=89=B2=E4=B8=BB=E9=A2=98?= =?UTF-8?q?=E8=B7=9F=E9=9A=8F=E7=B3=BB=E7=BB=9F=E5=81=8F=E5=A5=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.js | 48 ++++++++++++++++++++++++-------------------- renderer/renderer.js | 4 +++- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/main.js b/main.js index 1ef5164..3733071 100644 --- a/main.js +++ b/main.js @@ -159,15 +159,34 @@ function openFileInTab(filePath) { function switchActiveFile(filePath) { activeFilePath = filePath || null; startWatching(filePath); - if (filePath) { - mainWindow.setTitle(`MarkLite - ${path.basename(filePath)}`); - } else { - mainWindow.setTitle('MarkLite'); + 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) { diff --git a/renderer/renderer.js b/renderer/renderer.js index 38c8603..1e4b010 100644 --- a/renderer/renderer.js +++ b/renderer/renderer.js @@ -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');