| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- var __read = (this && this.__read) || function (o, n) {
- var m = typeof Symbol === "function" && o[Symbol.iterator];
- if (!m) return o;
- var i = m.call(o), r, ar = [], e;
- try {
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
- }
- catch (error) { e = { error: error }; }
- finally {
- try {
- if (r && !r.done && (m = i["return"])) m.call(i);
- }
- finally { if (e) throw e.error; }
- }
- return ar;
- };
- var __spread = (this && this.__spread) || function () {
- for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
- return ar;
- };
- import { cloneDeep } from 'lodash-es';
- import { isNodeInSegment } from './edge';
- var InsertNodeInPolyline = /** @class */ (function () {
- function InsertNodeInPolyline(_a) {
- var lf = _a.lf;
- this._lf = lf;
- // fix https://github.com/didi/LogicFlow/issues/754
- this.deviation = 20;
- this.dndAdd = true;
- this.dropAdd = true;
- this.eventHandler();
- }
- InsertNodeInPolyline.prototype.eventHandler = function () {
- var _this = this;
- // 监听事件
- if (this.dndAdd) {
- this._lf.on('node:dnd-add', function (_a) {
- var data = _a.data;
- _this.insetNode(data);
- });
- }
- if (this.dropAdd) {
- this._lf.on('node:drop', function (_a) {
- var data = _a.data;
- var edges = _this._lf.graphModel.edges;
- var id = data.id;
- // 只有游离节点才能插入到连线上
- var pureNode = true;
- for (var i = 0; i < edges.length; i++) {
- if (edges[i].sourceNodeId === id || edges[i].targetNodeId === id) {
- pureNode = false;
- break;
- }
- }
- if (pureNode) {
- _this.insetNode(data);
- }
- });
- }
- };
- InsertNodeInPolyline.prototype.insetNode = function (nodeData) {
- var edges = this._lf.graphModel.edges;
- var nodeModel = this._lf.getNodeModelById(nodeData.id);
- for (var i = 0; i < edges.length; i++) {
- // eslint-disable-next-line max-len
- var _a = isNodeInSegment(nodeModel, edges[i], this.deviation), crossIndex = _a.crossIndex, crossPoints = _a.crossPoints;
- if (crossIndex >= 0) {
- var _b = edges[i], sourceNodeId = _b.sourceNodeId, targetNodeId = _b.targetNodeId, id = _b.id, type = _b.type, pointsList = _b.pointsList;
- // fix https://github.com/didi/LogicFlow/issues/996
- var startPoint = cloneDeep(pointsList[0]);
- var endPoint = cloneDeep(crossPoints.startCrossPoint);
- this._lf.addEdge({
- type: type,
- sourceNodeId: sourceNodeId,
- targetNodeId: nodeData.id,
- startPoint: startPoint,
- endPoint: endPoint,
- pointsList: __spread(pointsList.slice(0, crossIndex), [crossPoints.startCrossPoint]),
- });
- this._lf.addEdge({
- type: type,
- sourceNodeId: nodeData.id,
- targetNodeId: targetNodeId,
- startPoint: cloneDeep(crossPoints.endCrossPoint),
- endPoint: cloneDeep(pointsList[pointsList.length - 1]),
- pointsList: __spread([crossPoints.endCrossPoint], pointsList.slice(crossIndex)),
- });
- this._lf.deleteEdge(id);
- break;
- }
- }
- };
- InsertNodeInPolyline.pluginName = 'insertNodeInPolyline';
- return InsertNodeInPolyline;
- }());
- export { InsertNodeInPolyline };
- export default InsertNodeInPolyline;
|