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