Skip to content

Commit 6db9c60

Browse files
author
fanyang
committed
feat(extension): snap插件能力和文档以及example优化
1 parent d3cf186 commit 6db9c60

File tree

6 files changed

+596
-397
lines changed

6 files changed

+596
-397
lines changed

examples/feature-examples/src/pages/extensions/snapshot/data.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,39 @@ export default {
33
{
44
type: 'uml',
55
x: 0,
6-
y: 0,
6+
y: 50,
77
id: 'uml_1',
88
properties: {
99
name: 'haod',
1010
body: '哈哈哈哈',
1111
},
1212
},
1313
{
14+
id: 'rect_1',
1415
type: 'rect',
15-
x: 150,
16+
x: 200,
1617
y: 0,
17-
text: {
18-
value: '你好',
19-
x: 150,
20-
y: 0,
21-
},
22-
id: 'rect_1',
18+
text: '你好1',
2319
},
2420
{
21+
id: 'rect_2',
2522
type: 'rect',
2623
x: 400,
27-
y: 100,
28-
text: {
29-
value: '你好2',
30-
x: 400,
31-
y: 100,
32-
},
33-
id: 'rect_2',
24+
y: 0,
25+
text: '你好2',
3426
},
3527
{
3628
id: '3',
3729
type: 'image',
38-
x: 550,
30+
x: 200,
3931
y: 100,
4032
text: '云',
4133
},
4234
{
4335
id: '5',
4436
type: 'image',
45-
x: 350,
46-
y: 250,
37+
x: 400,
38+
y: 100,
4739
text: '菱形',
4840
},
4941
],
@@ -57,7 +49,7 @@ export default {
5749
{
5850
id: 'e_3',
5951
type: 'polyline',
60-
sourceNodeId: '4',
52+
sourceNodeId: '3',
6153
targetNodeId: '5',
6254
},
6355
],
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
1-
#graph {
2-
width: 800px;
1+
.graph {
2+
width: 90%;
33
height: 500px;
44
}
5+
6+
.uml-wrapper {
7+
box-sizing: border-box;
8+
width: 100%;
9+
height: 100%;
10+
background: #efdbff;
11+
border: 2px solid #9254de;
12+
border-radius: 10px;
13+
}
14+
15+
.uml-head {
16+
font-weight: bold;
17+
font-size: 16px;
18+
line-height: 30px;
19+
text-align: center;
20+
}
21+
22+
.uml-body {
23+
padding: 5px 10px;
24+
font-size: 12px;
25+
border-top: 1px solid #9254de;
26+
border-bottom: 1px solid #9254de;
27+
}
28+
29+
.uml-footer {
30+
padding: 5px 10px;
31+
font-size: 14px;
32+
}

examples/feature-examples/src/pages/extensions/snapshot/index.tsx

Lines changed: 72 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ import ImageNode from './imageNode'
1717
import Uml from './uml'
1818
import data from './data'
1919

20-
import styles from './index.less'
20+
import './index.less'
2121
import '@logicflow/core/es/index.css'
2222
import '@logicflow/extension/es/index.css'
2323

2424
const config: Partial<LogicFlow.Options> = {
25-
isSilentMode: false,
26-
stopScrollGraph: false,
27-
stopZoomGraph: false,
28-
stopMoveGraph: false,
2925
style: {
3026
rect: {
3127
rx: 5,
@@ -58,12 +54,6 @@ const config: Partial<LogicFlow.Options> = {
5854
},
5955
}
6056

61-
interface SnapshotResponse {
62-
data: Blob
63-
width: number
64-
height: number
65-
}
66-
6757
/**
6858
* 框选插件 Snapshot 示例
6959
*/
@@ -76,6 +66,7 @@ export default function SnapshotExample() {
7666
const [width, setWidth] = useState<string>() // 宽度
7767
const [height, setHeight] = useState<string>() // 高度
7868
const [padding, setPaddding] = useState<string>() //padding
69+
const [quality, setQuality] = useState<string>() // 图片质量
7970

8071
const [blobData, setBlobData] = useState('')
8172
const [base64Data, setBase64Data] = useState('')
@@ -92,13 +83,6 @@ export default function SnapshotExample() {
9283
lf.register(Uml)
9384
lf.register(ImageNode)
9485

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-
10286
lf.setPatternItems([
10387
{
10488
type: 'circle',
@@ -114,7 +98,9 @@ export default function SnapshotExample() {
11498
},
11599
])
116100

117-
lf.extension.snapshot.useGlobalRules = false
101+
// 默认开启css样式
102+
lf.extension.snapshot.useGlobalRules = true
103+
// 不会覆盖css样式,会叠加,customCssRules优先级高
118104
lf.extension.snapshot.customCssRules = `
119105
.uml-wrapper {
120106
line-height: 1.2;
@@ -130,17 +116,27 @@ export default function SnapshotExample() {
130116
}
131117
}, [])
132118

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) } : {}),
136129
}
130+
console.log(params, 'params')
131+
lfRef.current && lfRef.current.getSnapshot(fileName, params)
137132
}
138133

139-
const handlePreviewSnapshotBlob = () => {
134+
// 预览 blob
135+
const previewBlob = () => {
140136
if (lfRef.current) {
141137
setBase64Data('')
142138
lfRef.current
143-
.getSnapshotBlob('#FFFFFF')
139+
.getSnapshotBlob('#FFFFFF', fileType)
144140
.then(
145141
({
146142
data,
@@ -158,7 +154,8 @@ export default function SnapshotExample() {
158154
}
159155
}
160156

161-
const handlePreviewSnapshotBase64 = () => {
157+
// 预览 base64
158+
const previewBase64 = () => {
162159
if (lfRef.current) {
163160
setBlobData('')
164161
lfRef.current
@@ -179,123 +176,74 @@ export default function SnapshotExample() {
179176
)
180177
}
181178
}
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-
}
221179

222180
return (
223181
<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>
267225
<Divider />
268226
<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>
275230
</Space>
276231
<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>
283234
</Flex>
284235
<Row>
285-
<Col span={12}>
286-
<div ref={containerRef} id="graph" className={styles.viewport}></div>
287-
</Col>
288236
<Col span={12}>
289237
{blobData && (
290238
<>
291239
<h2>blobData</h2>
292-
<img key="blob" src={blobData} className={styles.preview} />
240+
<img key="blob" src={blobData} />
293241
</>
294242
)}
295243
{base64Data && (
296244
<>
297245
<h2>base64Data</h2>
298-
<img key="base64" src={base64Data} className={styles.preview} />
246+
<img key="base64" src={base64Data} />
299247
</>
300248
)}
301249
</Col>

0 commit comments

Comments
 (0)