@logicflow/core 2.2.0-alpha.7 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/package.json +6 -1
  2. package/.turbo/turbo-build$colon$dev.log +0 -10
  3. package/.turbo/turbo-build.log +0 -33
  4. package/CHANGELOG.md +0 -1901
  5. package/__tests__/algorithm/egde.test.ts +0 -131
  6. package/__tests__/algorithm/index.test.ts +0 -74
  7. package/__tests__/algorithm/outline.test.ts +0 -43
  8. package/__tests__/bugs/1545-spec.test.ts +0 -42
  9. package/__tests__/event/event.test.ts +0 -22
  10. package/__tests__/history/history.test.ts +0 -28
  11. package/__tests__/logicflow.test.ts +0 -575
  12. package/__tests__/model/graphmodel.test.ts +0 -87
  13. package/__tests__/util/compatible.test.ts +0 -48
  14. package/__tests__/util/edge.test.ts +0 -224
  15. package/__tests__/util/geometry.test.ts +0 -14
  16. package/__tests__/util/graph.test.ts +0 -16
  17. package/__tests__/util/matrix.test.ts +0 -41
  18. package/__tests__/util/node.test.ts +0 -68
  19. package/__tests__/util/sampling.test.ts +0 -12
  20. package/__tests__/util/vector.test.ts +0 -50
  21. package/__tests__/util/zIndex.test.ts +0 -10
  22. package/src/LogicFlow.tsx +0 -2017
  23. package/src/algorithm/edge.ts +0 -67
  24. package/src/algorithm/index.ts +0 -70
  25. package/src/algorithm/outline.ts +0 -77
  26. package/src/algorithm/rotate.ts +0 -55
  27. package/src/common/drag.ts +0 -219
  28. package/src/common/history.ts +0 -108
  29. package/src/common/index.ts +0 -6
  30. package/src/common/keyboard.ts +0 -108
  31. package/src/common/matrix.ts +0 -122
  32. package/src/common/vector.ts +0 -93
  33. package/src/constant/index.ts +0 -179
  34. package/src/constant/theme.ts +0 -708
  35. package/src/event/event.md +0 -66
  36. package/src/event/eventArgs.ts +0 -643
  37. package/src/event/eventEmitter.ts +0 -156
  38. package/src/history/index.ts +0 -119
  39. package/src/index.less +0 -1
  40. package/src/index.ts +0 -26
  41. package/src/keyboard/index.ts +0 -112
  42. package/src/keyboard/shortcut.ts +0 -200
  43. package/src/model/BaseModel.ts +0 -250
  44. package/src/model/EditConfigModel.ts +0 -334
  45. package/src/model/GraphModel.ts +0 -1824
  46. package/src/model/NestedTransformModel.ts +0 -121
  47. package/src/model/SnaplineModel.ts +0 -256
  48. package/src/model/TransformModel.ts +0 -258
  49. package/src/model/edge/BaseEdgeModel.ts +0 -785
  50. package/src/model/edge/BezierEdgeModel.ts +0 -197
  51. package/src/model/edge/LineEdgeModel.ts +0 -36
  52. package/src/model/edge/PolylineEdgeModel.ts +0 -817
  53. package/src/model/edge/index.ts +0 -4
  54. package/src/model/index.ts +0 -9
  55. package/src/model/node/BaseNodeModel.ts +0 -959
  56. package/src/model/node/CircleNodeModel.ts +0 -91
  57. package/src/model/node/DiamondNodeModel.ts +0 -132
  58. package/src/model/node/EllipseNodeModel.ts +0 -98
  59. package/src/model/node/HtmlNodeModel.ts +0 -64
  60. package/src/model/node/PolygonNodeModel.ts +0 -152
  61. package/src/model/node/RectNodeModel.ts +0 -69
  62. package/src/model/node/TextNodeModel.ts +0 -54
  63. package/src/model/node/index.ts +0 -8
  64. package/src/options.ts +0 -150
  65. package/src/style/index.less +0 -262
  66. package/src/style/raw.ts +0 -221
  67. package/src/tool/MultipleSelectTool.tsx +0 -140
  68. package/src/tool/TextEditTool.tsx +0 -193
  69. package/src/tool/index.ts +0 -101
  70. package/src/typings.d.ts +0 -5
  71. package/src/util/animation.ts +0 -29
  72. package/src/util/browser.ts +0 -4
  73. package/src/util/compatible.ts +0 -15
  74. package/src/util/drag.ts +0 -219
  75. package/src/util/edge.ts +0 -1094
  76. package/src/util/geometry.ts +0 -154
  77. package/src/util/graph.ts +0 -46
  78. package/src/util/index.ts +0 -17
  79. package/src/util/matrix.ts +0 -129
  80. package/src/util/mobx.ts +0 -23
  81. package/src/util/node.ts +0 -543
  82. package/src/util/raf.ts +0 -28
  83. package/src/util/resize.ts +0 -606
  84. package/src/util/sampling.ts +0 -85
  85. package/src/util/theme.ts +0 -84
  86. package/src/util/uuid.ts +0 -26
  87. package/src/util/vector.ts +0 -93
  88. package/src/util/zIndex.ts +0 -6
  89. package/src/view/Anchor.tsx +0 -462
  90. package/src/view/Control.tsx +0 -510
  91. package/src/view/Graph.tsx +0 -141
  92. package/src/view/Rotate.tsx +0 -113
  93. package/src/view/behavior/dnd.ts +0 -162
  94. package/src/view/behavior/index.ts +0 -2
  95. package/src/view/behavior/snapline.ts +0 -16
  96. package/src/view/edge/AdjustPoint.tsx +0 -425
  97. package/src/view/edge/Arrow.tsx +0 -54
  98. package/src/view/edge/BaseEdge.tsx +0 -660
  99. package/src/view/edge/BezierEdge.tsx +0 -101
  100. package/src/view/edge/LineEdge.tsx +0 -81
  101. package/src/view/edge/PolylineEdge.tsx +0 -311
  102. package/src/view/edge/index.ts +0 -6
  103. package/src/view/index.ts +0 -8
  104. package/src/view/node/BaseNode.tsx +0 -585
  105. package/src/view/node/CircleNode.tsx +0 -21
  106. package/src/view/node/DiamondNode.tsx +0 -24
  107. package/src/view/node/EllipseNode.tsx +0 -22
  108. package/src/view/node/HtmlNode.tsx +0 -112
  109. package/src/view/node/PolygonNode.tsx +0 -28
  110. package/src/view/node/RectNode.tsx +0 -30
  111. package/src/view/node/TextNode.tsx +0 -39
  112. package/src/view/node/index.ts +0 -8
  113. package/src/view/overlay/BackgroundOverlay.tsx +0 -34
  114. package/src/view/overlay/BezierAdjustOverlay.tsx +0 -150
  115. package/src/view/overlay/CanvasOverlay.tsx +0 -290
  116. package/src/view/overlay/Grid.tsx +0 -319
  117. package/src/view/overlay/ModificationOverlay.tsx +0 -31
  118. package/src/view/overlay/OutlineOverlay.tsx +0 -158
  119. package/src/view/overlay/SnaplineOverlay.tsx +0 -44
  120. package/src/view/overlay/ToolOverlay.tsx +0 -65
  121. package/src/view/overlay/getTransformHoc.tsx +0 -50
  122. package/src/view/overlay/gridConfig.ts +0 -103
  123. package/src/view/overlay/index.ts +0 -8
  124. package/src/view/shape/Circle.tsx +0 -41
  125. package/src/view/shape/Ellipse.tsx +0 -42
  126. package/src/view/shape/Line.tsx +0 -39
  127. package/src/view/shape/Path.tsx +0 -22
  128. package/src/view/shape/Polygon.tsx +0 -54
  129. package/src/view/shape/Polyline.tsx +0 -31
  130. package/src/view/shape/Rect.tsx +0 -44
  131. package/src/view/shape/Text.tsx +0 -168
  132. package/src/view/shape/index.ts +0 -8
  133. package/src/view/text/BaseText.tsx +0 -134
  134. package/src/view/text/LineText.tsx +0 -168
  135. package/src/view/text/index.ts +0 -2
  136. package/stats.html +0 -4842
  137. package/tsconfig.json +0 -18
