From 730fe3255ad2c46c41e89dfbe7dc3e1d5ac87e11 Mon Sep 17 00:00:00 2001 From: antv Date: Wed, 11 Sep 2024 16:33:04 +0800 Subject: [PATCH] fix(elements): fix element cannot translate to origin point --- .../element-set-position-to-origin.spec.ts | 30 +++++++++++++++++++ packages/g6/src/elements/shapes/base-shape.ts | 4 +-- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 packages/g6/__tests__/bugs/element-set-position-to-origin.spec.ts diff --git a/packages/g6/__tests__/bugs/element-set-position-to-origin.spec.ts b/packages/g6/__tests__/bugs/element-set-position-to-origin.spec.ts new file mode 100644 index 0000000000..d1e8d26a48 --- /dev/null +++ b/packages/g6/__tests__/bugs/element-set-position-to-origin.spec.ts @@ -0,0 +1,30 @@ +import type { ID } from '@/src'; +import { createGraph } from '@@/utils'; + +describe('element set position to origin', () => { + it('suite 1', async () => { + const graph = createGraph({ + data: { + nodes: [{ id: 'node-1' }], + }, + }); + + await graph.draw(); + + // @ts-expect-error Property 'context' is protected + const getElementOf = (id: ID) => graph.context.element!.getElement(id)!; + + expect(graph.getNodeData('node-1').style).toEqual({}); + expect(getElementOf('node-1').style.transform).toBe('translate(0, 0)'); + + graph.translateElementTo('node-1', [100, 100]); + + expect(graph.getNodeData('node-1').style).toEqual({ x: 100, y: 100, z: 0 }); + expect(getElementOf('node-1').style.transform).toBe('translate(100, 100)'); + + graph.translateElementTo('node-1', [0, 0]); + + expect(graph.getNodeData('node-1').style).toEqual({ x: 0, y: 0, z: 0 }); + expect(getElementOf('node-1').style.transform).toBe('translate(0, 0)'); + }); +}); diff --git a/packages/g6/src/elements/shapes/base-shape.ts b/packages/g6/src/elements/shapes/base-shape.ts index 10ec044112..de7b3a7cab 100644 --- a/packages/g6/src/elements/shapes/base-shape.ts +++ b/packages/g6/src/elements/shapes/base-shape.ts @@ -124,8 +124,8 @@ export abstract class BaseShape extends */ protected transformPosition(attributes: Partial) { // Use `transform: translate3d()` instead of `x/y/z` - const { x = 0, y = 0, z = 0, transform } = attributes as any; - if (x !== 0 || y !== 0 || z !== 0) { + if ('x' in attributes || 'y' in attributes || 'z' in attributes) { + const { x = 0, y = 0, z = 0, transform } = attributes as any; this.style.transform = replaceTranslateInTransform(+x, +y, +z, transform); } }