Skip to content

Commit

Permalink
fix: print in firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Jul 18, 2024
1 parent 2d44be2 commit aea04bc
Showing 1 changed file with 57 additions and 15 deletions.
72 changes: 57 additions & 15 deletions src/component/elements/print/PrintContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import {
} from '@blueprintjs/core';
import { css } from '@emotion/react';
import { PageSizeName, PrintPageOptions } from 'nmr-load-save';
import { CSSProperties, ReactNode, useEffect, useRef, useState } from 'react';
import {
CSSProperties,
ReactNode,
useCallback,
useEffect,
useRef,
useState,
} from 'react';
import { createPortal } from 'react-dom';
import { Controller, useForm } from 'react-hook-form';

Expand All @@ -20,6 +27,8 @@ import { Select2Controller } from '../Select2Controller';

import { PrintProvider } from './PrintProvider';

const isFirefox = navigator.userAgent.toLowerCase().includes("firefox");

type Layout = 'portrait' | 'landscape';

interface BasePrintProps {
Expand All @@ -34,7 +43,7 @@ interface InnerPrintFrameProps {
}
interface PrintFrameProps
extends InnerPrintFrameProps,
Partial<BasePrintProps> {}
Partial<BasePrintProps> { }

interface PageSize {
name: PageSizeName;
Expand Down Expand Up @@ -168,7 +177,6 @@ export function PrintContent(props: PrintFrameProps) {
/>
);
}

return (
<InnerPrintFrame
printPageOptions={pageOptions}
Expand Down Expand Up @@ -204,7 +212,16 @@ export function InnerPrintFrame(props: InnerPrintFrameProps) {
const { width = 0, height = 0 } =
pageSizes.find((pageItem) => pageItem.name === size)?.[layout] || {};

useEffect(() => {
const handleAfterPrint = useCallback(() => {
onAfterPrint?.();
}, [onAfterPrint]);
const handleBeforePrint = useCallback(() => {
onBeforePrint?.();
}, [onBeforePrint]);



const load = useCallback(() => {
const contentWindow = frameRef.current?.contentWindow;
if (!contentWindow) return;
const document = contentWindow.document;
Expand All @@ -214,28 +231,53 @@ export function InnerPrintFrame(props: InnerPrintFrameProps) {
transferStyles(document);
appendPrintPageStyle(document, { size, layout, margin });

function handleAfterPrint() {
onAfterPrint?.();
}
function handleBeforePrint() {
onBeforePrint?.();
}
contentWindow.addEventListener('beforeprint', handleBeforePrint);
contentWindow.addEventListener('afterprint', handleAfterPrint);
contentWindow.addEventListener('beforeprint', handleBeforePrint);

return contentWindow;
}, [handleAfterPrint, handleBeforePrint, layout, margin, size])

useEffect(() => {
const contentWindow = frameRef.current?.contentWindow;

if (!isFirefox) {
load();
}

return () => {

if (!contentWindow) return;

contentWindow.removeEventListener('afterprint', handleAfterPrint);
contentWindow.removeEventListener('beforeprint', handleBeforePrint);
};
}, [layout, onBeforePrint, onAfterPrint, margin, size]);
}, [onBeforePrint, onAfterPrint, handleBeforePrint, handleAfterPrint, size, layout, margin, load]);



return (
<PrintProvider width={width} height={height} margin={margin}>
<iframe ref={frameRef} style={{ width: 0, height: 0, border: 'none' }}>
<iframe
ref={frameRef}
style={{
width: 0,
height: 0,
border: 'none',
}}
onLoad={() => {
if (isFirefox) {
load();
}
}
}
>
{content &&
createPortal(
<RenderContainer
onRenderComplete={() => frameRef.current?.contentWindow?.print()}
onRenderComplete={() => {
frameRef.current?.contentWindow?.focus();
frameRef.current?.contentWindow?.print();
}}
style={{
width: `${width - margin}cm`,
height: `${height - margin}cm`,
Expand All @@ -247,7 +289,7 @@ export function InnerPrintFrame(props: InnerPrintFrameProps) {
content,
)}
</iframe>
</PrintProvider>
</PrintProvider >
);
}

Expand Down

0 comments on commit aea04bc

Please sign in to comment.