@leafer-in/editor 1.0.0-rc.5 → 1.0.0-rc.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/editor.esm.js +1122 -362
- package/dist/editor.esm.min.js +1 -1
- package/dist/editor.js +1134 -363
- package/dist/editor.min.js +1 -1
- package/package.json +4 -6
- package/src/Editor.ts +224 -150
- package/src/config.ts +29 -0
- package/src/decorator/data.ts +13 -0
- package/src/display/EditBox.ts +279 -0
- package/src/display/EditPoint.ts +9 -0
- package/src/display/EditSelect.ts +234 -0
- package/src/display/SelectArea.ts +30 -0
- package/src/display/Stroker.ts +89 -0
- package/src/editor/cursor.ts +58 -0
- package/src/editor/simulate.ts +14 -0
- package/src/editor/target.ts +38 -0
- package/src/event/EditorEvent.ts +23 -0
- package/src/event/EditorMoveEvent.ts +17 -0
- package/src/event/EditorRotateEvent.ts +4 -10
- package/src/event/EditorScaleEvent.ts +27 -0
- package/src/event/EditorSkewEvent.ts +18 -0
- package/src/helper/EditDataHelper.ts +149 -0
- package/src/helper/EditSelectHelper.ts +33 -0
- package/src/helper/EditorHelper.ts +95 -0
- package/src/index.ts +26 -3
- package/src/tool/EditTool.ts +70 -0
- package/src/tool/{LineTool.ts → LineEditTool.ts} +15 -24
- package/src/tool/index.ts +21 -0
- package/types/index.d.ts +193 -43
- package/src/cursor.ts +0 -57
- package/src/event/EditorResizeEvent.ts +0 -34
- package/src/resize.ts +0 -87
- package/src/tool/RectTool.ts +0 -141
package/dist/editor.esm.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{MatrixHelper as t,Bounds as e,Matrix as i,Event as s,Group as o,Rect as r,Polygon as n,DataHelper as a,RotateEvent as h,PointHelper as l,MathHelper as c,DragEvent as g,PointerEvent as y,RenderEvent as d,KeyEvent as _}from"@leafer-ui/core";var u;!function(t){t[t.topLeft=0]="topLeft",t[t.top=1]="top",t[t.topRight=2]="topRight",t[t.right=3]="right",t[t.bottomRight=4]="bottomRight",t[t.bottom=5]="bottom",t[t.bottomLeft=6]="bottomLeft",t[t.left=7]="left"}(u||(u={}));const{scaleOfOuter:x,reset:b}=t,{topLeft:v,top:f,topRight:p,right:z,bottomRight:R,bottom:w,bottomLeft:m,left:E}=u,M={};const{topLeft:P,top:T,topRight:L,right:k,bottomRight:I,bottom:F,bottomLeft:O,left:D}=u;function X(t,e){const i=t.enterPoint;if(!i||!t.target||!t.visible)return;let{rotation:s}=t,{resizeCursor:o,rotateCursor:r,resizeable:n}=t.config;const a=t.tool.getMirrorData(t),{__direction:h,__isResizePoint:l}=i.__;t.enterPoint=i,l&&(e.metaKey||e.ctrlKey||!n)&&(o=r),(a.x||a.y)&&(Y(o=[...o],a.x,a.y),Y(r=[...r],a.y,a.x),a.x+a.y===1&&(s=-s));let c=(h+Math.round(s/45))%8;c<0&&(c+=8),i.cursor=l?o[c]:r[c]}function Y(t,e,i){if(e){const e=t[T],i=t[P],s=t[L];t[T]=t[F],t[P]=t[O],t[L]=t[I],t[F]=e,t[O]=i,t[I]=s}if(i){const e=t[D],i=t[P],s=t[O];t[D]=t[k],t[P]=t[L],t[O]=t[I],t[k]=e,t[L]=i,t[I]=s}}const A={name:"RectTool",getMirrorData(t){const{scaleX:e,scaleY:i}=t.target;return{x:e<0?1:0,y:i<0?1:0}},resize(t){const{target:e,bounds:i,resizeType:s,old:o}=t,{x:r,y:n,width:a,height:h}=i,l={x:r-o.x,y:n-o.y};e.innerToWorld(l,null,!0,e.parent),e.x+=l.x,e.y+=l.y,"scale"===s?(e.scaleX*=a/o.width,e.scaleY*=h/o.height):(a<0?(e.width=-a,e.scaleX*=-1):e.width!==a&&(e.width=a),h<0?(e.height=-h,e.scaleY*=-1):e.height!==h&&(e.height=h))},rotate(t){const{target:e,rotation:i,origin:s}=t;e.rotateOf(s,i)},update(t){const{target:s,config:o,rotatePoints:r,targetRect:n,rect:a,circle:h,resizeLines:l,resizePoints:c}=t,{type:g,resizeable:y,rotateable:d,stroke:_,pointFill:u,pointSize:x,pointRadius:b}=o,v={fill:u,stroke:_,width:x,height:x,cornerRadius:b},f=o.point instanceof Array?o.point:[o.point||v],p=new e(s.boxBounds),z=s.worldTransform,R=t.parent.worldTransform,w=new i(z);w.divide(R);const m=w.e,E=w.f;let{scaleX:M,scaleY:P,rotation:T,skewX:L,skewY:k}=z;M/=R.scaleX,P/=R.scaleY,T-=R.rotation,L-=R.skewX,k-=R.skewY;const{x:I,y:F,width:O,height:D}=p.scale(M,P);t.set({x:m,y:E,rotation:T,skewX:L,skewY:k}),n.set({x:I,y:F,width:p.width/M,height:p.height/P,scaleX:M,scaleY:P,visible:!0});const X=[{x:I,y:F},{x:I+O/2,y:F},{x:I+O,y:F},{x:I+O,y:F+D/2},{x:I+O,y:F+D},{x:I+O/2,y:F+D},{x:I,y:F+D},{x:I,y:F+D/2}],Y=[];let A,C,K,B,S;for(let t=0;t<8;t++)A=X[t],C=f[t%f.length],B=c[t],S=l[Math.floor(t/2)],K=r[t],B.set(C),B.x=K.x=S.x=A.x,B.y=K.y=S.y=A.y,B.visible=S.visible=y||d,K.visible=d&&y,t%2?((t+1)/2%2?(S.width=Math.abs(O),K.width=Math.max(10,Math.abs(O)-30)):(S.height=Math.abs(D),K.height=Math.max(10,Math.abs(D)-30)),B.rotation=90,B.visible="mobile"===g,K.visible=!1):(K.visible="mobile"!==g,Y.push(A.x,A.y));C=o.rotatePoint||C,h.set(C),h.x=I+O/2,C.y||(h.y=F-(10+(B.height+h.height)/2)*(this.getMirrorData(t).y?-1:1)),h.visible=d&&"mobile"===g,a.set(o.rect||{stroke:_}),a.points=Y,a.visible=!0}},{left:C,right:K}=u,B={name:"LineTool",getMirrorData:t=>({x:0,y:0}),resize(t){const{direction:e,dragEvent:i,lockRatio:s,around:o}=t,r=t.target,n={x:0,y:0},{toPoint:a}=r;r.rotation=0;let{x:h,y:l}=i.getInnerMove(r);s&&(Math.abs(h)>Math.abs(l)?l=0:h=0),e===C?(n.x+=h,n.y+=l,o&&(a.x-=h,a.y-=l)):(o&&(n.x-=h,n.y-=l),a.x+=h,a.y+=l),r.getLocalPointByInner(n,null,null,!0),r.getLocalPointByInner(a,null,null,!0),r.x=n.x,r.y=n.y,r.getInnerPointByLocal(a,null,null,!0),r.toPoint=a},rotate(t){A.rotate(t)},update(t){const{rotatePoints:e,circle:i,resizeLines:s,resizePoints:o}=t;A.update(t);for(let t=0;t<8;t++)t<4&&(s[t].visible=!1),o[t].visible=e[t].visible=t===C||t===K;i.visible=!1}};class S extends s{constructor(t,e){super(t),e&&Object.assign(this,e)}}S.RESIZE="editor.resize";class j extends s{constructor(t,e){super(t),e&&Object.assign(this,e)}}j.ROTATE="editor.rotate";class G extends o{get target(){return this._target}set target(t){this.__removeTargetEvents(),this.visible=!!t,this._target=t,t&&this.onTarget()}constructor(t,e){super(e),this.config={type:"pc",stroke:"#836DFF",pointFill:"#FFFFFF",pointSize:10,pointRadius:10,rotateGap:90,hideOnMove:!1,moveCursor:"move",resizeType:"auto",resizeCursor:["nwse-resize","ns-resize","nesw-resize","ew-resize","nwse-resize","ns-resize","nesw-resize","ew-resize"],rotateCursor:["ne-resize","e-resize","se-resize","s-resize","sw-resize","w-resize","nw-resize","n-resize"],resizeable:!0,rotateable:!0},this.resizePoints=[],this.rotatePoints=[],this.resizeLines=[],this.targetRect=new r({hitFill:"all",hitRadius:5}),this.rect=new n({hittable:!1,strokeAlign:"center"}),this.circle=new r({around:"center",hitRadius:10}),this.__eventIds=[],this.__targetEventIds=[],t&&(this.config=a.default(t,this.config)),this.init()}init(){let t,e,i;const{resizePoints:s,rotatePoints:o,resizeLines:n}=this,a=[{x:1,y:1},"center",{x:0,y:1},"center",{x:0,y:0},"center",{x:1,y:0},"center"];for(let h=0;h<8;h++)t=new r({around:a[h],width:15,height:15,hitFill:"all"}),o.push(t),this.__listenPointEvents(t,"rotate",h),h%2&&(e=new r({around:"center",width:10,height:10,hitFill:"all"}),n.push(e),this.__listenPointEvents(e,"resize",h)),i=new r({around:"center",hitRadius:5}),s.push(i),this.__listenPointEvents(i,"resize",h);this.__listenPointEvents(this.circle,"rotate",1),this.addMany(...o,this.targetRect,this.rect,this.circle,...n,...s),this.__listenEvents()}onTarget(){this.tool=this.getTool(this.target),this.waitLeafer((()=>{this.update(),this.updateMoveCursor(),this.__listenTargetEvents()}))}getTool(t){return"Line"===t.tag&&t.resizeable?B:A}update(){this.target&&this.tool.update(this)}onDrag(t){const{resizeable:e,rotateable:i}=this.config;t.metaKey||t.ctrlKey||!e?i&&this.onRotate(t):this.onResize(t)}onMove(t){const{target:e}=this,{x:i,y:s}=t.getLocalMove(e);t.shiftKey?Math.abs(i)>Math.abs(s)?e.x+=i:e.y+=s:(e.x+=i,e.y+=s)}onRotate(t){const{target:e}=this,{rotateGap:i}=this.config,{x:s,y:o,width:r,height:n}=e.boxBounds,a={x:s+r/2,y:o+n/2};let g;if(t instanceof h)g=t.rotation;else{const i=t,s={x:i.x-t.moveX,y:i.y-t.moveY};g=l.getChangeAngle(s,e.getWorldPoint(a),i)}g=c.getGapRotation(e.rotation+g,i)-e.rotation;const y=new j(j.ROTATE,{editor:this,target:e,origin:a,rotation:g});this.tool.rotate(y),e.emitEvent(y)}onResize(t){const{target:e}=this,{__direction:i}=t.current.__;let{resizeType:s,around:o,lockRatio:r}=this.config;t.shiftKey&&(r=!0),t.altKey&&!o&&(o="center"),"auto"===s&&(s=e.resizeable?"size":"scale");const n=function(t,e,i,s,o){o&&(i.x*=2,i.y*=2);let r,n=1,a=1;const{x:h,y:l,width:c,height:g}=t,y=(-i.y+g)/g,d=(i.x+c)/c,_=(i.y+g)/g,u=(-i.x+c)/c;switch(e){case f:a=y,s&&(n=a),r={x:h+c/2,y:l+g};break;case z:n=d,s&&(a=n),r={x:h,y:l+g/2};break;case w:a=_,s&&(n=a),r={x:h+c/2,y:l};break;case E:n=u,s&&(a=n),r={x:h+c,y:l+g/2};break;case v:a=y,n=u,s&&(n=a),r={x:h+c,y:l+g};break;case p:a=y,n=d,s&&(n=a),r={x:h,y:l+g};break;case R:a=_,n=d,s&&(n=a),r={x:h,y:l};break;case m:a=_,n=u,s&&(n=a),r={x:h+c,y:l}}return o&&(r="object"==typeof o?{x:h+c/o.x,y:l+g/o.y}:{x:h+c/2,y:l+g/2}),b(M),x(M,r,n,a),{bounds:{x:t.x+M.e,y:t.y+M.f,width:c*n,height:g*a},old:t,origin:r,scaleX:n,scaleY:a,direction:e,lockRatio:s,around:o}}(e.boxBounds,i,t.getInnerMove(this.targetRect),r,o),a=new S(S.RESIZE,Object.assign(Object.assign({},n),{target:e,editor:this,dragEvent:t,resizeType:s}));this.tool.resize(a),e.emitEvent(a)}updateMoveCursor(){this.targetRect.cursor=this.config.moveCursor}__listenEvents(){this.__eventIds=[this.targetRect.on_(g.START,(()=>{this.opacity=this.config.hideOnMove?0:1})),this.targetRect.on_(g.DRAG,this.onMove,this),this.targetRect.on_(g.END,(()=>{this.opacity=1})),this.targetRect.on_(y.ENTER,this.updateMoveCursor,this)]}__removeListenEvents(){this.targetRect.off_(this.__eventIds),this.__eventIds.length=0}__listenPointEvents(t,e,i){t.__.__direction=i;const s=t.__.__isResizePoint="resize"===e;t.on_(g.DRAG,s?this.onDrag:this.onRotate,this),t.on_(y.LEAVE,(()=>this.enterPoint=null)),t.on_(y.ENTER,(e=>{this.enterPoint=t,X(this,e)}))}__listenTargetEvents(){if(this.target){const{leafer:t}=this.target;this.__targetEventIds=[t.on_(d.START,this.update,this),t.on_([_.HOLD,_.UP],(t=>{X(this,t)}))]}}__removeTargetEvents(){if(this.__targetEventIds.length){const{leafer:t}=this.target;t&&t.off_(this.__targetEventIds),this.__targetEventIds.length=0}}destroy(){this.__removeListenEvents(),this._target=null,super.destroy()}}export{G as Editor,S as EditorResizeEvent,j as EditorRotateEvent,B as LineTool,A as RectTool};
|
|
1
|
+
import{Event as t,defineKey as e,MatrixHelper as i,UI as s,Paint as o,Group as n,Rect as r,Bounds as a,LeafList as h,PointerEvent as l,DragEvent as d,MoveEvent as c,ZoomEvent as g,PointHelper as u,AroundHelper as f,Box as p,Line as y,Matrix as x,DataHelper as w,RotateEvent as v,MathHelper as k,RenderEvent as m,KeyEvent as _,Creator as b}from"@leafer-ui/core";function E(t,e,i,s){var o,n=arguments.length,r=n<3?e:null===s?s=Object.getOwnPropertyDescriptor(e,i):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(t,e,i,s);else for(var a=t.length-1;a>=0;a--)(o=t[a])&&(r=(n<3?o(r):n>3?o(e,i,r):o(e,i))||r);return n>3&&r&&Object.defineProperty(e,i,r),r}"function"==typeof SuppressedError&&SuppressedError;class L extends t{constructor(t,e){super(t),e&&Object.assign(this,e)}}L.SELECT="editor.select",L.HOVER="editor.hover";class S extends L{constructor(t,e){super(t,e)}}S.MOVE="editor.move";class T extends L{constructor(t,e){super(t,e)}}T.SCALE="editor.scale";class O extends L{constructor(t,e){super(t,e)}}O.ROTATE="editor.rotate";class P extends L{constructor(t,e){super(t,e)}}function D(t){return(i,s)=>{const o="_"+s;e(i,s,{get(){return this[o]},set(e){this[o]!==e&&(this[o]=e,t(this))}})}}P.SKEW="editor.skew";const A=i.get(),{abs:z}=Math,{copy:R,scale:B}=i;class M extends s{constructor(){super(),this.list=[],this.hittable=!1,this.strokeAlign="center"}setTarget(t,e){const{stroke:i,strokeWidth:s}=e;this.set({stroke:i,strokeWidth:s}),this.target=t}__draw(t,e){const{list:i}=this;if(i.length){let s;const{stroke:n,strokeWidth:r}=this.__,{bounds:a}=e;for(let h=0;h<i.length;h++)if(s=i[h],a&&a.hit(s.__world,e.matrix)){let i;if("scale"===s.__.editSize){const o=z(s.__world.scaleX),n=z(s.__world.scaleY);if(o!==n){R(A,s.__world),B(A,1/o,1/n),t.setWorld(A,e.matrix),t.beginPath(),this.__.strokeWidth=r;const{x:a,y:h,width:l,height:d}=s.__layout.boxBounds;t.rect(a*o,h*n,l*o,d*n),i=!0}}i||(t.setWorld(s.__world,e.matrix),t.beginPath(),s.__.__pathForRender?s.__drawRenderPath(t):s.__drawPathByBox(t),this.__.strokeWidth=r/z(s.__world.scaleX)),"string"==typeof n?o.stroke(n,this,t,e):o.strokes(n,this,t,e)}this.__.strokeWidth=r}}destroy(){this.target=null,super.destroy()}}E([D((function(t){const e=t.target;t.list=e?e instanceof Array?e:[e]:[],t.forceUpdate()}))],M.prototype,"target",void 0);class I extends n{constructor(t){super(t),this.strokeArea=new r({strokeAlign:"center"}),this.fillArea=new r,this.visible=this.hittable=!1,this.addMany(this.fillArea,this.strokeArea)}setStyle(t,e){const{visible:i,stroke:s,strokeWidth:o}=t;this.visible=i,this.strokeArea.reset(Object.assign({stroke:s,strokeWidth:o},e||{})),this.fillArea.reset({visible:!e,fill:s,opacity:.2})}setBounds(t){this.strokeArea.set(t),this.fillArea.set(t)}}var W;!function(t){t[t.No=0]="No",t[t.Yes=1]="Yes",t[t.NoAndSkip=2]="NoAndSkip",t[t.YesAndSkip=3]="YesAndSkip"}(W||(W={}));const{No:Y,Yes:X,NoAndSkip:C,YesAndSkip:F}=W,K={findOne:t=>t.list.find((t=>t.editable)),findBounds(t,e){if(t.__.hittable&&!t.__.locked&&e.hit(t.__world)){if(t.__.editable){if(t.isBranch&&!t.__.hitChildren)return t.__.hitSelf?F:C;if(t.isFrame)return e.includes(t.__layout.boxBounds,t.__world)?F:Y;if(e.hit(t.__layout.boxBounds,t.__world)&&t.__.hitSelf)return X}return Y}return t.isBranch?C:Y}},{findOne:j}=K;class N extends n{get dragging(){return!!this.originList}get running(){return this.editor.hittable&&this.editor.config.selector}get isMoveMode(){return this.app&&this.app.interaction.moveMode}constructor(t){super(),this.hoverStroker=new M,this.targetStroker=new M,this.bounds=new a,this.selectArea=new I,this.__eventIds=[],this.editor=t,this.addMany(this.targetStroker,this.hoverStroker,this.selectArea),this.__listenEvents()}onHover(){const{editor:t}=this;if(!this.running||this.dragging||t.dragging)this.hoverStroker.target=null;else{const{stroke:e,strokeWidth:i,hover:s}=t.config;this.hoverStroker.setTarget(s?this.editor.hoverTarget:null,{stroke:e,strokeWidth:i})}}onSelect(){if(this.running){const{config:t,list:e}=this.editor,{stroke:i,strokeWidth:s}=t;this.targetStroker.setTarget(e,{stroke:i,strokeWidth:Math.max(1,s/2)}),this.hoverStroker.target=null}}update(){this.running&&this.targetStroker.forceUpdate()}onPointerMove(t){if(this.running&&!this.isMoveMode){const e=t.shiftKey?this.findDeepOne(t):j(t.path);this.editor.hoverTarget=this.editor.hasItem(e)?null:e}this.isMoveMode&&(this.editor.hoverTarget=null)}onBeforeDown(t){if(this.running&&!this.isMoveMode&&!t.middle){const e=this.lastDownLeaf=j(t.path);e?(t.shiftKey?this.editor.shiftItem(e):this.editor.target=e,this.editor.updateLayout(),e.leafer.interaction.updateDownData()):this.allow(t.target)&&(t.shiftKey||(this.editor.target=null))}}onTap(t){if(this.running&&t.shiftKey&&!t.middle&&!this.lastDownLeaf){const e=this.findDeepOne(t);e&&this.editor.shiftItem(e)}else this.isMoveMode&&(this.editor.target=null);this.lastDownLeaf=null}onDragStart(t){if(this.running&&this.allowDrag(t)){const{editor:e}=this,{stroke:i,strokeWidth:s,area:o}=e.config,{x:n,y:r}=t.getInner(this);this.bounds.set(n,r),this.selectArea.setStyle({visible:!0,stroke:i,strokeWidth:s,x:n,y:r},o),this.selectArea.setBounds(this.bounds.get()),this.originList=e.leafList.clone()}}onDrag(t){if(this.editor.dragging)this.onDragEnd();else if(this.dragging){const{editor:e}=this,i=t.getInnerTotal(this),s=this.bounds.clone().unsign(),o=new h(e.app.find(K.findBounds,s));if(this.bounds.width=i.x,this.bounds.height=i.y,this.selectArea.setBounds(s.get()),o.length){const t=[];this.originList.forEach((e=>{o.has(e)||t.push(e)})),o.forEach((e=>{this.originList.has(e)||t.push(e)})),e.target=t}else e.target=this.originList,e.leafList.length&&e.update()}}onDragEnd(){this.dragging&&(this.originList=null,this.selectArea.visible=!1)}onAutoMove(t){if(this.dragging){const{x:e,y:i}=t.getLocalMove(this);this.bounds.x+=e,this.bounds.y+=i}}allow(t){return t.leafer!==this.editor.leafer}allowDrag(t){return!(!this.editor.config.boxSelect||t.target.draggable)&&(!this.editor.hasTarget&&this.allow(t.target)||t.shiftKey&&!j(t.path))}findDeepOne(t){const e={exclude:new h(this.editor.editBox.rect)};return j(t.target.leafer.interaction.findPath(t,e))}__listenEvents(){const{editor:t}=this;t.waitLeafer((()=>{const{app:e}=t;e.selector.proxy=t,this.__eventIds=[t.on_(L.HOVER,this.onHover,this),t.on_(L.SELECT,this.onSelect,this),e.on_(l.MOVE,this.onPointerMove,this),e.on_(l.BEFORE_DOWN,this.onBeforeDown,this),e.on_(l.TAP,this.onTap,this),e.on_(d.START,this.onDragStart,this),e.on_(d.DRAG,this.onDrag,this),e.on_(d.END,this.onDragEnd,this),e.on_(c.MOVE,this.onAutoMove,this),e.on_([g.ZOOM,c.MOVE],(()=>{this.editor.hoverTarget=null}))]}))}__removeListenEvents(){this.__eventIds&&(this.off_(this.__eventIds),this.__eventIds.length=0)}destroy(){this.editor=this.originList=this.lastDownLeaf=null,this.__removeListenEvents(),super.destroy()}}var V;!function(t){t[t.topLeft=0]="topLeft",t[t.top=1]="top",t[t.topRight=2]="topRight",t[t.right=3]="right",t[t.bottomRight=4]="bottomRight",t[t.bottom=5]="bottom",t[t.bottomLeft=6]="bottomLeft",t[t.left=7]="left"}(V||(V={}));const{topLeft:G,top:H,topRight:U,right:Z,bottomRight:q,bottom:J,bottomLeft:Q,left:$}=V,{toPoint:tt}=f,et={getScaleData(t,e,i,s,o){let n,r=1,a=1;const{width:h,height:l}=t;o&&(i.x*=2,i.y*=2);const d=(-i.y+l)/l,c=(i.x+h)/h,g=(i.y+l)/l,u=(-i.x+h)/h;switch(e){case H:a=d,n={x:.5,y:1};break;case Z:r=c,n={x:0,y:.5};break;case J:a=g,n={x:.5,y:0};break;case $:r=u,n={x:1,y:.5};break;case G:a=d,r=u,n={x:1,y:1};break;case U:a=d,r=c,n={x:0,y:1};break;case q:a=g,r=c,n={x:0,y:0};break;case Q:a=g,r=u,n={x:1,y:0}}return s&&(1!==r?a=r:r=a),tt(o||n,t,n),{origin:n,scaleX:r,scaleY:a,direction:e,lockRatio:s,around:o}},getRotateData(t,e,i,s,o){let n;switch(e){case G:n={x:1,y:1};break;case U:n={x:0,y:1};break;case q:n={x:0,y:0};break;case Q:n={x:1,y:0};break;default:n={x:.5,y:.5}}return tt(o||n,t,n),{origin:n,rotation:u.getRotation(s,n,i)}},getSkewData(t,e,i,s){let o,n,r=0,a=0;switch(e){case H:n={x:.5,y:0},o={x:.5,y:1},r=1;break;case J:n={x:.5,y:1},o={x:.5,y:0},r=1;break;case $:n={x:0,y:.5},o={x:1,y:.5},a=1;break;case Z:n={x:1,y:.5},o={x:0,y:.5},a=1}const{x:h,y:l,width:d,height:c}=t;n.x=h+n.x*d,n.y=l+n.y*c,tt(s||o,t,o);const g=u.getRotation(n,o,{x:n.x+(r?i.x:0),y:n.y+(a?i.y:0)});return r?r=-g:a=g,{origin:o,skewX:r,skewY:a}},getAround:(t,e)=>e&&!t?f.center:t,getRotateDirection:(t,e,i=8)=>((t=(t+Math.round(e/(360/i)))%i)<0&&(t+=i),t)},{topLeft:it,top:st,topRight:ot,right:nt,bottomRight:rt,bottom:at,bottomLeft:ht,left:lt}=V;function dt(t,e){const{editBox:i}=t,s=i.enterPoint;if(!s||!t.hasTarget||!i.visible)return;let{rotation:o}=i,{resizeCursor:n,rotateCursor:r,resizeable:a,rotateable:h}=t.config;const{direction:l,pointType:d}=s;i.enterPoint=s;const c="resize"===d;if(c&&h&&(e.metaKey||e.ctrlKey||!a)&&(n=r),i.flipped){const{flippedX:t,flippedY:e}=i;gt(n=[...n],t,e),gt(r=[...r],e,t),i.flippedOne&&(o=-o)}const g=et.getRotateDirection(l,o);s.cursor=c?n[g]:r[g]}function ct(t){t.editBox.rect.cursor=t.config.moveCursor}function gt(t,e,i){if(e){const e=t[st],i=t[it],s=t[ot];t[st]=t[at],t[it]=t[ht],t[ot]=t[rt],t[at]=e,t[ht]=i,t[rt]=s}if(i){const e=t[lt],i=t[it],s=t[ht];t[lt]=t[nt],t[it]=t[ot],t[ht]=t[rt],t[nt]=e,t[ot]=i,t[rt]=s}}class ut extends p{}const ft=["top","right","bottom","left"];class pt extends n{get flipped(){return this.flippedX||this.flippedY}get flippedX(){return this.scaleX<0}get flippedY(){return this.scaleY<0}get flippedOne(){return this.scaleX*this.scaleY<0}constructor(t){super(),this.rect=new p({name:"rect",hitFill:"all",hitStroke:"none",strokeAlign:"center",hitRadius:5}),this.circle=new ut({name:"circle",strokeAlign:"outside",around:"center",cursor:"crosshair",hitRadius:5}),this.buttons=new n({around:"center",hitSelf:!1}),this.resizePoints=[],this.rotatePoints=[],this.resizeLines=[],this.__eventIds=[],this.editor=t,this.visible=!1,this.create(),this.__listenEvents()}create(){let t,e,i;const{resizePoints:s,rotatePoints:o,resizeLines:n,rect:r,circle:a,buttons:h}=this,l=[{x:1,y:1},{x:.5,y:1},{x:0,y:1},{x:0,y:.5},{x:0,y:0},{x:.5,y:0},{x:1,y:0},{x:1,y:.5}];for(let r=0;r<8;r++)t=new ut({around:l[r],width:15,height:15,hitFill:"all"}),o.push(t),this.listenPointEvents(t,"rotate",r),r%2&&(e=new ut({name:"resize-line",around:"center",width:10,height:10,hitFill:"all"}),n.push(e),this.listenPointEvents(e,"resize",r)),i=new ut({name:"resize-point",around:"center",strokeAlign:"outside",hitRadius:5}),s.push(i),this.listenPointEvents(i,"resize",r);h.add(a),this.listenPointEvents(a,"rotate",2),this.addMany(...o,r,h,...n,...s)}update(t){const{config:e,list:i}=this.editor,{width:s,height:o}=t,{rect:n,circle:r,resizePoints:a,rotatePoints:h,resizeLines:l}=this,{middlePoint:d,resizeable:c,rotateable:g,stroke:u,strokeWidth:f}=e,p=this.getDirection8Points(t),y=this.getPointsStyle(),x=this.getMiddlePointsStyle();let w,v,k,m,_;this.visible=i[0]&&!i[0].locked;for(let t=0;t<8;t++)w=p[t],v=this.getPointStyle(t%2?x[(t-1)/2%x.length]:y[t/2%y.length]),m=a[t],k=h[t],_=l[Math.floor(t/2)],m.set(v),m.set(w),k.set(w),_.set(w),m.visible=_.visible=c||g,k.visible=g&&c,t%2?(m.visible=k.visible=!!d,(t+1)/2%2?(_.width=s,m.width>s-30&&(m.visible=!1)):(_.height=o,m.rotation=90,m.width>o-30&&(m.visible=!1))):m.rotation=t/2*90;r.visible=g&&!!e.rotatePoint,r.set(this.getPointStyle(e.rotatePoint||y[0])),n.set(Object.assign({stroke:u,strokeWidth:f},e.rect||{})),n.set(Object.assign(Object.assign({},t),{visible:!0})),this.layoutButtons()}layoutButtons(){const{buttons:t,resizePoints:e}=this,{buttonsDirection:i,buttonsFixed:s,buttonsMargin:o,middlePoint:n}=this.editor.config,{flippedX:r,flippedY:a}=this;let h=ft.indexOf(i);(h%2&&r||(h+1)%2&&a)&&s&&(h=(h+2)%4);const l=s?et.getRotateDirection(h,this.flippedOne?this.rotation:-this.rotation,4):h,d=e[2*l+1],c=l%2,g=l&&3!==l?1:-1,u=(o+(h%2?(n?d.width:0)+t.boxBounds.width:(n?d.height:0)+t.boxBounds.height)/2)*g;c?(t.x=d.x+u,t.y=d.y):(t.x=d.x,t.y=d.y+u),s&&(t.rotation=90*(l-h),t.scaleX=r?-1:1,t.scaleY=a?-1:1)}getPointStyle(t){const{stroke:e,strokeWidth:i,pointFill:s,pointSize:o,pointRadius:n}=this.editor.config,r={fill:s,stroke:e,strokeWidth:i,width:o,height:o,cornerRadius:n};return t?Object.assign(r,t):r}getPointsStyle(){const{point:t}=this.editor.config;return t instanceof Array?t:[t]}getMiddlePointsStyle(){const{middlePoint:t}=this.editor.config;return t instanceof Array?t:t?[t]:this.getPointsStyle()}getDirection8Points(t){const{x:e,y:i,width:s,height:o}=t;return[{x:e,y:i},{x:e+s/2,y:i},{x:e+s,y:i},{x:e+s,y:i+o/2},{x:e+s,y:i+o},{x:e+s/2,y:i+o},{x:e,y:i+o},{x:e,y:i+o/2}]}onDragStart(t){this.dragging=!0,"rect"===t.target.name&&(this.editor.opacity=this.editor.config.hideOnMove?0:1)}onDragEnd(t){this.dragging=!1,"rect"===t.target.name&&(this.editor.opacity=1)}onDrag(t){const{editor:e}=this;"rotate"===t.current.pointType||t.metaKey||t.ctrlKey||!e.config.resizeable?e.config.rotateable&&e.onRotate(t):e.onScale(t)}onArrow(t){if(this.editor.hasTarget){const e={x:0,y:0},i=t.shiftKey?10:1;switch(t.code){case"ArrowDown":e.y=i;break;case"ArrowUp":e.y=-i;break;case"ArrowLeft":e.x=-i;break;case"ArrowRight":e.x=i}(e.x||e.y)&&this.editor.move(e.x,e.y)}}onDoubleClick(){const{editor:t}=this;t.single&&t.element.isBranch}listenPointEvents(t,e,i){const{editor:s}=this;t.direction=i,t.pointType=e,t.on_(d.START,this.onDragStart,this),t.on_(d.DRAG,this.onDrag,this),t.on_(d.END,this.onDragEnd,this),t.on_(l.LEAVE,(()=>this.enterPoint=null)),"circle"!==t.name&&t.on_(l.ENTER,(e=>{this.enterPoint=t,dt(s,e)}))}__listenEvents(){const{rect:t,editor:e}=this;this.__eventIds=[e.on_(L.SELECT,(()=>{this.visible=e.hasTarget})),t.on_(d.START,this.onDragStart,this),t.on_(d.DRAG,e.onMove,e),t.on_(d.END,this.onDragEnd,this),t.on_(l.ENTER,(()=>ct(e))),t.on_(l.DOUBLE_CLICK,this.onDoubleClick,this)]}__removeListenEvents(){this.off_(this.__eventIds),this.__eventIds.length=0}destroy(){this.editor=null,this.__removeListenEvents(),super.destroy()}}const yt={editSize:"auto",stroke:"#836DFF",strokeWidth:2,pointFill:"#FFFFFF",pointSize:8,pointRadius:16,rotateGap:45,buttonsDirection:"bottom",buttonsMargin:12,moveCursor:"move",resizeCursor:["nwse-resize","ns-resize","nesw-resize","ew-resize","nwse-resize","ns-resize","nesw-resize","ew-resize"],rotateCursor:["ne-resize","e-resize","se-resize","s-resize","sw-resize","w-resize","nw-resize","n-resize"],selector:!0,hover:!0,boxSelect:!0,resizeable:!0,rotateable:!0,skewable:!0};class xt{constructor(){this.tag="EditTool"}onMove(t){const{moveX:e,moveY:i,editor:s}=t,{app:o,list:n}=s;o.lockLayout(),n.forEach((t=>{const s=t.getLocalPoint({x:e,y:i},null,!0);t.move(s.x,s.y)})),o.unlockLayout()}onScale(t){const{scaleX:e,scaleY:i,transform:s,worldOrigin:o,editor:n}=t,{app:r,list:a}=n;r.lockLayout(),a.forEach((t=>{const r="size"===n.getEditSize(t);s?t.transform(s,r):t.scaleOf(t.getInnerPoint(o),e,i,r)})),r.unlockLayout()}onRotate(t){const{rotation:e,worldOrigin:i,editor:s}=t,{app:o,list:n}=s;o.lockLayout(),n.forEach((t=>{t.rotateOf(t.getInnerPoint(i),e)})),o.unlockLayout()}onSkew(t){const{skewX:e,skewY:i,transform:s,worldOrigin:o,editor:n}=t,{app:r,list:a}=n;r.lockLayout(),a.forEach((t=>{const r="size"===n.getEditSize(t);s?t.transform(s,r):t.skewOf(t.getInnerPoint(o),e,i,r)})),r.unlockLayout()}update(t){const{simulateTarget:e,element:i}=t;t.multiple&&e.parent.updateLayout();const{x:s,y:o,scaleX:n,scaleY:r,rotation:a,skewX:h,skewY:l,width:d,height:c}=i.getLayoutBounds("box",t,!0);t.editBox.set({x:s,y:o,scaleX:n,scaleY:r,rotation:a,skewX:h,skewY:l}),t.editBox.update({x:0,y:0,width:d,height:c})}}xt.list=[];const{left:wt,right:vt}=V;class kt extends xt{constructor(){super(...arguments),this.tag="LineEditTool",this.scaleOfEvent=!0}onScaleWithDrag(t){const{drag:e,direction:i,lockRatio:s,around:o}=t,n=t.target,r={x:0,y:0},{toPoint:a}=n;n.rotation=0;let{x:h,y:l}=e.getInnerMove(n);s&&(Math.abs(h)>Math.abs(l)?l=0:h=0),i===wt?(r.x+=h,r.y+=l,o&&(a.x-=h,a.y-=l)):(o&&(r.x-=h,r.y-=l),a.x+=h,a.y+=l),n.getLocalPointByInner(r,null,null,!0),n.getLocalPointByInner(a,null,null,!0),n.x=r.x,n.y=r.y,n.getInnerPointByLocal(a,null,null,!0),n.toPoint=a}onSkew(t){}update(t){const{rotatePoints:e,resizeLines:i,resizePoints:s}=t.editBox;super.update(t);for(let t=0;t<8;t++)t<4&&(i[t].visible=!1),s[t].visible=e[t].visible=t===wt||t===vt}}const mt=(t,e)=>t.parent.children.indexOf(t)-e.parent.children.indexOf(e),_t=(t,e)=>e.parent.children.indexOf(e)-t.parent.children.indexOf(t),bt={group(t,e,i){t.sort(_t);const{app:s,parent:o}=t[0];i||(i=new n),o.addAt(i,o.children.indexOf(t[0])),t.sort(mt);const r=new x(e.worldTransform);return r.divideParent(o.worldTransform),i.setTransform(r),i.editable=!0,i.hitChildren=!1,s.lockLayout(),t.forEach((t=>t.dropTo(i))),s.unlockLayout(),i},ungroup(t){const{app:e}=t[0],i=[];return e.lockLayout(),t.forEach((t=>{if(t.isBranch){const{parent:e,children:s}=t;for(;s.length;)i.push(s[0]),s[0].dropTo(e,e.children.indexOf(t));t.remove()}else i.push(t)})),e.unlockLayout(),i},toTop(t){t.sort(mt),t.forEach((t=>{let e;const{parent:i}=t;if(i){const{children:s}=i,o=s.length-1,n=s[o].__.zIndex,r=s.indexOf(t);r!==o?(s.splice(r,1),s.push(t),e=n+1):e=n,t.zIndex=e}}))},toBottom(t){t.sort(_t),t.forEach((t=>{let e;const{parent:i}=t;if(i){const{children:s}=i,o=s[0].__.zIndex,n=s.indexOf(t);0!==n?(s.splice(n,1),s.unshift(t),e=o-1):e=o,t.zIndex=e}}))}};class Et extends n{get list(){return this.leafList.list}get hasTarget(){return!!this.list.length}get multiple(){return this.list.length>1}get single(){return 1===this.list.length}get element(){return this.multiple?this.simulateTarget:this.list[0]}get buttons(){return this.editBox.buttons}get dragging(){return this.editBox.dragging}constructor(t,e){super(e),this.config=yt,this.leafList=new h,this.simulateTarget=new r({visible:!1}),this.editBox=new pt(this),this.selector=new N(this),this.targetEventIds=[],t&&(this.config=w.default(t,this.config)),this.addMany(this.selector,this.editBox)}hasItem(t){return this.leafList.has(t)}addItem(t){this.hasItem(t)||t.locked||(this.leafList.add(t),this.target=this.leafList.list)}removeItem(t){this.hasItem(t)&&(this.leafList.remove(t),this.target=this.leafList.list)}shiftItem(t){this.hasItem(t)?this.removeItem(t):this.addItem(t)}update(){this.target&&(this.editTool.update(this),this.selector.update())}updateEditTool(){this.editTool=function(t){if(1===t.length){const e=t[0];return e instanceof y&&!e.points?new kt:new xt}return new xt}(this.list)}getEditSize(t){let{editSize:e}=this.config;return"auto"===e?t.editSize:e}onMove(t){const e=t.getLocalMove(this.element);t.shiftKey&&(Math.abs(e.x)>Math.abs(e.y)?e.y=0:e.x=0),this.move(e.x,e.y)}onScale(t){const{element:e}=this,{direction:i}=t.current;let{around:s,lockRatio:o}=this.config;t.shiftKey&&(o=!0);const n=et.getScaleData(e.boxBounds,i,t.getInnerMove(e),o,et.getAround(s,t.altKey));this.editTool.onScaleWithDrag?(n.drag=t,this.scaleWithDrag(n)):this.scaleOf(n.origin,n.scaleX,n.scaleY)}onRotate(t){const{skewable:e,around:i,rotateGap:s}=this.config,{direction:o,name:n}=t.current;if(e&&"resize-line"===n)return this.onSkew(t);const{element:r,editBox:a}=this;let h,l;if(t instanceof v)l=t.rotation,h=r.getInnerPoint(t);else{const e={x:t.x-t.moveX,y:t.y-t.moveY},s=et.getRotateData(r.boxBounds,o,t.getInner(r),r.getInnerPoint(e),t.shiftKey?null:i||"center");l=s.rotation,h=s.origin}l=k.getGapRotation(l,s,r.rotation),l&&(a.flippedOne&&(l=-l),this.rotateOf(h,l))}onSkew(t){const{element:e}=this,{around:i}=this.config,{origin:s,skewX:o,skewY:n}=et.getSkewData(e.boxBounds,t.current.direction,t.getInnerMove(e),et.getAround(i,t.altKey));(o||n)&&this.skewOf(s,o,n)}move(t,e){const{element:i}=this,s=i.getWorldPointByLocal({x:t,y:e},null,!0),o=new S(S.MOVE,{target:i,editor:this,moveX:s.x,moveY:s.y});this.editTool.onMove(o),this.emitEvent(o),this.multiple&&i.move(t,e)}scaleWithDrag(t){const{element:e}=this,i=e.getWorldPoint(t.origin),s=new T(T.SCALE,Object.assign(Object.assign({},t),{target:e,editor:this,worldOrigin:i}));this.editTool.onScaleWithDrag(s),this.emitEvent(s)}scaleOf(t,e,i=e,s){const{element:o}=this,n=o.getWorldPoint(t);let r;if(this.multiple){const s=Object.assign({},o.localTransform);o.scaleOf(t,e,i),r=new x(o.localTransform).divide(s)}const a=new T(T.SCALE,{target:o,editor:this,worldOrigin:n,scaleX:e,scaleY:i,transform:r});this.editTool.onScale(a),this.emitEvent(a)}rotateOf(t,e){const{element:i}=this,s=i.getWorldPoint(t),o=new O(O.ROTATE,{target:i,editor:this,worldOrigin:s,rotation:e});this.editTool.onRotate(o),this.emitEvent(o),this.multiple&&i.rotateOf(t,e)}skewOf(t,e,i=0,s){const{element:o}=this,n=o.getWorldPoint(t);let r;if(this.multiple){const s=Object.assign({},o.localTransform);o.skewOf(t,e,i),r=new x(o.localTransform).divide(s)}const a=new P(P.SKEW,{target:o,editor:this,skewX:e,skewY:i,transform:r,worldOrigin:n});this.editTool.onSkew(a),this.emitEvent(a)}group(){this.multiple&&(this.target=bt.group(this.list,this.element))}ungroup(){this.list.length&&(this.target=bt.ungroup(this.list))}lock(){this.list.forEach((t=>t.locked=!0)),this.update()}unlock(){this.list.forEach((t=>t.locked=!1)),this.update()}toTop(){this.list.length&&(bt.toTop(this.list),this.leafList.update())}toBottom(){this.list.length&&(bt.toBottom(this.list),this.leafList.update())}listenTargetEvents(){if(!this.targetEventIds.length){const{leafer:t}=this.list[0];this.targetEventIds=[t.on_(m.START,this.update,this),t.on_([_.HOLD,_.UP],(t=>{dt(this,t)})),t.on_(_.DOWN,this.editBox.onArrow,this.editBox)]}}removeTargetEvents(){const{targetEventIds:t}=this;t.length&&(this.off_(t),t.length=0)}destroy(){this.destroyed||(this.simulateTarget.destroy(),this.target=this.hoverTarget=this.simulateTarget=null,super.destroy())}}E([D((function(t){t.emitEvent(new L(L.HOVER,{editor:t}))}))],Et.prototype,"hoverTarget",void 0),E([D((function(t){const{target:e}=t;e?t.leafList=e instanceof h?e:new h(e):t.leafList.reset(),t.emitEvent(new L(L.SELECT,{editor:t})),t.hasTarget?t.waitLeafer((()=>{t.multiple&&function(t){const{simulateTarget:e,leafList:i}=t,{x:s,y:o,width:n,height:r}=(new a).setListWithFn(i.list,(t=>t.worldBoxBounds)),h=e.parent=i.list[0].leafer.zoomLayer,{scaleX:l,scaleY:d,e:c,f:g}=h.__world;e.reset({x:(s-c)/l,y:(o-g)/d,width:n/l,height:r/d})}(t),ct(t),t.updateEditTool(),t.update(),t.listenTargetEvents()})):t.removeTargetEvents()}))],Et.prototype,"target",void 0),b.editor=function(t){return new Et(t)};export{pt as EditBox,et as EditDataHelper,ut as EditPoint,N as EditSelect,K as EditSelectHelper,xt as EditTool,Et as Editor,L as EditorEvent,bt as EditorHelper,S as EditorMoveEvent,O as EditorRotateEvent,T as EditorScaleEvent,P as EditorSkewEvent,kt as LineEditTool,I as SelectArea,M as Stroker};
|