fix: fix error when manipulate data (#5899)

* fix: fix error when delete node data in tree graph

* test: add case to test combo change

* fix: fix error when collapse combo with only one child
This commit is contained in:
Aaron 2024-06-20 15:48:03 +08:00 committed by GitHub
parent c42c27475e
commit 79bb790299
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 62 additions and 6 deletions

View File

@ -0,0 +1,30 @@
import { createGraph } from '@@/utils';
describe('render change combo', () => {
it('bug', async () => {
const graph = createGraph({});
await graph.render();
let count = 1;
const operation = async () => {
graph.setData({
nodes: [
{ id: 'node1', combo: `${count}A`, style: { x: 250, y: 150 } },
{ id: 'node2', combo: `${count}b`, style: { x: 350, y: 150 } },
{ id: 'node3', style: { x: 250, y: 300 } },
],
edges: [],
combos: [
{ id: `${count}A`, style: { labelText: `${count}A` } },
{ id: `${count}b`, style: { labelText: `${count}B` } },
],
});
await graph.render();
count++;
};
await operation();
await operation();
});
});

View File

@ -0,0 +1,15 @@
import { layoutCompactBoxBasic } from '@/__tests__/demos';
import { createDemoGraph } from '@@/utils';
describe('render deleted data', () => {
it('bug', async () => {
const graph = await createDemoGraph(layoutCompactBoxBasic);
const render = jest.fn(async () => {
graph.removeNodeData(['Classification']);
await graph.render();
});
expect(render).not.toThrow();
});
});

View File

@ -137,9 +137,16 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
protected getContentBBox(attributes: Required<S>): AABB {
const { context, childrenNode = [], padding } = attributes;
const childrenBBox = getCombinedBBox(
childrenNode.map((id) => context!.element!.getElement(id)).map((child) => child!.getBounds()),
);
const children = childrenNode.map((id) => context!.element!.getElement(id)).filter(Boolean);
if (children.length === 0) {
const bbox = new AABB();
const { x = 0, y = 0, size } = attributes;
const [width, height] = parseSize(size);
bbox.setMinMax([x - width / 2, y - height / 2, 0], [x + width / 2, y + height / 2, 0]);
return bbox;
}
const childrenBBox = getCombinedBBox(children.map((child) => child!.getBounds()));
if (!padding) return childrenBBox;

View File

@ -378,9 +378,13 @@ export class DataController {
const children = (datum as NodeData).children;
if (children !== undefined) {
model.attachTreeStructure(TREE_KEY);
children.forEach((child) => {
this.setParent(child, id, TREE_KEY);
});
const _children = children.filter((child) => model.hasNode(child));
_children.forEach((child) => this.setParent(child, id, TREE_KEY));
if (_children.length !== children.length) {
// 从数据中移除不存在的子节点
// Remove non-existent child nodes from the data
this.updateNodeData([{ id, children: _children }]);
}
}
});
}