Skip to content

Commit b6fd441

Browse files
committed
feat: add idGenerator global option
affects: @logicflow/core
1 parent edaf244 commit b6fd441

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

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: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,23 @@ export default class BaseNodeModel implements IBaseModel {
104104
if (!data.properties) {
105105
data.properties = {};
106106
}
107-
const id = this.createUniqueId();
108-
if (!data.id && id) {
109-
data.id = id;
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;
110114
}
115+
111116
this.formatText(data);
112117
assign(this, pickNodeConfig(data));
113118
}
114119

120+
createId() {
121+
return null;
122+
}
123+
115124
// 格式化text参数,未修改observable不作为action
116125
formatText(data): void {
117126
if (!data.text) {
@@ -247,10 +256,6 @@ export default class BaseNodeModel implements IBaseModel {
247256
return [];
248257
}
249258

250-
createUniqueId() {
251-
return null;
252-
}
253-
254259
@action
255260
move(deltaX, deltaY): void {
256261
this.x += deltaX;

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)