UNPKG

arrowjoin

Version:

ArrowJoin is a creative and functional React library that effortlessly connects two React components with a sleek arrow.

96 lines 3.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSvgPos = exports.getElemPos = exports.getShortestLine = exports.xStr2absRelative = exports.factorDpathStr = exports.getElementByPropGiven = void 0; var getElementByPropGiven = function (ref) { var myRef; if (typeof ref === 'string') { // myRef = document.getElementById(ref); myRef = document.getElementById(ref); } else myRef = ref === null || ref === void 0 ? void 0 : ref.current; return myRef; }; exports.getElementByPropGiven = getElementByPropGiven; // receives string representing a d path and factoring only the numbers var factorDpathStr = function (d, factor) { var l = d.split(/(\d+(?:\.\d+)?)/); l = l.map(function (s) { if (Number(s)) return (Number(s) * factor).toString(); else return s; }); return l.join(''); }; exports.factorDpathStr = factorDpathStr; // return relative,abs var xStr2absRelative = function (str) { if (typeof str !== 'string') return { abs: 0, relative: 0.5 }; var sp = str.split('%'); var absLen = 0, percentLen = 0; if (sp.length == 1) { var p = parseFloat(sp[0]); if (!isNaN(p)) { absLen = p; return { abs: absLen, relative: 0 }; } } else if (sp.length == 2) { var _a = [parseFloat(sp[0]), parseFloat(sp[1])], p1 = _a[0], p2 = _a[1]; if (!isNaN(p1)) percentLen = p1 / 100; if (!isNaN(p2)) absLen = p2; if (!isNaN(p1) || !isNaN(p2)) return { abs: absLen, relative: percentLen }; } }; exports.xStr2absRelative = xStr2absRelative; var dist = function (p1, p2) { //length of line return Math.sqrt(Math.pow((p1.x - p2.x), 2) + Math.pow((p1.y - p2.y), 2)); }; var getShortestLine = function (sPoints, ePoints) { // closes tPair Of Points which feet to the specified anchors var minDist = Infinity, d = Infinity; var closestPair; sPoints.forEach(function (sp) { ePoints.forEach(function (ep) { d = dist(sp, ep); if (d < minDist) { minDist = d; closestPair = { chosenStart: sp, chosenEnd: ep }; } }); }); return closestPair; }; exports.getShortestLine = getShortestLine; var getElemPos = function (elem) { if (!elem) return { x: 0, y: 0, right: 0, bottom: 0 }; var pos = elem.getBoundingClientRect(); return { x: pos.left, y: pos.top, right: pos.right, bottom: pos.bottom, }; }; exports.getElemPos = getElemPos; var getSvgPos = function (svgRef) { if (!svgRef.current) return { x: 0, y: 0 }; var _a = svgRef.current.getBoundingClientRect(), xarrowElemX = _a.left, xarrowElemY = _a.top; var xarrowStyle = getComputedStyle(svgRef.current); var xarrowStyleLeft = Number(xarrowStyle.left.slice(0, -2)); var xarrowStyleTop = Number(xarrowStyle.top.slice(0, -2)); return { x: xarrowElemX - xarrowStyleLeft, y: xarrowElemY - xarrowStyleTop, }; }; exports.getSvgPos = getSvgPos; //# sourceMappingURL=index.js.map