From e22f7b7ae635afd6c53499206634dd9314f77863 Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 9 Jan 2025 11:32:08 +0800 Subject: [PATCH] fix: unexpected element clear (#6710) * fix: unexpected element clear * chore: update version --- packages/g6-extension-3d/package.json | 2 +- packages/g6-extension-react/package.json | 2 +- packages/g6-ssr/package.json | 2 +- .../bugs/graph-draw-after-clear.spec.ts | 2 + .../bugs/graph-draw-after-clear/blank.svg | 7 ++ .../bugs/graph-draw-after-clear/default.svg | 73 ------------------- packages/g6/package.json | 2 +- packages/g6/src/runtime/element.ts | 9 ++- packages/g6/src/version.ts | 2 +- 9 files changed, 21 insertions(+), 80 deletions(-) create mode 100644 packages/g6/__tests__/snapshots/bugs/graph-draw-after-clear/blank.svg diff --git a/packages/g6-extension-3d/package.json b/packages/g6-extension-3d/package.json index d5b14db5c47..aacd3953466 100644 --- a/packages/g6-extension-3d/package.json +++ b/packages/g6-extension-3d/package.json @@ -1,6 +1,6 @@ { "name": "@antv/g6-extension-3d", - "version": "0.1.15", + "version": "0.1.16", "description": "3D extension for G6", "keywords": [ "antv", diff --git a/packages/g6-extension-react/package.json b/packages/g6-extension-react/package.json index 9fffd44d8dd..0ef813530c8 100644 --- a/packages/g6-extension-react/package.json +++ b/packages/g6-extension-react/package.json @@ -1,6 +1,6 @@ { "name": "@antv/g6-extension-react", - "version": "0.1.16", + "version": "0.1.17", "description": "Using React Component to Define Your G6 Graph Node", "keywords": [ "antv", diff --git a/packages/g6-ssr/package.json b/packages/g6-ssr/package.json index 85f36444e20..6ce9e40818f 100644 --- a/packages/g6-ssr/package.json +++ b/packages/g6-ssr/package.json @@ -1,6 +1,6 @@ { "name": "@antv/g6-ssr", - "version": "0.0.10", + "version": "0.0.11", "description": "Support SSR for G6", "keywords": [ "antv", diff --git a/packages/g6/__tests__/bugs/graph-draw-after-clear.spec.ts b/packages/g6/__tests__/bugs/graph-draw-after-clear.spec.ts index a4de275115f..5776a39b846 100644 --- a/packages/g6/__tests__/bugs/graph-draw-after-clear.spec.ts +++ b/packages/g6/__tests__/bugs/graph-draw-after-clear.spec.ts @@ -8,6 +8,8 @@ it('graph draw after clear', async () => { await graph.clear(); + await expect(graph).toMatchSnapshot(__filename, 'blank'); + await sleep(200); graph.addData(data); diff --git a/packages/g6/__tests__/snapshots/bugs/graph-draw-after-clear/blank.svg b/packages/g6/__tests__/snapshots/bugs/graph-draw-after-clear/blank.svg new file mode 100644 index 00000000000..b72eefeb677 --- /dev/null +++ b/packages/g6/__tests__/snapshots/bugs/graph-draw-after-clear/blank.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/packages/g6/__tests__/snapshots/bugs/graph-draw-after-clear/default.svg b/packages/g6/__tests__/snapshots/bugs/graph-draw-after-clear/default.svg index b30da88a6de..fc84aded266 100644 --- a/packages/g6/__tests__/snapshots/bugs/graph-draw-after-clear/default.svg +++ b/packages/g6/__tests__/snapshots/bugs/graph-draw-after-clear/default.svg @@ -2,64 +2,6 @@ - - - - - - - - - - - - - - - - - - line-active - - - - - - - - - - - - - - - - - - - - - - - - - - - line-highlight - - - - - - - - - - - - - @@ -203,21 +145,6 @@ - - - - - - - - - - - - - - - diff --git a/packages/g6/package.json b/packages/g6/package.json index bf3ee68c7ec..3d233c4016e 100644 --- a/packages/g6/package.json +++ b/packages/g6/package.json @@ -1,6 +1,6 @@ { "name": "@antv/g6", - "version": "5.0.41", + "version": "5.0.42", "description": "A Graph Visualization Framework in JavaScript", "keywords": [ "antv", diff --git a/packages/g6/src/runtime/element.ts b/packages/g6/src/runtime/element.ts index 9ac233a19ec..ccacd3697b9 100644 --- a/packages/g6/src/runtime/element.ts +++ b/packages/g6/src/runtime/element.ts @@ -53,7 +53,11 @@ export class ElementController { } public init() { - if (!this.container) { + this.initContainer(); + } + + private initContainer() { + if (!this.container || this.container.destroyed) { const { canvas } = this.context; this.container = canvas.appendChild(new Group({ className: 'elements' })); } @@ -854,7 +858,8 @@ export class ElementController { * clear all elements */ public clear() { - this.container.children.forEach((element) => element.destroy()); + this.container.destroy(); + this.initContainer(); this.elementMap = {}; this.shapeTypeMap = {}; this.defaultStyle = {}; diff --git a/packages/g6/src/version.ts b/packages/g6/src/version.ts index c7c25ea0f29..a58037ad90a 100644 --- a/packages/g6/src/version.ts +++ b/packages/g6/src/version.ts @@ -1 +1 @@ -export const version = '5.0.41'; +export const version = '5.0.42';