Files
svgedit/dist/editor/system/extensions/ext-markers/ext-markers.js
2020-09-06 08:32:10 +02:00

32 lines
9.2 KiB
JavaScript

System.register([],(function(e,t){"use strict";return{execute:function(){function asyncGeneratorStep(e,t,r,n,a,i,o){try{var s=e[i](o),l=s.value}catch(e){return void r(e)}s.done?t(l):Promise.resolve(l).then(n,a)}function _asyncToGenerator(e){return function(){var t=this,r=arguments;return new Promise((function(n,a){var i=e.apply(t,r);function _next(e){asyncGeneratorStep(i,n,a,_next,_throw,"next",e)}function _throw(e){asyncGeneratorStep(i,n,a,_next,_throw,"throw",e)}_next(void 0)}))}}
/**
* @file ext-markers.js
*
* @license Apache-2.0
*
* @copyright 2010 Will Schleter based on ext-arrows.js by Copyright(c) 2010 Alexis Deveria
*
* This extension provides for the addition of markers to the either end
* or the middle of a line, polyline, path, polygon.
*
* Markers may be either a graphic or arbitary text
*
* to simplify the coding and make the implementation as robust as possible,
* markers are not shared - every object has its own set of markers.
* this relationship is maintained by a naming convention between the
* ids of the markers and the ids of the object
*
* The following restrictions exist for simplicty of use and programming
* objects and their markers to have the same color
* marker size is fixed
* text marker font, size, and attributes are fixed
* an application specific attribute - se_type - is added to each marker element
* to store the type of marker
*
* @todo
* remove some of the restrictions above
* add option for keeping text aligned to horizontal
* add support for dimension extension lines
*
*/e("default",{name:"markers",init:function init(e){var r=this;return _asyncToGenerator(regeneratorRuntime.mark((function _callee3(){var n,a,i,o,s,l,u,c,m,d,h,f,p,g,_,k,v,b,x,y,A,w,L,C,M,S,E;return regeneratorRuntime.wrap((function _callee3$(T){for(;;)switch(T.prev=T.next){case 0:return S=function _buildButtonList(){var e=[];return o.each(u,(function(t,r){var n=r+"_marker_list",a=!0;Object.keys(d).forEach((function(t){var i=M(String(t));e.push({id:m+r+"_"+t,svgicon:t,icon:"markers-"+t+".png",title:i,type:"context",events:{click:L},panel:"marker_panel",list:n,isDefault:a}),a=!1}))})),e},M=function _getTitle(e){var t=i.langList.find((function(t){return t.id===e}));return t?t.title:e},C=function _setArrowFromButton3(){return(C=_asyncToGenerator(regeneratorRuntime.mark((function _callee2(e){var t,r,n;return regeneratorRuntime.wrap((function _callee2$(e){for(;;)switch(e.prev=e.next){case 0:if(t=this.id.split("_"),r=t[1],n=t[2],t[3]&&(n+="_"+t[3]),"textmarker"===n){e.next=8;break}y(r,"\\"+n),e.next=10;break;case 8:return e.next=10,A(r);case 10:case"end":return e.stop()}}),_callee2,this)})))).apply(this,arguments)},L=function _setArrowFromButton2(e){return C.apply(this,arguments)},w=function _showTextPrompt3(){return(w=_asyncToGenerator(regeneratorRuntime.mark((function _callee(e){var t,r;return regeneratorRuntime.wrap((function _callee$(n){for(;;)switch(n.prev=n.next){case 0:return"\\"===(t=o("#"+e+"_marker").val()).substr(0,1)&&(t=""),n.next=4,o.prompt("Enter text for "+e+" marker",t);case 4:(r=n.sent)&&y(e,r);case 6:case"end":return n.stop()}}),_callee)})))).apply(this,arguments)},A=function _showTextPrompt2(e){return w.apply(this,arguments)},y=function _triggerTextEntry(e,t){o("#"+e+"_marker").val(t),o("#"+e+"_marker").change()},x=function _updateReferences(e){o.each(u,(function(t,r){var n=c+r+"_"+e.id,a="marker-"+r,i=h(e,a);if(i&&i.attributes.se_type){var l=e.getAttribute(a);if(l){var u=e.id.length,m=l.substr(-u-1,u);if(e.id!==m){var d=o("#"+r+"_marker").attr("value");_(n,d),s.changeSelectedAttribute(a,"url(#"+n+")"),"line"===e.tagName&&"mid"===r&&(e=k(e)),s.call("changed",p)}}}}))},b=function _colorChanged(e){var t=e.getAttribute("stroke");o.each(u,(function(r,n){var a=h(e,"marker-"+n);if(a&&a.attributes.se_type){var i=a.lastElementChild;if(i){var o=i.getAttribute("fill"),s=i.getAttribute("stroke");o&&"none"!==o&&i.setAttribute("fill",t),s&&"none"!==s&&i.setAttribute("stroke",t)}}}))},v=function _setMarker(){var e={start_marker:"start",mid_marker:"mid",end_marker:"end"}[this.id],t="marker-"+e,r=p[0],n=h(r,t);n&&o(n).remove(),r.removeAttribute(t);var a=this.value;if(""===a&&(a="\\nomarker"),"\\nomarker"===a)return f(e,a),void s.call("changed",p);var i=c+e+"_"+r.id;_(i,a),s.changeSelectedAttribute(t,"url(#"+i+")"),"line"===r.tagName&&"mid"===e&&k(r),s.call("changed",p),f(e,a)},k=function _convertline(t){if("line"!==t.tagName)return t;var r=Number(t.getAttribute("x1")),n=Number(t.getAttribute("x2")),a=Number(t.getAttribute("y1")),i=Number(t.getAttribute("y2")),c=t.id,m=l({element:"polyline",attr:{points:r+","+a+(" "+(r+n)/2+","+(a+i)/2+" ")+n+","+i,stroke:t.getAttribute("stroke"),"stroke-width":t.getAttribute("stroke-width"),fill:"none",opacity:t.getAttribute("opacity")||1}});o.each(u,(function(e,r){var n="marker-"+r;t.getAttribute(n)&&m.setAttribute(n,t.getAttribute(n))}));var d=new e.BatchCommand;return d.addSubCommand(new e.RemoveElementCommand(t,t.parentNode)),d.addSubCommand(new e.InsertElementCommand(m)),o(t).after(m).remove(),s.clearSelection(),m.id=c,s.addToSelection([m]),e.addCommandToHistory(d),m},_=function _addMarker(e,t){var r=s.getElem(e);if(!r&&""!==t&&"\\nomarker"!==t){var n,a=p[0].getAttribute("stroke"),i=50,o=50,u="0 0 100 100",c=5,m=5;if(n="\\"===t.substr(0,1)?t.substr(1):"textmarker",d[n]){if(r=l({element:"marker",attr:{id:e,markerUnits:"strokeWidth",orient:"auto",style:"pointer-events:none",se_type:n}}),"textmarker"!==n){var h=l(d[n]),f="_o"===n.substr(-2)?"none":a;h.setAttribute("fill",f),h.setAttribute("stroke",a),h.setAttribute("stroke-width",10),r.append(h)}else{var g=l(d[n]);g.textContent=t;var _=g.getBBox(),k=_;k.x=0,k.y=0,k.width+=2,k.height+=2,g.setAttribute("x",1),g.setAttribute("y",k.height-1-_.height/4),g.setAttribute("fill",a),i=k.width/2+1,o=k.height/2+1,u=k.x+" "+k.y+" "+k.width+" "+k.height,c=k.width/10,m=k.height/10;var v=l({element:"rect",attr:{x:k.x,y:k.y,width:k.width,height:k.height,fill:"#ffffff",stroke:"none","stroke-width":0}});r.setAttribute("orient",0),r.append(v,g)}return r.setAttribute("viewBox",u),r.setAttribute("markerWidth",c),r.setAttribute("markerHeight",m),r.setAttribute("refX",i),r.setAttribute("refY",o),s.findDefs().append(r),r}}},g=function _showPanel(e){if(o("#marker_panel").toggle(e),e){var t,r,n=p[0];o.each(u,(function(e,a){var i=h(n,"marker-"+a),s=o("#"+a+"_marker");if(i){if(!i.attributes.se_type)return;t="\\"+i.attributes.se_type.textContent,r=t,"\\textmarker"===t?t=i.lastChild.textContent:s.hide()}else r=t="\\nomarker",s.hide();s.val(t),f(a,r)}))}},f=function _setIcon(e,t){"\\"!==t.substr(0,1)&&(t="\\textmarker");var r="#"+m+e+"_"+t.substr(1);n.setIcon("#cur_"+e+"_marker_list",o(r).children()),o(r).addClass("current").siblings().removeClass("current")},h=function _getLinked(e,t){var r=e.getAttribute(t);if(!r)return null;var n=r.match(/\(#(.*)\)/);return n&&2===n.length?s.getElem(n[1]):null},n=r,T.next=18,t.import("./locale/".concat(n.curPrefs.lang,".js"));case 18:return a=T.sent,i=a.default,o=e.$,s=n.canvas,l=s.addSVGElementFromJson,u=["start","mid","end"],c="se_marker_",m="mkr_",d={nomarker:{},leftarrow:{element:"path",attr:{d:"M0,50 L100,90 L70,50 L100,10 Z"}},rightarrow:{element:"path",attr:{d:"M100,50 L0,90 L30,50 L0,10 Z"}},textmarker:{element:"text",attr:{x:0,y:0,"stroke-width":0,stroke:"none","font-size":75,"font-family":"serif","text-anchor":"left","xml:space":"preserve"}},forwardslash:{element:"path",attr:{d:"M30,100 L70,0"}},reverseslash:{element:"path",attr:{d:"M30,0 L70,100"}},verticalslash:{element:"path",attr:{d:"M50,0 L50,100"}},box:{element:"path",attr:{d:"M20,20 L20,80 L80,80 L80,20 Z"}},star:{element:"path",attr:{d:"M10,30 L90,30 L20,90 L50,10 L80,90 Z"}},xmark:{element:"path",attr:{d:"M20,80 L80,20 M80,80 L20,20"}},triangle:{element:"path",attr:{d:"M10,80 L50,20 L80,80 Z"}},mcircle:{element:"circle",attr:{r:30,cx:50,cy:50}}},["leftarrow","rightarrow","box","star","mcircle","triangle"].forEach((function(e){d[e+"_o"]=d[e]})),E=[{type:"input",panel:"marker_panel",id:"start_marker",size:3,events:{change:v}},{type:"button-select",panel:"marker_panel",id:"start_marker_list",colnum:3,events:{change:L}},{type:"input",panel:"marker_panel",id:"mid_marker",defval:"",size:3,events:{change:v}},{type:"button-select",panel:"marker_panel",id:"mid_marker_list",colnum:3,events:{change:L}},{type:"input",panel:"marker_panel",id:"end_marker",size:3,events:{change:v}},{type:"button-select",panel:"marker_panel",id:"end_marker_list",colnum:3,events:{change:L}}],T.abrupt("return",{name:i.name,svgicons:"markers-icons.xml",callback:function callback(){o("#marker_panel").addClass("toolset").hide()},addLangData:function addLangData(e){e.importLocale,e.lang;return{data:i.langList}},selectedChanged:function selectedChanged(e){for(var t=["line","path","polyline","polygon"],r=(p=e.elems).length;r--;){var n=p[r];n&&t.includes(n.tagName)&&e.selectedElement&&!e.multiselected?g(!0):g(!1)}},elementChanged:function elementChanged(e){var t=e.elems[0];t&&(t.getAttribute("marker-start")||t.getAttribute("marker-mid")||t.getAttribute("marker-end"))&&(b(t),x(t))},buttons:S(),context_tools:i.contextTools.map((function(e,t){return Object.assign(E[t],e)}))});case 30:case"end":return T.stop()}}),_callee3)})))()}})}}}));