polotno 3.6.0 → 3.8.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.
- package/LICENSE.md +22 -7
- package/README.md +1 -1
- package/blueprint/side-panel/text-panel.js +3 -3
- package/blueprint/toolbar/text-toolbar.js +2 -2
- package/blueprint.polotno.css +116 -116
- package/canvas/figure-element.js +1 -1
- package/canvas/gif-element.js +1 -1
- package/canvas/html-element.js +1 -1
- package/canvas/image-element.js +1 -1
- package/canvas/page.js +1 -1
- package/canvas/quill-font-formats.js +1 -1
- package/canvas/render-tag-element.js +1 -1
- package/canvas/rich-editor.d.ts +0 -4
- package/canvas/rich-editor.js +5 -2
- package/canvas/text-element.js +1 -1
- package/canvas/use-mediabunny-video.js +1 -1
- package/canvas/video-element.js +1 -1
- package/model/group-model.d.ts +2 -12
- package/model/image-model.js +1 -1
- package/model/page-model.d.ts +2 -12
- package/model/store.d.ts +18 -108
- package/model/store.js +1 -1
- package/model/table-model.d.ts +2 -12
- package/model/table-model.js +1 -1
- package/model/text-model.js +1 -1
- package/package.json +28 -28
- package/pages-timeline/audio-track.js +1 -1
- package/pages-timeline/current-time.js +1 -1
- package/pages-timeline/element-track.js +1 -1
- package/pages-timeline/page-preview.js +1 -1
- package/pages-timeline/pages-timeline.js +1 -1
- package/pages-timeline/pages.js +1 -1
- package/pages-timeline/play-button.js +1 -1
- package/pages-timeline/time-ruler.js +1 -1
- package/pages-timeline/track-styles.js +1 -1
- package/polotno.bundle.js +171 -168
- package/primitives/select.d.ts +1 -1
- package/side-panel/images-grid.d.ts +2 -1
- package/side-panel/images-grid.js +1 -1
- package/side-panel/select-svg.js +1 -1
- package/side-panel/text-panel.js +1 -1
- package/side-panel/text-template.d.ts +12 -0
- package/side-panel/text-template.js +1 -0
- package/toolbar/text-toolbar.js +1 -1
- package/toolbar/video-toolbar.js +1 -1
- package/ui.css +2 -2
- package/utils/font-metric.js +1 -1
- package/utils/fonts.js +1 -1
- package/utils/html-to-svg/font-inliner.js +1 -1
- package/utils/html2canvas.js +1 -1
- package/utils/measure-text.js +1 -1
- package/utils/pointer-drag.d.ts +13 -0
- package/utils/pointer-drag.js +1 -0
- package/utils/px.d.ts +2 -0
- package/utils/px.js +1 -1
- package/utils/rich-text-html.js +1 -1
- package/utils/text-format-state.js +1 -1
- package/utils/text-format.js +1 -1
- package/utils/validate-key.js +1 -1
package/canvas/figure-element.js
CHANGED
|
@@ -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
|
|
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})})})});
|
package/canvas/gif-element.js
CHANGED
|
@@ -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
|
|
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})]})});
|
package/canvas/html-element.js
CHANGED
|
@@ -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{scaledFontSizeAttrs as c,roundedFontSizeAttrs as f,roundedTextDecorationAttrs as d,textMinResizeWidth as h}from"../utils/px.js";import u from"konva";import{isAlive as g}from"mobx-state-tree";import{nodeAutorun as m}from"../utils/mobx.js";import{flags as x}from"../utils/flags.js";import{applyFilter as p}from"./apply-filters.js";import{getLineHeight as y,useFontFamiliesLoader as w,usePrevious as v}from"./text-element.js";import{getCurvePath as b}from"@polotno/core/text-html";import{useColor as E}from"./use-color.js";import{useWorkspaceStyle as S}from"./workspace-style.js";import{detectSize as O,detectLineRects as k,htmlToCanvas as z}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as F}from"@polotno/core/background-shape";import{useFadeIn as j}from"./use-fadein.js";import{Html as M}from"react-konva-utils";import{incrementLoader as R,triggerLoadError as X}from"../utils/loader.js";import{removeTags as Y}from"@polotno/core/text";import*as I from"../utils/fonts.js";import{isTouchDevice as C}from"../utils/screen.js";import{getLimitedFontSize as T}from"./text-element/max-font-size.js";import{visiblePlaceholder as A}from"./text-element/placeholder.js";import{useDelayer as P}from"./use-delayer.js";import{getHtml as B}from"@polotno/core/text-html";import{hasMeasuredHeight as W,resolveTextBoxHeight as $}from"@polotno/core/text-types";import{normalizeRichTextHtml as D,extractFontFamiliesFromHtml as H,scaleRichTextFontSizesInHtml as _}from"../utils/rich-text-html.js";import{findFitFontSize as L}from"./text-element/fit-font-size.js";import{RichEditor as J,RichTextContainer as N,applyOpacityToColor as q,quillRef as V,createQuill as G,setQuillContent as K,setQuillFormats as Q,syncQuillFormatState as U,setCursorFromCoords as Z}from"./rich-editor.js";export{V as quillRef,G as createQuill,K as setQuillContent,Q as setQuillFormats,U as syncQuillFormatState,Z as setCursorFromCoords};function tt(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 et=/^((?!chrome|android).)*safari/i.test(navigator.userAgent),ot=navigator.userAgent.includes("Firefox");export const HTMLElement=n(({element:n,store:V})=>{var G,K;const{textOverflowIndicatorStyle:Q}=S(),U=r.useRef(null),[Z,rt]=r.useState(),[nt,it]=r.useState(!1),[at,st]=r.useState(!1),lt=r.useRef(n.height),ct=n.isSelected,ft=v(ct),dt=n.fontSize/3,{textVerticalResizeEnabled:ht}=x,ut=x.autoDeleteEmptyText,gt=v(n.fontFamily),mt=r.useMemo(()=>D(n.text,{fontSize:n.fontSize,fontFamily:n.fontFamily}),[n.text,n.fontSize,n.fontFamily]),xt=r.useMemo(()=>[...new Set([n.fontFamily,...H(mt)])].filter(Boolean),[n.fontFamily,mt]),[pt]=w(V,xt,n.fontStyle,n.fontWeight,n.text||n.placeholder),yt=n._editModeEnabled;j(U);const wt=pt?n.fontFamily:gt!==n.fontFamily?gt:"Arial",vt=E(n).fill,bt=B(n,{fontFamily:wt,color:vt,textOverflow:x.textOverflow}),Et=r.useRef([]),St=r.useRef(!0),[,Ot]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=Et.current;t.length>0&&(Et.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{St.current=!1;const t=Et.current;t.length>0&&(Et.current=[],t.forEach(t=>t()))},[]);let{width:kt,height:zt}=function(t,e,o){return r.useMemo(()=>O(t),[t,e.width,o])}(bt,n,pt);r.useEffect(()=>{if(!pt){return}const t=(t,e,o,r)=>{const i=R(`text ${n.id}`),a=()=>{V.history.ignore(t,e,o).then(()=>{St.current?(Et.current.push(i),Ot()):i()})};r?queueMicrotask(a):a()};if(n.curveEnabled&&Math.abs(kt-n.width)>1&&!nt&&t(()=>{if(g(n)&&Math.abs(kt-n.width)>1){const t=n.x+n.width/2;n.set({width:kt,x:t-kt/2})}},!1,!0),!W(n)){return void t(()=>{g(n)&&(W(n)||n.set({height:zt}))},void 0,void 0,!0)}const{textOverflow:e}=x;if("change-font-size"!==e||nt||yt||n.curveEnabled){"resize"===e&&(ht&&n.height<zt&&!nt&&t(()=>{g(n)&&n.set({height:zt})},!1,!0),ht||n.height===zt||nt||t(()=>{g(n)&&n.set({height:zt})},!1,!0))}else{const e=L(n,{fontFamily:n.fontFamily,textOverflow:x.textOverflow});e.fontSize!==n.fontSize?t(()=>{if(!g(n)){return}const t=c(n,e.fontSize);void 0!==e.text&&e.text!==n.text&&(t.text=e.text),n.set(t)},!1,!0):n.height!==zt&&(ht&&n.height<zt?t(()=>{g(n)&&n.set({height:zt})},!1,!0):ht||t(()=>{g(n)&&n.set({height:zt})},!1,!0))}});const Ft=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 z(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),jt=r.useRef(0),Mt=r.useRef(null);r.useEffect(()=>{nt||yt||(async()=>{jt.current++;const t=jt.current;let e=R(`text ${n.id} ${t}`);Mt.current&&Mt.current(),Mt.current=e,st(!0);let o=null;const r=et?5:1;for(let a=0;a<r;a++){const e=a>0?Ft:z;try{if(o=await e({skipCache:a>0,html:bt,width:kt||n.width||1,height:$(n,zt)||1,fontFamily:wt,fontFamilies:H(bt),resolveFont:t=>V.fonts.find(e=>e.fontFamily===t)||I.globalFonts.find(e=>e.fontFamily===t),padding:dt,pixelRatio:V._elementsPixelRatio,font:V.fonts.find(t=>t.fontFamily===wt)||I.globalFonts.find(t=>t.fontFamily===wt)}),t!==jt.current){return}if((et||ot)&&o&&tt(o)){await new Promise(t=>setTimeout(t,50*(a+1)));continue}break}catch(i){console.error(i),X(`Error rendering rich text with id ${n.id}`);break}}o?rt(o):e?(e(),e=null):console.error("Finish function is called twice!"),st(!1)})()},[bt,nt,zt,yt,wt,n.height,V._elementsPixelRatio,pt]);const[Rt,Xt]=P(at,300),[Yt]=P(nt,300,!0),It=Yt||Rt;r.useEffect(()=>{var t;if(!It){return m(n,()=>{const t=U.current;p(t,n)})}null===(t=U.current)||void 0===t||t.clearCache()},[Z,It,n.shadowColor,n.shadowOffsetX,n.shadowOffsetY,n.shadowOpacity]),r.useEffect(()=>{Z&&!at&&Mt.current&&(Mt.current(),Mt.current=null)},[Z,at]),r.useLayoutEffect(()=>{if(!pt){return}if(!n.curveEnabled){return}const t=new u.TextPath({data:b(n.a.width,n.a.height,n.curvePower,n.a.fontSize),text:Y(n.text),letterSpacing:n.letterSpacing*n.a.fontSize,fontSize:n.a.fontSize,fontFamily: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(()=>{g(n)&&n.set({width:e,x:n.a.x+r,y:n.a.y+i})})}t.destroy()},[pt,n.curveEnabled,n.curvePower,n.text,n.fontSize,n.fontFamily,n.fontWeight,n.fontStyle,n.letterSpacing]),r.useEffect(()=>()=>{Mt.current&&Mt.current()},[]),r.useEffect(()=>{ft&&!ct&&ut&&""===Y(n.text)&&n.removable&&!A(n.placeholder)&&V.deleteElements([n.id])},[ut,n.placeholder,n.removable,n.text,ct,ft]);const Ct=$(n,zt);let Tt=0;"middle"===n.verticalAlign&&(Tt=(Ct-zt)/2),"bottom"===n.verticalAlign&&(Tt=Ct-zt);const At=y({fontLoaded:pt,fontFamily:n.fontFamily,fontSize:n.fontSize,lineHeight:n.lineHeight}),Pt=n.backgroundPadding*(n.fontSize*At*.5),Bt=n.backgroundCornerRadius*(n.fontSize*At*.5),Wt=r.useMemo(()=>{if(!n.backgroundEnabled||n.curveEnabled||n.legacyBackground){return""}const t=k(bt);if(0===t.length){return""}const e=t.map(t=>({left:t.left+dt,right:t.right+dt,top:t.top+dt,bottom:t.bottom+dt}));return F({rects:e,padding:Pt,cornerRadius:Bt})},[n.backgroundEnabled,n.curveEnabled,n.legacyBackground,bt,pt,Pt,Bt,dt]),$t=C(),Dt=b(n.a.width,zt,n.curvePower,n.fontSize),Ht=r.useRef(null),_t=r.useRef(n.width),Lt=r.useRef(null),Jt=r.useRef(0),Nt=r.useRef(0),qt=r.useRef(null),Vt=yt&&n.strokeWidth>0&&!n.curveEnabled,Gt=kt>0?(n.a.width+2*dt)/(kt+2*dt):1;return o(e,{children:[n.curveEnabled||n.legacyBackground?t(l,{x:n.a.x,y:n.a.y,offsetX:Pt/Gt,offsetY:Pt/Gt,rotation:n.a.rotation,scaleX:Gt,scaleY:Gt,hideInExport:!n.showInExport,listening:!1,visible:n.backgroundEnabled,opacity:n.backgroundOpacity*n.a.opacity,fill:n.backgroundColor,width:n.width+2*Pt,height:Ct+2*Pt,cornerRadius:Bt}):t(s,{name:"html-background",x:n.a.x,y:n.a.y,offsetX:dt/Gt,offsetY:(dt-Tt)/Gt,rotation:n.a.rotation,scaleX:Gt,scaleY:Gt,hideInExport:!n.showInExport,listening:!1,visible:n.backgroundEnabled&&Wt.length>0,opacity:n.backgroundOpacity*n.a.opacity,fill:n.backgroundColor,data:Wt}),t(s,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,data:Dt,stroke:Q.stroke,strokeWidth:1,visible:!1}),t(l,{ref:U,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:!It,draggable:$t?n.draggable&&ct:n.draggable,preventDefault:!$t||ct,opacity:yt?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&&(qt.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];qt.current=o?{x:o.clientX,y:o.clientY}:null,n.toggleEditMode(!0)}},onTransformStart:t=>{var e,o;it(!0),lt.current=n.height,_t.current=n.width,Ht.current=t.target.position();const r=null===(e=t.target.getStage())||void 0===e?void 0:e.findOne("Transformer");Lt.current=null!==(o=null==r?void 0:r.getActiveAnchor())&&void 0!==o?o:null,Jt.current=n.a.rotation,Nt.current=n.height},onTransform:t=>{var e;const o=t.target,r=(null===(e=o.getStage())||void 0===e?void 0:e.findOne("Transformer")).getActiveAnchor(),i="middle-left"===r||"middle-right"===r,a="top-center"===r||"bottom-center"===r,s=o.scaleX();if(i){const t=o.scaleX(),e=o.width()*t,r=h(_t.current,n.fontSize);let i=e;e<r&&(i=r,Ht.current&&o.position(Ht.current)),o.width(i),o.scaleX(1),o.scaleY(1);const a=B(Object.assign(Object.assign({},n.toJSON()),{width:i}),{fontFamily:wt,color:vt,textOverflow:x.textOverflow}),s=O(a).height,l=x.textVerticalResizeEnabled?Math.max(s,lt.current):s;o.height(l),n.set({width:o.width(),height:l,x:o.x(),y:o.y()})}else if(a){const t="resize"===x.textOverflow,e="number"==typeof n.lineHeight?n.lineHeight:1,r=t?zt:e*n.fontSize,i=o.height()*o.scaleY();if(i<r){if(o.height(r),Ht.current){if("top-center"===Lt.current){const t=Jt.current*Math.PI/180,e=Nt.current-r;o.x(Ht.current.x-e*Math.sin(t)),o.y(Ht.current.y+e*Math.cos(t))}else{o.position(Ht.current)}}}else{o.height(i)}o.scaleY(1),o.scaleX(1),Nt.current=Math.max(r,i),n.set({x:o.x(),y:o.y(),height:o.height(),rotation:o.rotation()})}else{o.scaleX(1),o.scaleY(1),n.set({fontSize:n.fontSize*s,letterSpacing:n.letterSpacing,width:o.width()*s,x:o.x(),y:o.y(),rotation:o.rotation(),height:o.height()*s,shadowBlur:n.shadowBlur*s,shadowOffsetX:n.shadowOffsetX*s,shadowOffsetY:n.shadowOffsetY*s,strokeWidth:n.strokeWidth*s,text:_(n.text,s)})}Ht.current=t.target.position()},onTransformEnd:t=>{it(!1),st(!0);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),n.set(Object.assign(Object.assign({},f(n,n.fontSize*e)),{width:t.target.width()*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),text:_(n.text,e)}))}}),t(a,{ref:U,image:Z,x:n.a.x,y:n.a.y,offsetX:dt,offsetY:dt-Tt,listening:!1,rotation:n.a.rotation,width:n.a.width+2*dt,height:(n.a.width+2*dt)*((null!==(G=null==Z?void 0:Z.height)&&void 0!==G?G:0)/((null==Z?void 0:Z.width)||1)||1),visible:!It&&!yt,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||nt||It}),(It&&!yt||Vt)&&t(i,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,offsetY:-Tt,children:t(M,{divProps:{style:{pointerEvents:"none"}},children:t(N,{dangerouslySetInnerHTML:{__html:bt},style:{pointerEvents:"none",opacity:n.a.opacity,textShadow:n.shadowEnabled?`${n.shadowOffsetX}px ${n.shadowOffsetY}px ${n.shadowBlur}px ${q(n.shadowColor,null!==(K=n.shadowOpacity)&&void 0!==K?K:1)}`:void 0}})})}),yt&&t(i,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,offsetY:-Tt,children:t(M,{divProps:{style:{zIndex:20}},children:t(J,{element:n,onChange:t=>{const e=T({oldText:Y(n.text),newText:Y(t),element:n});n.set(Object.assign({text:t},c(n,e)))},onBlur:t=>{g(n)&&(n.set(d(n)),n.toggleEditMode(!1),Xt(!0))},clickCoords:qt.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,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})})})]})});
|
package/canvas/image-element.js
CHANGED
|
@@ -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{Arc as a,Group as n,Image as h,Rect as s,Text as d,Transformer as c}from"react-konva";import{ROLES as g}from"../model/store.js";import l from"use-image";import u from"konva";import{Portal as p}from"react-konva-utils";import{useWorkspaceStyle as m}from"./workspace-style.js";import{incrementLoader as f,triggerLoadError as w}from"../utils/loader.js";import{polotnoError as x}from"@polotno/core/errors";import{getCropRect as M}from"@polotno/core/image";import*as y from"@polotno/core/svg";import{flags as v}from"../utils/flags.js";import{trySetCanvasSize as b}from"../utils/canvas.js";import{roundEnabledPx as E,isCornerAnchor as S,scaledBoxStyleAttrs as I,roundedBoxStyleAttrs as C}from"../utils/px.js";import{useFilteredSource as R}from"./use-filtered-source.js";import{useFadeIn as X}from"./use-fadein.js";import{isTouchDevice as Y}from"../utils/screen.js";import{useDelayer as O}from"./use-delayer.js";import{useColor as T}from"./use-color.js";function j(){return document.createElement("canvas")}const A=t=>t.indexOf("data:image/svg+xml")>=0||t.indexOf(".svg")>=0;export const useSizeFixer=t=>{const[e,r]=o.useState(t);return o.useEffect(()=>{(async()=>{const o=await async function(t){if(!A(t)){return t}const e=await y.urlToString(t),r=y.fixSize(e);return y.svgToURL(r)}(t);o!==e&&r(o)})()},[t]),e};function D(t,e){return{x:(t.x+e.x)/2,y:(t.y+e.y)/2}}const W=(t,e)=>e.width<5||e.height<5?t:e,z={keepRatio:!1,rotateEnabled:!1,boundBoxFunc:W},k={enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],rotateEnabled:!1,borderEnabled:!1,boundBoxFunc:W};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 L=(t,e,r)=>Math.max(e,Math.min(r,t));export const useCornerRadiusAndCrop=(t,e,r,i,a=0,n=!1,h=!0)=>{var s,d;const c=Math.floor(L(t.a.width*i,1,1e4)),g=Math.floor(L(t.a.height*i,1,1e4)),l=Math.min(a*i,c/2,g/2),p=Math.max(t.a.width/r.width,t.a.height/r.height)*i,m=(null===(s=t.page)||void 0===s?void 0:s._exportingOrRendering)&&v.imageDownScalingEnabled&&p<1&&!n,f=0===r.x&&0===r.y&&r.width===(null==e?void 0:e.width)&&r.height===(null==e?void 0:e.height),w=o.useMemo(()=>{if(e&&e.width&&e.height){return f&&0===l&&!m?void 0:j()}},[e,l,m,f]);return o.useLayoutEffect(()=>{if(!w||!e){return}b(w,c,g);const t=w.getContext("2d");if(!t){return}l&&(t.beginPath(),t.moveTo(l,0),t.lineTo(c-l,0),t.arc(c-l,l,l,3*Math.PI/2,0,!1),t.lineTo(c,g-l),t.arc(c-l,g-l,l,0,Math.PI/2,!1),t.lineTo(l,g),t.arc(l,g-l,l,Math.PI/2,Math.PI,!1),t.lineTo(0,l),t.arc(l,l,l,Math.PI,3*Math.PI/2,!1),t.clip());const o=m?function(t,e){var r,o;const i=j();i.width=t.width,i.height=t.height;const a=Math.max(1,Math.floor(i.width*e)),n=Math.max(1,Math.floor(i.height*e));null===(r=i.getContext("2d"))||void 0===r||r.drawImage(t,0,0,i.width,i.height);const h=function(t,e,r,o,i,a,n){for(var h=new ImageData(e,r),s=new Int32Array(t.data.buffer),d=t.width,c=new Int32Array(h.data.buffer),g=h.width,l=e/a,u=r/n,p=Math.round(1/l),m=Math.round(1/u),f=p*m,w=0;w<h.height;w++){for(var x=0;x<g;x++){for(var M=0+Math.round(x/l)+(0+Math.round(w/u))*d,y=0,v=0,b=0,E=0,S=0;S<m;S++){for(var I=0;I<p;I++){var C=s[M+I+S*d];y+=C<<24>>>24,v+=C<<16>>>24,b+=C<<8>>>24,E+=C>>>24}}y=Math.round(y/f),v=Math.round(v/f),b=Math.round(b/f),E=Math.round(E/f),c[x+w*g]=E<<24|b<<16|v<<8|y}}return h}(i.getContext("2d").getImageData(0,0,i.width,i.height),a,n,0,0,i.width,i.height);return i.width=a,i.height=n,null===(o=i.getContext("2d"))||void 0===o||o.putImageData(h,0,0),i}(e,p):e,i=m?{x:Math.floor(r.x*p),y:Math.floor(r.y*p),width:Math.floor(r.width*p),height:Math.floor(r.height*p)}:r;t.drawImage(o,i.x,i.y,i.width,i.height,0,0,w.width,w.height)},[w,t.a.width,t.a.height,r.x,r.y,r.width,r.height,a,i,n,null===(d=t.page)||void 0===d?void 0:d._exportingOrRendering,m]),o.useEffect(()=>()=>{w&&"CANVAS"===w.nodeName&&u.Util.releaseCanvas(w)},[w]),w||e};const H=j(),_=i(({element:r})=>{const i=Math.min(30,r.a.width/4,r.a.height/4),h=o.useRef(null);o.useEffect(()=>{const t=h.current;if(!t){return}const e=new u.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}});const{mediaLoadingStyle:d}=m();return e(n,{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(a,{ref:h,x:r.a.width/2,y:r.a.height/2,fill:d.textFill,outerRadius:Math.abs(i),innerRadius:Math.max(1,i-5),angle:270})]})}),P=i(({element:r})=>{const{mediaErrorStyle:o}=m(),i=Math.max(10,Math.min(30,r.a.width/25));return e(n,{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: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 F=l;export const setImageLoaderHook=t=>{F=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=f(n)),"loading"!==t&&a(),"failed"===t&&w(x("IMAGE_FAILED",n,{src:o,elementId:r,reason:"load"}))},[t])};export const ImageElement=i(({element:i,store:a})=>{var d,l,f;const w=m(),[x,v]=o.useState(!1),W=o.useRef(null),L=o.useRef(null),N=i.isSelected,[B,U]=(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(()=>A(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 y.urlToString(t.src),n=y.fixSize(o);let h;h=t.colorsReplace?y.replaceColors(n,t.colorsReplace||new Map):y.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),[V,$]=F(B,"anonymous"),q="svg"!==i.type||"loaded"===U?$:"loading";useImageLoader(q,i.src,i.id);const G=(null===(d=i.page)||void 0===d?void 0:d._exportingOrRendering)?1:Math.max(1,a.scale),J=a._elementsPixelRatio*G,K=(({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:V,status:$,type:i.type}),Q=((t,e,r)=>{const i=o.useMemo(()=>{var o,i;const{flipX:a,flipY:n}=t,h="svg"===t.type||t.src.indexOf("data:image/svg+xml")>=0||t.src.indexOf(".svg")>=0,s=navigator.userAgent.toLowerCase().indexOf("firefox")>-1,d=(/^((?!chrome|android).)*safari/i.test(navigator.userAgent)||s)&&h||t.maskSrc;if(!a&&!n&&!d){return e}if(!e||!e.width||!e.height){return null}const c=j();let g=1;"svg"===t.type&&(g=Math.max(t.a.width/e.width*r,t.a.height/e.height*r)),b(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===(o=c.getContext("2d"))||void 0===o||o.scale(a?-1:1,n?-1:1),null===(i=c.getContext("2d"))||void 0===i||i.drawImage(e,l,u,c.width,c.height),c},[t.maskSrc,t.flipX,t.flipY,e,t.a.width,t.a.height,r]);return o.useEffect(()=>()=>{i&&"CANVAS"===i.nodeName&&u.Util.releaseCanvas(i)},[i]),i})(i,V||K,J),Z=((t,e)=>{const r=useSizeFixer(t.maskSrc||""),[i,a]=F(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=j();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,Q)||H,[tt=H]=R(i,Z);let{cropX:et,cropY:rt,cropWidth:ot,cropHeight:it}=i.a;"loaded"!==$&&(et=rt=0,ot=it=1);const at=M({stretchEnabled:i.stretchEnabled,cropX:et,cropY:rt,cropWidth:ot,cropHeight:it,width:i.a.width,height:i.a.height},tt),nt=at.width,ht=at.height,st=i.stretchEnabled,dt=null!==(l=i.cornerRadius)&&void 0!==l?l:0,ct=(null===(f=i.page)||void 0===f?void 0:f._exportingOrRendering)?1:Math.min(2,a.scale),gt=a._elementsPixelRatio*ct;let lt=((t,e,r,i)=>{const a=useSizeFixer(t.clipSrc||""),[n,h]=F(a,"anonymous"),s=t.clipSrc?h:"loaded";useImageLoader(s,t.clipSrc,t.id);const d=o.useMemo(()=>{if(e&&n){return j()}},[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(!d){return}const i=j(),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),d.width=Math.max(e.width,1),d.height=Math.max(e.height,1);const h=d.getContext("2d");h&&(h.save(),h.drawImage(i,0,0,e.width,e.height),u.Util.releaseCanvas(i),h.globalCompositeOperation="source-in",h.drawImage(e,0,0,d.width,d.height),h.restore())},[d,e,n,t.a.width,t.a.height,r,...i]),t.clipSrc&&n?d:e})(i,useCornerRadiusAndCrop(i,tt,at,gt,dt,x||i._cropModeEnabled||"svg"===i.type,!0),gt,[at,dt,gt]);const ut=Math.max(i.a.width/nt,i.a.height/ht);o.useEffect(()=>{var t;if(!i._cropModeEnabled){return}const e=null===(t=W.current)||void 0===t?void 0:t.getStage();function r(t){i._cropModeEnabled&&t.target!==L.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 pt=o.useRef(null),mt=o.useRef(null),ft=o.useRef(null);o.useLayoutEffect(()=>{i._cropModeEnabled&&(mt.current.nodes([pt.current]),ft.current.nodes([L.current]))},[i._cropModeEnabled]);var wt=o.useRef(null),xt=o.useRef(0),Mt=o.useRef(!1);const yt=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,nt/r),n=Math.min(1,ht/o),h=1-a,s=Math.min(h,Math.max(0,Math.round(-t.target.x())/r)),d=1-n,c=Math.min(d,Math.max(0,Math.round(-t.target.y())/o));t.target.setAttrs({x:-s*tt.width,y:-c*tt.height,scaleX:1,scaleY:1}),i.set({cropX:s,cropY:c,cropWidth:a,cropHeight:n})},vt=()=>{"svg"!==i.type&&i.contentEditable&&(i.stretchEnabled||setTimeout(()=>{i.toggleCropMode(!0)}))},bt="svg"===i.type&&K,Et="loading"===$&&!bt,[St]=O(Et,100,!1,!1),It="failed"===$,Ct=!St&&!It,Rt=o.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Xt=Ct?i.a.opacity:0;X(W,Xt);const Yt=i.selectable||a.role===g.ADMIN,Ot=Y(),Tt=T(i,i.borderColor,"stroke");return e(r,{children:[St&&t(_,{element:i}),It&&t(P,{element:i}),t(h,{ref:W,name:"element",id:i.id,image:lt,x:i.a.x,y:i.a.y,width:i.a.width||1,height:i.a.height||1,rotation:i.a.rotation,opacity:Xt,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,customCrop:at,listening:Yt,draggable:Ot?i.draggable&&N:i.draggable,preventDefault:!Ot||N,hideInExport:!i.showInExport,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDblClick:vt,onDblTap:vt,onTransformStart:()=>{v(!0),Rt.current={cropX:i.cropX,cropY:i.cropY,cropWidth:i.cropWidth,cropHeight:i.cropHeight}},onTransform:t=>{var e,r;const o=t.currentTarget,a=Math.abs(o.scaleX()-1)<1e-7?1:o.scaleX(),n=Math.abs(o.scaleY()-1)<1e-7?1:o.scaleY();o.scaleX(1),o.scaleY(1);const h=null===(e=t.target.getStage())||void 0===e?void 0:e.findOne("Transformer"),s=1-nt/tt.width,d=Math.min(s,Math.max(0,i.cropX)),c=1-ht/tt.height,g=Math.min(c,Math.max(0,i.cropY)),l=S(null!==(r=h.getActiveAnchor())&&void 0!==r?r:""),u=!l&&a<1&&Rt.current.cropHeight>ht/tt.height;let p=l?i.cropWidth:i.cropWidth*a;u&&(p=i.cropWidth);const m=!l&&n<1&&Rt.current.cropWidth>nt/tt.width;let f=l?i.cropHeight:i.cropHeight*n;m&&(f=i.cropHeight),st&&(p=i.cropWidth,f=i.cropHeight);const w=l?Math.min(a,n):1;i.set(Object.assign({cropX:d,cropY:g,x:o.x(),y:o.y(),width:o.width()*a,height:o.height()*n,rotation:t.target.rotation(),cropWidth:Math.min(p,1-d),cropHeight:Math.min(f,1-g)},1!==w&&Object.assign(Object.assign({},I(i,w)),{borderSize:i.borderSize*w})))},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:nt/tt.width,cropHeight:ht/tt.height},C(i)),{borderSize:E(i.borderSize)})),v(!1)}}),t(s,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:Xt,offsetX:-i.borderSize/2,offsetY:-i.borderSize/2},Tt,{strokeWidth:i.borderSize,listening:!1,visible:!!i.borderSize,rotation:i.rotation,cornerRadius:Math.max(0,dt-i.borderSize),hideInExport:!i.showInExport})),i._cropModeEnabled&&e(p,{selector:".page-abs-container",enabled:!0,children:[t(s,{x:-window.innerWidth/a.scale,y:-window.innerWidth/a.scale,width:window.innerWidth/a.scale*3,height:window.innerWidth/a.scale*3,fill:w.cropOverlayFill}),t(h,{listening:!1,image:lt,x:i.x,y:i.y,width:i.a.width,height:i.a.height,rotation:i.rotation,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur}),e(n,{x:i.x,y:i.y,rotation:i.rotation,scaleX:ut,scaleY:ut,children:[t(h,{image:tt,ref:L,opacity:.4,draggable:!0,x:-i.cropX*tt.width,y:-i.cropY*tt.height,onDragMove:yt,onTransform:yt,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()&&Mt.current&&(i.startDrag(),Mt.current=!1),r&&o){u.hitOnDragEnabled=!0,i.isDragging()&&(Mt.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(!wt.current){return void(wt.current=D(a,n))}var h=D(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);xt.current||(xt.current=s);const m=i.position();var d={x:h.x-m.x,y:h.y-m.y},c=s/xt.current;i.scaleX(c),i.scaleY(c);const f=e.point(h),w=e.point(wt.current);var g=f.x-w.x,l=f.y-w.y,p={x:Math.min(0,h.x-d.x*c+g),y:Math.min(0,h.y-d.y*c+l)};i.position(p),xt.current=s,wt.current=h,yt(t)}},onTouchEnd:t=>{xt.current=0,wt.current=null,u.hitOnDragEnabled=!1}}),t(c,Object.assign({ref:ft},k,w.outerImageCropTransformerStyle)),t(s,{width:nt,height:ht,ref:pt,listening:!1,onTransform:t=>{if(i.cropX<Math.abs(t.target.x()/tt.width)&&t.target.x()<0&&i.cropX>0){const e=(i.cropWidth+i.cropX)*tt.width;t.target.scaleX(1),t.target.width(e)}if(i.cropY<Math.abs(t.target.y()/tt.height)&&t.target.y()<0&&i.cropY>0){const e=(i.cropHeight+i.cropY)*tt.height;t.target.scaleY(1),t.target.height(e)}t.target.x()<-i.cropX*tt.width-1e-9&&(t.target.x(-i.cropX*tt.width),t.target.scaleX(1)),t.target.y()<-i.cropY*tt.height-1e-9&&(t.target.y(-i.cropY*tt.height),t.target.scaleY(1));const e=Math.min(1,Math.max(0,i.cropX+t.target.x()/tt.width)),r=Math.min(1,Math.max(0,t.target.y()/tt.height+i.cropY)),o=t.target.width()*t.target.scaleX(),a=t.target.height()*t.target.scaleY(),n=Math.min(1-e,o/tt.width),h=Math.min(1-r,a/tt.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*ut,tt.width*(1-e)*ut),height:Math.min(a*ut,tt.height*(1-r)*ut)})}}),t(c,Object.assign({ref:mt},z,w.innerImageCropTransformerStyle,{visible:i.resizable}))]})]})]})});
|
|
1
|
+
import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import o from"react";import{observer as i}from"mobx-react-lite";import{isAlive as a}from"mobx-state-tree";import{Arc as n,Group as h,Image as s,Rect as c,Text as d,Transformer as g}from"react-konva";import{ROLES as l}from"../model/store.js";import u from"use-image";import p from"konva";import{Portal as m}from"react-konva-utils";import{useWorkspaceStyle as f}from"./workspace-style.js";import{incrementLoader as w,triggerLoadError as x}from"../utils/loader.js";import{polotnoError as M}from"@polotno/core/errors";import{getCropRect as y}from"@polotno/core/image";import*as v from"@polotno/core/svg";import{flags as b}from"../utils/flags.js";import{trySetCanvasSize as E}from"../utils/canvas.js";import{roundEnabledPx as S,activeAnchorOf as I,isCornerAnchor as C,scaledBoxStyleAttrs as R,roundedBoxStyleAttrs as X}from"../utils/px.js";import{useFilteredSource as Y}from"./use-filtered-source.js";import{useFadeIn as O}from"./use-fadein.js";import{isTouchDevice as T}from"../utils/screen.js";import{useDelayer as j}from"./use-delayer.js";import{useColor as D}from"./use-color.js";function W(){return document.createElement("canvas")}export const useSizeFixer=t=>{const[e,r]=o.useState(t);return o.useEffect(()=>{(async()=>{const o=await async function(t){if(!v.isSvgUrl(t)){return t}const e=await v.urlToString(t),r=v.fixSize(e);return v.svgToURL(r)}(t);o!==e&&r(o)})()},[t]),e};function z(t,e){return{x:(t.x+e.x)/2,y:(t.y+e.y)/2}}const k=(t,e)=>e.width<5||e.height<5?t:e,A={keepRatio:!1,rotateEnabled:!1,boundBoxFunc:k},L={enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],rotateEnabled:!1,borderEnabled:!1,boundBoxFunc:k};export function maskCompositeSize(t,e){const r=y({stretchEnabled:e.stretchEnabled,cropX:0,cropY:0,cropWidth:1,cropHeight:1,width:Math.max(e.width,1),height:Math.max(e.height,1)},t);return{width:Math.max(Math.round(r.width),1),height:Math.max(Math.round(r.height),1)}}const H=(t,e,r)=>Math.max(e,Math.min(r,t));export const useCornerRadiusAndCrop=(t,e,r,i,a=0,n=!1,h=!0)=>{var s,c;const d=Math.floor(H(t.a.width*i,1,1e4)),g=Math.floor(H(t.a.height*i,1,1e4)),l=Math.min(a*i,d/2,g/2),u=Math.max(t.a.width/r.width,t.a.height/r.height)*i,m=(null===(s=t.page)||void 0===s?void 0:s._exportingOrRendering)&&b.imageDownScalingEnabled&&u<1&&!n,f=0===r.x&&0===r.y&&r.width===(null==e?void 0:e.width)&&r.height===(null==e?void 0:e.height),w=o.useMemo(()=>{if(e&&e.width&&e.height){return f&&0===l&&!m?void 0:W()}},[e,l,m,f]);return o.useLayoutEffect(()=>{if(!w||!e){return}E(w,d,g);const t=w.getContext("2d");if(!t){return}l&&(t.beginPath(),t.moveTo(l,0),t.lineTo(d-l,0),t.arc(d-l,l,l,3*Math.PI/2,0,!1),t.lineTo(d,g-l),t.arc(d-l,g-l,l,0,Math.PI/2,!1),t.lineTo(l,g),t.arc(l,g-l,l,Math.PI/2,Math.PI,!1),t.lineTo(0,l),t.arc(l,l,l,Math.PI,3*Math.PI/2,!1),t.clip());const o=m?function(t,e){var r,o;const i=W();i.width=t.width,i.height=t.height;const a=Math.max(1,Math.floor(i.width*e)),n=Math.max(1,Math.floor(i.height*e));null===(r=i.getContext("2d"))||void 0===r||r.drawImage(t,0,0,i.width,i.height);const h=function(t,e,r,o,i,a,n){for(var h=new ImageData(e,r),s=new Int32Array(t.data.buffer),c=t.width,d=new Int32Array(h.data.buffer),g=h.width,l=e/a,u=r/n,p=Math.round(1/l),m=Math.round(1/u),f=p*m,w=0;w<h.height;w++){for(var x=0;x<g;x++){for(var M=0+Math.round(x/l)+(0+Math.round(w/u))*c,y=0,v=0,b=0,E=0,S=0;S<m;S++){for(var I=0;I<p;I++){var C=s[M+I+S*c];y+=C<<24>>>24,v+=C<<16>>>24,b+=C<<8>>>24,E+=C>>>24}}y=Math.round(y/f),v=Math.round(v/f),b=Math.round(b/f),E=Math.round(E/f),d[x+w*g]=E<<24|b<<16|v<<8|y}}return h}(i.getContext("2d").getImageData(0,0,i.width,i.height),a,n,0,0,i.width,i.height);return i.width=a,i.height=n,null===(o=i.getContext("2d"))||void 0===o||o.putImageData(h,0,0),i}(e,u):e,i=m?{x:Math.floor(r.x*u),y:Math.floor(r.y*u),width:Math.floor(r.width*u),height:Math.floor(r.height*u)}:r;t.drawImage(o,i.x,i.y,i.width,i.height,0,0,w.width,w.height)},[w,t.a.width,t.a.height,r.x,r.y,r.width,r.height,a,i,n,null===(c=t.page)||void 0===c?void 0:c._exportingOrRendering,m]),o.useEffect(()=>()=>{w&&"CANVAS"===w.nodeName&&p.Util.releaseCanvas(w)},[w]),w||e};const _=W(),P=i(({element:r})=>{const i=Math.min(30,r.a.width/4,r.a.height/4),a=o.useRef(null);o.useEffect(()=>{const t=a.current;if(!t){return}const e=new p.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}});const{mediaLoadingStyle:s}=f();return e(h,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:s.fill}),t(n,{ref:a,x:r.a.width/2,y:r.a.height/2,fill:s.textFill,outerRadius:Math.abs(i),innerRadius:Math.max(1,i-5),angle:270})]})}),F=i(({element:r})=>{const{mediaErrorStyle:o}=f(),i=Math.max(10,Math.min(30,r.a.width/25));return e(h,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:o.fill}),t(d,{text:"Can not load the image...",fontSize:i,width:r.a.width,height:r.a.height,align:"center",fill:o.textFill,verticalAlign:"middle",padding:5})]})});let U=u;export const setImageLoaderHook=t=>{U=t};export const useImageLoader=(t,e="",r="")=>{const i=o.useRef(void 0),a=()=>{var t;null===(t=i.current)||void 0===t||t.call(i),i.current=void 0};o.useEffect(()=>a,[]),o.useLayoutEffect(()=>{const o=e.slice(0,200),n=`image with id ${r} url: ${o}`;"loading"!==t||i.current||(i.current=w(n)),"loading"!==t&&a(),"failed"===t&&x(M("IMAGE_FAILED",n,{src:o,elementId:r,reason:"load"}))},[t])};export const ImageElement=i(({element:i,store:n})=>{var d,u,w;const x=f(),[M,b]=o.useState(!1),k=o.useRef(null),H=o.useRef(null),N=i.isSelected,[B,V]=(t=>{const[e,r]=o.useReducer(t=>t+1,0),i=o.useRef("loading"),a=o.useRef(t.src),n=o.useRef(t.src);n.current!==t.src&&(n.current=t.src,i.current="loading");const h=o.useMemo(()=>v.isSvgUrl(t.src)||"svg"===t.type,[t.src,t.type]);return o.useEffect(()=>{if(!h){return}if(!t.src){return}let e=!1;return(async()=>{i.current="loading",r();const o=await v.urlToString(t.src),n=v.fixSize(o);let h;h=t.colorsReplace?v.replaceColors(n,t.colorsReplace||new Map):v.svgToURL(n),e||(a.current=h,i.current="loaded",r())})(),()=>{e=!0}},[t.src,JSON.stringify(t.colorsReplace)]),h?[a.current,i.current]:[t.src,"loaded"]})(i),[$,q]=U(B,"anonymous"),G="svg"!==i.type||"loaded"===V?q:"loading";useImageLoader(G,i.src,i.id);const J=(null===(d=i.page)||void 0===d?void 0:d._exportingOrRendering)?1:Math.max(1,n.scale),K=n._elementsPixelRatio*J,Q=(({image:t,status:e,type:r})=>{const i=o.useRef(void 0);return o.useEffect(()=>{i.current=t||i.current},[t]),"failed"!==e||"failed"!==e&&"svg"===r?i.current:void 0})({image:$,status:q,type:i.type}),Z=((t,e,r)=>{const i=o.useMemo(()=>{var o,i;const{flipX:a,flipY:n}=t,h="svg"===t.type||v.isSvgUrl(t.src),s=navigator.userAgent.toLowerCase().indexOf("firefox")>-1,c=(/^((?!chrome|android).)*safari/i.test(navigator.userAgent)||s)&&h||t.maskSrc;if(!a&&!n&&!c){return e}if(!e||!e.width||!e.height){return null}const d=W();let g=1;"svg"===t.type&&(g=Math.max(t.a.width/e.width*r,t.a.height/e.height*r)),E(d,Math.max(e.width*g,1),Math.max(e.height*g,1));let l=a?-d.width:0,u=n?-d.height:0;return null===(o=d.getContext("2d"))||void 0===o||o.scale(a?-1:1,n?-1:1),null===(i=d.getContext("2d"))||void 0===i||i.drawImage(e,l,u,d.width,d.height),d},[t.maskSrc,t.flipX,t.flipY,e,t.a.width,t.a.height,r]);return o.useEffect(()=>()=>{i&&"CANVAS"===i.nodeName&&p.Util.releaseCanvas(i)},[i]),i})(i,$||Q,K),tt=((t,e)=>{const r=useSizeFixer(t.maskSrc||""),[i,a]=U(r,"anonymous");return useImageLoader(r?a:"loaded",r||"",t.id),o.useMemo(()=>{if(!i){return e}if(!e||!e.width||!e.height){return e}const{width:r,height:o}=maskCompositeSize(e,{width:t.a.width,height:t.a.height,stretchEnabled:t.stretchEnabled}),a=W();a.width=r,a.height=o;const n=a.getContext("2d");if(!n){return e}n.drawImage(e,0,0,r,o,0,0,r,o),n.globalCompositeOperation="source-in";const h=function(t,e){const r=e.width/e.height;let o,i;return r>=t.width/t.height?(o=t.width,i=t.width/r):(o=t.height*r,i=t.height),{x:(t.width-o)/2,y:(t.height-i)/2,width:o,height:i}}(i,t);return n.drawImage(i,h.x,h.y,h.width,h.height,0,0,r,o),a},[e,i,t.a.width,t.a.height,t.stretchEnabled])})(i,Z)||_,[et=_]=Y(i,tt);let{cropX:rt,cropY:ot,cropWidth:it,cropHeight:at}=i.a;"loaded"!==q&&(rt=ot=0,it=at=1);const nt=y({stretchEnabled:i.stretchEnabled,cropX:rt,cropY:ot,cropWidth:it,cropHeight:at,width:i.a.width,height:i.a.height},et),ht=nt.width,st=nt.height,ct=i.stretchEnabled,dt=null!==(u=i.cornerRadius)&&void 0!==u?u:0,gt=(null===(w=i.page)||void 0===w?void 0:w._exportingOrRendering)?1:Math.min(2,n.scale),lt=n._elementsPixelRatio*gt;let ut=((t,e,r,i)=>{const a=useSizeFixer(t.clipSrc||""),[n,h]=U(a,"anonymous"),s=t.clipSrc?h:"loaded";useImageLoader(s,t.clipSrc,t.id);const c=o.useMemo(()=>{if(e&&n){return W()}},[e,n]);return o.useLayoutEffect(()=>{var o;if(!n){return}if(!e||!e.width||!e.height){return}if(!n||!n.width||!n.height){return}if(!c){return}const i=W(),a=Math.max(t.a.width/n.width*r,t.a.height/n.height*r);i.width=Math.max(n.width*a,1),i.height=Math.max(n.height*a,1),null===(o=i.getContext("2d"))||void 0===o||o.drawImage(n,0,0,i.width,i.height),c.width=Math.max(e.width,1),c.height=Math.max(e.height,1);const h=c.getContext("2d");h&&(h.save(),h.drawImage(i,0,0,e.width,e.height),p.Util.releaseCanvas(i),h.globalCompositeOperation="source-in",h.drawImage(e,0,0,c.width,c.height),h.restore())},[c,e,n,t.a.width,t.a.height,r,...i]),t.clipSrc&&n?c:e})(i,useCornerRadiusAndCrop(i,et,nt,lt,dt,M||i._cropModeEnabled||"svg"===i.type,!0),lt,[nt,dt,lt]);const pt=Math.max(i.a.width/ht,i.a.height/st);o.useEffect(()=>{var t;if(!i._cropModeEnabled){return}const e=null===(t=k.current)||void 0===t?void 0:t.getStage();function r(t){i._cropModeEnabled&&t.target!==H.current&&i.toggleCropMode(!1)}function o(t){i._cropModeEnabled&&t.target.parentNode!==(null==e?void 0:e.content)&&i.toggleCropMode(!1)}return document.body.addEventListener("click",o),null==e||e.on("click",r),null==e||e.on("tap",r),()=>{document.body.removeEventListener("click",o),document.body.removeEventListener("touchstart",o),null==e||e.off("click",r),null==e||e.off("click",r)}},[i._cropModeEnabled]);const mt=o.useRef(null),ft=o.useRef(null),wt=o.useRef(null);o.useLayoutEffect(()=>{i._cropModeEnabled&&(ft.current.nodes([mt.current]),wt.current.nodes([H.current]))},[i._cropModeEnabled]);var xt=o.useRef(null),Mt=o.useRef(0),yt=o.useRef(!1);const vt=t=>{var e;(null===(e=t.evt.touches)||void 0===e?void 0:e.length)>2&&t.target.stopDrag(),Math.round(t.target.x())>0&&(t.target.x(0),t.target.scaleX(1)),Math.round(t.target.y())>0&&(t.target.y(0),t.target.scaleY(1));const r=t.target.width()*t.target.scaleX(),o=t.target.height()*t.target.scaleY(),a=Math.min(1,ht/r),n=Math.min(1,st/o),h=1-a,s=Math.min(h,Math.max(0,Math.round(-t.target.x())/r)),c=1-n,d=Math.min(c,Math.max(0,Math.round(-t.target.y())/o));t.target.setAttrs({x:-s*et.width,y:-d*et.height,scaleX:1,scaleY:1}),i.set({cropX:s,cropY:d,cropWidth:a,cropHeight:n})},bt=()=>{"svg"!==i.type&&i.contentEditable&&(i.stretchEnabled||setTimeout(()=>{a(i)&&i.toggleCropMode(!0)}))},Et="svg"===i.type&&Q,St="loading"===q&&!Et,[It]=j(St,100,!1,!1),Ct="failed"===q,Rt=!It&&!Ct,Xt=o.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Yt=o.useRef(!1),Ot=Rt?i.a.opacity:0;O(k,Ot);const Tt=i.selectable||n.role===l.ADMIN,jt=T(),Dt=D(i,i.borderColor,"stroke");return e(r,{children:[It&&t(P,{element:i}),Ct&&t(F,{element:i}),t(s,{ref:k,name:"element",id:i.id,image:ut,x:i.a.x,y:i.a.y,width:i.a.width||1,height:i.a.height||1,rotation:i.a.rotation,opacity:Ot,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,customCrop:nt,listening:Tt,draggable:jt?i.draggable&&N:i.draggable,preventDefault:!jt||N,hideInExport:!i.showInExport,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDblClick:bt,onDblTap:bt,onTransformStart:t=>{b(!0),Yt.current=C(I(t.target)),Xt.current={cropX:i.cropX,cropY:i.cropY,cropWidth:i.cropWidth,cropHeight:i.cropHeight}},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),o=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const a=1-ht/et.width,n=Math.min(a,Math.max(0,i.cropX)),h=1-st/et.height,s=Math.min(h,Math.max(0,i.cropY)),c=Yt.current,d=!c&&r<1&&Xt.current.cropHeight>st/et.height;let g=c?i.cropWidth:i.cropWidth*r;d&&(g=i.cropWidth);const l=!c&&o<1&&Xt.current.cropWidth>ht/et.width;let u=c?i.cropHeight:i.cropHeight*o;l&&(u=i.cropHeight),ct&&(g=i.cropWidth,u=i.cropHeight);const p=c?Math.min(r,o):1;i.set(Object.assign({cropX:n,cropY:s,x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*o,rotation:t.target.rotation(),cropWidth:Math.min(g,1-n),cropHeight:Math.min(u,1-s)},1!==p&&Object.assign(Object.assign({},R(i,p)),{borderSize:i.borderSize*p})))},onTransformEnd:t=>{const e=t.currentTarget;i.set(Object.assign(Object.assign({width:e.width(),height:e.height(),x:e.x(),y:e.y(),rotation:t.target.rotation(),cropWidth:ht/et.width,cropHeight:st/et.height},X(i)),{borderSize:S(i.borderSize)})),b(!1)}}),t(c,Object.assign({x:i.x,y:i.y,width:Math.max(i.a.width-i.borderSize,0),height:Math.max(i.a.height-i.borderSize,0),opacity:Ot,offsetX:-i.borderSize/2,offsetY:-i.borderSize/2},Dt,{strokeWidth:i.borderSize,listening:!1,visible:!!i.borderSize,rotation:i.rotation,cornerRadius:Math.max(0,dt-i.borderSize),hideInExport:!i.showInExport})),i._cropModeEnabled&&e(m,{selector:".page-abs-container",enabled:!0,children:[t(c,{x:-window.innerWidth/n.scale,y:-window.innerWidth/n.scale,width:window.innerWidth/n.scale*3,height:window.innerWidth/n.scale*3,fill:x.cropOverlayFill}),t(s,{listening:!1,image:ut,x:i.x,y:i.y,width:i.a.width,height:i.a.height,rotation:i.rotation,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur}),e(h,{x:i.x,y:i.y,rotation:i.rotation,scaleX:pt,scaleY:pt,children:[t(s,{image:et,ref:H,opacity:.4,draggable:!0,x:-i.cropX*et.width,y:-i.cropY*et.height,onDragMove:vt,onTransform:vt,onTouchMove:t=>{t.evt.preventDefault();const e=t.target.getStage().getPointersPositions();var r=e[0],o=e[1];const i=t.target;if(r&&!o&&!i.isDragging()&&yt.current&&(i.startDrag(),yt.current=!1),r&&o){p.hitOnDragEnabled=!0,i.isDragging()&&(yt.current=!0,i.stopDrag());const e=t.target.getAbsoluteTransform().copy();e.invert();var a={x:r.x,y:r.y},n={x:o.x,y:o.y};if(!xt.current){return void(xt.current=z(a,n))}var h=z(a,n),s=function(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}(a,n);Mt.current||(Mt.current=s);const m=i.position();var c={x:h.x-m.x,y:h.y-m.y},d=s/Mt.current;i.scaleX(d),i.scaleY(d);const f=e.point(h),w=e.point(xt.current);var g=f.x-w.x,l=f.y-w.y,u={x:Math.min(0,h.x-c.x*d+g),y:Math.min(0,h.y-c.y*d+l)};i.position(u),Mt.current=s,xt.current=h,vt(t)}},onTouchEnd:t=>{Mt.current=0,xt.current=null,p.hitOnDragEnabled=!1}}),t(g,Object.assign({ref:wt},L,x.outerImageCropTransformerStyle)),t(c,{width:ht,height:st,ref:mt,listening:!1,onTransform:t=>{if(i.cropX<Math.abs(t.target.x()/et.width)&&t.target.x()<0&&i.cropX>0){const e=(i.cropWidth+i.cropX)*et.width;t.target.scaleX(1),t.target.width(e)}if(i.cropY<Math.abs(t.target.y()/et.height)&&t.target.y()<0&&i.cropY>0){const e=(i.cropHeight+i.cropY)*et.height;t.target.scaleY(1),t.target.height(e)}t.target.x()<-i.cropX*et.width-1e-9&&(t.target.x(-i.cropX*et.width),t.target.scaleX(1)),t.target.y()<-i.cropY*et.height-1e-9&&(t.target.y(-i.cropY*et.height),t.target.scaleY(1));const e=Math.min(1,Math.max(0,i.cropX+t.target.x()/et.width)),r=Math.min(1,Math.max(0,t.target.y()/et.height+i.cropY)),o=t.target.width()*t.target.scaleX(),a=t.target.height()*t.target.scaleY(),n=Math.min(1-e,o/et.width),h=Math.min(1-r,a/et.height),s=t.target.getAbsolutePosition(t.target.getParent().getParent());t.target.scale({x:1,y:1}),t.target.position({x:0,y:0}),i.set({x:s.x,y:s.y,cropX:e,cropY:r,cropWidth:n,cropHeight:h,width:Math.min(o*pt,et.width*(1-e)*pt),height:Math.min(a*pt,et.height*(1-r)*pt)})}}),t(g,Object.assign({ref:ft},A,x.innerImageCropTransformerStyle,{visible:i.resizable}))]})]})]})});
|
package/canvas/page.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=this&&this.__rest||function(e,t){var n={};for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++){t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(n[i[o]]=e[i[o]])}}return n};import{jsx as t,jsxs as n,Fragment as i}from"react/jsx-runtime";import o from"react";import{observer as r,useLocalObservable as l}from"mobx-react-lite";import{action as s,runInAction as a}from"mobx";import{ZERO_BLEED as d}from"@polotno/schema";import{Group as c,Image as h,Label as m,Layer as u,Line as g,Rect as f,Shape as p,Stage as x,Tag as v,Text as b,Transformer as y,KonvaRenderer as w}from"react-konva";import E from"use-image";import{useWorkspaceStyle as X}from"./workspace-style.js";import{ROLES as Y}from"../model/store.js";import S from"konva";import k from"./element.js";import{DrawingLayer as A}from"./drawing-layer.js";import{ensureDragOrder as M,useSnap as I}from"./use-transformer-snap.js";import{useImageLoader as O}from"./image-element.js";import{getCrop as P}from"../utils/crop.js";import{___ as R,____ as j,isCreditVisible as D}from"../utils/validate-key.js";import{getClientRect as C,getCorners as T,getTotalClientRect as B,polygonsIntersect as W}from"../utils/math.js";import{pxToUnitRounded as G,pxToUnitString as L}from"../utils/unit.js";import{isCornerAnchor as V,textMinResizeWidth as F,textMinWidthIsMeasured as z}from"../utils/px.js";import{flags as U}from"../utils/flags.js";import{useColor as Z}from"./use-color.js";import{isGradient as _}from"@polotno/core/gradient";import{Html as H}from"react-konva-utils";import{observerBatching as J}from"mobx-react-lite";const N=e=>w.batchedUpdates(e,null);J(e=>{w.isAlreadyRendering()?queueMicrotask(()=>N(e)):N(e)});const K=S.DD._drag;window.removeEventListener("mousemove",K),S.DD._drag=function(e){a(()=>{K.call(this,e)})},window.addEventListener("mousemove",S.DD._drag);const $={enabledAnchors:["top-left","top-center","top-right","middle-left","bottom-left","bottom-right","bottom-center","middle-right"],rotateEnabled:!0,rotationSnaps:[0,45,90,135,180,225,270,315],ignoreStroke:!0,flipEnabled:!1,keepRatio:!0},Q=e=>{const t=(e%180+180)%180;return t<.1||180-t<.1},q=e=>{if(!e.length){return null}const t=Q(e[0].rotation)?0:e[0].rotation;return e.every(e=>Q(e.rotation-t))?t:null},ee=e=>e.selectedShapes.filter(e=>!e._cropModeEnabled),te=(e,t,n)=>{const i=e.width!==t.width,o=e.height!==t.height;if(!i&&!o){return e}const r=Math.cos(t.rotation),l=Math.sin(t.rotation),s=t.x-n.x,a=t.y-n.y,d=i?0:s*r+a*l,c=o?0:-s*l+a*r;return e.x=n.x+d*r-c*l,e.y=n.y+d*l+c*r,e},ne={text:{enabledAnchors:["top-left","top-right","middle-left","bottom-left","bottom-right","middle-right"]},svg:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},gif:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},line:{enabledAnchors:[],borderEnabled:!1,rotateEnabled:!1},image:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},many:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},group:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},table:{keepRatio:!1,enabledAnchors:["top-left","top-center","top-right","middle-left","middle-right","bottom-left","bottom-center","bottom-right"]}};export function registerTransformerAttrs(e,t){ne[e]=ne[e]||t,Object.assign(ne[e],t)}const ie=e=>t(f,Object.assign({},e,{preventDefault:!1})),oe=n=>{var{url:i}=n,o=e(n,["url"]);const[r,l]=E(i,"anonymous"),s=r?P(r,{width:o.width,height:o.height},"center-middle"):{};return O(l,i,"page background"),t(h,Object.assign({image:r},o,s))},re=e=>{const n=Z({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return t(f,Object.assign({},e,n))},le=i=>{const{background:r,scale:l,borderColor:s,transparencyBackgroundColor:a}=i,d=e(i,["background","scale","borderColor","transparencyBackgroundColor"]),c=o.useMemo(()=>!!S.Util.colorToRGBA(r)||_(r),[r]),h=o.useMemo(()=>{if(_(r)){return!0}const e=S.Util.colorToRGBA(r);return!e||e.a<1},[r]),m=o.useMemo(()=>{if(!h||a){return null}const e=document.createElement("canvas"),t=30;e.width=60,e.height=60;const n=e.getContext("2d");return n&&(n.fillStyle="black",n.fillRect(t,0,t,t),n.fillRect(0,t,t,t)),e},[h,a]);return n(o.Fragment,{children:[h&&t(f,a?Object.assign({fill:a},d,{hideInExport:!0}):Object.assign({fillPatternImage:m},d,{opacity:.1,hideInExport:!0})),c?t(re,Object.assign({fill:r},d)):t(oe,Object.assign({url:r},d))]})},se=r(({selection:e})=>{const{selectionRectStyle:n}=X();return e.visible?t(f,{name:"selection",x:Math.min(e.x1,e.x2),y:Math.min(e.y1,e.y2),width:Math.abs(e.x1-e.x2),height:Math.abs(e.y1-e.y2),fill:n.fill,stroke:n.stroke,strokeWidth:n.strokeWidth}):null});function ae(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const de=r(({x:e,y:o,width:r,height:l,rotation:s,anchor:a,store:d})=>{const{transformLabelStyle:c}=X();if(void 0===e||void 0===o||void 0===r||void 0===l||void 0===s||void 0===a){return null}const h=C({x:e,y:o,width:r,height:l,rotation:S.Util.radToDeg(s)}),u=(l/2+70)*Math.cos(s-Math.PI/2),g=(l/2+70)*Math.sin(s-Math.PI/2),f=G({unit:d.unit,dpi:d.dpi,px:r/d.scale,precious:"px"===d.unit?0:1})+" x "+G({unit:d.unit,dpi:d.dpi,px:l/d.scale,precious:"px"===d.unit?0:1})+("px"===d.unit?"":" "+d.unit);return n(i,{children:[n(m,{x:(h.minX+h.maxX)/2+u,y:(h.minY+h.maxY)/2+g,offsetX:14,offsetY:14,visible:"rotater"===a,children:[t(v,{cornerRadius:5,fill:c.fill}),t(b,{align:"center",verticalAlign:"middle",fill:c.textFill,padding:8,text:ae(s).toString()+"°"})]}),n(m,{x:(h.minX+h.maxX)/2,y:h.maxY+20,visible:"rotater"!==a,children:[t(v,{cornerRadius:5,fill:c.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),t(b,{align:"center",verticalAlign:"middle",fill:c.textFill,padding:8,text:f})]})]})}),ce=r(({elements:e,store:n})=>{const i=e.filter(e=>e.alwaysOnTop),r=e.filter(e=>!e.alwaysOnTop).concat(i);return t(o.Fragment,{children:r.map(e=>t(k,{store:n,element:e,onClick:()=>{console.warn("Polotno warning: onClick callback is deprecated. Just stop using it. Polotno will do selection automatically.")}},e.id))})});export const useContextMenu=({store:e})=>{const[t,n]=o.useState(!1),[i,r]=o.useState({x:0,y:0}),l=o.useCallback(e=>{n(!0),r(e)},[]);return{open:l,close:o.useCallback(()=>{n(!1)},[]),props:{isOpen:t,offset:i,setIsOpen:n}}};let he=null;export const registerNextDomDrop=e=>{he=e};const me=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),ue=atob("I2MwMzkyYg=="),ge=atob("djAuOS4y"),fe=e=>t(i,{children:n(m,{fill:ue,height:200,children:[t(v,{fill:ue}),t(b,Object.assign({},e,{fill:"white",text:me,height:void 0,padding:10,fontSize:20}))]})}),pe=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),xe=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),ve=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),be=e=>{const i=1===j(),o=i?xe:pe,r=i?230:195;return n(c,Object.assign({},e,{listening:!1,children:[t(f,{fill:ue,width:r,height:24,cornerRadius:2}),t(b,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),i&&t(b,{text:ve,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default r(({store:e,page:i,width:r,height:a,pageControlsEnabled:h,components:w,altCloneEnabled:E,groupSelectionMode:S="drill",viewportSize:k,layout:O="vertical",tooltipSafeArea:P,transformerSafeArea:G})=>{var Z,_;const J=X(),N=J.transformerStyle,K=$,oe=i.computedBleed,re=e.bleedVisible?oe:d,ae=i.computedWidth+re.left+re.right,me=i.computedHeight+re.top+re.bottom,ue=(r-ae*e.scale)/2,pe=(a-me*e.scale)/2,xe=o.useRef(null),ve=o.useRef(null),ye=o.useRef(null),we=o.useRef(null),Ee=null==h||h,Xe=o.useRef(!1),[Ye,Se]=o.useState(null),[ke,Ae]=o.useState({}),[Me,Ie]=o.useState(0),Oe=useContextMenu({store:e}),Pe=e.selectedElements.find(e=>e._cropModeEnabled),Re=e.selectedElements.find(e=>e.curveEnabled),je=e.selectedShapes.filter(e=>!e.resizable).length>0,De=e.selectedShapes.filter(e=>!e.draggable).length>0,Ce=e.selectedElements.filter(e=>!e.visible).length>0,Te=ee(e),Be=1!==e.selectedElements.length||"group"===(null===(Z=e.selectedElements[0])||void 0===Z?void 0:Z.type),We=null!==(_=q(Te))&&void 0!==_?_:0,Ge=e=>Be&&Te.length>0&&Te.every(t=>((e,t,n)=>{if(!Q(e.rotation-n)){return!1}switch(e.type){case"text":return"x"===t&&!z(e);case"figure":case"video":case"table":return!0;case"image":case"svg":case"gif":return!e.keepRatio;default:return!1}})(t,e,We)),Le=Ge("x"),Ve=Ge("y"),Fe=()=>{var t,n,i;const o=xe.current;if(!o){return[]}const r=o.getStage(),l=ee(e),s=l.map(e=>r.findOne("#"+e.id)).filter(e=>!!e),a=1===e.selectedElements.length,d=a&&(null===(t=e.selectedElements[0])||void 0===t?void 0:t.type)||"many",c=ne[d],h=Object.assign(Object.assign({},K),c||{});c&&("svg"!==d&&"image"!==d&&"gif"!==d||e.selectedElements[0].keepRatio||(h.enabledAnchors=K.enabledAnchors),"text"===d&&U.textVerticalResizeEnabled&&(h.enabledAnchors=null===(n=ne.text.enabledAnchors)||void 0===n?void 0:n.concat(["bottom-center","top-center"])),"text"===d&&a&&e.selectedElements[0].curveEnabled&&(h.enabledAnchors=ne.many.enabledAnchors)),(Le||Ve)&&(h.enabledAnchors=[...h.enabledAnchors||[],...Le?["middle-left","middle-right"]:[],...Ve?["top-center","bottom-center"]:[]]),o.setAttrs(Object.assign(Object.assign({},h),N)),je&&o.enabledAnchors([]),De&&o.rotateEnabled(!1);const m=o.nodes();if(m.length===s.length&&s.every((e,t)=>m[t]===e)||o.nodes(s),Be&&s.length){const e=q(l);null===e||Q(o.rotation()-e)||(o.rotation(e),o.forceUpdate())}return null===(i=o.getLayer())||void 0===i||i.batchDraw(),s};o.useLayoutEffect(()=>{var e;if(null===(e=xe.current)||void 0===e?void 0:e.isTransforming()){return}const t=Fe();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),M())},[e.selectedShapes,Pe,je,Ce,De,Re,Le,Ve,We,N]);const ze=()=>{var e,t;const n=xe.current;if(!n){return}if(!n.isTransforming()){return void Ae({})}if(!n.nodes().length||!n.isTransforming()){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();Ae({anchor:o,x:i.x-(null!==(e=null==r?void 0:r.x())&&void 0!==e?e:0),y:i.y-(null!==(t=null==r?void 0:r.y())&&void 0!==t?t:0),rotation:i.rotation,width:i.width,height:i.height})},Ue=o.useCallback(()=>{var e;if(null===(e=xe.current)||void 0===e?void 0:e.isTransforming()){return}const t=xe.current;if(!t||!t.nodes().length){return void Ie(0)}const n=t.getClientRect();if(!t.getStage()){return}const i=function(e,t,n,i=0){const o=e.x+e.width/2,r=e.y+e.height/2,l=e.width/2,s=e.height/2,a=i*Math.PI/180,d=e=>{const t=e*Math.PI/180,i=(e%360+360)%360;let d;d=0===i||180===i?s:l;const c=Math.sin(t)*(d+n),h=-Math.cos(t)*(d+n),m=c*Math.cos(a)-h*Math.sin(a),u=c*Math.sin(a)+h*Math.cos(a);return{x:o+m,y:r+u}},c=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(c(d(0))){return 0}for(const h of[180,270,90]){if(c(d(h))){return h}}return 0}(n,{top:(null==G?void 0:G.top)||0,bottom:a-((null==G?void 0:G.bottom)||0),left:(null==G?void 0:G.left)||0,right:r-((null==G?void 0:G.right)||0)},N.rotateAnchorOffset,t.rotation());Ie(i)},[G,r,a]);o.useEffect(()=>{var e;null===(e=xe.current)||void 0===e||e.update(),ze(),Ue()},[e.scale,Ue]),o.useEffect(()=>{Ue()},[e.selectedShapes,Ue]);const Ze=l(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),_e=o.useRef(!1),He=s(t=>{var n,i,o,r,l,s,a;if(e.role===Y.VIEWER){return}_e.current=!1;const d=t.target.findAncestor(".elements-container"),c=t.target.findAncestor("Transformer"),h=t.target.findAncestor(".page-abs-container");if(d||c||h){return}const m=null===(n=t.target.getStage())||void 0===n?void 0:n.getPointerPosition();m&&(m.x-=null!==(o=null===(i=t.target.getStage())||void 0===i?void 0:i.x())&&void 0!==o?o:0,m.y-=null!==(l=null===(r=t.target.getStage())||void 0===r?void 0:r.y())&&void 0!==l?l:0,Ze.visible=!0,Ze.x1=m.x,Ze.y1=m.y,Ze.x2=m.x,Ze.y2=m.y,(null!==(a=null===(s=t.target.getStage())||void 0===s?void 0:s.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(Ze.visible=!1))});(({stageRef:e,containerRef:t,viewportSize:n})=>{o.useEffect(()=>{var n;const i=null===(n=t.current)||void 0===n?void 0:n.closest(".polotno-workspace-inner");function o(){var n;if(!e.current){return}const o=null===(n=t.current)||void 0===n?void 0:n.getBoundingClientRect(),r=null==i?void 0:i.getBoundingClientRect();if(!o||!r){return}const l=Math.max(0,r.left-o.left-100),s=Math.max(0,r.top-o.top-100);e.current.position({x:-l,y:-s}),e.current.container().style.transform=`translate(${l}px, ${s}px)`}return o(),null==i||i.addEventListener("scroll",o),()=>{null==i||i.removeEventListener("scroll",o)}},[n.width,n.height])})({stageRef:ye,containerRef:we,viewportSize:k}),o.useEffect(()=>{const t=s(e=>{var t,n,i,o,r,l;if(!Ze.visible){return}null===(t=ye.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=ye.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(o=null===(i=ye.current)||void 0===i?void 0:i.x())&&void 0!==o?o:0,s.y-=null!==(l=null===(r=ye.current)||void 0===r?void 0:r.y())&&void 0!==l?l:0):s={x:Ze.x2,y:Ze.y2},Ze.x2=s.x,Ze.y2=s.y}),n=s(()=>{if(!Ze.visible){return}if(!ye.current){return}const t=ye.current.findOne(".selection"),n=t?t.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(n.width&&n.height){const t=[{x:n.x,y:n.y},{x:n.x+n.width,y:n.y},{x:n.x+n.width,y:n.y+n.height},{x:n.x,y:n.y+n.height}],i=[];ye.current.find(".element").forEach(n=>{var o;const r=e.getElementById(n.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=n.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const d=T(r.a).map(e=>a.point(e));W(t,d)&&i.push(r.top.id)});const o=[...new Set(i)];_e.current=!0,e.selectElements(o)}Ze.visible=!1});return window.addEventListener("mousemove",t),window.addEventListener("touchmove",t),window.addEventListener("mouseup",n,{capture:!0}),window.addEventListener("touchend",n,{capture:!0}),()=>{window.removeEventListener("mousemove",t),window.removeEventListener("touchmove",t),window.removeEventListener("mouseup",n),window.removeEventListener("touchend",n)}},[]);const Je=o.useCallback(t=>{if(!e.selectedElements[0]){return}if(t&&e.selectedElementsIds.includes(t)){return}const n=B(e.selectedShapes),o=t?e.getElementById(t):{x:0,y:0,width:i.computedWidth,height:i.computedHeight,rotation:0},r=C(o),l=[];n.minX>r.maxX&&l.push({distance:n.minX-r.maxX,box1:n,box2:r,points:[{x:n.minX,y:n.minY+n.height/2},{x:r.maxX,y:n.minY+n.height/2},{x:r.maxX,y:r.minY+r.height/2}]}),n.maxX<r.minX&&l.push({distance:r.minX-n.maxX,box1:n,box2:r,points:[{x:n.maxX,y:n.minY+n.height/2},{x:r.minX,y:n.minY+n.height/2},{x:r.minX,y:r.minY+r.height/2}]}),n.minY>r.maxY&&l.push({box1:n,box2:r,distance:n.minY-r.maxY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:r.maxY},{x:r.minX+r.width/2,y:r.maxY}]}),n.maxY<r.minY&&l.push({box1:n,box2:r,distance:r.minY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:r.minY},{x:r.minX+r.width/2,y:r.minY}]});const s=n.minX>=r.minX&&n.maxX<=r.maxX&&n.minY>=r.minY&&n.maxY<=r.maxY;if(s&&(l.push({distance:n.minX-r.minX,box1:n,box2:r,points:[{x:n.minX,y:n.minY+n.height/2},{x:r.minX,y:n.minY+n.height/2},{x:r.minX,y:r.minY+r.height/2}]}),l.push({distance:r.maxX-n.maxX,box1:n,box2:r,points:[{x:n.maxX,y:n.minY+n.height/2},{x:r.maxX,y:n.minY+n.height/2},{x:r.maxX,y:r.minY+r.height/2}]}),l.push({box1:n,box2:r,distance:n.minY-r.minY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:r.minY},{x:r.minX+r.width/2,y:r.minY}]}),l.push({box1:n,box2:r,distance:r.maxY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:r.maxY},{x:r.minX+r.width/2,y:r.maxY}]})),0===l.length&&!s){const e=n.minX<r.maxX&&n.maxX>r.minX,t=n.minY<r.maxY&&n.maxY>r.minY;if(e&&t){const e=(Math.max(n.minY,r.minY)+Math.min(n.maxY,r.maxY))/2,t=(Math.max(n.minX,r.minX)+Math.min(n.maxX,r.maxX))/2;n.minX<r.minX?l.push({distance:r.minX-n.minX,box1:n,box2:r,points:[{x:n.minX,y:e},{x:r.minX,y:e},{x:r.minX,y:e}]}):l.push({distance:n.minX-r.minX,box1:n,box2:r,points:[{x:n.minX,y:e},{x:r.minX,y:e},{x:r.minX,y:e}]}),n.maxX>r.maxX?l.push({distance:n.maxX-r.maxX,box1:n,box2:r,points:[{x:r.maxX,y:e},{x:n.maxX,y:e},{x:r.maxX,y:e}]}):l.push({distance:r.maxX-n.maxX,box1:n,box2:r,points:[{x:n.maxX,y:e},{x:r.maxX,y:e},{x:r.maxX,y:e}]}),n.minY<r.minY?l.push({box1:n,box2:r,distance:r.minY-n.minY,points:[{x:t,y:n.minY},{x:t,y:r.minY},{x:t,y:r.minY}]}):l.push({box1:n,box2:r,distance:n.minY-r.minY,points:[{x:t,y:n.minY},{x:t,y:r.minY},{x:t,y:r.minY}]}),n.maxY>r.maxY?l.push({box1:n,box2:r,distance:n.maxY-r.maxY,points:[{x:t,y:r.maxY},{x:t,y:n.maxY},{x:t,y:r.maxY}]}):l.push({box1:n,box2:r,distance:r.maxY-n.maxY,points:[{x:t,y:n.maxY},{x:t,y:r.maxY},{x:t,y:r.maxY}]})}}JSON.stringify(Ye)!==JSON.stringify(l)&&Se(l)},[e,i]);o.useEffect(()=>{e.selectedElements[0]&&e.distanceGuidesVisible?Je():Ye&&Se(null)},[e.distanceGuidesVisible,Je,e.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|")]);const Ne=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=we.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{Ne.current=!0,clearTimeout(t),t=setTimeout(()=>{Ne.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const t=ye.current;if(!t){return}const n=t.container(),i=new Map,o=e=>{i.set(e.target,{x:e.target.x(),y:e.target.y()})},r=e=>{i.delete(e.target)};t.on("dragstart",o),t.on("dragend",r);const l=t=>{var n,o;t.touches.length>=2&&((null===(n=xe.current)||void 0===n?void 0:n.isDragging())&&xe.current.stopDrag(),null===(o=xe.current)||void 0===o||o.nodes().forEach(t=>{var n;const o=i.get(t);t.isDragging()&&t.stopDrag(),o&&(t.setAttrs(o),null===(n=e.getElementById(t.id()))||void 0===n||n.set({x:o.x,y:o.y}))}))};return n.addEventListener("touchstart",l,{passive:!0}),n.addEventListener("touchmove",l,{passive:!0}),()=>{t.off("dragstart",o),t.off("dragend",r),n.removeEventListener("touchstart",l),n.removeEventListener("touchmove",l)}},[e]);const Ke=t=>{if(e.role===Y.VIEWER){return}if(e.activePage!==i&&i.select(),Ne.current){return}if(_e.current){return}const n=t.evt.shiftKey,o=t.evt.ctrlKey||t.evt.metaKey,r=n||o,l=t.target.findAncestor(".elements-container"),s=t.target.findAncestor(".page-abs-container"),a=t.target.findAncestor("Transformer"),d=t.target.findAncestor(".page-container",!0);if(!(r||l||a||s||d||Ze.visible)){return e.selectElements([]),void e.selectPages([])}if(o&&!n){const t=ye.current,n=null==t?void 0:t.getPointerPosition();if(!t||!n){return}const i=t.getAllIntersections(n),o=new Set,r=[];for(const s of[...i].reverse()){const t=s.findAncestor(".element",!0);if(!t){continue}const n=e.getElementById(t.id()),i=null==n?void 0:n.top;i&&(o.has(i.id)||(o.add(i.id),r.push(i.id)))}if(0===r.length){return}let l;if(1===e.selectedElementsIds.length){const t=e.selectedElementsIds[0],n=r.indexOf(t);l=-1===n?r[0]:r[(n+1)%r.length]}else{l=r[0]}return e.selectElements([l]),void e.selectPages([])}const c=t.target.findAncestor(".element-container",!0),h=(null==c?void 0:c.findOne(".element"))||t.target.findAncestor(".element",!0),m=e.getElementById(null==h?void 0:h.id()),u=null==m?void 0:m.top;let g;if("group"===S){g=null==u?void 0:u.id}else if(u){const t=e.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===u.id});g=t?null==m?void 0:m.id:null==u?void 0:u.id}else{g=void 0}const f=null==m?void 0:m.id;if(f&&e.selectedElementsIds.indexOf(f)>=0&&!n){return}const p=!!g&&e.selectedElementsIds.indexOf(g)>=0,x=t.target.findAncestor(".page-container",!0);if(g&&n&&!p){const t=e.selectedElementsIds.concat([g]),n=new Set(t),i=t.filter(t=>{const i=e.getElementById(t);if(!i){return!1}for(let e=i.parent;e;e=null==e?void 0:e.parent){if(n.has(e.id)){return!1}}return!0});e.selectElements(i),e.selectPages([])}else{g&&n&&p?e.selectElements(e.selectedElementsIds.filter(e=>e!==g)):!g||n||p?g&&!n&&p||(x?(n||e.selectElements([]),e.selectPages([i.id])):e.selectPages([])):(e.selectElements([g]),e.selectPages([]))}};I(xe,e);const $e=e.activePage===i,Qe=e._selectedPagesIds.includes(i.id),qe=null==w?void 0:w.PageControls,et=null==w?void 0:w.Tooltip,tt=null==w?void 0:w.ContextMenu,nt=1/e.scale,it=0/e.scale;return n("div",{ref:we,onDragOver:e=>e.preventDefault(),onDrop:t=>{if(t.preventDefault(),e.role===Y.VIEWER){return}if(!ye.current){return}ye.current.setPointersPositions(t);const n=ye.current.findOne(".elements-container"),o=null==n?void 0:n.getRelativePointerPosition(),r=ye.current.getPointerPosition(),l=ye.current.getAllIntersections(r).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(l.reverse())].map(t=>e.getElementById(t.id())).filter(e=>!!e),a=s[0];he&&o&&(he(o,a,{elements:s,page:i}),he=null)},style:{position:"relative",width:r+"px",height:a+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+($e?" active-page":""),children:[t(x,{ref:ye,width:Math.min(r,k.width+200),height:Math.min(k.height+200,a),onClick:Ke,onTap:Ke,onContextMenu:t=>{if(t.evt.preventDefault(),e.role===Y.VIEWER){return}const n=t.target.findAncestor(".element",!0),i=e.getElementById(null==n?void 0:n.id()),o=null==i?void 0:i.top,r=null==o?void 0:o.id;if(!r){return e.selectElements([]),void Oe.close()}e.selectedElementsIds.indexOf(r)>=0||e.selectElements([r]),Oe.open({x:t.evt.clientX,y:t.evt.clientY})},onMouseDown:He,onMouseMove:t=>{if(!e.distanceGuidesVisible&&!t.evt.altKey){return void(Ye&&Se(null))}const n=t.target.findAncestor(".element",!0),i=null==n?void 0:n.id();Je(i)},onDragStart:t=>{var n;const i=t.target.getStage();if(i&&i.getPointersPositions().length>=2){return void t.target.stopDrag()}const o=t.target.findAncestor(".element",!0);if(o){const i=e.getElementById(null==o?void 0:o.id()),r=null==i?void 0:i.top,l=null==r?void 0:r.id;if(!(i&&e.selectedElementsIds.indexOf(i.id)>=0||l&&e.selectedElementsIds.indexOf(l)>=0)&&l){e.selectElements([l]);const i=Fe();t.target.stopDrag(),i.forEach(e=>{e.isDragging()||e.startDrag(t)}),null===(n=xe.current)||void 0===n||n.startDrag(t),M()}}Ye&&Se(null)},pageId:i.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:n(u,{children:[t(ie,{width:r,height:a,fill:J.workspaceBackgroundColor}),t(c,{x:ue,y:pe,scaleX:e.scale,scaleY:e.scale,name:"page-container",children:n(c,{name:"page-container-2",children:[t(c,{name:"page-background-group",x:re.left,y:re.top,children:t(le,{x:-oe.left,y:-oe.top,width:i.computedWidth+oe.left+oe.right,height:i.computedHeight+oe.top+oe.bottom,background:i.background,transparencyBackgroundColor:J.transparencyBackgroundColor,name:"page-background",preventDefault:!1,scale:e.scale})}),n(c,{x:re.left,y:re.top,name:"elements-container",listening:!e.isPlaying&&e.role!==Y.VIEWER,children:[t(f,{name:"elements-area",width:i.computedWidth,height:i.computedHeight,listening:!1}),t(ce,{elements:i.children,store:e})]}),t(p,{name:"bleed",fill:J.bleedColor,listening:!1,visible:e.bleedVisible&&(re.top>0||re.right>0||re.bottom>0||re.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:o,bottom:r,left:l}=oe,s=i.computedWidth,a=i.computedHeight,d=e._context;d.beginPath(),d.rect(0,0,l+s+o,n+a+r),d.rect(l,n,s,a),d.fillStyle=t.fill(),d.fill("evenodd")}}),R()===ge&&t(fe,{name:"hit-detection",x:-nt/2-it,y:-nt/2-it,width:ae+nt+2*it,height:me+nt+2*it})]})}),t(g,{name:"workspace-background",points:[0,0,r,0,r,a,0,a,0,0,ue,pe,ue,a-pe,r-ue,a-pe,r-ue,pe,ue,pe],listening:!1,closed:!0,fill:J.workspaceBackgroundColor}),t(c,{x:ue,y:pe,scaleX:e.scale,scaleY:e.scale,children:t(f,{name:"page-highlight",hideInExport:!0,x:-nt/2-it,y:-nt/2-it,width:ae+nt+2*it,height:me+nt+2*it,stroke:Qe?J.activePageBorderColor:J.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),n(c,{x:ue+re.left*e.scale,y:pe+re.top*e.scale,scaleX:e.scale,scaleY:e.scale,name:"page-abs-container",children:[t(y,{ref:xe,rotateAnchorAngle:Me,onDragStart:t=>{var n;const o=t.target.getStage();o&&o.getPointersPositions().length>=2?t.target.stopDrag():((null===(n=t.evt)||void 0===n?void 0:n.altKey)&&E&&e.selectedElements.forEach(e=>{const t=e.clone({},{skipSelect:!0}),n=i.children.indexOf(e);i.setElementZIndex(t.id,n)}),Xe.current||(Xe.current=!0,e.history.startTransaction()))},onDragEnd:()=>{Xe.current&&(e.history.endTransaction(),Xe.current=!1),Ue()},onTransformStart:()=>{e.history.startTransaction(),ve.current=new Map(Te.map(e=>[e.id,{width:e.width,height:e.height}])),Se(null)},boundBoxFunc:(t,n)=>{var i,o;const r=Math.abs(n.width)<1||Math.abs(n.height)<1,l=Math.abs(t.width)<1||Math.abs(t.height)<1;if(r&&!l){return t}const s=e.selectedElements;if(1===s.length&&"table"===(null===(i=s[0])||void 0===i?void 0:i.type)){const e=s[0],i=Math.abs(t.width),o=Math.abs(t.height);if(i>0&&o>0){const r=Math.abs(n.width)/i,l=Math.abs(n.height)/o,s=e.width*r,a=e.height*l,d=Object.assign({},n);return s<e.minWidth&&(d.width=t.width),a<e.minHeight&&(d.height=t.height),te(d,n,t)}}return Be&&Te.length>0?((e,t,n,i,o)=>{const r=Math.abs(e.width),l=Math.abs(e.height);if(0===r||0===l){return t}const s=Math.abs(t.width)/r,a=Math.abs(t.height)/l,d=!V(n),c=e=>i.some(t=>{var n;const i=null==o?void 0:o.get(t.id),r=null!==(n="x"===e?null==i?void 0:i.width:null==i?void 0:i.height)&&void 0!==n?n:"x"===e?t.width:t.height,l="x"===e?t.width:t.height,c="x"===e?s:a,h=((e,t,n,i)=>{if("table"===e.type){const n=e;return"x"===t?n.minWidth:n.minHeight}return"text"===e.type&&"x"===t&&n?F(i,e.fontSize):1})(t,e,d,r);return r>=h&&l*c<h}),h=Object.assign({},t),m=t.width*e.width<0,u=t.height*e.height<0,g=m||c("x"),f=u||c("y"),p=f||!d&&g;return(g||!d&&f)&&(h.width=e.width),p&&(h.height=e.height),te(h,t,e)})(t,n,(null===(o=xe.current)||void 0===o?void 0:o.getActiveAnchor())||"",Te,ve.current):n},onTransform:e=>{const t=xe.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{ze()},0)},onTransformEnd:t=>{Ae({}),e.history.endTransaction(),ve.current=null,Ue(),Fe()},visible:!e.isPlaying}),Ye&&Ye.map(({points:i,distance:o,box1:r,box2:l},s)=>n(c,{name:"distances-container",hideInExport:!0,listening:!1,children:[t(f,Object.assign({},r,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(f,Object.assign({},l,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(g,{points:[i[0].x,i[0].y,i[1].x,i[1].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),t(g,{points:[i[1].x,i[1].y,i[2].x,i[2].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(m,{x:(i[0].x+i[1].x)/2,y:(i[0].y+i[1].y)/2,offsetY:-10,scaleX:1/e.scale,scaleY:1/e.scale,children:[t(v,{cornerRadius:5,fill:J.distanceGuideStyle.labelFill,pointerDirection:"down"}),t(b,{align:"center",verticalAlign:"middle",fill:J.distanceGuideStyle.labelTextFill,padding:5,text:L({unit:e.unit,dpi:e.dpi,px:o})})]})]},s)),i._rendering&&n(c,{children:[t(f,{width:ae,height:me,fill:"rgba(255,255,255,0.9)"}),t(b,{text:"Rendering...",fontSize:60,width:ae,height:me,align:"center",verticalAlign:"middle"})]}),et&&t(et,{components:w,store:e,page:i,stageRef:ye,tooltipSafeArea:P}),tt&&t(H,{children:t(tt,Object.assign({components:w,store:e},Oe.props))})]}),t(A,{store:e,page:i,width:r,height:a,scale:e.scale,xPadding:ue,yPadding:pe,bleedLeft:re.left,bleedTop:re.top}),t(de,Object.assign({},ke,{store:e})),t(se,{selection:Ze}),j()>0&&n(c,{hideInExport:!0,children:[t(be,{name:"cache-bounds",x:r-(1===j()?250:210),y:a-(1===j()?95:45)}),t(be,{name:"cache-bounds-t",x:10,y:10})]}),D()&&t(b,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:r-170,y:a-18,onMouseEnter:e=>{e.target.getStage().container().style.cursor="pointer"},onMouseLeave:e=>{e.target.getStage().container().style.cursor=""},onTouchStart:e=>{e.cancelBubble=!0},onMouseDown:e=>{e.cancelBubble=!0},onClick:()=>{window.open("https://polotno.com","_blank","noopener")},onTap:()=>{window.open("https://polotno.com","_blank","noopener")}}),t(c,{name:"line-guides"})]})}),Ee&&qe&&t(qe,{store:e,page:i,xPadding:ue,yPadding:pe,layout:O})]})});
|
|
1
|
+
var e=this&&this.__rest||function(e,t){var n={};for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++){t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(n[i[o]]=e[i[o]])}}return n};import{jsx as t,jsxs as n,Fragment as i}from"react/jsx-runtime";import o from"react";import{observer as r,useLocalObservable as l}from"mobx-react-lite";import{action as s,runInAction as a}from"mobx";import{ZERO_BLEED as d}from"@polotno/schema";import{Group as c,Image as h,Label as m,Layer as u,Line as g,Rect as f,Shape as p,Stage as x,Tag as v,Text as b,Transformer as y,KonvaRenderer as w}from"react-konva";import E from"use-image";import{useWorkspaceStyle as X}from"./workspace-style.js";import{ROLES as Y}from"../model/store.js";import S from"konva";import k from"./element.js";import{DrawingLayer as A}from"./drawing-layer.js";import{ensureDragOrder as M,useSnap as I}from"./use-transformer-snap.js";import{useImageLoader as O}from"./image-element.js";import{getCrop as P}from"../utils/crop.js";import{___ as R,____ as j,isCreditVisible as D}from"../utils/validate-key.js";import{getClientRect as C,getCorners as T,getTotalClientRect as B,polygonsIntersect as W}from"../utils/math.js";import{pxToUnitRounded as G,pxToUnitString as L}from"../utils/unit.js";import{isCornerAnchor as V,textMinResizeWidth as F,textMinWidthIsMeasured as z}from"../utils/px.js";import{flags as U}from"../utils/flags.js";import{useColor as Z}from"./use-color.js";import{isGradient as _}from"@polotno/core/gradient";import{Html as H}from"react-konva-utils";import{observerBatching as J}from"mobx-react-lite";const N=e=>w.batchedUpdates(e,null);J(e=>{w.isAlreadyRendering()?queueMicrotask(()=>N(e)):N(e)});const K=S.DD._drag;window.removeEventListener("mousemove",K),S.DD._drag=function(e){a(()=>{K.call(this,e)})},window.addEventListener("mousemove",S.DD._drag);const $={enabledAnchors:["top-left","top-center","top-right","middle-left","bottom-left","bottom-right","bottom-center","middle-right"],rotateEnabled:!0,rotationSnaps:[0,45,90,135,180,225,270,315],ignoreStroke:!0,flipEnabled:!1,keepRatio:!0},Q=e=>{const t=(e%180+180)%180;return t<.1||180-t<.1},q=e=>{if(!e.length){return null}const t=Q(e[0].rotation)?0:e[0].rotation;return e.every(e=>Q(e.rotation-t))?t:null},ee=e=>e.selectedShapes.filter(e=>!e._cropModeEnabled),te=(e,t,n)=>{const i=e.width!==t.width,o=e.height!==t.height;if(!i&&!o){return e}const r=Math.cos(t.rotation),l=Math.sin(t.rotation),s=t.x-n.x,a=t.y-n.y,d=i?0:s*r+a*l,c=o?0:-s*l+a*r;return e.x=n.x+d*r-c*l,e.y=n.y+d*l+c*r,e},ne={text:{enabledAnchors:["top-left","top-right","middle-left","bottom-left","bottom-right","middle-right"]},svg:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},gif:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},line:{enabledAnchors:[],borderEnabled:!1,rotateEnabled:!1},image:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},many:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},group:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},table:{keepRatio:!1,enabledAnchors:["top-left","top-center","top-right","middle-left","middle-right","bottom-left","bottom-center","bottom-right"]}};export function registerTransformerAttrs(e,t){ne[e]=ne[e]||t,Object.assign(ne[e],t)}const ie=e=>t(f,Object.assign({},e,{preventDefault:!1})),oe=n=>{var{url:i}=n,o=e(n,["url"]);const[r,l]=E(i,"anonymous"),s=r?P(r,{width:o.width,height:o.height},"center-middle"):{};return O(l,i,"page background"),t(h,Object.assign({image:r},o,s))},re=e=>{const n=Z({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return t(f,Object.assign({},e,n))},le=i=>{const{background:r,scale:l,borderColor:s,transparencyBackgroundColor:a}=i,d=e(i,["background","scale","borderColor","transparencyBackgroundColor"]),c=o.useMemo(()=>!!S.Util.colorToRGBA(r)||_(r),[r]),h=o.useMemo(()=>{if(_(r)){return!0}const e=S.Util.colorToRGBA(r);return!e||e.a<1},[r]),m=o.useMemo(()=>{if(!h||a){return null}const e=document.createElement("canvas"),t=30;e.width=60,e.height=60;const n=e.getContext("2d");return n&&(n.fillStyle="black",n.fillRect(t,0,t,t),n.fillRect(0,t,t,t)),e},[h,a]);return n(o.Fragment,{children:[h&&t(f,a?Object.assign({fill:a},d,{hideInExport:!0}):Object.assign({fillPatternImage:m},d,{opacity:.1,hideInExport:!0})),c?t(re,Object.assign({fill:r},d)):t(oe,Object.assign({url:r},d))]})},se=r(({selection:e})=>{const{selectionRectStyle:n}=X();return e.visible?t(f,{name:"selection",x:Math.min(e.x1,e.x2),y:Math.min(e.y1,e.y2),width:Math.abs(e.x1-e.x2),height:Math.abs(e.y1-e.y2),fill:n.fill,stroke:n.stroke,strokeWidth:n.strokeWidth}):null});function ae(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const de=r(({x:e,y:o,width:r,height:l,rotation:s,anchor:a,store:d})=>{const{transformLabelStyle:c}=X();if(void 0===e||void 0===o||void 0===r||void 0===l||void 0===s||void 0===a){return null}const h=C({x:e,y:o,width:r,height:l,rotation:S.Util.radToDeg(s)}),u=(l/2+70)*Math.cos(s-Math.PI/2),g=(l/2+70)*Math.sin(s-Math.PI/2),f=G({unit:d.unit,dpi:d.dpi,px:r/d.scale,precious:"px"===d.unit?0:1})+" x "+G({unit:d.unit,dpi:d.dpi,px:l/d.scale,precious:"px"===d.unit?0:1})+("px"===d.unit?"":" "+d.unit);return n(i,{children:[n(m,{x:(h.minX+h.maxX)/2+u,y:(h.minY+h.maxY)/2+g,offsetX:14,offsetY:14,visible:"rotater"===a,children:[t(v,{cornerRadius:5,fill:c.fill}),t(b,{align:"center",verticalAlign:"middle",fill:c.textFill,padding:8,text:ae(s).toString()+"°"})]}),n(m,{x:(h.minX+h.maxX)/2,y:h.maxY+20,visible:"rotater"!==a,children:[t(v,{cornerRadius:5,fill:c.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),t(b,{align:"center",verticalAlign:"middle",fill:c.textFill,padding:8,text:f})]})]})}),ce=r(({elements:e,store:n})=>{const i=e.filter(e=>e.alwaysOnTop),r=e.filter(e=>!e.alwaysOnTop).concat(i);return t(o.Fragment,{children:r.map(e=>t(k,{store:n,element:e,onClick:()=>{console.warn("Polotno warning: onClick callback is deprecated. Just stop using it. Polotno will do selection automatically.")}},e.id))})});export const useContextMenu=({store:e})=>{const[t,n]=o.useState(!1),[i,r]=o.useState({x:0,y:0}),l=o.useCallback(e=>{n(!0),r(e)},[]);return{open:l,close:o.useCallback(()=>{n(!1)},[]),props:{isOpen:t,offset:i,setIsOpen:n}}};let he=null;export const registerNextDomDrop=e=>{he=e};const me=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),ue=atob("I2MwMzkyYg=="),ge=atob("djAuOS4y"),fe=e=>t(i,{children:n(m,{fill:ue,height:200,children:[t(v,{fill:ue}),t(b,Object.assign({},e,{fill:"white",text:me,height:void 0,padding:10,fontSize:20}))]})}),pe=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),xe=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),ve=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),be=e=>{const i=1===j(),o=i?xe:pe,r=i?230:195;return n(c,Object.assign({},e,{listening:!1,children:[t(f,{fill:ue,width:r,height:24,cornerRadius:2}),t(b,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),i&&t(b,{text:ve,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default r(({store:e,page:i,width:r,height:a,pageControlsEnabled:h,components:w,altCloneEnabled:E,groupSelectionMode:S="drill",viewportSize:k,layout:O="vertical",tooltipSafeArea:P,transformerSafeArea:G})=>{var Z,_;const J=X(),N=J.transformerStyle,K=$,oe=i.computedBleed,re=e.bleedVisible?oe:d,ae=i.computedWidth+re.left+re.right,me=i.computedHeight+re.top+re.bottom,ue=(r-ae*e.scale)/2,pe=(a-me*e.scale)/2,xe=o.useRef(null),ve=o.useRef(null),ye=o.useRef(null),we=o.useRef(null),Ee=null==h||h,Xe=o.useRef(!1),[Ye,Se]=o.useState(null),[ke,Ae]=o.useState({}),[Me,Ie]=o.useState(0),Oe=useContextMenu({store:e}),Pe=e.selectedElements.find(e=>e._cropModeEnabled),Re=e.selectedElements.find(e=>e.curveEnabled),je=e.selectedShapes.filter(e=>!e.resizable).length>0,De=e.selectedShapes.filter(e=>!e.draggable).length>0,Ce=e.selectedElements.filter(e=>!e.visible).length>0,Te=ee(e),Be=1!==e.selectedElements.length||"group"===(null===(Z=e.selectedElements[0])||void 0===Z?void 0:Z.type),We=null!==(_=q(Te))&&void 0!==_?_:0,Ge=e=>Be&&Te.length>0&&Te.every(t=>((e,t,n)=>{if(!Q(e.rotation-n)){return!1}switch(e.type){case"text":return"x"===t&&!z(e);case"figure":case"video":case"table":return!0;case"image":case"svg":case"gif":return!e.keepRatio;default:return!1}})(t,e,We)),Le=Ge("x"),Ve=Ge("y"),Fe=()=>{var t,n,i;const o=xe.current;if(!o){return[]}const r=o.getStage();if(!r){return[]}const l=ee(e),s=l.map(e=>r.findOne("#"+e.id)).filter(e=>!!e),a=1===e.selectedElements.length,d=a&&(null===(t=e.selectedElements[0])||void 0===t?void 0:t.type)||"many",c=ne[d],h=Object.assign(Object.assign({},K),c||{});c&&("svg"!==d&&"image"!==d&&"gif"!==d||e.selectedElements[0].keepRatio||(h.enabledAnchors=K.enabledAnchors),"text"===d&&U.textVerticalResizeEnabled&&(h.enabledAnchors=null===(n=ne.text.enabledAnchors)||void 0===n?void 0:n.concat(["bottom-center","top-center"])),"text"===d&&a&&e.selectedElements[0].curveEnabled&&(h.enabledAnchors=ne.many.enabledAnchors)),(Le||Ve)&&(h.enabledAnchors=[...h.enabledAnchors||[],...Le?["middle-left","middle-right"]:[],...Ve?["top-center","bottom-center"]:[]]),o.setAttrs(Object.assign(Object.assign({},h),N)),je&&o.enabledAnchors([]),De&&o.rotateEnabled(!1);const m=o.nodes();if(m.length===s.length&&s.every((e,t)=>m[t]===e)||o.nodes(s),Be&&s.length){const e=q(l);null===e||Q(o.rotation()-e)||(o.rotation(e),o.forceUpdate())}return null===(i=o.getLayer())||void 0===i||i.batchDraw(),s};o.useLayoutEffect(()=>{var e;if(null===(e=xe.current)||void 0===e?void 0:e.isTransforming()){return}const t=Fe();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),M())},[e.selectedShapes,Pe,je,Ce,De,Re,Le,Ve,We,N]);const ze=()=>{var e,t;const n=xe.current;if(!n){return}if(!n.isTransforming()){return void Ae({})}if(!n.nodes().length||!n.isTransforming()){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();Ae({anchor:o,x:i.x-(null!==(e=null==r?void 0:r.x())&&void 0!==e?e:0),y:i.y-(null!==(t=null==r?void 0:r.y())&&void 0!==t?t:0),rotation:i.rotation,width:i.width,height:i.height})},Ue=o.useCallback(()=>{var e;if(null===(e=xe.current)||void 0===e?void 0:e.isTransforming()){return}const t=xe.current;if(!t||!t.nodes().length){return void Ie(0)}const n=t.getClientRect();if(!t.getStage()){return}const i=function(e,t,n,i=0){const o=e.x+e.width/2,r=e.y+e.height/2,l=e.width/2,s=e.height/2,a=i*Math.PI/180,d=e=>{const t=e*Math.PI/180,i=(e%360+360)%360;let d;d=0===i||180===i?s:l;const c=Math.sin(t)*(d+n),h=-Math.cos(t)*(d+n),m=c*Math.cos(a)-h*Math.sin(a),u=c*Math.sin(a)+h*Math.cos(a);return{x:o+m,y:r+u}},c=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(c(d(0))){return 0}for(const h of[180,270,90]){if(c(d(h))){return h}}return 0}(n,{top:(null==G?void 0:G.top)||0,bottom:a-((null==G?void 0:G.bottom)||0),left:(null==G?void 0:G.left)||0,right:r-((null==G?void 0:G.right)||0)},N.rotateAnchorOffset,t.rotation());Ie(i)},[G,r,a]);o.useEffect(()=>{var e;null===(e=xe.current)||void 0===e||e.update(),ze(),Ue()},[e.scale,Ue]),o.useEffect(()=>{Ue()},[e.selectedShapes,Ue]);const Ze=l(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),_e=o.useRef(!1),He=s(t=>{var n,i,o,r,l,s,a;if(e.role===Y.VIEWER){return}_e.current=!1;const d=t.target.findAncestor(".elements-container"),c=t.target.findAncestor("Transformer"),h=t.target.findAncestor(".page-abs-container");if(d||c||h){return}const m=null===(n=t.target.getStage())||void 0===n?void 0:n.getPointerPosition();m&&(m.x-=null!==(o=null===(i=t.target.getStage())||void 0===i?void 0:i.x())&&void 0!==o?o:0,m.y-=null!==(l=null===(r=t.target.getStage())||void 0===r?void 0:r.y())&&void 0!==l?l:0,Ze.visible=!0,Ze.x1=m.x,Ze.y1=m.y,Ze.x2=m.x,Ze.y2=m.y,(null!==(a=null===(s=t.target.getStage())||void 0===s?void 0:s.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(Ze.visible=!1))});(({stageRef:e,containerRef:t,viewportSize:n})=>{o.useEffect(()=>{var n;const i=null===(n=t.current)||void 0===n?void 0:n.closest(".polotno-workspace-inner");function o(){var n;if(!e.current){return}const o=null===(n=t.current)||void 0===n?void 0:n.getBoundingClientRect(),r=null==i?void 0:i.getBoundingClientRect();if(!o||!r){return}const l=Math.max(0,r.left-o.left-100),s=Math.max(0,r.top-o.top-100);e.current.position({x:-l,y:-s}),e.current.container().style.transform=`translate(${l}px, ${s}px)`}return o(),null==i||i.addEventListener("scroll",o),()=>{null==i||i.removeEventListener("scroll",o)}},[n.width,n.height])})({stageRef:ye,containerRef:we,viewportSize:k}),o.useEffect(()=>{const t=s(e=>{var t,n,i,o,r,l;if(!Ze.visible){return}null===(t=ye.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=ye.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(o=null===(i=ye.current)||void 0===i?void 0:i.x())&&void 0!==o?o:0,s.y-=null!==(l=null===(r=ye.current)||void 0===r?void 0:r.y())&&void 0!==l?l:0):s={x:Ze.x2,y:Ze.y2},Ze.x2=s.x,Ze.y2=s.y}),n=s(()=>{if(!Ze.visible){return}if(!ye.current){return}const t=ye.current.findOne(".selection"),n=t?t.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(n.width&&n.height){const t=[{x:n.x,y:n.y},{x:n.x+n.width,y:n.y},{x:n.x+n.width,y:n.y+n.height},{x:n.x,y:n.y+n.height}],i=[];ye.current.find(".element").forEach(n=>{var o;const r=e.getElementById(n.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=n.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const d=T(r.a).map(e=>a.point(e));W(t,d)&&i.push(r.top.id)});const o=[...new Set(i)];_e.current=!0,e.selectElements(o)}Ze.visible=!1});return window.addEventListener("mousemove",t),window.addEventListener("touchmove",t),window.addEventListener("mouseup",n,{capture:!0}),window.addEventListener("touchend",n,{capture:!0}),()=>{window.removeEventListener("mousemove",t),window.removeEventListener("touchmove",t),window.removeEventListener("mouseup",n),window.removeEventListener("touchend",n)}},[]);const Je=o.useCallback(t=>{if(!e.selectedElements[0]){return}if(t&&e.selectedElementsIds.includes(t)){return}const n=B(e.selectedShapes),o=t?e.getElementById(t):{x:0,y:0,width:i.computedWidth,height:i.computedHeight,rotation:0},r=C(o),l=[];n.minX>r.maxX&&l.push({distance:n.minX-r.maxX,box1:n,box2:r,points:[{x:n.minX,y:n.minY+n.height/2},{x:r.maxX,y:n.minY+n.height/2},{x:r.maxX,y:r.minY+r.height/2}]}),n.maxX<r.minX&&l.push({distance:r.minX-n.maxX,box1:n,box2:r,points:[{x:n.maxX,y:n.minY+n.height/2},{x:r.minX,y:n.minY+n.height/2},{x:r.minX,y:r.minY+r.height/2}]}),n.minY>r.maxY&&l.push({box1:n,box2:r,distance:n.minY-r.maxY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:r.maxY},{x:r.minX+r.width/2,y:r.maxY}]}),n.maxY<r.minY&&l.push({box1:n,box2:r,distance:r.minY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:r.minY},{x:r.minX+r.width/2,y:r.minY}]});const s=n.minX>=r.minX&&n.maxX<=r.maxX&&n.minY>=r.minY&&n.maxY<=r.maxY;if(s&&(l.push({distance:n.minX-r.minX,box1:n,box2:r,points:[{x:n.minX,y:n.minY+n.height/2},{x:r.minX,y:n.minY+n.height/2},{x:r.minX,y:r.minY+r.height/2}]}),l.push({distance:r.maxX-n.maxX,box1:n,box2:r,points:[{x:n.maxX,y:n.minY+n.height/2},{x:r.maxX,y:n.minY+n.height/2},{x:r.maxX,y:r.minY+r.height/2}]}),l.push({box1:n,box2:r,distance:n.minY-r.minY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:r.minY},{x:r.minX+r.width/2,y:r.minY}]}),l.push({box1:n,box2:r,distance:r.maxY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:r.maxY},{x:r.minX+r.width/2,y:r.maxY}]})),0===l.length&&!s){const e=n.minX<r.maxX&&n.maxX>r.minX,t=n.minY<r.maxY&&n.maxY>r.minY;if(e&&t){const e=(Math.max(n.minY,r.minY)+Math.min(n.maxY,r.maxY))/2,t=(Math.max(n.minX,r.minX)+Math.min(n.maxX,r.maxX))/2;n.minX<r.minX?l.push({distance:r.minX-n.minX,box1:n,box2:r,points:[{x:n.minX,y:e},{x:r.minX,y:e},{x:r.minX,y:e}]}):l.push({distance:n.minX-r.minX,box1:n,box2:r,points:[{x:n.minX,y:e},{x:r.minX,y:e},{x:r.minX,y:e}]}),n.maxX>r.maxX?l.push({distance:n.maxX-r.maxX,box1:n,box2:r,points:[{x:r.maxX,y:e},{x:n.maxX,y:e},{x:r.maxX,y:e}]}):l.push({distance:r.maxX-n.maxX,box1:n,box2:r,points:[{x:n.maxX,y:e},{x:r.maxX,y:e},{x:r.maxX,y:e}]}),n.minY<r.minY?l.push({box1:n,box2:r,distance:r.minY-n.minY,points:[{x:t,y:n.minY},{x:t,y:r.minY},{x:t,y:r.minY}]}):l.push({box1:n,box2:r,distance:n.minY-r.minY,points:[{x:t,y:n.minY},{x:t,y:r.minY},{x:t,y:r.minY}]}),n.maxY>r.maxY?l.push({box1:n,box2:r,distance:n.maxY-r.maxY,points:[{x:t,y:r.maxY},{x:t,y:n.maxY},{x:t,y:r.maxY}]}):l.push({box1:n,box2:r,distance:r.maxY-n.maxY,points:[{x:t,y:n.maxY},{x:t,y:r.maxY},{x:t,y:r.maxY}]})}}JSON.stringify(Ye)!==JSON.stringify(l)&&Se(l)},[e,i]);o.useEffect(()=>{e.selectedElements[0]&&e.distanceGuidesVisible?Je():Ye&&Se(null)},[e.distanceGuidesVisible,Je,e.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|")]);const Ne=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=we.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{Ne.current=!0,clearTimeout(t),t=setTimeout(()=>{Ne.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const t=ye.current;if(!t){return}const n=t.container(),i=new Map,o=e=>{i.set(e.target,{x:e.target.x(),y:e.target.y()})},r=e=>{i.delete(e.target)};t.on("dragstart",o),t.on("dragend",r);const l=t=>{var n,o;t.touches.length>=2&&((null===(n=xe.current)||void 0===n?void 0:n.isDragging())&&xe.current.stopDrag(),null===(o=xe.current)||void 0===o||o.nodes().forEach(t=>{var n;const o=i.get(t);t.isDragging()&&t.stopDrag(),o&&(t.setAttrs(o),null===(n=e.getElementById(t.id()))||void 0===n||n.set({x:o.x,y:o.y}))}))};return n.addEventListener("touchstart",l,{passive:!0}),n.addEventListener("touchmove",l,{passive:!0}),()=>{t.off("dragstart",o),t.off("dragend",r),n.removeEventListener("touchstart",l),n.removeEventListener("touchmove",l)}},[e]);const Ke=t=>{if(e.role===Y.VIEWER){return}if(e.activePage!==i&&i.select(),Ne.current){return}if(_e.current){return}const n=t.evt.shiftKey,o=t.evt.ctrlKey||t.evt.metaKey,r=n||o,l=t.target.findAncestor(".elements-container"),s=t.target.findAncestor(".page-abs-container"),a=t.target.findAncestor("Transformer"),d=t.target.findAncestor(".page-container",!0);if(!(r||l||a||s||d||Ze.visible)){return e.selectElements([]),void e.selectPages([])}if(o&&!n){const t=ye.current,n=null==t?void 0:t.getPointerPosition();if(!t||!n){return}const i=t.getAllIntersections(n),o=new Set,r=[];for(const s of[...i].reverse()){const t=s.findAncestor(".element",!0);if(!t){continue}const n=e.getElementById(t.id()),i=null==n?void 0:n.top;i&&(o.has(i.id)||(o.add(i.id),r.push(i.id)))}if(0===r.length){return}let l;if(1===e.selectedElementsIds.length){const t=e.selectedElementsIds[0],n=r.indexOf(t);l=-1===n?r[0]:r[(n+1)%r.length]}else{l=r[0]}return e.selectElements([l]),void e.selectPages([])}const c=t.target.findAncestor(".element-container",!0),h=(null==c?void 0:c.findOne(".element"))||t.target.findAncestor(".element",!0),m=e.getElementById(null==h?void 0:h.id()),u=null==m?void 0:m.top;let g;if("group"===S){g=null==u?void 0:u.id}else if(u){const t=e.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===u.id});g=t?null==m?void 0:m.id:null==u?void 0:u.id}else{g=void 0}const f=null==m?void 0:m.id;if(f&&e.selectedElementsIds.indexOf(f)>=0&&!n){return}const p=!!g&&e.selectedElementsIds.indexOf(g)>=0,x=t.target.findAncestor(".page-container",!0);if(g&&n&&!p){const t=e.selectedElementsIds.concat([g]),n=new Set(t),i=t.filter(t=>{const i=e.getElementById(t);if(!i){return!1}for(let e=i.parent;e;e=null==e?void 0:e.parent){if(n.has(e.id)){return!1}}return!0});e.selectElements(i),e.selectPages([])}else{g&&n&&p?e.selectElements(e.selectedElementsIds.filter(e=>e!==g)):!g||n||p?g&&!n&&p||(x?(n||e.selectElements([]),e.selectPages([i.id])):e.selectPages([])):(e.selectElements([g]),e.selectPages([]))}};I(xe,e);const $e=e.activePage===i,Qe=e._selectedPagesIds.includes(i.id),qe=null==w?void 0:w.PageControls,et=null==w?void 0:w.Tooltip,tt=null==w?void 0:w.ContextMenu,nt=1/e.scale,it=0/e.scale;return n("div",{ref:we,onDragOver:e=>e.preventDefault(),onDrop:t=>{if(t.preventDefault(),e.role===Y.VIEWER){return}if(!ye.current){return}ye.current.setPointersPositions(t);const n=ye.current.findOne(".elements-container"),o=null==n?void 0:n.getRelativePointerPosition(),r=ye.current.getPointerPosition(),l=ye.current.getAllIntersections(r).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(l.reverse())].map(t=>e.getElementById(t.id())).filter(e=>!!e),a=s[0];he&&o&&(he(o,a,{elements:s,page:i}),he=null)},style:{position:"relative",width:r+"px",height:a+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+($e?" active-page":""),children:[t(x,{ref:ye,width:Math.min(r,k.width+200),height:Math.min(k.height+200,a),onClick:Ke,onTap:Ke,onContextMenu:t=>{if(t.evt.preventDefault(),e.role===Y.VIEWER){return}const n=t.target.findAncestor(".element",!0),i=e.getElementById(null==n?void 0:n.id()),o=null==i?void 0:i.top,r=null==o?void 0:o.id;if(!r){return e.selectElements([]),void Oe.close()}e.selectedElementsIds.indexOf(r)>=0||e.selectElements([r]),Oe.open({x:t.evt.clientX,y:t.evt.clientY})},onMouseDown:He,onMouseMove:t=>{if(!e.distanceGuidesVisible&&!t.evt.altKey){return void(Ye&&Se(null))}const n=t.target.findAncestor(".element",!0),i=null==n?void 0:n.id();Je(i)},onDragStart:t=>{var n;const i=t.target.getStage();if(i&&i.getPointersPositions().length>=2){return void t.target.stopDrag()}const o=t.target.findAncestor(".element",!0);if(o){const i=e.getElementById(null==o?void 0:o.id()),r=null==i?void 0:i.top,l=null==r?void 0:r.id;if(!(i&&e.selectedElementsIds.indexOf(i.id)>=0||l&&e.selectedElementsIds.indexOf(l)>=0)&&l){e.selectElements([l]);const i=Fe();t.target.stopDrag(),i.forEach(e=>{e.isDragging()||e.startDrag(t)}),null===(n=xe.current)||void 0===n||n.startDrag(t),M()}}Ye&&Se(null)},pageId:i.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:n(u,{children:[t(ie,{width:r,height:a,fill:J.workspaceBackgroundColor}),t(c,{x:ue,y:pe,scaleX:e.scale,scaleY:e.scale,name:"page-container",children:n(c,{name:"page-container-2",children:[t(c,{name:"page-background-group",x:re.left,y:re.top,children:t(le,{x:-oe.left,y:-oe.top,width:i.computedWidth+oe.left+oe.right,height:i.computedHeight+oe.top+oe.bottom,background:i.background,transparencyBackgroundColor:J.transparencyBackgroundColor,name:"page-background",preventDefault:!1,scale:e.scale})}),n(c,{x:re.left,y:re.top,name:"elements-container",listening:!e.isPlaying&&e.role!==Y.VIEWER,children:[t(f,{name:"elements-area",width:i.computedWidth,height:i.computedHeight,listening:!1}),t(ce,{elements:i.children,store:e})]}),t(p,{name:"bleed",fill:J.bleedColor,listening:!1,visible:e.bleedVisible&&(re.top>0||re.right>0||re.bottom>0||re.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:o,bottom:r,left:l}=oe,s=i.computedWidth,a=i.computedHeight,d=e._context;d.beginPath(),d.rect(0,0,l+s+o,n+a+r),d.rect(l,n,s,a),d.fillStyle=t.fill(),d.fill("evenodd")}}),R()===ge&&t(fe,{name:"hit-detection",x:-nt/2-it,y:-nt/2-it,width:ae+nt+2*it,height:me+nt+2*it})]})}),t(g,{name:"workspace-background",points:[0,0,r,0,r,a,0,a,0,0,ue,pe,ue,a-pe,r-ue,a-pe,r-ue,pe,ue,pe],listening:!1,closed:!0,fill:J.workspaceBackgroundColor}),t(c,{x:ue,y:pe,scaleX:e.scale,scaleY:e.scale,children:t(f,{name:"page-highlight",hideInExport:!0,x:-nt/2-it,y:-nt/2-it,width:ae+nt+2*it,height:me+nt+2*it,stroke:Qe?J.activePageBorderColor:J.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),n(c,{x:ue+re.left*e.scale,y:pe+re.top*e.scale,scaleX:e.scale,scaleY:e.scale,name:"page-abs-container",children:[t(y,{ref:xe,rotateAnchorAngle:Me,onDragStart:t=>{var n;const o=t.target.getStage();o&&o.getPointersPositions().length>=2?t.target.stopDrag():((null===(n=t.evt)||void 0===n?void 0:n.altKey)&&E&&e.selectedElements.forEach(e=>{const t=e.clone({},{skipSelect:!0}),n=i.children.indexOf(e);i.setElementZIndex(t.id,n)}),Xe.current||(Xe.current=!0,e.history.startTransaction()))},onDragEnd:()=>{Xe.current&&(e.history.endTransaction(),Xe.current=!1),Ue()},onTransformStart:()=>{e.history.startTransaction(),ve.current=new Map(Te.map(e=>[e.id,{width:e.width,height:e.height}])),Se(null)},boundBoxFunc:(t,n)=>{var i,o;const r=Math.abs(n.width)<1||Math.abs(n.height)<1,l=Math.abs(t.width)<1||Math.abs(t.height)<1;if(r&&!l){return t}const s=e.selectedElements;if(1===s.length&&"table"===(null===(i=s[0])||void 0===i?void 0:i.type)){const e=s[0],i=Math.abs(t.width),o=Math.abs(t.height);if(i>0&&o>0){const r=Math.abs(n.width)/i,l=Math.abs(n.height)/o,s=e.width*r,a=e.height*l,d=Object.assign({},n);return s<e.minWidth&&(d.width=t.width),a<e.minHeight&&(d.height=t.height),te(d,n,t)}}return Be&&Te.length>0?((e,t,n,i,o)=>{const r=Math.abs(e.width),l=Math.abs(e.height);if(0===r||0===l){return t}const s=Math.abs(t.width)/r,a=Math.abs(t.height)/l,d=!V(n),c=e=>i.some(t=>{var n;const i=null==o?void 0:o.get(t.id),r=null!==(n="x"===e?null==i?void 0:i.width:null==i?void 0:i.height)&&void 0!==n?n:"x"===e?t.width:t.height,l="x"===e?t.width:t.height,c="x"===e?s:a,h=((e,t,n,i)=>{if("table"===e.type){const n=e;return"x"===t?n.minWidth:n.minHeight}return"text"===e.type&&"x"===t&&n?F(i,e.fontSize):1})(t,e,d,r);return r>=h&&l*c<h}),h=Object.assign({},t),m=t.width*e.width<0,u=t.height*e.height<0,g=m||c("x"),f=u||c("y"),p=f||!d&&g;return(g||!d&&f)&&(h.width=e.width),p&&(h.height=e.height),te(h,t,e)})(t,n,(null===(o=xe.current)||void 0===o?void 0:o.getActiveAnchor())||"",Te,ve.current):n},onTransform:e=>{const t=xe.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{ze()},0)},onTransformEnd:t=>{Ae({}),e.history.endTransaction(),ve.current=null,Ue(),Fe()},visible:!e.isPlaying}),Ye&&Ye.map(({points:i,distance:o,box1:r,box2:l},s)=>n(c,{name:"distances-container",hideInExport:!0,listening:!1,children:[t(f,Object.assign({},r,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(f,Object.assign({},l,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(g,{points:[i[0].x,i[0].y,i[1].x,i[1].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),t(g,{points:[i[1].x,i[1].y,i[2].x,i[2].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(m,{x:(i[0].x+i[1].x)/2,y:(i[0].y+i[1].y)/2,offsetY:-10,scaleX:1/e.scale,scaleY:1/e.scale,children:[t(v,{cornerRadius:5,fill:J.distanceGuideStyle.labelFill,pointerDirection:"down"}),t(b,{align:"center",verticalAlign:"middle",fill:J.distanceGuideStyle.labelTextFill,padding:5,text:L({unit:e.unit,dpi:e.dpi,px:o})})]})]},s)),i._rendering&&n(c,{children:[t(f,{width:ae,height:me,fill:"rgba(255,255,255,0.9)"}),t(b,{text:"Rendering...",fontSize:60,width:ae,height:me,align:"center",verticalAlign:"middle"})]}),et&&t(et,{components:w,store:e,page:i,stageRef:ye,tooltipSafeArea:P}),tt&&t(H,{children:t(tt,Object.assign({components:w,store:e},Oe.props))})]}),t(A,{store:e,page:i,width:r,height:a,scale:e.scale,xPadding:ue,yPadding:pe,bleedLeft:re.left,bleedTop:re.top}),t(de,Object.assign({},ke,{store:e})),t(se,{selection:Ze}),j()>0&&n(c,{hideInExport:!0,children:[t(be,{name:"cache-bounds",x:r-(1===j()?250:210),y:a-(1===j()?95:45)}),t(be,{name:"cache-bounds-t",x:10,y:10})]}),D()&&t(b,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:r-170,y:a-18,onMouseEnter:e=>{e.target.getStage().container().style.cursor="pointer"},onMouseLeave:e=>{e.target.getStage().container().style.cursor=""},onTouchStart:e=>{e.cancelBubble=!0},onMouseDown:e=>{e.cancelBubble=!0},onClick:()=>{window.open("https://polotno.com","_blank","noopener")},onTap:()=>{window.open("https://polotno.com","_blank","noopener")}}),t(c,{name:"line-guides"})]})}),Ee&&qe&&t(qe,{store:e,page:i,xPadding:ue,yPadding:pe,layout:O})]})});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"quill";import{firstStopColor as e}from"@polotno/core/gradient";import{
|
|
1
|
+
import t from"quill";import{firstStopColor as e}from"@polotno/core/gradient";import{parsePrimaryFontFamily as r}from"@polotno/core/rich-text-html";import{cssFontFamily as o}from"@polotno/core/fonts";import{isClipGradientSpan as i}from"../utils/rich-text-html.js";let l=!1;const s=["background-image","background-size","-webkit-background-clip","background-clip","-webkit-text-fill-color","color"];export function registerQuillUnrestrictedFontFormats(){if(l){return}l=!0;const n=t.import("attributors/style/size"),c=t.import("attributors/style/font");n&&(delete n.whitelist,t.register(n,!0)),c&&(delete c.whitelist,c.add=(t,e)=>(t.style.fontFamily=o(e),!0),c.value=t=>r(t.style.fontFamily)||"",t.register(c,!0));const u=t.import("parchment");if((null==u?void 0:u.StyleAttributor)&&(null==u?void 0:u.Scope)){const r=new u.StyleAttributor("font-weight","font-weight",{scope:u.Scope.INLINE});t.register(r,!0),t.register(function(t){class r extends t.StyleAttributor{constructor(t,e,r){super(t,e,r)}add(t,r){if("string"!=typeof r||!r.includes("gradient")){return!1}t.style.setProperty("background-image",r),t.style.setProperty("background-size","100% 100%"),t.style.setProperty("-webkit-background-clip","text"),t.style.setProperty("background-clip","text"),t.style.setProperty("-webkit-text-fill-color","transparent");try{t.style.setProperty("color",e(r))}catch(o){}return!0}remove(t){for(const e of s){t.style.removeProperty(e)}}value(t){return i(t)?t.style.getPropertyValue("background-image"):""}}return new r("gradient","background-image",{scope:t.Scope.INLINE})}(u),!0);const o=t.import("attributors/style/color");if(o){const e=o.value.bind(o);o.value=t=>i(t)?"":e(t),t.register(o,!0)}}}
|
|
@@ -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 f}from"react-konva-utils";import{layout as u,drawLayout as g}from"render-tag";import{layoutTextOnPath as m,drawTextOnPathLayout as x}from"render-tag/path";import{scaledFontSizeAttrs as p,roundedFontSizeAttrs as y,roundedTextDecorationAttrs as b,textMinResizeWidth as w}from"../utils/px.js";import{flags as v}from"../utils/flags.js";import{applyFilter as O}from"./apply-filters.js";import{getLineHeight as E,usePrevious as S}from"./text-element.js";import{useFontSpecsLoader as j}from"./text-element/font-specs-loader.js";import{getHtml as M,getCurvePath as z,getCurveTextHeight as k}from"@polotno/core/text-html";import{useColor as R}from"./use-color.js";import{isGradient as Y}from"@polotno/core/gradient";import{detectSize as X}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as F}from"@polotno/core/background-shape";import{useFadeIn as T}from"./use-fadein.js";import{incrementLoader as I}from"../utils/loader.js";import{removeTags as P}from"@polotno/core/text";import{isTouchDevice as C}from"../utils/screen.js";import{getLimitedFontSize as B}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as A,extractFontSpecsFromHtml as W,scaleRichTextFontSizesInHtml as D}from"../utils/rich-text-html.js";import{RichEditor as J}from"./rich-editor.js";import{findFitFontSize as N}from"./text-element/fit-font-size.js";import{visiblePlaceholder as $}from"./text-element/placeholder.js";import{wrapHtmlForRenderTag as _}from"@polotno/core/reset-style";import{xmlEscape as L}from"@polotno/core/xml";import{hasMeasuredHeight as H,resolveTextBoxHeight as V}from"@polotno/core/text-types";export{_ as wrapHtmlForRenderTag};export const RenderTagElement=i(({element:i,store:q})=>{var G;const K=n.useRef(null),Q=n.useRef(null),[U,Z]=n.useState(!1),tt=n.useRef(i.height),et=i.isSelected,rt=S(et),{textVerticalResizeEnabled:ot}=v,nt=v.autoDeleteEmptyText,it=S(i.fontFamily),at=n.useMemo(()=>A(i.text,{fontSize:i.fontSize,fontFamily:i.fontFamily}),[i.text,i.fontSize,i.fontFamily]),st=n.useMemo(()=>W(at,{fontFamily:i.fontFamily,fontWeight:i.fontWeight,fontStyle:i.fontStyle}),[at,i.fontFamily,i.fontWeight,i.fontStyle]),[lt]=j(q,st,i.text||i.placeholder),ct=i._editModeEnabled;T(K);const ht=lt?i.fontFamily:it&&it!==i.fontFamily?it:"Arial",dt=R(i).fill,ft=Y(i.fill),ut=Y(i.stroke)&&!!i.strokeWidth,gt=!!i.strokeWidth&&(ut||ft),mt="ellipsis"===v.textOverflow&&!i.curveEnabled&&H(i)&&!gt,xt=$(i.placeholder),pt=!P(i.text)&&!!xt,yt=pt?L(xt):i.text,bt=pt?.6:i.a.opacity,wt=M(pt?Object.assign(Object.assign({},i.toJSON()),{text:yt}):i,{fontFamily:ht,color:dt,forEditor:!mt,textOverflow:v.textOverflow}),vt=i.width||1,Ot=i.curveEnabled,Et=function(t,e,r,o){return n.useMemo(()=>{if(o){return null}try{return u({html:_(t),width:Math.max(1,e),accuracy:"performance"})}catch(r){return console.error("render-tag layout failed:",r),null}},[t,e,r,o])}(wt,vt,lt,Ot),St=function(t,e,r){return n.useMemo(()=>{if(!e.curveEnabled){return null}const r=e.width||1,o=k(e),n=z(r,o,e.curvePower,e.fontSize);try{return m({html:_(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])}(wt,i,lt),jt=E({fontLoaded:lt,fontFamily:i.fontFamily,fontSize:i.fontSize,lineHeight:i.lineHeight}),Mt=Ot?0:Math.max(null!==(G=null==Et?void 0:Et.height)&&void 0!==G?G:0,i.fontSize*jt),zt=St?St.bounds:null,kt=n.useMemo(()=>Et?Et.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})):[],[Et]),Rt=n.useRef([]),Yt=n.useRef(!0),[,Xt]=n.useReducer(t=>t+1,0);n.useEffect(()=>{const t=Rt.current;t.length>0&&(Rt.current=[],t.forEach(t=>t()))});const Ft=n.useCallback((t,e,r,o)=>{const n=I(`text ${i.id}`),a=()=>{q.history.ignore(t,e,r).then(()=>{Yt.current?(Rt.current.push(n),Xt()):n()})};o?queueMicrotask(a):a()},[i.id,q]);n.useEffect(()=>{if(!lt){return}if(!Et){return}if(!H(i)){return void Ft(()=>{d(i)&&(H(i)||i.set({height:Mt}))},void 0,void 0,!0)}if(q.isPlaying){return}const{textOverflow:t}=v,e=i.height!==Mt&&(!ot||i.height<Mt),r=()=>Ft(()=>{d(i)&&i.set({height:Mt})},!1,!0);if("change-font-size"!==t||U||ct||i.curveEnabled){"resize"===t&&e&&!U&&r()}else{const t=N(pt?Object.assign(Object.assign({},i.toJSON()),{text:yt,toJSON:()=>Object.assign(Object.assign({},i.toJSON()),{text:yt})}):i,{fontFamily:ht,color:dt,forEditor:!0},t=>{try{return u({html:_(t),width:vt,accuracy:"performance"}).height}catch(G){return X(t).height}});t.fontSize!==i.fontSize?Ft(()=>{if(!d(i)){return}const e=p(i,t.fontSize);void 0!==t.text&&t.text!==i.text&&(e.text=t.text),i.set(e)},!1,!0):e&&r()}}),n.useEffect(()=>()=>{Yt.current=!1;const t=Rt.current;t.length>0&&(Rt.current=[],t.forEach(t=>t()))},[]),n.useLayoutEffect(()=>{if(!lt||!i.curveEnabled||U){return}if(!zt||!zt.width){return}const t=t=>t&&ot?Math.max(t,zt.height):zt.height;Math.abs(zt.width-i.width)<1&&Math.abs(t(i.height)-i.height)<1||Ft(()=>{if(!d(i)){return}const e=zt.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:zt.width,height:r,x:i.a.x+a,y:i.a.y+s})},!1,!0,!0)},[lt,i.curveEnabled,zt,U,ot,Ft]),n.useEffect(()=>{rt&&!et&&nt&&""===P(i.text)&&i.removable&&!$(i.placeholder)&&q.deleteElements([i.id])},[nt,i.placeholder,i.removable,i.text,et,rt]),n.useEffect(()=>h(i,()=>{const t=K.current;t&&O(t,i)}),[Et,St,i.a.width,i.a.height,i.shadowColor,i.shadowOffsetX,i.shadowOffsetY,i.shadowOpacity]);const Tt=V(i,Mt);let It=0;Ot||"middle"!==i.verticalAlign?Ot||"bottom"!==i.verticalAlign||(It=Tt-Mt):It=(Tt-Mt)/2;const Pt=i.backgroundPadding*(i.fontSize*jt*.5),Ct=i.backgroundCornerRadius*(i.fontSize*jt*.5),Bt=n.useMemo(()=>!i.backgroundEnabled||i.curveEnabled||i.legacyBackground||0===kt.length?"":F({rects:kt,padding:Pt,cornerRadius:Ct}),[i.backgroundEnabled,i.curveEnabled,i.legacyBackground,kt,Pt,Ct]),At=C(),Wt=n.useRef(null),Dt=n.useRef(i.width),Jt=n.useRef(null),Nt=n.useRef(null),$t=n.useRef(null),_t=n.useRef(0),Lt=n.useRef(0),Ht=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:Pt/Ht,offsetY:Pt/Ht,rotation:i.a.rotation,scaleX:Ht,scaleY:Ht,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled,opacity:i.backgroundOpacity*bt,fill:i.backgroundColor,width:i.width+2*Pt,height:Tt+2*Pt,cornerRadius:Ct}):e(s,{name:"rendertag-background",x:i.a.x,y:i.a.y,offsetY:-It/Ht,rotation:i.a.rotation,scaleX:Ht,scaleY:Ht,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled&&Bt.length>0,opacity:i.backgroundOpacity*bt,fill:i.backgroundColor,data:Bt}),e(c,{ref:K,name:"rendertag-text",x:i.a.x,y:i.a.y,rotation:i.a.rotation,scaleX:Ht,scaleY:Ht,offsetY:Ht>0?-It/Ht:0,width:i.width,height:Math.max(i.height,Mt),listening:!1,visible:!ct&&!!(Ot?St:Et),opacity:bt,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(Ot){if(!St||!zt){return}const t=(i.width-zt.width)/2-zt.x;r.save(),r.translate(t,-zt.y),x({layout:St,ctx:r}),r.restore()}else{if(!Et){return}g({layout:Et,width:vt,ctx:r,pixelRatio:1})}}catch(o){console.error("render-tag draw failed:",o)}}}}),e(l,{ref:Q,name:"element",x:i.a.x,y:i.a.y,listening:i.selectable,rotation:i.a.rotation,width:i.a.width,height:i.a.height,visible:!ct,draggable:At?i.draggable&&et:i.draggable,preventDefault:!At||et,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&&(Nt.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];Nt.current=r?{x:r.clientX,y:r.clientY}:null,i.toggleEditMode(!0)}},onTransformStart:t=>{var e,r;Z(!0),tt.current=i.height,Dt.current=i.width,Jt.current=null,Wt.current=t.target.position();const o=null===(e=t.target.getStage())||void 0===e?void 0:e.findOne("Transformer");$t.current=null!==(r=null==o?void 0:o.getActiveAnchor())&&void 0!==r?r:null,_t.current=i.a.rotation,Lt.current=i.height},onTransform:e=>{var r;const o=e.target,n=(null===(r=o.getStage())||void 0===r?void 0:r.findOne("Transformer")).getActiveAnchor(),a="middle-left"===n||"middle-right"===n,s="top-center"===n||"bottom-center"===n,l=o.scaleX();if(a){const e=o.scaleX(),n=o.width()*e;let a=Math.max(n,w(Dt.current,i.fontSize));const s=i.toJSON();let l=0;if(i.curveEnabled){l=k(Object.assign(Object.assign({},s),{width:a}))}else{const{x:e,y:o,width:n,height:i}=s,c=t(s,["x","y","width","height"]),h=`${JSON.stringify(c)}|${ht}|${lt}|${dt}`;if(Jt.current&&Jt.current.signature===h&&a<=Jt.current.width){a=Jt.current.width,l=Jt.current.height}else{const t=t=>{const e=M(Object.assign(Object.assign({},s),{text:yt,width:t}),{fontFamily:ht,color:dt,forEditor:!0});try{return u({html:_(e),width:t,accuracy:"performance"})}catch(r){return{height:X(e).height,lines:[]}}};let e=t(a);const o=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width));o>a+.5&&o<=Dt.current+.5&&(a=Math.min(Math.ceil(o),Dt.current),e=t(a),Jt.current={signature:h,width:a,height:e.height}),l=e.height}}a>n&&Wt.current&&o.position(Wt.current),o.width(a),o.scaleX(1),o.scaleY(1);let c=v.textVerticalResizeEnabled?Math.max(l,tt.current):l;"ellipsis"===v.textOverflow&&(c=tt.current),o.height(c),i.set({width:o.width(),height:c,x:o.x(),y:o.y()})}else if(s){const t="resize"===v.textOverflow?Ot?(null==zt?void 0:zt.height)||i.fontSize:Mt:jt*i.fontSize,e=o.height()*o.scaleY();if(e<t){if(o.height(t),Wt.current){if("top-center"===$t.current){const e=_t.current*Math.PI/180,r=Lt.current-t;o.x(Wt.current.x-r*Math.sin(e)),o.y(Wt.current.y+r*Math.cos(e))}else{o.position(Wt.current)}}}else{o.height(e)}o.scaleY(1),o.scaleX(1),Lt.current=Math.max(t,e),i.set({x:o.x(),y:o.y(),height:o.height(),rotation:o.rotation()})}else{o.scaleX(1),o.scaleY(1),i.set({fontSize:i.fontSize*l,letterSpacing:i.letterSpacing,width:o.width()*l,x:o.x(),y:o.y(),rotation:o.rotation(),height:o.height()*l,shadowBlur:i.shadowBlur*l,shadowOffsetX:i.shadowOffsetX*l,shadowOffsetY:i.shadowOffsetY*l,strokeWidth:i.strokeWidth*l,text:D(i.text,l)})}Wt.current=e.target.position()},onTransformEnd:t=>{Z(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),i.set(Object.assign(Object.assign({},y(i,i.fontSize*e)),{width:t.target.width()*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),text:D(i.text,e)}))}}),ct&&e(a,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,offsetY:-It,children:e(f,{divProps:{style:{zIndex:20}},children:e(J,{element:i,onChange:t=>{const e=B({oldText:P(i.text),newText:P(t),element:i});i.set(Object.assign({text:t},p(i,e)))},onBlur:()=>{d(i)&&(i.set(b(i)),i.toggleEditMode(!1))},clickCoords:Nt.current})})})]})});
|
|
1
|
+
var t=this&&this.__rest||function(t,e){var r={};for(var o in t){Object.prototype.hasOwnProperty.call(t,o)&&e.indexOf(o)<0&&(r[o]=t[o])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(o=Object.getOwnPropertySymbols(t);n<o.length;n++){e.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(t,o[n])&&(r[o[n]]=t[o[n]])}}return r};import{jsx as e,Fragment as r,jsxs as o}from"react/jsx-runtime";import n from"react";import{observer as i}from"mobx-react-lite";import{Group as a,Path as s,Rect as l,Shape as c}from"react-konva";import{nodeAutorun as h}from"../utils/mobx.js";import{isAlive as d}from"mobx-state-tree";import{Html as u}from"react-konva-utils";import{layout as f,drawLayout as g}from"render-tag";import{layoutTextOnPath as m,drawTextOnPathLayout as x}from"render-tag/path";import{activeAnchorOf as p,scaledFontSizeAttrs as y,roundedFontSizeAttrs as w,roundedTextDecorationAttrs as b,textMinResizeWidth as v}from"../utils/px.js";import{flags as E}from"../utils/flags.js";import{applyFilter as O}from"./apply-filters.js";import{getLineHeight as M,usePrevious as S}from"./text-element.js";import{useFontSpecsLoader as j}from"./text-element/font-specs-loader.js";import{getHtml as z,getCurvePath as k,getCurveTextHeight as R}from"@polotno/core/text-html";import{useColor as Y}from"./use-color.js";import{isGradient as X}from"@polotno/core/gradient";import{detectSize as F}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as I}from"@polotno/core/background-shape";import{useFadeIn as P}from"./use-fadein.js";import{incrementLoader as C}from"../utils/loader.js";import{removeTags as T}from"@polotno/core/text";import{isTouchDevice as B}from"../utils/screen.js";import{getLimitedFontSize as W}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as D,extractFontSpecsFromHtml as N,scaleRichTextFontSizesInHtml as $}from"../utils/rich-text-html.js";import{RichEditor as J}from"./rich-editor.js";import{findFitFontSize as _}from"./text-element/fit-font-size.js";import{visiblePlaceholder as A}from"./text-element/placeholder.js";import{wrapHtmlForRenderTag as L}from"@polotno/core/reset-style";import{xmlEscape as V}from"@polotno/core/xml";import{hasMeasuredHeight as H,resolveTextBoxHeight as q}from"@polotno/core/text-types";export{L as wrapHtmlForRenderTag};function G(t,e){return t!==e&&(!E.textVerticalResizeEnabled||t<e)}export const RenderTagElement=i(({element:i,store:K})=>{var Q;const U=n.useRef(null),Z=n.useRef(null),[tt,et]=n.useState(!1),rt=n.useRef(i.height),ot=i.isSelected,nt=S(ot),{textVerticalResizeEnabled:it}=E,at=E.autoDeleteEmptyText,st=S(i.fontFamily),lt=n.useMemo(()=>D(i.text,{fontSize:i.fontSize,fontFamily:i.fontFamily}),[i.text,i.fontSize,i.fontFamily]),ct=n.useMemo(()=>N(lt,{fontFamily:i.fontFamily,fontWeight:i.fontWeight,fontStyle:i.fontStyle}),[lt,i.fontFamily,i.fontWeight,i.fontStyle]),[ht]=j(K,ct,i.text||i.placeholder),dt=i._editModeEnabled;P(U);const ut=ht?i.fontFamily:st&&st!==i.fontFamily?st:"Arial",ft=Y(i).fill,gt=X(i.fill),mt=X(i.stroke)&&!!i.strokeWidth,xt=!!i.strokeWidth&&(mt||gt),pt="ellipsis"===E.textOverflow&&!i.curveEnabled&&H(i)&&!xt,yt=A(i.placeholder),wt=!!yt&&!T(i.text),bt=wt?V(yt):lt,vt=wt?.6:i.a.opacity,Et=z(i,{fontFamily:ut,color:ft,forEditor:!pt,textOverflow:E.textOverflow,text:bt,textIsNormalized:!0}),Ot=i.width||1,Mt=i.curveEnabled,St=function(t,e,r,o){return n.useMemo(()=>{if(o){return null}try{return f({html:L(t),width:Math.max(1,e),accuracy:"performance"})}catch(r){return console.error("render-tag layout failed:",r),null}},[t,e,r,o])}(Et,Ot,ht,Mt),jt=function(t,e,r){return n.useMemo(()=>{if(!e.curveEnabled){return null}const r=e.width||1,o=R(e),n=k(r,o,e.curvePower,e.fontSize);try{return m({html:L(t),path:n,align:"center",textBaseline:"middle"})}catch(i){return console.error("render-tag/path layoutTextOnPath failed:",i),null}},[r,e.curveEnabled,e.curvePower,e.fontSize,e.width,e.height,t])}(Et,i,ht),zt=M({fontLoaded:ht,fontFamily:i.fontFamily,fontSize:i.fontSize,lineHeight:i.lineHeight}),kt=Mt?0:Math.max(null!==(Q=null==St?void 0:St.height)&&void 0!==Q?Q:0,i.fontSize*zt),Rt=jt?jt.bounds:null,Yt=n.useMemo(()=>St?St.lines.map(t=>({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height})):[],[St]),Xt=n.useRef([]),Ft=n.useRef(null),It=n.useRef(!0),[,Pt]=n.useReducer(t=>t+1,0);n.useEffect(()=>{const t=Xt.current;t.length>0&&(Xt.current=[],t.forEach(t=>t()))});const Ct=n.useCallback((t,e,r,o)=>{const n=C(`text ${i.id}`),a=()=>{K.history.ignore(t,e,r).then(()=>{It.current?(Xt.current.push(n),Pt()):n()})};o?queueMicrotask(a):a()},[i.id,K]),Tt=n.useCallback(t=>{const e=null!==Ft.current;Ft.current=t,e||Ct(()=>{const t=Ft.current;Ft.current=null,d(i)&&G(i.height,t)&&i.set({height:t})},!1,!0)},[i,Ct]);n.useEffect(()=>{if(!ht){return}if(!St){return}if(!H(i)){return void Ct(()=>{d(i)&&(H(i)||i.set({height:kt}))},void 0,void 0,!0)}if(K.isPlaying){return}const{textOverflow:t}=E,e=G(i.height,kt),r=()=>Tt(kt);if("change-font-size"!==t||tt||dt||i.curveEnabled){"resize"===t&&e&&!tt&&r()}else{const t=_(wt?{toJSON:()=>Object.assign(Object.assign({},i.toJSON()),{text:bt})}:i,{fontFamily:ut,color:ft,forEditor:!0},t=>{try{return f({html:L(t),width:Ot,accuracy:"performance"}).height}catch(Q){return F(t).height}});t.fontSize!==i.fontSize?Ct(()=>{if(!d(i)){return}const e=y(i,t.fontSize);void 0!==t.text&&t.text!==i.text&&(e.text=t.text),i.set(e)},!1,!0):e&&r()}}),n.useEffect(()=>()=>{It.current=!1;const t=Xt.current;t.length>0&&(Xt.current=[],t.forEach(t=>t()))},[]),n.useLayoutEffect(()=>{if(!ht||!i.curveEnabled||tt){return}if(!Rt||!Rt.width){return}const t=t=>t&&it?Math.max(t,Rt.height):Rt.height;Math.abs(Rt.width-i.width)<1&&Math.abs(t(i.height)-i.height)<1||Ct(()=>{if(!d(i)){return}const e=Rt.width-i.width,r=t(i.height),o=r-i.height;if(Math.abs(e)<1&&Math.abs(o)<1){return}const n=i.a.rotation*Math.PI/180,a=-e/2*Math.cos(n),s=-e/2*Math.sin(n);i.set({width:Rt.width,height:r,x:i.a.x+a,y:i.a.y+s})},!1,!0,!0)},[ht,i.curveEnabled,Rt,tt,it,Ct]),n.useEffect(()=>{nt&&!ot&&at&&""===T(i.text)&&i.removable&&!A(i.placeholder)&&K.deleteElements([i.id])},[at,i.placeholder,i.removable,i.text,ot,nt]),n.useEffect(()=>h(i,()=>{const t=U.current;t&&O(t,i)}),[St,jt,i.a.width,i.a.height,i.shadowColor,i.shadowOffsetX,i.shadowOffsetY,i.shadowOpacity]);const Bt=q(i,kt),Wt=Mt||!ht||tt||K.isPlaying||"resize"!==E.textOverflow?i.a.height:it?Math.max(i.a.height,kt):kt;let Dt=0;Mt||"middle"!==i.verticalAlign?Mt||"bottom"!==i.verticalAlign||(Dt=Bt-kt):Dt=(Bt-kt)/2;const Nt=i.backgroundPadding*(i.fontSize*zt*.5),$t=i.backgroundCornerRadius*(i.fontSize*zt*.5),Jt=n.useMemo(()=>!i.backgroundEnabled||i.curveEnabled||i.legacyBackground||0===Yt.length?"":I({rects:Yt,padding:Nt,cornerRadius:$t}),[i.backgroundEnabled,i.curveEnabled,i.legacyBackground,Yt,Nt,$t]),_t=B(),At=n.useRef(null),Lt=n.useRef(i.width),Vt=n.useRef(null),Ht=n.useRef(null),qt=n.useRef(""),Gt=n.useRef(0),Kt=n.useRef(0),Qt=i.width>0?i.a.width/i.width:1;return o(r,{children:[i.curveEnabled||i.legacyBackground?e(l,{x:i.a.x,y:i.a.y,offsetX:Nt/Qt,offsetY:Nt/Qt,rotation:i.a.rotation,scaleX:Qt,scaleY:Qt,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled,opacity:i.backgroundOpacity*vt,fill:i.backgroundColor,width:i.width+2*Nt,height:Bt+2*Nt,cornerRadius:$t}):e(s,{name:"rendertag-background",x:i.a.x,y:i.a.y,offsetY:-Dt/Qt,rotation:i.a.rotation,scaleX:Qt,scaleY:Qt,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled&&Jt.length>0,opacity:i.backgroundOpacity*vt,fill:i.backgroundColor,data:Jt}),e(c,{ref:U,name:"rendertag-text",x:i.a.x,y:i.a.y,rotation:i.a.rotation,scaleX:Qt,scaleY:Qt,offsetY:Qt>0?-Dt/Qt:0,width:i.width,height:Math.max(i.height,kt),listening:!1,visible:!dt&&!!(Mt?jt:St),opacity:vt,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,hideInExport:!i.showInExport,sceneFunc:(t,e)=>{const r=t._context;if(r){try{if(Mt){if(!jt||!Rt){return}const t=(i.width-Rt.width)/2-Rt.x;r.save(),r.translate(t,-Rt.y),x({layout:jt,ctx:r}),r.restore()}else{if(!St){return}g({layout:St,width:Ot,ctx:r,pixelRatio:1})}}catch(o){console.error("render-tag draw failed:",o)}}}}),e(l,{ref:Z,name:"element",x:i.a.x,y:i.a.y,listening:i.selectable,rotation:i.a.rotation,width:i.a.width,height:Wt,visible:!dt,draggable:_t?i.draggable&&ot:i.draggable,preventDefault:!_t||ot,opacity:0,hideInExport:!0,id:i.id,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDblClick:t=>{i.contentEditable&&(Ht.current={x:t.evt.clientX,y:t.evt.clientY},i.toggleEditMode(!0))},onDblTap:t=>{var e;if(i.contentEditable){const r=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Ht.current=r?{x:r.clientX,y:r.clientY}:null,i.toggleEditMode(!0)}},onTransformStart:t=>{et(!0),rt.current=i.height,Lt.current=i.width,Vt.current=null,At.current=t.target.position(),qt.current=p(t.target),Gt.current=i.a.rotation,Kt.current=i.height},onTransform:e=>{const r=e.target,o=qt.current,n="middle-left"===o||"middle-right"===o,a="top-center"===o||"bottom-center"===o,s=r.scaleX();if(n){const e=r.scaleX(),o=r.width()*e;let n=Math.max(o,v(Lt.current,i.fontSize));const a=i.toJSON();let s=0;if(i.curveEnabled){s=R(Object.assign(Object.assign({},a),{width:n}))}else{const{x:e,y:r,width:o,height:i}=a,l=t(a,["x","y","width","height"]),c=`${JSON.stringify(l)}|${ut}|${ht}|${ft}`;if(Vt.current&&Vt.current.signature===c&&n<=Vt.current.width){n=Vt.current.width,s=Vt.current.height}else{const t=t=>{const e=z(Object.assign(Object.assign({},a),{width:t}),{fontFamily:ut,color:ft,forEditor:!0,text:bt,textIsNormalized:!0});try{return f({html:L(e),width:t,accuracy:"performance"})}catch(Q){return{height:F(e).height,lines:[]}}};let e=t(n);const r=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width));r>n+.5&&r<=Lt.current+.5&&(n=Math.min(Math.ceil(r),Lt.current),e=t(n),Vt.current={signature:c,width:n,height:e.height}),s=e.height}}n>o&&At.current&&r.position(At.current),r.width(n),r.scaleX(1),r.scaleY(1);let l=E.textVerticalResizeEnabled?Math.max(s,rt.current):s;"ellipsis"===E.textOverflow&&(l=rt.current),r.height(l),i.set({width:r.width(),height:l,x:r.x(),y:r.y()})}else if(a){const t="resize"===E.textOverflow?Mt?(null==Rt?void 0:Rt.height)||i.fontSize:kt:zt*i.fontSize,e=r.height()*r.scaleY();if(e<t){if(r.height(t),At.current){if("top-center"===qt.current){const e=Gt.current*Math.PI/180,o=Kt.current-t;r.x(At.current.x-o*Math.sin(e)),r.y(At.current.y+o*Math.cos(e))}else{r.position(At.current)}}}else{r.height(e)}r.scaleY(1),r.scaleX(1),Kt.current=Math.max(t,e),i.set({x:r.x(),y:r.y(),height:r.height(),rotation:r.rotation()})}else{r.scaleX(1),r.scaleY(1),i.set({fontSize:i.fontSize*s,letterSpacing:i.letterSpacing,width:r.width()*s,x:r.x(),y:r.y(),rotation:r.rotation(),height:r.height()*s,shadowBlur:i.shadowBlur*s,shadowOffsetX:i.shadowOffsetX*s,shadowOffsetY:i.shadowOffsetY*s,strokeWidth:i.strokeWidth*s,text:$(i.text,s)})}At.current=e.target.position()},onTransformEnd:t=>{et(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),i.set(Object.assign(Object.assign({},w(i,i.fontSize*e)),{width:t.target.width()*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),text:$(i.text,e)}))}}),dt&&e(a,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,offsetY:-Dt,children:e(u,{divProps:{style:{zIndex:20}},children:e(J,{element:i,onChange:t=>{const e=W({oldText:T(i.text),newText:T(t),element:i});i.set(Object.assign({text:t},y(i,e)))},onBlur:()=>{d(i)&&(i.set(b(i)),i.toggleEditMode(!1))},clickCoords:Ht.current})})})]})});
|
package/canvas/rich-editor.d.ts
CHANGED
|
@@ -6,10 +6,6 @@ export { applyOpacityToColor };
|
|
|
6
6
|
export declare const quillRef: {
|
|
7
7
|
enabled: boolean;
|
|
8
8
|
currentFormat: Record<string, any>;
|
|
9
|
-
lastSelection: {
|
|
10
|
-
index: number;
|
|
11
|
-
length: number;
|
|
12
|
-
} | null;
|
|
13
9
|
pendingToolbarInteraction: boolean;
|
|
14
10
|
editor: {
|
|
15
11
|
instance: any;
|
package/canvas/rich-editor.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
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}from"render-tag";import{removeTags as h}from"@polotno/core/text";import{cssFontFamily as g}from"@polotno/core/fonts";import{isGradient as b,firstStopColor as v}from"@polotno/core/gradient";import{getOptimalCaretColor as x}from"./text-element/caret-color.js";import{visiblePlaceholder as y}from"./text-element/placeholder.js";import{registerQuillUnrestrictedFontFormats as w}from"./quill-font-formats.js";import{ALL_QUILL_MIXED_KEYS as S,setActiveTextEditor as k,updateActiveTextFormat as q,clearActiveTextEditor as T,getActiveTextEditor as E}from"../utils/text-format-state.js";import C from"../utils/styled.js";import{applyOpacityToColor as L}from"@polotno/core/svg";export{L as applyOpacityToColor};export const quillRef=r.observable({enabled:!1,currentFormat:{},pendingToolbarInteraction:!1,editor:r.observable.object({instance:null},{},{deep:!1})});const F=S;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 F){c(n,r)&&(o[r]=!0)}return o}(e,t);r.runInAction(()=>{quillRef.currentFormat=o}),q(o,n,t)}const R="polotno-saved-selection";function $(){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(R,new Highlight(t)),M()}function O(){"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.delete(R)}function M(){setTimeout(()=>{"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.has(R)&&(document.querySelector(".polotno-portal")||I(document.activeElement)?M():O())},300)}if("undefined"!=typeof document&&"undefined"!=typeof CSS&&CSS.highlights&&!document.querySelector("[data-polotno-highlight]")){const D=document.createElement("style");D.setAttribute("data-polotno-highlight",""),D.textContent=`::highlight(${R}) { background-color: Highlight; }`,document.head.appendChild(D)}let j=null;function N(e,t){"text"===e.type&&t.push(e);for(const o of e.children||[]){N(o,t)}}export function computeMarkerVars(e){var t,o,n;const r=e.fontSize;if(!r||r<=0){return null}const i=g(e.fontFamily),l=u(e.fontWeight)?700:400,a="italic"===e.fontStyle?"italic":"normal",s=null!=j?j:j=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,h=`font-family:${i};font-size:${r}px;font-weight:${l};font-style:${a};line-height:${e.lineHeight};letter-spacing:${e.letterSpacing}em`,b=p(`<div style="${h}"><ul><li>x</li></ul><ol><li>x</li></ol></div>`);let v;try{v=m({html:b,width:Math.max(12*r,600)})}catch(T){return null}const x=[];N(v.layoutRoot,x);const y=x.find(e=>"•"===e.text),w=x.find(e=>/^\d/.test(e.text));if(!y){return null}const S=y.style.fontSize,k=S*(1+c-f)/2,q=2.1*r;return{bulletLeft:y.x,bulletTop:y.y-k,bulletFontSize:S,numberGap:w?Math.max(0,q-w.x-(null!==(n=w.width)&&void 0!==n?n:0)):0}}export const RichTextContainer=C("div",o.forwardRef)`
|
|
2
2
|
.ql-editor {
|
|
3
3
|
outline: none;
|
|
4
4
|
/* anchor for the absolutely-positioned ::before placeholder below */
|
|
@@ -6,6 +6,9 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
|
|
|
6
6
|
/* Element-level underline/strike — text-decoration doesn't inherit, so it is
|
|
7
7
|
applied here on the editable root (mirrors the static getHtml render). */
|
|
8
8
|
text-decoration: var(--polotno-text-decoration, none);
|
|
9
|
+
/* Chrome would otherwise break the underline at every descender; the
|
|
10
|
+
canvas draws one solid band. Mirrors getHtml (@polotno/core). */
|
|
11
|
+
text-decoration-skip-ink: none;
|
|
9
12
|
}
|
|
10
13
|
/* Placeholder for an empty editor. Quill toggles ql-blank + data-placeholder
|
|
11
14
|
but its core stylesheet (which paints it) is not loaded — do it here. Same
|
|
@@ -152,4 +155,4 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
|
|
|
152
155
|
&[style*='--polotno-gradient'] .ql-editor [style*='color:'] {
|
|
153
156
|
-webkit-text-fill-color: currentColor;
|
|
154
157
|
}
|
|
155
|
-
`;let
|
|
158
|
+
`;let z=["background","font-weight","color","gradient","font","italic","size","script","strike","underline","indent","list","direction"];export const setQuillFormats=e=>{z=e};let P=!1;function Q(){P||(P=!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 A(e){return e?"700":"400"}export const createQuill=(t,o)=>{w();const r=new n(t,{toolbar:!1,keyboard:!1,placeholder:(null==o?void 0:o.placeholder)||void 0,clipboard:{matchVisual:!1},formats:z});!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?(Q(),o("font-weight",A(t),n)):o(e,t,n),t.formatText=(t,o,r,i,l)=>{if(r&&"object"==typeof r){if("bold"in r){Q();const{bold:l}=r,a=e(r,["bold"]);return n(t,o,Object.assign(Object.assign({},a),{"font-weight":A(l)}),i)}return n(t,o,r,i)}return"bold"===r?(Q(),n(t,o,"font-weight",A(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:y(c.placeholder)}),r.runInAction(()=>{quillRef.editor.instance=t}),quillRef.pendingToolbarInteraction=!1,window.__polotnoQuill=t,k({instance:t,store:c.store,element:c});const o=e=>{e&&syncQuillFormatState(t,e)};t.on("text-change",()=>{const e=E(),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,w=null==v?void 0:v.scrollTop;u?setCursorFromCoords(t,u):t.setSelection(0,0,"api"),v&&void 0!==x&&void 0!==w&&(v.scrollLeft=x,v.scrollTop=w);const S=t.root;h=t=>{const o=t.relatedTarget;(null==o?void 0:o.classList.contains("ql-clipboard"))||(I(o)||quillRef.pendingToolbarInteraction?$():setTimeout(()=>{quillRef.pendingToolbarInteraction||I(document.activeElement)?$():(O(),e())},0))},g=()=>{O()},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),O(),i&&s&&f&&(i.removeEventListener("copy",s),i.removeEventListener("cut",f)),r.runInAction(()=>{quillRef.editor.instance=null,quillRef.currentFormat={}}),T(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(h(c.text))}`,()=>{const e=p.current;if(!e){return}const t="ltr"===s(h(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))||I(n)||quillRef.pendingToolbarInteraction||(O(),e())};return window.addEventListener("mousedown",t),window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",e),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t)}},[]);const w={color:c.fill};c.fill.indexOf("gradient")>=0&&(w["--polotno-gradient"]=c.fill,w.color="transparent"),c.strokeWidth>0&&(w.WebkitTextStroke=`${c.strokeWidth}px ${v(c.stroke)}`,w.paintOrder="stroke fill");const S=b(c.fill)?v(c.fill):x(c),q=c.a.rotation*Math.PI/180,C=c.shadowOffsetX*Math.cos(q)+c.shadowOffsetY*Math.sin(q),F=-c.shadowOffsetX*Math.sin(q)+c.shadowOffsetY*Math.cos(q);return t(RichTextContainer,{ref:p,style:Object.assign(Object.assign({},w),{fontSize:c.fontSize,fontFamily:g(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?`${C}px ${F}px ${c.shadowBlur}px ${L(c.shadowColor,null!==(f=c.shadowOpacity)&&void 0!==f?f:1)}`:void 0,caretColor:S}),dir:s(h(c.text))})};function I(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")}
|