Skip to content

Commit 9164a62

Browse files
committed
feat(extension & docs): highlight插件高亮时触发事件抛出高亮节点信息 fix #2138 & 补充文档中插件事件相关的内容
1 parent 6da2a83 commit 9164a62

File tree

4 files changed

+253
-53
lines changed

4 files changed

+253
-53
lines changed

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

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Card, Divider, Tag } from 'antd'
1+
import { Card, Divider, Radio, Tag } from 'antd'
22
import LogicFlow from '@logicflow/core'
3-
import { useEffect, useRef } from 'react'
3+
import { useEffect, useRef, useState } from 'react'
44
import { Highlight } from '@logicflow/extension'
55

66
import './index.less'
@@ -20,25 +20,41 @@ const config: Partial<LogicFlow.Options> = {
2020
},
2121
plugins: [Highlight],
2222
snapline: true, // 是否开启辅助对齐线
23-
pluginsOptions: {
24-
highlight: {
25-
mode: 'neighbour',
26-
},
27-
},
2823
}
2924

3025
export default function HighLightExtension() {
3126
const containerRef = useRef<HTMLDivElement | null>(null)
27+
const [highlightMode, setHighlightMode] = useState('path')
28+
const lfRef = useRef<LogicFlow | null>(null)
3229

3330
useEffect(() => {
3431
const lf: LogicFlow = new LogicFlow({
3532
container: containerRef.current!,
3633
...config,
34+
pluginsOptions: {
35+
highlight: {
36+
mode: highlightMode,
37+
},
38+
},
39+
})
40+
lfRef.current = lf
41+
lfRef.current.render(data)
42+
lfRef.current.translateCenter()
43+
lfRef.current.on('highlight:single', ({ model }) => {
44+
console.log('highlight:single', model)
45+
})
46+
lfRef.current.on('highlight:neighbours', ({ data, relateElements }) => {
47+
console.log('highlight:neighbours', data, relateElements)
48+
})
49+
lfRef.current.on('highlight:path', ({ data, relateElements }) => {
50+
console.log('highlight:path', data, relateElements)
3751
})
38-
39-
lf.render(data)
40-
lf.translateCenter()
4152
}, [])
53+
useEffect(() => {
54+
;(lfRef.current?.extension.highlight as Highlight)?.setMode(
55+
highlightMode as any,
56+
)
57+
}, [highlightMode])
4258
return (
4359
<Card title="LogicFlow Extension - Highlight" id="card">
4460
<p>
@@ -50,6 +66,17 @@ export default function HighLightExtension() {
5066
</p>
5167
<p>节点:高亮这个节点前后相关的所有边和节点</p>
5268
<p>边:高亮这个边指向的节点前后相关的所有边和节点</p>
69+
<Radio.Group
70+
value={highlightMode}
71+
onChange={(e) => {
72+
setHighlightMode(e.target.value)
73+
}}
74+
style={{ marginBottom: 16 }}
75+
>
76+
<Radio.Button value="path">全路径高亮</Radio.Button>
77+
<Radio.Button value="single">单元素高亮</Radio.Button>
78+
<Radio.Button value="neighbour">相邻元素高亮</Radio.Button>
79+
</Radio.Group>
5380
<Divider />
5481
<div ref={containerRef} id="graph"></div>
5582
</Card>

packages/extension/src/components/highlight/index.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import LogicFlow, { BaseNodeModel } from '@logicflow/core'
2-
import { concat } from 'lodash-es'
1+
import LogicFlow, { BaseEdgeModel, BaseNodeModel } from '@logicflow/core'
2+
import { uniqBy, concat } from 'lodash-es'
33

44
// 后续并入FlowPath
55
const getPath = (id: string, lf: LogicFlow) => {
@@ -99,7 +99,8 @@ export class Highlight {
9999
this.enable = enable
100100
}
101101

102-
setMode(mode: IMode) {
102+
public setMode(mode: IMode) {
103+
console.log('setMode', mode)
103104
this.mode = mode
104105
}
105106

@@ -119,11 +120,14 @@ export class Highlight {
119120
model.sourceNode.updateStyles(this.tempStyles[model.sourceNode.id])
120121
model.targetNode.updateStyles(this.tempStyles[model.targetNode.id])
121122
}
123+
this.lf.emit('highlight:single', {
124+
data: model,
125+
})
122126
}
123127

124128
private highlightNeighbours(id: string) {
125129
const model = this.lf.getModelById(id)
126-
130+
let relateElements: (BaseNodeModel | BaseEdgeModel)[] = []
127131
if (model?.BaseType === 'node') {
128132
// 高亮节点
129133
model.updateStyles(this.tempStyles[id])
@@ -135,23 +139,46 @@ export class Highlight {
135139
concat(incomingEdges, outgoingEdges).forEach((edge) => {
136140
edge.updateStyles(this.tempStyles[edge.id])
137141
})
142+
relateElements = uniqBy(
143+
concat(
144+
relateElements,
145+
incomingNodes,
146+
outgoingNodes,
147+
incomingEdges,
148+
outgoingEdges,
149+
),
150+
'id',
151+
)
138152
} else if (model?.BaseType === 'edge') {
139153
// 高亮边及对应的节点
140154
model.updateStyles(this.tempStyles[id])
141155
model.sourceNode.updateStyles(this.tempStyles[model.sourceNode.id])
142156
model.targetNode.updateStyles(this.tempStyles[model.targetNode.id])
157+
relateElements = [model.sourceNode, model.targetNode]
143158
}
159+
this.lf.emit('highlight:neighbours', {
160+
data: model,
161+
relateElements,
162+
})
144163
}
145164

146165
private highlightPath(id: string) {
147166
const path = getPath(id, this.lf)
167+
const relateElements: any[] = []
148168
path.forEach((_id) => {
169+
const elementModel = this.lf.getModelById(_id)
149170
// 高亮路径上所有的边和节点
150-
this.lf.getModelById(_id)?.updateStyles(this.tempStyles[_id])
171+
elementModel?.updateStyles(this.tempStyles[_id])
172+
relateElements.push(elementModel)
173+
})
174+
this.lf.emit('highlight:path', {
175+
data: this.lf.getModelById(id),
176+
relateElements,
151177
})
152178
}
153179

154180
highlight(id: string, mode: IMode = this.mode) {
181+
console.log('highlight', id, mode)
155182
if (!this.enable) return
156183
if (Object.keys(this.tempStyles).length) {
157184
this.restoreHighlight()

0 commit comments

Comments
 (0)