polotno 4.10.0 → 4.11.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.
@@ -1,3 +1,10 @@
1
1
  type DownscaleSource = HTMLCanvasElement | HTMLImageElement | HTMLVideoElement;
2
- export declare function downScaleCanvas(image: DownscaleSource, scale: number): HTMLCanvasElement;
2
+ export declare const RESAMPLE_SKIP_THRESHOLD = 0.95;
3
+ export interface SourceRegion {
4
+ x: number;
5
+ y: number;
6
+ width: number;
7
+ height: number;
8
+ }
9
+ export declare function resampleCanvas(image: DownscaleSource, region: SourceRegion, destWidth: number, destHeight: number, dest?: SourceRegion): HTMLCanvasElement;
3
10
  export {};
@@ -1 +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}
1
+ import t from"konva";function e(t){return t instanceof HTMLVideoElement?{width:t.videoWidth,height:t.videoHeight}:{width:t.width,height:t.height}}export const RESAMPLE_SKIP_THRESHOLD=.95;function n(t){const e=Math.abs(t);if(e<=.5){return.75-e*e}if(e<=1.5){const t=1.5-e;return.5*t*t}return 0}function h(t,e,h,i,o,a){const r=a/h,c=Math.max(1,1/r),s=1.5*c,g=[];for(let l=0;l<i;l++){const i=e+(l+.5-o)/r-.5,a=Math.max(0,Math.floor(e)),d=Math.min(t-1,Math.ceil(e+h)-1),f=Math.max(a,Math.floor(i-s)),u=Math.min(d,Math.ceil(i+s));if(u<f){g.push({start:a,weights:new Float32Array(0)});continue}const w=new Float32Array(u-f+1);let m=0;for(let t=f;t<=u;t++){const e=n((t-i)/c);w[t-f]=e,m+=e}if(m>0){for(let t=0;t<w.length;t++){w[t]/=m}}g.push({start:f,weights:w})}return g}function i(t,e,n,h,i){const o=new Float32Array(h*n*4);for(let a=0;a<n;a++){const r=a*e*4;for(let e=0;e<h;e++){const{start:h,weights:c}=i[e];let s=0,g=0,l=0,d=0;for(let e=0;e<c.length;e++){const n=r+4*(h+e),i=c[e];s+=t[n]*i,g+=t[n+1]*i,l+=t[n+2]*i,d+=t[n+3]*i}const f=4*(e*n+a);o[f]=s,o[f+1]=g,o[f+2]=l,o[f+3]=d}}return o}const o=-.25;export function resampleCanvas(n,a,r,c,s={x:0,y:0,width:r,height:c}){const g=e(n),l=function(n,h,i){let{width:o,height:a}=e(n),r=n;for(;o>2*h||a>2*i;){o=Math.max(2*h,Math.round(o/2)),a=Math.max(2*i,Math.round(a/2));const e=document.createElement("canvas");e.width=o,e.height=a;const c=e.getContext("2d");c.imageSmoothingQuality="high",c.drawImage(r,0,0,o,a),r!==n&&t.Util.releaseCanvas(r),r=e}return r}(n,g.width*r/a.width,g.height*c/a.height);let d;if(l instanceof HTMLCanvasElement){d=l}else{const t=e(l);d=document.createElement("canvas"),d.width=t.width,d.height=t.height,d.getContext("2d").drawImage(l,0,0)}const f=d.getContext("2d").getImageData(0,0,d.width,d.height),u=d.width/g.width,w=d.height/g.height,m={x:a.x*u,y:a.y*w,width:a.width*u,height:a.height*w};d!==n&&t.Util.releaseCanvas(d);const M=function(t,e,n,a,r){const c=i(function(t){const e=new Uint16Array(t.length);for(let n=0;n<t.length;n+=4){const h=t[n+3];e[n]=t[n]*h,e[n+1]=t[n+1]*h,e[n+2]=t[n+2]*h,e[n+3]=h}return e}(t.data),t.width,t.height,n,h(t.width,e.x,e.width,n,r.x,r.width)),s=function(t,e,n){const h=new ImageData(e,n),i=h.data;for(let o=0;o<i.length;o+=4){const e=t[o+3];if(e>.001){const n=1/e;i[o]=t[o]*n,i[o+1]=t[o+1]*n,i[o+2]=t[o+2]*n}i[o+3]=e}return h}(i(c,t.height,n,a,h(t.height,e.y,e.height,a,r.y,r.height)),n,a);return function(t){const{width:e,height:n,data:h}=t,i=new Float32Array(e*n*3);for(let a=0;a<n;a++){for(let t=0;t<e;t++){const n=4*(a*e+t),r=4*(a*e+Math.max(0,t-1)),c=4*(a*e+Math.min(e-1,t+1)),s=h[r+3]>0?o:0,g=h[c+3]>0?o:0,l=o-s+1.5+(o-g),d=3*(a*e+t);for(let t=0;t<3;t++){i[d+t]=l*h[n+t]+s*h[r+t]+g*h[c+t]}}}for(let a=0;a<n;a++){for(let t=0;t<e;t++){const r=4*(a*e+t);if(0===h[r+3]){continue}const c=Math.max(0,a-1)*e+t,s=Math.min(n-1,a+1)*e+t,g=h[4*c+3]>0?o:0,l=h[4*s+3]>0?o:0,d=o-g+1.5+(o-l),f=3*(a*e+t);for(let t=0;t<3;t++){h[r+t]=d*i[f+t]+g*i[3*c+t]+l*i[3*s+t]}}}}(s),s}(f,m,r,c,s),x=document.createElement("canvas");return x.width=r,x.height=c,x.getContext("2d").putImageData(M,0,0),x}
@@ -1 +1 @@
1
- import{jsx as t,Fragment as e,jsxs as a}from"react/jsx-runtime";import r from"react";import{observer as s}from"mobx-react-lite";import{Group as o,Rect as i,Path as h}from"react-konva";import{ROLES as n}from"../model/store.js";import{subTypeToPathDataFunc as d}from"@polotno/core/figure-to-svg";import{isTouchDevice as c}from"../utils/screen.js";import{roundEnabledPx as l,activeAnchorOf as g,isCornerAnchor as m,scaledBoxStyleAttrs as f,roundedBoxStyleAttrs as p}from"../utils/px.js";import{useColor as u}from"./use-color.js";const w={};export const FigureElement=s(({element:b,store:x})=>{const k=r.useRef(null),j=r.useRef(!1),y=b.selectable||x.role===n.ADMIN,O=c(),v=b.isSelected,W=Math.min(b.strokeWidth,b.width/2,b.height/2),M=Math.max(0,Math.min(b.width/2,b.height/2,b.cornerRadius)),E=u(b,b.a.fill,"fill"),R=Object.assign(Object.assign({width:b.a.width,height:b.a.height},E),{cornerRadius:M,shadowEnabled:b.shadowEnabled,shadowBlur:b.shadowBlur,shadowOffsetX:b.shadowOffsetX,shadowOffsetY:b.shadowOffsetY,shadowColor:b.shadowColor,shadowOpacity:b.shadowOpacity,preventDefault:!O||v,hideInExport:!b.showInExport}),T=u(b,b.stroke,"stroke"),P=Object.assign(Object.assign({visible:W>0,x:W/2,y:W/2,width:b.a.width-W,height:b.a.height-W},T),{strokeWidth:W,cornerRadius:Math.max(0,M-W),dash:b.dash.map(t=>t*W),hideInExport:!b.showInExport,listening:!1}),C=Math.max(0,10-Math.min(b.a.width,b.a.height)),I=(S=b.subType,w[S]||(w[S]=(D=d(S),s(({element:s,fillProps:n,strokeProps:d,hitStrokeWidth:c})=>{const l=D({width:s.a.width,height:s.a.height,cornerRadius:s.cornerRadius}),g=r.useRef(null);return a(e,{children:[t(i,{width:s.a.width,height:s.a.height,fill:"transparent",hitStrokeWidth:c}),t(h,Object.assign({},n,{ref:g,data:l})),t(o,{clipFunc:t=>{const e=g.current;if(e){var a=e.dataArray;t.beginPath();for(var r=0;r<a.length;r++){var s=a[r].command,o=a[r].points;switch(s){case"L":t.lineTo(o[0],o[1]);break;case"M":t.moveTo(o[0],o[1]);break;case"C":t.bezierCurveTo(o[0],o[1],o[2],o[3],o[4],o[5]);break;case"Q":t.quadraticCurveTo(o[0],o[1],o[2],o[3]);break;case"A":var i=o[0],h=o[1],n=o[2],d=o[3],c=o[4],l=o[5],m=o[6],f=o[7],p=n>d?n:d,u=n>d?1:n/d,w=n>d?d/n:1;t.translate(i,h),t.rotate(m),t.scale(u,w),t.arc(0,0,p,c,c+l,!f),t.scale(1/u,1/w),t.rotate(-m),t.translate(-i,-h);break;case"z":t.closePath()}}}},children:t(h,Object.assign({},d,{x:0,y:0,data:l,strokeWidth:2*d.strokeWidth,dash:d.dash.map(t=>t)}))})]})}))),w[S]||o);var S,D;return t(e,{children:t(o,{ref:k,id:b.id,x:b.a.x,y:b.a.y,rotation:b.a.rotation,opacity:b.a.opacity,listening:y,draggable:O?b.draggable&&v:b.draggable,name:"element",onDragMove:t=>{b.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{b.set({x:t.target.x(),y:t.target.y()})},onTransformStart:t=>{j.current=m(g(t.target))},onTransform:t=>{const e=t.target.scale();t.target.scaleX(1),t.target.scaleY(1);const a=j.current?Math.min(e.x,e.y):1;b.set(Object.assign({width:b.width*e.x,height:b.height*e.y,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation()},1!==a&&Object.assign(Object.assign({},f(b,a)),{strokeWidth:b.strokeWidth*a})))},onTransformEnd:()=>{b.set(Object.assign(Object.assign({},p(b)),{strokeWidth:l(b.strokeWidth)}))},children:t(I,{element:b,fillProps:R,strokeProps:P,hitStrokeWidth:C})})})});
1
+ import{jsx as t,Fragment as e,jsxs as a}from"react/jsx-runtime";import r from"react";import{observer as s}from"mobx-react-lite";import{Group as o,Rect as i,Path as h}from"react-konva";import{ROLES as n}from"../model/store.js";import{subTypeToPathDataFunc as d}from"@polotno/core/figure-to-svg";import{isTouchDevice as c}from"../utils/screen.js";import{roundEnabledPx as l,activeAnchorOf as g,isCornerAnchor as m,scaledBoxStyleAttrs as f,roundedBoxStyleAttrs as p,roundedGeometryAttrs as u,roundedPosition as b}from"../utils/px.js";import{useColor as w}from"./use-color.js";const x={};export const FigureElement=s(({element:k,store:j})=>{const O=r.useRef(null),y=r.useRef(!1),v=k.selectable||j.role===n.ADMIN,W=c(),M=k.isSelected,E=Math.min(k.strokeWidth,k.width/2,k.height/2),R=Math.max(0,Math.min(k.width/2,k.height/2,k.cornerRadius)),T=w(k,k.a.fill,"fill"),P=Object.assign(Object.assign({width:k.a.width,height:k.a.height},T),{cornerRadius:R,shadowEnabled:k.shadowEnabled,shadowBlur:k.shadowBlur,shadowOffsetX:k.shadowOffsetX,shadowOffsetY:k.shadowOffsetY,shadowColor:k.shadowColor,shadowOpacity:k.shadowOpacity,preventDefault:!W||M,hideInExport:!k.showInExport}),C=w(k,k.stroke,"stroke"),I=Object.assign(Object.assign({visible:E>0,x:E/2,y:E/2,width:k.a.width-E,height:k.a.height-E},C),{strokeWidth:E,cornerRadius:Math.max(0,R-E),dash:k.dash.map(t=>t*E),hideInExport:!k.showInExport,listening:!1}),S=Math.max(0,10-Math.min(k.a.width,k.a.height)),D=(A=k.subType,x[A]||(x[A]=(F=d(A),s(({element:s,fillProps:n,strokeProps:d,hitStrokeWidth:c})=>{const l=F({width:s.a.width,height:s.a.height,cornerRadius:s.cornerRadius}),g=r.useRef(null);return a(e,{children:[t(i,{width:s.a.width,height:s.a.height,fill:"transparent",hitStrokeWidth:c}),t(h,Object.assign({},n,{ref:g,data:l})),t(o,{clipFunc:t=>{const e=g.current;if(e){var a=e.dataArray;t.beginPath();for(var r=0;r<a.length;r++){var s=a[r].command,o=a[r].points;switch(s){case"L":t.lineTo(o[0],o[1]);break;case"M":t.moveTo(o[0],o[1]);break;case"C":t.bezierCurveTo(o[0],o[1],o[2],o[3],o[4],o[5]);break;case"Q":t.quadraticCurveTo(o[0],o[1],o[2],o[3]);break;case"A":var i=o[0],h=o[1],n=o[2],d=o[3],c=o[4],l=o[5],m=o[6],f=o[7],p=n>d?n:d,u=n>d?1:n/d,b=n>d?d/n:1;t.translate(i,h),t.rotate(m),t.scale(u,b),t.arc(0,0,p,c,c+l,!f),t.scale(1/u,1/b),t.rotate(-m),t.translate(-i,-h);break;case"z":t.closePath()}}}},children:t(h,Object.assign({},d,{x:0,y:0,data:l,strokeWidth:2*d.strokeWidth,dash:d.dash.map(t=>t)}))})]})}))),x[A]||o);var A,F;return t(e,{children:t(o,{ref:O,id:k.id,x:k.a.x,y:k.a.y,rotation:k.a.rotation,opacity:k.a.opacity,listening:v,draggable:W?k.draggable&&M:k.draggable,name:"element",onDragMove:t=>{k.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{k.set(b(t.target))},onTransformStart:t=>{y.current=m(g(t.target))},onTransform:t=>{const e=t.target.scale();t.target.scaleX(1),t.target.scaleY(1);const a=y.current?Math.min(e.x,e.y):1;k.set(Object.assign({width:k.width*e.x,height:k.height*e.y,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation()},1!==a&&Object.assign(Object.assign({},f(k,a)),{strokeWidth:k.strokeWidth*a})))},onTransformEnd:()=>{k.set(Object.assign(Object.assign(Object.assign({},u(k)),p(k)),{strokeWidth:l(k.strokeWidth)}))},children:t(D,{element:k,fillProps:P,strokeProps:I,hitStrokeWidth:S})})})});
@@ -1 +1 @@
1
- import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import i from"react";import{observer as o}from"mobx-react-lite";import{nodeAutorun as a}from"../utils/mobx.js";import{Image as n,Group as s,Rect as h,Arc as d,Text as l}from"react-konva";import{ROLES as c}from"../model/store.js";import g from"konva";import{parseGIF as m,decompressFrames as f}from"gifuct-js";import{useCornerRadiusAndCrop as u}from"./video-element.js";import{useImageLoader as p}from"./image-element.js";import{useWorkspaceStyle as w}from"./workspace-style.js";import{useFilteredSource as x}from"./use-filtered-source.js";import{useFadeIn as y}from"./use-fadein.js";import{isTouchDevice as b}from"../utils/screen.js";import{roundEnabledPx as E,activeAnchorOf as v,isCornerAnchor as S,scaledBoxStyleAttrs as j,roundedBoxStyleAttrs as I}from"../utils/px.js";import{useEvent as M}from"../hooks/use-event.js";function R(t,e,r){const i=e.getContext("2d"),o=r.getContext("2d");if(!i||!o){return}2===t.disposalType&&i.clearRect(0,0,e.width,e.height),r.width=t.width,r.height=t.height;const a=o.createImageData(t.width,t.height);a.data.set(t.patch),o.putImageData(a,0,0),i.drawImage(r,t.left,t.top)}const O=o(({element:r})=>{const o=Math.min(30,r.width/4,r.height/4),a=i.useRef(null);i.useEffect(()=>{const t=a.current;if(!t){return}const e=new g.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}},[]);const{mediaLoadingStyle:n}=w();return e(s,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(h,{width:r.width,height:r.height,fill:n.fill}),t(d,{ref:a,x:r.width/2,y:r.height/2,fill:n.textFill,outerRadius:Math.abs(o),innerRadius:Math.max(1,o-5),angle:270})]})}),z=o(({element:r})=>{const{mediaErrorStyle:i}=w(),o=Math.max(10,Math.min(30,r.width/22));return e(s,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(h,{width:r.width,height:r.height,fill:i.fill}),t(l,{text:"Cannot load the GIF...",fontSize:o,width:r.width,height:r.height,align:"center",fill:i.textFill,verticalAlign:"middle",padding:5})]})});export const GifElement=o(({element:o,store:s})=>{var d,l;const[w,C]=i.useState(!1),T=i.useRef(!1),D=o.isSelected,X=i.useRef(null),Y=i.useRef(void 0),[k,F,A,_]=function(t){const[e,r]=i.useState([]),[o,a]=i.useState(0),[n,s]=i.useState("loading"),[h,d]=i.useState({width:0,height:0});return i.useEffect(()=>{(async()=>{try{const e=await fetch(t),i=await e.arrayBuffer(),o=m(i),n=f(o,!0),h=o.lsd.width,l=o.lsd.height;d({width:h,height:l});const c=n.map(t=>({patch:new Uint8ClampedArray(t.patch),delay:t.delay,width:t.dims.width,height:t.dims.height,left:t.dims.left,top:t.dims.top,disposalType:t.disposalType})),g=c.reduce((t,e)=>t+e.delay,0);r(c),a(g),s("loaded")}catch(e){console.error("Failed to load GIF:",e),s("failed")}})()},[t]),[e,o,h,n]}(o.src);p(_,o.src,o.id),i.useEffect(()=>(Y.current=document.createElement("canvas"),()=>{Y.current&&g.Util.releaseCanvas(Y.current)}),[]),i.useEffect(()=>{if("loaded"===_&&Y.current&&(Y.current.width=A.width,Y.current.height=A.height,k.length>0)){const t=Y.current.getContext("2d");if(t){t.clearRect(0,0,A.width,A.height);const e=document.createElement("canvas");R(k[0],Y.current,e)}}},[A,_,k]),i.useEffect(()=>{var t;if(!k.length||!Y.current){return}const e=Y.current;e.width=A.width,e.height=A.height;const r=document.createElement("canvas"),i=e.getContext("2d");i&&i.clearRect(0,0,e.width,e.height);let n=-1;R(k[0],e,r),K(),n=0;const h=t=>{const i=(t=>{const e=t%F;let r=0;for(let i=0;i<k.length;i++){if(r+=k[i].delay,r>e){return i}}return 0})(t);i!==n&&(R(k[i],e,r),K(),X.current.getLayer().draw(),n=i)};if(s.isPlaying||(null===(t=o.page)||void 0===t?void 0:t._exportingOrRendering)){return a(o,()=>{h(s.currentTime-o.page.startTime)})}{const t=window.setInterval(()=>{h(s.currentTime||performance.now())},16);return()=>{clearInterval(t)}}},[s.isPlaying,k,F,null===(d=o.page)||void 0===d?void 0:d._exportingOrRendering]),i.useEffect(()=>{F&&s.history.ignore(()=>{o.set({duration:F})})},[F]);let{cropX:B,cropY:G,cropWidth:L,cropHeight:P}=o;"loaded"!==_&&(B=G=0,L=P=1);const U={x:A.width*B,y:A.height*G,width:A.width*L,height:A.height*P},W=null!==(l=o.cornerRadius)&&void 0!==l?l:0,[H,N]=x(o,Y.current),[q,J]=u(o,H,U,s._elementsPixelRatio,W,w||o._cropModeEnabled),K=M(()=>{N(),J()}),Q="loading"===_,V="failed"===_,Z=Q||V?0:o.a.opacity;y(X,Z);const $=o.selectable||s.role===c.ADMIN,tt=b();return e(r,{children:[Q&&t(O,{element:o}),V&&t(z,{element:o}),t(n,{ref:X,name:"element",id:o.id,image:q,x:o.a.x,y:o.a.y,width:o.a.width||1,height:o.a.height||1,rotation:o.a.rotation,opacity:Z,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity,customCrop:U,listening:$,draggable:tt?o.draggable&&D:o.draggable,preventDefault:!tt||D,hideInExport:!o.showInExport,onDragMove:t=>{o.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{o.set({x:t.target.x(),y:t.target.y()})},onTransformStart:t=>{C(!0),T.current=S(v(t.target))},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),i=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const a=T.current?Math.min(r,i):1;o.set(Object.assign({x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*i,rotation:e.rotation()},1!==a&&Object.assign(Object.assign({},j(o,a)),{borderSize:o.borderSize*a})))},onTransformEnd:()=>{o.set(Object.assign(Object.assign({},I(o)),{borderSize:E(o.borderSize)})),C(!1)}}),t(h,{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:Z,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,W-o.borderSize),hideInExport:!o.showInExport})]})});
1
+ import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import i from"react";import{observer as o}from"mobx-react-lite";import{nodeAutorun as a}from"../utils/mobx.js";import{Image as n,Group as s,Rect as h,Arc as d,Text as l}from"react-konva";import{ROLES as c}from"../model/store.js";import g from"konva";import{parseGIF as m,decompressFrames as f}from"gifuct-js";import{useCornerRadiusAndCrop as u}from"./video-element.js";import{useImageLoader as p}from"./image-element.js";import{useWorkspaceStyle as w}from"./workspace-style.js";import{useFilteredSource as x}from"./use-filtered-source.js";import{useFadeIn as y}from"./use-fadein.js";import{isTouchDevice as b}from"../utils/screen.js";import{roundEnabledPx as E,activeAnchorOf as v,isCornerAnchor as S,scaledBoxStyleAttrs as j,roundedBoxStyleAttrs as I,roundedGeometryAttrs as M,roundedPosition as O}from"../utils/px.js";import{useEvent as R}from"../hooks/use-event.js";function z(t,e,r){const i=e.getContext("2d"),o=r.getContext("2d");if(!i||!o){return}2===t.disposalType&&i.clearRect(0,0,e.width,e.height),r.width=t.width,r.height=t.height;const a=o.createImageData(t.width,t.height);a.data.set(t.patch),o.putImageData(a,0,0),i.drawImage(r,t.left,t.top)}const C=o(({element:r})=>{const o=Math.min(30,r.width/4,r.height/4),a=i.useRef(null);i.useEffect(()=>{const t=a.current;if(!t){return}const e=new g.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}},[]);const{mediaLoadingStyle:n}=w();return e(s,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(h,{width:r.width,height:r.height,fill:n.fill}),t(d,{ref:a,x:r.width/2,y:r.height/2,fill:n.textFill,outerRadius:Math.abs(o),innerRadius:Math.max(1,o-5),angle:270})]})}),T=o(({element:r})=>{const{mediaErrorStyle:i}=w(),o=Math.max(10,Math.min(30,r.width/22));return e(s,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(h,{width:r.width,height:r.height,fill:i.fill}),t(l,{text:"Cannot load the GIF...",fontSize:o,width:r.width,height:r.height,align:"center",fill:i.textFill,verticalAlign:"middle",padding:5})]})});export const GifElement=o(({element:o,store:s})=>{var d,l;const[w,D]=i.useState(!1),X=i.useRef(!1),Y=o.isSelected,k=i.useRef(null),F=i.useRef(void 0),[A,_,B,G]=function(t){const[e,r]=i.useState([]),[o,a]=i.useState(0),[n,s]=i.useState("loading"),[h,d]=i.useState({width:0,height:0});return i.useEffect(()=>{(async()=>{try{const e=await fetch(t),i=await e.arrayBuffer(),o=m(i),n=f(o,!0),h=o.lsd.width,l=o.lsd.height;d({width:h,height:l});const c=n.map(t=>({patch:new Uint8ClampedArray(t.patch),delay:t.delay,width:t.dims.width,height:t.dims.height,left:t.dims.left,top:t.dims.top,disposalType:t.disposalType})),g=c.reduce((t,e)=>t+e.delay,0);r(c),a(g),s("loaded")}catch(e){console.error("Failed to load GIF:",e),s("failed")}})()},[t]),[e,o,h,n]}(o.src);p(G,o.src,o.id),i.useEffect(()=>(F.current=document.createElement("canvas"),()=>{F.current&&g.Util.releaseCanvas(F.current)}),[]),i.useEffect(()=>{if("loaded"===G&&F.current&&(F.current.width=B.width,F.current.height=B.height,A.length>0)){const t=F.current.getContext("2d");if(t){t.clearRect(0,0,B.width,B.height);const e=document.createElement("canvas");z(A[0],F.current,e)}}},[B,G,A]),i.useEffect(()=>{var t;if(!A.length||!F.current){return}const e=F.current;e.width=B.width,e.height=B.height;const r=document.createElement("canvas"),i=e.getContext("2d");i&&i.clearRect(0,0,e.width,e.height);let n=-1;z(A[0],e,r),V(),n=0;const h=t=>{const i=(t=>{const e=t%_;let r=0;for(let i=0;i<A.length;i++){if(r+=A[i].delay,r>e){return i}}return 0})(t);i!==n&&(z(A[i],e,r),V(),k.current.getLayer().draw(),n=i)};if(s.isPlaying||(null===(t=o.page)||void 0===t?void 0:t._exportingOrRendering)){return a(o,()=>{h(s.currentTime-o.page.startTime)})}{const t=window.setInterval(()=>{h(s.currentTime||performance.now())},16);return()=>{clearInterval(t)}}},[s.isPlaying,A,_,null===(d=o.page)||void 0===d?void 0:d._exportingOrRendering]),i.useEffect(()=>{_&&s.history.ignore(()=>{o.set({duration:_})})},[_]);let{cropX:L,cropY:P,cropWidth:U,cropHeight:W}=o;"loaded"!==G&&(L=P=0,U=W=1);const H={x:B.width*L,y:B.height*P,width:B.width*U,height:B.height*W},N=null!==(l=o.cornerRadius)&&void 0!==l?l:0,[q,J]=x(o,F.current),[K,Q]=u(o,q,H,s._elementsPixelRatio,N,w||o._cropModeEnabled),V=R(()=>{J(),Q()}),Z="loading"===G,$="failed"===G,tt=Z||$?0:o.a.opacity;y(k,tt);const et=o.selectable||s.role===c.ADMIN,rt=b();return e(r,{children:[Z&&t(C,{element:o}),$&&t(T,{element:o}),t(n,{ref:k,name:"element",id:o.id,image:K,x:o.a.x,y:o.a.y,width:o.a.width||1,height:o.a.height||1,rotation:o.a.rotation,opacity:tt,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity,customCrop:H,listening:et,draggable:rt?o.draggable&&Y:o.draggable,preventDefault:!rt||Y,hideInExport:!o.showInExport,onDragMove:t=>{o.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{o.set(O(t.target))},onTransformStart:t=>{D(!0),X.current=S(v(t.target))},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),i=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const a=X.current?Math.min(r,i):1;o.set(Object.assign({x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*i,rotation:e.rotation()},1!==a&&Object.assign(Object.assign({},j(o,a)),{borderSize:o.borderSize*a})))},onTransformEnd:()=>{o.set(Object.assign(Object.assign(Object.assign({},M(o)),I(o)),{borderSize:E(o.borderSize)})),D(!1)}}),t(h,{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:tt,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,N-o.borderSize),hideInExport:!o.showInExport})]})});
@@ -1 +1 @@
1
- import{jsx as t,Fragment as e,jsxs as o}from"react/jsx-runtime";import r from"react";import{observer as n}from"mobx-react-lite";import{Group as i,Image as a,Path as s,Rect as l}from"react-konva";import{activeAnchorOf as c,scaledFontSizeAttrs as f,roundedFontSizeAttrs as d,roundedTextDecorationAttrs as h,textMinResizeWidth as u}from"../utils/px.js";import g from"konva";import{isAlive as m}from"mobx-state-tree";import{nodeAutorun as x}from"../utils/mobx.js";import{flags as p}from"../utils/flags.js";import{applyFilter as y}from"./apply-filters.js";import{getLineHeight as w,useFontFamiliesLoader as b,usePrevious as v}from"./text-element.js";import{getCurvePath as E}from"@polotno/core/text-html";import{useColor as S}from"./use-color.js";import{useWorkspaceStyle as O}from"./workspace-style.js";import{detectSize as k,detectLineRects as z,htmlToCanvas as F}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as j}from"@polotno/core/background-shape";import{useFadeIn as M}from"./use-fadein.js";import{Html as R}from"react-konva-utils";import{incrementLoader as X,triggerLoadError as Y}from"../utils/loader.js";import{removeTags as I}from"@polotno/core/text";import{cssFontFamily as C}from"@polotno/core/fonts";import*as P from"../utils/fonts.js";import{isTouchDevice as T}from"../utils/screen.js";import{getLimitedFontSize as B}from"./text-element/max-font-size.js";import{visiblePlaceholder as A}from"./text-element/placeholder.js";import{useDelayer as W}from"./use-delayer.js";import{getHtml as $}from"@polotno/core/text-html";import{hasMeasuredHeight as D,resolveTextBoxHeight as H}from"@polotno/core/text-types";import{normalizeRichTextHtml as _,extractFontFamiliesFromHtml as L,scaleRichTextFontSizesInHtml as J}from"../utils/rich-text-html.js";import{findFitFontSize as N}from"./text-element/fit-font-size.js";import{RichEditor as q,RichTextContainer as V,applyOpacityToColor as G,quillRef as K,createQuill as Q,setQuillContent as U,setQuillFormats as Z,syncQuillFormatState as tt,setCursorFromCoords as et}from"./rich-editor.js";export{K as quillRef,Q as createQuill,U as setQuillContent,Z as setQuillFormats,tt as syncQuillFormatState,et as setCursorFromCoords};function ot(t){const e=document.createElement("canvas"),o=e.getContext("2d");return null==o||o.drawImage(t,0,0,e.width,e.height),function(t){return!function(t){const e=t.getContext("2d").getImageData(0,0,t.width,t.height).data;for(let o=0;o<e.length;o+=4){if(0!==e[o+3]){return!0}}return!1}(t)}(e)}const rt=/^((?!chrome|android).)*safari/i.test(navigator.userAgent),nt=navigator.userAgent.includes("Firefox");export const HTMLElement=n(({element:n,store:K})=>{var Q,U;const{textOverflowIndicatorStyle:Z}=O(),tt=r.useRef(null),[et,it]=r.useState(),[at,st]=r.useState(!1),[lt,ct]=r.useState(!1),ft=r.useRef(n.height),dt=n.isSelected,ht=v(dt),ut=n.fontSize/3,{textVerticalResizeEnabled:gt}=p,mt=p.autoDeleteEmptyText,xt=v(n.fontFamily),pt=r.useMemo(()=>_(n.text,{fontSize:n.fontSize,fontFamily:n.fontFamily}),[n.text,n.fontSize,n.fontFamily]),yt=r.useMemo(()=>[...new Set([n.fontFamily,...L(pt)])].filter(Boolean),[n.fontFamily,pt]),[wt]=b(K,yt,n.fontStyle,n.fontWeight,n.text||n.placeholder),bt=n._editModeEnabled;M(tt);const vt=wt?n.fontFamily:xt!==n.fontFamily?xt:"Arial",Et=S(n).fill,St=$(n,{fontFamily:vt,color:Et,textOverflow:p.textOverflow}),Ot=r.useRef([]),kt=r.useRef(!0),[,zt]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=Ot.current;t.length>0&&(Ot.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{kt.current=!1;const t=Ot.current;t.length>0&&(Ot.current=[],t.forEach(t=>t()))},[]);let{width:Ft,height:jt}=function(t,e,o){return r.useMemo(()=>k(t),[t,e.width,o])}(St,n,wt);r.useEffect(()=>{if(!wt){return}const t=(t,e,o,r)=>{const i=X(`text ${n.id}`),a=()=>{K.history.ignore(t,e,o).then(()=>{kt.current?(Ot.current.push(i),zt()):i()})};r?queueMicrotask(a):a()};if(n.curveEnabled&&Math.abs(Ft-n.width)>1&&!at&&t(()=>{if(m(n)&&Math.abs(Ft-n.width)>1){const t=n.x+n.width/2;n.set({width:Ft,x:t-Ft/2})}},!1,!0),!D(n)){return void t(()=>{m(n)&&(D(n)||n.set({height:jt}))},void 0,void 0,!0)}const{textOverflow:e}=p;if("change-font-size"!==e||at||bt||n.curveEnabled){"resize"===e&&(gt&&n.height<jt&&!at&&t(()=>{m(n)&&n.set({height:jt})},!1,!0),gt||n.height===jt||at||t(()=>{m(n)&&n.set({height:jt})},!1,!0))}else{const e=N(n,{fontFamily:n.fontFamily,textOverflow:p.textOverflow});e.fontSize!==n.fontSize?t(()=>{if(!m(n)){return}const t=f(n,e.fontSize);void 0!==e.text&&e.text!==n.text&&(t.text=e.text),n.set(t)},!1,!0):n.height!==jt&&(gt&&n.height<jt?t(()=>{m(n)&&n.set({height:jt})},!1,!0):gt||t(()=>{m(n)&&n.set({height:jt})},!1,!0))}});const Mt=r.useMemo(()=>{const t={lastArgs:null,lastResult:null};return async function(e){return t.lastArgs&&t.lastResult&&(o=t.lastArgs,r=e,JSON.stringify(o)===JSON.stringify(r))||(t.lastResult=await F(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),Rt=r.useRef(0),Xt=r.useRef(null);r.useEffect(()=>{at||bt||(async()=>{Rt.current++;const t=Rt.current;let e=X(`text ${n.id} ${t}`);Xt.current&&Xt.current(),Xt.current=e,ct(!0);let o=null;const r=rt?5:1;for(let a=0;a<r;a++){const e=a>0?Mt:F;try{if(o=await e({skipCache:a>0,html:St,width:Ft||n.width||1,height:H(n,jt)||1,fontFamily:vt,fontFamilies:L(St),resolveFont:t=>K.fonts.find(e=>e.fontFamily===t)||P.globalFonts.find(e=>e.fontFamily===t),padding:ut,pixelRatio:K._elementsPixelRatio,font:K.fonts.find(t=>t.fontFamily===vt)||P.globalFonts.find(t=>t.fontFamily===vt)}),t!==Rt.current){return}if((rt||nt)&&o&&ot(o)){await new Promise(t=>setTimeout(t,50*(a+1)));continue}break}catch(i){console.error(i),Y(`Error rendering rich text with id ${n.id}`);break}}o?it(o):e?(e(),e=null):console.error("Finish function is called twice!"),ct(!1)})()},[St,at,jt,bt,vt,n.height,K._elementsPixelRatio,wt]);const[Yt,It]=W(lt,300),[Ct]=W(at,300,!0),Pt=Ct||Yt;r.useEffect(()=>{var t;if(!Pt){return x(n,()=>{const t=tt.current;y(t,n)})}null===(t=tt.current)||void 0===t||t.clearCache()},[et,Pt,n.shadowColor,n.shadowOffsetX,n.shadowOffsetY,n.shadowOpacity]),r.useEffect(()=>{et&&!lt&&Xt.current&&(Xt.current(),Xt.current=null)},[et,lt]),r.useLayoutEffect(()=>{if(!wt){return}if(!n.curveEnabled){return}const t=new g.TextPath({data:E(n.a.width,n.a.height,n.curvePower,n.a.fontSize),text:I(n.text),letterSpacing:n.letterSpacing*n.a.fontSize,fontSize:n.a.fontSize,fontFamily:C(n.fontFamily),fontWeight:n.fontWeight,fontStyle:n.fontStyle,align:"center",textBaseline:"middle",fill:n.fill}),e=t.getSelfRect().width;if(e){const t=e-n.a.width,o=n.a.rotation*Math.PI/180,r=-t/2*Math.cos(o),i=-t/2*Math.sin(o);queueMicrotask(()=>{m(n)&&n.set({width:e,x:n.a.x+r,y:n.a.y+i})})}t.destroy()},[wt,n.curveEnabled,n.curvePower,n.text,n.fontSize,n.fontFamily,n.fontWeight,n.fontStyle,n.letterSpacing]),r.useEffect(()=>()=>{Xt.current&&Xt.current()},[]),r.useEffect(()=>{ht&&!dt&&mt&&""===I(n.text)&&n.removable&&!A(n.placeholder)&&K.deleteElements([n.id])},[mt,n.placeholder,n.removable,n.text,dt,ht]);const Tt=H(n,jt);let Bt=0;"middle"===n.verticalAlign&&(Bt=(Tt-jt)/2),"bottom"===n.verticalAlign&&(Bt=Tt-jt);const At=w({fontLoaded:wt,fontFamily:n.fontFamily,fontSize:n.fontSize,lineHeight:n.lineHeight}),Wt=n.backgroundPadding*(n.fontSize*At*.5),$t=n.backgroundCornerRadius*(n.fontSize*At*.5),Dt=r.useMemo(()=>{if(!n.backgroundEnabled||n.curveEnabled||n.legacyBackground){return""}const t=z(St);if(0===t.length){return""}const e=t.map(t=>({left:t.left+ut,right:t.right+ut,top:t.top+ut,bottom:t.bottom+ut}));return j({rects:e,padding:Wt,cornerRadius:$t})},[n.backgroundEnabled,n.curveEnabled,n.legacyBackground,St,wt,Wt,$t,ut]),Ht=T(),_t=E(n.a.width,jt,n.curvePower,n.fontSize),Lt=r.useRef(null),Jt=r.useRef(n.width),Nt=r.useRef(""),qt=r.useRef(0),Vt=r.useRef(0),Gt=r.useRef(null),Kt=bt&&n.strokeWidth>0&&!n.curveEnabled,Qt=Ft>0?(n.a.width+2*ut)/(Ft+2*ut):1;return o(e,{children:[n.curveEnabled||n.legacyBackground?t(l,{x:n.a.x,y:n.a.y,offsetX:Wt/Qt,offsetY:Wt/Qt,rotation:n.a.rotation,scaleX:Qt,scaleY:Qt,hideInExport:!n.showInExport,listening:!1,visible:n.backgroundEnabled,opacity:n.backgroundOpacity*n.a.opacity,fill:n.backgroundColor,width:n.width+2*Wt,height:Tt+2*Wt,cornerRadius:$t}):t(s,{name:"html-background",x:n.a.x,y:n.a.y,offsetX:ut/Qt,offsetY:(ut-Bt)/Qt,rotation:n.a.rotation,scaleX:Qt,scaleY:Qt,hideInExport:!n.showInExport,listening:!1,visible:n.backgroundEnabled&&Dt.length>0,opacity:n.backgroundOpacity*n.a.opacity,fill:n.backgroundColor,data:Dt}),t(s,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,data:_t,stroke:Z.stroke,strokeWidth:1,visible:!1}),t(l,{ref:tt,name:"element",x:n.a.x,y:n.a.y,listening:n.selectable,rotation:n.a.rotation,width:n.a.width,height:n.a.height,visible:!Pt,draggable:Ht?n.draggable&&dt:n.draggable,preventDefault:!Ht||dt,opacity:bt?0:n.a.opacity,hideInExport:!n.showInExport,onDragMove:t=>{n.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{n.set({x:t.target.x(),y:t.target.y()})},id:n.id,onDblClick:t=>{n.contentEditable&&(Gt.current={x:t.evt.clientX,y:t.evt.clientY},n.toggleEditMode(!0))},onDblTap:t=>{var e;if(n.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Gt.current=o?{x:o.clientX,y:o.clientY}:null,n.toggleEditMode(!0)}},onTransformStart:t=>{st(!0),ft.current=n.height,Jt.current=n.width,Lt.current=t.target.position(),Nt.current=c(t.target),qt.current=n.a.rotation,Vt.current=n.height},onTransform:t=>{const e=t.target,o=Nt.current,r="middle-left"===o||"middle-right"===o,i="top-center"===o||"bottom-center"===o,a=e.scaleX();if(r){const t=e.scaleX(),o=e.width()*t,r=u(Jt.current,n.fontSize);let i=o;o<r&&(i=r,Lt.current&&e.position(Lt.current)),e.width(i),e.scaleX(1),e.scaleY(1);const a=$(Object.assign(Object.assign({},n.toJSON()),{width:i}),{fontFamily:vt,color:Et,textOverflow:p.textOverflow}),s=k(a).height,l=p.textVerticalResizeEnabled?Math.max(s,ft.current):s;e.height(l),n.set({width:e.width(),height:l,x:e.x(),y:e.y()})}else if(i){const t="resize"===p.textOverflow,o="number"==typeof n.lineHeight?n.lineHeight:1,r=t?jt:o*n.fontSize,i=e.height()*e.scaleY();if(i<r){if(e.height(r),Lt.current){if("top-center"===Nt.current){const t=qt.current*Math.PI/180,o=Vt.current-r;e.x(Lt.current.x-o*Math.sin(t)),e.y(Lt.current.y+o*Math.cos(t))}else{e.position(Lt.current)}}}else{e.height(i)}e.scaleY(1),e.scaleX(1),Vt.current=Math.max(r,i),n.set({x:e.x(),y:e.y(),height:e.height(),rotation:e.rotation()})}else{e.scaleX(1),e.scaleY(1),n.set({fontSize:n.fontSize*a,letterSpacing:n.letterSpacing,width:e.width()*a,x:e.x(),y:e.y(),rotation:e.rotation(),height:e.height()*a,shadowBlur:n.shadowBlur*a,shadowOffsetX:n.shadowOffsetX*a,shadowOffsetY:n.shadowOffsetY*a,strokeWidth:n.strokeWidth*a,text:J(n.text,a)})}Lt.current=t.target.position()},onTransformEnd:t=>{st(!1),ct(!0);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),n.set(Object.assign(Object.assign({},d(n,n.fontSize*e)),{width:t.target.width()*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),text:J(n.text,e)}))}}),t(a,{ref:tt,image:et,x:n.a.x,y:n.a.y,offsetX:ut,offsetY:ut-Bt,listening:!1,rotation:n.a.rotation,width:n.a.width+2*ut,height:(n.a.width+2*ut)*((null!==(Q=null==et?void 0:et.height)&&void 0!==Q?Q:0)/((null==et?void 0:et.width)||1)||1),visible:!Pt&&!bt,opacity:n.a.opacity,shadowEnabled:n.shadowEnabled,shadowBlur:n.shadowBlur,shadowOffsetX:n.shadowOffsetX,shadowOffsetY:n.shadowOffsetY,shadowColor:n.shadowColor,shadowOpacity:n.shadowOpacity,hideInExport:!n.showInExport,editModeEnabled:n._editModeEnabled||at||Pt}),(Pt&&!bt||Kt)&&t(i,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,offsetY:-Bt,children:t(R,{divProps:{style:{pointerEvents:"none"}},children:t(V,{dangerouslySetInnerHTML:{__html:St},style:{pointerEvents:"none",opacity:n.a.opacity,textShadow:n.shadowEnabled?`${n.shadowOffsetX}px ${n.shadowOffsetY}px ${n.shadowBlur}px ${G(n.shadowColor,null!==(U=n.shadowOpacity)&&void 0!==U?U:1)}`:void 0}})})}),bt&&t(i,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,offsetY:-Bt,children:t(R,{divProps:{style:{zIndex:20}},children:t(q,{element:n,onChange:t=>{const e=B({oldText:I(n.text),newText:I(t),element:n});n.set(Object.assign({text:t},f(n,e)))},onBlur:t=>{m(n)&&(n.set(h(n)),n.toggleEditMode(!1),It(!0))},clickCoords:Gt.current})})})]})});
1
+ import{jsx as t,Fragment as e,jsxs as o}from"react/jsx-runtime";import r from"react";import{observer as n}from"mobx-react-lite";import{Group as i,Image as a,Path as s,Rect as l}from"react-konva";import{activeAnchorOf as c,scaledFontSizeAttrs as f,roundedFontSizeAttrs as d,roundedTextDecorationAttrs as h,roundedGeometryPx as u,roundedPosition as g,textMinResizeWidth as m}from"../utils/px.js";import x from"konva";import{isAlive as p}from"mobx-state-tree";import{nodeAutorun as y}from"../utils/mobx.js";import{flags as w}from"../utils/flags.js";import{applyFilter as b}from"./apply-filters.js";import{getLineHeight as v,useFontFamiliesLoader as E,usePrevious as S}from"./text-element.js";import{getCurvePath as O}from"@polotno/core/text-html";import{useColor as k}from"./use-color.js";import{useWorkspaceStyle as z}from"./workspace-style.js";import{detectSize as F,detectLineRects as j,htmlToCanvas as M}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as R}from"@polotno/core/background-shape";import{useFadeIn as X}from"./use-fadein.js";import{Html as Y}from"react-konva-utils";import{incrementLoader as I,triggerLoadError as C}from"../utils/loader.js";import{removeTags as P}from"@polotno/core/text";import{cssFontFamily as T}from"@polotno/core/fonts";import*as B from"../utils/fonts.js";import{isTouchDevice as A}from"../utils/screen.js";import{getLimitedFontSize as W}from"./text-element/max-font-size.js";import{visiblePlaceholder as $}from"./text-element/placeholder.js";import{useDelayer as D}from"./use-delayer.js";import{getHtml as H,textLayoutWidth as _}from"@polotno/core/text-html";import{hasMeasuredHeight as L,resolveTextBoxHeight as J}from"@polotno/core/text-types";import{normalizeRichTextHtml as N,extractFontFamiliesFromHtml as q,scaleRichTextFontSizesInHtml as V}from"../utils/rich-text-html.js";import{findFitFontSize as G}from"./text-element/fit-font-size.js";import{RichEditor as K,RichTextContainer as Q,applyOpacityToColor as U,quillRef as Z,createQuill as tt,setQuillContent as et,setQuillFormats as ot,syncQuillFormatState as rt,setCursorFromCoords as nt}from"./rich-editor.js";export{Z as quillRef,tt as createQuill,et as setQuillContent,ot as setQuillFormats,rt as syncQuillFormatState,nt as setCursorFromCoords};function it(t){const e=document.createElement("canvas"),o=e.getContext("2d");return null==o||o.drawImage(t,0,0,e.width,e.height),function(t){return!function(t){const e=t.getContext("2d").getImageData(0,0,t.width,t.height).data;for(let o=0;o<e.length;o+=4){if(0!==e[o+3]){return!0}}return!1}(t)}(e)}const at=/^((?!chrome|android).)*safari/i.test(navigator.userAgent),st=navigator.userAgent.includes("Firefox");export const HTMLElement=n(({element:n,store:Z})=>{var tt,et;const{textOverflowIndicatorStyle:ot}=z(),rt=r.useRef(null),[nt,lt]=r.useState(),[ct,ft]=r.useState(!1),[dt,ht]=r.useState(!1),ut=r.useRef(n.height),gt=n.isSelected,mt=S(gt),xt=n.fontSize/3,pt=_(n.a.width,n.letterSpacing,n.fontSize)+2*xt,{textVerticalResizeEnabled:yt}=w,wt=w.autoDeleteEmptyText,bt=S(n.fontFamily),vt=r.useMemo(()=>N(n.text,{fontSize:n.fontSize,fontFamily:n.fontFamily}),[n.text,n.fontSize,n.fontFamily]),Et=r.useMemo(()=>[...new Set([n.fontFamily,...q(vt)])].filter(Boolean),[n.fontFamily,vt]),[St]=E(Z,Et,n.fontStyle,n.fontWeight,n.text||n.placeholder),Ot=n._editModeEnabled;X(rt);const kt=St?n.fontFamily:bt!==n.fontFamily?bt:"Arial",zt=k(n).fill,Ft=H(n,{fontFamily:kt,color:zt,textOverflow:w.textOverflow}),jt=r.useRef([]),Mt=r.useRef(!0),[,Rt]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=jt.current;t.length>0&&(jt.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{Mt.current=!1;const t=jt.current;t.length>0&&(jt.current=[],t.forEach(t=>t()))},[]);let{width:Xt,height:Yt}=function(t,e,o){return r.useMemo(()=>F(t),[t,e.width,o])}(Ft,n,St);r.useEffect(()=>{if(!St){return}const t=(t,e,o,r)=>{const i=I(`text ${n.id}`),a=()=>{Z.history.ignore(t,e,o).then(()=>{Mt.current?(jt.current.push(i),Rt()):i()})};r?queueMicrotask(a):a()};if(n.curveEnabled&&Math.abs(Xt-n.width)>1&&!ct&&t(()=>{if(p(n)&&Math.abs(Xt-n.width)>1){const t=n.x+n.width/2;n.set({width:Xt,x:t-Xt/2})}},!1,!0),!L(n)){return void t(()=>{p(n)&&(L(n)||n.set({height:Yt}))},void 0,void 0,!0)}const{textOverflow:e}=w;if("change-font-size"!==e||ct||Ot||n.curveEnabled){"resize"===e&&(yt&&n.height<Yt&&!ct&&t(()=>{p(n)&&n.set({height:Yt})},!1,!0),yt||n.height===Yt||ct||t(()=>{p(n)&&n.set({height:Yt})},!1,!0))}else{const e=G(n,{fontFamily:n.fontFamily,textOverflow:w.textOverflow});e.fontSize!==n.fontSize?t(()=>{if(!p(n)){return}const t=f(n,e.fontSize);void 0!==e.text&&e.text!==n.text&&(t.text=e.text),n.set(t)},!1,!0):n.height!==Yt&&(yt&&n.height<Yt?t(()=>{p(n)&&n.set({height:Yt})},!1,!0):yt||t(()=>{p(n)&&n.set({height:Yt})},!1,!0))}});const It=r.useMemo(()=>{const t={lastArgs:null,lastResult:null};return async function(e){return t.lastArgs&&t.lastResult&&(o=t.lastArgs,r=e,JSON.stringify(o)===JSON.stringify(r))||(t.lastResult=await M(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),Ct=r.useRef(0),Pt=r.useRef(null);r.useEffect(()=>{ct||Ot||(async()=>{Ct.current++;const t=Ct.current;let e=I(`text ${n.id} ${t}`);Pt.current&&Pt.current(),Pt.current=e,ht(!0);let o=null;const r=at?5:1;for(let a=0;a<r;a++){const e=a>0?It:M;try{if(o=await e({skipCache:a>0,html:Ft,width:Xt||n.width||1,height:J(n,Yt)||1,fontFamily:kt,fontFamilies:q(Ft),resolveFont:t=>Z.fonts.find(e=>e.fontFamily===t)||B.globalFonts.find(e=>e.fontFamily===t),padding:xt,pixelRatio:Z._elementsPixelRatio,font:Z.fonts.find(t=>t.fontFamily===kt)||B.globalFonts.find(t=>t.fontFamily===kt)}),t!==Ct.current){return}if((at||st)&&o&&it(o)){await new Promise(t=>setTimeout(t,50*(a+1)));continue}break}catch(i){console.error(i),C(`Error rendering rich text with id ${n.id}`);break}}o?lt(o):e?(e(),e=null):console.error("Finish function is called twice!"),ht(!1)})()},[Ft,ct,Yt,Ot,kt,n.height,Z._elementsPixelRatio,St]);const[Tt,Bt]=D(dt,300),[At]=D(ct,300,!0),Wt=At||Tt;r.useEffect(()=>{var t;if(!Wt){return y(n,()=>{const t=rt.current;b(t,n)})}null===(t=rt.current)||void 0===t||t.clearCache()},[nt,Wt,n.shadowColor,n.shadowOffsetX,n.shadowOffsetY,n.shadowOpacity]),r.useEffect(()=>{nt&&!dt&&Pt.current&&(Pt.current(),Pt.current=null)},[nt,dt]),r.useLayoutEffect(()=>{if(!St){return}if(!n.curveEnabled){return}const t=new x.TextPath({data:O(n.a.width,n.a.height,n.curvePower,n.a.fontSize),text:P(n.text),letterSpacing:n.letterSpacing*n.a.fontSize,fontSize:n.a.fontSize,fontFamily:T(n.fontFamily),fontWeight:n.fontWeight,fontStyle:n.fontStyle,align:"center",textBaseline:"middle",fill:n.fill}),e=t.getSelfRect().width;if(e){const t=e-n.a.width,o=n.a.rotation*Math.PI/180,r=-t/2*Math.cos(o),i=-t/2*Math.sin(o);queueMicrotask(()=>{p(n)&&n.set({width:e,x:n.a.x+r,y:n.a.y+i})})}t.destroy()},[St,n.curveEnabled,n.curvePower,n.text,n.fontSize,n.fontFamily,n.fontWeight,n.fontStyle,n.letterSpacing]),r.useEffect(()=>()=>{Pt.current&&Pt.current()},[]),r.useEffect(()=>{mt&&!gt&&wt&&""===P(n.text)&&n.removable&&!$(n.placeholder)&&Z.deleteElements([n.id])},[wt,n.placeholder,n.removable,n.text,gt,mt]);const $t=J(n,Yt);let Dt=0;"middle"===n.verticalAlign&&(Dt=($t-Yt)/2),"bottom"===n.verticalAlign&&(Dt=$t-Yt);const Ht=v({fontLoaded:St,fontFamily:n.fontFamily,fontSize:n.fontSize,lineHeight:n.lineHeight}),_t=n.backgroundPadding*(n.fontSize*Ht*.5),Lt=n.backgroundCornerRadius*(n.fontSize*Ht*.5),Jt=r.useMemo(()=>{if(!n.backgroundEnabled||n.curveEnabled||n.legacyBackground){return""}const t=j(Ft);if(0===t.length){return""}const e=t.map(t=>({left:t.left+xt,right:t.right+xt,top:t.top+xt,bottom:t.bottom+xt}));return R({rects:e,padding:_t,cornerRadius:Lt})},[n.backgroundEnabled,n.curveEnabled,n.legacyBackground,Ft,St,_t,Lt,xt]),Nt=A(),qt=O(n.a.width,Yt,n.curvePower,n.fontSize),Vt=r.useRef(null),Gt=r.useRef(n.width),Kt=r.useRef(""),Qt=r.useRef(0),Ut=r.useRef(0),Zt=r.useRef(null),te=Ot&&n.strokeWidth>0&&!n.curveEnabled,ee=Xt>0?pt/(Xt+2*xt):1;return o(e,{children:[n.curveEnabled||n.legacyBackground?t(l,{x:n.a.x,y:n.a.y,offsetX:_t/ee,offsetY:_t/ee,rotation:n.a.rotation,scaleX:ee,scaleY:ee,hideInExport:!n.showInExport,listening:!1,visible:n.backgroundEnabled,opacity:n.backgroundOpacity*n.a.opacity,fill:n.backgroundColor,width:n.width+2*_t,height:$t+2*_t,cornerRadius:Lt}):t(s,{name:"html-background",x:n.a.x,y:n.a.y,offsetX:xt/ee,offsetY:(xt-Dt)/ee,rotation:n.a.rotation,scaleX:ee,scaleY:ee,hideInExport:!n.showInExport,listening:!1,visible:n.backgroundEnabled&&Jt.length>0,opacity:n.backgroundOpacity*n.a.opacity,fill:n.backgroundColor,data:Jt}),t(s,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,data:qt,stroke:ot.stroke,strokeWidth:1,visible:!1}),t(l,{ref:rt,name:"element",x:n.a.x,y:n.a.y,listening:n.selectable,rotation:n.a.rotation,width:n.a.width,height:n.a.height,visible:!Wt,draggable:Nt?n.draggable&&gt:n.draggable,preventDefault:!Nt||gt,opacity:Ot?0:n.a.opacity,hideInExport:!n.showInExport,onDragMove:t=>{n.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{n.set(g(t.target))},id:n.id,onDblClick:t=>{n.contentEditable&&(Zt.current={x:t.evt.clientX,y:t.evt.clientY},n.toggleEditMode(!0))},onDblTap:t=>{var e;if(n.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Zt.current=o?{x:o.clientX,y:o.clientY}:null,n.toggleEditMode(!0)}},onTransformStart:t=>{ft(!0),ut.current=n.height,Gt.current=n.width,Vt.current=t.target.position(),Kt.current=c(t.target),Qt.current=n.a.rotation,Ut.current=n.height},onTransform:t=>{const e=t.target,o=Kt.current,r="middle-left"===o||"middle-right"===o,i="top-center"===o||"bottom-center"===o,a=e.scaleX();if(r){const t=e.scaleX(),o=e.width()*t,r=m(Gt.current,n.fontSize);let i=o;o<r&&(i=r,Vt.current&&e.position(Vt.current)),e.width(i),e.scaleX(1),e.scaleY(1);const a=H(Object.assign(Object.assign({},n.toJSON()),{width:i}),{fontFamily:kt,color:zt,textOverflow:w.textOverflow}),s=F(a).height,l=w.textVerticalResizeEnabled?Math.max(s,ut.current):s;e.height(l),n.set({width:e.width(),height:l,x:e.x(),y:e.y()})}else if(i){const t="resize"===w.textOverflow,o="number"==typeof n.lineHeight?n.lineHeight:1,r=t?Yt:o*n.fontSize,i=e.height()*e.scaleY();if(i<r){if(e.height(r),Vt.current){if("top-center"===Kt.current){const t=Qt.current*Math.PI/180,o=Ut.current-r;e.x(Vt.current.x-o*Math.sin(t)),e.y(Vt.current.y+o*Math.cos(t))}else{e.position(Vt.current)}}}else{e.height(i)}e.scaleY(1),e.scaleX(1),Ut.current=Math.max(r,i),n.set({x:e.x(),y:e.y(),height:e.height(),rotation:e.rotation()})}else{e.scaleX(1),e.scaleY(1),n.set({fontSize:n.fontSize*a,letterSpacing:n.letterSpacing,width:e.width()*a,x:e.x(),y:e.y(),rotation:e.rotation(),height:e.height()*a,shadowBlur:n.shadowBlur*a,shadowOffsetX:n.shadowOffsetX*a,shadowOffsetY:n.shadowOffsetY*a,strokeWidth:n.strokeWidth*a,text:V(n.text,a)})}Vt.current=t.target.position()},onTransformEnd:t=>{ft(!1),ht(!0);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),n.set(Object.assign(Object.assign({},d(n,n.fontSize*e)),{width:u(t.target.width()*e),x:u(t.target.x()),y:u(t.target.y()),rotation:u(t.target.rotation()),text:V(n.text,e)}))}}),t(a,{ref:rt,image:nt,x:n.a.x,y:n.a.y,offsetX:xt,offsetY:xt-Dt,listening:!1,rotation:n.a.rotation,width:pt,height:pt*((null!==(tt=null==nt?void 0:nt.height)&&void 0!==tt?tt:0)/((null==nt?void 0:nt.width)||1)||1),visible:!Wt&&!Ot,opacity:n.a.opacity,shadowEnabled:n.shadowEnabled,shadowBlur:n.shadowBlur,shadowOffsetX:n.shadowOffsetX,shadowOffsetY:n.shadowOffsetY,shadowColor:n.shadowColor,shadowOpacity:n.shadowOpacity,hideInExport:!n.showInExport,editModeEnabled:n._editModeEnabled||ct||Wt}),(Wt&&!Ot||te)&&t(i,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,offsetY:-Dt,children:t(Y,{divProps:{style:{pointerEvents:"none"}},children:t(Q,{dangerouslySetInnerHTML:{__html:Ft},style:{pointerEvents:"none",opacity:n.a.opacity,textShadow:n.shadowEnabled?`${n.shadowOffsetX}px ${n.shadowOffsetY}px ${n.shadowBlur}px ${U(n.shadowColor,null!==(et=n.shadowOpacity)&&void 0!==et?et:1)}`:void 0}})})}),Ot&&t(i,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,offsetY:-Dt,children:t(Y,{divProps:{style:{zIndex:20}},children:t(K,{element:n,onChange:t=>{const e=W({oldText:P(n.text),newText:P(t),element:n});n.set(Object.assign({text:t},f(n,e)))},onBlur:t=>{p(n)&&(n.set(h(n)),n.toggleEditMode(!1),Bt(!0))},clickCoords:Zt.current})})})]})});
@@ -1,6 +1,19 @@
1
1
  import React from 'react';
2
2
  import { StoreType } from '../model/store.js';
3
3
  import { ImageElementType } from '../model/image-model.js';
4
+ declare function alignToPixelGrid(element: ImageElementType, pixelRatio: number, ratio: number, enabled: boolean): {
5
+ x: number;
6
+ y: number;
7
+ width: number;
8
+ height: number;
9
+ deviceWidth: number;
10
+ deviceHeight: number;
11
+ phaseX: number;
12
+ phaseY: number;
13
+ spanX: number;
14
+ spanY: number;
15
+ aligned: boolean;
16
+ };
4
17
  export declare const useSizeFixer: (src: string) => string;
5
18
  export declare function maskCompositeSize(shapeRaster: {
6
19
  width: number;
@@ -18,7 +31,7 @@ export declare const useCornerRadiusAndCrop: (element: ImageElementType, image:
18
31
  y: number;
19
32
  width: number;
20
33
  height: number;
21
- }, pixelRatio: number, cornerRadius?: number, skipDownscale?: boolean, isVector?: boolean, borderInset?: number) => HTMLCanvasElement | HTMLImageElement | undefined;
34
+ }, pixelRatio: number, cornerRadius: number | undefined, skipDownscale: boolean | undefined, isVector: boolean | undefined, borderInset: number | undefined, align: ReturnType<typeof alignToPixelGrid>) => HTMLCanvasElement | HTMLImageElement | undefined;
22
35
  type ImageOrSVGElementType = ImageElementType & {
23
36
  maskSrc?: string;
24
37
  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 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
+ import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import i from"react";import{observer as o}from"mobx-react-lite";import{isAlive as a}from"mobx-state-tree";import{Arc as n,Group as h,Image as d,Rect as s,Text as c,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 y}from"@polotno/core/errors";import{getBorderInset as v,getCropRect as M}from"@polotno/core/image";import*as b from"@polotno/core/svg";import{flags as E}from"../utils/flags.js";import{trySetCanvasSize as S}from"../utils/canvas.js";import{roundEnabledPx as X,activeAnchorOf as Y,roundedCropFraction as C,roundedGeometryPx as R,roundedPosition as I,isCornerAnchor as O,scaledBoxStyleAttrs as W,roundedBoxStyleAttrs as j}from"../utils/px.js";import{useFilteredSource as z}from"./use-filtered-source.js";import{useFadeIn as H}from"./use-fadein.js";import{isTouchDevice as k}from"../utils/screen.js";import{useDelayer as D}from"./use-delayer.js";import{useColor as _}from"./use-color.js";import{resampleCanvas as A,RESAMPLE_SKIP_THRESHOLD as L}from"./downscale.js";const T=1e4;function F(){return document.createElement("canvas")}const B=t=>"svg"===t.type||b.isSvgUrl(t.src||"");export const useSizeFixer=t=>{const[e,r]=i.useState(t);return i.useEffect(()=>{(async()=>{const i=b.isSvgUrl(t)?b.prepareSvgSource(await b.urlToString(t)):t;i!==e&&r(i)})()},[t]),e};function P(t,e){return{x:(t.x+e.x)/2,y:(t.y+e.y)/2}}const N=(t,e)=>e.width<5||e.height<5?t:e,U={keepRatio:!1,rotateEnabled:!1,boundBoxFunc:N},V={enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],rotateEnabled:!1,borderEnabled:!1,boundBoxFunc:N};export function maskCompositeSize(t,e){const r=M({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 $=(t,e,r)=>Math.max(e,Math.min(r,t)),q=(t,e,r)=>Math.max(t.a.width/e.width,t.a.height/e.height)*r;export const useCornerRadiusAndCrop=(t,e,r,o,a=0,n=!1,h=!0,d=0,s)=>{var c,g,l;const u=T,m=T,f=s.aligned?$(s.deviceWidth,1,u):Math.floor($(t.a.width*o,1,u)),w=s.aligned?$(s.deviceHeight,1,m):Math.floor($(t.a.height*o,1,m)),x=Math.min(a*o,f/2,w/2),y=Math.min(d*o,f/2,w/2),v=q(t,r,o),M=Boolean(null===(c=t.page)||void 0===c?void 0:c._exporting)&&!(null===(g=t.page)||void 0===g?void 0:g._rendering)&&E.imageDownScalingEnabled&&!n&&(v<L||s.aligned),b=0===r.x&&0===r.y&&r.width===(null==e?void 0:e.width)&&r.height===(null==e?void 0:e.height),X=i.useMemo(()=>{if(e&&e.width&&e.height){return!b||0!==x||M||y?F():void 0}},[e,x,M,b,y]);return i.useLayoutEffect(()=>{var i;if(!X||!e){return}S(X,f,w);const o=X.getContext("2d");if(o){if((null===(i=t.page)||void 0===i?void 0:i._exportingOrRendering)&&(o.imageSmoothingQuality="high"),(x||y)&&(o.beginPath(),o.roundRect(y,y,f-2*y,w-2*y,Math.max(0,x-y)),o.clip()),M){const t=X.width===f&&X.height===w&&s.aligned,i=A(e,r,X.width,X.height,{x:t?s.phaseX:0,y:t?s.phaseY:0,width:t?s.spanX:X.width,height:t?s.spanY:X.height});return o.drawImage(i,0,0),void p.Util.releaseCanvas(i)}o.drawImage(e,r.x,r.y,r.width,r.height,0,0,X.width,X.height)}},[X,t.a.width,t.a.height,r.x,r.y,r.width,r.height,a,o,n,null===(l=t.page)||void 0===l?void 0:l._exportingOrRendering,M,s.phaseX,s.phaseY,y]),i.useEffect(()=>()=>{X&&"CANVAS"===X.nodeName&&p.Util.releaseCanvas(X)},[X]),X||e};const G=F(),J=o(({element:r})=>{const o=Math.min(30,r.a.width/4,r.a.height/4),a=i.useRef(null);i.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:d}=f();return e(h,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(s,{width:r.a.width,height:r.a.height,fill:d.fill}),t(n,{ref:a,x:r.a.width/2,y:r.a.height/2,fill:d.textFill,outerRadius:Math.abs(o),innerRadius:Math.max(1,o-5),angle:270})]})}),Q=o(({element:r})=>{const{mediaErrorStyle:i}=f(),o=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(s,{width:r.a.width,height:r.a.height,fill:i.fill}),t(c,{text:"Can not load the image...",fontSize:o,width:r.a.width,height:r.a.height,align:"center",fill:i.textFill,verticalAlign:"middle",padding:5})]})});let K=u;export const setImageLoaderHook=t=>{K=t};export const useImageLoader=(t,e="",r="")=>{const o=i.useRef(void 0),a=()=>{var t;null===(t=o.current)||void 0===t||t.call(o),o.current=void 0};i.useEffect(()=>a,[]),i.useLayoutEffect(()=>{const i=e.slice(0,200),n=`image with id ${r} url: ${i}`;"loading"!==t||o.current||(o.current=w(n)),"loading"!==t&&a(),"failed"===t&&x(y("IMAGE_FAILED",n,{src:i,elementId:r,reason:"load"}))},[t])};export const ImageElement=o(({element:o,store:n})=>{var c,u,w,x,y,A;const N=f(),[$,Z]=i.useState(!1),tt=i.useRef(null),et=i.useRef(null),rt=o.isSelected,[it,ot]=(t=>{var e;const[r,o]=i.useReducer(t=>t+1,0),a=B(t),n=a?t.src+"\n"+JSON.stringify(null!==(e=t.colorsReplace)&&void 0!==e?e:null):"",h=i.useRef("loading"),d=i.useRef(""),s=i.useRef(n);return s.current!==n&&(s.current=n,h.current="loading"),i.useEffect(()=>{if(!a||!t.src){return}let e=!1;return(async()=>{const r=b.prepareSvgSource(await b.urlToString(t.src),t.colorsReplace);e||(d.current=r,h.current="loaded",o())})().catch(()=>{e||(h.current="failed",o())}),()=>{e=!0}},[n]),a?[d.current,h.current]:[t.src,"loaded"]})(o),[at,nt]=K(it,"anonymous"),ht="loaded"===ot?nt:ot;useImageLoader(ht,o.src,o.id);const dt=(null===(c=o.page)||void 0===c?void 0:c._exportingOrRendering)?1:Math.max(1,n.scale),st=n._elementsPixelRatio*dt,ct=(({image:t,status:e})=>{const r=i.useRef(void 0);return i.useEffect(()=>{r.current=t||r.current},[t]),"failed"!==e?r.current:void 0})({image:at,status:ht}),gt=((t,e,r)=>{const o=i.useMemo(()=>{var i,o;const{flipX:a,flipY:n}=t,h=B(t),d=navigator.userAgent.toLowerCase().indexOf("firefox")>-1,s=(/^((?!chrome|android).)*safari/i.test(navigator.userAgent)||d)&&h||t.maskSrc;if(!a&&!n&&!s){return e}if(!e||!e.width||!e.height){return null}const c=F();let g=1;"svg"===t.type&&(g=Math.max(t.a.width/e.width*r,t.a.height/e.height*r)),S(c,Math.max(e.width*g,1),Math.max(e.height*g,1));let l=a?-c.width:0,u=n?-c.height:0;return null===(i=c.getContext("2d"))||void 0===i||i.scale(a?-1:1,n?-1:1),null===(o=c.getContext("2d"))||void 0===o||o.drawImage(e,l,u,c.width,c.height),c},[t.maskSrc,t.flipX,t.flipY,e,t.a.width,t.a.height,r]);return i.useEffect(()=>()=>{o&&"CANVAS"===o.nodeName&&p.Util.releaseCanvas(o)},[o]),o})(o,at||ct,st),lt=((t,e)=>{const r=useSizeFixer(t.maskSrc||""),[o,a]=K(r,"anonymous");return useImageLoader(r?a:"loaded",r||"",t.id),i.useMemo(()=>{if(!o){return e}if(!e||!e.width||!e.height){return e}const{width:r,height:i}=maskCompositeSize(e,{width:t.a.width,height:t.a.height,stretchEnabled:t.stretchEnabled}),a=F();a.width=r,a.height=i;const n=a.getContext("2d");if(!n){return e}n.drawImage(e,0,0,r,i,0,0,r,i),n.globalCompositeOperation="source-in";const h=function(t,e){const r=e.width/e.height;let i,o;return r>=t.width/t.height?(i=t.width,o=t.width/r):(i=t.height*r,o=t.height),{x:(t.width-i)/2,y:(t.height-o)/2,width:i,height:o}}(o,t);return n.drawImage(o,h.x,h.y,h.width,h.height,0,0,r,i),a},[e,o,t.a.width,t.a.height,t.stretchEnabled])})(o,gt)||G,[ut=G]=z(o,lt);let{cropX:pt,cropY:mt,cropWidth:ft,cropHeight:wt}=o.a;"loaded"!==nt&&(pt=mt=0,ft=wt=1);const xt=M({stretchEnabled:o.stretchEnabled,cropX:pt,cropY:mt,cropWidth:ft,cropHeight:wt,width:o.a.width,height:o.a.height},ut),yt=xt.width,vt=xt.height,Mt=o.stretchEnabled,bt=null!==(u=o.cornerRadius)&&void 0!==u?u:0,Et=(null===(w=o.page)||void 0===w?void 0:w._exportingOrRendering)?1:Math.min(2,n.scale),St=n._elementsPixelRatio*Et,Xt=$||o._cropModeEnabled||"svg"===o.type,Yt=function(t,e,r,i){const{x:o,y:a,width:n,height:h}=t.a,d={x:o,y:a,width:n,height:h,deviceWidth:0,deviceHeight:0,phaseX:0,phaseY:0,spanX:0,spanY:0,aligned:!1};if(!i){return d}const s=o*e,c=a*e,g=Math.round(s),l=Math.round(c),u=Math.max(1,Math.round(s+n*e)-g),p=Math.max(1,Math.round(c+h*e)-l),m=t=>Math.abs(t-Math.round(t))>.001;return m(s)||m(c)||m(n*e)||m(h*e)?u>T||p>T?d:r<L||r<=1&&u*p<=4e6?{x:g/e,y:l/e,width:u/e,height:p/e,deviceWidth:u,deviceHeight:p,phaseX:s-g,phaseY:c-l,spanX:n*e,spanY:h*e,aligned:!0}:d:d}(o,St,q(o,xt,St),Boolean(null===(x=o.page)||void 0===x?void 0:x._exporting)&&!(null===(y=o.page)||void 0===y?void 0:y._rendering)&&E.imageDownScalingEnabled&&0===o.a.rotation&&"group"!==(null===(A=o.parent)||void 0===A?void 0:A.type)&&!Xt),Ct=Yt.aligned?Yt:{x:o.x,y:o.y,width:o.a.width,height:o.a.height};let Rt=((t,e,r,o)=>{const a=useSizeFixer(t.clipSrc||""),[n,h]=K(a,"anonymous"),d=t.clipSrc?h:"loaded";useImageLoader(d,t.clipSrc,t.id);const s=i.useMemo(()=>{if(e&&n){return F()}},[e,n]);return i.useLayoutEffect(()=>{var i;if(!n){return}if(!e||!e.width||!e.height){return}if(!n||!n.width||!n.height){return}if(!s){return}const o=F(),a=Math.max(t.a.width/n.width*r,t.a.height/n.height*r);o.width=Math.max(n.width*a,1),o.height=Math.max(n.height*a,1),null===(i=o.getContext("2d"))||void 0===i||i.drawImage(n,0,0,o.width,o.height),s.width=Math.max(e.width,1),s.height=Math.max(e.height,1);const h=s.getContext("2d");h&&(h.save(),h.drawImage(o,0,0,e.width,e.height),p.Util.releaseCanvas(o),h.globalCompositeOperation="source-in",h.drawImage(e,0,0,s.width,s.height),h.restore())},[s,e,n,t.a.width,t.a.height,r,...o]),t.clipSrc&&n?s:e})(o,useCornerRadiusAndCrop(o,ut,xt,St,bt,Xt,!0,v({borderSize:o.borderSize,borderColor:o.borderColor,width:o.a.width,height:o.a.height}),Yt),St,[xt,bt,St]);const It=Math.max(o.a.width/yt,o.a.height/vt);i.useEffect(()=>{var t;if(!o._cropModeEnabled){return}const e=null===(t=tt.current)||void 0===t?void 0:t.getStage();function r(t){o._cropModeEnabled&&t.target!==et.current&&o.toggleCropMode(!1)}function i(t){o._cropModeEnabled&&t.target.parentNode!==(null==e?void 0:e.content)&&o.toggleCropMode(!1)}return document.body.addEventListener("click",i),null==e||e.on("click",r),null==e||e.on("tap",r),()=>{document.body.removeEventListener("click",i),document.body.removeEventListener("touchstart",i),null==e||e.off("click",r),null==e||e.off("click",r)}},[o._cropModeEnabled]);const Ot=i.useRef(null),Wt=i.useRef(null),jt=i.useRef(null);i.useLayoutEffect(()=>{o._cropModeEnabled&&(Wt.current.nodes([Ot.current]),jt.current.nodes([et.current]))},[o._cropModeEnabled]);var zt=i.useRef(null),Ht=i.useRef(0),kt=i.useRef(!1);const Dt=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(),i=t.target.height()*t.target.scaleY(),a=Math.min(1,yt/r),n=Math.min(1,vt/i),h=1-a,d=Math.min(h,Math.max(0,Math.round(-t.target.x())/r)),s=1-n,c=Math.min(s,Math.max(0,Math.round(-t.target.y())/i));t.target.setAttrs({x:-d*ut.width,y:-c*ut.height,scaleX:1,scaleY:1}),o.set({cropX:d,cropY:c,cropWidth:a,cropHeight:n})},_t=()=>{"svg"!==o.type&&o.contentEditable&&(o.stretchEnabled||setTimeout(()=>{a(o)&&o.toggleCropMode(!0)}))},At=B(o)&&ct,Lt="loading"===ht&&!At,[Tt]=D(Lt,100,!1,!1),Ft="failed"===ht,Bt=!Tt&&!Ft,Pt=i.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Nt=i.useRef(!1),Ut=Bt?o.a.opacity:0;H(tt,Ut);const Vt=o.selectable||n.role===l.ADMIN,$t=k(),qt=_(o,o.borderColor,"stroke");return e(r,{children:[Tt&&t(J,{element:o}),Ft&&t(Q,{element:o}),t(d,{ref:tt,name:"element",id:o.id,image:Rt,x:Yt.x,y:Yt.y,width:Yt.width||1,height:Yt.height||1,rotation:o.a.rotation,opacity:Ut,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity,customCrop:xt,listening:Vt,draggable:$t?o.draggable&&rt:o.draggable,preventDefault:!$t||rt,hideInExport:!o.showInExport,onDragMove:t=>{o.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{o.set(I(t.target))},onDblClick:_t,onDblTap:_t,onTransformStart:t=>{Z(!0),Nt.current=O(Y(t.target)),Pt.current={cropX:o.cropX,cropY:o.cropY,cropWidth:o.cropWidth,cropHeight:o.cropHeight}},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),i=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const a=1-yt/ut.width,n=Math.min(a,Math.max(0,o.cropX)),h=1-vt/ut.height,d=Math.min(h,Math.max(0,o.cropY)),s=Nt.current,c=!s&&r<1&&Pt.current.cropHeight>vt/ut.height;let g=s?o.cropWidth:o.cropWidth*r;c&&(g=o.cropWidth);const l=!s&&i<1&&Pt.current.cropWidth>yt/ut.width;let u=s?o.cropHeight:o.cropHeight*i;l&&(u=o.cropHeight),Mt&&(g=o.cropWidth,u=o.cropHeight);const p=s?Math.min(r,i):1;o.set(Object.assign({cropX:n,cropY:d,x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*i,rotation:t.target.rotation(),cropWidth:Math.min(g,1-n),cropHeight:Math.min(u,1-d)},1!==p&&Object.assign(Object.assign({},W(o,p)),{borderSize:o.borderSize*p})))},onTransformEnd:t=>{const e=t.currentTarget;o.set(Object.assign(Object.assign({width:R(e.width()),height:R(e.height()),x:R(e.x()),y:R(e.y()),rotation:R(t.target.rotation()),cropX:C(o.cropX),cropY:C(o.cropY),cropWidth:C(yt/ut.width),cropHeight:C(vt/ut.height)},j(o)),{borderSize:X(o.borderSize)})),Z(!1)}}),t(s,Object.assign({x:Ct.x,y:Ct.y,width:Math.max(Ct.width-o.borderSize,0),height:Math.max(Ct.height-o.borderSize,0),opacity:Ut,offsetX:-o.borderSize/2,offsetY:-o.borderSize/2},qt,{strokeWidth:o.borderSize,listening:!1,visible:!!o.borderSize,rotation:o.rotation,cornerRadius:Math.max(0,bt-o.borderSize),hideInExport:!o.showInExport})),o._cropModeEnabled&&e(m,{selector:".page-abs-container",enabled:!0,children:[t(s,{x:-window.innerWidth/n.scale,y:-window.innerWidth/n.scale,width:window.innerWidth/n.scale*3,height:window.innerWidth/n.scale*3,fill:N.cropOverlayFill}),t(d,{listening:!1,image:Rt,x:o.x,y:o.y,width:o.a.width,height:o.a.height,rotation:o.rotation,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur}),e(h,{x:o.x,y:o.y,rotation:o.rotation,scaleX:It,scaleY:It,children:[t(d,{image:ut,ref:et,opacity:.4,draggable:!0,x:-o.cropX*ut.width,y:-o.cropY*ut.height,onDragMove:Dt,onTransform:Dt,onTouchMove:t=>{t.evt.preventDefault();const e=t.target.getStage().getPointersPositions();var r=e[0],i=e[1];const o=t.target;if(r&&!i&&!o.isDragging()&&kt.current&&(o.startDrag(),kt.current=!1),r&&i){p.hitOnDragEnabled=!0,o.isDragging()&&(kt.current=!0,o.stopDrag());const e=t.target.getAbsoluteTransform().copy();e.invert();var a={x:r.x,y:r.y},n={x:i.x,y:i.y};if(!zt.current){return void(zt.current=P(a,n))}var h=P(a,n),d=function(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}(a,n);Ht.current||(Ht.current=d);const m=o.position();var s={x:h.x-m.x,y:h.y-m.y},c=d/Ht.current;o.scaleX(c),o.scaleY(c);const f=e.point(h),w=e.point(zt.current);var g=f.x-w.x,l=f.y-w.y,u={x:Math.min(0,h.x-s.x*c+g),y:Math.min(0,h.y-s.y*c+l)};o.position(u),Ht.current=d,zt.current=h,Dt(t)}},onTouchEnd:t=>{Ht.current=0,zt.current=null,p.hitOnDragEnabled=!1}}),t(g,Object.assign({ref:jt},V,N.outerImageCropTransformerStyle)),t(s,{width:yt,height:vt,ref:Ot,listening:!1,onTransform:t=>{if(o.cropX<Math.abs(t.target.x()/ut.width)&&t.target.x()<0&&o.cropX>0){const e=(o.cropWidth+o.cropX)*ut.width;t.target.scaleX(1),t.target.width(e)}if(o.cropY<Math.abs(t.target.y()/ut.height)&&t.target.y()<0&&o.cropY>0){const e=(o.cropHeight+o.cropY)*ut.height;t.target.scaleY(1),t.target.height(e)}t.target.x()<-o.cropX*ut.width-1e-9&&(t.target.x(-o.cropX*ut.width),t.target.scaleX(1)),t.target.y()<-o.cropY*ut.height-1e-9&&(t.target.y(-o.cropY*ut.height),t.target.scaleY(1));const e=Math.min(1,Math.max(0,o.cropX+t.target.x()/ut.width)),r=Math.min(1,Math.max(0,t.target.y()/ut.height+o.cropY)),i=t.target.width()*t.target.scaleX(),a=t.target.height()*t.target.scaleY(),n=Math.min(1-e,i/ut.width),h=Math.min(1-r,a/ut.height),d=t.target.getAbsolutePosition(t.target.getParent().getParent());t.target.scale({x:1,y:1}),t.target.position({x:0,y:0}),o.set({x:d.x,y:d.y,cropX:e,cropY:r,cropWidth:n,cropHeight:h,width:Math.min(i*It,ut.width*(1-e)*It),height:Math.min(a*It,ut.height*(1-r)*It)})}}),t(g,Object.assign({ref:Wt},U,N.innerImageCropTransformerStyle,{visible:o.resizable}))]})]})]})});
@@ -1 +1 @@
1
- import{jsx as t,jsxs as e,Fragment as o}from"react/jsx-runtime";import r from"react";import{observer as a}from"mobx-react-lite";import{Group as i,Line as n,Rect as s,Circle as l}from"react-konva";import{ROLES as h}from"../model/store.js";import{useColor as d}from"./use-color.js";import{getLineHeadGeometry as c}from"@polotno/core/line-head";import{Portal as g}from"react-konva-utils";import{useAnchorSnap as m}from"./use-transformer-snap.js";import{useWorkspaceStyle as f}from"./workspace-style.js";import{isTouchDevice as p}from"../utils/screen.js";export function getMiddlePoints(t){const{x:e,y:o,width:r,height:a,rotation:i}=t.a,n=i*Math.PI/180,s={x:e+a/2*Math.cos(n+Math.PI/2),y:o+a/2*Math.sin(n+Math.PI/2)};return{middleLeft:s,middleRight:{x:s.x+r*Math.cos(n),y:s.y+r*Math.sin(n)}}}export function getLinePositionFromMiddlePoints(t,e,o){const r=Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),a=Math.atan2(e.y-t.y,e.x-t.x),i=180*a/Math.PI;return{x:t.x+Math.cos(a-Math.PI/2)*o/2,y:t.y+Math.sin(a-Math.PI/2)*o/2,width:r,height:o,rotation:i}}const x=(t,e,o)=>{const r=o.anchorSize;return{offsetX:r/2/t,offsetY:r/2/t,width:r/t,height:r/t,cornerRadius:o.anchorCornerRadius/t,fill:o.anchorFill,stroke:o.anchorStroke,strokeWidth:o.anchorStrokeWidth,strokeScaleEnabled:!1,draggable:!0,dragDistance:0,onMouseEnter:t=>{t.target.getStage().container().style.cursor="crosshair"},onMouseLeave:t=>{t.target.getStage().container().style.cursor=""},onDragStart:t=>{e.history.startTransaction()},onDragEnd:t=>{e.history.endTransaction()}}},y=a(({element:e,type:o})=>{const r=d(e,e.a.color,"fill"),a=d(e,e.a.color,"stroke"),i=c(o,e.height);if(!i){return null}const s=Object.assign(Object.assign(Object.assign({strokeWidth:e.height,lineCap:"round",lineJoin:"round"},a),r),{opacity:e.a.opacity,hideInExport:!e.showInExport});return"circle"===i.kind?t(l,Object.assign({x:i.cx,y:i.cy,radius:i.r},s)):t(n,Object.assign({points:i.points.flat(),closed:i.filled},s))});export const LineElement=a(({element:a,store:l})=>{const{transformerStyle:c}=f(),u=r.useRef(null),w=r.useRef(null),M=r.useRef(null),b=a.selectable||l.role===h.ADMIN,v=p(),k=l.selectedElements.indexOf(a)>=0&&1===l.selectedElements.length,j=a.isSelected,{middleLeft:E,middleRight:O}=getMiddlePoints(a);m(w,[u],[k]),m(M,[u],[k]);const P=d(a,a.a.color,"stroke");return e(o,{children:[t(n,Object.assign({ref:u,name:"element",id:a.id,x:a.a.x,y:a.a.y,points:[0,0,a.a.width,0],offsetY:-a.a.height/2,strokeWidth:a.a.height,hitStrokeWidth:Math.max(a.a.height,20),dash:a.dash.map(t=>t*a.a.height)},P,{rotation:a.a.rotation,opacity:a.animated("opacity"),shadowEnabled:a.shadowEnabled,shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:a.shadowColor,shadowOpacity:a.shadowOpacity,listening:b,draggable:v?a.draggable&&j:a.draggable,preventDefault:!v||j,hideInExport:!a.showInExport,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set({x:t.target.x(),y:t.target.y()})},onTransform:t=>{const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),a.set({height:a.height*e,width:a.width*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation()})}})),t(i,{x:E.x,y:E.y,rotation:a.rotation,hideInExport:!a.showInExport,onClick:t=>{var e;return null===(e=u.current)||void 0===e?void 0:e.fire("click",t)},onTap:t=>{var e;return null===(e=u.current)||void 0===e?void 0:e.fire("tap",t)},children:t(y,{element:a,type:a.startHead})}),t(i,{x:O.x,y:O.y,rotation:a.rotation+180,onClick:t=>{var e;return null===(e=u.current)||void 0===e?void 0:e.fire("click",t)},onTap:t=>{var e;return null===(e=u.current)||void 0===e?void 0:e.fire("tap",t)},children:t(y,{element:a,type:a.endHead})}),k&&a.resizable&&t(g,{selector:".page-abs-container",enabled:!0,children:e(i,{visible:k,children:[t(s,Object.assign({x:E.x,y:E.y,ref:w,name:"line-anchor"},x(l.scale,l,c),{onDragMove:t=>{const e=getLinePositionFromMiddlePoints(t.target.position(),O,a.height);a.set({x:e.x,y:e.y,width:e.width,rotation:e.rotation})}})),t(s,Object.assign({x:O.x,y:O.y,ref:M,name:"line-anchor"},x(l.scale,l,c),{onDragMove:t=>{const e=getLinePositionFromMiddlePoints(E,t.target.position(),a.height);a.set({x:e.x,y:e.y,width:e.width,rotation:e.rotation})}}))]})})]})});
1
+ import{jsx as t,jsxs as e,Fragment as o}from"react/jsx-runtime";import r from"react";import{observer as a}from"mobx-react-lite";import{Group as n,Line as i,Rect as s,Circle as l}from"react-konva";import{ROLES as h}from"../model/store.js";import{useColor as d}from"./use-color.js";import{getLineHeadGeometry as c}from"@polotno/core/line-head";import{Portal as g}from"react-konva-utils";import{useAnchorSnap as m}from"./use-transformer-snap.js";import{useWorkspaceStyle as f}from"./workspace-style.js";import{isTouchDevice as p}from"../utils/screen.js";import{roundedGeometryAttrs as x,roundedPosition as u}from"../utils/px.js";export function getMiddlePoints(t){const{x:e,y:o,width:r,height:a,rotation:n}=t.a,i=n*Math.PI/180,s={x:e+a/2*Math.cos(i+Math.PI/2),y:o+a/2*Math.sin(i+Math.PI/2)};return{middleLeft:s,middleRight:{x:s.x+r*Math.cos(i),y:s.y+r*Math.sin(i)}}}export function getLinePositionFromMiddlePoints(t,e,o){const r=Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),a=Math.atan2(e.y-t.y,e.x-t.x),n=180*a/Math.PI;return{x:t.x+Math.cos(a-Math.PI/2)*o/2,y:t.y+Math.sin(a-Math.PI/2)*o/2,width:r,height:o,rotation:n}}const y=(t,e,o)=>{const r=o.anchorSize;return{offsetX:r/2/t,offsetY:r/2/t,width:r/t,height:r/t,cornerRadius:o.anchorCornerRadius/t,fill:o.anchorFill,stroke:o.anchorStroke,strokeWidth:o.anchorStrokeWidth,strokeScaleEnabled:!1,draggable:!0,dragDistance:0,onMouseEnter:t=>{t.target.getStage().container().style.cursor="crosshair"},onMouseLeave:t=>{t.target.getStage().container().style.cursor=""},onDragStart:t=>{e.history.startTransaction()},onDragEnd:t=>{e.history.endTransaction()}}},w=a(({element:e,type:o})=>{const r=d(e,e.a.color,"fill"),a=d(e,e.a.color,"stroke"),n=c(o,e.height);if(!n){return null}const s=Object.assign(Object.assign(Object.assign({strokeWidth:e.height,lineCap:"round",lineJoin:"round"},a),r),{opacity:e.a.opacity,hideInExport:!e.showInExport});return"circle"===n.kind?t(l,Object.assign({x:n.cx,y:n.cy,radius:n.r},s)):t(i,Object.assign({points:n.points.flat(),closed:n.filled},s))});export const LineElement=a(({element:a,store:l})=>{const{transformerStyle:c}=f(),M=r.useRef(null),b=r.useRef(null),v=r.useRef(null),k=a.selectable||l.role===h.ADMIN,j=p(),E=l.selectedElements.indexOf(a)>=0&&1===l.selectedElements.length,O=a.isSelected,{middleLeft:P,middleRight:I}=getMiddlePoints(a);m(b,[M],[E]),m(v,[M],[E]);const S=d(a,a.a.color,"stroke");return e(o,{children:[t(i,Object.assign({ref:M,name:"element",id:a.id,x:a.a.x,y:a.a.y,points:[0,0,a.a.width,0],offsetY:-a.a.height/2,strokeWidth:a.a.height,hitStrokeWidth:Math.max(a.a.height,20),dash:a.dash.map(t=>t*a.a.height)},S,{rotation:a.a.rotation,opacity:a.animated("opacity"),shadowEnabled:a.shadowEnabled,shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:a.shadowColor,shadowOpacity:a.shadowOpacity,listening:k,draggable:j?a.draggable&&O:a.draggable,preventDefault:!j||O,hideInExport:!a.showInExport,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(u(t.target))},onTransform:t=>{const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),a.set({height:a.height*e,width:a.width*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation()})},onTransformEnd:()=>{a.set(x(a))}})),t(n,{x:P.x,y:P.y,rotation:a.rotation,hideInExport:!a.showInExport,onClick:t=>{var e;return null===(e=M.current)||void 0===e?void 0:e.fire("click",t)},onTap:t=>{var e;return null===(e=M.current)||void 0===e?void 0:e.fire("tap",t)},children:t(w,{element:a,type:a.startHead})}),t(n,{x:I.x,y:I.y,rotation:a.rotation+180,onClick:t=>{var e;return null===(e=M.current)||void 0===e?void 0:e.fire("click",t)},onTap:t=>{var e;return null===(e=M.current)||void 0===e?void 0:e.fire("tap",t)},children:t(w,{element:a,type:a.endHead})}),E&&a.resizable&&t(g,{selector:".page-abs-container",enabled:!0,children:e(n,{visible:E,children:[t(s,Object.assign({x:P.x,y:P.y,ref:b,name:"line-anchor"},y(l.scale,l,c),{onDragMove:t=>{const e=getLinePositionFromMiddlePoints(t.target.position(),I,a.height);a.set({x:e.x,y:e.y,width:e.width,rotation:e.rotation})}})),t(s,Object.assign({x:I.x,y:I.y,ref:v,name:"line-anchor"},y(l.scale,l,c),{onDragMove:t=>{const e=getLinePositionFromMiddlePoints(P,t.target.position(),a.height);a.set({x:e.x,y:e.y,width:e.width,rotation:e.rotation})}}))]})})]})});
@@ -1 +1 @@
1
- import{jsx as e,jsxs as o}from"react/jsx-runtime";import{observer as i}from"mobx-react-lite";import t from"konva";import{PolotnoIcon as l}from"../icons/registry.js";import{ROLES as a}from"../model/store.js";import{t as n}from"../utils/l10n.js";import{TooltipIconButton as r}from"../primitives/tooltip-icon-button.js";import{useWorkspaceStyle as d}from"./workspace-style.js";export const PageControls=i(({store:i,page:c,xPadding:s,yPadding:p,layout:g="vertical"})=>{const v=i.pages.length>1,m=i.pages.indexOf(c),h="horizontal"===g,u=h?"ui.chevronLeft":"ui.chevronUp",b=h?"ui.chevronRight":"ui.chevronDown",k=n(h?"workspace.moveLeft":"workspace.moveUp"),f=n(h?"workspace.moveRight":"workspace.moveDown"),{workspaceBackgroundColor:P}=d(),x=(e=>{const o=t.Util.colorToRGBA(e);return(o?.299*o.r+.587*o.g+.114*o.b:232)>150?"rgba(0, 0, 0, 0.6)":"rgba(255, 255, 255, 0.85)"})(P);return"selection"!==i.tool||i.role===a.VIEWER?null:o("div",{className:"polotno-page-controls",style:{position:"absolute",top:p-40+"px",right:s+"px",color:x},children:[v&&e(r,{label:k,disabled:0===m,onClick:()=>{c.setZIndex(m-1)},children:e(l,{name:u})}),v&&e(r,{label:f,disabled:m===i.pages.length-1,onClick:()=>{const e=i.pages.indexOf(c);c.setZIndex(e+1)},children:e(l,{name:b})}),e(r,{label:n("workspace.duplicatePage"),onClick:()=>{c.clone()},children:e(l,{name:"action.duplicate"})}),v&&e(r,{label:n("workspace.removePage"),onClick:()=>{i.deletePages([c.id])},children:e(l,{name:"action.remove"})}),e(r,{label:n("workspace.addPage"),onClick:()=>{var e,o,t,l,a,n,r;const d=i.addPage({bleed:(null===(e=i.activePage)||void 0===e?void 0:e.bleed)||0,bleedTop:null===(o=i.activePage)||void 0===o?void 0:o.bleedTop,bleedRight:null===(t=i.activePage)||void 0===t?void 0:t.bleedRight,bleedBottom:null===(l=i.activePage)||void 0===l?void 0:l.bleedBottom,bleedLeft:null===(a=i.activePage)||void 0===a?void 0:a.bleedLeft,width:(null===(n=i.activePage)||void 0===n?void 0:n.width)||"auto",height:(null===(r=i.activePage)||void 0===r?void 0:r.height)||"auto"}),s=i.pages.indexOf(c);d.setZIndex(s+1)},children:e(l,{name:"action.add"})})]})});
1
+ import{jsx as e,jsxs as o}from"react/jsx-runtime";import{observer as i}from"mobx-react-lite";import t from"konva";import{PolotnoIcon as l}from"../icons/registry.js";import{ROLES as a}from"../model/store.js";import{t as d}from"../utils/l10n.js";import{TooltipIconButton as n}from"../primitives/tooltip-icon-button.js";import{useWorkspaceStyle as r}from"./workspace-style.js";export const PageControls=i(({store:i,page:s,xPadding:c,yPadding:p,layout:g="vertical"})=>{const v=i.pages.length>1,m=i.pages.indexOf(s),h="horizontal"===g,u=h?"ui.chevronLeft":"ui.chevronUp",b=h?"ui.chevronRight":"ui.chevronDown",k=d(h?"workspace.moveLeft":"workspace.moveUp"),f=d(h?"workspace.moveRight":"workspace.moveDown"),{workspaceBackgroundColor:P}=r(),x=(e=>{const o=t.Util.colorToRGBA(e);return(o?.299*o.r+.587*o.g+.114*o.b:232)>150?"rgba(0, 0, 0, 0.6)":"rgba(255, 255, 255, 0.85)"})(P);return"selection"!==i.tool||i.role===a.VIEWER?null:o("div",{className:"polotno-page-controls",style:{position:"absolute",top:p-40+"px",right:c+"px",color:x},children:[v&&e(n,{side:"top",label:k,disabled:0===m,onClick:()=>{s.setZIndex(m-1)},children:e(l,{name:u})}),v&&e(n,{side:"top",label:f,disabled:m===i.pages.length-1,onClick:()=>{const e=i.pages.indexOf(s);s.setZIndex(e+1)},children:e(l,{name:b})}),e(n,{side:"top",label:d("workspace.duplicatePage"),onClick:()=>{s.clone()},children:e(l,{name:"action.duplicate"})}),v&&e(n,{side:"top",label:d("workspace.removePage"),onClick:()=>{i.deletePages([s.id])},children:e(l,{name:"action.remove"})}),e(n,{side:"top",label:d("workspace.addPage"),onClick:()=>{var e,o,t,l,a,d,n;const r=i.addPage({bleed:(null===(e=i.activePage)||void 0===e?void 0:e.bleed)||0,bleedTop:null===(o=i.activePage)||void 0===o?void 0:o.bleedTop,bleedRight:null===(t=i.activePage)||void 0===t?void 0:t.bleedRight,bleedBottom:null===(l=i.activePage)||void 0===l?void 0:l.bleedBottom,bleedLeft:null===(a=i.activePage)||void 0===a?void 0:a.bleedLeft,width:(null===(d=i.activePage)||void 0===d?void 0:d.width)||"auto",height:(null===(n=i.activePage)||void 0===n?void 0:n.height)||"auto"}),c=i.pages.indexOf(s);r.setZIndex(c+1)},children:e(l,{name:"action.add"})})]})});
@@ -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 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
+ 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 l,Rect as s,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,roundedGeometryPx as v,roundedPosition as E,textMinResizeWidth as O}from"../utils/px.js";import{flags as S}from"../utils/flags.js";import{applyFilter as M}from"./apply-filters.js";import{getLineHeight as z,usePrevious as j}from"./text-element.js";import{useFontSpecsLoader as k}from"./text-element/font-specs-loader.js";import{getHtml as R,getCurvePath as Y,getCurveTextHeight as X,letterSpacingOverhang as F}from"@polotno/core/text-html";import{useColor as I}from"./use-color.js";import{isGradient as P}from"@polotno/core/gradient";import{detectSize as C}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as T,trimTrailingLetterSpace as B}from"@polotno/core/background-shape";import{useFadeIn as W}from"./use-fadein.js";import{incrementLoader as D}from"../utils/loader.js";import{removeTags as N}from"@polotno/core/text";import{isTouchDevice as $}from"../utils/screen.js";import{getLimitedFontSize as J}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as _,extractFontSpecsFromHtml as A,scaleRichTextFontSizesInHtml as L}from"../utils/rich-text-html.js";import{RichEditor as V}from"./rich-editor.js";import{findFitFontSize as H}from"./text-element/fit-font-size.js";import{visiblePlaceholder as q}from"./text-element/placeholder.js";import{wrapHtmlForRenderTag as G}from"@polotno/core/reset-style";import{xmlEscape as K}from"@polotno/core/xml";import{hasMeasuredHeight as Q,resolveTextBoxHeight as U}from"@polotno/core/text-types";export{G as wrapHtmlForRenderTag};function Z(t,e){return t!==e&&(!S.textVerticalResizeEnabled||t<e)}export const RenderTagElement=i(({element:i,store:tt})=>{var et;const rt=n.useRef(null),ot=n.useRef(null),[nt,it]=n.useState(!1),at=n.useRef(i.height),lt=i.isSelected,st=j(lt),{textVerticalResizeEnabled:ct}=S,ht=S.autoDeleteEmptyText,dt=j(i.fontFamily),ut=n.useMemo(()=>_(i.text,{fontSize:i.fontSize,fontFamily:i.fontFamily}),[i.text,i.fontSize,i.fontFamily]),ft=n.useMemo(()=>A(ut,{fontFamily:i.fontFamily,fontWeight:i.fontWeight,fontStyle:i.fontStyle}),[ut,i.fontFamily,i.fontWeight,i.fontStyle]),[gt]=k(tt,ft,i.text||i.placeholder),mt=i._editModeEnabled;W(rt);const xt=gt?i.fontFamily:dt&&dt!==i.fontFamily?dt:"Arial",pt=I(i).fill,yt=P(i.fill),bt=P(i.stroke)&&!!i.strokeWidth,wt=!!i.strokeWidth&&(bt||yt),vt="ellipsis"===S.textOverflow&&!i.curveEnabled&&Q(i)&&!wt,Et=q(i.placeholder),Ot=!!Et&&!N(i.text),St=Ot?K(Et):ut,Mt=Ot?.6:i.a.opacity,zt=R(i,{fontFamily:xt,color:pt,forEditor:!vt,textOverflow:S.textOverflow,text:St,textIsNormalized:!0}),jt=i.width||1,kt=F(i.letterSpacing,i.fontSize),Rt=i.curveEnabled,Yt=function(t,e,r,o){return n.useMemo(()=>{if(o){return null}try{return f({html:G(t),width:Math.max(1,e),accuracy:"performance"})}catch(r){return console.error("render-tag layout failed:",r),null}},[t,e,r,o])}(zt,jt,gt,Rt),Xt=function(t,e,r){return n.useMemo(()=>{if(!e.curveEnabled){return null}const r=e.width||1,o=X(e),n=Y(r,o,e.curvePower,e.fontSize);try{return m({html:G(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])}(zt,i,gt),Ft=z({fontLoaded:gt,fontFamily:i.fontFamily,fontSize:i.fontSize,lineHeight:i.lineHeight}),It=Rt?0:Math.max(null!==(et=null==Yt?void 0:Yt.height)&&void 0!==et?et:0,i.fontSize*Ft),Pt=Xt?Xt.bounds:null,Ct=n.useMemo(()=>Yt?Yt.lines.map(t=>B({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height},kt)):[],[Yt,kt]),Tt=n.useRef([]),Bt=n.useRef(null),Wt=n.useRef(!0),[,Dt]=n.useReducer(t=>t+1,0);n.useEffect(()=>{const t=Tt.current;t.length>0&&(Tt.current=[],t.forEach(t=>t()))});const Nt=n.useCallback((t,e,r,o)=>{const n=D(`text ${i.id}`),a=()=>{tt.history.ignore(t,e,r).then(()=>{Wt.current?(Tt.current.push(n),Dt()):n()})};o?queueMicrotask(a):a()},[i.id,tt]),$t=n.useCallback(t=>{const e=null!==Bt.current;Bt.current=t,e||Nt(()=>{const t=Bt.current;Bt.current=null,d(i)&&Z(i.height,t)&&i.set({height:t})},!1,!0)},[i,Nt]);n.useEffect(()=>{if(!gt){return}if(!Yt){return}if(!Q(i)){return void Nt(()=>{d(i)&&(Q(i)||i.set({height:It}))},void 0,void 0,!0)}if(tt.isPlaying){return}const{textOverflow:t}=S,e=Z(i.height,It),r=()=>$t(It);if("change-font-size"!==t||nt||mt||i.curveEnabled){"resize"===t&&e&&!nt&&r()}else{const t=H(Ot?{toJSON:()=>Object.assign(Object.assign({},i.toJSON()),{text:St})}:i,{fontFamily:xt,color:pt,forEditor:!0},t=>{try{return f({html:G(t),width:jt,accuracy:"performance"}).height}catch(et){return C(t).height}});t.fontSize!==i.fontSize?Nt(()=>{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(()=>()=>{Wt.current=!1;const t=Tt.current;t.length>0&&(Tt.current=[],t.forEach(t=>t()))},[]),n.useLayoutEffect(()=>{if(!gt||!i.curveEnabled||nt){return}if(!Pt||!Pt.width){return}const t=t=>t&&ct?Math.max(t,Pt.height):Pt.height;Math.abs(Pt.width-i.width)<1&&Math.abs(t(i.height)-i.height)<1||Nt(()=>{if(!d(i)){return}const e=Pt.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),l=-e/2*Math.sin(n);i.set({width:Pt.width,height:r,x:i.a.x+a,y:i.a.y+l})},!1,!0,!0)},[gt,i.curveEnabled,Pt,nt,ct,Nt]),n.useEffect(()=>{st&&!lt&&ht&&""===N(i.text)&&i.removable&&!q(i.placeholder)&&tt.deleteElements([i.id])},[ht,i.placeholder,i.removable,i.text,lt,st]),n.useEffect(()=>h(i,()=>{const t=rt.current;t&&M(t,i)}),[Yt,Xt,i.a.width,i.a.height,i.shadowColor,i.shadowOffsetX,i.shadowOffsetY,i.shadowOpacity]);const Jt=U(i,It),_t=Rt||!gt||nt||tt.isPlaying||"resize"!==S.textOverflow?i.a.height:ct?Math.max(i.a.height,It):It;let At=0;Rt||"middle"!==i.verticalAlign?Rt||"bottom"!==i.verticalAlign||(At=Jt-It):At=(Jt-It)/2;const Lt=i.backgroundPadding*(i.fontSize*Ft*.5),Vt=i.backgroundCornerRadius*(i.fontSize*Ft*.5),Ht=n.useMemo(()=>!i.backgroundEnabled||i.curveEnabled||i.legacyBackground||0===Ct.length?"":T({rects:Ct,padding:Lt,cornerRadius:Vt}),[i.backgroundEnabled,i.curveEnabled,i.legacyBackground,Ct,Lt,Vt]),qt=$(),Gt=n.useRef(null),Kt=n.useRef(i.width),Qt=n.useRef(null),Ut=n.useRef(null),Zt=n.useRef(""),te=n.useRef(0),ee=n.useRef(0),re=i.width>0?i.a.width/i.width:1;return o(r,{children:[i.curveEnabled||i.legacyBackground?e(s,{x:i.a.x,y:i.a.y,offsetX:Lt/re,offsetY:Lt/re,rotation:i.a.rotation,scaleX:re,scaleY:re,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled,opacity:i.backgroundOpacity*Mt,fill:i.backgroundColor,width:i.width+2*Lt,height:Jt+2*Lt,cornerRadius:Vt}):e(l,{name:"rendertag-background",x:i.a.x,y:i.a.y,offsetY:-At/re,rotation:i.a.rotation,scaleX:re,scaleY:re,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled&&Ht.length>0,opacity:i.backgroundOpacity*Mt,fill:i.backgroundColor,data:Ht}),e(c,{ref:rt,name:"rendertag-text",x:i.a.x,y:i.a.y,rotation:i.a.rotation,scaleX:re,scaleY:re,offsetY:re>0?-At/re:0,width:i.width,height:Math.max(i.height,It),listening:!1,visible:!mt&&!!(Rt?Xt:Yt),editModeEnabled:mt,opacity:Mt,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(Rt){if(!Xt||!Pt){return}const t=(i.width-Pt.width)/2-Pt.x;r.save(),r.translate(t,-Pt.y),x({layout:Xt,ctx:r}),r.restore()}else{if(!Yt){return}g({layout:Yt,width:jt,ctx:r,pixelRatio:1})}}catch(o){console.error("render-tag draw failed:",o)}}}}),e(s,{ref:ot,name:"element",x:i.a.x,y:i.a.y,listening:i.selectable,rotation:i.a.rotation,width:i.a.width,height:_t,visible:!mt,draggable:qt?i.draggable&&lt:i.draggable,preventDefault:!qt||lt,opacity:0,hideInExport:!0,id:i.id,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set(E(t.target))},onDblClick:t=>{i.contentEditable&&(Ut.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];Ut.current=r?{x:r.clientX,y:r.clientY}:null,i.toggleEditMode(!0)}},onTransformStart:t=>{it(!0),at.current=i.height,Kt.current=i.width,Qt.current=null,Gt.current=t.target.position(),Zt.current=p(t.target),te.current=i.a.rotation,ee.current=i.height},onTransform:e=>{const r=e.target,o=Zt.current,n="middle-left"===o||"middle-right"===o,a="top-center"===o||"bottom-center"===o,l=r.scaleX();if(n){const e=r.scaleX(),o=r.width()*e;let n=Math.max(o,O(Kt.current,i.fontSize));const a=i.toJSON();let l=0;if(i.curveEnabled){l=X(Object.assign(Object.assign({},a),{width:n}))}else{const{x:e,y:r,width:o,height:i}=a,s=t(a,["x","y","width","height"]),c=`${JSON.stringify(s)}|${xt}|${gt}|${pt}`;if(Qt.current&&Qt.current.signature===c&&n<=Qt.current.width){n=Qt.current.width,l=Qt.current.height}else{const t=t=>{const e=R(Object.assign(Object.assign({},a),{width:t}),{fontFamily:xt,color:pt,forEditor:!0,text:St,textIsNormalized:!0});try{return f({html:G(e),width:t,accuracy:"performance"})}catch(et){return{height:C(e).height,lines:[]}}};let e=t(n);const r=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width-kt));r>n+.5&&r<=Kt.current+.5&&(n=Math.min(Math.ceil(r),Kt.current),e=t(n),Qt.current={signature:c,width:n,height:e.height}),l=e.height}}n>o&&Gt.current&&r.position(Gt.current),r.width(n),r.scaleX(1),r.scaleY(1);let s=S.textVerticalResizeEnabled?Math.max(l,at.current):l;"ellipsis"===S.textOverflow&&(s=at.current),r.height(s),i.set({width:r.width(),height:s,x:r.x(),y:r.y()})}else if(a){const t="resize"===S.textOverflow?Rt?(null==Pt?void 0:Pt.height)||i.fontSize:It:Ft*i.fontSize,e=r.height()*r.scaleY();if(e<t){if(r.height(t),Gt.current){if("top-center"===Zt.current){const e=te.current*Math.PI/180,o=ee.current-t;r.x(Gt.current.x-o*Math.sin(e)),r.y(Gt.current.y+o*Math.cos(e))}else{r.position(Gt.current)}}}else{r.height(e)}r.scaleY(1),r.scaleX(1),ee.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*l,letterSpacing:i.letterSpacing,width:r.width()*l,x:r.x(),y:r.y(),rotation:r.rotation(),height:r.height()*l,shadowBlur:i.shadowBlur*l,shadowOffsetX:i.shadowOffsetX*l,shadowOffsetY:i.shadowOffsetY*l,strokeWidth:i.strokeWidth*l,text:L(i.text,l)})}Gt.current=e.target.position()},onTransformEnd:t=>{it(!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:v(t.target.width()*e),x:v(t.target.x()),y:v(t.target.y()),rotation:v(t.target.rotation()),text:L(i.text,e)}))}}),mt&&e(a,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,offsetY:-At,children:e(u,{divProps:{style:{zIndex:20}},children:e(V,{element:i,onChange:t=>{const e=J({oldText:N(i.text),newText:N(t),element:i});i.set(Object.assign({text:t},y(i,e)))},onBlur:()=>{d(i)&&(i.set(w(i)),i.toggleEditMode(!1))},clickCoords:Ut.current})})})]})});
@@ -1,4 +1,4 @@
1
- var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(o[n]=e[n])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(e);r<n.length;r++){t.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(e,n[r])&&(o[n[r]]=e[n[r]])}}return o};import{jsx as t}from"react/jsx-runtime";import o from"react";import n from"quill";import*as r from"mobx";import{nodeReaction as i}from"../utils/mobx.js";import{isAlive as l}from"mobx-state-tree";import{getQuillEditorHtml as a,getDir as s}from"@polotno/core/text-html";import{quillHtmlToModelText as d,isQuillFormatMixed as c,isBoldWeight as u}from"../utils/rich-text-html.js";import{resetStyleContent as f,wrapHtmlForRenderTag as p}from"@polotno/core/reset-style";import{layout as m,lineBaselineOffset as h}from"render-tag";import{removeTags as g}from"@polotno/core/text";import{cssFontFamily as b}from"@polotno/core/fonts";import{isGradient as v,firstStopColor as x}from"@polotno/core/gradient";import{getOptimalCaretColor as y}from"./text-element/caret-color.js";import{visiblePlaceholder as w}from"./text-element/placeholder.js";import{registerQuillUnrestrictedFontFormats as S}from"./quill-font-formats.js";import{ALL_QUILL_MIXED_KEYS as k,setActiveTextEditor as q,updateActiveTextFormat as T,clearActiveTextEditor as E,getActiveTextEditor as C}from"../utils/text-format-state.js";import L from"../utils/styled.js";import{applyOpacityToColor as F}from"@polotno/core/svg";export{F as applyOpacityToColor};export const quillRef=r.observable({enabled:!1,currentFormat:{},pendingToolbarInteraction:!1,editor:r.observable.object({instance:null},{},{deep:!1})});const R=k;export function syncQuillFormatState(e,t){const o=t?e.getFormat(t):{},n=function(e,t){const o={};if(!t||t.length<=0){return o}const n=e.getContents(t.index,t.length);for(const r of R){c(n,r)&&(o[r]=!0)}return o}(e,t);r.runInAction(()=>{quillRef.currentFormat=o}),T(o,n,t)}const $="polotno-saved-selection";function O(){const e=window.getSelection();if(!e||0===e.rangeCount||e.isCollapsed){return}if("undefined"==typeof Highlight||!CSS.highlights){return}const t=e.getRangeAt(0).cloneRange();CSS.highlights.set($,new Highlight(t)),j()}function M(){"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.delete($)}function j(){setTimeout(()=>{"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.has($)&&(document.querySelector(".polotno-portal")||D(document.activeElement)?j():M())},300)}if("undefined"!=typeof document&&"undefined"!=typeof CSS&&CSS.highlights&&!document.querySelector("[data-polotno-highlight]")){const H=document.createElement("style");H.setAttribute("data-polotno-highlight",""),H.textContent=`::highlight(${$}) { background-color: Highlight; }`,document.head.appendChild(H)}let N=null;function z(e,t){"text"===e.type&&t.push(e);for(const o of e.children||[]){z(o,t)}}export function computeMarkerVars(e){var t,o,n;const r=e.fontSize;if(!r||r<=0){return null}const i=b(e.fontFamily),l=u(e.fontWeight)?700:400,a="italic"===e.fontStyle?"italic":"normal",s=null!=N?N:N=document.createElement("canvas").getContext("2d");s.fontKerning="normal",s.font=`${r}px ${i}`;const d=s.measureText("M"),c=(null!==(t=d.fontBoundingBoxAscent)&&void 0!==t?t:0)/r,f=(null!==(o=d.fontBoundingBoxDescent)&&void 0!==o?o:0)/r,g=`font-family:${i};font-size:${r}px;font-weight:${l};font-style:${a};line-height:${e.lineHeight};letter-spacing:${e.letterSpacing}em`,v=p(`<div style="${g}"><ul><li>x</li></ul><ol><li>x</li></ol></div>`);let x;try{x=m({html:v,width:Math.max(12*r,600)})}catch(T){return null}const y=[];z(x.layoutRoot,y);const w=y.find(e=>"•"===e.text),S=y.find(e=>/^\d/.test(e.text));if(!w){return null}const k=w.style.fontSize,q=2.1*r;return{bulletLeft:w.x,bulletTop:w.y-h(k,k*c,k*f),bulletFontSize:k,numberGap:S?Math.max(0,q-S.x-(null!==(n=S.width)&&void 0!==n?n:0)):0}}export const RichTextContainer=L("div",o.forwardRef)`
1
+ var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(o[n]=e[n])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(e);r<n.length;r++){t.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(e,n[r])&&(o[n[r]]=e[n[r]])}}return o};import{jsx as t}from"react/jsx-runtime";import o from"react";import n from"quill";import*as r from"mobx";import{nodeReaction as i}from"../utils/mobx.js";import{isAlive as l}from"mobx-state-tree";import{getQuillEditorHtml as a,getDir as s,textLayoutWidth as d}from"@polotno/core/text-html";import{quillHtmlToModelText as c,isQuillFormatMixed as u,isBoldWeight as f}from"../utils/rich-text-html.js";import{resetStyleContent as p,wrapHtmlForRenderTag as m}from"@polotno/core/reset-style";import{layout as h,lineBaselineOffset as g}from"render-tag";import{removeTags as b}from"@polotno/core/text";import{cssFontFamily as v}from"@polotno/core/fonts";import{isGradient as x,firstStopColor as y}from"@polotno/core/gradient";import{getOptimalCaretColor as w}from"./text-element/caret-color.js";import{visiblePlaceholder as S}from"./text-element/placeholder.js";import{registerQuillUnrestrictedFontFormats as k}from"./quill-font-formats.js";import{ALL_QUILL_MIXED_KEYS as q,setActiveTextEditor as T,updateActiveTextFormat as E,clearActiveTextEditor as C,getActiveTextEditor as L}from"../utils/text-format-state.js";import F from"../utils/styled.js";import{applyOpacityToColor as R}from"@polotno/core/svg";export{R as applyOpacityToColor};export const quillRef=r.observable({enabled:!1,currentFormat:{},pendingToolbarInteraction:!1,editor:r.observable.object({instance:null},{},{deep:!1})});const $=q;export function syncQuillFormatState(e,t){const o=t?e.getFormat(t):{},n=function(e,t){const o={};if(!t||t.length<=0){return o}const n=e.getContents(t.index,t.length);for(const r of $){u(n,r)&&(o[r]=!0)}return o}(e,t);r.runInAction(()=>{quillRef.currentFormat=o}),E(o,n,t)}const O="polotno-saved-selection";function M(){const e=window.getSelection();if(!e||0===e.rangeCount||e.isCollapsed){return}if("undefined"==typeof Highlight||!CSS.highlights){return}const t=e.getRangeAt(0).cloneRange();CSS.highlights.set(O,new Highlight(t)),N()}function j(){"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.delete(O)}function N(){setTimeout(()=>{"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.has(O)&&(document.querySelector(".polotno-portal")||H(document.activeElement)?N():j())},300)}if("undefined"!=typeof document&&"undefined"!=typeof CSS&&CSS.highlights&&!document.querySelector("[data-polotno-highlight]")){const _=document.createElement("style");_.setAttribute("data-polotno-highlight",""),_.textContent=`::highlight(${O}) { background-color: Highlight; }`,document.head.appendChild(_)}let z=null;function P(e,t){"text"===e.type&&t.push(e);for(const o of e.children||[]){P(o,t)}}export function computeMarkerVars(e){var t,o,n;const r=e.fontSize;if(!r||r<=0){return null}const i=v(e.fontFamily),l=f(e.fontWeight)?700:400,a="italic"===e.fontStyle?"italic":"normal",s=null!=z?z:z=document.createElement("canvas").getContext("2d");s.fontKerning="normal",s.font=`${r}px ${i}`;const d=s.measureText("M"),c=(null!==(t=d.fontBoundingBoxAscent)&&void 0!==t?t:0)/r,u=(null!==(o=d.fontBoundingBoxDescent)&&void 0!==o?o:0)/r,p=`font-family:${i};font-size:${r}px;font-weight:${l};font-style:${a};line-height:${e.lineHeight};letter-spacing:${e.letterSpacing}em`,b=m(`<div style="${p}"><ul><li>x</li></ul><ol><li>x</li></ol></div>`);let x;try{x=h({html:b,width:Math.max(12*r,600)})}catch(T){return null}const y=[];P(x.layoutRoot,y);const w=y.find(e=>"•"===e.text),S=y.find(e=>/^\d/.test(e.text));if(!w){return null}const k=w.style.fontSize,q=2.1*r;return{bulletLeft:w.x,bulletTop:w.y-g(k,k*c,k*u),bulletFontSize:k,numberGap:S?Math.max(0,q-S.x-(null!==(n=S.width)&&void 0!==n?n:0)):0}}export const RichTextContainer=F("div",o.forwardRef)`
2
2
  .ql-editor {
3
3
  outline: none;
4
4
  /* anchor for the absolutely-positioned ::before placeholder below */
@@ -25,7 +25,7 @@ var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype
25
25
  pointer-events: none;
26
26
  opacity: 0;
27
27
  }
