@zklogic/draw.io
Version:
Draw.IO - Graph Editor integration in Angular-cli
1 lines • 4.85 kB
JavaScript
function mxFreehand(e){var t=null!=e.view&&null!=e.view.canvas?e.view.canvas.ownerSVGElement:null;if(null!=e.container&&null!=t){e.addListener(mxEvent.ESCAPE,mxUtils.bind(this,(function(){this.stopDrawing()})));var n,r,i=mxFreehand.prototype.NORMAL_SMOOTHING,l=null,s=[],a=[],o=!1,u=!0,h=!0,x=!0,c=!0,d=[],m=!1,v=!0;this.setClosedPath=function(e){o=e},this.setAutoClose=function(e){u=e},this.setAutoInsert=function(e){h=e},this.setAutoScroll=function(e){x=e},this.setOpenFill=function(e){c=e},this.setStopClickEnabled=function(e){v=e},this.setSmoothing=function(e){i=e};var f=function(t){m=t,e.getRubberband().setEnabled(!t),e.graphHandler.setSelectEnabled(!t),e.graphHandler.setMoveEnabled(!t),e.container.style.cursor=t?"crosshair":"",e.fireEvent(new mxEventObject("freehandStateChanged"))};this.startDrawing=function(){f(!0)},this.isDrawing=function(){return m};var p=mxUtils.bind(this,(function(e){if(l){var t=r.length,n=v&&a.length>0&&null!=r&&r.length<2;n||a.push.apply(a,r),r=[],a.push(null),s.push(l),l=null,(n||h)&&this.stopDrawing(),h&&t>=2&&this.startDrawing(),mxEvent.consume(e)}}));this.createStyle=function(e){return mxConstants.STYLE_SHAPE+"="+e+";fillColor=none;"},this.stopDrawing=function(){if(s.length>0){for(var t=a[0].x,n=a[0].x,r=a[0].y,i=a[0].y,h=1;h<a.length;h++)null!=a[h]&&(t=Math.max(t,a[h].x),n=Math.min(n,a[h].x),r=Math.max(r,a[h].y),i=Math.min(i,a[h].y));var x=t-n,d=r-i;if(x>0&&d>0){var m=100/x,v=100/d;a.map((function(e){return null==e||(e.x=(e.x-n)*m,e.y=(e.y-i)*v),e}));var p='<shape strokewidth="inherit"><foreground>',E=0;for(h=0;h<a.length;h++){var g=a[h];if(null==g){var y=!1,S=a[E],O=a[h-1];if(!o&&u){var C=S.x-O.x,w=S.y-O.y;y=Math.sqrt(C*C+w*w)<=e.tolerance}(o||y)&&(p+='<line x="'+S.x.toFixed(2)+'" y="'+S.y.toFixed(2)+'"/>'),p+="</path>"+(c||o||y?"<fillstroke/>":"<stroke/>"),E=h+1}else p+=h==E?'<path><move x="'+g.x.toFixed(2)+'" y="'+g.y.toFixed(2)+'"/>':'<line x="'+g.x.toFixed(2)+'" y="'+g.y.toFixed(2)+'"/>'}if(p+="</foreground></shape>",e.isEnabled()&&!e.isCellLocked(e.getDefaultParent())){var b=this.createStyle("stencil("+Graph.compress(p)+")"),M=e.view.scale,T=e.view.translate,L=new mxCell("",new mxGeometry(n/M-T.x,i/M-T.y,x/M,d/M),b);L.vertex=1,e.model.beginUpdate();try{L=e.addCell(L),e.fireEvent(new mxEventObject("cellsInserted","cells",[L])),e.fireEvent(new mxEventObject("freehandInserted","cell",L))}finally{e.model.endUpdate()}e.setSelectionCells([L])}}for(h=0;h<s.length;h++)s[h].parentNode.removeChild(s[h]);l=null,s=[],a=[]}f(!1)},e.addListener(mxEvent.FIRE_MOUSE_EVENT,mxUtils.bind(this,(function(e,t){var n=t.getProperty("eventName"),r=t.getProperty("event");n==mxEvent.MOUSE_MOVE&&m&&(null!=r.sourceState&&r.sourceState.setCursor("crosshair"),r.consume())})));var E=new mxCell;E.edge=!0,e.addMouseListener({mouseDown:mxUtils.bind(this,(function(i,s){if(e.isEnabled()&&!e.isCellLocked(e.getDefaultParent())){var o=s.getEvent();if(!m||mxEvent.isPopupTrigger(o)||mxEvent.isMultiTouchEvent(o))return;var u=e.getCurrentCellStyle(E),h=parseFloat(e.currentVertexStyle[mxConstants.STYLE_STROKEWIDTH]||1);h=Math.max(1,h*e.view.scale);var x=mxUtils.getValue(e.currentVertexStyle,mxConstants.STYLE_STROKECOLOR,mxUtils.getValue(u,mxConstants.STYLE_STROKECOLOR,"#000"));if("default"==x&&(x=e.shapeForegroundColor),(l=document.createElementNS("http://www.w3.org/2000/svg","path")).setAttribute("fill","none"),l.setAttribute("stroke",x),l.setAttribute("stroke-width",h),"1"==e.currentVertexStyle[mxConstants.STYLE_DASHED]){var c=e.currentVertexStyle[mxConstants.STYLE_DASH_PATTERN]||"3 3";c=c.split(" ").map((function(e){return parseFloat(e)*h})).join(" "),l.setAttribute("stroke-dasharray",c)}d=[];var v=g(o);y(v),n="M"+v.x+" "+v.y,a.push(v),r=[],l.setAttribute("d",n),t.appendChild(l),s.consume()}})),mouseMove:mxUtils.bind(this,(function(t,n){if(l&&e.isEnabled()&&!e.isCellLocked(e.getDefaultParent())){var r=n.getEvent(),i=g(r);if(y(i),O(),x){var s=e.view.translate;e.scrollRectToVisible(new mxRectangle(i.x-s.x,i.y-s.y).grow(20))}n.consume()}})),mouseUp:mxUtils.bind(this,(function(t,n){l&&e.isEnabled()&&!e.isCellLocked(e.getDefaultParent())&&(p(n.getEvent()),n.consume())}))});var g=function(t){return mxUtils.convertPoint(e.container,mxEvent.getClientX(t),mxEvent.getClientY(t))},y=function(e){for(d.push(e);d.length>i;)d.shift()},S=function(e){var t=d.length;if(t%2==1||t>=i){var n,r,l=0,s=0,a=0;for(r=e;r<t;r++)a++,l+=(n=d[r]).x,s+=n.y;return{x:l/a,y:s/a}}return null},O=function(){var e=S(0);if(e){n+=" L"+e.x+" "+e.y,a.push(e);var t="";r=[];for(var i=2;i<d.length;i+=2)t+=" L"+(e=S(i)).x+" "+e.y,r.push(e);l.setAttribute("d",n+t)}}}}mxFreehand.prototype.NO_SMOOTHING=1,mxFreehand.prototype.MILD_SMOOTHING=4,mxFreehand.prototype.NORMAL_SMOOTHING=8,mxFreehand.prototype.VERY_SMOOTH_SMOOTHING=12,mxFreehand.prototype.SUPER_SMOOTH_SMOOTHING=16,mxFreehand.prototype.HYPER_SMOOTH_SMOOTHING=20;