@logicflow/core 2.1.9 → 2.1.11

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.
package/lib/util/edge.js CHANGED
@@ -122,6 +122,15 @@ var pointDirection = function (point, bbox) {
122
122
  };
123
123
  exports.pointDirection = pointDirection;
124
124
  /* 获取扩展图形上的点,即起始终点相邻的点,上一个或者下一个节点 */
125
+ /**
126
+ * 计算扩展包围盒上的相邻点(起点或终点的下一个/上一个拐点)
127
+ * - 使用原始节点 bbox 来判定点相对中心的方向,避免 offset 扩展后宽高改变导致方向误判
128
+ * - 若 start 相对中心为水平方向,则返回扩展盒在 x 上的边界,y 保持不变
129
+ * - 若为垂直方向,则返回扩展盒在 y 上的边界,x 保持不变
130
+ * @param expendBBox 扩展后的包围盒(包含 offset)
131
+ * @param bbox 原始节点包围盒(用于正确的方向判定)
132
+ * @param point 起点或终点坐标
133
+ */
125
134
  var getExpandedBBoxPoint = function (expendBBox, bbox, point) {
126
135
  // https://github.com/didi/LogicFlow/issues/817
127
136
  // 没有修复前传入的参数bbox实际是expendBBox
@@ -337,7 +346,11 @@ var isSegmentCrossingBBox = function (p1, p2, bbox) {
337
346
  (0, exports.isSegmentsIntersected)(p1, p2, pc, pd));
338
347
  };
339
348
  exports.isSegmentCrossingBBox = isSegmentCrossingBBox;
