Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 12 additions & 32 deletions src/component/elements/export/ExportContent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ExportSettings } from '@zakodium/nmrium-core';
import type { ReactNode } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useState } from 'react';
import { createPortal } from 'react-dom';

import { ExportOptionsModal } from './ExportOptionsModal.js';
Expand All @@ -10,8 +10,6 @@ import { INITIAL_BASIC_EXPORT_OPTIONS } from './utilities/getExportOptions.js';
import { getSizeInPixel } from './utilities/getSizeInPixel.js';
import { transferDocumentStyles } from './utilities/transferDocumentStyles.js';

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

export interface BaseExportProps {
onExportOptionsChange: (options: ExportSettings) => void;
defaultExportOptions?: ExportSettings;
Expand Down Expand Up @@ -107,25 +105,15 @@ function InnerPrintFrame(props: InnerExportFrameProps) {
const { children, exportOptions, onExportReady, renderOptions } = props;
const { width, height } = getSizeInPixel(exportOptions);

const frameRef = useRef<HTMLIFrameElement>(null);
const [content, setContent] = useState<HTMLElement>();

const load = useCallback(() => {
const contentWindow = frameRef.current?.contentWindow;
if (!contentWindow) return;
const document = contentWindow.document;

setContent(document.body);
const [iframeDocument, setIframeDocument] = useState<Document>();

function refHandler(frame: HTMLIFrameElement | null) {
if (!frame) return;
const document = frame.contentWindow?.document;
if (!document) return;
setIframeDocument(document);
transferDocumentStyles(document);
return contentWindow;
}, []);

useEffect(() => {
if (!isFirefox) {
load();
}
}, [load]);
}

const exportWidthInPixel = Math.round(width);
const exportHeightInPixel = Math.round(height);
Expand All @@ -151,26 +139,18 @@ function InnerPrintFrame(props: InnerExportFrameProps) {
exportHeight={exportHeightInPixel}
>
<iframe
ref={frameRef}
ref={refHandler}
style={{
width: 0,
height: 0,
border: 'none',
}}
onLoad={() => {
if (isFirefox) {
load();
}
}}
>
{content &&
{iframeDocument &&
createPortal(
<RenderDetector
onRender={() => {
const document = frameRef.current?.contentWindow?.document;
if (document) {
onExportReady(document.documentElement, exportOptions);
}
onExportReady(iframeDocument.documentElement, exportOptions);
}}
style={{
width: `${widthInPixel}px`,
Expand All @@ -180,7 +160,7 @@ function InnerPrintFrame(props: InnerExportFrameProps) {
>
{children}
</RenderDetector>,
content,
iframeDocument.body,
)}
</iframe>
</ExportSettingsProvider>
Expand Down
Loading