index.js 6.0 KB

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