@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,510 +0,0 @@
1
- import { createElement as h, Component } from 'preact/compat'
2
- import { find, forEach, map, cloneDeep } from 'lodash-es'
3
- import { Rect } from './shape'
4
- import LogicFlow from '../LogicFlow'
5
- import { getNodeBBox, IDragParams, StepDrag, handleResize } from '../util'
6
- import { BaseNodeModel, GraphModel } from '../model'
7
-
8
- import NodeData = LogicFlow.NodeData
9
- import VectorData = LogicFlow.VectorData
10
- import { EventType } from '../constant'
11
- import ResizeNodeData = ResizeControl.ResizeNodeData
12
- import ResizeInfo = ResizeControl.ResizeInfo
13
- import ControlItemProps = ResizeControl.ControlItemProps
14
-
15
- export enum ResizeControlIndex {
16
- LEFT_TOP = 0,
17
- RIGHT_TOP = 1,
18
- RIGHT_BOTTOM = 2,
19
- LEFT_BOTTOM = 3,
20
- }
21
-
22
- export type IResizeControlProps = {
23
- model: BaseNodeModel
24
- graphModel: GraphModel
25
- } & ControlItemProps
26
-
27
- export type IResizeControlState = {
28
- startX: number
29
- startY: number
30
- endX: number
31
- endY: number
32
- dragging: boolean
33
- }
34
-
35
- export class ResizeControl extends Component<
36
- IResizeControlProps,
37
- IResizeControlState
38
- > {
39
- readonly index: ResizeControlIndex
40
- readonly nodeModel: BaseNodeModel
41
- readonly graphModel: GraphModel
42
- readonly dragHandler: StepDrag
43
-
44
- //判断Shift键状态
45
- private isShiftPressed = false
46
-
47
- constructor(props: IResizeControlProps) {
48
- super()
49
- const { index, model, graphModel } = props
50
- this.index = index
51
- this.nodeModel = model
52
- this.graphModel = graphModel
53
-
54
- this.dragHandler = new StepDrag({
55
- onDragStart: this.onDragStart,
56
- onDragging: this.onDragging,
57
- onDragEnd: this.onDragEnd,
58
- step: graphModel.gridSize,
59
- })
60
-
61
- this.bindKeyboardEvents()
62
- }
63
-
64
- //绑定键盘事件监听
65
- bindKeyboardEvents = () => {
66
- document.addEventListener('keydown', this.handleKeyDown)
67
- document.addEventListener('keyup', this.handleKeyUp)
68
- }
69
-
70
- //处理键盘按下事件
71
- handleKeyDown = (event: KeyboardEvent) => {
72
- if (event.key === 'Shift') {
73
- this.isShiftPressed = true
74
- }
75
- }
76
-
77
- handleKeyUp = (event: KeyboardEvent) => {
78
- if (event.key === 'Shift') {
79
- this.isShiftPressed = false
80
- }
81
- }
82
-
83
- componentWillUnmount() {
84
- this.dragHandler.destroy()
85
- document.removeEventListener('keydown', this.handleKeyDown)
86
- document.removeEventListener('keyup', this.handleKeyUp)
87
- }
88
-
89
- updateEdgePointByAnchors = () => {
90
- // https://github.com/didi/LogicFlow/issues/807
91
- // https://github.com/didi/LogicFlow/issues/875
92
- // 之前的做法,比如Rect是使用getRectResizeEdgePoint()计算边的point缩放后的位置
93
- // getRectResizeEdgePoint()考虑了瞄点在四条边以及在4个圆角的情况
94
- // 使用的是一种等比例缩放的模式,比如:
95
- // const pct = (y - beforeNode.y) / (beforeNode.height / 2 - radius)
96
- // afterPoint.y = afterNode.y + (afterNode.height / 2 - radius) * pct
97
- // 但是用户自定义的getDefaultAnchor()不一定是按照比例编写的
98
- // 它可能是 x: x + 20:每次缩放都会保持在x右边20的位置,因此用户自定义瞄点时,然后产生无法跟随的问题
99
- // 现在的做法是:直接获取用户自定义瞄点的位置,然后用这个位置作为边的新的起点,而不是自己进行计算
100
- const { id, anchors } = this.nodeModel
101
- const edges = this.graphModel.getNodeEdges(id)
102
- // 更新边
103
- forEach(edges, (edge) => {
104
- if (edge.sourceNodeId === id) {
105
- // 边是以该节点为 sourceNode 时
106
- const anchorItem = find(
107
- anchors,
108
- (anchor) => anchor.id === edge.sourceAnchorId,
109
- )
110
-
111
- if (anchorItem) {
112
- edge.updateStartPoint({
113
- x: anchorItem.x,
114
- y: anchorItem.y,
115
- })
116
- }
117
- } else if (edge.targetNodeId === id) {
118
- // 边是以该节点为 targetNode 时
119
- const anchorItem = find(
120
- anchors,
121
- (anchor) => anchor.id === edge.targetAnchorId,
122
- )
123
-
124
- if (anchorItem) {
125
- edge.updateEndPoint({
126
- x: anchorItem.x,
127
- y: anchorItem.y,
128
- })
129
- }
130
- }
131
- })
132
- }
133
-
134
- triggerResizeEvent = (
135
- preNodeData: ResizeNodeData,
136
- curNodeData: ResizeNodeData,
137
- deltaX,
138
- deltaY,
139
- index,
140
- nodeModel: BaseNodeModel,
141
- ) => {
142
- this.graphModel.eventCenter.emit(EventType.NODE_RESIZE, {
143
- preData: preNodeData,
144
- data: curNodeData,
145
- deltaX,
146
- deltaY,
147
- index,
148
- model: nodeModel,
149
- })
150
- }
151
-
152
- /**
153
- * 计算 Control 拖动后,节点的高度信息
154
- * @param index
155
- * @param resizeInfo
156
- * @param pct
157
- * @param freezeWidth
158
- * @param freezeHeight
159
- */
160
- recalcResizeInfo = (
161
- index: ResizeControlIndex,
162
- resizeInfo: ResizeInfo,
163
- pct = 1,
164
- freezeWidth = false,
165
- freezeHeight = false,
166
- ): ResizeInfo => {
167
- const nextResizeInfo = cloneDeep(resizeInfo)
168
- let { deltaX, deltaY } = nextResizeInfo
169
- const { width, height, PCTResizeInfo } = nextResizeInfo
170
- if (PCTResizeInfo) {
171
- const sensitivity = 4 // 越低越灵敏
172
- let deltaScale = 0
173
- let combineDelta = 0
174
- switch (index) {
175
- case ResizeControlIndex.LEFT_TOP:
176
- combineDelta = (deltaX * -1 - deltaY) / sensitivity
177
- break
178
- case ResizeControlIndex.RIGHT_TOP:
179
- combineDelta = (deltaX - deltaY) / sensitivity
180
- break
181
- case ResizeControlIndex.RIGHT_BOTTOM:
182
- combineDelta = (deltaX + deltaY) / sensitivity
183
- break
184
- case ResizeControlIndex.LEFT_BOTTOM:
185
- combineDelta = (deltaX * -1 + deltaY) / sensitivity
186
- break
187
- default:
188
- break
189
- }
190
-
191
- if (combineDelta !== 0) {
192
- deltaScale =
193
- Math.round(
194
- (combineDelta / PCTResizeInfo.ResizeBasis.basisHeight) * 100000,
195
- ) / 1000
196
- }
197
-
198
- PCTResizeInfo.ResizePCT.widthPCT = Math.max(
199
- Math.min(
200
- PCTResizeInfo.ResizePCT.widthPCT + deltaScale,
201
- PCTResizeInfo.ScaleLimit.maxScaleLimit,
202
- ),
203
- PCTResizeInfo.ScaleLimit.minScaleLimit,
204
- )
205
- PCTResizeInfo.ResizePCT.heightPCT = Math.max(
206
- Math.min(
207
- PCTResizeInfo.ResizePCT.heightPCT + deltaScale,
208
- PCTResizeInfo.ScaleLimit.maxScaleLimit,
209
- ),
210
- PCTResizeInfo.ScaleLimit.minScaleLimit,
211
- )
212
-
213
- const spcWidth = Math.round(
214
- (PCTResizeInfo.ResizePCT.widthPCT *
215
- PCTResizeInfo.ResizeBasis.basisWidth) /
216
- 100,
217
- )
218
- const spcHeight = Math.round(
219
- (PCTResizeInfo.ResizePCT.heightPCT *
220
- PCTResizeInfo.ResizeBasis.basisHeight) /
221
- 100,
222
- )
223
-
224
- switch (index) {
225
- case ResizeControlIndex.LEFT_TOP:
226
- deltaX = width - spcWidth
227
- deltaY = height - spcHeight
228
- break
229
- case ResizeControlIndex.RIGHT_TOP:
230
- deltaX = spcWidth - width
231
- deltaY = height - spcHeight
232
- break
233
- case ResizeControlIndex.RIGHT_BOTTOM:
234
- deltaX = spcWidth - width
235
- deltaY = spcHeight - height
236
- break
237
- case ResizeControlIndex.LEFT_BOTTOM:
238
- deltaX = width - spcWidth
239
- deltaY = spcHeight - height
240
- break
241
- default:
242
- break
243
- }
244
- return nextResizeInfo
245
- }
246
-
247
- // 如果限制了宽/高不变,对应的 width/height 保持一致
248
- switch (index) {
249
- case ResizeControlIndex.LEFT_TOP:
250
- nextResizeInfo.width = freezeWidth ? width : width - deltaX * pct
251
- nextResizeInfo.height = freezeHeight ? height : height - deltaY * pct
252
- break
253
- case ResizeControlIndex.RIGHT_TOP:
254
- nextResizeInfo.width = freezeWidth ? width : width + deltaX * pct
255
- nextResizeInfo.height = freezeHeight ? height : height - deltaY * pct
256
- break
257
- case ResizeControlIndex.RIGHT_BOTTOM:
258
- nextResizeInfo.width = freezeWidth ? width : width + deltaX * pct
259
- nextResizeInfo.height = freezeHeight ? height : height + deltaY * pct
260
- break
261
- case ResizeControlIndex.LEFT_BOTTOM:
262
- nextResizeInfo.width = freezeWidth ? width : width - deltaX * pct
263
- nextResizeInfo.height = freezeHeight ? height : height + deltaY * pct
264
- break
265
- default:
266
- break
267
- }
268
-
269
- return nextResizeInfo
270
- }
271
-
272
- resizeNode = ({ deltaX, deltaY }: VectorData) => {
273
- const { index } = this
274
- const { model, graphModel, x, y } = this.props
275
- // DONE: 调用每个节点中更新缩放时的方法 updateNode 函数,用来各节点缩放的方法
276
- handleResize({
277
- x,
278
- y,
279
- deltaX,
280
- deltaY,
281
- index,
282
- nodeModel: model,
283
- graphModel,
284
- forceProportional: this.isShiftPressed,
285
- cancelCallback: () => {
286
- this.dragHandler.cancelDrag()
287
- },
288
- })
289
- }
290
-
291
- onDragStart = () => {
292
- this.graphModel.selectNodeById(this.nodeModel.id)
293
- }
294
-
295
- onDragging = ({ deltaX, deltaY }: IDragParams) => {
296
- const { transformModel } = this.graphModel
297
- const [dx, dy] = transformModel.fixDeltaXY(deltaX, deltaY)
298
- this.resizeNode({
299
- deltaX: dx,
300
- deltaY: dy,
301
- })
302
- }
303
-
304
- // 由于将拖拽放大缩小改成丝滑模式,这个时候需要再拖拽结束的时候,将节点的位置更新到 grid 上。
305
- onDragEnd = () => {
306
- // TODO: 确认下面该代码是否还需要(应该是默认让节点拖拽以 gridSize 为步长移动)
307
- // const { gridSize = 1 } = this.graphModel
308
- // const x = gridSize * Math.round(this.nodeModel.x / gridSize)
309
- // const y = gridSize * Math.round(this.nodeModel.y / gridSize)
310
- const x = this.nodeModel.x
311
- const y = this.nodeModel.y
312
- this.nodeModel.moveTo(x, y)
313
-
314
- // 先触发 onDragging() -> 更新边 -> 再触发用户自定义的 getDefaultAnchor(),所以 onDragging()
315
- // 拿到的 anchors 是滞后的,为了正确的设置最终的位置,应该在拖拽结束的时候,再设置一次边的 Point 位置,
316
- // 此时拿到的 anchors 是最新的
317
- this.updateEdgePointByAnchors()
318
- }
319
-
320
- onPointerDown = (e: PointerEvent) => {
321
- e.stopPropagation()
322
- this.dragHandler.handleMouseDown(e)
323
- }
324
-
325
- getViewPosition(direction, x, y) {
326
- const { width = 8, height = 8 } = this.props.model.getResizeControlStyle()
327
- switch (direction) {
328
- case 'nw':
329
- return {
330
- x: x - width / 2,
331
- y: y - height / 2,
332
- }
333
- case 'ne': {
334
- return {
335
- x: x + width / 2,
336
- y: y - height / 2,
337
- }
338
- }
339
- case 'se': {
340
- return {
341
- x: x + width / 2,
342
- y: y + height / 2,
343
- }
344
- }
345
- case 'sw': {
346
- return {
347
- x: x - width / 2,
348
- y: y + height / 2,
349
- }
350
- }
351
- default: {
352
- return {
353
- x,
354
- y,
355
- }
356
- }
357
- }
358
- }
359
-
360
- render(): h.JSX.Element {
361
- const { x, y, direction, model } = this.props
362
- const { width, height, ...restStyle } = model.getResizeControlStyle()
363
- // 为了让调整点在视觉上在调整外框的中间,因此在渲染时转换一下
364
- const { x: viewX, y: viewY } = this.getViewPosition(direction, x, y)
365
- return (
366
- <g className={`lf-resize-control lf-resize-control-${direction}`}>
367
- <Rect
368
- className="lf-resize-control-content"
369
- x={viewX}
370
- y={viewY}
371
- width={width ?? 7}
372
- height={height ?? 7}
373
- {...restStyle}
374
- />
375
- <Rect
376
- className="lf-resize-control-content"
377
- x={viewX}
378
- y={viewY}
379
- width={width ? width + 5 : 25}
380
- height={width ? width + 5 : 25}
381
- fill="transparent"
382
- stroke="transparent"
383
- onPointerDown={this.onPointerDown}
384
- />
385
- </g>
386
- )
387
- }
388
- }
389
-
390
- interface IResizeControlGroupProps {
391
- style: LogicFlow.CommonTheme
392
- model: BaseNodeModel
393
- graphModel: GraphModel
394
- }
395
-
396
- export class ResizeControlGroup extends Component<IResizeControlGroupProps> {
397
- constructor() {
398
- super()
399
- }
400
-
401
- getResizeControl(): h.JSX.Element[] {
402
- const { model, graphModel } = this.props
403
- const { minX, minY, maxX, maxY } = getNodeBBox(model)
404
-
405
- const controlList: ControlItemProps[] = [
406
- {
407
- index: ResizeControlIndex.LEFT_TOP,
408
- direction: 'nw',
409
- x: minX,
410
- y: minY,
411
- }, // 左上角
412
- {
413
- index: ResizeControlIndex.RIGHT_TOP,
414
- direction: 'ne',
415
- x: maxX,
416
- y: minY,
417
- }, // 右上角
418
- {
419
- index: ResizeControlIndex.RIGHT_BOTTOM,
420
- direction: 'se',
421
- x: maxX,
422
- y: maxY,
423
- }, // 右下角
424
- {
425
- index: ResizeControlIndex.LEFT_BOTTOM,
426
- direction: 'sw',
427
- x: minX,
428
- y: maxY,
429
- }, // 左下角
430
- ]
431
-
432
- return map(controlList, (control) => (
433
- <ResizeControl {...control} model={model} graphModel={graphModel} />
434
- ))
435
- }
436
-
437
- getResizeOutline() {
438
- const { model } = this.props
439
- const { x, y, width, height } = model
440
- const style = model.getResizeOutlineStyle()
441
-
442
- return (
443
- <Rect
444
- {...style}
445
- pointer-events="none"
446
- x={x}
447
- y={y}
448
- // TODO:宽高padding后续改成配置化的
449
- width={width + 10}
450
- height={height + 10}
451
- />
452
- )
453
- }
454
-
455
- render(): h.JSX.Element {
456
- return (
457
- <g className="lf-resize-control-group">
458
- {this.getResizeOutline()}
459
- {this.getResizeControl()}
460
- </g>
461
- )
462
- }
463
- }
464
-
465
- export namespace ResizeControl {
466
- export type RectShapeResizeProps = {
467
- width: number
468
- height: number
469
- }
470
-
471
- export type PolygonShapeResizerProps = {
472
- rx: number
473
- ry: number
474
- }
475
- export type ResizeProps = RectShapeResizeProps | PolygonShapeResizerProps
476
-
477
- export type ResizeInfo = {
478
- width: number
479
- height: number
480
- deltaX: number
481
- deltaY: number
482
- PCTResizeInfo?: PCTResizeParams
483
- }
484
- export type ResizeNodeData = NodeData & Partial<ResizeProps>
485
-
486
- export type Direction = 'nw' | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w'
487
- export type ControlItemProps = {
488
- index: ResizeControlIndex
489
- direction: Direction
490
- x: number
491
- y: number
492
- }
493
-
494
- export type PCTResizeParams = {
495
- ResizePCT: {
496
- widthPCT: number
497
- heightPCT: number
498
- }
499
- ResizeBasis: {
500
- basisWidth: number
501
- basisHeight: number
502
- }
503
- ScaleLimit: {
504
- maxScaleLimit: number
505
- minScaleLimit: number
506
- }
507
- }
508
- }
509
-
510
- export default ResizeControlGroup
@@ -1,141 +0,0 @@
1
- import { Component, ComponentType } from 'preact/compat'
2
- import { map, throttle } from 'lodash-es'
3
- import {
4
- CanvasOverlay,
5
- ToolOverlay,
6
- BackgroundOverlay,
7
- Grid,
8
- SnaplineOverlay,
9
- OutlineOverlay,
10
- BezierAdjustOverlay,
11
- ModificationOverlay,
12
- } from './overlay'
13
- import DnD from './behavior/dnd'
14
- import { observer } from '..'
15
- import Tool from '../tool'
16
- import { Options as LFOptions } from '../options'
17
- import {
18
- GraphModel,
19
- BaseEdgeModel,
20
- BaseNodeModel,
21
- SnaplineModel,
22
- } from '../model'
23
- import { EventType } from '../constant'
24
-
25
- type IGraphProps = {
26
- getView: (type: string) => ComponentType<any> | undefined
27
- tool: Tool
28
- options: LFOptions.Definition
29
- dnd: DnD
30
- snaplineModel?: SnaplineModel
31
- graphModel: GraphModel
32
- }
33
-
34
- type ContainerStyle = {
35
- width?: string
36
- height?: string
37
- }
38
-
39
- @observer
40
- class Graph extends Component<IGraphProps> {
41
- private handleResize = () => {
42
- const { graphModel, options } = this.props
43
- const { width, height, isContainerWidth, isContainerHeight } = graphModel
44
- let resizeWidth: number | undefined = width
45
- let resizeHeight: number | undefined = height
46
- let needUpdate = false
47
- if (isContainerWidth) {
48
- resizeWidth = undefined
49
- needUpdate = true
50
- }
51
- if (isContainerHeight) {
52
- resizeHeight = undefined
53
- needUpdate = true
54
- }
55
- if (needUpdate) {
56
- graphModel.resize(resizeWidth, resizeHeight)
57
- }
58
- options.width = width
59
- options.height = height
60
- }
61
- private throttleResize = throttle(this.handleResize, 200)
62
-
63
- componentDidMount() {
64
- window.addEventListener('resize', this.throttleResize)
65
- }
66
-
67
- componentDidUpdate() {
68
- const data = this.props.graphModel.modelToGraphData()
69
- this.props.graphModel.eventCenter.emit(EventType.GRAPH_UPDATED, { data })
70
- }
71
-
72
- componentWillUnmount() {
73
- window.removeEventListener('resize', this.throttleResize)
74
- }
75
-
76
- getComponent(
77
- model: BaseEdgeModel | BaseNodeModel,
78
- graphModel: GraphModel,
79
- overlay = 'canvas-overlay',
80
- ) {
81
- const { getView } = this.props
82
- // https://juejin.cn/post/7046639346656493582 - 几种方式来声明React Component的类型
83
- const View = getView(model.type)
84
- if (View) {
85
- return (
86
- <View
87
- key={model.id}
88
- model={model}
89
- graphModel={graphModel}
90
- overlay={overlay}
91
- />
92
- )
93
- }
94
- return null
95
- }
96
-
97
- render() {
98
- const { graphModel, tool, options, dnd, snaplineModel } = this.props
99
- const style: ContainerStyle = {}
100
- // 如果初始化的时候,不传宽高,则默认为父容器宽高。
101
- if (options.width) {
102
- style.width = `${graphModel.width}px`
103
- }
104
- if (options.height) {
105
- style.height = `${graphModel.height}px`
106
- }
107
- const { fakeNode, editConfigModel, background } = graphModel
108
- const { adjustEdge } = editConfigModel
109
- return (
110
- <div className="lf-graph" flow-id={graphModel.flowId} style={style}>
111
- {/* 元素层 */}
112
- <CanvasOverlay graphModel={graphModel} dnd={dnd}>
113
- <g className="lf-base">
114
- {map(graphModel.sortElements, (nodeModel) =>
115
- this.getComponent(nodeModel, graphModel),
116
- )}
117
- </g>
118
- {fakeNode ? this.getComponent(fakeNode, graphModel) : ''}
119
- </CanvasOverlay>
120
- {/* 虚线边框 */}
121
- <ModificationOverlay graphModel={graphModel}>
122
- <OutlineOverlay graphModel={graphModel} />
123
- {adjustEdge ? <BezierAdjustOverlay graphModel={graphModel} /> : ''}
124
- {options.snapline !== false ? (
125
- <SnaplineOverlay snaplineModel={snaplineModel} />
126
- ) : (
127
- ''
128
- )}
129
- </ModificationOverlay>
130
- {/* 工具层:插件 */}
131
- <ToolOverlay graphModel={graphModel} tool={tool} />
132
- {/* 画布背景 */}
133
- {background && <BackgroundOverlay background={background} />}
134
- {/* 画布网格 */}
135
- <Grid graphModel={graphModel} />
136
- </div>
137
- )
138
- }
139
- }
140
-
141
- export default Graph