@@ -40,35 +40,45 @@ export async function exportToPng(
4040 diagram : DiagramInstance ,
4141 signal ?: AbortSignal
4242) {
43- const container = document . createElement ( 'div' ) ;
44- container . setAttribute ( 'data-testid' , 'export-diagram-container' ) ;
45- // Push it out of the viewport
46- container . style . position = 'fixed' ;
47- container . style . top = '100vh' ;
48- container . style . left = '100vw' ;
49- document . body . appendChild ( container ) ;
50-
5143 const dataUri = await raceWithAbort (
52- getExportPngDataUri ( container , diagram ) ,
44+ getExportPngDataUri ( diagram ) ,
5345 signal ?? new AbortController ( ) . signal
5446 ) ;
55- downloadFile ( dataUri , fileName , ( ) => {
56- container . remove ( ) ;
57- } ) ;
47+ downloadFile ( dataUri , fileName ) ;
5848}
5949
60- export function getExportPngDataUri (
61- container : HTMLDivElement ,
62- diagram : DiagramInstance
63- ) : Promise < string > {
64- return new Promise < string > ( ( resolve , reject ) => {
50+ export function getExportPngDataUri ( diagram : DiagramInstance ) : Promise < string > {
51+ return new Promise < string > ( ( resolve , _reject ) => {
6552 const nodes = diagram . getNodes ( ) ;
6653 const edges = diagram . getEdges ( ) ;
54+ const bounds = getNodesBounds ( nodes ) ;
55+
56+ const container = document . createElement ( 'div' ) ;
57+ container . setAttribute ( 'data-testid' , 'export-diagram-container' ) ;
58+ // Push it out of the viewport
59+ container . style . position = 'fixed' ;
60+ container . style . top = '100vh' ;
61+ container . style . left = '100vw' ;
62+ container . style . width = `${ bounds . width } px` ;
63+ container . style . height = `${ bounds . height } px` ;
64+ document . body . appendChild ( container ) ;
65+
66+ const diagramEdges = edges . map ( mapEdgeToDiagramEdge ) ;
67+ const diagramNodes = nodes . map ( mapNodeToDiagramNode ) . map ( ( node ) => ( {
68+ ...node ,
69+ selected : false , // Dont show selected state (blue border)
70+ } ) ) ;
71+
72+ const reject = ( error : Error ) => {
73+ document . body . removeChild ( container ) ;
74+ _reject ( error ) ;
75+ } ;
76+
6777 ReactDOM . render (
6878 < DiagramProvider >
6979 < Diagram
70- edges = { edges . map ( mapEdgeToDiagramEdge ) }
71- nodes = { nodes . map ( mapNodeToDiagramNode ) }
80+ edges = { diagramEdges }
81+ nodes = { diagramNodes }
7282 onlyRenderVisibleElements = { false }
7383 />
7484 </ DiagramProvider > ,
@@ -88,7 +98,6 @@ export function getExportPngDataUri(
8898 return reject ( new Error ( 'Diagram element not found' ) ) ;
8999 }
90100
91- const bounds = getNodesBounds ( nodes ) ;
92101 const transform = getViewportForBounds (
93102 bounds ,
94103 bounds . width ,
@@ -97,6 +106,7 @@ export function getExportPngDataUri(
97106 2 , // Maximum zoom
98107 `${ spacing [ 400 ] } px` // 16px padding
99108 ) ;
109+
100110 // Moving svg defs to the viewport element
101111 moveSvgDefsToViewportElement ( container , viewportElement ) ;
102112 rafraf ( ( ) => {
@@ -147,13 +157,13 @@ export function getExportJsonFromModel({
147157 } ;
148158}
149159
150- function downloadFile ( uri : string , fileName : string , cleanup : ( ) => void ) {
160+ function downloadFile ( uri : string , fileName : string , cleanup ? : ( ) => void ) {
151161 const link = document . createElement ( 'a' ) ;
152162 link . download = fileName ;
153163 link . href = uri ;
154164 link . click ( ) ;
155165 setTimeout ( ( ) => {
156166 link . remove ( ) ;
157- cleanup ( ) ;
167+ cleanup ?. ( ) ;
158168 } , 0 ) ;
159169}
0 commit comments