Skip to content

Commit d3a8dd5

Browse files
author
fanyang
committed
refactor(extension): snapshot细节问题优化和官网细节问题优化
1 parent 6880f5f commit d3a8dd5

File tree

8 files changed

+74
-58
lines changed

8 files changed

+74
-58
lines changed

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

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
Switch,
1717
} from 'antd'
1818
import ImageNode from './imageNode'
19-
import CustomHtml from '../../../components/nodes/custom-html/Html'
19+
import CustomHtml from '@/components/nodes/custom-html/Html'
2020
import data from './data'
2121
import { circle as circleSvgUrl, rect as rectSvgUrl } from './svg'
2222

@@ -141,7 +141,7 @@ export default function SnapshotExample() {
141141
quality,
142142
}
143143
console.log(params, 'params')
144-
lfRef.current && lfRef.current.getSnapshot(fileName, params)
144+
lfRef.current?.getSnapshot(fileName, params)
145145
}
146146

147147
// 预览 blob
@@ -172,7 +172,7 @@ export default function SnapshotExample() {
172172
if (lfRef.current) {
173173
setBlobData('')
174174
lfRef.current
175-
.getSnapshotBase64('#FFFFFF')
175+
.getSnapshotBase64('#fff')
176176
.then(
177177
({
178178
data,
@@ -190,35 +190,44 @@ export default function SnapshotExample() {
190190
}
191191
}
192192

193-
const handleWidthChange = (value: number | null | undefined) => {
193+
const handleInputChange = (
194+
value: number | null | undefined,
195+
prop: string,
196+
) => {
194197
if (value === null || value === undefined) {
195-
setWidth(undefined) // 处理 null 或 undefined 的情况
198+
switch (
199+
prop // 处理 null 或 undefined 的情况
200+
) {
201+
case 'width':
202+
setWidth(undefined)
203+
break
204+
case 'height':
205+
setHeight(undefined)
206+
break
207+
case 'padding':
208+
setPadding(undefined)
209+
break
210+
case 'quality':
211+
setQuality(undefined)
212+
break
213+
}
196214
} else {
197-
setWidth(value) // 设置有效的数字值
198-
}
199-
}
200-
201-
const handleHeightChange = (value: number | null | undefined) => {
202-
if (value === null || value === undefined) {
203-
setHeight(undefined) // 处理 null 或 undefined 的情况
204-
} else {
205-
setHeight(value) // 设置有效的数字值
206-
}
207-
}
208-
209-
const handlePaddingChange = (value: number | null | undefined) => {
210-
if (value === null || value === undefined) {
211-
setPadding(undefined) // 处理 null 或 undefined 的情况
212-
} else {
213-
setPadding(value) // 设置有效的数字值
214-
}
215-
}
216-
217-
const handleQualityChange = (value: number | null | undefined) => {
218-
if (value === null || value === undefined) {
219-
setQuality(undefined) // 处理 null 或 undefined 的情况
220-
} else {
221-
setQuality(value) // 设置有效的数字值
215+
switch (
216+
prop // 设置有效的数字值
217+
) {
218+
case 'width':
219+
setWidth(value)
220+
break
221+
case 'height':
222+
setHeight(value)
223+
break
224+
case 'padding':
225+
setPadding(value)
226+
break
227+
case 'quality':
228+
setQuality(value)
229+
break
230+
}
222231
}
223232
}
224233

@@ -246,25 +255,25 @@ export default function SnapshotExample() {
246255
<InputNumber
247256
addonBefore="宽度:"
248257
value={width}
249-
onChange={handleWidthChange}
258+
onChange={(value) => handleInputChange(value, 'width')}
250259
/>
251260
<InputNumber
252261
addonBefore="高度:"
253262
value={height}
254-
onChange={handleHeightChange}
263+
onChange={(value) => handleInputChange(value, 'height')}
255264
/>
256265
</Space>
257266
<p></p>
258267
<Space>
259268
<InputNumber
260269
addonBefore="padding:"
261270
value={padding}
262-
onChange={handlePaddingChange}
271+
onChange={(value) => handleInputChange(value, 'padding')}
263272
/>
264273
<InputNumber
265274
addonBefore="图片质量:"
266275
value={quality}
267-
onChange={handleQualityChange}
276+
onChange={(value) => handleInputChange(value, 'quality')}
268277
/>
269278
<span>导出局部渲染:</span>
270279
<Switch defaultChecked onChange={(partial) => setPartial(partial)} />

packages/core/src/event/eventArgs.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,11 @@ interface CommonEventArgs {
283283
data: GraphData
284284
}
285285
/**
286-
* 画布渲染数据更新后后触发,即改变画布上的属性,比如partial。
286+
* 画布重新更新后触发. 即 lf.render(graphData)方法被调用后或者改变画布(garphModel)上的属性后触发。
287+
* 如果是主动修改某个特定属性导致画布更新,想要在画布更新后做一些操作,建议注册事件后在回调函数中及时注销该事件,或者使用once事件代替on事件。
288+
* 因为其他属性也可能导致画布更新,触发该事件。
287289
*/
288-
'graph:updated': {
289-
/**
290-
* 渲染后的画布数据
291-
*/
292-
data: GraphData
293-
}
290+
'graph:updated': Record<string, undefined>
294291
}
295292

296293
type AnchorEventArgsPick<T extends 'data' | 'e' | 'nodeModel' | 'edgeModel'> =

packages/core/src/view/Graph.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ class Graph extends Component<IGraphProps> {
4747
}
4848

4949
componentDidUpdate() {
50-
this.props.graphModel.eventCenter.emit(EventType.GRAPH_UPDATED, {
51-
data: this.props.graphModel,
52-
})
50+
this.props.graphModel.eventCenter.emit(EventType.GRAPH_UPDATED, {})
5351
}
5452

5553
componentWillUnmount() {

packages/extension/src/tools/snapshot/index.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ export class Snapshot {
5050
this.lf.graphModel.eventCenter.on('graph:updated', async () => {
5151
await this.getSnapshot(fileName, toImageOptions)
5252
this.lf.graphModel.eventCenter.off('graph:updated')
53-
// TODO: 这里用这种方式会报错,不太理解 - fy
54-
// this.lf.off('graph:updated')
5553
// 恢复原来渲染模式
5654
this.lf.graphModel.setPartial(curPartial)
5755
})
@@ -82,18 +80,18 @@ export class Snapshot {
8280
}
8381

8482
/**
85-
* 通过 imgURI 下载图片
86-
* @param imgURI
83+
* 通过 imgUrl 下载图片
84+
* @param imgUrl
8785
*/
88-
triggerDownload(imgURI?: string) {
86+
triggerDownload(imgUrl: string) {
8987
const evt = new MouseEvent('click', {
9088
view: document.defaultView,
9189
bubbles: false,
9290
cancelable: true,
9391
})
9492
const a = document.createElement('a')
9593
a.setAttribute('download', this.fileName!)
96-
a.setAttribute('href', imgURI!)
94+
a.setAttribute('href', imgUrl)
9795
a.setAttribute('target', '_blank')
9896
a.dispatchEvent(evt)
9997
}
@@ -159,10 +157,10 @@ export class Snapshot {
159157
this.getCanvasData(svg, toImageOptions ?? {}).then(
160158
(canvas: HTMLCanvasElement) => {
161159
// canvas元素 => url image/octet-stream: 确保所有浏览器都能正常下载
162-
const imgURI = canvas
160+
const imgUrl = canvas
163161
.toDataURL(`image/${fileType}`, quality)
164162
.replace(`image/${fileType}`, 'image/octet-stream')
165-
this.triggerDownload(imgURI)
163+
this.triggerDownload(imgUrl)
166164
},
167165
)
168166
}
@@ -340,15 +338,15 @@ export class Snapshot {
340338
ctx.clearRect(0, 0, canvas.width, canvas.height)
341339
}
342340
}
341+
342+
const img = new Image()
343+
// TODO: question1: 初步排查是css这块上移后不生效,但不知道为什么
343344
// 设置css样式
344345
const style = document.createElement('style')
345346
style.innerHTML = this.getClassRules()
346347
const foreignObject = document.createElement('foreignObject')
347348
foreignObject.appendChild(style)
348349
copy.appendChild(foreignObject)
349-
const img = new Image()
350-
// TODO: question1: 初步排查是css这块上移后不生效,但不知道为什么
351-
352350
return new Promise((resolve) => {
353351
img.onload = () => {
354352
const isFirefox = navigator.userAgent.indexOf('Firefox') > -1
@@ -386,7 +384,6 @@ export class Snapshot {
386384
/*
387385
因为svg中存在dom存放在foreignObject元素中
388386
svg dom => Base64编码字符串 挂载到img上
389-
TODO: 会导致一些清晰度问题这个需要再解决
390387
fixme: XMLSerializer的中的css background url不会下载图片
391388
*/
392389
const svg2Img = `data:image/svg+xml;charset=utf-8,${new XMLSerializer().serializeToString(

sites/docs/docs/api/eventCenterApi.en-US.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ The event object contains the following:
108108
| text:update | Update text | data |
109109
| graph:transform | Triggered when panning or zooming the canvas | data |
110110
| graph:rendered | Triggered after the canvas renders data, i.e. after the lf.render(graphData) method is called. `Add in v1.1.0`| graphData |
111+
| graph:updated | Triggered after the canvas is updated. That is, it is triggered after the lf.render(graphData) method is called or after the properties on the canvas (graphModel) are changed. If the canvas is updated due to active modification of a property, and you want to do something after the canvas is updated, it is recommended to register the event and unregister the event in the callback function in time, or use the once event instead of the on event, because as other properties may also cause the canvas to update, trigger this event. `Add inv2.0.0` | -|
111112

112113
The event object contains the following:
113114

sites/docs/docs/api/eventCenterApi.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ table td:first-of-type {
109109
| text:update | 文案更新 | data |
110110
| graph:transform | 画布平移或者缩放触发 | data |
111111
| graph:rendered | 画布渲染数据后触发. 即 lf.render(graphData)方法被调用后触发。 `v1.1.0新增` | graphData |
112+
| graph:updated | 画布重新更新后触发. 即 lf.render(graphData)方法被调用后或者改变画布(garphModel)上的属性后触发。如果是主动修改某个属性导致画布更新,想要在画布更新后做一些操作,建议注册事件后在回调函数中及时注销该事件,或者使用once事件代替on事件,因为其他属性也可能导致画布更新,触发该事件。`v2.0.0新增` | - |
112113

113114
事件对象包含如下内容:
114115

sites/docs/docs/tutorial/getting-started.en-US.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,23 @@ toc: content
1717

1818
```bash [npm]
1919
npm install @logicflow/core --save
20+
21+
# extension package(No need to import when not using plugins)
22+
npm install @logicflow/extension --save
2023
```
2124

2225
```bash [yarn]
2326
yarn add @logicflow/core
27+
28+
# extension package(No need to import when not using plugins)
29+
yarn add @logicflow/extension
2430
```
2531

2632
```bash [pnpm]
2733
pnpm add @logicflow/core
34+
35+
# extension package(No need to import when not using plugins)
36+
pnpm add @logicflow/extension
2837
```
2938

3039
:::
@@ -38,8 +47,12 @@ pnpm add @logicflow/core
3847
<script src="https://cdn.jsdelivr.net/npm/@logicflow/[email protected]/dist/index.min.js"></script>
3948
<link href="https://cdn.jsdelivr.net/npm/@logicflow/[email protected]/lib/style/index.min.css" rel="stylesheet">
4049

50+
<!-- Import extension package. -->
51+
<script src="https://cdn.jsdelivr.net/npm/@logicflow/[email protected]/dist/index.min.js"></script>
52+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@logicflow/[email protected]/lib/style/index.min.css" />
53+
4154
```
42-
The current latest version is 2.0.0-beta.2. For other versions, please check: https://www.jsdelivr.com/package/npm/@logicflow/core
55+
The curren tversion is 2.0.0-beta.2. For other versions, please check: <a href="https://www.jsdelivr.com/package/npm/@logicflow/core" target="_blank">jsDelivr</a>
4356

4457
## Getting Started
4558

sites/docs/docs/tutorial/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pnpm add @logicflow/extension
5353
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@logicflow/[email protected]/lib/style/index.min.css" />
5454

5555
```
56-
当前最新版本是2.0.0-beta.2,其他版本请查看:<a href="https://www.jsdelivr.com/package/npm/@logicflow/core" target="_blank">jsDelivr</a>
56+
当前引入的版本是2.0.0-beta.2,其他版本请查看:<a href="https://www.jsdelivr.com/package/npm/@logicflow/core" target="_blank">jsDelivr</a>
5757

5858
## 开始使用
5959

0 commit comments

Comments
 (0)