@knotx/plugins-group 0.4.12 → 0.4.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.en.md +288 -0
  2. package/README.md +288 -0
  3. package/package.json +10 -10
package/README.en.md ADDED
@@ -0,0 +1,288 @@
1
+ # @knotx/plugins-group
2
+
3
+ Group plugin that provides node grouping management functionality.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @knotx/plugins-group
9
+ ```
10
+
11
+ ## Overview
12
+
13
+ `@knotx/plugins-group` is a grouping plugin for the KnotX framework that provides node grouping management functionality. It supports creating, dissolving, and deleting groups, as well as adding or removing nodes from groups. Group nodes can automatically calculate boundaries and support layered rendering and style customization.
14
+
15
+ ## Implementation Principles
16
+
17
+ The plugin implements grouping functionality through bidirectional relationship management:
18
+
19
+ 1. **Group Relationship Management**: Uses `DualRelation` to maintain parent-child node relationships
20
+ 2. **Boundary Calculation**: Automatically calculates group boundaries with support for custom padding
21
+ 3. **Layered Rendering**: Integrates node level getter to ensure group nodes are rendered at the correct level
22
+ 4. **Operation Pipeline**: Handles group-related node operations through operation pipeline
23
+
24
+ ## Dependencies
25
+
26
+ This plugin depends on the following packages:
27
+
28
+ - `@knotx/core` - Core functionality
29
+ - `@knotx/decorators` - Decorator support
30
+ - `@knotx/plugins-base-render` - Base render plugin (peer dependency)
31
+ - `@knotx/jsx` - JSX support (peer dependency)
32
+ - `rxjs` - Reactive programming
33
+
34
+ ## API Usage
35
+
36
+ ### Basic Usage
37
+
38
+ ```typescript
39
+ import { Group } from '@knotx/plugins-group'
40
+
41
+ // Create group plugin instance
42
+ const group = new Group()
43
+
44
+ // Register plugin in engine
45
+ engine.use(group)
46
+ ```
47
+
48
+ ### Create Group
49
+
50
+ ```typescript
51
+ const { createGroup } = useKnotX().group
52
+
53
+ // Create basic group
54
+ const groupId = createGroup({
55
+ id: 'group-1',
56
+ nodeIds: ['node-1', 'node-2', 'node-3'],
57
+ data: { label: 'My Group' }
58
+ })
59
+
60
+ // Create group with configuration
61
+ const groupId = createGroup({
62
+ id: 'group-2',
63
+ nodeIds: ['node-4', 'node-5'],
64
+ config: {
65
+ autoBounds: true,
66
+ padding: 20,
67
+ style: {
68
+ backgroundColor: 'rgba(59, 130, 246, 0.1)',
69
+ borderColor: '#3b82f6',
70
+ borderWidth: 2,
71
+ borderStyle: 'dashed',
72
+ borderRadius: 8
73
+ }
74
+ },
75
+ data: { label: 'Custom Group' }
76
+ })
77
+ ```
78
+
79
+ ### Group Operations
80
+
81
+ ```typescript
82
+ const { dissolveGroup, deleteGroup, addNodesToGroup, removeNodesFromGroup, moveGroup } = useKnotX().group
83
+
84
+ // Dissolve group
85
+ dissolveGroup('group-1')
86
+
87
+ // Delete group
88
+ deleteGroup('group-1')
89
+
90
+ // Add nodes to group
91
+ addNodesToGroup('group-1', ['node-6', 'node-7'])
92
+
93
+ // Remove nodes from group
94
+ removeNodesFromGroup('group-1', ['node-1'])
95
+
96
+ // Move group
97
+ moveGroup('group-1', { x: 100, y: 100 })
98
+
99
+ // Move group with delta
100
+ moveGroup('group-1', { x: 50, y: 50 }, true)
101
+ ```
102
+
103
+ ### Complete Example
104
+
105
+ ```typescript
106
+ import { Engine } from '@knotx/core'
107
+ import { BaseRender } from '@knotx/plugins-base-render'
108
+ import { Group } from '@knotx/plugins-group'
109
+
110
+ const engine = new Engine()
111
+
112
+ // Register dependency plugins
113
+ engine.use(new BaseRender())
114
+
115
+ // Register group plugin
116
+ engine.use(new Group())
117
+
118
+ // Create nodes
119
+ engine.dispatchNodeOperation({
120
+ type: 'batch',
121
+ operations: [
122
+ { type: 'add', node: { id: 'node-1', type: 'default', position: { x: 100, y: 100 }, data: { label: 'Node 1' } } },
123
+ { type: 'add', node: { id: 'node-2', type: 'default', position: { x: 200, y: 100 }, data: { label: 'Node 2' } } },
124
+ { type: 'add', node: { id: 'node-3', type: 'default', position: { x: 150, y: 200 }, data: { label: 'Node 3' } } }
125
+ ]
126
+ })
127
+
128
+ // Create group
129
+ const groupPlugin = engine.getPlugin('group')
130
+ const groupId = groupPlugin.createGroup({
131
+ id: 'my-group',
132
+ nodeIds: ['node-1', 'node-2', 'node-3'],
133
+ config: {
134
+ autoBounds: true,
135
+ padding: 15,
136
+ style: {
137
+ backgroundColor: 'rgba(34, 197, 94, 0.1)',
138
+ borderColor: '#22c55e',
139
+ borderWidth: 1,
140
+ borderStyle: 'solid',
141
+ borderRadius: 6
142
+ }
143
+ },
144
+ data: { label: 'My Group' }
145
+ })
146
+ ```
147
+
148
+ ## Configuration Interface
149
+
150
+ ### GroupConfig
151
+
152
+ ```typescript
153
+ interface GroupConfig {
154
+ /**
155
+ * Whether to automatically calculate boundaries
156
+ * @default true
157
+ */
158
+ autoBounds?: boolean
159
+ /**
160
+ * Padding
161
+ * @default 10
162
+ */
163
+ padding?: number
164
+ /**
165
+ * Group style
166
+ */
167
+ style?: GroupStyle
168
+ }
169
+ ```
170
+
171
+ ### GroupStyle
172
+
173
+ ```typescript
174
+ interface GroupStyle {
175
+ /**
176
+ * Background color
177
+ */
178
+ backgroundColor?: string
179
+ /**
180
+ * Border color
181
+ */
182
+ borderColor?: string
183
+ /**
184
+ * Border width
185
+ */
186
+ borderWidth?: number
187
+ /**
188
+ * Border style
189
+ */
190
+ borderStyle?: string
191
+ /**
192
+ * Border radius
193
+ */
194
+ borderRadius?: number
195
+ }
196
+ ```
197
+
198
+ ### GroupAPI
199
+
200
+ ```typescript
201
+ interface GroupAPI {
202
+ createGroup: (params: {
203
+ id: string
204
+ nodeIds: string[]
205
+ config?: GroupConfig
206
+ data?: Record<string, any>
207
+ }) => string
208
+ dissolveGroup: (groupId: string, recursive?: boolean) => boolean
209
+ deleteGroup: (groupId: string) => boolean
210
+ addNodesToGroup: (groupId: string, nodeIds: string[]) => boolean
211
+ removeNodesFromGroup: (groupId: string, nodeIds: string[]) => boolean
212
+ moveGroup: (groupId: string, position: NodePosition, isDelta?: boolean) => boolean
213
+ }
214
+ ```
215
+
216
+ ## Node Type Extension
217
+
218
+ ```typescript
219
+ declare module '@knotx/core' {
220
+ interface Node {
221
+ /**
222
+ * The group this node belongs to
223
+ */
224
+ group?: string
225
+ /**
226
+ * Current group configuration when type='group'
227
+ */
228
+ groupConfig?: GroupConfig
229
+ }
230
+ }
231
+ ```
232
+
233
+ ## Default Configuration
234
+
235
+ ```typescript
236
+ const DEFAULT_GROUP_PADDING = 10
237
+ const DEFAULT_GROUP_STYLE = {
238
+ backgroundColor: 'rgba(100, 100, 100, 0.1)',
239
+ borderColor: '#666',
240
+ borderWidth: 1,
241
+ borderStyle: 'solid',
242
+ borderRadius: 4
243
+ }
244
+ ```
245
+
246
+ ## Group Rendering
247
+
248
+ The plugin provides a default group renderer that supports:
249
+
250
+ - Automatic boundary calculation
251
+ - Custom styling
252
+ - Recursive group support
253
+ - Layered rendering
254
+
255
+ ```tsx
256
+ // Group nodes automatically use 'group' type
257
+ function GroupNode({ node }) {
258
+ // Automatically render group boundaries and styles
259
+ return <div className="group-node">{/* group content */}</div>
260
+ }
261
+ ```
262
+
263
+ ## Level Management
264
+
265
+ The plugin automatically integrates level management functionality:
266
+
267
+ - Group nodes are rendered behind child nodes
268
+ - Supports correct levels for nested groups
269
+ - Automatically calculates node levels
270
+
271
+ ## File Directory Structure
272
+
273
+ ```
274
+ packages/plugins-group/
275
+ ├── src/
276
+ │ ├── group.tsx # Group plugin main file
277
+ │ └── index.ts # Export file
278
+ ├── dist/ # Build output directory
279
+ ├── CHANGELOG.md # Change log
280
+ ├── package.json # Package configuration
281
+ ├── build.config.ts # Build configuration
282
+ ├── eslint.config.mjs # ESLint configuration
283
+ └── tsconfig.json # TypeScript configuration
284
+ ```
285
+
286
+ ## License
287
+
288
+ MIT
package/README.md ADDED
@@ -0,0 +1,288 @@
1
+ # @knotx/plugins-group
2
+
3
+ 分组插件,提供节点分组管理功能。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @knotx/plugins-group
9
+ ```
10
+
11
+ ## 基本概述
12
+
13
+ `@knotx/plugins-group` 是 KnotX 框架的分组插件,提供节点分组管理功能。支持创建、解散、删除分组,以及向分组中添加或移除节点。分组节点可以自动计算边界,支持分层渲染和样式定制。
14
+
15
+ ## 实现原理
16
+
17
+ 该插件通过双向关系管理实现分组功能:
18
+
19
+ 1. **分组关系管理**:使用 `DualRelation` 维护父子节点关系
20
+ 2. **边界计算**:自动计算分组边界并支持自定义内边距
21
+ 3. **分层渲染**:集成节点层级获取器,确保分组节点在正确的层级渲染
22
+ 4. **操作管道**:通过操作管道处理分组相关的节点操作
23
+
24
+ ## 依赖插件
25
+
26
+ 该插件依赖以下包:
27
+
28
+ - `@knotx/core` - 核心功能
29
+ - `@knotx/decorators` - 装饰器支持
30
+ - `@knotx/plugins-base-render` - 基础渲染插件(peer dependency)
31
+ - `@knotx/jsx` - JSX 支持(peer dependency)
32
+ - `rxjs` - 响应式编程
33
+
34
+ ## API 使用
35
+
36
+ ### 基本使用
37
+
38
+ ```typescript
39
+ import { Group } from '@knotx/plugins-group'
40
+
41
+ // 创建分组插件实例
42
+ const group = new Group()
43
+
44
+ // 在引擎中注册插件
45
+ engine.use(group)
46
+ ```
47
+
48
+ ### 创建分组
49
+
50
+ ```typescript
51
+ const { createGroup } = useKnotX().group
52
+
53
+ // 创建基础分组
54
+ const groupId = createGroup({
55
+ id: 'group-1',
56
+ nodeIds: ['node-1', 'node-2', 'node-3'],
57
+ data: { label: '我的分组' }
58
+ })
59
+
60
+ // 创建带配置的分组
61
+ const groupId = createGroup({
62
+ id: 'group-2',
63
+ nodeIds: ['node-4', 'node-5'],
64
+ config: {
65
+ autoBounds: true,
66
+ padding: 20,
67
+ style: {
68
+ backgroundColor: 'rgba(59, 130, 246, 0.1)',
69
+ borderColor: '#3b82f6',
70
+ borderWidth: 2,
71
+ borderStyle: 'dashed',
72
+ borderRadius: 8
73
+ }
74
+ },
75
+ data: { label: '自定义分组' }
76
+ })
77
+ ```
78
+
79
+ ### 分组操作
80
+
81
+ ```typescript
82
+ const { dissolveGroup, deleteGroup, addNodesToGroup, removeNodesFromGroup, moveGroup } = useKnotX().group
83
+
84
+ // 解散分组
85
+ dissolveGroup('group-1')
86
+
87
+ // 删除分组
88
+ deleteGroup('group-1')
89
+
90
+ // 向分组添加节点
91
+ addNodesToGroup('group-1', ['node-6', 'node-7'])
92
+
93
+ // 从分组移除节点
94
+ removeNodesFromGroup('group-1', ['node-1'])
95
+
96
+ // 移动分组
97
+ moveGroup('group-1', { x: 100, y: 100 })
98
+
99
+ // 增量移动分组
100
+ moveGroup('group-1', { x: 50, y: 50 }, true)
101
+ ```
102
+
103
+ ### 完整示例
104
+
105
+ ```typescript
106
+ import { Engine } from '@knotx/core'
107
+ import { BaseRender } from '@knotx/plugins-base-render'
108
+ import { Group } from '@knotx/plugins-group'
109
+
110
+ const engine = new Engine()
111
+
112
+ // 注册依赖插件
113
+ engine.use(new BaseRender())
114
+
115
+ // 注册分组插件
116
+ engine.use(new Group())
117
+
118
+ // 创建节点
119
+ engine.dispatchNodeOperation({
120
+ type: 'batch',
121
+ operations: [
122
+ { type: 'add', node: { id: 'node-1', type: 'default', position: { x: 100, y: 100 }, data: { label: '节点1' } } },
123
+ { type: 'add', node: { id: 'node-2', type: 'default', position: { x: 200, y: 100 }, data: { label: '节点2' } } },
124
+ { type: 'add', node: { id: 'node-3', type: 'default', position: { x: 150, y: 200 }, data: { label: '节点3' } } }
125
+ ]
126
+ })
127
+
128
+ // 创建分组
129
+ const groupPlugin = engine.getPlugin('group')
130
+ const groupId = groupPlugin.createGroup({
131
+ id: 'my-group',
132
+ nodeIds: ['node-1', 'node-2', 'node-3'],
133
+ config: {
134
+ autoBounds: true,
135
+ padding: 15,
136
+ style: {
137
+ backgroundColor: 'rgba(34, 197, 94, 0.1)',
138
+ borderColor: '#22c55e',
139
+ borderWidth: 1,
140
+ borderStyle: 'solid',
141
+ borderRadius: 6
142
+ }
143
+ },
144
+ data: { label: '我的分组' }
145
+ })
146
+ ```
147
+
148
+ ## 配置接口
149
+
150
+ ### GroupConfig
151
+
152
+ ```typescript
153
+ interface GroupConfig {
154
+ /**
155
+ * 是否自动计算边界
156
+ * @default true
157
+ */
158
+ autoBounds?: boolean
159
+ /**
160
+ * 内边距
161
+ * @default 10
162
+ */
163
+ padding?: number
164
+ /**
165
+ * 分组样式
166
+ */
167
+ style?: GroupStyle
168
+ }
169
+ ```
170
+
171
+ ### GroupStyle
172
+
173
+ ```typescript
174
+ interface GroupStyle {
175
+ /**
176
+ * 背景颜色
177
+ */
178
+ backgroundColor?: string
179
+ /**
180
+ * 边框颜色
181
+ */
182
+ borderColor?: string
183
+ /**
184
+ * 边框宽度
185
+ */
186
+ borderWidth?: number
187
+ /**
188
+ * 边框样式
189
+ */
190
+ borderStyle?: string
191
+ /**
192
+ * 边框圆角
193
+ */
194
+ borderRadius?: number
195
+ }
196
+ ```
197
+
198
+ ### GroupAPI
199
+
200
+ ```typescript
201
+ interface GroupAPI {
202
+ createGroup: (params: {
203
+ id: string
204
+ nodeIds: string[]
205
+ config?: GroupConfig
206
+ data?: Record<string, any>
207
+ }) => string
208
+ dissolveGroup: (groupId: string, recursive?: boolean) => boolean
209
+ deleteGroup: (groupId: string) => boolean
210
+ addNodesToGroup: (groupId: string, nodeIds: string[]) => boolean
211
+ removeNodesFromGroup: (groupId: string, nodeIds: string[]) => boolean
212
+ moveGroup: (groupId: string, position: NodePosition, isDelta?: boolean) => boolean
213
+ }
214
+ ```
215
+
216
+ ## 节点类型扩展
217
+
218
+ ```typescript
219
+ declare module '@knotx/core' {
220
+ interface Node {
221
+ /**
222
+ * 所处的组
223
+ */
224
+ group?: string
225
+ /**
226
+ * type='group' 时,当前组配置
227
+ */
228
+ groupConfig?: GroupConfig
229
+ }
230
+ }
231
+ ```
232
+
233
+ ## 默认配置
234
+
235
+ ```typescript
236
+ const DEFAULT_GROUP_PADDING = 10
237
+ const DEFAULT_GROUP_STYLE = {
238
+ backgroundColor: 'rgba(100, 100, 100, 0.1)',
239
+ borderColor: '#666',
240
+ borderWidth: 1,
241
+ borderStyle: 'solid',
242
+ borderRadius: 4
243
+ }
244
+ ```
245
+
246
+ ## 分组渲染
247
+
248
+ 插件提供了默认的分组渲染器,支持:
249
+
250
+ - 自动边界计算
251
+ - 自定义样式
252
+ - 递归分组支持
253
+ - 分层渲染
254
+
255
+ ```tsx
256
+ // 分组节点会自动使用 'group' 类型
257
+ function GroupNode({ node }) {
258
+ // 自动渲染分组边界和样式
259
+ return <div className="group-node">{/* 分组内容 */}</div>
260
+ }
261
+ ```
262
+
263
+ ## 层级管理
264
+
265
+ 插件自动集成了层级管理功能:
266
+
267
+ - 分组节点渲染在子节点后面
268
+ - 支持嵌套分组的正确层级
269
+ - 自动计算节点层级
270
+
271
+ ## 文件目录结构
272
+
273
+ ```
274
+ packages/plugins-group/
275
+ ├── src/
276
+ │ ├── group.tsx # 分组插件主文件
277
+ │ └── index.ts # 导出文件
278
+ ├── dist/ # 编译输出目录
279
+ ├── CHANGELOG.md # 变更日志
280
+ ├── package.json # 包配置
281
+ ├── build.config.ts # 构建配置
282
+ ├── eslint.config.mjs # ESLint 配置
283
+ └── tsconfig.json # TypeScript 配置
284
+ ```
285
+
286
+ ## 许可证
287
+
288
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knotx/plugins-group",
3
- "version": "0.4.12",
3
+ "version": "0.4.13",
4
4
  "description": "Group Plugin for Knotx",
