Skip to content

Commit

Permalink
fix(extension): dynamic-group 的 addChild 没有处理 nodeGroupMap
Browse files Browse the repository at this point in the history
通过 addChild 添加的节点没有在 nodeGroupMap 中建立映射关系,导致相关逻辑异常。改为在 addChild触发的事件中建立映射

close didi#2041,didi#2042
  • Loading branch information
ZivvW committed Jan 17, 2025
1 parent 6a837b6 commit f78b832
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ export default function DynamicGroupDemo() {
lf.graphModel.moveNode(selectedNode.id, 10, 0)
},
})
;(lf.extension.control as Control).addItem({
key: 'move-group',
iconClass: 'custom-minimap',
title: '',
text: 'addChild',
onClick: (lf) => {
const groupModel = lf.getNodeModelById('#2041_dynamic-group_1')
groupModel?.addChild('#2041_circle_1')
groupModel?.addChild('#2041_circle_2')
},
})

const dndPanelConfig = getDndPanelConfig(lf)
lf.setPatternItems(dndPanelConfig)
Expand Down Expand Up @@ -222,6 +233,44 @@ export default function DynamicGroupDemo() {
value: 'Rect',
},
},
// #2041
{
id: '#2041_circle_1',
type: 'circle',
x: 1022,
y: 170,
text: {
value: 'circle_1',
x: 1022,
y: 170,
draggable: true,
},
},
{
id: '#2041_circle_2',
type: 'circle',
x: 1180,
y: 170,
text: {
value: 'circle_2',
x: 1180,
y: 170,
draggable: true,
},
},
{
id: '#2041_dynamic-group_1',
type: 'dynamic-group',
x: 1042,
y: 189,
text: 'dynamic-group_fix_#2041',
resizable: true,
properties: {
width: 420,
height: 250,
radius: 5,
},
},
],
edges: [],
// 'nodes': [
Expand Down
14 changes: 12 additions & 2 deletions packages/extension/src/dynamic-group/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ export class DynamicGroup {
console.log('isAllowAppendIn', isAllowAppendIn)
if (isAllowAppendIn) {
group.addChild(node.id)
this.nodeGroupMap.set(node.id, group.id)
// 建立节点与 group 的映射关系放在了 group.addChild 触发的事件中,与直接调用 addChild 的行为保持一致
// TODO 下面这个是干什么的,是否需要一起移动到事件的逻辑中?
group.setAllowAppendChild(true)
} else {
// 抛出不允许插入的事件
Expand All @@ -242,6 +243,14 @@ export class DynamicGroup {
}
}

onGroupAddNode = ({
data: groupData,
childId,
}: CallbackArgs<'group:add-node'>) => {
console.log('group:add-node', groupData)
this.nodeGroupMap.set(childId, groupData.id)
}

removeNodeFromGroup = ({
data: node,
model,
Expand Down Expand Up @@ -666,7 +675,7 @@ export class DynamicGroup {

lf.on('graph:updated', ({ data }) => console.log('data', data))

lf.on('group:add-node', ({ data }) => console.log('group:add-node', data))
lf.on('group:add-node', this.onGroupAddNode)

// https://github.com/didi/LogicFlow/issues/1346
// 重写 addElements() 方法,在 addElements() 原有基础上增加对 group 内部所有 nodes 和 edges 的复制功能
Expand Down Expand Up @@ -733,6 +742,7 @@ export class DynamicGroup {
this.lf.off('node:click', this.onNodeSelect)
this.lf.off('node:mousemove', this.onNodeMove)
this.lf.off('graph:rendered', this.onGraphRendered)
this.lf.off('group:add-node', this.onGroupAddNode)

// 还原 lf.addElements 方法?
// 移除 graphModel 上重写的 addNodeMoveRules 方法?
Expand Down
5 changes: 4 additions & 1 deletion packages/extension/src/dynamic-group/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,10 @@ export class DynamicGroupNodeModel extends RectNodeModel<IGroupNodeProperties> {
addChild(id: string) {
this.children.add(id)
const groupData = this.getData()
this.graphModel.eventCenter.emit('group:add-node', { data: groupData })
this.graphModel.eventCenter.emit('group:add-node', {
data: groupData,
childId: id,
})
}

/**
Expand Down

0 comments on commit f78b832

Please sign in to comment.