polotno 2.4.4 → 2.4.6

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/tooltip.js CHANGED
@@ -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.Tooltip=void 0;const react_1=__importDefault(require("react")),mobx_react_lite_1=require("mobx-react-lite"),react_konva_utils_1=require("react-konva-utils"),core_1=require("@blueprintjs/core"),math_1=require("../utils/math"),element_container_1=require("../toolbar/element-container"),duplicate_button_1=require("../toolbar/duplicate-button"),remove_button_1=require("../toolbar/remove-button"),position_picker_1=require("../toolbar/position-picker");function findParentWithClass(e,t){return e.classList.contains(t)?e:e.parentElement?findParentWithClass(e.parentElement,t):null}exports.Tooltip=(0,mobx_react_lite_1.observer)((({store:e,page:t,components:n,stageRef:r})=>{var l,o;const a=(0,math_1.getTotalClientRect)(e.selectedShapes),i=e.selectedShapes.every((e=>e.page===t)),u=react_1.default.useRef(null),[s,c]=react_1.default.useState(!1),d=e.find((e=>"image"===e.type&&e._cropModeEnabled));react_1.default.useEffect((()=>{var e,t,n;const l=()=>{c(!0)},o=()=>{c(!1)};null===(e=null==r?void 0:r.current)||void 0===e||e.on("dragstart",l),null===(t=null==r?void 0:r.current)||void 0===t||t.on("dragend",o);const a=null===(n=null==r?void 0:r.current)||void 0===n?void 0:n.findOne("Transformer");return null==a||a.on("transformstart",l),null==a||a.on("transformend",o),()=>{var e,t;null===(e=null==r?void 0:r.current)||void 0===e||e.off("dragstart",l),null===(t=null==r?void 0:r.current)||void 0===t||t.off("dragend",o),null==a||a.off("transformstart",l),null==a||a.off("transformend",o)}}),[]);const[f,_]=react_1.default.useState({fit:!0,needCalculate:!1,token:Math.random()});if(react_1.default.useEffect((()=>{0!==e.selectedElements.length&&_({fit:!0,needCalculate:!0,token:Math.random()})}),[e.selectedElements,s]),react_1.default.useEffect((()=>{setTimeout((()=>{if(!u.current)return;if(!f.needCalculate)return;const e=findParentWithClass(u.current,"polotno-workspace-container");if(!e)return;const t=e.getBoundingClientRect(),n=u.current.getBoundingClientRect(),r=(n.right,t.left,n.top-t.top),l=(n.left,t.left,n.bottom-t.top);r<20&&f.fit&&_({fit:!1,needCalculate:!1,token:f.token}),l-t.height>-20&&!f.fit&&_({fit:!0,needCalculate:!1,token:f.token})}),10)}),[f.needCalculate,u.current,f.token]),react_1.default.useEffect((()=>{const t=findParentWithClass(r.current.content,"polotno-workspace-inner"),n=()=>{e.selectedElements.length&&_({fit:!0,needCalculate:!0,token:Math.random()})};return null==t||t.addEventListener("scroll",n),()=>{null==t||t.removeEventListener("scroll",n)}}),[]),0===e.selectedShapes.length)return null;if(s)return null;if(!i)return null;if(e.activePage!==t)return null;if(d)return null;const m=(null==n?void 0:n.Position)||position_picker_1.PositionPicker,p=(null==n?void 0:n.Duplicate)||duplicate_button_1.DuplicateButton,v=(null==n?void 0:n.Remove)||remove_button_1.RemoveButton,h=e.selectedElements[0].type,b=(0,element_container_1.extendToolbar)({components:n,type:h,usedItems:[]}),E=(null===(o=null===(l=null==r?void 0:r.current)||void 0===l?void 0:l.findOne("Transformer"))||void 0===o?void 0:o.rotation())||0;let g=30;return Math.abs(E)<5&&f.fit&&(g=80),Math.abs(E)>160&&!f.fit&&(g=80),react_1.default.createElement(react_konva_utils_1.Html,{transformFunc:e=>{const t=a.x+a.width/2,n=f.fit?a.y*e.scaleY-g:a.y*e.scaleY+a.height*e.scaleY+g;return Object.assign(Object.assign({},e),{x:e.x+t*e.scaleX,y:e.y+n,scaleX:1,scaleY:1})},divProps:{style:{pointerEvents:"none"}}},react_1.default.createElement("div",{ref:u,style:{pointerEvents:"none"}},react_1.default.createElement(core_1.Navbar,{style:{padding:"2px",borderRadius:"5px",height:"34px",transform:"translate(-50%, -50%)",pointerEvents:"auto"}},react_1.default.createElement(core_1.NavbarGroup,{style:{height:"30px"}},b.map((t=>{const r=n[t];return react_1.default.createElement(r,{elements:e.selectedElements,element:e.selectedElements[0],store:e,key:t})})),react_1.default.createElement(p,{store:e}),react_1.default.createElement(v,{store:e}),react_1.default.createElement(m,{store:e})))))}));
1
+ "use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Tooltip=void 0;const react_1=__importDefault(require("react")),mobx_react_lite_1=require("mobx-react-lite"),react_konva_utils_1=require("react-konva-utils"),core_1=require("@blueprintjs/core"),math_1=require("../utils/math"),element_container_1=require("../toolbar/element-container"),duplicate_button_1=require("../toolbar/duplicate-button"),remove_button_1=require("../toolbar/remove-button"),position_picker_1=require("../toolbar/position-picker");function findParentWithClass(e,t){return e.classList.contains(t)?e:e.parentElement?findParentWithClass(e.parentElement,t):null}exports.Tooltip=(0,mobx_react_lite_1.observer)((({store:e,page:t,components:n,stageRef:r})=>{var l,o;const a=(0,math_1.getTotalClientRect)(e.selectedShapes),u=e.selectedShapes.every((e=>e.page===t)),i=react_1.default.useRef(null),[s,c]=react_1.default.useState(!1),d=e._hasCroppedImages;react_1.default.useEffect((()=>{var e,t,n;const l=()=>{c(!0)},o=()=>{c(!1)};null===(e=null==r?void 0:r.current)||void 0===e||e.on("dragstart",l),null===(t=null==r?void 0:r.current)||void 0===t||t.on("dragend",o);const a=null===(n=null==r?void 0:r.current)||void 0===n?void 0:n.findOne("Transformer");return null==a||a.on("transformstart",l),null==a||a.on("transformend",o),()=>{var e,t;null===(e=null==r?void 0:r.current)||void 0===e||e.off("dragstart",l),null===(t=null==r?void 0:r.current)||void 0===t||t.off("dragend",o),null==a||a.off("transformstart",l),null==a||a.off("transformend",o)}}),[]);const[f,_]=react_1.default.useState({fit:!0,needCalculate:!1,token:Math.random()});if(react_1.default.useEffect((()=>{0!==e.selectedElements.length&&_({fit:!0,needCalculate:!0,token:Math.random()})}),[e.selectedElements,s]),react_1.default.useEffect((()=>{setTimeout((()=>{if(!i.current)return;if(!f.needCalculate)return;const e=findParentWithClass(i.current,"polotno-workspace-container");if(!e)return;const t=e.getBoundingClientRect(),n=i.current.getBoundingClientRect(),r=(n.right,t.left,n.top-t.top),l=(n.left,t.left,n.bottom-t.top);r<20&&f.fit&&_({fit:!1,needCalculate:!1,token:f.token}),l-t.height>-20&&!f.fit&&_({fit:!0,needCalculate:!1,token:f.token})}),10)}),[f.needCalculate,i.current,f.token]),react_1.default.useEffect((()=>{const t=findParentWithClass(r.current.content,"polotno-workspace-inner"),n=()=>{e.selectedElements.length&&_({fit:!0,needCalculate:!0,token:Math.random()})};return null==t||t.addEventListener("scroll",n),()=>{null==t||t.removeEventListener("scroll",n)}}),[]),0===e.selectedShapes.length)return null;if(s)return null;if(!u)return null;if(e.activePage!==t)return null;if(d)return null;const m=(null==n?void 0:n.Position)||position_picker_1.PositionPicker,p=(null==n?void 0:n.Duplicate)||duplicate_button_1.DuplicateButton,v=(null==n?void 0:n.Remove)||remove_button_1.RemoveButton,h=e.selectedElements[0].type,b=(0,element_container_1.extendToolbar)({components:n,type:h,usedItems:[]}),g=(null===(o=null===(l=null==r?void 0:r.current)||void 0===l?void 0:l.findOne("Transformer"))||void 0===o?void 0:o.rotation())||0;let E=30;return Math.abs(g)<5&&f.fit&&(E=80),Math.abs(g)>160&&!f.fit&&(E=80),react_1.default.createElement(react_konva_utils_1.Html,{transformFunc:e=>{const t=a.x+a.width/2,n=f.fit?a.y*e.scaleY-E:a.y*e.scaleY+a.height*e.scaleY+E;return Object.assign(Object.assign({},e),{x:e.x+t*e.scaleX,y:e.y+n,scaleX:1,scaleY:1})},divProps:{style:{pointerEvents:"none"}}},react_1.default.createElement("div",{ref:i,style:{pointerEvents:"none"}},react_1.default.createElement(core_1.Navbar,{style:{padding:"2px",borderRadius:"5px",height:"34px",transform:"translate(-50%, -50%)",pointerEvents:"auto"}},react_1.default.createElement(core_1.NavbarGroup,{style:{height:"30px"}},b.map((t=>{const r=n[t];return react_1.default.createElement(r,{elements:e.selectedElements,element:e.selectedElements[0],store:e,key:t})})),react_1.default.createElement(p,{store:e}),react_1.default.createElement(v,{store:e}),react_1.default.createElement(m,{store: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.WorkspaceCanvas=void 0;const react_1=__importDefault(require("react")),mobx_react_lite_1=require("mobx-react-lite"),page_1=__importDefault(require("./page")),rules_1=require("./rules"),hotkeys_1=require("./hotkeys"),l10n_1=require("../utils/l10n"),limit=(e,t,r)=>Math.max(t,Math.min(r,e)),ZERO_SIZE_WARNING="Polotno warning: <Workspace /> component can not automatically detect its size.\nWidth or height of parent elements is equal 0.\nPlease make sure it has non-zero size. You may need to adjust it with your styles. <Workspace /> will automatically fit into parent container.\nFor simpler debugging here is the log of the parent element:",useSaveScrollOnScaleChange=(e,t,r,a,n,l)=>{const o=react_1.default.useRef({width:t,height:r}),c=react_1.default.useRef({top:0,left:0}),s=react_1.default.useRef(!1),i=react_1.default.useRef(n.pages.length);s.current=i.current!==n.pages.length,i.current=n.pages.length,react_1.default.useEffect((()=>{const t=e.current,r=e=>{c.current={top:t.scrollTop,left:t.scrollLeft}};return t.addEventListener("scroll",r),()=>{t.removeEventListener("scroll",r)}}),[]),react_1.default.useLayoutEffect((()=>{if(!e.current)return;if(s.current)return;const a=e.current,n=(c.current.left+a.offsetWidth/2)/o.current.width,i=(c.current.top+a.offsetHeight/2)/o.current.height;l.current=!0,a.scrollLeft=n*t-a.offsetWidth/2,a.scrollTop=i*r-a.offsetHeight/2,o.current={width:t,height:r}}),[a,t,r])},useScrollOnActiveChange=(e,t,r,a,n)=>{const l=react_1.default.useRef(!1),o=react_1.default.useRef(null);react_1.default.useEffect((()=>{const t=e.current,r=()=>{n.current};return t.addEventListener("scroll",r),()=>{t.removeEventListener("scroll",r)}}),[]);const c=r.pages.indexOf(r.activePage);react_1.default.useLayoutEffect((()=>{if(!r.activePage)return;if(!e.current)return;if(l.current)return;const a=e.current,o=r.pages.indexOf(r.activePage)*t;Math.abs(o-a.scrollTop)>.5*t&&(n.current=!0,a.scrollTop=o)}),[r.activePage,c]);return{handleScroll:e=>{if(n.current)return void(n.current=!1);l.current=!0,clearTimeout(o.current),o.current=setTimeout((()=>{l.current=!1}),300);const t=e.currentTarget.childNodes[0].offsetHeight,c=e.currentTarget.scrollTop,s=Math.floor((c+a.height/3)/t),i=r.pages[s];i&&i.select()}}},NoPages=({store:e})=>react_1.default.createElement("div",{style:{position:"absolute",top:"50%",left:"50%",transform:"translate(-50%, -50%)",textAlign:"center"}},react_1.default.createElement("p",null,(0,l10n_1.t)("workspace.noPages")),react_1.default.createElement("button",{onClick:()=>{e.addPage()}},(0,l10n_1.t)("workspace.addPage"))),PagePlaceholder=({width:e,height:t,xPadding:r,yPadding:a,backgroundColor:n})=>react_1.default.createElement("div",{style:{width:e+"px",height:t+"px",backgroundColor:n,paddingLeft:r+"px",paddingRight:r+"px",paddingTop:a+"px",paddingBottom:a+"px"}},react_1.default.createElement("div",{style:{width:" 100%",height:"100%",backgroundColor:"white"}}));exports.WorkspaceCanvas=(0,mobx_react_lite_1.observer)((({store:e,pageControlsEnabled:t,backgroundColor:r,pageBorderColor:a,activePageBorderColor:n,bleedColor:l,components:o,onKeyDown:c,paddingX:s,paddingY:i})=>{const u=null!=s?s:20,d=null!=i?i:55,[h,g]=react_1.default.useState({width:100,height:100}),f=react_1.default.useRef(h),p=react_1.default.useRef(null),_=react_1.default.useRef(null),m=e.bleedVisible?Math.max(0,...e.pages.map((e=>e.bleed))):0,v=Math.max(...e.pages.map((e=>e.computedWidth))),w=Math.max(...e.pages.map((e=>e.computedHeight))),b=v+2*m,x=w+2*m,E=async()=>{if(await new Promise((e=>setTimeout(e,50))),null===p.current)return;const t=p.current.getBoundingClientRect();0!==t.width&&0!==t.height||(console.warn(ZERO_SIZE_WARNING),console.log(p.current));const r=_.current.clientWidth||t.width,a={width:r,height:t.height};(f.current.width!==a.width||f.current.height!==a.height)&&(g(a),f.current=a);const n=(r-2*u)/b,l=(t.height-2*d)/x,o=Math.max(Math.min(n,l),.01);e.scaleToFit!==o&&(e.setScale(o),e._setScaleToFit(o))};react_1.default.useEffect((()=>{E()}),[b,x]),react_1.default.useEffect((()=>{const e=p.current;if(window.ResizeObserver){const t=new ResizeObserver(E);return t.observe(e),()=>t.unobserve(e)}{const e=setInterval(E,100);return()=>clearInterval(e)}}),[b,x]);const y=Math.max(u,(h.width-b*e.scale)/2),P=x*e.scale*e.pages.length,k=Math.max(d,(h.height-P)/e.pages.length/2);react_1.default.useEffect((()=>{const t=t=>{(c||hotkeys_1.handleHotkey)(t,e)};return window.addEventListener("keydown",t),()=>window.removeEventListener("keydown",t)}),[]),react_1.default.useEffect((()=>{var t;const r=t=>{if(t.ctrlKey||t.metaKey){t.preventDefault();const l=Math.max(3,e.scaleToFit),o=Math.min(.1,e.scaleToFit),c=(r=t.deltaY<0?1.05*e.scale:e.scale/1.05,a=o,n=l,Math.max(a,Math.min(n,r)));e.setScale(c)}else var r,a,n};return null===(t=_.current)||void 0===t||t.addEventListener("wheel",r),()=>{var e;return null===(e=_.current)||void 0===e?void 0:e.removeEventListener("wheel",r)}}),[]);const C=react_1.default.useRef(!1);useSaveScrollOnScaleChange(_,b*e.scale+2*y,x*e.scale+2*k,e.scale,e,C);const{handleScroll:R}=useScrollOnActiveChange(_,x*e.scale+2*k,e,h,C),M=h.width>=b*e.scale+2*y,S=r||"rgba(232, 232, 232, 0.9)",T=e.pages.indexOf(e.activePage),L=(null==o?void 0:o.NoPages)||NoPages;return react_1.default.createElement("div",{ref:p,style:{width:"100%",height:"100%",position:"relative",outline:"none",flex:1,backgroundColor:S},tabIndex:0,className:"polotno-workspace-container"},react_1.default.createElement("div",{ref:_,onScroll:R,style:{position:"absolute",top:0,left:0,width:"100%",height:"100%",overflow:"auto",overflowX:M?"hidden":"auto"},className:"polotno-workspace-inner"},e.pages.map(((r,c)=>Math.abs(c-T)<=1||r._exporting?react_1.default.createElement(page_1.default,{key:r.id,page:r,xPadding:y,yPadding:k,width:b*e.scale+2*y,height:x*e.scale+2*k,store:e,pageControlsEnabled:t,backColor:S,pageBorderColor:a||"lightgrey",activePageBorderColor:n||"rgb(0, 161, 255)",bleedColor:l||"rgba(255, 0, 0, 0.1)",components:o}):react_1.default.createElement(PagePlaceholder,{key:r.id,width:b*e.scale+2*y,height:x*e.scale+2*k,backgroundColor:S,xPadding:y,yPadding:k}))),e.rulesVisible&&react_1.default.createElement(rules_1.TopRules,{store:e,xPadding:y,yPadding:k,width:b*e.scale+2*y,height:x*e.scale+2*k}),0===e.pages.length&&react_1.default.createElement(L,{store:e})))})),exports.default=exports.WorkspaceCanvas;
1
+ "use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.WorkspaceCanvas=void 0;const react_1=__importDefault(require("react")),mobx_react_lite_1=require("mobx-react-lite"),page_1=__importDefault(require("./page")),rules_1=require("./rules"),hotkeys_1=require("./hotkeys"),l10n_1=require("../utils/l10n"),limit=(e,t,r)=>Math.max(t,Math.min(r,e)),ZERO_SIZE_WARNING="Polotno warning: <Workspace /> component can not automatically detect its size.\nWidth or height of parent elements is equal 0.\nPlease make sure it has non-zero size. You may need to adjust it with your styles. <Workspace /> will automatically fit into parent container.\nFor simpler debugging here is the log of the parent element:",useSaveScrollOnScaleChange=(e,t,r,a,n,l)=>{const o=react_1.default.useRef({width:t,height:r}),c=react_1.default.useRef({top:0,left:0}),s=react_1.default.useRef(!1),i=react_1.default.useRef(n.pages.length);s.current=i.current!==n.pages.length,i.current=n.pages.length,react_1.default.useEffect((()=>{const t=e.current,r=e=>{c.current={top:t.scrollTop,left:t.scrollLeft}};return t.addEventListener("scroll",r),()=>{t.removeEventListener("scroll",r)}}),[]),react_1.default.useLayoutEffect((()=>{if(!e.current)return;if(s.current)return;const a=e.current,n=(c.current.left+a.offsetWidth/2)/o.current.width,i=(c.current.top+a.offsetHeight/2)/o.current.height;l.current=!0,a.scrollLeft=n*t-a.offsetWidth/2,a.scrollTop=i*r-a.offsetHeight/2,o.current={width:t,height:r}}),[a,t,r])},useScrollOnActiveChange=(e,t,r,a,n)=>{const l=react_1.default.useRef(!1),o=react_1.default.useRef(null);react_1.default.useEffect((()=>{const t=e.current,r=()=>{n.current};return t.addEventListener("scroll",r),()=>{t.removeEventListener("scroll",r)}}),[]);const c=r.pages.indexOf(r.activePage);react_1.default.useLayoutEffect((()=>{if(!r.activePage)return;if(!e.current)return;if(l.current)return;const a=e.current,o=r.pages.indexOf(r.activePage)*t;Math.abs(o-a.scrollTop)>.5*t&&(n.current=!0,a.scrollTop=o)}),[r.activePage,c]);return{handleScroll:e=>{if(n.current)return void(n.current=!1);l.current=!0,clearTimeout(o.current),o.current=setTimeout((()=>{l.current=!1}),300);const t=e.currentTarget.childNodes[0].offsetHeight,c=e.currentTarget.scrollTop,s=Math.floor((c+a.height/3)/t),i=r.pages[s];i&&i.select()}}},NoPages=({store:e})=>react_1.default.createElement("div",{style:{position:"absolute",top:"50%",left:"50%",transform:"translate(-50%, -50%)",textAlign:"center"}},react_1.default.createElement("p",null,(0,l10n_1.t)("workspace.noPages")),react_1.default.createElement("button",{onClick:()=>{e.addPage()}},(0,l10n_1.t)("workspace.addPage"))),PagePlaceholder=({width:e,height:t,xPadding:r,yPadding:a,backgroundColor:n})=>react_1.default.createElement("div",{style:{width:e+"px",height:t+"px",backgroundColor:n,paddingLeft:r+"px",paddingRight:r+"px",paddingTop:a+"px",paddingBottom:a+"px"}},react_1.default.createElement("div",{style:{width:" 100%",height:"100%",backgroundColor:"white"}}));exports.WorkspaceCanvas=(0,mobx_react_lite_1.observer)((({store:e,pageControlsEnabled:t,backgroundColor:r,pageBorderColor:a,activePageBorderColor:n,bleedColor:l,components:o,onKeyDown:c,paddingX:s,paddingY:i})=>{const u=null!=s?s:20,d=null!=i?i:55,[h,g]=react_1.default.useState({width:100,height:100}),f=react_1.default.useRef(h),p=react_1.default.useRef(null),m=react_1.default.useRef(null),_=e.bleedVisible?Math.max(0,...e.pages.map((e=>e.bleed))):0,v=Math.max(...e.pages.map((e=>e.computedWidth))),w=Math.max(...e.pages.map((e=>e.computedHeight))),b=v+2*_,x=w+2*_,E=async({skipTimeout:t}={skipTimeout:!1})=>{if(t||await new Promise((e=>setTimeout(e,50))),null===p.current)return;const r=p.current.getBoundingClientRect();0!==r.width&&0!==r.height||(console.warn(ZERO_SIZE_WARNING),console.log(p.current));const a=m.current.clientWidth||r.width,n={width:a,height:r.height};(f.current.width!==n.width||f.current.height!==n.height)&&(g(n),f.current=n);const l=(a-2*u)/b,o=(r.height-2*d)/x,c=Math.max(Math.min(l,o),.01);e.scaleToFit!==c&&(e.setScale(c),e._setScaleToFit(c))};react_1.default.useLayoutEffect((()=>{E({skipTimeout:!0})}),[]),react_1.default.useEffect((()=>{E()}),[b,x]),react_1.default.useEffect((()=>{const e=p.current;if(window.ResizeObserver){const t=new ResizeObserver((()=>{E({skipTimeout:!0})}));return t.observe(e),()=>t.unobserve(e)}{const e=setInterval((()=>{E({skipTimeout:!0})}),100);return()=>clearInterval(e)}}),[b,x]);const k=Math.max(u,(h.width-b*e.scale)/2),y=x*e.scale*e.pages.length,P=Math.max(d,(h.height-y)/e.pages.length/2);react_1.default.useEffect((()=>{const t=t=>{(c||hotkeys_1.handleHotkey)(t,e)};return window.addEventListener("keydown",t),()=>window.removeEventListener("keydown",t)}),[]),react_1.default.useEffect((()=>{var t;const r=t=>{if(t.ctrlKey||t.metaKey){t.preventDefault();const l=Math.max(3,e.scaleToFit),o=Math.min(.1,e.scaleToFit),c=(r=t.deltaY<0?1.05*e.scale:e.scale/1.05,a=o,n=l,Math.max(a,Math.min(n,r)));e.setScale(c)}else var r,a,n};return null===(t=m.current)||void 0===t||t.addEventListener("wheel",r),()=>{var e;return null===(e=m.current)||void 0===e?void 0:e.removeEventListener("wheel",r)}}),[]);const C=react_1.default.useRef(!1);useSaveScrollOnScaleChange(m,b*e.scale+2*k,x*e.scale+2*P,e.scale,e,C);const{handleScroll:T}=useScrollOnActiveChange(m,x*e.scale+2*P,e,h,C),R=h.width>=b*e.scale+2*k,M=r||"rgba(232, 232, 232, 0.9)",S=e.pages.indexOf(e.activePage),L=(null==o?void 0:o.NoPages)||NoPages;return react_1.default.createElement("div",{ref:p,style:{width:"100%",height:"100%",position:"relative",outline:"none",flex:1,backgroundColor:M},tabIndex:0,className:"polotno-workspace-container"},react_1.default.createElement("div",{ref:m,onScroll:T,style:{position:"absolute",top:0,left:0,width:"100%",height:"100%",overflow:"auto",overflowX:R?"hidden":"auto"},className:"polotno-workspace-inner"},e.pages.map(((r,c)=>Math.abs(c-S)<=1||r._exporting?react_1.default.createElement(page_1.default,{key:r.id,page:r,xPadding:k,yPadding:P,width:b*e.scale+2*k,height:x*e.scale+2*P,store:e,pageControlsEnabled:t,backColor:M,pageBorderColor:a||"lightgrey",activePageBorderColor:n||"rgb(0, 161, 255)",bleedColor:l||"rgba(255, 0, 0, 0.1)",components:o}):react_1.default.createElement(PagePlaceholder,{key:r.id,width:b*e.scale+2*k,height:x*e.scale+2*P,backgroundColor:M,xPadding:k,yPadding:P}))),e.rulesVisible&&react_1.default.createElement(rules_1.TopRules,{store:e,xPadding:k,yPadding:P,width:b*e.scale+2*k,height:x*e.scale+2*P}),0===e.pages.length&&react_1.default.createElement(L,{store:e})))})),exports.default=exports.WorkspaceCanvas;
@@ -1 +1 @@
1
- "use strict";var __rest=this&&this.__rest||function(e,t){var r={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(r[o]=e[o]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var l=0;for(o=Object.getOwnPropertySymbols(e);l<o.length;l++)t.indexOf(o[l])<0&&Object.prototype.propertyIsEnumerable.call(e,o[l])&&(r[o[l]]=e[o[l]])}return r};Object.defineProperty(exports,"__esModule",{value:!0}),exports.registerShapeModel=exports.TYPES_MAP=exports.GroupElement=exports.ElementTypes=exports.forEveryChild=void 0;const mobx_state_tree_1=require("mobx-state-tree"),node_model_1=require("./node-model"),shape_model_1=require("./shape-model"),text_model_1=require("./text-model"),image_model_1=require("./image-model"),video_model_1=require("./video-model"),line_model_1=require("./line-model"),svg_model_1=require("./svg-model"),figure_model_1=require("./figure-model"),forEveryChild=(e,t)=>{if(e.children)for(const r of e.children){if(t(r))break;(0,exports.forEveryChild)(r,t)}};exports.forEveryChild=forEveryChild;const additionalTypesUnion=[...new Array(20)].map(((e,t)=>mobx_state_tree_1.types.late((()=>ADDITIONAL_TYPES[t]))));exports.ElementTypes=mobx_state_tree_1.types.union({dispatcher:e=>{const t=exports.TYPES_MAP[e.type];if(!t)throw new Error(`Unknown element type: "${e.type}"`);return t}},svg_model_1.SVGElement,text_model_1.TextElement,image_model_1.ImageElement,line_model_1.LineElement,video_model_1.VideoElement,figure_model_1.FigureElement,mobx_state_tree_1.types.late((()=>exports.GroupElement)),...additionalTypesUnion),exports.GroupElement=node_model_1.Node.named("Group").props({type:"group",children:mobx_state_tree_1.types.array(exports.ElementTypes)}).views((e=>({get draggable(){let t=!0;return(0,exports.forEveryChild)(e,(e=>{e.draggable||(t=!1)})),t},get resizable(){let t=!0;return(0,exports.forEveryChild)(e,(e=>{e.resizable||(t=!1)})),t},get contentEditable(){let t=!0;return(0,exports.forEveryChild)(e,(e=>{e.contentEditable||(t=!1)})),t},get styleEditable(){let t=!0;return(0,exports.forEveryChild)(e,(e=>{e.styleEditable||(t=!1)})),t},get locked(){let t=!0;return(0,exports.forEveryChild)(e,(e=>{e.locked||(t=!1)})),t}}))).actions((e=>({set(t){var{draggable:r,contentEditable:o,styleEditable:l,resizable:i}=t,s=__rest(t,["draggable","contentEditable","styleEditable","resizable"]);void 0!==r&&(0,exports.forEveryChild)(e,(e=>{e.set({draggable:r})})),void 0!==o&&(0,exports.forEveryChild)(e,(e=>{e.set({contentEditable:o})})),void 0!==l&&(0,exports.forEveryChild)(e,(e=>{e.set({styleEditable:l})})),void 0!==i&&(0,exports.forEveryChild)(e,(e=>{e.set({resizable:i})})),Object.assign(e,s)}})));const ADDITIONAL_TYPES=[];function registerShapeModel(e,t){const r=e.type;if(!r)throw new Error('You must pass "type" attribute to custom model.');let o=shape_model_1.Shape.named(r).props(e);t&&(o=t(o)),exports.TYPES_MAP[r]=o,ADDITIONAL_TYPES.push(o)}exports.TYPES_MAP={svg:svg_model_1.SVGElement,text:text_model_1.TextElement,image:image_model_1.ImageElement,group:exports.GroupElement,line:line_model_1.LineElement,video:video_model_1.VideoElement,figure:figure_model_1.FigureElement},exports.registerShapeModel=registerShapeModel;
1
+ "use strict";var __rest=this&&this.__rest||function(e,t){var r={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(r[o]=e[o]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var l=0;for(o=Object.getOwnPropertySymbols(e);l<o.length;l++)t.indexOf(o[l])<0&&Object.prototype.propertyIsEnumerable.call(e,o[l])&&(r[o[l]]=e[o[l]])}return r};Object.defineProperty(exports,"__esModule",{value:!0}),exports.registerShapeModel=exports.TYPES_MAP=exports.GroupElement=exports.ElementTypes=exports.forEveryChild=void 0;const mobx_state_tree_1=require("mobx-state-tree"),node_model_1=require("./node-model"),shape_model_1=require("./shape-model"),text_model_1=require("./text-model"),image_model_1=require("./image-model"),video_model_1=require("./video-model"),line_model_1=require("./line-model"),svg_model_1=require("./svg-model"),figure_model_1=require("./figure-model"),forEveryChild=(e,t)=>{if(e.children)for(const r of e.children){if(!0===t(r))break;(0,exports.forEveryChild)(r,t)}};exports.forEveryChild=forEveryChild;const additionalTypesUnion=[...new Array(20)].map(((e,t)=>mobx_state_tree_1.types.late((()=>ADDITIONAL_TYPES[t]))));exports.ElementTypes=mobx_state_tree_1.types.union({dispatcher:e=>{const t=exports.TYPES_MAP[e.type];if(!t)throw new Error(`Unknown element type: "${e.type}"`);return t}},svg_model_1.SVGElement,text_model_1.TextElement,image_model_1.ImageElement,line_model_1.LineElement,video_model_1.VideoElement,figure_model_1.FigureElement,mobx_state_tree_1.types.late((()=>exports.GroupElement)),...additionalTypesUnion),exports.GroupElement=node_model_1.Node.named("Group").props({type:"group",children:mobx_state_tree_1.types.array(exports.ElementTypes)}).views((e=>({get draggable(){let t=!0;return(0,exports.forEveryChild)(e,(e=>{e.draggable||(t=!1)})),t},get resizable(){let t=!0;return(0,exports.forEveryChild)(e,(e=>{e.resizable||(t=!1)})),t},get contentEditable(){let t=!0;return(0,exports.forEveryChild)(e,(e=>{e.contentEditable||(t=!1)})),t},get styleEditable(){let t=!0;return(0,exports.forEveryChild)(e,(e=>{e.styleEditable||(t=!1)})),t},get locked(){let t=!0;return(0,exports.forEveryChild)(e,(e=>{e.locked||(t=!1)})),t}}))).actions((e=>({set(t){var{draggable:r,contentEditable:o,styleEditable:l,resizable:i}=t,s=__rest(t,["draggable","contentEditable","styleEditable","resizable"]);void 0!==r&&(0,exports.forEveryChild)(e,(e=>{e.set({draggable:r})})),void 0!==o&&(0,exports.forEveryChild)(e,(e=>{e.set({contentEditable:o})})),void 0!==l&&(0,exports.forEveryChild)(e,(e=>{e.set({styleEditable:l})})),void 0!==i&&(0,exports.forEveryChild)(e,(e=>{e.set({resizable:i})})),Object.assign(e,s)}})));const ADDITIONAL_TYPES=[];function registerShapeModel(e,t){const r=e.type;if(!r)throw new Error('You must pass "type" attribute to custom model.');let o=shape_model_1.Shape.named(r).props(e);t&&(o=t(o)),exports.TYPES_MAP[r]=o,ADDITIONAL_TYPES.push(o)}exports.TYPES_MAP={svg:svg_model_1.SVGElement,text:text_model_1.TextElement,image:image_model_1.ImageElement,group:exports.GroupElement,line:line_model_1.LineElement,video:video_model_1.VideoElement,figure:figure_model_1.FigureElement},exports.registerShapeModel=registerShapeModel;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Page=void 0;const mobx_state_tree_1=require("mobx-state-tree"),nanoid_1=require("nanoid"),group_model_1=require("./group-model"),store_1=require("./store"),group_model_2=require("./group-model");exports.Page=mobx_state_tree_1.types.model("Page",{id:mobx_state_tree_1.types.identifier,children:mobx_state_tree_1.types.array(mobx_state_tree_1.types.late((()=>group_model_1.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(),duration:5e3,_exporting:!1,_rendering:!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,store_1.Store)},get startTime(){let t=0;for(const o of e.store.pages){if(o.id===e.id)return t;t+=o.duration}return t}}))).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:i}of n){if(-1==i)continue;const n=i<e.children.length-1&&e.children[i+1],s=t.indexOf(null==n?void 0:n.id)>=0;i===e.children.length-1||s||o(i)}},_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:i}of n){if(-1==i)continue;const n=i>0&&e.children[i-1],s=t.indexOf(null==n?void 0:n.id)>=0;0===i||s||o(i)}return!1}}))).actions((e=>({clone(t={}){const o=e.toJSON();o.children.forEach((e=>{e.id=(0,nanoid_1.nanoid)(10),(0,group_model_2.forEveryChild)(e,(e=>{e.id=(0,nanoid_1.nanoid)(10)}))}));const n=Object.assign(Object.assign(Object.assign({},o),{id:(0,nanoid_1.nanoid)(10)}),t),i=e.store.addPage(n),s=e.store.pages.indexOf(e);i.setZIndex(s+1),i.select()},setZIndex(t){e.store.setPageZIndex(e.id,t)},set(t){Object.assign(e,t)},select(){e.store.selectPage(e.id)},addElement(t,{skipSelect:o=!1}={}){const n=group_model_2.TYPES_MAP[t.type];if(!n)return void console.error("Can not find model with type "+t.type);t.children&&t.children.forEach((e=>{e.id=(0,nanoid_1.nanoid)(10)}));const i=n.create(Object.assign({id:(0,nanoid_1.nanoid)(10)},t));return e.children.push(i),i.selectable&&!o&&e.store.selectElements([i.id]),i},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,softChange:i}){if(n){const n=t/e.computedWidth,i=o/e.computedHeight,s=Math.min(n,i),r=Math.max(0,(t-e.computedWidth*s)/2),d=Math.max(0,(o-e.computedHeight*s)/2);(0,group_model_2.forEveryChild)(e,(async t=>{if("group"===t.type)return;"image"===t.type&&t.x<1&&t.y<1&&t.width>=e.computedWidth-2&&t.height>=e.computedHeight-2?t.set({x:t.x*s,y:t.y*s,width:t.width*n,height:t.height*i,cropX:0,cropY:0,cropWidth:1,cropHeight:1}):(t.set({x:r+t.x*s,y:d+t.y*s,width:t.width*s,height:t.height*s}),"text"===t.type&&t.set({fontSize:t.fontSize*s}))}))}i||(e.width=t),i||(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])}})));
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Page=void 0;const mobx_state_tree_1=require("mobx-state-tree"),nanoid_1=require("nanoid"),group_model_1=require("./group-model"),store_1=require("./store"),group_model_2=require("./group-model");exports.Page=mobx_state_tree_1.types.model("Page",{id:mobx_state_tree_1.types.identifier,children:mobx_state_tree_1.types.array(mobx_state_tree_1.types.late((()=>group_model_1.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(),duration:5e3,_exporting:!1,_rendering:!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,store_1.Store)},get startTime(){let t=0;for(const o of e.store.pages){if(o.id===e.id)return t;t+=o.duration}return t}}))).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:i}of n){if(-1==i)continue;const n=i<e.children.length-1&&e.children[i+1],s=t.indexOf(null==n?void 0:n.id)>=0;i===e.children.length-1||s||o(i)}},_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:i}of n){if(-1==i)continue;const n=i>0&&e.children[i-1],s=t.indexOf(null==n?void 0:n.id)>=0;0===i||s||o(i)}return!1}}))).actions((e=>({clone(t={}){const o=e.toJSON();o.children.forEach((e=>{e.id=(0,nanoid_1.nanoid)(10),(0,group_model_2.forEveryChild)(e,(e=>{e.id=(0,nanoid_1.nanoid)(10)}))}));const n=Object.assign(Object.assign(Object.assign({},o),{id:(0,nanoid_1.nanoid)(10)}),t),i=e.store.addPage(n),s=e.store.pages.indexOf(e);i.setZIndex(s+1),i.select()},setZIndex(t){e.store.setPageZIndex(e.id,t)},set(t){Object.assign(e,t)},select(){e.store.selectPage(e.id)},addElement(t,{skipSelect:o=!1}={}){const n=group_model_2.TYPES_MAP[t.type];if(!n)return void console.error("Can not find model with type "+t.type);t.children&&t.children.forEach((e=>{e.id=(0,nanoid_1.nanoid)(10)}));const i=n.create(Object.assign({id:(0,nanoid_1.nanoid)(10)},t));return e.children.push(i),i.selectable&&!o&&e.store.selectElements([i.id]),i},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,softChange:i}){if(n){const n=t/e.computedWidth,i=o/e.computedHeight,s=Math.min(n,i),r=Math.max(0,(t-e.computedWidth*s)/2),d=Math.max(0,(o-e.computedHeight*s)/2);(0,group_model_2.forEveryChild)(e,(t=>{if("group"===t.type)return;"image"===t.type&&t.x<1&&t.y<1&&t.width>=e.computedWidth-2&&t.height>=e.computedHeight-2?t.set({x:t.x*s,y:t.y*s,width:t.width*n,height:t.height*i,cropX:0,cropY:0,cropWidth:1,cropHeight:1}):(t.set({x:r+t.x*s,y:d+t.y*s,width:t.width*s,height:t.height*s}),"text"===t.type?t.set({fontSize:t.fontSize*s}):"figure"===t.type&&t.set({strokeWidth:t.strokeWidth*s}))}))}i||(e.width=t),i||(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])}})));
package/model/store.d.ts CHANGED
@@ -318,6 +318,7 @@ export declare const Store: import("mobx-state-tree").IModelType<{
318
318
  moveElementBottom(id: any): void;
319
319
  }, import("mobx-state-tree")._NotCustomized, {}>>;
320
320
  readonly duration: number;
321
+ readonly _hasCroppedImages: any;
321
322
  find(callback: (element: NodeType) => boolean): NodeType | undefined;
322
323
  getElementById(id: string): NodeType | undefined;
323
324
  } & {
package/model/store.js CHANGED
@@ -1 +1 @@
1
- "use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,i,a){void 0===a&&(a=i);var o=Object.getOwnPropertyDescriptor(t,i);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[i]}}),Object.defineProperty(e,a,o)}:function(e,t,i,a){void 0===a&&(a=i),e[a]=t[i]}),__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 i in e)"default"!==i&&Object.prototype.hasOwnProperty.call(e,i)&&__createBinding(t,e,i);return __setModuleDefault(t,e),t},__rest=this&&this.__rest||function(e,t){var i={};for(var a in e)Object.prototype.hasOwnProperty.call(e,a)&&t.indexOf(a)<0&&(i[a]=e[a]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(a=Object.getOwnPropertySymbols(e);o<a.length;o++)t.indexOf(a[o])<0&&Object.prototype.propertyIsEnumerable.call(e,a[o])&&(i[a[o]]=e[a[o]])}return i},__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=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"),gif_lib_1=require("../utils/gif-lib"),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"),html_1=require("../utils/html"),to_svg_1=require("../utils/to-svg"),page_model_1=require("./page-model"),group_model_1=require("./group-model");function createStore({key:e,showCredit:t}={key:"",showCredit:!1}){const i=exports.Store.create();return(0,validate_key_1.validateKey)(e,t),i}(0,mobx_state_tree_1.setLivelinessChecking)("ignore"),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(page_model_1.Page),fonts:mobx_state_tree_1.types.array(exports.Font),width:1080,height:1080,currentTime:0,isPlaying:!1,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),animatedElementsIds: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 i of e.pages)for(const e of i.children)if(e.id===t)return e})).filter((e=>!!e))},get children(){return e.pages},get selectedShapes(){const t=[];return e.selectedElementsIds.forEach((i=>{for(const a of e.pages)for(const e of a.children)e.id===i&&"group"!==e.type&&t.push(e),"group"===e.type&&e.id===i&&t.push(...e.children)})),t},get activePage(){return e.pages.slice().find((t=>t.id===e._activePageId))||(e.pages.length?e.pages[0]:null)},get duration(){let t=0;return e.pages.forEach((e=>{t+=e.duration})),t},find(t){let i;return(0,group_model_1.forEveryChild)({children:e.pages},(e=>{if(!i&&t(e))return i=e,!0})),i},getElementById:t=>e.find((e=>e.id===t))}))).actions((e=>{let t=0;return{afterCreate(){e.history.canUndo},setCurrentTime(t){e.currentTime=t},play({animatedElementsIds:i=[],currentTime:a=0}={}){e.animatedElementsIds=(0,mobx_state_tree_1.cast)(i),e.currentTime=a,e.isPlaying=!0,t=Date.now(),requestAnimationFrame(e.seek)},seek(){if(!e.isPlaying)return;const i=Date.now(),a=i-t;t=i,e.currentTime+=a;let o=0;for(const t of e.pages){if(e.currentTime>=t.startTime&&e.currentTime<t.startTime+t.duration){e.selectPage(t.id);break}o+=t.duration}e.isPlaying&&e.currentTime<e.duration?requestAnimationFrame(e.seek):e.stop()},stop(){e.isPlaying=!1,e.currentTime=0,e.animatedElementsIds=(0,mobx_state_tree_1.cast)([])}}})).actions((e=>({setUnit({unit:t,dpi:i}){e.unit=t||e.unit,e.dpi=i||e.dpi},setRole(t){e.role=t},addPage(t){const i=page_model_1.Page.create(Object.assign({id:(0,nanoid_1.nanoid)(10)},t));return e.pages.push(i),e._activePageId=i.id,i},selectPage(t){e._activePageId=t},selectElements(t){const i=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)(i)},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,i,a){e.pages.forEach((e=>{e.setSize({width:t,height:i,useMagic:a,softChange:!0})})),e.width=t,e.height=i},setPageZIndex(t,i){const a=e.pages.find((e=>e.id===t));a&&((0,mobx_state_tree_1.detach)(a),e.pages.remove(a),e.pages.splice(i,0,a))},deletePages(t){const i=e.pages.indexOf(e.activePage);t.forEach((t=>{const i=e.pages.find((e=>e.id===t));(0,mobx_state_tree_1.destroy)(i)}));const a=Math.min(e.pages.length-1,i),o=e.pages[a];o&&(e._activePageId=o.id),e.selectedElementsIds=(0,mobx_state_tree_1.cast)(e.selectedElementsIds.filter((t=>e.getElementById(t))))},groupElements(t){const i=t.map((t=>e.getElementById(t)));i.forEach((e=>{e&&(0,mobx_state_tree_1.detach)(e)}));const a=e.activePage,o={id:(0,nanoid_1.nanoid)(10),children:i,type:"group"};return a.children.push(o),e.selectedElementsIds=(0,mobx_state_tree_1.cast)([o.id]),o},ungroupElements(t){const i=t.map((t=>e.getElementById(t))),a=[];i.forEach((e=>{if(e&&"group"===e.type){const t=e.page,i=t.children.indexOf(e);e.children.forEach((e=>{a.push(e.id)})),e.children.forEach((e=>{(0,mobx_state_tree_1.detach)(e),t.children.push(e)})),t.children.splice(i,1)}})),e.selectedElementsIds=(0,mobx_state_tree_1.cast)(a)},deleteElements(t){t.forEach((t=>{e.pages.forEach((e=>{const i=e.children.find((e=>e.id===t));i&&(0,mobx_state_tree_1.destroy)(i)}))})),e.selectedElementsIds=(0,mobx_state_tree_1.cast)(e.selectedElementsIds.filter((t=>e.getElementById(t))))},on(t,i){if("change"===t){let t=e.toJSON();return(0,mobx_state_tree_1.onSnapshot)(e,(a=>{const o=e.toJSON();!(0,deep_equal_1.deepEqual)(t,o)&&(t=o,i(o))}))}},async _toCanvas({pixelRatio:t,ignoreBackground:i,pageId:a,mimeType:o,includeBleed:n}={}){var s;const r=t||1;a=a||(null===(s=e.pages[0])||void 0===s?void 0:s.id);const l=e.pages.find((e=>e.id===a));if(!l)throw new Error(`No page for export with id ${a}`);null==l||l.set({_exporting:!0});const d=await(0,wait_1.waitTillAvailable)((()=>konva_1.default.stages.find((e=>e.getAttr("pageId")===a))));if(!d)throw new Error(`Export is failed. Can not find stage for page ${a}. Looks like <Workspace /> component is not mounted, but it is required in order to process the export.`);const c=!!d.findOne(".page-container");await e.waitLoading();const p=d.findOne(".page-container");if(!p)throw new Error(`Export is failed. Can't find page container. ${c}`);d.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 g=p.findOne(".page-background-group"),u=g.clip();g.clip({x:null,y:null,width:null,height:null});const _=p.findOne(".elements-container"),m=_.clip();_.clip({x:null,y:null,width:null,height:null});const f=p.find((e=>e.getAttr("hideInExport")));f.forEach((e=>{e.setAttr("oldVisible",e.visible()),e.hide()}));const h=p.find((e=>!e.visible()&&e.getAttr("editModeEnabled")));h.forEach((e=>{e.setAttr("oldVisible",e.visible()),e.show()})),i&&p.find(".page-background").forEach((e=>e.hide()));const b=n?l.bleed:0;let y=b;!e.bleedVisible&&n||(e.bleedVisible||n?e.bleedVisible&&n?y=0:e.bleedVisible&&!n&&(y=-l.bleed):y=0);const x=document.createElement("canvas");x.width=Math.round((l.computedWidth+2*b)*r),x.height=Math.round((l.computedHeight+2*b)*r);const w=x.getContext("2d");"image/jpeg"===o&&(w.fillStyle="white",w.fillRect(0,0,x.width,x.height));const v=p.scale();p.scale({x:1,y:1});const E=p.toCanvas({x:p.x()-y,y:p.y()-y,width:l.computedWidth+2*b,height:l.computedHeight+2*b,pixelRatio:r});return p.scale(v),w.drawImage(E,0,0,x.width,x.height),konva_1.default.Util.releaseCanvas(E),i&&p.find(".page-background").forEach((e=>e.show())),f.forEach((e=>{e.visible(e.getAttr("oldVisible"))})),h.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))),d.find("Transformer").forEach((e=>e.visible(!0))),p.find(".highlighter").forEach((e=>e.visible(!0))),g.clip(u),_.clip(m),null==l||l.set({_exporting:!1}),x},async toDataURL({pixelRatio:t,ignoreBackground:i,pageId:a,mimeType:o,includeBleed:n,quality:s}={}){const r=await e._toCanvas({pixelRatio:t,ignoreBackground:i,pageId:a,mimeType:o,includeBleed:n}),l=r.toDataURL(o,s);return konva_1.default.Util.releaseCanvas(r),l},async toBlob({pixelRatio:t,ignoreBackground:i,pageId:a,mimeType:o,includeBleed:n,quality:s}={}){const r=await e._toCanvas({pixelRatio:t,ignoreBackground:i,pageId:a,mimeType:o,includeBleed:n}),l=new Promise((e=>{r.toBlob(e,o,s)}));return konva_1.default.Util.releaseCanvas(r),l},async saveAsImage(t={}){var{fileName:i}=t,a=__rest(t,["fileName"]);const o=a.mimeType||"image/png",n=o.split("/")[1];(0,download_1.downloadFile)(await e.toDataURL(a),i||"polotno."+n,o)},async _toPDF(t){const i=t.dpi||e.dpi,a=t.parallel||1,o=t.unit||("px"===e.unit?"mm":e.unit),n=t.pixelRatio||1,s=t.pageIds||e.pages.map((e=>e.id)),r=e.pages.filter((e=>s.includes(e.id))),l=await(0,pdf_1.getJsPDF)(),d=e=>(0,unit_1.pxToUnit)({px:e,unit:o,dpi:i}),c=d(t.cropMarkSize||0),p=r[0]||{},g=t.includeBleed?p.bleed:0,u=d(p.computedWidth+2*g)+2*c,_=d(p.computedHeight+2*g)+2*c;var m=new l({unit:o,orientation:u>_?"landscape":"portrait",format:[u,_],compress:!0,putOnlyUsedFonts:!0});m.deletePage(1);const f=e._elementsPixelRatio;e.setElementsPixelRatio(n),await new Promise((e=>setTimeout(e)));const h=((e,t)=>{for(var i=[],a=0;a<e.length;a+=t)i.push(e.slice(a,a+t));return i})(r,a);for(const i of h){const a=i.map((async i=>{const a=t.includeBleed?i.bleed:0,o=d(i.computedWidth+2*a)+2*c,s=d(i.computedHeight+2*a)+2*c;let r=0,l=n;for(;r<10;){r+=1,2===r&&console.error("Polotno can not export PDF with current settings. Quality is automatically reduced.");const a=await e.toDataURL(Object.assign(Object.assign({},t),{pageId:i.id,pixelRatio:l}));if(a.length>20)return{url:a,width:o,height:s};l*=.8}}));(await Promise.all(a)).forEach((({url:e,width:t,height:i})=>{m.addPage([t,i],t>i?"landscape":"portrait"),c&&(m.setLineWidth(d(1)),m.line(2*c,0,2*c,c),m.line(0,2*c,c,2*c),m.line(t-2*c,0,t-2*c,c),m.line(t,2*c,t-c,2*c),m.line(0,i-2*c,c,i-2*c),m.line(2*c,i,2*c,i-c),m.line(t,i-2*c,t-c,i-2*c),m.line(t-2*c,i,t-2*c,i-c)),m.addImage(e,c,c,t-2*c,i-2*c,void 0,"FAST")}))}return e.setElementsPixelRatio(f),m},toPDFDataURL:async t=>(await e._toPDF(Object.assign({mimeType:"image/jpeg"},t))).output("datauristring"),async toGIFDataURL(t={}){const i=t.pixelRatio||1,a=await(0,gif_lib_1.createGIF)({width:e.width*i,height:e.height*i}),o=1e3/(t.fps||10),n=e.duration/o;for(let t=0;t<n-1;t++){const n=t*o||1;e.setCurrentTime(n);let s=0,r="";for(const t of e.pages)if(s+=t.duration,t.set({_rendering:s>n}),s>n){r=t.id;break}const l=await e._toCanvas({pixelRatio:i,pageId:r});a.addFrame(l.getContext("2d"),{delay:o,copy:!0})}for(const t of e.pages)t.set({_rendering:!1});e.stop(),a.render();return new Promise((e=>{a.on("finished",(function(t){!function(e,t){var i=new FileReader;i.onload=function(e){t(e.target.result)},i.readAsDataURL(e)}(t,e)}))}))},async saveAsGIF(t={}){var{fileName:i}=t,a=__rest(t,["fileName"]);const o=await e.toGIFDataURL(a);(0,download_1.downloadFile)(o,i||"polotno.gif")},async toHTML(){const t=e.toJSON();return(0,html_1.jsonToHTML)({json:t})},async saveAsHTML({fileName:t}={}){const i=await e.toHTML(),a="data:text/html;base64,"+window.btoa(unescape(encodeURIComponent(i)));(0,download_1.downloadFile)(a,t||"polotno.html")},async toSVG(){const t=e.toJSON();return(0,to_svg_1.jsonToSVG)({json:t})},async saveAsSVG({fileName:t}={}){const i=await e.toSVG(),a="data:text/svg;base64,"+window.btoa(unescape(encodeURIComponent(i)));(0,download_1.downloadFile)(a,t||"polotno.svg")},async saveAsPDF(t={}){var{fileName:i}=t,a=__rest(t,["fileName"]);(await e._toPDF(Object.assign({mimeType:"image/jpeg"},a))).save(i||"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,i=!1){var a;const o=Object.assign({},t),n=e.pages.indexOf(e.activePage);let s=null===(a=o.pages[n]||o.pages[0])||void 0===a?void 0:a.id;o._activePageId=s;const r=Object.assign({},(0,mobx_state_tree_1.getSnapshot)(e));Object.assign(r,o),r.history=i?e.history.toJSON():{history:[],undoIdx:-1},(0,mobx_state_tree_1.applySnapshot)(e,r)},clear({keepHistory:t=!1}={}){const i=e.pages.map((e=>e.id));e.deletePages(i),t||e.history.clear()},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 i=e.fonts.find((e=>e.fontFamily===t))||fonts.globalFonts.find((e=>e.fontFamily===t));i?fonts.injectCustomFont(i):fonts.injectGoogleFont(t),await fonts.loadFont(t)},validate:e=>exports.Store.validate(e,[{path:"",type:exports.Store}]).map((e=>({path:"store"+e.context.map((e=>e.path)).join("."),message:e.message})))}))),exports.createStore=createStore,exports.default=createStore;
1
+ "use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,a,i){void 0===i&&(i=a);var o=Object.getOwnPropertyDescriptor(t,a);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[a]}}),Object.defineProperty(e,i,o)}:function(e,t,a,i){void 0===i&&(i=a),e[i]=t[a]}),__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 a in e)"default"!==a&&Object.prototype.hasOwnProperty.call(e,a)&&__createBinding(t,e,a);return __setModuleDefault(t,e),t},__rest=this&&this.__rest||function(e,t){var a={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(a[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++)t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(a[i[o]]=e[i[o]])}return a},__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=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"),gif_lib_1=require("../utils/gif-lib"),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"),html_1=require("../utils/html"),to_svg_1=require("../utils/to-svg"),page_model_1=require("./page-model"),group_model_1=require("./group-model");function createStore({key:e,showCredit:t}={key:"",showCredit:!1}){const a=exports.Store.create();return(0,validate_key_1.validateKey)(e,t),a}(0,mobx_state_tree_1.setLivelinessChecking)("ignore"),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(page_model_1.Page),fonts:mobx_state_tree_1.types.array(exports.Font),width:1080,height:1080,currentTime:0,isPlaying:!1,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),animatedElementsIds: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 a of e.pages)for(const e of a.children)if(e.id===t)return e})).filter((e=>!!e))},get children(){return e.pages},get selectedShapes(){const t=[];return e.selectedElementsIds.forEach((a=>{for(const i of e.pages)for(const e of i.children)e.id===a&&"group"!==e.type&&t.push(e),"group"===e.type&&e.id===a&&t.push(...e.children)})),t},get activePage(){return e.pages.slice().find((t=>t.id===e._activePageId))||(e.pages.length?e.pages[0]:null)},get duration(){let t=0;return e.pages.forEach((e=>{t+=e.duration})),t},get _hasCroppedImages(){return e.find((e=>"image"===e.type&&e._cropModeEnabled))},find(t){let a;return(0,group_model_1.forEveryChild)({children:e.pages},(e=>{if(!a&&t(e))return a=e,!0})),a},getElementById:t=>e.find((e=>e.id===t))}))).actions((e=>{let t=0;return{afterCreate(){e.history.canUndo},setCurrentTime(t){e.currentTime=t},play({animatedElementsIds:a=[],currentTime:i=0}={}){e.animatedElementsIds=(0,mobx_state_tree_1.cast)(a),e.currentTime=i,e.isPlaying=!0,t=Date.now(),requestAnimationFrame(e.seek)},seek(){if(!e.isPlaying)return;const a=Date.now(),i=a-t;t=a,e.currentTime+=i;let o=0;for(const t of e.pages){if(e.currentTime>=t.startTime&&e.currentTime<t.startTime+t.duration){e.selectPage(t.id);break}o+=t.duration}e.isPlaying&&e.currentTime<e.duration?requestAnimationFrame(e.seek):e.stop()},stop(){e.isPlaying=!1,e.currentTime=0,e.animatedElementsIds=(0,mobx_state_tree_1.cast)([])}}})).actions((e=>({setUnit({unit:t,dpi:a}){e.unit=t||e.unit,e.dpi=a||e.dpi},setRole(t){e.role=t},addPage(t){const a=page_model_1.Page.create(Object.assign({id:(0,nanoid_1.nanoid)(10)},t));return e.pages.push(a),e._activePageId=a.id,a},selectPage(t){e._activePageId=t},selectElements(t){const a=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)(a)},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,a,i){e.pages.forEach((e=>{e.setSize({width:t,height:a,useMagic:i,softChange:!0})})),e.width=t,e.height=a},setPageZIndex(t,a){const i=e.pages.find((e=>e.id===t));i&&((0,mobx_state_tree_1.detach)(i),e.pages.remove(i),e.pages.splice(a,0,i))},deletePages(t){const a=e.pages.indexOf(e.activePage);t.forEach((t=>{const a=e.pages.find((e=>e.id===t));(0,mobx_state_tree_1.destroy)(a)}));const i=Math.min(e.pages.length-1,a),o=e.pages[i];o&&(e._activePageId=o.id),e.selectedElementsIds=(0,mobx_state_tree_1.cast)(e.selectedElementsIds.filter((t=>e.getElementById(t))))},groupElements(t){const a=t.map((t=>e.getElementById(t)));a.forEach((e=>{e&&(0,mobx_state_tree_1.detach)(e)}));const i=e.activePage,o={id:(0,nanoid_1.nanoid)(10),children:a,type:"group"};return i.children.push(o),e.selectedElementsIds=(0,mobx_state_tree_1.cast)([o.id]),o},ungroupElements(t){const a=t.map((t=>e.getElementById(t))),i=[];a.forEach((e=>{if(e&&"group"===e.type){const t=e.page,a=t.children.indexOf(e);e.children.forEach((e=>{i.push(e.id)})),e.children.forEach((e=>{(0,mobx_state_tree_1.detach)(e),t.children.push(e)})),t.children.splice(a,1)}})),e.selectedElementsIds=(0,mobx_state_tree_1.cast)(i)},deleteElements(t){t.forEach((t=>{e.pages.forEach((e=>{const a=e.children.find((e=>e.id===t));a&&(0,mobx_state_tree_1.destroy)(a)}))})),e.selectedElementsIds=(0,mobx_state_tree_1.cast)(e.selectedElementsIds.filter((t=>e.getElementById(t))))},on(t,a){if("change"===t){let t=e.toJSON();return(0,mobx_state_tree_1.onSnapshot)(e,(i=>{const o=e.toJSON();!(0,deep_equal_1.deepEqual)(t,o)&&(t=o,a(o))}))}},async _toCanvas({pixelRatio:t,ignoreBackground:a,pageId:i,mimeType:o,includeBleed:n}={}){var s;const r=t||1;i=i||(null===(s=e.pages[0])||void 0===s?void 0:s.id);const l=e.pages.find((e=>e.id===i));if(!l)throw new Error(`No page for export with id ${i}`);null==l||l.set({_exporting:!0});const d=await(0,wait_1.waitTillAvailable)((()=>konva_1.default.stages.find((e=>e.getAttr("pageId")===i))));if(!d)throw new Error(`Export is failed. Can not find stage for page ${i}. Looks like <Workspace /> component is not mounted, but it is required in order to process the export.`);const c=!!d.findOne(".page-container");await e.waitLoading();const p=d.findOne(".page-container");if(!p)throw new Error(`Export is failed. Can't find page container. ${c}`);d.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 g=p.findOne(".page-background-group"),u=g.clip();g.clip({x:null,y:null,width:null,height:null});const _=p.findOne(".elements-container"),m=_.clip();_.clip({x:null,y:null,width:null,height:null});const f=p.find((e=>e.getAttr("hideInExport")));f.forEach((e=>{e.setAttr("oldVisible",e.visible()),e.hide()}));const h=p.find((e=>!e.visible()&&e.getAttr("editModeEnabled")));h.forEach((e=>{e.setAttr("oldVisible",e.visible()),e.show()})),a&&p.find(".page-background").forEach((e=>e.hide()));const b=n?l.bleed:0;let y=b;!e.bleedVisible&&n||(e.bleedVisible||n?e.bleedVisible&&n?y=0:e.bleedVisible&&!n&&(y=-l.bleed):y=0);const x=document.createElement("canvas");x.width=Math.round((l.computedWidth+2*b)*r),x.height=Math.round((l.computedHeight+2*b)*r);const w=x.getContext("2d");"image/jpeg"===o&&(w.fillStyle="white",w.fillRect(0,0,x.width,x.height));const v=p.scale();p.scale({x:1,y:1});const E=p.toCanvas({x:p.x()-y,y:p.y()-y,width:l.computedWidth+2*b,height:l.computedHeight+2*b,pixelRatio:r});return p.scale(v),w.drawImage(E,0,0,x.width,x.height),konva_1.default.Util.releaseCanvas(E),a&&p.find(".page-background").forEach((e=>e.show())),f.forEach((e=>{e.visible(e.getAttr("oldVisible"))})),h.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))),d.find("Transformer").forEach((e=>e.visible(!0))),p.find(".highlighter").forEach((e=>e.visible(!0))),g.clip(u),_.clip(m),null==l||l.set({_exporting:!1}),x},async toDataURL({pixelRatio:t,ignoreBackground:a,pageId:i,mimeType:o,includeBleed:n,quality:s}={}){const r=await e._toCanvas({pixelRatio:t,ignoreBackground:a,pageId:i,mimeType:o,includeBleed:n}),l=r.toDataURL(o,s);return konva_1.default.Util.releaseCanvas(r),l},async toBlob({pixelRatio:t,ignoreBackground:a,pageId:i,mimeType:o,includeBleed:n,quality:s}={}){const r=await e._toCanvas({pixelRatio:t,ignoreBackground:a,pageId:i,mimeType:o,includeBleed:n}),l=new Promise((e=>{r.toBlob(e,o,s)}));return konva_1.default.Util.releaseCanvas(r),l},async saveAsImage(t={}){var{fileName:a}=t,i=__rest(t,["fileName"]);const o=i.mimeType||"image/png",n=o.split("/")[1];(0,download_1.downloadFile)(await e.toDataURL(i),a||"polotno."+n,o)},async _toPDF(t){const a=t.dpi||e.dpi,i=t.parallel||1,o=t.unit||("px"===e.unit?"mm":e.unit),n=t.pixelRatio||1,s=t.pageIds||e.pages.map((e=>e.id)),r=e.pages.filter((e=>s.includes(e.id))),l=await(0,pdf_1.getJsPDF)(),d=e=>(0,unit_1.pxToUnit)({px:e,unit:o,dpi:a}),c=d(t.cropMarkSize||0),p=r[0]||{},g=t.includeBleed?p.bleed:0,u=d(p.computedWidth+2*g)+2*c,_=d(p.computedHeight+2*g)+2*c;var m=new l({unit:o,orientation:u>_?"landscape":"portrait",format:[u,_],compress:!0,putOnlyUsedFonts:!0});m.deletePage(1);const f=e._elementsPixelRatio;e.setElementsPixelRatio(n),await new Promise((e=>setTimeout(e)));const h=((e,t)=>{for(var a=[],i=0;i<e.length;i+=t)a.push(e.slice(i,i+t));return a})(r,i);for(const a of h){const i=a.map((async a=>{const i=t.includeBleed?a.bleed:0,o=d(a.computedWidth+2*i)+2*c,s=d(a.computedHeight+2*i)+2*c;let r=0,l=n;for(;r<10;){r+=1,2===r&&console.error("Polotno can not export PDF with current settings. Quality is automatically reduced.");const i=await e.toDataURL(Object.assign(Object.assign({},t),{pageId:a.id,pixelRatio:l}));if(i.length>20)return{url:i,width:o,height:s};l*=.8}}));(await Promise.all(i)).forEach((({url:e,width:t,height:a})=>{m.addPage([t,a],t>a?"landscape":"portrait"),c&&(m.setLineWidth(d(1)),m.line(2*c,0,2*c,c),m.line(0,2*c,c,2*c),m.line(t-2*c,0,t-2*c,c),m.line(t,2*c,t-c,2*c),m.line(0,a-2*c,c,a-2*c),m.line(2*c,a,2*c,a-c),m.line(t,a-2*c,t-c,a-2*c),m.line(t-2*c,a,t-2*c,a-c)),m.addImage(e,c,c,t-2*c,a-2*c,void 0,"FAST")}))}return e.setElementsPixelRatio(f),m},toPDFDataURL:async t=>(await e._toPDF(Object.assign({mimeType:"image/jpeg"},t))).output("datauristring"),async toGIFDataURL(t={}){const a=t.pixelRatio||1,i=await(0,gif_lib_1.createGIF)({width:e.width*a,height:e.height*a}),o=1e3/(t.fps||10),n=e.duration/o;for(let t=0;t<n-1;t++){const n=t*o||1;e.setCurrentTime(n);let s=0,r="";for(const t of e.pages)if(s+=t.duration,t.set({_rendering:s>n}),s>n){r=t.id;break}const l=await e._toCanvas({pixelRatio:a,pageId:r});i.addFrame(l.getContext("2d"),{delay:o,copy:!0})}for(const t of e.pages)t.set({_rendering:!1});e.stop(),i.render();return new Promise((e=>{i.on("finished",(function(t){!function(e,t){var a=new FileReader;a.onload=function(e){t(e.target.result)},a.readAsDataURL(e)}(t,e)}))}))},async saveAsGIF(t={}){var{fileName:a}=t,i=__rest(t,["fileName"]);const o=await e.toGIFDataURL(i);(0,download_1.downloadFile)(o,a||"polotno.gif")},async toHTML(){const t=e.toJSON();return(0,html_1.jsonToHTML)({json:t})},async saveAsHTML({fileName:t}={}){const a=await e.toHTML(),i="data:text/html;base64,"+window.btoa(unescape(encodeURIComponent(a)));(0,download_1.downloadFile)(i,t||"polotno.html")},async toSVG(){const t=e.toJSON();return(0,to_svg_1.jsonToSVG)({json:t})},async saveAsSVG({fileName:t}={}){const a=await e.toSVG(),i="data:text/svg;base64,"+window.btoa(unescape(encodeURIComponent(a)));(0,download_1.downloadFile)(i,t||"polotno.svg")},async saveAsPDF(t={}){var{fileName:a}=t,i=__rest(t,["fileName"]);(await e._toPDF(Object.assign({mimeType:"image/jpeg"},i))).save(a||"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,a=!1){var i;const o=Object.assign({},t),n=e.pages.indexOf(e.activePage);let s=null===(i=o.pages[n]||o.pages[0])||void 0===i?void 0:i.id;o._activePageId=s;const r=Object.assign({},(0,mobx_state_tree_1.getSnapshot)(e));Object.assign(r,o),r.history=a?e.history.toJSON():{history:[],undoIdx:-1},(0,mobx_state_tree_1.applySnapshot)(e,r)},clear({keepHistory:t=!1}={}){const a=e.pages.map((e=>e.id));e.deletePages(a),t||e.history.clear()},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 a=e.fonts.find((e=>e.fontFamily===t))||fonts.globalFonts.find((e=>e.fontFamily===t));a?fonts.injectCustomFont(a):fonts.injectGoogleFont(t),await fonts.loadFont(t)},validate:e=>exports.Store.validate(e,[{path:"",type:exports.Store}]).map((e=>({path:"store"+e.context.map((e=>e.path)).join("."),message:e.message})))}))),exports.createStore=createStore,exports.default=createStore;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polotno",
3
- "version": "2.4.4",
3
+ "version": "2.4.6",
4
4
  "description": "Design Editor Framework",
