Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
cce62ac
extract diagram editor toolbar:
mabaasit Jun 17, 2025
a43ed7e
add export modal
mabaasit Jun 17, 2025
39793bd
json export
mabaasit Jun 20, 2025
e47687e
tests
mabaasit Jun 20, 2025
dbc1ca0
close modal
mabaasit Jun 20, 2025
6d8de76
Merge branch 'main' into COMPASS-9448-diagram-json-export
mabaasit Jun 20, 2025
087936c
tests
mabaasit Jun 20, 2025
6c21b63
ensure test run
mabaasit Jun 20, 2025
b7d8dcf
fix toast
mabaasit Jun 20, 2025
7c4c14e
fix electron test
mabaasit Jun 20, 2025
f9bcc51
fix link
mabaasit Jun 20, 2025
b65f4c8
ensure its thrown
mabaasit Jun 23, 2025
adc19a1
asset number of selected collections
mabaasit Jun 23, 2025
6c41226
fix modal styles
mabaasit Jun 23, 2025
86e8a92
Merge branch 'main' into COMPASS-9448-diagram-json-export
mabaasit Jun 23, 2025
7fa0480
return null for tests
mabaasit Jun 24, 2025
8c5c14d
Merge branch 'main' into COMPASS-9448-diagram-json-export
mabaasit Jun 24, 2025
ffef0b7
remove comment
mabaasit Jun 24, 2025
88a227b
export image to png
mabaasit Jun 26, 2025
487a2b9
Merge branch 'main' of https://github.com/mongodb-js/compass into COM…
mabaasit Jun 26, 2025
56aadf0
move container to the export png
mabaasit Jun 26, 2025
42b9c8e
add ocr e2e test
mabaasit Jun 26, 2025
68d00cf
abortable export
mabaasit Jun 27, 2025
7b220ca
Merge branch 'main' of https://github.com/mongodb-js/compass into COM…
mabaasit Jun 27, 2025
8a08691
use package methods
mabaasit Jun 30, 2025
3b16d26
clean up
mabaasit Jun 30, 2025
c035d7a
Merge branch 'main' into COMPASS-9449-export-to-png
mabaasit Jun 30, 2025
cda452d
update diagramming
mabaasit Jul 1, 2025
a47faf8
Merge remote-tracking branch 'origin' into COMPASS-9449-export-to-png
mabaasit Jul 1, 2025
15bf100
npm install
mabaasit Jul 1, 2025
ae8e3c6
npm check
mabaasit Jul 1, 2025
840c078
fix test
mabaasit Jul 2, 2025
4bca5a2
Merge branch 'main' of https://github.com/mongodb-js/compass into COM…
mabaasit Jul 2, 2025
0a9a6e4
update npm
mabaasit Jul 2, 2025
aab9aca
Merge branch 'main' of https://github.com/mongodb-js/compass into COM…
mabaasit Jul 2, 2025
c6bc709
fix spaces issue
mabaasit Jul 2, 2025
010b0c6
fix popup for firefox
mabaasit Jul 2, 2025
b34c1f5
skip on win and lowercase test assertions
mabaasit Jul 3, 2025
6136ff4
cr comments
mabaasit Jul 3, 2025
fd4ee26
Merge branch 'main' of https://github.com/mongodb-js/compass into COM…
mabaasit Jul 3, 2025
065851a
npm install
mabaasit Jul 3, 2025
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
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/compass-data-modeling/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@mongodb-js/compass-telemetry": "^1.10.0",
"@mongodb-js/compass-user-data": "^0.7.2",
"@mongodb-js/compass-workspaces": "^0.42.0",
"html-to-image": "1.11.11",
"bson": "^6.10.3",
"compass-preferences-model": "^2.41.0",
"@mongodb-js/compass-app-registry": "^9.4.11",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from 'react';
import React, { useCallback, useMemo, useRef, useState } from 'react';
import {
Button,
css,
Expand All @@ -12,6 +12,7 @@ import {
Radio,
RadioGroup,
spacing,
SpinLoader,
} from '@mongodb-js/compass-components';
import {
closeExportModal,
Expand All @@ -21,7 +22,8 @@ import {
import { connect } from 'react-redux';
import type { DataModelingState } from '../store/reducer';
import type { StaticModel } from '../services/data-model-storage';
import { exportToJson } from '../services/export-diagram';
import { exportToJson, exportToPng } from '../services/export-diagram';
import { getNodesBounds, useDiagram } from '@mongodb-js/diagramming';

const nbsp = '\u00a0';

Expand Down Expand Up @@ -59,15 +61,25 @@ const ExportDiagramModal = ({
model,
onCloseClick,
}: ExportDiagramModalProps) => {
const [exportFormat, setExportFormat] = useState<'json' | null>(null);
const [exportFormat, setExportFormat] = useState<'png' | 'json' | null>(null);
const diagram = useDiagram();
const [isExporting, setIsExporting] = useState(false);
const exportDiagramContainerRef = useRef<HTMLDivElement>(null);
const bounds = useMemo(() => getNodesBounds(diagram.getNodes()), [diagram]);

const onExport = useCallback(() => {
const onExport = useCallback(async () => {
Copy link
Collaborator

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

Copy link
Collaborator Author

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.

Copy link
Collaborator

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 👍

if (!exportFormat || !model) {
return;
}
exportToJson(diagramLabel, model);
setIsExporting(true);
if (exportFormat === 'json') {
exportToJson(diagramLabel, model);
} else if (exportFormat === 'png') {
await exportToPng(diagramLabel, exportDiagramContainerRef, diagram);
}
onCloseClick();
}, [exportFormat, onCloseClick, model, diagramLabel]);
setIsExporting(false);
}, [exportFormat, onCloseClick, model, diagram, diagramLabel]);

return (
<Modal
Expand Down Expand Up @@ -95,6 +107,17 @@ const ExportDiagramModal = ({
<div className={contentContainerStyles}>
<Label htmlFor="">Select file format:</Label>
<RadioGroup className={contentContainerStyles} value={exportFormat}>
<div className={radioItemStyles}>
<Icon glyph="Diagram2" />
<Radio
checked={exportFormat === 'png'}
value="png"
aria-label="PNG"
onClick={() => setExportFormat('png')}
>
PNG
</Radio>
</div>
<div className={radioItemStyles}>
<Icon glyph="CurlyBraces" />
<Radio
Expand All @@ -108,12 +131,26 @@ const ExportDiagramModal = ({
</div>
</RadioGroup>
</div>
{/* Container where we render export diagram when exporting png */}
<div
style={{
width: bounds.width,
height: bounds.height,
// Fixed at bottom right corner
position: 'fixed',
top: '100vh',
left: '100vw',
}}
ref={exportDiagramContainerRef}
/>
</ModalBody>
<ModalFooter className={footerStyles}>
<Button
variant="primary"
onClick={() => void onExport()}
data-testid="export-button"
disabled={isExporting}
loadingIndicator={<SpinLoader />}
>
Export
</Button>
Expand Down
33 changes: 0 additions & 33 deletions packages/compass-data-modeling/src/services/export-diagram.ts

This file was deleted.

184 changes: 184 additions & 0 deletions packages/compass-data-modeling/src/services/export-diagram.tsx
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
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) => {
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this configurable in the diagramming package?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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?

I'll evaluate this, last time (in poc) i checked this, it did not give me good results.

Can we make this configurable in the diagramming package?

These custom marker are rendered here in the package. I am not sure if i follow what you mean by making it configurable?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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:

It excludes diagram title, minmap, and other UI elements

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 (.react-flow) and if not, then maybe we don't need it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
Loading