Skip to content

Commit 79b41a3

Browse files
Aarebeccarubbishmakerouxiaofeng.utlfhustcc
authored
feat: add hasNode/hasEdge/hasCombo API (#7345)
* feat: Add graph method (#7319) * feat: expose methods to determine whether there are nodes, edges, and combo to graph * feat: unit * docs: add docs --------- Co-authored-by: ouxiaofeng.utlf <[email protected]> Co-authored-by: hustcc <[email protected]> Co-authored-by: Aaron <[email protected]> * test: fix test case * fix: fix types --------- Co-authored-by: xiaofeng ou <[email protected]> Co-authored-by: ouxiaofeng.utlf <[email protected]> Co-authored-by: hustcc <[email protected]>
1 parent 0a37c19 commit 79b41a3

File tree

6 files changed

+228
-7
lines changed

6 files changed

+228
-7
lines changed

packages/g6/__tests__/unit/runtime/graph/graph.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,48 @@ describe('Graph', () => {
4343
});
4444
});
4545

46+
it('hasNode', () => {
47+
expect(graph.hasNode('0')).toBe(true);
48+
expect(graph.hasNode('1')).toBe(true);
49+
50+
expect(graph.hasNode('non-existent-node')).toBe(false);
51+
expect(graph.hasNode('node-999')).toBe(false);
52+
53+
expect(graph.hasNode('')).toBe(false);
54+
expect(graph.hasNode(null as any)).toBe(false);
55+
expect(graph.hasNode(undefined as any)).toBe(false);
56+
});
57+
58+
it('hasEdge', () => {
59+
expect(graph.hasEdge('0-1')).toBe(true);
60+
61+
expect(graph.hasEdge('non-existent-edge')).toBe(false);
62+
expect(graph.hasEdge('edge-999')).toBe(false);
63+
64+
expect(graph.hasEdge('')).toBe(false);
65+
expect(graph.hasEdge(null as any)).toBe(false);
66+
expect(graph.hasEdge(undefined as any)).toBe(false);
67+
});
68+
69+
it('hasCombo', () => {
70+
graph.addComboData([
71+
{ id: 'combo-test-1', style: {} },
72+
{ id: 'combo-test-2', style: {} },
73+
]);
74+
75+
expect(graph.hasCombo('combo-test-1')).toBe(true);
76+
expect(graph.hasCombo('combo-test-2')).toBe(true);
77+
78+
expect(graph.hasCombo('non-existent-combo')).toBe(false);
79+
expect(graph.hasCombo('combo-999')).toBe(false);
80+
81+
expect(graph.hasCombo('')).toBe(false);
82+
expect(graph.hasCombo(null as any)).toBe(false);
83+
expect(graph.hasCombo(undefined as any)).toBe(false);
84+
85+
graph.removeComboData(['combo-test-1', 'combo-test-2']);
86+
});
87+
4688
it('setSize/getSize', () => {
4789
expect(graph.getSize()).toEqual([500, 500]);
4890
expect(createGraph({}).getSize()).toEqual([0, 0]);

packages/g6/src/runtime/graph.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,40 @@ export class Graph extends EventEmitter {
504504
public getData(): Required<GraphData> {
505505
return this.context.model.getData();
506506
}
507+
/**
508+
* <zh/> 判断图中是否存在指定节点
509+
* <en/> Determine whether a specified node exists in the graph
510+
* @param {ID} id
511+
* @returns {boolean}
512+
* @remarks <zh/> 判断图中是否存在指定节点,避免在不存在的节点上进行操作
513+
* <en/> Determine whether a specified node exists in the graph and avoid operating on non-existent nodes
514+
*/
515+
public hasNode(id: ID): boolean {
516+
return this.context.model.hasNode(id);
517+
}
518+
/**
519+
* <zh/> 判断图中是否存在指定边
520+
* <en/> Determine whether a specified edge exists in the graph
521+
* @param {ID} id
522+
* @returns {boolean}
523+
* @remarks <zh/> 判断图中是否存在指定边,避免在不存在的边上进行操作
524+
* <en/> Determine whether a specified edge exists in the graph and avoid operating on non-existent edges
525+
*/
526+
public hasEdge(id: ID): boolean {
527+
return this.context.model.hasEdge(id);
528+
}
529+
530+
/**
531+
* <zh/> 判断图中是否存在指定组合
532+
* <en/> Determine whether a specified combo exists in the graph
533+
* @param {ID} id
534+
* @returns {boolean}
535+
* @remarks <zh/> 判断图中是否存在指定组合,避免在不存在的组合上进行操作
536+
* <en/> Determine whether a specified combo exists in the graph and avoid operating on non-existent combos
537+
*/
538+
public hasCombo(id: ID): boolean {
539+
return this.context.model.hasCombo(id);
540+
}
507541

508542
/**
509543
* <zh/> 获取单个元素数据

packages/g6/src/runtime/viewport.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { AABB, ICamera } from '@antv/g';
22
import { clamp, isNumber, pick } from '@antv/util';
33
import { AnimationType, GraphEvent } from '../constants';
4-
import type { FitViewOptions, ID, Point, TransformOptions, Vector2, ViewportAnimationEffectTiming } from '../types';
4+
import type {
5+
FitViewOptions,
6+
ID,
7+
Point,
8+
TransformOptions,
9+
Vector2,
10+
Vector3,
11+
ViewportAnimationEffectTiming,
12+
} from '../types';
513
import type { Element } from '../types/element';
614
import { getAnimationOptions } from '../utils/animation';
715
import { getBBoxSize, getCombinedBBox, getExpandedBBox, isBBoxInside, isPointInBBox } from '../utils/bbox';
@@ -131,8 +139,8 @@ export class ViewportController {
131139

132140
return mode === 'relative'
133141
? {
134-
position: add(position, delta),
135-
focalPoint: add(focalPoint, delta),
142+
position: add(position as Vector3, delta),
143+
focalPoint: add(focalPoint as Vector3, delta),
136144
}
137145
: {
138146
position: add([cx, cy, position[2]], delta),

packages/g6/src/utils/element.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export function isSimplePort(portStyle: NodePortStyleProps): boolean {
148148
* @returns <zh/> 连接桩的位置 | <en/> Port Position
149149
*/
150150
export function getPortPosition(port: Port): Point {
151-
return isPoint(port) ? port : port.getPosition();
151+
return isPoint(port) ? port : (port.getPosition() as Point);
152152
}
153153

154154
/**
@@ -246,7 +246,7 @@ export function getPortConnectionPoint(port: Port, opposite: Node | Port): Point
246246
if (isPoint(port)) return port;
247247

248248
// 1. linkToCenter 为 true,则返回连接桩的中心 | If linkToCenter is true, return the center of the port
249-
if (port.attributes.linkToCenter) return port.getPosition();
249+
if (port.attributes.linkToCenter) return port.getPosition() as Point;
250250

251251
// 2. 推导对端的具体点:如果是连接桩或节点,则返回它的中心;如果是具体点,则直接返回
252252
// 2. Get a specific opposite point: if it is a port or a node, return its center; if it is a specific point, return directly
@@ -257,7 +257,7 @@ export function getPortConnectionPoint(port: Port, opposite: Node | Port): Point
257257
: opposite.getPosition();
258258

259259
// 3. 返回连接桩边界上的交点 | Return the intersection point on the port boundary
260-
return getEllipseIntersectPoint(oppositePosition, port.getBounds());
260+
return getEllipseIntersectPoint(oppositePosition as Point, port.getBounds());
261261
}
262262

263263
/**
@@ -275,7 +275,7 @@ export function getNodeConnectionPoint(nodeLike: Node | Combo, opposite: Node |
275275
? opposite
276276
: isNode(opposite)
277277
? opposite.getCenter()
278-
: opposite.getPosition();
278+
: (opposite.getPosition() as Point);
279279
return nodeLike.getIntersectPoint(oppositePosition) || nodeLike.getCenter();
280280
}
281281

packages/site/docs/api/data.en.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,74 @@ updateComboData(data: (ComboData[] | ((prev: ComboData[]) => ComboData[]))): voi
806806
graph.updateComboData([{ id: 'combo-1', style: { x: 100, y: 100 } }]);
807807
```
808808

809+
### Graph.hasNode()
810+
811+
Determine if an node exists
812+
813+
```typescript
814+
hasNode(id:ID): boolean;
815+
```
816+
817+
**参数**:
818+
| Parameter | Description | Type | Default | Required |
819+
| ---- | -------------------- | --------- | ------ | ---- |
820+
| id | Node ID to be judged | [ID](#id) | - ||
821+
822+
**返回值**:
823+
824+
- **类型**: boolean
825+
**示例**:
826+
827+
```typescript
828+
graph.hasNode('node-1');
829+
```
830+
831+
### Graph.hasEdge()
832+
833+
Determine if an edge exists
834+
835+
```typescript
836+
hasEdge(id:ID): boolean;
837+
```
838+
839+
**参数**:
840+
841+
| Parameter | Description | Type | Default | Required |
842+
| --------- | -------------------- | --------- | ------- | -------- |
843+
| id | Edge ID to be judged | [ID](#id) | - ||
844+
845+
**返回值**:
846+
847+
- **类型**: boolean
848+
**示例**:
849+
850+
```typescript
851+
graph.hasEdge('edge-1');
852+
```
853+
854+
### Graph.hasCombo()
855+
856+
Determine if combo exists
857+
858+
```typescript
859+
hasCombo(id:ID): boolean;
860+
```
861+
862+
**参数**:
863+
864+
| Parameter | Description | Type | Default | Required |
865+
| --------- | --------------------- | --------- | ------- | -------- |
866+
| id | Combo ID to be judged | [ID](#id) | - ||
867+
868+
**返回值**:
869+
870+
- **类型**: boolean
871+
**示例**:
872+
873+
```typescript
874+
graph.hasCombo('combo-1');
875+
```
876+
809877
## Type Definitions
810878

811879
### ID

packages/site/docs/api/data.zh.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,75 @@ updateComboData(data: (ComboData[] | ((prev: ComboData[]) => ComboData[]))): voi
806806
graph.updateComboData([{ id: 'combo-1', style: { x: 100, y: 100 } }]);
807807
```
808808

809+
### Graph.hasNode()
810+
811+
判断节点是否存在
812+
813+
```typescript
814+
hasNode(id:ID): boolean;
815+
```
816+
817+
**参数**:
818+
819+
| 参数 | 描述 | 类型 | 默认值 | 必选 |
820+
| ---- | -------------------- | --------- | ------ | ---- |
821+
| id | 需要进行判断的节点id | [ID](#id) | - ||
822+
823+
**返回值**:
824+
825+
- **类型**: boolean
826+
**示例**:
827+
828+
```typescript
829+
graph.hasNode('node-1');
830+
```
831+
832+
### Graph.hasEdge()
833+
834+
判断边是否存在
835+
836+
```typescript
837+
hasEdge(id:ID): boolean;
838+
```
839+
840+
**参数**:
841+
842+
| 参数 | 描述 | 类型 | 默认值 | 必选 |
843+
| ---- | ------------------ | --------- | ------ | ---- |
844+
| id | 需要进行判断的边id | [ID](#id) | - ||
845+
846+
**返回值**:
847+
848+
- **类型**: boolean
849+
**示例**:
850+
851+
```typescript
852+
graph.hasEdge('edge-1');
853+
```
854+
855+
### Graph.hasCombo()
856+
857+
判断combo是否存在
858+
859+
```typescript
860+
hasCombo(id:ID): boolean;
861+
```
862+
863+
**参数**:
864+
865+
| 参数 | 描述 | 类型 | 默认值 | 必选 |
866+
| ---- | ------------------------- | --------- | ------ | ---- |
867+
| id | 需要进行判断的combo组合id | [ID](#id) | - ||
868+
869+
**返回值**:
870+
871+
- **类型**: boolean
872+
**示例**:
873+
874+
```typescript
875+
graph.hasCombo('combo-1');
876+
```
877+
809878
## 类型定义
810879

811880
### ID

0 commit comments

Comments
 (0)