1 line
13 KiB
Plaintext
1 line
13 KiB
Plaintext
{"version":3,"file":"ext-foreignobject.js","sources":["../../../src/editor/extensions/ext-foreignobject.js"],"sourcesContent":["/**\n * @file ext-foreignobject.js\n *\n * @license Apache-2.0\n *\n * @copyright 2010 Jacques Distler, 2010 Alexis Deveria\n *\n */\n\nexport default {\n name: 'foreignobject',\n async init (S) {\n const svgEditor = this;\n const {$, text2xml, NS, importLocale} = S;\n const svgCanvas = svgEditor.canvas;\n const\n // {svgcontent} = S,\n // addElem = svgCanvas.addSVGElementFromJson,\n svgdoc = S.svgroot.parentNode.ownerDocument;\n const strings = await importLocale();\n\n const properlySourceSizeTextArea = function () {\n // TODO: remove magic numbers here and get values from CSS\n const height = $('#svg_source_container').height() - 80;\n $('#svg_source_textarea').css('height', height);\n };\n\n /**\n * @param {boolean} on\n * @returns {void}\n */\n function showPanel (on) {\n let fcRules = $('#fc_rules');\n if (!fcRules.length) {\n fcRules = $('<style id=\"fc_rules\"></style>').appendTo('head');\n }\n fcRules.text(!on ? '' : ' #tool_topath { display: none !important; }');\n $('#foreignObject_panel').toggle(on);\n }\n\n /**\n * @param {boolean} on\n * @returns {void}\n */\n function toggleSourceButtons (on) {\n $('#tool_source_save, #tool_source_cancel').toggle(!on);\n $('#foreign_save, #foreign_cancel').toggle(on);\n }\n\n let selElems,\n started,\n newFO,\n editingforeign = false;\n\n /**\n * This function sets the content of element elt to the input XML.\n * @param {string} xmlString - The XML text\n * @returns {boolean} This function returns false if the set was unsuccessful, true otherwise.\n */\n function setForeignString (xmlString) {\n const elt = selElems[0]; // The parent `Element` to append to\n try {\n // convert string into XML document\n const newDoc = text2xml('<svg xmlns=\"' + NS.SVG + '\" xmlns:xlink=\"' + NS.XLINK + '\">' + xmlString + '</svg>');\n // run it through our sanitizer to remove anything we do not support\n svgCanvas.sanitizeSvg(newDoc.documentElement);\n elt.replaceWith(svgdoc.importNode(newDoc.documentElement.firstChild, true));\n svgCanvas.call('changed', [elt]);\n svgCanvas.clearSelection();\n } catch (e) {\n // Todo: Surface error to user\n console.log(e); // eslint-disable-line no-console\n return false;\n }\n\n return true;\n }\n\n /**\n *\n * @returns {void}\n */\n function showForeignEditor () {\n const elt = selElems[0];\n if (!elt || editingforeign) { return; }\n editingforeign = true;\n toggleSourceButtons(true);\n elt.removeAttribute('fill');\n\n const str = svgCanvas.svgToString(elt, 0);\n $('#svg_source_textarea').val(str);\n $('#svg_source_editor').fadeIn();\n properlySourceSizeTextArea();\n $('#svg_source_textarea').focus();\n }\n\n /**\n * @param {string} attr\n * @param {string|Float} val\n * @returns {void}\n */\n function setAttr (attr, val) {\n svgCanvas.changeSelectedAttribute(attr, val);\n svgCanvas.call('changed', selElems);\n }\n\n const buttons = [{\n id: 'tool_foreign',\n icon: 'foreignobject-tool.png',\n type: 'mode',\n events: {\n click () {\n svgCanvas.setMode('foreign');\n }\n }\n }, {\n id: 'edit_foreign',\n icon: 'foreignobject-edit.png',\n type: 'context',\n panel: 'foreignObject_panel',\n events: {\n click () {\n showForeignEditor();\n }\n }\n }];\n\n const contextTools = [\n {\n type: 'input',\n panel: 'foreignObject_panel',\n id: 'foreign_width',\n size: 3,\n events: {\n change () {\n setAttr('width', this.value);\n }\n }\n }, {\n type: 'input',\n panel: 'foreignObject_panel',\n id: 'foreign_height',\n events: {\n change () {\n setAttr('height', this.value);\n }\n }\n }, {\n type: 'input',\n panel: 'foreignObject_panel',\n id: 'foreign_font_size',\n size: 2,\n defval: 16,\n events: {\n change () {\n setAttr('font-size', this.value);\n }\n }\n }\n ];\n\n return {\n name: strings.name,\n svgicons: 'foreignobject-icons.xml',\n buttons: strings.buttons.map((button, i) => {\n return Object.assign(buttons[i], button);\n }),\n context_tools: strings.contextTools.map((contextTool, i) => {\n return Object.assign(contextTools[i], contextTool);\n }),\n callback () {\n $('#foreignObject_panel').hide();\n\n const endChanges = function () {\n $('#svg_source_editor').hide();\n editingforeign = false;\n $('#svg_source_textarea').blur();\n toggleSourceButtons(false);\n };\n\n // TODO: Needs to be done after orig icon loads\n setTimeout(function () {\n // Create source save/cancel buttons\n /* const save = */ $('#tool_source_save').clone()\n .hide().attr('id', 'foreign_save').unbind()\n .appendTo('#tool_source_back').click(async function () {\n if (!editingforeign) { return; }\n\n if (!setForeignString($('#svg_source_textarea').val())) {\n const ok = await $.confirm('Errors found. Revert to original?');\n if (!ok) { return; }\n endChanges();\n } else {\n endChanges();\n }\n // setSelectMode();\n });\n\n /* const cancel = */ $('#tool_source_cancel').clone()\n .hide().attr('id', 'foreign_cancel').unbind()\n .appendTo('#tool_source_back').click(function () {\n endChanges();\n });\n }, 3000);\n },\n mouseDown (opts) {\n // const e = opts.event;\n if (svgCanvas.getMode() !== 'foreign') {\n return undefined;\n }\n started = true;\n newFO = svgCanvas.addSVGElementFromJson({\n element: 'foreignObject',\n attr: {\n x: opts.start_x,\n y: opts.start_y,\n id: svgCanvas.getNextId(),\n 'font-size': 16, // cur_text.font_size,\n width: '48',\n height: '20',\n style: 'pointer-events:inherit'\n }\n });\n const m = svgdoc.createElementNS(NS.MATH, 'math');\n m.setAttributeNS(NS.XMLNS, 'xmlns', NS.MATH);\n m.setAttribute('display', 'inline');\n const mi = svgdoc.createElementNS(NS.MATH, 'mi');\n mi.setAttribute('mathvariant', 'normal');\n mi.textContent = '\\u03A6';\n const mo = svgdoc.createElementNS(NS.MATH, 'mo');\n mo.textContent = '\\u222A';\n const mi2 = svgdoc.createElementNS(NS.MATH, 'mi');\n mi2.textContent = '\\u2133';\n m.append(mi, mo, mi2);\n newFO.append(m);\n return {\n started: true\n };\n },\n mouseUp (opts) {\n // const e = opts.event;\n if (svgCanvas.getMode() !== 'foreign' || !started) {\n return undefined;\n }\n const attrs = $(newFO).attr(['width', 'height']);\n const keep = (attrs.width !== '0' || attrs.height !== '0');\n svgCanvas.addToSelection([newFO], true);\n\n return {\n keep,\n element: newFO\n };\n },\n selectedChanged (opts) {\n // Use this to update the current selected elements\n selElems = opts.elems;\n\n let i = selElems.length;\n while (i--) {\n const elem = selElems[i];\n if (elem && elem.tagName === 'foreignObject') {\n if (opts.selectedElement && !opts.multiselected) {\n $('#foreign_font_size').val(elem.getAttribute('font-size'));\n $('#foreign_width').val(elem.getAttribute('width'));\n $('#foreign_height').val(elem.getAttribute('height'));\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 }\n };\n }\n};\n"],"names":["name","init","S","showPanel","toggleSourceButtons","setForeignString","showForeignEditor","setAttr","attr","val","svgCanvas","changeSelectedAttribute","call","selElems","elt","editingforeign","removeAttribute","str","svgToString","$","fadeIn","properlySourceSizeTextArea","focus","xmlString","newDoc","text2xml","NS","SVG","XLINK","sanitizeSvg","documentElement","replaceWith","svgdoc","importNode","firstChild","clearSelection","e","console","log","on","toggle","fcRules","length","appendTo","text","svgEditor","_this","importLocale","canvas","svgroot","parentNode","ownerDocument","strings","height","css","buttons","id","icon","type","events","click","setMode","panel","contextTools","size","change","this","value","defval","svgicons","map","button","i","Object","assign","context_tools","contextTool","callback","hide","endChanges","blur","setTimeout","clone","unbind","confirm","mouseDown","opts","getMode","started","newFO","addSVGElementFromJson","element","x","start_x","y","start_y","getNextId","width","style","m","createElementNS","MATH","setAttributeNS","XMLNS","setAttribute","mi","textContent","mo","mi2","append","mouseUp","attrs","keep","addToSelection","selectedChanged","elems","elem","tagName","selectedElement","multiselected","getAttribute","elementChanged"],"mappings":";;;;;;;;;AASA,MAAe,CACbA,KAAM,gBACAC,mBAAMC,0GAoBDC,EAaAC,UAeAC,EAuBAC,EAmBAC,uGAAAA,oBAASC,EAAMC,GACtBC,EAAUC,wBAAwBH,EAAMC,GACxCC,EAAUE,KAAK,UAAWC,IArBnBP,oCACDQ,EAAMD,EAAS,MAChBC,IAAOC,GACZA,GAAiB,EACjBX,GAAoB,GACpBU,EAAIE,gBAAgB,YAEdC,EAAMP,EAAUQ,YAAYJ,EAAK,GACvCK,EAAE,wBAAwBV,IAAIQ,GAC9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