@@ -1,785 +0,0 @@
1
- import { assign, cloneDeep, find, isUndefined } from 'lodash-es'
2
- import { action, computed, isObservable, observable, set, toJS } from 'mobx'
3
- import { BaseNodeModel, GraphModel, Model } from '..'
4
- import LogicFlow from '../../LogicFlow'
5
- import {
6
- createUuid,
7
- formatData,
8
- getAnchors,
9
- getZIndex,
10
- pickEdgeConfig,
11
- twoPointDistance,
12
- } from '../../util'
13
- import {
14
- ElementState,
15
- ElementType,
16
- ModelType,
17
- OverlapMode,
18
- TextMode,
19
- } from '../../constant'
20
-
21
- import Point = LogicFlow.Point
22
- import EdgeData = LogicFlow.EdgeData
23
- import EdgeConfig = LogicFlow.EdgeConfig
24
- import TextConfig = LogicFlow.TextConfig
25
- import PropertiesType = LogicFlow.PropertiesType
26
-
27
- export interface IBaseEdgeModel<P extends PropertiesType>
28
- extends Model.BaseModel<P> {
29
- /**
30
- * model 基础类型,固定为 edge
31
- */
32
- readonly BaseType: ElementType.EDGE
33
- properties: P
34
-
35
- sourceNodeId: string
36
- targetNodeId: string
37
-
38
- startPoint?: Point
39
- endPoint?: Point
40
- points: string
41
- pointsList: Point[]
42
-
43
- isAnimation: boolean
44
- isDragging?: boolean
45
- isShowAdjustPoint: boolean // 是否显示边两端的调整点
46
-
47
- sourceAnchorId?: string
48
- targetAnchorId?: string
49
- arrowConfig?: LogicFlow.ArrowConfig
50
- }
51
-
52
- export class BaseEdgeModel<P extends PropertiesType = PropertiesType>
53
- implements IBaseEdgeModel<P>
54
- {
55
- readonly BaseType = ElementType.EDGE
56
- static BaseType: ElementType = ElementType.EDGE
57
-
58
- // 数据属性
59
- public id = ''
60
- @observable type = ''
61
- @observable sourceNodeId = ''
62
- @observable targetNodeId = ''
63
- @observable startPoint!: Point
64
- @observable endPoint!: Point
65
-
66
- @observable textMode = TextMode.TEXT
67
- @observable text: Required<TextConfig> = {
68
- value: '',
69
- x: 0,
70
- y: 0,
71
- draggable: false,
72
- editable: true,
73
- }
74
- @observable properties: P
75
- @observable points = ''
76
- @observable pointsList: Point[] = []
77
-
78
- // 状态属性
79
- virtual = false
80
- @observable isSelected = false
81
- @observable isHovered = false
82
- @observable isHitable = true // 细粒度控制边是否对用户操作进行反应
83
- @observable isHittable = true // 细粒度控制边是否对用户操作进行反应
84
- @observable draggable = true
85
- @observable visible = true
86
-
87
- // 边特有属性,动画及调整点
88
- @observable isAnimation = false
89
- @observable isShowAdjustPoint = false // 是否显示边两端的调整点
90
- isDragging?: boolean
91
- // 引用属性
92
- graphModel: GraphModel
93
- @observable zIndex: number = 0
94
- @observable state = ElementState.DEFAULT
95
- modelType = ModelType.EDGE
96
- additionStateData?: Model.AdditionStateDataType
97
-
98
- sourceAnchorId?: string
99
- targetAnchorId?: string
100
-
101
- menu?: LogicFlow.MenuConfig[]
102
- customTextPosition = false // 是否自定义边文本位置
103
- @observable style: LogicFlow.CommonTheme = {} // 每条边自己的样式,动态修改
104
-
105
- // TODO: 每个边独立生成一个marker没必要
106
- // 箭头属性
107
- @observable arrowConfig: LogicFlow.ArrowConfig = {
108
- markerEnd: `url(#marker-end-${this.id})`,
109
- markerStart: `url(#marker-start-${this.id})`,
110
- };
111
- [propName: string]: unknown // 支持自定义
112
-
113
- constructor(data: EdgeConfig<P>, graphModel: GraphModel) {
114
- this.graphModel = graphModel
115
- this.properties = data.properties ?? ({} as P)
116
-
117
- this.initEdgeData(data)
118
- this.setAttributes()
119
- }
120
-
121
- /**
122
- * 初始化边数据
123
- * @overridable 支持重写
124
- * initNodeData和setAttributes的区别在于
125
- * initNodeData只在节点初始化的时候调用,用于初始化节点的所有属性。
126
- * setAttributes除了初始化调用外,还会在properties发生变化后调用。
127
- */
128
- initEdgeData(data: EdgeConfig) {
129
- if (!data.properties) {
130
- data.properties = {}
131
- }
132
-
133
- if (!data.id) {
134
- // 自定义边id > 全局定义边id > 内置
135
- const { idGenerator } = this.graphModel
136
- const globalId = idGenerator && idGenerator(data.type)
137
- const nodeId = this.createId()
138
- data.id = nodeId || globalId || createUuid()
139
- }
140
- this.arrowConfig.markerEnd = `url(#marker-end-${data.id})`
141
- this.arrowConfig.markerStart = `url(#marker-start-${data.id})`
142
- const {
143
- editConfigModel: { adjustEdgeStartAndEnd },
144
- } = this.graphModel
145
- this.isShowAdjustPoint = adjustEdgeStartAndEnd
146
- assign(this, pickEdgeConfig(data))
147
- const { overlapMode, eventCenter } = this.graphModel
148
- this.updateZIndexByOverlap(overlapMode, data.zIndex || getZIndex())
149
- // 设置边的 anchors,也就是边的两个端点
150
- // 端点依赖于 edgeData 的 sourceNode 和 targetNode
151
- this.setAnchors()
152
- // 边的拐点依赖于两个端点
153
- this.initPoints()
154
- // 文本位置依赖于边上的所有拐点
155
- this.formatText(data)
156
-
157
- eventCenter.on('overlap:change', (data) => {
158
- const { overlapMode: newMode } = data
159
- this.updateZIndexByOverlap(newMode, this.zIndex || getZIndex())
160
- })
161
- }
162
-
163
- /**
164
- * 设置model属性
165
- * @overridable 支持重写
166
- * 每次properties发生变化会触发
167
- */
168
- setAttributes() {}
169
-
170
- createId(): string | null {
171
- return null
172
- }
173
-
174
- /**
175
- * 自定义边样式
176
- *
177
- * @overridable 支持重写
178
- * @returns 自定义边样式
179
- */
180
- getEdgeStyle(): LogicFlow.EdgeTheme {
181
- return {
182
- ...this.graphModel.theme.baseEdge,
183
- ...this.style,
184
- }
185
- }
186
-
187
- /**
188
- * 自定义边调整点样式
189
- *
190
- * @overridable 支持重写
191
- * 在isShowAdjustPoint为true时会显示调整点。
192
- */
193
- getAdjustPointStyle() {
194
- return {
195
- ...this.graphModel.theme.edgeAdjust,
196
- }
197
- }
198
- /**
199
- * 自定义边文本样式
200
- *
201
- * @overridable 支持重写
202
- */
203
- getTextStyle() {
204
- // 透传 edgeText
205
- const { edgeText } = this.graphModel.theme
206
- return cloneDeep(edgeText)
207
- }
208
-
209
- /**
210
- * 自定义边动画样式
211
- *
212
- * @overridable 支持重写
213
- * @example
214
- * getEdgeAnimationStyle() {
215
- * const style = super.getEdgeAnimationStyle()
216
- * style.stroke = 'blue'
217
- * style.animationDuration = '30s'
218
- * style.animationDirection = 'reverse'
219
- * return style
220
- * }
221
- */
222
- getEdgeAnimationStyle() {
223
- const { edgeAnimation } = this.graphModel.theme
224
- return cloneDeep(edgeAnimation)
225
- }
226
-
227
- /**
228
- * 自定义边箭头样式
229
- *
230
- * @overridable 支持重写
231
- * @example
232
- * getArrowStyle() {
233
- * const style = super.getArrowStyle()
234
- * style.stroke = 'green'
235
- * return style
236
- * }
237
- */
238
- getArrowStyle(): LogicFlow.ArrowTheme {
239
- const edgeStyle = this.getEdgeStyle()
240
- const edgeAnimationStyle = this.getEdgeAnimationStyle()
241
- const { arrow } = this.graphModel.theme
242
- const stroke = this.isAnimation
243
- ? edgeAnimationStyle.stroke
244
- : edgeStyle.stroke
245
- return {
246
- ...edgeStyle,
247
- fill: stroke,
248
- stroke,
249
- ...arrow,
250
- }
251
- }
252
-
253
- /**
254
- * 自定义边被选中时展示其范围的矩形框样式
255
- *
256
- * @overridable 支持重写
257
- * @example
258
- * // 隐藏outline
259
- * getOutlineStyle() {
260
- * const style = super.getOutlineStyle()
261
- * style.stroke = "none"
262
- * style.hover.stroke = "none"
263
- * return style
264
- * }
265
- */
266
- getOutlineStyle(): LogicFlow.OutlineTheme {
267
- const { graphModel } = this
268
- const { edgeOutline } = graphModel.theme
269
- let attributes = { ...edgeOutline }
270
- if (this.isHovered) {
271
- const hoverStyle = edgeOutline.hover || {}
272
- attributes = {
273
- ...attributes,
274
- ...hoverStyle,
275
- }
276
- }
277
- return cloneDeep(attributes)
278
- }
279
-
280
- /**
281
- * 重新自定义文本位置
282
- *
283
- * @overridable 支持重写
284
- */
285
- getTextPosition(): Point {
286
- return {
287
- x: 0,
288
- y: 0,
289
- }
290
- }
291
-
292
- /**
293
- * 边的前一个节点
294
- */
295
- @computed get sourceNode() {
296
- return this.graphModel?.nodesMap[this.sourceNodeId]?.model
297
- }
298
-
299
- /**
300
- * 边的后一个节点
301
- */
302
- @computed get targetNode() {
303
- return this.graphModel?.nodesMap[this.targetNodeId]?.model
304
- }
305
-
306
- @computed get textPosition(): Point {
307
- return this.getTextPosition()
308
- }
309
-
310
- /**
311
- * 内部方法,计算两个节点相连时的起点位置
312
- */
313
- getBeginAnchor(
314
- sourceNode: BaseNodeModel,
315
- targetNode: BaseNodeModel,
316
- sourceAnchorId?: string,
317
- ): Point | undefined {
318
- // https://github.com/didi/LogicFlow/issues/1077
319
- // 可能拿到的sourceAnchors为空数组,因此position可能返回为undefined
320
- let position: Point | undefined
321
- let minDistance: number | undefined
322
- const sourceAnchors = getAnchors(sourceNode)
323
- if (sourceAnchorId) {
324
- position = find(sourceAnchors, (anchor) => anchor.id === sourceAnchorId)
325
- // 如果指定了起始锚点,且指定锚点是节点拥有的锚点时,就把该点设置为起点
326
- if (position) {
327
- return position
328
- }
329
- console.warn(
330
- `未在节点上找到指定的起点锚点${sourceAnchorId},已使用默认锚点作为起点`,
331
- )
332
- }
333
- sourceAnchors.forEach((anchor) => {
334
- const distance = twoPointDistance(anchor, targetNode)
335
- if (minDistance === undefined) {
336
- minDistance = distance
337
- position = anchor
338
- } else if (distance < minDistance) {
339
- minDistance = distance
340
- position = anchor
341
- }
342
- })
343
- return position
344
- }
345
-
346
- /**
347
- * 内部方法,计算两个节点相连时的终点位置
348
- */
349
- getEndAnchor(
350
- targetNode: BaseNodeModel,
351
- targetAnchorId?: string,
352
- ): Point | undefined {
353
- // https://github.com/didi/LogicFlow/issues/1077
354
- // 可能拿到的targetAnchors为空数组,因此position可能返回为undefined
355
- let position: Point | undefined
356
- let minDistance: number | undefined
357
- const targetAnchors = getAnchors(targetNode)
358
- if (targetAnchorId) {
359
- position = find(targetAnchors, (anchor) => anchor.id === targetAnchorId)
360
- // 如果指定了终点锚点,且指定锚点是节点拥有的锚点时,就把该点设置为终点
361
- if (position) {
362
- return position
363
- }
364
- console.warn(
365
- `未在节点上找到指定的终点锚点${targetAnchorId},已使用默认锚点作为终点`,
366
- )
367
- }
368
- targetAnchors.forEach((anchor) => {
369
- if (!this.startPoint) return // 如果此时 this.startPoint 为 undefined,直接返回
370
-
371
- const distance = twoPointDistance(anchor, this.startPoint)
372
- if (minDistance === undefined) {
373
- minDistance = distance
374
- position = anchor
375
- } else if (distance < minDistance) {
376
- minDistance = distance
377
- position = anchor
378
- }
379
- })
380
- return position
381
- }
382
-
383
- /**
384
- * 获取当前边的properties
385
- */
386
- getProperties() {
387
- return toJS(this.properties)
388
- }
389
-
390
- /**
391
- * 获取被保存时返回的数据
392
- *
393
- * @overridable 支持重写
394
- */
395
- getData(): EdgeData {
396
- let { properties } = this
397
- if (isObservable(properties)) {
398
- properties = toJS(properties)
399
- }
400
- const data: EdgeData = {
401
- id: this.id,
402
- type: this.type,
403
- properties,
404
- sourceNodeId: this.sourceNodeId,
405
- targetNodeId: this.targetNodeId,
406
- sourceAnchorId: this.sourceAnchorId,
407
- targetAnchorId: this.targetAnchorId,
408
- startPoint: assign({}, this.startPoint),
409
- endPoint: assign({}, this.endPoint),
410
- }
411
- // 因为默认模式和边在上模式下,对节点的zIndex要求不高(因为渲染的时候会按照模式对所有元素进行排序)
412
- // 所以只在递增模式和静态模式下设置zIndex
413
- if (
414
- [OverlapMode.INCREASE, OverlapMode.STATIC].includes(
415
- this.graphModel.overlapMode,
416
- )
417
- ) {
418
- data.zIndex = this.zIndex
419
- }
420
- const { x, y, value } = this.text
421
- if (value) {
422
- data.text = {
423
- x,
424
- y,
425
- value,
426
- }
427
- }
428
- return data
429
- }
430
-
431
- /**
432
- * 获取边的数据
433
- *
434
- * @overridable 支持重写
435
- * 用于在历史记录时获取节点数据。
436
- * 在某些情况下,如果希望某个属性变化不引起history的变化,
437
- * 可以重写此方法。
438
- */
439
- getHistoryData(): EdgeData {
440
- return this.getData()
441
- }
442
-
443
- /**
444
- * 设置边的属性,会触发重新渲染
445
- * @param key 属性名
446
- * @param val 属性值
447
- */
448
- @action setProperty(key: string, val: any): void {
449
- set(this.properties, key, formatData(val))
450
- this.setAttributes()
451
- }
452
-
453
- /**
454
- * 删除边的属性,会触发重新渲染
455
- * @param key 属性名
456
- */
457
- @action deleteProperty(key: string): void {
458
- delete this.properties[key]
459
- this.setAttributes()
460
- }
461
-
462
- /**
463
- * 设置边的属性,会触发重新渲染
464
- * @param properties 要更新的 properties,会做合并
465
- */
466
- @action
467
- setProperties(properties: Record<string, any>): void {
468
- this.properties = {
469
- ...toJS(this.properties),
470
- ...formatData(properties),
471
- }
472
- this.setAttributes()
473
- }
474
-
475
- /**
476
- * 修改边的id
477
- */
478
- @action
479
- changeEdgeId(id: string) {
480
- const { markerEnd, markerStart } = this.arrowConfig
481
- if (markerStart && markerStart === `url(#marker-start-${this.id})`) {
482
- this.arrowConfig.markerStart = `url(#marker-start-${id})`
483
- }
484
- if (markerEnd && markerEnd === `url(#marker-end-${this.id})`) {
485
- this.arrowConfig.markerEnd = `url(#marker-end-${id})`
486
- }
487
- this.id = id
488
- }
489
-
490
- /**
491
- * 设置边样式,用于插件开发时跳过自定义边的渲染。大多数情况下,不需要使用此方法。
492
- * 如果需要设置边的样式,请使用 getEdgeStyle 方法自定义边样式。
493
- */
494
- @action
495
- setStyle(key: string, val): void {
496
- this.style = {
497
- ...this.style,
498
- [key]: formatData(val),
499
- }
500
- }
501
-
502
- /**
503
- * 设置边样式,用于插件开发时跳过自定义边的渲染。大多数情况下,不需要使用此方法。
504
- * 如果需要设置边的样式,请使用 getEdgeStyle 方法自定义边样式。
505
- */
506
- @action
507
- setStyles(styles): void {
508
- this.style = {
509
- ...this.style,
510
- ...formatData(styles),
511
- }
512
- }
513
-
514
- /**
515
- * 设置边样式,用于插件开发时跳过自定义边的渲染。大多数情况下,不需要使用此方法。
516
- * 如果需要设置边的样式,请使用 getEdgeStyle 方法自定义边样式。
517
- */
518
- @action
519
- updateStyles(styles): void {
520
- this.style = {
521
- ...formatData(styles),
522
- }
523
- }
524
-
525
- /**
526
- * 设置当前元素的文本模式
527
- * @param mode
528
- */
529
- @action setTextMode(mode: TextMode) {
530
- this.textMode = mode
531
- }
532
-
533
- /**
534
- * 内部方法,处理初始化文本格式
535
- */
536
- @action formatText(data: EdgeConfig) {
537
- const {
538
- editConfigModel: { edgeTextDraggable, edgeTextEdit },
539
- } = this.graphModel
540
- const { x, y } = this.textPosition
541
- const { text } = data
542
- let textConfig: Required<TextConfig> = {
543
- value: '',
544
- x,
545
- y,
546
- draggable: edgeTextDraggable,
547
- editable: edgeTextEdit,
548
- }
549
-
550
- if (text) {
551
- if (typeof text === 'string') {
552
- textConfig = {
553
- ...textConfig,
554
- value: text,
555
- }
556
- } else {
557
- textConfig = {
558
- ...textConfig,
559
- x: text.x ?? x,
560
- y: text.y ?? y,
561
- value: text.value ?? '',
562
- }
563
- if (!isUndefined(text.draggable)) {
564
- textConfig.draggable = text.draggable
565
- }
566
- if (!isUndefined(text.editable)) {
567
- textConfig.editable = text.editable
568
- }
569
- }
570
- }
571
- this.text = textConfig
572
- }
573
- /**
574
- * 重置文本位置
575
- */
576
- @action resetTextPosition() {
577
- const { x, y } = this.textPosition
578
- this.text.x = x
579
- this.text.y = y
580
- }
581
-
582
- /**
583
- * 移动边上的文本
584
- */
585
- @action moveText(deltaX: number, deltaY: number): void {
586
- const { x, y, value, draggable, editable } = this.text
587
- this.text = {
588
- value,
589
- editable,
590
- draggable,
591
- x: x + deltaX,
592
- y: y + deltaY,
593
- }
594
- }
595
-
596
- /**
597
- * 设置文本位置和值
598
- */
599
- @action setText(textConfig: LogicFlow.TextConfig): void {
600
- if (textConfig) {
601
- assign(this.text, textConfig)
602
- }
603
- }
604
-
605
- /**
606
- * 更新文本的值
607
- */
608
- @action updateText(value: string): void {
609
- this.text = {
610
- ...toJS(this.text),
611
- value,
612
- }
613
- }
614
-
615
- /**
616
- * 内部方法,计算边的起点和终点和其对于的锚点Id
617
- */
618
- @action
619
- setAnchors(): void {
620
- if (!this.sourceAnchorId || !this.startPoint) {
621
- const anchor = this.getBeginAnchor(
622
- this.sourceNode,
623
- this.targetNode,
624
- this.sourceAnchorId,
625
- )
626
- if (!anchor) {
627
- // https://github.com/didi/LogicFlow/issues/1077
628
- // 当用户自定义getDefaultAnchor(){return []}时,表示:不显示锚点,也不允许其他节点连接到此节点
629
- // 此时拿到的anchor=undefined,下面会直接报错
630
- throw new Error(
631
- '无法获取beginAnchor,请检查anchors相关逻辑,anchors不能为空',
632
- )
633
- }
634
- if (!this.startPoint) {
635
- this.startPoint = {
636
- x: anchor.x,
637
- y: anchor.y,
638
- }
639
- }
640
- if (!this.sourceAnchorId) {
641
- this.sourceAnchorId = anchor.id
642
- }
643
- }
644
- if (!this.targetAnchorId || !this.endPoint) {
645
- const anchor = this.getEndAnchor(this.targetNode, this.targetAnchorId)
646
- if (!anchor) {
647
- // https://github.com/didi/LogicFlow/issues/1077
648
- // 当用户自定义getDefaultAnchor(){return []}时,表示:不显示锚点,也不允许其他节点连接到此节点
649
- // 此时拿到的anchor=undefined,下面会直接报错
650
- throw new Error(
651
- '无法获取endAnchor,请检查anchors相关逻辑,anchors不能为空',
652
- )
653
- }
654
- if (!this.endPoint) {
655
- this.endPoint = {
656
- x: anchor.x,
657
- y: anchor.y,
658
- }
659
- }
660
- if (!this.targetAnchorId) {
661
- this.targetAnchorId = anchor.id
662
- }
663
- }
664
- }
665
-
666
- @action
667
- setSelected(flag = true): void {
668
- this.isSelected = flag
669
- }
670
-
671
- @action
672
- setHovered(flag = true): void {
673
- this.isHovered = flag
674
- }
675
-
676
- @action
677
- setHitable(flag = true): void {
678
- this.isHitable = flag
679
- }
680
-
681
- @action
682
- setHittable(flag = true): void {
683
- this.isHittable = flag
684
- }
685
-
686
- @action
687
- openEdgeAnimation(): void {
688
- this.isAnimation = true
689
- }
690
-
691
- @action
692
- closeEdgeAnimation(): void {
693
- this.isAnimation = false
694
- }
695
-
696
- @action
697
- setElementState(
698
- state: ElementState,
699
- additionStateData?: Model.AdditionStateDataType,
700
- ): void {
701
- this.state = state
702
- this.additionStateData = additionStateData
703
- }
704
-
705
- @action
706
- updateStartPoint(anchor: Point): void {
707
- this.startPoint = anchor
708
- }
709
-
710
- @action
711
- moveStartPoint(deltaX: number, deltaY: number): void {
712
- if (this.startPoint) {
713
- this.startPoint.x += deltaX
714
- this.startPoint.y += deltaY
715
- }
716
- }
717
-
718
- @action
719
- updateEndPoint(anchor: Point): void {
720
- this.endPoint = anchor
721
- }
722
-
723
- @action
724
- moveEndPoint(deltaX: number, deltaY: number): void {
725
- if (this.endPoint) {
726
- this.endPoint.x += deltaX
727
- this.endPoint.y += deltaY
728
- }
729
- }
730
-
731
- @action
732
- setZIndex(zIndex = 0): void {
733
- this.zIndex = zIndex
734
- }
735
-
736
- @action
737
- initPoints() {}
738
-
739
- @action
740
- updateAttributes(attributes) {
741
- assign(this, attributes)
742
- }
743
-
744
- // 获取边调整的起点
745
- @action
746
- getAdjustStart() {
747
- return this.startPoint
748
- }
749
-
750
- // 获取边调整的终点
751
- @action
752
- getAdjustEnd() {
753
- return this.endPoint
754
- }
755
-
756
- // 起终点拖拽调整过程中,进行直线路径更新
757
- @action
758
- updateAfterAdjustStartAndEnd({
759
- startPoint,
760
- endPoint,
761
- }: Record<'startPoint' | 'endPoint', Point>) {
762
- this.updateStartPoint({ x: startPoint.x, y: startPoint.y })
763
- this.updateEndPoint({ x: endPoint.x, y: endPoint.y })
764
- }
765
-
766
- // 堆叠模式变化时,更新zIndex
767
- @action
768
- updateZIndexByOverlap(overlapMode: OverlapMode, defaultZIndex) {
769
- switch (overlapMode) {
770
- case OverlapMode.DEFAULT:
771
- this.zIndex = 0
772
- break
773
- case OverlapMode.EDGE_TOP:
774
- this.zIndex = 1
775
- break
776
- case OverlapMode.INCREASE:
777
- this.zIndex = defaultZIndex
778
- break
779
- default:
780
- break
781
- }
782
- }
783
- }
784
-
785
- export default BaseEdgeModel