Skip to content

Commit 82a4c43

Browse files
committed
feat(extension): 继续开发 dynamic-group 插件,修复若干类型问题
- 修复 node-selection 中 properties 未定义类型导致的报错
1 parent d3f86de commit 82a4c43

File tree

2 files changed

+135
-10
lines changed

2 files changed

+135
-10
lines changed

packages/extension/src/dynamic-group/groupNode.ts

Lines changed: 129 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1-
import LogicFlow, { h, RectNode, RectNodeModel } from '@logicflow/core'
2-
import RectSize = LogicFlow.RectSize
1+
import LogicFlow, {
2+
h,
3+
GraphModel,
4+
RectNode,
5+
RectNodeModel,
6+
IRectNodeProperties,
7+
} from '@logicflow/core'
8+
import NodeConfig = LogicFlow.NodeConfig
39

4-
export class GroupNode extends RectNode {
10+
export type IGroupNodeProps = {
11+
model: GroupNodeModel
12+
graphModel: GraphModel
13+
}
14+
15+
// 分组节点默认展开时的大小
16+
const DEFAULT_GROUP_EXPAND_WIDTH = 500
17+
const DEFAULT_GROUP_EXPAND_HEIGHT = 300
18+
// 分组节点默认收起时的大小
19+
const DEFAULT_GROUP_COLLAPSE_WIDTH = 80
20+
const DEFAULT_GROUP_COLLAPSE_HEIGHT = 60
21+
22+
const DEFAULT_BOTTOM_Z_INDEX = -10000
23+
24+
export class GroupNode<
25+
P extends IGroupNodeProps = IGroupNodeProps,
26+
> extends RectNode<P> {
527
getResizeControl(): h.JSX.Element | null {
628
const { resizable, properties } = this.props.model
729
const showResizeControl = resizable && properties._isCollapsed
830
return showResizeControl ? super.getResizeControl() : null
931
}
1032

1133
getGroupShape(): h.JSX.Element | null {
34+
// TODO: 此区域用于初始化 group container, 即元素拖拽进入感应区域
1235
return null
1336
}
1437

@@ -55,7 +78,7 @@ export class GroupNode extends RectNode {
5578
x: sx,
5679
y: sy,
5780
onClick: () => {
58-
model.foldGroup(!model.properties.isFolded)
81+
model.toggleCollapse(!model.properties.isCollapsed)
5982
},
6083
}),
6184
operateIcon,
@@ -71,19 +94,116 @@ export class GroupNode extends RectNode {
7194
}
7295
}
7396

74-
export class GroupNodeModel extends RectNodeModel {
97+
export type IGroupNodeProperties = {
98+
children?: string[]
99+
isCollapsed?: boolean
100+
isRestrict?: boolean
101+
collapsible?: boolean
102+
collapsedWidth?: number
103+
collapsedHeight?: number
104+
105+
expandWidth?: number
106+
expandHeight?: number
107+
zIndex?: number
108+
autoToFront?: boolean
109+
} & IRectNodeProperties
110+
111+
export class GroupNodeModel extends RectNodeModel<IGroupNodeProperties> {
75112
readonly isGroup = true
76113

77114
// 保存组内的节点
78115
children: Set<string> = new Set()
79116
// 是否限制组内节点的移动范围。默认不限制
80117
isRestrict: boolean = false
81118
// 分组节点是否可以折叠
82-
foldable: boolean = true
83-
isFolded: boolean = false
119+
collapsible: boolean = true
120+
// 当前组是否收起状态
121+
isCollapsed: boolean = false
122+
123+
// 分组节点 初始化尺寸(默认展开),后续支持从 properties 中传入 width 和 height 设置
124+
expandWidth: number = DEFAULT_GROUP_EXPAND_WIDTH
125+
expandHeight: number = DEFAULT_GROUP_EXPAND_HEIGHT
126+
// 折叠后
127+
collapsedWidth: number = DEFAULT_GROUP_COLLAPSE_WIDTH
128+
collapsedHeight: number = DEFAULT_GROUP_COLLAPSE_HEIGHT
129+
130+
childrenLastFoldState: Record<string, boolean> = {}
131+
132+
constructor(data: NodeConfig<IGroupNodeProperties>, graphModel: GraphModel) {
133+
super(data, graphModel)
134+
135+
this.setAttributes()
136+
}
137+
138+
setAttributes() {
139+
super.setAttributes()
140+
141+
const {
142+
children,
143+
width,
144+
height,
145+
collapsible,
146+
isCollapsed,
147+
zIndex,
148+
isRestrict,
149+
autoToFront,
150+
} = this.properties
84151

85-
defaultSize: RectSize = { width: 400, height: 200 }
86-
expandSize: RectSize = { width: 80, height: 40 }
152+
if (children) this.children = new Set(children)
153+
154+
if (width) {
155+
this.width = width
156+
this.expandWidth = width
157+
}
158+
if (height) {
159+
this.height = height
160+
this.expandHeight = height
161+
}
162+
this.zIndex = zIndex ?? DEFAULT_BOTTOM_Z_INDEX
163+
164+
this.isRestrict = isRestrict ?? false
165+
this.collapsible = collapsible ?? true
166+
this.autoToFront = autoToFront ?? false
167+
168+
// 禁用掉 Group 节点的文本编辑能力
169+
this.text.editable = false
170+
this.text.draggable = false
171+
172+
// 当前状态为折叠时,调用一下折叠的方法
173+
// 确认是否
174+
isCollapsed && this.collapseGroup()
175+
}
176+
177+
/**
178+
* 折叠 or 展开分组
179+
* 1. 折叠分组的宽高
180+
* 2. 处理分组子节点
181+
* 3. 处理连线
182+
* @param collapsed
183+
*/
184+
toggleCollapse(collapsed?: boolean) {
185+
console.log('collapsed', collapsed)
186+
}
187+
188+
collapse() {}
189+
190+
expand() {}
191+
192+
/**
193+
* 重写 Group 节点的 Resize Outline
194+
*/
195+
getResizeOutlineStyle(): LogicFlow.CommonTheme {
196+
const style = super.getResizeOutlineStyle()
197+
style.stroke = 'none'
198+
return style
199+
}
200+
201+
// TODO: 是否是设置 group 节点没有锚点,而不是设置成透明???
202+
getAnchorStyle() {
203+
const style = super.getAnchorStyle()
204+
style.stroke = 'transparent'
205+
return style
206+
}
87207
}
88208

89209
export const groupNode = {

packages/extension/src/materials/node-selection/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { get } from 'lodash-es'
22
import { h, PolygonNode, PolygonNodeModel } from '@logicflow/core'
33

4+
export type INodeSelectionProperties = {
5+
strokeColor?: string | 'none'
6+
node_selection_ids?: string[]
7+
}
8+
49
class NodeSelectionView extends PolygonNode {
510
getLabelShape(): h.JSX.Element {
611
const { id, x, y, width, height, properties } = this.props.model
@@ -74,7 +79,7 @@ class NodeSelectionView extends PolygonNode {
7479
}
7580
}
7681

77-
class NodeSelectionModel extends PolygonNodeModel {
82+
class NodeSelectionModel extends PolygonNodeModel<INodeSelectionProperties> {
7883
d = 10
7984

8085
initNodeData(data) {

0 commit comments

Comments
 (0)