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

33 lines
9.0 KiB
JavaScript

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
*
*/var e={name:"markers",init:function init(e){var t=this;return _asyncToGenerator(regeneratorRuntime.mark((function _callee3(){var r,n,a,i,o,s,l,u,c,m,d,h,f,p,g,_,k,v,b,x,y,A,w,L,C,M,E;return regeneratorRuntime.wrap((function _callee3$(S){for(;;)switch(S.prev=S.next){case 0:return M=function _buildButtonList(){var e=[];return i.each(l,(function(t,r){var n=r+"_marker_list",a=!0;Object.keys(m).forEach((function(t){var i=C(String(t));e.push({id:c+r+"_"+t,svgicon:t,icon:"markers-"+t+".png",title:i,type:"context",events:{click:w},panel:"marker_panel",list:n,isDefault:a}),a=!1}))})),e},C=function _getTitle(e){var t=a.langList.find((function(t){return t.id===e}));return t?t.title:e},L=function _setArrowFromButton3(){return(L=_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}x(r,"\\"+n),e.next=10;break;case 8:return e.next=10,y(r);case 10:case"end":return e.stop()}}),_callee2,this)})))).apply(this,arguments)},w=function _setArrowFromButton2(e){return L.apply(this,arguments)},A=function _showTextPrompt3(){return(A=_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=i("#"+e+"_marker").val()).substr(0,1)&&(t=""),n.next=4,i.prompt("Enter text for "+e+" marker",t);case 4:(r=n.sent)&&x(e,r);case 6:case"end":return n.stop()}}),_callee)})))).apply(this,arguments)},y=function _showTextPrompt2(e){return A.apply(this,arguments)},x=function _triggerTextEntry(e,t){i("#"+e+"_marker").val(t),i("#"+e+"_marker").change()},b=function _updateReferences(e){i.each(l,(function(t,r){var n=u+r+"_"+e.id,a="marker-"+r,s=d(e,a);if(s&&s.attributes.se_type){var l=e.getAttribute(a);if(l){var c=e.id.length,m=l.substr(-c-1,c);if(e.id!==m){var h=i("#"+r+"_marker").attr("value");g(n,h),o.changeSelectedAttribute(a,"url(#"+n+")"),"line"===e.tagName&&"mid"===r&&(e=_(e)),o.call("changed",f)}}}}))},v=function _colorChanged(e){var t=e.getAttribute("stroke");i.each(l,(function(r,n){var a=d(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)}}}))},k=function _setMarker(){var e={start_marker:"start",mid_marker:"mid",end_marker:"end"}[this.id],t="marker-"+e,r=f[0],n=d(r,t);n&&i(n).remove(),r.removeAttribute(t);var a=this.value;if(""===a&&(a="\\nomarker"),"\\nomarker"===a)return h(e,a),void o.call("changed",f);var s=u+e+"_"+r.id;g(s,a),o.changeSelectedAttribute(t,"url(#"+s+")"),"line"===r.tagName&&"mid"===e&&_(r),o.call("changed",f),h(e,a)},_=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")),u=Number(t.getAttribute("y2")),c=t.id,m=s({element:"polyline",attr:{points:r+","+a+(" "+(r+n)/2+","+(a+u)/2+" ")+n+","+u,stroke:t.getAttribute("stroke"),"stroke-width":t.getAttribute("stroke-width"),fill:"none",opacity:t.getAttribute("opacity")||1}});i.each(l,(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)),i(t).after(m).remove(),o.clearSelection(),m.id=c,o.addToSelection([m]),e.addCommandToHistory(d),m},g=function _addMarker(e,t){var r=o.getElem(e);if(!r&&""!==t&&"\\nomarker"!==t){var n,a=f[0].getAttribute("stroke"),i=50,l=50,u="0 0 100 100",c=5,d=5;if(n="\\"===t.substr(0,1)?t.substr(1):"textmarker",m[n]){if(r=s({element:"marker",attr:{id:e,markerUnits:"strokeWidth",orient:"auto",style:"pointer-events:none",se_type:n}}),"textmarker"!==n){var h=s(m[n]),p="_o"===n.substr(-2)?"none":a;h.setAttribute("fill",p),h.setAttribute("stroke",a),h.setAttribute("stroke-width",10),r.append(h)}else{var g=s(m[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,l=k.height/2+1,u=k.x+" "+k.y+" "+k.width+" "+k.height,c=k.width/10,d=k.height/10;var v=s({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",d),r.setAttribute("refX",i),r.setAttribute("refY",l),o.findDefs().append(r),r}}},p=function _showPanel(e){if(i("#marker_panel").toggle(e),e){var t,r,n=f[0];i.each(l,(function(e,a){var o=d(n,"marker-"+a),s=i("#"+a+"_marker");if(o){if(!o.attributes.se_type)return;t="\\"+o.attributes.se_type.textContent,r=t,"\\textmarker"===t?t=o.lastChild.textContent:s.hide()}else r=t="\\nomarker",s.hide();s.val(t),h(a,r)}))}},h=function _setIcon(e,t){"\\"!==t.substr(0,1)&&(t="\\textmarker");var n="#"+c+e+"_"+t.substr(1);r.setIcon("#cur_"+e+"_marker_list",i(n).children()),i(n).addClass("current").siblings().removeClass("current")},d=function _getLinked(e,t){var r=e.getAttribute(t);if(!r)return null;var n=r.match(/\(#(.*)\)/);return n&&2===n.length?o.getElem(n[1]):null},r=t,S.next=18,import("./locale/".concat(r.curPrefs.lang,".js"));case 18:return n=S.sent,a=n.default,i=e.$,o=r.canvas,s=o.addSVGElementFromJson,l=["start","mid","end"],u="se_marker_",c="mkr_",m={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){m[e+"_o"]=m[e]})),E=[{type:"input",panel:"marker_panel",id:"start_marker",size:3,events:{change:k}},{type:"button-select",panel:"marker_panel",id:"start_marker_list",colnum:3,events:{change:w}},{type:"input",panel:"marker_panel",id:"mid_marker",defval:"",size:3,events:{change:k}},{type:"button-select",panel:"marker_panel",id:"mid_marker_list",colnum:3,events:{change:w}},{type:"input",panel:"marker_panel",id:"end_marker",size:3,events:{change:k}},{type:"button-select",panel:"marker_panel",id:"end_marker_list",colnum:3,events:{change:w}}],S.abrupt("return",{name:a.name,svgicons:"markers-icons.xml",callback:function callback(){i("#marker_panel").addClass("toolset").hide()},addLangData:function addLangData(e){e.importLocale,e.lang;return{data:a.langList}},selectedChanged:function selectedChanged(e){for(var t=["line","path","polyline","polygon"],r=(f=e.elems).length;r--;){var n=f[r];n&&t.includes(n.tagName)&&e.selectedElement&&!e.multiselected?p(!0):p(!1)}},elementChanged:function elementChanged(e){var t=e.elems[0];t&&(t.getAttribute("marker-start")||t.getAttribute("marker-mid")||t.getAttribute("marker-end"))&&(v(t),b(t))},buttons:M(),context_tools:a.contextTools.map((function(e,t){return Object.assign(E[t],e)}))});case 30:case"end":return S.stop()}}),_callee3)})))()}};export default e;
//# sourceMappingURL=ext-markers.js.map