index.js 3.8 KB

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