@@ -17,15 +17,11 @@ import ImageNode from './imageNode'
17
17
import Uml from './uml'
18
18
import data from './data'
19
19
20
- import styles from './index.less'
20
+ import './index.less'
21
21
import '@logicflow/core/es/index.css'
22
22
import '@logicflow/extension/es/index.css'
23
23
24
24
const config : Partial < LogicFlow . Options > = {
25
- isSilentMode : false ,
26
- stopScrollGraph : false ,
27
- stopZoomGraph : false ,
28
- stopMoveGraph : false ,
29
25
style : {
30
26
rect : {
31
27
rx : 5 ,
@@ -58,12 +54,6 @@ const config: Partial<LogicFlow.Options> = {
58
54
} ,
59
55
}
60
56
61
- interface SnapshotResponse {
62
- data : Blob
63
- width : number
64
- height : number
65
- }
66
-
67
57
/**
68
58
* 框选插件 Snapshot 示例
69
59
*/
@@ -76,6 +66,7 @@ export default function SnapshotExample() {
76
66
const [ width , setWidth ] = useState < string > ( ) // 宽度
77
67
const [ height , setHeight ] = useState < string > ( ) // 高度
78
68
const [ padding , setPaddding ] = useState < string > ( ) //padding
69
+ const [ quality , setQuality ] = useState < string > ( ) // 图片质量
79
70
80
71
const [ blobData , setBlobData ] = useState ( '' )
81
72
const [ base64Data , setBase64Data ] = useState ( '' )
@@ -92,13 +83,6 @@ export default function SnapshotExample() {
92
83
lf . register ( Uml )
93
84
lf . register ( ImageNode )
94
85
95
- lf . on (
96
- 'selection:selected-area' ,
97
- ( { topLeft, bottomRight } : Record < string , LogicFlow . PointTuple > ) => {
98
- console . log ( 'get selection area:' , topLeft , bottomRight )
99
- } ,
100
- )
101
-
102
86
lf . setPatternItems ( [
103
87
{
104
88
type : 'circle' ,
@@ -114,7 +98,9 @@ export default function SnapshotExample() {
114
98
} ,
115
99
] )
116
100
117
- lf . extension . snapshot . useGlobalRules = false
101
+ // 默认开启css样式
102
+ lf . extension . snapshot . useGlobalRules = true
103
+ // 不会覆盖css样式,会叠加,customCssRules优先级高
118
104
lf . extension . snapshot . customCssRules = `
119
105
.uml-wrapper {
120
106
line-height: 1.2;
@@ -130,17 +116,27 @@ export default function SnapshotExample() {
130
116
}
131
117
} , [ ] )
132
118
133
- const handleGetSnapshot = ( ) => {
134
- if ( lfRef . current ) {
135
- lfRef . current . getSnapshot ( )
119
+ // 下载
120
+ const downLoad = ( ) => {
121
+ const params = {
122
+ fileType,
123
+ backgroundColor : 'yellow' ,
124
+ partialElement : true ,
125
+ ...( width ? { width : Number ( width ) } : { } ) ,
126
+ ...( height ? { height : Number ( height ) } : { } ) ,
127
+ ...( padding ? { padding : Number ( padding ) } : { } ) ,
128
+ ...( quality ? { quality : Number ( quality ) } : { } ) ,
136
129
}
130
+ console . log ( params , 'params' )
131
+ lfRef . current && lfRef . current . getSnapshot ( fileName , params )
137
132
}
138
133
139
- const handlePreviewSnapshotBlob = ( ) => {
134
+ // 预览 blob
135
+ const previewBlob = ( ) => {
140
136
if ( lfRef . current ) {
141
137
setBase64Data ( '' )
142
138
lfRef . current
143
- . getSnapshotBlob ( '#FFFFFF' )
139
+ . getSnapshotBlob ( '#FFFFFF' , fileType )
144
140
. then (
145
141
( {
146
142
data,
@@ -158,7 +154,8 @@ export default function SnapshotExample() {
158
154
}
159
155
}
160
156
161
- const handlePreviewSnapshotBase64 = ( ) => {
157
+ // 预览 base64
158
+ const previewBase64 = ( ) => {
162
159
if ( lfRef . current ) {
163
160
setBlobData ( '' )
164
161
lfRef . current
@@ -179,123 +176,74 @@ export default function SnapshotExample() {
179
176
)
180
177
}
181
178
}
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
- }
221
179
222
180
return (
223
181
< 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 >
182
+ < Space >
183
+ < Input
184
+ addonBefore = " 文件名:"
185
+ value = { fileName }
186
+ onChange = { ( e ) => setFileName ( e . target . value ) }
187
+ />
188
+ < span > 文件类型: </ span >
189
+ < Select
190
+ defaultValue = { fileType }
191
+ style = { { width : 120 } }
192
+ onChange = { ( value ) => setFileType ( value ) }
193
+ options = { [
194
+ { value : 'png' , label : 'png' } ,
195
+ { value : 'jpeg' , label : 'jpeg' } ,
196
+ { value : 'webp ' , label : 'webp ' } ,
197
+ { value : 'gif ' , label : 'gif ' } ,
198
+ { value : 'svg ' , label : 'svg ' } ,
199
+ ] }
200
+ />
201
+ < Input
202
+ addonBefore = "宽度:"
203
+ value = { width }
204
+ onChange = { ( e ) => setWidth ( e . target . value ) }
205
+ />
206
+ < Input
207
+ addonBefore = "高度:"
208
+ value = { height }
209
+ onChange = { ( e ) => setHeight ( e . target . value ) }
210
+ />
211
+ </ Space >
212
+ < p > </ p >
213
+ < Space >
214
+ < Input
215
+ addonBefore = "padding:"
216
+ value = { padding }
217
+ onChange = { ( e ) => setPaddding ( e . target . value ) }
218
+ />
219
+ < Input
220
+ addonBefore = "图片质量:"
221
+ value = { quality }
222
+ onChange = { ( e ) => setQuality ( e . target . value ) }
223
+ / >
224
+ </ Space >
267
225
< Divider />
268
226
< Space >
269
- { /* <Button id="preview" onClick={preview}>
270
- 预览
271
- </Button> */ }
272
- < Button id = "base64" onClick = { logBase64 } >
273
- 打印base64
274
- </ Button >
227
+ < Button onClick = { downLoad } > 下载快照</ Button >
228
+ < Button onClick = { previewBlob } > 预览(blob)</ Button >
229
+ < Button onClick = { previewBase64 } > 预览(base64)</ Button >
275
230
</ Space >
276
231
< Divider />
277
- < Flex wrap = "wrap" gap = "middle" align = "center" justify = "space-between" >
278
- < Space >
279
- < Button onClick = { handleGetSnapshot } > 下载快照</ Button >
280
- < Button onClick = { handlePreviewSnapshotBlob } > 预览(blob)</ Button >
281
- < Button onClick = { handlePreviewSnapshotBase64 } > 预览(base64)</ Button >
282
- </ Space >
232
+ < Flex align = "center" justify = "center" >
233
+ < div ref = { containerRef } className = "graph" > </ div >
283
234
</ Flex >
284
235
< Row >
285
- < Col span = { 12 } >
286
- < div ref = { containerRef } id = "graph" className = { styles . viewport } > </ div >
287
- </ Col >
288
236
< Col span = { 12 } >
289
237
{ blobData && (
290
238
< >
291
239
< h2 > blobData</ h2 >
292
- < img key = "blob" src = { blobData } className = { styles . preview } />
240
+ < img key = "blob" src = { blobData } />
293
241
</ >
294
242
) }
295
243
{ base64Data && (
296
244
< >
297
245
< h2 > base64Data</ h2 >
298
- < img key = "base64" src = { base64Data } className = { styles . preview } />
246
+ < img key = "base64" src = { base64Data } />
299
247
</ >
300
248
) }
301
249
</ Col >
0 commit comments