polotno 0.43.11 → 0.43.14
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/html-element.js +1 -1
- package/model/store.js +1 -1
- package/package.json +10 -10
- package/polotno.bundle.js +6 -6
- package/toolbar/toolbar.d.ts +25 -0
- package/utils/html2canvas.d.ts +2 -1
- package/utils/html2canvas.js +1 -1
package/canvas/html-element.js
CHANGED
|
@@ -16,4 +16,4 @@
|
|
|
16
16
|
.ql-direction-rtl {
|
|
17
17
|
direction: rtl;
|
|
18
18
|
}
|
|
19
|
-
`,RichEditor=({html:e,onBlur:t,onChange:r,element:i})=>{const a=react_1.default.useRef(null);return react_1.default.useEffect((()=>{if(!a.current)return;const e=new quill_1.default(a.current,{toolbar:!1,keyboard:!1,clipboard:{matchVisual:!1}});return exports.quillRef.editor.instance=e,window.__polotnoQuill=e,e.on("text-change",(()=>{e.getSelection()&&(exports.quillRef.currentFormat=e.getFormat()),setTimeout((()=>{var e;r(null===(e=a.current)||void 0===e?void 0:e.childNodes[0].innerHTML)}),10)})),e.setSelection(0,0,"api"),e.on("selection-change",(r=>{r||t(),r&&(exports.quillRef.currentFormat=e.getFormat())})),a.current.childNodes[0].addEventListener("blur",(e=>{var r;(null===(r=e.relatedTarget)||void 0===r?void 0:r.classList.contains("ql-clipboard"))||t()})),()=>{exports.quillRef.editor.instance=null,exports.quillRef.currentFormat={},delete window.__polotnoQuill}}),[]),react_1.default.useEffect((()=>{var t;const r=exports.quillRef.editor.instance,n=r.getSelection();if(!((null===(t=a.current)||void 0===t?void 0:t.childNodes[0].innerHTML)===i.text)){var l=r.clipboard.convert("<div class='ql-editor' style='outline: none;'>"+e+"<p><br></p></div>");r.setContents(l),r.history.clear(),n&&(r.setSelection(n.index,n.length),exports.quillRef.currentFormat=r.getFormat())}}),[i.text]),react_1.default.createElement(RichTextContainer,{ref:a,style:{fontSize:i.fontSize,color:i.fill,width:i.width,fontFamily:i.fontFamily,lineHeight:i.lineHeight,letterSpacing:i.letterSpacing+"rem",textAlign:i.align},dir:(0,text_element_1.getDir)((0,text_1.removeTags)(i.text))})};function useHtmlSize(e,t,r){return react_1.default.useMemo((()=>(0,html2canvas_1.detectSize)(e)),[e,t.width,r])}function getHtml(e,t){return`<div style="white-space: pre-wrap; width: ${e.width||100}px; color: ${e.fill}; font-size: ${e.fontSize}px; font-family: '${t}'; text-decoration: ${e.textDecoration}; text-align: ${e.align}; line-height: ${e.lineHeight}; letter-spacing: ${e.letterSpacing}rem; font-style: ${e.fontStyle}; font-weight: ${e.fontWeight}; -webkit-text-stroke: ${e.strokeWidth}px ${e.stroke};" contentEditable dir="${(0,text_element_1.getDir)((0,text_1.removeTags)(e.text))}">${e.text}</div>`}const findFitFontSize=e=>{let t=e.fontSize;for(let r=1;r<50;r++){const r=getHtml(Object.assign(Object.assign({},e.toJSON()),{fontSize:t}),e.fontFamily),{height:i}=(0,html2canvas_1.detectSize)(r);if(!(e.height&&i>e.height))break;t-=.5}return t};exports.HTMLElement=(0,mobx_react_lite_1.observer)((({element:e,store:t})=>{const r=react_1.default.useRef(null),[i,a]=react_1.default.useState(),[n,l]=react_1.default.useState(!1),[o,s]=react_1.default.useState(!0),[c,u]=react_1.default.useState(!1),[d,f]=react_1.default.useState(!1),h=react_1.default.useRef(e.height),_=t.selectedElements.indexOf(e)>=0,g=e.fontSize/4,{textVerticalResizeEnabled:m}=flags_1.flags,[x]=(0,text_element_1.useFontLoader)(t,e.fontFamily);(0,use_fadein_1.useFadeIn)(r);const p=x?e.fontFamily:"Arial",v=getHtml(e,p),{width:y,height:b}=useHtmlSize(v,e,x);react_1.default.useEffect((()=>{if(!x)return;const{forceTextFitEnabled:t}=flags_1.flags;if(e.height&&t&&!n){const t=findFitFontSize(e);t!==e.fontSize?e.set({fontSize:t}):e.height!==b&&e.set({height:b})}else m&&e.height<b&&e.set({height:b}),m||e.height===b||e.set({height:b})}));const w=react_1.default.useRef(0);react_1.default.useLayoutEffect((()=>{if(n||c)return;w.current++;const
|
|
19
|
+
`,RichEditor=({html:e,onBlur:t,onChange:r,element:i})=>{const a=react_1.default.useRef(null);return react_1.default.useEffect((()=>{if(!a.current)return;const e=new quill_1.default(a.current,{toolbar:!1,keyboard:!1,clipboard:{matchVisual:!1}});return exports.quillRef.editor.instance=e,window.__polotnoQuill=e,e.on("text-change",(()=>{e.getSelection()&&(exports.quillRef.currentFormat=e.getFormat()),setTimeout((()=>{var e;r(null===(e=a.current)||void 0===e?void 0:e.childNodes[0].innerHTML)}),10)})),e.setSelection(0,0,"api"),e.on("selection-change",(r=>{r||t(),r&&(exports.quillRef.currentFormat=e.getFormat())})),a.current.childNodes[0].addEventListener("blur",(e=>{var r;(null===(r=e.relatedTarget)||void 0===r?void 0:r.classList.contains("ql-clipboard"))||t()})),()=>{exports.quillRef.editor.instance=null,exports.quillRef.currentFormat={},delete window.__polotnoQuill}}),[]),react_1.default.useEffect((()=>{var t;const r=exports.quillRef.editor.instance,n=r.getSelection();if(!((null===(t=a.current)||void 0===t?void 0:t.childNodes[0].innerHTML)===i.text)){var l=r.clipboard.convert("<div class='ql-editor' style='outline: none;'>"+e+"<p><br></p></div>");r.setContents(l),r.history.clear(),n&&(r.setSelection(n.index,n.length),exports.quillRef.currentFormat=r.getFormat())}}),[i.text]),react_1.default.createElement(RichTextContainer,{ref:a,style:{fontSize:i.fontSize,color:i.fill,width:i.width,fontFamily:i.fontFamily,lineHeight:i.lineHeight,letterSpacing:i.letterSpacing+"rem",textAlign:i.align},dir:(0,text_element_1.getDir)((0,text_1.removeTags)(i.text))})};function useHtmlSize(e,t,r){return react_1.default.useMemo((()=>(0,html2canvas_1.detectSize)(e)),[e,t.width,r])}function getHtml(e,t){return`<div style="white-space: pre-wrap; width: ${e.width||100}px; color: ${e.fill}; font-size: ${e.fontSize}px; font-family: '${t}'; text-decoration: ${e.textDecoration}; text-align: ${e.align}; line-height: ${e.lineHeight}; letter-spacing: ${e.letterSpacing}rem; font-style: ${e.fontStyle}; font-weight: ${e.fontWeight}; -webkit-text-stroke: ${e.strokeWidth}px ${e.stroke};" contentEditable dir="${(0,text_element_1.getDir)((0,text_1.removeTags)(e.text))}">${e.text}</div>`}const findFitFontSize=e=>{let t=e.fontSize;for(let r=1;r<50;r++){const r=getHtml(Object.assign(Object.assign({},e.toJSON()),{fontSize:t}),e.fontFamily),{height:i}=(0,html2canvas_1.detectSize)(r);if(!(e.height&&i>e.height))break;t-=.5}return t};exports.HTMLElement=(0,mobx_react_lite_1.observer)((({element:e,store:t})=>{const r=react_1.default.useRef(null),[i,a]=react_1.default.useState(),[n,l]=react_1.default.useState(!1),[o,s]=react_1.default.useState(!0),[c,u]=react_1.default.useState(!1),[d,f]=react_1.default.useState(!1),h=react_1.default.useRef(e.height),_=t.selectedElements.indexOf(e)>=0,g=e.fontSize/4,{textVerticalResizeEnabled:m}=flags_1.flags,[x]=(0,text_element_1.useFontLoader)(t,e.fontFamily);(0,use_fadein_1.useFadeIn)(r);const p=x?e.fontFamily:"Arial",v=getHtml(e,p),{width:y,height:b}=useHtmlSize(v,e,x);react_1.default.useEffect((()=>{if(!x)return;const{forceTextFitEnabled:t}=flags_1.flags;if(e.height&&t&&!n){const t=findFitFontSize(e);t!==e.fontSize?e.set({fontSize:t}):e.height!==b&&e.set({height:b})}else m&&e.height<b&&e.set({height:b}),m||e.height===b||e.set({height:b})}));const w=react_1.default.useRef(0);react_1.default.useLayoutEffect((()=>{if(n||c)return;w.current++;const r=w.current,i=(0,loader_1.incrementLoader)(`text ${e.id}`);s(!0),async function(){await new Promise((e=>setTimeout(e)));const n=await(0,html2canvas_1.htmlToCanvas)({html:v,width:e.width||1,height:e.height||b||1,fontFamily:p,padding:g,font:t.fonts.find((e=>e.fontFamily===p))||t.globalFonts.find((e=>e.fontFamily===p))});r===w.current?(a(n),s(!1),konva_1.default.Util.requestAnimFrame(i)):i()}()}),[v,n,b,c,p]);const S=n||o;react_1.default.useLayoutEffect((()=>mobx.autorun((()=>{const t=r.current;(0,apply_filters_1.applyFilter)(t,e)}))),[i,S]);let E=0;return"middle"===e.verticalAlign&&(E=(e.height-b)/2),"bottom"===e.verticalAlign&&(E=e.height-b),react_1.default.createElement(react_1.default.Fragment,null,react_1.default.createElement(react_konva_1.Rect,{ref:r,name:"element",x:e.x,y:e.y,listening:e.selectable,rotation:e.rotation,width:e.width,height:e.height,visible:!S,draggable:!e.locked,opacity:c?0:e.opacity,shadowEnabled:e.shadowEnabled,shadowBlur:e.shadowBlur,hideInExport:!e.showInExport,onMouseEnter:()=>{f(!0)},onMouseLeave:()=>{f(!1)},onDragStart:()=>{t.history.startTransaction()},onDragMove:t=>{setTimeout((()=>{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()},id:e.id,onDblClick:()=>{e.locked||u(!0)},onDblTap:()=>{e.locked||u(!0)},onTransformStart:t=>{l(!0),h.current=e.height},onTransform:t=>{var r;const i=t.target,a=(null===(r=i.getStage())||void 0===r?void 0:r.findOne("Transformer")).getActiveAnchor(),n="middle-left"===a||"middle-right"===a,l="top-center"===a||"bottom-center"===a,o=i.scaleX();if(n){const t=i.scaleX(),r=Math.max(i.width()*t,e.fontSize);if(i.width(r),i.scaleX(1),flags_1.flags.textVerticalResizeEnabled){const t=Math.max(b,h.current);e.set({height:t})}e.set({width:r,x:i.x()})}else if(l){const r=Math.max(b),a=Math.max(r,t.target.height()*t.target.scaleY());i.scaleY(1),e.set({x:i.x(),y:i.y(),height:a,rotation:i.rotation()})}else i.scaleX(1),i.scaleY(1),e.set({fontSize:e.fontSize*o,width:i.width()*o,x:i.x(),y:i.y(),rotation:i.rotation()})},onTransformEnd:t=>{l(!1),s(!0);const r=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),e.set({fontSize:e.fontSize*r,width:t.target.width()*r,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation()})}}),react_1.default.createElement(react_konva_1.Image,{ref:r,image:i,x:e.x,y:e.y,offsetX:g,offsetY:g-E,listening:!1,rotation:e.rotation,width:e.width+2*g,height:e.height+2*g,visible:!S,opacity:c?0:e.opacity,shadowEnabled:e.shadowEnabled,shadowBlur:e.shadowBlur,hideInExport:!e.showInExport}),S&&react_1.default.createElement(react_konva_1.Group,{x:e.x,y:e.y,rotation:e.rotation,offsetY:-E},react_1.default.createElement(react_konva_utils_1.Html,{divProps:{style:{pointerEvents:"none"}}},react_1.default.createElement(RichTextContainer,{dangerouslySetInnerHTML:{__html:v},style:{pointerEvents:"none",display:"inline-block"}}))),c&&react_1.default.createElement(react_konva_1.Group,{x:e.x,y:e.y,rotation:e.rotation,offsetY:-E},react_1.default.createElement(react_konva_utils_1.Html,null,react_1.default.createElement(RichEditor,{html:v,element:e,onChange:t=>{e.set({text:t})},onBlur:e=>{u(!1)}}))),(_||d)&&react_1.default.createElement(highlighter_1.Highlighter,{element:e}))}));
|
package/model/store.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,o,s){void 0===s&&(s=o);var n=Object.getOwnPropertyDescriptor(t,o);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[o]}}),Object.defineProperty(e,s,n)}:function(e,t,o,s){void 0===s&&(s=o),e[s]=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 s in e)Object.prototype.hasOwnProperty.call(e,s)&&t.indexOf(s)<0&&(o[s]=e[s]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(s=Object.getOwnPropertySymbols(e);n<s.length;n++)t.indexOf(s[n])<0&&Object.prototype.propertyIsEnumerable.call(e,s[n])&&(o[s[n]]=e[s[n]])}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")),svg_1=require("../utils/svg"),loader_1=require("../utils/loader"),unit_1=require("../utils/unit");(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,visible:!0,locked:!1,blurEnabled:!1,blurRadius:10,brightnessEnabled:!1,brightness:0,sepiaEnabled:!1,grayscaleEnabled:!1,shadowEnabled:!1,shadowBlur:5,shadowOffsetX:0,shadowOffsetY:0,shadowColor:"black",custom:mobx_state_tree_1.types.frozen(),selectable:!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),t})).postProcessSnapshot((e=>{const t=Object.assign({},e),o={};for(var s in t)"_"!==s[0]&&(o[s]=e[s]);return o})).views((e=>({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:"middle",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,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}))).views((e=>({get colors(){return e.__svgString?(0,svg_1.getColors)(e.__svgString):[]},get __finalSrc(){return e.__svgString?(0,svg_1.replaceColors)(e.__svgString,e.colorsReplace):this.src},get __isLoaded(){if(!e.__svgString)return!1;return!(Array.from(e.colorsReplace.keys()).length>0)||e.__finalSrc!==e.src}}))).actions((e=>{let t=()=>{};return{async _loadSVG(){if(!e.src)return;const t=await(0,svg_1.urlToString)(e.src);(0,mobx_state_tree_1.isAlive)(e)&&(0,mobx_state_tree_1.hasParent)(e)&&e.store.history.ignore((()=>{e.set({__svgString:(0,svg_1.fixSize)(t)})}))},async afterCreate(){e._loadSVG();let o=e.src;t=(0,mobx_state_tree_1.onSnapshot)(e,(t=>{t.src===o&&e.__svgString||(e._loadSVG(),o=e.src)}))},beforeDestroy(){t()},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 s in t)"_"!==s[0]&&(o[s]=e[s]);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 s=t.map((t=>({id:t,index:e.children.findIndex((e=>e.id===t))})));s.sort(((e,t)=>t.index-e.index));for(const{index:n}of s){if(-1==n)continue;const s=n<e.children.length-1&&e.children[n+1],i=t.indexOf(null==s?void 0:s.id)>=0;n===e.children.length-1||i||o(n)}},_forEachElementDown(t,o){const s=t.map((t=>({id:t,index:e.children.findIndex((e=>e.id===t))})));s.sort(((e,t)=>e.index-t.index));for(const{index:n}of s){if(-1==n)continue;const s=n>0&&e.children[n-1],i=t.indexOf(null==s?void 0:s.id)>=0;0===n||i||o(n)}return!1}}))).actions((e=>({clone(t){const o=e.toJSON();o.children.forEach((e=>{e.id=(0,nanoid_1.nanoid)(10)}));const s=Object.assign(Object.assign(Object.assign({},o),{id:(0,nanoid_1.nanoid)(10)}),t),n=e.store.addPage(s),i=e.store.pages.indexOf(e);n.setZIndex(i+1),n.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 s=o.create(Object.assign({id:(0,nanoid_1.nanoid)(10)},t));return e.children.push(s),s.selectable&&e.store.selectElements([s.id]),s},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=[],s=[];e.children.forEach((e=>{t.indexOf(e.id)>=0?o.push(e):s.push(e)})),e.children.replace(s.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=[],s=[];e.children.forEach((e=>{t.indexOf(e.id)>=0?o.push(e):s.push(e)})),e.children.replace(o.concat(s))},setElementZIndex(t,o){const s=e.children.find((e=>e.id===t));s&&((0,mobx_state_tree_1.detach)(s),e.children.remove(s),e.children.splice(o,0,s))}}))).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 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=>({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){e.selectedElementsIds=(0,mobx_state_tree_1.cast)(t)},_toggleBleed(t){e._bleedVisible=null!=t?t:!e._bleedVisible},openSidePanel(t){e.openedSidePanel=t},setScale(t){e.scale=t},_setScaleToFit(t){e.scaleToFit=t},setElementsPixelRatio(t){e._elementsPixelRatio=t},setSize(t,o,s){if(s){const s=t/e.width,n=o/e.height;for(const t of e.pages)for(const e of t.children)e.set({x:e.x*s,y:e.y*n}),"text"===e.type?e.set({fontSize:e.fontSize*s,width:Math.max(e.width*s,2)}):"image"===e.type?e.set({width:e.width*s,height:e.height*n}):"svg"===e.type&&e.set({width:Math.max(e.width*s,2),height:Math.max(e.height*s,2)})}e.width=t,e.height=o},setPageZIndex(t,o){const s=e.pages.find((e=>e.id===t));s&&((0,mobx_state_tree_1.detach)(s),e.pages.remove(s),e.pages.splice(o,0,s))},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 s=Math.min(e.pages.length-1,o),n=e.pages[s];n&&(e._activePageId=n.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)([])},on(t,o){if("change"===t){let t=e.toJSON();return(0,mobx_state_tree_1.onSnapshot)(e,(s=>{const n=e.toJSON();JSON.stringify(n)!==JSON.stringify(t)&&(t=n,o(n))}))}},async toDataURL({pixelRatio:t,ignoreBackground:o,pageId:s,mimeType:n,includeBleed:i,quality:a}={}){var r;const l=t||1;s=s||(null===(r=e.pages[0])||void 0===r?void 0:r.id);const d=e.pages.find((e=>e.id===s));if(!d)throw new Error(`No page for export with id ${s}`);null==d||d.set({_exporting:!0}),await new Promise((e=>{setTimeout(e,0)})),await e.waitLoading();const c=konva_1.default.stages.find((e=>e.getAttr("pageId")===s));if(!c)throw new Error(`Export is failed. Can not find stage for page ${s}. 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.findOne(".page-background").shadowEnabled(!1),p.findOne(".page-background").strokeEnabled(!1),p.find(".highlighter").forEach((e=>e.visible(!1)));const _=p.findOne(".page-background-group"),m=_.clip();_.clip({x:null,y:null,width:null,height:null});const g=p.find((e=>e.getAttr("hideInExport")));g.forEach((e=>{e.setAttr("oldVisible",e.visible()),e.hide()})),o&&p.findOne(".page-background").hide();const h=i?d.bleed:0;let u=h;e._bleedVisible&&i?u=0:e._bleedVisible&&!i&&(u=-d.bleed);const f=document.createElement("canvas");f.width=(d.computedWidth+2*h)*l,f.height=(d.computedHeight+2*h)*l;const b=f.getContext("2d");"image/jpeg"===n&&(b.fillStyle="white",b.fillRect(0,0,f.width,f.height));const x=p.toCanvas({x:p.x()-u*p.scaleX(),y:p.y()-u*p.scaleY(),width:(d.computedWidth+2*h)*p.scaleX(),height:(d.computedHeight+2*h)*p.scaleY(),pixelRatio:1/p.scaleX()*l});b.drawImage(x,0,0),console.log("export");const y=f.toDataURL(n,a);return o&&p.findOne(".page-background").show(),g.forEach((e=>{e.visible(e.getAttr("oldVisible"))})),p.findOne(".page-background").shadowEnabled(!0),p.findOne(".page-background").strokeEnabled(!0),c.find("Transformer").forEach((e=>e.visible(!0))),p.find(".highlighter").forEach((e=>e.visible(!0))),_.clip(m),null==d||d.set({_exporting:!1}),y},async saveAsImage(t={}){var{fileName:o}=t,s=__rest(t,["fileName"]);const n=s.mimeType||"image/png",i=n.split("/")[1];(0,download_1.downloadFile)(await e.toDataURL(s),o||"polotno."+i,n)},async _toPDF(t){const o=t.dpi||e.dpi,s=t.parallel||1,n=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:n,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 _=new a({unit:n,orientation:c>p?"landscape":"portrait",format:[c,p],compress:!0});_.deletePage(1);const m=e._elementsPixelRatio;e.setElementsPixelRatio(i),await new Promise((e=>setTimeout(e)));const g=((e,t)=>{for(var o=[],s=0;s<e.length;s+=t)o.push(e.slice(s,s+t));return o})(e.pages,s);for(const o of g){const s=o.map((async o=>{const s=t.includeBleed?o.bleed:0,n=r(o.computedWidth+2*s),a=r(o.computedHeight+2*s);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 s=await e.toDataURL(Object.assign(Object.assign({},t),{pageId:o.id,pixelRatio:d}));if(s.length>20)return{url:s,width:n,height:a};d*=.8}}));(await Promise.all(s)).forEach((({url:e,width:t,height:o})=>{_.addPage([t,o],t>o?"landscape":"portrait"),_.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,s=__rest(t,["fileName"]);(await e._toPDF(Object.assign({mimeType:"image/jpeg"},s))).save(o||"polotno.pdf")},async waitLoading(){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 s;const n=Object.assign({},t);e.pages.forEach((e=>e.children.forEach((e=>(0,mobx_state_tree_1.detach)(e)))));const i=e.pages.indexOf(e.activePage);let a=null===(s=n.pages[i]||n.pages[0])||void 0===s?void 0:s.id;e.pages=(0,mobx_state_tree_1.cast)([]),n._activePageId=a,n.scale=e.scale,n.scaleToFit=e.scaleToFit,n.openedSidePanel=e.openedSidePanel,n._bleedVisible=e._bleedVisible,o&&(n.history=e.history.toJSON()),(0,mobx_state_tree_1.applySnapshot)(e,n)},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,s){void 0===s&&(s=o);var n=Object.getOwnPropertyDescriptor(t,o);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[o]}}),Object.defineProperty(e,s,n)}:function(e,t,o,s){void 0===s&&(s=o),e[s]=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 s in e)Object.prototype.hasOwnProperty.call(e,s)&&t.indexOf(s)<0&&(o[s]=e[s]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(s=Object.getOwnPropertySymbols(e);n<s.length;n++)t.indexOf(s[n])<0&&Object.prototype.propertyIsEnumerable.call(e,s[n])&&(o[s[n]]=e[s[n]])}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")),svg_1=require("../utils/svg"),loader_1=require("../utils/loader"),unit_1=require("../utils/unit");(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,visible:!0,locked:!1,blurEnabled:!1,blurRadius:10,brightnessEnabled:!1,brightness:0,sepiaEnabled:!1,grayscaleEnabled:!1,shadowEnabled:!1,shadowBlur:5,shadowOffsetX:0,shadowOffsetY:0,shadowColor:"black",custom:mobx_state_tree_1.types.frozen(),selectable:!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),t})).postProcessSnapshot((e=>{const t=Object.assign({},e),o={};for(var s in t)"_"!==s[0]&&(o[s]=e[s]);return o})).views((e=>({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:"middle",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,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}))).views((e=>({get colors(){return e.__svgString?(0,svg_1.getColors)(e.__svgString):[]},get __finalSrc(){return e.__svgString?(0,svg_1.replaceColors)(e.__svgString,e.colorsReplace):this.src},get __isLoaded(){if(!e.__svgString)return!1;return!(Array.from(e.colorsReplace.keys()).length>0)||e.__finalSrc!==e.src}}))).actions((e=>{let t=()=>{};return{async _loadSVG(){if(!e.src)return;const t=await(0,svg_1.urlToString)(e.src);(0,mobx_state_tree_1.isAlive)(e)&&(0,mobx_state_tree_1.hasParent)(e)&&e.store.history.ignore((()=>{e.set({__svgString:(0,svg_1.fixSize)(t)})}))},async afterCreate(){e._loadSVG();let o=e.src;t=(0,mobx_state_tree_1.onSnapshot)(e,(t=>{t.src===o&&e.__svgString||(e._loadSVG(),o=e.src)}))},beforeDestroy(){t()},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 s in t)"_"!==s[0]&&(o[s]=e[s]);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 s=t.map((t=>({id:t,index:e.children.findIndex((e=>e.id===t))})));s.sort(((e,t)=>t.index-e.index));for(const{index:n}of s){if(-1==n)continue;const s=n<e.children.length-1&&e.children[n+1],i=t.indexOf(null==s?void 0:s.id)>=0;n===e.children.length-1||i||o(n)}},_forEachElementDown(t,o){const s=t.map((t=>({id:t,index:e.children.findIndex((e=>e.id===t))})));s.sort(((e,t)=>e.index-t.index));for(const{index:n}of s){if(-1==n)continue;const s=n>0&&e.children[n-1],i=t.indexOf(null==s?void 0:s.id)>=0;0===n||i||o(n)}return!1}}))).actions((e=>({clone(t){const o=e.toJSON();o.children.forEach((e=>{e.id=(0,nanoid_1.nanoid)(10)}));const s=Object.assign(Object.assign(Object.assign({},o),{id:(0,nanoid_1.nanoid)(10)}),t),n=e.store.addPage(s),i=e.store.pages.indexOf(e);n.setZIndex(i+1),n.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 s=o.create(Object.assign({id:(0,nanoid_1.nanoid)(10)},t));return e.children.push(s),s.selectable&&e.store.selectElements([s.id]),s},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=[],s=[];e.children.forEach((e=>{t.indexOf(e.id)>=0?o.push(e):s.push(e)})),e.children.replace(s.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=[],s=[];e.children.forEach((e=>{t.indexOf(e.id)>=0?o.push(e):s.push(e)})),e.children.replace(o.concat(s))},setElementZIndex(t,o){const s=e.children.find((e=>e.id===t));s&&((0,mobx_state_tree_1.detach)(s),e.children.remove(s),e.children.splice(o,0,s))}}))).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 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=>({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){e.selectedElementsIds=(0,mobx_state_tree_1.cast)(t)},_toggleBleed(t){e._bleedVisible=null!=t?t:!e._bleedVisible},openSidePanel(t){e.openedSidePanel=t},setScale(t){e.scale=t},_setScaleToFit(t){e.scaleToFit=t},setElementsPixelRatio(t){e._elementsPixelRatio=t},setSize(t,o,s){if(s){const s=t/e.width,n=o/e.height;for(const t of e.pages)for(const e of t.children)e.set({x:e.x*s,y:e.y*n}),"text"===e.type?e.set({fontSize:e.fontSize*s,width:Math.max(e.width*s,2)}):"image"===e.type?e.set({width:e.width*s,height:e.height*n}):"svg"===e.type&&e.set({width:Math.max(e.width*s,2),height:Math.max(e.height*s,2)})}e.width=t,e.height=o},setPageZIndex(t,o){const s=e.pages.find((e=>e.id===t));s&&((0,mobx_state_tree_1.detach)(s),e.pages.remove(s),e.pages.splice(o,0,s))},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 s=Math.min(e.pages.length-1,o),n=e.pages[s];n&&(e._activePageId=n.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)([])},on(t,o){if("change"===t){let t=e.toJSON();return(0,mobx_state_tree_1.onSnapshot)(e,(s=>{const n=e.toJSON();JSON.stringify(n)!==JSON.stringify(t)&&(t=n,o(n))}))}},async toDataURL({pixelRatio:t,ignoreBackground:o,pageId:s,mimeType:n,includeBleed:i,quality:a}={}){var r;const l=t||1;s=s||(null===(r=e.pages[0])||void 0===r?void 0:r.id);const d=e.pages.find((e=>e.id===s));if(!d)throw new Error(`No page for export with id ${s}`);null==d||d.set({_exporting:!0}),await new Promise((e=>{setTimeout(e,0)})),await e.waitLoading();const c=konva_1.default.stages.find((e=>e.getAttr("pageId")===s));if(!c)throw new Error(`Export is failed. Can not find stage for page ${s}. 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.findOne(".page-background").shadowEnabled(!1),p.findOne(".page-background").strokeEnabled(!1),p.find(".highlighter").forEach((e=>e.visible(!1)));const _=p.findOne(".page-background-group"),m=_.clip();_.clip({x:null,y:null,width:null,height:null});const g=p.find((e=>e.getAttr("hideInExport")));g.forEach((e=>{e.setAttr("oldVisible",e.visible()),e.hide()})),o&&p.findOne(".page-background").hide();const h=i?d.bleed:0;let u=h;e._bleedVisible&&i?u=0:e._bleedVisible&&!i&&(u=-d.bleed);const f=document.createElement("canvas");f.width=(d.computedWidth+2*h)*l,f.height=(d.computedHeight+2*h)*l;const b=f.getContext("2d");"image/jpeg"===n&&(b.fillStyle="white",b.fillRect(0,0,f.width,f.height));const x=p.toCanvas({x:p.x()-u*p.scaleX(),y:p.y()-u*p.scaleY(),width:(d.computedWidth+2*h)*p.scaleX(),height:(d.computedHeight+2*h)*p.scaleY(),pixelRatio:1/p.scaleX()*l});b.drawImage(x,0,0);const y=f.toDataURL(n,a);return o&&p.findOne(".page-background").show(),g.forEach((e=>{e.visible(e.getAttr("oldVisible"))})),p.findOne(".page-background").shadowEnabled(!0),p.findOne(".page-background").strokeEnabled(!0),c.find("Transformer").forEach((e=>e.visible(!0))),p.find(".highlighter").forEach((e=>e.visible(!0))),_.clip(m),null==d||d.set({_exporting:!1}),y},async saveAsImage(t={}){var{fileName:o}=t,s=__rest(t,["fileName"]);const n=s.mimeType||"image/png",i=n.split("/")[1];(0,download_1.downloadFile)(await e.toDataURL(s),o||"polotno."+i,n)},async _toPDF(t){const o=t.dpi||e.dpi,s=t.parallel||1,n=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:n,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 _=new a({unit:n,orientation:c>p?"landscape":"portrait",format:[c,p],compress:!0});_.deletePage(1);const m=e._elementsPixelRatio;e.setElementsPixelRatio(i),await new Promise((e=>setTimeout(e)));const g=((e,t)=>{for(var o=[],s=0;s<e.length;s+=t)o.push(e.slice(s,s+t));return o})(e.pages,s);for(const o of g){const s=o.map((async o=>{const s=t.includeBleed?o.bleed:0,n=r(o.computedWidth+2*s),a=r(o.computedHeight+2*s);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 s=await e.toDataURL(Object.assign(Object.assign({},t),{pageId:o.id,pixelRatio:d}));if(s.length>20)return{url:s,width:n,height:a};d*=.8}}));(await Promise.all(s)).forEach((({url:e,width:t,height:o})=>{_.addPage([t,o],t>o?"landscape":"portrait"),_.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,s=__rest(t,["fileName"]);(await e._toPDF(Object.assign({mimeType:"image/jpeg"},s))).save(o||"polotno.pdf")},async waitLoading(){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 s;const n=Object.assign({},t);e.pages.forEach((e=>e.children.forEach((e=>(0,mobx_state_tree_1.detach)(e)))));const i=e.pages.indexOf(e.activePage);let a=null===(s=n.pages[i]||n.pages[0])||void 0===s?void 0:s.id;e.pages=(0,mobx_state_tree_1.cast)([]),n._activePageId=a,n.scale=e.scale,n.scaleToFit=e.scaleToFit,n.openedSidePanel=e.openedSidePanel,n._bleedVisible=e._bleedVisible,o&&(n.history=e.history.toJSON()),(0,mobx_state_tree_1.applySnapshot)(e,n)},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;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "polotno",
|
|
3
|
-
"version": "0.43.
|
|
3
|
+
"version": "0.43.14",
|
|
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": "^4.
|
|
14
|
-
"@blueprintjs/popover2": "^1.
|
|
15
|
-
"@blueprintjs/select": "^4.
|
|
13
|
+
"@blueprintjs/core": "^4.5.1",
|
|
14
|
+
"@blueprintjs/popover2": "^1.4.1",
|
|
15
|
+
"@blueprintjs/select": "^4.4.1",
|
|
16
16
|
"@meronex/icons": "^4.0.0",
|
|
17
17
|
"gradient-parser": "^1.0.2",
|
|
18
|
-
"konva": "^8.3.
|
|
19
|
-
"mobx": "6.6.
|
|
18
|
+
"konva": "^8.3.10",
|
|
19
|
+
"mobx": "6.6.1",
|
|
20
20
|
"mobx-react-lite": "^3.4.0",
|
|
21
21
|
"mobx-state-tree": "5.1.5",
|
|
22
22
|
"nanoid": "3.3.4",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"react-color": "^2.19.3",
|
|
26
26
|
"react-konva": "17.0.2-6",
|
|
27
27
|
"react-konva-utils": "^0.2.0",
|
|
28
|
-
"react-sortablejs": "
|
|
28
|
+
"react-sortablejs": "6.1.1",
|
|
29
29
|
"react-window": "^1.8.7",
|
|
30
30
|
"sortablejs": "^1.15.0",
|
|
31
31
|
"swr": "^1.3.0",
|
|
@@ -51,18 +51,18 @@
|
|
|
51
51
|
],
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/jest": "^27",
|
|
54
|
-
"@types/react": "^18.0.
|
|
54
|
+
"@types/react": "^18.0.14",
|
|
55
55
|
"@types/sanitize-html": "^2.6.2",
|
|
56
56
|
"buffer": "^6.0.3",
|
|
57
57
|
"jest": "^27",
|
|
58
|
-
"parcel": "^2.6.
|
|
58
|
+
"parcel": "^2.6.2",
|
|
59
59
|
"process": "^0.11.10",
|
|
60
60
|
"querystring-es3": "^0.2.1",
|
|
61
61
|
"react": "^17.0.2",
|
|
62
62
|
"react-dom": "^17.0.2",
|
|
63
63
|
"svg-path-bounds": "^1.0.2",
|
|
64
64
|
"ts-jest": "^27.1.4",
|
|
65
|
-
"typescript": "^4.7.
|
|
65
|
+
"typescript": "^4.7.4",
|
|
66
66
|
"uglifyjs-folder": "^3.1.2"
|
|
67
67
|
},
|
|
68
68
|
"homepage": "https://polotno.dev/",
|