Skip to content

Commit 5f16642

Browse files
authored
Merge pull request #223 from yhlchao/master
feat: add createId API
2 parents 69686de + b6fd441 commit 5f16642

File tree

6 files changed

+187
-21
lines changed

6 files changed

+187
-21
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@typescript-eslint/eslint-plugin": "^3.6.1",
2929
"@typescript-eslint/parser": "^3.2.0",
3030
"@vuepress-reco/vuepress-plugin-back-to-top": "^1.6.0",
31+
"commitizen": "^4.2.4",
3132
"cz-lerna-changelog": "^2.0.3",
3233
"eslint": "^7.0.0",
3334
"eslint-config-airbnb-typescript": "^9.0.0",

packages/core/src/model/GraphModel.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class GraphModel {
3737
topElement: BaseNodeModel | BaseEdgeModel; // 当前位于顶部的元素
3838
selectElement: BaseNodeModel | BaseEdgeModel; // 当前位于顶部的元素
3939
selectElements = new Map<string, BaseElementModel>(); // 多选
40+
idGenerator: () => number | string;
4041
@observable selectElementSize = 0;
4142
@observable edgeType: string;
4243
@observable nodes: BaseNodeModel[] = [];
@@ -61,6 +62,7 @@ class GraphModel {
6162
grid: { size = 1 } = {},
6263
isSilentMode = false,
6364
eventCenter,
65+
idGenerator,
6466
} = config;
6567
this.background = background;
6668
this.isSlient = isSilentMode;
@@ -73,6 +75,7 @@ class GraphModel {
7375
this.width = config.width;
7476
this.height = config.height;
7577
this.partial = config.partial;
78+
this.idGenerator = idGenerator;
7679
}
7780
@computed get nodesMap(): { [key: string]: { index: number, model: BaseNodeModel } } {
7881
return this.nodes.reduce((nMap, model, index) => {

packages/core/src/model/edge/BaseEdgeModel.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,22 @@ class BaseEdgeModel implements IBaseModel {
9090
if (!data.properties) {
9191
data.properties = {};
9292
}
93+
94+
if (!data.id) {
95+
const { idGenerator } = this.graphModel;
96+
const globalId = idGenerator && idGenerator();
97+
if (globalId) data.id = globalId;
98+
const nodeId = this.createId();
99+
if (nodeId) data.id = nodeId;
100+
}
101+
93102
assign(this, pickEdgeConfig(data));
94103
}
95104

105+
createId() {
106+
return null;
107+
}
108+
96109
setAttributes() { }
97110

98111
@computed get sourceNode() {

packages/core/src/model/node/BaseNodeModel.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,23 @@ export default class BaseNodeModel implements IBaseModel {
104104
if (!data.properties) {
105105
data.properties = {};
106106
}
107+
108+
if (!data.id) {
109+
const { idGenerator } = this.graphModel;
110+
const globalId = idGenerator && idGenerator();
111+
if (globalId) data.id = globalId;
112+
const nodeId = this.createId();
113+
if (nodeId) data.id = nodeId;
114+
}
115+
107116
this.formatText(data);
108117
assign(this, pickNodeConfig(data));
109118
}
110119

120+
createId() {
121+
return null;
122+
}
123+
111124
// 格式化text参数,未修改observable不作为action
112125
formatText(data): void {
113126
if (!data.text) {

packages/core/src/options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export type Definition = {
5454
hideAnchors?: boolean; // 是否隐藏anchor
5555

5656
hoverOutline?: boolean; // 是否显示节点hover时的outline
57+
58+
idGenerator?: () => number | string; // 元素id生成器
5759
};
5860

5961
export interface GuardsTypes {

0 commit comments

Comments
 (0)