|
| 1 | +import './App.css'; |
| 2 | +import { DiagramComponent, PrintAndExport, |
| 3 | + NodeModel, ConnectorModel, |
| 4 | + Inject |
| 5 | +} from '@syncfusion/ej2-react-diagrams'; |
| 6 | +import { UploaderComponent } from '@syncfusion/ej2-react-inputs'; |
| 7 | +import { useRef, useState } from 'react'; |
| 8 | +function App() { |
| 9 | + const diagramRef = useRef<DiagramComponent>(null); |
| 10 | + const [diagramData, setDiagramData] = useState<string | undefined>(); |
| 11 | + const nodes : NodeModel[] = [ |
| 12 | + { |
| 13 | + id: "startNode", |
| 14 | + offsetX: 440, |
| 15 | + offsetY: 60, |
| 16 | + shape: { type: "Flow", shape: "Terminator" }, |
| 17 | + annotations: [{ content: 'Start', style: { color: 'white' } }] |
| 18 | + }, |
| 19 | + { |
| 20 | + id: "inputNode", |
| 21 | + offsetX: 440, |
| 22 | + offsetY: 180, |
| 23 | + shape: { type: "Flow", shape: "Data" }, |
| 24 | + annotations: [{ content: 'Enter a number', style: { color: 'white' } }] |
| 25 | + }, |
| 26 | + { |
| 27 | + id: "decisionNode", |
| 28 | + offsetX: 440, |
| 29 | + offsetY: 300, |
| 30 | + shape: { type: "Flow", shape: "Decision" }, |
| 31 | + annotations: [{ content: 'N divisible by 2 ?', style: { color: 'white' } }] |
| 32 | + }, |
| 33 | + { |
| 34 | + id: "processEvenNode", |
| 35 | + offsetX: 700, |
| 36 | + offsetY: 300, |
| 37 | + shape: { type: "Flow", shape: "Process" }, |
| 38 | + annotations: [{ content: 'N is Even', style: { color: 'white' } }] |
| 39 | + }, |
| 40 | + { |
| 41 | + id: "processOddNode", |
| 42 | + offsetX: 440, |
| 43 | + offsetY: 420, |
| 44 | + shape: { type: "Flow", shape: "Process" }, |
| 45 | + annotations: [{ content: 'N is Odd', style: { color: 'white' } }] |
| 46 | + }, |
| 47 | + { |
| 48 | + id: "endNode", |
| 49 | + offsetX: 440, |
| 50 | + offsetY: 540, |
| 51 | + shape: { type: "Flow", shape: "Terminator" }, |
| 52 | + annotations: [{ content: 'End', style: { color: 'white' } }] |
| 53 | + } |
| 54 | + ]; |
| 55 | + |
| 56 | + const connectors : ConnectorModel[]= [ |
| 57 | + { |
| 58 | + id: 'startToInputConnector', |
| 59 | + sourceID: 'startNode', |
| 60 | + targetID: 'inputNode' |
| 61 | + }, |
| 62 | + { |
| 63 | + id: 'inputToDecisionConnector', |
| 64 | + sourceID: 'inputNode', |
| 65 | + targetID: 'decisionNode' |
| 66 | + }, |
| 67 | + { |
| 68 | + id: 'decisionToProcessEvenConnector', |
| 69 | + sourceID: 'decisionNode', |
| 70 | + targetID: 'processEvenNode', |
| 71 | + annotations: [{ content: 'true', alignment: 'Before', displacement: { x: 5, y: 5 } }] |
| 72 | + }, |
| 73 | + { |
| 74 | + id: 'decisionToProcessOddConnector', |
| 75 | + sourceID: 'decisionNode', |
| 76 | + targetID: 'processOddNode', |
| 77 | + annotations: [{ content: 'false', alignment: 'After', displacement: { x: 5, y: 5 } }] |
| 78 | + }, |
| 79 | + { |
| 80 | + id: 'processOddToEndConnector', |
| 81 | + sourceID: 'processOddNode', |
| 82 | + targetID: 'endNode' |
| 83 | + }, |
| 84 | + { |
| 85 | + id: 'processEvenToEndConnector', |
| 86 | + sourceID: 'processEvenNode', |
| 87 | + targetID: 'endNode', |
| 88 | + type: "Orthogonal", |
| 89 | + segments: [{ type: "Orthogonal", direction: "Bottom", length: 200 }] |
| 90 | + } |
| 91 | + ]; |
| 92 | + const nodeDefaults =(node:NodeModel) =>{ |
| 93 | + node.height = 60; |
| 94 | + node.width = 150; |
| 95 | + node.style = { fill: '#6495ED' }; |
| 96 | + return node; |
| 97 | + } |
| 98 | + const asyncSettings = { |
| 99 | + saveUrl: "https://services.syncfusion.com/react/production/api/FileUploader/Save" |
| 100 | + } |
| 101 | + const onSave = ()=>{ |
| 102 | + const saveData = diagramRef.current?.saveDiagram() |
| 103 | + // setDiagramData(saveData); |
| 104 | + // console.log(saveData); |
| 105 | + saveData && download(saveData); |
| 106 | + } |
| 107 | + const onLoad = ()=>{ |
| 108 | + // if(diagramData){ |
| 109 | + // diagramRef.current?.loadDiagram(diagramData); |
| 110 | + // } |
| 111 | + document.querySelector('.e-file-select-wrap')?.querySelector('button')?.click(); |
| 112 | + } |
| 113 | + const download = (JSONData : string) =>{ |
| 114 | + const a = document.createElement('a'); |
| 115 | + a.href = "data:text/json;charset=utf-8," + encodeURIComponent(JSONData); |
| 116 | + a.download = 'Diagram.json'; |
| 117 | + document.body.appendChild(a); |
| 118 | + a.click(); |
| 119 | + document.body.removeChild(a); |
| 120 | + } |
| 121 | + const fileUploadSuccess = (args : any) =>{ |
| 122 | + const reader = new FileReader(); |
| 123 | + reader.onloadend = () =>{ |
| 124 | + diagramRef.current?.loadDiagram(reader.result as string); |
| 125 | + } |
| 126 | + reader.readAsText(args.file.rawFile); |
| 127 | + } |
| 128 | + const onExport = ()=>{ |
| 129 | + diagramRef.current?.exportDiagram({ |
| 130 | + mode:'Download', format:'PNG', |
| 131 | + region: 'PageSettings', |
| 132 | + pageHeight:800, pageWidth: 800 |
| 133 | + }) |
| 134 | + } |
| 135 | + const onPrint = ()=>{ |
| 136 | + diagramRef.current?.print({ region:'PageSettings', |
| 137 | + pageHeight:800, pageWidth: 800 |
| 138 | + }) |
| 139 | + } |
| 140 | + return ( |
| 141 | + <div className="app"> |
| 142 | + <div className='button-container'> |
| 143 | + <button className='button' onClick={onSave}>Save</button> |
| 144 | + <button className='button' onClick={onLoad}>Load</button> |
| 145 | + <button className='button' onClick={onExport}>Export</button> |
| 146 | + <button className='button' onClick={onPrint}>Print</button> |
| 147 | + </div> |
| 148 | + <div style={{display:"none"}}> |
| 149 | + <UploaderComponent |
| 150 | + asyncSettings={asyncSettings} |
| 151 | + success={fileUploadSuccess} |
| 152 | + ></UploaderComponent> |
| 153 | + </div> |
| 154 | + <div className='container'> |
| 155 | + <DiagramComponent |
| 156 | + ref={diagramRef} |
| 157 | + width={'902px'} |
| 158 | + height={'602px'} |
| 159 | + nodes={nodes} |
| 160 | + connectors={connectors} |
| 161 | + getNodeDefaults={nodeDefaults} |
| 162 | + > |
| 163 | + <Inject services={[PrintAndExport]} /> |
| 164 | + </DiagramComponent> |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + ); |
| 168 | +} |
| 169 | +export default App; |
0 commit comments