@antv/g6 5.0.11 → 5.0.13
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/elements/nodes/base-node.d.ts +1 -0
- package/dist/lib/elements/nodes/image.d.ts +4 -2
- package/dist/lib/elements/shapes/icon.d.ts +3 -2
- package/dist/lib/elements/shapes/image.d.ts +38 -0
- package/dist/lib/elements/shapes/index.d.ts +2 -0
- package/dist/lib/exports.d.ts +1 -1
- package/dist/lib/runtime/layout.d.ts +1 -0
- package/dist/lib/utils/shape.d.ts +9 -1
- package/dist/lib/utils/tree.d.ts +1 -1
- package/dist/lib/version.d.ts +1 -1
- package/esm/behaviors/drag-canvas.js +9 -9
- package/esm/behaviors/drag-canvas.js.map +1 -1
- package/esm/elements/combos/base-combo.js +3 -0
- package/esm/elements/combos/base-combo.js.map +1 -1
- package/esm/elements/nodes/base-node.d.ts +1 -0
- package/esm/elements/nodes/base-node.js +13 -2
- package/esm/elements/nodes/base-node.js.map +1 -1
- package/esm/elements/nodes/image.d.ts +4 -2
- package/esm/elements/nodes/image.js +12 -2
- package/esm/elements/nodes/image.js.map +1 -1
- package/esm/elements/shapes/base-shape.js +2 -1
- package/esm/elements/shapes/base-shape.js.map +1 -1
- package/esm/elements/shapes/icon.d.ts +3 -2
- package/esm/elements/shapes/icon.js +7 -5
- package/esm/elements/shapes/icon.js.map +1 -1
- package/esm/elements/shapes/image.d.ts +38 -0
- package/esm/elements/shapes/image.js +75 -0
- package/esm/elements/shapes/image.js.map +1 -0
- package/esm/elements/shapes/index.d.ts +2 -0
- package/esm/elements/shapes/index.js +1 -0
- package/esm/elements/shapes/index.js.map +1 -1
- package/esm/exports.d.ts +1 -1
- package/esm/plugins/minimap/index.js +2 -0
- package/esm/plugins/minimap/index.js.map +1 -1
- package/esm/plugins/tooltip.js +4 -1
- package/esm/plugins/tooltip.js.map +1 -1
- package/esm/runtime/graph.js +1 -0
- package/esm/runtime/graph.js.map +1 -1
- package/esm/runtime/layout.d.ts +1 -0
- package/esm/runtime/layout.js +48 -0
- package/esm/runtime/layout.js.map +1 -1
- package/esm/utils/shape.d.ts +9 -1
- package/esm/utils/shape.js +17 -1
- package/esm/utils/shape.js.map +1 -1
- package/esm/utils/tree.d.ts +1 -1
- package/esm/utils/tree.js +2 -2
- package/esm/utils/tree.js.map +1 -1
- package/esm/version.d.ts +1 -1
- package/esm/version.js +1 -1
- package/lib/behaviors/drag-canvas.js +8 -8
- package/lib/behaviors/drag-canvas.js.map +1 -1
- package/lib/elements/combos/base-combo.js +3 -0
- package/lib/elements/combos/base-combo.js.map +1 -1
- package/lib/elements/nodes/base-node.d.ts +1 -0
- package/lib/elements/nodes/base-node.js +13 -2
- package/lib/elements/nodes/base-node.js.map +1 -1
- package/lib/elements/nodes/image.d.ts +4 -2
- package/lib/elements/nodes/image.js +11 -1
- package/lib/elements/nodes/image.js.map +1 -1
- package/lib/elements/shapes/base-shape.js +2 -1
- package/lib/elements/shapes/base-shape.js.map +1 -1
- package/lib/elements/shapes/icon.d.ts +3 -2
- package/lib/elements/shapes/icon.js +6 -4
- package/lib/elements/shapes/icon.js.map +1 -1
- package/lib/elements/shapes/image.d.ts +38 -0
- package/lib/elements/shapes/image.js +81 -0
- package/lib/elements/shapes/image.js.map +1 -0
- package/lib/elements/shapes/index.d.ts +2 -0
- package/lib/elements/shapes/index.js +3 -1
- package/lib/elements/shapes/index.js.map +1 -1
- package/lib/exports.d.ts +1 -1
- package/lib/plugins/minimap/index.js +2 -0
- package/lib/plugins/minimap/index.js.map +1 -1
- package/lib/plugins/tooltip.js +4 -1
- package/lib/plugins/tooltip.js.map +1 -1
- package/lib/runtime/graph.js +1 -0
- package/lib/runtime/graph.js.map +1 -1
- package/lib/runtime/layout.d.ts +1 -0
- package/lib/runtime/layout.js +48 -0
- package/lib/runtime/layout.js.map +1 -1
- package/lib/utils/shape.d.ts +9 -1
- package/lib/utils/shape.js +18 -1
- package/lib/utils/shape.js.map +1 -1
- package/lib/utils/tree.d.ts +1 -1
- package/lib/utils/tree.js +2 -2
- package/lib/utils/tree.js.map +1 -1
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +8 -8
- package/src/behaviors/drag-canvas.ts +9 -9
- package/src/elements/combos/base-combo.ts +3 -0
- package/src/elements/nodes/base-node.ts +14 -2
- package/src/elements/nodes/image.ts +14 -3
- package/src/elements/shapes/base-shape.ts +3 -1
- package/src/elements/shapes/icon.ts +8 -5
- package/src/elements/shapes/image.ts +88 -0
- package/src/elements/shapes/index.ts +2 -0
- package/src/exports.ts +1 -0
- package/src/plugins/minimap/index.ts +3 -1
- package/src/plugins/tooltip.ts +4 -1
- package/src/runtime/graph.ts +1 -0
- package/src/runtime/layout.ts +55 -0
- package/src/utils/shape.ts +18 -1
- package/src/utils/tree.ts +3 -3
- package/src/version.ts +1 -1
|
@@ -30,6 +30,7 @@ import { BaseElement } from '../base-element';
|
|
|
30
30
|
import { effect } from '../effect';
|
|
31
31
|
import type { BadgeStyleProps, IconStyleProps, LabelStyleProps } from '../shapes';
|
|
32
32
|
import { Badge, Icon, Label } from '../shapes';
|
|
33
|
+
import { connectImage, dispatchPositionChange } from '../shapes/image';
|
|
33
34
|
|
|
34
35
|
/**
|
|
35
36
|
* <zh/> 节点通用样式配置项
|
|
@@ -374,6 +375,7 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
|
|
|
374
375
|
@effect((self, attributes) => self.getIconStyle(attributes))
|
|
375
376
|
protected drawIconShape(attributes: Required<S>, container: Group): void {
|
|
376
377
|
this.upsert('icon', Icon, this.getIconStyle(attributes), container);
|
|
378
|
+
connectImage(this);
|
|
377
379
|
}
|
|
378
380
|
|
|
379
381
|
@effect((self, attributes) => self.getBadgesStyle(attributes))
|
|
@@ -427,6 +429,13 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
|
|
|
427
429
|
this.drawPortShapes(attributes, container);
|
|
428
430
|
}
|
|
429
431
|
|
|
432
|
+
public update(attr?: Partial<S>): void {
|
|
433
|
+
super.update(attr);
|
|
434
|
+
if (attr && ('x' in attr || 'y' in attr || 'z' in attr)) {
|
|
435
|
+
dispatchPositionChange(this);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
430
439
|
protected onframe() {
|
|
431
440
|
this.drawBadgeShapes(this.parsedAttributes, this);
|
|
432
441
|
this.drawLabelShape(this.parsedAttributes, this);
|
|
@@ -434,9 +443,12 @@ export abstract class BaseNode<S extends BaseNodeStyleProps = BaseNodeStyleProps
|
|
|
434
443
|
}
|
|
435
444
|
|
|
436
445
|
/**
|
|
446
|
+
* <zh/> 在离屏画布中获取图形包围盒
|
|
437
447
|
*
|
|
438
|
-
*
|
|
439
|
-
* @param
|
|
448
|
+
* <en/> Get the bounding box of the graphic in the off-screen canvas
|
|
449
|
+
* @param context - <zh/> 运行时上下文 <en/> Runtime context
|
|
450
|
+
* @param shape - <zh/> 图形实例 <en/> Graphic instance
|
|
451
|
+
* @returns <zh/> 图形包围盒 <en/> Graphic bounding box
|
|
440
452
|
*/
|
|
441
453
|
function getBoundsInOffscreen(context: RuntimeContext, shape: DisplayObject) {
|
|
442
454
|
if (!context) return shape.getLocalBounds();
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type { DisplayObjectConfig, RectStyleProps as GRectStyleProps, Group } from '@antv/g';
|
|
2
|
-
import {
|
|
2
|
+
import { ImageStyleProps as GImageStyleProps, Rect as GRect } from '@antv/g';
|
|
3
3
|
import { ICON_SIZE_RATIO } from '../../constants/element';
|
|
4
4
|
import { subStyleProps } from '../../utils/prefix';
|
|
5
5
|
import { mergeOptions } from '../../utils/style';
|
|
6
6
|
import { add } from '../../utils/vector';
|
|
7
7
|
import type { IconStyleProps } from '../shapes';
|
|
8
|
+
import { Image as ImageShape } from '../shapes';
|
|
9
|
+
import { connectImage, dispatchPositionChange } from '../shapes/image';
|
|
8
10
|
import type { BaseNodeStyleProps } from './base-node';
|
|
9
11
|
import { BaseNode } from './base-node';
|
|
10
12
|
|
|
@@ -79,11 +81,20 @@ export class Image extends BaseNode<ImageStyleProps> {
|
|
|
79
81
|
: false;
|
|
80
82
|
}
|
|
81
83
|
|
|
82
|
-
protected drawKeyShape(attributes: Required<ImageStyleProps>, container: Group):
|
|
83
|
-
|
|
84
|
+
protected drawKeyShape(attributes: Required<ImageStyleProps>, container: Group): ImageShape | undefined {
|
|
85
|
+
const image = this.upsert('key', ImageShape, this.getKeyStyle(attributes), container);
|
|
86
|
+
connectImage(this);
|
|
87
|
+
return image;
|
|
84
88
|
}
|
|
85
89
|
|
|
86
90
|
protected drawHaloShape(attributes: Required<ImageStyleProps>, container: Group): void {
|
|
87
91
|
this.upsert('halo', GRect, this.getHaloStyle(attributes), container);
|
|
88
92
|
}
|
|
93
|
+
|
|
94
|
+
public update(attr?: Partial<ImageStyleProps>): void {
|
|
95
|
+
super.update(attr);
|
|
96
|
+
if (attr && ('x' in attr || 'y' in attr || 'z' in attr)) {
|
|
97
|
+
dispatchPositionChange(this);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
89
100
|
}
|
|
@@ -66,7 +66,9 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
|
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
// create
|
|
69
|
-
if (!target || target.destroyed) {
|
|
69
|
+
if (!target || target.destroyed || !(target instanceof Ctor)) {
|
|
70
|
+
target?.destroy();
|
|
71
|
+
|
|
70
72
|
const instance = new Ctor({ className, style });
|
|
71
73
|
container.appendChild(instance);
|
|
72
74
|
this.shapeMap[className] = instance;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { DisplayObjectConfig,
|
|
1
|
+
import { DisplayObjectConfig, Text as GText, Group, TextStyleProps } from '@antv/g';
|
|
2
2
|
import type { BaseShapeStyleProps } from './base-shape';
|
|
3
3
|
import { BaseShape } from './base-shape';
|
|
4
|
+
import type { ImageStyleProps } from './image';
|
|
5
|
+
import { Image as GImage } from './image';
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* <zh/> 图标样式
|
|
@@ -23,14 +25,15 @@ export class Icon extends BaseShape<IconStyleProps> {
|
|
|
23
25
|
super(options);
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
private
|
|
27
|
-
|
|
28
|
+
private isImage() {
|
|
29
|
+
const { src } = this.attributes;
|
|
30
|
+
return !!src;
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
protected getIconStyle(attributes: IconStyleProps = this.attributes): IconStyleProps {
|
|
31
34
|
const { width = 0, height = 0 } = attributes;
|
|
32
35
|
const style = this.getGraphicStyle(attributes);
|
|
33
|
-
if (this.
|
|
36
|
+
if (this.isImage()) {
|
|
34
37
|
return {
|
|
35
38
|
x: -width / 2,
|
|
36
39
|
y: -height / 2,
|
|
@@ -45,6 +48,6 @@ export class Icon extends BaseShape<IconStyleProps> {
|
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
public render(attributes = this.attributes, container: Group = this): void {
|
|
48
|
-
this.upsert('icon', (this.
|
|
51
|
+
this.upsert('icon', (this.isImage() ? GImage : GText) as any, this.getIconStyle(attributes), container);
|
|
49
52
|
}
|
|
50
53
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { DisplayObject, DisplayObjectConfig, ImageStyleProps as GImageStyleProps } from '@antv/g';
|
|
2
|
+
import { ElementEvent, Image as GImage, Rect as GRect } from '@antv/g';
|
|
3
|
+
import { getAncestorShapes } from '../../utils/shape';
|
|
4
|
+
|
|
5
|
+
export interface ImageStyleProps extends GImageStyleProps {
|
|
6
|
+
/**
|
|
7
|
+
* <zh/> 圆角半径
|
|
8
|
+
*
|
|
9
|
+
* <en/> Radius of the rounded corner
|
|
10
|
+
*/
|
|
11
|
+
radius?: number | number[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class Image extends GImage {
|
|
15
|
+
constructor(options: DisplayObjectConfig<ImageStyleProps>) {
|
|
16
|
+
super(options);
|
|
17
|
+
current = this;
|
|
18
|
+
this.isMutationObserved = true;
|
|
19
|
+
this.addEventListener(ElementEvent.MOUNTED, this.onMounted);
|
|
20
|
+
this.addEventListener(ElementEvent.ATTR_MODIFIED, this.onAttrModified);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private onMounted = () => {
|
|
24
|
+
this.handleRadius();
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
private onAttrModified = () => {
|
|
28
|
+
this.handleRadius();
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
public handleRadius() {
|
|
32
|
+
const { radius, clipPath, width = 0, height = 0 } = this.attributes as ImageStyleProps;
|
|
33
|
+
|
|
34
|
+
if (radius && width && height) {
|
|
35
|
+
const [x, y] = this.getBounds().min;
|
|
36
|
+
const clipPathStyle = { x, y, radius, width, height };
|
|
37
|
+
|
|
38
|
+
if (clipPath) {
|
|
39
|
+
Object.assign(this.parsedStyle.clipPath!.style, clipPathStyle);
|
|
40
|
+
} else {
|
|
41
|
+
const rect = new GRect({ style: clipPathStyle });
|
|
42
|
+
this.style.clipPath = rect;
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
if (clipPath) this.style.clipPath = null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const ImagesWeakMap = new WeakMap<DisplayObject, Image[]>();
|
|
51
|
+
|
|
52
|
+
let current: Image | null = null;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* <zh/> 由于 g clipPath 不支持相对位置,因此当作用的元素发生位置变化时,需要通知 Image 更新 clipPath。
|
|
56
|
+
*
|
|
57
|
+
* 通过 connectImage 创建图形与图片的关联,并结合 dispatchPositionChange 方法触发更新
|
|
58
|
+
*
|
|
59
|
+
* ⚠️ 这是一种临时的、黑盒的解决方案,如果后续 g 支持相对位置,会移除该方法。
|
|
60
|
+
*
|
|
61
|
+
* <en/> Since g clipPath does not support relative positions, when the position of the affected element changes, the Image needs to be notified to update the clipPath.
|
|
62
|
+
*
|
|
63
|
+
* Use connectImage to create an association between the shape and the image, and combine it with the dispatchPositionChange method to trigger the update.
|
|
64
|
+
*
|
|
65
|
+
* ⚠️ This is a temporary, black-box solution, and if g supports relative positions in the future, this method will be removed.
|
|
66
|
+
* @param target - <zh/> 目标元素 <en/> Target element
|
|
67
|
+
*/
|
|
68
|
+
export const connectImage = (target: DisplayObject) => {
|
|
69
|
+
if (current && getAncestorShapes(current).includes(target)) {
|
|
70
|
+
const images = ImagesWeakMap.get(target);
|
|
71
|
+
if (images) {
|
|
72
|
+
if (!images.includes(current)) images.push(current);
|
|
73
|
+
} else ImagesWeakMap.set(target, [current]);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* <zh/> 触发关联的图片更新位置
|
|
79
|
+
*
|
|
80
|
+
* <en/> Trigger the associated image to update its position
|
|
81
|
+
* @param target - <zh/> 目标元素 <en/> Target element
|
|
82
|
+
*/
|
|
83
|
+
export const dispatchPositionChange = (target: DisplayObject) => {
|
|
84
|
+
const image = ImagesWeakMap.get(target);
|
|
85
|
+
if (image) {
|
|
86
|
+
image.forEach((i) => i.handleRadius());
|
|
87
|
+
}
|
|
88
|
+
};
|
|
@@ -8,11 +8,13 @@ export { Badge } from './badge';
|
|
|
8
8
|
export { BaseShape } from './base-shape';
|
|
9
9
|
export { Contour } from './contour';
|
|
10
10
|
export { Icon } from './icon';
|
|
11
|
+
export { Image } from './image';
|
|
11
12
|
export { Label } from './label';
|
|
12
13
|
|
|
13
14
|
export type { BadgeStyleProps } from './badge';
|
|
14
15
|
export type { BaseShapeStyleProps } from './base-shape';
|
|
15
16
|
export type { ContourStyleProps } from './contour';
|
|
16
17
|
export type { IconStyleProps } from './icon';
|
|
18
|
+
export type { ImageStyleProps } from './image';
|
|
17
19
|
export type { LabelStyleProps } from './label';
|
|
18
20
|
export type { PolygonStyleProps } from './polygon';
|
package/src/exports.ts
CHANGED
|
@@ -187,7 +187,9 @@ export class Minimap extends BasePlugin<MinimapOptions> {
|
|
|
187
187
|
const id = idOf(datum);
|
|
188
188
|
ids.add(id);
|
|
189
189
|
|
|
190
|
-
const target = element!.getElement(id)
|
|
190
|
+
const target = element!.getElement(id);
|
|
191
|
+
if (!target) return;
|
|
192
|
+
|
|
191
193
|
const shape = target.getShape('key');
|
|
192
194
|
const cloneShape = this.shapes.get(id) || shape.cloneNode();
|
|
193
195
|
|
package/src/plugins/tooltip.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Tooltip as TooltipComponent } from '@antv/component';
|
|
|
3
3
|
import { get } from '@antv/util';
|
|
4
4
|
import type { RuntimeContext } from '../runtime/types';
|
|
5
5
|
import type { ElementDatum, ElementType, ID, IElementEvent } from '../types';
|
|
6
|
+
import { isToBeDestroyed } from '../utils/element';
|
|
6
7
|
import type { BasePluginOptions } from './base-plugin';
|
|
7
8
|
import { BasePlugin } from './base-plugin';
|
|
8
9
|
|
|
@@ -235,8 +236,10 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
|
|
|
235
236
|
client,
|
|
236
237
|
target: { id },
|
|
237
238
|
} = event;
|
|
238
|
-
|
|
239
|
+
if (isToBeDestroyed(event.target)) return;
|
|
239
240
|
if (!this.tooltipElement || !this.isEnable(event)) return;
|
|
241
|
+
|
|
242
|
+
const targetType = this.context.graph.getElementType(id);
|
|
240
243
|
const { getContent, title } = this.options;
|
|
241
244
|
this.currentTarget = id;
|
|
242
245
|
const items: ElementDatum[] = this.getElementData(id, targetType as ElementType);
|
package/src/runtime/graph.ts
CHANGED
|
@@ -1733,6 +1733,7 @@ export class Graph extends EventEmitter {
|
|
|
1733
1733
|
|
|
1734
1734
|
const elementType = model.getElementType(id);
|
|
1735
1735
|
|
|
1736
|
+
await this.frontElement(id);
|
|
1736
1737
|
this.isCollapsingExpanding = true;
|
|
1737
1738
|
this.setElementCollapsibility(id, true);
|
|
1738
1739
|
if (elementType === 'node') await element!.collapseNode(id, animation);
|
package/src/runtime/layout.ts
CHANGED
|
@@ -156,6 +156,7 @@ export class LayoutController {
|
|
|
156
156
|
|
|
157
157
|
createTreeStructure(model);
|
|
158
158
|
|
|
159
|
+
const layoutPreset: GraphData = { nodes: [], edges: [] };
|
|
159
160
|
const layoutResult: GraphData = { nodes: [], edges: [] };
|
|
160
161
|
|
|
161
162
|
const roots = model.getRoots(TREE_KEY) as unknown as TreeData[];
|
|
@@ -170,11 +171,13 @@ export class LayoutController {
|
|
|
170
171
|
);
|
|
171
172
|
|
|
172
173
|
const result = layout(root, options);
|
|
174
|
+
const { x: rx, y: ry, z: rz = 0 } = result;
|
|
173
175
|
// 将布局结果转化为 LayoutMapping 格式 / Convert the layout result to LayoutMapping format
|
|
174
176
|
dfs(
|
|
175
177
|
result,
|
|
176
178
|
(node) => {
|
|
177
179
|
const { id, x, y, z = 0 } = node;
|
|
180
|
+
layoutPreset.nodes!.push({ id, style: { x: rx, y: ry, z: rz } });
|
|
178
181
|
layoutResult.nodes!.push({ id, style: { x, y, z } });
|
|
179
182
|
},
|
|
180
183
|
(node) => node.children,
|
|
@@ -182,13 +185,45 @@ export class LayoutController {
|
|
|
182
185
|
);
|
|
183
186
|
});
|
|
184
187
|
|
|
188
|
+
const offset = this.inferTreeLayoutOffset(layoutResult);
|
|
189
|
+
applyTreeLayoutOffset(layoutResult, offset);
|
|
190
|
+
|
|
185
191
|
if (animation) {
|
|
192
|
+
// 先将所有节点移动到根节点位置 / Move all nodes to the root node position first
|
|
193
|
+
applyTreeLayoutOffset(layoutPreset, offset);
|
|
194
|
+
this.updateElementPosition(layoutPreset, false);
|
|
195
|
+
|
|
186
196
|
this.updateElementPosition(layoutResult, animation);
|
|
187
197
|
}
|
|
188
198
|
|
|
189
199
|
return layoutResult;
|
|
190
200
|
}
|
|
191
201
|
|
|
202
|
+
private inferTreeLayoutOffset(data: GraphData) {
|
|
203
|
+
let [minX, maxX] = [Infinity, -Infinity];
|
|
204
|
+
let [minY, maxY] = [Infinity, -Infinity];
|
|
205
|
+
|
|
206
|
+
data.nodes?.forEach((node) => {
|
|
207
|
+
const { x = 0, y = 0 } = node.style || {};
|
|
208
|
+
minX = Math.min(minX, x);
|
|
209
|
+
maxX = Math.max(maxX, x);
|
|
210
|
+
minY = Math.min(minY, y);
|
|
211
|
+
maxY = Math.max(maxY, y);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const { canvas } = this.context;
|
|
215
|
+
const canvasSize = canvas.getSize();
|
|
216
|
+
const [x1, y1] = canvas.getCanvasByViewport([0, 0]);
|
|
217
|
+
const [x2, y2] = canvas.getCanvasByViewport(canvasSize);
|
|
218
|
+
|
|
219
|
+
if (minX >= x1 && maxX <= x2 && minY >= y1 && maxY <= y2) return [0, 0] as [number, number];
|
|
220
|
+
|
|
221
|
+
const cx = (x1 + x2) / 2;
|
|
222
|
+
const cy = (y1 + y2) / 2;
|
|
223
|
+
|
|
224
|
+
return [cx - (minX + maxX) / 2, cy - (minY + maxY) / 2] as [number, number];
|
|
225
|
+
}
|
|
226
|
+
|
|
192
227
|
public stopLayout() {
|
|
193
228
|
if (this.instance && isLayoutWithIterations(this.instance)) {
|
|
194
229
|
this.instance.stop();
|
|
@@ -303,3 +338,23 @@ export class LayoutController {
|
|
|
303
338
|
this.animationResult = undefined;
|
|
304
339
|
}
|
|
305
340
|
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* <zh/> 对树形布局结果应用偏移
|
|
344
|
+
*
|
|
345
|
+
* <en/> Apply offset to tree layout result
|
|
346
|
+
* @param data - <zh/> 布局数据 | <en/> Layout data
|
|
347
|
+
* @param offset - <zh/> 偏移量 | <en/> Offset
|
|
348
|
+
*/
|
|
349
|
+
const applyTreeLayoutOffset = (data: GraphData, offset: [number, number]) => {
|
|
350
|
+
const [ox, oy] = offset;
|
|
351
|
+
data.nodes?.forEach((node) => {
|
|
352
|
+
if (node.style) {
|
|
353
|
+
const { x = 0, y = 0 } = node.style;
|
|
354
|
+
node.style.x = x + ox;
|
|
355
|
+
node.style.y = y + oy;
|
|
356
|
+
} else {
|
|
357
|
+
node.style = { x: ox, y: oy };
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
};
|
package/src/utils/shape.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { DisplayObject } from '@antv/g';
|
|
|
4
4
|
* <zh/> 获取图形的所有子元素
|
|
5
5
|
*
|
|
6
6
|
* <en/> Get all the child elements of the shape
|
|
7
|
-
* @param shape <zh/> 图形元素 | <en/> shape
|
|
7
|
+
* @param shape - <zh/> 图形元素 | <en/> shape
|
|
8
8
|
* @returns <zh/> 子元素数组 | <en/> child elements array
|
|
9
9
|
*/
|
|
10
10
|
export function getDescendantShapes<T extends DisplayObject>(shape: T) {
|
|
@@ -24,3 +24,20 @@ export function getDescendantShapes<T extends DisplayObject>(shape: T) {
|
|
|
24
24
|
|
|
25
25
|
return succeeds;
|
|
26
26
|
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* <zh/> 获取图形的所有祖先元素
|
|
30
|
+
*
|
|
31
|
+
* <en/> Get all the ancestor elements of the shape
|
|
32
|
+
* @param shape - <zh/> 图形元素 | <en/> shape
|
|
33
|
+
* @returns <zh/> 祖先元素数组 | <en/> ancestor elements array
|
|
34
|
+
*/
|
|
35
|
+
export function getAncestorShapes<T extends DisplayObject>(shape: T) {
|
|
36
|
+
const ancestors: DisplayObject[] = [];
|
|
37
|
+
let currentNode = shape.parentNode as DisplayObject;
|
|
38
|
+
while (currentNode) {
|
|
39
|
+
ancestors.push(currentNode);
|
|
40
|
+
currentNode = currentNode.parentNode as DisplayObject;
|
|
41
|
+
}
|
|
42
|
+
return ancestors;
|
|
43
|
+
}
|
package/src/utils/tree.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { TreeData } from '../types';
|
|
|
3
3
|
import { dfs } from './traverse';
|
|
4
4
|
|
|
5
5
|
type TreeDataGetter = {
|
|
6
|
-
getNodeData?: (datum: TreeData) => NodeData;
|
|
6
|
+
getNodeData?: (datum: TreeData, depth: number) => NodeData;
|
|
7
7
|
getEdgeData?: (source: TreeData, target: TreeData) => EdgeData;
|
|
8
8
|
getChildren?: (datum: TreeData) => TreeData[];
|
|
9
9
|
};
|
|
@@ -32,8 +32,8 @@ export function treeToGraphData(treeData: TreeData, getter?: TreeDataGetter): Gr
|
|
|
32
32
|
|
|
33
33
|
dfs(
|
|
34
34
|
treeData,
|
|
35
|
-
(node) => {
|
|
36
|
-
nodes.push(getNodeData(node));
|
|
35
|
+
(node, depth) => {
|
|
36
|
+
nodes.push(getNodeData(node, depth));
|
|
37
37
|
const children = getChildren(node);
|
|
38
38
|
for (const child of children) {
|
|
39
39
|
edges.push(getEdgeData(node, child));
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '5.0.
|
|
1
|
+
export const version = '5.0.13';
|