Skip to content

Commit

Permalink
fix: fix new node invisible after invoke graph.clear (#6702)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca authored Jan 8, 2025
1 parent 9b21901 commit 6eb5c9e
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 10 deletions.
17 changes: 17 additions & 0 deletions packages/g6/__tests__/bugs/graph-draw-after-clear.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { elementEdgeLine } from '@@/demos';
import { createDemoGraph, sleep } from '@@/utils';

it('graph draw after clear', async () => {
const graph = await createDemoGraph(elementEdgeLine);

const data = graph.getData();

await graph.clear();

await sleep(200);

graph.addData(data);
await graph.draw();

await expect(graph).toMatchSnapshot(__filename);
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 0 additions & 6 deletions packages/g6/src/runtime/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ export class DataController {
}
}

/**
* <zh/> [警告] 此 API 仅供 Element Controller 调用
*
* <en/> [WARNING] This API is only for Element Controller
* @returns <zh/> 数据变更 | <en/> data changes
*/
public getChanges(): DataChange[] {
return this.changes;
}
Expand Down
14 changes: 12 additions & 2 deletions packages/g6/src/runtime/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,13 +848,23 @@ export class ElementController {
)?.finished;
}

public destroy() {
this.container.destroy();
/**
* <zh/> 清空所有元素
*
* <en/> clear all elements
*/
public clear() {
this.container.children.forEach((element) => element.destroy());
this.elementMap = {};
this.shapeTypeMap = {};
this.defaultStyle = {};
this.stateStyle = {};
this.paletteStyle = {};
}

public destroy() {
this.clear();
this.container.destroy();
// @ts-expect-error force delete
this.context = {};
}
Expand Down
6 changes: 4 additions & 2 deletions packages/g6/src/runtime/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1200,8 +1200,10 @@ export class Graph extends EventEmitter {
* @apiCategory canvas
*/
public async clear(): Promise<void> {
this.context.model.setData({});
await this.draw();
const { model, element } = this.context;
model.setData({});
model.clearChanges();
element?.clear();
}

/**
Expand Down

0 comments on commit 6eb5c9e

Please sign in to comment.