-
Notifications
You must be signed in to change notification settings - Fork 247
feat(data-modeling): export diagram to png COMPASS-9449 #7055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 20 commits
cce62ac
a43ed7e
39793bd
e47687e
dbc1ca0
6d8de76
087936c
6c21b63
b7d8dcf
7c4c14e
f9bcc51
b65f4c8
adc19a1
6c41226
86e8a92
7fa0480
8c5c14d
ffef0b7
88a227b
487a2b9
56aadf0
42b9c8e
68d00cf
7b220ca
8a08691
3b16d26
c035d7a
cda452d
a47faf8
15bf100
ae8e3c6
840c078
4bca5a2
0a9a6e4
aab9aca
c6bc709
010b0c6
b34c1f5
6136ff4
fd4ee26
065851a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import React from 'react'; | ||
| import { | ||
| getNodesBounds, | ||
| getViewportForBounds, | ||
| DiagramProvider, | ||
| Diagram, | ||
| } from '@mongodb-js/diagramming'; | ||
| import type { | ||
| useDiagram, | ||
| NodeProps, | ||
| NodeType, | ||
| EdgeProps, | ||
| Marker, | ||
| } from '@mongodb-js/diagramming'; | ||
| import type { StaticModel } from './data-model-storage'; | ||
| import ReactDOM from 'react-dom'; | ||
| import { toPng } from 'html-to-image'; | ||
| import { rafraf, spacing } from '@mongodb-js/compass-components'; | ||
|
|
||
| // TODO: Export these methods (and type) from the diagramming package | ||
mabaasit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| type DiagramInstance = ReturnType<typeof useDiagram>; | ||
| function mapNodeToDiagramNode( | ||
| node: ReturnType<DiagramInstance['getNodes']>[number] | ||
| ): NodeProps { | ||
| const { data, type, ...restOfNode } = node; | ||
| return { | ||
| ...restOfNode, | ||
| ...(data as any), // TODO: Type data (or expose these methods from the diagramming package) | ||
| type: type as NodeType, | ||
| }; | ||
| } | ||
| function mapEdgeToDiagramEdge( | ||
| edge: ReturnType<DiagramInstance['getEdges']>[number] | ||
| ): EdgeProps { | ||
| const { markerStart, markerEnd, ...restOfEdge } = edge; | ||
|
|
||
| // The diagramming package only allows string based markers | ||
| if (typeof markerStart !== 'string' || typeof markerEnd !== 'string') { | ||
| throw new Error('Unexpected edge with non-string markers'); | ||
| } | ||
|
|
||
| return { | ||
| ...restOfEdge, | ||
| markerEnd: markerEnd.replace('end-', '') as Marker, | ||
| markerStart: markerStart.replace('start-', '') as Marker, | ||
| }; | ||
| } | ||
|
|
||
| function moveSvgDefsToViewportElement( | ||
| container: Element, | ||
| targetElement: Element | ||
| ) { | ||
| const markerDef = container.querySelector('svg defs'); | ||
| if (!markerDef) { | ||
| return; | ||
| } | ||
| const diagramSvgElements = targetElement.querySelectorAll('svg'); | ||
| diagramSvgElements.forEach((svg) => { | ||
| const pathsWithMarkers = svg.querySelectorAll( | ||
| 'path[marker-end], path[marker-start]' | ||
| ); | ||
| if (pathsWithMarkers.length !== 0) { | ||
| const clonedDef = markerDef.cloneNode(true) as SVGMarkerElement; | ||
| svg.insertBefore(clonedDef, svg.firstChild); | ||
| } | ||
| }); | ||
| markerDef.remove(); | ||
| } | ||
|
|
||
| export async function exportToPng( | ||
| fileName: string, | ||
| containerRef: React.RefObject<HTMLDivElement>, | ||
| diagram: DiagramInstance | ||
| ) { | ||
| const container = containerRef.current; | ||
| if (!container) { | ||
| throw new Error('Container reference is not set'); | ||
| } | ||
| const dataUri = await getExportPngDataUri(container, diagram); | ||
| downloadFile(dataUri, fileName, () => { | ||
| ReactDOM.unmountComponentAtNode(container); | ||
| }); | ||
| } | ||
|
|
||
| export function getExportPngDataUri( | ||
| container: HTMLDivElement, | ||
| diagram: DiagramInstance | ||
| ): Promise<string> { | ||
| return new Promise<string>((resolve, reject) => { | ||
mabaasit marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const nodes = diagram.getNodes(); | ||
| const edges = diagram.getEdges(); | ||
| ReactDOM.render( | ||
| <DiagramProvider> | ||
| <Diagram | ||
| edges={edges.map(mapEdgeToDiagramEdge)} | ||
| nodes={nodes.map(mapNodeToDiagramNode)} | ||
| onlyRenderVisibleElements={false} | ||
| /> | ||
| </DiagramProvider>, | ||
| container, | ||
| () => { | ||
| rafraf(() => { | ||
| // For export we are selecting react-flow__viewport element, | ||
| // which contains the export canvas. It excludes diagram | ||
| // title, minmap, and other UI elements. However, it also | ||
| // excludes the svg defs that are currently outside of this element. | ||
| // So, when exporting, we need to include those defs as well so that | ||
| // edge markers are exported correctly. | ||
|
Comment on lines
+88
to
+89
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be easier to hide what we don't need instead of doing this?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this configurable in the diagramming package?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'll evaluate this, last time (in poc) i checked this, it did not give me good results.
These custom marker are rendered here in the package. I am not sure if i follow what you mean by making it configurable?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, comment somehow attached to the wrong place, should've been this part: We're doing all this to avoid including controls in the diagram screenshot. Can we change diagramming package to allow us to render it with hidden controls? Then you don't need to do anything to target an element that doesn't include everything that you need
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, yeah that should be possible with the package. but again it depends on if we are able to export the root container (
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to do this and was not able to get it to export correctly using this approach. |
||
| const viewportElement = container.querySelector( | ||
| '.react-flow__viewport' | ||
| ); | ||
|
Comment on lines
+90
to
+92
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: instead of relying on arbitrary frames you can probably wait for this element to be in the view (similar to what we do in tests sometimes) |
||
| if (!viewportElement) { | ||
| throw new Error('Diagram element not found'); | ||
| } | ||
|
|
||
| const bounds = getNodesBounds(nodes); | ||
| const transform = getViewportForBounds( | ||
| bounds, | ||
| bounds.width, | ||
| bounds.height, | ||
| 0.5, // Minimum zoom | ||
| 2, // Maximum zoom | ||
| `${spacing[400]}px` // 16px padding | ||
| ); | ||
| // Moving svg defs to the viewport element | ||
| moveSvgDefsToViewportElement(container, viewportElement); | ||
| rafraf(() => { | ||
| toPng(viewportElement as HTMLElement, { | ||
| backgroundColor: '#fff', | ||
| pixelRatio: 2, | ||
| width: bounds.width, | ||
| height: bounds.height, | ||
| style: { | ||
| width: `${bounds.width}px`, | ||
| height: `${bounds.height}px`, | ||
| transform: `translate(${transform.x}px, ${transform.y}px) scale(${transform.zoom})`, | ||
| }, | ||
| }) | ||
| .then(resolve) | ||
| .catch(reject); | ||
| }); | ||
| }); | ||
| } | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| export function exportToJson(fileName: string, model: StaticModel) { | ||
| const json = getExportJsonFromModel(model); | ||
| const blob = new Blob([JSON.stringify(json, null, 2)], { | ||
| type: 'application/json', | ||
| }); | ||
| const url = window.URL.createObjectURL(blob); | ||
| downloadFile(url, fileName, () => { | ||
| window.URL.revokeObjectURL(url); | ||
| }); | ||
| } | ||
|
|
||
| export function getExportJsonFromModel({ | ||
| collections, | ||
| relationships, | ||
| }: StaticModel) { | ||
| return { | ||
| collections: Object.fromEntries( | ||
| collections.map((collection) => { | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const { ns, jsonSchema, ...ignoredProps } = collection; | ||
| return [ns, { ns, jsonSchema }]; | ||
| }) | ||
| ), | ||
| relationships, | ||
| }; | ||
| } | ||
|
|
||
| function downloadFile(uri: string, fileName: string, cleanup: () => void) { | ||
| const link = document.createElement('a'); | ||
| link.download = fileName; | ||
| link.href = uri; | ||
| link.click(); | ||
| setTimeout(() => { | ||
| link.remove(); | ||
| cleanup(); | ||
| }, 0); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is way too much logic in the UI here (like if you see that you're keeping abort controllers in render, you probably should start thinking about that), this should really be an action in the store instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, i'll add another slice for export. Or, I can do it as a follow up if that sounds okay to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow-up sounds good, let's not hold this code in the branch for too long 👍