Skip to content

Commit

Permalink
fix: trigger printing after the NMRium viewer is rendered and rescale…
Browse files Browse the repository at this point in the history
…d to an A4 page
  • Loading branch information
hamed-musallam committed Jul 10, 2024
1 parent ae9d1f7 commit 489c4cb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
18 changes: 13 additions & 5 deletions src/component/main/InnerNMRiumContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,23 @@ export function InnerNMRiumContents(props: InnerNMRiumContentsProps) {
const [printFlag, setPrintFlag] = useState(false);

useEffect(() => {
function handleBeforePrint() {
setPrintFlag(true);
}
function handleAfterPrint() {
setPrintFlag(false);
}

window.addEventListener('beforeprint', handleBeforePrint);
function handleKeyDow(event) {
if ((event.ctrlKey || event.metaKey) && event.key === 'p') {
event.preventDefault();
setPrintFlag(true);
}
}

window.addEventListener('keydown', handleKeyDow);
window.addEventListener('afterprint', handleAfterPrint);

return () => {
window.removeEventListener('beforeprint', handleBeforePrint);
window.removeEventListener('afterprint', handleAfterPrint);
window.removeEventListener('keydown', handleKeyDow);
};
}, []);

Expand Down Expand Up @@ -187,6 +191,10 @@ export function InnerNMRiumContents(props: InnerNMRiumContentsProps) {
style={{
width: '297mm',
height: '210mm',
padding: '0.5cm',
}}
onRender={() => {
window.print();
}}
/>
)}
Expand Down
25 changes: 22 additions & 3 deletions src/component/main/NMRiumViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CSSProperties, RefObject } from 'react';
import { CSSProperties, RefObject, useDeferredValue, useEffect } from 'react';

import Viewer1D from '../1d/Viewer1D';
import FloatMoleculeStructures from '../1d-2d/components/FloatMoleculeStructures';
Expand All @@ -11,11 +11,29 @@ interface NMRiumViewerProps {
emptyText: NMRiumProps['emptyText'];
viewerRef: RefObject<HTMLDivElement>;
style?: CSSProperties;
onRender?: () => void;
}

export function NMRiumViewer(props: NMRiumViewerProps) {
const { emptyText, viewerRef, style = {} } = props;
const { displayerMode } = useChartData();
const { emptyText, viewerRef, onRender, style = {} } = props;
const { displayerMode, width, height } = useChartData();
const renderDimension = useDeferredValue({ width, height });
useEffect(() => {
if (typeof onRender !== 'function') {
return;
}

requestAnimationFrame(() => {
setTimeout(() => {
if (
renderDimension.width !== width ||
renderDimension.height !== height
) {
onRender();
}
}, 0);
});
}, [onRender, width, height, renderDimension.width, renderDimension.height]);

return (
<div
Expand All @@ -26,6 +44,7 @@ export function NMRiumViewer(props: NMRiumViewerProps) {
width: '100%',
height: '100%',
position: 'relative',
backgroundColor: 'white',
...style,
}}
>
Expand Down

0 comments on commit 489c4cb

Please sign in to comment.