Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2296] Create a dedicated hook for the export to image action
Also move the code for diagram panel in a dedicated folder Bug: #2296 Signed-off-by: Axel RICHARD <axel.richard@obeo.fr>
- Loading branch information
1 parent
d2d98bd
commit a9981b1
Showing
9 changed files
with
84 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
50 changes: 50 additions & 0 deletions
50
...ams/frontend/sirius-components-diagrams-reactflow/src/renderer/panel/useExportToImage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Obeo and others. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
import { toSvg } from 'html-to-image'; | ||
import { useCallback } from 'react'; | ||
import { Rect, Transform, getRectOfNodes, getTransformForBounds, useReactFlow } from 'reactflow'; | ||
import { UseExportToImage } from './useExportToImage.types'; | ||
|
||
const downloadImage = (dataUrl: string) => { | ||
const a: HTMLAnchorElement = document.createElement('a'); | ||
a.setAttribute('download', 'diagram.svg'); | ||
a.setAttribute('href', dataUrl); | ||
a.click(); | ||
}; | ||
|
||
export const useExportToImage = (): UseExportToImage => { | ||
const reactFlow = useReactFlow(); | ||
|
||
const exportToImage = useCallback(() => { | ||
const nodesBounds: Rect = getRectOfNodes(reactFlow.getNodes()); | ||
const imageWidth: number = nodesBounds.width; | ||
const imageHeight: number = nodesBounds.height; | ||
const transform: Transform = getTransformForBounds(nodesBounds, imageWidth, imageHeight, 0.5, 2, 0.2); | ||
|
||
const viewport: HTMLElement | null = document.querySelector<HTMLElement>('.react-flow__viewport'); | ||
if (viewport) { | ||
toSvg(viewport, { | ||
backgroundColor: '#ffffff', | ||
width: imageWidth, | ||
height: imageHeight, | ||
style: { | ||
width: imageWidth.toString(), | ||
height: imageHeight.toString(), | ||
transform: `translate(${transform[0]}px, ${transform[1]}px) scale(${transform[2]})`, | ||
}, | ||
}).then(downloadImage); | ||
} | ||
}, []); | ||
return { exportToImage }; | ||
}; |
16 changes: 16 additions & 0 deletions
16
...rontend/sirius-components-diagrams-reactflow/src/renderer/panel/useExportToImage.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Obeo and others. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
export interface UseExportToImage { | ||
exportToImage: () => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters