polotno 1.2.2 → 1.2.3
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/canvas/image-element.js +1 -1
- package/canvas/use-transformer-snap.js +1 -1
- package/model/store.js +1 -1
- package/package.json +1 -1
- package/polotno.bundle.js +10 -10
- package/side-panel/background-panel.js +1 -1
- package/side-panel/photos-panel.js +1 -1
- package/side-panel/side-panel.js +1 -1
- package/side-panel/tab-button.js +1 -1
- package/side-panel/text-panel.js +1 -1
- package/utils/svg.js +1 -1
- package/utils/wait.d.ts +1 -0
- package/utils/wait.js +1 -0
package/canvas/image-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,a){void 0===a&&(a=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,a,o)}:function(e,t,r,a){void 0===a&&(a=r),e[a]=t[r]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&__createBinding(t,e,r);return __setModuleDefault(t,e),t},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ImageElement=exports.useImageLoader=exports.setImageLoaderHook=void 0;const react_1=__importDefault(require("react")),mobx_react_lite_1=require("mobx-react-lite"),mobx_1=require("mobx"),react_konva_1=require("react-konva"),use_image_1=__importDefault(require("use-image")),konva_1=__importDefault(require("konva")),react_konva_utils_1=require("react-konva-utils"),highlighter_1=require("./highlighter"),loader_1=require("../utils/loader"),svg=__importStar(require("../utils/svg")),flags_1=require("../utils/flags"),apply_filters_1=require("./apply-filters"),use_fadein_1=require("./use-fadein");async function getFixedUrl(e){if(!(e.indexOf("data:image/svg+xml")>=0||e.indexOf(".svg")>=0))return e;const t=await svg.urlToString(e),r=svg.fixSize(t);return svg.svgToURL(r)}const useSizeFixer=e=>{const[t,r]=react_1.default.useState(e);return react_1.default.useEffect((()=>{(async()=>{const a=await getFixedUrl(e);a!==t&&r(a)})()}),[e]),t},useFlip=(e,t,r)=>react_1.default.useMemo((()=>{var a,o;const{flipX:i,flipY:n}=e,h="svg"===e.type||e.src.indexOf("data:image/svg+xml")>=0||e.src.indexOf(".svg")>=0;if(!i&&!n&&!h)return t;if(!t||!t.width||!t.height)return null;const c=document.createElement("canvas");let d=1;"svg"===e.type&&(d=Math.max(e.width/t.width*r,e.height/t.height*r)),c.width=Math.max(t.width*d,1),c.height=Math.max(t.height*d,1);let l=i?-c.width:0,s=n?-c.height:0;return null===(a=c.getContext("2d"))||void 0===a||a.scale(i?-1:1,n?-1:1),null===(o=c.getContext("2d"))||void 0===o||o.drawImage(t,l,s,c.width,c.height),c}),[e.flipX,e.flipY,t,e.width,e.height,r]);function getCrop(e,t){const r=t.width/t.height;let a,o;r>=e.width/e.height?(a=e.width,o=e.width/r):(a=e.height*r,o=e.height);return{x:(e.width-a)/2,y:(e.height-o)/2,width:a,height:o}}function downsample(e,t,r,a,o,i,n){for(var h=new ImageData(t,r),c=new Int32Array(e.data.buffer),d=e.width,l=new Int32Array(h.data.buffer),s=h.width,u=t/i,g=r/n,f=Math.round(1/u),m=Math.round(1/g),_=f*m,p=0;p<h.height;p++)for(var w=0;w<s;w++){for(var x=a+Math.round(w/u)+(o+Math.round(p/g))*d,v=0,y=0,M=0,b=0,E=0;E<m;E++)for(var I=0;I<f;I++){var k=c[x+I+E*d];v+=k<<24>>>24,y+=k<<16>>>24,M+=k<<8>>>24,b+=k>>>24}v=Math.round(v/_),y=Math.round(y/_),M=Math.round(M/_),b=Math.round(b/_),l[w+p*s]=b<<24|M<<16|y<<8|v}return h}function downScaleCanvas(e,t){var r,a;const o=document.createElement("canvas");o.width=e.width,o.height=e.height,null===(r=o.getContext("2d"))||void 0===r||r.drawImage(e,0,0,o.width,o.height);const i=downsample(o.getContext("2d").getImageData(0,0,o.width,o.height),Math.floor(o.width*t),Math.floor(o.height*t),0,0,o.width,o.height);return o.width=Math.floor(o.width*t),o.height=Math.floor(o.height*t),null===(a=o.getContext("2d"))||void 0===a||a.putImageData(i,0,0),o}const useMask=(e,t)=>{const[r,a]=useImageHook(e.maskSrc,"anonymous"),o=e.maskSrc?a:"loaded";return(0,exports.useImageLoader)(o,e.id),react_1.default.useMemo((()=>{if(!r)return t;if(!t||!t.width||!t.height)return t;const a=document.createElement("canvas");a.width=Math.max(t.width,1),a.height=Math.max(t.height,1);const o=a.getContext("2d");if(!o)return t;o.drawImage(t,0,0),o.globalCompositeOperation="source-in";const i=getCrop(r,e);return o.drawImage(r,i.x,i.y,i.width,i.height,0,0,t.width,t.height),a}),[t,r,e.width,e.height])},useCornerRadius=(e,t,r,a,o=0,i=!1)=>{const n=react_1.default.useMemo((()=>{if(t&&t.width&&t.height)return document.createElement("canvas")}),[t]);return react_1.default.useLayoutEffect((()=>{if(!n||!t)return;const h=Math.floor(Math.max(e.width*a,1)),c=Math.floor(Math.max(e.height*a,1));n.width=h,n.height=c;const d=n.getContext("2d");if(!d)return;const l=Math.min(o*a,h/2,c/2);l&&(d.beginPath(),d.moveTo(l,0),d.lineTo(h-l,0),d.arc(h-l,l,l,3*Math.PI/2,0,!1),d.lineTo(h,c-l),d.arc(h-l,c-l,l,0,Math.PI/2,!1),d.lineTo(l,c),d.arc(l,c-l,l,Math.PI/2,Math.PI,!1),d.lineTo(0,l),d.arc(l,l,l,Math.PI,3*Math.PI/2,!1),d.clip());const s=Math.max(e.width/r.width,e.height/r.height)*a,u=e.page._exporting&&flags_1.flags.imageDownScalingEnabled&&s<1&&!i,g=u?downScaleCanvas(t,s):t,f=u?{x:Math.floor(r.x*s),y:Math.floor(r.y*s),width:Math.floor(r.width*s),height:Math.floor(r.height*s)}:r;d.drawImage(g,f.x,f.y,f.width,f.height,0,0,n.width,n.height)}),[n,e.width,e.height,r.x,r.y,r.width,r.height,o,a,i,e.page._exporting]),n},useClip=(e,t,r,a)=>{const o=useSizeFixer(e.clipSrc||""),[i]=useImageHook(o,"anonymous"),n=react_1.default.useMemo((()=>{if(t&&i)return document.createElement("canvas")}),[t,i]);react_1.default.useLayoutEffect((()=>{var a;if(!i)return;if(!t||!t.width||!t.height)return;if(!i||!i.width||!i.height)return;if(!n)return;const o=document.createElement("canvas"),h=Math.max(e.width/i.width*r,e.height/i.height*r);o.width=i.width*h,o.height=i.height*h,null===(a=o.getContext("2d"))||void 0===a||a.drawImage(i,0,0,o.width,o.height),n.width=Math.max(t.width,1),n.height=Math.max(t.height,1);const c=n.getContext("2d");c&&(c.save(),c.drawImage(o,0,0,t.width,t.height),c.globalCompositeOperation="source-in",c.drawImage(t,0,0,n.width,n.height),c.restore())}),[n,t,i,e.width,e.height,r,...a]);return e.clipSrc&&i?n:t},PLACEHOLDER_CANVAS=document.createElement("canvas"),LoadingPlaceholder=(0,mobx_react_lite_1.observer)((({element:e})=>{const t=Math.min(30,e.width/4,e.height/4),r=react_1.default.useRef(null);return react_1.default.useEffect((()=>{const e=r.current;if(!e)return;const t=new konva_1.default.Animation((t=>{e.rotate(((null==t?void 0:t.timeDiff)||0)/2)}),e.getLayer());return t.start(),()=>{t.stop()}})),react_1.default.createElement(react_konva_1.Group,{x:e.x,y:e.y,rotation:e.rotation,listening:!1,opacity:e.opacity,hideInExport:!e.showInExport},react_1.default.createElement(react_konva_1.Rect,{width:e.width,height:e.height,fill:"rgba(124, 173, 212, 0.8)"}),react_1.default.createElement(react_konva_1.Arc,{ref:r,x:e.width/2,y:e.height/2,fill:"white",outerRadius:Math.abs(t),innerRadius:Math.max(1,t-5),angle:270}))})),ErrorPlaceholder=(0,mobx_react_lite_1.observer)((({element:e})=>{const t="Can not load the image...",r=Math.max(10,Math.min(30,e.width/t.length));return react_1.default.createElement(react_konva_1.Group,{x:e.x,y:e.y,rotation:e.rotation,listening:!1,opacity:e.opacity,hideInExport:!e.showInExport},react_1.default.createElement(react_konva_1.Rect,{width:e.width,height:e.height,fill:"rgba(223, 102, 102, 0.8)"}),react_1.default.createElement(react_konva_1.Text,{text:t,fontSize:r,width:e.width,height:e.height,align:"center",fill:"white",verticalAlign:"middle",padding:5}))}));let useImageHook=use_image_1.default;const setImageLoaderHook=e=>{useImageHook=e};exports.setImageLoaderHook=setImageLoaderHook;const useImageLoader=(e,t)=>{const r=react_1.default.useRef(),a=()=>{var e;null===(e=r.current)||void 0===e||e.call(r),r.current=void 0};react_1.default.useEffect((()=>a),[]),react_1.default.useLayoutEffect((()=>{"loading"!==e||r.current||(r.current=(0,loader_1.incrementLoader)(`image ${t}`)),"loading"!==e&&a(),"failed"===e&&(0,loader_1.triggerLoadError)(`image ${t}`)}),[e])};exports.useImageLoader=useImageLoader;const usePreviousImage=({image:e,status:t,type:r})=>{const a=react_1.default.useRef();react_1.default.useEffect((()=>{a.current=e||a.current}),[e]);return"failed"!==t||"failed"!==t&&"svg"===r?a.current:void 0},useImageSource=e=>{const[t,r]=react_1.default.useReducer((e=>e+1),0),a=react_1.default.useRef("loading"),o=react_1.default.useRef(e.src),i=react_1.default.useRef(e.src);return i.current!==e.src&&(i.current=e.src,a.current="loading"),react_1.default.useEffect((()=>{if("image"===e.type)return;if(!e.src)return;let t=!1;return(async()=>{a.current="loading",r();const i=await svg.urlToString(e.src),n=svg.fixSize(i),h=svg.replaceColors(n,e.colorsReplace);t||(o.current=h,a.current="loaded",r())})(),()=>{t=!0}}),[e.src,JSON.stringify(e.colorsReplace)]),"image"===e.type?[e.src,"loaded"]:[o.current,a.current]};exports.ImageElement=(0,mobx_react_lite_1.observer)((({element:e,store:t})=>{var r;const[a,o]=react_1.default.useState(!1),i=react_1.default.useRef(null),n=react_1.default.useRef(null),[h,c]=react_1.default.useState(!1),d=t.selectedElements.indexOf(e)>=0,[l,s]=useImageSource(e),[u,g]=useImageHook(l,"anonymous"),f="svg"!==e.type||"loaded"===s?g:"loading";(0,exports.useImageLoader)(f,e.id);const m=usePreviousImage({image:u,status:g,type:e.type}),_=useMask(e,useFlip(e,u||m,t._elementsPixelRatio))||PLACEHOLDER_CANVAS;let{cropX:p,cropY:w,cropWidth:x,cropHeight:v}=e;"loaded"!==g&&(p=w=0,x=v=1);const y=_.width*x,M=_.height*v,b=e.width/e.height;let E,I;const k=y/M,S="svg"===e.type;S?(E=y,I=M):b>=k?(E=y,I=y/b):(E=M*b,I=M);const C={x:_.width*p,y:_.height*w,width:E,height:I},R=null!==(r=e.cornerRadius)&&void 0!==r?r:0;let O=useClip(e,useCornerRadius(e,_,C,t._elementsPixelRatio,R,a||e._cropModeEnabled||"svg"===e.type),t._elementsPixelRatio,[C,R,t._elementsPixelRatio]);const L=Math.max(e.width/E,e.height/I);react_1.default.useEffect((()=>{var t;if(!e._cropModeEnabled)return;const r=null===(t=i.current)||void 0===t?void 0:t.getStage();function a(t){e._cropModeEnabled&&t.target!==n.current&&e.toggleCropMode(!1)}function o(t){e._cropModeEnabled&&"CANVAS"!==t.target.nodeName&&e.toggleCropMode(!1)}return document.body.addEventListener("click",o),null==r||r.on("click",a),null==r||r.on("tap",a),()=>{document.body.removeEventListener("click",o),document.body.removeEventListener("touchstart",o),null==r||r.off("click",a),null==r||r.off("click",a)}}),[e._cropModeEnabled]),react_1.default.useLayoutEffect((()=>{if(a||e._cropModeEnabled)return;(0,apply_filters_1.applyFilter)(i.current,e);return(0,mobx_1.autorun)((()=>{(0,apply_filters_1.applyFilter)(i.current,e)}),{delay:100})}),[_,a,x,v,e._cropModeEnabled]),react_1.default.useLayoutEffect((()=>{var t;a||e._cropModeEnabled?null===(t=i.current)||void 0===t||t.clearCache():(0,apply_filters_1.applyFilter)(i.current,e)}),[a,e.width,e.height,e._cropModeEnabled]),react_1.default.useEffect((()=>{(0,apply_filters_1.applyFilter)(i.current,e)}),[e.shadowEnabled,e.shadowBlur,e.cornerRadius,e.shadowColor,e.shadowOffsetX,e.shadowOffsetY,e.shadowOpacity]);const P=react_1.default.useRef(null),X=react_1.default.useRef(null),Y=react_1.default.useRef(null);react_1.default.useLayoutEffect((()=>{e._cropModeEnabled&&(X.current.nodes([P.current]),Y.current.nodes([n.current]))}),[e._cropModeEnabled]);const H=t=>{Math.round(t.target.x())>0&&(t.target.x(0),t.target.scaleX(1)),Math.round(t.target.y())>0&&(t.target.y(0),t.target.scaleY(1));const r=t.target.width()*t.target.scaleX(),a=t.target.height()*t.target.scaleY(),o=Math.min(1,E/r),i=Math.min(1,I/a),n=1-o,h=Math.min(n,Math.max(0,Math.round(-t.target.x())/r)),c=1-i,d=Math.min(c,Math.max(0,Math.round(-t.target.y())/a));t.target.setAttrs({x:-h*_.width,y:-d*_.height,scaleX:1,scaleY:1}),e.set({cropX:h,cropY:d,cropWidth:o,cropHeight:i})},T=()=>{"svg"!==e.type&&e.contentEditable&&setTimeout((()=>{e.toggleCropMode(!0)}))},D="svg"===e.type&&m,A="loading"===g&&!D,W="failed"===g,F=!A&&!W,q=react_1.default.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),z=F?e.opacity:0;(0,use_fadein_1.useFadeIn)(i,z);const B=e.selectable||"admin"===t.role;return react_1.default.createElement(react_1.default.Fragment,null,A&&react_1.default.createElement(LoadingPlaceholder,{element:e}),W&&react_1.default.createElement(ErrorPlaceholder,{element:e}),react_1.default.createElement(react_konva_1.Image,{ref:i,name:"element",id:e.id,image:O,x:e.x,y:e.y,width:e.width||1,height:e.height||1,rotation:e.rotation,opacity:z,shadowEnabled:e.shadowEnabled,shadowBlur:e.shadowBlur,shadowOffsetX:e.shadowOffsetX,shadowOffsetY:e.shadowOffsetY,shadowColor:e.shadowColor,shadowOpacity:e.shadowOpacity,customCrop:C,listening:B,draggable:e.draggable,hideInExport:!e.showInExport,onMouseEnter:()=>{c(!0)},onMouseLeave:()=>{c(!1)},onDragStart:()=>{t.history.startTransaction()},onDragMove:t=>{e.set({x:t.target.x(),y:t.target.y()})},onDragEnd:r=>{e.set({x:r.target.x(),y:r.target.y()}),t.history.endTransaction()},onDblClick:T,onDblTap:T,onTransformStart:()=>{o(!0),t.history.startTransaction(),q.current={cropX:e.cropX,cropY:e.cropY,cropWidth:e.cropWidth,cropHeight:e.cropHeight}},onTransform:t=>{var r;const a=t.currentTarget,o=Math.abs(a.scaleX()-1)<1e-7?1:a.scaleX(),i=Math.abs(a.scaleY()-1)<1e-7?1:a.scaleY();a.scaleX(1),a.scaleY(1);const n=null===(r=t.target.getStage())||void 0===r?void 0:r.findOne("Transformer"),h=1-E/_.width,c=Math.min(h,Math.max(0,e.cropX)),d=1-I/_.height,l=Math.min(d,Math.max(0,e.cropY)),s=n.getActiveAnchor(),u=!(s.indexOf("middle")>=0||s.indexOf("center")>=0),g=!u&&o<1&&q.current.cropHeight>I/_.height;let f=u?e.cropWidth:e.cropWidth*o;g&&(f=e.cropWidth);const m=!u&&i<1&&q.current.cropWidth>E/_.width;let p=u?e.cropHeight:e.cropHeight*i;m&&(p=e.cropHeight),S&&(f=e.cropWidth,p=e.cropHeight),e.set({cropX:c,cropY:l,x:a.x(),y:a.y(),width:a.width()*o,height:a.height()*i,rotation:t.target.rotation(),cropWidth:Math.min(f,1-c),cropHeight:Math.min(p,1-l)})},onTransformEnd:r=>{const a=r.currentTarget;e.set({width:a.width(),height:a.height(),x:a.x(),y:a.y(),rotation:r.target.rotation(),cropWidth:E/_.width,cropHeight:I/_.height}),o(!1),t.history.endTransaction()}}),react_1.default.createElement(react_konva_1.Rect,{x:e.x,y:e.y,width:e.width-e.borderSize,height:e.height-e.borderSize,offsetX:-e.borderSize/2,offsetY:-e.borderSize/2,stroke:e.borderColor,strokeWidth:e.borderSize,listening:!1,visible:!!e.borderSize,rotation:e.rotation,cornerRadius:Math.max(0,R-e.borderSize),hideInExport:!e.showInExport}),e._cropModeEnabled&&react_1.default.createElement(react_konva_utils_1.Portal,{selector:".page-abs-container",enabled:!0},react_1.default.createElement(react_konva_1.Rect,{x:-1500,y:-1500,width:4500,height:4500,fill:"rgba(0,0,0,0.3)"}),react_1.default.createElement(react_konva_1.Image,{listening:!1,image:O,x:e.x,y:e.y,width:e.width,height:e.height,rotation:e.rotation,shadowEnabled:e.shadowEnabled,shadowBlur:e.shadowBlur}),react_1.default.createElement(react_konva_1.Group,{x:e.x,y:e.y,rotation:e.rotation,scaleX:L,scaleY:L},react_1.default.createElement(react_konva_1.Image,{image:_,ref:n,opacity:.4,draggable:!0,x:-e.cropX*_.width,y:-e.cropY*_.height,onDragMove:H,onTransform:H}),react_1.default.createElement(react_konva_1.Transformer,{ref:Y,anchorSize:20,enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],boundBoxFunc:(e,t)=>t.width<5||t.height<5?e:t,rotateEnabled:!1,borderEnabled:!1,anchorCornerRadius:10,anchorStrokeWidth:2,borderStrokeWidth:2}),react_1.default.createElement(react_konva_1.Rect,{width:E,height:I,ref:P,listening:!1,onTransform:t=>{t.target.x()<-e.cropX*_.width-1e-9&&(t.target.x(-e.cropX*_.width),t.target.scaleX(1)),t.target.y()<-e.cropY*_.height-1e-9&&(t.target.y(-e.cropY*_.height),t.target.scaleY(1));const r=Math.min(1,Math.max(0,e.cropX+t.target.x()/_.width)),a=Math.min(1,Math.max(0,t.target.y()/_.height+e.cropY)),o=t.target.width()*t.target.scaleX(),i=t.target.height()*t.target.scaleY(),n=Math.min(1-r,o/_.width),h=Math.min(1-a,i/_.height),c=t.target.getAbsolutePosition(t.target.getParent().getParent());t.target.scale({x:1,y:1}),t.target.position({x:0,y:0}),e.set({x:c.x,y:c.y,cropX:r,cropY:a,cropWidth:n,cropHeight:h,width:Math.min(o*L,_.width*(1-r)*L),height:Math.min(i*L,_.height*(1-a)*L)})}}),react_1.default.createElement(react_konva_1.Transformer,{ref:X,enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],boundBoxFunc:(e,t)=>t.width<5||t.height<5?e:t,keepRatio:!1,rotateEnabled:!1,anchorFill:"rgb(240, 240, 240)",anchorStrokeWidth:2,borderStrokeWidth:2}))),(h||d)&&react_1.default.createElement(highlighter_1.Highlighter,{element:e}))}));
|
|
1
|
+
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,a){void 0===a&&(a=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,a,o)}:function(e,t,r,a){void 0===a&&(a=r),e[a]=t[r]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)"default"!==r&&Object.prototype.hasOwnProperty.call(e,r)&&__createBinding(t,e,r);return __setModuleDefault(t,e),t},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ImageElement=exports.useImageLoader=exports.setImageLoaderHook=void 0;const react_1=__importDefault(require("react")),mobx_react_lite_1=require("mobx-react-lite"),mobx_1=require("mobx"),react_konva_1=require("react-konva"),use_image_1=__importDefault(require("use-image")),konva_1=__importDefault(require("konva")),react_konva_utils_1=require("react-konva-utils"),highlighter_1=require("./highlighter"),loader_1=require("../utils/loader"),svg=__importStar(require("../utils/svg")),flags_1=require("../utils/flags"),apply_filters_1=require("./apply-filters"),use_fadein_1=require("./use-fadein");async function getFixedUrl(e){if(!(e.indexOf("data:image/svg+xml")>=0||e.indexOf(".svg")>=0))return e;const t=await svg.urlToString(e),r=svg.fixSize(t);return svg.svgToURL(r)}const useSizeFixer=e=>{const[t,r]=react_1.default.useState(e);return react_1.default.useEffect((()=>{(async()=>{const a=await getFixedUrl(e);a!==t&&r(a)})()}),[e]),t},useFlip=(e,t,r)=>react_1.default.useMemo((()=>{var a,o;const{flipX:i,flipY:n}=e,h="svg"===e.type||e.src.indexOf("data:image/svg+xml")>=0||e.src.indexOf(".svg")>=0;if(!i&&!n&&!h)return t;if(!t||!t.width||!t.height)return null;const c=document.createElement("canvas");let d=1;"svg"===e.type&&(d=Math.max(e.width/t.width*r,e.height/t.height*r)),c.width=Math.max(t.width*d,1),c.height=Math.max(t.height*d,1);let l=i?-c.width:0,s=n?-c.height:0;return null===(a=c.getContext("2d"))||void 0===a||a.scale(i?-1:1,n?-1:1),null===(o=c.getContext("2d"))||void 0===o||o.drawImage(t,l,s,c.width,c.height),c}),[e.flipX,e.flipY,t,e.width,e.height,r]);function getCrop(e,t){const r=t.width/t.height;let a,o;r>=e.width/e.height?(a=e.width,o=e.width/r):(a=e.height*r,o=e.height);return{x:(e.width-a)/2,y:(e.height-o)/2,width:a,height:o}}function downsample(e,t,r,a,o,i,n){for(var h=new ImageData(t,r),c=new Int32Array(e.data.buffer),d=e.width,l=new Int32Array(h.data.buffer),s=h.width,u=t/i,g=r/n,f=Math.round(1/u),m=Math.round(1/g),_=f*m,p=0;p<h.height;p++)for(var w=0;w<s;w++){for(var x=a+Math.round(w/u)+(o+Math.round(p/g))*d,v=0,y=0,M=0,b=0,E=0;E<m;E++)for(var I=0;I<f;I++){var k=c[x+I+E*d];v+=k<<24>>>24,y+=k<<16>>>24,M+=k<<8>>>24,b+=k>>>24}v=Math.round(v/_),y=Math.round(y/_),M=Math.round(M/_),b=Math.round(b/_),l[w+p*s]=b<<24|M<<16|y<<8|v}return h}function downScaleCanvas(e,t){var r,a;const o=document.createElement("canvas");o.width=e.width,o.height=e.height,null===(r=o.getContext("2d"))||void 0===r||r.drawImage(e,0,0,o.width,o.height);const i=downsample(o.getContext("2d").getImageData(0,0,o.width,o.height),Math.floor(o.width*t),Math.floor(o.height*t),0,0,o.width,o.height);return o.width=Math.floor(o.width*t),o.height=Math.floor(o.height*t),null===(a=o.getContext("2d"))||void 0===a||a.putImageData(i,0,0),o}const useMask=(e,t)=>{const[r,a]=useImageHook(e.maskSrc,"anonymous"),o=e.maskSrc?a:"loaded";return(0,exports.useImageLoader)(o,e.id),react_1.default.useMemo((()=>{if(!r)return t;if(!t||!t.width||!t.height)return t;const a=document.createElement("canvas");a.width=Math.max(t.width,1),a.height=Math.max(t.height,1);const o=a.getContext("2d");if(!o)return t;o.drawImage(t,0,0),o.globalCompositeOperation="source-in";const i=getCrop(r,e);return o.drawImage(r,i.x,i.y,i.width,i.height,0,0,t.width,t.height),a}),[t,r,e.width,e.height])},useCornerRadius=(e,t,r,a,o=0,i=!1)=>{const n=react_1.default.useMemo((()=>{if(t&&t.width&&t.height)return document.createElement("canvas")}),[t]);return react_1.default.useLayoutEffect((()=>{if(!n||!t)return;const h=Math.floor(Math.max(e.width*a,1)),c=Math.floor(Math.max(e.height*a,1));n.width=h,n.height=c;const d=n.getContext("2d");if(!d)return;const l=Math.min(o*a,h/2,c/2);l&&(d.beginPath(),d.moveTo(l,0),d.lineTo(h-l,0),d.arc(h-l,l,l,3*Math.PI/2,0,!1),d.lineTo(h,c-l),d.arc(h-l,c-l,l,0,Math.PI/2,!1),d.lineTo(l,c),d.arc(l,c-l,l,Math.PI/2,Math.PI,!1),d.lineTo(0,l),d.arc(l,l,l,Math.PI,3*Math.PI/2,!1),d.clip());const s=Math.max(e.width/r.width,e.height/r.height)*a,u=e.page._exporting&&flags_1.flags.imageDownScalingEnabled&&s<1&&!i,g=u?downScaleCanvas(t,s):t,f=u?{x:Math.floor(r.x*s),y:Math.floor(r.y*s),width:Math.floor(r.width*s),height:Math.floor(r.height*s)}:r;d.drawImage(g,f.x,f.y,f.width,f.height,0,0,n.width,n.height)}),[n,e.width,e.height,r.x,r.y,r.width,r.height,o,a,i,e.page._exporting]),n},useClip=(e,t,r,a)=>{const o=useSizeFixer(e.clipSrc||""),[i]=useImageHook(o,"anonymous"),n=react_1.default.useMemo((()=>{if(t&&i)return document.createElement("canvas")}),[t,i]);react_1.default.useLayoutEffect((()=>{var a;if(!i)return;if(!t||!t.width||!t.height)return;if(!i||!i.width||!i.height)return;if(!n)return;const o=document.createElement("canvas"),h=Math.max(e.width/i.width*r,e.height/i.height*r);o.width=i.width*h,o.height=i.height*h,null===(a=o.getContext("2d"))||void 0===a||a.drawImage(i,0,0,o.width,o.height),n.width=Math.max(t.width,1),n.height=Math.max(t.height,1);const c=n.getContext("2d");c&&(c.save(),c.drawImage(o,0,0,t.width,t.height),c.globalCompositeOperation="source-in",c.drawImage(t,0,0,n.width,n.height),c.restore())}),[n,t,i,e.width,e.height,r,...a]);return e.clipSrc&&i?n:t},PLACEHOLDER_CANVAS=document.createElement("canvas"),LoadingPlaceholder=(0,mobx_react_lite_1.observer)((({element:e})=>{const t=Math.min(30,e.width/4,e.height/4),r=react_1.default.useRef(null);return react_1.default.useEffect((()=>{const e=r.current;if(!e)return;const t=new konva_1.default.Animation((t=>{e.rotate(((null==t?void 0:t.timeDiff)||0)/2)}),e.getLayer());return t.start(),()=>{t.stop()}})),react_1.default.createElement(react_konva_1.Group,{x:e.x,y:e.y,rotation:e.rotation,listening:!1,opacity:e.opacity,hideInExport:!e.showInExport},react_1.default.createElement(react_konva_1.Rect,{width:e.width,height:e.height,fill:"rgba(124, 173, 212, 0.8)"}),react_1.default.createElement(react_konva_1.Arc,{ref:r,x:e.width/2,y:e.height/2,fill:"white",outerRadius:Math.abs(t),innerRadius:Math.max(1,t-5),angle:270}))})),ErrorPlaceholder=(0,mobx_react_lite_1.observer)((({element:e})=>{const t="Can not load the image...",r=Math.max(10,Math.min(30,e.width/t.length));return react_1.default.createElement(react_konva_1.Group,{x:e.x,y:e.y,rotation:e.rotation,listening:!1,opacity:e.opacity,hideInExport:!e.showInExport},react_1.default.createElement(react_konva_1.Rect,{width:e.width,height:e.height,fill:"rgba(223, 102, 102, 0.8)"}),react_1.default.createElement(react_konva_1.Text,{text:t,fontSize:r,width:e.width,height:e.height,align:"center",fill:"white",verticalAlign:"middle",padding:5}))}));let useImageHook=use_image_1.default;const setImageLoaderHook=e=>{useImageHook=e};exports.setImageLoaderHook=setImageLoaderHook;const useImageLoader=(e,t)=>{const r=react_1.default.useRef(),a=()=>{var e;null===(e=r.current)||void 0===e||e.call(r),r.current=void 0};react_1.default.useEffect((()=>a),[]),react_1.default.useLayoutEffect((()=>{"loading"!==e||r.current||(r.current=(0,loader_1.incrementLoader)(`image ${t}`)),"loading"!==e&&a(),"failed"===e&&(0,loader_1.triggerLoadError)(`image ${t}`)}),[e])};exports.useImageLoader=useImageLoader;const usePreviousImage=({image:e,status:t,type:r})=>{const a=react_1.default.useRef();react_1.default.useEffect((()=>{a.current=e||a.current}),[e]);return"failed"!==t||"failed"!==t&&"svg"===r?a.current:void 0},useImageSource=e=>{const[t,r]=react_1.default.useReducer((e=>e+1),0),a=react_1.default.useRef("loading"),o=react_1.default.useRef(e.src),i=react_1.default.useRef(e.src);return i.current!==e.src&&(i.current=e.src,a.current="loading"),react_1.default.useEffect((()=>{if("image"===e.type)return;if(!e.src)return;let t=!1;return(async()=>{a.current="loading",r();const i=await svg.urlToString(e.src),n=svg.fixSize(i),h=svg.replaceColors(n,e.colorsReplace);t||(o.current=h,a.current="loaded",r())})(),()=>{t=!0}}),[e.src,JSON.stringify(e.colorsReplace)]),"image"===e.type?[e.src,"loaded"]:[o.current,a.current]};exports.ImageElement=(0,mobx_react_lite_1.observer)((({element:e,store:t})=>{var r;const[a,o]=react_1.default.useState(!1),i=react_1.default.useRef(null),n=react_1.default.useRef(null),[h,c]=react_1.default.useState(!1),d=t.selectedElements.indexOf(e)>=0,[l,s]=useImageSource(e),[u,g]=useImageHook(l,"anonymous"),f="svg"!==e.type||"loaded"===s?g:"loading";(0,exports.useImageLoader)(f,e.id);const m=usePreviousImage({image:u,status:g,type:e.type}),_=useMask(e,useFlip(e,u||m,t._elementsPixelRatio))||PLACEHOLDER_CANVAS;let{cropX:p,cropY:w,cropWidth:x,cropHeight:v}=e;"loaded"!==g&&(p=w=0,x=v=1);const y=_.width*x,M=_.height*v,b=e.width/e.height;let E,I;const k=y/M,S="svg"===e.type;S?(E=y,I=M):b>=k?(E=y,I=y/b):(E=M*b,I=M);const C={x:_.width*p,y:_.height*w,width:E,height:I},R=null!==(r=e.cornerRadius)&&void 0!==r?r:0;let O=useClip(e,useCornerRadius(e,_,C,t._elementsPixelRatio,R,a||e._cropModeEnabled||"svg"===e.type),t._elementsPixelRatio,[C,R,t._elementsPixelRatio]);const L=Math.max(e.width/E,e.height/I);react_1.default.useEffect((()=>{var t;if(!e._cropModeEnabled)return;const r=null===(t=i.current)||void 0===t?void 0:t.getStage();function a(t){e._cropModeEnabled&&t.target!==n.current&&e.toggleCropMode(!1)}function o(t){e._cropModeEnabled&&"CANVAS"!==t.target.nodeName&&e.toggleCropMode(!1)}return document.body.addEventListener("click",o),null==r||r.on("click",a),null==r||r.on("tap",a),()=>{document.body.removeEventListener("click",o),document.body.removeEventListener("touchstart",o),null==r||r.off("click",a),null==r||r.off("click",a)}}),[e._cropModeEnabled]),react_1.default.useLayoutEffect((()=>{if(a||e._cropModeEnabled)return;(0,apply_filters_1.applyFilter)(i.current,e);return(0,mobx_1.autorun)((()=>{(0,apply_filters_1.applyFilter)(i.current,e)}),{delay:100})}),[_,a,x,v,e._cropModeEnabled]),react_1.default.useLayoutEffect((()=>{var t;a||e._cropModeEnabled?null===(t=i.current)||void 0===t||t.clearCache():(0,apply_filters_1.applyFilter)(i.current,e)}),[a,e.width,e.height,e._cropModeEnabled]),react_1.default.useEffect((()=>{(0,apply_filters_1.applyFilter)(i.current,e)}),[e.shadowEnabled,e.shadowBlur,e.cornerRadius,e.shadowColor,e.shadowOffsetX,e.shadowOffsetY,e.shadowOpacity]);const P=react_1.default.useRef(null),X=react_1.default.useRef(null),Y=react_1.default.useRef(null);react_1.default.useLayoutEffect((()=>{e._cropModeEnabled&&(X.current.nodes([P.current]),Y.current.nodes([n.current]))}),[e._cropModeEnabled]);const H=t=>{Math.round(t.target.x())>0&&(t.target.x(0),t.target.scaleX(1)),Math.round(t.target.y())>0&&(t.target.y(0),t.target.scaleY(1));const r=t.target.width()*t.target.scaleX(),a=t.target.height()*t.target.scaleY(),o=Math.min(1,E/r),i=Math.min(1,I/a),n=1-o,h=Math.min(n,Math.max(0,Math.round(-t.target.x())/r)),c=1-i,d=Math.min(c,Math.max(0,Math.round(-t.target.y())/a));t.target.setAttrs({x:-h*_.width,y:-d*_.height,scaleX:1,scaleY:1}),e.set({cropX:h,cropY:d,cropWidth:o,cropHeight:i})},T=()=>{"svg"!==e.type&&e.contentEditable&&setTimeout((()=>{e.toggleCropMode(!0)}))},D="svg"===e.type&&m,A="loading"===g&&!D,W="failed"===g,F=!A&&!W,q=react_1.default.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),z=F?e.opacity:0;(0,use_fadein_1.useFadeIn)(i,z);const B=e.selectable||"admin"===t.role;return react_1.default.createElement(react_1.default.Fragment,null,A&&react_1.default.createElement(LoadingPlaceholder,{element:e}),W&&react_1.default.createElement(ErrorPlaceholder,{element:e}),react_1.default.createElement(react_konva_1.Image,{ref:i,name:"element",id:e.id,image:O,x:e.x,y:e.y,width:e.width||1,height:e.height||1,rotation:e.rotation,opacity:z,shadowEnabled:e.shadowEnabled,shadowBlur:e.shadowBlur,shadowOffsetX:e.shadowOffsetX,shadowOffsetY:e.shadowOffsetY,shadowColor:e.shadowColor,shadowOpacity:e.shadowOpacity,customCrop:C,listening:B,draggable:e.draggable,hideInExport:!e.showInExport,onMouseEnter:()=>{c(!0)},onMouseLeave:()=>{c(!1)},onDragStart:()=>{t.history.startTransaction()},onDragMove:t=>{e.set({x:t.target.x(),y:t.target.y()})},onDragEnd:r=>{e.set({x:r.target.x(),y:r.target.y()}),t.history.endTransaction()},onDblClick:T,onDblTap:T,onTransformStart:()=>{o(!0),t.history.startTransaction(),q.current={cropX:e.cropX,cropY:e.cropY,cropWidth:e.cropWidth,cropHeight:e.cropHeight}},onTransform:t=>{var r;const a=t.currentTarget,o=Math.abs(a.scaleX()-1)<1e-7?1:a.scaleX(),i=Math.abs(a.scaleY()-1)<1e-7?1:a.scaleY();a.scaleX(1),a.scaleY(1);const n=null===(r=t.target.getStage())||void 0===r?void 0:r.findOne("Transformer"),h=1-E/_.width,c=Math.min(h,Math.max(0,e.cropX)),d=1-I/_.height,l=Math.min(d,Math.max(0,e.cropY)),s=n.getActiveAnchor(),u=!(s.indexOf("middle")>=0||s.indexOf("center")>=0),g=!u&&o<1&&q.current.cropHeight>I/_.height;let f=u?e.cropWidth:e.cropWidth*o;g&&(f=e.cropWidth);const m=!u&&i<1&&q.current.cropWidth>E/_.width;let p=u?e.cropHeight:e.cropHeight*i;m&&(p=e.cropHeight),S&&(f=e.cropWidth,p=e.cropHeight),e.set({cropX:c,cropY:l,x:a.x(),y:a.y(),width:a.width()*o,height:a.height()*i,rotation:t.target.rotation(),cropWidth:Math.min(f,1-c),cropHeight:Math.min(p,1-l)})},onTransformEnd:r=>{const a=r.currentTarget;e.set({width:a.width(),height:a.height(),x:a.x(),y:a.y(),rotation:r.target.rotation(),cropWidth:E/_.width,cropHeight:I/_.height}),o(!1),t.history.endTransaction()}}),react_1.default.createElement(react_konva_1.Rect,{x:e.x,y:e.y,width:Math.max(e.width-e.borderSize,0),height:Math.max(e.height-e.borderSize,0),offsetX:-e.borderSize/2,offsetY:-e.borderSize/2,stroke:e.borderColor,strokeWidth:e.borderSize,listening:!1,visible:!!e.borderSize,rotation:e.rotation,cornerRadius:Math.max(0,R-e.borderSize),hideInExport:!e.showInExport}),e._cropModeEnabled&&react_1.default.createElement(react_konva_utils_1.Portal,{selector:".page-abs-container",enabled:!0},react_1.default.createElement(react_konva_1.Rect,{x:-1500,y:-1500,width:4500,height:4500,fill:"rgba(0,0,0,0.3)"}),react_1.default.createElement(react_konva_1.Image,{listening:!1,image:O,x:e.x,y:e.y,width:e.width,height:e.height,rotation:e.rotation,shadowEnabled:e.shadowEnabled,shadowBlur:e.shadowBlur}),react_1.default.createElement(react_konva_1.Group,{x:e.x,y:e.y,rotation:e.rotation,scaleX:L,scaleY:L},react_1.default.createElement(react_konva_1.Image,{image:_,ref:n,opacity:.4,draggable:!0,x:-e.cropX*_.width,y:-e.cropY*_.height,onDragMove:H,onTransform:H}),react_1.default.createElement(react_konva_1.Transformer,{ref:Y,anchorSize:20,enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],boundBoxFunc:(e,t)=>t.width<5||t.height<5?e:t,rotateEnabled:!1,borderEnabled:!1,anchorCornerRadius:10,anchorStrokeWidth:2,borderStrokeWidth:2}),react_1.default.createElement(react_konva_1.Rect,{width:E,height:I,ref:P,listening:!1,onTransform:t=>{t.target.x()<-e.cropX*_.width-1e-9&&(t.target.x(-e.cropX*_.width),t.target.scaleX(1)),t.target.y()<-e.cropY*_.height-1e-9&&(t.target.y(-e.cropY*_.height),t.target.scaleY(1));const r=Math.min(1,Math.max(0,e.cropX+t.target.x()/_.width)),a=Math.min(1,Math.max(0,t.target.y()/_.height+e.cropY)),o=t.target.width()*t.target.scaleX(),i=t.target.height()*t.target.scaleY(),n=Math.min(1-r,o/_.width),h=Math.min(1-a,i/_.height),c=t.target.getAbsolutePosition(t.target.getParent().getParent());t.target.scale({x:1,y:1}),t.target.position({x:0,y:0}),e.set({x:c.x,y:c.y,cropX:r,cropY:a,cropWidth:n,cropHeight:h,width:Math.min(o*L,_.width*(1-r)*L),height:Math.min(i*L,_.height*(1-a)*L)})}}),react_1.default.createElement(react_konva_1.Transformer,{ref:X,enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],boundBoxFunc:(e,t)=>t.width<5||t.height<5?e:t,keepRatio:!1,rotateEnabled:!1,anchorFill:"rgb(240, 240, 240)",anchorStrokeWidth:2,borderStrokeWidth:2}))),(h||d)&&react_1.default.createElement(highlighter_1.Highlighter,{element:e}))}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.useSnap=void 0;const react_1=__importDefault(require("react")),konva_1=__importDefault(require("konva")),math_1=require("../utils/math");var GUIDELINE_OFFSET=5;function getObjectSnappingEdges(e){const t=e.__getNodeRect(),n=(0,math_1.getClientRect)(Object.assign(Object.assign({},t),{rotation:konva_1.default.Util.radToDeg(t.rotation)})),a=e.getAbsolutePosition();return{vertical:[{guide:n.x,offset:a.x-n.x,snap:"start"},{guide:n.x+n.width/2,offset:a.x-n.x-n.width/2,snap:"center"},{guide:n.x+n.width,offset:a.x-n.x-n.width,snap:"end"}],horizontal:[{guide:n.y,offset:a.y-n.y,snap:"start"},{guide:n.y+n.height/2,offset:a.y-n.y-n.height/2,snap:"center"},{guide:n.y+n.height,offset:a.y-n.y-n.height,snap:"end"}]}}function getGuides(e,t){var n=[],a=[];e.vertical.forEach((e=>{t.vertical.forEach((t=>{var a=Math.abs(e-t.guide);a<GUIDELINE_OFFSET&&n.push({lineGuide:e,diff:a,snap:t.snap,offset:t.offset})}))})),e.horizontal.forEach((e=>{t.horizontal.forEach((t=>{var n=Math.abs(e-t.guide);n<GUIDELINE_OFFSET&&a.push({lineGuide:e,diff:n,snap:t.snap,offset:t.offset})}))}));var i=[];const r=n.sort(((e,t)=>e.diff-t.diff)),s=a.sort(((e,t)=>e.diff-t.diff));var o=r[0],d=s[0];if(o){r.filter((e=>Math.abs(e.diff-o.diff)<.1)).forEach((e=>{i.push(Object.assign({orientation:"V"},e))}))}if(d){s.filter((e=>Math.abs(e.diff-d.diff)<.1)).forEach((e=>{i.push(Object.assign({orientation:"H"},e))}))}return i}function useSnap(e){const t=e=>e.hasName("element")||e.hasName("page-background")||e.hasName("elements-area");function n(n){var a;const i=null===(a=e.current)||void 0===a?void 0:a.getStage();var r=[],s=[];return i.find(t).forEach((e=>{if(!(n.indexOf(e)>=0)){var t=e.getClientRect({skipShadow:!0,skipStroke:!0});r.push(t.x,t.x+t.width,t.x+t.width/2),s.push(t.y,t.y+t.height,t.y+t.height/2)}})),{vertical:r,horizontal:s}}function a(t){var n;const a=null===(n=e.current)||void 0===n?void 0:n.getLayer(),i=null==a?void 0:a.children.find((e=>"line-guides"===e.name()));t.forEach((e=>{if("H"===e.orientation){var t=new konva_1.default.Line({points:[-6e3,e.lineGuide,6e3,e.lineGuide],stroke:"rgb(0, 161, 255)",strokeWidth:1,name:"guid-line",dash:[4,6]});null==i||i.add(t),a.batchDraw()}else if("V"===e.orientation){t=new konva_1.default.Line({points:[e.lineGuide,-6e3,e.lineGuide,6e3],stroke:"rgb(0, 161, 255)",strokeWidth:1,name:"guid-line",dash:[4,6]});null==i||i.add(t)}}))}const i=e=>{e.target.getLayer().children.find((e=>"line-guides"===e.name())).destroyChildren();var t=getGuides(n(e.target.nodes()),getObjectSnappingEdges(e.target));if(!t.length)return;a(t);const i=e.target.getAbsolutePosition(),r=Object.assign({},i);t.forEach((e=>{switch(e.snap){case"start":case"center":case"end":switch(e.orientation){case"V":r.x=e.lineGuide+e.offset;break;case"H":r.y=e.lineGuide+e.offset}}}));const s=r.x-i.x,o=r.y-i.y;e.evt.ctrlKey||e.evt.metaKey||e.target.nodes().forEach((e=>{const t=e.getAbsolutePosition();e.setAbsolutePosition({x:t.x+s,y:t.y+o})}))},r=(t,i,r)=>{const s=e.current,o=s.getLayer();if(o.children.find((e=>"line-guides"===e.name())).destroyChildren(),"rotater"===s.getActiveAnchor())return i;if(Math.sqrt(Math.pow(i.x-t.x,2)+Math.pow(i.y-t.y,2))>50)return i;if(!o)return i;var d=getGuides(n(s.nodes()),{vertical:[{guide:i.x,offset:0,snap:"start"}],horizontal:[{guide:i.y,offset:0,snap:"start"}]});if(!d.length)return i;a(d);if(r.ctrlKey||r.metaKey)return i;let f=i.x,u=i.x;d.forEach((e=>{switch(e.orientation){case"V":f=e.lineGuide;break;case"H":u=e.lineGuide}}));const c=Math.abs(i.x-f)<10,l=Math.abs(i.y-u)<10;return c&&!l?{x:f,y:t.y}:l&&!c?{x:t.x,y:u}:c&&l?{x:f,y:u}:i},s=e=>{const t=e.target.getLayer();t.children.find((e=>"line-guides"===e.name())).destroyChildren(),t.batchDraw()};react_1.default.useEffect((()=>{e.current&&(e.current.anchorDragBoundFunc(r),e.current.on("dragstart",(()=>{setTimeout((()=>{const e=[...konva_1.default.DD._dragElements.entries()];e.reverse(),konva_1.default.DD._dragElements.clear(),e.forEach((([e,t])=>{konva_1.default.DD._dragElements.set(e,t)}))}))})),e.current.on("dragmove",i),e.current.on("dragend",s),e.current.on("transformend",s))}),[])}exports.useSnap=useSnap;
|
|
1
|
+
"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.useSnap=void 0;const react_1=__importDefault(require("react")),konva_1=__importDefault(require("konva")),math_1=require("../utils/math");var GUIDELINE_OFFSET=5;function getObjectSnappingEdges(e){const t=e.__getNodeRect(),n=(0,math_1.getClientRect)(Object.assign(Object.assign({},t),{rotation:konva_1.default.Util.radToDeg(t.rotation)})),a=e.getAbsolutePosition();return{vertical:[{guide:n.x,offset:a.x-n.x,snap:"start"},{guide:n.x+n.width/2,offset:a.x-n.x-n.width/2,snap:"center"},{guide:n.x+n.width,offset:a.x-n.x-n.width,snap:"end"}],horizontal:[{guide:n.y,offset:a.y-n.y,snap:"start"},{guide:n.y+n.height/2,offset:a.y-n.y-n.height/2,snap:"center"},{guide:n.y+n.height,offset:a.y-n.y-n.height,snap:"end"}]}}function getGuides(e,t){var n=[],a=[];e.vertical.forEach((e=>{t.vertical.forEach((t=>{var a=Math.abs(e-t.guide);a<GUIDELINE_OFFSET&&n.push({lineGuide:e,diff:a,snap:t.snap,offset:t.offset})}))})),e.horizontal.forEach((e=>{t.horizontal.forEach((t=>{var n=Math.abs(e-t.guide);n<GUIDELINE_OFFSET&&a.push({lineGuide:e,diff:n,snap:t.snap,offset:t.offset})}))}));var i=[];const r=n.sort(((e,t)=>e.diff-t.diff)),s=a.sort(((e,t)=>e.diff-t.diff));var o=r[0],d=s[0];if(o){r.filter((e=>Math.abs(e.diff-o.diff)<.1)).forEach((e=>{i.push(Object.assign({orientation:"V"},e))}))}if(d){s.filter((e=>Math.abs(e.diff-d.diff)<.1)).forEach((e=>{i.push(Object.assign({orientation:"H"},e))}))}return i}function useSnap(e){const t=e=>e.hasName("element")||e.hasName("page-background")||e.hasName("elements-area");function n(n){var a;const i=null===(a=e.current)||void 0===a?void 0:a.getStage();var r=[],s=[];return i.find(t).forEach((e=>{if(!(n.indexOf(e)>=0)){var t=e.getClientRect({skipShadow:!0,skipStroke:!0});r.push(t.x,t.x+t.width,t.x+t.width/2),s.push(t.y,t.y+t.height,t.y+t.height/2)}})),{vertical:r,horizontal:s}}function a(t){var n;const a=null===(n=e.current)||void 0===n?void 0:n.getLayer(),i=null==a?void 0:a.children.find((e=>"line-guides"===e.name()));t.forEach((e=>{if("H"===e.orientation){var t=new konva_1.default.Line({points:[-6e3,e.lineGuide,6e3,e.lineGuide],stroke:"rgb(0, 161, 255)",strokeWidth:1,name:"guid-line",dash:[4,6]});null==i||i.add(t),a.batchDraw()}else if("V"===e.orientation){t=new konva_1.default.Line({points:[e.lineGuide,-6e3,e.lineGuide,6e3],stroke:"rgb(0, 161, 255)",strokeWidth:1,name:"guid-line",dash:[4,6]});null==i||i.add(t)}}))}const i=e=>{e.target.getLayer().children.find((e=>"line-guides"===e.name())).destroyChildren();var t=getGuides(n(e.target.nodes()),getObjectSnappingEdges(e.target));if(!t.length)return;a(t);const i=e.target.getAbsolutePosition(),r=Object.assign({},i);t.forEach((e=>{switch(e.snap){case"start":case"center":case"end":switch(e.orientation){case"V":r.x=e.lineGuide+e.offset;break;case"H":r.y=e.lineGuide+e.offset}}}));const s=r.x-i.x,o=r.y-i.y;e.evt.ctrlKey||e.evt.metaKey||e.target.nodes().forEach((e=>{const t=e.getAbsolutePosition();e.setAbsolutePosition({x:t.x+s,y:t.y+o})}))},r=(t,i,r)=>{const s=e.current,o=s.getLayer();if(o.children.find((e=>"line-guides"===e.name())).destroyChildren(),"rotater"===s.getActiveAnchor())return i;if(Math.sqrt(Math.pow(i.x-t.x,2)+Math.pow(i.y-t.y,2))>50)return i;if(!o)return i;var d=getGuides(n(s.nodes()),{vertical:[{guide:i.x,offset:0,snap:"start"}],horizontal:[{guide:i.y,offset:0,snap:"start"}]});if(!d.length)return i;a(d);if(r.ctrlKey||r.metaKey)return i;let f=i.x,u=i.x;d.forEach((e=>{switch(e.orientation){case"V":f=e.lineGuide;break;case"H":u=e.lineGuide}}));const c=Math.abs(i.x-f)<10,l=Math.abs(i.y-u)<10;return c&&!l?{x:f,y:t.y}:l&&!c?{x:t.x,y:u}:c&&l?{x:f,y:u}:i},s=e=>{if(!e.target)return;const t=e.target.getLayer();t.children.find((e=>"line-guides"===e.name())).destroyChildren(),t.batchDraw()};react_1.default.useEffect((()=>{e.current&&(e.current.anchorDragBoundFunc(r),e.current.on("dragstart",(()=>{setTimeout((()=>{const e=[...konva_1.default.DD._dragElements.entries()];e.reverse(),konva_1.default.DD._dragElements.clear(),e.forEach((([e,t])=>{konva_1.default.DD._dragElements.set(e,t)}))}))})),e.current.on("dragmove",i),e.current.on("dragend",s),e.current.on("transformend",s))}),[])}exports.useSnap=useSnap;
|
package/model/store.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,o,n){void 0===n&&(n=o);var s=Object.getOwnPropertyDescriptor(t,o);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[o]}}),Object.defineProperty(e,n,s)}:function(e,t,o,n){void 0===n&&(n=o),e[n]=t[o]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var o in e)"default"!==o&&Object.prototype.hasOwnProperty.call(e,o)&&__createBinding(t,e,o);return __setModuleDefault(t,e),t},__rest=this&&this.__rest||function(e,t){var o={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(o[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(n=Object.getOwnPropertySymbols(e);s<n.length;s++)t.indexOf(n[s])<0&&Object.prototype.propertyIsEnumerable.call(e,n[s])&&(o[n[s]]=e[n[s]])}return o},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.createStore=exports.Store=exports.Font=exports.Page=exports.registerShapeModel=exports.SVGElement=exports.ImageElement=exports.TextElement=exports.Element=void 0;const mobx_state_tree_1=require("mobx-state-tree"),history_1=require("./history"),nanoid_1=require("nanoid"),konva_1=__importDefault(require("konva")),download_1=require("../utils/download"),pdf_1=require("../utils/pdf"),validate_key_1=require("../utils/validate-key"),fonts=__importStar(require("../utils/fonts")),loader_1=require("../utils/loader"),unit_1=require("../utils/unit"),deep_equal_1=require("../utils/deep-equal");(0,mobx_state_tree_1.setLivelinessChecking)("ignore"),exports.Element=mobx_state_tree_1.types.model("Element",{id:mobx_state_tree_1.types.identifier,type:"none",name:"",x:0,y:0,rotation:0,opacity:1,blurEnabled:!1,blurRadius:10,brightnessEnabled:!1,brightness:0,sepiaEnabled:!1,grayscaleEnabled:!1,shadowEnabled:!1,shadowBlur:5,shadowOffsetX:0,shadowOffsetY:0,shadowColor:"black",shadowOpacity:1,custom:mobx_state_tree_1.types.frozen(),visible:!0,draggable:!0,selectable:!0,contentEditable:!0,styleEditable:!0,alwaysOnTop:!1,showInExport:!0}).preProcessSnapshot((e=>{const t=Object.assign(Object.assign({},e),{x:e.x||0,y:e.y||0});return"width"in e&&(t.width=t.width||1),"height"in e&&(t.height=t.height||1),e.locked&&(t.draggable=!1,t.contentEditable=!1,t.styleEditable=!1),t})).postProcessSnapshot((e=>{const t=Object.assign({},e),o={};for(var n in t)"_"!==n[0]&&(o[n]=e[n]);return o})).views((e=>({get locked(){return!e.draggable&&!e.contentEditable&&!e.styleEditable},get page(){return(0,mobx_state_tree_1.getParentOfType)(e,exports.Page)},get store(){return(0,mobx_state_tree_1.getParentOfType)(e,exports.Store)}}))).actions((e=>({toJSON:()=>Object.assign({},(0,mobx_state_tree_1.getSnapshot)(e))}))).actions((e=>({clone(t){const o=e.toJSON();return t.id=t.id||(0,nanoid_1.nanoid)(10),Object.assign(o,t),e.page.addElement(o)},set(t){Object.assign(e,t)},moveUp(){e.page.moveElementsUp([e.id])},moveTop(){e.page.moveElementsTop([e.id])},moveDown(){e.page.moveElementsDown([e.id])},moveBottom(){e.page.moveElementsBottom([e.id])},beforeDestroy(){e.store.history.endTransaction()}}))),exports.TextElement=exports.Element.named("Text").props({type:"text",text:"",placeholder:"",fontSize:14,fontFamily:"Roboto",fontStyle:"normal",fontWeight:"normal",textDecoration:"",fill:"black",align:"center",width:100,height:0,verticalAlign:"top",strokeWidth:0,stroke:"black",lineHeight:mobx_state_tree_1.types.optional(mobx_state_tree_1.types.union(mobx_state_tree_1.types.number,mobx_state_tree_1.types.string),1.2),letterSpacing:0,_editModeEnabled:!1}).preProcessSnapshot((e=>Object.assign({},e))).actions((e=>({toggleEditMode(t){e._editModeEnabled=null!=t?t:!e._editModeEnabled,e._editModeEnabled?e.store.history.startTransaction():e.store.history.endTransaction()}}))),exports.ImageElement=exports.Element.named("Image").props({type:"image",width:100,height:100,src:"",cropX:0,cropY:0,cropWidth:1,cropHeight:1,cornerRadius:0,flipX:!1,flipY:!1,clipSrc:"",borderColor:"black",borderSize:0,_cropModeEnabled:!1}).actions((e=>({toggleCropMode(t){e._cropModeEnabled=null!=t?t:!e._cropModeEnabled,e._cropModeEnabled?e.store.history.startTransaction():e.store.history.endTransaction()}}))),exports.SVGElement=exports.Element.named("SVG").props({type:"svg",src:"",maskSrc:"",__svgString:"",cropX:0,cropY:0,cropWidth:1,cropHeight:1,keepRatio:!0,flipX:!1,flipY:!1,width:100,height:100,borderColor:"black",borderSize:0,cornerRadius:0,colorsReplace:mobx_state_tree_1.types.map(mobx_state_tree_1.types.string)}).preProcessSnapshot((e=>Object.assign(Object.assign({},e),{src:e.src||e.svgSource}))).actions((e=>({replaceColor(t,o){e.colorsReplace.set(t,o)}})));const TYPES_MAP={svg:exports.SVGElement,text:exports.TextElement,image:exports.ImageElement},ADDITIONAL_TYPES=[];function registerShapeModel(e){const t=e.type;if(!t)throw new Error('You must pass "type" attribute to custom model.');const o=exports.Element.named(t).props(e);TYPES_MAP[t]=o,ADDITIONAL_TYPES.push(o)}exports.registerShapeModel=registerShapeModel;const additionalTypesUnion=[...new Array(20)].map(((e,t)=>mobx_state_tree_1.types.late((()=>ADDITIONAL_TYPES[t])))),ElementTypes=mobx_state_tree_1.types.union({dispatcher:e=>{const t=TYPES_MAP[e.type];if(!t)throw new Error(`Unknown element type: "${e.type}"`);return t}},exports.SVGElement,exports.TextElement,exports.ImageElement,...additionalTypesUnion);function createStore({key:e,showCredit:t}={key:"",showCredit:!1}){const o=exports.Store.create();return(0,validate_key_1.validateKey)(e,t),o}exports.Page=mobx_state_tree_1.types.model("Page",{id:mobx_state_tree_1.types.identifier,children:mobx_state_tree_1.types.array(ElementTypes),width:mobx_state_tree_1.types.optional(mobx_state_tree_1.types.union(mobx_state_tree_1.types.number,mobx_state_tree_1.types.literal("auto")),"auto"),height:mobx_state_tree_1.types.optional(mobx_state_tree_1.types.union(mobx_state_tree_1.types.number,mobx_state_tree_1.types.literal("auto")),"auto"),background:"white",bleed:0,custom:mobx_state_tree_1.types.frozen(),_exporting:!1}).postProcessSnapshot((e=>{const t=Object.assign({},e),o={};for(var n in t)"_"!==n[0]&&(o[n]=e[n]);return o})).views((e=>({get store(){return(0,mobx_state_tree_1.getParentOfType)(e,exports.Store)}}))).views((e=>({get computedWidth(){return"auto"===e.width?e.store.width:e.width},get computedHeight(){return"auto"===e.height?e.store.height:e.height}}))).actions((e=>({toJSON:()=>JSON.parse(JSON.stringify((0,mobx_state_tree_1.getSnapshot)(e))),_forEachElementUp(t,o){const n=t.map((t=>({id:t,index:e.children.findIndex((e=>e.id===t))})));n.sort(((e,t)=>t.index-e.index));for(const{index:s}of n){if(-1==s)continue;const n=s<e.children.length-1&&e.children[s+1],i=t.indexOf(null==n?void 0:n.id)>=0;s===e.children.length-1||i||o(s)}},_forEachElementDown(t,o){const n=t.map((t=>({id:t,index:e.children.findIndex((e=>e.id===t))})));n.sort(((e,t)=>e.index-t.index));for(const{index:s}of n){if(-1==s)continue;const n=s>0&&e.children[s-1],i=t.indexOf(null==n?void 0:n.id)>=0;0===s||i||o(s)}return!1}}))).actions((e=>({clone(t){const o=e.toJSON();o.children.forEach((e=>{e.id=(0,nanoid_1.nanoid)(10)}));const n=Object.assign(Object.assign(Object.assign({},o),{id:(0,nanoid_1.nanoid)(10)}),t),s=e.store.addPage(n),i=e.store.pages.indexOf(e);s.setZIndex(i+1),s.select()},setZIndex(t){e.store.setPageZIndex(e.id,t)},set(t){Object.assign(e,t)},select(){e.store.selectPage(e.id)},addElement(t){const o=TYPES_MAP[t.type];if(!o)return void console.error("Can not find model with type "+t.type);const n=o.create(Object.assign({id:(0,nanoid_1.nanoid)(10)},t));return e.children.push(n),n.selectable&&e.store.selectElements([n.id]),n},canMoveElementsUp(t){let o=!1;return e._forEachElementUp(t,(()=>{o=o||!0})),o},moveElementsUp(t){e._forEachElementUp(t,(t=>{const o=e.children[t];(0,mobx_state_tree_1.detach)(o),e.children.splice(t+1,0,o)}))},canMoveElementsTop(e){return this.canMoveElementsUp(e)},moveElementsTop(t){const o=[],n=[];e.children.forEach((e=>{t.indexOf(e.id)>=0?o.push(e):n.push(e)})),e.children.replace(n.concat(o))},canMoveElementsDown(t){let o=!1;return e._forEachElementDown(t,(()=>{o=o||!0})),o},moveElementsDown(t){e._forEachElementDown(t,(t=>{const o=e.children[t];(0,mobx_state_tree_1.detach)(o),e.children.splice(t-1,0,o)}))},canMoveElementsBottom(e){return this.canMoveElementsDown(e)},moveElementsBottom(t){const o=[],n=[];e.children.forEach((e=>{t.indexOf(e.id)>=0?o.push(e):n.push(e)})),e.children.replace(o.concat(n))},setElementZIndex(t,o){const n=e.children.find((e=>e.id===t));n&&((0,mobx_state_tree_1.detach)(n),e.children.remove(n),e.children.splice(o,0,n))},setSize({width:t,height:o},{useMagic:n}){e.width=t,e.height=o}}))).actions((e=>({moveElementUp(t){console.warn("page.moveElementUp(id) is deprecated. Please use page.moveElementsUp([id1, id2]) instead."),e.moveElementsUp([t])},moveElementDown(t){console.warn("page.moveElementDown(id) is deprecated. Please use page.moveElementsDown([id1, id2]) instead."),e.moveElementsDown([t])},moveElementTop(t){console.warn("page.moveElementTop(id) is deprecated. Please use page.moveElementsTop([id1, id2]) instead."),e.moveElementsTop([t])},moveElementBottom(t){console.warn("page.moveElementBottom(id) is deprecated. Please use page.moveElementsBottom([id1, id2]) instead."),e.moveElementsBottom([t])}}))),exports.Font=mobx_state_tree_1.types.model("Font",{fontFamily:mobx_state_tree_1.types.string,url:mobx_state_tree_1.types.optional(mobx_state_tree_1.types.string,""),styles:mobx_state_tree_1.types.frozen()}).preProcessSnapshot((e=>Object.assign(Object.assign({},e),{fontFamily:e.fontFamily||e.name}))),exports.Store=mobx_state_tree_1.types.model("Store",{role:"",pages:mobx_state_tree_1.types.array(exports.Page),fonts:mobx_state_tree_1.types.array(exports.Font),width:1080,height:1080,scale:1,scaleToFit:1,unit:"px",dpi:72,bleedVisible:!1,rulesVisible:!1,openedSidePanel:"",selectedElementsIds:mobx_state_tree_1.types.array(mobx_state_tree_1.types.string),history:mobx_state_tree_1.types.optional(history_1.UndoManager,{targetPath:"../pages"}),_elementsPixelRatio:2,_activePageId:""}).views((e=>({get _bleedVisible(){return console.warn("store._bleedVisible is deprecated. Please use store.bleedVisible instead."),e.bleedVisible},get selectedElements(){return e.selectedElementsIds.map((t=>{for(const o of e.pages)for(const e of o.children)if(e.id===t)return e})).filter((e=>!!e))},get activePage(){return e.pages.slice().find((t=>t.id===e._activePageId))||(e.pages.length?e.pages[0]:null)}}))).actions((e=>({afterCreate(){e.history.canUndo},setUnit({unit:t,dpi:o}){e.unit=t||e.unit,e.dpi=o||e.dpi},setRole(t){e.role=t},getElementById(t){for(const o of e.pages)for(const e of o.children)if(e.id===t)return e},addPage(t){const o=exports.Page.create(Object.assign({id:(0,nanoid_1.nanoid)(10)},t));return e.pages.push(o),e._activePageId=o.id,o},selectPage(t){e._activePageId=t},selectElements(t){const o=t.map((t=>e.getElementById(t))).sort(((e,t)=>e.page.children.indexOf(e)-e.page.children.indexOf(t))).filter((e=>!!e)).map((e=>e.id));e.selectedElementsIds=(0,mobx_state_tree_1.cast)(o)},toggleBleed(t){e.bleedVisible=null!=t?t:!e.bleedVisible},toggleRulers(t){e.rulesVisible=null!=t?t:!e.rulesVisible},openSidePanel(t){e.openedSidePanel=t},setScale(t){e.scale=t},_setScaleToFit(t){e.scaleToFit=t},setElementsPixelRatio(t){e._elementsPixelRatio=t},setSize(t,o,n){if(n){const n=t/e.width,s=o/e.height;for(const t of e.pages)for(const e of t.children)e.set({x:e.x*n,y:e.y*s}),"text"===e.type?e.set({fontSize:e.fontSize*n,width:Math.max(e.width*n,2)}):"image"===e.type?e.set({width:e.width*n,height:e.height*s}):"svg"===e.type&&e.set({width:Math.max(e.width*n,2),height:Math.max(e.height*n,2)})}e.width=t,e.height=o},setPageZIndex(t,o){const n=e.pages.find((e=>e.id===t));n&&((0,mobx_state_tree_1.detach)(n),e.pages.remove(n),e.pages.splice(o,0,n))},deletePages(t){const o=e.pages.indexOf(e.activePage);t.forEach((t=>{const o=e.pages.find((e=>e.id===t));(0,mobx_state_tree_1.destroy)(o)}));const n=Math.min(e.pages.length-1,o),s=e.pages[n];s&&(e._activePageId=s.id),e.selectedElementsIds=(0,mobx_state_tree_1.cast)(e.selectedElementsIds.filter((t=>e.getElementById(t))))},deleteElements(t){t.forEach((t=>{e.pages.forEach((e=>{const o=e.children.find((e=>e.id===t));o&&(0,mobx_state_tree_1.destroy)(o)}))})),e.selectedElementsIds=(0,mobx_state_tree_1.cast)(e.selectedElementsIds.filter((t=>e.getElementById(t))))},on(t,o){if("change"===t){let t=e.toJSON();return(0,mobx_state_tree_1.onSnapshot)(e,(n=>{const s=e.toJSON();!(0,deep_equal_1.deepEqual)(t,s)&&(t=s,o(s))}))}},async toDataURL({pixelRatio:t,ignoreBackground:o,pageId:n,mimeType:s,includeBleed:i,quality:a}={}){var r;const l=t||1;n=n||(null===(r=e.pages[0])||void 0===r?void 0:r.id);const d=e.pages.find((e=>e.id===n));if(!d)throw new Error(`No page for export with id ${n}`);null==d||d.set({_exporting:!0}),await e.waitLoading();const c=konva_1.default.stages.find((e=>e.getAttr("pageId")===n));if(!c)throw new Error(`Export is failed. Can not find stage for page ${n}. Looks like <Workspace /> component is not mounted, but it is required in order to process the export.`);const p=c.findOne(".page-container");c.find("Transformer").forEach((e=>e.visible(!1))),p.find(".page-background").forEach((e=>e.shadowEnabled(!1))),p.find(".page-background").forEach((e=>e.strokeEnabled(!1))),p.find(".highlighter").forEach((e=>e.visible(!1)));const m=p.findOne(".page-background-group"),_=m.clip();m.clip({x:null,y:null,width:null,height:null});const g=p.findOne(".elements-container"),h=g.clip();g.clip({x:null,y:null,width:null,height:null});const u=p.find((e=>e.getAttr("hideInExport")));u.forEach((e=>{e.setAttr("oldVisible",e.visible()),e.hide()}));const f=p.find((e=>!e.visible()&&e.getAttr("editModeEnabled")));f.forEach((e=>{e.setAttr("oldVisible",e.visible()),e.show()})),o&&p.find(".page-background").forEach((e=>e.hide()));const b=i?d.bleed:0;let x=b;e.bleedVisible&&i?x=0:e.bleedVisible&&!i&&(x=-d.bleed);const y=document.createElement("canvas");y.width=(d.computedWidth+2*b)*l,y.height=(d.computedHeight+2*b)*l;const E=y.getContext("2d");"image/jpeg"===s&&(E.fillStyle="white",E.fillRect(0,0,y.width,y.height));const v=p.toCanvas({x:p.x()-x*p.scaleX(),y:p.y()-x*p.scaleY(),width:(d.computedWidth+2*b)*p.scaleX(),height:(d.computedHeight+2*b)*p.scaleY(),pixelRatio:1/p.scaleX()*l});E.drawImage(v,0,0);const w=y.toDataURL(s,a);return o&&p.find(".page-background").forEach((e=>e.show())),u.forEach((e=>{e.visible(e.getAttr("oldVisible"))})),f.forEach((e=>{e.visible(e.getAttr("oldVisible"))})),p.find(".page-background").forEach((e=>e.shadowEnabled(!0))),p.find(".page-background").forEach((e=>e.strokeEnabled(!0))),c.find("Transformer").forEach((e=>e.visible(!0))),p.find(".highlighter").forEach((e=>e.visible(!0))),m.clip(_),g.clip(h),null==d||d.set({_exporting:!1}),w},async saveAsImage(t={}){var{fileName:o}=t,n=__rest(t,["fileName"]);const s=n.mimeType||"image/png",i=s.split("/")[1];(0,download_1.downloadFile)(await e.toDataURL(n),o||"polotno."+i,s)},async _toPDF(t){const o=t.dpi||e.dpi,n=t.parallel||1,s=t.unit||("px"===e.unit?"mm":e.unit),i=t.pixelRatio||1,a=await(0,pdf_1.getJsPDF)(),r=e=>(0,unit_1.pxToUnit)({px:e,unit:s,dpi:o}),l=e.pages[0]||{},d=t.includeBleed?l.bleed:0,c=r(l.computedWidth+2*d),p=r(l.computedHeight+2*d);var m=new a({unit:s,orientation:c>p?"landscape":"portrait",format:[c,p],compress:!0});m.deletePage(1);const _=e._elementsPixelRatio;e.setElementsPixelRatio(i),await new Promise((e=>setTimeout(e)));const g=((e,t)=>{for(var o=[],n=0;n<e.length;n+=t)o.push(e.slice(n,n+t));return o})(e.pages,n);for(const o of g){const n=o.map((async o=>{const n=t.includeBleed?o.bleed:0,s=r(o.computedWidth+2*n),a=r(o.computedHeight+2*n);let l=0,d=i;for(;l<10;){l+=1,2===l&&console.error("Polotno can not export PDF with current settings. Quality is automatically reduced.");const n=await e.toDataURL(Object.assign(Object.assign({},t),{pageId:o.id,pixelRatio:d}));if(n.length>20)return{url:n,width:s,height:a};d*=.8}}));(await Promise.all(n)).forEach((({url:e,width:t,height:o})=>{m.addPage([t,o],t>o?"landscape":"portrait"),m.addImage(e,0,0,t,o,void 0,"FAST")}))}return e.setElementsPixelRatio(_),m},toPDFDataURL:async t=>(await e._toPDF(Object.assign({mimeType:"image/jpeg"},t))).output("datauristring"),async saveAsPDF(t={}){var{fileName:o}=t,n=__rest(t,["fileName"]);(await e._toPDF(Object.assign({mimeType:"image/jpeg"},n))).save(o||"polotno.pdf")},async waitLoading(){await new Promise((e=>setTimeout(e,100))),await(0,loader_1.whenLoaded)()},toJSON:()=>({width:e.width,height:e.height,fonts:(0,mobx_state_tree_1.getSnapshot)(e.fonts),pages:(0,mobx_state_tree_1.getSnapshot)(e.pages),unit:e.unit,dpi:e.dpi}),loadJSON(t,o=!1){var n;const s=Object.assign({},t),i=e.pages.indexOf(e.activePage);let a=null===(n=s.pages[i]||s.pages[0])||void 0===n?void 0:n.id;s._activePageId=a;const r=Object.assign({},(0,mobx_state_tree_1.getSnapshot)(e));Object.assign(r,s),r.history=o?e.history.toJSON():{history:[],undoIdx:-1},(0,mobx_state_tree_1.applySnapshot)(e,r)},addFont(t){e.removeFont(t.fontFamily),e.fonts.push(t),e.loadFont(t.fontFamily)},removeFont(t){e.fonts.filter((e=>e.fontFamily===t)).forEach((e=>(0,mobx_state_tree_1.destroy)(e)))},async loadFont(t){const o=e.fonts.find((e=>e.fontFamily===t))||fonts.globalFonts.find((e=>e.fontFamily===t));o?fonts.injectCustomFont(o):fonts.injectGoogleFont(t),await fonts.loadFont(t)}}))),exports.createStore=createStore,exports.default=createStore;
|
|
1
|
+
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,o,n){void 0===n&&(n=o);var s=Object.getOwnPropertyDescriptor(t,o);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[o]}}),Object.defineProperty(e,n,s)}:function(e,t,o,n){void 0===n&&(n=o),e[n]=t[o]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var o in e)"default"!==o&&Object.prototype.hasOwnProperty.call(e,o)&&__createBinding(t,e,o);return __setModuleDefault(t,e),t},__rest=this&&this.__rest||function(e,t){var o={};for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(o[n]=e[n]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var s=0;for(n=Object.getOwnPropertySymbols(e);s<n.length;s++)t.indexOf(n[s])<0&&Object.prototype.propertyIsEnumerable.call(e,n[s])&&(o[n[s]]=e[n[s]])}return o},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.createStore=exports.Store=exports.Font=exports.Page=exports.registerShapeModel=exports.SVGElement=exports.ImageElement=exports.TextElement=exports.Element=void 0;const mobx_state_tree_1=require("mobx-state-tree"),history_1=require("./history"),nanoid_1=require("nanoid"),konva_1=__importDefault(require("konva")),download_1=require("../utils/download"),pdf_1=require("../utils/pdf"),validate_key_1=require("../utils/validate-key"),fonts=__importStar(require("../utils/fonts")),loader_1=require("../utils/loader"),unit_1=require("../utils/unit"),deep_equal_1=require("../utils/deep-equal"),wait_1=require("../utils/wait");(0,mobx_state_tree_1.setLivelinessChecking)("ignore"),exports.Element=mobx_state_tree_1.types.model("Element",{id:mobx_state_tree_1.types.identifier,type:"none",name:"",x:0,y:0,rotation:0,opacity:1,blurEnabled:!1,blurRadius:10,brightnessEnabled:!1,brightness:0,sepiaEnabled:!1,grayscaleEnabled:!1,shadowEnabled:!1,shadowBlur:5,shadowOffsetX:0,shadowOffsetY:0,shadowColor:"black",shadowOpacity:1,custom:mobx_state_tree_1.types.frozen(),visible:!0,draggable:!0,selectable:!0,contentEditable:!0,styleEditable:!0,alwaysOnTop:!1,showInExport:!0}).preProcessSnapshot((e=>{const t=Object.assign(Object.assign({},e),{x:e.x||0,y:e.y||0});return"width"in e&&(t.width=t.width||1),"height"in e&&(t.height=t.height||1),e.locked&&(t.draggable=!1,t.contentEditable=!1,t.styleEditable=!1),t})).postProcessSnapshot((e=>{const t=Object.assign({},e),o={};for(var n in t)"_"!==n[0]&&(o[n]=e[n]);return o})).views((e=>({get locked(){return!e.draggable&&!e.contentEditable&&!e.styleEditable},get page(){return(0,mobx_state_tree_1.getParentOfType)(e,exports.Page)},get store(){return(0,mobx_state_tree_1.getParentOfType)(e,exports.Store)}}))).actions((e=>({toJSON:()=>Object.assign({},(0,mobx_state_tree_1.getSnapshot)(e))}))).actions((e=>({clone(t){const o=e.toJSON();return t.id=t.id||(0,nanoid_1.nanoid)(10),Object.assign(o,t),e.page.addElement(o)},set(t){Object.assign(e,t)},moveUp(){e.page.moveElementsUp([e.id])},moveTop(){e.page.moveElementsTop([e.id])},moveDown(){e.page.moveElementsDown([e.id])},moveBottom(){e.page.moveElementsBottom([e.id])},beforeDestroy(){e.store.history.endTransaction()}}))),exports.TextElement=exports.Element.named("Text").props({type:"text",text:"",placeholder:"",fontSize:14,fontFamily:"Roboto",fontStyle:"normal",fontWeight:"normal",textDecoration:"",fill:"black",align:"center",width:100,height:0,verticalAlign:"top",strokeWidth:0,stroke:"black",lineHeight:mobx_state_tree_1.types.optional(mobx_state_tree_1.types.union(mobx_state_tree_1.types.number,mobx_state_tree_1.types.string),1.2),letterSpacing:0,_editModeEnabled:!1}).preProcessSnapshot((e=>Object.assign({},e))).actions((e=>({toggleEditMode(t){e._editModeEnabled=null!=t?t:!e._editModeEnabled,e._editModeEnabled?e.store.history.startTransaction():e.store.history.endTransaction()}}))),exports.ImageElement=exports.Element.named("Image").props({type:"image",width:100,height:100,src:"",cropX:0,cropY:0,cropWidth:1,cropHeight:1,cornerRadius:0,flipX:!1,flipY:!1,clipSrc:"",borderColor:"black",borderSize:0,_cropModeEnabled:!1}).actions((e=>({toggleCropMode(t){e._cropModeEnabled=null!=t?t:!e._cropModeEnabled,e._cropModeEnabled?e.store.history.startTransaction():e.store.history.endTransaction()}}))),exports.SVGElement=exports.Element.named("SVG").props({type:"svg",src:"",maskSrc:"",__svgString:"",cropX:0,cropY:0,cropWidth:1,cropHeight:1,keepRatio:!0,flipX:!1,flipY:!1,width:100,height:100,borderColor:"black",borderSize:0,cornerRadius:0,colorsReplace:mobx_state_tree_1.types.map(mobx_state_tree_1.types.string)}).preProcessSnapshot((e=>Object.assign(Object.assign({},e),{src:e.src||e.svgSource}))).actions((e=>({replaceColor(t,o){e.colorsReplace.set(t,o)}})));const TYPES_MAP={svg:exports.SVGElement,text:exports.TextElement,image:exports.ImageElement},ADDITIONAL_TYPES=[];function registerShapeModel(e){const t=e.type;if(!t)throw new Error('You must pass "type" attribute to custom model.');const o=exports.Element.named(t).props(e);TYPES_MAP[t]=o,ADDITIONAL_TYPES.push(o)}exports.registerShapeModel=registerShapeModel;const additionalTypesUnion=[...new Array(20)].map(((e,t)=>mobx_state_tree_1.types.late((()=>ADDITIONAL_TYPES[t])))),ElementTypes=mobx_state_tree_1.types.union({dispatcher:e=>{const t=TYPES_MAP[e.type];if(!t)throw new Error(`Unknown element type: "${e.type}"`);return t}},exports.SVGElement,exports.TextElement,exports.ImageElement,...additionalTypesUnion);function createStore({key:e,showCredit:t}={key:"",showCredit:!1}){const o=exports.Store.create();return(0,validate_key_1.validateKey)(e,t),o}exports.Page=mobx_state_tree_1.types.model("Page",{id:mobx_state_tree_1.types.identifier,children:mobx_state_tree_1.types.array(ElementTypes),width:mobx_state_tree_1.types.optional(mobx_state_tree_1.types.union(mobx_state_tree_1.types.number,mobx_state_tree_1.types.literal("auto")),"auto"),height:mobx_state_tree_1.types.optional(mobx_state_tree_1.types.union(mobx_state_tree_1.types.number,mobx_state_tree_1.types.literal("auto")),"auto"),background:"white",bleed:0,custom:mobx_state_tree_1.types.frozen(),_exporting:!1}).postProcessSnapshot((e=>{const t=Object.assign({},e),o={};for(var n in t)"_"!==n[0]&&(o[n]=e[n]);return o})).views((e=>({get store(){return(0,mobx_state_tree_1.getParentOfType)(e,exports.Store)}}))).views((e=>({get computedWidth(){return"auto"===e.width?e.store.width:e.width},get computedHeight(){return"auto"===e.height?e.store.height:e.height}}))).actions((e=>({toJSON:()=>JSON.parse(JSON.stringify((0,mobx_state_tree_1.getSnapshot)(e))),_forEachElementUp(t,o){const n=t.map((t=>({id:t,index:e.children.findIndex((e=>e.id===t))})));n.sort(((e,t)=>t.index-e.index));for(const{index:s}of n){if(-1==s)continue;const n=s<e.children.length-1&&e.children[s+1],i=t.indexOf(null==n?void 0:n.id)>=0;s===e.children.length-1||i||o(s)}},_forEachElementDown(t,o){const n=t.map((t=>({id:t,index:e.children.findIndex((e=>e.id===t))})));n.sort(((e,t)=>e.index-t.index));for(const{index:s}of n){if(-1==s)continue;const n=s>0&&e.children[s-1],i=t.indexOf(null==n?void 0:n.id)>=0;0===s||i||o(s)}return!1}}))).actions((e=>({clone(t){const o=e.toJSON();o.children.forEach((e=>{e.id=(0,nanoid_1.nanoid)(10)}));const n=Object.assign(Object.assign(Object.assign({},o),{id:(0,nanoid_1.nanoid)(10)}),t),s=e.store.addPage(n),i=e.store.pages.indexOf(e);s.setZIndex(i+1),s.select()},setZIndex(t){e.store.setPageZIndex(e.id,t)},set(t){Object.assign(e,t)},select(){e.store.selectPage(e.id)},addElement(t){const o=TYPES_MAP[t.type];if(!o)return void console.error("Can not find model with type "+t.type);const n=o.create(Object.assign({id:(0,nanoid_1.nanoid)(10)},t));return e.children.push(n),n.selectable&&e.store.selectElements([n.id]),n},canMoveElementsUp(t){let o=!1;return e._forEachElementUp(t,(()=>{o=o||!0})),o},moveElementsUp(t){e._forEachElementUp(t,(t=>{const o=e.children[t];(0,mobx_state_tree_1.detach)(o),e.children.splice(t+1,0,o)}))},canMoveElementsTop(e){return this.canMoveElementsUp(e)},moveElementsTop(t){const o=[],n=[];e.children.forEach((e=>{t.indexOf(e.id)>=0?o.push(e):n.push(e)})),e.children.replace(n.concat(o))},canMoveElementsDown(t){let o=!1;return e._forEachElementDown(t,(()=>{o=o||!0})),o},moveElementsDown(t){e._forEachElementDown(t,(t=>{const o=e.children[t];(0,mobx_state_tree_1.detach)(o),e.children.splice(t-1,0,o)}))},canMoveElementsBottom(e){return this.canMoveElementsDown(e)},moveElementsBottom(t){const o=[],n=[];e.children.forEach((e=>{t.indexOf(e.id)>=0?o.push(e):n.push(e)})),e.children.replace(o.concat(n))},setElementZIndex(t,o){const n=e.children.find((e=>e.id===t));n&&((0,mobx_state_tree_1.detach)(n),e.children.remove(n),e.children.splice(o,0,n))},setSize({width:t,height:o},{useMagic:n}){e.width=t,e.height=o}}))).actions((e=>({moveElementUp(t){console.warn("page.moveElementUp(id) is deprecated. Please use page.moveElementsUp([id1, id2]) instead."),e.moveElementsUp([t])},moveElementDown(t){console.warn("page.moveElementDown(id) is deprecated. Please use page.moveElementsDown([id1, id2]) instead."),e.moveElementsDown([t])},moveElementTop(t){console.warn("page.moveElementTop(id) is deprecated. Please use page.moveElementsTop([id1, id2]) instead."),e.moveElementsTop([t])},moveElementBottom(t){console.warn("page.moveElementBottom(id) is deprecated. Please use page.moveElementsBottom([id1, id2]) instead."),e.moveElementsBottom([t])}}))),exports.Font=mobx_state_tree_1.types.model("Font",{fontFamily:mobx_state_tree_1.types.string,url:mobx_state_tree_1.types.optional(mobx_state_tree_1.types.string,""),styles:mobx_state_tree_1.types.frozen()}).preProcessSnapshot((e=>Object.assign(Object.assign({},e),{fontFamily:e.fontFamily||e.name}))),exports.Store=mobx_state_tree_1.types.model("Store",{role:"",pages:mobx_state_tree_1.types.array(exports.Page),fonts:mobx_state_tree_1.types.array(exports.Font),width:1080,height:1080,scale:1,scaleToFit:1,unit:"px",dpi:72,bleedVisible:!1,rulesVisible:!1,openedSidePanel:"",selectedElementsIds:mobx_state_tree_1.types.array(mobx_state_tree_1.types.string),history:mobx_state_tree_1.types.optional(history_1.UndoManager,{targetPath:"../pages"}),_elementsPixelRatio:2,_activePageId:""}).views((e=>({get _bleedVisible(){return console.warn("store._bleedVisible is deprecated. Please use store.bleedVisible instead."),e.bleedVisible},get selectedElements(){return e.selectedElementsIds.map((t=>{for(const o of e.pages)for(const e of o.children)if(e.id===t)return e})).filter((e=>!!e))},get activePage(){return e.pages.slice().find((t=>t.id===e._activePageId))||(e.pages.length?e.pages[0]:null)}}))).actions((e=>({afterCreate(){e.history.canUndo},setUnit({unit:t,dpi:o}){e.unit=t||e.unit,e.dpi=o||e.dpi},setRole(t){e.role=t},getElementById(t){for(const o of e.pages)for(const e of o.children)if(e.id===t)return e},addPage(t){const o=exports.Page.create(Object.assign({id:(0,nanoid_1.nanoid)(10)},t));return e.pages.push(o),e._activePageId=o.id,o},selectPage(t){e._activePageId=t},selectElements(t){const o=t.map((t=>e.getElementById(t))).sort(((e,t)=>e.page.children.indexOf(e)-e.page.children.indexOf(t))).filter((e=>!!e)).map((e=>e.id));e.selectedElementsIds=(0,mobx_state_tree_1.cast)(o)},toggleBleed(t){e.bleedVisible=null!=t?t:!e.bleedVisible},toggleRulers(t){e.rulesVisible=null!=t?t:!e.rulesVisible},openSidePanel(t){e.openedSidePanel=t},setScale(t){e.scale=t},_setScaleToFit(t){e.scaleToFit=t},setElementsPixelRatio(t){e._elementsPixelRatio=t},setSize(t,o,n){if(n){const n=t/e.width,s=o/e.height;for(const t of e.pages)for(const e of t.children)e.set({x:e.x*n,y:e.y*s}),"text"===e.type?e.set({fontSize:e.fontSize*n,width:Math.max(e.width*n,2)}):"image"===e.type?e.set({width:e.width*n,height:e.height*s}):"svg"===e.type&&e.set({width:Math.max(e.width*n,2),height:Math.max(e.height*n,2)})}e.width=t,e.height=o},setPageZIndex(t,o){const n=e.pages.find((e=>e.id===t));n&&((0,mobx_state_tree_1.detach)(n),e.pages.remove(n),e.pages.splice(o,0,n))},deletePages(t){const o=e.pages.indexOf(e.activePage);t.forEach((t=>{const o=e.pages.find((e=>e.id===t));(0,mobx_state_tree_1.destroy)(o)}));const n=Math.min(e.pages.length-1,o),s=e.pages[n];s&&(e._activePageId=s.id),e.selectedElementsIds=(0,mobx_state_tree_1.cast)(e.selectedElementsIds.filter((t=>e.getElementById(t))))},deleteElements(t){t.forEach((t=>{e.pages.forEach((e=>{const o=e.children.find((e=>e.id===t));o&&(0,mobx_state_tree_1.destroy)(o)}))})),e.selectedElementsIds=(0,mobx_state_tree_1.cast)(e.selectedElementsIds.filter((t=>e.getElementById(t))))},on(t,o){if("change"===t){let t=e.toJSON();return(0,mobx_state_tree_1.onSnapshot)(e,(n=>{const s=e.toJSON();!(0,deep_equal_1.deepEqual)(t,s)&&(t=s,o(s))}))}},async toDataURL({pixelRatio:t,ignoreBackground:o,pageId:n,mimeType:s,includeBleed:i,quality:a}={}){var r;const l=t||1;n=n||(null===(r=e.pages[0])||void 0===r?void 0:r.id);const d=e.pages.find((e=>e.id===n));if(!d)throw new Error(`No page for export with id ${n}`);null==d||d.set({_exporting:!0});const c=await(0,wait_1.waitTillAvailable)((()=>konva_1.default.stages.find((e=>e.getAttr("pageId")===n))));if(!c)throw new Error(`Export is failed. Can not find stage for page ${n}. Looks like <Workspace /> component is not mounted, but it is required in order to process the export.`);await e.waitLoading();const p=c.findOne(".page-container");c.find("Transformer").forEach((e=>e.visible(!1))),p.find(".page-background").forEach((e=>e.shadowEnabled(!1))),p.find(".page-background").forEach((e=>e.strokeEnabled(!1))),p.find(".highlighter").forEach((e=>e.visible(!1)));const m=p.findOne(".page-background-group"),_=m.clip();m.clip({x:null,y:null,width:null,height:null});const g=p.findOne(".elements-container"),h=g.clip();g.clip({x:null,y:null,width:null,height:null});const u=p.find((e=>e.getAttr("hideInExport")));u.forEach((e=>{e.setAttr("oldVisible",e.visible()),e.hide()}));const f=p.find((e=>!e.visible()&&e.getAttr("editModeEnabled")));f.forEach((e=>{e.setAttr("oldVisible",e.visible()),e.show()})),o&&p.find(".page-background").forEach((e=>e.hide()));const b=i?d.bleed:0;let x=b;e.bleedVisible&&i?x=0:e.bleedVisible&&!i&&(x=-d.bleed);const y=document.createElement("canvas");y.width=(d.computedWidth+2*b)*l,y.height=(d.computedHeight+2*b)*l;const E=y.getContext("2d");"image/jpeg"===s&&(E.fillStyle="white",E.fillRect(0,0,y.width,y.height));const w=p.toCanvas({x:p.x()-x*p.scaleX(),y:p.y()-x*p.scaleY(),width:(d.computedWidth+2*b)*p.scaleX(),height:(d.computedHeight+2*b)*p.scaleY(),pixelRatio:1/p.scaleX()*l});E.drawImage(w,0,0);const v=y.toDataURL(s,a);return o&&p.find(".page-background").forEach((e=>e.show())),u.forEach((e=>{e.visible(e.getAttr("oldVisible"))})),f.forEach((e=>{e.visible(e.getAttr("oldVisible"))})),p.find(".page-background").forEach((e=>e.shadowEnabled(!0))),p.find(".page-background").forEach((e=>e.strokeEnabled(!0))),c.find("Transformer").forEach((e=>e.visible(!0))),p.find(".highlighter").forEach((e=>e.visible(!0))),m.clip(_),g.clip(h),null==d||d.set({_exporting:!1}),v},async saveAsImage(t={}){var{fileName:o}=t,n=__rest(t,["fileName"]);const s=n.mimeType||"image/png",i=s.split("/")[1];(0,download_1.downloadFile)(await e.toDataURL(n),o||"polotno."+i,s)},async _toPDF(t){const o=t.dpi||e.dpi,n=t.parallel||1,s=t.unit||("px"===e.unit?"mm":e.unit),i=t.pixelRatio||1,a=await(0,pdf_1.getJsPDF)(),r=e=>(0,unit_1.pxToUnit)({px:e,unit:s,dpi:o}),l=e.pages[0]||{},d=t.includeBleed?l.bleed:0,c=r(l.computedWidth+2*d),p=r(l.computedHeight+2*d);var m=new a({unit:s,orientation:c>p?"landscape":"portrait",format:[c,p],compress:!0});m.deletePage(1);const _=e._elementsPixelRatio;e.setElementsPixelRatio(i),await new Promise((e=>setTimeout(e)));const g=((e,t)=>{for(var o=[],n=0;n<e.length;n+=t)o.push(e.slice(n,n+t));return o})(e.pages,n);for(const o of g){const n=o.map((async o=>{const n=t.includeBleed?o.bleed:0,s=r(o.computedWidth+2*n),a=r(o.computedHeight+2*n);let l=0,d=i;for(;l<10;){l+=1,2===l&&console.error("Polotno can not export PDF with current settings. Quality is automatically reduced.");const n=await e.toDataURL(Object.assign(Object.assign({},t),{pageId:o.id,pixelRatio:d}));if(n.length>20)return{url:n,width:s,height:a};d*=.8}}));(await Promise.all(n)).forEach((({url:e,width:t,height:o})=>{m.addPage([t,o],t>o?"landscape":"portrait"),m.addImage(e,0,0,t,o,void 0,"FAST")}))}return e.setElementsPixelRatio(_),m},toPDFDataURL:async t=>(await e._toPDF(Object.assign({mimeType:"image/jpeg"},t))).output("datauristring"),async saveAsPDF(t={}){var{fileName:o}=t,n=__rest(t,["fileName"]);(await e._toPDF(Object.assign({mimeType:"image/jpeg"},n))).save(o||"polotno.pdf")},async waitLoading(){await new Promise((e=>setTimeout(e,50))),await(0,loader_1.whenLoaded)()},toJSON:()=>({width:e.width,height:e.height,fonts:(0,mobx_state_tree_1.getSnapshot)(e.fonts),pages:(0,mobx_state_tree_1.getSnapshot)(e.pages),unit:e.unit,dpi:e.dpi}),loadJSON(t,o=!1){var n;const s=Object.assign({},t),i=e.pages.indexOf(e.activePage);let a=null===(n=s.pages[i]||s.pages[0])||void 0===n?void 0:n.id;s._activePageId=a;const r=Object.assign({},(0,mobx_state_tree_1.getSnapshot)(e));Object.assign(r,s),r.history=o?e.history.toJSON():{history:[],undoIdx:-1},(0,mobx_state_tree_1.applySnapshot)(e,r)},addFont(t){e.removeFont(t.fontFamily),e.fonts.push(t),e.loadFont(t.fontFamily)},removeFont(t){e.fonts.filter((e=>e.fontFamily===t)).forEach((e=>(0,mobx_state_tree_1.destroy)(e)))},async loadFont(t){const o=e.fonts.find((e=>e.fontFamily===t))||fonts.globalFonts.find((e=>e.fontFamily===t));o?fonts.injectCustomFont(o):fonts.injectGoogleFont(t),await fonts.loadFont(t)}}))),exports.createStore=createStore,exports.default=createStore;
|