Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(elements): adjust the computation logic of combo #6275

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions packages/g6/__tests__/unit/elements/combo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,36 @@ describe('combo drag zIndex', () => {
await expect(graph).toMatchSnapshot(__filename, 'combo-zIndex');
});
});

describe('combo with position', () => {
it('combo with position', async () => {
const graph = createGraph({
data: {
nodes: [
{ id: 'node-1', combo: 'combo-1', style: { x: 50, y: 100 } },
{ id: 'node-2', combo: 'combo-2' },
{ id: 'node-3', combo: 'combo-3' },
],
combos: [
{ id: 'combo-1', style: { x: 0, y: 0, collapsed: true } },
{ id: 'combo-2', style: { x: 100, y: 100, collapsed: true } },
{ id: 'combo-3', style: { collapsed: true } },
],
},
node: {
style: {
labelText: (d) => d.id,
},
},
combo: {
style: {
labelText: (d) => d.id,
},
},
});

await graph.draw();

await expect(graph).toMatchSnapshot(__filename, 'combo-with-position');
});
});
16 changes: 15 additions & 1 deletion packages/g6/__tests__/unit/utils/position.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getXYByAnchor, getXYByPlacement, getXYByRelativePlacement, positionOf } from '@/src/utils/position';
import {
getXYByAnchor,
getXYByPlacement,
getXYByRelativePlacement,
hasPosition,
positionOf,
} from '@/src/utils/position';
import { AABB } from '@antv/g';

describe('position', () => {
Expand Down Expand Up @@ -36,4 +42,12 @@ describe('position', () => {
expect(positionOf({ id: 'node', style: { x: 10, y: 20 } })).toEqual([10, 20, 0]);
expect(positionOf({ id: 'node', style: { x: 10, y: 20, z: 30 } })).toEqual([10, 20, 30]);
});

it('hasPosition', () => {
expect(hasPosition({ id: 'node' })).toBeFalsy();
expect(hasPosition({ id: 'node', style: { x: 10 } })).toBeTruthy();
expect(hasPosition({ id: 'node', style: { y: 20 } })).toBeTruthy();
expect(hasPosition({ id: 'node', style: { z: 30 } })).toBeTruthy();
expect(hasPosition({ id: 'node', style: { x: 10, y: 20, z: 30 } })).toBeTruthy();
});
});
4 changes: 2 additions & 2 deletions packages/g6/src/elements/combos/base-combo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
import { getBBoxHeight, getBBoxWidth, getCombinedBBox, getExpandedBBox } from '../../utils/bbox';
import { idOf } from '../../utils/id';
import { parsePadding } from '../../utils/padding';
import { getXYByPlacement, positionOf } from '../../utils/position';
import { getXYByPlacement, hasPosition, positionOf } from '../../utils/position';
import { subStyleProps } from '../../utils/prefix';
import { parseSize } from '../../utils/size';
import { mergeOptions } from '../../utils/style';
Expand Down Expand Up @@ -210,7 +210,7 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
const { model } = context!;
const descendants = model.getDescendantsData(this.id).filter((datum) => !model.isCombo(idOf(datum)));

if (descendants.length > 0) {
if (descendants.length > 0 && descendants.some(hasPosition)) {
// combo 被收起,返回平均中心位置 / combo is collapsed, return the average center position
const totalPosition = descendants.reduce((acc, datum) => add(acc, positionOf(datum)), [0, 0, 0] as Point);
return divide(totalPosition, descendants.length);
Expand Down
12 changes: 12 additions & 0 deletions packages/g6/src/utils/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ export function positionOf(datum: NodeLikeData): Point {
return [+x, +y, +z];
}

/**
* <zh/> 检查数据是否有位置坐标
*
* <en/> Check if the data has a position coordinate
* @param datum - <zh/> 节点/ combo 的数据 | <en/> data of node/combo
* @returns - <zh/> 是否有位置坐标 | <en/> Whether there is a position coordinate
*/
export function hasPosition(datum: NodeLikeData): boolean {
const { x, y, z } = datum.style || {};
return x !== undefined || y !== undefined || z !== undefined;
}

/**
* <zh/> 获取相对位置坐标
*
Expand Down
Loading