Skip to content

Commit 07c77ee

Browse files
authored
docs: impove behavior comments and add api interactive demos (#5715)
* docs: add reference page title * docs: refine behavior api interactive demos
1 parent 1cee4a5 commit 07c77ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1140
-242
lines changed

packages/g6/src/behaviors/brush-select.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ export class BrushSelect extends BaseBehavior<BrushSelectOptions> {
137137
this.bindEvents();
138138
}
139139

140+
/**
141+
* Triggered when the pointer is pressed
142+
* @param event - Pointer event
143+
* @internal
144+
*/
140145
protected onPointerDown(event: IPointerEvent) {
141146
if (!this.validate(event) || !this.isKeydown() || this.startPoint) return;
142147
const { canvas } = this.context;
@@ -147,6 +152,11 @@ export class BrushSelect extends BaseBehavior<BrushSelectOptions> {
147152
this.startPoint = [event.canvas.x, event.canvas.y];
148153
}
149154

155+
/**
156+
* Triggered when the pointer is moved
157+
* @param event - Pointer event
158+
* @internal
159+
*/
150160
protected onPointerMove(event: IPointerEvent) {
151161
if (!this.startPoint) return;
152162
const { immediately, mode } = this.options;
@@ -163,6 +173,11 @@ export class BrushSelect extends BaseBehavior<BrushSelectOptions> {
163173
if (immediately && mode === 'default') this.updateElementsStates(getBoundingPoints(this.startPoint, this.endPoint));
164174
}
165175

176+
/**
177+
* Triggered when the pointer is released
178+
* @param event - Pointer event
179+
* @internal
180+
*/
166181
protected onPointerUp(event: IPointerEvent) {
167182
if (!this.startPoint) return;
168183
if (!this.endPoint) {

packages/g6/src/behaviors/click-element.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ export interface ClickElementOptions extends BaseBehaviorOptions {
2828
*
2929
* <en/> Whether to enable the function of clicking the element
3030
* @defaultValue true
31+
* @remarks
32+
* <zh/> 可以通过函数的方式动态控制是否启用,例如只有节点被选中时才启用。
33+
*
34+
* <en/> Whether to enable can be dynamically controlled by functions, such as only when nodes are selected.
35+
*
36+
* ```json
37+
* { enable: event => event.targetType === 'node'}
38+
* ```
3139
*/
3240
enable?: boolean | ((event: IPointerEvent) => boolean);
3341
/**
@@ -64,19 +72,13 @@ export interface ClickElementOptions extends BaseBehaviorOptions {
6472
* <en/> The degree to determine the scope of influence
6573
* @defaultValue 0
6674
* @remarks
67-
* <zh/> 对于节点来说,
68-
* - 0 表示只选中当前节点
69-
* - 1 表示选中当前节点及其直接相邻的节点和边,以此类推
70-
* 对于边来说,
71-
* - 0 表示只选中当前边
72-
* - 1 表示选中当前边及其直接相邻的节点,以此类推
75+
* <zh/> 对于节点来说,`0` 表示只选中当前节点,`1` 表示选中当前节点及其直接相邻的节点和边,以此类推。
7376
*
74-
* <en/> For nodes,
75-
* - 0 means only the current node is selected
76-
* - 1 means the current node and its directly adjacent nodes and edges are selected, etc
77-
* For edges,
78-
* - 0 means only the current edge is selected
79-
* - 1 means the current edge and its directly adjacent nodes are selected, etc
77+
* <zh/> 对于边来说,`0` 表示只选中当前边,`1` 表示选中当前边及其直接相邻的节点,以此类推。
78+
*
79+
* <en/> For nodes, `0` means only the current node is selected, `1` means the current node and its directly adjacent nodes and edges are selected, etc.
80+
*
81+
* <en/> For edges, `0 `means only the current edge is selected,`1` means the current edge and its directly adjacent nodes are selected, etc.
8082
*/
8183
degree?: number;
8284
}
@@ -86,11 +88,9 @@ export interface ClickElementOptions extends BaseBehaviorOptions {
8688
*
8789
* <en/> Click Element
8890
* @remarks
89-
* ```typescript
90-
* const graph = new Graph({
91-
* behaviors: ['click-element'],
92-
* });
93-
* ```
91+
* <zh/> 当鼠标点击元素时,可以激活元素的状态,例如选中节点或边。当 degree 设置为 `1` 时,点击节点会高亮当前节点及其直接相邻的节点和边。
92+
*
93+
* <en/> When the mouse clicks on an element, you can activate the state of the element, such as selecting nodes or edges. When the degree is 1, clicking on a node will highlight the current node and its directly adjacent nodes and edges.
9494
*/
9595
export class ClickElement extends BaseBehavior<ClickElementOptions> {
9696
private selectedElementIds: ID[] = [];

packages/g6/src/behaviors/collapse-expand.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ import { isElement } from '../utils/element';
66
import type { BaseBehaviorOptions } from './base-behavior';
77
import { BaseBehavior } from './base-behavior';
88

9+
/**
10+
* <zh/> 展开/收起组合元素交互配置项
11+
*
12+
* <en/> Collapse/Expand combo behavior options
13+
*/
914
export interface CollapseExpandOptions extends BaseBehaviorOptions {
1015
/**
1116
* <zh/> 是否启用动画
1217
*
1318
* <en/> Whether to enable animation
1419
* @defaultValue true
1520
*/
16-
animation: boolean;
21+
animation?: boolean;
1722
/**
1823
* <zh/> 是否启用展开/收起功能
1924
*
@@ -34,10 +39,15 @@ export interface CollapseExpandOptions extends BaseBehaviorOptions {
3439
*/
3540
onExpand?: (id: ID) => void;
3641
}
42+
3743
/**
38-
* <zh/> 展开/收起
44+
* <zh/> 展开/收起组合元素交互
45+
*
46+
* <en/> Collapse/Expand Combo behavior
47+
* @remarks
48+
* <zh/> 通过双击 Combo 进行展开或收起 Combo。
3949
*
40-
* <en/> Collapse/Expand
50+
* <en/> Expand or collapse a Combo by double-clicking on it.
4151
*/
4252
export class CollapseExpand extends BaseBehavior<CollapseExpandOptions> {
4353
static defaultOptions: Partial<CollapseExpandOptions> = {

packages/g6/src/behaviors/create-edge.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,30 @@ import { CommonEvent } from '../constants';
33
import type { RuntimeContext } from '../runtime/types';
44
import type { EdgeData } from '../spec';
55
import type { EdgeStyle } from '../spec/element/edge';
6-
import type { Element, ID, IPointerEvent } from '../types';
6+
import type { ID, IElementEvent, IPointerEvent } from '../types';
77
import type { BaseBehaviorOptions } from './base-behavior';
88
import { BaseBehavior } from './base-behavior';
99

1010
const ASSIST_EDGE_ID = 'g6-create-edge-assist-edge-id';
1111
const ASSIST_NODE_ID = 'g6-create-edge-assist-node-id';
1212

13+
/**
14+
* <zh/> 创建边交互配置项
15+
*
16+
* <en/> Create edge behavior options
17+
*/
1318
export interface CreateEdgeOptions extends BaseBehaviorOptions {
1419
/**
15-
* <zh/> 是否启用悬浮元素的功能
20+
* <zh/> 是否启用创建边的功能
1621
*
17-
* <en/> Whether to enable hover element function
22+
* <en/> Whether to enable the function of creating edges
1823
* @defaultValue true
1924
*/
2025
enable?: boolean | ((event: IPointerEvent) => boolean);
2126
/**
22-
* <zh/> 边配置
27+
* <zh/> 新建边的样式配置
2328
*
24-
* <en/> edge config
29+
* <en/> Style configs of the new edge
2530
*/
2631
style?: EdgeStyle;
2732
/**
@@ -45,6 +50,15 @@ export interface CreateEdgeOptions extends BaseBehaviorOptions {
4550
onCreate?: (edge: EdgeData) => EdgeData;
4651
}
4752

53+
/**
54+
* <zh/> 创建边交互
55+
*
56+
* <en/> Create edge behavior
57+
* @remarks
58+
* <zh/> 通过拖拽或点击节点创建边,支持自定义边样式。
59+
*
60+
* <en/> Create edges by dragging or clicking nodes, and support custom edge styles.
61+
*/
4862
export class CreateEdge extends BaseBehavior<CreateEdgeOptions> {
4963
static defaultOptions: Partial<CreateEdgeOptions> = {
5064
animation: true,
@@ -62,6 +76,11 @@ export class CreateEdge extends BaseBehavior<CreateEdgeOptions> {
6276
this.bindEvents();
6377
}
6478

79+
/**
80+
* Update options
81+
* @param options - The options to update
82+
* @internal
83+
*/
6584
public update(options: Partial<CreateEdgeOptions>): void {
6685
super.update(options);
6786
this.bindEvents();
@@ -203,5 +222,3 @@ export class CreateEdge extends BaseBehavior<CreateEdgeOptions> {
203222
super.destroy();
204223
}
205224
}
206-
207-
type IElementEvent = IPointerEvent<Element>;

packages/g6/src/behaviors/drag-canvas.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ export interface DragCanvasOptions extends BaseBehaviorOptions {
3333
*
3434
* <en/> The way to trigger dragging, default to dragging with the pointer pressed
3535
*/
36-
trigger?: CombinationKey;
36+
trigger?: {
37+
up: ShortcutKey;
38+
down: ShortcutKey;
39+
left: ShortcutKey;
40+
right: ShortcutKey;
41+
};
3742
/**
3843
* <zh/> 触发一次按键移动的距离
3944
*
@@ -49,13 +54,11 @@ export interface DragCanvasOptions extends BaseBehaviorOptions {
4954
onFinish?: () => void;
5055
}
5156

52-
type CombinationKey = {
53-
up: ShortcutKey;
54-
down: ShortcutKey;
55-
left: ShortcutKey;
56-
right: ShortcutKey;
57-
};
58-
57+
/**
58+
* <zh/> 拖拽画布交互
59+
*
60+
* <en/> Drag canvas behavior
61+
*/
5962
export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
6063
static defaultOptions: Partial<DragCanvasOptions> = {
6164
enable: true,
@@ -76,6 +79,18 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
7679
context.canvas.setCursor('grab');
7780
}
7881

82+
/**
83+
* <zh/> 更新配置
84+
*
85+
* <en/> Update options
86+
* @param options - <zh/> 配置项 | <en/> Options
87+
* @internal
88+
*/
89+
public update(options: Partial<DragCanvasOptions>): void {
90+
super.update(options);
91+
this.bindEvents();
92+
}
93+
7994
private bindEvents() {
8095
const { trigger } = this.options;
8196
this.shortcut.unbindAll();
@@ -110,6 +125,14 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
110125
this.options.onFinish?.();
111126
}
112127

128+
/**
129+
* <zh/> 平移画布
130+
*
131+
* <en/> Translate canvas
132+
* @param offset - <zh/> 平移距离 | <en/> Translation distance
133+
* @param animation - <zh/> 动画配置 | <en/> Animation configuration
134+
* @internal
135+
*/
113136
protected async translate(offset: Vector2, animation?: ViewportAnimationEffectTiming) {
114137
await this.context.graph.translateBy(offset, animation);
115138
}

packages/g6/src/behaviors/drag-element-force.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
1-
import type { Element, ID, IDragEvent, Point } from '../types';
1+
import type { ID, IElementDragEvent, Point } from '../types';
22
import { idOf } from '../utils/id';
33
import { getLayoutProperty, invokeLayoutMethod } from '../utils/layout';
44
import { add } from '../utils/vector';
55
import type { DragElementOptions } from './drag-element';
66
import { DragElement } from './drag-element';
77

8+
/**
9+
* <zh/> 调用力导布局拖拽元素交互配置项
10+
*
11+
* <en/> Call d3-force layout to drag element behavior options
12+
*/
813
export interface DragElementForceOptions extends Omit<DragElementOptions, 'animation' | 'dropEffect' | 'shadow'> {}
914

15+
/**
16+
* <zh/> 调用力导布局拖拽元素的交互
17+
*
18+
* <en/> Call d3-force layout to drag element behavior
19+
* @remarks
20+
* <zh/> 只能在使用 d3-force 布局时使用该交互,在拖拽过程中会实时重新计算布局。
21+
*
22+
* <en/> This behavior can only be used with d3-force layout. The layout will be recalculated in real time during dragging.
23+
*/
1024
export class DragElementForce extends DragElement {
1125
private get forceLayoutInstance() {
1226
return this.context.layout!.getLayoutInstance().find((layout) => ['d3-force', 'd3-force-3d'].includes(layout?.id));
1327
}
1428

15-
protected validate(event: IDragEvent<Element>): boolean {
29+
/**
30+
* Whether the behavior is enabled
31+
* @param event - The event object
32+
* @returns Is the behavior enabled
33+
* @internal
34+
*/
35+
protected validate(event: IElementDragEvent): boolean {
1636
if (!this.context.layout) return false;
1737

1838
// 未使用力导布局 / The force layout is not used
@@ -24,6 +44,12 @@ export class DragElementForce extends DragElement {
2444
return super.validate(event);
2545
}
2646

47+
/**
48+
* Move selected elements by offset
49+
* @param ids - The selected element IDs
50+
* @param offset - The offset to move
51+
* @internal
52+
*/
2753
protected async moveElement(ids: ID[], offset: Point) {
2854
const layout = this.forceLayoutInstance;
2955
this.context.graph.getNodeData(ids).forEach((element, index) => {
@@ -32,7 +58,12 @@ export class DragElementForce extends DragElement {
3258
});
3359
}
3460

35-
protected onDragStart(event: IDragEvent<Element>) {
61+
/**
62+
* Triggered when the drag starts
63+
* @param event - The event object
64+
* @internal
65+
*/
66+
protected onDragStart(event: IElementDragEvent) {
3667
this.enable = this.validate(event);
3768
if (!this.enable) return;
3869

@@ -49,13 +80,22 @@ export class DragElementForce extends DragElement {
4980
});
5081
}
5182

52-
protected onDrag(event: IDragEvent<Element>) {
83+
/**
84+
* Triggered when dragging
85+
* @param event - The event object
86+
* @internal
87+
*/
88+
protected onDrag(event: IElementDragEvent) {
5389
if (!this.enable) return;
5490

5591
const delta = this.getDelta(event);
5692
this.moveElement(this.target, delta);
5793
}
5894

95+
/**
96+
* Triggered when the drag ends
97+
* @internal
98+
*/
5999
protected onDragEnd() {
60100
const layout = this.forceLayoutInstance;
61101
if (layout) getLayoutProperty(layout, 'simulation').alphaTarget(0);

0 commit comments

Comments
 (0)