-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathBaseNodeModel.ts
More file actions
660 lines (633 loc) · 16.2 KB
/
BaseNodeModel.ts
File metadata and controls
660 lines (633 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
import { assign, cloneDeep, isNil } from 'lodash-es';
import { observable, action, toJS, isObservable, computed } from '../../util/mobx';
import { createUuid } from '../../util/uuid';
import { OutlineTheme } from '../../constant/DefaultTheme';
import {
ModelType, ElementType, OverlapMode,
} from '../../constant/constant';
import {
AdditionData,
NodeData,
NodeConfig,
NodeMoveRule,
Bounds,
AnchorConfig,
PointAnchor,
AnchorsOffsetItem,
PointTuple,
ShapeStyleAttribute,
IsAllowMove,
Point,
AnchorInfo,
} from '../../type';
import GraphModel from '../GraphModel';
import { IBaseModel } from '../BaseModel';
import { formatData } from '../../util/compatible';
import { getClosestAnchor, pickNodeConfig } from '../../util/node';
import { getZIndex } from '../../util/zIndex';
import { BaseEdgeModel } from '../edge';
import { AdjustType } from '../../view/edge/AdjustPoint';
export type ConnectRule = {
message: string;
validate: (
source?: BaseNodeModel,
target?: BaseNodeModel,
sourceAnchor?: AnchorConfig,
targetAnchor?: AnchorConfig,
// 调整的边的id,在开启adjustEdgeStartAndEnd后调整边连接的节点时会传入,见https://github.com/didi/LogicFlow/issues/926#issuecomment-1371823306
edgeId?: string,
) => boolean;
};
export type ConnectRuleResult = {
isAllPass: boolean;
msg?: string;
};
interface IBaseNodeModel extends IBaseModel {
/**
* model基础类型,固定为node
*/
readonly BaseType: ElementType.NODE,
}
export { BaseNodeModel };
export default class BaseNodeModel implements IBaseNodeModel {
// 数据属性
id = '';
@observable type = '';
@observable x = 0;
@observable y = 0;
@observable text = {
value: '',
x: 0,
y: 0,
draggable: false,
editable: true,
};
@observable properties: Record<string, any> = {};
// 形状属性
@observable private _width = 100;
public get width() {
return this._width;
}
public set width(value) {
this._width = value;
}
@observable private _height = 80;
public get height() {
return this._height;
}
public set height(value) {
this._height = value;
}
@observable anchorsOffset: AnchorsOffsetItem[] = []; // 根据与(x, y)的偏移量计算anchors的坐标
// 状态属性
@observable isSelected = false;
@observable isHovered = false;
@observable isShowAnchor = false;
@observable isDragging = false;
@observable isHitable = true; // 细粒度控制节点是否对用户操作进行反应
@observable draggable = true;
@observable visible = true;
virtual = false;
// 其它属性
graphModel: GraphModel;
@observable zIndex = 1;
@observable state = 1;
@observable autoToFront = true; // 节点选中时是否自动置顶,默认为true.
@observable style: ShapeStyleAttribute = { }; // 每个节点自己的样式,动态修改
readonly BaseType = ElementType.NODE;
modelType = ModelType.NODE;
additionStateData: AdditionData;
targetRules: ConnectRule[] = [];
sourceRules: ConnectRule[] = [];
moveRules: NodeMoveRule[] = []; // 节点移动之前的hook
hasSetTargetRules = false; // 用来限制rules的重复值
hasSetSourceRules = false; // 用来限制rules的重复值
[propName: string]: any; // 支持自定义
constructor(data: NodeConfig, graphModel: GraphModel) {
this.graphModel = graphModel;
this.initNodeData(data);
this.setAttributes();
}
/**
* 获取进入当前节点的边和节点
*/
@computed get incoming(): { nodes: BaseNodeModel[], edges: BaseEdgeModel[] } {
return {
nodes: this.graphModel.getNodeIncomingNode(this.id),
edges: this.graphModel.getNodeIncomingEdge(this.id),
};
}
/*
* 获取离开当前节点的边和节点
*/
@computed get outgoing(): { nodes: BaseNodeModel[], edges: BaseEdgeModel[] } {
return {
nodes: this.graphModel.getNodeOutgoingNode(this.id),
edges: this.graphModel.getNodeOutgoingEdge(this.id),
};
}
/**
* @overridable 可以重写
* 初始化节点数据
* initNodeData和setAttributes的区别在于
* initNodeData只在节点初始化的时候调用,用于初始化节点的所有属性。
* setAttributes除了初始化调用外,还会在properties发生变化了调用。
*/
public initNodeData(data) {
if (!data.properties) {
data.properties = {};
}
if (!data.id) {
// 自定义节点id > 全局定义id > 内置
const { idGenerator } = this.graphModel;
const globalId = idGenerator && idGenerator(data.type);
const nodeId = this.createId();
data.id = nodeId || globalId || createUuid();
}
this.formatText(data);
assign(this, pickNodeConfig(data));
const { overlapMode } = this.graphModel;
if (overlapMode === OverlapMode.INCREASE) {
this.zIndex = data.zIndex || getZIndex();
}
}
/**
* 设置model属性,每次properties发生变化会触发
* 例如设置节点的宽度
* @example
*
* setAttributes () {
* this.width = 300
* this.height = 200
* }
*
* @overridable 支持重写
*/
public setAttributes() {}
/**
* @overridable 支持重写,自定义此类型节点默认生成方式
* @returns string
*/
public createId(): string {
return null;
}
/**
* 初始化文本属性
*/
private formatText(data): void {
if (!data.text) {
data.text = {
value: '',
x: data.x,
y: data.y,
draggable: false,
editable: true,
};
}
if (data.text && typeof data.text === 'string') {
data.text = {
value: data.text,
x: data.x,
y: data.y,
draggable: false,
editable: true,
};
} else if (data.text && data.text.editable === undefined) {
data.text.editable = true;
}
}
/**
* 获取被保存时返回的数据
* @overridable 支持重写
*/
getData(): NodeData {
const { x, y, value } = this.text;
let { properties } = this;
if (isObservable(properties)) {
properties = toJS(properties);
}
const data: NodeData = {
id: this.id,
type: this.type,
x: this.x,
y: this.y,
properties,
};
if (this.graphModel.overlapMode === OverlapMode.INCREASE) {
data.zIndex = this.zIndex;
}
if (value) {
data.text = {
x,
y,
value,
};
}
return data;
}
/**
* 用于在历史记录时获取节点数据,
* 在某些情况下,如果希望某个属性变化不引起history的变化,
* 可以重写此方法。
*/
getHistoryData(): NodeData {
return this.getData();
}
/**
* 获取当前节点的properties
*/
getProperties() {
return toJS(this.properties);
}
/**
* @overridable 支持重写
* 获取当前节点样式
* @returns 自定义节点样式
*/
getNodeStyle(): ShapeStyleAttribute {
return {
...this.graphModel.theme.baseNode,
...this.style,
};
}
/**
* @overridable 支持重写
* 获取当前节点文本样式
*/
getTextStyle() {
// 透传 nodeText
const { nodeText } = this.graphModel.theme;
return cloneDeep(nodeText);
}
/**
* @overridable 支持重写
* 获取当前节点锚点样式
* @returns 自定义样式
*/
getAnchorStyle(anchorInfo): Record<string, any> {
const { anchor } = this.graphModel.theme;
// 防止被重写覆盖主题。
return cloneDeep(anchor);
}
/**
* @overridable 支持重写
* 获取当前节点锚点拖出连线样式
* @returns 自定义锚点拖出样式
*/
getAnchorLineStyle(anchorInfo) {
const { anchorLine } = this.graphModel.theme;
return cloneDeep(anchorLine);
}
/**
* @overridable 支持重写
* 获取outline样式,重写可以定义此类型节点outline样式, 默认使用主题样式
* @returns 自定义outline样式
*/
getOutlineStyle(): OutlineTheme {
const { outline } = this.graphModel.theme;
return cloneDeep(outline);
}
/**
* @over
* 在边的时候,是否允许这个节点为source节点,边到target节点。
*
* @param edgeId 调整的边的id,在开启adjustEdgeStartAndEnd后调整边连接的节点时会传入,见https://github.com/didi/LogicFlow/issues/926#issuecomment-1371823306
*/
isAllowConnectedAsSource(
target: BaseNodeModel,
sourceAnchor: AnchorConfig,
targetAnchor: AnchorConfig,
edgeId?: string,
): ConnectRuleResult | Boolean {
const rules = !this.hasSetSourceRules
? this.getConnectedSourceRules()
: this.sourceRules;
this.hasSetSourceRules = true;
let isAllPass = true;
let msg: string;
for (let i = 0; i < rules.length; i++) {
const rule = rules[i];
if (!rule.validate.call(this, this, target, sourceAnchor, targetAnchor, edgeId)) {
isAllPass = false;
msg = rule.message;
break;
}
}
return {
isAllPass,
msg,
};
}
/**
* 获取当前节点作为连接的起始节点规则。
*/
getConnectedSourceRules(): ConnectRule[] {
return this.sourceRules;
}
/**
* 在连线的时候,是否允许这个节点为target节点
*
* @param edgeId 调整的边的id,在开启adjustEdgeStartAndEnd后调整边连接的节点时会传入,见https://github.com/didi/LogicFlow/issues/926#issuecomment-1371823306
*/
isAllowConnectedAsTarget(
source: BaseNodeModel,
sourceAnchor: AnchorConfig,
targetAnchor: AnchorConfig,
edgeId?: string,
): ConnectRuleResult | Boolean {
const rules = !this.hasSetTargetRules
? this.getConnectedTargetRules()
: this.targetRules;
this.hasSetTargetRules = true;
let isAllPass = true;
let msg: string;
for (let i = 0; i < rules.length; i++) {
const rule = rules[i];
if (!rule.validate.call(this, source, this, sourceAnchor, targetAnchor, edgeId)) {
isAllPass = false;
msg = rule.message;
break;
}
}
return {
isAllPass,
msg,
};
}
/**
* 内部方法
* 是否允许移动节点到新的位置
*/
isAllowMoveNode(deltaX, deltaY): boolean | IsAllowMove {
let isAllowMoveX = true;
let isAllowMoveY = true;
const rules = this.moveRules.concat(this.graphModel.nodeMoveRules);
for (const rule of rules) {
const r = rule(this, deltaX, deltaY);
if (!r) return false;
if (
typeof r === 'object'
) {
const r1 = r as IsAllowMove;
if (r1.x === false && r1.y === false) {
return false;
}
isAllowMoveX = isAllowMoveX && r1.x;
isAllowMoveY = isAllowMoveY && r1.y;
}
}
return {
x: isAllowMoveX,
y: isAllowMoveY,
};
}
/**
* 获取作为连线终点时的所有规则。
*/
getConnectedTargetRules(): ConnectRule[] {
return this.targetRules;
}
/**
* @returns Point[] 锚点坐标构成的数组
*/
getAnchorsByOffset(): PointAnchor[] {
const {
anchorsOffset,
id,
x,
y,
} = this;
if (anchorsOffset && anchorsOffset.length > 0) {
return anchorsOffset.map((el, idx) => {
if (el.length) {
el = el as PointTuple; // 历史数据格式
return {
id: `${id}_${idx}`,
x: x + el[0],
y: y + el[1],
};
}
el = el as PointAnchor;
return {
...el,
x: x + el.x,
y: y + el.y,
id: el.id || `${id}_${idx}`,
};
});
}
return this.getDefaultAnchor();
}
/**
* @overridable 子类重写此方法设置默认锚点
* 获取节点默认情况下的锚点
*/
public getDefaultAnchor(): PointAnchor[] {
return [];
}
/**
* @overridable 子类重写此方法获取手动连接边到节点时,需要连接的锚点
* 手动连接边到节点时,需要连接的锚点
*/
public getTargetAnchor(position: Point, type: AdjustType): AnchorInfo {
return getClosestAnchor(position, this);
}
/**
* 获取节点BBox
*/
public getBounds(): Bounds {
return {
x1: this.x - this.width / 2,
y1: this.y - this.height / 2,
x2: this.x + this.width / 2,
y2: this.y + this.height / 2,
};
}
get anchors(): PointAnchor[] {
return this.getAnchorsByOffset();
}
getAnchorInfo(anchorId: string) {
if (isNil(anchorId)) return;
for (let i = 0; i < this.anchors.length; i++) {
const anchor = this.anchors[i];
if (anchor.id === anchorId) {
return anchor;
}
}
}
@action
addNodeMoveRules(fn: NodeMoveRule) {
if (!this.moveRules.includes(fn)) {
this.moveRules.push(fn);
}
}
@action
move(deltaX, deltaY, isIgnoreRule = false): boolean {
let isAllowMoveX = false;
let isAllowMoveY = false;
if (isIgnoreRule) {
isAllowMoveX = true;
isAllowMoveY = true;
} else {
const r = this.isAllowMoveNode(deltaX, deltaY);
if (typeof r === 'boolean') {
isAllowMoveX = r;
isAllowMoveY = r;
} else {
isAllowMoveX = r.x;
isAllowMoveY = r.y;
}
}
if (isAllowMoveX) {
const targetX = this.x + deltaX;
this.x = targetX;
this.text && this.moveText(deltaX, 0);
}
if (isAllowMoveY) {
const targetY = this.y + deltaY;
this.y = targetY;
this.text && this.moveText(0, deltaY);
}
return isAllowMoveX || isAllowMoveY;
}
@action
getMoveDistance(deltaX: number, deltaY: number, isIgnoreRule = false) : [number, number] {
let isAllowMoveX = false;
let isAllowMoveY = false;
let moveX = 0;
let moveY = 0;
if (isIgnoreRule) {
isAllowMoveX = true;
isAllowMoveY = true;
} else {
const r = this.isAllowMoveNode(deltaX, deltaY);
if (typeof r === 'boolean') {
isAllowMoveX = r;
isAllowMoveY = r;
} else {
isAllowMoveX = r.x;
isAllowMoveY = r.y;
}
}
if (isAllowMoveX && deltaX) {
const targetX = this.x + deltaX;
this.x = targetX;
this.text && this.moveText(deltaX, 0);
moveX = deltaX;
}
if (isAllowMoveY && deltaY) {
const targetY = this.y + deltaY;
this.y = targetY;
this.text && this.moveText(0, deltaY);
moveY = deltaY;
}
return [moveX, moveY];
}
@action
moveTo(x, y, isIgnoreRule = false): boolean {
const deltaX = x - this.x;
const deltaY = y - this.y;
if (!isIgnoreRule && !this.isAllowMoveNode(deltaX, deltaY)) return false;
if (this.text) {
this.text && this.moveText(deltaX, deltaY);
}
this.x = x;
this.y = y;
return true;
}
@action
moveText(deltaX, deltaY): void {
const {
x,
y,
value,
draggable,
editable,
} = this.text;
this.text = {
value,
editable,
draggable,
x: x + deltaX,
y: y + deltaY,
};
}
@action
updateText(value: string): void {
this.text = {
...toJS(this.text),
value,
};
}
@action
setSelected(flag = true): void {
this.isSelected = flag;
}
@action
setHovered(flag = true): void {
this.isHovered = flag;
this.setIsShowAnchor(flag);
}
@action
setIsShowAnchor(flag = true): void {
this.isShowAnchor = flag;
}
@action
setHitable(flag = true): void {
this.isHitable = flag;
}
@action
setElementState(state: number, additionStateData?: AdditionData): void {
this.state = state;
this.additionStateData = additionStateData;
}
@action
setProperty(key, val): void {
this.properties = {
...toJS(this.properties),
[key]: formatData(val),
};
this.setAttributes();
}
@action
setProperties(properties): void {
this.properties = {
...toJS(this.properties),
...formatData(properties),
};
this.setAttributes();
}
@action
deleteProperty(key: string): void {
delete this.properties[key];
this.setAttributes();
}
@action
setStyle(key, val): void {
this.style = {
...this.style,
[key]: formatData(val),
};
}
@action
setStyles(styles): void {
this.style = {
...this.style,
...formatData(styles),
};
}
@action
updateStyles(styles): void {
this.style = {
...formatData(styles),
};
}
@action
setZIndex(zIndex = 1): void {
this.zIndex = zIndex;
}
@action
updateAttributes(attributes) {
assign(this, attributes);
}
}