polotno 4.8.0 → 4.9.0

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.
@@ -0,0 +1,3 @@
1
+ type DownscaleSource = HTMLCanvasElement | HTMLImageElement | HTMLVideoElement;
2
+ export declare function downScaleCanvas(image: DownscaleSource, scale: number): HTMLCanvasElement;
3
+ export {};
@@ -0,0 +1 @@
1
+ export function downScaleCanvas(t,e){const a=document.createElement("canvas"),{width:n,height:h}=function(t){return t instanceof HTMLVideoElement?{width:t.videoWidth,height:t.videoHeight}:{width:t.width,height:t.height}}(t);a.width=n,a.height=h;const o=a.getContext("2d");o.drawImage(t,0,0,n,h);const r=Math.max(1,Math.floor(n*e)),d=Math.max(1,Math.floor(h*e)),i=function(t,e,a){const n=new ImageData(e,a),h=new Int32Array(t.data.buffer),o=new Int32Array(n.data.buffer),r=e/t.width,d=a/t.height,i=Math.round(1/r),u=Math.round(1/d),c=i*u;for(let f=0;f<a;f++){for(let a=0;a<e;a++){const n=Math.round(a/r)+Math.round(f/d)*t.width;let g=0,w=0,M=0,l=0;for(let e=0;e<u;e++){for(let a=0;a<i;a++){const o=h[n+a+e*t.width],r=o>>>24;g+=(o<<24>>>24)*r,w+=(o<<16>>>24)*r,M+=(o<<8>>>24)*r,l+=r}}const s=l||1;g=Math.round(g/s),w=Math.round(w/s),M=Math.round(M/s),l=Math.round(l/c),o[a+f*e]=l<<24|M<<16|w<<8|g}}return n}(o.getImageData(0,0,n,h),r,d);return a.width=r,a.height=d,o.putImageData(i,0,0),a}
@@ -18,7 +18,7 @@ export declare const useCornerRadiusAndCrop: (element: ImageElementType, image:
18
18
  y: number;
19
19
  width: number;
20
20
  height: number;
21
- }, pixelRatio: number, cornerRadius?: number, skipDownscale?: boolean, isVector?: boolean) => HTMLCanvasElement | HTMLImageElement | undefined;
21
+ }, pixelRatio: number, cornerRadius?: number, skipDownscale?: boolean, isVector?: boolean, borderInset?: number) => HTMLCanvasElement | HTMLImageElement | undefined;
22
22
  type ImageOrSVGElementType = ImageElementType & {
23
23
  maskSrc?: string;
24
24
  colorsReplace?: Map<string, string>;
@@ -1 +1 @@
1
- import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import o from"react";import{observer as i}from"mobx-react-lite";import{isAlive as a}from"mobx-state-tree";import{Arc as n,Group as h,Image as s,Rect as c,Text as d,Transformer as g}from"react-konva";import{ROLES as l}from"../model/store.js";import u from"use-image";import p from"konva";import{Portal as m}from"react-konva-utils";import{useWorkspaceStyle as f}from"./workspace-style.js";import{incrementLoader as w,triggerLoadError as x}from"../utils/loader.js";import{polotnoError as M}from"@polotno/core/errors";import{getCropRect as y}from"@polotno/core/image";import*as v from"@polotno/core/svg";import{flags as b}from"../utils/flags.js";import{trySetCanvasSize as E}from"../utils/canvas.js";import{roundEnabledPx as S,activeAnchorOf as I,isCornerAnchor as C,scaledBoxStyleAttrs as R,roundedBoxStyleAttrs as X}from"../utils/px.js";import{useFilteredSource as Y}from"./use-filtered-source.js";import{useFadeIn as O}from"./use-fadein.js";import{isTouchDevice as T}from"../utils/screen.js";import{useDelayer as j}from"./use-delayer.js";import{useColor as D}from"./use-color.js";function W(){return document.createElement("canvas")}export const useSizeFixer=t=>{const[e,r]=o.useState(t);return o.useEffect(()=>{(async()=>{const o=await async function(t){if(!v.isSvgUrl(t)){return t}const e=await v.urlToString(t),r=v.fixSize(e);return v.svgToURL(r)}(t);o!==e&&r(o)})()},[t]),e};function z(t,e){return{x:(t.x+e.x)/2,y:(t.y+e.y)/2}}const k=(t,e)=>e.width<5||e.height<5?t:e,A={keepRatio:!1,rotateEnabled:!1,boundBoxFunc:k},L={enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],rotateEnabled:!1,borderEnabled:!1,boundBoxFunc:k};export function maskCompositeSize(t,e){const r=y({stretchEnabled:e.stretchEnabled,cropX:0,cropY:0,cropWidth:1,cropHeight:1,width:Math.max(e.width,1),height:Math.max(e.height,1)},t);return{width:Math.max(Math.round(r.width),1),height:Math.max(Math.round(r.height),1)}}const H=(t,e,r)=>Math.max(e,Math.min(r,t));export const useCornerRadiusAndCrop=(t,e,r,i,a=0,n=!1,h=!0)=>{var s,c;const d=Math.floor(H(t.a.width*i,1,1e4)),g=Math.floor(H(t.a.height*i,1,1e4)),l=Math.min(a*i,d/2,g/2),u=Math.max(t.a.width/r.width,t.a.height/r.height)*i,m=(null===(s=t.page)||void 0===s?void 0:s._exportingOrRendering)&&b.imageDownScalingEnabled&&u<1&&!n,f=0===r.x&&0===r.y&&r.width===(null==e?void 0:e.width)&&r.height===(null==e?void 0:e.height),w=o.useMemo(()=>{if(e&&e.width&&e.height){return f&&0===l&&!m?void 0:W()}},[e,l,m,f]);return o.useLayoutEffect(()=>{if(!w||!e){return}E(w,d,g);const t=w.getContext("2d");if(!t){return}l&&(t.beginPath(),t.moveTo(l,0),t.lineTo(d-l,0),t.arc(d-l,l,l,3*Math.PI/2,0,!1),t.lineTo(d,g-l),t.arc(d-l,g-l,l,0,Math.PI/2,!1),t.lineTo(l,g),t.arc(l,g-l,l,Math.PI/2,Math.PI,!1),t.lineTo(0,l),t.arc(l,l,l,Math.PI,3*Math.PI/2,!1),t.clip());const o=m?function(t,e){var r,o;const i=W();i.width=t.width,i.height=t.height;const a=Math.max(1,Math.floor(i.width*e)),n=Math.max(1,Math.floor(i.height*e));null===(r=i.getContext("2d"))||void 0===r||r.drawImage(t,0,0,i.width,i.height);const h=function(t,e,r,o,i,a,n){for(var h=new ImageData(e,r),s=new Int32Array(t.data.buffer),c=t.width,d=new Int32Array(h.data.buffer),g=h.width,l=e/a,u=r/n,p=Math.round(1/l),m=Math.round(1/u),f=p*m,w=0;w<h.height;w++){for(var x=0;x<g;x++){for(var M=0+Math.round(x/l)+(0+Math.round(w/u))*c,y=0,v=0,b=0,E=0,S=0;S<m;S++){for(var I=0;I<p;I++){var C=s[M+I+S*c];y+=C<<24>>>24,v+=C<<16>>>24,b+=C<<8>>>24,E+=C>>>24}}y=Math.round(y/f),v=Math.round(v/f),b=Math.round(b/f),E=Math.round(E/f),d[x+w*g]=E<<24|b<<16|v<<8|y}}return h}(i.getContext("2d").getImageData(0,0,i.width,i.height),a,n,0,0,i.width,i.height);return i.width=a,i.height=n,null===(o=i.getContext("2d"))||void 0===o||o.putImageData(h,0,0),i}(e,u):e,i=m?{x:Math.floor(r.x*u),y:Math.floor(r.y*u),width:Math.floor(r.width*u),height:Math.floor(r.height*u)}:r;t.drawImage(o,i.x,i.y,i.width,i.height,0,0,w.width,w.height)},[w,t.a.width,t.a.height,r.x,r.y,r.width,r.height,a,i,n,null===(c=t.page)||void 0===c?void 0:c._exportingOrRendering,m]),o.useEffect(()=>()=>{w&&"CANVAS"===w.nodeName&&p.Util.releaseCanvas(w)},[w]),w||e};const _=W(),P=i(({element:r})=>{const i=Math.min(30,r.a.width/4,r.a.height/4),a=o.useRef(null);o.useEffect(()=>{const t=a.current;if(!t){return}const e=new p.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}});const{mediaLoadingStyle:s}=f();return e(h,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:s.fill}),t(n,{ref:a,x:r.a.width/2,y:r.a.height/2,fill:s.textFill,outerRadius:Math.abs(i),innerRadius:Math.max(1,i-5),angle:270})]})}),F=i(({element:r})=>{const{mediaErrorStyle:o}=f(),i=Math.max(10,Math.min(30,r.a.width/25));return e(h,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:o.fill}),t(d,{text:"Can not load the image...",fontSize:i,width:r.a.width,height:r.a.height,align:"center",fill:o.textFill,verticalAlign:"middle",padding:5})]})});let U=u;export const setImageLoaderHook=t=>{U=t};export const useImageLoader=(t,e="",r="")=>{const i=o.useRef(void 0),a=()=>{var t;null===(t=i.current)||void 0===t||t.call(i),i.current=void 0};o.useEffect(()=>a,[]),o.useLayoutEffect(()=>{const o=e.slice(0,200),n=`image with id ${r} url: ${o}`;"loading"!==t||i.current||(i.current=w(n)),"loading"!==t&&a(),"failed"===t&&x(M("IMAGE_FAILED",n,{src:o,elementId:r,reason:"load"}))},[t])};export const ImageElement=i(({element:i,store:n})=>{var d,u,w;const x=f(),[M,b]=o.useState(!1),k=o.useRef(null),H=o.useRef(null),N=i.isSelected,[B,V]=(t=>{const[e,r]=o.useReducer(t=>t+1,0),i=o.useRef("loading"),a=o.useRef(t.src),n=o.useRef(t.src);n.current!==t.src&&(n.current=t.src,i.current="loading");const h=o.useMemo(()=>v.isSvgUrl(t.src)||"svg"===t.type,[t.src,t.type]);return o.useEffect(()=>{if(!h){return}if(!t.src){return}let e=!1;return(async()=>{i.current="loading",r();const o=await v.urlToString(t.src),n=v.fixSize(o);let h;h=t.colorsReplace?v.replaceColors(n,t.colorsReplace||new Map):v.svgToURL(n),e||(a.current=h,i.current="loaded",r())})(),()=>{e=!0}},[t.src,JSON.stringify(t.colorsReplace)]),h?[a.current,i.current]:[t.src,"loaded"]})(i),[$,q]=U(B,"anonymous"),G="svg"!==i.type||"loaded"===V?q:"loading";useImageLoader(G,i.src,i.id);const J=(null===(d=i.page)||void 0===d?void 0:d._exportingOrRendering)?1:Math.max(1,n.scale),K=n._elementsPixelRatio*J,Q=(({image:t,status:e,type:r})=>{const i=o.useRef(void 0);return o.useEffect(()=>{i.current=t||i.current},[t]),"failed"!==e||"failed"!==e&&"svg"===r?i.current:void 0})({image:$,status:q,type:i.type}),Z=((t,e,r)=>{const i=o.useMemo(()=>{var o,i;const{flipX:a,flipY:n}=t,h="svg"===t.type||v.isSvgUrl(t.src),s=navigator.userAgent.toLowerCase().indexOf("firefox")>-1,c=(/^((?!chrome|android).)*safari/i.test(navigator.userAgent)||s)&&h||t.maskSrc;if(!a&&!n&&!c){return e}if(!e||!e.width||!e.height){return null}const d=W();let g=1;"svg"===t.type&&(g=Math.max(t.a.width/e.width*r,t.a.height/e.height*r)),E(d,Math.max(e.width*g,1),Math.max(e.height*g,1));let l=a?-d.width:0,u=n?-d.height:0;return null===(o=d.getContext("2d"))||void 0===o||o.scale(a?-1:1,n?-1:1),null===(i=d.getContext("2d"))||void 0===i||i.drawImage(e,l,u,d.width,d.height),d},[t.maskSrc,t.flipX,t.flipY,e,t.a.width,t.a.height,r]);return o.useEffect(()=>()=>{i&&"CANVAS"===i.nodeName&&p.Util.releaseCanvas(i)},[i]),i})(i,$||Q,K),tt=((t,e)=>{const r=useSizeFixer(t.maskSrc||""),[i,a]=U(r,"anonymous");return useImageLoader(r?a:"loaded",r||"",t.id),o.useMemo(()=>{if(!i){return e}if(!e||!e.width||!e.height){return e}const{width:r,height:o}=maskCompositeSize(e,{width:t.a.width,height:t.a.height,stretchEnabled:t.stretchEnabled}),a=W();a.width=r,a.height=o;const n=a.getContext("2d");if(!n){return e}n.drawImage(e,0,0,r,o,0,0,r,o),n.globalCompositeOperation="source-in";const h=function(t,e){const r=e.width/e.height;let o,i;return r>=t.width/t.height?(o=t.width,i=t.width/r):(o=t.height*r,i=t.height),{x:(t.width-o)/2,y:(t.height-i)/2,width:o,height:i}}(i,t);return n.drawImage(i,h.x,h.y,h.width,h.height,0,0,r,o),a},[e,i,t.a.width,t.a.height,t.stretchEnabled])})(i,Z)||_,[et=_]=Y(i,tt);let{cropX:rt,cropY:ot,cropWidth:it,cropHeight:at}=i.a;"loaded"!==q&&(rt=ot=0,it=at=1);const nt=y({stretchEnabled:i.stretchEnabled,cropX:rt,cropY:ot,cropWidth:it,cropHeight:at,width:i.a.width,height:i.a.height},et),ht=nt.width,st=nt.height,ct=i.stretchEnabled,dt=null!==(u=i.cornerRadius)&&void 0!==u?u:0,gt=(null===(w=i.page)||void 0===w?void 0:w._exportingOrRendering)?1:Math.min(2,n.scale),lt=n._elementsPixelRatio*gt;let ut=((t,e,r,i)=>{const a=useSizeFixer(t.clipSrc||""),[n,h]=U(a,"anonymous"),s=t.clipSrc?h:"loaded";useImageLoader(s,t.clipSrc,t.id);const c=o.useMemo(()=>{if(e&&n){return W()}},[e,n]);return o.useLayoutEffect(()=>{var o;if(!n){return}if(!e||!e.width||!e.height){return}if(!n||!n.width||!n.height){return}if(!c){return}const i=W(),a=Math.max(t.a.width/n.width*r,t.a.height/n.height*r);i.width=Math.max(n.width*a,1),i.height=Math.max(n.height*a,1),null===(o=i.getContext("2d"))||void 0===o||o.drawImage(n,0,0,i.width,i.height),c.width=Math.max(e.width,1),c.height=Math.max(e.height,1);const h=c.getContext("2d");h&&(h.save(),h.drawImage(i,0,0,e.width,e.height),p.Util.releaseCanvas(i),h.globalCompositeOperation="source-in",h.drawImage(e,0,0,c.width,c.height),h.restore())},[c,e,n,t.a.width,t.a.height,r,...i]),t.clipSrc&&n?c:e})(i,useCornerRadiusAndCrop(i,et,nt,lt,dt,M||i._cropModeEnabled||"svg"===i.type,!0),lt,[nt,dt,lt]);const pt=Math.max(i.a.width/ht,i.a.height/st);o.useEffect(()=>{var t;if(!i._cropModeEnabled){return}const e=null===(t=k.current)||void 0===t?void 0:t.getStage();function r(t){i._cropModeEnabled&&t.target!==H.current&&i.toggleCropMode(!1)}function o(t){i._cropModeEnabled&&t.target.parentNode!==(null==e?void 0:e.content)&&i.toggleCropMode(!1)}return document.body.addEventListener("click",o),null==e||e.on("click",r),null==e||e.on("tap",r),()=>{document.body.removeEventListener("click",o),document.body.removeEventListener("touchstart",o),null==e||e.off("click",r),null==e||e.off("click",r)}},[i._cropModeEnabled]);const mt=o.useRef(null),ft=o.useRef(null),wt=o.useRef(null);o.useLayoutEffect(()=>{i._cropModeEnabled&&(ft.current.nodes([mt.current]),wt.current.nodes([H.current]))},[i._cropModeEnabled]);var xt=o.useRef(null),Mt=o.useRef(0),yt=o.useRef(!1);const vt=t=>{var e;(null===(e=t.evt.touches)||void 0===e?void 0:e.length)>2&&t.target.stopDrag(),Math.round(t.target.x())>0&&(t.target.x(0),t.target.scaleX(1)),Math.round(t.target.y())>0&&(t.target.y(0),t.target.scaleY(1));const r=t.target.width()*t.target.scaleX(),o=t.target.height()*t.target.scaleY(),a=Math.min(1,ht/r),n=Math.min(1,st/o),h=1-a,s=Math.min(h,Math.max(0,Math.round(-t.target.x())/r)),c=1-n,d=Math.min(c,Math.max(0,Math.round(-t.target.y())/o));t.target.setAttrs({x:-s*et.width,y:-d*et.height,scaleX:1,scaleY:1}),i.set({cropX:s,cropY:d,cropWidth:a,cropHeight:n})},bt=()=>{"svg"!==i.type&&i.contentEditable&&(i.stretchEnabled||setTimeout(()=>{a(i)&&i.toggleCropMode(!0)}))},Et="svg"===i.type&&Q,St="loading"===q&&!Et,[It]=j(St,100,!1,!1),Ct="failed"===q,Rt=!It&&!Ct,Xt=o.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Yt=o.useRef(!1),Ot=Rt?i.a.opacity:0;O(k,Ot);const Tt=i.selectable||n.role===l.ADMIN,jt=T(),Dt=D(i,i.borderColor,"stroke");return e(r,{children:[It&&t(P,{element:i}),Ct&&t(F,{element:i}),t(s,{ref:k,name:"element",id:i.id,image:ut,x:i.a.x,y:i.a.y,width:i.a.width||1,height:i.a.height||1,rotation:i.a.rotation,opacity:Ot,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,customCrop:nt,listening:Tt,draggable:jt?i.draggable&&N:i.draggable,preventDefault:!jt||N,hideInExport:!i.showInExport,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDblClick:bt,onDblTap:bt,onTransformStart:t=>{b(!0),Yt.current=C(I(t.target)),Xt.current={cropX:i.cropX,cropY:i.cropY,cropWidth:i.cropWidth,cropHeight:i.cropHeight}},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),o=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const a=1-ht/et.width,n=Math.min(a,Math.max(0,i.cropX)),h=1-st/et.height,s=Math.min(h,Math.max(0,i.cropY)),c=Yt.current,d=!c&&r<1&&Xt.current.cropHeight>st/et.height;let g=c?i.cropWidth:i.cropWidth*r;d&&(g=i.cropWidth);const l=!c&&o<1&&Xt.current.cropWidth>ht/et.width;let u=c?i.cropHeight:i.cropHeight*o;l&&(u=i.cropHeight),ct&&(g=i.cropWidth,u=i.cropHeight);const p=c?Math.min(r,o):1;i.set(Object.assign({cropX:n,cropY:s,x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*o,rotation:t.target.rotation(),cropWidth:Math.min(g,1-n),cropHeight:Math.min(u,1-s)},1!==p&&Object.assign(Object.assign({},R(i,p)),{borderSize:i.borderSize*p})))},onTransformEnd:t=>{const e=t.currentTarget;i.set(Object.assign(Object.assign({width:e.width(),height:e.height(),x:e.x(),y:e.y(),rotation:t.target.rotation(),cropWidth:ht/et.width,cropHeight:st/et.height},X(i)),{borderSize:S(i.borderSize)})),b(!1)}}),t(c,Object.assign({x:i.x,y:i.y,width:Math.max(i.a.width-i.borderSize,0),height:Math.max(i.a.height-i.borderSize,0),opacity:Ot,offsetX:-i.borderSize/2,offsetY:-i.borderSize/2},Dt,{strokeWidth:i.borderSize,listening:!1,visible:!!i.borderSize,rotation:i.rotation,cornerRadius:Math.max(0,dt-i.borderSize),hideInExport:!i.showInExport})),i._cropModeEnabled&&e(m,{selector:".page-abs-container",enabled:!0,children:[t(c,{x:-window.innerWidth/n.scale,y:-window.innerWidth/n.scale,width:window.innerWidth/n.scale*3,height:window.innerWidth/n.scale*3,fill:x.cropOverlayFill}),t(s,{listening:!1,image:ut,x:i.x,y:i.y,width:i.a.width,height:i.a.height,rotation:i.rotation,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur}),e(h,{x:i.x,y:i.y,rotation:i.rotation,scaleX:pt,scaleY:pt,children:[t(s,{image:et,ref:H,opacity:.4,draggable:!0,x:-i.cropX*et.width,y:-i.cropY*et.height,onDragMove:vt,onTransform:vt,onTouchMove:t=>{t.evt.preventDefault();const e=t.target.getStage().getPointersPositions();var r=e[0],o=e[1];const i=t.target;if(r&&!o&&!i.isDragging()&&yt.current&&(i.startDrag(),yt.current=!1),r&&o){p.hitOnDragEnabled=!0,i.isDragging()&&(yt.current=!0,i.stopDrag());const e=t.target.getAbsoluteTransform().copy();e.invert();var a={x:r.x,y:r.y},n={x:o.x,y:o.y};if(!xt.current){return void(xt.current=z(a,n))}var h=z(a,n),s=function(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}(a,n);Mt.current||(Mt.current=s);const m=i.position();var c={x:h.x-m.x,y:h.y-m.y},d=s/Mt.current;i.scaleX(d),i.scaleY(d);const f=e.point(h),w=e.point(xt.current);var g=f.x-w.x,l=f.y-w.y,u={x:Math.min(0,h.x-c.x*d+g),y:Math.min(0,h.y-c.y*d+l)};i.position(u),Mt.current=s,xt.current=h,vt(t)}},onTouchEnd:t=>{Mt.current=0,xt.current=null,p.hitOnDragEnabled=!1}}),t(g,Object.assign({ref:wt},L,x.outerImageCropTransformerStyle)),t(c,{width:ht,height:st,ref:mt,listening:!1,onTransform:t=>{if(i.cropX<Math.abs(t.target.x()/et.width)&&t.target.x()<0&&i.cropX>0){const e=(i.cropWidth+i.cropX)*et.width;t.target.scaleX(1),t.target.width(e)}if(i.cropY<Math.abs(t.target.y()/et.height)&&t.target.y()<0&&i.cropY>0){const e=(i.cropHeight+i.cropY)*et.height;t.target.scaleY(1),t.target.height(e)}t.target.x()<-i.cropX*et.width-1e-9&&(t.target.x(-i.cropX*et.width),t.target.scaleX(1)),t.target.y()<-i.cropY*et.height-1e-9&&(t.target.y(-i.cropY*et.height),t.target.scaleY(1));const e=Math.min(1,Math.max(0,i.cropX+t.target.x()/et.width)),r=Math.min(1,Math.max(0,t.target.y()/et.height+i.cropY)),o=t.target.width()*t.target.scaleX(),a=t.target.height()*t.target.scaleY(),n=Math.min(1-e,o/et.width),h=Math.min(1-r,a/et.height),s=t.target.getAbsolutePosition(t.target.getParent().getParent());t.target.scale({x:1,y:1}),t.target.position({x:0,y:0}),i.set({x:s.x,y:s.y,cropX:e,cropY:r,cropWidth:n,cropHeight:h,width:Math.min(o*pt,et.width*(1-e)*pt),height:Math.min(a*pt,et.height*(1-r)*pt)})}}),t(g,Object.assign({ref:ft},A,x.innerImageCropTransformerStyle,{visible:i.resizable}))]})]})]})});
1
+ import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import o from"react";import{observer as i}from"mobx-react-lite";import{isAlive as a}from"mobx-state-tree";import{Arc as n,Group as h,Image as s,Rect as c,Text as d,Transformer as l}from"react-konva";import{ROLES as g}from"../model/store.js";import u from"use-image";import p from"konva";import{Portal as m}from"react-konva-utils";import{useWorkspaceStyle as f}from"./workspace-style.js";import{incrementLoader as w,triggerLoadError as x}from"../utils/loader.js";import{polotnoError as y}from"@polotno/core/errors";import{getBorderInset as M,getCropRect as b}from"@polotno/core/image";import*as v from"@polotno/core/svg";import{flags as E}from"../utils/flags.js";import{trySetCanvasSize as S}from"../utils/canvas.js";import{roundEnabledPx as R,activeAnchorOf as C,isCornerAnchor as X,scaledBoxStyleAttrs as Y,roundedBoxStyleAttrs as I}from"../utils/px.js";import{useFilteredSource as O}from"./use-filtered-source.js";import{useFadeIn as j}from"./use-fadein.js";import{isTouchDevice as z}from"../utils/screen.js";import{useDelayer as W}from"./use-delayer.js";import{useColor as k}from"./use-color.js";import{downScaleCanvas as D}from"./downscale.js";function L(){return document.createElement("canvas")}export const useSizeFixer=t=>{const[e,r]=o.useState(t);return o.useEffect(()=>{(async()=>{const o=await async function(t){if(!v.isSvgUrl(t)){return t}const e=await v.urlToString(t),r=v.fixSize(e);return v.svgToURL(r)}(t);o!==e&&r(o)})()},[t]),e};function T(t,e){return{x:(t.x+e.x)/2,y:(t.y+e.y)/2}}const A=(t,e)=>e.width<5||e.height<5?t:e,H={keepRatio:!1,rotateEnabled:!1,boundBoxFunc:A},_={enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],rotateEnabled:!1,borderEnabled:!1,boundBoxFunc:A};export function maskCompositeSize(t,e){const r=b({stretchEnabled:e.stretchEnabled,cropX:0,cropY:0,cropWidth:1,cropHeight:1,width:Math.max(e.width,1),height:Math.max(e.height,1)},t);return{width:Math.max(Math.round(r.width),1),height:Math.max(Math.round(r.height),1)}}const F=(t,e,r)=>Math.max(e,Math.min(r,t));export const useCornerRadiusAndCrop=(t,e,r,i,a=0,n=!1,h=!0,s=0)=>{var c,d;const l=Math.floor(F(t.a.width*i,1,1e4)),g=Math.floor(F(t.a.height*i,1,1e4)),u=Math.min(a*i,l/2,g/2),m=Math.min(s*i,l/2,g/2),f=Math.max(t.a.width/r.width,t.a.height/r.height)*i,w=(null===(c=t.page)||void 0===c?void 0:c._exportingOrRendering)&&E.imageDownScalingEnabled&&f<1&&!n,x=0===r.x&&0===r.y&&r.width===(null==e?void 0:e.width)&&r.height===(null==e?void 0:e.height),y=o.useMemo(()=>{if(e&&e.width&&e.height){return!x||0!==u||w||m?L():void 0}},[e,u,w,x,m]);return o.useLayoutEffect(()=>{if(!y||!e){return}S(y,l,g);const t=y.getContext("2d");if(!t){return}(u||m)&&(t.beginPath(),t.roundRect(m,m,l-2*m,g-2*m,Math.max(0,u-m)),t.clip());const o=w?D(e,f):e,i=w?{x:Math.floor(r.x*f),y:Math.floor(r.y*f),width:Math.floor(r.width*f),height:Math.floor(r.height*f)}:r;t.drawImage(o,i.x,i.y,i.width,i.height,0,0,y.width,y.height)},[y,t.a.width,t.a.height,r.x,r.y,r.width,r.height,a,i,n,null===(d=t.page)||void 0===d?void 0:d._exportingOrRendering,w,m]),o.useEffect(()=>()=>{y&&"CANVAS"===y.nodeName&&p.Util.releaseCanvas(y)},[y]),y||e};const P=L(),U=i(({element:r})=>{const i=Math.min(30,r.a.width/4,r.a.height/4),a=o.useRef(null);o.useEffect(()=>{const t=a.current;if(!t){return}const e=new p.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}});const{mediaLoadingStyle:s}=f();return e(h,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:s.fill}),t(n,{ref:a,x:r.a.width/2,y:r.a.height/2,fill:s.textFill,outerRadius:Math.abs(i),innerRadius:Math.max(1,i-5),angle:270})]})}),N=i(({element:r})=>{const{mediaErrorStyle:o}=f(),i=Math.max(10,Math.min(30,r.a.width/25));return e(h,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:o.fill}),t(d,{text:"Can not load the image...",fontSize:i,width:r.a.width,height:r.a.height,align:"center",fill:o.textFill,verticalAlign:"middle",padding:5})]})});let B=u;export const setImageLoaderHook=t=>{B=t};export const useImageLoader=(t,e="",r="")=>{const i=o.useRef(void 0),a=()=>{var t;null===(t=i.current)||void 0===t||t.call(i),i.current=void 0};o.useEffect(()=>a,[]),o.useLayoutEffect(()=>{const o=e.slice(0,200),n=`image with id ${r} url: ${o}`;"loading"!==t||i.current||(i.current=w(n)),"loading"!==t&&a(),"failed"===t&&x(y("IMAGE_FAILED",n,{src:o,elementId:r,reason:"load"}))},[t])};export const ImageElement=i(({element:i,store:n})=>{var d,u,w;const x=f(),[y,E]=o.useState(!1),D=o.useRef(null),A=o.useRef(null),F=i.isSelected,[V,$]=(t=>{const[e,r]=o.useReducer(t=>t+1,0),i=o.useRef("loading"),a=o.useRef(t.src),n=o.useRef(t.src);n.current!==t.src&&(n.current=t.src,i.current="loading");const h=o.useMemo(()=>v.isSvgUrl(t.src)||"svg"===t.type,[t.src,t.type]);return o.useEffect(()=>{if(!h){return}if(!t.src){return}let e=!1;return(async()=>{i.current="loading",r();const o=await v.urlToString(t.src),n=v.fixSize(o);let h;h=t.colorsReplace?v.replaceColors(n,t.colorsReplace||new Map):v.svgToURL(n),e||(a.current=h,i.current="loaded",r())})(),()=>{e=!0}},[t.src,JSON.stringify(t.colorsReplace)]),h?[a.current,i.current]:[t.src,"loaded"]})(i),[q,G]=B(V,"anonymous"),J="svg"!==i.type||"loaded"===$?G:"loading";useImageLoader(J,i.src,i.id);const K=(null===(d=i.page)||void 0===d?void 0:d._exportingOrRendering)?1:Math.max(1,n.scale),Q=n._elementsPixelRatio*K,Z=(({image:t,status:e,type:r})=>{const i=o.useRef(void 0);return o.useEffect(()=>{i.current=t||i.current},[t]),"failed"!==e||"failed"!==e&&"svg"===r?i.current:void 0})({image:q,status:G,type:i.type}),tt=((t,e,r)=>{const i=o.useMemo(()=>{var o,i;const{flipX:a,flipY:n}=t,h="svg"===t.type||v.isSvgUrl(t.src),s=navigator.userAgent.toLowerCase().indexOf("firefox")>-1,c=(/^((?!chrome|android).)*safari/i.test(navigator.userAgent)||s)&&h||t.maskSrc;if(!a&&!n&&!c){return e}if(!e||!e.width||!e.height){return null}const d=L();let l=1;"svg"===t.type&&(l=Math.max(t.a.width/e.width*r,t.a.height/e.height*r)),S(d,Math.max(e.width*l,1),Math.max(e.height*l,1));let g=a?-d.width:0,u=n?-d.height:0;return null===(o=d.getContext("2d"))||void 0===o||o.scale(a?-1:1,n?-1:1),null===(i=d.getContext("2d"))||void 0===i||i.drawImage(e,g,u,d.width,d.height),d},[t.maskSrc,t.flipX,t.flipY,e,t.a.width,t.a.height,r]);return o.useEffect(()=>()=>{i&&"CANVAS"===i.nodeName&&p.Util.releaseCanvas(i)},[i]),i})(i,q||Z,Q),et=((t,e)=>{const r=useSizeFixer(t.maskSrc||""),[i,a]=B(r,"anonymous");return useImageLoader(r?a:"loaded",r||"",t.id),o.useMemo(()=>{if(!i){return e}if(!e||!e.width||!e.height){return e}const{width:r,height:o}=maskCompositeSize(e,{width:t.a.width,height:t.a.height,stretchEnabled:t.stretchEnabled}),a=L();a.width=r,a.height=o;const n=a.getContext("2d");if(!n){return e}n.drawImage(e,0,0,r,o,0,0,r,o),n.globalCompositeOperation="source-in";const h=function(t,e){const r=e.width/e.height;let o,i;return r>=t.width/t.height?(o=t.width,i=t.width/r):(o=t.height*r,i=t.height),{x:(t.width-o)/2,y:(t.height-i)/2,width:o,height:i}}(i,t);return n.drawImage(i,h.x,h.y,h.width,h.height,0,0,r,o),a},[e,i,t.a.width,t.a.height,t.stretchEnabled])})(i,tt)||P,[rt=P]=O(i,et);let{cropX:ot,cropY:it,cropWidth:at,cropHeight:nt}=i.a;"loaded"!==G&&(ot=it=0,at=nt=1);const ht=b({stretchEnabled:i.stretchEnabled,cropX:ot,cropY:it,cropWidth:at,cropHeight:nt,width:i.a.width,height:i.a.height},rt),st=ht.width,ct=ht.height,dt=i.stretchEnabled,lt=null!==(u=i.cornerRadius)&&void 0!==u?u:0,gt=(null===(w=i.page)||void 0===w?void 0:w._exportingOrRendering)?1:Math.min(2,n.scale),ut=n._elementsPixelRatio*gt;let pt=((t,e,r,i)=>{const a=useSizeFixer(t.clipSrc||""),[n,h]=B(a,"anonymous"),s=t.clipSrc?h:"loaded";useImageLoader(s,t.clipSrc,t.id);const c=o.useMemo(()=>{if(e&&n){return L()}},[e,n]);return o.useLayoutEffect(()=>{var o;if(!n){return}if(!e||!e.width||!e.height){return}if(!n||!n.width||!n.height){return}if(!c){return}const i=L(),a=Math.max(t.a.width/n.width*r,t.a.height/n.height*r);i.width=Math.max(n.width*a,1),i.height=Math.max(n.height*a,1),null===(o=i.getContext("2d"))||void 0===o||o.drawImage(n,0,0,i.width,i.height),c.width=Math.max(e.width,1),c.height=Math.max(e.height,1);const h=c.getContext("2d");h&&(h.save(),h.drawImage(i,0,0,e.width,e.height),p.Util.releaseCanvas(i),h.globalCompositeOperation="source-in",h.drawImage(e,0,0,c.width,c.height),h.restore())},[c,e,n,t.a.width,t.a.height,r,...i]),t.clipSrc&&n?c:e})(i,useCornerRadiusAndCrop(i,rt,ht,ut,lt,y||i._cropModeEnabled||"svg"===i.type,!0,M({borderSize:i.borderSize,borderColor:i.borderColor,width:i.a.width,height:i.a.height})),ut,[ht,lt,ut]);const mt=Math.max(i.a.width/st,i.a.height/ct);o.useEffect(()=>{var t;if(!i._cropModeEnabled){return}const e=null===(t=D.current)||void 0===t?void 0:t.getStage();function r(t){i._cropModeEnabled&&t.target!==A.current&&i.toggleCropMode(!1)}function o(t){i._cropModeEnabled&&t.target.parentNode!==(null==e?void 0:e.content)&&i.toggleCropMode(!1)}return document.body.addEventListener("click",o),null==e||e.on("click",r),null==e||e.on("tap",r),()=>{document.body.removeEventListener("click",o),document.body.removeEventListener("touchstart",o),null==e||e.off("click",r),null==e||e.off("click",r)}},[i._cropModeEnabled]);const ft=o.useRef(null),wt=o.useRef(null),xt=o.useRef(null);o.useLayoutEffect(()=>{i._cropModeEnabled&&(wt.current.nodes([ft.current]),xt.current.nodes([A.current]))},[i._cropModeEnabled]);var yt=o.useRef(null),Mt=o.useRef(0),bt=o.useRef(!1);const vt=t=>{var e;(null===(e=t.evt.touches)||void 0===e?void 0:e.length)>2&&t.target.stopDrag(),Math.round(t.target.x())>0&&(t.target.x(0),t.target.scaleX(1)),Math.round(t.target.y())>0&&(t.target.y(0),t.target.scaleY(1));const r=t.target.width()*t.target.scaleX(),o=t.target.height()*t.target.scaleY(),a=Math.min(1,st/r),n=Math.min(1,ct/o),h=1-a,s=Math.min(h,Math.max(0,Math.round(-t.target.x())/r)),c=1-n,d=Math.min(c,Math.max(0,Math.round(-t.target.y())/o));t.target.setAttrs({x:-s*rt.width,y:-d*rt.height,scaleX:1,scaleY:1}),i.set({cropX:s,cropY:d,cropWidth:a,cropHeight:n})},Et=()=>{"svg"!==i.type&&i.contentEditable&&(i.stretchEnabled||setTimeout(()=>{a(i)&&i.toggleCropMode(!0)}))},St="svg"===i.type&&Z,Rt="loading"===G&&!St,[Ct]=W(Rt,100,!1,!1),Xt="failed"===G,Yt=!Ct&&!Xt,It=o.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Ot=o.useRef(!1),jt=Yt?i.a.opacity:0;j(D,jt);const zt=i.selectable||n.role===g.ADMIN,Wt=z(),kt=k(i,i.borderColor,"stroke");return e(r,{children:[Ct&&t(U,{element:i}),Xt&&t(N,{element:i}),t(s,{ref:D,name:"element",id:i.id,image:pt,x:i.a.x,y:i.a.y,width:i.a.width||1,height:i.a.height||1,rotation:i.a.rotation,opacity:jt,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,customCrop:ht,listening:zt,draggable:Wt?i.draggable&&F:i.draggable,preventDefault:!Wt||F,hideInExport:!i.showInExport,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDblClick:Et,onDblTap:Et,onTransformStart:t=>{E(!0),Ot.current=X(C(t.target)),It.current={cropX:i.cropX,cropY:i.cropY,cropWidth:i.cropWidth,cropHeight:i.cropHeight}},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),o=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const a=1-st/rt.width,n=Math.min(a,Math.max(0,i.cropX)),h=1-ct/rt.height,s=Math.min(h,Math.max(0,i.cropY)),c=Ot.current,d=!c&&r<1&&It.current.cropHeight>ct/rt.height;let l=c?i.cropWidth:i.cropWidth*r;d&&(l=i.cropWidth);const g=!c&&o<1&&It.current.cropWidth>st/rt.width;let u=c?i.cropHeight:i.cropHeight*o;g&&(u=i.cropHeight),dt&&(l=i.cropWidth,u=i.cropHeight);const p=c?Math.min(r,o):1;i.set(Object.assign({cropX:n,cropY:s,x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*o,rotation:t.target.rotation(),cropWidth:Math.min(l,1-n),cropHeight:Math.min(u,1-s)},1!==p&&Object.assign(Object.assign({},Y(i,p)),{borderSize:i.borderSize*p})))},onTransformEnd:t=>{const e=t.currentTarget;i.set(Object.assign(Object.assign({width:e.width(),height:e.height(),x:e.x(),y:e.y(),rotation:t.target.rotation(),cropWidth:st/rt.width,cropHeight:ct/rt.height},I(i)),{borderSize:R(i.borderSize)})),E(!1)}}),t(c,Object.assign({x:i.x,y:i.y,width:Math.max(i.a.width-i.borderSize,0),height:Math.max(i.a.height-i.borderSize,0),opacity:jt,offsetX:-i.borderSize/2,offsetY:-i.borderSize/2},kt,{strokeWidth:i.borderSize,listening:!1,visible:!!i.borderSize,rotation:i.rotation,cornerRadius:Math.max(0,lt-i.borderSize),hideInExport:!i.showInExport})),i._cropModeEnabled&&e(m,{selector:".page-abs-container",enabled:!0,children:[t(c,{x:-window.innerWidth/n.scale,y:-window.innerWidth/n.scale,width:window.innerWidth/n.scale*3,height:window.innerWidth/n.scale*3,fill:x.cropOverlayFill}),t(s,{listening:!1,image:pt,x:i.x,y:i.y,width:i.a.width,height:i.a.height,rotation:i.rotation,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur}),e(h,{x:i.x,y:i.y,rotation:i.rotation,scaleX:mt,scaleY:mt,children:[t(s,{image:rt,ref:A,opacity:.4,draggable:!0,x:-i.cropX*rt.width,y:-i.cropY*rt.height,onDragMove:vt,onTransform:vt,onTouchMove:t=>{t.evt.preventDefault();const e=t.target.getStage().getPointersPositions();var r=e[0],o=e[1];const i=t.target;if(r&&!o&&!i.isDragging()&&bt.current&&(i.startDrag(),bt.current=!1),r&&o){p.hitOnDragEnabled=!0,i.isDragging()&&(bt.current=!0,i.stopDrag());const e=t.target.getAbsoluteTransform().copy();e.invert();var a={x:r.x,y:r.y},n={x:o.x,y:o.y};if(!yt.current){return void(yt.current=T(a,n))}var h=T(a,n),s=function(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}(a,n);Mt.current||(Mt.current=s);const m=i.position();var c={x:h.x-m.x,y:h.y-m.y},d=s/Mt.current;i.scaleX(d),i.scaleY(d);const f=e.point(h),w=e.point(yt.current);var l=f.x-w.x,g=f.y-w.y,u={x:Math.min(0,h.x-c.x*d+l),y:Math.min(0,h.y-c.y*d+g)};i.position(u),Mt.current=s,yt.current=h,vt(t)}},onTouchEnd:t=>{Mt.current=0,yt.current=null,p.hitOnDragEnabled=!1}}),t(l,Object.assign({ref:xt},_,x.outerImageCropTransformerStyle)),t(c,{width:st,height:ct,ref:ft,listening:!1,onTransform:t=>{if(i.cropX<Math.abs(t.target.x()/rt.width)&&t.target.x()<0&&i.cropX>0){const e=(i.cropWidth+i.cropX)*rt.width;t.target.scaleX(1),t.target.width(e)}if(i.cropY<Math.abs(t.target.y()/rt.height)&&t.target.y()<0&&i.cropY>0){const e=(i.cropHeight+i.cropY)*rt.height;t.target.scaleY(1),t.target.height(e)}t.target.x()<-i.cropX*rt.width-1e-9&&(t.target.x(-i.cropX*rt.width),t.target.scaleX(1)),t.target.y()<-i.cropY*rt.height-1e-9&&(t.target.y(-i.cropY*rt.height),t.target.scaleY(1));const e=Math.min(1,Math.max(0,i.cropX+t.target.x()/rt.width)),r=Math.min(1,Math.max(0,t.target.y()/rt.height+i.cropY)),o=t.target.width()*t.target.scaleX(),a=t.target.height()*t.target.scaleY(),n=Math.min(1-e,o/rt.width),h=Math.min(1-r,a/rt.height),s=t.target.getAbsolutePosition(t.target.getParent().getParent());t.target.scale({x:1,y:1}),t.target.position({x:0,y:0}),i.set({x:s.x,y:s.y,cropX:e,cropY:r,cropWidth:n,cropHeight:h,width:Math.min(o*mt,rt.width*(1-e)*mt),height:Math.min(a*mt,rt.height*(1-r)*mt)})}}),t(l,Object.assign({ref:wt},H,x.innerImageCropTransformerStyle,{visible:i.resizable}))]})]})]})});
@@ -1 +1 @@
1
- var t=this&&this.__rest||function(t,e){var r={};for(var o in t){Object.prototype.hasOwnProperty.call(t,o)&&e.indexOf(o)<0&&(r[o]=t[o])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(o=Object.getOwnPropertySymbols(t);n<o.length;n++){e.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(t,o[n])&&(r[o[n]]=t[o[n]])}}return r};import{jsx as e,Fragment as r,jsxs as o}from"react/jsx-runtime";import n from"react";import{observer as i}from"mobx-react-lite";import{Group as a,Path as s,Rect as l,Shape as c}from"react-konva";import{nodeAutorun as h}from"../utils/mobx.js";import{isAlive as d}from"mobx-state-tree";import{Html as u}from"react-konva-utils";import{layout as f,drawLayout as g}from"render-tag";import{layoutTextOnPath as m,drawTextOnPathLayout as x}from"render-tag/path";import{activeAnchorOf as p,scaledFontSizeAttrs as y,roundedFontSizeAttrs as w,roundedTextDecorationAttrs as b,textMinResizeWidth as v}from"../utils/px.js";import{flags as E}from"../utils/flags.js";import{applyFilter as O}from"./apply-filters.js";import{getLineHeight as M,usePrevious as S}from"./text-element.js";import{useFontSpecsLoader as j}from"./text-element/font-specs-loader.js";import{getHtml as z,getCurvePath as k,getCurveTextHeight as R}from"@polotno/core/text-html";import{useColor as Y}from"./use-color.js";import{isGradient as X}from"@polotno/core/gradient";import{detectSize as F}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as I}from"@polotno/core/background-shape";import{useFadeIn as P}from"./use-fadein.js";import{incrementLoader as C}from"../utils/loader.js";import{removeTags as T}from"@polotno/core/text";import{isTouchDevice as B}from"../utils/screen.js";import{getLimitedFontSize as W}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as D,extractFontSpecsFromHtml as N,scaleRichTextFontSizesInHtml as $}from"../utils/rich-text-html.js";import{RichEditor as J}from"./rich-editor.js";import{findFitFontSize as _}from"./text-element/fit-font-size.js";import{visiblePlaceholder as A}from"./text-element/placeholder.js";import{wrapHtmlForRenderTag as L}from"@polotno/core/reset-style";import{xmlEscape as V}from"@polotno/core/xml";import{hasMeasuredHeight as H,resolveTextBoxHeight as q}from"@polotno/core/text-types";export{L as wrapHtmlForRenderTag};function G(t,e){return t!==e&&(!E.textVerticalResizeEnabled||t<e)}export const RenderTagElement=i(({element:i,store:K})=>{var Q;const U=n.useRef(null),Z=n.useRef(null),[tt,et]=n.useState(!1),rt=n.useRef(i.height),ot=i.isSelected,nt=S(ot),{textVerticalResizeEnabled:it}=E,at=E.autoDeleteEmptyText,st=S(i.fontFamily),lt=n.useMemo(()=>D(i.text,{fontSize:i.fontSize,fontFamily:i.fontFamily}),[i.text,i.fontSize,i.fontFamily]),ct=n.useMemo(()=>N(lt,{fontFamily:i.fontFamily,fontWeight:i.fontWeight,fontStyle:i.fontStyle}),[lt,i.fontFamily,i.fontWeight,i.fontStyle]),[ht]=j(K,ct,i.text||i.placeholder),dt=i._editModeEnabled;P(U);const ut=ht?i.fontFamily:st&&st!==i.fontFamily?st:"Arial",ft=Y(i).fill,gt=X(i.fill),mt=X(i.stroke)&&!!i.strokeWidth,xt=!!i.strokeWidth&&(mt||gt),pt="ellipsis"===E.textOverflow&&!i.curveEnabled&&H(i)&&!xt,yt=A(i.placeholder),wt=!!yt&&!T(i.text),bt=wt?V(yt):lt,vt=wt?.6:i.a.opacity,Et=z(i,{fontFamily:ut,color:ft,forEditor:!pt,textOverflow:E.textOverflow,text:bt,textIsNormalized:!0}),Ot=i.width||1,Mt=i.curveEnabled,St=function(t,e,r,o){return n.useMemo(()=>{if(o){return null}try{return f({html:L(t),width:Math.max(1,e),accuracy:"performance"})}catch(r){return console.error("render-tag layout failed:",r),null}},[t,e,r,o])}(Et,Ot,ht,Mt),jt=function(t,e,r){return n.useMemo(()=>{if(!e.curveEnabled){return null}const r=e.width||1,o=R(e),n=k(r,o,e.curvePower,e.fontSize);try{return m({html:L(t),path:n,align:"center",textBaseline:"middle"})}catch(i){return console.error("render-tag/path layoutTextOnPath failed:",i),null}},[r,e.curveEnabled,e.curvePower,e.fontSize,e.width,e.height,t])}(Et,i,ht),zt=M({fontLoaded:ht,fontFamily:i.fontFamily,fontSize:i.fontSize,lineHeight:i.lineHeight}),kt=Mt?0:Math.max(null!==(Q=null==St?void 0:St.height)&&void 0!==Q?Q:0,i.fontSize*zt),Rt=jt?jt.bounds:null,Yt=n.useMemo(()=>St?St.lines.map(t=>({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height})):[],[St]),Xt=n.useRef([]),Ft=n.useRef(null),It=n.useRef(!0),[,Pt]=n.useReducer(t=>t+1,0);n.useEffect(()=>{const t=Xt.current;t.length>0&&(Xt.current=[],t.forEach(t=>t()))});const Ct=n.useCallback((t,e,r,o)=>{const n=C(`text ${i.id}`),a=()=>{K.history.ignore(t,e,r).then(()=>{It.current?(Xt.current.push(n),Pt()):n()})};o?queueMicrotask(a):a()},[i.id,K]),Tt=n.useCallback(t=>{const e=null!==Ft.current;Ft.current=t,e||Ct(()=>{const t=Ft.current;Ft.current=null,d(i)&&G(i.height,t)&&i.set({height:t})},!1,!0)},[i,Ct]);n.useEffect(()=>{if(!ht){return}if(!St){return}if(!H(i)){return void Ct(()=>{d(i)&&(H(i)||i.set({height:kt}))},void 0,void 0,!0)}if(K.isPlaying){return}const{textOverflow:t}=E,e=G(i.height,kt),r=()=>Tt(kt);if("change-font-size"!==t||tt||dt||i.curveEnabled){"resize"===t&&e&&!tt&&r()}else{const t=_(wt?{toJSON:()=>Object.assign(Object.assign({},i.toJSON()),{text:bt})}:i,{fontFamily:ut,color:ft,forEditor:!0},t=>{try{return f({html:L(t),width:Ot,accuracy:"performance"}).height}catch(Q){return F(t).height}});t.fontSize!==i.fontSize?Ct(()=>{if(!d(i)){return}const e=y(i,t.fontSize);void 0!==t.text&&t.text!==i.text&&(e.text=t.text),i.set(e)},!1,!0):e&&r()}}),n.useEffect(()=>()=>{It.current=!1;const t=Xt.current;t.length>0&&(Xt.current=[],t.forEach(t=>t()))},[]),n.useLayoutEffect(()=>{if(!ht||!i.curveEnabled||tt){return}if(!Rt||!Rt.width){return}const t=t=>t&&it?Math.max(t,Rt.height):Rt.height;Math.abs(Rt.width-i.width)<1&&Math.abs(t(i.height)-i.height)<1||Ct(()=>{if(!d(i)){return}const e=Rt.width-i.width,r=t(i.height),o=r-i.height;if(Math.abs(e)<1&&Math.abs(o)<1){return}const n=i.a.rotation*Math.PI/180,a=-e/2*Math.cos(n),s=-e/2*Math.sin(n);i.set({width:Rt.width,height:r,x:i.a.x+a,y:i.a.y+s})},!1,!0,!0)},[ht,i.curveEnabled,Rt,tt,it,Ct]),n.useEffect(()=>{nt&&!ot&&at&&""===T(i.text)&&i.removable&&!A(i.placeholder)&&K.deleteElements([i.id])},[at,i.placeholder,i.removable,i.text,ot,nt]),n.useEffect(()=>h(i,()=>{const t=U.current;t&&O(t,i)}),[St,jt,i.a.width,i.a.height,i.shadowColor,i.shadowOffsetX,i.shadowOffsetY,i.shadowOpacity]);const Bt=q(i,kt),Wt=Mt||!ht||tt||K.isPlaying||"resize"!==E.textOverflow?i.a.height:it?Math.max(i.a.height,kt):kt;let Dt=0;Mt||"middle"!==i.verticalAlign?Mt||"bottom"!==i.verticalAlign||(Dt=Bt-kt):Dt=(Bt-kt)/2;const Nt=i.backgroundPadding*(i.fontSize*zt*.5),$t=i.backgroundCornerRadius*(i.fontSize*zt*.5),Jt=n.useMemo(()=>!i.backgroundEnabled||i.curveEnabled||i.legacyBackground||0===Yt.length?"":I({rects:Yt,padding:Nt,cornerRadius:$t}),[i.backgroundEnabled,i.curveEnabled,i.legacyBackground,Yt,Nt,$t]),_t=B(),At=n.useRef(null),Lt=n.useRef(i.width),Vt=n.useRef(null),Ht=n.useRef(null),qt=n.useRef(""),Gt=n.useRef(0),Kt=n.useRef(0),Qt=i.width>0?i.a.width/i.width:1;return o(r,{children:[i.curveEnabled||i.legacyBackground?e(l,{x:i.a.x,y:i.a.y,offsetX:Nt/Qt,offsetY:Nt/Qt,rotation:i.a.rotation,scaleX:Qt,scaleY:Qt,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled,opacity:i.backgroundOpacity*vt,fill:i.backgroundColor,width:i.width+2*Nt,height:Bt+2*Nt,cornerRadius:$t}):e(s,{name:"rendertag-background",x:i.a.x,y:i.a.y,offsetY:-Dt/Qt,rotation:i.a.rotation,scaleX:Qt,scaleY:Qt,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled&&Jt.length>0,opacity:i.backgroundOpacity*vt,fill:i.backgroundColor,data:Jt}),e(c,{ref:U,name:"rendertag-text",x:i.a.x,y:i.a.y,rotation:i.a.rotation,scaleX:Qt,scaleY:Qt,offsetY:Qt>0?-Dt/Qt:0,width:i.width,height:Math.max(i.height,kt),listening:!1,visible:!dt&&!!(Mt?jt:St),opacity:vt,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,hideInExport:!i.showInExport,sceneFunc:(t,e)=>{const r=t._context;if(r){try{if(Mt){if(!jt||!Rt){return}const t=(i.width-Rt.width)/2-Rt.x;r.save(),r.translate(t,-Rt.y),x({layout:jt,ctx:r}),r.restore()}else{if(!St){return}g({layout:St,width:Ot,ctx:r,pixelRatio:1})}}catch(o){console.error("render-tag draw failed:",o)}}}}),e(l,{ref:Z,name:"element",x:i.a.x,y:i.a.y,listening:i.selectable,rotation:i.a.rotation,width:i.a.width,height:Wt,visible:!dt,draggable:_t?i.draggable&&ot:i.draggable,preventDefault:!_t||ot,opacity:0,hideInExport:!0,id:i.id,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDblClick:t=>{i.contentEditable&&(Ht.current={x:t.evt.clientX,y:t.evt.clientY},i.toggleEditMode(!0))},onDblTap:t=>{var e;if(i.contentEditable){const r=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Ht.current=r?{x:r.clientX,y:r.clientY}:null,i.toggleEditMode(!0)}},onTransformStart:t=>{et(!0),rt.current=i.height,Lt.current=i.width,Vt.current=null,At.current=t.target.position(),qt.current=p(t.target),Gt.current=i.a.rotation,Kt.current=i.height},onTransform:e=>{const r=e.target,o=qt.current,n="middle-left"===o||"middle-right"===o,a="top-center"===o||"bottom-center"===o,s=r.scaleX();if(n){const e=r.scaleX(),o=r.width()*e;let n=Math.max(o,v(Lt.current,i.fontSize));const a=i.toJSON();let s=0;if(i.curveEnabled){s=R(Object.assign(Object.assign({},a),{width:n}))}else{const{x:e,y:r,width:o,height:i}=a,l=t(a,["x","y","width","height"]),c=`${JSON.stringify(l)}|${ut}|${ht}|${ft}`;if(Vt.current&&Vt.current.signature===c&&n<=Vt.current.width){n=Vt.current.width,s=Vt.current.height}else{const t=t=>{const e=z(Object.assign(Object.assign({},a),{width:t}),{fontFamily:ut,color:ft,forEditor:!0,text:bt,textIsNormalized:!0});try{return f({html:L(e),width:t,accuracy:"performance"})}catch(Q){return{height:F(e).height,lines:[]}}};let e=t(n);const r=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width));r>n+.5&&r<=Lt.current+.5&&(n=Math.min(Math.ceil(r),Lt.current),e=t(n),Vt.current={signature:c,width:n,height:e.height}),s=e.height}}n>o&&At.current&&r.position(At.current),r.width(n),r.scaleX(1),r.scaleY(1);let l=E.textVerticalResizeEnabled?Math.max(s,rt.current):s;"ellipsis"===E.textOverflow&&(l=rt.current),r.height(l),i.set({width:r.width(),height:l,x:r.x(),y:r.y()})}else if(a){const t="resize"===E.textOverflow?Mt?(null==Rt?void 0:Rt.height)||i.fontSize:kt:zt*i.fontSize,e=r.height()*r.scaleY();if(e<t){if(r.height(t),At.current){if("top-center"===qt.current){const e=Gt.current*Math.PI/180,o=Kt.current-t;r.x(At.current.x-o*Math.sin(e)),r.y(At.current.y+o*Math.cos(e))}else{r.position(At.current)}}}else{r.height(e)}r.scaleY(1),r.scaleX(1),Kt.current=Math.max(t,e),i.set({x:r.x(),y:r.y(),height:r.height(),rotation:r.rotation()})}else{r.scaleX(1),r.scaleY(1),i.set({fontSize:i.fontSize*s,letterSpacing:i.letterSpacing,width:r.width()*s,x:r.x(),y:r.y(),rotation:r.rotation(),height:r.height()*s,shadowBlur:i.shadowBlur*s,shadowOffsetX:i.shadowOffsetX*s,shadowOffsetY:i.shadowOffsetY*s,strokeWidth:i.strokeWidth*s,text:$(i.text,s)})}At.current=e.target.position()},onTransformEnd:t=>{et(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),i.set(Object.assign(Object.assign({},w(i,i.fontSize*e)),{width:t.target.width()*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),text:$(i.text,e)}))}}),dt&&e(a,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,offsetY:-Dt,children:e(u,{divProps:{style:{zIndex:20}},children:e(J,{element:i,onChange:t=>{const e=W({oldText:T(i.text),newText:T(t),element:i});i.set(Object.assign({text:t},y(i,e)))},onBlur:()=>{d(i)&&(i.set(b(i)),i.toggleEditMode(!1))},clickCoords:Ht.current})})})]})});
1
+ var t=this&&this.__rest||function(t,e){var r={};for(var o in t){Object.prototype.hasOwnProperty.call(t,o)&&e.indexOf(o)<0&&(r[o]=t[o])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(o=Object.getOwnPropertySymbols(t);n<o.length;n++){e.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(t,o[n])&&(r[o[n]]=t[o[n]])}}return r};import{jsx as e,Fragment as r,jsxs as o}from"react/jsx-runtime";import n from"react";import{observer as i}from"mobx-react-lite";import{Group as a,Path as s,Rect as l,Shape as c}from"react-konva";import{nodeAutorun as h}from"../utils/mobx.js";import{isAlive as d}from"mobx-state-tree";import{Html as u}from"react-konva-utils";import{layout as f,drawLayout as g}from"render-tag";import{layoutTextOnPath as m,drawTextOnPathLayout as x}from"render-tag/path";import{activeAnchorOf as p,scaledFontSizeAttrs as y,roundedFontSizeAttrs as b,roundedTextDecorationAttrs as w,textMinResizeWidth as v}from"../utils/px.js";import{flags as E}from"../utils/flags.js";import{applyFilter as O}from"./apply-filters.js";import{getLineHeight as M,usePrevious as S}from"./text-element.js";import{useFontSpecsLoader as j}from"./text-element/font-specs-loader.js";import{getHtml as z,getCurvePath as k,getCurveTextHeight as R}from"@polotno/core/text-html";import{useColor as Y}from"./use-color.js";import{isGradient as X}from"@polotno/core/gradient";import{detectSize as F}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as I}from"@polotno/core/background-shape";import{useFadeIn as P}from"./use-fadein.js";import{incrementLoader as C}from"../utils/loader.js";import{removeTags as T}from"@polotno/core/text";import{isTouchDevice as B}from"../utils/screen.js";import{getLimitedFontSize as W}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as D,extractFontSpecsFromHtml as N,scaleRichTextFontSizesInHtml as $}from"../utils/rich-text-html.js";import{RichEditor as J}from"./rich-editor.js";import{findFitFontSize as _}from"./text-element/fit-font-size.js";import{visiblePlaceholder as A}from"./text-element/placeholder.js";import{wrapHtmlForRenderTag as L}from"@polotno/core/reset-style";import{xmlEscape as V}from"@polotno/core/xml";import{hasMeasuredHeight as H,resolveTextBoxHeight as q}from"@polotno/core/text-types";export{L as wrapHtmlForRenderTag};function G(t,e){return t!==e&&(!E.textVerticalResizeEnabled||t<e)}export const RenderTagElement=i(({element:i,store:K})=>{var Q;const U=n.useRef(null),Z=n.useRef(null),[tt,et]=n.useState(!1),rt=n.useRef(i.height),ot=i.isSelected,nt=S(ot),{textVerticalResizeEnabled:it}=E,at=E.autoDeleteEmptyText,st=S(i.fontFamily),lt=n.useMemo(()=>D(i.text,{fontSize:i.fontSize,fontFamily:i.fontFamily}),[i.text,i.fontSize,i.fontFamily]),ct=n.useMemo(()=>N(lt,{fontFamily:i.fontFamily,fontWeight:i.fontWeight,fontStyle:i.fontStyle}),[lt,i.fontFamily,i.fontWeight,i.fontStyle]),[ht]=j(K,ct,i.text||i.placeholder),dt=i._editModeEnabled;P(U);const ut=ht?i.fontFamily:st&&st!==i.fontFamily?st:"Arial",ft=Y(i).fill,gt=X(i.fill),mt=X(i.stroke)&&!!i.strokeWidth,xt=!!i.strokeWidth&&(mt||gt),pt="ellipsis"===E.textOverflow&&!i.curveEnabled&&H(i)&&!xt,yt=A(i.placeholder),bt=!!yt&&!T(i.text),wt=bt?V(yt):lt,vt=bt?.6:i.a.opacity,Et=z(i,{fontFamily:ut,color:ft,forEditor:!pt,textOverflow:E.textOverflow,text:wt,textIsNormalized:!0}),Ot=i.width||1,Mt=i.curveEnabled,St=function(t,e,r,o){return n.useMemo(()=>{if(o){return null}try{return f({html:L(t),width:Math.max(1,e),accuracy:"performance"})}catch(r){return console.error("render-tag layout failed:",r),null}},[t,e,r,o])}(Et,Ot,ht,Mt),jt=function(t,e,r){return n.useMemo(()=>{if(!e.curveEnabled){return null}const r=e.width||1,o=R(e),n=k(r,o,e.curvePower,e.fontSize);try{return m({html:L(t),path:n,align:"center",textBaseline:"middle"})}catch(i){return console.error("render-tag/path layoutTextOnPath failed:",i),null}},[r,e.curveEnabled,e.curvePower,e.fontSize,e.width,e.height,t])}(Et,i,ht),zt=M({fontLoaded:ht,fontFamily:i.fontFamily,fontSize:i.fontSize,lineHeight:i.lineHeight}),kt=Mt?0:Math.max(null!==(Q=null==St?void 0:St.height)&&void 0!==Q?Q:0,i.fontSize*zt),Rt=jt?jt.bounds:null,Yt=n.useMemo(()=>St?St.lines.map(t=>({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height})):[],[St]),Xt=n.useRef([]),Ft=n.useRef(null),It=n.useRef(!0),[,Pt]=n.useReducer(t=>t+1,0);n.useEffect(()=>{const t=Xt.current;t.length>0&&(Xt.current=[],t.forEach(t=>t()))});const Ct=n.useCallback((t,e,r,o)=>{const n=C(`text ${i.id}`),a=()=>{K.history.ignore(t,e,r).then(()=>{It.current?(Xt.current.push(n),Pt()):n()})};o?queueMicrotask(a):a()},[i.id,K]),Tt=n.useCallback(t=>{const e=null!==Ft.current;Ft.current=t,e||Ct(()=>{const t=Ft.current;Ft.current=null,d(i)&&G(i.height,t)&&i.set({height:t})},!1,!0)},[i,Ct]);n.useEffect(()=>{if(!ht){return}if(!St){return}if(!H(i)){return void Ct(()=>{d(i)&&(H(i)||i.set({height:kt}))},void 0,void 0,!0)}if(K.isPlaying){return}const{textOverflow:t}=E,e=G(i.height,kt),r=()=>Tt(kt);if("change-font-size"!==t||tt||dt||i.curveEnabled){"resize"===t&&e&&!tt&&r()}else{const t=_(bt?{toJSON:()=>Object.assign(Object.assign({},i.toJSON()),{text:wt})}:i,{fontFamily:ut,color:ft,forEditor:!0},t=>{try{return f({html:L(t),width:Ot,accuracy:"performance"}).height}catch(Q){return F(t).height}});t.fontSize!==i.fontSize?Ct(()=>{if(!d(i)){return}const e=y(i,t.fontSize);void 0!==t.text&&t.text!==i.text&&(e.text=t.text),i.set(e)},!1,!0):e&&r()}}),n.useEffect(()=>()=>{It.current=!1;const t=Xt.current;t.length>0&&(Xt.current=[],t.forEach(t=>t()))},[]),n.useLayoutEffect(()=>{if(!ht||!i.curveEnabled||tt){return}if(!Rt||!Rt.width){return}const t=t=>t&&it?Math.max(t,Rt.height):Rt.height;Math.abs(Rt.width-i.width)<1&&Math.abs(t(i.height)-i.height)<1||Ct(()=>{if(!d(i)){return}const e=Rt.width-i.width,r=t(i.height),o=r-i.height;if(Math.abs(e)<1&&Math.abs(o)<1){return}const n=i.a.rotation*Math.PI/180,a=-e/2*Math.cos(n),s=-e/2*Math.sin(n);i.set({width:Rt.width,height:r,x:i.a.x+a,y:i.a.y+s})},!1,!0,!0)},[ht,i.curveEnabled,Rt,tt,it,Ct]),n.useEffect(()=>{nt&&!ot&&at&&""===T(i.text)&&i.removable&&!A(i.placeholder)&&K.deleteElements([i.id])},[at,i.placeholder,i.removable,i.text,ot,nt]),n.useEffect(()=>h(i,()=>{const t=U.current;t&&O(t,i)}),[St,jt,i.a.width,i.a.height,i.shadowColor,i.shadowOffsetX,i.shadowOffsetY,i.shadowOpacity]);const Bt=q(i,kt),Wt=Mt||!ht||tt||K.isPlaying||"resize"!==E.textOverflow?i.a.height:it?Math.max(i.a.height,kt):kt;let Dt=0;Mt||"middle"!==i.verticalAlign?Mt||"bottom"!==i.verticalAlign||(Dt=Bt-kt):Dt=(Bt-kt)/2;const Nt=i.backgroundPadding*(i.fontSize*zt*.5),$t=i.backgroundCornerRadius*(i.fontSize*zt*.5),Jt=n.useMemo(()=>!i.backgroundEnabled||i.curveEnabled||i.legacyBackground||0===Yt.length?"":I({rects:Yt,padding:Nt,cornerRadius:$t}),[i.backgroundEnabled,i.curveEnabled,i.legacyBackground,Yt,Nt,$t]),_t=B(),At=n.useRef(null),Lt=n.useRef(i.width),Vt=n.useRef(null),Ht=n.useRef(null),qt=n.useRef(""),Gt=n.useRef(0),Kt=n.useRef(0),Qt=i.width>0?i.a.width/i.width:1;return o(r,{children:[i.curveEnabled||i.legacyBackground?e(l,{x:i.a.x,y:i.a.y,offsetX:Nt/Qt,offsetY:Nt/Qt,rotation:i.a.rotation,scaleX:Qt,scaleY:Qt,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled,opacity:i.backgroundOpacity*vt,fill:i.backgroundColor,width:i.width+2*Nt,height:Bt+2*Nt,cornerRadius:$t}):e(s,{name:"rendertag-background",x:i.a.x,y:i.a.y,offsetY:-Dt/Qt,rotation:i.a.rotation,scaleX:Qt,scaleY:Qt,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled&&Jt.length>0,opacity:i.backgroundOpacity*vt,fill:i.backgroundColor,data:Jt}),e(c,{ref:U,name:"rendertag-text",x:i.a.x,y:i.a.y,rotation:i.a.rotation,scaleX:Qt,scaleY:Qt,offsetY:Qt>0?-Dt/Qt:0,width:i.width,height:Math.max(i.height,kt),listening:!1,visible:!dt&&!!(Mt?jt:St),editModeEnabled:dt,opacity:vt,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,hideInExport:!i.showInExport,sceneFunc:(t,e)=>{const r=t._context;if(r){try{if(Mt){if(!jt||!Rt){return}const t=(i.width-Rt.width)/2-Rt.x;r.save(),r.translate(t,-Rt.y),x({layout:jt,ctx:r}),r.restore()}else{if(!St){return}g({layout:St,width:Ot,ctx:r,pixelRatio:1})}}catch(o){console.error("render-tag draw failed:",o)}}}}),e(l,{ref:Z,name:"element",x:i.a.x,y:i.a.y,listening:i.selectable,rotation:i.a.rotation,width:i.a.width,height:Wt,visible:!dt,draggable:_t?i.draggable&&ot:i.draggable,preventDefault:!_t||ot,opacity:0,hideInExport:!0,id:i.id,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDblClick:t=>{i.contentEditable&&(Ht.current={x:t.evt.clientX,y:t.evt.clientY},i.toggleEditMode(!0))},onDblTap:t=>{var e;if(i.contentEditable){const r=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Ht.current=r?{x:r.clientX,y:r.clientY}:null,i.toggleEditMode(!0)}},onTransformStart:t=>{et(!0),rt.current=i.height,Lt.current=i.width,Vt.current=null,At.current=t.target.position(),qt.current=p(t.target),Gt.current=i.a.rotation,Kt.current=i.height},onTransform:e=>{const r=e.target,o=qt.current,n="middle-left"===o||"middle-right"===o,a="top-center"===o||"bottom-center"===o,s=r.scaleX();if(n){const e=r.scaleX(),o=r.width()*e;let n=Math.max(o,v(Lt.current,i.fontSize));const a=i.toJSON();let s=0;if(i.curveEnabled){s=R(Object.assign(Object.assign({},a),{width:n}))}else{const{x:e,y:r,width:o,height:i}=a,l=t(a,["x","y","width","height"]),c=`${JSON.stringify(l)}|${ut}|${ht}|${ft}`;if(Vt.current&&Vt.current.signature===c&&n<=Vt.current.width){n=Vt.current.width,s=Vt.current.height}else{const t=t=>{const e=z(Object.assign(Object.assign({},a),{width:t}),{fontFamily:ut,color:ft,forEditor:!0,text:wt,textIsNormalized:!0});try{return f({html:L(e),width:t,accuracy:"performance"})}catch(Q){return{height:F(e).height,lines:[]}}};let e=t(n);const r=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width));r>n+.5&&r<=Lt.current+.5&&(n=Math.min(Math.ceil(r),Lt.current),e=t(n),Vt.current={signature:c,width:n,height:e.height}),s=e.height}}n>o&&At.current&&r.position(At.current),r.width(n),r.scaleX(1),r.scaleY(1);let l=E.textVerticalResizeEnabled?Math.max(s,rt.current):s;"ellipsis"===E.textOverflow&&(l=rt.current),r.height(l),i.set({width:r.width(),height:l,x:r.x(),y:r.y()})}else if(a){const t="resize"===E.textOverflow?Mt?(null==Rt?void 0:Rt.height)||i.fontSize:kt:zt*i.fontSize,e=r.height()*r.scaleY();if(e<t){if(r.height(t),At.current){if("top-center"===qt.current){const e=Gt.current*Math.PI/180,o=Kt.current-t;r.x(At.current.x-o*Math.sin(e)),r.y(At.current.y+o*Math.cos(e))}else{r.position(At.current)}}}else{r.height(e)}r.scaleY(1),r.scaleX(1),Kt.current=Math.max(t,e),i.set({x:r.x(),y:r.y(),height:r.height(),rotation:r.rotation()})}else{r.scaleX(1),r.scaleY(1),i.set({fontSize:i.fontSize*s,letterSpacing:i.letterSpacing,width:r.width()*s,x:r.x(),y:r.y(),rotation:r.rotation(),height:r.height()*s,shadowBlur:i.shadowBlur*s,shadowOffsetX:i.shadowOffsetX*s,shadowOffsetY:i.shadowOffsetY*s,strokeWidth:i.strokeWidth*s,text:$(i.text,s)})}At.current=e.target.position()},onTransformEnd:t=>{et(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),i.set(Object.assign(Object.assign({},b(i,i.fontSize*e)),{width:t.target.width()*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),text:$(i.text,e)}))}}),dt&&e(a,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,offsetY:-Dt,children:e(u,{divProps:{style:{zIndex:20}},children:e(J,{element:i,onChange:t=>{const e=W({oldText:T(i.text),newText:T(t),element:i});i.set(Object.assign({text:t},y(i,e)))},onBlur:()=>{d(i)&&(i.set(w(i)),i.toggleEditMode(!1))},clickCoords:Ht.current})})})]})});
@@ -1 +1 @@
1
- import{jsx as t,Fragment as e,jsxs as r}from"react/jsx-runtime";import n from"react";import{observer as o}from"mobx-react-lite";import{Group as i,Path as a,Text as s,TextPath as l}from"react-konva";import{Html as c}from"react-konva-utils";import{nodeAutorun as u}from"../utils/mobx.js";import d from"konva";import{parsePath as f,roundCommands as h}from"svg-round-corners/lib/index.js";import{generateBackgroundShapeFromRects as g,textLinesToRects as p}from"@polotno/core/background-shape";import{useColor as m}from"./use-color.js";import{useWorkspaceStyle as x}from"./workspace-style.js";import{ROLES as w}from"../model/store.js";import{incrementLoader as v}from"../utils/loader.js";import{activeAnchorOf as y,scaledFontSizeAttrs as b,roundedFontSizeAttrs as S,roundedTextDecorationAttrs as E,textMinResizeWidth as z}from"../utils/px.js";import{isFontLoaded as k}from"../utils/fonts.js";import{flags as O}from"../utils/flags.js";import{removeTags as R}from"@polotno/core/text";import{getDir as F,getCurvePath as A,resolveLineHeight as M,normalizeStrokeLineJoin as j}from"@polotno/core/text-html";import{cssFontFamily as L}from"@polotno/core/fonts";import{applyFilter as C}from"./apply-filters.js";import{useFadeIn as T}from"./use-fadein.js";import{isTouchDevice as H}from"../utils/screen.js";import{isAlive as P,getParent as X}from"mobx-state-tree";import{getLimitedFontSize as Y}from"./text-element/max-font-size.js";import{getOptimalCaretColor as W}from"./text-element/caret-color.js";import{visiblePlaceholder as D}from"./text-element/placeholder.js";import{hasMeasuredHeight as $,resolveTextBoxHeight as B}from"@polotno/core/text-types";let I;function N(){return I||(I=document.getElementById("polotno-text-style"),I||(I=document.createElement("style"),I.id="polotno-text-style",document.head.appendChild(I)),I)}d._fixTextRendering=!0;const J={border:"none",padding:"0px",overflow:"hidden",background:"none",outline:"none",resize:"none",overflowWrap:"break-word",whiteSpace:"pre-wrap",userSelect:"text",wordBreak:"normal",textTransform:"none"};export{getDir}from"@polotno/core/text-html";const _=o(({textNodeRef:e,element:r,onBlur:o,selectAll:i,cursorPosition:a})=>{const[s,l]=n.useState(J),c=e.current;n.useLayoutEffect(()=>{if(!c){return}const t={};t.width=c.width()-2*c.padding()+"px",t.height=c.height()-2*c.padding()+c.fontSize()*c.lineHeight()+"px",t.fontSize=c.fontSize()+"px",t.lineHeight=c.lineHeight()+.01,t.fontFamily=c.fontFamily(),t.textAlign=c.align(),t.color=c.fill(),t.fontWeight=r.fontWeight,t.fontStyle=r.fontStyle,t.letterSpacing=r.letterSpacing+"em",t.opacity=Math.max(r.a.opacity,.2),t.textTransform=r.textTransform,t.caretColor=W(r);const e=`\n .polotno-input::placeholder {\n color: ${s.color};\n opacity: 0.6;\n }\n `,n=N();n.innerHTML="",n.appendChild(document.createTextNode(e)),JSON.stringify(t)!==JSON.stringify(s)&&l(t)});const u=n.useRef(null);if(n.useLayoutEffect(()=>{var t;const e=u.current;if(!e){return}null===(t=u.current)||void 0===t||t.focus({preventScroll:!0});const r=a||e.value.length;e.selectionStart=e.selectionEnd=r,i&&(null==e||e.select(),document.execCommand("selectAll",!1,void 0))},[]),n.useEffect(()=>{window.addEventListener("blur",o);const t=t=>{var e;(null===(e=u.current)||void 0===e?void 0:e.contains(t.target))||o()};return window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",o),window.removeEventListener("touchstart",t)}},[]),!c){return null}let d=0;const f=c.textArr.length*c.lineHeight()*c.fontSize(),h=B(r.a,f);"middle"===r.verticalAlign&&(d=(h-f)/2),"bottom"===r.verticalAlign&&(d=h-f);const g=R(r.text);return t("textarea",{className:"polotno-input",ref:u,dir:F(g),style:Object.assign(Object.assign(Object.assign({},J),s),{paddingTop:d+"px"}),value:g,onChange:t=>{const e=Y({oldText:r.text,newText:t.target.value,element:r});r.set(Object.assign({text:t.target.value},b(r,e)))},placeholder:D(r.placeholder),onBlur:o})}),V=e=>t(c,{children:t(_,Object.assign({},e))});export const useFontLoader=(t,e,r,o,i)=>{const[a,s]=n.useReducer(t=>t+1,0),l=n.useRef(k(e,r,o)),c=n.useRef(null);return n.useLayoutEffect(()=>{if(l.current=k(e,r,o),l.current){return}let n=!0;return(async()=>{l.current=!1,s();const a=v(`text ${e}`);await t.loadFont(e,r,o,i),n?(c.current=a,l.current=!0,s()):a()})(),()=>{c.current&&(c.current(),c.current=null),n=!1}},[e,r,o]),n.useEffect(()=>{l.current&&setTimeout(()=>{c.current&&(c.current(),c.current=null)})},[l.current]),[l.current]};export const useFontFamiliesLoader=(t,e,r,o,i)=>{const[a,s]=n.useReducer(t=>t+1,0),l=e.join("\0"),c=n.useRef(e.every(t=>k(t,r,o))),u=n.useRef(null);return n.useLayoutEffect(()=>{if(c.current=e.every(t=>k(t,r,o)),c.current){return}let n=!0;return(async()=>{c.current=!1,s();const a=v(`text fonts ${l}`);await Promise.all(e.map(e=>t.loadFont(e,r,o,i))),n?(u.current=a,c.current=!0,s()):a()})(),()=>{u.current&&(u.current(),u.current=null),n=!1}},[l,r,o]),n.useEffect(()=>{c.current&&setTimeout(()=>{u.current&&(u.current(),u.current=null)})},[c.current]),[c.current]};export const getLineHeight=({fontLoaded:t,fontFamily:e,fontSize:r,lineHeight:o})=>n.useMemo(()=>M({fontFamily:e,fontSize:r,lineHeight:o}),[t,e,r,o]);export function usePrevious(t){const e=n.useRef(t),r=n.useRef(t);return n.useMemo(()=>{r.current=e.current,e.current=t},[t]),r.current}function q(t,e,r=!1){const n=t.fontSize(),o=t.height(),i=R(e.text);let a=e.a.fontSize;t.height(void 0);const s=Math.round(2*e.a.fontSize)-1;for(let l=1;l<s;l++){const n=$(e.a)&&t.height()>e.a.height;let o=i.split("\n").join(" ").split(/[\s-]+/).reduce((t,e)=>(e=e.toLowerCase(),/[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\uAC00-\uD7AF]/.test(e)?t.concat(e.split("")):t.concat(e)),[]),s=t.textArr.map(t=>t.text.toLowerCase()).join(";");const l=o.find(t=>!s.includes(t)||(s=s.replace(t,""),!1));if(!(n||l&&!r)){break}a-=.5,t.fontSize(a)}return t.fontSize(n),t.height(o),a}export{getCurvePath}from"@polotno/core/text-html";export const TextElement=o(({element:o,store:c})=>{const{textOverflowIndicatorStyle:d}=x(),k=n.useRef(null),M=n.useRef(null),Y=n.useRef(null),W=n.useRef([]),I=n.useRef(!0),[,N]=n.useReducer(t=>t+1,0),{editorEnabled:J,selectAll:_}=(t=>{const[e,r]=n.useState(!1),o=n.useRef(!1);return n.useEffect(()=>{var e=!0;return setTimeout(()=>{e&&(t._editModeEnabled&&(o.current=!0),r(!0),setTimeout(()=>{o.current=!1},50))},50),()=>{e=!1}},[]),{editorEnabled:e&&t._editModeEnabled,selectAll:o.current}})(o),[U,Z]=n.useState(!1),[G,K]=n.useState(!1),Q=n.useRef(o.a.height),tt=n.useRef(0),et=n.useRef(""),rt=n.useRef(0),nt=o.isSelected,ot=usePrevious(nt),{textVerticalResizeEnabled:it}=O,at=O.autoDeleteEmptyText,st=usePrevious(o.fontFamily),[lt,ct]=n.useState([]);n.useEffect(()=>{var t,e;const r=null!==(e=null===(t=k.current)||void 0===t?void 0:t.textArr)&&void 0!==e?e:[];JSON.stringify(r)!==JSON.stringify(lt)&&ct(r)}),n.useEffect(()=>{if(o.a.width||"tablecell"===o.type){return}const t=k.current;t.width(600),o.set({width:1.4*t.getTextWidth()})},[]),n.useEffect(()=>{ot&&!nt&&at&&""===o.text&&o.removable&&!D(o.placeholder)&&c.deleteElements([o.id])},[at,o.placeholder,o.removable,o.text,nt,ot]);const ut=()=>{if(o.curveEnabled){const t=k.current;null==t||t.clearCache(),null==t||t.setAttrs({filters:[]});const e=M.current;e&&C(e,o)}else{const t=k.current;t&&C(t,o)}};n.useLayoutEffect(()=>{if(!G){return u(o,()=>{ut()})}}),n.useEffect(()=>{const t=W.current;t.length>0&&(W.current=[],t.forEach(t=>t()))}),n.useEffect(()=>()=>{I.current=!1;const t=W.current;t.length>0&&(W.current=[],t.forEach(t=>t()))},[]);let dt=R(o.text);"uppercase"===o.textTransform&&(dt=dt.toUpperCase());const[ft]=useFontLoader(c,o.fontFamily,o.fontStyle,o.fontWeight,o.text||o.placeholder),ht=()=>{if(o.curveEnabled){const t=M.current;return t.getSelfRect().height||t.fontSize()}const t=k.current.clone({height:void 0}),e=Math.ceil(t.fontSize()*t.lineHeight()*t.textArr.length+1);return t.destroy(),e};n.useLayoutEffect(()=>{if(!ft){return}if("tablecell"===o.type){const{textOverflow:t}=O;if("resize"===t){const t=ht(),e=P(o)?X(o,2):null;(null==e?void 0:e.setCellContentHeight)&&e.setCellContentHeight(o.id,t)}else if("change-font-size"===t){const t=k.current;if(t){const e=q(t,o,O.textSplitAllowed);e!==o.fontSize&&c.history.ignore(()=>{o.set({fontSize:e})})}}return}const{textOverflow:t,textSplitAllowed:e}=O,r=(t,e,r,n)=>{const i=v(`text ${o.id}`),a=()=>{c.history.ignore(t,e,r).then(()=>{I.current?(W.current.push(i),N()):i()})};n?queueMicrotask(a):a()};if(!$(o)){const t=ht();return void r(()=>{$(o)||o.set({height:t})},void 0,void 0,!0)}if(!c.isPlaying){if("change-font-size"!==t||U){if("resize"===t){const t=ht();it&&o.height<t&&!U&&r(()=>{var e;P(o)&&o.set({height:t}),null===(e=k.current)||void 0===e||e.height(t)},!1,!0),it||o.height===t||U||r(()=>{var e;P(o)&&o.set({height:t}),null===(e=k.current)||void 0===e||e.height(t)},!1,!0)}}else{const t=k.current,n=o.curveEnabled?o.fontSize:q(t,o,e);if(n!==o.fontSize){return void r(()=>{o.set(b(o,n))},!1,!0)}const i=ht();o.height===i||it||r(()=>{o.set({height:i})},!1,!0)}}}),n.useLayoutEffect(()=>{var t;if(ft&&o.curveEnabled){const e=null===(t=M.current)||void 0===t?void 0:t.getSelfRect().width;if(e){const t=e-o.a.width,r=o.a.rotation*Math.PI/180,n=-t/2*Math.cos(r),i=-t/2*Math.sin(r);o.set({width:e,x:o.a.x+n,y:o.a.y+i})}}},[ft,o.curveEnabled,o.curvePower,o.text,o.fontSize,o.fontFamily,o.fontWeight,o.fontStyle,o.letterSpacing]),n.useLayoutEffect(()=>{const t=k.current;t&&(t.width(t.width()+1e-8),t.width(t.width()-1e-8),t._setTextData(),ut())},[ft]);const gt=n.useRef(null),pt=n.useRef(o.width),mt=n.useRef(0),xt=t=>{t.evt.preventDefault();const e=c.selectedShapes.find(t=>t===o);e&&o.contentEditable&&(mt.current=function(t){var e;const r=t.target,n=function(t){var e=t.getAbsoluteTransform().copy();e.invert();var r=t.getStage().getPointerPosition();return e.point(r)}(r),o=r.textArr,i=Math.floor(n.y/(r.fontSize()*r.lineHeight())),a=o.slice(0,i).reduce((t,e)=>t+e.text.length,i),s=null!==(e=o[i])&&void 0!==e?e:o[0];let l=0;return"right"===r.align()?l=r.width()-s.width:"center"===r.align()&&(l=r.width()/2-s.width/2),a+Math.round((n.x-l)/s.width*s.text.length)}(t),o.toggleEditMode())},wt=D(o.placeholder),vt=!dt&&wt?.6:o.a.opacity;T(k,vt);const yt=getLineHeight({fontLoaded:ft,fontFamily:o.fontFamily,fontSize:o.a.fontSize,lineHeight:o.lineHeight}),bt=o.selectable||c.role===w.ADMIN,St=m(o),Et=m(o,o.stroke,"stroke"),zt=n.useMemo(()=>o.backgroundEnabled?o.curveEnabled?function({width:t,height:e,padding:r=0,cornerRadius:n=0}){const o=-r,i=-r,a=t+r,s=e+r,l=f(`M ${o} ${i} L ${a} ${i} L ${a} ${s} L ${o} ${s} Z`);return h(l,n).path}({width:o.a.width,height:o.a.height,cornerRadius:o.backgroundCornerRadius*(o.a.fontSize*yt*.5),padding:o.backgroundPadding*(o.a.fontSize*yt*.5)}):function({lines:t,lineHeight:e,width:r,align:n="left",padding:o=0,cornerRadius:i=0}){const a=p({lines:t,lineHeight:e,width:r,align:n});return g({rects:a,padding:o,cornerRadius:i})}({lines:JSON.parse(JSON.stringify(lt)),cornerRadius:o.backgroundCornerRadius*(o.a.fontSize*yt*.5),lineHeight:yt*o.a.fontSize,padding:o.backgroundPadding*(o.a.fontSize*yt*.5),width:o.a.width,align:o.align}):"",[o.backgroundEnabled,o.curveEnabled,o.backgroundCornerRadius,o.a.fontSize,o.a.height,yt,o.backgroundPadding,o.a.width,o.align,lt]),kt=H();let Ot=0;if(!o.curveEnabled){const t=lt.length*yt*o.a.fontSize,e=B(o.a,t);"middle"===o.verticalAlign?Ot=(e-t)/2:"bottom"===o.verticalAlign&&(Ot=e-t)}const Rt=o.curveEnabled?A(o.a.width,o.a.height,o.curvePower,o.a.fontSize):"",Ft=ft?L(o.fontFamily):st===o.fontFamily?"Arial":L(st),At=F(dt);return r(e,{children:[t(a,{ref:Y,x:o.a.x,y:o.a.y,rotation:o.a.rotation,hideInExport:!o.showInExport,listening:!1,visible:o.backgroundEnabled,opacity:o.backgroundOpacity*vt,data:zt,fill:o.backgroundColor,offsetY:-Ot}),t(a,{data:Rt,stroke:d.stroke,strokeWidth:1,x:o.a.x,y:o.a.y,rotation:o.a.rotation,visible:!1}),t(l,Object.assign({ref:M,visible:o.curveEnabled,data:Rt,text:dt||wt,listening:!1,align:"center",textBaseline:"middle",direction:At},St,Et,{strokeWidth:o.strokeWidth,lineJoin:j(o.strokeLineJoin),fillAfterStrokeEnabled:!0,fontSize:o.a.fontSize,fontFamily:`${L(o.fontFamily)}, ${L(st)}`,fontStyle:o.fontStyle+" "+o.fontWeight,textDecoration:o.textDecoration.trim(),letterSpacing:o.letterSpacing*o.a.fontSize,x:o.a.x,y:o.a.y,rotation:o.a.rotation,opacity:o._editModeEnabled?.3:vt,hideInExport:!o.showInExport,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity})),t(s,Object.assign({ref:k,id:o.selectable?o.id:void 0,name:o.selectable?"element":"",hideInExport:!o.showInExport,editModeEnabled:o._editModeEnabled,x:o.a.x,y:o.a.y,rotation:o.a.rotation,width:o.a.width,height:$(o.a)?o.a.height:void 0,text:dt||wt,direction:At},St,Et,{lineJoin:j(o.strokeLineJoin),strokeWidth:o.strokeWidth,fillAfterStrokeEnabled:!0,fontSize:o.a.fontSize,fontFamily:Ft,fontStyle:o.fontStyle+" "+o.fontWeight,textDecoration:o.textDecoration,align:o.align,verticalAlign:o.verticalAlign,draggable:kt?o.draggable&&nt:o.draggable,preventDefault:!kt||nt,opacity:o.curveEnabled?0:vt,visible:!o._editModeEnabled,ellipsis:"ellipsis"===O.textOverflow,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity,lineHeight:yt,letterSpacing:o.letterSpacing*o.a.fontSize,listening:!!o.selectable&&bt,onDragStart:()=>{K(!0)},onDragMove:t=>{o.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{K(!1),o.set({x:t.target.x(),y:t.target.y()})},onClick:xt,onTap:xt,onTransformStart:()=>{Z(!0);const t=k.current;Q.current=t.height(),pt.current=o.a.width,gt.current=t.position(),tt.current=t.rotation(),rt.current=t.height(),et.current=y(t)},onTransform:t=>{var e,r;const n=t.target;null===(e=Y.current)||void 0===e||e.setAttrs({x:n.x(),y:n.y(),rotation:n.rotation(),scale:n.scale()});const i=et.current;if("middle-left"===i||"middle-right"===i){const t=n.scaleX(),e=n.width()*t,r=z(pt.current,o.a.fontSize);let i=e;e<r&&(i=r,gt.current&&n.position(gt.current)),n.width(i),n.scaleX(1),n.scaleY(1);let a=ht();if("ellipsis"===O.textOverflow){a=Q.current}else{const t=Math.max(a,Q.current);n.height(t),o.set({height:n.height()})}const s=O.textVerticalResizeEnabled?Math.max(a,Q.current):ht();o.set({x:n.x(),width:n.width(),rotation:n.rotation(),height:s}),n.skewX(0),n.skewY(0),ut()}if("top-center"===i||"bottom-center"===i){const e="resize"===O.textOverflow?ht():yt*o.a.fontSize,r=t.target.height()*t.target.scaleY();if(r<e){if(t.target.height(e),gt.current){if("top-center"===et.current){const r=tt.current*Math.PI/180,n=rt.current-e;t.target.x(gt.current.x-n*Math.sin(r)),t.target.y(gt.current.y+n*Math.cos(r))}else{t.target.position(gt.current)}}}else{t.target.height(r)}t.target.scaleY(1),t.target.scaleX(1),t.target.rotation(tt.current),t.target.skewX(0),t.target.skewY(0),rt.current=Math.max(e,r)}gt.current=t.target.position();const a=t.target.scaleX();null===(r=Y.current)||void 0===r||r.setAttrs({scaleX:1,scaleY:1}),t.target.scaleX(1),t.target.scaleY(1),o.set({fontSize:o.a.fontSize*a,width:t.target.width()*a,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*a,shadowBlur:o.shadowBlur*a,shadowOffsetX:o.shadowOffsetX*a,shadowOffsetY:o.shadowOffsetY*a,strokeWidth:o.strokeWidth*a})},onTransformEnd:t=>{var e;const r=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),o.set(Object.assign(Object.assign({},S(o,o.fontSize*r)),{width:Math.ceil(t.target.width()*r),x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*r})),null===(e=Y.current)||void 0===e||e.setAttrs({scaleX:1,scaleY:1}),Z(!1)}})),J&&o.contentEditable&&t(i,{x:o.a.x,y:o.a.y,rotation:o.a.rotation,children:t(V,{textNodeRef:k,element:o,selectAll:_,cursorPosition:mt.current,onBlur:()=>{o.set(E(o)),o.toggleEditMode(!1)}})})]})});
1
+ import{jsx as t,Fragment as e,jsxs as r}from"react/jsx-runtime";import n from"react";import{observer as o}from"mobx-react-lite";import{Group as i,Path as a,Text as s,TextPath as l}from"react-konva";import{Html as c}from"react-konva-utils";import{nodeAutorun as u}from"../utils/mobx.js";import d from"konva";import{parsePath as f,roundCommands as h}from"svg-round-corners/lib/index.js";import{generateBackgroundShapeFromRects as g,textLinesToRects as p}from"@polotno/core/background-shape";import{useColor as m}from"./use-color.js";import{useWorkspaceStyle as x}from"./workspace-style.js";import{ROLES as w}from"../model/store.js";import{incrementLoader as v}from"../utils/loader.js";import{activeAnchorOf as y,scaledFontSizeAttrs as b,roundedFontSizeAttrs as S,roundedTextDecorationAttrs as E,textMinResizeWidth as z}from"../utils/px.js";import{isFontLoaded as k}from"../utils/fonts.js";import{flags as O}from"../utils/flags.js";import{removeTags as R}from"@polotno/core/text";import{getDir as F,getCurvePath as A,resolveLineHeight as M,normalizeStrokeLineJoin as j}from"@polotno/core/text-html";import{cssFontFamily as L}from"@polotno/core/fonts";import{FIT_FONT_SIZE_STEP as C,FIT_HEIGHT_EPSILON as T}from"@polotno/core/fit-text";import{applyFilter as H}from"./apply-filters.js";import{useFadeIn as P}from"./use-fadein.js";import{isTouchDevice as X}from"../utils/screen.js";import{isAlive as Y,getParent as W}from"mobx-state-tree";import{getLimitedFontSize as D}from"./text-element/max-font-size.js";import{getOptimalCaretColor as $}from"./text-element/caret-color.js";import{visiblePlaceholder as B}from"./text-element/placeholder.js";import{hasMeasuredHeight as I,resolveTextBoxHeight as N}from"@polotno/core/text-types";let J;function _(){return J||(J=document.getElementById("polotno-text-style"),J||(J=document.createElement("style"),J.id="polotno-text-style",document.head.appendChild(J)),J)}d._fixTextRendering=!0;const V={border:"none",padding:"0px",overflow:"hidden",background:"none",outline:"none",resize:"none",overflowWrap:"break-word",whiteSpace:"pre-wrap",userSelect:"text",wordBreak:"normal",textTransform:"none"};export{getDir}from"@polotno/core/text-html";const q=o(({textNodeRef:e,element:r,onBlur:o,selectAll:i,cursorPosition:a})=>{const[s,l]=n.useState(V),c=e.current;n.useLayoutEffect(()=>{if(!c){return}const t={};t.width=c.width()-2*c.padding()+"px",t.height=c.height()-2*c.padding()+c.fontSize()*c.lineHeight()+"px",t.fontSize=c.fontSize()+"px",t.lineHeight=c.lineHeight()+.01,t.fontFamily=c.fontFamily(),t.textAlign=c.align(),t.color=c.fill(),t.fontWeight=r.fontWeight,t.fontStyle=r.fontStyle,t.letterSpacing=r.letterSpacing+"em",t.opacity=Math.max(r.a.opacity,.2),t.textTransform=r.textTransform,t.caretColor=$(r);const e=`\n .polotno-input::placeholder {\n color: ${s.color};\n opacity: 0.6;\n }\n `,n=_();n.innerHTML="",n.appendChild(document.createTextNode(e)),JSON.stringify(t)!==JSON.stringify(s)&&l(t)});const u=n.useRef(null);if(n.useLayoutEffect(()=>{var t;const e=u.current;if(!e){return}null===(t=u.current)||void 0===t||t.focus({preventScroll:!0});const r=a||e.value.length;e.selectionStart=e.selectionEnd=r,i&&(null==e||e.select(),document.execCommand("selectAll",!1,void 0))},[]),n.useEffect(()=>{window.addEventListener("blur",o);const t=t=>{var e;(null===(e=u.current)||void 0===e?void 0:e.contains(t.target))||o()};return window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",o),window.removeEventListener("touchstart",t)}},[]),!c){return null}let d=0;const f=c.textArr.length*c.lineHeight()*c.fontSize(),h=N(r.a,f);"middle"===r.verticalAlign&&(d=(h-f)/2),"bottom"===r.verticalAlign&&(d=h-f);const g=R(r.text);return t("textarea",{className:"polotno-input",ref:u,dir:F(g),style:Object.assign(Object.assign(Object.assign({},V),s),{paddingTop:d+"px"}),value:g,onChange:t=>{const e=D({oldText:r.text,newText:t.target.value,element:r});r.set(Object.assign({text:t.target.value},b(r,e)))},placeholder:B(r.placeholder),onBlur:o})}),U=e=>t(c,{children:t(q,Object.assign({},e))});export const useFontLoader=(t,e,r,o,i)=>{const[a,s]=n.useReducer(t=>t+1,0),l=n.useRef(k(e,r,o)),c=n.useRef(null);return n.useLayoutEffect(()=>{if(l.current=k(e,r,o),l.current){return}let n=!0;return(async()=>{l.current=!1,s();const a=v(`text ${e}`);await t.loadFont(e,r,o,i),n?(c.current=a,l.current=!0,s()):a()})(),()=>{c.current&&(c.current(),c.current=null),n=!1}},[e,r,o]),n.useEffect(()=>{l.current&&setTimeout(()=>{c.current&&(c.current(),c.current=null)})},[l.current]),[l.current]};export const useFontFamiliesLoader=(t,e,r,o,i)=>{const[a,s]=n.useReducer(t=>t+1,0),l=e.join("\0"),c=n.useRef(e.every(t=>k(t,r,o))),u=n.useRef(null);return n.useLayoutEffect(()=>{if(c.current=e.every(t=>k(t,r,o)),c.current){return}let n=!0;return(async()=>{c.current=!1,s();const a=v(`text fonts ${l}`);await Promise.all(e.map(e=>t.loadFont(e,r,o,i))),n?(u.current=a,c.current=!0,s()):a()})(),()=>{u.current&&(u.current(),u.current=null),n=!1}},[l,r,o]),n.useEffect(()=>{c.current&&setTimeout(()=>{u.current&&(u.current(),u.current=null)})},[c.current]),[c.current]};export const getLineHeight=({fontLoaded:t,fontFamily:e,fontSize:r,lineHeight:o})=>n.useMemo(()=>M({fontFamily:e,fontSize:r,lineHeight:o}),[t,e,r,o]);export function usePrevious(t){const e=n.useRef(t),r=n.useRef(t);return n.useMemo(()=>{r.current=e.current,e.current=t},[t]),r.current}function Z(t,e,r=!1){const n=t.fontSize(),o=t.height(),i=R(e.text);let a=e.a.fontSize;t.height(void 0);const s=Math.round(2*e.a.fontSize)-1;for(let l=1;l<s;l++){const n=I(e.a)&&t.height()>e.a.height+T;let o=i.split("\n").join(" ").split(/[\s-]+/).reduce((t,e)=>(e=e.toLowerCase(),/[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\uAC00-\uD7AF]/.test(e)?t.concat(e.split("")):t.concat(e)),[]),s=t.textArr.map(t=>t.text.toLowerCase()).join(";");const l=o.find(t=>!s.includes(t)||(s=s.replace(t,""),!1));if(!(n||l&&!r)){break}a-=C,t.fontSize(a)}return t.fontSize(n),t.height(o),a}export{getCurvePath}from"@polotno/core/text-html";export const TextElement=o(({element:o,store:c})=>{const{textOverflowIndicatorStyle:d}=x(),k=n.useRef(null),M=n.useRef(null),C=n.useRef(null),T=n.useRef([]),D=n.useRef(!0),[,$]=n.useReducer(t=>t+1,0),{editorEnabled:J,selectAll:_}=(t=>{const[e,r]=n.useState(!1),o=n.useRef(!1);return n.useEffect(()=>{var e=!0;return setTimeout(()=>{e&&(t._editModeEnabled&&(o.current=!0),r(!0),setTimeout(()=>{o.current=!1},50))},50),()=>{e=!1}},[]),{editorEnabled:e&&t._editModeEnabled,selectAll:o.current}})(o),[V,q]=n.useState(!1),[G,K]=n.useState(!1),Q=n.useRef(o.a.height),tt=n.useRef(0),et=n.useRef(""),rt=n.useRef(0),nt=o.isSelected,ot=usePrevious(nt),{textVerticalResizeEnabled:it}=O,at=O.autoDeleteEmptyText,st=usePrevious(o.fontFamily),[lt,ct]=n.useState([]);n.useEffect(()=>{var t,e;const r=null!==(e=null===(t=k.current)||void 0===t?void 0:t.textArr)&&void 0!==e?e:[];JSON.stringify(r)!==JSON.stringify(lt)&&ct(r)}),n.useEffect(()=>{if(o.a.width||"tablecell"===o.type){return}const t=k.current;t.width(600),o.set({width:1.4*t.getTextWidth()})},[]),n.useEffect(()=>{ot&&!nt&&at&&""===o.text&&o.removable&&!B(o.placeholder)&&c.deleteElements([o.id])},[at,o.placeholder,o.removable,o.text,nt,ot]);const ut=()=>{if(o.curveEnabled){const t=k.current;null==t||t.clearCache(),null==t||t.setAttrs({filters:[]});const e=M.current;e&&H(e,o)}else{const t=k.current;t&&H(t,o)}};n.useLayoutEffect(()=>{if(!G){return u(o,()=>{ut()})}}),n.useEffect(()=>{const t=T.current;t.length>0&&(T.current=[],t.forEach(t=>t()))}),n.useEffect(()=>()=>{D.current=!1;const t=T.current;t.length>0&&(T.current=[],t.forEach(t=>t()))},[]);let dt=R(o.text);"uppercase"===o.textTransform&&(dt=dt.toUpperCase());const[ft]=useFontLoader(c,o.fontFamily,o.fontStyle,o.fontWeight,o.text||o.placeholder),ht=()=>{if(o.curveEnabled){const t=M.current;return t.getSelfRect().height||t.fontSize()}const t=k.current.clone({height:void 0}),e=Math.ceil(t.fontSize()*t.lineHeight()*t.textArr.length+1);return t.destroy(),e};n.useLayoutEffect(()=>{if(!ft){return}if("tablecell"===o.type){const{textOverflow:t}=O;if("resize"===t){const t=ht(),e=Y(o)?W(o,2):null;(null==e?void 0:e.setCellContentHeight)&&e.setCellContentHeight(o.id,t)}else if("change-font-size"===t){const t=k.current;if(t){const e=Z(t,o,O.textSplitAllowed);e!==o.fontSize&&c.history.ignore(()=>{o.set({fontSize:e})})}}return}const{textOverflow:t,textSplitAllowed:e}=O,r=(t,e,r,n)=>{const i=v(`text ${o.id}`),a=()=>{c.history.ignore(t,e,r).then(()=>{D.current?(T.current.push(i),$()):i()})};n?queueMicrotask(a):a()};if(!I(o)){const t=ht();return void r(()=>{I(o)||o.set({height:t})},void 0,void 0,!0)}if(!c.isPlaying){if("change-font-size"!==t||V){if("resize"===t){const t=ht();it&&o.height<t&&!V&&r(()=>{var e;Y(o)&&o.set({height:t}),null===(e=k.current)||void 0===e||e.height(t)},!1,!0),it||o.height===t||V||r(()=>{var e;Y(o)&&o.set({height:t}),null===(e=k.current)||void 0===e||e.height(t)},!1,!0)}}else{const t=k.current,n=o.curveEnabled?o.fontSize:Z(t,o,e);if(n!==o.fontSize){return void r(()=>{o.set(b(o,n))},!1,!0)}const i=ht();o.height===i||it||r(()=>{o.set({height:i})},!1,!0)}}}),n.useLayoutEffect(()=>{var t;if(ft&&o.curveEnabled){const e=null===(t=M.current)||void 0===t?void 0:t.getSelfRect().width;if(e){const t=e-o.a.width,r=o.a.rotation*Math.PI/180,n=-t/2*Math.cos(r),i=-t/2*Math.sin(r);o.set({width:e,x:o.a.x+n,y:o.a.y+i})}}},[ft,o.curveEnabled,o.curvePower,o.text,o.fontSize,o.fontFamily,o.fontWeight,o.fontStyle,o.letterSpacing]),n.useLayoutEffect(()=>{const t=k.current;t&&(t.width(t.width()+1e-8),t.width(t.width()-1e-8),t._setTextData(),ut())},[ft]);const gt=n.useRef(null),pt=n.useRef(o.width),mt=n.useRef(0),xt=t=>{t.evt.preventDefault();const e=c.selectedShapes.find(t=>t===o);e&&o.contentEditable&&(mt.current=function(t){var e;const r=t.target,n=function(t){var e=t.getAbsoluteTransform().copy();e.invert();var r=t.getStage().getPointerPosition();return e.point(r)}(r),o=r.textArr,i=Math.floor(n.y/(r.fontSize()*r.lineHeight())),a=o.slice(0,i).reduce((t,e)=>t+e.text.length,i),s=null!==(e=o[i])&&void 0!==e?e:o[0];let l=0;return"right"===r.align()?l=r.width()-s.width:"center"===r.align()&&(l=r.width()/2-s.width/2),a+Math.round((n.x-l)/s.width*s.text.length)}(t),o.toggleEditMode())},wt=B(o.placeholder),vt=!dt&&wt?.6:o.a.opacity;P(k,vt);const yt=getLineHeight({fontLoaded:ft,fontFamily:o.fontFamily,fontSize:o.a.fontSize,lineHeight:o.lineHeight}),bt=o.selectable||c.role===w.ADMIN,St=m(o),Et=m(o,o.stroke,"stroke"),zt=n.useMemo(()=>o.backgroundEnabled?o.curveEnabled?function({width:t,height:e,padding:r=0,cornerRadius:n=0}){const o=-r,i=-r,a=t+r,s=e+r,l=f(`M ${o} ${i} L ${a} ${i} L ${a} ${s} L ${o} ${s} Z`);return h(l,n).path}({width:o.a.width,height:o.a.height,cornerRadius:o.backgroundCornerRadius*(o.a.fontSize*yt*.5),padding:o.backgroundPadding*(o.a.fontSize*yt*.5)}):function({lines:t,lineHeight:e,width:r,align:n="left",padding:o=0,cornerRadius:i=0}){const a=p({lines:t,lineHeight:e,width:r,align:n});return g({rects:a,padding:o,cornerRadius:i})}({lines:JSON.parse(JSON.stringify(lt)),cornerRadius:o.backgroundCornerRadius*(o.a.fontSize*yt*.5),lineHeight:yt*o.a.fontSize,padding:o.backgroundPadding*(o.a.fontSize*yt*.5),width:o.a.width,align:o.align}):"",[o.backgroundEnabled,o.curveEnabled,o.backgroundCornerRadius,o.a.fontSize,o.a.height,yt,o.backgroundPadding,o.a.width,o.align,lt]),kt=X();let Ot=0;if(!o.curveEnabled){const t=lt.length*yt*o.a.fontSize,e=N(o.a,t);"middle"===o.verticalAlign?Ot=(e-t)/2:"bottom"===o.verticalAlign&&(Ot=e-t)}const Rt=o.curveEnabled?A(o.a.width,o.a.height,o.curvePower,o.a.fontSize):"",Ft=ft?L(o.fontFamily):st===o.fontFamily?"Arial":L(st),At=F(dt);return r(e,{children:[t(a,{ref:C,x:o.a.x,y:o.a.y,rotation:o.a.rotation,hideInExport:!o.showInExport,listening:!1,visible:o.backgroundEnabled,opacity:o.backgroundOpacity*vt,data:zt,fill:o.backgroundColor,offsetY:-Ot}),t(a,{data:Rt,stroke:d.stroke,strokeWidth:1,x:o.a.x,y:o.a.y,rotation:o.a.rotation,visible:!1}),t(l,Object.assign({ref:M,visible:o.curveEnabled,data:Rt,text:dt||wt,listening:!1,align:"center",textBaseline:"middle",direction:At},St,Et,{strokeWidth:o.strokeWidth,lineJoin:j(o.strokeLineJoin),fillAfterStrokeEnabled:!0,fontSize:o.a.fontSize,fontFamily:`${L(o.fontFamily)}, ${L(st)}`,fontStyle:o.fontStyle+" "+o.fontWeight,textDecoration:o.textDecoration.trim(),letterSpacing:o.letterSpacing*o.a.fontSize,x:o.a.x,y:o.a.y,rotation:o.a.rotation,opacity:o._editModeEnabled?.3:vt,hideInExport:!o.showInExport,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity})),t(s,Object.assign({ref:k,id:o.selectable?o.id:void 0,name:o.selectable?"element":"",hideInExport:!o.showInExport,editModeEnabled:o._editModeEnabled,x:o.a.x,y:o.a.y,rotation:o.a.rotation,width:o.a.width,height:I(o.a)?o.a.height:void 0,text:dt||wt,direction:At},St,Et,{lineJoin:j(o.strokeLineJoin),strokeWidth:o.strokeWidth,fillAfterStrokeEnabled:!0,fontSize:o.a.fontSize,fontFamily:Ft,fontStyle:o.fontStyle+" "+o.fontWeight,textDecoration:o.textDecoration,align:o.align,verticalAlign:o.verticalAlign,draggable:kt?o.draggable&&nt:o.draggable,preventDefault:!kt||nt,opacity:o.curveEnabled?0:vt,visible:!o._editModeEnabled,ellipsis:"ellipsis"===O.textOverflow,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity,lineHeight:yt,letterSpacing:o.letterSpacing*o.a.fontSize,listening:!!o.selectable&&bt,onDragStart:()=>{K(!0)},onDragMove:t=>{o.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{K(!1),o.set({x:t.target.x(),y:t.target.y()})},onClick:xt,onTap:xt,onTransformStart:()=>{q(!0);const t=k.current;Q.current=t.height(),pt.current=o.a.width,gt.current=t.position(),tt.current=t.rotation(),rt.current=t.height(),et.current=y(t)},onTransform:t=>{var e,r;const n=t.target;null===(e=C.current)||void 0===e||e.setAttrs({x:n.x(),y:n.y(),rotation:n.rotation(),scale:n.scale()});const i=et.current;if("middle-left"===i||"middle-right"===i){const t=n.scaleX(),e=n.width()*t,r=z(pt.current,o.a.fontSize);let i=e;e<r&&(i=r,gt.current&&n.position(gt.current)),n.width(i),n.scaleX(1),n.scaleY(1);let a=ht();if("ellipsis"===O.textOverflow){a=Q.current}else{const t=Math.max(a,Q.current);n.height(t),o.set({height:n.height()})}const s=O.textVerticalResizeEnabled?Math.max(a,Q.current):ht();o.set({x:n.x(),width:n.width(),rotation:n.rotation(),height:s}),n.skewX(0),n.skewY(0),ut()}if("top-center"===i||"bottom-center"===i){const e="resize"===O.textOverflow?ht():yt*o.a.fontSize,r=t.target.height()*t.target.scaleY();if(r<e){if(t.target.height(e),gt.current){if("top-center"===et.current){const r=tt.current*Math.PI/180,n=rt.current-e;t.target.x(gt.current.x-n*Math.sin(r)),t.target.y(gt.current.y+n*Math.cos(r))}else{t.target.position(gt.current)}}}else{t.target.height(r)}t.target.scaleY(1),t.target.scaleX(1),t.target.rotation(tt.current),t.target.skewX(0),t.target.skewY(0),rt.current=Math.max(e,r)}gt.current=t.target.position();const a=t.target.scaleX();null===(r=C.current)||void 0===r||r.setAttrs({scaleX:1,scaleY:1}),t.target.scaleX(1),t.target.scaleY(1),o.set({fontSize:o.a.fontSize*a,width:t.target.width()*a,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*a,shadowBlur:o.shadowBlur*a,shadowOffsetX:o.shadowOffsetX*a,shadowOffsetY:o.shadowOffsetY*a,strokeWidth:o.strokeWidth*a})},onTransformEnd:t=>{var e;const r=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),o.set(Object.assign(Object.assign({},S(o,o.fontSize*r)),{width:Math.ceil(t.target.width()*r),x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*r})),null===(e=C.current)||void 0===e||e.setAttrs({scaleX:1,scaleY:1}),q(!1)}})),J&&o.contentEditable&&t(i,{x:o.a.x,y:o.a.y,rotation:o.a.rotation,children:t(U,{textNodeRef:k,element:o,selectAll:_,cursorPosition:mt.current,onBlur:()=>{o.set(E(o)),o.toggleEditMode(!1)}})})]})});
@@ -1 +1 @@
1
- import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import o from"react";import{observer as i}from"mobx-react-lite";import{isAlive as n}from"mobx-state-tree";import{nodeAutorun as a}from"../utils/mobx.js";import{Arc as d,Group as h,Image as s,Rect as l,Text as c,Transformer as u}from"react-konva";import{ROLES as g}from"../model/store.js";import m from"use-image";import f from"konva";import{Portal as p}from"react-konva-utils";import{useWorkspaceStyle as v}from"./workspace-style.js";import{incrementLoader as w,triggerLoadError as x}from"../utils/loader.js";import{polotnoError as M}from"@polotno/core/errors";import{getCropRect as y}from"@polotno/core/image";import*as E from"@polotno/core/svg";import{flags as b}from"../utils/flags.js";import{roundEnabledPx as L,activeAnchorOf as T,isCornerAnchor as C,scaledBoxStyleAttrs as S,roundedBoxStyleAttrs as R}from"../utils/px.js";import{useFilteredSource as I}from"./use-filtered-source.js";import{useEvent as k}from"../hooks/use-event.js";import{useFadeIn as H}from"./use-fadein.js";import{isTouchDevice as W}from"../utils/screen.js";import{createPlayGate as X,playMedia as Y}from"../utils/media.js";import{useMediabunnyVideo as j}from"./use-mediabunny-video.js";import{getElementTimeRange as P,getVideoRestMs as A}from"../utils/timeline.js";function O(){return document.createElement("canvas")}const _=new window.Image;_.src=E.svgToURL('\n <svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">\n <path opacity="0.6" d="M4 5.49683V18.5032C4 20.05 5.68077 21.0113 7.01404 20.227L18.0694 13.7239C19.384 12.9506 19.384 11.0494 18.0694 10.2761L7.01404 3.77296C5.68077 2.98869 4 3.95 4 5.49683Z" fill="white"/>\n <path d="M4 5.49683V18.5032C4 20.05 5.68077 21.0113 7.01404 20.227L18.0694 13.7239C19.384 12.9506 19.384 11.0494 18.0694 10.2761L7.01404 3.77296C5.68077 2.98869 4 3.95 4 5.49683Z" stroke="#323232" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>\n </svg>\n');const V=new window.Image;V.src=E.svgToURL('\n <svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">\n <path opacity="0.6" d="M14 19L14 5C14 3.89543 14.8954 3 16 3L17 3C18.1046 3 19 3.89543 19 5L19 19C19 20.1046 18.1046 21 17 21L16 21C14.8954 21 14 20.1046 14 19Z" fill="white"/>\n <path opacity="0.6" d="M10 19L10 5C10 3.89543 9.10457 3 8 3L7 3C5.89543 3 5 3.89543 5 5L5 19C5 20.1046 5.89543 21 7 21L8 21C9.10457 21 10 20.1046 10 19Z" fill="white"/>\n <path d="M14 19L14 5C14 3.89543 14.8954 3 16 3L17 3C18.1046 3 19 3.89543 19 5L19 19C19 20.1046 18.1046 21 17 21L16 21C14.8954 21 14 20.1046 14 19Z" stroke="#323232" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>\n <path d="M10 19L10 5C10 3.89543 9.10457 3 8 3L7 3C5.89543 3 5 3.89543 5 5L5 19C5 20.1046 5.89543 21 7 21L8 21C9.10457 21 10 20.1046 10 19Z" stroke="#323232" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>\n </svg>\n');export const useCornerRadiusAndCrop=(e,t,r,i,n=0,a=!1)=>{var d,h;const s=Math.floor(Math.max(e.width*i,1)),l=Math.floor(Math.max(e.height*i,1)),c=Math.min(n*i,s/2,l/2),u=Math.max(e.width/r.width,e.height/r.height)*i,g=(null===(d=e.page)||void 0===d?void 0:d._exportingOrRendering)&&b.imageDownScalingEnabled&&u<1&&!a,m=t instanceof HTMLVideoElement?t.videoWidth:null==t?void 0:t.width,p=t instanceof HTMLVideoElement?t.videoHeight:null==t?void 0:t.height,v=0===r.x&&0===r.y&&r.width===m&&r.height===p,w=o.useMemo(()=>{if(t&&m&&p){return v&&0===c&&!g?void 0:O()}},[t,c,g,v,m,p]),x=()=>{if(!w||!t){return}w.width=s,w.height=l;const e=w.getContext("2d");if(!e){return}c&&(e.beginPath(),e.moveTo(c,0),e.lineTo(s-c,0),e.arc(s-c,c,c,3*Math.PI/2,0,!1),e.lineTo(s,l-c),e.arc(s-c,l-c,c,0,Math.PI/2,!1),e.lineTo(c,l),e.arc(c,l-c,c,Math.PI/2,Math.PI,!1),e.lineTo(0,c),e.arc(c,c,c,Math.PI,3*Math.PI/2,!1),e.clip());const o=g?function(e,t){var r,o;const i=O(),n=e instanceof HTMLVideoElement?e.videoWidth:e.width,a=e instanceof HTMLVideoElement?e.videoHeight:e.height;i.width=n,i.height=a,null===(r=i.getContext("2d"))||void 0===r||r.drawImage(e,0,0,i.width,i.height);const d=function(e,t,r,o,i,n,a){for(var d=new ImageData(t,r),h=new Int32Array(e.data.buffer),s=e.width,l=new Int32Array(d.data.buffer),c=d.width,u=t/n,g=r/a,m=Math.round(1/u),f=Math.round(1/g),p=m*f,v=0;v<d.height;v++){for(var w=0;w<c;w++){for(var x=0+Math.round(w/u)+(0+Math.round(v/g))*s,M=0,y=0,E=0,b=0,L=0;L<f;L++){for(var T=0;T<m;T++){var C=h[x+T+L*s];M+=C<<24>>>24,y+=C<<16>>>24,E+=C<<8>>>24,b+=C>>>24}}M=Math.round(M/p),y=Math.round(y/p),E=Math.round(E/p),b=Math.round(b/p),l[w+v*c]=b<<24|E<<16|y<<8|M}}return d}(i.getContext("2d").getImageData(0,0,i.width,i.height),Math.floor(i.width*t),Math.floor(i.height*t),0,0,i.width,i.height);return i.width=Math.floor(i.width*t),i.height=Math.floor(i.height*t),null===(o=i.getContext("2d"))||void 0===o||o.putImageData(d,0,0),i}(t,u):t,i=g?{x:Math.floor(r.x*u),y:Math.floor(r.y*u),width:Math.floor(r.width*u),height:Math.floor(r.height*u)}:r;e.drawImage(o,i.x,i.y,i.width,i.height,0,0,w.width,w.height)};return o.useLayoutEffect(()=>{x()},[w,e.width,e.height,r.x,r.y,r.width,r.height,n,i,a,null===(h=e.page)||void 0===h?void 0:h._exportingOrRendering,g]),o.useEffect(()=>()=>{w&&"CANVAS"===w.nodeName&&f.Util.releaseCanvas(w)},[w]),[w||t,x]};const D=O(),z=i(({element:r})=>{const i=Math.min(30,r.width/4,r.height/4),n=o.useRef(null);o.useEffect(()=>{const e=n.current;if(!e){return}const t=new f.Animation(t=>{e.rotate(((null==t?void 0:t.timeDiff)||0)/2)},e.getLayer());return t.start(),()=>{t.stop()}});const{mediaLoadingStyle:a}=v();return t(h,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[e(l,{width:r.width,height:r.height,fill:a.fill}),e(d,{ref:n,x:r.width/2,y:r.height/2,fill:a.textFill,outerRadius:Math.abs(i),innerRadius:Math.max(1,i-5),angle:270})]})}),U=i(({element:r})=>{const{mediaErrorStyle:o}=v(),i=Math.max(10,Math.min(30,r.width/25));return t(h,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[e(l,{width:r.width,height:r.height,fill:o.fill}),e(c,{text:"Can not load the video...",fontSize:i,width:r.width,height:r.height,align:"center",fill:o.textFill,verticalAlign:"middle",padding:5})]})});let B=function(e,t,r){const i=o.useRef("loading"),n=o.useRef(void 0),[a,d]=o.useState(0),h=o.useRef(void 0),s=o.useRef(void 0),l=o.useRef(void 0);return h.current===e&&s.current===t&&l.current===r||(i.current="loading",n.current=void 0,h.current=e,s.current=t,l.current=r),o.useLayoutEffect(function(){if(e){var o=document.createElement("video");return o.addEventListener("canplay",a),o.addEventListener("error",h),t&&(o.crossOrigin=t),r&&(o.referrerPolicy=r),o.src=e,o.load(),function(){o.removeEventListener("canplay",a),o.removeEventListener("error",h)}}function a(){i.current="loaded",n.current=o,n.current.currentTime=0,d(Math.random()),n.current.removeEventListener("canplay",a)}function h(e){var t;const r=4===o.readyState,a=o.buffered&&o.buffered.length>0&&o.buffered.end(o.buffered.length-1)/o.duration*100>=100;if(r&&a){return}const h=e.message||(null===(t=o.error)||void 0===t?void 0:t.message)||"Unknown error",s=new Error("Video failed to load: "+h);console.error(s),i.current="failed",n.current=void 0,d(Math.random())}},[e,t,r]),[n.current,i.current]};export const setVideoLoaderHook=e=>{B=e};function F(e){return e.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA}export const useLoader=(e,t,r)=>{const i=o.useRef(void 0),n=()=>{var e;null===(e=i.current)||void 0===e||e.call(i),i.current=void 0};o.useEffect(()=>n,[]),o.useLayoutEffect(()=>{const o=t.slice(0,200),a=`video with id ${r} url: ${o}`;"loading"!==e||i.current||(i.current=w(a)),"loading"!==e&&n(),"failed"===e&&x(M("VIDEO_FAILED",a,{src:o,elementId:r,reason:"load"}))},[e])};export const VideoElement=i(({element:i,store:d})=>{var c,v,x;const[M,b]=o.useState(!1),N=o.useRef(null),Z=o.useRef(null),$=i.isSelected,[q,G]=o.useState(!1),[J,K]=B(i.src,"anonymous"),[Q,ee]=o.useState(!1),te=d.activePage===i.page,re=(null===(c=i.page)||void 0===c?void 0:c._exportingOrRendering)&&"loaded"===K,oe=j(i,d,re);useLoader(K,i.src,i.id),J&&(J.playsInline=!0);const ie=((e,t,r)=>{const i=o.useMemo(()=>{var o,i;const{flipX:n,flipY:a}=e,d="svg"===e.type||E.isSvgUrl(e.src),h=navigator.userAgent.toLowerCase().indexOf("firefox")>-1;if(!(n||a||h&&d)){return t}if(!t){return null}const s=t instanceof HTMLVideoElement?t.videoWidth:null==t?void 0:t.width,l=t instanceof HTMLVideoElement?t.videoHeight:null==t?void 0:t.height;if(!s||!l){return null}const c=O();let u=1;"svg"===e.type&&(u=Math.max(e.width/s*r,e.height/l*r)),c.width=Math.max(s*u,1),c.height=Math.max(l*u,1);let g=n?-c.width:0,m=a?-c.height:0;return null===(o=c.getContext("2d"))||void 0===o||o.scale(n?-1:1,a?-1:1),null===(i=c.getContext("2d"))||void 0===i||i.drawImage(t,g,m,c.width,c.height),c},[e.flipX,e.flipY,t,e.width,e.height,r]);return o.useEffect(()=>()=>{i&&"CANVAS"===i.nodeName&&f.Util.releaseCanvas(i)},[i]),i})(i,J,d._elementsPixelRatio),ne=re&&oe||ie||D,[ae=D,de]=I(i,ne);o.useEffect(()=>{J&&d.history.ignore(()=>{i.set({duration:1e3*J.duration})})},[J]);const he=o.useMemo(X,[J]);o.useEffect(()=>{var e;if(!J){return}const t=d.animatedElementsIds,r=(!t.length||t.includes(i.id))&&d.isPlaying;if(!te||!r&&!q){return void J.pause()}q&&(J.muted=!1,J.currentTime=i.startTime*J.duration,Y(J).then(e=>{"refused"===e&&G(!1)}));const o=new f.Animation(()=>{Ce()},null===(e=N.current)||void 0===e?void 0:e.getLayer());o.start();const n=()=>{G(!1),J.currentTime=i.startTime*J.duration},a=()=>{J.currentTime>=i.endTime*J.duration&&(J.pause(),J.currentTime=i.startTime*J.duration,G(!1))};return J.addEventListener("ended",n),J.addEventListener("timeupdate",a),()=>{J.pause(),o.stop(),J.removeEventListener("ended",n),J.removeEventListener("timeupdate",a)}},[J,q,d.isPlaying,te]),o.useEffect(()=>{J&&(J.volume=i.volume)},[J,i.volume]),o.useEffect(()=>{J&&(J.playbackRate=i.speed,J.preservesPitch=!1)},[J,i.speed]);const se=o.useRef(null);o.useEffect(()=>{const e=a(i,()=>{var e,t;if(J){if(re&&oe){return Ce(),void d.currentTime}const r=d.animatedElementsIds;if(r.length&&!r.includes(i.id)){return}const o=d.activePage===i.page,n=d.currentTime>=i.page.startTime&&d.currentTime<i.page.startTime+i.page.duration,a=i.startTime*J.duration*1e3,h=J.duration*(i.endTime-i.startTime)*1e3,{start:s,end:l}=P(i.animations,i.page.duration),c=(d.currentTime||i.page.startTime)-i.page.startTime,u=n?A(d.currentTime,i.page.startTime,s,h,a,i.speed):0,g=u/1e3,m=n&&c>=s&&c<l;q||(J.muted=!m,m&&d.isPlaying&&o?J.paused?(J.currentTime=g,he.play(J)):Math.abs(1e3*J.currentTime-u)>500&&(J.currentTime=g):(he.reset(),J.paused||J.pause(),g!==J.currentTime&&(J.currentTime=g),!F(J)&&!d.isPlaying&&!se.current&&(se.current=w(`video ${i.id}`)))),Ce(),null===(t=null===(e=N.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()}});return()=>{e(),se.current&&(se.current(),se.current=null)}},[d,J,re,oe,q]),o.useEffect(()=>{var e,t;if(!J){return}if(re){return null===(e=se.current)||void 0===e||e.call(se),void(se.current=null)}const r=()=>{var e,t,r;!F(J)&&(null===(e=i.page)||void 0===e?void 0:e._exportingOrRendering)||se.current&&(Ce(),null===(r=null===(t=N.current)||void 0===t?void 0:t.getLayer())||void 0===r||r.batchDraw(),se.current(),se.current=null)};let o;return(null===(t=i.page)||void 0===t?void 0:t._exportingOrRendering)&&(o=setInterval(r,20)),J.addEventListener("timeupdate",r),J.addEventListener("canplay",r),()=>{clearInterval(o),J.removeEventListener("timeupdate",r),J.removeEventListener("canplay",r)}},[null===(v=i.page)||void 0===v?void 0:v._exportingOrRendering,J,i.id,w,re]);let{cropX:le,cropY:ce,cropWidth:ue,cropHeight:ge}=i;"loaded"!==K&&(le=ce=0,ue=ge=1);const me=(fe=ae)instanceof HTMLVideoElement?fe.videoWidth:(null==fe?void 0:fe.width)||0;var fe;const pe=(e=>e instanceof HTMLVideoElement?e.videoHeight:(null==e?void 0:e.height)||0)(ae),ve=y({cropX:le,cropY:ce,cropWidth:ue,cropHeight:ge,width:i.width,height:i.height},{width:me,height:pe}),we=ve.width,xe=ve.height,Me="svg"===i.type,ye=null!==(x=i.cornerRadius)&&void 0!==x?x:0,[Ee,be]=useCornerRadiusAndCrop(i,ae,ve,d._elementsPixelRatio,ye,!0);let[Le,Te]=((e,t,r,i)=>{const n=(e=>{const[t,r]=o.useState(e);return o.useEffect(()=>{(async()=>{const o=await async function(e){if(!E.isSvgUrl(e)){return e}const t=await E.urlToString(e),r=E.fixSize(t);return E.svgToURL(r)}(e);o!==t&&r(o)})()},[e]),t})(e.clipSrc||""),[a,d]=m(n,"anonymous"),h=e.clipSrc?d:"loaded";useLoader(h,e.clipSrc,e.id);const s=o.useMemo(()=>{if(t&&a){return O()}},[t,a]);function l(){var o;if(!a){return}const i=t instanceof HTMLVideoElement?t.videoWidth:null==t?void 0:t.width,n=t instanceof HTMLVideoElement?t.videoHeight:null==t?void 0:t.height;if(!t||!i||!n){return}if(!a||!a.width||!a.height){return}if(!s){return}const d=O(),h=Math.max(e.width/a.width*r,e.height/a.height*r);d.width=a.width*h,d.height=a.height*h,null===(o=d.getContext("2d"))||void 0===o||o.drawImage(a,0,0,d.width,d.height),s.width=Math.max(i,1),s.height=Math.max(n,1);const l=s.getContext("2d");l&&(l.save(),l.drawImage(d,0,0,i,n),f.Util.releaseCanvas(d),l.globalCompositeOperation="source-in",l.drawImage(t,0,0,s.width,s.height),l.restore())}return o.useLayoutEffect(()=>{l()},[s,t,a,e.width,e.height,r,...i]),[e.clipSrc&&a?s:t,l]})(i,Ee,d._elementsPixelRatio,[ve,ye,d._elementsPixelRatio]);const Ce=k(()=>{var e,t;de(),be(),Te(),null===(t=null===(e=N.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()});o.useEffect(()=>{Ce()});const Se=Math.max(i.width/we,i.height/xe);o.useEffect(()=>{var e;if(!i._cropModeEnabled){return}const t=null===(e=N.current)||void 0===e?void 0:e.getStage();function r(e){i._cropModeEnabled&&e.target!==Z.current&&i.toggleCropMode(!1)}function o(e){i._cropModeEnabled&&e.target.parentNode!==(null==t?void 0:t.content)&&i.toggleCropMode(!1)}return document.body.addEventListener("click",o),null==t||t.on("click",r),null==t||t.on("tap",r),()=>{document.body.removeEventListener("click",o),document.body.removeEventListener("touchstart",o),null==t||t.off("click",r),null==t||t.off("click",r)}},[i._cropModeEnabled]);const Re=o.useRef(null),Ie=o.useRef(null),ke=o.useRef(null);o.useLayoutEffect(()=>{i._cropModeEnabled&&(Ie.current.nodes([Re.current]),ke.current.nodes([Z.current]))},[i._cropModeEnabled]);const He=e=>{Math.round(e.target.x())>0&&(e.target.x(0),e.target.scaleX(1)),Math.round(e.target.y())>0&&(e.target.y(0),e.target.scaleY(1));const t=e.target.width()*e.target.scaleX(),r=e.target.height()*e.target.scaleY(),o=Math.min(1,we/t),n=Math.min(1,xe/r),a=1-o,d=Math.min(a,Math.max(0,Math.round(-e.target.x())/t)),h=1-n,s=Math.min(h,Math.max(0,Math.round(-e.target.y())/r));e.target.setAttrs({x:-d*me,y:-s*pe,scaleX:1,scaleY:1}),i.set({cropX:d,cropY:s,cropWidth:o,cropHeight:n})},We=()=>{"svg"!==i.type&&i.contentEditable&&setTimeout(()=>{n(i)&&i.toggleCropMode(!0)})},Xe="loading"===K,Ye="failed"===K,je=!Xe&&!Ye,Pe=o.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Ae=o.useRef(!1),Oe=je?i.a.opacity:0;H(N,Oe);const _e=i.selectable||d.role===g.ADMIN,Ve=W();return e(r,{children:t(h,{onMouseEnter:()=>ee(!0),onMouseLeave:()=>ee(!1),children:[Xe&&e(z,{element:i}),Ye&&e(U,{element:i}),e(s,{ref:N,name:"element",id:i.id,image:Le,x:i.a.x,y:i.a.y,width:i.a.width||1,height:i.a.height||1,rotation:i.a.rotation,opacity:Oe,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,customCrop:ve,listening:_e,draggable:Ve?i.draggable&&$:i.draggable,preventDefault:!Ve||$,hideInExport:!i.showInExport,onDragMove:e=>{i.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{i.set({x:e.target.x(),y:e.target.y()})},onDblClick:We,onDblTap:We,onTransformStart:e=>{b(!0),Ae.current=C(T(e.target)),Pe.current={cropX:i.cropX,cropY:i.cropY,cropWidth:i.cropWidth,cropHeight:i.cropHeight}},onTransform:e=>{const t=e.currentTarget,r=Math.abs(t.scaleX()-1)<1e-7?1:t.scaleX(),o=Math.abs(t.scaleY()-1)<1e-7?1:t.scaleY();t.scaleX(1),t.scaleY(1);const n=1-we/me,a=Math.min(n,Math.max(0,i.cropX)),d=1-xe/pe,h=Math.min(d,Math.max(0,i.cropY)),s=Ae.current,l=!s&&r<1&&Pe.current.cropHeight>xe/pe;let c=s?i.cropWidth:i.cropWidth*r;l&&(c=i.cropWidth);const u=!s&&o<1&&Pe.current.cropWidth>we/me;let g=s?i.cropHeight:i.cropHeight*o;u&&(g=i.cropHeight),Me&&(c=i.cropWidth,g=i.cropHeight);const m=s?Math.min(r,o):1;i.set(Object.assign({cropX:a,cropY:h,x:t.x(),y:t.y(),width:t.width()*r,height:t.height()*o,rotation:e.target.rotation(),cropWidth:Math.min(c,1-a),cropHeight:Math.min(g,1-h)},1!==m&&Object.assign(Object.assign({},S(i,m)),{borderSize:i.borderSize*m})))},onTransformEnd:e=>{const t=e.currentTarget;i.set(Object.assign(Object.assign({width:t.width(),height:t.height(),x:t.x(),y:t.y(),rotation:e.target.rotation(),cropWidth:we/me,cropHeight:xe/pe},R(i)),{borderSize:L(i.borderSize)})),b(!1)}}),e(l,{x:i.a.x,y:i.a.y,width:Math.max(i.a.width-i.borderSize,0),height:Math.max(i.a.height-i.borderSize,0),opacity:Oe,offsetX:-i.borderSize/2,offsetY:-i.borderSize/2,stroke:i.borderColor,strokeWidth:i.borderSize,listening:!1,visible:!!i.borderSize,rotation:i.rotation,cornerRadius:Math.max(0,ye-i.borderSize),hideInExport:!i.showInExport}),!d.isPlaying&&e(s,{image:q?Q||Ve?V:void 0:_,x:i.a.x,y:i.a.y,offsetX:-i.a.width/2+15/d.scale,offsetY:-i.a.height/2+15/d.scale,rotation:i.a.rotation,width:30/d.scale,height:30/d.scale,hideInExport:!0,onClick:()=>G(!q),onTap:()=>G(!q)}),i._cropModeEnabled&&t(p,{selector:".page-abs-container",enabled:!0,children:[e(l,{x:-window.innerWidth/d.scale,y:-window.innerWidth/d.scale,width:window.innerWidth/d.scale*3,height:window.innerWidth/d.scale*3,fill:"rgba(0,0,0,0.3)"}),e(s,{listening:!1,image:Le,x:i.a.x,y:i.a.y,width:i.a.width,height:i.a.height,rotation:i.a.rotation,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur}),t(h,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,scaleX:Se,scaleY:Se,children:[e(s,{image:ae,ref:Z,width:me,height:pe,opacity:.4,draggable:!0,x:-i.cropX*me,y:-i.cropY*pe,onDragMove:He,onTransform:He}),e(u,{ref:ke,anchorSize:20,enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],boundBoxFunc:(e,t)=>t.width<5||t.height<5?e:t,rotateEnabled:!1,borderEnabled:!1,anchorCornerRadius:10,anchorStrokeWidth:2,borderStrokeWidth:2}),e(l,{width:we,height:xe,ref:Re,listening:!1,onTransform:e=>{e.target.x()<-i.cropX*me-1e-9&&(e.target.x(-i.cropX*me),e.target.scaleX(1)),e.target.y()<-i.cropY*pe-1e-9&&(e.target.y(-i.cropY*pe),e.target.scaleY(1));const t=Math.min(1,Math.max(0,i.cropX+e.target.x()/me)),r=Math.min(1,Math.max(0,e.target.y()/pe+i.cropY)),o=e.target.width()*e.target.scaleX(),n=e.target.height()*e.target.scaleY(),a=Math.min(1-t,o/me),d=Math.min(1-r,n/pe),h=e.target.getAbsolutePosition(e.target.getParent().getParent());e.target.scale({x:1,y:1}),e.target.position({x:0,y:0}),i.set({x:h.x,y:h.y,cropX:t,cropY:r,cropWidth:a,cropHeight:d,width:Math.min(o*Se,me*(1-t)*Se),height:Math.min(n*Se,pe*(1-r)*Se)})}}),e(u,{ref:Ie,enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],boundBoxFunc:(e,t)=>t.width<5||t.height<5?e:t,keepRatio:!1,rotateEnabled:!1,anchorFill:"rgb(240, 240, 240)",anchorStrokeWidth:2,borderStrokeWidth:2,visible:i.resizable})]})]})]})})});
1
+ import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import i from"react";import{observer as o}from"mobx-react-lite";import{isAlive as n}from"mobx-state-tree";import{nodeAutorun as a}from"../utils/mobx.js";import{Arc as d,Group as s,Image as l,Rect as c,Text as h,Transformer as u}from"react-konva";import{ROLES as g}from"../model/store.js";import m from"use-image";import p from"konva";import{Portal as f}from"react-konva-utils";import{useWorkspaceStyle as v}from"./workspace-style.js";import{incrementLoader as w,triggerLoadError as x}from"../utils/loader.js";import{polotnoError as y}from"@polotno/core/errors";import{getCropRect as M}from"@polotno/core/image";import*as E from"@polotno/core/svg";import{flags as b}from"../utils/flags.js";import{roundEnabledPx as L,activeAnchorOf as T,isCornerAnchor as C,scaledBoxStyleAttrs as S,roundedBoxStyleAttrs as R}from"../utils/px.js";import{useFilteredSource as k}from"./use-filtered-source.js";import{useEvent as I}from"../hooks/use-event.js";import{useFadeIn as H}from"./use-fadein.js";import{isTouchDevice as X}from"../utils/screen.js";import{createPlayGate as Y,playMedia as W}from"../utils/media.js";import{useMediabunnyVideo as j}from"./use-mediabunny-video.js";import{getElementTimeRange as P,getVideoRestMs as O}from"../utils/timeline.js";import{downScaleCanvas as _}from"./downscale.js";function A(){return document.createElement("canvas")}const V=new window.Image;V.src=E.svgToURL('\n <svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">\n <path opacity="0.6" d="M4 5.49683V18.5032C4 20.05 5.68077 21.0113 7.01404 20.227L18.0694 13.7239C19.384 12.9506 19.384 11.0494 18.0694 10.2761L7.01404 3.77296C5.68077 2.98869 4 3.95 4 5.49683Z" fill="white"/>\n <path d="M4 5.49683V18.5032C4 20.05 5.68077 21.0113 7.01404 20.227L18.0694 13.7239C19.384 12.9506 19.384 11.0494 18.0694 10.2761L7.01404 3.77296C5.68077 2.98869 4 3.95 4 5.49683Z" stroke="#323232" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>\n </svg>\n');const z=new window.Image;z.src=E.svgToURL('\n <svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">\n <path opacity="0.6" d="M14 19L14 5C14 3.89543 14.8954 3 16 3L17 3C18.1046 3 19 3.89543 19 5L19 19C19 20.1046 18.1046 21 17 21L16 21C14.8954 21 14 20.1046 14 19Z" fill="white"/>\n <path opacity="0.6" d="M10 19L10 5C10 3.89543 9.10457 3 8 3L7 3C5.89543 3 5 3.89543 5 5L5 19C5 20.1046 5.89543 21 7 21L8 21C9.10457 21 10 20.1046 10 19Z" fill="white"/>\n <path d="M14 19L14 5C14 3.89543 14.8954 3 16 3L17 3C18.1046 3 19 3.89543 19 5L19 19C19 20.1046 18.1046 21 17 21L16 21C14.8954 21 14 20.1046 14 19Z" stroke="#323232" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>\n <path d="M10 19L10 5C10 3.89543 9.10457 3 8 3L7 3C5.89543 3 5 3.89543 5 5L5 19C5 20.1046 5.89543 21 7 21L8 21C9.10457 21 10 20.1046 10 19Z" stroke="#323232" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>\n </svg>\n');export const useCornerRadiusAndCrop=(e,t,r,o,n=0,a=!1)=>{var d,s;const l=Math.floor(Math.max(e.width*o,1)),c=Math.floor(Math.max(e.height*o,1)),h=Math.min(n*o,l/2,c/2),u=Math.max(e.width/r.width,e.height/r.height)*o,g=(null===(d=e.page)||void 0===d?void 0:d._exportingOrRendering)&&b.imageDownScalingEnabled&&u<1&&!a,m=t instanceof HTMLVideoElement?t.videoWidth:null==t?void 0:t.width,f=t instanceof HTMLVideoElement?t.videoHeight:null==t?void 0:t.height,v=0===r.x&&0===r.y&&r.width===m&&r.height===f,w=i.useMemo(()=>{if(t&&m&&f){return v&&0===h&&!g?void 0:A()}},[t,h,g,v,m,f]),x=()=>{if(!w||!t){return}w.width=l,w.height=c;const e=w.getContext("2d");if(!e){return}h&&(e.beginPath(),e.moveTo(h,0),e.lineTo(l-h,0),e.arc(l-h,h,h,3*Math.PI/2,0,!1),e.lineTo(l,c-h),e.arc(l-h,c-h,h,0,Math.PI/2,!1),e.lineTo(h,c),e.arc(h,c-h,h,Math.PI/2,Math.PI,!1),e.lineTo(0,h),e.arc(h,h,h,Math.PI,3*Math.PI/2,!1),e.clip());const i=g?_(t,u):t,o=g?{x:Math.floor(r.x*u),y:Math.floor(r.y*u),width:Math.floor(r.width*u),height:Math.floor(r.height*u)}:r;e.drawImage(i,o.x,o.y,o.width,o.height,0,0,w.width,w.height)};return i.useLayoutEffect(()=>{x()},[w,e.width,e.height,r.x,r.y,r.width,r.height,n,o,a,null===(s=e.page)||void 0===s?void 0:s._exportingOrRendering,g]),i.useEffect(()=>()=>{w&&"CANVAS"===w.nodeName&&p.Util.releaseCanvas(w)},[w]),[w||t,x]};const D=A(),U=o(({element:r})=>{const o=Math.min(30,r.width/4,r.height/4),n=i.useRef(null);i.useEffect(()=>{const e=n.current;if(!e){return}const t=new p.Animation(t=>{e.rotate(((null==t?void 0:t.timeDiff)||0)/2)},e.getLayer());return t.start(),()=>{t.stop()}});const{mediaLoadingStyle:a}=v();return t(s,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[e(c,{width:r.width,height:r.height,fill:a.fill}),e(d,{ref:n,x:r.width/2,y:r.height/2,fill:a.textFill,outerRadius:Math.abs(o),innerRadius:Math.max(1,o-5),angle:270})]})}),B=o(({element:r})=>{const{mediaErrorStyle:i}=v(),o=Math.max(10,Math.min(30,r.width/25));return t(s,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[e(c,{width:r.width,height:r.height,fill:i.fill}),e(h,{text:"Can not load the video...",fontSize:o,width:r.width,height:r.height,align:"center",fill:i.textFill,verticalAlign:"middle",padding:5})]})});let F=function(e,t,r){const o=i.useRef("loading"),n=i.useRef(void 0),[a,d]=i.useState(0),s=i.useRef(void 0),l=i.useRef(void 0),c=i.useRef(void 0);return s.current===e&&l.current===t&&c.current===r||(o.current="loading",n.current=void 0,s.current=e,l.current=t,c.current=r),i.useLayoutEffect(function(){if(e){var i=document.createElement("video");return i.addEventListener("canplay",a),i.addEventListener("error",s),t&&(i.crossOrigin=t),r&&(i.referrerPolicy=r),i.src=e,i.load(),function(){i.removeEventListener("canplay",a),i.removeEventListener("error",s)}}function a(){o.current="loaded",n.current=i,n.current.currentTime=0,d(Math.random()),n.current.removeEventListener("canplay",a)}function s(e){var t;const r=4===i.readyState,a=i.buffered&&i.buffered.length>0&&i.buffered.end(i.buffered.length-1)/i.duration*100>=100;if(r&&a){return}const s=e.message||(null===(t=i.error)||void 0===t?void 0:t.message)||"Unknown error",l=new Error("Video failed to load: "+s);console.error(l),o.current="failed",n.current=void 0,d(Math.random())}},[e,t,r]),[n.current,o.current]};export const setVideoLoaderHook=e=>{F=e};function N(e){return e.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA}export const useLoader=(e,t,r)=>{const o=i.useRef(void 0),n=()=>{var e;null===(e=o.current)||void 0===e||e.call(o),o.current=void 0};i.useEffect(()=>n,[]),i.useLayoutEffect(()=>{const i=t.slice(0,200),a=`video with id ${r} url: ${i}`;"loading"!==e||o.current||(o.current=w(a)),"loading"!==e&&n(),"failed"===e&&x(y("VIDEO_FAILED",a,{src:i,elementId:r,reason:"load"}))},[e])};export const VideoElement=o(({element:o,store:d})=>{var h,v,x;const[y,b]=i.useState(!1),_=i.useRef(null),Z=i.useRef(null),$=o.isSelected,[q,G]=i.useState(!1),[J,K]=F(o.src,"anonymous"),[Q,ee]=i.useState(!1),te=d.activePage===o.page,re=(null===(h=o.page)||void 0===h?void 0:h._exportingOrRendering)&&"loaded"===K,ie=j(o,d,re);useLoader(K,o.src,o.id),J&&(J.playsInline=!0);const oe=((e,t,r)=>{const o=i.useMemo(()=>{var i,o;const{flipX:n,flipY:a}=e,d="svg"===e.type||E.isSvgUrl(e.src),s=navigator.userAgent.toLowerCase().indexOf("firefox")>-1;if(!(n||a||s&&d)){return t}if(!t){return null}const l=t instanceof HTMLVideoElement?t.videoWidth:null==t?void 0:t.width,c=t instanceof HTMLVideoElement?t.videoHeight:null==t?void 0:t.height;if(!l||!c){return null}const h=A();let u=1;"svg"===e.type&&(u=Math.max(e.width/l*r,e.height/c*r)),h.width=Math.max(l*u,1),h.height=Math.max(c*u,1);let g=n?-h.width:0,m=a?-h.height:0;return null===(i=h.getContext("2d"))||void 0===i||i.scale(n?-1:1,a?-1:1),null===(o=h.getContext("2d"))||void 0===o||o.drawImage(t,g,m,h.width,h.height),h},[e.flipX,e.flipY,t,e.width,e.height,r]);return i.useEffect(()=>()=>{o&&"CANVAS"===o.nodeName&&p.Util.releaseCanvas(o)},[o]),o})(o,J,d._elementsPixelRatio),ne=re&&ie||oe||D,[ae=D,de]=k(o,ne);i.useEffect(()=>{J&&d.history.ignore(()=>{o.set({duration:1e3*J.duration})})},[J]);const se=i.useMemo(Y,[J]);i.useEffect(()=>{var e;if(!J){return}const t=d.animatedElementsIds,r=(!t.length||t.includes(o.id))&&d.isPlaying;if(!te||!r&&!q){return void J.pause()}q&&(J.muted=!1,J.currentTime=o.startTime*J.duration,W(J).then(e=>{"refused"===e&&G(!1)}));const i=new p.Animation(()=>{Ce()},null===(e=_.current)||void 0===e?void 0:e.getLayer());i.start();const n=()=>{G(!1),J.currentTime=o.startTime*J.duration},a=()=>{J.currentTime>=o.endTime*J.duration&&(J.pause(),J.currentTime=o.startTime*J.duration,G(!1))};return J.addEventListener("ended",n),J.addEventListener("timeupdate",a),()=>{J.pause(),i.stop(),J.removeEventListener("ended",n),J.removeEventListener("timeupdate",a)}},[J,q,d.isPlaying,te]),i.useEffect(()=>{J&&(J.volume=o.volume)},[J,o.volume]),i.useEffect(()=>{J&&(J.playbackRate=o.speed,J.preservesPitch=!1)},[J,o.speed]);const le=i.useRef(null);i.useEffect(()=>{const e=a(o,()=>{var e,t;if(J){if(re&&ie){return Ce(),void d.currentTime}const r=d.animatedElementsIds;if(r.length&&!r.includes(o.id)){return}const i=d.activePage===o.page,n=d.currentTime>=o.page.startTime&&d.currentTime<o.page.startTime+o.page.duration,a=o.startTime*J.duration*1e3,s=J.duration*(o.endTime-o.startTime)*1e3,{start:l,end:c}=P(o.animations,o.page.duration),h=(d.currentTime||o.page.startTime)-o.page.startTime,u=n?O(d.currentTime,o.page.startTime,l,s,a,o.speed):0,g=u/1e3,m=n&&h>=l&&h<c;q||(J.muted=!m,m&&d.isPlaying&&i?J.paused?(J.currentTime=g,se.play(J)):Math.abs(1e3*J.currentTime-u)>500&&(J.currentTime=g):(se.reset(),J.paused||J.pause(),g!==J.currentTime&&(J.currentTime=g),!N(J)&&!d.isPlaying&&!le.current&&(le.current=w(`video ${o.id}`)))),Ce(),null===(t=null===(e=_.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()}});return()=>{e(),le.current&&(le.current(),le.current=null)}},[d,J,re,ie,q]),i.useEffect(()=>{var e,t;if(!J){return}if(re){return null===(e=le.current)||void 0===e||e.call(le),void(le.current=null)}const r=()=>{var e,t,r;!N(J)&&(null===(e=o.page)||void 0===e?void 0:e._exportingOrRendering)||le.current&&(Ce(),null===(r=null===(t=_.current)||void 0===t?void 0:t.getLayer())||void 0===r||r.batchDraw(),le.current(),le.current=null)};let i;return(null===(t=o.page)||void 0===t?void 0:t._exportingOrRendering)&&(i=setInterval(r,20)),J.addEventListener("timeupdate",r),J.addEventListener("canplay",r),()=>{clearInterval(i),J.removeEventListener("timeupdate",r),J.removeEventListener("canplay",r)}},[null===(v=o.page)||void 0===v?void 0:v._exportingOrRendering,J,o.id,w,re]);let{cropX:ce,cropY:he,cropWidth:ue,cropHeight:ge}=o;"loaded"!==K&&(ce=he=0,ue=ge=1);const me=(pe=ae)instanceof HTMLVideoElement?pe.videoWidth:(null==pe?void 0:pe.width)||0;var pe;const fe=(e=>e instanceof HTMLVideoElement?e.videoHeight:(null==e?void 0:e.height)||0)(ae),ve=M({cropX:ce,cropY:he,cropWidth:ue,cropHeight:ge,width:o.width,height:o.height},{width:me,height:fe}),we=ve.width,xe=ve.height,ye="svg"===o.type,Me=null!==(x=o.cornerRadius)&&void 0!==x?x:0,[Ee,be]=useCornerRadiusAndCrop(o,ae,ve,d._elementsPixelRatio,Me,!0);let[Le,Te]=((e,t,r,o)=>{const n=(e=>{const[t,r]=i.useState(e);return i.useEffect(()=>{(async()=>{const i=await async function(e){if(!E.isSvgUrl(e)){return e}const t=await E.urlToString(e),r=E.fixSize(t);return E.svgToURL(r)}(e);i!==t&&r(i)})()},[e]),t})(e.clipSrc||""),[a,d]=m(n,"anonymous"),s=e.clipSrc?d:"loaded";useLoader(s,e.clipSrc,e.id);const l=i.useMemo(()=>{if(t&&a){return A()}},[t,a]);function c(){var i;if(!a){return}const o=t instanceof HTMLVideoElement?t.videoWidth:null==t?void 0:t.width,n=t instanceof HTMLVideoElement?t.videoHeight:null==t?void 0:t.height;if(!t||!o||!n){return}if(!a||!a.width||!a.height){return}if(!l){return}const d=A(),s=Math.max(e.width/a.width*r,e.height/a.height*r);d.width=a.width*s,d.height=a.height*s,null===(i=d.getContext("2d"))||void 0===i||i.drawImage(a,0,0,d.width,d.height),l.width=Math.max(o,1),l.height=Math.max(n,1);const c=l.getContext("2d");c&&(c.save(),c.drawImage(d,0,0,o,n),p.Util.releaseCanvas(d),c.globalCompositeOperation="source-in",c.drawImage(t,0,0,l.width,l.height),c.restore())}return i.useLayoutEffect(()=>{c()},[l,t,a,e.width,e.height,r,...o]),[e.clipSrc&&a?l:t,c]})(o,Ee,d._elementsPixelRatio,[ve,Me,d._elementsPixelRatio]);const Ce=I(()=>{var e,t;de(),be(),Te(),null===(t=null===(e=_.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()});i.useEffect(()=>{Ce()});const Se=Math.max(o.width/we,o.height/xe);i.useEffect(()=>{var e;if(!o._cropModeEnabled){return}const t=null===(e=_.current)||void 0===e?void 0:e.getStage();function r(e){o._cropModeEnabled&&e.target!==Z.current&&o.toggleCropMode(!1)}function i(e){o._cropModeEnabled&&e.target.parentNode!==(null==t?void 0:t.content)&&o.toggleCropMode(!1)}return document.body.addEventListener("click",i),null==t||t.on("click",r),null==t||t.on("tap",r),()=>{document.body.removeEventListener("click",i),document.body.removeEventListener("touchstart",i),null==t||t.off("click",r),null==t||t.off("click",r)}},[o._cropModeEnabled]);const Re=i.useRef(null),ke=i.useRef(null),Ie=i.useRef(null);i.useLayoutEffect(()=>{o._cropModeEnabled&&(ke.current.nodes([Re.current]),Ie.current.nodes([Z.current]))},[o._cropModeEnabled]);const He=e=>{Math.round(e.target.x())>0&&(e.target.x(0),e.target.scaleX(1)),Math.round(e.target.y())>0&&(e.target.y(0),e.target.scaleY(1));const t=e.target.width()*e.target.scaleX(),r=e.target.height()*e.target.scaleY(),i=Math.min(1,we/t),n=Math.min(1,xe/r),a=1-i,d=Math.min(a,Math.max(0,Math.round(-e.target.x())/t)),s=1-n,l=Math.min(s,Math.max(0,Math.round(-e.target.y())/r));e.target.setAttrs({x:-d*me,y:-l*fe,scaleX:1,scaleY:1}),o.set({cropX:d,cropY:l,cropWidth:i,cropHeight:n})},Xe=()=>{"svg"!==o.type&&o.contentEditable&&setTimeout(()=>{n(o)&&o.toggleCropMode(!0)})},Ye="loading"===K,We="failed"===K,je=!Ye&&!We,Pe=i.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Oe=i.useRef(!1),_e=je?o.a.opacity:0;H(_,_e);const Ae=o.selectable||d.role===g.ADMIN,Ve=X();return e(r,{children:t(s,{onMouseEnter:()=>ee(!0),onMouseLeave:()=>ee(!1),children:[Ye&&e(U,{element:o}),We&&e(B,{element:o}),e(l,{ref:_,name:"element",id:o.id,image:Le,x:o.a.x,y:o.a.y,width:o.a.width||1,height:o.a.height||1,rotation:o.a.rotation,opacity:_e,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity,customCrop:ve,listening:Ae,draggable:Ve?o.draggable&&$:o.draggable,preventDefault:!Ve||$,hideInExport:!o.showInExport,onDragMove:e=>{o.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{o.set({x:e.target.x(),y:e.target.y()})},onDblClick:Xe,onDblTap:Xe,onTransformStart:e=>{b(!0),Oe.current=C(T(e.target)),Pe.current={cropX:o.cropX,cropY:o.cropY,cropWidth:o.cropWidth,cropHeight:o.cropHeight}},onTransform:e=>{const t=e.currentTarget,r=Math.abs(t.scaleX()-1)<1e-7?1:t.scaleX(),i=Math.abs(t.scaleY()-1)<1e-7?1:t.scaleY();t.scaleX(1),t.scaleY(1);const n=1-we/me,a=Math.min(n,Math.max(0,o.cropX)),d=1-xe/fe,s=Math.min(d,Math.max(0,o.cropY)),l=Oe.current,c=!l&&r<1&&Pe.current.cropHeight>xe/fe;let h=l?o.cropWidth:o.cropWidth*r;c&&(h=o.cropWidth);const u=!l&&i<1&&Pe.current.cropWidth>we/me;let g=l?o.cropHeight:o.cropHeight*i;u&&(g=o.cropHeight),ye&&(h=o.cropWidth,g=o.cropHeight);const m=l?Math.min(r,i):1;o.set(Object.assign({cropX:a,cropY:s,x:t.x(),y:t.y(),width:t.width()*r,height:t.height()*i,rotation:e.target.rotation(),cropWidth:Math.min(h,1-a),cropHeight:Math.min(g,1-s)},1!==m&&Object.assign(Object.assign({},S(o,m)),{borderSize:o.borderSize*m})))},onTransformEnd:e=>{const t=e.currentTarget;o.set(Object.assign(Object.assign({width:t.width(),height:t.height(),x:t.x(),y:t.y(),rotation:e.target.rotation(),cropWidth:we/me,cropHeight:xe/fe},R(o)),{borderSize:L(o.borderSize)})),b(!1)}}),e(c,{x:o.a.x,y:o.a.y,width:Math.max(o.a.width-o.borderSize,0),height:Math.max(o.a.height-o.borderSize,0),opacity:_e,offsetX:-o.borderSize/2,offsetY:-o.borderSize/2,stroke:o.borderColor,strokeWidth:o.borderSize,listening:!1,visible:!!o.borderSize,rotation:o.rotation,cornerRadius:Math.max(0,Me-o.borderSize),hideInExport:!o.showInExport}),!d.isPlaying&&e(l,{image:q?Q||Ve?z:void 0:V,x:o.a.x,y:o.a.y,offsetX:-o.a.width/2+15/d.scale,offsetY:-o.a.height/2+15/d.scale,rotation:o.a.rotation,width:30/d.scale,height:30/d.scale,hideInExport:!0,onClick:()=>G(!q),onTap:()=>G(!q)}),o._cropModeEnabled&&t(f,{selector:".page-abs-container",enabled:!0,children:[e(c,{x:-window.innerWidth/d.scale,y:-window.innerWidth/d.scale,width:window.innerWidth/d.scale*3,height:window.innerWidth/d.scale*3,fill:"rgba(0,0,0,0.3)"}),e(l,{listening:!1,image:Le,x:o.a.x,y:o.a.y,width:o.a.width,height:o.a.height,rotation:o.a.rotation,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur}),t(s,{x:o.a.x,y:o.a.y,rotation:o.a.rotation,scaleX:Se,scaleY:Se,children:[e(l,{image:ae,ref:Z,width:me,height:fe,opacity:.4,draggable:!0,x:-o.cropX*me,y:-o.cropY*fe,onDragMove:He,onTransform:He}),e(u,{ref:Ie,anchorSize:20,enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],boundBoxFunc:(e,t)=>t.width<5||t.height<5?e:t,rotateEnabled:!1,borderEnabled:!1,anchorCornerRadius:10,anchorStrokeWidth:2,borderStrokeWidth:2}),e(c,{width:we,height:xe,ref:Re,listening:!1,onTransform:e=>{e.target.x()<-o.cropX*me-1e-9&&(e.target.x(-o.cropX*me),e.target.scaleX(1)),e.target.y()<-o.cropY*fe-1e-9&&(e.target.y(-o.cropY*fe),e.target.scaleY(1));const t=Math.min(1,Math.max(0,o.cropX+e.target.x()/me)),r=Math.min(1,Math.max(0,e.target.y()/fe+o.cropY)),i=e.target.width()*e.target.scaleX(),n=e.target.height()*e.target.scaleY(),a=Math.min(1-t,i/me),d=Math.min(1-r,n/fe),s=e.target.getAbsolutePosition(e.target.getParent().getParent());e.target.scale({x:1,y:1}),e.target.position({x:0,y:0}),o.set({x:s.x,y:s.y,cropX:t,cropY:r,cropWidth:a,cropHeight:d,width:Math.min(i*Se,me*(1-t)*Se),height:Math.min(n*Se,fe*(1-r)*Se)})}}),e(u,{ref:ke,enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],boundBoxFunc:(e,t)=>t.width<5||t.height<5?e:t,keepRatio:!1,rotateEnabled:!1,anchorFill:"rgb(240, 240, 240)",anchorStrokeWidth:2,borderStrokeWidth:2,visible:o.resizable})]})]})]})})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polotno",
3
- "version": "4.8.0",
3
+ "version": "4.9.0",
4
4
  "description": "Design Editor Framework",
5
5
  "author": "Anton Lavrenov",
6
6
  "keywords": [
@@ -96,14 +96,14 @@
96
96
  },
97
97
  "dependencies": {
98
98
  "@base-ui/react": "^1.7.0",
99
- "@polotno/core": "0.6.0",
100
- "@polotno/html-export": "0.3.1",
99
+ "@polotno/core": "0.7.0",
100
+ "@polotno/html-export": "0.4.0",
101
101
  "@polotno/schema": "0.6.0",
102
- "@polotno/svg-export": "0.3.1",
102
+ "@polotno/svg-export": "0.4.0",
103
103
  "class-variance-authority": "^0.7.1",
104
104
  "clsx": "^2.1.1",
105
105
  "gifuct-js": "^2.1.2",
106
- "konva": "^10.3.0",
106
+ "konva": "^10.3.1",
107
107
  "lodash-es": "^4.18.1",
108
108
  "lucide-react": "^1.29.0",
109
109
  "mediabunny": "^1.52.3",