1 line
13 KiB
Plaintext
1 line
13 KiB
Plaintext
{"version":3,"file":"ext-polygon.js","sources":["../../../../src/editor/extensions/ext-polygon/ext-polygon.js"],"sourcesContent":["/**\n * @file ext-polygon.js\n *\n *\n * @copyright 2010 CloudCanvas, Inc. All rights reserved\n *\n */\n\nexport default {\n name: 'polygon',\n async init (S) {\n const svgEditor = this;\n const svgCanvas = svgEditor.canvas;\n const {$} = S, // {svgcontent}\n // addElem = svgCanvas.addSVGElementFromJson,\n editingitex = false;\n // eslint-disable-next-line node/no-unsupported-features/es-syntax\n const {default: strings} = await import(`./locale/${svgEditor.curPrefs.lang}.js`);\n let selElems,\n // svgdoc = S.svgroot.parentNode.ownerDocument,\n // newFOG, newFOGParent, newDef, newImageName, newMaskID, modeChangeG,\n // edg = 0,\n // undoCommand = 'Not image';\n started, newFO;\n\n // const ccZoom;\n // const wEl, hEl;\n // const wOffset, hOffset;\n // const ccRBG;\n // const ccOpacity;\n // const brushW, brushH;\n\n // const ccDebug = document.getElementById('debugpanel');\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 $('#polygon_panel').toggle(on);\n }\n\n /*\n function toggleSourceButtons(on){\n $('#tool_source_save, #tool_source_cancel').toggle(!on);\n $('#polygon_save, #polygon_cancel').toggle(on);\n }\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 /**\n * @param {Float} n\n * @returns {Float}\n */\n function cot (n) {\n return 1 / Math.tan(n);\n }\n\n /**\n * @param {Float} n\n * @returns {Float}\n */\n function sec (n) {\n return 1 / Math.cos(n);\n }\n\n /**\n * Obtained from http://code.google.com/p/passenger-top/source/browse/instiki/public/svg-edit/editor/extensions/ext-itex.js?r=3\n * This function sets the content of of the currently-selected foreignObject element,\n * based on the itex contained in string.\n * @param {string} tex The itex text.\n * @returns {boolean} This function returns false if the set was unsuccessful, true otherwise.\n */\n /*\n function setItexString(tex) {\n const mathns = 'http://www.w3.org/1998/Math/MathML',\n xmlnsns = 'http://www.w3.org/2000/xmlns/',\n ajaxEndpoint = '../../itex';\n const elt = selElems[0];\n try {\n const math = svgdoc.createElementNS(mathns, 'math');\n math.setAttributeNS(xmlnsns, 'xmlns', mathns);\n math.setAttribute('display', 'inline');\n const semantics = document.createElementNS(mathns, 'semantics');\n const annotation = document.createElementNS(mathns, 'annotation');\n annotation.setAttribute('encoding', 'application/x-tex');\n annotation.textContent = tex;\n const mrow = document.createElementNS(mathns, 'mrow');\n semantics.append(mrow, annotation);\n math.append(semantics);\n // make an AJAX request to the server, to get the MathML\n $.post(ajaxEndpoint, {tex, display: 'inline'}, function(data){\n const children = data.documentElement.childNodes;\n while (children.length > 0) {\n mrow.append(svgdoc.adoptNode(children[0], true));\n }\n svgCanvas.sanitizeSvg(math);\n svgCanvas.call('changed', [elt]);\n });\n elt.firstChild.replaceWith(math);\n svgCanvas.call('changed', [elt]);\n svgCanvas.clearSelection();\n } catch(e) {\n console.log(e);\n return false;\n }\n\n return true;\n }\n */\n const buttons = [{\n id: 'tool_polygon',\n icon: 'polygon.png',\n type: 'mode',\n position: 11,\n events: {\n click () {\n svgCanvas.setMode('polygon');\n showPanel(true);\n }\n }\n }];\n const contextTools = [{\n type: 'input',\n panel: 'polygon_panel',\n id: 'polySides',\n size: 3,\n defval: 5,\n events: {\n change () {\n setAttr('sides', this.value);\n }\n }\n }];\n return {\n name: strings.name,\n svgicons: 'polygon-icons.svg',\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\n callback () {\n $('#polygon_panel').hide();\n\n const endChanges = function () {\n // Todo: Missing?\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().hide().attr(\n 'id', 'polygon_save'\n ).unbind().appendTo(\n '#tool_source_back'\n ).click(function () {\n if (!editingitex) {\n return;\n }\n // Todo: Uncomment the setItexString() function above and handle ajaxEndpoint?\n /*\n if (!setItexString($('#svg_source_textarea').val())) {\n const ok = await $.confirm('Errors found. Revert to original?', function (ok) {\n if (!ok) {\n return false;\n }\n endChanges();\n } else { */\n endChanges();\n // }\n // setSelectMode();\n });\n\n /* const cancel = */ $('#tool_source_cancel').clone().hide().attr(\n 'id', 'polygon_cancel'\n ).unbind().appendTo('#tool_source_back').click(function () {\n endChanges();\n });\n }, 3000);\n },\n mouseDown (opts) {\n if (svgCanvas.getMode() !== 'polygon') {\n return undefined;\n }\n // const e = opts.event;\n const rgb = svgCanvas.getColor('fill');\n // const ccRgbEl = rgb.substring(1, rgb.length);\n const sRgb = svgCanvas.getColor('stroke');\n // ccSRgbEl = sRgb.substring(1, rgb.length);\n const sWidth = svgCanvas.getStrokeWidth();\n\n started = true;\n\n newFO = svgCanvas.addSVGElementFromJson({\n element: 'polygon',\n attr: {\n cx: opts.start_x,\n cy: opts.start_y,\n id: svgCanvas.getNextId(),\n shape: 'regularPoly',\n sides: document.getElementById('polySides').value,\n orient: 'x',\n edge: 0,\n fill: rgb,\n strokecolor: sRgb,\n strokeWidth: sWidth\n }\n });\n\n return {\n started: true\n };\n },\n mouseMove (opts) {\n if (!started || svgCanvas.getMode() !== 'polygon') {\n return undefined;\n }\n // const e = opts.event;\n const c = $(newFO).attr(['cx', 'cy', 'sides', 'orient', 'fill', 'strokecolor', 'strokeWidth']);\n let x = opts.mouse_x;\n let y = opts.mouse_y;\n const {cx, cy, fill, strokecolor, strokeWidth, sides} = c, // {orient} = c,\n edg = (Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy))) / 1.5;\n newFO.setAttribute('edge', edg);\n\n const inradius = (edg / 2) * cot(Math.PI / sides);\n const circumradius = inradius * sec(Math.PI / sides);\n let points = '';\n for (let s = 0; sides >= s; s++) {\n const angle = 2.0 * Math.PI * s / sides;\n x = (circumradius * Math.cos(angle)) + cx;\n y = (circumradius * Math.sin(angle)) + cy;\n\n points += x + ',' + y + ' ';\n }\n\n // const poly = newFO.createElementNS(NS.SVG, 'polygon');\n newFO.setAttribute('points', points);\n newFO.setAttribute('fill', fill);\n newFO.setAttribute('stroke', strokecolor);\n newFO.setAttribute('stroke-width', strokeWidth);\n // newFO.setAttribute('transform', 'rotate(-90)');\n // const shape = newFO.getAttribute('shape');\n // newFO.append(poly);\n // DrawPoly(cx, cy, sides, edg, orient);\n return {\n started: true\n };\n },\n\n mouseUp (opts) {\n if (svgCanvas.getMode() !== 'polygon') {\n return undefined;\n }\n const attrs = $(newFO).attr('edge');\n const keep = (attrs.edge !== '0');\n // svgCanvas.addToSelection([newFO], true);\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.getAttribute('shape') === 'regularPoly') {\n if (opts.selectedElement && !opts.multiselected) {\n $('#polySides').val(elem.getAttribute('sides'));\n\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","setAttr","cot","sec","n","Math","cos","tan","attr","val","svgCanvas","changeSelectedAttribute","call","selElems","on","fcRules","$","length","appendTo","text","toggle","svgEditor","_this","canvas","import","curPrefs","lang","strings","buttons","id","icon","type","position","events","click","setMode","contextTools","panel","size","defval","change","this","value","svgicons","map","button","i","Object","assign","context_tools","contextTool","callback","hide","setTimeout","clone","unbind","mouseDown","opts","getMode","rgb","getColor","sRgb","sWidth","getStrokeWidth","started","newFO","addSVGElementFromJson","element","cx","start_x","cy","start_y","getNextId","shape","sides","document","getElementById","orient","edge","fill","strokecolor","strokeWidth","mouseMove","c","x","mouse_x","y","mouse_y","edg","sqrt","setAttribute","circumradius","PI","points","s","angle","sin","mouseUp","keep","selectedChanged","elems","elem","getAttribute","selectedElement","multiselected","elementChanged"],"mappings":"0IAQA,MAAe,CACbA,KAAM,UACAC,mBAAMC,0WAkCDC,EAqBAC,EASAC,EAQAC,sGAAAA,gBAAKC,UACL,EAAIC,KAAKC,IAAIF,IATbF,gBAAKE,UACL,EAAIC,KAAKE,IAAIH,IAVbH,oBAASO,EAAMC,GACtBC,EAAUC,wBAAwBH,EAAMC,GACxCC,EAAUE,KAAK,UAAWC,IAvBnBb,sBAAWc,OACdC,EAAUC,EAAE,aACXD,EAAQE,SACXF,EAAUC,EAAE,iCAAiCE,SAAS,SAExDH,EAAQI,KAAML,EAAU,8CAAL,IACnBE,EAAE,kBAAkBI,OAAON,IAtCvBJ,GADAW,EAAYC,GACUC,OACrBP,EAAKjB,EAALiB,GAES,WAEiBQ,0BAAmBH,EAAUI,SAASC,oCAAvDC,YAiHVC,EAAU,CAAC,CACfC,GAAI,eACJC,KAAM,cACNC,KAAM,OACNC,SAAU,GACVC,OAAQ,CACNC,uBACExB,EAAUyB,QAAQ,WAClBnC,GAAU,OAIVoC,EAAe,CAAC,CACpBL,KAAM,QACNM,MAAO,gBACPR,GAAI,YACJS,KAAM,EACNC,OAAQ,EACRN,OAAQ,CACNO,yBACEvC,EAAQ,QAASwC,KAAKC,6BAIrB,CACL7C,KAAM8B,EAAQ9B,KACd8C,SAAU,oBACVf,QAASD,EAAQC,QAAQgB,KAAI,SAACC,EAAQC,UAC7BC,OAAOC,OAAOpB,EAAQkB,GAAID,MAEnCI,cAAetB,EAAQS,aAAaQ,KAAI,SAACM,EAAaJ,UAC7CC,OAAOC,OAA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