Browse Source

refactor: use DataUri in @antv/x6 to download file

master
smallstonesk 4 years ago
parent
commit
1b28a6e032
  1. 10
      packages/core/src/mods/header/export/exportModal.tsx
  2. 20
      packages/core/src/utils/index.ts

10
packages/core/src/mods/header/export/exportModal.tsx

@ -5,9 +5,8 @@ import styles from './index.module.less';
import JSZip from 'jszip';
import { Modal } from 'antd';
import { Graph } from '@antv/x6';
import { DataUri, Graph } from '@antv/x6';
import compileCode from '@imove/compile-code';
import { base64Url2Blob, downloadFile } from '../../../utils';
interface IExportModalProps {
flowChart: Graph;
@ -21,7 +20,7 @@ const ExportModal: React.FC<IExportModalProps> = (props) => {
const onExportDSL = () => {
const dsl = JSON.stringify(flowChart.toJSON(), null, 2);
const blob = new Blob([dsl], { type: 'text/plain' });
downloadFile('imove.dsl.json', blob);
DataUri.downloadBlob(blob, 'imove.dsl.json');
};
const onExportCode = () => {
const zip = new JSZip();
@ -29,13 +28,12 @@ const ExportModal: React.FC<IExportModalProps> = (props) => {
const output = compileCode(dsl);
Helper.recursiveZip(zip, output);
zip.generateAsync({ type: 'blob' }).then((blob) => {
downloadFile('logic.zip', blob);
DataUri.downloadBlob(blob, 'logic.zip');
});
};
const onExportFlowChart = () => {
flowChart.toJPEG((dataUri: string) => {
const blob = base64Url2Blob(dataUri);
downloadFile('flowChart.png', blob);
DataUri.downloadDataUri(dataUri, 'flowChart.png');
});
};

20
packages/core/src/utils/index.ts

@ -36,23 +36,3 @@ const parseConfig = {
export const parseQuery = (): { [key: string]: any } => {
return parse(location.search, parseConfig);
};
export const base64Url2Blob = (base64: string) => {
const bytes = window.atob(base64.split(',')[1]);
const buffer = new ArrayBuffer(bytes.length);
const arr = new Uint8Array(buffer);
for (let i = 0; i < bytes.length; i++) {
arr[i] = bytes.charCodeAt(i);
}
return new Blob([arr], { type: 'image/png' });
};
export const downloadFile = (fileName: string, blob: Blob) => {
const elem = document.createElement('a');
elem.href = URL.createObjectURL(blob);
elem.download = fileName;
document.body.appendChild(elem);
elem.click();
URL.revokeObjectURL(elem.href);
document.body.removeChild(elem);
};

Loading…
Cancel
Save