Files
XCSDD/electron/main.mjs
ssdfasd d66f5b09e6 feat: migrate to react-markdown for robust markdown rendering
- Replace custom markdown parser with react-markdown + remark-gfm
- Fix document link navigation (./ and ../ references)
- Simplify doc viewing flow (direct markdown content instead of parsed structure)
- Update electron main process to only use api folder for docs
- Add blueprint loading from docs/blueprint.md dynamically
- Fix sidebar file selection path matching
- Update preload scripts for new API structure
2026-03-18 18:50:26 +08:00

146 lines
3.6 KiB
JavaScript

import { app, BrowserWindow, ipcMain } from 'electron';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let mainWindow = null;
const LOG_FILE = path.join(__dirname, '..', 'electron.log');
function log(...args) {
const msg = args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a)).join(' ');
const ts = new Date().toISOString();
const line = `[${ts}] [main] ${msg}\n`;
fs.appendFileSync(LOG_FILE, line);
console.log(line.trim());
}
function parseArgs() {
const args = { docs: null, headless: false };
const docsIndex = process.argv.indexOf('--docs');
if (docsIndex !== -1 && process.argv[docsIndex + 1]) {
args.docs = process.argv[docsIndex + 1];
}
if (process.argv.includes('--headless')) {
args.headless = true;
}
return args;
}
const args = parseArgs();
function getIndexPath() {
if (app.isPackaged) {
return path.join(__dirname, '..', 'dist', 'index.html');
}
return path.join(__dirname, '..', 'dist', 'index.html');
}
function createWindow() {
const indexPath = getIndexPath();
log('Creating window, indexPath:', indexPath);
log('__dirname:', __dirname);
log('dist exists:', fs.existsSync(path.join(__dirname, '..', 'dist')));
log('index.html exists:', fs.existsSync(indexPath));
mainWindow = new BrowserWindow({
show: !args.headless,
width: 1200,
height: 800,
webPreferences: {
preload: path.join(__dirname, 'preload.cjs'),
contextIsolation: true,
nodeIntegration: false,
devTools: true,
},
});
mainWindow.loadFile(indexPath);
mainWindow.webContents.on('did-fail-load', (_event, errorCode, errorDesc) => {
log('FAIL LOAD:', errorCode, errorDesc);
});
mainWindow.webContents.on('did-finish-load', () => {
log('Page finished loading');
});
if (!args.headless) {
mainWindow.webContents.openDevTools();
}
mainWindow.on('closed', () => {
mainWindow = null;
});
}
ipcMain.handle('list-docs-files', (_event, basePath) => {
const docsPath = path.join(basePath, 'api');
if (!fs.existsSync(docsPath)) {
return [];
}
const result = [];
function walkDir(dir, baseDir) {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
walkDir(fullPath, baseDir);
} else if (entry.name.endsWith('.md')) {
const relativePath = path.relative(baseDir, fullPath);
result.push({
name: entry.name.replace('.md', ''),
path: fullPath,
relativePath: relativePath.replace(/\\/g, '/')
});
}
}
}
walkDir(docsPath, docsPath);
return result;
});
ipcMain.handle('read-doc-file', (_event, filePath) => {
if (!fs.existsSync(filePath)) {
return null;
}
return fs.readFileSync(filePath, 'utf-8');
});
ipcMain.on('renderer-log', (_event, level, ...args) => {
log(`[renderer][${level}]`, ...args);
});
app.whenReady().then(() => {
log('App ready, creating window');
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
}).catch(err => {
log('FATAL ERROR:', err);
process.exit(1);
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
process.on('uncaughtException', (err) => {
log('Uncaught exception:', err);
});
process.on('unhandledRejection', (reason) => {
log('Unhandled rejection:', reason);
});