5
5
  "author": "boenfu",
6
6
  "license": "MIT",
@@ -28,20 +28,20 @@
28
28
  "dist"
29
29
  ],
30
30
  "peerDependencies": {
31
- "@knotx/jsx": "0.4.12",
32
- "@knotx/plugins-base-render": "0.4.12"
31
+ "@knotx/jsx": "0.4.13",
32
+ "@knotx/plugins-base-render": "0.4.13"
33
33
  },
34
34
  "dependencies": {
35
35
  "rxjs": "^7.8.1",
36
- "@knotx/core": "0.4.12",
37
- "@knotx/decorators": "0.4.12"
36
+ "@knotx/core": "0.4.13",
37
+ "@knotx/decorators": "0.4.13"
38
38
  },
39
39
  "devDependencies": {
40
- "@knotx/build-config": "0.4.12",
41
- "@knotx/eslint-config": "0.4.12",
42
- "@knotx/jsx": "0.4.12",
43
- "@knotx/plugins-base-render": "0.4.12",
44
- "@knotx/typescript-config": "0.4.12"
40
+ "@knotx/build-config": "0.4.13",
41
+ "@knotx/eslint-config": "0.4.13",
42
+ "@knotx/jsx": "0.4.13",
43
+ "@knotx/plugins-base-render": "0.4.13",
44
+ "@knotx/typescript-config": "0.4.13"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "unbuild",