svgedit/dist/editor/extensions/ext-arrows.js.map

1 line
13 KiB
Plaintext

{"version":3,"file":"ext-arrows.js","sources":["../../../src/editor/extensions/ext-arrows.js"],"sourcesContent":["/**\n * @file ext-arrows.js\n *\n * @license MIT\n *\n * @copyright 2010 Alexis Deveria\n *\n */\n\nexport default {\n name: 'arrows',\n async init (S) {\n const strings = await S.importLocale();\n const svgEditor = this;\n const svgCanvas = svgEditor.canvas;\n const // {svgcontent} = S,\n addElem = svgCanvas.addSVGElementFromJson,\n {nonce, $} = S,\n prefix = 'se_arrow_';\n\n let selElems, arrowprefix, randomizeIds = S.randomize_ids;\n\n /**\n * @param {Window} win\n * @param {!(string|Integer)} n\n * @returns {void}\n */\n function setArrowNonce (win, n) {\n randomizeIds = true;\n arrowprefix = prefix + n + '_';\n pathdata.fw.id = arrowprefix + 'fw';\n pathdata.bk.id = arrowprefix + 'bk';\n }\n\n /**\n * @param {Window} win\n * @returns {void}\n */\n function unsetArrowNonce (win) {\n randomizeIds = false;\n arrowprefix = prefix;\n pathdata.fw.id = arrowprefix + 'fw';\n pathdata.bk.id = arrowprefix + 'bk';\n }\n\n svgCanvas.bind('setnonce', setArrowNonce);\n svgCanvas.bind('unsetnonce', unsetArrowNonce);\n\n if (randomizeIds) {\n arrowprefix = prefix + nonce + '_';\n } else {\n arrowprefix = prefix;\n }\n\n const pathdata = {\n fw: {d: 'm0,0l10,5l-10,5l5,-5l-5,-5z', refx: 8, id: arrowprefix + 'fw'},\n bk: {d: 'm10,0l-10,5l10,5l-5,-5l5,-5z', refx: 2, id: arrowprefix + 'bk'}\n };\n\n /**\n * Gets linked element.\n * @param {Element} elem\n * @param {string} attr\n * @returns {Element}\n */\n function getLinked (elem, attr) {\n const str = elem.getAttribute(attr);\n if (!str) { return null; }\n const m = str.match(/\\(#(.*)\\)/);\n // const m = str.match(/\\(#(?<id>.+)\\)/);\n // if (!m || !m.groups.id) {\n if (!m || m.length !== 2) {\n return null;\n }\n return svgCanvas.getElem(m[1]);\n // return svgCanvas.getElem(m.groups.id);\n }\n\n /**\n * @param {boolean} on\n * @returns {void}\n */\n function showPanel (on) {\n $('#arrow_panel').toggle(on);\n if (on) {\n const el = selElems[0];\n const end = el.getAttribute('marker-end');\n const start = el.getAttribute('marker-start');\n const mid = el.getAttribute('marker-mid');\n let val;\n if (end && start) {\n val = 'both';\n } else if (end) {\n val = 'end';\n } else if (start) {\n val = 'start';\n } else if (mid) {\n val = 'mid';\n if (mid.includes('bk')) {\n val = 'mid_bk';\n }\n }\n\n if (!start && !mid && !end) {\n val = 'none';\n }\n\n $('#arrow_list').val(val);\n }\n }\n\n /**\n *\n * @returns {void}\n */\n function resetMarker () {\n const el = selElems[0];\n el.removeAttribute('marker-start');\n el.removeAttribute('marker-mid');\n el.removeAttribute('marker-end');\n }\n\n /**\n * @param {\"bk\"|\"fw\"} dir\n * @param {\"both\"|\"mid\"|\"end\"|\"start\"} type\n * @param {string} id\n * @returns {Element}\n */\n function addMarker (dir, type, id) {\n // TODO: Make marker (or use?) per arrow type, since refX can be different\n id = id || arrowprefix + dir;\n\n const data = pathdata[dir];\n\n if (type === 'mid') {\n data.refx = 5;\n }\n\n let marker = svgCanvas.getElem(id);\n if (!marker) {\n marker = addElem({\n element: 'marker',\n attr: {\n viewBox: '0 0 10 10',\n id,\n refY: 5,\n markerUnits: 'strokeWidth',\n markerWidth: 5,\n markerHeight: 5,\n orient: 'auto',\n style: 'pointer-events:none' // Currently needed for Opera\n }\n });\n const arrow = addElem({\n element: 'path',\n attr: {\n d: data.d,\n fill: '#000000'\n }\n });\n marker.append(arrow);\n svgCanvas.findDefs().append(marker);\n }\n\n marker.setAttribute('refX', data.refx);\n\n return marker;\n }\n\n /**\n *\n * @returns {void}\n */\n function setArrow () {\n resetMarker();\n\n let type = this.value;\n if (type === 'none') {\n return;\n }\n\n // Set marker on element\n let dir = 'fw';\n if (type === 'mid_bk') {\n type = 'mid';\n dir = 'bk';\n } else if (type === 'both') {\n addMarker('bk', type);\n svgCanvas.changeSelectedAttribute('marker-start', 'url(#' + pathdata.bk.id + ')');\n type = 'end';\n dir = 'fw';\n } else if (type === 'start') {\n dir = 'bk';\n }\n\n addMarker(dir, type);\n svgCanvas.changeSelectedAttribute('marker-' + type, 'url(#' + pathdata[dir].id + ')');\n svgCanvas.call('changed', selElems);\n }\n\n /**\n * @param {Element} elem\n * @returns {void}\n */\n function colorChanged (elem) {\n const color = elem.getAttribute('stroke');\n const mtypes = ['start', 'mid', 'end'];\n const defs = svgCanvas.findDefs();\n\n $.each(mtypes, function (i, type) {\n const marker = getLinked(elem, 'marker-' + type);\n if (!marker) { return; }\n\n const curColor = $(marker).children().attr('fill');\n const curD = $(marker).children().attr('d');\n if (curColor === color) { return; }\n\n const allMarkers = $(defs).find('marker');\n let newMarker = null;\n // Different color, check if already made\n allMarkers.each(function () {\n const attrs = $(this).children().attr(['fill', 'd']);\n if (attrs.fill === color && attrs.d === curD) {\n // Found another marker with this color and this path\n newMarker = this;\n }\n });\n\n if (!newMarker) {\n // Create a new marker with this color\n const lastId = marker.id;\n const dir = lastId.includes('_fw') ? 'fw' : 'bk';\n\n newMarker = addMarker(dir, type, arrowprefix + dir + allMarkers.length);\n\n $(newMarker).children().attr('fill', color);\n }\n\n $(elem).attr('marker-' + type, 'url(#' + newMarker.id + ')');\n\n // Check if last marker can be removed\n let remove = true;\n $(S.svgcontent).find('line, polyline, path, polygon').each(function () {\n const element = this;\n $.each(mtypes, function (j, mtype) {\n if ($(element).attr('marker-' + mtype) === 'url(#' + marker.id + ')') {\n remove = false;\n return remove;\n }\n return undefined;\n });\n if (!remove) { return false; }\n return undefined;\n });\n\n // Not found, so can safely remove\n if (remove) {\n $(marker).remove();\n }\n });\n }\n\n const contextTools = [\n {\n type: 'select',\n panel: 'arrow_panel',\n id: 'arrow_list',\n defval: 'none',\n events: {\n change: setArrow\n }\n }\n ];\n\n return {\n name: strings.name,\n context_tools: strings.contextTools.map((contextTool, i) => {\n return Object.assign(contextTools[i], contextTool);\n }),\n callback () {\n $('#arrow_panel').hide();\n // Set ID so it can be translated in locale file\n $('#arrow_list option')[0].id = 'connector_no_arrow';\n },\n async addLangData ({lang, importLocale}) {\n const {langList} = await importLocale();\n return {\n data: langList\n };\n },\n selectedChanged (opts) {\n // Use this to update the current selected elements\n selElems = opts.elems;\n\n const markerElems = ['line', 'path', 'polyline', 'polygon'];\n let i = selElems.length;\n while (i--) {\n const elem = selElems[i];\n if (elem && markerElems.includes(elem.tagName)) {\n if (opts.selectedElement && !opts.multiselected) {\n showPanel(true);\n } else {\n showPanel(false);\n }\n } else {\n showPanel(false);\n }\n }\n },\n elementChanged (opts) {\n const elem = opts.elems[0];\n if (elem && (\n elem.getAttribute('marker-start') ||\n elem.getAttribute('marker-mid') ||\n elem.getAttribute('marker-end')\n )) {\n // const start = elem.getAttribute('marker-start');\n // const mid = elem.getAttribute('marker-mid');\n // const end = elem.getAttribute('marker-end');\n // Has marker, so see if it should match color\n colorChanged(elem);\n }\n }\n };\n }\n};\n"],"names":["name","init","S","setArrowNonce","unsetArrowNonce","getLinked","showPanel","resetMarker","addMarker","setArrow","colorChanged","elem","color","getAttribute","mtypes","defs","svgCanvas","findDefs","$","each","i","type","marker","curColor","children","attr","curD","allMarkers","find","newMarker","attrs","this","fill","d","dir","id","includes","arrowprefix","length","remove","svgcontent","element","j","mtype","value","changeSelectedAttribute","pathdata","bk","call","selElems","data","refx","getElem","addElem","viewBox","refY","markerUnits","markerWidth","markerHeight","orient","style","arrow","append","setAttribute","el","removeAttribute","on","toggle","val","end","start","mid","str","m","match","win","randomizeIds","prefix","fw","n","importLocale","strings","_this","canvas","addSVGElementFromJson","nonce","randomize_ids","bind","contextTools","panel","defval","events","change","context_tools","map","contextTool","Object","assign","callback","hide","addLangData","lang","langList","selectedChanged","opts","markerElems","elems","tagName","selectedElement","multiselected","elementChanged"],"mappings":";;;;;;;;;AASA,MAAe,CACbA,KAAM,SACAC,mBAAMC,0GAgBDC,EAWAC,IA2BAC,EAiBAC,EAiCAC,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