Skip to content

Commit

Permalink
fix: fix removing parent combo of node not effect (#6566)
Browse files Browse the repository at this point in the history
* chore: update graphlib version

* fix: support remove parent

* docs: update docs
  • Loading branch information
Aarebecca authored Nov 27, 2024
1 parent 05044e1 commit e8f7455
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 7 deletions.
34 changes: 34 additions & 0 deletions packages/g6/__tests__/bugs/model-remove-parent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createGraph } from '@@/utils';

it('model remove parent', async () => {
const data = {
nodes: [
{ id: 'node1', combo: 'combo1', style: { x: 250, y: 150 } },
{ id: 'node2', combo: 'combo1', style: { x: 350, y: 150 } },
{ id: 'node3', combo: 'combo1', style: { x: 250, y: 300 } },
],
edges: [],
combos: [{ id: 'combo1' }],
};

const graph = createGraph({
data,
node: {
style: {
labelText: (d) => d.id,
},
},
combo: {
type: 'rect',
},
});

await graph.render();

await expect(graph).toMatchSnapshot(__filename);

graph.updateNodeData([{ id: 'node3', combo: null }]);
await graph.draw();

await expect(graph).toMatchSnapshot(__dirname, 'remove-parent');
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions packages/g6/__tests__/snapshots/bugs/remove-parent.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/g6/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@antv/g": "^6.1.11",
"@antv/g-canvas": "^2.0.29",
"@antv/g-plugin-dragndrop": "^2.0.22",
"@antv/graphlib": "^2.0.3",
"@antv/graphlib": "^2.0.4",
"@antv/hierarchy": "^0.6.14",
"@antv/layout": "1.2.14-beta.9",
"@antv/util": "^3.3.10",
Expand Down
15 changes: 11 additions & 4 deletions packages/g6/src/runtime/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Graph as GraphLib } from '@antv/graphlib';
import { isNumber, isUndefined, uniq } from '@antv/util';
import { isNil, isNumber, uniq } from '@antv/util';
import { COMBO_KEY, ChangeType, TREE_KEY } from '../constants';
import type { ComboData, EdgeData, GraphData, NodeData } from '../spec';
import type {
Expand Down Expand Up @@ -518,8 +518,15 @@ export class DataController {
const id = idOf(datum);
const parent = parentIdOf(datum);

if (parent) {
if (parent !== undefined) {
if (!model.hasTreeStructure(COMBO_KEY)) model.attachTreeStructure(COMBO_KEY);

// 解除原父节点的子节点关系,更新原父节点及其祖先的数据
// Remove the child relationship of the original parent node, update the data of the original parent node and its ancestors
if (parent === null) {
this.refreshComboData(id);
}

this.setParent(id, parentIdOf(datum), COMBO_KEY);
}

Expand Down Expand Up @@ -665,7 +672,7 @@ export class DataController {
* @param hierarchyKey - <zh/> 层次结构类型 | <en/> hierarchy type
* @param update - <zh/> 添加新/旧父节点数据更新记录 | <en/> add new/old parent node data update record
*/
public setParent(id: ID, parent: ID | undefined, hierarchyKey: HierarchyKey, update: boolean = true) {
public setParent(id: ID, parent: ID | undefined | null, hierarchyKey: HierarchyKey, update: boolean = true) {
if (id === parent) return;
const elementData = this.getNodeLikeDatum(id);
const originalParentId = parentIdOf(elementData);
Expand Down Expand Up @@ -891,7 +898,7 @@ export class DataController {
this.model.mergeNodeData(idOf(childData), value);
});

if (!isUndefined(grandParent)) this.refreshComboData(grandParent);
if (!isNil(grandParent)) this.refreshComboData(grandParent);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/g6/src/spec/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface NodeData {
*
* <en/> ID of the combo to which the node belongs
*/
combo?: ID;
combo?: ID | null;
/**
* <zh/> 子节点 ID
*
Expand Down Expand Up @@ -161,7 +161,7 @@ export interface ComboData {
*
* <en/> ID of the combo to which the combo belongs
*/
combo?: ID;
combo?: ID | null;
[key: string]: unknown;
}

Expand Down
8 changes: 8 additions & 0 deletions packages/site/docs/manual/faq.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,11 @@ graph.on(NodeEvent.CLICK, (event: IPointerEvent) => {
// handler
});
```

### Remove the parent combo of the node

Update the node data, set the `combo` value to `null`.

```typescript
graph.updateNodeData([{ id: 'node-id', combo: null }]);
```
8 changes: 8 additions & 0 deletions packages/site/docs/manual/faq.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,11 @@ graph.on(NodeEvent.CLICK, (event: IPointerEvent) => {
// handler
});
```

### 解除节点所在组合

更新节点数据,`combo` 值设置为 `null`

```typescript
graph.updateNodeData([{ id: 'node-id', combo: null }]);
```

0 comments on commit e8f7455

Please sign in to comment.