polotno 2.25.2 → 2.25.4
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 +3 -3
- package/model/store.js +1 -1
- package/package.json +3 -3
- package/polotno.bundle.js +100 -100
- package/utils/fonts.js +1 -1
- package/utils/html2canvas.d.ts +0 -1
- package/utils/html2canvas.js +1 -1
- package/utils/reset-style.d.ts +2 -0
- package/utils/reset-style.js +1 -0
- package/utils/to-html.js +1 -1
- package/utils/to-pptx.d.ts +2 -2
- package/utils/to-pptx.js +1 -1
- package/utils/to-svg.js +1 -1
- package/utils/validate-key.js +1 -1
package/canvas/html-element.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
var e,t=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),n=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),r=this&&this.__importStar||(e=function(t){return e=Object.getOwnPropertyNames||function(e){var t=[];for(var n in e){Object.prototype.hasOwnProperty.call(e,n)&&(t[t.length]=n)}return t},e(t)},function(r){if(r&&r.__esModule){return r}var o={};if(null!=r){for(var i=e(r),a=0;a<i.length;a++){"default"!==i[a]&&t(o,r,i[a])}}return n(o,r),o}),o=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.HTMLElement=exports.setQuillContent=exports.createQuill=exports.setQuillFormats=exports.quillRef=void 0,exports.setCursorFromCoords=
|
|
1
|
+
var e,t=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,o)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),n=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),r=this&&this.__importStar||(e=function(t){return e=Object.getOwnPropertyNames||function(e){var t=[];for(var n in e){Object.prototype.hasOwnProperty.call(e,n)&&(t[t.length]=n)}return t},e(t)},function(r){if(r&&r.__esModule){return r}var o={};if(null!=r){for(var i=e(r),a=0;a<i.length;a++){"default"!==i[a]&&t(o,r,i[a])}}return n(o,r),o}),o=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.HTMLElement=exports.setQuillContent=exports.createQuill=exports.setQuillFormats=exports.quillRef=void 0,exports.setCursorFromCoords=_;const i=o(require("react")),a=require("mobx-react-lite"),l=require("react-konva"),s=o(require("quill")),c=r(require("mobx")),u=require("../utils/flags"),d=require("./apply-filters"),f=require("./text-element"),g=require("./use-color"),h=require("../utils/html2canvas"),m=require("../utils/reset-style"),p=require("./use-fadein"),x=require("react-konva-utils"),y=o(require("../utils/styled")),b=require("../utils/loader"),w=require("../utils/text"),v=r(require("../utils/fonts")),S=require("../utils/screen"),E=require("./text-element/max-font-size"),k=require("mobx"),z=require("./use-delayer");exports.quillRef=c.observable({enabled:!1,currentFormat:{},editor:c.observable.object({instance:null},{},{deep:!1})});const O=(0,y.default)("div",i.default.forwardRef)`
|
|
2
2
|
.ql-editor {
|
|
3
3
|
outline: none;
|
|
4
4
|
}
|
|
5
5
|
.ql-clipboard {
|
|
6
6
|
pointer-events: none;
|
|
7
7
|
}
|
|
8
|
-
${
|
|
8
|
+
${m.resetStyleContent}
|
|
9
9
|
strong {
|
|
10
10
|
font-weight: 700;
|
|
11
11
|
}
|
|
12
12
|
.ql-direction-rtl {
|
|
13
13
|
direction: rtl;
|
|
14
14
|
}
|
|
15
|
-
`;let O=["bold","color","font","italic","size","strike","underline","indent","list","direction"];exports.setQuillFormats=e=>{O=e},exports.createQuill=e=>new s.default(e,{toolbar:!1,keyboard:!1,clipboard:{matchVisual:!1},formats:O}),exports.setQuillContent=(e,t)=>{var n=e.clipboard.convert("<div class='ql-editor' style='outline: none;'>"+t+"<p><br></p></div>");e.setContents(n),e.history.clear()};const F=({html:e,onBlur:t,onChange:n,element:r,clickCoords:o})=>{const a=i.default.useRef(null);i.default.useEffect(()=>{if(!a.current){return}const r=(0,exports.createQuill)(a.current);return c.runInAction(()=>{exports.quillRef.editor.instance=r}),window.__polotnoQuill=r,r.on("text-change",()=>{r.getSelection()&&c.runInAction(()=>{exports.quillRef.currentFormat=r.getFormat()}),setTimeout(()=>{var e;const t=null===(e=a.current)||void 0===e?void 0:e.childNodes[0];n(t.innerHTML)},10)}),(0,exports.setQuillContent)(r,e),o?T(r,o):r.setSelection(0,0,"api"),r.on("selection-change",(e,t,n)=>{e&&c.runInAction(()=>{exports.quillRef.currentFormat=r.getFormat()})}),a.current.childNodes[0].addEventListener("blur",e=>{var n;if(null===(n=e.relatedTarget)||void 0===n?void 0:n.classList.contains("ql-clipboard")){return}const r=function(e){return!!e&&!!e.closest(".sketch-picker")}(e.relatedTarget);r||t()}),()=>{c.runInAction(()=>{exports.quillRef.editor.instance=null,exports.quillRef.currentFormat={}}),delete window.__polotnoQuill}},[]),i.default.useEffect(()=>(0,E.reaction)(()=>r.text,()=>{var t;const n=exports.quillRef.editor.instance;if(!n){return}const o=n.getSelection();(null===(t=a.current)||void 0===t?void 0:t.childNodes[0].innerHTML)===r.text||((0,exports.setQuillContent)(n,e),o&&(n.setSelection(o.index,o.length),c.runInAction(()=>{exports.quillRef.currentFormat=n.getFormat()})))},{fireImmediately:!0}),[]),i.default.useEffect(()=>{window.addEventListener("blur",t);const e=e=>{var n;(null===(n=a.current)||void 0===n?void 0:n.contains(e.target))||t()};return window.addEventListener("touchstart",e),()=>{window.removeEventListener("blur",t),window.removeEventListener("touchstart",e)}},[]);const l={color:r.fill};return r.fill.indexOf("gradient")>=0&&(l.backgroundColor=r.fill,l.backgroundImage=r.fill,l.backgroundSize="100% 100%",l.backgroundRepeat="repeat",l.webkitBackgroundClip="text",l.MozBackgroundClip="text",l.WebkitTextFillColor="transparent",l.MozTextFillColor="transparent"),i.default.createElement(z,{ref:a,style:Object.assign(Object.assign({},l),{fontSize:r.fontSize,fontWeight:r.fontWeight,textTransform:r.textTransform,width:r.a.width,fontFamily:'"'+r.fontFamily+'"',lineHeight:r.lineHeight,letterSpacing:r.letterSpacing*r.fontSize+"px",textAlign:r.align,opacity:Math.max(r.a.opacity,.2)}),dir:(0,f.getDir)((0,b.removeTags)(r.text))})};function q(e){return!function(e){const t=e.getContext("2d").getImageData(0,0,e.width,e.height).data;for(let n=0;n<t.length;n+=4){if(0!==t[n+3]){return!0}}return!1}(e)}function R(e,{fontFamily:t="",color:n="black"}={}){let r=`color: ${n||e.fill}`;return e.fill.indexOf("gradient")>=0&&(r=`\n background-color: ${n};\n background-image: ${e.fill};\n background-size: 100% 100%;\n background-repeat: repeat;\n -webkit-background-clip: text;\n -moz-background-clip: text;\n -webkit-text-fill-color: transparent;\n -moz-text-fill-color: transparent;\n `),`<div style="${["white-space: pre-wrap",`width: ${Math.round(e.width||100)}px`,r,`font-size: ${e.fontSize}px`,`font-family: '${t}'`,`text-align: ${e.align}`,`text-transform: ${e.textTransform}`,e.textDecoration?`text-decoration: ${e.textDecoration}; text-decoration-color: ${n||e.fill}; text-decoration-layer: over`:"",e.lineHeight?`line-height: ${e.lineHeight}`:"",e.letterSpacing?`letter-spacing: ${e.letterSpacing*e.fontSize}px`:"",e.fontStyle?`font-style: ${e.fontStyle}`:"",e.fontWeight?`font-weight: ${e.fontWeight}`:"",e.strokeWidth?`-webkit-text-stroke: ${e.strokeWidth}px ${e.stroke}`:"",e.strokeWidth?"paint-order: stroke fill":""].filter(Boolean).join("; ")}" contentEditable dir="${(0,f.getDir)((0,b.removeTags)(e.text))}">${(0,b.sanitizeHtml)(e.text).replace(/\n/g,"</br>")}</div>`}const C=/^((?!chrome|android).)*safari/i.test(navigator.userAgent);function T(e,t){if(!e||!t){return}const{x:n,y:r}=t;try{let t=null;if(document.caretRangeFromPoint){t=document.caretRangeFromPoint(n,r)}else if(document.caretPositionFromPoint){const e=document.caretPositionFromPoint(n,r);e&&(t=document.createRange(),t.setStart(e.offsetNode,e.offset))}if(t){const n=s.default.find(t.startContainer,!0);if(n){const r=n.offset(e.scroll)+t.startOffset;return void e.setSelection(r,0,"api")}}}catch(o){}e.setSelection(0,0,"api")}exports.HTMLElement=(0,a.observer)(({element:e,store:t})=>{const n=i.default.useRef(null),[r,o]=i.default.useState(),[a,s]=i.default.useState(!1),[x,E]=i.default.useState(!1),O=i.default.useRef(e.height),T=t.selectedShapes.indexOf(e)>=0&&e.selectable,_=e.fontSize/3,{textVerticalResizeEnabled:M}=u.flags,$=(0,f.usePrevious)(e.fontFamily),[P]=(0,f.useFontLoader)(t,e.fontFamily),I=e._editModeEnabled;(0,m.useFadeIn)(n);const L=P?e.fontFamily:$!==e.fontFamily?$:"Arial",A=(0,g.useColor)(e).fill,D=R(e,{fontFamily:L,color:A}),{width:H,height:j}=function(e,t,n){return i.default.useMemo(()=>(0,h.detectSize)(e),[e,t.width,n])}(D,e,P);i.default.useEffect(()=>{if(!P){return}if(!e.height){return void e.set({height:j})}const{textOverflow:n}=u.flags;if("change-font-size"!==n||a){"resize"===n&&(M&&e.height<j&&t.history.ignore(()=>{e.set({height:j})}),M||e.height===j||t.history.ignore(()=>{e.set({height:j})}))}else{const n=(e=>{let t=e.fontSize;for(let n=1;n<50;n++){const n=R(Object.assign(Object.assign({},e.toJSON()),{fontSize:t}),{fontFamily:e.fontFamily}),{height:r}=(0,h.detectSize)(n),o=e.height&&r>e.height,i=(0,h.isContentWrapping)({html:n});if(!o&&!i){break}t-=.5}return t})(e);n!==e.fontSize?t.history.ignore(()=>{e.set({fontSize:n})}):e.height!==j&&(M&&e.height<j?t.history.ignore(()=>{e.set({height:j})}):M||t.history.ignore(()=>{e.set({height:j})}))}});const X=i.default.useMemo(()=>{const e={lastArgs:null,lastResult:null};return async function(t){return e.lastArgs&&e.lastResult&&(n=e.lastArgs,r=t,JSON.stringify(n)===JSON.stringify(r))||(e.lastResult=await(0,h.htmlToCanvas)(t),e.lastArgs=Object.assign({},t)),e.lastResult;var n,r}},[]),Y=i.default.useRef(0),Q=i.default.useRef(null);i.default.useEffect(()=>{a||I||(async()=>{Y.current++;const n=Y.current;let r=(0,y.incrementLoader)(`text ${e.id} ${n}`);Q.current&&Q.current(),Q.current=r,E(!0);let i=null;const a=C?5:1;for(let o=0;o<a;o++){const r=o>0?X:h.htmlToCanvas;try{if(i=await r({skipCache:o>0,html:D,width:e.width||1,height:e.height||j||1,fontFamily:L,padding:_,pixelRatio:t._elementsPixelRatio,font:t.fonts.find(e=>e.fontFamily===L)||w.globalFonts.find(e=>e.fontFamily===L)}),n!==Y.current){return}if(q(i)&&C){await new Promise(e=>setTimeout(e,50*(o+1)));continue}break}catch(l){console.error(l),(0,y.triggerLoadError)(`Error rendering rich text with id ${e.id}`);break}}i?o(i):r?(r(),r=null):console.error("Finish function is called twice!"),E(!1)})()},[D,a,j,I,L,e.height,t._elementsPixelRatio,P]);const[W,B]=(0,k.useDelayer)(x,300),[N]=(0,k.useDelayer)(a,300,!0),J=N||W;i.default.useEffect(()=>{if(!J){return c.autorun(()=>{const t=n.current;(0,d.applyFilter)(t,e)})}},[r,J,e.shadowColor,e.shadowOffsetX,e.shadowOffsetY,e.shadowOpacity]),i.default.useEffect(()=>{r&&!x&&Q.current&&(Q.current(),Q.current=null)},[r,x]),i.default.useEffect(()=>()=>{Q.current&&Q.current()},[]);let V=0;"middle"===e.verticalAlign&&(V=(e.height-j)/2),"bottom"===e.verticalAlign&&(V=e.height-j);const G=(0,f.getLineHeight)({fontLoaded:P,fontFamily:e.fontFamily,fontSize:e.fontSize,lineHeight:e.lineHeight}),K=(0,v.isTouchDevice)(),U=i.default.useRef(null),Z=i.default.useRef(null);return i.default.createElement(i.default.Fragment,null,i.default.createElement(l.Rect,{x:e.a.x,y:e.a.y,offsetX:e.backgroundPadding*(e.fontSize*G*.5),offsetY:e.backgroundPadding*(e.fontSize*G*.5),rotation:e.a.rotation,hideInExport:!e.showInExport,listening:!1,visible:e.backgroundEnabled,opacity:e.backgroundOpacity*e.a.opacity,fill:e.backgroundColor,width:e.a.width+e.backgroundPadding*(e.fontSize*G),height:e.a.height+e.backgroundPadding*(e.fontSize*G),cornerRadius:e.backgroundCornerRadius*(e.fontSize*G*.5)}),i.default.createElement(l.Rect,{ref:n,name:"element",x:e.a.x,y:e.a.y,listening:e.selectable,rotation:e.a.rotation,width:e.a.width,height:e.a.height,visible:!J,draggable:K?e.draggable&&T:e.draggable,preventDefault:!K||T,opacity:I?0:e.a.opacity,hideInExport:!e.showInExport,onDragMove:t=>{e.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{e.set({x:t.target.x(),y:t.target.y()})},id:e.id,onDblClick:t=>{e.contentEditable&&(Z.current={x:t.evt.clientX,y:t.evt.clientY},e.toggleEditMode(!0))},onDblTap:t=>{var n;if(e.contentEditable){const r=null===(n=t.evt.changedTouches)||void 0===n?void 0:n[0];Z.current=r?{x:r.clientX,y:r.clientY}:null,e.toggleEditMode(!0)}},onTransformStart:t=>{s(!0),O.current=e.height},onTransform:t=>{var n;const r=t.target,o=(null===(n=r.getStage())||void 0===n?void 0:n.findOne("Transformer")).getActiveAnchor(),i="middle-left"===o||"middle-right"===o,a="top-center"===o||"bottom-center"===o,l=r.scaleX();if(i){const t=r.scaleX(),n=r.width()*t,o=e.fontSize;let i=n;if(n<o&&(i=o,U.current&&r.position(U.current)),r.width(i),r.scaleX(1),u.flags.textVerticalResizeEnabled){const t=Math.max(j,O.current);e.set({height:t})}e.set({width:r.width(),x:r.x(),y:r.y()})}else if(a){let n="resize"===u.flags.textOverflow?j:e.lineHeight*e.fontSize;const o=Math.max(n,t.target.height()*t.target.scaleY());r.scaleY(1),e.set({x:r.x(),y:r.y(),height:o,rotation:r.rotation()})}else{r.scaleX(1),r.scaleY(1),e.set({fontSize:e.fontSize*l,letterSpacing:e.letterSpacing,width:r.width()*l,x:r.x(),y:r.y(),rotation:r.rotation(),height:r.height()*l})}U.current=t.target.position()},onTransformEnd:t=>{s(!1),E(!0);const n=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),e.set({fontSize:e.fontSize*n,width:t.target.width()*n,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),shadowBlur:e.shadowBlur*n,shadowOffsetX:e.shadowOffsetX*n,shadowOffsetY:e.shadowOffsetY*n,strokeWidth:e.strokeWidth*n})}}),i.default.createElement(l.Image,{ref:n,image:r,x:e.a.x,y:e.a.y,offsetX:_,offsetY:_-V,listening:!1,rotation:e.a.rotation,width:e.a.width+2*_,height:(e.a.width+2*_)*((null==r?void 0:r.height)/(null==r?void 0:r.width)||1),visible:!J&&!I,opacity:e.a.opacity,shadowEnabled:e.shadowEnabled,shadowBlur:e.shadowBlur,shadowOffsetX:e.shadowOffsetX,shadowOffsetY:e.shadowOffsetY,shadowColor:e.shadowColor,shadowOpacity:e.shadowOpacity,hideInExport:!e.showInExport,editModeEnabled:e._editModeEnabled}),J&&i.default.createElement(l.Group,{x:e.a.x,y:e.a.y,rotation:e.a.rotation,offsetY:-V},i.default.createElement(p.Html,{divProps:{style:{pointerEvents:"none"}}},i.default.createElement(z,{dangerouslySetInnerHTML:{__html:D},style:{pointerEvents:"none",opacity:e.a.opacity}}))),I&&i.default.createElement(l.Group,{x:e.a.x,y:e.a.y,rotation:e.a.rotation,offsetY:-V},i.default.createElement(p.Html,null,i.default.createElement(F,{html:D,element:e,onChange:t=>{const n=(0,S.getLimitedFontSize)({oldText:(0,b.removeTags)(e.text),newText:(0,b.removeTags)(t),element:e});e.set({text:t,fontSize:n})},onBlur:n=>{e.toggleEditMode(!1),B(!0),""===(0,b.removeTags)(e.text)&&e.removable&&!e.placeholder&&t.deleteElements([e.id])},clickCoords:Z.current}))))});
|
|
15
|
+
`;let F=["bold","color","font","italic","size","strike","underline","indent","list","direction"];exports.setQuillFormats=e=>{F=e},exports.createQuill=e=>new s.default(e,{toolbar:!1,keyboard:!1,clipboard:{matchVisual:!1},formats:F}),exports.setQuillContent=(e,t)=>{var n=e.clipboard.convert("<div class='ql-editor' style='outline: none;'>"+t+"<p><br></p></div>");e.setContents(n),e.history.clear()};const q=({html:e,onBlur:t,onChange:n,element:r,clickCoords:o})=>{const a=i.default.useRef(null);i.default.useEffect(()=>{if(!a.current){return}const r=(0,exports.createQuill)(a.current);return c.runInAction(()=>{exports.quillRef.editor.instance=r}),window.__polotnoQuill=r,r.on("text-change",()=>{r.getSelection()&&c.runInAction(()=>{exports.quillRef.currentFormat=r.getFormat()}),setTimeout(()=>{var e;const t=null===(e=a.current)||void 0===e?void 0:e.childNodes[0];n(t.innerHTML)},10)}),(0,exports.setQuillContent)(r,e),o?_(r,o):r.setSelection(0,0,"api"),r.on("selection-change",(e,t,n)=>{e&&c.runInAction(()=>{exports.quillRef.currentFormat=r.getFormat()})}),a.current.childNodes[0].addEventListener("blur",e=>{var n;if(null===(n=e.relatedTarget)||void 0===n?void 0:n.classList.contains("ql-clipboard")){return}const r=function(e){return!!e&&!!e.closest(".sketch-picker")}(e.relatedTarget);r||t()}),()=>{c.runInAction(()=>{exports.quillRef.editor.instance=null,exports.quillRef.currentFormat={}}),delete window.__polotnoQuill}},[]),i.default.useEffect(()=>(0,k.reaction)(()=>r.text,()=>{var t;const n=exports.quillRef.editor.instance;if(!n){return}const o=n.getSelection();(null===(t=a.current)||void 0===t?void 0:t.childNodes[0].innerHTML)===r.text||((0,exports.setQuillContent)(n,e),o&&(n.setSelection(o.index,o.length),c.runInAction(()=>{exports.quillRef.currentFormat=n.getFormat()})))},{fireImmediately:!0}),[]),i.default.useEffect(()=>{window.addEventListener("blur",t);const e=e=>{var n;(null===(n=a.current)||void 0===n?void 0:n.contains(e.target))||t()};return window.addEventListener("touchstart",e),()=>{window.removeEventListener("blur",t),window.removeEventListener("touchstart",e)}},[]);const l={color:r.fill};return r.fill.indexOf("gradient")>=0&&(l.backgroundColor=r.fill,l.backgroundImage=r.fill,l.backgroundSize="100% 100%",l.backgroundRepeat="repeat",l.webkitBackgroundClip="text",l.MozBackgroundClip="text",l.WebkitTextFillColor="transparent",l.MozTextFillColor="transparent"),i.default.createElement(O,{ref:a,style:Object.assign(Object.assign({},l),{fontSize:r.fontSize,fontWeight:r.fontWeight,textTransform:r.textTransform,width:r.a.width,fontFamily:'"'+r.fontFamily+'"',lineHeight:r.lineHeight,letterSpacing:r.letterSpacing*r.fontSize+"px",textAlign:r.align,opacity:Math.max(r.a.opacity,.2)}),dir:(0,f.getDir)((0,w.removeTags)(r.text))})};function R(e){return!function(e){const t=e.getContext("2d").getImageData(0,0,e.width,e.height).data;for(let n=0;n<t.length;n+=4){if(0!==t[n+3]){return!0}}return!1}(e)}function C(e,{fontFamily:t="",color:n="black"}={}){let r=`color: ${n||e.fill}`;return e.fill.indexOf("gradient")>=0&&(r=`\n background-color: ${n};\n background-image: ${e.fill};\n background-size: 100% 100%;\n background-repeat: repeat;\n -webkit-background-clip: text;\n -moz-background-clip: text;\n -webkit-text-fill-color: transparent;\n -moz-text-fill-color: transparent;\n `),`<div style="${["white-space: pre-wrap",`width: ${Math.round(e.width||100)}px`,r,`font-size: ${e.fontSize}px`,`font-family: '${t}'`,`text-align: ${e.align}`,`text-transform: ${e.textTransform}`,e.textDecoration?`text-decoration: ${e.textDecoration}; text-decoration-color: ${n||e.fill}; text-decoration-layer: over`:"",e.lineHeight?`line-height: ${e.lineHeight}`:"",e.letterSpacing?`letter-spacing: ${e.letterSpacing*e.fontSize}px`:"",e.fontStyle?`font-style: ${e.fontStyle}`:"",e.fontWeight?`font-weight: ${e.fontWeight}`:"",e.strokeWidth?`-webkit-text-stroke: ${e.strokeWidth}px ${e.stroke}`:"",e.strokeWidth?"paint-order: stroke fill":""].filter(Boolean).join("; ")}" contentEditable dir="${(0,f.getDir)((0,w.removeTags)(e.text))}">${(0,w.sanitizeHtml)(e.text).replace(/\n/g,"</br>")}</div>`}const T=/^((?!chrome|android).)*safari/i.test(navigator.userAgent);function _(e,t){if(!e||!t){return}const{x:n,y:r}=t;try{let t=null;if(document.caretRangeFromPoint){t=document.caretRangeFromPoint(n,r)}else if(document.caretPositionFromPoint){const e=document.caretPositionFromPoint(n,r);e&&(t=document.createRange(),t.setStart(e.offsetNode,e.offset))}if(t){const n=s.default.find(t.startContainer,!0);if(n){const r=n.offset(e.scroll)+t.startOffset;return void e.setSelection(r,0,"api")}}}catch(o){}e.setSelection(0,0,"api")}exports.HTMLElement=(0,a.observer)(({element:e,store:t})=>{const n=i.default.useRef(null),[r,o]=i.default.useState(),[a,s]=i.default.useState(!1),[m,y]=i.default.useState(!1),k=i.default.useRef(e.height),F=t.selectedShapes.indexOf(e)>=0&&e.selectable,_=e.fontSize/3,{textVerticalResizeEnabled:M}=u.flags,$=(0,f.usePrevious)(e.fontFamily),[P]=(0,f.useFontLoader)(t,e.fontFamily),I=e._editModeEnabled;(0,p.useFadeIn)(n);const L=P?e.fontFamily:$!==e.fontFamily?$:"Arial",A=(0,g.useColor)(e).fill,D=C(e,{fontFamily:L,color:A}),{width:H,height:j}=function(e,t,n){return i.default.useMemo(()=>(0,h.detectSize)(e),[e,t.width,n])}(D,e,P);i.default.useEffect(()=>{if(!P){return}if(!e.height){return void e.set({height:j})}const{textOverflow:n}=u.flags;if("change-font-size"!==n||a){"resize"===n&&(M&&e.height<j&&t.history.ignore(()=>{e.set({height:j})}),M||e.height===j||t.history.ignore(()=>{e.set({height:j})}))}else{const n=(e=>{let t=e.fontSize;for(let n=1;n<50;n++){const n=C(Object.assign(Object.assign({},e.toJSON()),{fontSize:t}),{fontFamily:e.fontFamily}),{height:r}=(0,h.detectSize)(n),o=e.height&&r>e.height,i=(0,h.isContentWrapping)({html:n});if(!o&&!i){break}t-=.5}return t})(e);n!==e.fontSize?t.history.ignore(()=>{e.set({fontSize:n})}):e.height!==j&&(M&&e.height<j?t.history.ignore(()=>{e.set({height:j})}):M||t.history.ignore(()=>{e.set({height:j})}))}});const X=i.default.useMemo(()=>{const e={lastArgs:null,lastResult:null};return async function(t){return e.lastArgs&&e.lastResult&&(n=e.lastArgs,r=t,JSON.stringify(n)===JSON.stringify(r))||(e.lastResult=await(0,h.htmlToCanvas)(t),e.lastArgs=Object.assign({},t)),e.lastResult;var n,r}},[]),Y=i.default.useRef(0),Q=i.default.useRef(null);i.default.useEffect(()=>{a||I||(async()=>{Y.current++;const n=Y.current;let r=(0,b.incrementLoader)(`text ${e.id} ${n}`);Q.current&&Q.current(),Q.current=r,y(!0);let i=null;const a=T?5:1;for(let o=0;o<a;o++){const r=o>0?X:h.htmlToCanvas;try{if(i=await r({skipCache:o>0,html:D,width:e.width||1,height:e.height||j||1,fontFamily:L,padding:_,pixelRatio:t._elementsPixelRatio,font:t.fonts.find(e=>e.fontFamily===L)||v.globalFonts.find(e=>e.fontFamily===L)}),n!==Y.current){return}if(T&&R(i)){await new Promise(e=>setTimeout(e,50*(o+1)));continue}break}catch(l){console.error(l),(0,b.triggerLoadError)(`Error rendering rich text with id ${e.id}`);break}}i?o(i):r?(r(),r=null):console.error("Finish function is called twice!"),y(!1)})()},[D,a,j,I,L,e.height,t._elementsPixelRatio,P]);const[W,B]=(0,z.useDelayer)(m,300),[N]=(0,z.useDelayer)(a,300,!0),J=N||W;i.default.useEffect(()=>{if(!J){return c.autorun(()=>{const t=n.current;(0,d.applyFilter)(t,e)})}},[r,J,e.shadowColor,e.shadowOffsetX,e.shadowOffsetY,e.shadowOpacity]),i.default.useEffect(()=>{r&&!m&&Q.current&&(Q.current(),Q.current=null)},[r,m]),i.default.useEffect(()=>()=>{Q.current&&Q.current()},[]);let V=0;"middle"===e.verticalAlign&&(V=(e.height-j)/2),"bottom"===e.verticalAlign&&(V=e.height-j);const G=(0,f.getLineHeight)({fontLoaded:P,fontFamily:e.fontFamily,fontSize:e.fontSize,lineHeight:e.lineHeight}),K=(0,S.isTouchDevice)(),U=i.default.useRef(null),Z=i.default.useRef(null);return i.default.createElement(i.default.Fragment,null,i.default.createElement(l.Rect,{x:e.a.x,y:e.a.y,offsetX:e.backgroundPadding*(e.fontSize*G*.5),offsetY:e.backgroundPadding*(e.fontSize*G*.5),rotation:e.a.rotation,hideInExport:!e.showInExport,listening:!1,visible:e.backgroundEnabled,opacity:e.backgroundOpacity*e.a.opacity,fill:e.backgroundColor,width:e.a.width+e.backgroundPadding*(e.fontSize*G),height:e.a.height+e.backgroundPadding*(e.fontSize*G),cornerRadius:e.backgroundCornerRadius*(e.fontSize*G*.5)}),i.default.createElement(l.Rect,{ref:n,name:"element",x:e.a.x,y:e.a.y,listening:e.selectable,rotation:e.a.rotation,width:e.a.width,height:e.a.height,visible:!J,draggable:K?e.draggable&&F:e.draggable,preventDefault:!K||F,opacity:I?0:e.a.opacity,hideInExport:!e.showInExport,onDragMove:t=>{e.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{e.set({x:t.target.x(),y:t.target.y()})},id:e.id,onDblClick:t=>{e.contentEditable&&(Z.current={x:t.evt.clientX,y:t.evt.clientY},e.toggleEditMode(!0))},onDblTap:t=>{var n;if(e.contentEditable){const r=null===(n=t.evt.changedTouches)||void 0===n?void 0:n[0];Z.current=r?{x:r.clientX,y:r.clientY}:null,e.toggleEditMode(!0)}},onTransformStart:t=>{s(!0),k.current=e.height},onTransform:t=>{var n;const r=t.target,o=(null===(n=r.getStage())||void 0===n?void 0:n.findOne("Transformer")).getActiveAnchor(),i="middle-left"===o||"middle-right"===o,a="top-center"===o||"bottom-center"===o,l=r.scaleX();if(i){const t=r.scaleX(),n=r.width()*t,o=e.fontSize;let i=n;if(n<o&&(i=o,U.current&&r.position(U.current)),r.width(i),r.scaleX(1),u.flags.textVerticalResizeEnabled){const t=Math.max(j,k.current);e.set({height:t})}e.set({width:r.width(),x:r.x(),y:r.y()})}else if(a){let n="resize"===u.flags.textOverflow?j:e.lineHeight*e.fontSize;const o=Math.max(n,t.target.height()*t.target.scaleY());r.scaleY(1),e.set({x:r.x(),y:r.y(),height:o,rotation:r.rotation()})}else{r.scaleX(1),r.scaleY(1),e.set({fontSize:e.fontSize*l,letterSpacing:e.letterSpacing,width:r.width()*l,x:r.x(),y:r.y(),rotation:r.rotation(),height:r.height()*l})}U.current=t.target.position()},onTransformEnd:t=>{s(!1),y(!0);const n=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),e.set({fontSize:e.fontSize*n,width:t.target.width()*n,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),shadowBlur:e.shadowBlur*n,shadowOffsetX:e.shadowOffsetX*n,shadowOffsetY:e.shadowOffsetY*n,strokeWidth:e.strokeWidth*n})}}),i.default.createElement(l.Image,{ref:n,image:r,x:e.a.x,y:e.a.y,offsetX:_,offsetY:_-V,listening:!1,rotation:e.a.rotation,width:e.a.width+2*_,height:(e.a.width+2*_)*((null==r?void 0:r.height)/(null==r?void 0:r.width)||1),visible:!J&&!I,opacity:e.a.opacity,shadowEnabled:e.shadowEnabled,shadowBlur:e.shadowBlur,shadowOffsetX:e.shadowOffsetX,shadowOffsetY:e.shadowOffsetY,shadowColor:e.shadowColor,shadowOpacity:e.shadowOpacity,hideInExport:!e.showInExport,editModeEnabled:e._editModeEnabled}),J&&i.default.createElement(l.Group,{x:e.a.x,y:e.a.y,rotation:e.a.rotation,offsetY:-V},i.default.createElement(x.Html,{divProps:{style:{pointerEvents:"none"}}},i.default.createElement(O,{dangerouslySetInnerHTML:{__html:D},style:{pointerEvents:"none",opacity:e.a.opacity}}))),I&&i.default.createElement(l.Group,{x:e.a.x,y:e.a.y,rotation:e.a.rotation,offsetY:-V},i.default.createElement(x.Html,null,i.default.createElement(q,{html:D,element:e,onChange:t=>{const n=(0,E.getLimitedFontSize)({oldText:(0,w.removeTags)(e.text),newText:(0,w.removeTags)(t),element:e});e.set({text:t,fontSize:n})},onBlur:n=>{e.toggleEditMode(!1),B(!0),""===(0,w.removeTags)(e.text)&&e.removable&&!e.placeholder&&t.deleteElements([e.id])},clickCoords:Z.current}))))});
|
package/model/store.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e,t=this&&this.__createBinding||(Object.create?function(e,t,i,n){void 0===n&&(n=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,n,o)}:function(e,t,i,n){void 0===n&&(n=i),e[n]=t[i]}),i=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),n=this&&this.__importStar||(e=function(t){return e=Object.getOwnPropertyNames||function(e){var t=[];for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&(t[t.length]=i)}return t},e(t)},function(n){if(n&&n.__esModule){return n}var o={};if(null!=n){for(var a=e(n),s=0;s<a.length;s++){"default"!==a[s]&&t(o,n,a[s])}}return i(o,n),o}),o=this&&this.__rest||function(e,t){var i={};for(var n in e){Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(i[n]=e[n])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(e);o<n.length;o++){t.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(e,n[o])&&(i[n[o]]=e[n[o]])}}return i},a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Store=exports.Font=void 0,exports.createStore=S;const s=require("mobx-state-tree"),r=require("./history"),l=require("nanoid"),d=a(require("konva")),c=require("mobx"),p=require("../utils/download"),u=require("../utils/pdf"),g=require("../utils/gif-lib"),m=require("../utils/validate-key"),h=n(require("../utils/fonts")),f=require("../utils/flags"),y=require("../utils/loader"),b=require("../utils/unit"),v=require("../utils/deep-equal"),w=require("../utils/wait"),P=require("../utils/to-html"),x=require("../utils/to-svg"),E=require("./page-model"),_=require("./group-model"),O=require("./audio-model");function S({key:e,showCredit:t}={key:"",showCredit:!1}){return exports.Store.create({_forceShowCredit:t,_key:e})}(0,s.setLivelinessChecking)("ignore"),exports.Font=s.types.model("Font",{fontFamily:s.types.string,url:s.types.optional(s.types.string,""),styles:s.types.frozen()}).preProcessSnapshot(e=>Object.assign(Object.assign({},e),{fontFamily:e.fontFamily||e.name})),exports.Store=s.types.model("Store",{role:"",pages:s.types.array(E.Page),fonts:s.types.array(exports.Font),audios:s.types.array(O.Audio),width:1080,height:1080,currentTime:0,isPlaying:!1,scale:1,scaleToFit:1,unit:"px",dpi:72,schemaVersion:2,bleedVisible:!1,rulesVisible:!1,openedSidePanel:"",previousOpenedSidePanel:"",custom:s.types.frozen(),selectedElementsIds:s.types.array(s.types.string),animatedElementsIds:s.types.array(s.types.string),history:s.types.optional(r.UndoManager,{targetPath:"../pages"}),_elementsPixelRatio:Math.min(2,window.devicePixelRatio||1),_activePageId:"",_forceShowCredit:!1,_key:"",_validated:!1}).views(e=>{const t=(0,c.computed)(()=>{const t={};return(0,_.forEveryChild)({children:e.pages},e=>(t[e.id]=e,!1)),t},{keepAlive:!0});return{get _idsMap(){return t.get()}}}).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(0,_.forEveryChild)({children:e.selectedElements},e=>{"group"!==e.type&&t.push(e)}),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 i;return(0,_.forEveryChild)({children:e.pages},e=>{if(!i&&t(e)){return i=e,!0}}),i},getElementById:t=>e._idsMap[t]})).actions(e=>{let t=0,i=null,n=!1;return{afterCreate(){e.history.canUndo},setCurrentTime(t){e.currentTime=t},_togglePlaying(t=!e.isPlaying){e.isPlaying=t},play({animatedElementsIds:o=[],startTime:a=0,currentTime:r=0,endTime:l=e.duration,repeat:d=!1}={}){r&&(console.warn("currentTime property of store.play() is deprecated. Please use startTime instead."),a=r),e.animatedElementsIds=(0,s.cast)(o),e.currentTime=a,e.isPlaying=!0,t=Date.now(),i=l,n=d,requestAnimationFrame(e.seek)},checkActivePage(){let t=0;for(const i of e.pages){if(e.currentTime>=i.startTime&&e.currentTime<i.startTime+i.duration){e.selectPage(i.id);break}t+=i.duration}},seek(){if(!e.isPlaying){return}const o=Date.now(),a=o-t;t=o,e.currentTime+=a,e.checkActivePage();const s=i||e.duration;e.isPlaying&&e.currentTime<s?requestAnimationFrame(e.seek):e.isPlaying&&n?(e.currentTime=0,requestAnimationFrame(e.seek)):e.stop()},stop(){e.isPlaying=!1,e.currentTime=0,e.animatedElementsIds=(0,s.cast)([]),e.checkActivePage()}}}).actions(e=>({__(){e._validated||((0,m.validateKey)(e._key,e._forceShowCredit),e._validated=!0)},set(t){Object.assign(e,t)},setUnit({unit:t,dpi:i}){e.unit=t||e.unit,e.dpi=i||e.dpi},setRole(t){e.role=t},addPage(t){const i=E.Page.create(Object.assign({id:(0,l.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,s.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&&(e.previousOpenedSidePanel=e.openedSidePanel,e.openedSidePanel=t)},setScale(t){e.scale=t},_setScaleToFit(t){e.scaleToFit=t},setElementsPixelRatio(t){e._elementsPixelRatio=t},setSize(t,i,n){e.pages.forEach(e=>{e.setSize({width:t,height:i,useMagic:n,softChange:!0})}),e.width=t,e.height=i},setPageZIndex(t,i){const n=e.pages.find(e=>e.id===t);n&&((0,s.detach)(n),e.pages.remove(n),e.pages.splice(i,0,n))},deletePages(t){const i=e.pages.indexOf(e.activePage);t.forEach(t=>{const i=e.pages.find(e=>e.id===t);(0,s.destroy)(i)});const n=Math.min(e.pages.length-1,i),o=e.pages[n];o&&(e._activePageId=o.id),e.selectedElementsIds=(0,s.cast)(e.selectedElementsIds.filter(t=>e.getElementById(t)))},groupElements(t,i={}){const n=t.map(t=>e.getElementById(t)),o=n[0].page;if(n.forEach(e=>{e&&(0,s.detach)(e)}),!n.length){return}const a=Object.assign({id:(0,l.nanoid)(10),children:n,type:"group"},i);return o.children.push(a),e.selectedElementsIds=(0,s.cast)([a.id]),o.children.find(e=>e.id===a.id)},ungroupElements(t){const i=t.map(t=>e.getElementById(t)),n=[];i.forEach(e=>{if(e&&"group"===e.type){const t=e.page,i=t.children.indexOf(e);e.children.forEach(e=>{n.push(e.id)}),e.children.forEach(e=>{(0,s.detach)(e),t.children.push(e)}),t.children.splice(i,1)}}),e.selectedElementsIds=(0,s.cast)(n)},deleteElements(t){const i=[];e.find(e=>(t.includes(e.id)&&i.push(e),!1)),i.forEach(e=>{(0,s.destroy)(e)}),e.selectedElementsIds=(0,s.cast)(e.selectedElementsIds.filter(t=>e.getElementById(t)))},on(t,i){if("change"===t){let t=e.toJSON();return(0,s.onSnapshot)(e,n=>{const o=e.toJSON();!(0,v.deepEqual)(t,o)&&(t=o,i(o))})}},async _toCanvas({pixelRatio:t,ignoreBackground:i,pageId:n,mimeType:o,includeBleed:a,_skipTimeout:s,quickMode:r=!1}={}){var l;const c=t||1;n=n||(null===(l=e.pages[0])||void 0===l?void 0:l.id);const p=e.pages.find(e=>e.id===n);if(!p){throw new Error(`No page for export with id ${n}`)}const u=e._elementsPixelRatio;c>e._elementsPixelRatio&&e.setElementsPixelRatio(c),r?null==p||p.set({_forceMount:!0}):null==p||p.set({_exporting:!0});const g=await(0,w.waitTillAvailable)(()=>d.default.stages.find(e=>e.getAttr("pageId")===n));if(!g){throw null==p||p.set({_forceMount:!1,_exporting:!1}),e.setElementsPixelRatio(u),new Error(`Export is failed. Can not find stage for page ${n}. Looks like <Workspace /> component is not mounted, but it is required in order to process the export.`)}await e.waitLoading({_skipTimeout:s});const m=g.findOne(".page-container");if(!m){throw e.setElementsPixelRatio(u),null==p||p.set({_forceMount:!1,_exporting:!1}),new Error("Export is failed. Canvas was unmounted.")}const h=g.position();g.position({x:0,y:0}),g.find("Transformer").forEach(e=>{e.setAttr("oldVisible",e.visible()),e.visible(!1)}),m.find(".page-background").forEach(e=>e.shadowEnabled(!1)),m.find(".page-background").forEach(e=>e.strokeEnabled(!1)),m.find(".highlighter").forEach(e=>e.visible(!1));const f=m.findOne(".page-background-group"),y=f.clip();f.clip({x:null,y:null,width:null,height:null});const b=m.findOne(".elements-container"),v=b.clip();b.clip({x:null,y:null,width:null,height:null});const P=m.find(e=>e.getAttr("hideInExport"));P.forEach(e=>{e.setAttr("oldVisible",e.visible()),e.hide()});const x=m.find(e=>!e.visible()&&e.getAttr("editModeEnabled"));x.forEach(e=>{e.setAttr("oldVisible",e.visible()),e.show()}),i&&m.find(".page-background").forEach(e=>e.hide());const E=a?p.bleed:0;let _=E;!e.bleedVisible&&a||(e.bleedVisible||a?e.bleedVisible&&a?_=0:e.bleedVisible&&!a&&(_=-p.bleed):_=0);const O=document.createElement("canvas");O.width=Math.round((p.computedWidth+2*E)*c),O.height=Math.round((p.computedHeight+2*E)*c);const S=O.getContext("2d");"image/jpeg"===o&&(S.fillStyle="white",S.fillRect(0,0,O.width,O.height));const k=m.scale();m.scale({x:1,y:1});const I=m.toCanvas({x:m.x()-_,y:m.y()-_,width:p.computedWidth+2*E,height:p.computedHeight+2*E,pixelRatio:c});return m.scale(k),S.drawImage(I,0,0,O.width,O.height),d.default.Util.releaseCanvas(I),i&&m.find(".page-background").forEach(e=>e.show()),P.forEach(e=>{e.visible(e.getAttr("oldVisible"))}),x.forEach(e=>{e.visible(e.getAttr("oldVisible"))}),m.find(".page-background").forEach(e=>e.shadowEnabled(!0)),m.find(".page-background").forEach(e=>e.strokeEnabled(!0)),g.find("Transformer").forEach(e=>{e.visible(e.getAttr("oldVisible"))}),m.find(".highlighter").forEach(e=>e.visible(!0)),f.clip(y),b.clip(v),g.position(h),null==p||p.set({_exporting:!1,_forceMount:!1}),await new Promise(e=>setTimeout(e)),e.setElementsPixelRatio(u),O},async toDataURL(t={}){var{mimeType:i,quality:n}=t,a=o(t,["mimeType","quality"]);const s=await e._toCanvas(Object.assign({mimeType:i},a)),r=s.toDataURL(i,n);return d.default.Util.releaseCanvas(s),r},async toBlob(t={}){var{mimeType:i,quality:n}=t,a=o(t,["mimeType","quality"]);const s=await e._toCanvas(Object.assign({mimeType:i},a)),r=await new Promise(e=>{s.toBlob(e,i,n)});return d.default.Util.releaseCanvas(s),r},async saveAsImage(t={}){var{fileName:i}=t,n=o(t,["fileName"]);const a=n.mimeType||"image/png",s=a.split("/")[1];(0,p.downloadFile)(await e.toDataURL(n),i||"polotno."+s,a)},async _toPDF(t){const i=t.dpi||e.dpi,n=t.parallel||1,o=t.unit||("px"===e.unit?"mm":e.unit),a=t.pixelRatio||1,s=t.pageIds||e.pages.map(e=>e.id),r=e.pages.filter(e=>s.includes(e.id)),l=await(0,u.getJsPDF)(),d=e=>(0,b.pxToUnit)({px:e,unit:o,dpi:i}),c=t.cropMarkSize||0,p=d(c),g=r[0]||{},m=t.includeBleed?g.bleed:0,h=d(g.computedWidth+2*m+2*p),f=d(g.computedHeight+2*m+2*p);var y=new l({unit:o,orientation:h>f?"landscape":"portrait",format:[h,f],compress:!0,putOnlyUsedFonts:!0});y.deletePage(1);const v=((e,t)=>{for(var i=[],n=0;n<e.length;n+=t){i.push(e.slice(n,n+t))}return i})(r,n);let w=0;for(const u of v){const i=u.map(async i=>{const n=t.includeBleed?i.bleed:0,o=i.computedWidth+2*n+2*c,r=i.computedHeight+2*n+2*c,l=d(o),p=d(r);let u=0,g=a;for(;u<10;){u+=1,2===u&&console.error("Polotno can not export PDF with current settings. Quality is automatically reduced.");const n=await e.toDataURL(Object.assign(Object.assign({},t),{pageId:i.id,pixelRatio:g}));if(n.length>20){return t.onProgress&&t.onProgress(++w/s.length*.9),{url:n,width:l,height:p,widthPx:o,heightPx:r}}g*=.8}});(await Promise.all(i)).forEach(({url:e,width:t,height:i,widthPx:n,heightPx:a})=>{y.addPage([t,i],t>i?"landscape":"portrait");const s=y.getCurrentPageInfo();var r;switch(o){case"pt":r=1;break;case"mm":r=72/25.4;break;case"cm":r=72/2.54;break;case"in":r=72;break;case"px":r=.75;break;case"pc":case"em":r=12;break;case"ex":r=6;break;default:throw"Invalid unit: "+o}if(s.pageContext.cropBox={bottomLeftX:0,bottomLeftY:0,topRightX:t*r,topRightY:i*r},s.pageContext.artBox={bottomLeftX:d(c+m)*r,bottomLeftY:d(c+m)*r,topRightX:d(n-c-m)*r,topRightY:d(a-c-m)*r},s.pageContext.bleedBox={bottomLeftX:d(c+m)*r,bottomLeftY:d(c+m)*r,topRightX:d(n-c-m)*r,topRightY:d(a-c-m)*r},p){y.setLineWidth(d(1));const e=p+d(m);y.line(e,0,e,p),y.line(0,e,p,e),y.line(t-e,0,t-e,p),y.line(t,e,t-p,e),y.line(0,i-e,p,i-e),y.line(e,i,e,i-p),y.line(t,i-e,t-p,i-e),y.line(t-e,i,t-e,i-p)}y.addImage(e,p,p,t-2*p,i-2*p,void 0,"FAST")})}return y},toPDFDataURL:async t=>(await e._toPDF(Object.assign({mimeType:"image/jpeg"},t))).output("datauristring"),async toGIFDataURL(t={}){const i=t.pixelRatio||1,n=await(0,g.createGIF)({width:e.width*i,height:e.height*i}),o=1e3/(t.fps||10),a=e.duration/o;for(let s=0;s<a-1;s++){const t=s*o||1;e.setCurrentTime(t);let a=0,r="";for(const i of e.pages){if(a+=i.duration,i.set({_rendering:a>t}),a>t){r=i.id;break}}const l=await e._toCanvas({pixelRatio:i,pageId:r,_skipTimeout:!0});n.addFrame(l.getContext("2d"),{delay:o,copy:!0})}for(const s of e.pages){s.set({_rendering:!1})}return e.stop(),n.render(),new Promise(e=>{n.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,n=o(t,["fileName"]);const a=await e.toGIFDataURL(n);(0,p.downloadFile)(a,i||"polotno.gif")},async toHTML({elementHook:t}={elementHook:void 0}){const i=e.toJSON();return(0,P.jsonToHTML)({json:i,elementHook:t})},async saveAsHTML({fileName:t}={}){const i=await e.toHTML(),n="data:text/html;base64,"+window.btoa(unescape(encodeURIComponent(i)));(0,p.downloadFile)(n,t||"polotno.html")},async toSVG({elementHook:t,pageId:i}={elementHook:void 0,pageId:void 0}){var n;const o=e.toJSON();i=i||(null===(n=o.pages[0])||void 0===n?void 0:n.id);const a=o.pages.find(e=>e.id===i);return(0,x.jsonToSVG)({json:Object.assign(Object.assign({},o),{pages:a?[a]:[]}),elementHook:t})},async saveAsSVG({fileName:t,elementHook:i,pageId:n}={}){const o=await e.toSVG({elementHook:i,pageId:n}),a="data:text/svg;base64,"+window.btoa(unescape(encodeURIComponent(o)));(0,p.downloadFile)(a,t||"polotno.svg")},async saveAsPDF(t={}){var{fileName:i}=t,n=o(t,["fileName"]);(await e._toPDF(Object.assign({mimeType:"image/jpeg"},n))).save(i||"polotno.pdf")},async waitLoading({_skipTimeout:e=!1}={}){e||await new Promise(e=>setTimeout(e,50)),await(0,y.whenLoaded)()},toJSON:()=>({width:e.width,height:e.height,fonts:(0,s.getSnapshot)(e.fonts),pages:(0,s.getSnapshot)(e.pages),audios:(0,s.getSnapshot)(e.audios),unit:e.unit,dpi:e.dpi,custom:e.custom,schemaVersion:e.schemaVersion}),loadJSON(t,i=!1){var n;const o=JSON.parse(JSON.stringify(t)),a=o.schemaVersion||0;a<1&&f.flags.htmlRenderEnabled&&(0,_.forEveryChild)({children:o.pages},e=>{if("text"===e.type){const t=16,i=e.letterSpacing*t;e.letterSpacing=i/e.fontSize}}),a<2&&(0,_.forEveryChild)({children:o.pages},e=>{e.filters&&Object.keys(e.filters).forEach(t=>{if(["warm","cold","natural"].includes(t)){return}const i=e.filters[t];i&&"number"==typeof i.intensity&&(i.intensity=i.intensity/100)})}),delete o.schemaVersion;const r=e.pages.indexOf(e.activePage);let l=null===(n=o.pages[r]||o.pages[0])||void 0===n?void 0:n.id;o._activePageId=l;const d=Object.assign({},(0,s.getSnapshot)(e));Object.assign(d,o),d.history=i?e.history.toJSON():{history:[],undoIdx:-1},(0,s.applySnapshot)(e,d)},clear({keepHistory:t=!1}={}){const i=e.pages.map(e=>e.id);e.deletePages(i),e.custom=null,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,s.destroy)(e))},addAudio(t){const i=O.Audio.create(Object.assign({id:(0,l.nanoid)(10)},t));e.audios.push(i)},removeAudio(t){const i=e.audios.find(e=>e.id===t);i&&e.audios.remove(i)},async loadFont(t){const i=e.fonts.find(e=>e.fontFamily===t)||h.globalFonts.find(e=>e.fontFamily===t);let n=[{fontStyle:"normal",fontWeight:"normal"}];return i?(i.styles&&(n=i.styles.map(e=>({fontStyle:e.fontStyle||"normal",fontWeight:e.fontWeight||"normal"}))),h.injectCustomFont(i)):h.injectGoogleFont(t),Promise.all(n.map(e=>h.loadFont(t,e.fontStyle,e.fontWeight)))},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.default=S;
|
|
1
|
+
var e,t=this&&this.__createBinding||(Object.create?function(e,t,i,n){void 0===n&&(n=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,n,o)}:function(e,t,i,n){void 0===n&&(n=i),e[n]=t[i]}),i=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),n=this&&this.__importStar||(e=function(t){return e=Object.getOwnPropertyNames||function(e){var t=[];for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&(t[t.length]=i)}return t},e(t)},function(n){if(n&&n.__esModule){return n}var o={};if(null!=n){for(var a=e(n),s=0;s<a.length;s++){"default"!==a[s]&&t(o,n,a[s])}}return i(o,n),o}),o=this&&this.__rest||function(e,t){var i={};for(var n in e){Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(i[n]=e[n])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(e);o<n.length;o++){t.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(e,n[o])&&(i[n[o]]=e[n[o]])}}return i},a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Store=exports.Font=void 0,exports.createStore=S;const s=require("mobx-state-tree"),r=require("./history"),l=require("nanoid"),d=a(require("konva")),c=require("mobx"),p=require("../utils/download"),u=require("../utils/pdf"),g=require("../utils/gif-lib"),m=require("../utils/validate-key"),h=n(require("../utils/fonts")),f=require("../utils/flags"),y=require("../utils/loader"),b=require("../utils/unit"),v=require("../utils/deep-equal"),w=require("../utils/wait"),P=require("../utils/to-html"),x=require("../utils/to-svg"),E=require("./page-model"),_=require("./group-model"),O=require("./audio-model");function S({key:e,showCredit:t}={key:"",showCredit:!1}){return exports.Store.create({_forceShowCredit:t,_key:e})}(0,s.setLivelinessChecking)("ignore"),exports.Font=s.types.model("Font",{fontFamily:s.types.string,url:s.types.optional(s.types.string,""),styles:s.types.frozen()}).preProcessSnapshot(e=>Object.assign(Object.assign({},e),{fontFamily:e.fontFamily||e.name})),exports.Store=s.types.model("Store",{role:"",pages:s.types.array(E.Page),fonts:s.types.array(exports.Font),audios:s.types.array(O.Audio),width:1080,height:1080,currentTime:0,isPlaying:!1,scale:1,scaleToFit:1,unit:"px",dpi:72,schemaVersion:2,bleedVisible:!1,rulesVisible:!1,openedSidePanel:"",previousOpenedSidePanel:"",custom:s.types.frozen(),selectedElementsIds:s.types.array(s.types.string),animatedElementsIds:s.types.array(s.types.string),history:s.types.optional(r.UndoManager,{targetPath:"../pages"}),_elementsPixelRatio:Math.min(2,"undefined"!=typeof window&&window.devicePixelRatio||1),_activePageId:"",_forceShowCredit:!1,_key:"",_validated:!1}).views(e=>{const t=(0,c.computed)(()=>{const t={};return(0,_.forEveryChild)({children:e.pages},e=>(t[e.id]=e,!1)),t},{keepAlive:!0});return{get _idsMap(){return t.get()}}}).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(0,_.forEveryChild)({children:e.selectedElements},e=>{"group"!==e.type&&t.push(e)}),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 i;return(0,_.forEveryChild)({children:e.pages},e=>{if(!i&&t(e)){return i=e,!0}}),i},getElementById:t=>e._idsMap[t]})).actions(e=>{let t=0,i=null,n=!1;return{afterCreate(){e.history.canUndo},setCurrentTime(t){e.currentTime=t},_togglePlaying(t=!e.isPlaying){e.isPlaying=t},play({animatedElementsIds:o=[],startTime:a=0,currentTime:r=0,endTime:l=e.duration,repeat:d=!1}={}){r&&(console.warn("currentTime property of store.play() is deprecated. Please use startTime instead."),a=r),e.animatedElementsIds=(0,s.cast)(o),e.currentTime=a,e.isPlaying=!0,t=Date.now(),i=l,n=d,requestAnimationFrame(e.seek)},checkActivePage(){let t=0;for(const i of e.pages){if(e.currentTime>=i.startTime&&e.currentTime<i.startTime+i.duration){e.selectPage(i.id);break}t+=i.duration}},seek(){if(!e.isPlaying){return}const o=Date.now(),a=o-t;t=o,e.currentTime+=a,e.checkActivePage();const s=i||e.duration;e.isPlaying&&e.currentTime<s?requestAnimationFrame(e.seek):e.isPlaying&&n?(e.currentTime=0,requestAnimationFrame(e.seek)):e.stop()},stop(){e.isPlaying=!1,e.currentTime=0,e.animatedElementsIds=(0,s.cast)([]),e.checkActivePage()}}}).actions(e=>({__(){e._validated||((0,m.validateKey)(e._key,e._forceShowCredit),e._validated=!0)},set(t){Object.assign(e,t)},setUnit({unit:t,dpi:i}){e.unit=t||e.unit,e.dpi=i||e.dpi},setRole(t){e.role=t},addPage(t){const i=E.Page.create(Object.assign({id:(0,l.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,s.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&&(e.previousOpenedSidePanel=e.openedSidePanel,e.openedSidePanel=t)},setScale(t){e.scale=t},_setScaleToFit(t){e.scaleToFit=t},setElementsPixelRatio(t){e._elementsPixelRatio=t},setSize(t,i,n){e.pages.forEach(e=>{e.setSize({width:t,height:i,useMagic:n,softChange:!0})}),e.width=t,e.height=i},setPageZIndex(t,i){const n=e.pages.find(e=>e.id===t);n&&((0,s.detach)(n),e.pages.remove(n),e.pages.splice(i,0,n))},deletePages(t){const i=e.pages.indexOf(e.activePage);t.forEach(t=>{const i=e.pages.find(e=>e.id===t);(0,s.destroy)(i)});const n=Math.min(e.pages.length-1,i),o=e.pages[n];o&&(e._activePageId=o.id),e.selectedElementsIds=(0,s.cast)(e.selectedElementsIds.filter(t=>e.getElementById(t)))},groupElements(t,i={}){const n=t.map(t=>e.getElementById(t)),o=n[0].page;if(n.forEach(e=>{e&&(0,s.detach)(e)}),!n.length){return}const a=Object.assign({id:(0,l.nanoid)(10),children:n,type:"group"},i);return o.children.push(a),e.selectedElementsIds=(0,s.cast)([a.id]),o.children.find(e=>e.id===a.id)},ungroupElements(t){const i=t.map(t=>e.getElementById(t)),n=[];i.forEach(e=>{if(e&&"group"===e.type){const t=e.page,i=t.children.indexOf(e);e.children.forEach(e=>{n.push(e.id)}),e.children.forEach(e=>{(0,s.detach)(e),t.children.push(e)}),t.children.splice(i,1)}}),e.selectedElementsIds=(0,s.cast)(n)},deleteElements(t){const i=[];e.find(e=>(t.includes(e.id)&&i.push(e),!1)),i.forEach(e=>{(0,s.destroy)(e)}),e.selectedElementsIds=(0,s.cast)(e.selectedElementsIds.filter(t=>e.getElementById(t)))},on(t,i){if("change"===t){let t=e.toJSON();return(0,s.onSnapshot)(e,n=>{const o=e.toJSON();!(0,v.deepEqual)(t,o)&&(t=o,i(o))})}},async _toCanvas({pixelRatio:t,ignoreBackground:i,pageId:n,mimeType:o,includeBleed:a,_skipTimeout:s,quickMode:r=!1}={}){var l;const c=t||1;n=n||(null===(l=e.pages[0])||void 0===l?void 0:l.id);const p=e.pages.find(e=>e.id===n);if(!p){throw new Error(`No page for export with id ${n}`)}const u=e._elementsPixelRatio;c>e._elementsPixelRatio&&e.setElementsPixelRatio(c),r?null==p||p.set({_forceMount:!0}):null==p||p.set({_exporting:!0});const g=await(0,w.waitTillAvailable)(()=>d.default.stages.find(e=>e.getAttr("pageId")===n));if(!g){throw null==p||p.set({_forceMount:!1,_exporting:!1}),e.setElementsPixelRatio(u),new Error(`Export is failed. Can not find stage for page ${n}. Looks like <Workspace /> component is not mounted, but it is required in order to process the export.`)}await e.waitLoading({_skipTimeout:s});const m=g.findOne(".page-container");if(!m){throw e.setElementsPixelRatio(u),null==p||p.set({_forceMount:!1,_exporting:!1}),new Error("Export is failed. Canvas was unmounted.")}const h=g.position();g.position({x:0,y:0}),g.find("Transformer").forEach(e=>{e.setAttr("oldVisible",e.visible()),e.visible(!1)}),m.find(".page-background").forEach(e=>e.shadowEnabled(!1)),m.find(".page-background").forEach(e=>e.strokeEnabled(!1)),m.find(".highlighter").forEach(e=>e.visible(!1));const f=m.findOne(".page-background-group"),y=f.clip();f.clip({x:null,y:null,width:null,height:null});const b=m.findOne(".elements-container"),v=b.clip();b.clip({x:null,y:null,width:null,height:null});const P=m.find(e=>e.getAttr("hideInExport"));P.forEach(e=>{e.setAttr("oldVisible",e.visible()),e.hide()});const x=m.find(e=>!e.visible()&&e.getAttr("editModeEnabled"));x.forEach(e=>{e.setAttr("oldVisible",e.visible()),e.show()}),i&&m.find(".page-background").forEach(e=>e.hide());const E=a?p.bleed:0;let _=E;!e.bleedVisible&&a||(e.bleedVisible||a?e.bleedVisible&&a?_=0:e.bleedVisible&&!a&&(_=-p.bleed):_=0);const O=document.createElement("canvas");O.width=Math.round((p.computedWidth+2*E)*c),O.height=Math.round((p.computedHeight+2*E)*c);const S=O.getContext("2d");"image/jpeg"===o&&(S.fillStyle="white",S.fillRect(0,0,O.width,O.height));const k=m.scale();m.scale({x:1,y:1});const I=m.toCanvas({x:m.x()-_,y:m.y()-_,width:p.computedWidth+2*E,height:p.computedHeight+2*E,pixelRatio:c});return m.scale(k),S.drawImage(I,0,0,O.width,O.height),d.default.Util.releaseCanvas(I),i&&m.find(".page-background").forEach(e=>e.show()),P.forEach(e=>{e.visible(e.getAttr("oldVisible"))}),x.forEach(e=>{e.visible(e.getAttr("oldVisible"))}),m.find(".page-background").forEach(e=>e.shadowEnabled(!0)),m.find(".page-background").forEach(e=>e.strokeEnabled(!0)),g.find("Transformer").forEach(e=>{e.visible(e.getAttr("oldVisible"))}),m.find(".highlighter").forEach(e=>e.visible(!0)),f.clip(y),b.clip(v),g.position(h),null==p||p.set({_exporting:!1,_forceMount:!1}),await new Promise(e=>setTimeout(e)),e.setElementsPixelRatio(u),O},async toDataURL(t={}){var{mimeType:i,quality:n}=t,a=o(t,["mimeType","quality"]);const s=await e._toCanvas(Object.assign({mimeType:i},a)),r=s.toDataURL(i,n);return d.default.Util.releaseCanvas(s),r},async toBlob(t={}){var{mimeType:i,quality:n}=t,a=o(t,["mimeType","quality"]);const s=await e._toCanvas(Object.assign({mimeType:i},a)),r=await new Promise(e=>{s.toBlob(e,i,n)});return d.default.Util.releaseCanvas(s),r},async saveAsImage(t={}){var{fileName:i}=t,n=o(t,["fileName"]);const a=n.mimeType||"image/png",s=a.split("/")[1];(0,p.downloadFile)(await e.toDataURL(n),i||"polotno."+s,a)},async _toPDF(t){const i=t.dpi||e.dpi,n=t.parallel||1,o=t.unit||("px"===e.unit?"mm":e.unit),a=t.pixelRatio||1,s=t.pageIds||e.pages.map(e=>e.id),r=e.pages.filter(e=>s.includes(e.id)),l=await(0,u.getJsPDF)(),d=e=>(0,b.pxToUnit)({px:e,unit:o,dpi:i}),c=t.cropMarkSize||0,p=d(c),g=r[0]||{},m=t.includeBleed?g.bleed:0,h=d(g.computedWidth+2*m+2*p),f=d(g.computedHeight+2*m+2*p);var y=new l({unit:o,orientation:h>f?"landscape":"portrait",format:[h,f],compress:!0,putOnlyUsedFonts:!0});y.deletePage(1);const v=((e,t)=>{for(var i=[],n=0;n<e.length;n+=t){i.push(e.slice(n,n+t))}return i})(r,n);let w=0;for(const u of v){const i=u.map(async i=>{const n=t.includeBleed?i.bleed:0,o=i.computedWidth+2*n+2*c,r=i.computedHeight+2*n+2*c,l=d(o),p=d(r);let u=0,g=a;for(;u<10;){u+=1,2===u&&console.error("Polotno can not export PDF with current settings. Quality is automatically reduced.");const n=await e.toDataURL(Object.assign(Object.assign({},t),{pageId:i.id,pixelRatio:g}));if(n.length>20){return t.onProgress&&t.onProgress(++w/s.length*.9),{url:n,width:l,height:p,widthPx:o,heightPx:r}}g*=.8}});(await Promise.all(i)).forEach(({url:e,width:t,height:i,widthPx:n,heightPx:a})=>{y.addPage([t,i],t>i?"landscape":"portrait");const s=y.getCurrentPageInfo();var r;switch(o){case"pt":r=1;break;case"mm":r=72/25.4;break;case"cm":r=72/2.54;break;case"in":r=72;break;case"px":r=.75;break;case"pc":case"em":r=12;break;case"ex":r=6;break;default:throw"Invalid unit: "+o}if(s.pageContext.cropBox={bottomLeftX:0,bottomLeftY:0,topRightX:t*r,topRightY:i*r},s.pageContext.artBox={bottomLeftX:d(c+m)*r,bottomLeftY:d(c+m)*r,topRightX:d(n-c-m)*r,topRightY:d(a-c-m)*r},s.pageContext.bleedBox={bottomLeftX:d(c+m)*r,bottomLeftY:d(c+m)*r,topRightX:d(n-c-m)*r,topRightY:d(a-c-m)*r},p){y.setLineWidth(d(1));const e=p+d(m);y.line(e,0,e,p),y.line(0,e,p,e),y.line(t-e,0,t-e,p),y.line(t,e,t-p,e),y.line(0,i-e,p,i-e),y.line(e,i,e,i-p),y.line(t,i-e,t-p,i-e),y.line(t-e,i,t-e,i-p)}y.addImage(e,p,p,t-2*p,i-2*p,void 0,"FAST")})}return y},toPDFDataURL:async t=>(await e._toPDF(Object.assign({mimeType:"image/jpeg"},t))).output("datauristring"),async toGIFDataURL(t={}){const i=t.pixelRatio||1,n=await(0,g.createGIF)({width:e.width*i,height:e.height*i}),o=1e3/(t.fps||10),a=e.duration/o;for(let s=0;s<a-1;s++){const t=s*o||1;e.setCurrentTime(t);let a=0,r="";for(const i of e.pages){if(a+=i.duration,i.set({_rendering:a>t}),a>t){r=i.id;break}}const l=await e._toCanvas({pixelRatio:i,pageId:r,_skipTimeout:!0});n.addFrame(l.getContext("2d"),{delay:o,copy:!0})}for(const s of e.pages){s.set({_rendering:!1})}return e.stop(),n.render(),new Promise(e=>{n.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,n=o(t,["fileName"]);const a=await e.toGIFDataURL(n);(0,p.downloadFile)(a,i||"polotno.gif")},async toHTML({elementHook:t}={elementHook:void 0}){const i=e.toJSON();return(0,P.jsonToHTML)({json:i,elementHook:t})},async saveAsHTML({fileName:t}={}){const i=await e.toHTML(),n="data:text/html;base64,"+window.btoa(unescape(encodeURIComponent(i)));(0,p.downloadFile)(n,t||"polotno.html")},async toSVG({elementHook:t,pageId:i}={elementHook:void 0,pageId:void 0}){var n;const o=e.toJSON();i=i||(null===(n=o.pages[0])||void 0===n?void 0:n.id);const a=o.pages.find(e=>e.id===i);return(0,x.jsonToSVG)({json:Object.assign(Object.assign({},o),{pages:a?[a]:[]}),elementHook:t})},async saveAsSVG({fileName:t,elementHook:i,pageId:n}={}){const o=await e.toSVG({elementHook:i,pageId:n}),a="data:text/svg;base64,"+window.btoa(unescape(encodeURIComponent(o)));(0,p.downloadFile)(a,t||"polotno.svg")},async saveAsPDF(t={}){var{fileName:i}=t,n=o(t,["fileName"]);(await e._toPDF(Object.assign({mimeType:"image/jpeg"},n))).save(i||"polotno.pdf")},async waitLoading({_skipTimeout:e=!1}={}){e||await new Promise(e=>setTimeout(e,50)),await(0,y.whenLoaded)()},toJSON:()=>({width:e.width,height:e.height,fonts:(0,s.getSnapshot)(e.fonts),pages:(0,s.getSnapshot)(e.pages),audios:(0,s.getSnapshot)(e.audios),unit:e.unit,dpi:e.dpi,custom:e.custom,schemaVersion:e.schemaVersion}),loadJSON(t,i=!1){var n;const o=JSON.parse(JSON.stringify(t)),a=o.schemaVersion||0;a<1&&f.flags.htmlRenderEnabled&&(0,_.forEveryChild)({children:o.pages},e=>{if("text"===e.type){const t=16,i=e.letterSpacing*t;e.letterSpacing=i/e.fontSize}}),a<2&&(0,_.forEveryChild)({children:o.pages},e=>{e.filters&&Object.keys(e.filters).forEach(t=>{if(["warm","cold","natural"].includes(t)){return}const i=e.filters[t];i&&"number"==typeof i.intensity&&(i.intensity=i.intensity/100)})}),delete o.schemaVersion;const r=e.pages.indexOf(e.activePage);let l=null===(n=o.pages[r]||o.pages[0])||void 0===n?void 0:n.id;o._activePageId=l;const d=Object.assign({},(0,s.getSnapshot)(e));Object.assign(d,o),d.history=i?e.history.toJSON():{history:[],undoIdx:-1},(0,s.applySnapshot)(e,d)},clear({keepHistory:t=!1}={}){const i=e.pages.map(e=>e.id);e.deletePages(i),e.custom=null,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,s.destroy)(e))},addAudio(t){const i=O.Audio.create(Object.assign({id:(0,l.nanoid)(10)},t));e.audios.push(i)},removeAudio(t){const i=e.audios.find(e=>e.id===t);i&&e.audios.remove(i)},async loadFont(t){const i=e.fonts.find(e=>e.fontFamily===t)||h.globalFonts.find(e=>e.fontFamily===t);let n=[{fontStyle:"normal",fontWeight:"normal"},{fontStyle:"normal",fontWeight:"bold"}];return i?(i.styles&&(n=i.styles.map(e=>({fontStyle:e.fontStyle||"normal",fontWeight:e.fontWeight||"normal"}))),h.injectCustomFont(i)):h.injectGoogleFont(t),Promise.all(n.map(e=>h.loadFont(t,e.fontStyle,e.fontWeight)))},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.default=S;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "polotno",
|
|
3
|
-
"version": "2.25.
|
|
3
|
+
"version": "2.25.4",
|
|
4
4
|
"description": "Design Editor Framework",
|
|
5
5
|
"author": "Anton Lavrenov",
|
|
6
6
|
"keywords": [
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"functions-have-names": "^1.2.3",
|
|
19
19
|
"gifuct-js": "^2.1.2",
|
|
20
20
|
"gradient-parser": "^1.1.1",
|
|
21
|
-
"konva": "^9.3.
|
|
21
|
+
"konva": "^9.3.22",
|
|
22
22
|
"mensch": "^0.3.4",
|
|
23
23
|
"mobx": "6.13.7",
|
|
24
24
|
"mobx-react-lite": "^4.1.0",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"rasterizehtml": "^1.3.1",
|
|
29
29
|
"react-color": "^2.19.3",
|
|
30
30
|
"react-konva": "18.2.12",
|
|
31
|
-
"react-konva-utils": "^1.1.
|
|
31
|
+
"react-konva-utils": "^1.1.1",
|
|
32
32
|
"react-sortablejs": "6.1.4",
|
|
33
33
|
"react-window": "^1.8.11",
|
|
34
34
|
"sortablejs": "^1.15.6",
|