Skip to content

Commit 5e2e614

Browse files
committed
fix(extension): 修复线的 menu 无法正常显示的 bug
- 修复初始化时 edgeConfig 中 type 未传值场景下,未使用 graphModel.edgeType(默认边类型) 初始化边的 bug
1 parent 1d0d44e commit 5e2e614

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import styles from './index.less'
88
import '@logicflow/core/es/index.css'
99
import '@logicflow/extension/es/index.css'
1010

11+
import EdgeData = LogicFlow.EdgeData
12+
import NodeData = LogicFlow.NodeData
13+
1114
const config: Partial<LogicFlow.Options> = {
1215
isSilentMode: false,
1316
stopScrollGraph: true,
@@ -93,6 +96,13 @@ const data = {
9396
text: 'html节点',
9497
},
9598
],
99+
edges: [
100+
{
101+
type: 'polyline',
102+
sourceNodeId: '1',
103+
targetNodeId: '2',
104+
},
105+
],
96106
}
97107

98108
export default function MenuExtension() {
@@ -112,6 +122,58 @@ export default function MenuExtension() {
112122
plugins: [Menu],
113123
})
114124

125+
lf.addMenuConfig({
126+
nodeMenu: [
127+
{
128+
text: '分享',
129+
callback() {
130+
alert('分享成功!')
131+
},
132+
},
133+
{
134+
text: '属性',
135+
callback(node: NodeData) {
136+
alert(`
137+
节点id:${node.id}
138+
节点类型:${node.type}
139+
节点坐标:(x: ${node.x}, y: ${node.y})
140+
`)
141+
},
142+
},
143+
],
144+
edgeMenu: [
145+
{
146+
text: '属性',
147+
callback(edge: EdgeData) {
148+
const {
149+
id,
150+
type,
151+
startPoint,
152+
endPoint,
153+
sourceNodeId,
154+
targetNodeId,
155+
} = edge
156+
alert(`
157+
边id:${id}
158+
边类型:${type}
159+
边起点坐标:(startPoint: [${startPoint.x}, ${startPoint.y}])
160+
边终点坐标:(endPoint: [${endPoint.x}, ${endPoint.y}])
161+
源节点id:${sourceNodeId}
162+
目标节点id:${targetNodeId}
163+
`)
164+
},
165+
},
166+
],
167+
graphMenu: [
168+
{
169+
text: '分享',
170+
callback() {
171+
alert('分享成功!')
172+
},
173+
},
174+
],
175+
})
176+
115177
lf.render(data)
116178
lfRef.current = lf
117179
}

packages/core/src/model/GraphModel.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,11 @@ export class GraphModel {
420420
this.nodes = []
421421
}
422422
if (graphData.edges) {
423+
const currEdgeType = this.edgeType
423424
this.edges = map(graphData.edges, (edge) => {
424-
const Model = this.getModel(edge.type ?? '') as BaseEdgeModelCtor
425+
const Model = this.getModel(
426+
edge.type ?? currEdgeType,
427+
) as BaseEdgeModelCtor
425428
if (!Model) {
426429
throw new Error(`找不到${edge.type}对应的边。`)
427430
}

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import LogicFlow from '@logicflow/core'
22

3+
import GraphData = LogicFlow.GraphData
4+
import NodeData = LogicFlow.NodeData
5+
import EdgeData = LogicFlow.EdgeData
6+
37
type SetType = 'add' | 'reset'
48

59
export type MenuItem = {
@@ -25,7 +29,7 @@ class Menu {
2529
private __container?: HTMLElement
2630
private __menuDOM?: HTMLElement
2731
private menuTypeMap?: Map<string, MenuItem[]>
28-
private __currentData: any
32+
private __currentData: EdgeData | NodeData | GraphData | null = null
2933
static pluginName = 'menu'
3034

3135
constructor({ lf }) {
@@ -107,7 +111,7 @@ class Menu {
107111
this.menuTypeMap?.set(DefaultSelectionMenuKey, DefaultSelectionMenu)
108112
}
109113

110-
render(lf, container) {
114+
render(lf: LogicFlow, container: HTMLElement) {
111115
if (lf.options.isSilentMode) return
112116
this.__container = container
113117
this.__currentData = null // 当前展示的菜单所属元素的model数据
@@ -147,7 +151,7 @@ class Menu {
147151
)
148152
}
149153
// 通过事件控制菜单的显示和隐藏
150-
this.lf.on('node:contextmenu', ({ data, position, e }: any) => {
154+
this.lf.on('node:contextmenu', ({ data, position, e }) => {
151155
const {
152156
domOverlayPosition: { x, y },
153157
} = position
@@ -175,15 +179,15 @@ class Menu {
175179
clientY: e.clientY,
176180
})
177181
})
178-
this.lf.on('edge:contextmenu', ({ data, position, e }: any) => {
182+
this.lf.on('edge:contextmenu', ({ data, position, e }) => {
179183
const {
180184
domOverlayPosition: { x, y },
181185
} = position
182186
const { id } = data
183187
const model = this.lf.graphModel.getEdgeModelById(id)
184188
if (!model) return
185189
let menuList: any = []
186-
const typeMenus = this.menuTypeMap?.get(model.type) ?? []
190+
const typeMenus = this.menuTypeMap?.get(model.type)
187191
// 如果单个节点自定义了边
188192
if (model && model.menu && Array.isArray(model.menu)) {
189193
menuList = model.menu
@@ -202,21 +206,22 @@ class Menu {
202206
clientY: e.clientY,
203207
})
204208
})
205-
this.lf.on('blank:contextmenu', ({ position }: any) => {
209+
this.lf.on('blank:contextmenu', ({ position }) => {
206210
const menuList = this.menuTypeMap?.get(DefaultGraphMenuKey) ?? []
207211
const {
208212
domOverlayPosition: { x, y },
209213
} = position
210214
this.showMenu(x, y, menuList)
211215
})
212-
this.lf.on('selection:contextmenu', ({ data, position }: any) => {
216+
this.lf.on('selection:contextmenu', ({ data, position }) => {
213217
const menuList = this.menuTypeMap?.get(DefaultSelectionMenuKey)
214218
const {
215219
domOverlayPosition: { x, y },
216220
} = position
217221
this.__currentData = data
218222
this.showMenu(x, y, menuList)
219223
})
224+
220225
this.lf.on('node:mousedown', () => {
221226
this.__menuDOM!.style.display = 'none'
222227
})

0 commit comments

Comments
 (0)