Skip to content

Commit

Permalink
refactor: compute element zindex when add and update data stage
Browse files Browse the repository at this point in the history
  • Loading branch information
antv committed Nov 12, 2024
1 parent 0ec431f commit 20c25aa
Show file tree
Hide file tree
Showing 52 changed files with 810 additions and 574 deletions.
21 changes: 15 additions & 6 deletions packages/g6/__tests__/bugs/api-expand-element-z-index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,30 @@ describe('api expand element z-index', () => {

await graph.draw();

// @ts-expect-error context is private
const context = graph.context;
const getZIndexOf = (id: string): number => {
// @ts-expect-error context is private
const context = graph.context;
return context.element!.getElement(id)!.style.zIndex;
};

expect(getZIndexOf('combo-1')).toBe(0);
// expect(getZIndexOf('combo-2')).toBe(1); // not exists because it is collapsed
expect(getZIndexOf('node-1')).toBe(2);
// expect(getZIndexOf('node-2')).toBe(1 + 2);

graph.frontElement('node-1');

expect(context.element!.getElement('node-1')!.style.zIndex).toBe(1);
expect(getZIndexOf('node-1')).toBe(2);

graph.frontElement('combo-1');

expect(context.element!.getElement('combo-1')!.style.zIndex).toBe(2);
expect(getZIndexOf('combo-1')).toBe(3);

await graph.expandElement('combo-1', false);

await graph.expandElement('combo-2', false);

expect(context.element!.getElement('node-2')!.style.zIndex).toBe(2);
expect(getZIndexOf('combo-1')).toBe(3);
expect(getZIndexOf('combo-2')).toBe(4);
expect(getZIndexOf('node-2')).toBe(4 + 2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ describe('element set position to origin', () => {
// @ts-expect-error Property 'context' is protected
const getElementOf = (id: ID) => graph.context.element!.getElement(id)!;

expect(graph.getNodeData('node-1').style).toEqual({});
expect(graph.getNodeData('node-1').style).toEqual({ zIndex: 2 });
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(graph.getNodeData('node-1').style).toEqual({ x: 100, y: 100, z: 0, zIndex: 2 });
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(graph.getNodeData('node-1').style).toEqual({ x: 0, y: 0, z: 0, zIndex: 2 });
expect(getElementOf('node-1').style.transform).toBe('translate(0, 0)');
});
});
53 changes: 53 additions & 0 deletions packages/g6/__tests__/bugs/model-add-edge-in-combo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { createGraph } from '@@/utils';

describe('add edge in combo', () => {
it('add edge in combo without zIndex', async () => {
const graph = createGraph({
data: {
nodes: [
{ id: 'node-1', combo: 'combo-1', style: { x: 100, y: 100 } },
{ id: 'node-2', combo: 'combo-1', style: { x: 200, y: 200 } },
],
combos: [{ id: 'combo-1' }],
},
});

await graph.draw();

graph.addEdgeData([{ id: 'edge', source: 'node-1', target: 'node-2' }]);
await graph.draw();

expect(graph.getEdgeData('edge').style?.zIndex).toBe(1);
// @ts-expect-error skip the type check
expect(graph.context.element?.getElement('edge')?.style.zIndex).toBe(1);
});

it('add edge in combo with zIndex', async () => {
const graph = createGraph({
data: {
nodes: [
{ id: 'node-1', combo: 'combo-1', style: { x: 100, y: 100 } },
{ id: 'node-2', combo: 'combo-1', style: { x: 200, y: 200 } },
{ id: 'node-3', style: { x: 300, y: 300, zIndex: 5 } },
],
combos: [{ id: 'combo-1' }],
},
});

await graph.draw();

expect(graph.getComboData('combo-1').style?.zIndex).toBe(0);

await graph.frontElement('combo-1');

expect(graph.getComboData('combo-1').style?.zIndex).toBe(5 + 1);
expect(graph.getNodeData('node-1').style?.zIndex).toBe(5 + 1 + 2);

graph.addEdgeData([{ id: 'edge', source: 'node-1', target: 'node-2' }]);
await graph.draw();

expect(graph.getEdgeData('edge').style?.zIndex).toBe(5 + 1 + 1);
// @ts-expect-error skip the type check
expect(graph.context.element?.getElement('edge')?.style.zIndex).toBe(5 + 1 + 1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ describe('bug: plugin-history-align-fields', () => {

await graph.render();

expect(graph.getNodeData('node-1').style).toEqual({ x: 50, y: 100 });
expect(graph.getNodeData('node-1').style).toEqual({ x: 50, y: 100, zIndex: 2 });
expect(graph.getNodeData('node-1').data!.aaa).toEqual({
bbb: false,
ccc: true,
ddd: '1234',
});

await graph.translateElementBy('node-1', [100, 100]);
expect(graph.getNodeData('node-1').style).toEqual({ x: 150, y: 200, z: 0 });
expect(graph.getNodeData('node-1').style).toEqual({ x: 150, y: 200, z: 0, zIndex: 2 });
expect(graph.getNodeData('node-1').data!.aaa).toEqual({
bbb: false,
ccc: true,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 20c25aa

Please sign in to comment.