@logicflow/extension
Version: 
LogicFlow Extensions
208 lines (207 loc) • 7.22 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurvedEdgePath = exports.CurvedEdgeModel = exports.CurvedEdge = void 0;
var core_1 = require("@logicflow/core");
var directionMap = {
    tr: 'tl',
    lb: 'tl',
    tl: 'tr',
    rb: 'tr',
    br: 'bl',
    lt: 'bl',
    bl: 'br',
    rt: 'br',
};
function pointFilter(points) {
    var all = points;
    var i = 1;
    while (i < all.length - 1) {
        var _a = __read(all[i - 1], 2), x = _a[0], y = _a[1];
        var _b = __read(all[i], 2), x1 = _b[0], y1 = _b[1];
        var _c = __read(all[i + 1], 2), x2 = _c[0], y2 = _c[1];
        if ((x === x1 && x1 === x2) || (y === y1 && y1 === y2)) {
            all.splice(i, 1);
        }
        else {
            i++;
        }
    }
    return all;
}
function getMidPoints(cur, key, orientation, radius) {
    var mid1 = [cur[0], cur[1]];
    var mid2 = [cur[0], cur[1]];
    switch (orientation) {
        case 'tl': {
            if (key === 'tr') {
                mid1[1] += radius;
                mid2[0] += radius;
            }
            else if (key === 'lb') {
                mid1[0] += radius;
                mid2[1] += radius;
            }
            return [mid1, mid2];
        }
        case 'tr': {
            if (key === 'tl') {
                mid1[1] += radius;
                mid2[0] -= radius;
            }
            else if (key === 'rb') {
                mid1[0] -= radius;
                mid2[1] += radius;
            }
            return [mid1, mid2];
        }
        case 'bl': {
            if (key === 'br') {
                mid1[1] -= radius;
                mid2[0] += radius;
            }
            else if (key === 'lt') {
                mid1[0] += radius;
                mid2[1] -= radius;
            }
            return [mid1, mid2];
        }
        case 'br': {
            if (key === 'bl') {
                mid1[1] -= radius;
                mid2[0] -= radius;
            }
            else if (key === 'rt') {
                mid1[0] -= radius;
                mid2[1] -= radius;
            }
            return [mid1, mid2];
        }
        default:
            return [];
    }
}
function getPartialPath(prev, cur, next, radius) {
    var _a;
    var dir1 = '';
    var dir2 = '';
    if (prev[0] === cur[0]) {
        dir1 = prev[1] > cur[1] ? 't' : 'b';
    }
    else if (prev[1] === cur[1]) {
        dir1 = prev[0] > cur[0] ? 'l' : 'r';
    }
    if (cur[0] === next[0]) {
        dir2 = cur[1] > next[1] ? 't' : 'b';
    }
    else if (cur[1] === next[1]) {
        dir2 = cur[0] > next[0] ? 'l' : 'r';
    }
    var r = Math.min(Math.hypot(cur[0] - prev[0], cur[1] - prev[1]) / 2, Math.hypot(next[0] - cur[0], next[1] - cur[1]) / 2, radius) || (1 / 5) * radius;
    var key = "".concat(dir1).concat(dir2);
    var orientation = directionMap[key] || '-';
    var path = "L ".concat(prev[0], " ").concat(prev[1]);
    if (orientation === '-') {
        path += "L ".concat(cur[0], " ").concat(cur[1], " L ").concat(next[0], " ").concat(next[1]);
    }
    else {
        var _b = __read(getMidPoints(cur, key, orientation, r), 2), mid1 = _b[0], mid2 = _b[1];
        if (mid1 && mid2) {
            path += "L ".concat(mid1[0], " ").concat(mid1[1], " Q ").concat(cur[0], " ").concat(cur[1], " ").concat(mid2[0], " ").concat(mid2[1]);
            _a = __read(mid2, 2), cur[0] = _a[0], cur[1] = _a[1];
        }
    }
    return path;
}
function getCurvedEdgePath(points, radius) {
    var i = 0;
    var d = '';
    if (points.length === 2) {
        d += "M".concat(points[i][0], " ").concat(points[i++][1], " L ").concat(points[i][0], " ").concat(points[i][1]);
    }
    else {
        d += "M".concat(points[i][0], " ").concat(points[i++][1]);
        for (; i + 1 < points.length;) {
            var prev = points[i - 1];
            var cur = points[i];
            var next = points[i++ + 1];
            d += getPartialPath(prev, cur, next, radius);
        }
        d += "L ".concat(points[i][0], " ").concat(points[i][1]);
    }
    return d;
}
exports.getCurvedEdgePath = getCurvedEdgePath;
var CurvedEdge = /** @class */ (function (_super) {
    __extends(CurvedEdge, _super);
    function CurvedEdge() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    CurvedEdge.prototype.getEdge = function () {
        var model = this.props.model;
        var pointsStr = model.points, isAnimation = model.isAnimation, arrowConfig = model.arrowConfig, _a = model.radius, radius = _a === void 0 ? 5 : _a;
        var style = model.getEdgeStyle();
        var animationStyle = model.getEdgeAnimationStyle();
        var points = pointFilter(pointsStr.split(' ').map(function (p) { return p.split(',').map(function (a) { return +a; }); }));
        var d = getCurvedEdgePath(points, radius);
        var attrs = __assign(__assign(__assign({ style: isAnimation ? animationStyle : {} }, style), arrowConfig), { fill: 'none' });
        return (0, core_1.h)('path', __assign({ d: d }, attrs));
    };
    return CurvedEdge;
}(core_1.PolylineEdge));
exports.CurvedEdge = CurvedEdge;
var CurvedEdgeModel = /** @class */ (function (_super) {
    __extends(CurvedEdgeModel, _super);
    function CurvedEdgeModel() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return CurvedEdgeModel;
}(core_1.PolylineEdgeModel));
exports.CurvedEdgeModel = CurvedEdgeModel;
var defaultCurvedEdge = {
    type: 'curved-edge',
    view: CurvedEdge,
    model: CurvedEdgeModel,
};
exports.default = defaultCurvedEdge;