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