|
| 1 | +# Working with Groups |
| 2 | + |
| 3 | +Groups in the library provide a way to organize and manage collections of blocks. They support visual styling, behavior configuration, and geometric properties. |
| 4 | + |
| 5 | +## How to Use Groups |
| 6 | + |
| 7 | +There are two ways to work with groups: |
| 8 | + |
| 9 | +### 1. Automatic Groups |
| 10 | + |
| 11 | +Use this when you want groups to update automatically based on block properties. For example, you can group blocks by their color or type. |
| 12 | + |
| 13 | +```typescript |
| 14 | +// Make groups automatically |
| 15 | +const AutoGroups = BlockGroups.withBlockGrouping({ |
| 16 | + // Put blocks in groups |
| 17 | + groupingFn: (blocks) => { |
| 18 | + const groups = {}; |
| 19 | + blocks.forEach(block => { |
| 20 | + const groupId = block.type; // or any other property |
| 21 | + if (!groups[groupId]) groups[groupId] = []; |
| 22 | + groups[groupId].push(block); |
| 23 | + }); |
| 24 | + return groups; |
| 25 | + }, |
| 26 | + // Set how groups look |
| 27 | + mapToGroups: (groupId, { rect }) => ({ |
| 28 | + id: groupId, |
| 29 | + rect, |
| 30 | + style: { |
| 31 | + background: "rgba(0, 100, 200, 0.1)" |
| 32 | + } |
| 33 | + }) |
| 34 | +}); |
| 35 | + |
| 36 | +// Add groups to graph |
| 37 | +graph.addLayer(AutoGroups, { |
| 38 | + draggable: true, // groups can be moved |
| 39 | + updateBlocksOnDrag: true // blocks move with group. Works only if group is draggable and used Automatic Groups |
| 40 | +}); |
| 41 | +``` |
| 42 | + |
| 43 | +### 2. Manual Groups |
| 44 | + |
| 45 | +Use direct `BlockGroups` methods when you need manual control over groups. This approach is useful when: |
| 46 | +- Groups are created/updated based on user actions |
| 47 | +- You need custom group management logic |
| 48 | +- Groups are independent of block properties |
| 49 | + |
| 50 | +```typescript |
| 51 | +// Add groups to graph |
| 52 | +const groups = graph.addLayer(BlockGroups, { |
| 53 | + draggable: true, |
| 54 | +}); |
| 55 | + |
| 56 | +// Set groups |
| 57 | +blockGroups.setGroups([ |
| 58 | + { |
| 59 | + id: "group1", |
| 60 | + rect: { x: 0, y: 0, width: 100, height: 100 }, |
| 61 | + style: { |
| 62 | + background: "rgba(0, 100, 200, 0.1)" |
| 63 | + } |
| 64 | + } |
| 65 | +]); |
| 66 | + |
| 67 | +// Update specific groups |
| 68 | +blockGroups.updateGroups([ |
| 69 | + { |
| 70 | + id: "group1", |
| 71 | + rect: { x: 50, y: 50, width: 100, height: 100 } |
| 72 | + } |
| 73 | +]); |
| 74 | +``` |
| 75 | + |
| 76 | +## Basic Usage |
| 77 | + |
| 78 | +### Creating a Group |
| 79 | + |
| 80 | +```typescript |
| 81 | +// Add fixed area groups to graph |
| 82 | +const areas = graph.addLayer(BlockGroups, { |
| 83 | + draggable: false, // areas cannot be moved |
| 84 | +}); |
| 85 | + |
| 86 | +// Create fixed areas |
| 87 | +areas.setGroups([ |
| 88 | + { |
| 89 | + id: "area1", |
| 90 | + rect: { x: 0, y: 0, width: 800, height: 400 }, |
| 91 | + style: { |
| 92 | + background: "rgba(100, 149, 237, 0.1)", |
| 93 | + border: "rgba(100, 149, 237, 0.3)" |
| 94 | + } |
| 95 | + }, |
| 96 | + { |
| 97 | + id: "area2", |
| 98 | + rect: { x: 800, y: 0, width: 800, height: 400 }, |
| 99 | + style: { |
| 100 | + background: "rgba(144, 238, 144, 0.1)", |
| 101 | + border: "rgba(144, 238, 144, 0.3)" |
| 102 | + } |
| 103 | + } |
| 104 | +]); |
| 105 | +``` |
| 106 | + |
| 107 | +Important: Dragging is not yet implemented properly for fixed area groups. Always set `draggable: false` for such groups. They should remain fixed while blocks can move freely between them. |
| 108 | + |
| 109 | +## Group Settings |
| 110 | + |
| 111 | +### Style |
| 112 | +You can change how groups look: |
| 113 | +```typescript |
| 114 | +{ |
| 115 | + background: "rgba(100, 100, 100, 0.1)", // group color |
| 116 | + border: "rgba(100, 100, 100, 0.3)", // border color |
| 117 | + borderWidth: 2, // border thickness |
| 118 | + selectedBackground: "rgba(100, 100, 100, 1)", // color when selected |
| 119 | + selectedBorder: "rgba(100, 100, 100, 1)", // border when selected |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +### Size and Space |
| 124 | +You can set space around blocks in group: |
| 125 | +```typescript |
| 126 | +{ |
| 127 | + padding: [20, 20, 20, 20] // space at [top, right, bottom, left] |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +### Behavior |
| 132 | +You can control how groups work: |
| 133 | +```typescript |
| 134 | +{ |
| 135 | + draggable: true, // can move group with mouse |
| 136 | + updateBlocksOnDrag: true // blocks move with group. Works only if group is draggable and used Automatic Groups |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## API Reference |
| 141 | + |
| 142 | +### BlockGroups Methods |
| 143 | +```typescript |
| 144 | +// Create or replace all groups |
| 145 | +setGroups(groups: TGroup[]): void; |
| 146 | + |
| 147 | +// Update some groups |
| 148 | +updateGroups(groups: TGroup[]): void; |
| 149 | +``` |
| 150 | + |
| 151 | +### Group Properties |
| 152 | +```typescript |
| 153 | +// What you can set in a group |
| 154 | +{ |
| 155 | + id: string; // unique group ID |
| 156 | + rect: { // where and how big |
| 157 | + x: number; |
| 158 | + y: number; |
| 159 | + width: number; |
| 160 | + height: number; |
| 161 | + }; |
| 162 | + selected?: boolean; // is it selected |
| 163 | + style?: {...}; // how it looks |
| 164 | + geometry?: {...}; // padding settings |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +## Advanced Usage |
| 169 | + |
| 170 | +### Custom Group with Extended Properties |
| 171 | + |
| 172 | +You can create your own group type with additional properties. The key is to pass your custom group component to `BlockGroups`. Here's a complete example: |
| 173 | + |
| 174 | +```typescript |
| 175 | +// 1. Define extended group type |
| 176 | +interface ExtendedTGroup extends TGroup { |
| 177 | + description: string; |
| 178 | + priority: number; |
| 179 | +} |
| 180 | + |
| 181 | +// 2. Create custom group class |
| 182 | +class CustomGroup extends Group<ExtendedTGroup> { |
| 183 | + protected override render() { |
| 184 | + super.render(); |
| 185 | + const ctx = this.context.ctx; |
| 186 | + const rect = this.getRect(); |
| 187 | + |
| 188 | + // Draw description |
| 189 | + if (this.state.description) { |
| 190 | + ctx.font = "12px Arial"; |
| 191 | + ctx.fillStyle = this.style.textColor; |
| 192 | + ctx.fillText(this.state.description, rect.x + 10, rect.y + 25); |
| 193 | + } |
| 194 | + |
| 195 | + // Draw priority in top right corner |
| 196 | + if (this.state.priority) { |
| 197 | + ctx.font = "bold 14px Arial"; |
| 198 | + const text = `P${this.state.priority}`; |
| 199 | + const textWidth = ctx.measureText(text).width; |
| 200 | + ctx.fillText(text, rect.x + rect.width - textWidth - 10, rect.y + 25); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +// 3. Create groups with extended properties |
| 206 | +const blockGroups = graph.addLayer(BlockGroups, { |
| 207 | + draggable: false, |
| 208 | + groupComponent: CustomGroup, // Use our custom component |
| 209 | +}); |
| 210 | + |
| 211 | +// 4. Set groups with extended properties |
| 212 | +blockGroups.setGroups([ |
| 213 | + { |
| 214 | + id: "group1", |
| 215 | + description: "Contains critical blocks", |
| 216 | + priority: 1, |
| 217 | + rect: { x: 0, y: 0, width: 800, height: 400 } |
| 218 | + }, |
| 219 | + { |
| 220 | + id: "group2", |
| 221 | + description: "Contains regular blocks", |
| 222 | + priority: 2, |
| 223 | + rect: { x: 850, y: 0, width: 800, height: 400 } |
| 224 | + } |
| 225 | +]); |
| 226 | +``` |
| 227 | + |
| 228 | +## Examples |
| 229 | + |
| 230 | +You can find complete working examples in our Storybook stories: |
| 231 | + |
| 232 | +1. **Basic Groups** ([`default.stories.tsx`](https://github.com/aschetinin/graph/blob/main/src/stories/canvas/groups/default.stories.tsx)) |
| 233 | + - Shows basic usage of automatic groups |
| 234 | + - Groups are created based on block properties |
| 235 | + - Demonstrates group styling and behavior |
| 236 | + |
| 237 | +2. **Large Graph** ([`large.stories.tsx`](https://github.com/aschetinin/graph/blob/main/src/stories/canvas/groups/large.stories.tsx)) |
| 238 | + - Shows how to work with many blocks (225+) |
| 239 | + - Automatically groups every 10 blocks |
| 240 | + - Demonstrates performance with multiple groups |
| 241 | + |
| 242 | +3. **Manual Groups** ([`manual.stories.tsx`](https://github.com/aschetinin/graph/blob/main/src/stories/canvas/groups/manual.stories.tsx)) |
| 243 | + - Shows how to create fixed area groups |
| 244 | + - Creates non-draggable zones with different colors |
| 245 | + - Demonstrates manual group management |
| 246 | + |
| 247 | +4. **Extended Groups** ([`extended.stories.tsx`](https://github.com/aschetinin/graph/blob/main/src/stories/canvas/groups/extended.stories.tsx)) |
| 248 | + - Shows how to create custom groups with extended properties |
| 249 | + - Adds description and priority to groups |
| 250 | + - Demonstrates custom rendering and styling |
| 251 | + |
| 252 | +Each example includes different styling approaches and group configurations that you can use as a reference for your own implementation. |
0 commit comments