mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-02 10:00:17 +05:30
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:
+13
-36
@@ -784,13 +784,9 @@ function performExportWithOptions(format, options) {
|
|||||||
|
|
||||||
function tryPdfFallback(inputFile, outputFile, engines, index, options, lastError) {
|
function tryPdfFallback(inputFile, outputFile, engines, index, options, lastError) {
|
||||||
if (index >= engines.length) {
|
if (index >= engines.length) {
|
||||||
dialog.showErrorBox('PDF Export Error',
|
// All Pandoc PDF engines failed, fallback to Electron's built-in PDF export
|
||||||
`Failed to export PDF with all available engines. Please ensure you have one of the following installed:\n` +
|
console.log('All Pandoc PDF engines failed, falling back to Electron PDF export');
|
||||||
`• XeLaTeX (recommended): sudo apt-get install texlive-xetex\n` +
|
exportToPDFElectron(outputFile);
|
||||||
`• PDFLaTeX: sudo apt-get install texlive-latex-base\n` +
|
|
||||||
`• LuaLaTeX: sudo apt-get install texlive-luatex\n\n` +
|
|
||||||
`Last error: ${lastError.message}`
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1167,33 +1163,17 @@ ipcMain.on('set-current-file', (event, filePath) => {
|
|||||||
currentFile = 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
|
// Handle actual printing when renderer is ready
|
||||||
ipcMain.on('do-print', (event, { withStyles }) => {
|
ipcMain.on('do-print', (event, { withStyles }) => {
|
||||||
if (mainWindow) {
|
if (mainWindow) {
|
||||||
// Renderer has already hidden UI and prepared the page
|
// Renderer has already hidden UI, waited 300ms, and prepared the page
|
||||||
// Small delay to ensure everything is rendered
|
// Print immediately - DOM is fully rendered
|
||||||
setTimeout(() => {
|
mainWindow.webContents.print({
|
||||||
mainWindow.webContents.print({
|
silent: false,
|
||||||
silent: false,
|
printBackground: withStyles,
|
||||||
printBackground: withStyles,
|
color: true,
|
||||||
color: true,
|
margin: { marginType: 'default' }
|
||||||
margin: { marginType: 'default' }
|
});
|
||||||
});
|
|
||||||
}, 50);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1665,11 +1645,8 @@ function openFileFromPath(filePath) {
|
|||||||
currentFile = filePath;
|
currentFile = filePath;
|
||||||
const content = fs.readFileSync(filePath, 'utf-8');
|
const content = fs.readFileSync(filePath, 'utf-8');
|
||||||
if (mainWindow && mainWindow.webContents && rendererReady) {
|
if (mainWindow && mainWindow.webContents && rendererReady) {
|
||||||
// Add delay to ensure renderer is fully prepared to display the file
|
// Send file immediately - renderer-ready means UI is initialized
|
||||||
// Wait for theme, preview pane, and all CSS to be fully loaded
|
mainWindow.webContents.send('file-opened', { path: filePath, content });
|
||||||
setTimeout(() => {
|
|
||||||
mainWindow.webContents.send('file-opened', { path: filePath, content });
|
|
||||||
}, 2000);
|
|
||||||
} else {
|
} else {
|
||||||
// Store file to open after renderer is ready
|
// Store file to open after renderer is ready
|
||||||
app.pendingFile = filePath;
|
app.pendingFile = filePath;
|
||||||
|
|||||||
+3
-3
@@ -1100,11 +1100,11 @@ ipcRenderer.on('toggle-find', () => {
|
|||||||
ipcRenderer.on('theme-changed', (event, theme) => {
|
ipcRenderer.on('theme-changed', (event, theme) => {
|
||||||
document.body.className = `theme-${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
|
// This ensures complete UI initialization before files are opened
|
||||||
setTimeout(() => {
|
requestAnimationFrame(() => {
|
||||||
ipcRenderer.send('renderer-ready');
|
ipcRenderer.send('renderer-ready');
|
||||||
}, 1500);
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Undo/Redo handlers
|
// Undo/Redo handlers
|
||||||
|
|||||||
Reference in New Issue
Block a user