28
- ${f}
28
+ ${p}
29
29
 
30
30
  /* ===== Editor-only list markers (Quill flat-DOM hack) ======================
31
31
  Quill emits a FLAT <ol> with per-item data-list + ql-indent classes (not
@@ -155,4 +155,4 @@ var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype
155
155
  &[style*='--polotno-gradient'] .ql-editor [style*='color:'] {
156
156
  -webkit-text-fill-color: currentColor;
157
157
  }
158
- `;let P=["background","font-weight","color","gradient","font","italic","size","script","strike","underline","indent","list","direction"];export const setQuillFormats=e=>{P=e};let Q=!1;function A(){Q||(Q=!0,console.warn("[polotno] The Quill 'bold' format is deprecated. Use the numeric 'font-weight' format instead, e.g. quill.format('font-weight', '700') or applyTextFormat({ fontWeight: 700 })."))}function I(e){return e?"700":"400"}export const createQuill=(t,o)=>{S();const r=new n(t,{toolbar:!1,keyboard:!1,placeholder:(null==o?void 0:o.placeholder)||void 0,clipboard:{matchVisual:!1},formats:P});!function(t){const o=t.format.bind(t),n=t.formatText.bind(t),r=t.getFormat.bind(t);t.format=(e,t,n)=>"bold"===e?(A(),o("font-weight",I(t),n)):o(e,t,n),t.formatText=(t,o,r,i,l)=>{if(r&&"object"==typeof r){if("bold"in r){A();const{bold:l}=r,a=e(r,["bold"]);return n(t,o,Object.assign(Object.assign({},a),{"font-weight":I(l)}),i)}return n(t,o,r,i)}return"bold"===r?(A(),n(t,o,"font-weight",I(i),l)):n(t,o,r,i,l)},t.getFormat=(...e)=>{const t=r(...e);return t&&null!=t["font-weight"]&&!("bold"in t)&&(t.bold=u(t["font-weight"])),t}}(r);const i=n.import("delta"),l=(e,t)=>t.compose((new i).retain(t.length(),{"font-weight":"700"}));return r.clipboard.addMatcher("STRONG",l),r.clipboard.addMatcher("B",l),r};export const setQuillContent=(e,t)=>{var o=e.clipboard.convert({html:"<div class='ql-editor' style='outline: none;'>"+t.replace(/\t/g,"")+"<p><br></p></div>",text:""});o.ops.forEach(e=>{"string"==typeof e.insert&&e.insert.includes("")&&(e.insert=e.insert.split("").join("\t"))}),e.setContents(o),e.history.clear()};export const RichEditor=({onBlur:e,onChange:n,element:c,clickCoords:u})=>{var f;const p=o.useRef(null),m=o.useRef(!1);o.useLayoutEffect(()=>{let t,o=!1,i=null,s=null,f=null,h=null,g=null;const b=()=>{if(!o&&p.current){try{t=createQuill(p.current,{placeholder:w(c.placeholder)}),r.runInAction(()=>{quillRef.editor.instance=t}),quillRef.pendingToolbarInteraction=!1,window.__polotnoQuill=t,q({instance:t,store:c.store,element:c});const o=e=>{e&&syncQuillFormatState(t,e)};t.on("text-change",()=>{const e=C(),r=e&&e.instance===t?e.lastSelection:null;o(t.getSelection()||r),setTimeout(()=>{var e,o;const r=t.root;if(!r||!r.isConnected){return}if(!l(c)){return}const i=r.innerHTML;m.current=!0,n(d(i,{fontSize:c.fontSize,fontFamily:c.fontFamily}));const a=t.getContents(),s=c.fill;let u=null,f=!0;for(const t of a.ops){if("string"!=typeof t.insert||"\n"===t.insert){continue}if(null===(e=t.attributes)||void 0===e?void 0:e.gradient){f=!1;break}const n=(null===(o=t.attributes)||void 0===o?void 0:o.color)||s;if(null===u){u=n}else if(n!==u){f=!1;break}}f&&u&&c.set({fill:u})},10)}),t.on("selection-change",o);const b=a(c,c.fontFamily);setQuillContent(t,b),m.current=!0;const v=p.current.closest(".polotno-workspace-inner"),x=null==v?void 0:v.scrollLeft,y=null==v?void 0:v.scrollTop;u?setCursorFromCoords(t,u):t.setSelection(0,0,"api"),v&&void 0!==x&&void 0!==y&&(v.scrollLeft=x,v.scrollTop=y);const S=t.root;h=t=>{const o=t.relatedTarget;(null==o?void 0:o.classList.contains("ql-clipboard"))||(D(o)||quillRef.pendingToolbarInteraction?O():setTimeout(()=>{quillRef.pendingToolbarInteraction||D(document.activeElement)?O():(M(),e())},0))},g=()=>{M()},S.addEventListener("blur",h),S.addEventListener("focus",g),i=S,s=e=>{var o,n;const r=t.getSelection();if(!r||0===r.length){return}const i=t.getText(r.index,r.length).replace(/\n$/,""),l=window.getSelection();let a="";if(l&&l.rangeCount>0){const e=l.getRangeAt(0);let t=e.cloneContents(),o=e.startContainer;o&&o.nodeType===Node.TEXT_NODE&&(o=o.parentNode);let n=e.endContainer;if(n&&n.nodeType===Node.TEXT_NODE&&(n=n.parentNode),o&&o===n){let e=o;for(;e&&e!==S&&e.nodeType===Node.ELEMENT_NODE;){const o=e.tagName;if("SPAN"!==o&&"EM"!==o&&"STRONG"!==o&&"U"!==o&&"S"!==o&&"A"!==o){break}const n=e.cloneNode(!1);n.appendChild(t),t=n,e=e.parentNode}}const r=document.createElement("div");r.appendChild(t),a=r.innerHTML}if(null===(o=e.clipboardData)||void 0===o||o.setData("text/plain",i),a){const t='<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>\x3c!--StartFragment--\x3e<div style="white-space: pre-wrap; overflow-wrap: break-word;">'+a+"</div>\x3c!--EndFragment--\x3e</body></html>";null===(n=e.clipboardData)||void 0===n||n.setData("text/html",t)}e.preventDefault()},f=e=>{s&&s(e);const o=t.getSelection();o&&o.length>0&&t.deleteText(o.index,o.length)},i.addEventListener("copy",s),i.addEventListener("cut",f)}catch(b){console.error("RichEditor init failed:",b)}}};return p.current?b():queueMicrotask(b),()=>{o=!0,i&&h&&i.removeEventListener("blur",h),i&&g&&i.removeEventListener("focus",g),M(),i&&s&&f&&(i.removeEventListener("copy",s),i.removeEventListener("cut",f)),r.runInAction(()=>{quillRef.editor.instance=null,quillRef.currentFormat={}}),E(t),delete window.__polotnoQuill}},[]),o.useEffect(()=>i(c,()=>`${c.text}\0${c.fontSize}\0${c.fontFamily}`,()=>{const e=quillRef.editor.instance;if(!e){return}if(m.current){return void(m.current=!1)}const t=e.getSelection();setQuillContent(e,a(c,c.fontFamily)),m.current=!0,t&&(e.setSelection(t.index,t.length),syncQuillFormatState(e,e.getSelection()))},{fireImmediately:!0}),[]),o.useEffect(()=>i(c,()=>`${c.fontFamily}\0${c.fontSize}\0${c.fontWeight}\0${c.fontStyle}\0${c.lineHeight}\0${c.letterSpacing}\0${s(g(c.text))}`,()=>{const e=p.current;if(!e){return}const t="ltr"===s(g(c.text)),o=computeMarkerVars(c);o&&e.style.setProperty("--rt-number-gap",`${o.numberGap}px`),o&&t?(e.style.setProperty("--rt-bullet-left",`${o.bulletLeft}px`),e.style.setProperty("--rt-bullet-top",`${o.bulletTop}px`),e.style.setProperty("--rt-bullet-fs",`${o.bulletFontSize}px`),e.classList.add("rt-marker-geom")):e.classList.remove("rt-marker-geom")},{fireImmediately:!0}),[]),o.useEffect(()=>{window.addEventListener("blur",e);const t=t=>{var o;const n=t.target;"number"==typeof(null==n?void 0:n.nodeType)&&(null===(o=p.current)||void 0===o?void 0:o.contains(n))||D(n)||quillRef.pendingToolbarInteraction||(M(),e())};return window.addEventListener("mousedown",t),window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",e),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t)}},[]);const h={color:c.fill};c.fill.indexOf("gradient")>=0&&(h["--polotno-gradient"]=c.fill,h.color="transparent"),c.strokeWidth>0&&(h.WebkitTextStroke=`${c.strokeWidth}px ${x(c.stroke)}`,h.paintOrder="stroke fill");const S=v(c.fill)?x(c.fill):y(c),k=c.a.rotation*Math.PI/180,T=c.shadowOffsetX*Math.cos(k)+c.shadowOffsetY*Math.sin(k),L=-c.shadowOffsetX*Math.sin(k)+c.shadowOffsetY*Math.cos(k);return t(RichTextContainer,{ref:p,style:Object.assign(Object.assign({},h),{fontSize:c.fontSize,fontFamily:b(c.fontFamily),fontWeight:c.fontWeight,fontStyle:c.fontStyle,"--polotno-text-decoration":c.textDecoration||"none",textTransform:c.textTransform,width:c.a.width,lineHeight:c.lineHeight,letterSpacing:c.letterSpacing*c.fontSize+"px",textAlign:c.align,opacity:Math.max(c.a.opacity,.2),textShadow:c.shadowEnabled?`${T}px ${L}px ${c.shadowBlur}px ${F(c.shadowColor,null!==(f=c.shadowOpacity)&&void 0!==f?f:1)}`:void 0,caretColor:S}),dir:s(g(c.text))})};function D(e){const t=e;return!("function"!=typeof(null==t?void 0:t.closest)||!t.closest(".sketch-picker")&&!t.closest("[data-polotno-keep-focus]")&&!t.closest(".polotno-portal")&&!t.closest(".polotno-toolbar"))}export function setCursorFromCoords(e,t){if(!e||!t){return}const{x:o,y:r}=t;try{let t=null;if(document.caretRangeFromPoint){t=document.caretRangeFromPoint(o,r)}else if(document.caretPositionFromPoint){const e=document.caretPositionFromPoint(o,r);e&&(t=document.createRange(),t.setStart(e.offsetNode,e.offset))}if(t){const o=n.find(t.startContainer,!0);if(o){const n=o.offset(e.scroll)+t.startOffset;return void e.setSelection(n,0,"api")}}}catch(i){}e.setSelection(0,0,"api")}
158
+ `;let Q=["background","font-weight","color","gradient","font","italic","size","script","strike","underline","indent","list","direction"];export const setQuillFormats=e=>{Q=e};let A=!1;function I(){A||(A=!0,console.warn("[polotno] The Quill 'bold' format is deprecated. Use the numeric 'font-weight' format instead, e.g. quill.format('font-weight', '700') or applyTextFormat({ fontWeight: 700 })."))}function D(e){return e?"700":"400"}export const createQuill=(t,o)=>{k();const r=new n(t,{toolbar:!1,keyboard:!1,placeholder:(null==o?void 0:o.placeholder)||void 0,clipboard:{matchVisual:!1},formats:Q});!function(t){const o=t.format.bind(t),n=t.formatText.bind(t),r=t.getFormat.bind(t);t.format=(e,t,n)=>"bold"===e?(I(),o("font-weight",D(t),n)):o(e,t,n),t.formatText=(t,o,r,i,l)=>{if(r&&"object"==typeof r){if("bold"in r){I();const{bold:l}=r,a=e(r,["bold"]);return n(t,o,Object.assign(Object.assign({},a),{"font-weight":D(l)}),i)}return n(t,o,r,i)}return"bold"===r?(I(),n(t,o,"font-weight",D(i),l)):n(t,o,r,i,l)},t.getFormat=(...e)=>{const t=r(...e);return t&&null!=t["font-weight"]&&!("bold"in t)&&(t.bold=f(t["font-weight"])),t}}(r);const i=n.import("delta"),l=(e,t)=>t.compose((new i).retain(t.length(),{"font-weight":"700"}));return r.clipboard.addMatcher("STRONG",l),r.clipboard.addMatcher("B",l),r};export const setQuillContent=(e,t)=>{var o=e.clipboard.convert({html:"<div class='ql-editor' style='outline: none;'>"+t.replace(/\t/g,"")+"<p><br></p></div>",text:""});o.ops.forEach(e=>{"string"==typeof e.insert&&e.insert.includes("")&&(e.insert=e.insert.split("").join("\t"))}),e.setContents(o),e.history.clear()};export const RichEditor=({onBlur:e,onChange:n,element:u,clickCoords:f})=>{var p;const m=o.useRef(null),h=o.useRef(!1);o.useLayoutEffect(()=>{let t,o=!1,i=null,s=null,d=null,p=null,g=null;const b=()=>{if(!o&&m.current){try{t=createQuill(m.current,{placeholder:S(u.placeholder)}),r.runInAction(()=>{quillRef.editor.instance=t}),quillRef.pendingToolbarInteraction=!1,window.__polotnoQuill=t,T({instance:t,store:u.store,element:u});const o=e=>{e&&syncQuillFormatState(t,e)};t.on("text-change",()=>{const e=L(),r=e&&e.instance===t?e.lastSelection:null;o(t.getSelection()||r),setTimeout(()=>{var e,o;const r=t.root;if(!r||!r.isConnected){return}if(!l(u)){return}const i=r.innerHTML;h.current=!0,n(c(i,{fontSize:u.fontSize,fontFamily:u.fontFamily}));const a=t.getContents(),s=u.fill;let d=null,f=!0;for(const t of a.ops){if("string"!=typeof t.insert||"\n"===t.insert){continue}if(null===(e=t.attributes)||void 0===e?void 0:e.gradient){f=!1;break}const n=(null===(o=t.attributes)||void 0===o?void 0:o.color)||s;if(null===d){d=n}else if(n!==d){f=!1;break}}f&&d&&u.set({fill:d})},10)}),t.on("selection-change",o);const b=a(u,u.fontFamily);setQuillContent(t,b),h.current=!0;const v=m.current.closest(".polotno-workspace-inner"),x=null==v?void 0:v.scrollLeft,y=null==v?void 0:v.scrollTop;f?setCursorFromCoords(t,f):t.setSelection(0,0,"api"),v&&void 0!==x&&void 0!==y&&(v.scrollLeft=x,v.scrollTop=y);const w=t.root;p=t=>{const o=t.relatedTarget;(null==o?void 0:o.classList.contains("ql-clipboard"))||(H(o)||quillRef.pendingToolbarInteraction?M():setTimeout(()=>{quillRef.pendingToolbarInteraction||H(document.activeElement)?M():(j(),e())},0))},g=()=>{j()},w.addEventListener("blur",p),w.addEventListener("focus",g),i=w,s=e=>{var o,n;const r=t.getSelection();if(!r||0===r.length){return}const i=t.getText(r.index,r.length).replace(/\n$/,""),l=window.getSelection();let a="";if(l&&l.rangeCount>0){const e=l.getRangeAt(0);let t=e.cloneContents(),o=e.startContainer;o&&o.nodeType===Node.TEXT_NODE&&(o=o.parentNode);let n=e.endContainer;if(n&&n.nodeType===Node.TEXT_NODE&&(n=n.parentNode),o&&o===n){let e=o;for(;e&&e!==w&&e.nodeType===Node.ELEMENT_NODE;){const o=e.tagName;if("SPAN"!==o&&"EM"!==o&&"STRONG"!==o&&"U"!==o&&"S"!==o&&"A"!==o){break}const n=e.cloneNode(!1);n.appendChild(t),t=n,e=e.parentNode}}const r=document.createElement("div");r.appendChild(t),a=r.innerHTML}if(null===(o=e.clipboardData)||void 0===o||o.setData("text/plain",i),a){const t='<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>\x3c!--StartFragment--\x3e<div style="white-space: pre-wrap; overflow-wrap: break-word;">'+a+"</div>\x3c!--EndFragment--\x3e</body></html>";null===(n=e.clipboardData)||void 0===n||n.setData("text/html",t)}e.preventDefault()},d=e=>{s&&s(e);const o=t.getSelection();o&&o.length>0&&t.deleteText(o.index,o.length)},i.addEventListener("copy",s),i.addEventListener("cut",d)}catch(b){console.error("RichEditor init failed:",b)}}};return m.current?b():queueMicrotask(b),()=>{o=!0,i&&p&&i.removeEventListener("blur",p),i&&g&&i.removeEventListener("focus",g),j(),i&&s&&d&&(i.removeEventListener("copy",s),i.removeEventListener("cut",d)),r.runInAction(()=>{quillRef.editor.instance=null,quillRef.currentFormat={}}),C(t),delete window.__polotnoQuill}},[]),o.useEffect(()=>i(u,()=>`${u.text}\0${u.fontSize}\0${u.fontFamily}`,()=>{const e=quillRef.editor.instance;if(!e){return}if(h.current){return void(h.current=!1)}const t=e.getSelection();setQuillContent(e,a(u,u.fontFamily)),h.current=!0,t&&(e.setSelection(t.index,t.length),syncQuillFormatState(e,e.getSelection()))},{fireImmediately:!0}),[]),o.useEffect(()=>i(u,()=>`${u.fontFamily}\0${u.fontSize}\0${u.fontWeight}\0${u.fontStyle}\0${u.lineHeight}\0${u.letterSpacing}\0${s(b(u.text))}`,()=>{const e=m.current;if(!e){return}const t="ltr"===s(b(u.text)),o=computeMarkerVars(u);o&&e.style.setProperty("--rt-number-gap",`${o.numberGap}px`),o&&t?(e.style.setProperty("--rt-bullet-left",`${o.bulletLeft}px`),e.style.setProperty("--rt-bullet-top",`${o.bulletTop}px`),e.style.setProperty("--rt-bullet-fs",`${o.bulletFontSize}px`),e.classList.add("rt-marker-geom")):e.classList.remove("rt-marker-geom")},{fireImmediately:!0}),[]),o.useEffect(()=>{window.addEventListener("blur",e);const t=t=>{var o;const n=t.target;"number"==typeof(null==n?void 0:n.nodeType)&&(null===(o=m.current)||void 0===o?void 0:o.contains(n))||H(n)||quillRef.pendingToolbarInteraction||(j(),e())};return window.addEventListener("mousedown",t),window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",e),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t)}},[]);const g={color:u.fill};u.fill.indexOf("gradient")>=0&&(g["--polotno-gradient"]=u.fill,g.color="transparent"),u.strokeWidth>0&&(g.WebkitTextStroke=`${u.strokeWidth}px ${y(u.stroke)}`,g.paintOrder="stroke fill");const k=x(u.fill)?y(u.fill):w(u),q=u.a.rotation*Math.PI/180,E=u.shadowOffsetX*Math.cos(q)+u.shadowOffsetY*Math.sin(q),F=-u.shadowOffsetX*Math.sin(q)+u.shadowOffsetY*Math.cos(q);return t(RichTextContainer,{ref:m,style:Object.assign(Object.assign({},g),{fontSize:u.fontSize,fontFamily:v(u.fontFamily),fontWeight:u.fontWeight,fontStyle:u.fontStyle,"--polotno-text-decoration":u.textDecoration||"none",textTransform:u.textTransform,width:d(u.a.width,u.letterSpacing,u.fontSize),lineHeight:u.lineHeight,letterSpacing:u.letterSpacing*u.fontSize+"px",textAlign:u.align,opacity:Math.max(u.a.opacity,.2),textShadow:u.shadowEnabled?`${E}px ${F}px ${u.shadowBlur}px ${R(u.shadowColor,null!==(p=u.shadowOpacity)&&void 0!==p?p:1)}`:void 0,caretColor:k}),dir:s(b(u.text))})};function H(e){const t=e;return!("function"!=typeof(null==t?void 0:t.closest)||!t.closest(".sketch-picker")&&!t.closest("[data-polotno-keep-focus]")&&!t.closest(".polotno-portal")&&!t.closest(".polotno-toolbar"))}export function setCursorFromCoords(e,t){if(!e||!t){return}const{x:o,y:r}=t;try{let t=null;if(document.caretRangeFromPoint){t=document.caretRangeFromPoint(o,r)}else if(document.caretPositionFromPoint){const e=document.caretPositionFromPoint(o,r);e&&(t=document.createRange(),t.setStart(e.offsetNode,e.offset))}if(t){const o=n.find(t.startContainer,!0);if(o){const n=o.offset(e.scroll)+t.startOffset;return void e.setSelection(n,0,"api")}}}catch(i){}e.setSelection(0,0,"api")}
@@ -1 +1 @@
1
- import{jsx as t,Fragment as e,jsxs as o}from"react/jsx-runtime";import i from"react";import{observer as n}from"mobx-react-lite";import{observable as r,runInAction as l}from"mobx";import{nodeAutorun as s}from"../utils/mobx.js";import{Group as a,Rect as h,Line as c,Circle as d}from"react-konva";import{Html as g}from"react-konva-utils";import{ROLES as u}from"../model/store.js";import{isTouchDevice as w}from"../utils/screen.js";import{TextElement as p}from"./text-element.js";const f="#0d83dd",x=(t,e)=>{const o=t.target.getStage();o&&(o.container().style.cursor=e)},y=n(({axis:i,hovered:n,pillX:r,pillY:l,pillW:s,pillH:a,hitX:c,hitY:d,hitW:g,hitH:u,tint:w,hoverKind:p,idx:y,onSelect:v,onHoverChange:m})=>o(e,{children:[n&&t(h,{x:w.x,y:w.y,width:w.width,height:w.height,fill:"rgba(13, 131, 221, 0.08)",listening:!1}),t(h,{x:r,y:l,width:s,height:a,cornerRadius:Math.min(s,a)/2,fill:n?f:"#9ca3af",opacity:n?.55:.18,listening:!1}),t(h,{x:c,y:d,width:g,height:u,fill:"transparent",onMouseEnter:t=>{m({kind:p,idx:y}),x(t,"pointer")},onMouseLeave:t=>{m(null),x(t,"")},onClick:v})]})),v=n(({x:i,y:n,hovered:r,hoverKind:l,idx:s,isTouch:g,sizes:u,onInsert:w,onHoverChange:p})=>{const{hit:y,r:v,halo:m,stroke:b}=u,C=r||g;return o(a,{x:i,y:n,onMouseEnter:t=>{p({kind:l,idx:s}),x(t,"pointer")},onMouseLeave:t=>{p(null),x(t,"")},onClick:t=>{t.cancelBubble=!0,w()},children:[t(h,{x:-y/2,y:-y/2,width:y,height:y,fill:"transparent"}),C&&o(e,{children:[t(d,{radius:v+m,fill:"white",listening:!1}),t(d,{radius:v,fill:f,listening:!1}),t(c,{points:[.55*-v,0,.55*v,0],stroke:"white",strokeWidth:b,lineCap:"round",listening:!1}),t(c,{points:[0,.55*-v,0,.55*v],stroke:"white",strokeWidth:b,lineCap:"round",listening:!1})]})]})}),m=n(({element:o,store:i,inv:n,isTouch:r,hover:l,setHover:s})=>{const a=16*n,h=2*n,c=10*n,d={hit:22*n,r:7*n,halo:2*n,stroke:2*n},g=(t,e)=>null!==l&&l.kind===t&&l.idx===e,u=(t,e)=>i=>{i.cancelBubble=!0;const n=o._anchorCellId?o.cells.find(t=>t.id===o._anchorCellId):void 0;i.evt.shiftKey&&n?"col"===t?o.selectColumnRange(n.col,e):o.selectRowRange(n.row,e):"col"===t?o.selectColumn(e):o.selectRow(e)},w=[0],p=[0];for(let t=0;t<o.cols;t++){w.push(w[t]+o.colWidths[t]*o.a.width)}for(let t=0;t<o.rows;t++){p.push(p[t]+o.rowHeights[t]*o.a.height)}const f=[];for(let e=0;e<o.cols;e++){const i=Math.max(0,w[e+1]-w[e]-6);i<=0||f.push(t(y,{axis:"col",hovered:g("colSelect",e),pillX:w[e]+3,pillY:-h-c,pillW:i,pillH:h,hitX:w[e]+1,hitY:-a-c+h,hitW:i+4,hitH:a,tint:{x:w[e],y:0,width:w[e+1]-w[e],height:o.a.height},hoverKind:"colSelect",idx:e,onSelect:u("col",e),onHoverChange:s},`col-pill-${e}`))}for(let e=0;e<=o.cols;e++){f.push(t(v,{x:w[e],y:-c-h/2,hovered:g("colInsert",e),hoverKind:"colInsert",idx:e,isTouch:r,sizes:d,onInsert:()=>i.history.transaction(()=>o.addColumn(e)),onHoverChange:s},`col-insert-${e}`))}for(let e=0;e<o.rows;e++){const i=Math.max(0,p[e+1]-p[e]-6);i<=0||f.push(t(y,{axis:"row",hovered:g("rowSelect",e),pillX:-h-c,pillY:p[e]+3,pillW:h,pillH:i,hitX:-a-c+h,hitY:p[e]+1,hitW:a,hitH:i+4,tint:{x:0,y:p[e],width:o.a.width,height:p[e+1]-p[e]},hoverKind:"rowSelect",idx:e,onSelect:u("row",e),onHoverChange:s},`row-pill-${e}`))}for(let e=0;e<=o.rows;e++){f.push(t(v,{x:-c-h/2,y:p[e],hovered:g("rowInsert",e),hoverKind:"rowInsert",idx:e,isTouch:r,sizes:d,onInsert:()=>i.history.transaction(()=>o.addRow(e)),onHoverChange:s},`row-insert-${e}`))}return t(e,{children:f})});export const TableElement=n(({element:n,store:d})=>{const f=i.useRef(null),x=i.useRef(null),y=n.selectable||d.role===u.ADMIN,v=w(),b=n.isSelected,C=n._focusedCellIds.length>0,k=n.editingCell,S=1/Math.max(.01,d.scale),[E,I]=i.useState(null);i.useEffect(()=>{b||(n.clearCellFocus(),I(null))},[b]);const H=null==k?void 0:k._editModeEnabled;i.useEffect(()=>{n._editingCellId&&k&&!k._editModeEnabled&&n.exitCellEdit()},[H]),i.useEffect(()=>{C&&!n._editingCellId&&setTimeout(()=>{var t;return null===(t=x.current)||void 0===t?void 0:t.focus()},0)},[C,n._editingCellId]);const M=i.useMemo(()=>r.box(!1),[]);i.useEffect(()=>s(n,()=>{M.get()||n._fitRowsToContent&&d.history.ignore(()=>{n._applyFitRowsToContent()})}),[n,d,M]);const R=t=>{const e=t.target.getStage();if(!e){return}const o=e.getPointerPosition();if(!o){return}const i=f.current;if(!i){return}const r=i.getAbsoluteTransform().copy().invert().point(o);for(const l of n.visibleCells){const t=n.getCellRect(l.row,l.col,l.rowSpan,l.colSpan);if(r.x>=t.x&&r.x<=t.x+t.width&&r.y>=t.y&&r.y<=t.y+t.height){return l}}},W=(t,e)=>"dashed"===t?[4*e,2*e]:"dotted"===t?[e,e]:void 0,T=[];for(const e of n.visibleCells){const o=n.getCellRect(e.row,e.col,e.rowSpan,e.colSpan),i=0===e.row,r=e.row+e.rowSpan===n.rows,l=0===e.col,s=e.col+e.colSpan===n.cols;if(0===e.row){const i=e.getEffectiveBorder("top");if("none"!==i.style&&i.width>0){const n=o.y+i.width/2,r=o.x-(l?0:i.width/2),a=o.x+o.width+(s?0:i.width/2);T.push(t(c,{points:[r,n,a,n],stroke:i.color,strokeWidth:i.width,dash:W(i.style,i.width),listening:!1},`bt-${e.id}`))}}if(0===e.col){const n=e.getEffectiveBorder("left");if("none"!==n.style&&n.width>0){const l=o.x+n.width/2,s=o.y-(i?0:n.width/2),a=o.y+o.height+(r?0:n.width/2);T.push(t(c,{points:[l,s,l,a],stroke:n.color,strokeWidth:n.width,dash:W(n.style,n.width),listening:!1},`bl-${e.id}`))}}{const i=e.getEffectiveBorder("bottom");if("none"!==i.style&&i.width>0){const n=r?o.y+o.height-i.width/2:o.y+o.height,a=o.x-(l?0:i.width/2),h=o.x+o.width+(s?0:i.width/2);T.push(t(c,{points:[a,n,h,n],stroke:i.color,strokeWidth:i.width,dash:W(i.style,i.width),listening:!1},`bb-${e.id}`))}}{const n=e.getEffectiveBorder("right");if("none"!==n.style&&n.width>0){const l=s?o.x+o.width-n.width/2:o.x+o.width,a=o.y-(i?0:n.width/2),h=o.y+o.height+(r?0:n.width/2);T.push(t(c,{points:[l,a,l,h],stroke:n.color,strokeWidth:n.width,dash:W(n.style,n.width),listening:!1},`br-${e.id}`))}}}return o(e,{children:[t(h,{id:n.id,name:"element",x:n.a.x,y:n.a.y,rotation:n.a.rotation,width:n.a.width,height:n.a.height,fill:"transparent",opacity:n.a.opacity,hideInExport:!n.showInExport,listening:y,draggable:v?n.draggable&&b:n.draggable,onClick:t=>{if(!b){return}if(n.locked||!n.contentEditable){return}const e=R(t);e&&(t.evt.shiftKey&&C?(t.cancelBubble=!0,n.focusCellRange(e.row,e.col)):n.focusCell(e.id))},onDblClick:t=>{if(!b){return}if(n.locked||!n.contentEditable){return}const e=R(t);e&&n.enterCellEdit(e.id)},onDragMove:t=>{var e;const o=t.target;n.set({x:o.x(),y:o.y()}),null===(e=f.current)||void 0===e||e.position({x:o.x(),y:o.y()})},onDragEnd:t=>{const e=t.target;n.set({x:e.x(),y:e.y()})},onTransformStart:()=>{l(()=>M.set(!0))},onTransformEnd:()=>{n._resetBaseRowHeights(),l(()=>M.set(!1))},onTransform:t=>{var e,o;const i=t.target,r=i.scaleX(),l=i.scaleY();i.scaleX(1),i.scaleY(1);const s=n.width*r,a=n.height*l;i.width(s),i.height(a);const h=i.x(),c=i.y(),d=i.rotation();null===(e=f.current)||void 0===e||e.position({x:h,y:c}),null===(o=f.current)||void 0===o||o.rotation(d),n.set({width:s,x:h,y:c,rotation:d}),n.setHeightRedistribute(a)}}),o(a,{ref:f,x:n.a.x,y:n.a.y,rotation:n.a.rotation,opacity:n.a.opacity,hideInExport:!n.showInExport,scaleX:n.a.width/n.width,scaleY:n.a.height/n.height,children:[n.visibleCells.map(e=>{const o=n.getCellRect(e.row,e.col,e.rowSpan,e.colSpan);return"transparent"===e.cellBackground?null:t(h,{x:o.x,y:o.y,width:o.width,height:o.height,fill:e.cellBackground,listening:!1},`bg-${e.id}`)}),T,n.visibleCells.map(e=>t(p,{element:e,store:d},`text-${e.id}`)),b&&n._focusedCellIds.map(e=>{const o=n.cells.find(t=>t.id===e);if(!o){return null}const i=n.getCellRect(o.row,o.col,o.rowSpan,o.colSpan),r=2*S;return t(h,{x:i.x,y:i.y,width:i.width,height:i.height,fill:"rgba(13, 131, 221, 0.1)",stroke:"#0d83dd",strokeWidth:r,listening:!1},`focus-${e}`)}),C&&!k&&t(g,{children:t("div",{ref:x,tabIndex:-1,style:{position:"absolute",opacity:0,width:"1px",height:"1px",pointerEvents:"none",outline:"none"}})}),b&&!k&&!d.isPlaying&&(()=>{const e=[];let o=0;for(let i=0;i<n.cols-1;i++){o+=n.colWidths[i]*n.a.width;const r=o,l=i;e.push(t(c,{x:r,points:[0,0,0,n.a.height],stroke:"transparent",strokeWidth:1,hitStrokeWidth:16,draggable:!0,onMouseEnter:t=>{var e;const o=null===(e=t.target.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor="col-resize"),I({kind:"colInsert",idx:l+1})},onMouseLeave:t=>{var e;const o=null===(e=t.target.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor=""),I(null)},onDragStart:t=>{t.cancelBubble=!0,d.history.startTransaction()},onDragMove:t=>{t.cancelBubble=!0;const e=t.target,o=(e.x()-r)/n.width;e.x(r),n.resizeColumn(l,o)},onDragEnd:t=>{t.cancelBubble=!0,t.target.x(r),d.history.endTransaction()}},`col-resize-${i}`))}return e})(),b&&!k&&!d.isPlaying&&(()=>{const e=[];let o=0;for(let i=0;i<n.rows-1;i++){o+=n.rowHeights[i]*n.a.height;const r=o,s=i;e.push(t(c,{y:r,points:[0,0,n.a.width,0],stroke:"transparent",strokeWidth:1,hitStrokeWidth:16,draggable:!0,onMouseEnter:t=>{var e;const o=null===(e=t.target.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor="row-resize"),I({kind:"rowInsert",idx:s+1})},onMouseLeave:t=>{var e;const o=null===(e=t.target.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor=""),I(null)},onDragStart:t=>{t.cancelBubble=!0,l(()=>M.set(!0)),d.history.startTransaction()},onDragMove:t=>{t.cancelBubble=!0;const e=t.target,o=(e.y()-r)/n.height;e.y(r),n.resizeRow(s,o)},onDragEnd:t=>{t.cancelBubble=!0,t.target.y(r),n._resetBaseRowHeights(),l(()=>M.set(!1)),d.history.endTransaction()}},`row-resize-${i}`))}return e})(),b&&!k&&!d.isPlaying&&t(m,{element:n,store:d,inv:S,isTouch:v,hover:E,setHover:I})]})]})});
1
+ import{jsx as t,Fragment as e,jsxs as o}from"react/jsx-runtime";import i from"react";import{observer as n}from"mobx-react-lite";import{observable as r,runInAction as l}from"mobx";import{nodeAutorun as s}from"../utils/mobx.js";import{Group as a,Rect as h,Line as c,Circle as d}from"react-konva";import{Html as g}from"react-konva-utils";import{ROLES as u}from"../model/store.js";import{isTouchDevice as w}from"../utils/screen.js";import{roundedGeometryPx as p,roundedPosition as f}from"../utils/px.js";import{TextElement as x}from"./text-element.js";const y="#0d83dd",v=(t,e)=>{const o=t.target.getStage();o&&(o.container().style.cursor=e)},m=n(({axis:i,hovered:n,pillX:r,pillY:l,pillW:s,pillH:a,hitX:c,hitY:d,hitW:g,hitH:u,tint:w,hoverKind:p,idx:f,onSelect:x,onHoverChange:m})=>o(e,{children:[n&&t(h,{x:w.x,y:w.y,width:w.width,height:w.height,fill:"rgba(13, 131, 221, 0.08)",listening:!1}),t(h,{x:r,y:l,width:s,height:a,cornerRadius:Math.min(s,a)/2,fill:n?y:"#9ca3af",opacity:n?.55:.18,listening:!1}),t(h,{x:c,y:d,width:g,height:u,fill:"transparent",onMouseEnter:t=>{m({kind:p,idx:f}),v(t,"pointer")},onMouseLeave:t=>{m(null),v(t,"")},onClick:x})]})),b=n(({x:i,y:n,hovered:r,hoverKind:l,idx:s,isTouch:g,sizes:u,onInsert:w,onHoverChange:p})=>{const{hit:f,r:x,halo:m,stroke:b}=u,C=r||g;return o(a,{x:i,y:n,onMouseEnter:t=>{p({kind:l,idx:s}),v(t,"pointer")},onMouseLeave:t=>{p(null),v(t,"")},onClick:t=>{t.cancelBubble=!0,w()},children:[t(h,{x:-f/2,y:-f/2,width:f,height:f,fill:"transparent"}),C&&o(e,{children:[t(d,{radius:x+m,fill:"white",listening:!1}),t(d,{radius:x,fill:y,listening:!1}),t(c,{points:[.55*-x,0,.55*x,0],stroke:"white",strokeWidth:b,lineCap:"round",listening:!1}),t(c,{points:[0,.55*-x,0,.55*x],stroke:"white",strokeWidth:b,lineCap:"round",listening:!1})]})]})}),C=n(({element:o,store:i,inv:n,isTouch:r,hover:l,setHover:s})=>{const a=16*n,h=2*n,c=10*n,d={hit:22*n,r:7*n,halo:2*n,stroke:2*n},g=(t,e)=>null!==l&&l.kind===t&&l.idx===e,u=(t,e)=>i=>{i.cancelBubble=!0;const n=o._anchorCellId?o.cells.find(t=>t.id===o._anchorCellId):void 0;i.evt.shiftKey&&n?"col"===t?o.selectColumnRange(n.col,e):o.selectRowRange(n.row,e):"col"===t?o.selectColumn(e):o.selectRow(e)},w=[0],p=[0];for(let t=0;t<o.cols;t++){w.push(w[t]+o.colWidths[t]*o.a.width)}for(let t=0;t<o.rows;t++){p.push(p[t]+o.rowHeights[t]*o.a.height)}const f=[];for(let e=0;e<o.cols;e++){const i=Math.max(0,w[e+1]-w[e]-6);i<=0||f.push(t(m,{axis:"col",hovered:g("colSelect",e),pillX:w[e]+3,pillY:-h-c,pillW:i,pillH:h,hitX:w[e]+1,hitY:-a-c+h,hitW:i+4,hitH:a,tint:{x:w[e],y:0,width:w[e+1]-w[e],height:o.a.height},hoverKind:"colSelect",idx:e,onSelect:u("col",e),onHoverChange:s},`col-pill-${e}`))}for(let e=0;e<=o.cols;e++){f.push(t(b,{x:w[e],y:-c-h/2,hovered:g("colInsert",e),hoverKind:"colInsert",idx:e,isTouch:r,sizes:d,onInsert:()=>i.history.transaction(()=>o.addColumn(e)),onHoverChange:s},`col-insert-${e}`))}for(let e=0;e<o.rows;e++){const i=Math.max(0,p[e+1]-p[e]-6);i<=0||f.push(t(m,{axis:"row",hovered:g("rowSelect",e),pillX:-h-c,pillY:p[e]+3,pillW:h,pillH:i,hitX:-a-c+h,hitY:p[e]+1,hitW:a,hitH:i+4,tint:{x:0,y:p[e],width:o.a.width,height:p[e+1]-p[e]},hoverKind:"rowSelect",idx:e,onSelect:u("row",e),onHoverChange:s},`row-pill-${e}`))}for(let e=0;e<=o.rows;e++){f.push(t(b,{x:-c-h/2,y:p[e],hovered:g("rowInsert",e),hoverKind:"rowInsert",idx:e,isTouch:r,sizes:d,onInsert:()=>i.history.transaction(()=>o.addRow(e)),onHoverChange:s},`row-insert-${e}`))}return t(e,{children:f})});export const TableElement=n(({element:n,store:d})=>{const y=i.useRef(null),v=i.useRef(null),m=n.selectable||d.role===u.ADMIN,b=w(),k=n.isSelected,S=n._focusedCellIds.length>0,E=n.editingCell,I=1/Math.max(.01,d.scale),[H,M]=i.useState(null);i.useEffect(()=>{k||(n.clearCellFocus(),M(null))},[k]);const R=null==E?void 0:E._editModeEnabled;i.useEffect(()=>{n._editingCellId&&E&&!E._editModeEnabled&&n.exitCellEdit()},[R]),i.useEffect(()=>{S&&!n._editingCellId&&setTimeout(()=>{var t;return null===(t=v.current)||void 0===t?void 0:t.focus()},0)},[S,n._editingCellId]);const W=i.useMemo(()=>r.box(!1),[]);i.useEffect(()=>s(n,()=>{W.get()||n._fitRowsToContent&&d.history.ignore(()=>{n._applyFitRowsToContent()})}),[n,d,W]);const T=t=>{const e=t.target.getStage();if(!e){return}const o=e.getPointerPosition();if(!o){return}const i=y.current;if(!i){return}const r=i.getAbsoluteTransform().copy().invert().point(o);for(const l of n.visibleCells){const t=n.getCellRect(l.row,l.col,l.rowSpan,l.colSpan);if(r.x>=t.x&&r.x<=t.x+t.width&&r.y>=t.y&&r.y<=t.y+t.height){return l}}},B=(t,e)=>"dashed"===t?[4*e,2*e]:"dotted"===t?[e,e]:void 0,$=[];for(const e of n.visibleCells){const o=n.getCellRect(e.row,e.col,e.rowSpan,e.colSpan),i=0===e.row,r=e.row+e.rowSpan===n.rows,l=0===e.col,s=e.col+e.colSpan===n.cols;if(0===e.row){const i=e.getEffectiveBorder("top");if("none"!==i.style&&i.width>0){const n=o.y+i.width/2,r=o.x-(l?0:i.width/2),a=o.x+o.width+(s?0:i.width/2);$.push(t(c,{points:[r,n,a,n],stroke:i.color,strokeWidth:i.width,dash:B(i.style,i.width),listening:!1},`bt-${e.id}`))}}if(0===e.col){const n=e.getEffectiveBorder("left");if("none"!==n.style&&n.width>0){const l=o.x+n.width/2,s=o.y-(i?0:n.width/2),a=o.y+o.height+(r?0:n.width/2);$.push(t(c,{points:[l,s,l,a],stroke:n.color,strokeWidth:n.width,dash:B(n.style,n.width),listening:!1},`bl-${e.id}`))}}{const i=e.getEffectiveBorder("bottom");if("none"!==i.style&&i.width>0){const n=r?o.y+o.height-i.width/2:o.y+o.height,a=o.x-(l?0:i.width/2),h=o.x+o.width+(s?0:i.width/2);$.push(t(c,{points:[a,n,h,n],stroke:i.color,strokeWidth:i.width,dash:B(i.style,i.width),listening:!1},`bb-${e.id}`))}}{const n=e.getEffectiveBorder("right");if("none"!==n.style&&n.width>0){const l=s?o.x+o.width-n.width/2:o.x+o.width,a=o.y-(i?0:n.width/2),h=o.y+o.height+(r?0:n.width/2);$.push(t(c,{points:[l,a,l,h],stroke:n.color,strokeWidth:n.width,dash:B(n.style,n.width),listening:!1},`br-${e.id}`))}}}return o(e,{children:[t(h,{id:n.id,name:"element",x:n.a.x,y:n.a.y,rotation:n.a.rotation,width:n.a.width,height:n.a.height,fill:"transparent",opacity:n.a.opacity,hideInExport:!n.showInExport,listening:m,draggable:b?n.draggable&&k:n.draggable,onClick:t=>{if(!k){return}if(n.locked||!n.contentEditable){return}const e=T(t);e&&(t.evt.shiftKey&&S?(t.cancelBubble=!0,n.focusCellRange(e.row,e.col)):n.focusCell(e.id))},onDblClick:t=>{if(!k){return}if(n.locked||!n.contentEditable){return}const e=T(t);e&&n.enterCellEdit(e.id)},onDragMove:t=>{var e;const o=t.target;n.set({x:o.x(),y:o.y()}),null===(e=y.current)||void 0===e||e.position({x:o.x(),y:o.y()})},onDragEnd:t=>{n.set(f(t.target))},onTransformStart:()=>{l(()=>W.set(!0))},onTransformEnd:()=>{n.set({x:p(n.x),y:p(n.y),width:p(n.width),rotation:p(n.rotation)}),n._resetBaseRowHeights(),l(()=>W.set(!1))},onTransform:t=>{var e,o;const i=t.target,r=i.scaleX(),l=i.scaleY();i.scaleX(1),i.scaleY(1);const s=n.width*r,a=n.height*l;i.width(s),i.height(a);const h=i.x(),c=i.y(),d=i.rotation();null===(e=y.current)||void 0===e||e.position({x:h,y:c}),null===(o=y.current)||void 0===o||o.rotation(d),n.set({width:s,x:h,y:c,rotation:d}),n.setHeightRedistribute(a)}}),o(a,{ref:y,x:n.a.x,y:n.a.y,rotation:n.a.rotation,opacity:n.a.opacity,hideInExport:!n.showInExport,scaleX:n.a.width/n.width,scaleY:n.a.height/n.height,children:[n.visibleCells.map(e=>{const o=n.getCellRect(e.row,e.col,e.rowSpan,e.colSpan);return"transparent"===e.cellBackground?null:t(h,{x:o.x,y:o.y,width:o.width,height:o.height,fill:e.cellBackground,listening:!1},`bg-${e.id}`)}),$,n.visibleCells.map(e=>t(x,{element:e,store:d},`text-${e.id}`)),k&&n._focusedCellIds.map(e=>{const o=n.cells.find(t=>t.id===e);if(!o){return null}const i=n.getCellRect(o.row,o.col,o.rowSpan,o.colSpan),r=2*I;return t(h,{x:i.x,y:i.y,width:i.width,height:i.height,fill:"rgba(13, 131, 221, 0.1)",stroke:"#0d83dd",strokeWidth:r,listening:!1},`focus-${e}`)}),S&&!E&&t(g,{children:t("div",{ref:v,tabIndex:-1,style:{position:"absolute",opacity:0,width:"1px",height:"1px",pointerEvents:"none",outline:"none"}})}),k&&!E&&!d.isPlaying&&(()=>{const e=[];let o=0;for(let i=0;i<n.cols-1;i++){o+=n.colWidths[i]*n.a.width;const r=o,l=i;e.push(t(c,{x:r,points:[0,0,0,n.a.height],stroke:"transparent",strokeWidth:1,hitStrokeWidth:16,draggable:!0,onMouseEnter:t=>{var e;const o=null===(e=t.target.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor="col-resize"),M({kind:"colInsert",idx:l+1})},onMouseLeave:t=>{var e;const o=null===(e=t.target.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor=""),M(null)},onDragStart:t=>{t.cancelBubble=!0,d.history.startTransaction()},onDragMove:t=>{t.cancelBubble=!0;const e=t.target,o=(e.x()-r)/n.width;e.x(r),n.resizeColumn(l,o)},onDragEnd:t=>{t.cancelBubble=!0,t.target.x(r),d.history.endTransaction()}},`col-resize-${i}`))}return e})(),k&&!E&&!d.isPlaying&&(()=>{const e=[];let o=0;for(let i=0;i<n.rows-1;i++){o+=n.rowHeights[i]*n.a.height;const r=o,s=i;e.push(t(c,{y:r,points:[0,0,n.a.width,0],stroke:"transparent",strokeWidth:1,hitStrokeWidth:16,draggable:!0,onMouseEnter:t=>{var e;const o=null===(e=t.target.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor="row-resize"),M({kind:"rowInsert",idx:s+1})},onMouseLeave:t=>{var e;const o=null===(e=t.target.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor=""),M(null)},onDragStart:t=>{t.cancelBubble=!0,l(()=>W.set(!0)),d.history.startTransaction()},onDragMove:t=>{t.cancelBubble=!0;const e=t.target,o=(e.y()-r)/n.height;e.y(r),n.resizeRow(s,o)},onDragEnd:t=>{t.cancelBubble=!0,t.target.y(r),n._resetBaseRowHeights(),l(()=>W.set(!1)),d.history.endTransaction()}},`row-resize-${i}`))}return e})(),k&&!E&&!d.isPlaying&&t(C,{element:n,store:d,inv:I,isTouch:b,hover:H,setHover:M})]})]})});