@antv/g6 5.0.19 → 5.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/g6.min.js +2 -2
- package/dist/lib/behaviors/drag-canvas.d.ts +9 -0
- package/dist/lib/behaviors/hover-activate.d.ts +14 -1
- package/dist/lib/behaviors/scroll-canvas.d.ts +9 -0
- package/dist/lib/elements/combos/circle.d.ts +1 -1
- package/dist/lib/elements/edges/cubic-radial.d.ts +24 -0
- package/dist/lib/elements/edges/index.d.ts +2 -0
- package/dist/lib/elements/nodes/base-node.d.ts +2 -1
- package/dist/lib/elements/nodes/circle.d.ts +1 -1
- package/dist/lib/elements/nodes/ellipse.d.ts +1 -1
- package/dist/lib/elements/shapes/polygon.d.ts +1 -1
- package/dist/lib/exports.d.ts +7 -5
- package/dist/lib/runtime/data.d.ts +1 -0
- package/dist/lib/runtime/element.d.ts +1 -0
- package/dist/lib/runtime/graph.d.ts +2 -0
- package/dist/lib/runtime/layout.d.ts +1 -0
- package/dist/lib/transforms/base-transform.d.ts +1 -0
- package/dist/lib/transforms/index.d.ts +3 -0
- package/dist/lib/transforms/position-radial-labels.d.ts +27 -0
- package/dist/lib/types/element.d.ts +2 -1
- package/dist/lib/utils/data.d.ts +14 -0
- package/dist/lib/utils/diff.d.ts +2 -1
- package/dist/lib/utils/point.d.ts +17 -5
- package/dist/lib/utils/position.d.ts +8 -0
- package/dist/lib/utils/relation.d.ts +6 -4
- package/dist/lib/utils/vector.d.ts +8 -0
- package/dist/lib/version.d.ts +1 -1
- package/esm/behaviors/drag-canvas.d.ts +9 -0
- package/esm/behaviors/drag-canvas.js +28 -3
- package/esm/behaviors/drag-canvas.js.map +1 -1
- package/esm/behaviors/hover-activate.d.ts +14 -1
- package/esm/behaviors/hover-activate.js +3 -2
- package/esm/behaviors/hover-activate.js.map +1 -1
- package/esm/behaviors/scroll-canvas.d.ts +9 -0
- package/esm/behaviors/scroll-canvas.js +31 -4
- package/esm/behaviors/scroll-canvas.js.map +1 -1
- package/esm/elements/combos/base-combo.js +2 -2
- package/esm/elements/combos/base-combo.js.map +1 -1
- package/esm/elements/combos/circle.d.ts +1 -1
- package/esm/elements/combos/circle.js +2 -2
- package/esm/elements/combos/circle.js.map +1 -1
- package/esm/elements/edges/cubic-radial.d.ts +24 -0
- package/esm/elements/edges/cubic-radial.js +52 -0
- package/esm/elements/edges/cubic-radial.js.map +1 -0
- package/esm/elements/edges/index.d.ts +2 -0
- package/esm/elements/edges/index.js +1 -0
- package/esm/elements/edges/index.js.map +1 -1
- package/esm/elements/nodes/base-node.d.ts +2 -1
- package/esm/elements/nodes/base-node.js +3 -2
- package/esm/elements/nodes/base-node.js.map +1 -1
- package/esm/elements/nodes/circle.d.ts +1 -1
- package/esm/elements/nodes/circle.js +2 -2
- package/esm/elements/nodes/circle.js.map +1 -1
- package/esm/elements/nodes/ellipse.d.ts +1 -1
- package/esm/elements/nodes/ellipse.js +2 -2
- package/esm/elements/nodes/ellipse.js.map +1 -1
- package/esm/elements/shapes/polygon.d.ts +1 -1
- package/esm/elements/shapes/polygon.js +2 -2
- package/esm/elements/shapes/polygon.js.map +1 -1
- package/esm/exports.d.ts +7 -5
- package/esm/exports.js +5 -3
- package/esm/exports.js.map +1 -1
- package/esm/plugins/tooltip.js +5 -2
- package/esm/plugins/tooltip.js.map +1 -1
- package/esm/registry/build-in.js +4 -2
- package/esm/registry/build-in.js.map +1 -1
- package/esm/runtime/animation.js +12 -10
- package/esm/runtime/animation.js.map +1 -1
- package/esm/runtime/data.d.ts +1 -0
- package/esm/runtime/data.js +21 -13
- package/esm/runtime/data.js.map +1 -1
- package/esm/runtime/element.d.ts +1 -0
- package/esm/runtime/element.js +21 -11
- package/esm/runtime/element.js.map +1 -1
- package/esm/runtime/graph.d.ts +2 -0
- package/esm/runtime/graph.js +42 -22
- package/esm/runtime/graph.js.map +1 -1
- package/esm/runtime/layout.d.ts +1 -0
- package/esm/runtime/layout.js +5 -0
- package/esm/runtime/layout.js.map +1 -1
- package/esm/transforms/base-transform.d.ts +1 -0
- package/esm/transforms/base-transform.js +1 -0
- package/esm/transforms/base-transform.js.map +1 -1
- package/esm/transforms/collapse-expand-combo.js +3 -0
- package/esm/transforms/collapse-expand-combo.js.map +1 -1
- package/esm/transforms/collapse-expand-node.js +2 -0
- package/esm/transforms/collapse-expand-node.js.map +1 -1
- package/esm/transforms/index.d.ts +3 -0
- package/esm/transforms/index.js +1 -0
- package/esm/transforms/index.js.map +1 -1
- package/esm/transforms/position-radial-labels.d.ts +27 -0
- package/esm/transforms/position-radial-labels.js +50 -0
- package/esm/transforms/position-radial-labels.js.map +1 -0
- package/esm/types/element.d.ts +2 -1
- package/esm/utils/data.d.ts +14 -0
- package/esm/utils/data.js +39 -0
- package/esm/utils/data.js.map +1 -1
- package/esm/utils/diff.d.ts +2 -1
- package/esm/utils/diff.js +3 -2
- package/esm/utils/diff.js.map +1 -1
- package/esm/utils/point.d.ts +17 -5
- package/esm/utils/point.js +24 -8
- package/esm/utils/point.js.map +1 -1
- package/esm/utils/position.d.ts +8 -0
- package/esm/utils/position.js +11 -0
- package/esm/utils/position.js.map +1 -1
- package/esm/utils/relation.d.ts +6 -4
- package/esm/utils/relation.js +10 -8
- package/esm/utils/relation.js.map +1 -1
- package/esm/utils/vector.d.ts +8 -0
- package/esm/utils/vector.js +13 -0
- package/esm/utils/vector.js.map +1 -1
- package/esm/version.d.ts +1 -1
- package/esm/version.js +1 -1
- package/lib/behaviors/drag-canvas.d.ts +9 -0
- package/lib/behaviors/drag-canvas.js +27 -2
- package/lib/behaviors/drag-canvas.js.map +1 -1
- package/lib/behaviors/hover-activate.d.ts +14 -1
- package/lib/behaviors/hover-activate.js +3 -2
- package/lib/behaviors/hover-activate.js.map +1 -1
- package/lib/behaviors/scroll-canvas.d.ts +9 -0
- package/lib/behaviors/scroll-canvas.js +31 -4
- package/lib/behaviors/scroll-canvas.js.map +1 -1
- package/lib/elements/combos/base-combo.js +1 -1
- package/lib/elements/combos/base-combo.js.map +1 -1
- package/lib/elements/combos/circle.d.ts +1 -1
- package/lib/elements/combos/circle.js +2 -2
- package/lib/elements/combos/circle.js.map +1 -1
- package/lib/elements/edges/cubic-radial.d.ts +24 -0
- package/lib/elements/edges/cubic-radial.js +56 -0
- package/lib/elements/edges/cubic-radial.js.map +1 -0
- package/lib/elements/edges/index.d.ts +2 -0
- package/lib/elements/edges/index.js +3 -1
- package/lib/elements/edges/index.js.map +1 -1
- package/lib/elements/nodes/base-node.d.ts +2 -1
- package/lib/elements/nodes/base-node.js +3 -2
- package/lib/elements/nodes/base-node.js.map +1 -1
- package/lib/elements/nodes/circle.d.ts +1 -1
- package/lib/elements/nodes/circle.js +2 -2
- package/lib/elements/nodes/circle.js.map +1 -1
- package/lib/elements/nodes/ellipse.d.ts +1 -1
- package/lib/elements/nodes/ellipse.js +2 -2
- package/lib/elements/nodes/ellipse.js.map +1 -1
- package/lib/elements/shapes/polygon.d.ts +1 -1
- package/lib/elements/shapes/polygon.js +2 -2
- package/lib/elements/shapes/polygon.js.map +1 -1
- package/lib/exports.d.ts +7 -5
- package/lib/exports.js +14 -6
- package/lib/exports.js.map +1 -1
- package/lib/plugins/tooltip.js +5 -2
- package/lib/plugins/tooltip.js.map +1 -1
- package/lib/registry/build-in.js +2 -0
- package/lib/registry/build-in.js.map +1 -1
- package/lib/runtime/animation.js +12 -10
- package/lib/runtime/animation.js.map +1 -1
- package/lib/runtime/data.d.ts +1 -0
- package/lib/runtime/data.js +19 -11
- package/lib/runtime/data.js.map +1 -1
- package/lib/runtime/element.d.ts +1 -0
- package/lib/runtime/element.js +21 -11
- package/lib/runtime/element.js.map +1 -1
- package/lib/runtime/graph.d.ts +2 -0
- package/lib/runtime/graph.js +42 -22
- package/lib/runtime/graph.js.map +1 -1
- package/lib/runtime/layout.d.ts +1 -0
- package/lib/runtime/layout.js +5 -0
- package/lib/runtime/layout.js.map +1 -1
- package/lib/transforms/base-transform.d.ts +1 -0
- package/lib/transforms/base-transform.js +1 -0
- package/lib/transforms/base-transform.js.map +1 -1
- package/lib/transforms/collapse-expand-combo.js +3 -0
- package/lib/transforms/collapse-expand-combo.js.map +1 -1
- package/lib/transforms/collapse-expand-node.js +2 -0
- package/lib/transforms/collapse-expand-node.js.map +1 -1
- package/lib/transforms/index.d.ts +3 -0
- package/lib/transforms/index.js +3 -1
- package/lib/transforms/index.js.map +1 -1
- package/lib/transforms/position-radial-labels.d.ts +27 -0
- package/lib/transforms/position-radial-labels.js +54 -0
- package/lib/transforms/position-radial-labels.js.map +1 -0
- package/lib/types/element.d.ts +2 -1
- package/lib/utils/data.d.ts +14 -0
- package/lib/utils/data.js +40 -0
- package/lib/utils/data.js.map +1 -1
- package/lib/utils/diff.d.ts +2 -1
- package/lib/utils/diff.js +3 -2
- package/lib/utils/diff.js.map +1 -1
- package/lib/utils/point.d.ts +17 -5
- package/lib/utils/point.js +25 -8
- package/lib/utils/point.js.map +1 -1
- package/lib/utils/position.d.ts +8 -0
- package/lib/utils/position.js +12 -0
- package/lib/utils/position.js.map +1 -1
- package/lib/utils/relation.d.ts +6 -4
- package/lib/utils/relation.js +10 -8
- package/lib/utils/relation.js.map +1 -1
- package/lib/utils/vector.d.ts +8 -0
- package/lib/utils/vector.js +14 -0
- package/lib/utils/vector.js.map +1 -1
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +2 -2
- package/src/behaviors/drag-canvas.ts +42 -3
- package/src/behaviors/hover-activate.ts +17 -3
- package/src/behaviors/scroll-canvas.ts +45 -4
- package/src/elements/combos/base-combo.ts +2 -2
- package/src/elements/combos/circle.ts +2 -2
- package/src/elements/edges/cubic-radial.ts +77 -0
- package/src/elements/edges/index.ts +2 -0
- package/src/elements/nodes/base-node.ts +3 -2
- package/src/elements/nodes/circle.ts +2 -2
- package/src/elements/nodes/ellipse.ts +2 -2
- package/src/elements/shapes/polygon.ts +2 -2
- package/src/exports.ts +19 -7
- package/src/plugins/tooltip.ts +4 -2
- package/src/registry/build-in.ts +4 -0
- package/src/runtime/animation.ts +16 -11
- package/src/runtime/data.ts +20 -13
- package/src/runtime/element.ts +23 -11
- package/src/runtime/graph.ts +51 -22
- package/src/runtime/layout.ts +7 -0
- package/src/transforms/base-transform.ts +2 -0
- package/src/transforms/collapse-expand-combo.ts +2 -0
- package/src/transforms/collapse-expand-node.ts +3 -0
- package/src/transforms/index.ts +3 -0
- package/src/transforms/position-radial-labels.ts +76 -0
- package/src/types/element.ts +2 -1
- package/src/utils/data.ts +49 -0
- package/src/utils/diff.ts +8 -2
- package/src/utils/point.ts +25 -7
- package/src/utils/position.ts +12 -0
- package/src/utils/relation.ts +22 -9
- package/src/utils/vector.ts +13 -0
- package/src/version.ts +1 -1
package/src/registry/build-in.ts
CHANGED
|
@@ -32,6 +32,7 @@ import {
|
|
|
32
32
|
CircleCombo,
|
|
33
33
|
Cubic,
|
|
34
34
|
CubicHorizontal,
|
|
35
|
+
CubicRadial,
|
|
35
36
|
CubicVertical,
|
|
36
37
|
Diamond,
|
|
37
38
|
Donut,
|
|
@@ -92,6 +93,7 @@ import {
|
|
|
92
93
|
CollapseExpandCombo,
|
|
93
94
|
CollapseExpandNode,
|
|
94
95
|
GetEdgeActualEnds,
|
|
96
|
+
PositionRadialLabels,
|
|
95
97
|
ProcessParallelEdges,
|
|
96
98
|
UpdateRelatedEdge,
|
|
97
99
|
} from '../transforms';
|
|
@@ -138,6 +140,7 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
|
|
|
138
140
|
polyline: Polyline,
|
|
139
141
|
quadratic: Quadratic,
|
|
140
142
|
'cubic-horizontal': CubicHorizontal,
|
|
143
|
+
'cubic-radial': CubicRadial,
|
|
141
144
|
'cubic-vertical': CubicVertical,
|
|
142
145
|
},
|
|
143
146
|
layout: {
|
|
@@ -207,6 +210,7 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
|
|
|
207
210
|
'collapse-expand-node': CollapseExpandNode,
|
|
208
211
|
'process-parallel-edges': ProcessParallelEdges,
|
|
209
212
|
'get-edge-actual-ends': GetEdgeActualEnds,
|
|
213
|
+
'position-radial-labels': PositionRadialLabels,
|
|
210
214
|
},
|
|
211
215
|
shape: {
|
|
212
216
|
circle: GCircle,
|
package/src/runtime/animation.ts
CHANGED
|
@@ -79,12 +79,14 @@ export class Animation {
|
|
|
79
79
|
* @param options - <zh/> 扩展选项 | <en/> Extend options
|
|
80
80
|
* @returns <zh/> 始态样式与终态样式 | <en/> Initial style and final style
|
|
81
81
|
*/
|
|
82
|
-
public inferStyle(
|
|
82
|
+
public inferStyle(
|
|
83
|
+
context: AnimationContext,
|
|
84
|
+
options?: ExtendOptions,
|
|
85
|
+
): [Record<string, unknown>, Record<string, unknown>] {
|
|
83
86
|
const { element, elementType, stage, originalStyle, modifiedStyle } = context;
|
|
84
87
|
|
|
85
|
-
const fromStyle = {
|
|
86
|
-
const toStyle = {
|
|
87
|
-
const getCacheOpacity = () => getCachedStyle(element, 'opacity') ?? inferDefaultValue('opacity');
|
|
88
|
+
const fromStyle: Record<string, unknown> = {};
|
|
89
|
+
const toStyle: Record<string, unknown> = {};
|
|
88
90
|
|
|
89
91
|
if (stage === 'enter') {
|
|
90
92
|
Object.assign(fromStyle, { opacity: 0 });
|
|
@@ -92,9 +94,9 @@ export class Animation {
|
|
|
92
94
|
Object.assign(toStyle, { opacity: 0 });
|
|
93
95
|
} else if (stage === 'show') {
|
|
94
96
|
Object.assign(fromStyle, { opacity: 0 });
|
|
95
|
-
Object.assign(toStyle, { opacity:
|
|
97
|
+
Object.assign(toStyle, { opacity: getCachedStyle(element, 'opacity') ?? inferDefaultValue('opacity') });
|
|
96
98
|
} else if (stage === 'hide') {
|
|
97
|
-
Object.assign(fromStyle, { opacity:
|
|
99
|
+
Object.assign(fromStyle, { opacity: getCachedStyle(element, 'opacity') ?? inferDefaultValue('opacity') });
|
|
98
100
|
Object.assign(toStyle, { opacity: 0 });
|
|
99
101
|
} else if (stage === 'collapse') {
|
|
100
102
|
const { collapse } = options || {};
|
|
@@ -109,10 +111,10 @@ export class Animation {
|
|
|
109
111
|
} else if (elementType === 'combo') {
|
|
110
112
|
if (element.id === target || descendants.includes(element.id)) {
|
|
111
113
|
const [x, y] = position;
|
|
112
|
-
Object.assign(toStyle, { x, y, childrenNode:
|
|
114
|
+
Object.assign(toStyle, { x, y, childrenNode: originalStyle.childrenNode });
|
|
113
115
|
}
|
|
114
116
|
} else if (elementType === 'edge') {
|
|
115
|
-
Object.assign(toStyle, { sourceNode:
|
|
117
|
+
Object.assign(toStyle, { sourceNode: modifiedStyle.sourceNode, targetNode: modifiedStyle.targetNode });
|
|
116
118
|
}
|
|
117
119
|
} else if (stage === 'expand') {
|
|
118
120
|
const { expand } = options || {};
|
|
@@ -129,16 +131,19 @@ export class Animation {
|
|
|
129
131
|
// Set the child elements of the expanded combo
|
|
130
132
|
if (element.id === target || descendants.includes(element.id)) {
|
|
131
133
|
const [x, y, z] = position;
|
|
132
|
-
Object.assign(fromStyle, { x, y, z, childrenNode:
|
|
134
|
+
Object.assign(fromStyle, { x, y, z, childrenNode: modifiedStyle.childrenNode });
|
|
133
135
|
}
|
|
134
136
|
} else if (elementType === 'edge') {
|
|
135
137
|
// 设置展开后的边的起点和终点
|
|
136
138
|
// Set the starting point and end point of the edge after expansion
|
|
137
|
-
Object.assign(fromStyle, { sourceNode:
|
|
139
|
+
Object.assign(fromStyle, { sourceNode: modifiedStyle.sourceNode, targetNode: modifiedStyle.targetNode });
|
|
138
140
|
}
|
|
139
141
|
}
|
|
140
142
|
|
|
141
|
-
return [
|
|
143
|
+
return [
|
|
144
|
+
Object.keys(fromStyle).length > 0 ? Object.assign({}, originalStyle, fromStyle) : originalStyle,
|
|
145
|
+
Object.keys(toStyle).length > 0 ? Object.assign({}, modifiedStyle, toStyle) : modifiedStyle,
|
|
146
|
+
];
|
|
142
147
|
}
|
|
143
148
|
|
|
144
149
|
public stop() {
|
package/src/runtime/data.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Graph as GraphLib } from '@antv/graphlib';
|
|
2
|
-
import {
|
|
2
|
+
import { isUndefined, uniq } from '@antv/util';
|
|
3
3
|
import { COMBO_KEY, ChangeType, TREE_KEY } from '../constants';
|
|
4
4
|
import type { ComboData, EdgeData, GraphData, NodeData } from '../spec';
|
|
5
5
|
import type {
|
|
@@ -20,7 +20,7 @@ import type {
|
|
|
20
20
|
} from '../types';
|
|
21
21
|
import type { EdgeDirection } from '../types/edge';
|
|
22
22
|
import type { ElementType } from '../types/element';
|
|
23
|
-
import { cloneElementData, mergeElementsData } from '../utils/data';
|
|
23
|
+
import { cloneElementData, isElementDataEqual, mergeElementsData } from '../utils/data';
|
|
24
24
|
import { arrayDiff } from '../utils/diff';
|
|
25
25
|
import { toG6Data, toGraphlibData } from '../utils/graphlib';
|
|
26
26
|
import { idOf, parentIdOf } from '../utils/id';
|
|
@@ -166,6 +166,10 @@ export class DataController {
|
|
|
166
166
|
}, [] as ComboData[]);
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
+
public getRootsData(hierarchyKey: HierarchyKey = TREE_KEY) {
|
|
170
|
+
return this.model.getRoots(hierarchyKey).map(toG6Data);
|
|
171
|
+
}
|
|
172
|
+
|
|
169
173
|
public getAncestorsData(id: ID, hierarchyKey: HierarchyKey): NodeLikeData[] {
|
|
170
174
|
const { model } = this;
|
|
171
175
|
if (!model.hasNode(id) || !model.hasTreeStructure(hierarchyKey)) return [];
|
|
@@ -292,9 +296,9 @@ export class DataController {
|
|
|
292
296
|
const { nodes: modifiedNodes = [], edges: modifiedEdges = [], combos: modifiedCombos = [] } = data;
|
|
293
297
|
const { nodes: originalNodes, edges: originalEdges, combos: originalCombos } = this.getData();
|
|
294
298
|
|
|
295
|
-
const nodeDiff = arrayDiff(originalNodes, modifiedNodes, (node) => idOf(node));
|
|
296
|
-
const edgeDiff = arrayDiff(originalEdges, modifiedEdges, (edge) => idOf(edge));
|
|
297
|
-
const comboDiff = arrayDiff(originalCombos, modifiedCombos, (combo) => idOf(combo));
|
|
299
|
+
const nodeDiff = arrayDiff(originalNodes, modifiedNodes, (node) => idOf(node), isElementDataEqual);
|
|
300
|
+
const edgeDiff = arrayDiff(originalEdges, modifiedEdges, (edge) => idOf(edge), isElementDataEqual);
|
|
301
|
+
const comboDiff = arrayDiff(originalCombos, modifiedCombos, (combo) => idOf(combo), isElementDataEqual);
|
|
298
302
|
|
|
299
303
|
this.batch(() => {
|
|
300
304
|
this.addData({
|
|
@@ -381,13 +385,16 @@ export class DataController {
|
|
|
381
385
|
|
|
382
386
|
data.forEach((datum) => {
|
|
383
387
|
const id = idOf(datum);
|
|
388
|
+
const parent = parentIdOf(datum);
|
|
384
389
|
|
|
385
|
-
|
|
386
|
-
|
|
390
|
+
if (parent) {
|
|
391
|
+
if (!model.hasTreeStructure(COMBO_KEY)) model.attachTreeStructure(COMBO_KEY);
|
|
392
|
+
this.setParent(id, parentIdOf(datum), COMBO_KEY);
|
|
393
|
+
}
|
|
387
394
|
|
|
388
|
-
const children = (datum as NodeData).children;
|
|
389
|
-
if (children
|
|
390
|
-
model.attachTreeStructure(TREE_KEY);
|
|
395
|
+
const children = (datum as NodeData).children || [];
|
|
396
|
+
if (children.length) {
|
|
397
|
+
if (!model.hasTreeStructure(TREE_KEY)) model.attachTreeStructure(TREE_KEY);
|
|
391
398
|
const _children = children.filter((child) => model.hasNode(child));
|
|
392
399
|
_children.forEach((child) => this.setParent(child, id, TREE_KEY));
|
|
393
400
|
if (_children.length !== children.length) {
|
|
@@ -430,7 +437,7 @@ export class DataController {
|
|
|
430
437
|
nodes.forEach((modifiedNode) => {
|
|
431
438
|
const id = idOf(modifiedNode);
|
|
432
439
|
const originalNode = toG6Data(model.getNode(id));
|
|
433
|
-
if (
|
|
440
|
+
if (isElementDataEqual(originalNode, modifiedNode)) return;
|
|
434
441
|
|
|
435
442
|
const value = mergeElementsData(originalNode, modifiedNode);
|
|
436
443
|
this.pushChange({ value, original: originalNode, type: ChangeType.NodeUpdated });
|
|
@@ -476,7 +483,7 @@ export class DataController {
|
|
|
476
483
|
edges.forEach((modifiedEdge) => {
|
|
477
484
|
const id = idOf(modifiedEdge);
|
|
478
485
|
const originalEdge = toG6Data(model.getEdge(id));
|
|
479
|
-
if (
|
|
486
|
+
if (isElementDataEqual(originalEdge, modifiedEdge)) return;
|
|
480
487
|
|
|
481
488
|
if (modifiedEdge.source && originalEdge.source !== modifiedEdge.source) {
|
|
482
489
|
model.updateEdgeSource(id, modifiedEdge.source);
|
|
@@ -499,7 +506,7 @@ export class DataController {
|
|
|
499
506
|
combos.forEach((modifiedCombo) => {
|
|
500
507
|
const id = idOf(modifiedCombo);
|
|
501
508
|
const originalCombo = toG6Data(model.getNode(id)) as ComboData;
|
|
502
|
-
if (
|
|
509
|
+
if (isElementDataEqual(originalCombo, modifiedCombo)) return;
|
|
503
510
|
|
|
504
511
|
const value = mergeElementsData(originalCombo, modifiedCombo);
|
|
505
512
|
this.pushChange({ value, original: originalCombo, type: ChangeType.ComboUpdated });
|
package/src/runtime/element.ts
CHANGED
|
@@ -19,6 +19,7 @@ import type {
|
|
|
19
19
|
ElementType,
|
|
20
20
|
ID,
|
|
21
21
|
Node,
|
|
22
|
+
NodeLikeData,
|
|
22
23
|
State,
|
|
23
24
|
StyleIterationContext,
|
|
24
25
|
} from '../types';
|
|
@@ -287,7 +288,6 @@ export class ElementController {
|
|
|
287
288
|
const { animation, silence } = context;
|
|
288
289
|
|
|
289
290
|
const { type = 'draw' } = context;
|
|
290
|
-
const willRender = type === 'render';
|
|
291
291
|
|
|
292
292
|
return this.context.animation!.animate(
|
|
293
293
|
animation,
|
|
@@ -296,7 +296,7 @@ export class ElementController {
|
|
|
296
296
|
: {
|
|
297
297
|
before: () =>
|
|
298
298
|
this.emit(
|
|
299
|
-
new GraphLifeCycleEvent(GraphEvent.BEFORE_DRAW, { dataChanges, animation, render:
|
|
299
|
+
new GraphLifeCycleEvent(GraphEvent.BEFORE_DRAW, { dataChanges, animation, render: type === 'render' }),
|
|
300
300
|
context,
|
|
301
301
|
),
|
|
302
302
|
beforeAnimate: (animation) =>
|
|
@@ -305,7 +305,7 @@ export class ElementController {
|
|
|
305
305
|
this.emit(new AnimateEvent(GraphEvent.AFTER_ANIMATE, AnimationType.DRAW, animation, drawData), context),
|
|
306
306
|
after: () =>
|
|
307
307
|
this.emit(
|
|
308
|
-
new GraphLifeCycleEvent(GraphEvent.AFTER_DRAW, { dataChanges, animation, render:
|
|
308
|
+
new GraphLifeCycleEvent(GraphEvent.AFTER_DRAW, { dataChanges, animation, render: type === 'render' }),
|
|
309
309
|
context,
|
|
310
310
|
),
|
|
311
311
|
},
|
|
@@ -433,8 +433,24 @@ export class ElementController {
|
|
|
433
433
|
});
|
|
434
434
|
}
|
|
435
435
|
|
|
436
|
+
private getUpdateStageStyle(elementType: ElementType, datum: ElementDatum, context: DrawContext) {
|
|
437
|
+
const { stage = 'update' } = context;
|
|
438
|
+
|
|
439
|
+
// 优化 translate 阶段,直接返回 x, y, z,避免计算样式
|
|
440
|
+
// Optimize the translate stage, return x, y, z directly to avoid calculating style
|
|
441
|
+
if (stage === 'translate') {
|
|
442
|
+
if (elementType === 'node' || elementType === 'combo') {
|
|
443
|
+
const { style: { x = 0, y = 0, z = 0 } = {} } = datum as NodeLikeData;
|
|
444
|
+
return { x, y, z };
|
|
445
|
+
} else return {};
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
return this.getElementComputedStyle(elementType, datum);
|
|
449
|
+
}
|
|
450
|
+
|
|
436
451
|
private updateElement(elementType: ElementType, datum: ElementDatum, context: DrawContext) {
|
|
437
452
|
const id = idOf(datum);
|
|
453
|
+
const { stage = 'update' } = context;
|
|
438
454
|
|
|
439
455
|
const element = this.getElement(id);
|
|
440
456
|
if (!element) return () => null;
|
|
@@ -442,7 +458,7 @@ export class ElementController {
|
|
|
442
458
|
this.emit(new ElementLifeCycleEvent(GraphEvent.BEFORE_ELEMENT_UPDATE, elementType, datum), context);
|
|
443
459
|
|
|
444
460
|
const type = this.getElementType(elementType, datum);
|
|
445
|
-
const style = this.
|
|
461
|
+
const style = this.getUpdateStageStyle(elementType, datum, context);
|
|
446
462
|
|
|
447
463
|
// 如果类型不同,需要先销毁原有元素,再创建新元素
|
|
448
464
|
// If the type is different, you need to destroy the original element first, and then create a new element
|
|
@@ -454,8 +470,6 @@ export class ElementController {
|
|
|
454
470
|
this.createElement(elementType, datum, { animation: false, silence: true });
|
|
455
471
|
}
|
|
456
472
|
|
|
457
|
-
const { stage = 'update' } = context;
|
|
458
|
-
|
|
459
473
|
const exactStage = stage !== 'visibility' ? stage : style.visibility === 'hidden' ? 'hide' : 'show';
|
|
460
474
|
|
|
461
475
|
// 避免立即将 visibility 设置为 hidden,导致元素不可见,而是在 after 阶段再设置
|
|
@@ -664,11 +678,9 @@ export class ElementController {
|
|
|
664
678
|
|
|
665
679
|
const context = { animation, stage: 'expand', data: drawData } as const;
|
|
666
680
|
|
|
667
|
-
//
|
|
668
|
-
add.edges.forEach((edge) =>
|
|
669
|
-
|
|
670
|
-
if (!update.edges.has(id)) update.edges.set(id, edge);
|
|
671
|
-
});
|
|
681
|
+
// 将新增节点/边添加到更新列表 / Add new nodes/edges to the update list
|
|
682
|
+
add.edges.forEach((edge) => update.edges.set(idOf(edge), edge));
|
|
683
|
+
add.nodes.forEach((node) => update.nodes.set(idOf(node), node));
|
|
672
684
|
|
|
673
685
|
this.updateElements(update, context);
|
|
674
686
|
|
package/src/runtime/graph.ts
CHANGED
|
@@ -41,6 +41,7 @@ import type {
|
|
|
41
41
|
} from '../types';
|
|
42
42
|
import { isCollapsed } from '../utils/collapsibility';
|
|
43
43
|
import { sizeOf } from '../utils/dom';
|
|
44
|
+
import { getSubgraphRelatedEdges } from '../utils/edge';
|
|
44
45
|
import { GraphLifeCycleEvent, emit } from '../utils/event';
|
|
45
46
|
import { idOf } from '../utils/id';
|
|
46
47
|
import { format } from '../utils/print';
|
|
@@ -87,7 +88,7 @@ export class Graph extends EventEmitter {
|
|
|
87
88
|
constructor(options: GraphOptions) {
|
|
88
89
|
super();
|
|
89
90
|
this.options = Object.assign({}, Graph.defaultOptions, options);
|
|
90
|
-
this.
|
|
91
|
+
this._setOptions(this.options, true);
|
|
91
92
|
this.context.graph = this;
|
|
92
93
|
|
|
93
94
|
// Listening window.resize to autoResize.
|
|
@@ -117,32 +118,28 @@ export class Graph extends EventEmitter {
|
|
|
117
118
|
* @apiCategory option
|
|
118
119
|
*/
|
|
119
120
|
public setOptions(options: GraphOptions): void {
|
|
120
|
-
|
|
121
|
-
|
|
121
|
+
this._setOptions(options, false);
|
|
122
|
+
}
|
|
122
123
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (canvas) {
|
|
126
|
-
this.emit(GraphEvent.BEFORE_RENDERER_CHANGE, { renderer: this.options.renderer });
|
|
127
|
-
canvas.setRenderer(renderer);
|
|
128
|
-
this.emit(GraphEvent.AFTER_RENDERER_CHANGE, { renderer });
|
|
129
|
-
}
|
|
130
|
-
}
|
|
124
|
+
private _setOptions(options: GraphOptions, isInit: boolean) {
|
|
125
|
+
this.updateCanvas(options);
|
|
131
126
|
|
|
127
|
+
if (isInit) {
|
|
128
|
+
const { data } = options;
|
|
129
|
+
if (data) this.addData(data);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
132
|
Object.assign(this.options, options);
|
|
133
|
-
|
|
134
|
-
if (cursor) this.context.canvas?.setCursor(cursor);
|
|
133
|
+
const { behaviors, combo, data, edge, layout, node, plugins, theme, transforms } = options;
|
|
135
134
|
if (behaviors) this.setBehaviors(behaviors);
|
|
136
|
-
if (combo) this.setCombo(combo);
|
|
137
135
|
if (data) this.setData(data);
|
|
136
|
+
if (node) this.setNode(node);
|
|
138
137
|
if (edge) this.setEdge(edge);
|
|
138
|
+
if (combo) this.setCombo(combo);
|
|
139
139
|
if (layout) this.setLayout(layout);
|
|
140
|
-
if (node) this.setNode(node);
|
|
141
140
|
if (theme) this.setTheme(theme);
|
|
142
141
|
if (plugins) this.setPlugins(plugins);
|
|
143
142
|
if (transforms) this.setTransforms(transforms);
|
|
144
|
-
if (isNumber(width) || isNumber(height))
|
|
145
|
-
this.setSize(width ?? this.options.width ?? 0, height ?? this.options.height ?? 0);
|
|
146
143
|
}
|
|
147
144
|
|
|
148
145
|
/**
|
|
@@ -1015,7 +1012,15 @@ export class Graph extends EventEmitter {
|
|
|
1015
1012
|
private async initCanvas() {
|
|
1016
1013
|
if (this.context.canvas) return await this.context.canvas.ready;
|
|
1017
1014
|
|
|
1018
|
-
const {
|
|
1015
|
+
const {
|
|
1016
|
+
container = 'container',
|
|
1017
|
+
width,
|
|
1018
|
+
height,
|
|
1019
|
+
renderer,
|
|
1020
|
+
cursor,
|
|
1021
|
+
background,
|
|
1022
|
+
devicePixelRatio = window.devicePixelRatio ?? 1,
|
|
1023
|
+
} = this.options;
|
|
1019
1024
|
if (container instanceof Canvas) {
|
|
1020
1025
|
this.context.canvas = container;
|
|
1021
1026
|
if (cursor) container.setCursor(cursor);
|
|
@@ -1034,6 +1039,7 @@ export class Graph extends EventEmitter {
|
|
|
1034
1039
|
background,
|
|
1035
1040
|
renderer,
|
|
1036
1041
|
cursor,
|
|
1042
|
+
devicePixelRatio,
|
|
1037
1043
|
});
|
|
1038
1044
|
|
|
1039
1045
|
this.context.canvas = canvas;
|
|
@@ -1042,6 +1048,23 @@ export class Graph extends EventEmitter {
|
|
|
1042
1048
|
}
|
|
1043
1049
|
}
|
|
1044
1050
|
|
|
1051
|
+
private updateCanvas(options: GraphOptions) {
|
|
1052
|
+
const { renderer, cursor, height, width } = options;
|
|
1053
|
+
const canvas = this.context.canvas;
|
|
1054
|
+
if (!canvas) return;
|
|
1055
|
+
|
|
1056
|
+
if (renderer) {
|
|
1057
|
+
this.emit(GraphEvent.BEFORE_RENDERER_CHANGE, { renderer: this.options.renderer });
|
|
1058
|
+
canvas.setRenderer(renderer);
|
|
1059
|
+
this.emit(GraphEvent.AFTER_RENDERER_CHANGE, { renderer });
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
if (cursor) canvas.setCursor(cursor);
|
|
1063
|
+
|
|
1064
|
+
if (isNumber(width) || isNumber(height))
|
|
1065
|
+
this.setSize(width ?? this.options.width ?? 0, height ?? this.options.height ?? 0);
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1045
1068
|
private initRuntime() {
|
|
1046
1069
|
this.context.options = this.options;
|
|
1047
1070
|
if (!this.context.batch) this.context.batch = new BatchController(this.context);
|
|
@@ -1621,7 +1644,7 @@ export class Graph extends EventEmitter {
|
|
|
1621
1644
|
public async frontElement(id: ID | ID[]): Promise<void> {
|
|
1622
1645
|
const ids = Array.isArray(id) ? id : [id];
|
|
1623
1646
|
const { model, element } = this.context;
|
|
1624
|
-
const
|
|
1647
|
+
const zIndexes: Record<ID, number> = {};
|
|
1625
1648
|
|
|
1626
1649
|
ids.map((_id) => {
|
|
1627
1650
|
const zIndex = element!.getFrontZIndex(_id);
|
|
@@ -1631,12 +1654,18 @@ export class Graph extends EventEmitter {
|
|
|
1631
1654
|
const combos = [ancestor, ...model.getDescendantsData(idOf(ancestor))];
|
|
1632
1655
|
const delta = zIndex - element!.getElementZIndex(_id);
|
|
1633
1656
|
combos.forEach((combo) => {
|
|
1634
|
-
|
|
1657
|
+
zIndexes[idOf(combo)] = this.getElementZIndex(idOf(combo)) + delta;
|
|
1658
|
+
});
|
|
1659
|
+
|
|
1660
|
+
const { internal } = getSubgraphRelatedEdges(combos.map(idOf), (id) => model.getRelatedEdgesData(id));
|
|
1661
|
+
internal.forEach((edge) => {
|
|
1662
|
+
const edgeId = idOf(edge);
|
|
1663
|
+
zIndexes[edgeId] = this.getElementZIndex(edgeId) + delta;
|
|
1635
1664
|
});
|
|
1636
|
-
} else
|
|
1665
|
+
} else zIndexes[_id] = zIndex;
|
|
1637
1666
|
});
|
|
1638
1667
|
|
|
1639
|
-
await this.setElementZIndex(
|
|
1668
|
+
await this.setElementZIndex(zIndexes);
|
|
1640
1669
|
}
|
|
1641
1670
|
|
|
1642
1671
|
/**
|
package/src/runtime/layout.ts
CHANGED
|
@@ -69,6 +69,13 @@ export class LayoutController {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
emit(graph, new GraphLifeCycleEvent(GraphEvent.AFTER_LAYOUT));
|
|
72
|
+
this.transformDataAfterLayout();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private transformDataAfterLayout() {
|
|
76
|
+
const transforms = this.context.transform.getTransformInstance();
|
|
77
|
+
|
|
78
|
+
Object.values(transforms).forEach((transform) => transform.afterLayout());
|
|
72
79
|
}
|
|
73
80
|
|
|
74
81
|
/**
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { COMBO_KEY } from '../constants';
|
|
1
2
|
import type { DrawContext } from '../runtime/element';
|
|
2
3
|
import type { ComboData } from '../spec';
|
|
3
4
|
import { isCollapsed } from '../utils/collapsibility';
|
|
@@ -16,6 +17,7 @@ import { reassignTo } from './utils';
|
|
|
16
17
|
export class CollapseExpandCombo extends BaseTransform {
|
|
17
18
|
public beforeDraw(input: DrawData, context: DrawContext): DrawData {
|
|
18
19
|
if (context.stage === 'visibility') return input;
|
|
20
|
+
if (!this.context.model.model.hasTreeStructure(COMBO_KEY)) return input;
|
|
19
21
|
|
|
20
22
|
const { model } = this.context;
|
|
21
23
|
const { add, update } = input;
|
|
@@ -47,6 +47,9 @@ export class CollapseExpandNode extends BaseTransform {
|
|
|
47
47
|
|
|
48
48
|
public beforeDraw(input: DrawData): DrawData {
|
|
49
49
|
const { graph, model } = this.context;
|
|
50
|
+
|
|
51
|
+
if (!model.model.hasTreeStructure(TREE_KEY)) return input;
|
|
52
|
+
|
|
50
53
|
const {
|
|
51
54
|
add: { nodes: nodesToAdd, edges: edgesToAdd },
|
|
52
55
|
update: { nodes: nodesToUpdate },
|
package/src/transforms/index.ts
CHANGED
|
@@ -3,7 +3,10 @@ export { BaseTransform } from './base-transform';
|
|
|
3
3
|
export { CollapseExpandCombo } from './collapse-expand-combo';
|
|
4
4
|
export { CollapseExpandNode } from './collapse-expand-node';
|
|
5
5
|
export { GetEdgeActualEnds } from './get-edge-actual-ends';
|
|
6
|
+
export { PositionRadialLabels } from './position-radial-labels';
|
|
6
7
|
export { ProcessParallelEdges } from './process-parallel-edges';
|
|
7
8
|
export { UpdateRelatedEdge } from './update-related-edge';
|
|
8
9
|
|
|
9
10
|
export type { BaseTransformOptions } from './base-transform';
|
|
11
|
+
export type { PositionRadialLabelsOptions } from './position-radial-labels';
|
|
12
|
+
export type { ProcessParallelEdgesOptions } from './process-parallel-edges';
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { rad2deg } from '@antv/g';
|
|
2
|
+
import type { RuntimeContext } from '../runtime/types';
|
|
3
|
+
import type { NodeData } from '../spec';
|
|
4
|
+
import { idOf } from '../utils/id';
|
|
5
|
+
import { positionOf } from '../utils/position';
|
|
6
|
+
import { parseSize } from '../utils/size';
|
|
7
|
+
import { rad, subtract } from '../utils/vector';
|
|
8
|
+
import type { BaseTransformOptions } from './base-transform';
|
|
9
|
+
import { BaseTransform } from './base-transform';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* <zh/> 根据径向布局自动调整节点标签样式的配置项
|
|
13
|
+
*
|
|
14
|
+
* <en/> Options for automatically adjusting the style of node labels according to the radial layout
|
|
15
|
+
*/
|
|
16
|
+
export interface PositionRadialLabelsOptions extends BaseTransformOptions {
|
|
17
|
+
/**
|
|
18
|
+
* <zh/> 偏移量
|
|
19
|
+
*
|
|
20
|
+
* <en/> Offset
|
|
21
|
+
*/
|
|
22
|
+
offset?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* <zh/> 根据径向布局自动调整节点标签样式,包括位置和旋转角度
|
|
27
|
+
*
|
|
28
|
+
* <en/> Automatically adjust the style of node labels according to the radial layout, including position and rotation angle
|
|
29
|
+
*/
|
|
30
|
+
export class PositionRadialLabels extends BaseTransform<PositionRadialLabelsOptions> {
|
|
31
|
+
static defaultOptions: Partial<PositionRadialLabelsOptions> = {
|
|
32
|
+
offset: 5,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
constructor(context: RuntimeContext, options: PositionRadialLabelsOptions) {
|
|
36
|
+
super(context, Object.assign({}, PositionRadialLabels.defaultOptions, options));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private get ref(): NodeData {
|
|
40
|
+
return this.context.model.getRootsData()[0];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public afterLayout() {
|
|
44
|
+
const refPoint = positionOf(this.ref);
|
|
45
|
+
|
|
46
|
+
const { graph, model } = this.context;
|
|
47
|
+
const data = model.getData();
|
|
48
|
+
|
|
49
|
+
data.nodes?.forEach((datum) => {
|
|
50
|
+
if (idOf(datum) === idOf(this.ref)) return;
|
|
51
|
+
|
|
52
|
+
const radian = rad(subtract(positionOf(datum), refPoint));
|
|
53
|
+
const isLeft = Math.abs(radian) > Math.PI / 2;
|
|
54
|
+
|
|
55
|
+
const isLeaf = !datum.children || datum.children.length === 0;
|
|
56
|
+
const nodeHalfWidth = parseSize(graph.getElementRenderStyle(idOf(datum)).size)[0] / 2;
|
|
57
|
+
const offset = (isLeaf ? 1 : -1) * (nodeHalfWidth + this.options.offset);
|
|
58
|
+
|
|
59
|
+
const translate = `translate(${offset * Math.cos(radian)},${offset * Math.sin(radian)})`;
|
|
60
|
+
const rotate = `rotate(${isLeft ? rad2deg(radian) + 180 : rad2deg(radian)}deg)`;
|
|
61
|
+
|
|
62
|
+
model.updateNodeData([
|
|
63
|
+
{
|
|
64
|
+
id: idOf(datum),
|
|
65
|
+
style: {
|
|
66
|
+
labelTextAlign: isLeft === isLeaf ? 'right' : 'left',
|
|
67
|
+
labelTextBaseline: 'middle',
|
|
68
|
+
labelTransform: `${translate} ${rotate}`,
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
graph.draw();
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/types/element.ts
CHANGED
|
@@ -29,13 +29,14 @@ export interface Node extends DisplayObject, ElementHooks, ElementMethods {
|
|
|
29
29
|
*
|
|
30
30
|
* <en/> Get the intersection point
|
|
31
31
|
* @param point - <zh/> 外部位置 | <en/> external position
|
|
32
|
+
* @param useExtendedLine - <zh/> 是否使用延长线 | <en/> whether to use the extended line
|
|
32
33
|
* @returns <zh/> 交点位置 | <en/> intersection point
|
|
33
34
|
* @remarks
|
|
34
35
|
* <zh/> 给定一个外部位置,返回当前节点与该位置的连边与节点的交点位置
|
|
35
36
|
*
|
|
36
37
|
* <en/> Given an external position, return the intersection point of the edge between the current node and the position and the node
|
|
37
38
|
*/
|
|
38
|
-
getIntersectPoint(point: Point): Point;
|
|
39
|
+
getIntersectPoint(point: Point, useExtendedLine?: boolean): Point;
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
/**
|
package/src/utils/data.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { get } from '@antv/util';
|
|
2
2
|
import type { ComboData, EdgeData, GraphData, NodeData } from '../spec';
|
|
3
|
+
import type { ElementDatum, ID } from '../types';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* <zh/> 合并两个 节点/边/Combo 的数据
|
|
@@ -62,3 +63,51 @@ export function cloneElementData<T extends NodeData | EdgeData | ComboData>(data
|
|
|
62
63
|
export function isEmptyData(data: GraphData) {
|
|
63
64
|
return !get(data, ['nodes', 'length']) && !get(data, ['edges', 'length']) && !get(data, ['combos', 'length']);
|
|
64
65
|
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* <zh/> 判断两个元素数据是否相等
|
|
69
|
+
*
|
|
70
|
+
* <en/> Determine if two element data are equal
|
|
71
|
+
* @param original - <zh/> 原始数据 | <en/> original data
|
|
72
|
+
* @param modified - <zh/> 修改后的数据 | <en/> modified data
|
|
73
|
+
* @returns <zh/> 是否相等 | <en/> is equal
|
|
74
|
+
* @remarks
|
|
75
|
+
* <zh/> 相比于 isEqual,这个方法不会比较更下层的数据
|
|
76
|
+
*
|
|
77
|
+
* <en/> Compared to isEqual, this method does not compare data at a lower level
|
|
78
|
+
*/
|
|
79
|
+
export function isElementDataEqual(original: Partial<ElementDatum> = {}, modified: Partial<ElementDatum> = {}) {
|
|
80
|
+
const {
|
|
81
|
+
states: originalStates = [],
|
|
82
|
+
data: originalData = {},
|
|
83
|
+
style: originalStyle = {},
|
|
84
|
+
children: originalChildren = [],
|
|
85
|
+
...originalAttrs
|
|
86
|
+
} = original;
|
|
87
|
+
const {
|
|
88
|
+
states: modifiedStates = [],
|
|
89
|
+
data: modifiedData = {},
|
|
90
|
+
style: modifiedStyle = {},
|
|
91
|
+
children: modifiedChildren = [],
|
|
92
|
+
...modifiedAttrs
|
|
93
|
+
} = modified;
|
|
94
|
+
|
|
95
|
+
const isArrayEqual = (arr1: unknown[], arr2: unknown[]) => {
|
|
96
|
+
if (arr1.length !== arr2.length) return false;
|
|
97
|
+
return arr1.every((item, index) => item === arr2[index]);
|
|
98
|
+
};
|
|
99
|
+
const isObjectEqual = (obj1: Record<string, unknown>, obj2: Record<string, unknown>) => {
|
|
100
|
+
const keys1 = Object.keys(obj1);
|
|
101
|
+
const keys2 = Object.keys(obj2);
|
|
102
|
+
if (keys1.length !== keys2.length) return false;
|
|
103
|
+
return keys1.every((key) => obj1[key] === obj2[key]);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
if (!isObjectEqual(originalAttrs, modifiedAttrs)) return false;
|
|
107
|
+
if (!isArrayEqual(originalChildren as ID[], modifiedChildren as ID[])) return false;
|
|
108
|
+
if (!isArrayEqual(originalStates, modifiedStates)) return false;
|
|
109
|
+
if (!isObjectEqual(originalData, modifiedData)) return false;
|
|
110
|
+
if (!isObjectEqual(originalStyle, modifiedStyle)) return false;
|
|
111
|
+
|
|
112
|
+
return true;
|
|
113
|
+
}
|
package/src/utils/diff.ts
CHANGED
|
@@ -7,9 +7,15 @@ import { isEqual } from '@antv/util';
|
|
|
7
7
|
* @param original - <zh/> 原始数组 | <en/> original array
|
|
8
8
|
* @param modified - <zh/> 修改后的数组 | <en/> modified array
|
|
9
9
|
* @param key - <zh/> 比较的 key | <en/> key to compare
|
|
10
|
+
* @param comparator - <zh/> 比较函数 | <en/> compare function
|
|
10
11
|
* @returns <zh/> 数组差异 | <en/> array diff
|
|
11
12
|
*/
|
|
12
|
-
export function arrayDiff<T>(
|
|
13
|
+
export function arrayDiff<T>(
|
|
14
|
+
original: T[],
|
|
15
|
+
modified: T[],
|
|
16
|
+
key: (d: T) => string | number,
|
|
17
|
+
comparator: (a?: T, b?: T) => boolean = isEqual,
|
|
18
|
+
) {
|
|
13
19
|
const originalMap = new Map(original.map((d) => [key(d), d]));
|
|
14
20
|
const modifiedMap = new Map(modified.map((d) => [key(d), d]));
|
|
15
21
|
|
|
@@ -23,7 +29,7 @@ export function arrayDiff<T>(original: T[], modified: T[], key: (d: T) => string
|
|
|
23
29
|
|
|
24
30
|
modifiedSet.forEach((key) => {
|
|
25
31
|
if (originalSet.has(key)) {
|
|
26
|
-
if (!
|
|
32
|
+
if (!comparator(originalMap.get(key), modifiedMap.get(key))) {
|
|
27
33
|
update.push(modifiedMap.get(key)!);
|
|
28
34
|
} else {
|
|
29
35
|
keep.push(modifiedMap.get(key)!);
|