index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. var __spread = (this && this.__spread) || function () {
  19. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  20. return ar;
  21. };
  22. Object.defineProperty(exports, "__esModule", { value: true });
  23. exports.InsertNodeInPolyline = void 0;
  24. var lodash_es_1 = require("lodash-es");
  25. var edge_1 = require("./edge");
  26. var InsertNodeInPolyline = /** @class */ (function () {
  27. function InsertNodeInPolyline(_a) {
  28. var lf = _a.lf;
  29. this._lf = lf;
  30. // fix https://github.com/didi/LogicFlow/issues/754
  31. this.deviation = 20;
  32. this.dndAdd = true;
  33. this.dropAdd = true;
  34. this.eventHandler();
  35. }
  36. InsertNodeInPolyline.prototype.eventHandler = function () {
  37. var _this = this;
  38. // 监听事件
  39. if (this.dndAdd) {
  40. this._lf.on('node:dnd-add', function (_a) {
  41. var data = _a.data;
  42. _this.insetNode(data);
  43. });
  44. }
  45. if (this.dropAdd) {
  46. this._lf.on('node:drop', function (_a) {
  47. var data = _a.data;
  48. var edges = _this._lf.graphModel.edges;
  49. var id = data.id;
  50. // 只有游离节点才能插入到连线上
  51. var pureNode = true;
  52. for (var i = 0; i < edges.length; i++) {
  53. if (edges[i].sourceNodeId === id || edges[i].targetNodeId === id) {
  54. pureNode = false;
  55. break;
  56. }
  57. }
  58. if (pureNode) {
  59. _this.insetNode(data);
  60. }
  61. });
  62. }
  63. };
  64. InsertNodeInPolyline.prototype.insetNode = function (nodeData) {
  65. var edges = this._lf.graphModel.edges;
  66. var nodeModel = this._lf.getNodeModelById(nodeData.id);
  67. for (var i = 0; i < edges.length; i++) {
  68. // eslint-disable-next-line max-len
  69. var _a = edge_1.isNodeInSegment(nodeModel, edges[i], this.deviation), crossIndex = _a.crossIndex, crossPoints = _a.crossPoints;
  70. if (crossIndex >= 0) {
  71. var _b = edges[i], sourceNodeId = _b.sourceNodeId, targetNodeId = _b.targetNodeId, id = _b.id, type = _b.type, pointsList = _b.pointsList;
  72. // fix https://github.com/didi/LogicFlow/issues/996
  73. var startPoint = lodash_es_1.cloneDeep(pointsList[0]);
  74. var endPoint = lodash_es_1.cloneDeep(crossPoints.startCrossPoint);
  75. this._lf.addEdge({
  76. type: type,
  77. sourceNodeId: sourceNodeId,
  78. targetNodeId: nodeData.id,
  79. startPoint: startPoint,
  80. endPoint: endPoint,
  81. pointsList: __spread(pointsList.slice(0, crossIndex), [crossPoints.startCrossPoint]),
  82. });
  83. this._lf.addEdge({
  84. type: type,
  85. sourceNodeId: nodeData.id,
  86. targetNodeId: targetNodeId,
  87. startPoint: lodash_es_1.cloneDeep(crossPoints.endCrossPoint),
  88. endPoint: lodash_es_1.cloneDeep(pointsList[pointsList.length - 1]),
  89. pointsList: __spread([crossPoints.endCrossPoint], pointsList.slice(crossIndex)),
  90. });
  91. this._lf.deleteEdge(id);
  92. break;
  93. }
  94. }
  95. };
  96. InsertNodeInPolyline.pluginName = 'insertNodeInPolyline';
  97. return InsertNodeInPolyline;
  98. }());
  99. exports.InsertNodeInPolyline = InsertNodeInPolyline;
  100. exports.default = InsertNodeInPolyline;