Skip to content

Commit 56aadf0

Browse files
committed
move container to the export png
1 parent 487a2b9 commit 56aadf0

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

packages/compass-data-modeling/src/components/export-diagram-modal.tsx

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useMemo, useRef, useState } from 'react';
1+
import React, { useCallback, useState } from 'react';
22
import {
33
Button,
44
css,
@@ -23,7 +23,7 @@ import { connect } from 'react-redux';
2323
import type { DataModelingState } from '../store/reducer';
2424
import type { StaticModel } from '../services/data-model-storage';
2525
import { exportToJson, exportToPng } from '../services/export-diagram';
26-
import { getNodesBounds, useDiagram } from '@mongodb-js/diagramming';
26+
import { useDiagram } from '@mongodb-js/diagramming';
2727

2828
const nbsp = '\u00a0';
2929

@@ -64,8 +64,6 @@ const ExportDiagramModal = ({
6464
const [exportFormat, setExportFormat] = useState<'png' | 'json' | null>(null);
6565
const diagram = useDiagram();
6666
const [isExporting, setIsExporting] = useState(false);
67-
const exportDiagramContainerRef = useRef<HTMLDivElement>(null);
68-
const bounds = useMemo(() => getNodesBounds(diagram.getNodes()), [diagram]);
6967

7068
const onExport = useCallback(async () => {
7169
if (!exportFormat || !model) {
@@ -75,7 +73,7 @@ const ExportDiagramModal = ({
7573
if (exportFormat === 'json') {
7674
exportToJson(diagramLabel, model);
7775
} else if (exportFormat === 'png') {
78-
await exportToPng(diagramLabel, exportDiagramContainerRef, diagram);
76+
await exportToPng(diagramLabel, diagram);
7977
}
8078
onCloseClick();
8179
setIsExporting(false);
@@ -131,18 +129,6 @@ const ExportDiagramModal = ({
131129
</div>
132130
</RadioGroup>
133131
</div>
134-
{/* Container where we render export diagram when exporting png */}
135-
<div
136-
style={{
137-
width: bounds.width,
138-
height: bounds.height,
139-
// Fixed at bottom right corner
140-
position: 'fixed',
141-
top: '100vh',
142-
left: '100vw',
143-
}}
144-
ref={exportDiagramContainerRef}
145-
/>
146132
</ModalBody>
147133
<ModalFooter className={footerStyles}>
148134
<Button

packages/compass-data-modeling/src/services/export-diagram.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,25 @@ function moveSvgDefsToViewportElement(
6060
'path[marker-end], path[marker-start]'
6161
);
6262
if (pathsWithMarkers.length !== 0) {
63-
const clonedDef = markerDef.cloneNode(true) as SVGMarkerElement;
63+
const clonedDef = markerDef.cloneNode(true);
6464
svg.insertBefore(clonedDef, svg.firstChild);
6565
}
6666
});
6767
markerDef.remove();
6868
}
6969

70-
export async function exportToPng(
71-
fileName: string,
72-
containerRef: React.RefObject<HTMLDivElement>,
73-
diagram: DiagramInstance
74-
) {
75-
const container = containerRef.current;
76-
if (!container) {
77-
throw new Error('Container reference is not set');
78-
}
70+
export async function exportToPng(fileName: string, diagram: DiagramInstance) {
71+
const container = document.createElement('div');
72+
container.setAttribute('data-testid', 'export-diagram-container');
73+
// Push it out of the viewport
74+
container.style.position = 'fixed';
75+
container.style.top = '100vh';
76+
container.style.left = '100vw';
77+
document.body.appendChild(container);
78+
7979
const dataUri = await getExportPngDataUri(container, diagram);
8080
downloadFile(dataUri, fileName, () => {
81-
ReactDOM.unmountComponentAtNode(container);
81+
container.remove();
8282
});
8383
}
8484

0 commit comments

Comments
 (0)