Skip to content

Commit

Permalink
fix(elements): fix element cannot translate to origin point (#6319)
Browse files Browse the repository at this point in the history
Co-authored-by: antv <antv@antfin.com>
  • Loading branch information
Aarebecca and antv authored Sep 12, 2024
1 parent f2697cd commit 387f421
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
30 changes: 30 additions & 0 deletions packages/g6/__tests__/bugs/element-set-position-to-origin.spec.ts
Original file line number Diff line number Diff line change
@@ -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)');
});
});
4 changes: 2 additions & 2 deletions packages/g6/src/elements/shapes/base-shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
*/
protected transformPosition(attributes: Partial<StyleProps>) {
// 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);
}
}
Expand Down

0 comments on commit 387f421

Please sign in to comment.