新增多标签页支持:Ctrl+T 新建、Ctrl+W 关闭、Ctrl+Tab 切换、拖拽多文件打开
This commit is contained in:
@@ -3,7 +3,7 @@ const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
let mainWindow = null;
|
||||
let currentFilePath = null;
|
||||
let activeFilePath = null; // Currently active tab's file path
|
||||
let pendingFilePath = null;
|
||||
let fileWatcher = null;
|
||||
|
||||
@@ -25,7 +25,7 @@ function createWindow() {
|
||||
|
||||
mainWindow.webContents.on('did-finish-load', () => {
|
||||
if (pendingFilePath) {
|
||||
openFile(pendingFilePath);
|
||||
openFileInTab(pendingFilePath);
|
||||
pendingFilePath = null;
|
||||
}
|
||||
});
|
||||
@@ -38,18 +38,16 @@ function createWindow() {
|
||||
|
||||
mainWindow.setMenu(null);
|
||||
|
||||
// Unsaved changes warning on close (with timeout fallback)
|
||||
// Unsaved changes warning on close
|
||||
mainWindow.on('close', (e) => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
mainWindow.webContents.send('window:confirmClose');
|
||||
} catch (err) {
|
||||
// Renderer is gone, force close
|
||||
mainWindow.removeAllListeners('close');
|
||||
mainWindow.close();
|
||||
return;
|
||||
}
|
||||
// Safety: if renderer doesn't respond within 5s, force close
|
||||
setTimeout(() => {
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.removeAllListeners('close');
|
||||
@@ -64,9 +62,10 @@ function createWindow() {
|
||||
});
|
||||
}
|
||||
|
||||
// File watcher - detect external modifications
|
||||
// File watcher
|
||||
function startWatching(filePath) {
|
||||
stopWatching();
|
||||
if (!filePath) return;
|
||||
try {
|
||||
fileWatcher = fs.watch(filePath, (eventType) => {
|
||||
if (eventType === 'change' && mainWindow && !mainWindow.isDestroyed()) {
|
||||
@@ -74,7 +73,7 @@ function startWatching(filePath) {
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
// Silently ignore watch errors
|
||||
// Silently ignore
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,42 +84,82 @@ function stopWatching() {
|
||||
}
|
||||
}
|
||||
|
||||
async function showOpenDialog() {
|
||||
async function showOpenDialog(multi) {
|
||||
const result = await dialog.showOpenDialog(mainWindow, {
|
||||
properties: ['openFile'],
|
||||
properties: multi ? ['openFile', 'multiSelections'] : ['openFile'],
|
||||
filters: [
|
||||
{ name: 'Markdown 文件', extensions: ['md', 'markdown', 'txt'] },
|
||||
{ name: '所有文件', extensions: ['*'] }
|
||||
]
|
||||
});
|
||||
if (!result.canceled && result.filePaths.length > 0) {
|
||||
return result.filePaths[0];
|
||||
return result.filePaths;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function openFile(filePath) {
|
||||
function readFileContent(filePath) {
|
||||
return fs.readFileSync(filePath, 'utf-8');
|
||||
}
|
||||
|
||||
// Open file in a new tab (renderer manages tabs)
|
||||
function openFileInTab(filePath) {
|
||||
try {
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
currentFilePath = filePath;
|
||||
const content = readFileContent(filePath);
|
||||
activeFilePath = filePath;
|
||||
startWatching(filePath);
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(filePath)}`);
|
||||
mainWindow.webContents.send('file:opened', { filePath, content });
|
||||
mainWindow.webContents.send('file:openInTab', { filePath, content });
|
||||
} catch (err) {
|
||||
dialog.showErrorBox('错误', `无法读取文件: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Switch active file (for tab switching)
|
||||
function switchActiveFile(filePath) {
|
||||
activeFilePath = filePath || null;
|
||||
startWatching(filePath);
|
||||
if (filePath) {
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(filePath)}`);
|
||||
} else {
|
||||
mainWindow.setTitle('MarkLite');
|
||||
}
|
||||
}
|
||||
|
||||
// IPC Handlers
|
||||
|
||||
// Open single file via dialog
|
||||
ipcMain.handle('dialog:openFile', async () => {
|
||||
try {
|
||||
const filePath = await showOpenDialog();
|
||||
if (filePath) {
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
currentFilePath = filePath;
|
||||
startWatching(filePath);
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(filePath)}`);
|
||||
return { filePath, content };
|
||||
const files = await showOpenDialog(false);
|
||||
if (files && files.length > 0) {
|
||||
const content = readFileContent(files[0]);
|
||||
activeFilePath = files[0];
|
||||
startWatching(files[0]);
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(files[0])}`);
|
||||
return { filePath: files[0], content };
|
||||
}
|
||||
return null;
|
||||
} catch (err) {
|
||||
return { error: err.message };
|
||||
}
|
||||
});
|
||||
|
||||
// Open multiple files via dialog
|
||||
ipcMain.handle('dialog:openFiles', async () => {
|
||||
try {
|
||||
const files = await showOpenDialog(true);
|
||||
if (files && files.length > 0) {
|
||||
const results = [];
|
||||
for (const fp of files) {
|
||||
try {
|
||||
const content = readFileContent(fp);
|
||||
results.push({ filePath: fp, content });
|
||||
} catch (err) {
|
||||
results.push({ filePath: fp, error: err.message });
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
return null;
|
||||
} catch (err) {
|
||||
@@ -130,7 +169,7 @@ ipcMain.handle('dialog:openFile', async () => {
|
||||
|
||||
ipcMain.handle('file:read', async (event, filePath) => {
|
||||
try {
|
||||
const content = fs.readFileSync(filePath, 'utf-8');
|
||||
const content = readFileContent(filePath);
|
||||
return { success: true, content };
|
||||
} catch (err) {
|
||||
return { success: false, error: err.message };
|
||||
@@ -142,7 +181,7 @@ ipcMain.handle('file:save', async (event, { filePath, content }) => {
|
||||
if (filePath) {
|
||||
stopWatching();
|
||||
fs.writeFileSync(filePath, content, 'utf-8');
|
||||
currentFilePath = filePath;
|
||||
activeFilePath = filePath;
|
||||
startWatching(filePath);
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(filePath)}`);
|
||||
return { success: true, filePath };
|
||||
@@ -156,7 +195,7 @@ ipcMain.handle('file:save', async (event, { filePath, content }) => {
|
||||
if (!result.canceled) {
|
||||
stopWatching();
|
||||
fs.writeFileSync(result.filePath, content, 'utf-8');
|
||||
currentFilePath = result.filePath;
|
||||
activeFilePath = result.filePath;
|
||||
startWatching(result.filePath);
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(result.filePath)}`);
|
||||
return { success: true, filePath: result.filePath };
|
||||
@@ -179,7 +218,7 @@ ipcMain.handle('file:saveAs', async (event, { content }) => {
|
||||
if (!result.canceled) {
|
||||
stopWatching();
|
||||
fs.writeFileSync(result.filePath, content, 'utf-8');
|
||||
currentFilePath = result.filePath;
|
||||
activeFilePath = result.filePath;
|
||||
startWatching(result.filePath);
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(result.filePath)}`);
|
||||
return { success: true, filePath: result.filePath };
|
||||
@@ -191,46 +230,45 @@ ipcMain.handle('file:saveAs', async (event, { content }) => {
|
||||
});
|
||||
|
||||
ipcMain.handle('file:getCurrentPath', () => {
|
||||
return currentFilePath;
|
||||
return activeFilePath;
|
||||
});
|
||||
|
||||
ipcMain.handle('file:stats', async (event, filePath) => {
|
||||
try {
|
||||
const stats = fs.statSync(filePath);
|
||||
return {
|
||||
success: true,
|
||||
size: stats.size,
|
||||
mtime: stats.mtime.toISOString()
|
||||
};
|
||||
return { success: true, size: stats.size, mtime: stats.mtime.toISOString() };
|
||||
} catch (err) {
|
||||
return { success: false, error: err.message };
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('file:reload', async () => {
|
||||
if (!currentFilePath) return { success: false, error: '没有打开的文件' };
|
||||
if (!activeFilePath) return { success: false, error: '没有打开的文件' };
|
||||
try {
|
||||
const content = fs.readFileSync(currentFilePath, 'utf-8');
|
||||
return { success: true, content, filePath: currentFilePath };
|
||||
const content = readFileContent(activeFilePath);
|
||||
return { success: true, content, filePath: activeFilePath };
|
||||
} catch (err) {
|
||||
return { success: false, error: err.message };
|
||||
}
|
||||
});
|
||||
|
||||
// Tab switched - update active file tracking
|
||||
ipcMain.handle('tab:switched', (event, filePath) => {
|
||||
switchActiveFile(filePath);
|
||||
});
|
||||
|
||||
ipcMain.handle('window:forceClose', () => {
|
||||
stopWatching();
|
||||
mainWindow.removeAllListeners('close');
|
||||
mainWindow.close();
|
||||
});
|
||||
|
||||
// Handle file open from command line or file association
|
||||
// Command line file
|
||||
function getCommandLineFile() {
|
||||
const args = process.argv.slice(1);
|
||||
if (args.length > 0 && !args[0].startsWith('--')) {
|
||||
const filePath = args[0];
|
||||
if (fs.existsSync(filePath)) {
|
||||
return filePath;
|
||||
}
|
||||
if (fs.existsSync(filePath)) return filePath;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -242,7 +280,7 @@ app.whenReady().then(() => {
|
||||
if (mainWindow && mainWindow.webContents.isLoading()) {
|
||||
pendingFilePath = filePath;
|
||||
} else if (mainWindow) {
|
||||
openFile(filePath);
|
||||
openFileInTab(filePath);
|
||||
} else {
|
||||
pendingFilePath = filePath;
|
||||
}
|
||||
@@ -257,13 +295,9 @@ app.whenReady().then(() => {
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
if (process.platform !== 'darwin') app.quit();
|
||||
});
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
createWindow();
|
||||
}
|
||||
if (BrowserWindow.getAllWindows().length === 0) createWindow();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user