修复:close超时保护、移除冗余变量、优化文件大小计算、去重formatBytes
This commit is contained in:
@@ -6,7 +6,6 @@ let mainWindow = null;
|
|||||||
let currentFilePath = null;
|
let currentFilePath = null;
|
||||||
let pendingFilePath = null;
|
let pendingFilePath = null;
|
||||||
let fileWatcher = null;
|
let fileWatcher = null;
|
||||||
let isModifiedExternally = false;
|
|
||||||
|
|
||||||
function createWindow() {
|
function createWindow() {
|
||||||
mainWindow = new BrowserWindow({
|
mainWindow = new BrowserWindow({
|
||||||
@@ -39,11 +38,24 @@ function createWindow() {
|
|||||||
|
|
||||||
mainWindow.setMenu(null);
|
mainWindow.setMenu(null);
|
||||||
|
|
||||||
// Unsaved changes warning on close
|
// Unsaved changes warning on close (with timeout fallback)
|
||||||
mainWindow.on('close', (e) => {
|
mainWindow.on('close', (e) => {
|
||||||
mainWindow.webContents.send('window:closing');
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
try {
|
||||||
mainWindow.webContents.send('window:confirmClose');
|
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');
|
||||||
|
mainWindow.close();
|
||||||
|
}
|
||||||
|
}, 5000);
|
||||||
});
|
});
|
||||||
|
|
||||||
mainWindow.on('closed', () => {
|
mainWindow.on('closed', () => {
|
||||||
@@ -56,7 +68,6 @@ function createWindow() {
|
|||||||
function startWatching(filePath) {
|
function startWatching(filePath) {
|
||||||
stopWatching();
|
stopWatching();
|
||||||
try {
|
try {
|
||||||
isModifiedExternally = false;
|
|
||||||
fileWatcher = fs.watch(filePath, (eventType) => {
|
fileWatcher = fs.watch(filePath, (eventType) => {
|
||||||
if (eventType === 'change' && mainWindow && !mainWindow.isDestroyed()) {
|
if (eventType === 'change' && mainWindow && !mainWindow.isDestroyed()) {
|
||||||
isModifiedExternally = true;
|
isModifiedExternally = true;
|
||||||
@@ -201,7 +212,6 @@ ipcMain.handle('file:reload', async () => {
|
|||||||
if (!currentFilePath) return { success: false, error: '没有打开的文件' };
|
if (!currentFilePath) return { success: false, error: '没有打开的文件' };
|
||||||
try {
|
try {
|
||||||
const content = fs.readFileSync(currentFilePath, 'utf-8');
|
const content = fs.readFileSync(currentFilePath, 'utf-8');
|
||||||
isModifiedExternally = false;
|
|
||||||
return { success: true, content, filePath: currentFilePath };
|
return { success: true, content, filePath: currentFilePath };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return { success: false, error: err.message };
|
return { success: false, error: err.message };
|
||||||
|
|||||||
+8
-12
@@ -140,13 +140,15 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ===== File Size =====
|
// ===== File Size =====
|
||||||
|
function formatBytes(bytes) {
|
||||||
|
if (bytes < 1024) return bytes + ' B';
|
||||||
|
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
|
||||||
|
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
|
||||||
|
}
|
||||||
|
|
||||||
function updateFileSize() {
|
function updateFileSize() {
|
||||||
const bytes = new TextEncoder().encode(editor.value).length;
|
const bytes = new Blob([editor.value]).size;
|
||||||
let display;
|
statusSize.textContent = formatBytes(bytes);
|
||||||
if (bytes < 1024) display = bytes + ' B';
|
|
||||||
else if (bytes < 1024 * 1024) display = (bytes / 1024).toFixed(1) + ' KB';
|
|
||||||
else display = (bytes / (1024 * 1024)).toFixed(1) + ' MB';
|
|
||||||
statusSize.textContent = display;
|
|
||||||
statusSizeDivider.classList.add('visible');
|
statusSizeDivider.classList.add('visible');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,12 +217,6 @@
|
|||||||
return filePath.split(/[/\\]/).pop();
|
return filePath.split(/[/\\]/).pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatBytes(bytes) {
|
|
||||||
if (bytes < 1024) return bytes + ' B';
|
|
||||||
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
|
|
||||||
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
|
|
||||||
}
|
|
||||||
|
|
||||||
async function updateFileStats(filePath) {
|
async function updateFileStats(filePath) {
|
||||||
if (typeof window.electronAPI !== 'undefined' && filePath) {
|
if (typeof window.electronAPI !== 'undefined' && filePath) {
|
||||||
const stats = await window.electronAPI.getFileStats(filePath);
|
const stats = await window.electronAPI.getFileStats(filePath);
|
||||||
|
|||||||
Reference in New Issue
Block a user