Skip to content

Commit 1cfac05

Browse files
committed
use version and encode data
1 parent db5a846 commit 1cfac05

File tree

7 files changed

+372
-21
lines changed

7 files changed

+372
-21
lines changed

packages/compass-data-modeling/src/components/diagram-editor-toolbar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { connect } from 'react-redux';
33
import type { DataModelingState } from '../store/reducer';
4-
import { downloadDiagram, redoEdit, undoEdit } from '../store/diagram';
4+
import { saveDiagram, redoEdit, undoEdit } from '../store/diagram';
55
import { showExportModal } from '../store/export-diagram';
66
import { Icon, IconButton } from '@mongodb-js/compass-components';
77

@@ -56,6 +56,6 @@ export default connect(
5656
onUndoClick: undoEdit,
5757
onRedoClick: redoEdit,
5858
onExportClick: showExportModal,
59-
onDownloadClick: downloadDiagram,
59+
onDownloadClick: saveDiagram,
6060
}
6161
)(DiagramEditorToolbar);

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
Diagram,
77
} from '@mongodb-js/diagramming';
88
import type { DiagramInstance } from '@mongodb-js/diagramming';
9-
import type { Edit, StaticModel } from './data-model-storage';
9+
import type { StaticModel } from './data-model-storage';
1010
import ReactDOM from 'react-dom';
1111
import { toPng } from 'html-to-image';
1212
import { rafraf, spacing } from '@mongodb-js/compass-components';
@@ -155,18 +155,11 @@ export function getExportJsonFromModel({
155155
};
156156
}
157157

158-
export function exportEdits(fileName: string, edits: Edit[]) {
159-
const json = { name: fileName, edits };
160-
const blob = new Blob([JSON.stringify(json, null, 2)], {
161-
type: 'application/json',
162-
});
163-
const url = window.URL.createObjectURL(blob);
164-
downloadFile(url, `${fileName}.compass`, () => {
165-
window.URL.revokeObjectURL(url);
166-
});
167-
}
168-
169-
function downloadFile(uri: string, fileName: string, cleanup?: () => void) {
158+
export function downloadFile(
159+
uri: string,
160+
fileName: string,
161+
cleanup?: () => void
162+
) {
170163
const link = document.createElement('a');
171164
link.download = fileName;
172165
link.href = uri;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { expect } from 'chai';
2+
import { getDownloadDiagramContent } from './open-and-download-diagram';
3+
import FlightDiagram from '../../test/fixtures/flights-diagram.json';
4+
5+
describe('open-and-download-diagram', function () {
6+
it('should return correct content to download', function () {
7+
const fileName = 'test-diagram';
8+
9+
const { edits, ...restOfContent } = getDownloadDiagramContent(
10+
fileName,
11+
FlightDiagram.edits as any
12+
);
13+
expect(restOfContent).to.deep.equal({
14+
type: 'Compass Data Modeling Diagram',
15+
version: 1,
16+
name: fileName,
17+
});
18+
19+
const decodedEdits = JSON.parse(
20+
Buffer.from(edits, 'base64').toString('utf-8')
21+
);
22+
expect(decodedEdits).to.deep.equal(FlightDiagram.edits);
23+
});
24+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { Edit } from './data-model-storage';
2+
import { downloadFile } from './export-diagram';
3+
4+
const kCurrentVersion = 1;
5+
const kFileTypeDescription = 'Compass Data Modeling Diagram';
6+
7+
export function downloadDiagram(fileName: string, edits: Edit[]) {
8+
const blob = new Blob(
9+
[JSON.stringify(getDownloadDiagramContent(fileName, edits), null, 2)],
10+
{
11+
type: 'application/json',
12+
}
13+
);
14+
const url = window.URL.createObjectURL(blob);
15+
downloadFile(url, `${fileName}.compass`, () => {
16+
window.URL.revokeObjectURL(url);
17+
});
18+
}
19+
20+
export function getDownloadDiagramContent(name: string, edits: Edit[]) {
21+
return {
22+
type: kFileTypeDescription,
23+
version: kCurrentVersion,
24+
name,
25+
edits: Buffer.from(JSON.stringify(edits)).toString('base64'),
26+
};
27+
}

packages/compass-data-modeling/src/store/diagram.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { AnalysisProcessActionTypes } from './analysis-process';
1111
import { memoize } from 'lodash';
1212
import type { DataModelingState, DataModelingThunkAction } from './reducer';
1313
import { showConfirmation, showPrompt } from '@mongodb-js/compass-components';
14-
import { exportEdits } from '../services/export-diagram';
14+
import { downloadDiagram } from '../services/open-and-download-diagram';
1515

1616
function isNonEmptyArray<T>(arr: T[]): arr is [T, ...T[]] {
1717
return Array.isArray(arr) && arr.length > 0;
@@ -316,13 +316,13 @@ export function deleteDiagram(
316316
};
317317
}
318318

319-
export function downloadDiagram(): DataModelingThunkAction<void, never> {
319+
export function saveDiagram(): DataModelingThunkAction<void, never> {
320320
return (_dispatch, getState) => {
321321
const { diagram } = getState();
322322
if (!diagram) {
323323
return;
324324
}
325-
exportEdits(diagram.name, diagram.edits.current);
325+
downloadDiagram(diagram.name, diagram.edits.current);
326326
};
327327
}
328328

0 commit comments

Comments
 (0)