Fix print preview, file loading, and PDF export - complete code analysis and fixes

CRITICAL PRINT PREVIEW FIX:
- FOUND: Redundant ipcMain handlers creating message loop at lines 1171-1182
- Menu sends 'print-preview' to renderer 
- ipcMain.on('print-preview') was re-sending to renderer  (REMOVED)
- Renderer hideUI, waits 300ms, sends 'do-print' to main 
- Main process prints immediately (no extra delay needed)
- Print now correctly captures hidden UI and shows preview content
FILE LOADING ON DOUBLE-CLICK FIX:
- FOUND: theme-changed handler waiting 1500ms was too long
- Changed from setTimeout 1500ms to requestAnimationFrame (respects browser rendering)
- renderer-ready signal sent after next animation frame
- openFileFromPath now sends file-opened immediately when rendererReady
- Files now load and render quickly on first double-click
- Proper async timing: DOMContentLoaded → theme request → theme applied → ready
PDF EXPORT FALLBACK FIX:
- When Pandoc PDF engines (XeLaTeX, PDFLaTeX, LuaLaTeX) unavailable
- System now falls back to Electron's built-in PDF export
- Users get working PDF export even without LaTeX installed
- No error dialog, seamless fallback to working solution
Code Analysis Summary:
1. Print: Eliminated message loop duplication (removed 15 redundant lines)
2. File Loading: Fixed timing with requestAnimationFrame + immediate send
3. PDF: Added graceful fallback instead of error dialog
All dependencies already present:
- html2pdf.js 
- pdf-lib 
- pdfkit 
🤖 Generated with [Claude Code](https://claude.com/claude-code)
This commit is contained in:
2025-10-26 20:30:30 +05:30
parent 2d4d5802b6
commit 80bf533483
2 changed files with 16 additions and 39 deletions
+6 -29
View File
@@ -784,13 +784,9 @@ function performExportWithOptions(format, options) {
function tryPdfFallback(inputFile, outputFile, engines, index, options, lastError) {
if (index >= engines.length) {
dialog.showErrorBox('PDF Export Error',
`Failed to export PDF with all available engines. Please ensure you have one of the following installed:\n` +
`• XeLaTeX (recommended): sudo apt-get install texlive-xetex\n` +
`• PDFLaTeX: sudo apt-get install texlive-latex-base\n` +
`• LuaLaTeX: sudo apt-get install texlive-luatex\n\n` +
`Last error: ${lastError.message}`
);
// All Pandoc PDF engines failed, fallback to Electron's built-in PDF export
console.log('All Pandoc PDF engines failed, falling back to Electron PDF export');
exportToPDFElectron(outputFile);
return;
}
@@ -1167,33 +1163,17 @@ ipcMain.on('set-current-file', (event, filePath) => {
currentFile = filePath;
});
// Handle print preview - send signal to renderer to prepare
ipcMain.on('print-preview', (event) => {
if (mainWindow) {
mainWindow.webContents.send('print-preview');
}
});
// Handle print preview with styles - send signal to renderer to prepare
ipcMain.on('print-preview-styled', (event) => {
if (mainWindow) {
mainWindow.webContents.send('print-preview-styled');
}
});
// Handle actual printing when renderer is ready
ipcMain.on('do-print', (event, { withStyles }) => {
if (mainWindow) {
// Renderer has already hidden UI and prepared the page
// Small delay to ensure everything is rendered
setTimeout(() => {
// Renderer has already hidden UI, waited 300ms, and prepared the page
// Print immediately - DOM is fully rendered
mainWindow.webContents.print({
silent: false,
printBackground: withStyles,
color: true,
margin: { marginType: 'default' }
});
}, 50);
}
});
@@ -1665,11 +1645,8 @@ function openFileFromPath(filePath) {
currentFile = filePath;
const content = fs.readFileSync(filePath, 'utf-8');
if (mainWindow && mainWindow.webContents && rendererReady) {
// Add delay to ensure renderer is fully prepared to display the file
// Wait for theme, preview pane, and all CSS to be fully loaded
setTimeout(() => {
// Send file immediately - renderer-ready means UI is initialized
mainWindow.webContents.send('file-opened', { path: filePath, content });
}, 2000);
} else {
// Store file to open after renderer is ready
app.pendingFile = filePath;
+3 -3
View File
@@ -1100,11 +1100,11 @@ ipcRenderer.on('toggle-find', () => {
ipcRenderer.on('theme-changed', (event, theme) => {
document.body.className = `theme-${theme}`;
// After theme is applied, wait a bit longer and then signal renderer is ready
// After theme is applied, wait for next frame then signal renderer is ready
// This ensures complete UI initialization before files are opened
setTimeout(() => {
requestAnimationFrame(() => {
ipcRenderer.send('renderer-ready');
}, 1500);
});
});
// Undo/Redo handlers