340
- /* 获取下一个相邻的点 */
349
+ /**
350
+ * 基于轴对齐规则获取某点的相邻可连通点(不穿越节点)
351
+ * - 仅考虑 x 或 y 相同的候选点,保证严格水平/垂直
352
+ * - 使用 isSegmentCrossingBBox 校验线段不穿越源/目标节点
353
+ */
341
354
  var getNextNeighborPoints = function (points, point, bbox1, bbox2) {
342
355
  var neighbors = [];
343
356
  points.forEach(function (p) {
@@ -353,9 +366,12 @@ var getNextNeighborPoints = function (points, point, bbox1, bbox2) {
353
366
  return (0, exports.filterRepeatPoints)(neighbors);
354
367
  };
355
368
  exports.getNextNeighborPoints = getNextNeighborPoints;
356
- /* 路径查找,AStar查找+曼哈顿距离
357
- * 算法wiki:https://zh.wikipedia.org/wiki/A*%E6%90%9C%E5%B0%8B%E6%BC%94%E7%AE%97%E6%B3%95
358
- * 方法无法复用,且调用了很多polyline相关的方法,暂不抽离到src/algorithm中
369
+ /**
370
+ * 使用 A* + 曼哈顿启发式在候选点图上查找正交路径
371
+ * - 开放集/关闭集管理遍历
372
+ * - gScore 为累计实际代价,fScore = gScore + 启发式
373
+ * - 邻居仅为与当前点 x 或 y 相同且不穿越节点的点
374
+ * 参考:https://zh.wikipedia.org/wiki/A*%E6%90%9C%E5%B0%8B%E6%BC%94%E7%AE%97%E6%B3%95
359
375
  */
360
376
  var pathFinder = function (points, start, goal, sBBox, tBBox, os, ot) {
361
377
  // 定义已经遍历过的点
@@ -396,6 +412,7 @@ var pathFinder = function (points, start, goal, sBBox, tBBox, os, ot) {
396
412
  (0, exports.removeClosePointFromOpenList)(openSet, current);
397
413
  closedSet.push(current);
398
414
  (0, exports.getNextNeighborPoints)(points, current, sBBox, tBBox).forEach(function (neighbor) {
415
+ var _a;
399
416
  if (closedSet.indexOf(neighbor) !== -1) {
400
417
  return;
401
418
  }
@@ -403,7 +420,8 @@ var pathFinder = function (points, start, goal, sBBox, tBBox, os, ot) {
403
420
  openSet.push(neighbor);
404
421
  }
405
422
  if ((current === null || current === void 0 ? void 0 : current.id) && (neighbor === null || neighbor === void 0 ? void 0 : neighbor.id)) {
406
- var tentativeGScore = fScore[current.id] + (0, exports.estimateDistance)(current, neighbor);
423
+ // 修复:累计代价应基于 gScore[current] 而非 fScore[current]
424
+ var tentativeGScore = ((_a = gScore[current.id]) !== null && _a !== void 0 ? _a : 0) + (0, exports.estimateDistance)(current, neighbor);
407
425
  if (gScore[neighbor.id] && tentativeGScore >= gScore[neighbor.id]) {
408
426
  return;
409
427
  }
@@ -427,7 +445,10 @@ var getBoxByOriginNode = function (node) {
427
445
  return (0, _1.getNodeBBox)(node);
428
446
  };
429
447
  exports.getBoxByOriginNode = getBoxByOriginNode;
430
- /* 保证一条直线上只有2个节点: 删除x/y相同的中间节点 */
448
+ /**
449
+ * 去除共线冗余中间点,保持每条直线段仅保留两端点
450
+ * - 若三点在同一水平线或同一垂直线,移除中间点
451
+ */
431
452
  var pointFilter = function (points) {
432
453
  var i = 1;
433
454
  while (i < points.length - 1) {
@@ -445,7 +466,16 @@ var pointFilter = function (points) {
445
466
  return points;
446
467
  };
447
468
  exports.pointFilter = pointFilter;
448
- /* 计算折线点 */
469
+ /**
470
+ * 计算折线点(正交候选点 + A* 路径)
471
+ * 步骤:
472
+ * 1) 取源/目标节点的扩展包围盒与相邻点 sPoint/tPoint
473
+ * 2) 若两个扩展盒重合,使用简单路径 getSimplePoints
474
+ * 3) 构造 lineBBox/sMixBBox/tMixBBox,并收集其角点与中心交点
475
+ * 4) 过滤掉落在两个扩展盒内部的点,形成 connectPoints
476
+ * 5) 以 sPoint/tPoint 为起止,用 A* 查找路径
477
+ * 6) 拼入原始 start/end,并用 pointFilter 去除冗余共线点
478
+ */
449
479
  var getPolylinePoints = function (start, end, sNode, tNode, offset) {
450
480
  var sBBox = (0, exports.getBoxByOriginNode)(sNode);
451
481
  var tBBox = (0, exports.getBoxByOriginNode)(tNode);
@@ -589,6 +619,10 @@ var points2PointsList = function (points) {
589
619
  return pointsList;
590
620
  };
591
621
  exports.points2PointsList = points2PointsList;
622
+ /**
623
+ * 当扩展 bbox 重合时的简化拐点计算
624
+ * - 根据起止段的方向(水平/垂直)插入 1~2 个中间点,避免折线重合与穿越
625
+ */
592
626
  var getSimplePoints = function (start, end, sPoint, tPoint) {
593
627
  var points = [];
594
628
  // start,sPoint的方向,水平或者垂直,即路径第一条线段的方向
@@ -106,6 +106,9 @@ var CanvasOverlay = /** @class */ (function (_super) {
106
106
  if (selectElements.size > 0) {
107
107
  graphModel.clearSelectElements();
108
108
  }
109
+ // 如果是拖拽状态,不触发点击事件
110
+ if (_this.state.isDragging)
111
+ return;
109
112
  graphModel.eventCenter.emit(constant_1.EventType.BLANK_CLICK, { e: ev });
110
113
  }
111
114
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logicflow/core",
3
- "version": "2.1.9",
3
+ "version": "2.1.11",
4
4
  "description": "LogicFlow, help you quickly create flowcharts",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
@@ -34,13 +34,154 @@ export class PolylineEdgeModel extends BaseEdgeModel {
34
34
  @observable dbClickPosition?: Point
35
35
 
36
36
  initEdgeData(data: LogicFlow.EdgeConfig): void {
37
- this.offset = get(data, 'properties.offset', 30)
37
+ const providedOffset = get(data, 'properties.offset')
38
+ // 当用户未传入 offset 时,按“箭头与折线重叠长度 + 5”作为默认值
39
+ // 其中“重叠长度”采用箭头样式中的 offset(沿边方向的长度)
40
+ this.offset =
41
+ typeof providedOffset === 'number'
42
+ ? providedOffset
43
+ : this.getDefaultOffset()
38
44
  if (data.pointsList) {
39
- this.pointsList = data.pointsList
45
+ const corrected = this.orthogonalizePath(data.pointsList)
46
+ ;(data as any).pointsList = corrected
47
+ this.pointsList = corrected
40
48
  }
41
49
  super.initEdgeData(data)
42
50
  }
43
51
 
52
+ setAttributes() {
53
+ const { offset: newOffset } = this.properties
54
+ if (newOffset && newOffset !== this.offset) {
55
+ this.offset = newOffset
56
+ this.updatePoints()
57
+ }
58
+ }
59
+
60
+ orthogonalizePath(points: Point[]): Point[] {
61
+ // 输入非法或不足两点时直接返回副本
62
+ if (!Array.isArray(points) || points.length < 2) {
63
+ return points
64
+ }
65
+ // pushUnique: 向数组中添加唯一点,避免重复
66
+ const pushUnique = (arr: Point[], p: Point) => {
67
+ const last = arr[arr.length - 1]
68
+ if (!last || last.x !== p.x || last.y !== p.y) {
69
+ arr.push({ x: p.x, y: p.y })
70
+ }
71
+ }
72
+ // isAxisAligned: 检查两点是否在同一条轴上
73
+ const isAxisAligned = (a: Point, b: Point) => a.x === b.x || a.y === b.y
74
+ // manhattanDistance: 计算两点在曼哈顿距离上的距离
75
+ const manhattanDistance = (a: Point, b: Point) =>
76
+ Math.abs(a.x - b.x) + Math.abs(a.y - b.y)
77
+
78
+ // 1) 生成严格正交路径,尽量延续前一段方向以减少折点
79
+ const orthogonal: Point[] = []
80
+ pushUnique(orthogonal, points[0])
81
+ // previousDirection: 记录前一段的方向,用于判断当前段的PreferredCorner
82
+ let previousDirection: SegmentDirection | undefined
83
+ // 遍历所有点对,生成正交路径
84
+ for (let i = 0; i < points.length - 1; i++) {
85
+ const current = orthogonal[orthogonal.length - 1]
86
+ const next = points[i + 1]
87
+ if (!current || !next) continue
88
+
89
+ if (isAxisAligned(current, next)) {
90
+ pushUnique(orthogonal, next)
91
+ previousDirection =
92
+ current.x === next.x
93
+ ? SegmentDirection.VERTICAL
94
+ : SegmentDirection.HORIZONTAL
95
+ continue
96
+ }
97
+
98
+ const cornerHV: Point = { x: next.x, y: current.y }
99
+ const cornerVH: Point = { x: current.x, y: next.y }
100
+
101
+ // 根据前一段的方向,优先选择能延续该方向的拐角点,以减少折点数量;
102
+ // 若前一段为垂直方向,则优先选择垂直-水平拐角(cornerVH);
103
+ // 若前一段为水平方向,则优先选择水平-垂直拐角(cornerHV);
104
+ // 若前一段无方向(初始情况),则比较两个拐角的曼哈顿距离,选更近者。
105
+ const preferredCorner =
106
+ previousDirection === SegmentDirection.VERTICAL
107
+ ? cornerVH
108
+ : previousDirection === SegmentDirection.HORIZONTAL
109
+ ? cornerHV
110
+ : manhattanDistance(current, cornerHV) <=
111
+ manhattanDistance(current, cornerVH)
112
+ ? cornerHV
113
+ : cornerVH
114
+
115
+ if (preferredCorner.x !== current.x || preferredCorner.y !== current.y) {
116
+ pushUnique(orthogonal, preferredCorner)
117
+ }
118
+ pushUnique(orthogonal, next)
119
+
120
+ const a = orthogonal[orthogonal.length - 2]
121
+ const b = orthogonal[orthogonal.length - 1]
122
+ previousDirection =
123
+ a && b
124
+ ? a.x === b.x
125
+ ? SegmentDirection.VERTICAL
126
+ : SegmentDirection.HORIZONTAL
127
+ : previousDirection
128
+ }
129
+
130
+ // 2) 去除冗余共线中间点
131
+ const simplified: Point[] = []
132
+ for (let i = 0; i < orthogonal.length; i++) {
133
+ const prev = orthogonal[i - 1]
134
+ const curr = orthogonal[i]
135
+ const next = orthogonal[i + 1]
136
+ // 如果当前点与前一个点和后一个点在同一条水平线或垂直线上,则跳过该点,去除冗余的共线中间点
137
+ if (
138
+ prev &&
139
+ curr &&
140
+ next &&
141
+ ((prev.x === curr.x && curr.x === next.x) || // 水平共线
142
+ (prev.y === curr.y && curr.y === next.y)) // 垂直共线
143
+ ) {
144
+ continue
145
+ }
146
+ pushUnique(simplified, curr)
147
+ }
148
+
149
+ // 3) 保留原始起点与终点位置
150
+ if (simplified.length >= 2) {
151
+ simplified[0] = { x: points[0].x, y: points[0].y }
152
+ simplified[simplified.length - 1] = {
153
+ x: points[points.length - 1].x,
154
+ y: points[points.length - 1].y,
155
+ }
156
+ }
157
+
158
+ // 4) 结果校验:任意相邻段都必须为水平/垂直;失败则退化为起止两点
159
+ const isOrthogonal =
160
+ simplified.length < 2 ||
161
+ simplified.every((_, idx, arr) => {
162
+ if (idx === 0) return true
163
+ return isAxisAligned(arr[idx - 1], arr[idx])
164
+ })
165
+
166
+ return isOrthogonal
167
+ ? simplified
168
+ : [
169
+ { x: points[0].x, y: points[0].y },
170
+ { x: points[points.length - 1].x, y: points[points.length - 1].y },
171
+ ]
172
+ }
173
+
174
+ /**
175
+ * 计算默认 offset:箭头与折线重叠长度 + 5
176
+ * 重叠长度采用箭头样式中的 offset(沿边方向的长度)
177
+ */
178
+ private getDefaultOffset(): number {
179
+ const arrowStyle = this.getArrowStyle()
180
+ const arrowOverlap =
181
+ typeof arrowStyle.offset === 'number' ? arrowStyle.offset : 0
182
+ return arrowOverlap + 5
183
+ }
184
+
44
185
  getEdgeStyle() {
45
186
  const { polyline } = this.graphModel.theme
46
187
  const style = super.getEdgeStyle()
@@ -319,7 +460,7 @@ export class PolylineEdgeModel extends BaseEdgeModel {
319
460
  }
320
461
 
321
462
  updatePath(pointList: Point[]) {
322
- this.pointsList = pointList
463
+ this.pointsList = this.orthogonalizePath(pointList)
323
464
  this.points = this.getPath(this.pointsList)
324
465
  }
325
466
 
@@ -362,8 +503,10 @@ export class PolylineEdgeModel extends BaseEdgeModel {
362
503
  this.targetNode,
363
504
  this.offset || 0,
364
505
  )
365
- this.pointsList = pointsList
366
- this.points = pointsList.map((point) => `${point.x},${point.y}`).join(' ')
506
+ this.pointsList = this.orthogonalizePath(pointsList)
507
+ this.points = this.pointsList
508
+ .map((point) => `${point.x},${point.y}`)
509
+ .join(' ')
367
510
  }
368
511
 
369
512
  @action
@@ -651,18 +794,20 @@ export class PolylineEdgeModel extends BaseEdgeModel {
651
794
  sourceNode: BaseNodeModel
652
795
  targetNode: BaseNodeModel
653
796
  }) {
654
- this.pointsList = getPolylinePoints(
655
- {
656
- x: startPoint.x,
657
- y: startPoint.y,
658
- },
659
- {
660
- x: endPoint.x,
661
- y: endPoint.y,
662
- },
663
- sourceNode,
664
- targetNode,
665
- this.offset || 0,
797
+ this.pointsList = this.orthogonalizePath(
798
+ getPolylinePoints(
799
+ {
800
+ x: startPoint.x,
801
+ y: startPoint.y,
802
+ },
803
+ {
804
+ x: endPoint.x,
805
+ y: endPoint.y,
806
+ },
807
+ sourceNode,
808
+ targetNode,
809
+ this.offset || 0,
810
+ ),
666
811
  )
667
812
 
668
813
  this.initPoints()
@@ -27,6 +27,13 @@ export default class MultipleSelect extends Component<IToolProps> {
27
27
  }
28
28
 
29
29
  handleMouseDown = (ev: MouseEvent) => {
30
+ // 多选区域的拖拽步长随缩放变化
31
+ const {
32
+ graphModel: { gridSize },
33
+ lf,
34
+ } = this.props
35
+ const { SCALE_X } = lf.getTransform()
36
+ this.stepDrag.setStep(gridSize * SCALE_X)
30
37
  this.stepDrag.handleMouseDown(ev)
31
38
  }
32
39
  // 使多选区域的滚轮事件可以触发画布的滚轮事件
package/src/util/edge.ts CHANGED
@@ -107,6 +107,15 @@ export const pointDirection = (point: Point, bbox: BoxBounds): Direction => {
107
107
  }
108
108
 
109
109
  /* 获取扩展图形上的点,即起始终点相邻的点,上一个或者下一个节点 */
110
+ /**
111
+ * 计算扩展包围盒上的相邻点(起点或终点的下一个/上一个拐点)
112
+ * - 使用原始节点 bbox 来判定点相对中心的方向,避免 offset 扩展后宽高改变导致方向误判
113
+ * - 若 start 相对中心为水平方向,则返回扩展盒在 x 上的边界,y 保持不变
114
+ * - 若为垂直方向,则返回扩展盒在 y 上的边界,x 保持不变
115
+ * @param expendBBox 扩展后的包围盒(包含 offset)
116
+ * @param bbox 原始节点包围盒(用于正确的方向判定)
117
+ * @param point 起点或终点坐标
118
+ */
110
119
  export const getExpandedBBoxPoint = (
111
120
  expendBBox: BoxBounds,
112
121
  bbox: BoxBounds,
@@ -377,7 +386,11 @@ export const isSegmentCrossingBBox = (
377
386
  )
378
387
  }
379
388
 
380
- /* 获取下一个相邻的点 */
389
+ /**
390
+ * 基于轴对齐规则获取某点的相邻可连通点(不穿越节点)
391
+ * - 仅考虑 x 或 y 相同的候选点,保证严格水平/垂直
392
+ * - 使用 isSegmentCrossingBBox 校验线段不穿越源/目标节点
393
+ */
381
394
  export const getNextNeighborPoints = (
382
395
  points: Point[],
383
396
  point: Point,
@@ -400,9 +413,12 @@ export const getNextNeighborPoints = (
400
413
  return filterRepeatPoints(neighbors)
401
414
  }
402
415
 
403
- /* 路径查找,AStar查找+曼哈顿距离
404
- * 算法wiki:https://zh.wikipedia.org/wiki/A*%E6%90%9C%E5%B0%8B%E6%BC%94%E7%AE%97%E6%B3%95
405
- * 方法无法复用,且调用了很多polyline相关的方法,暂不抽离到src/algorithm中
416
+ /**
417
+ * 使用 A* + 曼哈顿启发式在候选点图上查找正交路径
418
+ * - 开放集/关闭集管理遍历
419
+ * - gScore 为累计实际代价,fScore = gScore + 启发式
420
+ * - 邻居仅为与当前点 x 或 y 相同且不穿越节点的点
421
+ * 参考:https://zh.wikipedia.org/wiki/A*%E6%90%9C%E5%B0%8B%E6%BC%94%E7%AE%97%E6%B3%95
406
422
  */
407
423
  export const pathFinder = (
408
424
  points: Point[],
@@ -474,8 +490,9 @@ export const pathFinder = (
474
490
  }
475
491
 
476
492
  if (current?.id && neighbor?.id) {
493
+ // 修复:累计代价应基于 gScore[current] 而非 fScore[current]
477
494
  const tentativeGScore =
478
- fScore[current.id] + estimateDistance(current, neighbor)
495
+ (gScore[current.id] ?? 0) + estimateDistance(current, neighbor)
479
496
  if (gScore[neighbor.id] && tentativeGScore >= gScore[neighbor.id]) {
480
497
  return
481
498
  }
@@ -494,7 +511,10 @@ export const pathFinder = (
494
511
  export const getBoxByOriginNode = (node: BaseNodeModel): BoxBounds => {
495
512
  return getNodeBBox(node)
496
513
  }
497
- /* 保证一条直线上只有2个节点: 删除x/y相同的中间节点 */
514
+ /**
515
+ * 去除共线冗余中间点,保持每条直线段仅保留两端点
516
+ * - 若三点在同一水平线或同一垂直线,移除中间点
517
+ */
498
518
  export const pointFilter = (points: Point[]): Point[] => {
499
519
  let i = 1
500
520
  while (i < points.length - 1) {
@@ -513,7 +533,16 @@ export const pointFilter = (points: Point[]): Point[] => {
513
533
  return points
514
534
  }
515
535
 
516
- /* 计算折线点 */
536
+ /**
537
+ * 计算折线点(正交候选点 + A* 路径)
538
+ * 步骤:
539
+ * 1) 取源/目标节点的扩展包围盒与相邻点 sPoint/tPoint
540
+ * 2) 若两个扩展盒重合,使用简单路径 getSimplePoints
541
+ * 3) 构造 lineBBox/sMixBBox/tMixBBox,并收集其角点与中心交点
542
+ * 4) 过滤掉落在两个扩展盒内部的点,形成 connectPoints
543
+ * 5) 以 sPoint/tPoint 为起止,用 A* 查找路径
544
+ * 6) 拼入原始 start/end,并用 pointFilter 去除冗余共线点
545
+ */
517
546
  export const getPolylinePoints = (
518
547
  start: Point,
519
548
  end: Point,
@@ -699,6 +728,10 @@ export const points2PointsList = (points: string): Point[] => {
699
728
  return pointsList
700
729
  }
701
730
 
731
+ /**
732
+ * 当扩展 bbox 重合时的简化拐点计算
733
+ * - 根据起止段的方向(水平/垂直)插入 1~2 个中间点,避免折线重合与穿越
734
+ */
702
735
  export const getSimplePoints = (
703
736
  start: Point,
704
737
  end: Point,
@@ -105,6 +105,8 @@ export class CanvasOverlay extends Component<IProps, IState> {
105
105
  if (selectElements.size > 0) {
106
106
  graphModel.clearSelectElements()
107
107
  }
108
+ // 如果是拖拽状态,不触发点击事件
109
+ if (this.state.isDragging) return
108
110
  graphModel.eventCenter.emit(EventType.BLANK_CLICK, { e: ev })
109
111
  }
110
112
  }