index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = function (d, b) {
  3. extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return extendStatics(d, b);
  7. };
  8. return function (d, b) {
  9. extendStatics(d, b);
  10. function __() { this.constructor = d; }
  11. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  12. };
  13. })();
  14. var __assign = (this && this.__assign) || function () {
  15. __assign = Object.assign || function(t) {
  16. for (var s, i = 1, n = arguments.length; i < n; i++) {
  17. s = arguments[i];
  18. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  19. t[p] = s[p];
  20. }
  21. return t;
  22. };
  23. return __assign.apply(this, arguments);
  24. };
  25. var __read = (this && this.__read) || function (o, n) {
  26. var m = typeof Symbol === "function" && o[Symbol.iterator];
  27. if (!m) return o;
  28. var i = m.call(o), r, ar = [], e;
  29. try {
  30. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  31. }
  32. catch (error) { e = { error: error }; }
  33. finally {
  34. try {
  35. if (r && !r.done && (m = i["return"])) m.call(i);
  36. }
  37. finally { if (e) throw e.error; }
  38. }
  39. return ar;
  40. };
  41. import { PolylineEdge, PolylineEdgeModel, h } from '@logicflow/core';
  42. import searchMiddleIndex from './searchMiddleIndex';
  43. var CurvedEdge = /** @class */ (function (_super) {
  44. __extends(CurvedEdge, _super);
  45. function CurvedEdge() {
  46. return _super !== null && _super.apply(this, arguments) || this;
  47. }
  48. CurvedEdge.prototype.pointFilter = function (points) {
  49. var allPoints = points;
  50. var i = 1;
  51. while (i < allPoints.length - 1) {
  52. var _a = __read(allPoints[i - 1], 2), x = _a[0], y = _a[1];
  53. var _b = __read(allPoints[i], 2), x1 = _b[0], y1 = _b[1];
  54. var _c = __read(allPoints[i + 1], 2), x2 = _c[0], y2 = _c[1];
  55. if ((x === x1 && x1 === x2)
  56. || (y === y1 && y1 === y2)) {
  57. allPoints.splice(i, 1);
  58. }
  59. else {
  60. i++;
  61. }
  62. }
  63. return allPoints;
  64. };
  65. CurvedEdge.prototype.getEdge = function () {
  66. var model = this.props.model;
  67. var points = model.points, isAnimation = model.isAnimation, arrowConfig = model.arrowConfig, _a = model.radius, radius = _a === void 0 ? 5 : _a;
  68. var style = model.getEdgeStyle();
  69. var animationStyle = model.getEdgeAnimationStyle();
  70. var points2 = this.pointFilter(points.split(' ').map(function (p) { return p.split(',').map(function (a) { return Number(a); }); }));
  71. var res = searchMiddleIndex(points2);
  72. if (res) {
  73. var _b = __read(res, 2), first = _b[0], last = _b[1];
  74. var firstPoint = points2[first];
  75. var lastPoint_1 = points2[last];
  76. var flag = firstPoint.some(function (num, index) { return num === lastPoint_1[index]; });
  77. if (!flag) {
  78. var diff = (lastPoint_1[1] - firstPoint[1]) / 2;
  79. var firstNextPoint = [lastPoint_1[0], lastPoint_1[1] - diff];
  80. var lastPrePoint = [firstPoint[0], firstPoint[1] + diff];
  81. points2.splice(first + 1, 0, lastPrePoint, firstNextPoint);
  82. }
  83. }
  84. var _c = __read(points2[0], 2), startX = _c[0], startY = _c[1];
  85. var d = "M" + startX + " " + startY;
  86. // 1) 如果一个点不为开始和结束,则在这个点的前后增加弧度开始和结束点。
  87. // 2) 判断这个点与前一个点的坐标
  88. // 如果x相同则前一个点的x也不变,
  89. // y为(这个点的y 大于前一个点的y, 则 为 这个点的y - 5;小于前一个点的y, 则为这个点的y+5)
  90. // 同理,判断这个点与后一个点的x,y是否相同,如果x相同,则y进行加减,如果y相同,则x进行加减
  91. for (var i = 1; i < points2.length - 1; i++) {
  92. var _d = __read(points2[i - 1], 2), preX = _d[0], preY = _d[1];
  93. var _e = __read(points2[i], 2), currentX = _e[0], currentY = _e[1];
  94. var _f = __read(points2[i + 1], 2), nextX = _f[0], nextY = _f[1];
  95. if (currentX === preX && currentY !== preY) {
  96. var y = currentY > preY ? currentY - radius : currentY + radius;
  97. d = d + " L " + currentX + " " + y;
  98. }
  99. if (currentY === preY && currentX !== preX) {
  100. var x = currentX > preX ? currentX - radius : currentX + radius;
  101. d = d + " L " + x + " " + currentY;
  102. }
  103. d = d + " Q " + currentX + " " + currentY;
  104. if (currentX === nextX && currentY !== nextY) {
  105. var y = currentY > nextY ? currentY - radius : currentY + radius;
  106. d = d + " " + currentX + " " + y;
  107. }
  108. if (currentY === nextY && currentX !== nextX) {
  109. var x = currentX > nextX ? currentX - radius : currentX + radius;
  110. d = d + " " + x + " " + currentY;
  111. }
  112. }
  113. var _g = __read(points2[points2.length - 1], 2), endX = _g[0], endY = _g[1];
  114. d = d + " L " + endX + " " + endY;
  115. var attrs = __assign(__assign(__assign({ d: d, style: isAnimation ? animationStyle : {} }, style), arrowConfig), { fill: 'none' });
  116. return h('path', __assign({ d: d }, attrs));
  117. };
  118. return CurvedEdge;
  119. }(PolylineEdge));
  120. var CurvedEdgeModel = /** @class */ (function (_super) {
  121. __extends(CurvedEdgeModel, _super);
  122. function CurvedEdgeModel() {
  123. return _super !== null && _super.apply(this, arguments) || this;
  124. }
  125. return CurvedEdgeModel;
  126. }(PolylineEdgeModel));
  127. export { CurvedEdge,
  128. // CurvedEdgeView,
  129. CurvedEdgeModel, };