UNPKG

@leafer-in/resize

Version:
3 lines (2 loc) 3.88 kB
import{PathCommandMap as t,MatrixHelper as e,Direction9 as i,isUndefined as s,isArray as o,isObject as a,Leaf as n,Text as c,Path as h,Line as l,Polygon as r,Group as p,Box as _,Plugin as f}from"@leafer-ui/draw";const{M:d,L:u,C:g,Q:y,Z:z,N:x,D:b,X:k,G:R,F:S,O:w,P:m,U:v}=t,O={scale(t,e,i){if(!t)return;let s,o=0,a=t.length;for(;o<a;)switch(s=t[o],s){case d:case u:B(t,e,i,o,1),o+=3;break;case g:B(t,e,i,o,3),o+=7;break;case y:B(t,e,i,o,2),o+=5;break;case z:o+=1;break;case x:B(t,e,i,o,2),o+=5;break;case b:B(t,e,i,o,2),o+=9;break;case k:B(t,e,i,o,2),o+=6;break;case R:B(t,e,i,o,2),o+=9;break;case S:B(t,e,i,o,2),o+=5;break;case w:t[o]=R,t.splice(o+4,0,t[o+3],0),B(t,e,i,o,2),o+=9,a+=2;break;case m:t[o]=S,t.splice(o+4,0,t[o+3]),B(t,e,i,o,2),o+=5,a+=1;break;case v:B(t,e,i,o,2),o+=6}},scalePoints(t,e,i,s,o){for(let a=o?s+1:0,n=o?a+2*o:t.length;a<n;a+=2)t[a]*=e,t[a+1]*=i}},{scalePoints:B}=O,C=e.get(),{topLeft:H,top:I,topRight:P,right:W,bottom:X,left:A}=i;function F(t,e,i){t.pathInputed?M(t,e,i):(1!==e&&(t.width*=e),1!==i&&(t.height*=i))}function L(t,e,i,n){let c=e;if(!s(n)){const s=t.__layout;let{width:o,height:a}=s.boxBounds;switch(o*=i-e,a*=e-i,n){case I:case X:c=i,s.affectScaleOrRotation?t.moveInner(-o/2,0):t.x-=o/2;break;case A:case W:s.affectScaleOrRotation?t.moveInner(0,-a/2):t.y-=a/2;break;case H:case P:s.affectScaleOrRotation?t.moveInner(0,-a):t.y-=a}}t.fontSize*=c;const h=t.__,{padding:l,lineHeight:r,letterSpacing:p}=h;l&&(t.padding=o(l)?l.map(t=>t*c):l*c),h.__autoWidth||(t.width*=c),h.__autoHeight||(t.height*=c),a(r)?"px"===r.type&&(h.lineHeight={type:"px",value:r.value*c}):r&&(h.lineHeight=r*c),a(p)?"px"===p.type&&(h.letterSpacing={type:"px",value:p.value*c}):p&&(h.letterSpacing=p*c)}function M(t,e,i){O.scale(t.__.path,e,i),t.path=t.__.path}function Y(t,e,i){const{points:s}=t;a(s[0])?s.forEach(t=>{t.x*=e,t.y*=i}):O.scalePoints(s,e,i),t.points=s}function D(t,e,i){const{children:s}=t;for(let t=0;t<s.length;t++)C.a=e,C.d=i,s[t].transform(C,!0)}const E=n.prototype,G={},N={},{copy:Q,scale:U,toOuterPoint:Z}=e;E.scaleResize=function(t,e=t,i,s){const o=this;if(i||o.editConfig&&"scale"===o.editConfig.editSize)o.scaleX*=t,o.scaleY*=e;else{if(t<0&&(o.scaleX*=-1,t=-t),e<0&&(o.scaleY*=-1,e=-e),"stroke"===s){Q(N,this.__localMatrix),t<0&&U(N,-1,1),e<0&&U(N,1,-1);const{boxBounds:i,strokeBounds:s}=this.__layout,{x:o,y:a,width:n,height:c}=s;G.x=(o-i.x)*(t-1),G.y=(a-i.y)*(e-1),Z(N,G,G,!0),this.x+=G.x,this.y+=G.y,t=(n*t-(n-i.width))/i.width,e=(c*e-(c-i.height))/i.height}this.__scaleResize(t,e)}},E.__scaleResize=function(t,e){F(this,t,e)},E.resizeWidth=function(t,e="inner"){const i=t/this.getBounds("box",e).width||1;this.scaleOf("top-left",i,this.__.lockRatio?i:1,!0)},E.resizeHeight=function(t,e="inner"){const i=t/this.getBounds("box",e).height||1;this.scaleOf("top-left",this.__.lockRatio?i:1,i,!0)},c.prototype.__scaleResize=function(t,e){const{app:i,editConfig:s}=this,o=i&&i.editor,a=o&&o.dragPoint;if(this.__.resizeFontSize||s&&"font-size"===s.editSize||a&&"font-size"===o.mergedConfig.editSize)L(this,t,e,a&&a.direction);else{const{__autoWidth:i,__autoHeight:s,textAlign:o,verticalAlign:a}=this.__,{boxBounds:n}=this.__layout;i&&"left"!==o&&1!==t&&(this.x+=n.x),s&&"top"!==a&&1!==e&&(this.y+=n.y),F(this,t,e)}},h.prototype.__scaleResize=function(t,e){M(this,t,e)},l.prototype.__scaleResize=function(t,e){this.pathInputed?M(this,t,e):this.points?Y(this,t,e):this.width*=t},r.prototype.__scaleResize=function(t,e){this.pathInputed?M(this,t,e):this.points?Y(this,t,e):F(this,t,e)},p.prototype.__scaleResize=function(t,e){D(this,t,e)},_.prototype.__scaleResize=function(t,e){const{resizeChildren:i,__autoSize:s}=this.__;s&&i||F(this,t,e),i&&D(this,t,e)},f.add("resize");export{O as PathScaler,F as scaleResize,L as scaleResizeFontSize,D as scaleResizeGroup,M as scaleResizePath,Y as scaleResizePoints}; //# sourceMappingURL=resize.esm.min.js.map