Skip to content

Build your personal knowledge base with Trilium Notes

feat(print_preview): display dialog first and preview second

Elian Doranca29a11b097d
diff --git a/apps/client/src/widgets/NoteDetail.tsx b/apps/client/src/widgets/NoteDetail.tsx
index 8476e32172..1d7b75b59a 100644
--- a/apps/client/src/widgets/NoteDetail.tsx
+++ b/apps/client/src/widgets/NoteDetail.tsx
@@ -147,19 +147,11 @@ export default function NoteDetail() {
toast.closePersistent("printing");
handlePrintReport(printReport);
};
- const onPreviewResult = (_e: any, { buffer, notePath }: { buffer: Uint8Array; notePath: string }) => {
- toast.closePersistent("printing");
- if (note) {
- appContext.triggerCommand("showPrintPreview", { pdfBuffer: buffer, note, notePath });
- }
- };
ipcRenderer.on("print-progress", onPrintProgress);
ipcRenderer.on("print-done", onPrintDone);
- ipcRenderer.on("export-as-pdf-preview-result", onPreviewResult);
return () => {
ipcRenderer.off("print-progress", onPrintProgress);
ipcRenderer.off("print-done", onPrintDone);
- ipcRenderer.off("export-as-pdf-preview-result", onPreviewResult);
};
}, [note]);
@@ -185,20 +177,8 @@ export default function NoteDetail() {
// PDF printing is handled by the PDF viewer's own print mechanism.
if (note.type === "file" && note.mime === "application/pdf") return;
- if (isElectron()) {
- // On Electron, open the print preview dialog. Actual print/PDF actions
- // are triggered from the dialog's footer buttons.
- showToast("exporting_pdf");
- const { ipcRenderer } = dynamicRequire("electron");
- ipcRenderer.send("export-as-pdf-preview", {
- title: note.title,
- notePath: noteContext.notePath,
- pageSize: note.getAttributeValue("label", "printPageSize") ?? "Letter",
- landscape: note.hasAttribute("label", "printLandscape"),
- scale: parseFloat(note.getAttributeValue("label", "printScale") ?? "1") || 1,
- margins: note.getAttributeValue("label", "printMargins") ?? "default",
- pageRanges: ""
- });
+ if (isElectron() && noteContext.notePath) {
+ appContext.triggerCommand("showPrintPreview", { note, notePath: noteContext.notePath });
return;
}
diff --git a/apps/client/src/widgets/dialogs/print_preview.tsx b/apps/client/src/widgets/dialogs/print_preview.tsx
index 4931260a04..88a463be99 100644
--- a/apps/client/src/widgets/dialogs/print_preview.tsx
+++ b/apps/client/src/widgets/dialogs/print_preview.tsx
@@ -75,7 +75,6 @@ function isValidPageRanges(value: string): boolean {
}
export interface PrintPreviewData {
- pdfBuffer: Uint8Array;
note: FNote;
notePath: string;
}
@@ -96,7 +95,7 @@ export default function PrintPreviewDialog() {
const bufferRef = useRef<Uint8Array>();
const notePathRef = useRef("");
const pdfUrlRef = useRef<string>();
- const generationRef = useRef(0);
+
const [landscape, setLandscape] = useNoteLabelBoolean(note, "printLandscape");
const [pageSize, setPageSize] = useNoteLabelWithDefault(note, "printPageSize", "Letter");
@@ -114,8 +113,6 @@ export default function PrintPreviewDialog() {
const [printers, setPrinters] = useState<PrinterInfo[]>([]);
const [destination, setDestination] = useState<string>(DESTINATION_PDF);
- const skipNextRegenRef = useRef(false);
-
useEffect(() => {
if (!shown || !isElectron()) return;
const { ipcRenderer } = dynamicRequire("electron");
@@ -141,15 +138,11 @@ export default function PrintPreviewDialog() {
}, []);
useTriliumEvent("showPrintPreview", (data: PrintPreviewData) => {
- // When the dialog is already open, it manages its own regeneration via
- // a persistent IPC listener. Ignore duplicate events from NoteDetail's
- // listener to avoid overwriting the preview with stale data.
if (shown) return;
- skipNextRegenRef.current = true;
setNote(data.note);
notePathRef.current = data.notePath;
- updatePreview(data.pdfBuffer);
+ setLoading(true);
setShown(true);
});
@@ -161,8 +154,6 @@ export default function PrintPreviewDialog() {
const { ipcRenderer } = dynamicRequire("electron");
const onResult = (_e: any, { buffer, error }: { buffer?: Uint8Array; error?: string }) => {
- if (generationRef.current <= 0) return;
-
toast.closePersistent("printing");
if (error) {
setLoading(false);
@@ -192,7 +183,6 @@ export default function PrintPreviewDialog() {
const regeneratePreview = useCallback((opts: PreviewOpts) => {
if (!isElectron()) return;
- ++generationRef.current;
setLoading(true);
const { ipcRenderer } = dynamicRequire("electron");
ipcRenderer.send("export-as-pdf-preview", {
@@ -207,10 +197,6 @@ export default function PrintPreviewDialog() {
useEffect(() => {
if (!shown || !pageRangesValid) return;
- if (skipNextRegenRef.current) {
- skipNextRegenRef.current = false;
- return;
- }
const handle = setTimeout(() => {
regeneratePreview({ landscape, pageSize, scale, margins: marginsStr, pageRanges: pageRanges.trim() });
}, 400);