1 line
31 KiB
Plaintext
1 line
31 KiB
Plaintext
|
{"version":3,"file":"ext-connector.js","sources":["../../../../src/editor/extensions/ext-connector/ext-connector.js"],"sourcesContent":["/**\n * @file ext-connector.js\n *\n * @license MIT\n *\n * @copyright 2010 Alexis Deveria\n *\n */\n\nexport default {\n name: 'connector',\n async init (S) {\n const svgEditor = this;\n const svgCanvas = svgEditor.canvas;\n const {getElem} = svgCanvas;\n const {$, svgroot} = S,\n addElem = svgCanvas.addSVGElementFromJson,\n selManager = S.selectorManager,\n connSel = '.se_connector',\n // connect_str = '-SE_CONNECT-',\n elData = $.data;\n\n let startX,\n startY,\n curLine,\n startElem,\n endElem,\n seNs,\n {svgcontent} = S,\n started = false,\n connections = [],\n selElems = [];\n\n /**\n *\n * @param {Float} x\n * @param {Float} y\n * @param {module:utilities.BBoxObject} bb\n * @param {Float} offset\n * @returns {module:math.XYObject}\n */\n function getBBintersect (x, y, bb, offset) {\n if (offset) {\n offset -= 0;\n bb = $.extend({}, bb);\n bb.width += offset;\n bb.height += offset;\n bb.x -= offset / 2;\n bb.y -= offset / 2;\n }\n\n const midX = bb.x + bb.width / 2;\n const midY = bb.y + bb.height / 2;\n const lenX = x - midX;\n const lenY = y - midY;\n\n const slope = Math.abs(lenY / lenX);\n\n let ratio;\n if (slope < bb.height / bb.width) {\n ratio = (bb.width / 2) / Math.abs(lenX);\n } else {\n ratio = lenY\n ? (bb.height / 2) / Math.abs(lenY)\n : 0;\n }\n\n return {\n x: midX + lenX * ratio,\n y: midY + lenY * ratio\n };\n }\n\n /**\n * @param {\"start\"|\"end\"} side\n * @param {Element} line\n * @returns {Float}\n */\n function getOffset (side, line) {\n const giveOffset = line.getAttribute('marker-' + side);\n // const giveOffset = $(line).data(side+'_off');\n\n // TODO: Make this number (5) be based on marker width/height\n const size = line.getAttribute('stroke-width') * 5;\n return giveOffset ? size : 0;\n }\n\n /**\n * @param {boolean} on\n * @returns {void}\n */\n function showPanel (on) {\n let connRules = $('#connector_rules');\n if (!connRules.length) {\n connRules = $('<style id=\"connector_rules\"></style>').appendTo('head');\n }\n connRules.text(!on ? '' : '#tool_clone, #tool_topath, #tool_angle, #xy_panel { display: none !important; }');\n $('#connector_panel').toggle(on);\n }\n\n /**\n * @param {Element} elem\n * @param {Integer|\"end\"} pos\n * @param {Float} x\n * @param {Float} y\n * @param {boolean} [setMid]\n * @returns {void}\n */\n function setPoint (elem, pos, x, y, setMid) {\n const pts = elem.points;\n const pt = svgroot.createSVGPoint();\n pt.x = x;\n pt.y = y;\n if (pos === 'end') { pos = pts.numberOfItems - 1; }\n // TODO: Test for this on init, then use alt only if needed\n try {\n pts.replaceItem(pt, pos);\n } catch (err) {\n // Should only occur in FF which formats points attr as \"n,n n,n\", so just split\n const ptArr = elem.getAttribute('points').split(' ');\n for (let i = 0; i < ptArr.length; i++) {\n if (i === pos) {\n ptArr[i] = x + ',' + y;\n }\n }\n elem.setAttribute('points', ptArr.join(' '));\n }\n\n if (setMid) {\n // Add center point\n const ptStart = pts.getItem(0);\n const ptEnd = pts.getItem(pts.numberOfItems - 1);\n setPoint(elem, 1, (ptEnd.x + ptStart.x) / 2, (ptEnd.y + ptStart.y) / 2);\n }\n }\n\n /**\n * @param {Float} diffX\n * @param {Float} diffY\n * @returns {void}\n */\n function updateLine (diffX, diffY) {\n // Update line with element\n let i = connections.length;\n while (i--) {\n const conn = connections[i];\n const l
|