fix: BOM处理、异步读取、10MB文件限制、滚动同步精度优化
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
const { app, BrowserWindow, dialog, ipcMain } = require('electron');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const { readFile, stat } = require('fs/promises');
|
||||
|
||||
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
|
||||
|
||||
let mainWindow = null;
|
||||
let activeFilePath = null; // Currently active tab's file path
|
||||
@@ -149,15 +152,23 @@ async function showOpenDialog() {
|
||||
return null;
|
||||
}
|
||||
|
||||
function readFileContent(filePath) {
|
||||
return fs.readFileSync(filePath, 'utf-8');
|
||||
async function readFileContent(filePath) {
|
||||
// 文件大小检查
|
||||
const stats = await stat(filePath);
|
||||
if (stats.size > MAX_FILE_SIZE) {
|
||||
throw new Error(`文件过大(${(stats.size / 1024 / 1024).toFixed(1)} MB),暂不支持超过 10MB 的文件`);
|
||||
}
|
||||
let content = await readFile(filePath, 'utf-8');
|
||||
// 剥离 UTF-8 BOM(Windows 某些编辑器会添加)
|
||||
if (content.charCodeAt(0) === 0xFEFF) content = content.slice(1);
|
||||
return content;
|
||||
}
|
||||
|
||||
// Open file in a new tab (renderer manages tabs)
|
||||
function openFileInTab(filePath) {
|
||||
async function openFileInTab(filePath) {
|
||||
if (!mainWindow || mainWindow.isDestroyed()) return;
|
||||
try {
|
||||
const content = readFileContent(filePath);
|
||||
const content = await readFileContent(filePath);
|
||||
activeFilePath = filePath;
|
||||
startWatching(filePath);
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(filePath)}`);
|
||||
@@ -213,7 +224,7 @@ ipcMain.handle('dialog:openFile', async () => {
|
||||
try {
|
||||
const files = await showOpenDialog();
|
||||
if (files && files.length > 0) {
|
||||
const content = readFileContent(files[0]);
|
||||
const content = await readFileContent(files[0]);
|
||||
activeFilePath = files[0];
|
||||
startWatching(files[0]);
|
||||
mainWindow.setTitle(`MarkLite - ${path.basename(files[0])}`);
|
||||
@@ -228,7 +239,7 @@ ipcMain.handle('dialog:openFile', async () => {
|
||||
|
||||
ipcMain.handle('file:read', async (event, filePath) => {
|
||||
try {
|
||||
const content = readFileContent(filePath);
|
||||
const content = await readFileContent(filePath);
|
||||
return { success: true, content };
|
||||
} catch (err) {
|
||||
return { success: false, error: err.message };
|
||||
@@ -277,8 +288,8 @@ ipcMain.handle('file:getCurrentPath', () => {
|
||||
|
||||
ipcMain.handle('file:stats', async (event, filePath) => {
|
||||
try {
|
||||
const stats = fs.statSync(filePath);
|
||||
return { success: true, size: stats.size, mtime: stats.mtime.toISOString() };
|
||||
const fileStat = await stat(filePath);
|
||||
return { success: true, size: fileStat.size, mtime: fileStat.mtime.toISOString() };
|
||||
} catch (err) {
|
||||
return { success: false, error: err.message };
|
||||
}
|
||||
@@ -287,7 +298,7 @@ ipcMain.handle('file:stats', async (event, filePath) => {
|
||||
ipcMain.handle('file:reload', async () => {
|
||||
if (!activeFilePath) return { success: false, error: '没有打开的文件' };
|
||||
try {
|
||||
const content = readFileContent(activeFilePath);
|
||||
const content = await readFileContent(activeFilePath);
|
||||
return { success: true, content, filePath: activeFilePath };
|
||||
} catch (err) {
|
||||
return { success: false, error: err.message };
|
||||
|
||||
Reference in New Issue
Block a user