1
1
import LogicFlow from '@logicflow/core'
2
- import { Snapshot } from '@logicflow/extension'
3
-
4
- import { Button , Card , Col , Divider , Flex , Row , Space } from 'antd'
5
2
import { useEffect , useRef , useState } from 'react'
3
+ import { Snapshot , DndPanel } from '@logicflow/extension'
4
+
5
+ import {
6
+ Button ,
7
+ Card ,
8
+ Col ,
9
+ Divider ,
10
+ Flex ,
11
+ Row ,
12
+ Space ,
13
+ Select ,
14
+ Input ,
15
+ } from 'antd'
6
16
import ImageNode from './imageNode'
17
+ import Uml from './uml'
18
+ import data from './data'
7
19
8
20
import styles from './index.less'
9
21
import '@logicflow/core/es/index.css'
@@ -13,7 +25,7 @@ const config: Partial<LogicFlow.Options> = {
13
25
isSilentMode : false ,
14
26
stopScrollGraph : false ,
15
27
stopZoomGraph : false ,
16
- stopMoveGraph : true ,
28
+ stopMoveGraph : false ,
17
29
style : {
18
30
rect : {
19
31
rx : 5 ,
@@ -41,80 +53,15 @@ const config: Partial<LogicFlow.Options> = {
41
53
fontSize : 12 ,
42
54
} ,
43
55
} ,
56
+ grid : {
57
+ size : 20 ,
58
+ } ,
44
59
}
45
60
46
- const data = {
47
- nodes : [
48
- {
49
- id : '1' ,
50
- type : 'rect' ,
51
- x : 150 ,
52
- y : 100 ,
53
- text : '矩形' ,
54
- } ,
55
- {
56
- id : '2' ,
57
- type : 'circle' ,
58
- x : 350 ,
59
- y : 100 ,
60
- text : '圆形' ,
61
- } ,
62
- {
63
- id : '3' ,
64
- type : 'image' ,
65
- x : 550 ,
66
- y : 100 ,
67
- text : '云' ,
68
- } ,
69
- {
70
- id : '4' ,
71
- type : 'polygon' ,
72
- x : 150 ,
73
- y : 250 ,
74
- text : '多边形' ,
75
- } ,
76
- {
77
- id : '5' ,
78
- type : 'image' ,
79
- x : 350 ,
80
- y : 250 ,
81
- text : '菱形' ,
82
- } ,
83
- {
84
- id : '6' ,
85
- type : 'text' ,
86
- x : 550 ,
87
- y : 250 ,
88
- text : '纯文本节点' ,
89
- } ,
90
- {
91
- id : '7' ,
92
- type : 'html' ,
93
- x : 150 ,
94
- y : 400 ,
95
- text : 'html节点' ,
96
- } ,
97
- ] ,
98
- edges : [
99
- {
100
- id : 'e_1' ,
101
- type : 'polyline' ,
102
- sourceNodeId : '1' ,
103
- targetNodeId : '2' ,
104
- } ,
105
- {
106
- id : 'e_2' ,
107
- type : 'polyline' ,
108
- sourceNodeId : '2' ,
109
- targetNodeId : '3' ,
110
- } ,
111
- {
112
- id : 'e_3' ,
113
- type : 'polyline' ,
114
- sourceNodeId : '4' ,
115
- targetNodeId : '5' ,
116
- } ,
117
- ] ,
61
+ interface SnapshotResponse {
62
+ data : Blob
63
+ width : number
64
+ height : number
118
65
}
119
66
120
67
/**
@@ -123,6 +70,13 @@ const data = {
123
70
export default function SnapshotExample ( ) {
124
71
const lfRef = useRef < LogicFlow > ( )
125
72
const containerRef = useRef < HTMLDivElement > ( null )
73
+
74
+ const [ fileName , setFileName ] = useState < string > ( ) // 文件名
75
+ const [ fileType , setFileType ] = useState < string > ( 'png' ) // 下载的图片类型
76
+ const [ width , setWidth ] = useState < string > ( ) // 宽度
77
+ const [ height , setHeight ] = useState < string > ( ) // 高度
78
+ const [ padding , setPaddding ] = useState < string > ( ) //padding
79
+
126
80
const [ blobData , setBlobData ] = useState ( '' )
127
81
const [ base64Data , setBase64Data ] = useState ( '' )
128
82
@@ -132,12 +86,10 @@ export default function SnapshotExample() {
132
86
const lf = new LogicFlow ( {
133
87
...config ,
134
88
container : containerRef . current ! ,
135
- grid : {
136
- size : 20 ,
137
- } ,
138
- stopMoveGraph : false ,
139
- plugins : [ Snapshot ] ,
89
+ plugins : [ Snapshot , DndPanel ] ,
140
90
} )
91
+
92
+ lf . register ( Uml )
141
93
lf . register ( ImageNode )
142
94
143
95
lf . on (
@@ -146,8 +98,34 @@ export default function SnapshotExample() {
146
98
console . log ( 'get selection area:' , topLeft , bottomRight )
147
99
} ,
148
100
)
101
+
102
+ lf . setPatternItems ( [
103
+ {
104
+ type : 'circle' ,
105
+ text : 'circle' ,
106
+ label : 'circle' ,
107
+ icon : 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAAH6ji2bAAAABGdBTUEAALGPC/xhBQAAAnBJREFUOBGdVL1rU1EcPfdGBddmaZLiEhdx1MHZQXApraCzQ7GKLgoRBxMfcRELuihWKcXFRcEWF8HBf0DdDCKYRZpnl7p0svLe9Zzbd29eQhTbC8nv+9zf130AT63jvooOGS8Vf9Nt5zxba7sXQwODfkWpkbjTQfCGUd9gIp3uuPP8bZ946g56dYQvnBg+b1HB8VIQmMFrazKcKSvFW2dQTxJnJdQ77urmXWOMBCmXM2Rke4S7UAW+/8ywwFoewmBps2tu7mbTdp8VMOkIRAkKfrVawalJTtIliclFbaOBqa0M2xImHeVIfd/nKAfVq/LGnPss5Kh00VEdSzfwnBXPUpmykNss4lUI9C1ga+8PNrBD5YeqRY2Zz8PhjooIbfJXjowvQJBqkmEkVnktWhwu2SM7SMx7Cj0N9IC0oQXRo8xwAGzQms+xrB/nNSUWVveI48ayrFGyC2+E2C+aWrZHXvOuz+CiV6iycWe1Rd1Q6+QUG07nb5SbPrL4426d+9E1axKjY3AoRrlEeSQo2Eu0T6BWAAr6COhTcWjRaYfKG5csnvytvUr/WY4rrPMB53Uo7jZRjXaG6/CFfNMaXEu75nG47X+oepU7PKJvvzGDY1YLSKHJrK7vFUwXKkaxwhCW3u+sDFMVrIju54RYYbFKpALZAo7sB6wcKyyrd+aBMryMT2gPyD6GsQoRFkGHr14TthZni9ck0z+Pnmee460mHXbRAypKNy3nuMdrWgVKj8YVV8E7PSzp1BZ9SJnJAsXdryw/h5ctboUVi4AFiCd+lQaYMw5z3LGTBKjLQOeUF35k89f58Vv/tGh+l+PE/wG0rgfIUbZK5AAAAABJRU5ErkJggg==' ,
108
+ } ,
109
+ {
110
+ type : 'rect' ,
111
+ label : 'rect' ,
112
+ text : 'circle' ,
113
+ icon : 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAATCAYAAAEFVwZaAAAABGdBTUEAALGPC/xhBQAAAqlJREFUOBF9VM9rE0EUfrMJNUKLihGbpLGtaCOIR8VjQMGDePCgCCIiCNqzCAp2MyYUCXhUtF5E0D+g1t48qAd7CCLqQUQKEWkStcEfVGlLdp/fm3aW2QQdyLzf33zz5m2IsAZ9XhDpyaaIZkTS4ASzK41TFao88GuJ3hsr2pAbipHxuSYyKRugagICGANkfFnNh3HeE2N0b3nN2cgnpcictw5veJIzxmDamSlxxQZicq/mflxhbaH8BLRbuRwNtZp0JAhoplVRUdzmCe/vO27wFuuA3S5qXruGdboy5/PRGFsbFGKo/haRtQHIrM83bVeTrOgNhZReWaYGnE4aUQgTJNvijJFF4jQ8BxJE5xfKatZWmZcTQ+BVgh7s8SgPlCkcec4mGTmieTP4xd7PcpIEg1TX6gdeLW8rTVMVLVvb7ctXoH0Cydl2QOPJBG21STE5OsnbweVYzAnD3A7PVILuY0yiiyDwSm2g441r6rMSgp6iK42yqroI2QoXeJVeA+YeZSa47gZdXaZWQKTrG93rukk/l2Al6Kzh5AZEl7dDQy+JjgFahQjRopSxPbrbvK7GRe9ePWBo1wcU7sYrFZtavXALwGw/7Dnc50urrHJuTPSoO2IMV3gUQGNg87IbSOIY9BpiT9HV7FCZ94nPXb3MSnwHn/FFFE1vG6DTby+r31KAkUktB3Qf6ikUPWxW1BkXSPQeMHHiW0+HAd2GelJsZz1OJegCxqzl+CLVHa/IibuHeJ1HAKzhuDR+ymNaRFM+4jU6UWKXorRmbyqkq/D76FffevwdCp+jN3UAN/C9JRVTDuOxC/oh+EdMnqIOrlYteKSfadVRGLJFJPSB/ti/6K8f0CNymg/iH2gO/f0DwE0yjAFO6l8JaR5j0VPwPwfaYHqOqrCI319WzwhwzNW/aQAAAABJRU5ErkJggg==' ,
114
+ } ,
115
+ ] )
116
+
117
+ lf . extension . snapshot . useGlobalRules = false
118
+ lf . extension . snapshot . customCssRules = `
119
+ .uml-wrapper {
120
+ line-height: 1.2;
121
+ text-align: center;
122
+ color: blue;
123
+ }
124
+ `
125
+
149
126
lf . render ( data )
150
- lf . fitView ( )
127
+ lf . translateCenter ( )
128
+
151
129
lfRef . current = lf
152
130
}
153
131
} , [ ] )
@@ -201,17 +179,108 @@ export default function SnapshotExample() {
201
179
)
202
180
}
203
181
}
182
+ // 下载
183
+ const downLoad = ( ) => {
184
+ lfRef . current &&
185
+ lfRef . current . getSnapshot ( fileName , {
186
+ fileType,
187
+ height : height ,
188
+ padding : padding ,
189
+ backgroundColor : 'yellow' ,
190
+ partialElement : true ,
191
+ ...( width ? { width } : { } ) ,
192
+ ...( height ? { height } : { } ) ,
193
+ ...( padding ? { padding } : { } ) ,
194
+ } )
195
+ }
196
+
197
+ // 预览
198
+ // const preview = () => {
199
+ // lfRef.current &&
200
+ // lfRef.current
201
+ // .getSnapshotBlob('#FFFFFF', fileType)
202
+ // .then(({ data }: SnapshotResponse) => {
203
+ // if (imgRef.current) {
204
+ // imgRef.current.width = 500
205
+ // imgRef.current.height = 400
206
+ // imgRef.current.src = window.URL.createObjectURL(data)
207
+ // }
208
+ // })
209
+ // }
210
+
211
+ // 打印base64地址
212
+ const logBase64 = ( ) => {
213
+ lfRef . current &&
214
+ lfRef . current
215
+ . getSnapshotBase64 ( undefined , fileType )
216
+ . then ( ( { data, width, height } : SnapshotResponse ) => {
217
+ // document.getElementById('img').src = data
218
+ console . log ( width , height , data )
219
+ } )
220
+ }
204
221
205
222
return (
206
223
< Card title = "LogicFlow Extension - Snapshot" >
224
+ < div style = { { marginBottom : '10px' } } >
225
+ < Space >
226
+ < span > 文件名:</ span >
227
+ < Input
228
+ placeholder = "文件名"
229
+ value = { fileName }
230
+ onChange = { ( e ) => setFileName ( e . target . value ) }
231
+ />
232
+ < span > 文件类型(默认png):</ span >
233
+ < Select
234
+ defaultValue = { fileType }
235
+ style = { { width : 120 } }
236
+ onChange = { ( value ) => setFileType ( value ) }
237
+ options = { [
238
+ { value : 'png' , label : 'png' } ,
239
+ { value : 'jpeg' , label : 'jpeg' } ,
240
+ { value : 'webp' , label : 'webp' } ,
241
+ { value : 'gif' , label : 'gif' } ,
242
+ ] }
243
+ />
244
+ < span > 宽度</ span >
245
+ < Input
246
+ placeholder = "width"
247
+ value = { width }
248
+ onChange = { ( e ) => setWidth ( e . target . value ) }
249
+ />
250
+ < span > 高度</ span >
251
+ < Input
252
+ placeholder = "height"
253
+ value = { height }
254
+ onChange = { ( e ) => setHeight ( e . target . value ) }
255
+ />
256
+ < span > padding</ span >
257
+ < Input
258
+ placeholder = "padding"
259
+ value = { padding }
260
+ onChange = { ( e ) => setPaddding ( e . target . value ) }
261
+ />
262
+ < Button id = "download" onClick = { downLoad } >
263
+ 下载快照
264
+ </ Button >
265
+ </ Space >
266
+ </ div >
267
+ < Divider />
268
+ < Space >
269
+ { /* <Button id="preview" onClick={preview}>
270
+ 预览
271
+ </Button> */ }
272
+ < Button id = "base64" onClick = { logBase64 } >
273
+ 打印base64
274
+ </ Button >
275
+ </ Space >
276
+ < Divider />
207
277
< Flex wrap = "wrap" gap = "middle" align = "center" justify = "space-between" >
208
278
< Space >
209
279
< Button onClick = { handleGetSnapshot } > 下载快照</ Button >
210
280
< Button onClick = { handlePreviewSnapshotBlob } > 预览(blob)</ Button >
211
281
< Button onClick = { handlePreviewSnapshotBase64 } > 预览(base64)</ Button >
212
282
</ Space >
213
283
</ Flex >
214
- < Divider />
215
284
< Row >
216
285
< Col span = { 12 } >
217
286
< div ref = { containerRef } id = "graph" className = { styles . viewport } > </ div >
0 commit comments