5
5
  "author": "Anton Lavrenov",
6
6
  "keywords": [
@@ -10,13 +10,13 @@
10
10
  "design editor"
11
11
  ],
12
12
  "dependencies": {
13
- "@blueprintjs/core": "5.8.0",
14
- "@blueprintjs/select": "5.0.21",
13
+ "@blueprintjs/core": "5.8.2",
14
+ "@blueprintjs/select": "5.0.23",
15
15
  "@meronex/icons": "^4.0.0",
16
16
  "fast-json-patch": "^3.1.1",
17
17
  "functions-have-names": "^1.2.3",
18
18
  "gradient-parser": "^1.0.2",
19
- "konva": "^9.3.0",
19
+ "konva": "^9.3.1",
20
20
  "mobx": "6.12.0",
21
21
  "mobx-react-lite": "^4.0.5",
22
22
  "mobx-state-tree": "5.4.0",
@@ -61,7 +61,7 @@
61
61
  ],
62
62
  "devDependencies": {
63
63
  "@types/jest": "^29",
64
- "@types/react": "^18.2.47",
64
+ "@types/react": "^18.2.48",
65
65
  "@types/sanitize-html": "^2.9.5",
66
66
  "@vitest/browser": "^0.34.6",
67
67
  "assert": "^2.1.0",