UNPKG

wetrade-design

Version:

一款多语言支持Vue3的UI框架

265 lines 10.3 kB
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; /** * User: CHT * Date: 2020/5/8 * Time: 14:01 */ import { uuid, vector, mark } from './index'; import { Direction, DIRECTION_VECTOR } from './types'; function calcMatrix(point1, point2, isMerge) { var pathVerticalVec = [0, point2[1] - point1[1]]; var pointVector = isMerge ? GraphLink.distance_point_merge_vector : GraphLink.distance_point_vector; // distance 点 var distancePoint = vector(point2).minus(pointVector).end; // 拐弯点 var inflectionPoint = vector(distancePoint).minus(pathVerticalVec).end; return [point1, inflectionPoint, distancePoint, point2]; } var GraphLink = /*#__PURE__*/function () { function GraphLink(options, graph) { _classCallCheck(this, GraphLink); var start = options.start, _options$end = options.end, end = _options$end === void 0 ? null : _options$end, _options$startAt = options.startAt, startAt = _options$startAt === void 0 ? [0, 0] : _options$startAt, _options$endAt = options.endAt, endAt = _options$endAt === void 0 ? [0, 0] : _options$endAt, _options$meta = options.meta, meta = _options$meta === void 0 ? null : _options$meta; this.$options = options; var id = options[mark.relationMark] || uuid('link'); this.key = uuid('link'); this[mark.relationMark] = id; this.graph = graph; this.start = start; this.meta = meta; this.end = end; this.startDirection = DIRECTION_VECTOR[Direction.TOP]; this.endDirection = DIRECTION_VECTOR[Direction.TOP]; this.startAt = startAt; this.endAt = endAt; } _createClass(GraphLink, [{ key: "end", get: function get() { return this._end; }, set: function set(node) { if (this.start !== node) { this._end = node; } } }, { key: "startAt", get: function get() { return this._startAt; }, set: function set(offset) { var relative = this.start.relative(offset); this._startAt = relative.position; this.startDirection = relative.direction; } }, { key: "endAt", get: function get() { return this._endAt; }, set: function set(offset) { if (this.end) { var relative = this.end.relative(offset); this._endAt = relative.position; this.endDirection = relative.direction; } else { this._endAt = offset; } } }, { key: "movePosition", get: function get() { return this._movePosition; }, set: function set(offset) { this._movePosition = offset; if (this.end) return; var relative = this.start.relative(vector(offset).minus(this.graph.origin).minus(this.start.coordinate).end); this.endDirection = vector(relative.direction).multiply(-1).end; } }, { key: "pathPointList", get: function get() { var pointList = this.coordinateList(), xList = [], yList = []; pointList.forEach(function (item) { xList.push(item[0]); yList.push(item[1]); }); return { pointList: pointList, xList: xList, yList: yList, minX: Math.min.apply(Math, xList), maxX: Math.max.apply(Math, xList), minY: Math.min.apply(Math, yList), maxY: Math.max.apply(Math, yList) }; } }, { key: "startCoordinate", value: function startCoordinate() { return vector(this.start.position).add(this.startAt).end; } }, { key: "endCoordinate", value: function endCoordinate() { if (this.end) { return vector(this.end.position).add(this.endAt).end; } else { return this.movePosition; } } }, { key: "coordinateList", value: function coordinateList() { var _this$meta$stationPoi, _this$meta, _this$meta$stationPoi2; // const entryPoint = this.startCoordinate(); // const exitPoint = this.endCoordinate(); // const entryDirection = this.startDirection; // let exitDirection = this.endDirection; // const startPoint = vector(entryDirection).multiply(GraphLink.distance).add(entryPoint).end; // // 路径终点 // const endPoint = vector(exitDirection).multiply(GraphLink.distance).add(exitPoint).end; // // 入口方向取反 // exitDirection = vector(exitDirection).multiply(-1).end; // // 终点 - 起点 水平向量 // const pathHorizontalVec = [endPoint[0] - startPoint[0], 0]; // // 终点 - 起点 垂直向量 // const pathVerticalVec = [0, endPoint[1] - startPoint[1]]; // const startDirection = this.pathDirection(pathVerticalVec, pathHorizontalVec, entryDirection); // const endDirection = this.pathDirection(pathVerticalVec, pathHorizontalVec, exitDirection); // const splitNum = vector(startDirection).dotProduct(endDirection).end > 0 ? 2 : 1; // const pathMiddle = endDirection === pathHorizontalVec ? pathVerticalVec : pathHorizontalVec; // const points = []; // points.push(entryPoint, startPoint); // if (splitNum === 1) { // const point1 = vector(startPoint).add(startDirection).end; // const point2 = vector(point1).add(endDirection).end; // points.push(point1, point2); // } else { // const point1 = vector(startDirection).multiply(turnRatio).add(startPoint).end; // const point2 = vector(point1).add(pathMiddle).end; // const point3 = vector(endDirection) // .multiply(1 - turnRatio) // .add(point2).end; // points.push(point1, point2, point3); // } // points.push(exitPoint); /** * 只需要4个点 * 起点、终点 * 同一水平线距离终点前distance的距离的点 * 与起点同一水平线且与第三个点垂直线相同的点 */ var points = []; // 起点 var entryPoint = this.startCoordinate(); // 终点 var exitPoint = this.endCoordinate(); var stationPoint = (_this$meta$stationPoi = (_this$meta = this.meta) === null || _this$meta === void 0 ? void 0 : (_this$meta$stationPoi2 = _this$meta.stationPoint) === null || _this$meta$stationPoi2 === void 0 ? void 0 : _this$meta$stationPoi2.map(function (item) { return [exitPoint[0] - item[0], exitPoint[1] - item[1]]; })) !== null && _this$meta$stationPoi !== void 0 ? _this$meta$stationPoi : []; var list = [entryPoint].concat(_toConsumableArray(stationPoint), [exitPoint]); // 给定两个点坐标,得出4个点 for (var i = 1; i < list.length; i++) { var _this$meta2; var point1 = list[i - 1]; var point2 = list[i]; var isLast = i === list.length - 1; var isMerge = isLast && ((_this$meta2 = this.meta) === null || _this$meta2 === void 0 ? void 0 : _this$meta2.activeId) !== -1; points.push.apply(points, _toConsumableArray(calcMatrix(point1, point2, isMerge))); } // // // 水平向量 // // const pathHorizontalVec = [exitPoint[0] - entryPoint[0], 0]; // // 终点 - 起点 垂直向量 // const pathVerticalVec = [0, exitPoint[1] - entryPoint[1]]; // const pointVector = // this.meta?.activeId === -1 // ? GraphLink.distance_point_vector // : GraphLink.distance_point_merge_vector; // // distance 点 // const distancePoint = vector(exitPoint).minus(pointVector).end; // // 拐弯点 // const inflectionPoint = vector(distancePoint).minus(pathVerticalVec).end; // points.push(entryPoint, inflectionPoint, distancePoint, exitPoint); return points; } }, { key: "pathDirection", value: function pathDirection(vertical, horizontal, Direction) { if (vector(horizontal).parallel(Direction).end) { if (vector(horizontal).dotProduct(Direction).end > 0) { return horizontal; } else { return vertical; } } else { if (vector(vertical).dotProduct(Direction).end > 0) { return vertical; } else { return horizontal; } } } // 鼠标是否移入线段 }, { key: "isPointInLink", value: function isPointInLink(position, pathPointList) { var _ref = pathPointList || this.pathPointList, pointList = _ref.pointList, minX = _ref.minX, minY = _ref.minY, maxX = _ref.maxX, maxY = _ref.maxY; var n = 5; if (position[0] < minX - n || position[0] > maxX + n || position[1] < minY - n || position[1] > maxY + n) { return false; } for (var i = 0; i < pointList.length - 2; i++) { var prev = pointList[i]; var current = pointList[i + 1]; var top = Math.min(prev[1], current[1]) - n; var right = Math.max(prev[0], current[0]) + n; var bottom = Math.max(prev[1], current[1]) + n; var left = Math.min(prev[0], current[0]) - n; var _position = _slicedToArray(position, 2), x = _position[0], y = _position[1]; if (x > left && x < right && y > top && y < bottom) { return true; } } return false; } }, { key: "remove", value: function remove() { return this.graph.removeLink(this); } }, { key: "toJSON", value: function toJSON() { var _ref2; return _ref2 = {}, _defineProperty(_ref2, mark.relationMark, this[mark.relationMark]), _defineProperty(_ref2, mark.startMark, this.start[mark.relationMark]), _defineProperty(_ref2, mark.endMark, this.end[mark.relationMark]), _defineProperty(_ref2, "startAt", this.startAt), _defineProperty(_ref2, "endAt", this.endAt), _defineProperty(_ref2, "meta", this.meta), _ref2; } }]); return GraphLink; }(); _defineProperty(GraphLink, "distance", 80); _defineProperty(GraphLink, "distance_point_vector", [52, 0]); _defineProperty(GraphLink, "distance_point_merge_vector", [0, 0]); export { GraphLink as default };