polotno 4.15.3 → 4.16.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/blueprint/side-panel/select-video.d.ts +2 -0
- package/canvas/element.js +1 -1
- 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/render-tag-element.js +1 -1
- package/canvas/rich-editor.d.ts +3 -1
- package/canvas/rich-editor.js +2 -2
- package/canvas/text-element.js +1 -1
- package/canvas/use-color.d.ts +2 -2
- package/canvas/video-element.js +1 -1
- package/model/figure-model.d.ts +1 -0
- package/model/gif-model.d.ts +1 -0
- package/model/group-model.d.ts +12 -0
- package/model/image-model.d.ts +1 -0
- package/model/line-model.d.ts +1 -0
- package/model/node-model.d.ts +1 -0
- package/model/node-model.js +1 -1
- package/model/page-model.d.ts +36 -0
- package/model/page-model.js +1 -1
- package/model/shape-model.d.ts +1 -0
- package/model/store.d.ts +6 -0
- package/model/store.js +1 -1
- package/model/svg-model.d.ts +1 -0
- package/model/table-model.d.ts +1 -0
- package/model/text-model.d.ts +5 -0
- package/model/text-model.js +1 -1
- package/model/video-model.d.ts +1 -0
- package/package.json +21 -21
- package/polotno.bundle.js +166 -165
- package/primitives/numeric-input-delayed.d.ts +12 -0
- package/primitives/numeric-input-delayed.js +1 -0
- package/primitives/popover.js +1 -1
- package/side-panel/effects-panel.js +1 -1
- package/side-panel/layer-selection.d.ts +26 -0
- package/side-panel/layer-selection.js +1 -0
- package/side-panel/layers-panel.js +1 -1
- package/side-panel/select-video.d.ts +2 -0
- package/side-panel/size-panel.js +1 -1
- package/side-panel/text-effect-presets.d.ts +11 -0
- package/side-panel/text-effect-presets.js +1 -0
- package/toolbar/group-toolbar.js +1 -1
- package/toolbar/image-toolbar.d.ts +3 -2
- package/toolbar/image-toolbar.js +1 -1
- package/toolbar/many-toolbar.js +1 -1
- package/toolbar/opacity-picker.d.ts +3 -2
- package/toolbar/opacity-picker.js +1 -1
- package/toolbar/position-picker.js +1 -1
- package/toolbar/selection-colors.d.ts +18 -0
- package/toolbar/selection-colors.js +1 -0
- package/toolbar/selection-size.d.ts +12 -0
- package/toolbar/selection-size.js +1 -0
- package/toolbar/svg-toolbar.js +1 -1
- package/ui.css +1 -1
- package/utils/alignment.d.ts +16 -0
- package/utils/alignment.js +1 -1
- package/utils/array.d.ts +1 -0
- package/utils/array.js +1 -0
- package/utils/clipboard.d.ts +10 -0
- package/utils/duplicate.d.ts +2 -0
- package/utils/duplicate.js +1 -1
- package/utils/file.d.ts +1 -1
- package/utils/flags.d.ts +1 -1
- package/utils/l10n.d.ts +13 -0
- package/utils/l10n.js +1 -1
- package/utils/measure-text.d.ts +6 -3
- package/utils/measure-text.js +1 -1
- package/utils/px.d.ts +11 -18
- package/utils/px.js +1 -1
- package/utils/rich-text-html.d.ts +5 -0
- package/utils/rich-text-html.js +1 -1
- package/utils/selection-colors.d.ts +15 -0
- package/utils/selection-colors.js +1 -0
- package/utils/selection-transform.d.ts +19 -0
- package/utils/selection-transform.js +1 -0
- package/utils/selection.d.ts +3 -0
- package/utils/selection.js +1 -0
- package/utils/text-sizing.d.ts +5 -0
- package/utils/text-sizing.js +1 -0
- package/utils/use-svg-colors.d.ts +1 -0
- package/utils/use-svg-colors.js +1 -1
- package/utils/validate-key.js +1 -1
- package/canvas/text-element/fit-font-size.d.ts +0 -5
- package/canvas/text-element/fit-font-size.js +0 -1
|
@@ -148,6 +148,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
148
148
|
showInExport: boolean;
|
|
149
149
|
}>>;
|
|
150
150
|
readonly parent: any;
|
|
151
|
+
readonly isHighlighted: boolean;
|
|
151
152
|
readonly zIndex: any;
|
|
152
153
|
} & {
|
|
153
154
|
toJSON(): {
|
|
@@ -354,6 +355,7 @@ export declare const selectVideo: ({ src, droppedPos, targetElement, store, attr
|
|
|
354
355
|
showInExport: boolean;
|
|
355
356
|
}>>;
|
|
356
357
|
readonly parent: any;
|
|
358
|
+
readonly isHighlighted: boolean;
|
|
357
359
|
readonly zIndex: any;
|
|
358
360
|
} & {
|
|
359
361
|
toJSON(): {
|
package/canvas/element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as e,jsxs as t}from"react/jsx-runtime";import{createElement as o}from"react";import r from"react";import{observer as n}from"mobx-react-lite";import{ROLES as i}from"../model/store.js";import s from"konva";import{MAIN_TRANSFORMER_NAME as l}from"../utils/px.js";import{TextElement as a}from"./text-element.js";import{ImageElement as m}from"./image-element.js";import{HTMLElement as f}from"./html-element.js";import{RenderTagElement as p}from"./render-tag-element.js";import{LineElement as c}from"./line-element.js";import{VideoElement as d}from"./video-element.js";import{FigureElement as u}from"./figure-element.js";import{GifElement as g}from"./gif-element.js";import{TableElement as h}from"./table-element.js";import{Group as v}from"react-konva";import{Highlighter as x}from"./highlighter.js";import{forEveryChild as y}from"../model/group-model.js";import{flags as j}from"../utils/flags.js";import{blendModeOf as b,isIsolatedGroup as E}from"@polotno/core/page";const w={text:a,image:m,svg:m,line:c,video:d,figure:u,group:n(t=>{var r;const{element:n,store:s}=t,l=n,{children:a}=l,m=n.selectable||s.role===i.ADMIN,f=l.a;return e(v,{x:f.x,y:f.y,offsetX:f.offsetX,offsetY:f.offsetY,scaleX:f.scaleX,scaleY:f.scaleY,rotation:f.rotation,opacity:f.opacity,isolated:E(l),globalCompositeOperation:null!==(r=b(l))&&void 0!==r?r:"source-over",listening:m,hideInExport:!n.showInExport,id:n.id,children:a.map(e=>o(S,Object.assign({},t,{key:e.id,store:s,element:e})))})}),gif:g,table:h};export function registerShapeComponent(e,t){w[e]=t}const O=(e,t)=>{const o=[];y(e,e=>{"group"!==e.type&&o.push(e.a)});const r=[];o.forEach(e=>{const t=[{x:0,y:0},{x:0+e.width,y:0},{x:0+e.width,y:0+e.height},{x:0,y:0+e.height}],o=new s.Transform;o.translate(e.x,e.y),o.rotate(s.Util.degToRad(e.rotation)),t.forEach(e=>{const t=o.point(e);r.push(t)})});const n=new s.Transform;n.rotate(-s.Util.degToRad(t));let i=1/0,l=1/0,a=-1/0,m=-1/0;r.forEach(e=>{const t=n.point(e);i=Math.min(i,t.x),l=Math.min(l,t.y),a=Math.max(a,t.x),m=Math.max(m,t.y)}),n.invert();const f=n.point({x:i,y:l});return{x:f.x,y:f.y,width:a-i,height:m-l,rotation:t}},S=n(o=>{var n,i;const{element:s,store:a}=o,[m,c]=r.useState(!1),d=s.isSelectedDirectly,u="group"===(null===(n=s.parent)||void 0===n?void 0:n.type),g="group"!==s.type||u?null:a.selectedElements,h=!!g&&1===g.length&&g[0]!==s&&g[0].top===s,[y,E]=r.useState(!1),[S,T]=r.useState(null),C=r.useCallback(e=>{T(e?e.getStage():null)},[]),M=r.useRef(void 0),k=()=>(M.current||(M.current=null==S?void 0:S.findOne(`.${l}`)),M.current);r.useEffect(()=>{if(S){const e=e=>{const t=e.target.findAncestor(".element",!0),o=a.getElementById(null==t?void 0:t.id()),r=null==o?void 0:o.top,n=null==r?void 0:r.id;c(n===s.id)};S.on("mouseover",e);const t=()=>{c(!1)};return S.on("mouseleave",t),()=>{S.off("mouseover",e),S.off("mouseleave",t)}}},[S]),r.useEffect(()=>{if(!h){return}const e=k();if(!S||!e){return}E(!1);const t=()=>E(!0),o=()=>E(!1);return S.on("dragstart",t),S.on("dragend",o),e.on("transformstart",t),e.on("transformend",o),()=>{S.off("dragstart",t),S.off("dragend",o),e.off("transformstart",t),e.off("transformend",o)}},[S,h]);let I=w[o.element.type];if("text"===o.element.type&&(j.renderTagTextEnabled?I=p:j.htmlRenderEnabled&&(I=f)),!o.element.visible){return null}if(!I){return console.error("Can not find component for "+o.element.type),null}const R=b(s);return t(v,{name:"element-container",ref:C,visible:s.active,listening:s.active,children:["group"===s.type?e(I,Object.assign({},o)):e(v,{isolated:!!R,opacity:R?s.a.opacity:1,globalCompositeOperation:null!=R?R:"source-over",children:e(I,Object.assign({},o))}),(m||h)&&!d&&!
|
|
1
|
+
import{jsx as e,jsxs as t}from"react/jsx-runtime";import{createElement as o}from"react";import r from"react";import{observer as n}from"mobx-react-lite";import{ROLES as i}from"../model/store.js";import s from"konva";import{MAIN_TRANSFORMER_NAME as l}from"../utils/px.js";import{TextElement as a}from"./text-element.js";import{ImageElement as m}from"./image-element.js";import{HTMLElement as f}from"./html-element.js";import{RenderTagElement as p}from"./render-tag-element.js";import{LineElement as c}from"./line-element.js";import{VideoElement as d}from"./video-element.js";import{FigureElement as u}from"./figure-element.js";import{GifElement as g}from"./gif-element.js";import{TableElement as h}from"./table-element.js";import{Group as v}from"react-konva";import{Highlighter as x}from"./highlighter.js";import{forEveryChild as y}from"../model/group-model.js";import{flags as j}from"../utils/flags.js";import{blendModeOf as b,isIsolatedGroup as E}from"@polotno/core/page";const w={text:a,image:m,svg:m,line:c,video:d,figure:u,group:n(t=>{var r;const{element:n,store:s}=t,l=n,{children:a}=l,m=n.selectable||s.role===i.ADMIN,f=l.a;return e(v,{x:f.x,y:f.y,offsetX:f.offsetX,offsetY:f.offsetY,scaleX:f.scaleX,scaleY:f.scaleY,rotation:f.rotation,opacity:f.opacity,isolated:E(l),globalCompositeOperation:null!==(r=b(l))&&void 0!==r?r:"source-over",listening:m,hideInExport:!n.showInExport,id:n.id,children:a.map(e=>o(S,Object.assign({},t,{key:e.id,store:s,element:e})))})}),gif:g,table:h};export function registerShapeComponent(e,t){w[e]=t}const O=(e,t)=>{const o=[];y(e,e=>{"group"!==e.type&&o.push(e.a)});const r=[];o.forEach(e=>{const t=[{x:0,y:0},{x:0+e.width,y:0},{x:0+e.width,y:0+e.height},{x:0,y:0+e.height}],o=new s.Transform;o.translate(e.x,e.y),o.rotate(s.Util.degToRad(e.rotation)),t.forEach(e=>{const t=o.point(e);r.push(t)})});const n=new s.Transform;n.rotate(-s.Util.degToRad(t));let i=1/0,l=1/0,a=-1/0,m=-1/0;r.forEach(e=>{const t=n.point(e);i=Math.min(i,t.x),l=Math.min(l,t.y),a=Math.max(a,t.x),m=Math.max(m,t.y)}),n.invert();const f=n.point({x:i,y:l});return{x:f.x,y:f.y,width:a-i,height:m-l,rotation:t}},S=n(o=>{var n,i;const{element:s,store:a}=o,[m,c]=r.useState(!1),d=s.isSelectedDirectly,u="group"===(null===(n=s.parent)||void 0===n?void 0:n.type),g="group"!==s.type||u?null:a.selectedElements,h=!!g&&1===g.length&&g[0]!==s&&g[0].top===s,[y,E]=r.useState(!1),[S,T]=r.useState(null),C=r.useCallback(e=>{T(e?e.getStage():null)},[]),M=r.useRef(void 0),k=()=>(M.current||(M.current=null==S?void 0:S.findOne(`.${l}`)),M.current);r.useEffect(()=>{if(S){const e=e=>{const t=e.target.findAncestor(".element",!0),o=a.getElementById(null==t?void 0:t.id()),r=null==o?void 0:o.top,n=null==r?void 0:r.id;c(n===s.id)};S.on("mouseover",e);const t=()=>{c(!1)};return S.on("mouseleave",t),()=>{S.off("mouseover",e),S.off("mouseleave",t)}}},[S]),r.useEffect(()=>{if(!h){return}const e=k();if(!S||!e){return}E(!1);const t=()=>E(!0),o=()=>E(!1);return S.on("dragstart",t),S.on("dragend",o),e.on("transformstart",t),e.on("transformend",o),()=>{S.off("dragstart",t),S.off("dragend",o),e.off("transformstart",t),e.off("transformend",o)}},[S,h]);let I=w[o.element.type];if("text"===o.element.type&&(j.renderTagTextEnabled?I=p:j.htmlRenderEnabled&&(I=f)),!o.element.visible){return null}if(!I){return console.error("Can not find component for "+o.element.type),null}const R=b(s);return t(v,{name:"element-container",ref:C,visible:s.active,listening:s.active,children:["group"===s.type?e(I,Object.assign({},o)):e(v,{isolated:!!R,opacity:R?s.a.opacity:1,globalCompositeOperation:null!=R?R:"source-over",children:e(I,Object.assign({},o))}),((m||h)&&!u||s.isHighlighted)&&!d&&!y&&e(x,{element:"group"===s.type?{a:O(s,h?"rotation"in g[0]?g[0].rotation:0:m&&(null===(i=k())||void 0===i?void 0:i.rotation())||0)}:s})]})});export default S;
|
package/canvas/figure-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,Fragment as e,jsxs as r}from"react/jsx-runtime";import a from"react";import{toDisplayColor as o}from"@polotno/core/color";import{observer as s}from"mobx-react-lite";import{Group as i,Rect as h,Shape as n}from"react-konva";import{ROLES as l}from"../model/store.js";import d from"konva";import{getFigureFillClip as c,subTypeToPathDataFunc as
|
|
1
|
+
import{jsx as t,Fragment as e,jsxs as r}from"react/jsx-runtime";import a from"react";import{toDisplayColor as o}from"@polotno/core/color";import{observer as s}from"mobx-react-lite";import{Group as i,Rect as h,Shape as n}from"react-konva";import{ROLES as l}from"../model/store.js";import d from"konva";import{getFigureFillClip as c,subTypeToPathDataFunc as m}from"@polotno/core/figure-to-svg";import{isOpaquePaint as p}from"@polotno/core/gradient";import{isTouchDevice as g}from"../utils/screen.js";import{activeAnchorOf as f,isCornerAnchor as u,scaledStyleAttrs as w,roundedStyleAttrs as b,roundedGeometryAttrs as k,roundedPosition as x}from"../utils/px.js";import{useColor as y}from"./use-color.js";import{elementOpacity as j,paintOpacity as O}from"./paint-opacity.js";const v=(t,e)=>{t.beginPath();for(const{command:r,points:a}of e){switch(r){case"L":t.lineTo(a[0],a[1]);break;case"M":t.moveTo(a[0],a[1]);break;case"C":t.bezierCurveTo(a[0],a[1],a[2],a[3],a[4],a[5]);break;case"Q":t.quadraticCurveTo(a[0],a[1],a[2],a[3]);break;case"A":t.ellipse(a[0],a[1],a[2],a[3],a[6],a[4],a[4]+a[5],!a[7]);break;case"z":t.closePath()}}},E={};export const FigureElement=s(({element:P,store:T})=>{const M=a.useRef(null),S=a.useRef(!1),C=P.selectable||T.role===l.ADMIN,D=g(),R=P.isSelected,W=Math.min(P.strokeWidth,P.width/2,P.height/2),F=y(P,P.fill,"fill"),I=Object.assign(Object.assign({width:P.a.width,height:P.a.height},F),{shadowEnabled:P.shadowEnabled,shadowBlur:P.shadowBlur,shadowOffsetX:P.shadowOffsetX,shadowOffsetY:P.shadowOffsetY,shadowColor:o(P.shadowColor),shadowOpacity:P.shadowOpacity,preventDefault:!D||R,hideInExport:!P.showInExport}),X=y(P,P.stroke,"stroke"),Y=Object.assign(Object.assign({},X),{strokeWidth:2*W,dash:P.dash.map(t=>t*W)}),z=Math.max(0,10-Math.min(P.a.width,P.a.height)),A=c({subType:P.subType,width:P.a.width,height:P.a.height,cornerRadius:P.cornerRadius,strokeWidth:W,stroke:P.stroke,dash:P.dash,opacity:O(P)}),B=!P.shadowEnabled||p(P.fill)?A:null,q=(L=P.subType,E[L]||(E[L]=(N=m(L),s(({element:o,fillProps:s,strokeProps:i,hitStrokeWidth:l,fillClip:c})=>{const m=N({width:o.a.width,height:o.a.height,cornerRadius:o.cornerRadius}),p=a.useMemo(()=>d.Path.parsePathData(m),[m]),g=a.useMemo(()=>c?d.Path.parsePathData(c):null,[c]),f=!!g;return r(e,{children:[t(h,{width:o.a.width,height:o.a.height,fill:"transparent",hitStrokeWidth:l}),t(n,Object.assign({},s,i,{perfectDrawEnabled:f,shadowForStrokeEnabled:f,selfRectFunc:t=>({x:0,y:0,width:t.width(),height:t.height()}),sceneFunc:(t,e)=>{t.save(),g?(v(t,p),t.clip(),v(t,g),t.clip()):v(t,p),t.fillShape(e),t.restore(),t.save(),v(t,p),t.clip(),t.strokeShape(e),t.restore()}}))]})}))),E[L]||i);var L,N;return t(e,{children:t(i,{ref:M,id:P.id,x:P.a.x,y:P.a.y,rotation:P.a.rotation,opacity:j(P),listening:C,draggable:D?P.draggable&&R:P.draggable,name:"element",onDragMove:t=>{P.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{P.set(x(t.target))},onTransformStart:t=>{S.current=u(f(t.target))},onTransform:t=>{const e=t.target.scale();t.target.scaleX(1),t.target.scaleY(1);const r=S.current?Math.min(e.x,e.y):1;P.set(Object.assign({width:P.width*e.x,height:P.height*e.y,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation()},1!==r&&w(P,r)))},onTransformEnd:()=>{P.set(Object.assign(Object.assign({},k(P)),b(P)))},children:t(q,{element:P,fillProps:I,strokeProps:Y,hitStrokeWidth:z,fillClip:B})})})});
|
package/canvas/gif-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import{elementOpacity as i}from"./paint-opacity.js";import o from"react";import{toDisplayColor as a}from"@polotno/core/color";import{observer as n}from"mobx-react-lite";import{nodeAutorun as s}from"../utils/mobx.js";import{Image as h,Group as d,Rect as l,Arc as c,Text as m}from"react-konva";import{ROLES as g}from"../model/store.js";import f from"konva";import{parseGIF as u,decompressFrames as p}from"gifuct-js";import{useClip as w,useCornerRadiusAndCrop as x}from"./video-element.js";import{useImageLoader as y}from"./image-element.js";import{fetchWithRetry as b}from"@polotno/core/retry";import{useWorkspaceStyle as E}from"./workspace-style.js";import{useFilteredSource as v}from"./use-filtered-source.js";import{useFadeIn as j}from"./use-fadein.js";import{isTouchDevice as S}from"../utils/screen.js";import{
|
|
1
|
+
import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import{elementOpacity as i}from"./paint-opacity.js";import o from"react";import{toDisplayColor as a}from"@polotno/core/color";import{observer as n}from"mobx-react-lite";import{nodeAutorun as s}from"../utils/mobx.js";import{Image as h,Group as d,Rect as l,Arc as c,Text as m}from"react-konva";import{ROLES as g}from"../model/store.js";import f from"konva";import{parseGIF as u,decompressFrames as p}from"gifuct-js";import{useClip as w,useCornerRadiusAndCrop as x}from"./video-element.js";import{useImageLoader as y}from"./image-element.js";import{fetchWithRetry as b}from"@polotno/core/retry";import{useWorkspaceStyle as E}from"./workspace-style.js";import{useFilteredSource as v}from"./use-filtered-source.js";import{useFadeIn as j}from"./use-fadein.js";import{isTouchDevice as S}from"../utils/screen.js";import{activeAnchorOf as I,isCornerAnchor as R,scaledStyleAttrs as M,roundedStyleAttrs as C,roundedGeometryAttrs as O,roundedPosition as T}from"../utils/px.js";import{useEvent as z}from"../hooks/use-event.js";function D(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 X=n(({element:r})=>{const a=Math.min(30,r.width/4,r.height/4),n=o.useRef(null);o.useEffect(()=>{const t=n.current;if(!t){return}const e=new f.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}},[]);const{mediaLoadingStyle:s}=E();return e(d,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:i(r),hideInExport:!r.showInExport,children:[t(l,{width:r.width,height:r.height,fill:s.fill}),t(c,{ref:n,x:r.width/2,y:r.height/2,fill:s.textFill,outerRadius:Math.abs(a),innerRadius:Math.max(1,a-5),angle:270})]})}),Y=n(({element:r})=>{const{mediaErrorStyle:o}=E(),a=Math.max(10,Math.min(30,r.width/22));return e(d,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:i(r),hideInExport:!r.showInExport,children:[t(l,{width:r.width,height:r.height,fill:o.fill}),t(m,{text:"Cannot load the GIF...",fontSize:a,width:r.width,height:r.height,align:"center",fill:o.textFill,verticalAlign:"middle",padding:5})]})});export const GifElement=n(({element:n,store:d})=>{var c,m;const[E,k]=o.useState(!1),F=o.useRef(!1),_=n.isSelected,A=o.useRef(null),P=o.useRef(void 0),[B,G,L,U]=function(t){const[e,r]=o.useState([]),[i,a]=o.useState(0),[n,s]=o.useState("loading"),[h,d]=o.useState({width:0,height:0});return o.useEffect(()=>{(async()=>{try{const e=await b(t),i=await e.arrayBuffer(),o=u(i),n=p(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})),m=c.reduce((t,e)=>t+e.delay,0);r(c),a(m),s("loaded")}catch(e){console.error("Failed to load GIF:",e),s("failed")}})()},[t]),[e,i,h,n]}(n.src);y(U,n.src,n.id),o.useEffect(()=>(P.current=document.createElement("canvas"),()=>{P.current&&f.Util.releaseCanvas(P.current)}),[]),o.useEffect(()=>{if("loaded"===U&&P.current&&(P.current.width=L.width,P.current.height=L.height,B.length>0)){const t=P.current.getContext("2d");if(t){t.clearRect(0,0,L.width,L.height);const e=document.createElement("canvas");D(B[0],P.current,e)}}},[L,U,B]),o.useEffect(()=>{var t;if(!B.length||!P.current){return}const e=P.current;e.width=L.width,e.height=L.height;const r=document.createElement("canvas"),i=e.getContext("2d");i&&i.clearRect(0,0,e.width,e.height);let o=-1;D(B[0],e,r),rt(),o=0;const a=t=>{const i=(t=>{const e=t%G;let r=0;for(let i=0;i<B.length;i++){if(r+=B[i].delay,r>e){return i}}return 0})(t);i!==o&&(D(B[i],e,r),rt(),A.current.getLayer().draw(),o=i)};if(d.isPlaying||(null===(t=n.page)||void 0===t?void 0:t._exportingOrRendering)){return s(n,()=>{a(d.currentTime-n.page.startTime)})}{const t=window.setInterval(()=>{a(d.currentTime||performance.now())},16);return()=>{clearInterval(t)}}},[d.isPlaying,B,G,null===(c=n.page)||void 0===c?void 0:c._exportingOrRendering]),o.useEffect(()=>{G&&d.history.ignore(()=>{n.set({duration:G})})},[G]);let{cropX:W,cropY:H,cropWidth:N,cropHeight:q}=n;"loaded"!==U&&(W=H=0,N=q=1);const J={x:L.width*W,y:L.height*H,width:L.width*N,height:L.height*q},K=null!==(m=n.cornerRadius)&&void 0!==m?m:0,[Q,V]=v(n,P.current),[Z,$]=x(n,Q,J,d._elementsPixelRatio,K,E||n._cropModeEnabled),[tt,et]=w(n,Z,d._elementsPixelRatio,[J,K]),rt=z(()=>{V(),$(),et()}),it="loading"===U,ot="failed"===U,at=it||ot?0:i(n);j(A,at);const nt=n.selectable||d.role===g.ADMIN,st=S();return e(r,{children:[it&&t(X,{element:n}),ot&&t(Y,{element:n}),t(h,{ref:A,name:"element",id:n.id,image:tt,x:n.a.x,y:n.a.y,width:n.a.width||1,height:n.a.height||1,rotation:n.a.rotation,opacity:at,shadowEnabled:n.shadowEnabled,shadowBlur:n.shadowBlur,shadowOffsetX:n.shadowOffsetX,shadowOffsetY:n.shadowOffsetY,shadowColor:a(n.shadowColor),shadowOpacity:n.shadowOpacity,customCrop:J,listening:nt,draggable:st?n.draggable&&_:n.draggable,preventDefault:!st||_,hideInExport:!n.showInExport,onDragMove:t=>{n.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{n.set(T(t.target))},onTransformStart:t=>{k(!0),F.current=R(I(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 o=F.current?Math.min(r,i):1;n.set(Object.assign({x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*i,rotation:e.rotation()},1!==o&&M(n,o)))},onTransformEnd:()=>{n.set(Object.assign(Object.assign({},O(n)),C(n))),k(!1)}}),t(l,{x:n.a.x,y:n.a.y,width:Math.max(n.a.width-n.borderSize,0),height:Math.max(n.a.height-n.borderSize,0),opacity:at,offsetX:-n.borderSize/2,offsetY:-n.borderSize/2,stroke:a(n.borderColor),strokeWidth:n.borderSize,listening:!1,visible:!!n.borderSize,rotation:n.rotation,cornerRadius:Math.max(0,K-n.borderSize),hideInExport:!n.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{toDisplayColor as n}from"@polotno/core/color";import{elementOpacity as i}from"./paint-opacity.js";import{observer as a}from"mobx-react-lite";import{Group as s,Image as l,Path as c,Rect as f}from"react-konva";import{activeAnchorOf as d,scaledFontSizeAttrs as h,roundedFontSizeAttrs as u,roundedTextDecorationAttrs as g,roundedGeometryPx as m,roundedPosition as x,textMinResizeWidth as p}from"../utils/px.js";import b from"konva";import{isAlive as y}from"mobx-state-tree";import{nodeAutorun as w}from"../utils/mobx.js";import{flags as v}from"../utils/flags.js";import{applyFilter as E}from"./apply-filters.js";import{getLineHeight as S,useFontFamiliesLoader as z,usePrevious as k}from"./text-element.js";import{getCurvePath as O}from"@polotno/core/text-html";import{useColor as j,withDisplayTextPaints as F}from"./use-color.js";import{useWorkspaceStyle as M}from"./workspace-style.js";import{detectSize as R,detectLineRects as X,htmlToCanvas as Y}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as C}from"@polotno/core/background-shape";import{useFadeIn as I}from"./use-fadein.js";import{Html as P}from"react-konva-utils";import{incrementLoader as T,triggerLoadError as B}from"../utils/loader.js";import{removeTags as W}from"@polotno/core/text";import{cssFontFamily as A}from"@polotno/core/fonts";import{shadowCastByBackground as $}from"@polotno/core/filters";import*as D from"../utils/fonts.js";import{isTouchDevice as _}from"../utils/screen.js";import{getLimitedFontSize as H}from"./text-element/max-font-size.js";import{visiblePlaceholder as J}from"./text-element/placeholder.js";import{useDelayer as L}from"./use-delayer.js";import{getHtml as N,textLayoutWidth as q}from"@polotno/core/text-html";import{hasMeasuredHeight as V,resolveTextBoxHeight as G}from"@polotno/core/text-types";import{normalizeRichTextHtml as K,extractFontFamiliesFromHtml as Q,scaleRichTextFontSizesInHtml as U}from"../utils/rich-text-html.js";import{findFitFontSize as Z}from"./text-element/fit-font-size.js";import{RichEditor as tt,RichTextContainer as et,applyOpacityToColor as ot,quillRef as rt,createQuill as nt,setQuillContent as it,setQuillFormats as at,syncQuillFormatState as st,setCursorFromCoords as lt}from"./rich-editor.js";export{rt as quillRef,nt as createQuill,it as setQuillContent,at as setQuillFormats,st as syncQuillFormatState,lt as setCursorFromCoords};function ct(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 ft=/^((?!chrome|android).)*safari/i.test(navigator.userAgent),dt=navigator.userAgent.includes("Firefox");export const HTMLElement=a(({element:a,store:rt})=>{var nt,it,at;const{textOverflowIndicatorStyle:st}=M(),lt=r.useRef(null),[ht,ut]=r.useState(),[gt,mt]=r.useState(!1),[xt,pt]=r.useState(!1),bt=r.useRef(a.height),yt=a.isSelected,wt=k(yt),vt=a.fontSize/3,Et=q(a.a.width,a.letterSpacing,a.fontSize)+2*vt,{textVerticalResizeEnabled:St}=v,zt=v.autoDeleteEmptyText,kt=k(a.fontFamily),Ot=r.useMemo(()=>K(a.text,{fontSize:a.fontSize,fontFamily:a.fontFamily}),[a.text,a.fontSize,a.fontFamily]),jt=r.useMemo(()=>[...new Set([a.fontFamily,...Q(Ot)])].filter(Boolean),[a.fontFamily,Ot]),[Ft]=z(rt,jt,a.fontStyle,a.fontWeight,a.text||a.placeholder),Mt=a._editModeEnabled,Rt=Mt&&!(null===(nt=a.page)||void 0===nt?void 0:nt._exporting);I(lt);const Xt=Ft?a.fontFamily:kt!==a.fontFamily?kt:"Arial",Yt=j(a).fill,Ct=N(F(a),{fontFamily:Xt,color:Yt,textOverflow:v.textOverflow}),It=r.useRef([]),Pt=r.useRef(!0),[,Tt]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=It.current;t.length>0&&(It.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{Pt.current=!1;const t=It.current;t.length>0&&(It.current=[],t.forEach(t=>t()))},[]);let{width:Bt,height:Wt}=function(t,e,o){return r.useMemo(()=>R(t),[t,e.width,o])}(Ct,a,Ft);r.useEffect(()=>{if(!Ft){return}const t=(t,e,o,r)=>{const n=T(`text ${a.id}`),i=()=>{rt.history.ignore(t,e,o).then(()=>{Pt.current?(It.current.push(n),Tt()):n()})};r?queueMicrotask(i):i()};if(a.curveEnabled&&Math.abs(Bt-a.width)>1&&!gt&&t(()=>{if(y(a)&&Math.abs(Bt-a.width)>1){const t=a.x+a.width/2;a.set({width:Bt,x:t-Bt/2})}},!1,!0),!V(a)){return void t(()=>{y(a)&&(V(a)||a.set({height:Wt}))},void 0,void 0,!0)}const{textOverflow:e}=v;if("change-font-size"!==e||gt||Mt||a.curveEnabled){"resize"===e&&(St&&a.height<Wt&&!gt&&t(()=>{y(a)&&a.set({height:Wt})},!1,!0),St||a.height===Wt||gt||t(()=>{y(a)&&a.set({height:Wt})},!1,!0))}else{const e=Z(a,{fontFamily:a.fontFamily,textOverflow:v.textOverflow});e.fontSize!==a.fontSize?t(()=>{if(!y(a)){return}const t=h(a,e.fontSize);void 0!==e.text&&e.text!==a.text&&(t.text=e.text),a.set(t)},!1,!0):a.height!==Wt&&(St&&a.height<Wt?t(()=>{y(a)&&a.set({height:Wt})},!1,!0):St||t(()=>{y(a)&&a.set({height:Wt})},!1,!0))}});const At=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 Y(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),$t=r.useRef(0),Dt=r.useRef(null);r.useEffect(()=>{gt||Rt||(async()=>{$t.current++;const t=$t.current;let e=T(`text ${a.id} ${t}`);Dt.current&&Dt.current(),Dt.current=e,pt(!0);let o=null;const r=ft?5:1;for(let i=0;i<r;i++){const e=i>0?At:Y;try{if(o=await e({skipCache:i>0,html:Ct,width:Bt||a.width||1,height:G(a,Wt)||1,fontFamily:Xt,fontFamilies:Q(Ct),resolveFont:t=>rt.fonts.find(e=>e.fontFamily===t)||D.globalFonts.find(e=>e.fontFamily===t),padding:vt,pixelRatio:rt._elementsPixelRatio,font:rt.fonts.find(t=>t.fontFamily===Xt)||D.globalFonts.find(t=>t.fontFamily===Xt)}),t!==$t.current){return}if((ft||dt)&&o&&ct(o)){await new Promise(t=>setTimeout(t,50*(i+1)));continue}break}catch(n){console.error(n),B(`Error rendering rich text with id ${a.id}`);break}}o?ut(o):e?(e(),e=null):console.error("Finish function is called twice!"),pt(!1)})()},[Ct,gt,Wt,Rt,Xt,a.height,rt._elementsPixelRatio,Ft]);const[_t,Ht]=L(xt,300),[Jt]=L(gt,300,!0),Lt=Jt||_t;r.useEffect(()=>{var t;if(!Lt){return w(a,()=>{const t=lt.current;E(t,a)})}null===(t=lt.current)||void 0===t||t.clearCache()},[ht,Lt,a.shadowColor,a.shadowOffsetX,a.shadowOffsetY,a.shadowOpacity]),r.useEffect(()=>{ht&&!xt&&Dt.current&&(Dt.current(),Dt.current=null)},[ht,xt]),r.useLayoutEffect(()=>{if(!Ft){return}if(!a.curveEnabled){return}const t=new b.TextPath({data:O(a.a.width,a.a.height,a.curvePower,a.a.fontSize),text:W(a.text),letterSpacing:a.letterSpacing*a.a.fontSize,fontSize:a.a.fontSize,fontFamily:A(a.fontFamily),fontWeight:a.fontWeight,fontStyle:a.fontStyle,align:"center",textBaseline:"middle",fill:a.fill}),e=t.getSelfRect().width;if(e){const t=e-a.a.width,o=a.a.rotation*Math.PI/180,r=-t/2*Math.cos(o),n=-t/2*Math.sin(o);queueMicrotask(()=>{y(a)&&a.set({width:e,x:a.a.x+r,y:a.a.y+n})})}t.destroy()},[Ft,a.curveEnabled,a.curvePower,a.text,a.fontSize,a.fontFamily,a.fontWeight,a.fontStyle,a.letterSpacing]),r.useEffect(()=>()=>{Dt.current&&Dt.current()},[]),r.useEffect(()=>{wt&&!yt&&zt&&""===W(a.text)&&a.removable&&!J(a.placeholder)&&rt.deleteElements([a.id])},[zt,a.placeholder,a.removable,a.text,yt,wt]);const Nt=G(a,Wt);let qt=0;"middle"===a.verticalAlign&&(qt=(Nt-Wt)/2),"bottom"===a.verticalAlign&&(qt=Nt-Wt);const Vt=S({fontLoaded:Ft,fontFamily:a.fontFamily,fontSize:a.fontSize,lineHeight:a.lineHeight}),Gt=a.backgroundPadding*(a.fontSize*Vt*.5),Kt=a.backgroundCornerRadius*(a.fontSize*Vt*.5),Qt=r.useMemo(()=>{if(!a.backgroundEnabled&&!a.borderSize||a.curveEnabled||a.legacyBackground){return""}const t=X(Ct);if(0===t.length){return""}const e=t.map(t=>({left:t.left+vt,right:t.right+vt,top:t.top+vt,bottom:t.bottom+vt}));return C({rects:e,padding:Gt,cornerRadius:Kt})},[a.backgroundEnabled,a.borderSize,a.curveEnabled,a.legacyBackground,Ct,Ft,Gt,Kt,vt]),Ut=_(),Zt=O(a.a.width,Wt,a.curvePower,a.fontSize),te=r.useRef(null),ee=r.useRef(a.width),oe=r.useRef(""),re=r.useRef(0),ne=r.useRef(0),ie=r.useRef(null),ae=Mt&&a.strokeWidth>0&&!a.curveEnabled,se=Bt>0?Et/(Bt+2*vt):1,le=a.shadowEnabled&&!$(a),ce={shadowEnabled:a.shadowEnabled&&($(a)||a.borderSize>0),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:n(a.shadowColor),shadowOpacity:a.shadowOpacity};return o(e,{children:[a.curveEnabled||a.legacyBackground?t(f,Object.assign({x:a.a.x,y:a.a.y,offsetX:Gt/se,offsetY:Gt/se,rotation:a.a.rotation,scaleX:se,scaleY:se,hideInExport:!a.showInExport,listening:!1,visible:a.backgroundEnabled||a.borderSize>0,opacity:i(a),fillEnabled:a.backgroundEnabled,fill:ot(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:n(a.borderColor),strokeWidth:a.borderSize,width:a.width+2*Gt,height:Nt+2*Gt,cornerRadius:Kt},ce)):t(c,Object.assign({name:"html-background",x:a.a.x,y:a.a.y,offsetX:vt/se,offsetY:(vt-qt)/se,rotation:a.a.rotation,scaleX:se,scaleY:se,hideInExport:!a.showInExport,listening:!1,visible:(a.backgroundEnabled||a.borderSize>0)&&Qt.length>0,opacity:i(a),fillEnabled:a.backgroundEnabled,fill:ot(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:n(a.borderColor),strokeWidth:a.borderSize,lineJoin:Kt>0?"round":"miter",data:Qt},ce)),t(c,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,data:Zt,stroke:st.stroke,strokeWidth:1,visible:!1}),t(f,{ref:lt,name:"element",x:a.a.x,y:a.a.y,listening:a.selectable,rotation:a.a.rotation,width:a.a.width,height:a.a.height,visible:!Lt,draggable:Ut?a.draggable&&yt:a.draggable,preventDefault:!Ut||yt,opacity:Mt?0:i(a),hideInExport:!a.showInExport,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(x(t.target))},id:a.id,onDblClick:t=>{a.contentEditable&&(ie.current={x:t.evt.clientX,y:t.evt.clientY},a.toggleEditMode(!0))},onDblTap:t=>{var e;if(a.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];ie.current=o?{x:o.clientX,y:o.clientY}:null,a.toggleEditMode(!0)}},onTransformStart:t=>{mt(!0),bt.current=a.height,ee.current=a.width,te.current=t.target.position(),oe.current=d(t.target),re.current=a.a.rotation,ne.current=a.height},onTransform:t=>{const e=t.target,o=oe.current,r="middle-left"===o||"middle-right"===o,n="top-center"===o||"bottom-center"===o,i=e.scaleX();if(r){const t=e.scaleX(),o=e.width()*t,r=p(ee.current,a.fontSize);let n=o;o<r&&(n=r,te.current&&e.position(te.current)),e.width(n),e.scaleX(1),e.scaleY(1);const i=N(Object.assign(Object.assign({},a.toJSON()),{width:n}),{fontFamily:Xt,color:Yt,textOverflow:v.textOverflow}),s=R(i).height,l=v.textVerticalResizeEnabled?Math.max(s,bt.current):s;e.height(l),a.set({width:e.width(),height:l,x:e.x(),y:e.y()})}else if(n){const t="resize"===v.textOverflow,o="number"==typeof a.lineHeight?a.lineHeight:1,r=t?Wt:o*a.fontSize,n=e.height()*e.scaleY();if(n<r){if(e.height(r),te.current){if("top-center"===oe.current){const t=re.current*Math.PI/180,o=ne.current-r;e.x(te.current.x-o*Math.sin(t)),e.y(te.current.y+o*Math.cos(t))}else{e.position(te.current)}}}else{e.height(n)}e.scaleY(1),e.scaleX(1),ne.current=Math.max(r,n),a.set({x:e.x(),y:e.y(),height:e.height(),rotation:e.rotation()})}else{e.scaleX(1),e.scaleY(1),a.set({fontSize:a.fontSize*i,letterSpacing:a.letterSpacing,width:e.width()*i,x:e.x(),y:e.y(),rotation:e.rotation(),height:e.height()*i,shadowBlur:a.shadowBlur*i,shadowOffsetX:a.shadowOffsetX*i,shadowOffsetY:a.shadowOffsetY*i,strokeWidth:a.strokeWidth*i,borderSize:a.borderSize*i,text:U(a.text,i)})}te.current=t.target.position()},onTransformEnd:t=>{mt(!1),pt(!0);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},u(a,a.fontSize*e)),{width:m(t.target.width()*e),x:m(t.target.x()),y:m(t.target.y()),rotation:m(t.target.rotation()),text:U(a.text,e)}))}}),t(l,Object.assign({ref:lt,image:ht,x:a.a.x,y:a.a.y,offsetX:vt,offsetY:vt-qt,listening:!1,rotation:a.a.rotation,width:Et,height:Et*((null!==(it=null==ht?void 0:ht.height)&&void 0!==it?it:0)/((null==ht?void 0:ht.width)||1)||1),visible:!Lt&&!Mt,opacity:i(a)},ce,{shadowEnabled:le,hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled||gt||Lt})),(Lt&&!Mt||ae)&&t(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-qt,children:t(P,{divProps:{style:{pointerEvents:"none"}},children:t(et,{dangerouslySetInnerHTML:{__html:Ct},style:{pointerEvents:"none",opacity:a.a.opacity,textShadow:le?`${a.shadowOffsetX}px ${a.shadowOffsetY}px ${a.shadowBlur}px ${ot(a.shadowColor,null!==(at=a.shadowOpacity)&&void 0!==at?at:1)}`:void 0}})})}),Mt&&t(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-qt,children:t(P,{divProps:{style:{zIndex:20}},children:t(tt,{element:a,onChange:t=>{const e=H({oldText:W(a.text),newText:W(t),element:a});a.set(Object.assign({text:t},h(a,e)))},onBlur:t=>{y(a)&&(a.set(g(a)),a.toggleEditMode(!1),Ht(!0))},clickCoords:ie.current})})})]})});
|
|
1
|
+
import{jsx as t,Fragment as e,jsxs as o}from"react/jsx-runtime";import r from"react";import{toDisplayColor as i}from"@polotno/core/color";import{elementOpacity as n}from"./paint-opacity.js";import{observer as a}from"mobx-react-lite";import{Group as s,Image as l,Path as c,Rect as d}from"react-konva";import{activeAnchorOf as f,scaledFontSizeAttrs as h,roundedFontSizeAttrs as u,roundedTextDecorationAttrs as g,roundedGeometryPx as m,roundedPosition as p,textMinResizeWidth as x}from"../utils/px.js";import b from"konva";import{isAlive as y}from"mobx-state-tree";import{nodeAutorun as w}from"../utils/mobx.js";import{flags as E}from"../utils/flags.js";import{textOverflowOf as S}from"../utils/text-sizing.js";import{layoutTextSizing as v,legacyTextOverflow as z,renderTagLayoutAt as k,resolveTextSizing as O}from"@polotno/core/text-sizing";import{applyFilter as j}from"./apply-filters.js";import{getLineHeight as F,useFontFamiliesLoader as M,usePrevious as R}from"./text-element.js";import{getCurvePath as X}from"@polotno/core/text-html";import{useColor as Y,withDisplayTextPaints as C}from"./use-color.js";import{useWorkspaceStyle as I}from"./workspace-style.js";import{detectSize as A,detectLineRects as P,htmlToCanvas as T}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as B}from"@polotno/core/background-shape";import{useFadeIn as W}from"./use-fadein.js";import{Html as $}from"react-konva-utils";import{incrementLoader as D,triggerLoadError as H}from"../utils/loader.js";import{removeTags as _}from"@polotno/core/text";import{cssFontFamily as L}from"@polotno/core/fonts";import{shadowCastByBackground as J}from"@polotno/core/filters";import*as N from"../utils/fonts.js";import{isTouchDevice as q}from"../utils/screen.js";import{getLimitedFontSize as V}from"./text-element/max-font-size.js";import{visiblePlaceholder as G}from"./text-element/placeholder.js";import{useDelayer as K}from"./use-delayer.js";import{getHtml as Q,textLayoutWidth as U}from"@polotno/core/text-html";import{hasMeasuredHeight as Z,resolveTextBoxHeight as tt}from"@polotno/core/text-types";import{normalizeRichTextHtml as et,extractFontFamiliesFromHtml as ot,scaleRichTextFontSizesInHtml as rt}from"../utils/rich-text-html.js";import{RichEditor as it,RichTextContainer as nt,applyOpacityToColor as at,quillRef as st,createQuill as lt,setQuillContent as ct,setQuillFormats as dt,syncQuillFormatState as ft,setCursorFromCoords as ht}from"./rich-editor.js";export{st as quillRef,lt as createQuill,ct as setQuillContent,dt as setQuillFormats,ft as syncQuillFormatState,ht as setCursorFromCoords};function ut(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 gt=/^((?!chrome|android).)*safari/i.test(navigator.userAgent),mt=navigator.userAgent.includes("Firefox");export const HTMLElement=a(({element:a,store:st})=>{var lt,ct,dt;const{textOverflowIndicatorStyle:ft}=I(),ht=r.useRef(null),[pt,xt]=r.useState(),[bt,yt]=r.useState(!1),[wt,Et]=r.useState(!1),St=r.useRef(a.height),vt=a.isSelected,zt=R(vt),kt=a.fontSize/3,Ot=U(a.a.width,a.letterSpacing,a.fontSize)+2*kt,{textVerticalResizeEnabled:jt}=E,Ft=E.autoDeleteEmptyText,Mt=R(a.fontFamily),Rt=r.useMemo(()=>et(a.text,{fontSize:a.fontSize,fontFamily:a.fontFamily}),[a.text,a.fontSize,a.fontFamily]),Xt=r.useMemo(()=>[...new Set([a.fontFamily,...ot(Rt)])].filter(Boolean),[a.fontFamily,Rt]),[Yt]=M(st,Xt,a.fontStyle,a.fontWeight,a.text||a.placeholder),Ct=a._editModeEnabled,It=Ct&&!(null===(lt=a.page)||void 0===lt?void 0:lt._exporting);W(ht);const At=Yt?a.fontFamily:Mt!==a.fontFamily?Mt:"Arial",Pt=Y(a).fill,Tt=Q(C(a),{fontFamily:At,color:Pt,textOverflow:S(a)}),Bt=r.useRef([]),Wt=r.useRef(!0),[,$t]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=Bt.current;t.length>0&&(Bt.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{Wt.current=!1;const t=Bt.current;t.length>0&&(Bt.current=[],t.forEach(t=>t()))},[]);let{width:Dt,height:Ht}=function(t,e,o){return r.useMemo(()=>A(t),[t,e.width,o])}(Tt,a,Yt);r.useEffect(()=>{if(!Yt){return}const t=(t,e,o,r)=>{const i=D(`text ${a.id}`),n=()=>{st.history.ignore(t,e,o).then(()=>{Wt.current?(Bt.current.push(i),$t()):i()})};r?queueMicrotask(n):n()};if(a.curveEnabled&&Math.abs(Dt-a.width)>1&&!bt&&t(()=>{if(y(a)&&Math.abs(Dt-a.width)>1){const t=a.x+a.width/2;a.set({width:Dt,x:t-Dt/2})}},!1,!0),!Z(a)){return void t(()=>{y(a)&&(Z(a)||a.set({height:Ht}))},void 0,void 0,!0)}const e=z(S(a));if("change-font-size"!==e||bt||Ct||a.curveEnabled){"resize"===e&&(jt&&a.height<Ht&&!bt&&t(()=>{y(a)&&a.set({height:Ht})},!1,!0),jt||a.height===Ht||bt||t(()=>{y(a)&&a.set({height:Ht})},!1,!0))}else{const o=v({element:a,sizing:O({text:a.text,width:a.width,height:a.height,fontSize:a.fontSize,maxLines:a.maxLines,curveEnabled:a.curveEnabled},{textOverflow:e,textSplitAllowed:E.textSplitAllowed}),layoutAt:k(a,{getHtmlOptions:{fontFamily:a.fontFamily}})});o.fontSize!==a.fontSize?t(()=>{if(!y(a)){return}const t=h(a,o.fontSize);o.text!==a.text&&(t.text=o.text),a.set(t)},!1,!0):a.height!==Ht&&(jt&&a.height<Ht?t(()=>{y(a)&&a.set({height:Ht})},!1,!0):jt||t(()=>{y(a)&&a.set({height:Ht})},!1,!0))}});const _t=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 T(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),Lt=r.useRef(0),Jt=r.useRef(null);r.useEffect(()=>{bt||It||(async()=>{Lt.current++;const t=Lt.current;let e=D(`text ${a.id} ${t}`);Jt.current&&Jt.current(),Jt.current=e,Et(!0);let o=null;const r=gt?5:1;for(let n=0;n<r;n++){const e=n>0?_t:T;try{if(o=await e({skipCache:n>0,html:Tt,width:Dt||a.width||1,height:tt(a,Ht)||1,fontFamily:At,fontFamilies:ot(Tt),resolveFont:t=>st.fonts.find(e=>e.fontFamily===t)||N.globalFonts.find(e=>e.fontFamily===t),padding:kt,pixelRatio:st._elementsPixelRatio,font:st.fonts.find(t=>t.fontFamily===At)||N.globalFonts.find(t=>t.fontFamily===At)}),t!==Lt.current){return}if((gt||mt)&&o&&ut(o)){await new Promise(t=>setTimeout(t,50*(n+1)));continue}break}catch(i){console.error(i),H(`Error rendering rich text with id ${a.id}`);break}}o?xt(o):e?(e(),e=null):console.error("Finish function is called twice!"),Et(!1)})()},[Tt,bt,Ht,It,At,a.height,st._elementsPixelRatio,Yt]);const[Nt,qt]=K(wt,300),[Vt]=K(bt,300,!0),Gt=Vt||Nt;r.useEffect(()=>{var t;if(!Gt){return w(a,()=>{const t=ht.current;j(t,a)})}null===(t=ht.current)||void 0===t||t.clearCache()},[pt,Gt,a.shadowColor,a.shadowOffsetX,a.shadowOffsetY,a.shadowOpacity]),r.useEffect(()=>{pt&&!wt&&Jt.current&&(Jt.current(),Jt.current=null)},[pt,wt]),r.useLayoutEffect(()=>{if(!Yt){return}if(!a.curveEnabled){return}const t=new b.TextPath({data:X(a.a.width,a.a.height,a.curvePower,a.a.fontSize),text:_(a.text),letterSpacing:a.letterSpacing*a.a.fontSize,fontSize:a.a.fontSize,fontFamily:L(a.fontFamily),fontWeight:a.fontWeight,fontStyle:a.fontStyle,align:"center",textBaseline:"middle",fill:a.fill}),e=t.getSelfRect().width;if(e){const t=e-a.a.width,o=a.a.rotation*Math.PI/180,r=-t/2*Math.cos(o),i=-t/2*Math.sin(o);queueMicrotask(()=>{y(a)&&a.set({width:e,x:a.a.x+r,y:a.a.y+i})})}t.destroy()},[Yt,a.curveEnabled,a.curvePower,a.text,a.fontSize,a.fontFamily,a.fontWeight,a.fontStyle,a.letterSpacing]),r.useEffect(()=>()=>{Jt.current&&Jt.current()},[]),r.useEffect(()=>{zt&&!vt&&Ft&&""===_(a.text)&&a.removable&&!G(a.placeholder)&&st.deleteElements([a.id])},[Ft,a.placeholder,a.removable,a.text,vt,zt]);const Kt=tt(a,Ht);let Qt=0;"middle"===a.verticalAlign&&(Qt=(Kt-Ht)/2),"bottom"===a.verticalAlign&&(Qt=Kt-Ht);const Ut=F({fontLoaded:Yt,fontFamily:a.fontFamily,fontSize:a.fontSize,lineHeight:a.lineHeight}),Zt=a.backgroundPadding*(a.fontSize*Ut*.5),te=a.backgroundCornerRadius*(a.fontSize*Ut*.5),ee=r.useMemo(()=>{if(!a.backgroundEnabled&&!a.borderSize||a.curveEnabled||a.legacyBackground){return""}const t=P(Tt);if(0===t.length){return""}const e=t.map(t=>({left:t.left+kt,right:t.right+kt,top:t.top+kt,bottom:t.bottom+kt}));return B({rects:e,padding:Zt,cornerRadius:te})},[a.backgroundEnabled,a.borderSize,a.curveEnabled,a.legacyBackground,Tt,Yt,Zt,te,kt]),oe=q(),re=X(a.a.width,Ht,a.curvePower,a.fontSize),ie=r.useRef(null),ne=r.useRef(a.width),ae=r.useRef(""),se=r.useRef(0),le=r.useRef(0),ce=r.useRef(null),de=Ct&&a.strokeWidth>0&&!a.curveEnabled,fe=Dt>0?Ot/(Dt+2*kt):1,he=a.shadowEnabled&&!J(a),ue={shadowEnabled:a.shadowEnabled&&(J(a)||a.borderSize>0),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:i(a.shadowColor),shadowOpacity:a.shadowOpacity};return o(e,{children:[a.curveEnabled||a.legacyBackground?t(d,Object.assign({x:a.a.x,y:a.a.y,offsetX:Zt/fe,offsetY:Zt/fe,rotation:a.a.rotation,scaleX:fe,scaleY:fe,hideInExport:!a.showInExport,listening:!1,visible:a.backgroundEnabled||a.borderSize>0,opacity:n(a),fillEnabled:a.backgroundEnabled,fill:at(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,width:a.width+2*Zt,height:Kt+2*Zt,cornerRadius:te},ue)):t(c,Object.assign({name:"html-background",x:a.a.x,y:a.a.y,offsetX:kt/fe,offsetY:(kt-Qt)/fe,rotation:a.a.rotation,scaleX:fe,scaleY:fe,hideInExport:!a.showInExport,listening:!1,visible:(a.backgroundEnabled||a.borderSize>0)&&ee.length>0,opacity:n(a),fillEnabled:a.backgroundEnabled,fill:at(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,lineJoin:te>0?"round":"miter",data:ee},ue)),t(c,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,data:re,stroke:ft.stroke,strokeWidth:1,visible:!1}),t(d,{ref:ht,name:"element",x:a.a.x,y:a.a.y,listening:a.selectable,rotation:a.a.rotation,width:a.a.width,height:a.a.height,visible:!Gt,draggable:oe?a.draggable&&vt:a.draggable,preventDefault:!oe||vt,opacity:Ct?0:n(a),hideInExport:!a.showInExport,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(p(t.target))},id:a.id,onDblClick:t=>{a.contentEditable&&(ce.current={x:t.evt.clientX,y:t.evt.clientY},a.toggleEditMode(!0))},onDblTap:t=>{var e;if(a.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];ce.current=o?{x:o.clientX,y:o.clientY}:null,a.toggleEditMode(!0)}},onTransformStart:t=>{yt(!0),St.current=a.height,ne.current=a.width,ie.current=t.target.position(),ae.current=f(t.target),se.current=a.a.rotation,le.current=a.height},onTransform:t=>{const e=t.target,o=ae.current,r="middle-left"===o||"middle-right"===o,i="top-center"===o||"bottom-center"===o,n=e.scaleX();if(r){const t=e.scaleX(),o=e.width()*t,r=x(ne.current,a.fontSize);let i=o;o<r&&(i=r,ie.current&&e.position(ie.current)),e.width(i),e.scaleX(1),e.scaleY(1);const n=Q(Object.assign(Object.assign({},a.toJSON()),{width:i}),{fontFamily:At,color:Pt,textOverflow:S(a)}),s=A(n).height,l=E.textVerticalResizeEnabled?Math.max(s,St.current):s;e.height(l),a.set({width:e.width(),height:l,x:e.x(),y:e.y()})}else if(i){const t="resize"===S(a),o="number"==typeof a.lineHeight?a.lineHeight:1,r=t?Ht:o*a.fontSize,i=e.height()*e.scaleY();if(i<r){if(e.height(r),ie.current){if("top-center"===ae.current){const t=se.current*Math.PI/180,o=le.current-r;e.x(ie.current.x-o*Math.sin(t)),e.y(ie.current.y+o*Math.cos(t))}else{e.position(ie.current)}}}else{e.height(i)}e.scaleY(1),e.scaleX(1),le.current=Math.max(r,i),a.set({x:e.x(),y:e.y(),height:e.height(),rotation:e.rotation()})}else{e.scaleX(1),e.scaleY(1),a.set({fontSize:a.fontSize*n,letterSpacing:a.letterSpacing,width:e.width()*n,x:e.x(),y:e.y(),rotation:e.rotation(),height:e.height()*n,shadowBlur:a.shadowBlur*n,shadowOffsetX:a.shadowOffsetX*n,shadowOffsetY:a.shadowOffsetY*n,strokeWidth:a.strokeWidth*n,borderSize:a.borderSize*n,text:rt(a.text,n)})}ie.current=t.target.position()},onTransformEnd:t=>{yt(!1),Et(!0);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},u(a,a.fontSize*e)),{width:m(t.target.width()*e),x:m(t.target.x()),y:m(t.target.y()),rotation:m(t.target.rotation()),text:rt(a.text,e)}))}}),t(l,Object.assign({ref:ht,image:pt,x:a.a.x,y:a.a.y,offsetX:kt,offsetY:kt-Qt,listening:!1,rotation:a.a.rotation,width:Ot,height:Ot*((null!==(ct=null==pt?void 0:pt.height)&&void 0!==ct?ct:0)/((null==pt?void 0:pt.width)||1)||1),visible:!Gt&&!Ct,opacity:n(a)},ue,{shadowEnabled:he,hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled||bt||Gt})),(Gt&&!Ct||de)&&t(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-Qt,children:t($,{divProps:{style:{pointerEvents:"none"}},children:t(nt,{dangerouslySetInnerHTML:{__html:Tt},style:{pointerEvents:"none",opacity:a.a.opacity,textShadow:he?`${a.shadowOffsetX}px ${a.shadowOffsetY}px ${a.shadowBlur}px ${at(a.shadowColor,null!==(dt=a.shadowOpacity)&&void 0!==dt?dt:1)}`:void 0}})})}),Ct&&t(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-Qt,children:t($,{divProps:{style:{zIndex:20}},children:t(it,{element:a,onChange:t=>{const e=V({oldText:_(a.text),newText:_(t),element:a});a.set(Object.assign({text:t},h(a,e)))},onBlur:t=>{y(a)&&(a.set(g(a)),a.toggleEditMode(!1),qt(!0))},clickCoords:ce.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 i from"react";import{toDisplayColor as o}from"@polotno/core/color";import{observer as a}from"mobx-react-lite";import{isAlive as h}from"mobx-state-tree";import{Arc as n,Group as s,Image as d,Rect as c,Text as l}from"react-konva";import{ROLES as g}from"../model/store.js";import u from"./use-image.js";import m from"konva";import{useWorkspaceStyle as p}from"./workspace-style.js";import{incrementLoader as f,triggerLoadError as w}from"../utils/loader.js";import{polotnoError as x}from"@polotno/core/errors";import{getBorderInset as v,getCropRect as M}from"@polotno/core/image";import{elementOpacity as y,paintOpacity as b}from"./paint-opacity.js";import*as S from"@polotno/core/svg";import{trySetCanvasSize as E}from"../utils/canvas.js";import{roundEnabledPx as C,activeAnchorOf as I,roundedCropFraction as R,roundedGeometryPx as X,roundedPosition as Y,isCornerAnchor as j,scaledBoxStyleAttrs as H,roundedBoxStyleAttrs as O}from"../utils/px.js";import{useFilteredSource as W}from"./use-filtered-source.js";import{useFadeIn as z}from"./use-fadein.js";import{isTouchDevice as A}from"../utils/screen.js";import{useDelayer as D}from"./use-delayer.js";import{useColor as k}from"./use-color.js";import{commitCropSource as T,cropDraftWriter as L,CropOverlay as _}from"./crop-overlay.js";import{ImageToolControls as N}from"./image-tool-controls.js";import{drawReduced as U,exportKernelEnabled as F,shouldResample as P}from"./downscale.js";const q=1e4,B=t=>"svg"===t.type||S.isSvgUrl(t.src||""),V=t=>{const[e,r]=i.useState({src:"",value:"",status:"loaded"}),o=!!t&&S.isSvgUrl(t);return i.useEffect(()=>{if(!o){return}let e=!0;return(async()=>{try{const i=S.prepareSvgSource(await S.urlToString(t));e&&r({src:t,value:i,status:"loaded"})}catch(i){e&&r({src:t,value:"",status:"failed"})}})(),()=>{e=!1}},[t,o]),o?[e.value,e.src===t?e.status:"loading"]:[t,"loaded"]};export const useSizeFixer=t=>V(t)[0]||t;function $(t,e){return{x:(t.x+e.x)/2,y:(t.y+e.y)/2}}export function svgSourceRatio(t,e,r){if(!(e.width>0&&e.height>0)){return 1}const i=Math.max(t.width/e.width*r,t.height/e.height*r),o=Math.min(t.cropWidth,t.cropHeight),a=o>0&&o<=1?Math.pow(2,Math.ceil(Math.log2(1/o))):1;return Math.min(i*a,q/Math.max(e.width,e.height),Math.sqrt(16777216/(e.width*e.height)))}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 G=(t,e,r)=>Math.max(e,Math.min(r,t)),J=(t,e,r)=>Math.max(t.a.width/e.width,t.a.height/e.height)*r;export const useCornerRadiusAndCrop=(t,e,r,o,a=0,h=!1,n=!0,s=0,d)=>{var c;const l=q,g=q,u=d.aligned?G(d.deviceWidth,1,l):Math.floor(G(t.a.width*o,1,l)),p=d.aligned?G(d.deviceHeight,1,g):Math.floor(G(t.a.height*o,1,g)),f=Math.min(a*o,u/2,p/2),w=Math.min(s*o,u/2,p/2),x=J(t,r,o),v=F(t.page)&&!h&&(P(x,u,p)||d.aligned),M=0===r.x&&0===r.y&&r.width===(null==e?void 0:e.width)&&r.height===(null==e?void 0:e.height),y=i.useMemo(()=>{if(e&&e.width&&e.height){return!M||0!==f||v||w?document.createElement("canvas"):void 0}},[e,f,v,M,w]);return i.useLayoutEffect(()=>{var i;if(!y||!e){return}E(y,u,p);const o=y.getContext("2d");if(o){if((null===(i=t.page)||void 0===i?void 0:i._exportingOrRendering)&&(o.imageSmoothingQuality="high"),(f||w)&&(o.beginPath(),o.roundRect(w,w,u-2*w,p-2*w,Math.max(0,f-w)),o.clip()),v){const t=y.width===u&&y.height===p&&d.aligned;return void U(o,e,r,y.width,y.height,{x:t?d.phaseX:0,y:t?d.phaseY:0,width:t?d.spanX:y.width,height:t?d.spanY:y.height})}o.drawImage(e,r.x,r.y,r.width,r.height,0,0,y.width,y.height)}},[y,t.a.width,t.a.height,r.x,r.y,r.width,r.height,a,o,h,null===(c=t.page)||void 0===c?void 0:c._exportingOrRendering,v,d.phaseX,d.phaseY,w]),i.useEffect(()=>()=>{y&&"CANVAS"===y.nodeName&&m.Util.releaseCanvas(y)},[y]),y||e};const Q=document.createElement("canvas"),K=a(({element:r})=>{const o=Math.min(30,r.a.width/4,r.a.height/4),a=i.useRef(null);i.useEffect(()=>{const t=a.current;if(!t){return}const e=new m.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}});const{mediaLoadingStyle:h}=p();return e(s,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:y(r),hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:h.fill}),t(n,{ref:a,x:r.a.width/2,y:r.a.height/2,fill:h.textFill,outerRadius:Math.abs(o),innerRadius:Math.max(1,o-5),angle:270})]})}),Z=a(({element:r})=>{const{mediaErrorStyle:i}=p(),o=Math.max(10,Math.min(30,r.a.width/25));return e(s,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:y(r),hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:i.fill}),t(l,{text:"Can not load the image...",fontSize:o,width:r.a.width,height:r.a.height,align:"center",fill:i.textFill,verticalAlign:"middle",padding:5})]})});let tt=u;export const setImageLoaderHook=t=>{tt=t};export const useCanvasImage=(t,e)=>tt(t,e);export const useClipImage=t=>{const[e,r]=V(t),[o,a]=useCanvasImage(e,"anonymous"),h="loaded"===r?a:r,n=i.useRef(void 0);return t&&"failed"!==h?o&&(n.current=o):n.current=void 0,[t&&"failed"!==h?o||n.current:void 0,h]};export const useImageLoader=(t,e="",r="")=>{const o=i.useRef(void 0),a=()=>{var t;null===(t=o.current)||void 0===t||t.call(o),o.current=void 0};i.useEffect(()=>a,[]),i.useLayoutEffect(()=>{const i=e.slice(0,200),h=`image with id ${r} url: ${i}`;"loading"!==t||o.current||(o.current=f(h)),"loading"!==t&&a(),"failed"===t&&w(x("IMAGE_FAILED",h,{src:i,elementId:r,reason:"load"}))},[t])};export const ImageElement=a(({element:a,store:n})=>{var s,l,u,p,f;const[w,x]=i.useState(!1),U=i.useRef(null),G=a.isSelected,[et,rt]=(t=>{var e;const[r,o]=i.useReducer(t=>t+1,0),a=B(t),h=a?t.src+"\n"+JSON.stringify(null!==(e=t.colorsReplace)&&void 0!==e?e:null):"",n=i.useRef("loading"),s=i.useRef(""),d=i.useRef(h);return d.current!==h&&(d.current=h,n.current="loading"),i.useEffect(()=>{if(!a||!t.src){return}let e=!1;return(async()=>{const r=S.prepareSvgSource(await S.urlToString(t.src),t.colorsReplace);e||(s.current=r,n.current="loaded",o())})().catch(()=>{e||(n.current="failed",o())}),()=>{e=!0}},[h]),a?[s.current,n.current]:[t.src,"loaded"]})(a),[it,ot]=tt(et,"anonymous"),at="loaded"===rt?ot:rt;useImageLoader(at,a.src,a.id);const ht=(null===(s=a.page)||void 0===s?void 0:s._exportingOrRendering)?1:Math.max(1,n.scale),nt=n._elementsPixelRatio*ht,st=(({image:t,status:e})=>{const r=i.useRef(void 0);return i.useEffect(()=>{r.current=t||r.current},[t]),"failed"!==e?r.current:void 0})({image:it,status:at}),dt=((t,e,r)=>{const o=B(t)&&e?svgSourceRatio({cropWidth:t.a.cropWidth,cropHeight:t.a.cropHeight,width:t.a.width,height:t.a.height},e,r):1,a=i.useMemo(()=>{var r,i;const{flipX:a,flipY:h}=t,n=B(t),s=navigator.userAgent.toLowerCase().indexOf("firefox")>-1,d=(/^((?!chrome|android).)*safari/i.test(navigator.userAgent)||s)&&n||t.maskSrc;if(!a&&!h&&!d){return e}if(!e||!e.width||!e.height){return null}const c=document.createElement("canvas");E(c,Math.max(e.width*o,1),Math.max(e.height*o,1));let l=a?-c.width:0,g=h?-c.height:0;return null===(r=c.getContext("2d"))||void 0===r||r.scale(a?-1:1,h?-1:1),null===(i=c.getContext("2d"))||void 0===i||i.drawImage(e,l,g,c.width,c.height),c},[t.maskSrc,t.flipX,t.flipY,e,o]);return i.useEffect(()=>()=>{a&&"CANVAS"===a.nodeName&&m.Util.releaseCanvas(a)},[a]),a})(a,it||st,nt),ct=((t,e)=>{const[r,o]=V(t.maskSrc||""),[a,h]=tt(r,"anonymous"),n=t.maskSrc?"loaded"===o?h:o:"loaded";return useImageLoader(n,t.maskSrc||"",t.id),i.useMemo(()=>{if(!a){return e}if(!e||!e.width||!e.height){return e}const{width:r,height:i}=maskCompositeSize(e,{width:t.a.width,height:t.a.height,stretchEnabled:t.stretchEnabled}),o=document.createElement("canvas");o.width=r,o.height=i;const h=o.getContext("2d");if(!h){return e}h.drawImage(e,0,0,r,i,0,0,r,i),h.globalCompositeOperation="source-in";const n=function(t,e){const r=e.width/e.height;let i,o;return r>=t.width/t.height?(i=t.width,o=t.width/r):(i=t.height*r,o=t.height),{x:(t.width-i)/2,y:(t.height-o)/2,width:i,height:o}}(a,t);return h.drawImage(a,n.x,n.y,n.width,n.height,0,0,r,i),o},[e,a,t.a.width,t.a.height,t.stretchEnabled])})(a,dt)||Q,[lt=Q]=W(a,ct);i.useLayoutEffect(()=>{"loaded"===at&&a._setSourceSize({width:lt.width,height:lt.height},a.src)},[a,a.src,at,lt.width,lt.height]);let{cropX:gt,cropY:ut,cropWidth:mt,cropHeight:pt}=a.a;"loaded"!==ot&&(gt=ut=0,mt=pt=1);const ft=M({stretchEnabled:a.stretchEnabled,cropX:gt,cropY:ut,cropWidth:mt,cropHeight:pt,width:a.a.width,height:a.a.height},lt),wt=ft.width,xt=ft.height,vt=a.stretchEnabled,Mt=null!==(l=a.cornerRadius)&&void 0!==l?l:0,yt=(null===(u=a.page)||void 0===u?void 0:u._exportingOrRendering)?1:Math.min(2,n.scale),bt=n._elementsPixelRatio*yt,St=w||a._cropModeEnabled||"svg"===a.type,Et=function(t,e,r,i){const{x:o,y:a,width:h,height:n}=t.a,s={x:o,y:a,width:h,height:n,deviceWidth:0,deviceHeight:0,phaseX:0,phaseY:0,spanX:0,spanY:0,aligned:!1};if(!i){return s}const d=o*e,c=a*e,l=Math.round(d),g=Math.round(c),u=Math.max(1,Math.round(d+h*e)-l),m=Math.max(1,Math.round(c+n*e)-g),p=t=>Math.abs(t-Math.round(t))>.001;return p(d)||p(c)||p(h*e)||p(n*e)?u>q||m>q?s:P(r,u,m)||r<=1&&u*m<=4e6?{x:l/e,y:g/e,width:u/e,height:m/e,deviceWidth:u,deviceHeight:m,phaseX:d-l,phaseY:c-g,spanX:h*e,spanY:n*e,aligned:!0}:s:s}(a,bt,J(a,ft,bt),F(a.page)&&0===a.a.rotation&&"group"!==(null===(p=a.parent)||void 0===p?void 0:p.type)&&!St),Ct=Et.aligned?Et:{x:a.x,y:a.y,width:a.a.width,height:a.a.height};let It=((t,e,r,o)=>{const[a,h]=useClipImage(t.clipSrc||""),n=t.clipSrc?h:"loaded";useImageLoader(n,t.clipSrc,t.id);const s=i.useMemo(()=>{if(e&&a){return document.createElement("canvas")}},[e,a]);return i.useLayoutEffect(()=>{var i;if(!a){return}if(!e||!e.width||!e.height){return}if(!a||!a.width||!a.height){return}if(!s){return}const o=document.createElement("canvas"),h=Math.max(t.a.width/a.width*r,t.a.height/a.height*r);E(o,Math.min(1e4,Math.max(a.width*h,1)),Math.min(1e4,Math.max(a.height*h,1))),null===(i=o.getContext("2d"))||void 0===i||i.drawImage(a,0,0,o.width,o.height),E(s,Math.min(1e4,Math.max(e.width,Math.ceil(t.a.width*r),1)),Math.min(1e4,Math.max(e.height,Math.ceil(t.a.height*r),1)));const n=s.getContext("2d");n&&(n.save(),n.drawImage(o,0,0,s.width,s.height),m.Util.releaseCanvas(o),n.globalCompositeOperation="source-in",n.drawImage(e,0,0,s.width,s.height),n.restore())},[s,e,a,t.a.width,t.a.height,r,...o]),i.useEffect(()=>()=>{s&&m.Util.releaseCanvas(s)},[s]),t.clipSrc&&a?s:e})(a,useCornerRadiusAndCrop(a,lt,ft,bt,Mt,St,!0,v({borderSize:a.borderSize,borderColor:o(a.borderColor),width:a.a.width,height:a.a.height,opacity:b(a)}),Et),bt,[ft,Mt,bt]);var Rt=i.useRef(null),Xt=i.useRef(0),Yt=i.useRef(!1);const jt=()=>{var t;a._cropEnabled&&!a.pendingEdit&&(null===(t=n.activeTool)||void 0===t?void 0:t.elementId)!==a.id&&setTimeout(()=>{h(a)&&a.toggleCropMode(!0)})},Ht=B(a)&&st,Ot="loading"===at&&!Ht,[Wt]=D(Ot,100,!1,!1),zt="failed"===at,At=!Wt&&!zt,Dt=i.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),kt=i.useRef(!1),Tt=At?y(a):0;z(U,Tt);const Lt=a.selectable||n.role===g.ADMIN,_t=A(),Nt=k(a,a.borderColor,"stroke");return e(r,{children:[Wt&&t(K,{element:a}),zt&&t(Z,{element:a}),t(d,{ref:U,name:"element",id:a.id,image:It,x:Et.x,y:Et.y,width:Et.width||1,height:Et.height||1,rotation:a.a.rotation,opacity:Tt,shadowEnabled:a.shadowEnabled,shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:o(a.shadowColor),shadowOpacity:a.shadowOpacity,customCrop:ft,listening:Lt,draggable:!a.pendingEdit&&(null===(f=n.activeTool)||void 0===f?void 0:f.elementId)!==a.id&&(_t?a.draggable&&G:a.draggable),preventDefault:!_t||G,hideInExport:!a.showInExport,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(Y(t.target))},onDblClick:jt,onDblTap:jt,onTransformStart:t=>{x(!0),kt.current=j(I(t.target)),Dt.current={cropX:a.cropX,cropY:a.cropY,cropWidth:a.cropWidth,cropHeight:a.cropHeight}},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),i=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const o=1-wt/lt.width,h=Math.min(o,Math.max(0,a.cropX)),n=1-xt/lt.height,s=Math.min(n,Math.max(0,a.cropY)),d=kt.current,c=!d&&r<1&&Dt.current.cropHeight>xt/lt.height;let l=d?a.cropWidth:a.cropWidth*r;c&&(l=a.cropWidth);const g=!d&&i<1&&Dt.current.cropWidth>wt/lt.width;let u=d?a.cropHeight:a.cropHeight*i;g&&(u=a.cropHeight),vt&&(l=a.cropWidth,u=a.cropHeight);const m=d?Math.min(r,i):1;a.set(Object.assign({cropX:h,cropY:s,x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*i,rotation:t.target.rotation(),cropWidth:Math.min(l,1-h),cropHeight:Math.min(u,1-s)},1!==m&&Object.assign(Object.assign({},H(a,m)),{borderSize:a.borderSize*m})))},onTransformEnd:t=>{const e=t.currentTarget;a.set(Object.assign(Object.assign({width:X(e.width()),height:X(e.height()),x:X(e.x()),y:X(e.y()),rotation:X(t.target.rotation()),cropX:R(a.cropX),cropY:R(a.cropY),cropWidth:R(wt/lt.width),cropHeight:R(xt/lt.height)},O(a)),{borderSize:C(a.borderSize)})),x(!1)}}),t(c,Object.assign({x:Ct.x,y:Ct.y,width:Math.max(Ct.width-a.borderSize,0),height:Math.max(Ct.height-a.borderSize,0),opacity:Tt,offsetX:-a.borderSize/2,offsetY:-a.borderSize/2},Nt,{strokeWidth:a.borderSize,listening:!1,visible:!!a.borderSize,rotation:a.rotation,cornerRadius:Math.max(0,Mt-a.borderSize),hideInExport:!a.showInExport})),a._cropModeEnabled&&"loaded"===at&&t(_,{element:a,store:n,previewImage:It,sourceImage:lt,sourceWidth:lt.width,sourceHeight:lt.height,cropAbsoluteWidth:wt,cropAbsoluteHeight:xt,onSourceTouchMove:t=>{t.evt.preventDefault();const e=t.target.getStage().getPointersPositions();var r=e[0],i=e[1];const o=t.target;if(r&&!i&&!o.isDragging()&&Yt.current&&(o.startDrag(),Yt.current=!1),r&&i){m.hitOnDragEnabled=!0,o.isDragging()&&(Yt.current=!0,o.stopDrag());const e=t.target.getAbsoluteTransform().copy();e.invert();var h={x:r.x,y:r.y},s={x:i.x,y:i.y};if(!Rt.current){return void(Rt.current=$(h,s))}var d=$(h,s),c=function(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}(h,s);Xt.current||(Xt.current=c);const w=o.position();var l={x:d.x-w.x,y:d.y-w.y},g=c/Xt.current;o.scaleX(g),o.scaleY(g);const x=e.point(d),v=e.point(Rt.current);var u=x.x-v.x,p=x.y-v.y,f={x:Math.min(0,d.x-l.x*g+u),y:Math.min(0,d.y-l.y*g+p)};o.position(f),Xt.current=c,Rt.current=d,(t=>{T(t,a,L(a,n),lt.width,lt.height,wt,xt)})(t)}},onSourceTouchEnd:t=>{Xt.current=0,Rt.current=null,m.hitOnDragEnabled=!1}}),"image"===a.type&&t(N,{element:a,store:n})]})});
|
|
1
|
+
import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import i from"react";import{toDisplayColor as o}from"@polotno/core/color";import{observer as a}from"mobx-react-lite";import{isAlive as h}from"mobx-state-tree";import{Arc as n,Group as s,Image as d,Rect as c,Text as l}from"react-konva";import{ROLES as g}from"../model/store.js";import u from"./use-image.js";import m from"konva";import{useWorkspaceStyle as p}from"./workspace-style.js";import{incrementLoader as f,triggerLoadError as w}from"../utils/loader.js";import{polotnoError as x}from"@polotno/core/errors";import{getBorderInset as v,getCropRect as M}from"@polotno/core/image";import{elementOpacity as y,paintOpacity as E}from"./paint-opacity.js";import*as b from"@polotno/core/svg";import{trySetCanvasSize as S}from"../utils/canvas.js";import{activeAnchorOf as C,roundedCropFraction as I,roundedGeometryPx as R,roundedPosition as X,isCornerAnchor as Y,scaledStyleAttrs as H,roundedStyleAttrs as W}from"../utils/px.js";import{useFilteredSource as j}from"./use-filtered-source.js";import{useFadeIn as O}from"./use-fadein.js";import{isTouchDevice as A}from"../utils/screen.js";import{useDelayer as D}from"./use-delayer.js";import{useColor as k}from"./use-color.js";import{commitCropSource as z,cropDraftWriter as T,CropOverlay as L}from"./crop-overlay.js";import{ImageToolControls as _}from"./image-tool-controls.js";import{drawReduced as N,exportKernelEnabled as U,shouldResample as F}from"./downscale.js";const P=1e4,q=t=>"svg"===t.type||b.isSvgUrl(t.src||""),B=t=>{const[e,r]=i.useState({src:"",value:"",status:"loaded"}),o=!!t&&b.isSvgUrl(t);return i.useEffect(()=>{if(!o){return}let e=!0;return(async()=>{try{const i=b.prepareSvgSource(await b.urlToString(t));e&&r({src:t,value:i,status:"loaded"})}catch(i){e&&r({src:t,value:"",status:"failed"})}})(),()=>{e=!1}},[t,o]),o?[e.value,e.src===t?e.status:"loading"]:[t,"loaded"]};export const useSizeFixer=t=>B(t)[0]||t;function V(t,e){return{x:(t.x+e.x)/2,y:(t.y+e.y)/2}}export function svgSourceRatio(t,e,r){if(!(e.width>0&&e.height>0)){return 1}const i=Math.max(t.width/e.width*r,t.height/e.height*r),o=Math.min(t.cropWidth,t.cropHeight),a=o>0&&o<=1?Math.pow(2,Math.ceil(Math.log2(1/o))):1;return Math.min(i*a,P/Math.max(e.width,e.height),Math.sqrt(16777216/(e.width*e.height)))}export function maskCompositeSize(t,e){const r=M({stretchEnabled:e.stretchEnabled,cropX:0,cropY:0,cropWidth:1,cropHeight:1,width:Math.max(e.width,1),height:Math.max(e.height,1)},t);return{width:Math.max(Math.round(r.width),1),height:Math.max(Math.round(r.height),1)}}const $=(t,e,r)=>Math.max(e,Math.min(r,t)),G=(t,e,r)=>Math.max(t.a.width/e.width,t.a.height/e.height)*r;export const useCornerRadiusAndCrop=(t,e,r,o,a=0,h=!1,n=!0,s=0,d)=>{var c;const l=P,g=P,u=d.aligned?$(d.deviceWidth,1,l):Math.floor($(t.a.width*o,1,l)),p=d.aligned?$(d.deviceHeight,1,g):Math.floor($(t.a.height*o,1,g)),f=Math.min(a*o,u/2,p/2),w=Math.min(s*o,u/2,p/2),x=G(t,r,o),v=U(t.page)&&!h&&(F(x,u,p)||d.aligned),M=0===r.x&&0===r.y&&r.width===(null==e?void 0:e.width)&&r.height===(null==e?void 0:e.height),y=i.useMemo(()=>{if(e&&e.width&&e.height){return!M||0!==f||v||w?document.createElement("canvas"):void 0}},[e,f,v,M,w]);return i.useLayoutEffect(()=>{var i;if(!y||!e){return}S(y,u,p);const o=y.getContext("2d");if(o){if((null===(i=t.page)||void 0===i?void 0:i._exportingOrRendering)&&(o.imageSmoothingQuality="high"),(f||w)&&(o.beginPath(),o.roundRect(w,w,u-2*w,p-2*w,Math.max(0,f-w)),o.clip()),v){const t=y.width===u&&y.height===p&&d.aligned;return void N(o,e,r,y.width,y.height,{x:t?d.phaseX:0,y:t?d.phaseY:0,width:t?d.spanX:y.width,height:t?d.spanY:y.height})}o.drawImage(e,r.x,r.y,r.width,r.height,0,0,y.width,y.height)}},[y,t.a.width,t.a.height,r.x,r.y,r.width,r.height,a,o,h,null===(c=t.page)||void 0===c?void 0:c._exportingOrRendering,v,d.phaseX,d.phaseY,w]),i.useEffect(()=>()=>{y&&"CANVAS"===y.nodeName&&m.Util.releaseCanvas(y)},[y]),y||e};const J=document.createElement("canvas"),Q=a(({element:r})=>{const o=Math.min(30,r.a.width/4,r.a.height/4),a=i.useRef(null);i.useEffect(()=>{const t=a.current;if(!t){return}const e=new m.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}});const{mediaLoadingStyle:h}=p();return e(s,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:y(r),hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:h.fill}),t(n,{ref:a,x:r.a.width/2,y:r.a.height/2,fill:h.textFill,outerRadius:Math.abs(o),innerRadius:Math.max(1,o-5),angle:270})]})}),K=a(({element:r})=>{const{mediaErrorStyle:i}=p(),o=Math.max(10,Math.min(30,r.a.width/25));return e(s,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:y(r),hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:i.fill}),t(l,{text:"Can not load the image...",fontSize:o,width:r.a.width,height:r.a.height,align:"center",fill:i.textFill,verticalAlign:"middle",padding:5})]})});let Z=u;export const setImageLoaderHook=t=>{Z=t};export const useCanvasImage=(t,e)=>Z(t,e);export const useClipImage=t=>{const[e,r]=B(t),[o,a]=useCanvasImage(e,"anonymous"),h="loaded"===r?a:r,n=i.useRef(void 0);return t&&"failed"!==h?o&&(n.current=o):n.current=void 0,[t&&"failed"!==h?o||n.current:void 0,h]};export const useImageLoader=(t,e="",r="")=>{const o=i.useRef(void 0),a=()=>{var t;null===(t=o.current)||void 0===t||t.call(o),o.current=void 0};i.useEffect(()=>a,[]),i.useLayoutEffect(()=>{const i=e.slice(0,200),h=`image with id ${r} url: ${i}`;"loading"!==t||o.current||(o.current=f(h)),"loading"!==t&&a(),"failed"===t&&w(x("IMAGE_FAILED",h,{src:i,elementId:r,reason:"load"}))},[t])};export const ImageElement=a(({element:a,store:n})=>{var s,l,u,p,f;const[w,x]=i.useState(!1),N=i.useRef(null),$=a.isSelected,[tt,et]=(t=>{var e;const[r,o]=i.useReducer(t=>t+1,0),a=q(t),h=a?t.src+"\n"+JSON.stringify(null!==(e=t.colorsReplace)&&void 0!==e?e:null):"",n=i.useRef("loading"),s=i.useRef(""),d=i.useRef(h);return d.current!==h&&(d.current=h,n.current="loading"),i.useEffect(()=>{if(!a||!t.src){return}let e=!1;return(async()=>{const r=b.prepareSvgSource(await b.urlToString(t.src),t.colorsReplace);e||(s.current=r,n.current="loaded",o())})().catch(()=>{e||(n.current="failed",o())}),()=>{e=!0}},[h]),a?[s.current,n.current]:[t.src,"loaded"]})(a),[rt,it]=Z(tt,"anonymous"),ot="loaded"===et?it:et;useImageLoader(ot,a.src,a.id);const at=(null===(s=a.page)||void 0===s?void 0:s._exportingOrRendering)?1:Math.max(1,n.scale),ht=n._elementsPixelRatio*at,nt=(({image:t,status:e})=>{const r=i.useRef(void 0);return i.useEffect(()=>{r.current=t||r.current},[t]),"failed"!==e?r.current:void 0})({image:rt,status:ot}),st=((t,e,r)=>{const o=q(t)&&e?svgSourceRatio({cropWidth:t.a.cropWidth,cropHeight:t.a.cropHeight,width:t.a.width,height:t.a.height},e,r):1,a=i.useMemo(()=>{var r,i;const{flipX:a,flipY:h}=t,n=q(t),s=navigator.userAgent.toLowerCase().indexOf("firefox")>-1,d=(/^((?!chrome|android).)*safari/i.test(navigator.userAgent)||s)&&n||t.maskSrc;if(!a&&!h&&!d){return e}if(!e||!e.width||!e.height){return null}const c=document.createElement("canvas");S(c,Math.max(e.width*o,1),Math.max(e.height*o,1));let l=a?-c.width:0,g=h?-c.height:0;return null===(r=c.getContext("2d"))||void 0===r||r.scale(a?-1:1,h?-1:1),null===(i=c.getContext("2d"))||void 0===i||i.drawImage(e,l,g,c.width,c.height),c},[t.maskSrc,t.flipX,t.flipY,e,o]);return i.useEffect(()=>()=>{a&&"CANVAS"===a.nodeName&&m.Util.releaseCanvas(a)},[a]),a})(a,rt||nt,ht),dt=((t,e)=>{const[r,o]=B(t.maskSrc||""),[a,h]=Z(r,"anonymous"),n=t.maskSrc?"loaded"===o?h:o:"loaded";return useImageLoader(n,t.maskSrc||"",t.id),i.useMemo(()=>{if(!a){return e}if(!e||!e.width||!e.height){return e}const{width:r,height:i}=maskCompositeSize(e,{width:t.a.width,height:t.a.height,stretchEnabled:t.stretchEnabled}),o=document.createElement("canvas");o.width=r,o.height=i;const h=o.getContext("2d");if(!h){return e}h.drawImage(e,0,0,r,i,0,0,r,i),h.globalCompositeOperation="source-in";const n=function(t,e){const r=e.width/e.height;let i,o;return r>=t.width/t.height?(i=t.width,o=t.width/r):(i=t.height*r,o=t.height),{x:(t.width-i)/2,y:(t.height-o)/2,width:i,height:o}}(a,t);return h.drawImage(a,n.x,n.y,n.width,n.height,0,0,r,i),o},[e,a,t.a.width,t.a.height,t.stretchEnabled])})(a,st)||J,[ct=J]=j(a,dt);i.useLayoutEffect(()=>{"loaded"===ot&&a._setSourceSize({width:ct.width,height:ct.height},a.src)},[a,a.src,ot,ct.width,ct.height]);let{cropX:lt,cropY:gt,cropWidth:ut,cropHeight:mt}=a.a;"loaded"!==it&&(lt=gt=0,ut=mt=1);const pt=M({stretchEnabled:a.stretchEnabled,cropX:lt,cropY:gt,cropWidth:ut,cropHeight:mt,width:a.a.width,height:a.a.height},ct),ft=pt.width,wt=pt.height,xt=a.stretchEnabled,vt=null!==(l=a.cornerRadius)&&void 0!==l?l:0,Mt=(null===(u=a.page)||void 0===u?void 0:u._exportingOrRendering)?1:Math.min(2,n.scale),yt=n._elementsPixelRatio*Mt,Et=w||a._cropModeEnabled||"svg"===a.type,bt=function(t,e,r,i){const{x:o,y:a,width:h,height:n}=t.a,s={x:o,y:a,width:h,height:n,deviceWidth:0,deviceHeight:0,phaseX:0,phaseY:0,spanX:0,spanY:0,aligned:!1};if(!i){return s}const d=o*e,c=a*e,l=Math.round(d),g=Math.round(c),u=Math.max(1,Math.round(d+h*e)-l),m=Math.max(1,Math.round(c+n*e)-g),p=t=>Math.abs(t-Math.round(t))>.001;return p(d)||p(c)||p(h*e)||p(n*e)?u>P||m>P?s:F(r,u,m)||r<=1&&u*m<=4e6?{x:l/e,y:g/e,width:u/e,height:m/e,deviceWidth:u,deviceHeight:m,phaseX:d-l,phaseY:c-g,spanX:h*e,spanY:n*e,aligned:!0}:s:s}(a,yt,G(a,pt,yt),U(a.page)&&0===a.a.rotation&&"group"!==(null===(p=a.parent)||void 0===p?void 0:p.type)&&!Et),St=bt.aligned?bt:{x:a.x,y:a.y,width:a.a.width,height:a.a.height};let Ct=((t,e,r,o)=>{const[a,h]=useClipImage(t.clipSrc||""),n=t.clipSrc?h:"loaded";useImageLoader(n,t.clipSrc,t.id);const s=i.useMemo(()=>{if(e&&a){return document.createElement("canvas")}},[e,a]);return i.useLayoutEffect(()=>{var i;if(!a){return}if(!e||!e.width||!e.height){return}if(!a||!a.width||!a.height){return}if(!s){return}const o=document.createElement("canvas"),h=Math.max(t.a.width/a.width*r,t.a.height/a.height*r);S(o,Math.min(1e4,Math.max(a.width*h,1)),Math.min(1e4,Math.max(a.height*h,1))),null===(i=o.getContext("2d"))||void 0===i||i.drawImage(a,0,0,o.width,o.height),S(s,Math.min(1e4,Math.max(e.width,Math.ceil(t.a.width*r),1)),Math.min(1e4,Math.max(e.height,Math.ceil(t.a.height*r),1)));const n=s.getContext("2d");n&&(n.save(),n.drawImage(o,0,0,s.width,s.height),m.Util.releaseCanvas(o),n.globalCompositeOperation="source-in",n.drawImage(e,0,0,s.width,s.height),n.restore())},[s,e,a,t.a.width,t.a.height,r,...o]),i.useEffect(()=>()=>{s&&m.Util.releaseCanvas(s)},[s]),t.clipSrc&&a?s:e})(a,useCornerRadiusAndCrop(a,ct,pt,yt,vt,Et,!0,v({borderSize:a.borderSize,borderColor:o(a.borderColor),width:a.a.width,height:a.a.height,opacity:E(a)}),bt),yt,[pt,vt,yt]);var It=i.useRef(null),Rt=i.useRef(0),Xt=i.useRef(!1);const Yt=()=>{var t;a._cropEnabled&&!a.pendingEdit&&(null===(t=n.activeTool)||void 0===t?void 0:t.elementId)!==a.id&&setTimeout(()=>{h(a)&&a.toggleCropMode(!0)})},Ht=q(a)&&nt,Wt="loading"===ot&&!Ht,[jt]=D(Wt,100,!1,!1),Ot="failed"===ot,At=!jt&&!Ot,Dt=i.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),kt=i.useRef(!1),zt=At?y(a):0;O(N,zt);const Tt=a.selectable||n.role===g.ADMIN,Lt=A(),_t=k(a,a.borderColor,"stroke");return e(r,{children:[jt&&t(Q,{element:a}),Ot&&t(K,{element:a}),t(d,{ref:N,name:"element",id:a.id,image:Ct,x:bt.x,y:bt.y,width:bt.width||1,height:bt.height||1,rotation:a.a.rotation,opacity:zt,shadowEnabled:a.shadowEnabled,shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:o(a.shadowColor),shadowOpacity:a.shadowOpacity,customCrop:pt,listening:Tt,draggable:!a.pendingEdit&&(null===(f=n.activeTool)||void 0===f?void 0:f.elementId)!==a.id&&(Lt?a.draggable&&$:a.draggable),preventDefault:!Lt||$,hideInExport:!a.showInExport,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(X(t.target))},onDblClick:Yt,onDblTap:Yt,onTransformStart:t=>{x(!0),kt.current=Y(C(t.target)),Dt.current={cropX:a.cropX,cropY:a.cropY,cropWidth:a.cropWidth,cropHeight:a.cropHeight}},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),i=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const o=1-ft/ct.width,h=Math.min(o,Math.max(0,a.cropX)),n=1-wt/ct.height,s=Math.min(n,Math.max(0,a.cropY)),d=kt.current,c=!d&&r<1&&Dt.current.cropHeight>wt/ct.height;let l=d?a.cropWidth:a.cropWidth*r;c&&(l=a.cropWidth);const g=!d&&i<1&&Dt.current.cropWidth>ft/ct.width;let u=d?a.cropHeight:a.cropHeight*i;g&&(u=a.cropHeight),xt&&(l=a.cropWidth,u=a.cropHeight);const m=d?Math.min(r,i):1;a.set(Object.assign({cropX:h,cropY:s,x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*i,rotation:t.target.rotation(),cropWidth:Math.min(l,1-h),cropHeight:Math.min(u,1-s)},1!==m&&H(a,m)))},onTransformEnd:t=>{const e=t.currentTarget;a.set(Object.assign({width:R(e.width()),height:R(e.height()),x:R(e.x()),y:R(e.y()),rotation:R(t.target.rotation()),cropX:I(a.cropX),cropY:I(a.cropY),cropWidth:I(ft/ct.width),cropHeight:I(wt/ct.height)},W(a))),x(!1)}}),t(c,Object.assign({x:St.x,y:St.y,width:Math.max(St.width-a.borderSize,0),height:Math.max(St.height-a.borderSize,0),opacity:zt,offsetX:-a.borderSize/2,offsetY:-a.borderSize/2},_t,{strokeWidth:a.borderSize,listening:!1,visible:!!a.borderSize,rotation:a.rotation,cornerRadius:Math.max(0,vt-a.borderSize),hideInExport:!a.showInExport})),a._cropModeEnabled&&"loaded"===ot&&t(L,{element:a,store:n,previewImage:Ct,sourceImage:ct,sourceWidth:ct.width,sourceHeight:ct.height,cropAbsoluteWidth:ft,cropAbsoluteHeight:wt,onSourceTouchMove:t=>{t.evt.preventDefault();const e=t.target.getStage().getPointersPositions();var r=e[0],i=e[1];const o=t.target;if(r&&!i&&!o.isDragging()&&Xt.current&&(o.startDrag(),Xt.current=!1),r&&i){m.hitOnDragEnabled=!0,o.isDragging()&&(Xt.current=!0,o.stopDrag());const e=t.target.getAbsoluteTransform().copy();e.invert();var h={x:r.x,y:r.y},s={x:i.x,y:i.y};if(!It.current){return void(It.current=V(h,s))}var d=V(h,s),c=function(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}(h,s);Rt.current||(Rt.current=c);const w=o.position();var l={x:d.x-w.x,y:d.y-w.y},g=c/Rt.current;o.scaleX(g),o.scaleY(g);const x=e.point(d),v=e.point(It.current);var u=x.x-v.x,p=x.y-v.y,f={x:Math.min(0,d.x-l.x*g+u),y:Math.min(0,d.y-l.y*g+p)};o.position(f),Rt.current=c,It.current=d,(t=>{z(t,a,T(a,n),ct.width,ct.height,ft,wt)})(t)}},onSourceTouchEnd:t=>{Rt.current=0,It.current=null,m.hitOnDragEnabled=!1}}),"image"===a.type&&t(_,{element:a,store:n})]})});
|
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 c}from"@polotno/schema";import{Group as d,Image as u,Label as h,Layer as m,Line as g,Rect as f,Shape as p,Stage as x,KonvaRenderer as v,Tag as b,Text as y,Transformer as w}from"react-konva";import{useWorkspaceStyle as E}from"./workspace-style.js";import{ROLES as X}from"../model/store.js";import Y from"konva";import S from"./element.js";import{DrawingLayer as k}from"./drawing-layer.js";import{ensureDragOrder as A,useSnap as I}from"./use-transformer-snap.js";import{useCanvasImage as M,useImageLoader as O}from"./image-element.js";import{FadeOverlay as P}from"./fade-overlay.js";import{getCrop as j}from"../utils/crop.js";import{___ as R,____ as T,isCreditVisible as B}from"../utils/validate-key.js";import{getClientRect as D,getCorners as C,getTotalClientRect as W,polygonsIntersect as G}from"../utils/math.js";import{pxToUnitRounded as L,pxToUnitString as F}from"../utils/unit.js";import{isCornerAnchor as V,MAIN_TRANSFORMER_NAME as z,textMinResizeWidth as U,textMinWidthIsMeasured as Z}from"../utils/px.js";import{restoreHeldPoint as _}from"./transform-box.js";import{flags as H}from"../utils/flags.js";import{useColor as J}from"./use-color.js";import{isGradient as N}from"@polotno/core/gradient";import{toDisplayColor as K}from"@polotno/core/color";import{Html as $}from"react-konva-utils";const Q=o.version.startsWith("18.")?o.forwardRef((e,n)=>(o.useLayoutEffect(()=>{v.flushSync()}),t(x,Object.assign({},e,{ref:n})))):x,q={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},ee=e=>{const t=(e%180+180)%180;return t<.1||180-t<.1},te=e=>{if(!e.length){return null}const t=ee(e[0].rotation)?0:e[0].rotation;return e.every(e=>ee(e.rotation-t))?t:null},ne=e=>e.selectedShapes.filter(t=>{var n;return(null===(n=e.activeTool)||void 0===n?void 0:n.elementId)!==t.id&&!t.pendingEdit}),ie={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){ie[e]=ie[e]||t,Object.assign(ie[e],t)}const oe=e=>t(f,Object.assign({},e,{preventDefault:!1})),re=n=>{var{url:i}=n,o=e(n,["url"]);const[r,l]=M(i,"anonymous"),s=r?j(r,{width:o.width,height:o.height},"center-middle"):{};return O(l,i,"page background"),t(u,Object.assign({image:r},o,s))},le=e=>{const n=J({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return t(f,Object.assign({},e,n))},se=n=>{var{background:i,transparencyBackgroundColor:r}=n,l=e(n,["background","transparencyBackgroundColor"]);const s=o.useMemo(()=>{if(N(i)){return!0}const e=Y.Util.colorToRGBA(i);return!e||e.a<1},[i]),a=o.useMemo(()=>{if(!s||r){return}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},[s,r]);return s?t(f,r?Object.assign({fill:r},l,{hideInExport:!0}):Object.assign({fillPatternImage:a},l,{opacity:.1,hideInExport:!0})):null},ae=n=>{var{background:i}=n,r=e(n,["background"]);return o.useMemo(()=>!!Y.Util.colorToRGBA(i)||N(i),[i])?t(le,Object.assign({fill:i},r)):t(re,Object.assign({url:i},r))},ce=r(({selection:e})=>{const{selectionRectStyle:n}=E();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 de(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const ue=r(({x:e,y:o,width:r,height:l,rotation:s,anchor:a,store:c})=>{const{transformLabelStyle:d}=E();if(void 0===e||void 0===o||void 0===r||void 0===l||void 0===s||void 0===a){return null}const u=D({x:e,y:o,width:r,height:l,rotation:Y.Util.radToDeg(s)}),m=(l/2+70)*Math.cos(s-Math.PI/2),g=(l/2+70)*Math.sin(s-Math.PI/2),f=L({unit:c.unit,dpi:c.dpi,px:r/c.scale,precious:"px"===c.unit?0:1})+" x "+L({unit:c.unit,dpi:c.dpi,px:l/c.scale,precious:"px"===c.unit?0:1})+("px"===c.unit?"":" "+c.unit);return n(i,{children:[n(h,{x:(u.minX+u.maxX)/2+m,y:(u.minY+u.maxY)/2+g,offsetX:14,offsetY:14,visible:"rotater"===a,children:[t(b,{cornerRadius:5,fill:d.fill}),t(y,{align:"center",verticalAlign:"middle",fill:d.textFill,padding:8,text:de(s).toString()+"°"})]}),n(h,{x:(u.minX+u.maxX)/2,y:u.maxY+20,visible:"rotater"!==a,children:[t(b,{cornerRadius:5,fill:d.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),t(y,{align:"center",verticalAlign:"middle",fill:d.textFill,padding:8,text:f})]})]})}),he=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(S,{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 me=null;export const registerNextDomDrop=e=>{me=e};const ge=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),fe=atob("I2MwMzkyYg=="),pe=atob("djAuOS4y"),xe=e=>t(i,{children:n(h,{fill:fe,height:200,children:[t(b,{fill:fe}),t(y,Object.assign({},e,{fill:"white",text:ge,height:void 0,padding:10,fontSize:20}))]})}),ve=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),be=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),ye=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),we=e=>{const i=1===T(),o=i?be:ve,r=i?230:195;return n(d,Object.assign({},e,{listening:!1,children:[t(f,{fill:fe,width:r,height:24,cornerRadius:2}),t(y,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),i&&t(y,{text:ye,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default r(({store:i,page:r,width:u,height:x,pageControlsEnabled:v,components:S,altCloneEnabled:M,groupSelectionMode:O="drill",viewportSize:j,layout:L="vertical",tooltipSafeArea:J,transformerSafeArea:N})=>{var re,le;const de=E(),ge=de.transformerStyle,fe=q,ve=r.computedBleed,be=i.bleedVisible?ve:c,ye=r.computedWidth+be.left+be.right,Ee=r.computedHeight+be.top+be.bottom,Xe=(u-ye*i.scale)/2,Ye=(x-Ee*i.scale)/2,Se={x:-ve.left,y:-ve.top,width:r.computedWidth+ve.left+ve.right,height:r.computedHeight+ve.top+ve.bottom,background:K(r.background),name:"page-background",preventDefault:!1},ke={x:-be.left,y:-be.top,width:ye,height:Ee},Ae=o.useRef(null),Ie=o.useRef(null),Me=o.useRef(null),Oe=o.useRef(null),Pe=null==v||v,je=o.useRef(!1),[Re,Te]=o.useState(null),[Be,De]=o.useState({}),[Ce,We]=o.useState(0),Ge=useContextMenu({store:i}),Le=i.selectedElements.find(e=>e.curveEnabled),Fe=i.selectedShapes.filter(e=>!e.resizable).length>0,Ve=i.selectedShapes.filter(e=>!e.draggable).length>0,ze=i.selectedElements.filter(e=>!e.visible).length>0,Ue=ne(i),Ze=JSON.stringify(Ue.map(e=>e.id)),_e=1!==i.selectedElements.length||"group"===(null===(re=i.selectedElements[0])||void 0===re?void 0:re.type),He=null!==(le=te(Ue))&&void 0!==le?le:0,Je=e=>_e&&Ue.length>0&&Ue.every(t=>((e,t,n)=>{if(!ee(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,He)),Ne=!_e&&i.selectedElements[0].keepRatio,Ke=Je("x"),$e=Je("y"),Qe=()=>{var t,n,o;const r=Ae.current;if(!r){return[]}const l=r.getStage();if(!l){return[]}const s=ne(i),a=s.map(e=>l.findOne("#"+e.id)).filter(e=>!!e),c=1===i.selectedElements.length,d=c&&(null===(t=i.selectedElements[0])||void 0===t?void 0:t.type)||"many",u=ie[d],h=Object.assign(Object.assign({},fe),u||{});u&&("svg"!==d&&"image"!==d&&"gif"!==d||i.selectedElements[0].keepRatio||(h.enabledAnchors=fe.enabledAnchors),"text"===d&&H.textVerticalResizeEnabled&&(h.enabledAnchors=null===(n=ie.text.enabledAnchors)||void 0===n?void 0:n.concat(["bottom-center","top-center"])),"text"===d&&c&&i.selectedElements[0].curveEnabled&&(h.enabledAnchors=ie.many.enabledAnchors)),(Ke||$e)&&(h.enabledAnchors=[...h.enabledAnchors||[],...Ke?["middle-left","middle-right"]:[],...$e?["top-center","bottom-center"]:[]]);const{boundBoxFunc:m}=ge,g=e(ge,["boundBoxFunc"]);r.setAttrs(Object.assign(Object.assign(Object.assign({},h),g),{name:z})),Fe&&r.enabledAnchors([]),Ve&&r.rotateEnabled(!1);const f=r.nodes();if(f.length===a.length&&a.every((e,t)=>f[t]===e)||r.nodes(a),_e&&a.length){const e=te(s);null===e||ee(r.rotation()-e)||(r.rotation(e),r.forceUpdate())}return null===(o=r.getLayer())||void 0===o||o.batchDraw(),a};o.useLayoutEffect(()=>{var e;if(null===(e=Ae.current)||void 0===e?void 0:e.isTransforming()){return}const t=Qe();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),A())},[i.selectedShapes,Ze,Fe,ze,Ve,Le,Ke,$e,Ne,He,ge]);const qe=()=>{var e,t;const n=Ae.current;if(!n){return}if(!n.isTransforming()){return void De({})}if(!n.nodes().length||!n.isTransforming()){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();De({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})},et=o.useCallback(()=>{var e;if(null===(e=Ae.current)||void 0===e?void 0:e.isTransforming()){return}const t=Ae.current;if(!t||!t.nodes().length){return void We(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,c=e=>{const t=e*Math.PI/180,i=(e%360+360)%360;let c;c=0===i||180===i?s:l;const d=Math.sin(t)*(c+n),u=-Math.cos(t)*(c+n),h=d*Math.cos(a)-u*Math.sin(a),m=d*Math.sin(a)+u*Math.cos(a);return{x:o+h,y:r+m}},d=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(d(c(0))){return 0}for(const u of[180,270,90]){if(d(c(u))){return u}}return 0}(n,{top:(null==N?void 0:N.top)||0,bottom:x-((null==N?void 0:N.bottom)||0),left:(null==N?void 0:N.left)||0,right:u-((null==N?void 0:N.right)||0)},ge.rotateAnchorOffset,t.rotation());We(i)},[N,u,x]);o.useEffect(()=>{var e;null===(e=Ae.current)||void 0===e||e.update(),qe(),et()},[i.scale,et]),o.useEffect(()=>{et()},[i.selectedShapes,et]);const tt=l(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),nt=o.useRef(!1),it=e=>{const t=Me.current;if(!t||i.role===X.VIEWER||i.activePage!==r||i.isPlaying||"button"in e.evt&&0!==e.evt.button||1!==t.getPointersPositions().length||!e.target.findAncestor(".elements-container")||!e.target.findAncestor(".element",!0)||!i.selectedShapes.length||i.selectedShapes.some(e=>!e.draggable)){return}const n=t.getPointerPosition();if(!n){return}const o=Qe();if(o.some(t=>t===e.target||t.isAncestorOf(e.target))){return}const l=o.find(e=>!!(e.draggable()&&e.isVisible()&&e.isListening())&&(e instanceof Y.Shape?[e]:e instanceof Y.Container?e.find("Shape"):[]).some(e=>e.intersects(n)));if(l){for(const[t,n]of Y.DD._dragElements){"ready"!==n.dragStatus||n.node!==e.target&&!n.node.isAncestorOf(e.target)||Y.DD._dragElements.delete(t)}l._createDragElement(e),"touches"in e.evt&&e.evt.cancelable&&l.preventDefault()&&e.evt.preventDefault()}},ot=s(e=>{var t,n,o,r,l,s,a;if(i.role===X.VIEWER){return}if(nt.current=!1,i.activeTool&&"crop"!==i.activeTool.name){return}const c=e.target.findAncestor(".elements-container"),d=e.target.findAncestor("Transformer"),u=e.target.findAncestor(".page-abs-container");if(c||d||u){return}const h=null===(t=e.target.getStage())||void 0===t?void 0:t.getPointerPosition();h&&(h.x-=null!==(o=null===(n=e.target.getStage())||void 0===n?void 0:n.x())&&void 0!==o?o:0,h.y-=null!==(l=null===(r=e.target.getStage())||void 0===r?void 0:r.y())&&void 0!==l?l:0,tt.visible=!0,tt.x1=h.x,tt.y1=h.y,tt.x2=h.x,tt.y2=h.y,(null!==(a=null===(s=e.target.getStage())||void 0===s?void 0:s.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(tt.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:Me,containerRef:Oe,viewportSize:j}),o.useEffect(()=>{const e=s(e=>{var t,n,i,o,r,l;if(!tt.visible){return}null===(t=Me.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=Me.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(o=null===(i=Me.current)||void 0===i?void 0:i.x())&&void 0!==o?o:0,s.y-=null!==(l=null===(r=Me.current)||void 0===r?void 0:r.y())&&void 0!==l?l:0):s={x:tt.x2,y:tt.y2},tt.x2=s.x,tt.y2=s.y}),t=s(()=>{if(!tt.visible){return}if(!Me.current){return}const e=Me.current.findOne(".selection"),t=e?e.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(t.width&&t.height){const e=[{x:t.x,y:t.y},{x:t.x+t.width,y:t.y},{x:t.x+t.width,y:t.y+t.height},{x:t.x,y:t.y+t.height}],n=[];Me.current.find(".element").forEach(t=>{var o;const r=i.getElementById(t.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=t.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const c=C(r.a).map(e=>a.point(e));G(e,c)&&n.push(r.top.id)});const o=[...new Set(n)];nt.current=!0,i.selectElements(o)}tt.visible=!1});return window.addEventListener("mousemove",e),window.addEventListener("touchmove",e),window.addEventListener("mouseup",t,{capture:!0}),window.addEventListener("touchend",t,{capture:!0}),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("touchmove",e),window.removeEventListener("mouseup",t),window.removeEventListener("touchend",t)}},[]);const rt=o.useCallback(e=>{if(!i.selectedElements[0]){return}if(e&&i.selectedElementsIds.includes(e)){return}const t=W(i.selectedShapes),n=e?i.getElementById(e):{x:0,y:0,width:r.computedWidth,height:r.computedHeight,rotation:0},o=D(n),l=[];t.minX>o.maxX&&l.push({distance:t.minX-o.maxX,box1:t,box2:o,points:[{x:t.minX,y:t.minY+t.height/2},{x:o.maxX,y:t.minY+t.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),t.maxX<o.minX&&l.push({distance:o.minX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:t.minY+t.height/2},{x:o.minX,y:t.minY+t.height/2},{x:o.minX,y:o.minY+o.height/2}]}),t.minY>o.maxY&&l.push({box1:t,box2:o,distance:t.minY-o.maxY,points:[{x:t.minX+t.width/2,y:t.minY},{x:t.minX+t.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]}),t.maxY<o.minY&&l.push({box1:t,box2:o,distance:o.minY-t.maxY,points:[{x:t.minX+t.width/2,y:t.maxY},{x:t.minX+t.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]});const s=t.minX>=o.minX&&t.maxX<=o.maxX&&t.minY>=o.minY&&t.maxY<=o.maxY;if(s&&(l.push({distance:t.minX-o.minX,box1:t,box2:o,points:[{x:t.minX,y:t.minY+t.height/2},{x:o.minX,y:t.minY+t.height/2},{x:o.minX,y:o.minY+o.height/2}]}),l.push({distance:o.maxX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:t.minY+t.height/2},{x:o.maxX,y:t.minY+t.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),l.push({box1:t,box2:o,distance:t.minY-o.minY,points:[{x:t.minX+t.width/2,y:t.minY},{x:t.minX+t.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]}),l.push({box1:t,box2:o,distance:o.maxY-t.maxY,points:[{x:t.minX+t.width/2,y:t.maxY},{x:t.minX+t.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]})),0===l.length&&!s){const e=t.minX<o.maxX&&t.maxX>o.minX,n=t.minY<o.maxY&&t.maxY>o.minY;if(e&&n){const e=(Math.max(t.minY,o.minY)+Math.min(t.maxY,o.maxY))/2,n=(Math.max(t.minX,o.minX)+Math.min(t.maxX,o.maxX))/2;t.minX<o.minX?l.push({distance:o.minX-t.minX,box1:t,box2:o,points:[{x:t.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}):l.push({distance:t.minX-o.minX,box1:t,box2:o,points:[{x:t.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}),t.maxX>o.maxX?l.push({distance:t.maxX-o.maxX,box1:t,box2:o,points:[{x:o.maxX,y:e},{x:t.maxX,y:e},{x:o.maxX,y:e}]}):l.push({distance:o.maxX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:e},{x:o.maxX,y:e},{x:o.maxX,y:e}]}),t.minY<o.minY?l.push({box1:t,box2:o,distance:o.minY-t.minY,points:[{x:n,y:t.minY},{x:n,y:o.minY},{x:n,y:o.minY}]}):l.push({box1:t,box2:o,distance:t.minY-o.minY,points:[{x:n,y:t.minY},{x:n,y:o.minY},{x:n,y:o.minY}]}),t.maxY>o.maxY?l.push({box1:t,box2:o,distance:t.maxY-o.maxY,points:[{x:n,y:o.maxY},{x:n,y:t.maxY},{x:n,y:o.maxY}]}):l.push({box1:t,box2:o,distance:o.maxY-t.maxY,points:[{x:n,y:t.maxY},{x:n,y:o.maxY},{x:n,y:o.maxY}]})}}JSON.stringify(Re)!==JSON.stringify(l)&&Te(l)},[i,r]);o.useEffect(()=>{i.selectedElements[0]&&i.distanceGuidesVisible?rt():Re&&Te(null)},[i.distanceGuidesVisible,rt,i.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|")]);const lt=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=Oe.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{lt.current=!0,clearTimeout(t),t=setTimeout(()=>{lt.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const e=Me.current;if(!e){return}const t=e.container(),n=new Map,o=e=>{var t,i;n.has(e.target)||[e.target,...null!==(i=null===(t=Ae.current)||void 0===t?void 0:t.nodes())&&void 0!==i?i:[]].forEach(e=>{n.has(e)||n.set(e,{x:e.x(),y:e.y()})})},r=e=>{e.target===Ae.current?n.clear():n.delete(e.target)};e.on("dragstart",o),e.on("dragend",r);const l=e=>{e.touches.length>=2&&a(()=>{var e,t;const o=new Map(n);(null===(e=Ae.current)||void 0===e?void 0:e.isDragging())&&Ae.current.stopDrag(),null===(t=Ae.current)||void 0===t||t.nodes().forEach(e=>{var t;const n=o.get(e);e.isDragging()&&e.stopDrag(),n&&(e.setAttrs(n),null===(t=i.getElementById(e.id()))||void 0===t||t.set({x:n.x,y:n.y}))})})};return t.addEventListener("touchstart",l,{passive:!0}),t.addEventListener("touchmove",l,{passive:!0}),()=>{e.off("dragstart",o),e.off("dragend",r),t.removeEventListener("touchstart",l),t.removeEventListener("touchmove",l)}},[i]);const st=e=>{if(i.role===X.VIEWER){return}if(i.activePage!==r&&r.select(),lt.current){return}if(nt.current){return}const t=e.evt.shiftKey,n=e.evt.ctrlKey||e.evt.metaKey,o=t||n,l=e.target.findAncestor(".elements-container"),s=e.target.findAncestor(".page-abs-container"),a=e.target.findAncestor("Transformer"),c=e.target.findAncestor(".page-container",!0),d=!(o||l||a||s||c||tt.visible);if(i.activeTool&&"crop"!==i.activeTool.name&&!l&&!a){return}if(d){return i.selectElements([]),void i.selectPages([])}if(n&&!t){const e=Me.current,t=null==e?void 0:e.getPointerPosition();if(!e||!t){return}const n=e.getAllIntersections(t),o=new Set,r=[];for(const s of[...n].reverse()){const e=s.findAncestor(".element",!0);if(!e){continue}const t=i.getElementById(e.id()),n=null==t?void 0:t.top;n&&(o.has(n.id)||(o.add(n.id),r.push(n.id)))}if(0===r.length){return}let l;if(1===i.selectedElementsIds.length){const e=i.selectedElementsIds[0],t=r.indexOf(e);l=-1===t?r[0]:r[(t+1)%r.length]}else{l=r[0]}return i.selectElements([l]),void i.selectPages([])}const u=e.target.findAncestor(".element-container",!0),h=(null==u?void 0:u.findOne(".element"))||e.target.findAncestor(".element",!0),m=i.getElementById(null==h?void 0:h.id());m&&"pendingEdit"in m&&m._setImageEditError(null);const g=null==m?void 0:m.top;let f;if("group"===O){f=null==g?void 0:g.id}else if(g){const e=i.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===g.id});f=e?null==m?void 0:m.id:null==g?void 0:g.id}else{f=void 0}const p=null==m?void 0:m.id;if(p&&i.selectedElementsIds.indexOf(p)>=0&&!t){return}const x=!!f&&i.selectedElementsIds.indexOf(f)>=0,v=e.target.findAncestor(".page-container",!0);if(f&&t&&!x){const e=i.selectedElementsIds.concat([f]),t=new Set(e),n=e.filter(e=>{const n=i.getElementById(e);if(!n){return!1}for(let i=n.parent;i;i=null==i?void 0:i.parent){if(t.has(i.id)){return!1}}return!0});i.selectElements(n),i.selectPages([])}else{f&&t&&x?i.selectElements(i.selectedElementsIds.filter(e=>e!==f)):!f||t||x?f&&!t&&x||(v?(t||i.selectElements([]),i.selectPages([r.id])):i.selectPages([])):(i.selectElements([f]),i.selectPages([]))}};I(Ae,i);const at=i.activePage===r,ct=i._selectedPagesIds.includes(r.id),dt=null==S?void 0:S.PageControls,ut=null==S?void 0:S.Tooltip,ht=null==S?void 0:S.ContextMenu,mt=1/i.scale,gt=0/i.scale;return n("div",{ref:Oe,onDragOver:e=>e.preventDefault(),onDrop:e=>{if(e.preventDefault(),i.role===X.VIEWER){return}if(!Me.current){return}Me.current.setPointersPositions(e);const t=Me.current.findOne(".elements-container"),n=null==t?void 0:t.getRelativePointerPosition(),o=Me.current.getPointerPosition(),l=Me.current.getAllIntersections(o).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(l.reverse())].map(e=>i.getElementById(e.id())).filter(e=>!!e),a=s[0];me&&n&&(me(n,a,{elements:s,page:r}),me=null)},style:{position:"relative",width:u+"px",height:x+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+(at?" active-page":""),children:[t(Q,{ref:Me,eventBatchFunc:a,width:Math.min(u,j.width+200),height:Math.min(j.height+200,x),onClick:st,onTap:st,onContextMenu:e=>{if(e.evt.preventDefault(),i.role===X.VIEWER){return}const t=e.target.findAncestor(".element",!0),n=i.getElementById(null==t?void 0:t.id()),o=null==n?void 0:n.top,r=null==o?void 0:o.id;if(!r){return i.selectElements([]),void Ge.close()}i.selectedElementsIds.indexOf(r)>=0||i.selectElements([r]),Ge.open({x:e.evt.clientX,y:e.evt.clientY})},onMouseDown:e=>{it(e),ot(e)},onTouchStart:it,onMouseMove:e=>{if(!i.distanceGuidesVisible&&!e.evt.altKey){return void(Re&&Te(null))}const t=e.target.findAncestor(".element",!0),n=null==t?void 0:t.id();rt(n)},onDragStart:e=>{var t;const n=e.target.getStage();if(n&&n.getPointersPositions().length>=2){return void e.target.stopDrag()}const o=e.target.findAncestor(".element",!0);if(o){const n=i.getElementById(null==o?void 0:o.id()),r=null==n?void 0:n.top,l=null==r?void 0:r.id;if(!(n&&i.selectedElementsIds.indexOf(n.id)>=0||l&&i.selectedElementsIds.indexOf(l)>=0)&&l){i.selectElements([l]);const n=Qe();e.target.stopDrag(),n.forEach(t=>{t.isDragging()||t.startDrag(e)}),null===(t=Ae.current)||void 0===t||t.startDrag(e),A()}}Re&&Te(null)},onDragMove:e=>{const t=Ae.current;t&&e.target===t&&t.nodes().forEach(e=>{const t=i.getElementById(e.id());!t||t.x===e.x()&&t.y===e.y()||t.set({x:e.x(),y:e.y()})})},pageId:r.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:n(m,{children:[t(oe,{width:u,height:x,fill:de.workspaceBackgroundColor}),t(d,{x:Xe,y:Ye,scaleX:i.scale,scaleY:i.scale,name:"page-container",children:n(d,{name:"page-container-2",children:[t(d,{x:be.left,y:be.top,children:t(se,Object.assign({},Se,ke,{transparencyBackgroundColor:de.transparencyBackgroundColor}))}),n(d,{isolated:r.hasBlendMode,children:[t(d,{name:"page-background-group",x:be.left,y:be.top,clip:ke,children:t(ae,Object.assign({},Se))}),n(d,{x:be.left,y:be.top,name:"elements-container",listening:!i.isPlaying&&i.role!==X.VIEWER,children:[t(f,{name:"elements-area",width:r.computedWidth,height:r.computedHeight,listening:!1}),t(he,{elements:r.children,store:i})]})]}),t(p,{name:"bleed",fill:de.bleedColor,listening:!1,visible:i.bleedVisible&&(be.top>0||be.right>0||be.bottom>0||be.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:i,bottom:o,left:l}=ve,s=r.computedWidth,a=r.computedHeight,c=e._context;c.beginPath(),c.rect(0,0,l+s+i,n+a+o),c.rect(l,n,s,a),c.fillStyle=t.fill(),c.fill("evenodd")}}),R()===pe&&t(xe,{name:"hit-detection",x:-mt/2-gt,y:-mt/2-gt,width:ye+mt+2*gt,height:Ee+mt+2*gt})]})}),t(g,{name:"workspace-background",points:[0,0,u,0,u,x,0,x,0,0,Xe,Ye,Xe,x-Ye,u-Xe,x-Ye,u-Xe,Ye,Xe,Ye],listening:!1,closed:!0,fill:de.workspaceBackgroundColor}),t(d,{x:Xe,y:Ye,scaleX:i.scale,scaleY:i.scale,children:t(f,{name:"page-highlight",hideInExport:!0,x:-mt/2-gt,y:-mt/2-gt,width:ye+mt+2*gt,height:Ee+mt+2*gt,stroke:ct?de.activePageBorderColor:de.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),n(d,{x:Xe+be.left*i.scale,y:Ye+be.top*i.scale,scaleX:i.scale,scaleY:i.scale,name:"page-abs-container",children:[t(w,{ref:Ae,name:z,rotateAnchorAngle:Ce,onDragStart:e=>{var t;const n=e.target.getStage();n&&n.getPointersPositions().length>=2?e.target.stopDrag():((null===(t=e.evt)||void 0===t?void 0:t.altKey)&&M&&i.selectedElements.forEach(e=>{const t=e.clone({},{skipSelect:!0}),n=r.children.indexOf(e);r.setElementZIndex(t.id,n)}),je.current||(je.current=!0,i.history.startTransaction()))},onDragEnd:()=>{je.current&&(i.history.endTransaction(),je.current=!1),et()},onTransformStart:()=>{i.history.startTransaction(),Ie.current=new Map(Ue.map(e=>[e.id,{width:e.width,height:e.height}])),Te(null)},boundBoxFunc:(e,t)=>{var n,o,r,l;const s=null!==(o=null===(n=ge.boundBoxFunc)||void 0===n?void 0:n.call(ge,e,t))&&void 0!==o?o:t,a=Math.abs(s.width)<1||Math.abs(s.height)<1,c=Math.abs(e.width)<1||Math.abs(e.height)<1;if(a&&!c){return e}const d=i.selectedElements;if(1===d.length&&"table"===(null===(r=d[0])||void 0===r?void 0:r.type)){const t=d[0],n=Math.abs(e.width),i=Math.abs(e.height);if(n>0&&i>0){const o=Math.abs(s.width)/n,r=Math.abs(s.height)/i,l=t.width*o,a=t.height*r,c=Object.assign({},s);return l<t.minWidth&&(c.width=e.width),a<t.minHeight&&(c.height=e.height),_(c,s,e)}}return _e&&Ue.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,c=!V(n),d=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,d="x"===e?s:a,u=((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?U(i,e.fontSize):1})(t,e,c,r);return r>=u&&l*d<u}),u=Object.assign({},t),h=t.width*e.width<0,m=t.height*e.height<0,g=h||d("x"),f=m||d("y"),p=f||!c&&g;return(g||!c&&f)&&(u.width=e.width),p&&(u.height=e.height),_(u,t,e)})(e,s,(null===(l=Ae.current)||void 0===l?void 0:l.getActiveAnchor())||"",Ue,Ie.current):s},onTransform:e=>{const t=Ae.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{qe()},0)},onTransformEnd:e=>{De({}),i.history.endTransaction(),Ie.current=null,et(),Qe()},visible:!i.isPlaying}),Re&&Re.map(({points:e,distance:o,box1:r,box2:l},s)=>n(d,{name:"distances-container",hideInExport:!0,listening:!1,children:[t(f,Object.assign({},r,{stroke:de.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(f,Object.assign({},l,{stroke:de.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(g,{points:[e[0].x,e[0].y,e[1].x,e[1].y],stroke:de.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),t(g,{points:[e[1].x,e[1].y,e[2].x,e[2].y],stroke:de.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(h,{x:(e[0].x+e[1].x)/2,y:(e[0].y+e[1].y)/2,offsetY:-10,scaleX:1/i.scale,scaleY:1/i.scale,children:[t(b,{cornerRadius:5,fill:de.distanceGuideStyle.labelFill,pointerDirection:"down"}),t(y,{align:"center",verticalAlign:"middle",fill:de.distanceGuideStyle.labelTextFill,padding:5,text:F({unit:i.unit,dpi:i.dpi,px:o})})]})]},s)),r._rendering&&n(d,{children:[t(f,{width:ye,height:Ee,fill:"rgba(255,255,255,0.9)"}),t(y,{text:"Rendering...",fontSize:60,width:ye,height:Ee,align:"center",verticalAlign:"middle"})]}),ut&&t(ut,{components:S,store:i,page:r,stageRef:Me,tooltipSafeArea:J}),ht&&t($,{children:t(ht,Object.assign({components:S,store:i},Ge.props))}),t(P,{store:i,pageId:r.id})]}),t(k,{store:i,page:r,width:u,height:x,scale:i.scale,xPadding:Xe,yPadding:Ye,bleedLeft:be.left,bleedTop:be.top}),t(ue,Object.assign({},Be,{store:i})),t(ce,{selection:tt}),T()>0&&n(d,{hideInExport:!0,children:[t(we,{name:"cache-bounds",x:u-(1===T()?250:210),y:x-(1===T()?95:45)}),t(we,{name:"cache-bounds-t",x:10,y:10})]}),B()&&t(y,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:u-170,y:x-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(d,{name:"line-guides"})]})}),Pe&&dt&&t(dt,{store:i,page:r,xPadding:Xe,yPadding:Ye,layout:L})]})});
|
|
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 m,Label as u,Layer as h,Line as g,Rect as f,Shape as p,Stage as x,KonvaRenderer as v,Tag as b,Text as y,Transformer as w}from"react-konva";import{useWorkspaceStyle as E}from"./workspace-style.js";import{ROLES as X}from"../model/store.js";import Y from"konva";import S from"./element.js";import{DrawingLayer as k}from"./drawing-layer.js";import{ensureDragOrder as A,useSnap as M}from"./use-transformer-snap.js";import{useCanvasImage as j,useImageLoader as I}from"./image-element.js";import{FadeOverlay as O}from"./fade-overlay.js";import{getCrop as P}from"../utils/crop.js";import{cloneBehind as R}from"../utils/duplicate.js";import{___ as T,____ as D,isCreditVisible as B}from"../utils/validate-key.js";import{getClientRect as C,getCorners as W,getTotalClientRect as G,polygonsIntersect as L}from"../utils/math.js";import{pxToUnitRounded as F,pxToUnitString as V}from"../utils/unit.js";import{isCornerAnchor as z,MAIN_TRANSFORMER_NAME as U}from"../utils/px.js";import{canStretchAxis as Z,isAxisAligned as _,leafMinSize as H}from"../utils/selection-transform.js";import{restoreHeldPoint as J}from"./transform-box.js";import{flags as N}from"../utils/flags.js";import{textOverflowOf as K}from"../utils/text-sizing.js";import{useColor as $}from"./use-color.js";import{isGradient as Q}from"@polotno/core/gradient";import{toDisplayColor as q}from"@polotno/core/color";import{Html as ee}from"react-konva-utils";import{getParentGroupId as te,withoutDescendants as ne}from"../utils/selection.js";const ie=o.version.startsWith("18.")?o.forwardRef((e,n)=>(o.useLayoutEffect(()=>{v.flushSync()}),t(x,Object.assign({},e,{ref:n})))):x,oe={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},re=e=>{if(!e.length){return null}const t=_(e[0].rotation)?0:e[0].rotation;return e.every(e=>_(e.rotation-t))?t:null},le=e=>e.selectedShapes.filter(t=>{var n;return(null===(n=e.activeTool)||void 0===n?void 0:n.elementId)!==t.id&&!t.pendingEdit}),se={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){se[e]=se[e]||t,Object.assign(se[e],t)}const ae=e=>t(f,Object.assign({},e,{preventDefault:!1})),de=n=>{var{url:i}=n,o=e(n,["url"]);const[r,l]=j(i,"anonymous"),s=r?P(r,{width:o.width,height:o.height},"center-middle"):{};return I(l,i,"page background"),t(m,Object.assign({image:r},o,s))},ce=e=>{const n=$({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return t(f,Object.assign({},e,n))},me=n=>{var{background:i,transparencyBackgroundColor:r}=n,l=e(n,["background","transparencyBackgroundColor"]);const s=o.useMemo(()=>{if(Q(i)){return!0}const e=Y.Util.colorToRGBA(i);return!e||e.a<1},[i]),a=o.useMemo(()=>{if(!s||r){return}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},[s,r]);return s?t(f,r?Object.assign({fill:r},l,{hideInExport:!0}):Object.assign({fillPatternImage:a},l,{opacity:.1,hideInExport:!0})):null},ue=n=>{var{background:i}=n,r=e(n,["background"]);return o.useMemo(()=>!!Y.Util.colorToRGBA(i)||Q(i),[i])?t(ce,Object.assign({fill:i},r)):t(de,Object.assign({url:i},r))},he=r(({selection:e})=>{const{selectionRectStyle:n}=E();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 ge(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const fe=r(({x:e,y:o,width:r,height:l,rotation:s,anchor:a,store:d})=>{const{transformLabelStyle:c}=E();if(void 0===e||void 0===o||void 0===r||void 0===l||void 0===s||void 0===a){return null}const m=C({x:e,y:o,width:r,height:l,rotation:Y.Util.radToDeg(s)}),h=(l/2+70)*Math.cos(s-Math.PI/2),g=(l/2+70)*Math.sin(s-Math.PI/2),f=F({unit:d.unit,dpi:d.dpi,px:r/d.scale,precious:"px"===d.unit?0:1})+" x "+F({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(u,{x:(m.minX+m.maxX)/2+h,y:(m.minY+m.maxY)/2+g,offsetX:14,offsetY:14,visible:"rotater"===a,children:[t(b,{cornerRadius:5,fill:c.fill}),t(y,{align:"center",verticalAlign:"middle",fill:c.textFill,padding:8,text:ge(s).toString()+"°"})]}),n(u,{x:(m.minX+m.maxX)/2,y:m.maxY+20,visible:"rotater"!==a,children:[t(b,{cornerRadius:5,fill:c.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),t(y,{align:"center",verticalAlign:"middle",fill:c.textFill,padding:8,text:f})]})]})}),pe=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(S,{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 xe=null;export const registerNextDomDrop=e=>{xe=e};const ve=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),be=atob("I2MwMzkyYg=="),ye=atob("djAuOS4y"),we=e=>t(i,{children:n(u,{fill:be,height:200,children:[t(b,{fill:be}),t(y,Object.assign({},e,{fill:"white",text:ve,height:void 0,padding:10,fontSize:20}))]})}),Ee=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),Xe=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),Ye=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),Se=e=>{const i=1===D(),o=i?Xe:Ee,r=i?230:195;return n(c,Object.assign({},e,{listening:!1,children:[t(f,{fill:be,width:r,height:24,cornerRadius:2}),t(y,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),i&&t(y,{text:Ye,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default r(({store:i,page:r,width:m,height:x,pageControlsEnabled:v,components:S,altCloneEnabled:j,groupSelectionMode:I="drill",viewportSize:P,layout:F="vertical",tooltipSafeArea:$,transformerSafeArea:Q})=>{var de,ce,ge;const ve=E(),be=ve.transformerStyle,Ee=oe,Xe=r.computedBleed,Ye=i.bleedVisible?Xe:d,ke=r.computedWidth+Ye.left+Ye.right,Ae=r.computedHeight+Ye.top+Ye.bottom,Me=(m-ke*i.scale)/2,je=(x-Ae*i.scale)/2,Ie={x:-Xe.left,y:-Xe.top,width:r.computedWidth+Xe.left+Xe.right,height:r.computedHeight+Xe.top+Xe.bottom,background:q(r.background),name:"page-background",preventDefault:!1},Oe={x:-Ye.left,y:-Ye.top,width:ke,height:Ae},Pe=o.useRef(null),Re=o.useRef(null),Te=o.useRef(null),De=o.useRef(null),Be=null==v||v,Ce=o.useRef(!1),[We,Ge]=o.useState(null),[Le,Fe]=o.useState({}),[Ve,ze]=o.useState(0),Ue=useContextMenu({store:i}),Ze=i.selectedElements.find(e=>e.curveEnabled),_e=i.selectedShapes.filter(e=>!e.resizable).length>0,He=i.selectedShapes.filter(e=>!e.draggable).length>0,Je=i.selectedElements.filter(e=>!e.visible).length>0,Ne=le(i),Ke=JSON.stringify(Ne.map(e=>e.id)),$e=1!==i.selectedElements.length||"group"===(null===(de=i.selectedElements[0])||void 0===de?void 0:de.type),Qe=null!==(ce=re(Ne))&&void 0!==ce?ce:0,qe=e=>$e&&Ne.length>0&&Ne.every(t=>Z(t,e,Qe)),et=!$e&&i.selectedElements[0].keepRatio,tt=!$e&&"text"===(null===(ge=i.selectedElements[0])||void 0===ge?void 0:ge.type)&&"fit"===K(i.selectedElements[0]),nt=qe("x"),it=qe("y"),ot=()=>{var t,n,o;const r=Pe.current;if(!r){return[]}const l=r.getStage();if(!l){return[]}const s=le(i),a=s.map(e=>l.findOne("#"+e.id)).filter(e=>!!e),d=1===i.selectedElements.length,c=d&&(null===(t=i.selectedElements[0])||void 0===t?void 0:t.type)||"many",m=se[c],u=Object.assign(Object.assign({},Ee),m||{});m&&("svg"!==c&&"image"!==c&&"gif"!==c||i.selectedElements[0].keepRatio||(u.enabledAnchors=Ee.enabledAnchors),"text"===c&&(N.textVerticalResizeEnabled||tt)&&(u.enabledAnchors=null===(n=se.text.enabledAnchors)||void 0===n?void 0:n.concat(["bottom-center","top-center"])),"text"===c&&d&&i.selectedElements[0].curveEnabled&&(u.enabledAnchors=se.many.enabledAnchors)),(nt||it)&&(u.enabledAnchors=[...u.enabledAnchors||[],...nt?["middle-left","middle-right"]:[],...it?["top-center","bottom-center"]:[]]);const{boundBoxFunc:h}=be,g=e(be,["boundBoxFunc"]);r.setAttrs(Object.assign(Object.assign(Object.assign({},u),g),{name:U})),_e&&r.enabledAnchors([]),He&&r.rotateEnabled(!1);const f=r.nodes();if(f.length===a.length&&a.every((e,t)=>f[t]===e)||r.nodes(a),$e&&a.length){const e=re(s);null===e||_(r.rotation()-e)||(r.rotation(e),r.forceUpdate())}return null===(o=r.getLayer())||void 0===o||o.batchDraw(),a};o.useLayoutEffect(()=>{var e;if(null===(e=Pe.current)||void 0===e?void 0:e.isTransforming()){return}const t=ot();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),A())},[i.selectedShapes,Ke,_e,Je,He,Ze,nt,it,et,tt,Qe,be]);const rt=()=>{var e,t;const n=Pe.current;if(!n){return}if(!n.isTransforming()){return void Fe({})}if(!n.nodes().length||!n.isTransforming()){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();Fe({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})},lt=o.useCallback(()=>{var e;if(null===(e=Pe.current)||void 0===e?void 0:e.isTransforming()){return}const t=Pe.current;if(!t||!t.nodes().length){return void ze(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),m=-Math.cos(t)*(d+n),u=c*Math.cos(a)-m*Math.sin(a),h=c*Math.sin(a)+m*Math.cos(a);return{x:o+u,y:r+h}},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 m of[180,270,90]){if(c(d(m))){return m}}return 0}(n,{top:(null==Q?void 0:Q.top)||0,bottom:x-((null==Q?void 0:Q.bottom)||0),left:(null==Q?void 0:Q.left)||0,right:m-((null==Q?void 0:Q.right)||0)},be.rotateAnchorOffset,t.rotation());ze(i)},[Q,m,x]);o.useEffect(()=>{var e;null===(e=Pe.current)||void 0===e||e.update(),rt(),lt()},[i.scale,lt]),o.useEffect(()=>{lt()},[i.selectedShapes,lt]);const st=l(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),at=o.useRef(!1),dt=e=>{const t=Te.current;if(!t||i.role===X.VIEWER||i.activePage!==r||i.isPlaying||"button"in e.evt&&0!==e.evt.button||1!==t.getPointersPositions().length||!e.target.findAncestor(".elements-container")||!e.target.findAncestor(".element",!0)||!i.selectedShapes.length||i.selectedShapes.some(e=>!e.draggable)){return}const n=t.getPointerPosition();if(!n){return}const o=ot();if(o.some(t=>t===e.target||t.isAncestorOf(e.target))){return}const l=o.find(e=>!!(e.draggable()&&e.isVisible()&&e.isListening())&&(e instanceof Y.Shape?[e]:e instanceof Y.Container?e.find("Shape"):[]).some(e=>e.intersects(n)));if(l){for(const[t,n]of Y.DD._dragElements){"ready"!==n.dragStatus||n.node!==e.target&&!n.node.isAncestorOf(e.target)||Y.DD._dragElements.delete(t)}l._createDragElement(e),"touches"in e.evt&&e.evt.cancelable&&l.preventDefault()&&e.evt.preventDefault()}},ct=s(e=>{var t,n,o,r,l,s,a;if(i.role===X.VIEWER){return}if(at.current=!1,i.activeTool&&"crop"!==i.activeTool.name){return}const d=e.target.findAncestor(".elements-container"),c=e.target.findAncestor("Transformer"),m=e.target.findAncestor(".page-abs-container");if(d||c||m){return}const u=null===(t=e.target.getStage())||void 0===t?void 0:t.getPointerPosition();u&&(u.x-=null!==(o=null===(n=e.target.getStage())||void 0===n?void 0:n.x())&&void 0!==o?o:0,u.y-=null!==(l=null===(r=e.target.getStage())||void 0===r?void 0:r.y())&&void 0!==l?l:0,st.visible=!0,st.x1=u.x,st.y1=u.y,st.x2=u.x,st.y2=u.y,(null!==(a=null===(s=e.target.getStage())||void 0===s?void 0:s.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(st.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:Te,containerRef:De,viewportSize:P}),o.useEffect(()=>{const e=s(e=>{var t,n,i,o,r,l;if(!st.visible){return}null===(t=Te.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=Te.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(o=null===(i=Te.current)||void 0===i?void 0:i.x())&&void 0!==o?o:0,s.y-=null!==(l=null===(r=Te.current)||void 0===r?void 0:r.y())&&void 0!==l?l:0):s={x:st.x2,y:st.y2},st.x2=s.x,st.y2=s.y}),t=s(()=>{if(!st.visible){return}if(!Te.current){return}const e=Te.current.findOne(".selection"),t=e?e.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(t.width&&t.height){const e=[{x:t.x,y:t.y},{x:t.x+t.width,y:t.y},{x:t.x+t.width,y:t.y+t.height},{x:t.x,y:t.y+t.height}],n=[];Te.current.find(".element").forEach(t=>{var o;const r=i.getElementById(t.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=t.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const d=W(r.a).map(e=>a.point(e));L(e,d)&&n.push(r.top.id)});const o=[...new Set(n)];at.current=!0,i.selectElements(o)}st.visible=!1});return window.addEventListener("mousemove",e),window.addEventListener("touchmove",e),window.addEventListener("mouseup",t,{capture:!0}),window.addEventListener("touchend",t,{capture:!0}),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("touchmove",e),window.removeEventListener("mouseup",t),window.removeEventListener("touchend",t)}},[]);const mt=o.useCallback(e=>{if(!i.selectedElements[0]){return}if(e&&i.selectedElementsIds.includes(e)){return}const t=G(i.selectedShapes),n=e?i.getElementById(e):{x:0,y:0,width:r.computedWidth,height:r.computedHeight,rotation:0},o=C(n),l=[];t.minX>o.maxX&&l.push({distance:t.minX-o.maxX,box1:t,box2:o,points:[{x:t.minX,y:t.minY+t.height/2},{x:o.maxX,y:t.minY+t.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),t.maxX<o.minX&&l.push({distance:o.minX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:t.minY+t.height/2},{x:o.minX,y:t.minY+t.height/2},{x:o.minX,y:o.minY+o.height/2}]}),t.minY>o.maxY&&l.push({box1:t,box2:o,distance:t.minY-o.maxY,points:[{x:t.minX+t.width/2,y:t.minY},{x:t.minX+t.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]}),t.maxY<o.minY&&l.push({box1:t,box2:o,distance:o.minY-t.maxY,points:[{x:t.minX+t.width/2,y:t.maxY},{x:t.minX+t.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]});const s=t.minX>=o.minX&&t.maxX<=o.maxX&&t.minY>=o.minY&&t.maxY<=o.maxY;if(s&&(l.push({distance:t.minX-o.minX,box1:t,box2:o,points:[{x:t.minX,y:t.minY+t.height/2},{x:o.minX,y:t.minY+t.height/2},{x:o.minX,y:o.minY+o.height/2}]}),l.push({distance:o.maxX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:t.minY+t.height/2},{x:o.maxX,y:t.minY+t.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),l.push({box1:t,box2:o,distance:t.minY-o.minY,points:[{x:t.minX+t.width/2,y:t.minY},{x:t.minX+t.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]}),l.push({box1:t,box2:o,distance:o.maxY-t.maxY,points:[{x:t.minX+t.width/2,y:t.maxY},{x:t.minX+t.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]})),0===l.length&&!s){const e=t.minX<o.maxX&&t.maxX>o.minX,n=t.minY<o.maxY&&t.maxY>o.minY;if(e&&n){const e=(Math.max(t.minY,o.minY)+Math.min(t.maxY,o.maxY))/2,n=(Math.max(t.minX,o.minX)+Math.min(t.maxX,o.maxX))/2;t.minX<o.minX?l.push({distance:o.minX-t.minX,box1:t,box2:o,points:[{x:t.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}):l.push({distance:t.minX-o.minX,box1:t,box2:o,points:[{x:t.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}),t.maxX>o.maxX?l.push({distance:t.maxX-o.maxX,box1:t,box2:o,points:[{x:o.maxX,y:e},{x:t.maxX,y:e},{x:o.maxX,y:e}]}):l.push({distance:o.maxX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:e},{x:o.maxX,y:e},{x:o.maxX,y:e}]}),t.minY<o.minY?l.push({box1:t,box2:o,distance:o.minY-t.minY,points:[{x:n,y:t.minY},{x:n,y:o.minY},{x:n,y:o.minY}]}):l.push({box1:t,box2:o,distance:t.minY-o.minY,points:[{x:n,y:t.minY},{x:n,y:o.minY},{x:n,y:o.minY}]}),t.maxY>o.maxY?l.push({box1:t,box2:o,distance:t.maxY-o.maxY,points:[{x:n,y:o.maxY},{x:n,y:t.maxY},{x:n,y:o.maxY}]}):l.push({box1:t,box2:o,distance:o.maxY-t.maxY,points:[{x:n,y:t.maxY},{x:n,y:o.maxY},{x:n,y:o.maxY}]})}}JSON.stringify(We)!==JSON.stringify(l)&&Ge(l)},[i,r]);o.useEffect(()=>{i.selectedElements[0]&&i.distanceGuidesVisible?mt():We&&Ge(null)},[i.distanceGuidesVisible,mt,i.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|")]);const ut=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=De.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{ut.current=!0,clearTimeout(t),t=setTimeout(()=>{ut.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const e=Te.current;if(!e){return}const t=e.container(),n=new Map,o=e=>{var t,i;n.has(e.target)||[e.target,...null!==(i=null===(t=Pe.current)||void 0===t?void 0:t.nodes())&&void 0!==i?i:[]].forEach(e=>{n.has(e)||n.set(e,{x:e.x(),y:e.y()})})},r=e=>{e.target===Pe.current?n.clear():n.delete(e.target)};e.on("dragstart",o),e.on("dragend",r);const l=e=>{e.touches.length>=2&&a(()=>{var e,t;const o=new Map(n);(null===(e=Pe.current)||void 0===e?void 0:e.isDragging())&&Pe.current.stopDrag(),null===(t=Pe.current)||void 0===t||t.nodes().forEach(e=>{var t;const n=o.get(e);e.isDragging()&&e.stopDrag(),n&&(e.setAttrs(n),null===(t=i.getElementById(e.id()))||void 0===t||t.set({x:n.x,y:n.y}))})})};return t.addEventListener("touchstart",l,{passive:!0}),t.addEventListener("touchmove",l,{passive:!0}),()=>{e.off("dragstart",o),e.off("dragend",r),t.removeEventListener("touchstart",l),t.removeEventListener("touchmove",l)}},[i]);const ht=e=>{if(i.role===X.VIEWER){return}if(i.activePage!==r&&r.select(),ut.current){return}if(at.current){return}const t=e.evt.shiftKey,n=e.evt.ctrlKey||e.evt.metaKey,o=t||n,l=e.target.findAncestor(".elements-container"),s=e.target.findAncestor(".page-abs-container"),a=e.target.findAncestor("Transformer"),d=e.target.findAncestor(".page-container",!0),c=!(o||l||a||s||d||st.visible);if(i.activeTool&&"crop"!==i.activeTool.name&&!l&&!a){return}if(c){return i.selectElements([]),void i.selectPages([])}if(n&&!t){const e=Te.current,t=null==e?void 0:e.getPointerPosition();if(!e||!t){return}const n=e.getAllIntersections(t),o=new Set,r=[];for(const s of[...n].reverse()){const e=s.findAncestor(".element",!0);if(!e){continue}const t=i.getElementById(e.id()),n=null==t?void 0:t.top;n&&(o.has(n.id)||(o.add(n.id),r.push(n.id)))}if(0===r.length){return}let l;if(1===i.selectedElementsIds.length){const e=i.selectedElementsIds[0],t=r.indexOf(e);l=-1===t?r[0]:r[(t+1)%r.length]}else{l=r[0]}return i.selectElements([l]),void i.selectPages([])}const m=e.target.findAncestor(".element-container",!0),u=(null==m?void 0:m.findOne(".element"))||e.target.findAncestor(".element",!0),h=i.getElementById(null==u?void 0:u.id());h&&"pendingEdit"in h&&h._setImageEditError(null);const g=null==h?void 0:h.top;let f;if("group"===I){f=null==g?void 0:g.id}else if(g){const e=i.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===g.id});f=e?null==h?void 0:h.id:null==g?void 0:g.id}else{f=void 0}const p=null==h?void 0:h.id;if(p&&i.selectedElementsIds.indexOf(p)>=0&&!t){return}const x=!!f&&i.selectedElementsIds.indexOf(f)>=0,v=e.target.findAncestor(".page-container",!0);if(f&&t&&!x){const e=ne(i.selectedElementsIds.concat([f]),e=>te(i,e));i.selectElements(e),i.selectPages([])}else{f&&t&&x?i.selectElements(i.selectedElementsIds.filter(e=>e!==f)):!f||t||x?f&&!t&&x||(v?(t||i.selectElements([]),i.selectPages([r.id])):i.selectPages([])):(i.selectElements([f]),i.selectPages([]))}};M(Pe,i);const gt=i.activePage===r,ft=i._selectedPagesIds.includes(r.id),pt=null==S?void 0:S.PageControls,xt=null==S?void 0:S.Tooltip,vt=null==S?void 0:S.ContextMenu,bt=1/i.scale,yt=0/i.scale;return n("div",{ref:De,onDragOver:e=>e.preventDefault(),onDrop:e=>{if(e.preventDefault(),i.role===X.VIEWER){return}if(!Te.current){return}Te.current.setPointersPositions(e);const t=Te.current.findOne(".elements-container"),n=null==t?void 0:t.getRelativePointerPosition(),o=Te.current.getPointerPosition(),l=Te.current.getAllIntersections(o).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(l.reverse())].map(e=>i.getElementById(e.id())).filter(e=>!!e),a=s[0];xe&&n&&(xe(n,a,{elements:s,page:r}),xe=null)},style:{position:"relative",width:m+"px",height:x+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+(gt?" active-page":""),children:[t(ie,{ref:Te,eventBatchFunc:a,width:Math.min(m,P.width+200),height:Math.min(P.height+200,x),onClick:ht,onTap:ht,onContextMenu:e=>{if(e.evt.preventDefault(),i.role===X.VIEWER){return}const t=e.target.findAncestor(".element",!0),n=i.getElementById(null==t?void 0:t.id()),o=null==n?void 0:n.top,r=null==o?void 0:o.id;if(!r){return i.selectElements([]),void Ue.close()}i.selectedElementsIds.indexOf(r)>=0||i.selectElements([r]),Ue.open({x:e.evt.clientX,y:e.evt.clientY})},onMouseDown:e=>{dt(e),ct(e)},onTouchStart:dt,onMouseMove:e=>{if(!i.distanceGuidesVisible&&!e.evt.altKey){return void(We&&Ge(null))}const t=e.target.findAncestor(".element",!0),n=null==t?void 0:t.id();mt(n)},onDragStart:e=>{var t;const n=e.target.getStage();if(n&&n.getPointersPositions().length>=2){return void e.target.stopDrag()}const o=e.target.findAncestor(".element",!0);if(o){const n=i.getElementById(null==o?void 0:o.id()),r=null==n?void 0:n.top,l=null==r?void 0:r.id;if(!(n&&i.selectedElementsIds.indexOf(n.id)>=0||l&&i.selectedElementsIds.indexOf(l)>=0)&&l){i.selectElements([l]);const n=ot();e.target.stopDrag(),n.forEach(t=>{t.isDragging()||t.startDrag(e)}),null===(t=Pe.current)||void 0===t||t.startDrag(e),A()}}We&&Ge(null)},onDragMove:e=>{const t=Pe.current;t&&e.target===t&&t.nodes().forEach(e=>{const t=i.getElementById(e.id());!t||t.x===e.x()&&t.y===e.y()||t.set({x:e.x(),y:e.y()})})},pageId:r.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:n(h,{children:[t(ae,{width:m,height:x,fill:ve.workspaceBackgroundColor}),t(c,{x:Me,y:je,scaleX:i.scale,scaleY:i.scale,name:"page-container",children:n(c,{name:"page-container-2",children:[t(c,{x:Ye.left,y:Ye.top,children:t(me,Object.assign({},Ie,Oe,{transparencyBackgroundColor:ve.transparencyBackgroundColor}))}),n(c,{isolated:r.hasBlendMode,children:[t(c,{name:"page-background-group",x:Ye.left,y:Ye.top,clip:Oe,children:t(ue,Object.assign({},Ie))}),n(c,{x:Ye.left,y:Ye.top,name:"elements-container",listening:!i.isPlaying&&i.role!==X.VIEWER,children:[t(f,{name:"elements-area",width:r.computedWidth,height:r.computedHeight,listening:!1}),t(pe,{elements:r.children,store:i})]})]}),t(p,{name:"bleed",fill:ve.bleedColor,listening:!1,visible:i.bleedVisible&&(Ye.top>0||Ye.right>0||Ye.bottom>0||Ye.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:i,bottom:o,left:l}=Xe,s=r.computedWidth,a=r.computedHeight,d=e._context;d.beginPath(),d.rect(0,0,l+s+i,n+a+o),d.rect(l,n,s,a),d.fillStyle=t.fill(),d.fill("evenodd")}}),T()===ye&&t(we,{name:"hit-detection",x:-bt/2-yt,y:-bt/2-yt,width:ke+bt+2*yt,height:Ae+bt+2*yt})]})}),t(g,{name:"workspace-background",points:[0,0,m,0,m,x,0,x,0,0,Me,je,Me,x-je,m-Me,x-je,m-Me,je,Me,je],listening:!1,closed:!0,fill:ve.workspaceBackgroundColor}),t(c,{x:Me,y:je,scaleX:i.scale,scaleY:i.scale,children:t(f,{name:"page-highlight",hideInExport:!0,x:-bt/2-yt,y:-bt/2-yt,width:ke+bt+2*yt,height:Ae+bt+2*yt,stroke:ft?ve.activePageBorderColor:ve.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),n(c,{x:Me+Ye.left*i.scale,y:je+Ye.top*i.scale,scaleX:i.scale,scaleY:i.scale,name:"page-abs-container",children:[t(w,{ref:Pe,name:U,rotateAnchorAngle:Ve,onDragStart:e=>{var t;const n=e.target.getStage();n&&n.getPointersPositions().length>=2?e.target.stopDrag():((null===(t=e.evt)||void 0===t?void 0:t.altKey)&&j&&R(i.selectedElements,r,i),Ce.current||(Ce.current=!0,i.history.startTransaction()))},onDragEnd:()=>{Ce.current&&(i.history.endTransaction(),Ce.current=!1),lt()},onTransformStart:()=>{i.history.startTransaction(),Re.current=new Map(Ne.map(e=>[e.id,{width:e.width,height:e.height}])),Ge(null)},boundBoxFunc:(e,t)=>{var n,o,r,l;const s=null!==(o=null===(n=be.boundBoxFunc)||void 0===n?void 0:n.call(be,e,t))&&void 0!==o?o:t,a=Math.abs(s.width)<1||Math.abs(s.height)<1,d=Math.abs(e.width)<1||Math.abs(e.height)<1;if(a&&!d){return e}const c=i.selectedElements;if(1===c.length&&"table"===(null===(r=c[0])||void 0===r?void 0:r.type)){const t=c[0],n=Math.abs(e.width),i=Math.abs(e.height);if(n>0&&i>0){const o=Math.abs(s.width)/n,r=Math.abs(s.height)/i,l=t.width*o,a=t.height*r,d=Object.assign({},s);return l<t.minWidth&&(d.width=e.width),a<t.minHeight&&(d.height=e.height),J(d,s,e)}}return $e&&Ne.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=!z(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,m=H(t,e,d,r);return r>=m&&l*c<m}),m=Object.assign({},t),u=t.width*e.width<0,h=t.height*e.height<0,g=u||c("x"),f=h||c("y"),p=f||!d&&g;return(g||!d&&f)&&(m.width=e.width),p&&(m.height=e.height),J(m,t,e)})(e,s,(null===(l=Pe.current)||void 0===l?void 0:l.getActiveAnchor())||"",Ne,Re.current):s},onTransform:e=>{const t=Pe.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{rt()},0)},onTransformEnd:e=>{Fe({}),i.history.endTransaction(),Re.current=null,lt(),ot()},visible:!i.isPlaying}),We&&We.map(({points:e,distance:o,box1:r,box2:l},s)=>n(c,{name:"distances-container",hideInExport:!0,listening:!1,children:[t(f,Object.assign({},r,{stroke:ve.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(f,Object.assign({},l,{stroke:ve.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(g,{points:[e[0].x,e[0].y,e[1].x,e[1].y],stroke:ve.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),t(g,{points:[e[1].x,e[1].y,e[2].x,e[2].y],stroke:ve.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(u,{x:(e[0].x+e[1].x)/2,y:(e[0].y+e[1].y)/2,offsetY:-10,scaleX:1/i.scale,scaleY:1/i.scale,children:[t(b,{cornerRadius:5,fill:ve.distanceGuideStyle.labelFill,pointerDirection:"down"}),t(y,{align:"center",verticalAlign:"middle",fill:ve.distanceGuideStyle.labelTextFill,padding:5,text:V({unit:i.unit,dpi:i.dpi,px:o})})]})]},s)),r._rendering&&n(c,{children:[t(f,{width:ke,height:Ae,fill:"rgba(255,255,255,0.9)"}),t(y,{text:"Rendering...",fontSize:60,width:ke,height:Ae,align:"center",verticalAlign:"middle"})]}),xt&&t(xt,{components:S,store:i,page:r,stageRef:Te,tooltipSafeArea:$}),vt&&t(ee,{children:t(vt,Object.assign({components:S,store:i},Ue.props))}),t(O,{store:i,pageId:r.id})]}),t(k,{store:i,page:r,width:m,height:x,scale:i.scale,xPadding:Me,yPadding:je,bleedLeft:Ye.left,bleedTop:Ye.top}),t(fe,Object.assign({},Le,{store:i})),t(he,{selection:st}),D()>0&&n(c,{hideInExport:!0,children:[t(Se,{name:"cache-bounds",x:m-(1===D()?250:210),y:x-(1===D()?95:45)}),t(Se,{name:"cache-bounds-t",x:10,y:10})]}),B()&&t(y,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:m-170,y:x-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"})]})}),Be&&pt&&t(pt,{store:i,page:r,xPadding:Me,yPadding:je,layout:F})]})});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var t=this&&this.__rest||function(t,e){var o={};for(var r in t){Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(o[r]=t[r])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(r=Object.getOwnPropertySymbols(t);n<r.length;n++){e.indexOf(r[n])<0&&Object.prototype.propertyIsEnumerable.call(t,r[n])&&(o[r[n]]=t[r[n]])}}return o};import{jsx as e,Fragment as o,jsxs as r}from"react/jsx-runtime";import{elementOpacity as n}from"./paint-opacity.js";import i from"react";import{observer as a}from"mobx-react-lite";import{Group as l,Path as s,Rect as d,Shape as c}from"react-konva";import{nodeAutorun as h}from"../utils/mobx.js";import{isAlive as u}from"mobx-state-tree";import{Html as f}from"react-konva-utils";import{layout as g,drawLayout as m}from"render-tag";import{layoutTextOnPath as p,drawTextOnPathLayout as x}from"render-tag/path";import{activeAnchorOf as b,scaledFontSizeAttrs as w,roundedFontSizeAttrs as y,roundedTextDecorationAttrs as v,roundedGeometryPx as O,roundedPosition as E,textMinResizeWidth as S}from"../utils/px.js";import{flags as z}from"../utils/flags.js";import{applyFilter as j}from"./apply-filters.js";import{getLineHeight as k,usePrevious as M}from"./text-element.js";import{useFontSpecsLoader as R}from"./text-element/font-specs-loader.js";import{getHtml as Y,getCurvePath as X,getCurveTextHeight as C,letterSpacingOverhang as F}from"@polotno/core/text-html";import{useColor as B,withDisplayTextPaints as P}from"./use-color.js";import{isGradient as I}from"@polotno/core/gradient";import{toDisplayColor as T}from"@polotno/core/color";import{detectSize as W}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as D,trimTrailingLetterSpace as N}from"@polotno/core/background-shape";import{useFadeIn as J}from"./use-fadein.js";import{incrementLoader as $}from"../utils/loader.js";import{removeTags as H}from"@polotno/core/text";import{shadowCastByBackground as _}from"@polotno/core/filters";import{applyOpacityToColor as A}from"@polotno/core/svg";import{isTouchDevice as L}from"../utils/screen.js";import{getLimitedFontSize as V}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as q,extractFontSpecsFromHtml as G,scaleRichTextFontSizesInHtml as K}from"../utils/rich-text-html.js";import{RichEditor as Q}from"./rich-editor.js";import{findFitFontSize as U}from"./text-element/fit-font-size.js";import{visiblePlaceholder as Z}from"./text-element/placeholder.js";import{wrapHtmlForRenderTag as tt}from"@polotno/core/reset-style";import{xmlEscape as et}from"@polotno/core/xml";import{hasMeasuredHeight as ot,resolveTextBoxHeight as rt}from"@polotno/core/text-types";export{tt as wrapHtmlForRenderTag};function nt(t,e){return t!==e&&(!z.textVerticalResizeEnabled||t<e)}export const RenderTagElement=a(({element:a,store:it})=>{var at,lt,st,dt,ct,ht,ut,ft,gt;const mt=i.useRef(null),pt=i.useRef(null),[xt,bt]=i.useState(!1),wt=i.useRef(a.height),yt=i.useRef(null),vt=a.isSelected,Ot=M(vt),{textVerticalResizeEnabled:Et}=z,St=z.autoDeleteEmptyText,zt=M(a.fontFamily),jt=i.useMemo(()=>q(a.text,{fontSize:a.fontSize,fontFamily:a.fontFamily}),[a.text,a.fontSize,a.fontFamily]),kt=i.useMemo(()=>G(jt,{fontFamily:a.fontFamily,fontWeight:a.fontWeight,fontStyle:a.fontStyle}),[jt,a.fontFamily,a.fontWeight,a.fontStyle]),[Mt]=R(it,kt,a.text||a.placeholder),Rt=a._editModeEnabled;J(mt);const Yt=Mt?a.fontFamily:zt&&zt!==a.fontFamily?zt:"Arial",Xt=B(a).fill,Ct=I(a.fill),Ft=I(a.stroke)&&!!a.strokeWidth,Bt=!!a.strokeWidth&&(Ft||Ct),Pt="ellipsis"===z.textOverflow&&!a.curveEnabled&&ot(a)&&!Bt,It=Z(a.placeholder),Tt=!!It&&!H(a.text),Wt=Tt?et(It):jt,Dt=Tt?.6:n(a),Nt=Y(P(a),{fontFamily:Yt,color:Xt,forEditor:!Pt,textOverflow:z.textOverflow,text:Wt,textIsNormalized:!0}),Jt=a.width||1,$t=F(a.letterSpacing,a.fontSize),Ht=a.curveEnabled,_t=xt?yt.current:null,At=null!==(at=null==_t?void 0:_t.width)&&void 0!==at?at:Jt,Lt=_t?a.width/_t.width:1,Vt=null!==(lt=null==_t?void 0:_t.overhang)&&void 0!==lt?lt:$t,qt=function(t,e,o,r){return i.useMemo(()=>{if(r){return null}try{return g({html:tt(t),width:Math.max(1,e),accuracy:"performance"})}catch(o){return console.error("render-tag layout failed:",o),null}},[t,e,o,r])}(null!==(st=null==_t?void 0:_t.html)&&void 0!==st?st:Nt,At,Mt,Ht),Gt=function(t,e,o){return i.useMemo(()=>{if(!e.curveEnabled){return null}const o=e.width||1,r=C(e),n=X(o,r,e.curvePower,e.fontSize);try{return p({html:tt(t),path:n,align:"center",textBaseline:"middle"})}catch(i){return console.error("render-tag/path layoutTextOnPath failed:",i),null}},[o,e.curveEnabled,e.curvePower,e.fontSize,e.width,e.height,t])}(Nt,a,Mt),Kt=k({fontLoaded:Mt,fontFamily:a.fontFamily,fontSize:a.fontSize,lineHeight:a.lineHeight}),Qt=Ht?0:Math.max(null!==(dt=null==qt?void 0:qt.height)&&void 0!==dt?dt:0,a.fontSize*Kt),Ut=Gt?Gt.bounds:null,Zt=Ut?(a.width-Ut.width)/2-Ut.x:0,te=i.useMemo(()=>{var t;if(Gt&&Ut){const t=Gt.paintBounds;return Object.assign(Object.assign({},t),{x:t.x+Zt,y:t.y-Ut.y})}return null!==(t=null==qt?void 0:qt.paintBounds)&&void 0!==t?t:null},[qt,Gt,Ut,Zt]),ee=i.useMemo(()=>qt?qt.lines.map(t=>N({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height},Vt)):[],[qt,Vt]),oe=i.useRef([]),re=i.useRef(null),ne=i.useRef(!0),[,ie]=i.useReducer(t=>t+1,0);i.useEffect(()=>{const t=oe.current;t.length>0&&(oe.current=[],t.forEach(t=>t()))});const ae=i.useCallback((t,e,o,r)=>{const n=$(`text ${a.id}`),i=()=>{it.history.ignore(t,e,o).then(()=>{ne.current?(oe.current.push(n),ie()):n()})};r?queueMicrotask(i):i()},[a.id,it]),le=i.useCallback(t=>{const e=null!==re.current;re.current=t,e||ae(()=>{const t=re.current;re.current=null,u(a)&&nt(a.height,t)&&a.set({height:t})},!1,!0)},[a,ae]);i.useEffect(()=>{if(!Mt){return}if(!qt){return}if(!ot(a)){return void ae(()=>{u(a)&&(ot(a)||a.set({height:Qt}))},void 0,void 0,!0)}if(it.isPlaying){return}const{textOverflow:t}=z,e=nt(a.height,Qt),o=()=>le(Qt);if("change-font-size"!==t||xt||Rt||a.curveEnabled){"resize"===t&&e&&!xt&&o()}else{const t=U(Tt?{toJSON:()=>Object.assign(Object.assign({},a.toJSON()),{text:Wt})}:a,{fontFamily:Yt,color:Xt,forEditor:!0},t=>{try{return g({html:tt(t),width:Jt,accuracy:"performance"}).height}catch(at){return W(t).height}});t.fontSize!==a.fontSize?ae(()=>{if(!u(a)){return}const e=w(a,t.fontSize);void 0!==t.text&&t.text!==a.text&&(e.text=t.text),a.set(e)},!1,!0):e&&o()}}),i.useEffect(()=>()=>{ne.current=!1;const t=oe.current;t.length>0&&(oe.current=[],t.forEach(t=>t()))},[]),i.useLayoutEffect(()=>{if(!Mt||!a.curveEnabled||xt){return}if(!Ut||!Ut.width){return}const t=t=>t&&Et?Math.max(t,Ut.height):Ut.height;Math.abs(Ut.width-a.width)<1&&Math.abs(t(a.height)-a.height)<1||ae(()=>{if(!u(a)){return}const e=Ut.width-a.width,o=t(a.height),r=o-a.height;if(Math.abs(e)<1&&Math.abs(r)<1){return}const n=a.a.rotation*Math.PI/180,i=-e/2*Math.cos(n),l=-e/2*Math.sin(n);a.set({width:Ut.width,height:o,x:a.a.x+i,y:a.a.y+l})},!1,!0,!0)},[Mt,a.curveEnabled,Ut,xt,Et,ae]),i.useEffect(()=>{Ot&&!vt&&St&&""===H(a.text)&&a.removable&&!Z(a.placeholder)&&it.deleteElements([a.id])},[St,a.placeholder,a.removable,a.text,vt,Ot]),i.useEffect(()=>h(a,()=>{const t=mt.current;t&&j(t,a)}),[qt,Gt,a.a.width,a.a.height,a.shadowColor,a.shadowOffsetX,a.shadowOffsetY,a.shadowOpacity]);const se=rt(a,Qt),de=Ht||!Mt||xt||it.isPlaying||"resize"!==z.textOverflow?a.a.height:Et?Math.max(a.a.height,Qt):Qt;let ce=0;Ht||"middle"!==a.verticalAlign?Ht||"bottom"!==a.verticalAlign||(ce=se-Qt*Lt):ce=(se-Qt*Lt)/2;const he=null!==(ct=null==_t?void 0:_t.backgroundPadding)&&void 0!==ct?ct:a.backgroundPadding*(a.fontSize*Kt*.5),ue=null!==(ht=null==_t?void 0:_t.backgroundCornerRadius)&&void 0!==ht?ht:a.backgroundCornerRadius*(a.fontSize*Kt*.5),fe=a.backgroundEnabled||a.borderSize>0,ge=i.useMemo(()=>!fe||a.curveEnabled||a.legacyBackground||0===ee.length?"":D({rects:ee,padding:he,cornerRadius:ue}),[fe,a.curveEnabled,a.legacyBackground,ee,he,ue]),me=L(),pe=i.useRef(null),xe=i.useRef(a.width),be=i.useRef(null),we=i.useRef(null),ye=i.useRef(""),ve=i.useRef(0),Oe=i.useRef(0),Ee=a.width>0?a.a.width/a.width:1,Se=Ee*Lt,ze=null!=_t?_t:a,je={shadowEnabled:a.shadowEnabled&&fe,shadowBlur:ze.shadowBlur,shadowOffsetX:ze.shadowOffsetX,shadowOffsetY:ze.shadowOffsetY,shadowColor:T(a.shadowColor),shadowOpacity:a.shadowOpacity};return r(o,{children:[a.curveEnabled||a.legacyBackground?e(d,Object.assign({x:a.a.x,y:a.a.y,offsetX:he/Ee,offsetY:he/Ee,rotation:a.a.rotation,scaleX:Se,scaleY:Se,hideInExport:!a.showInExport,listening:!1,visible:fe,opacity:Dt,fillEnabled:a.backgroundEnabled,fill:A(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:T(a.borderColor),strokeWidth:null!==(ut=null==_t?void 0:_t.borderSize)&&void 0!==ut?ut:a.borderSize},je,{width:At+2*he,height:(null!==(ft=null==_t?void 0:_t.boxHeight)&&void 0!==ft?ft:se)+2*he,cornerRadius:ue})):e(s,Object.assign({name:"rendertag-background",x:a.a.x,y:a.a.y,offsetY:-ce/Se,rotation:a.a.rotation,scaleX:Se,scaleY:Se,hideInExport:!a.showInExport,listening:!1,visible:fe&&ge.length>0,opacity:Dt,fillEnabled:a.backgroundEnabled,fill:A(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:T(a.borderColor),strokeWidth:null!==(gt=null==_t?void 0:_t.borderSize)&&void 0!==gt?gt:a.borderSize,lineJoin:ue>0?"round":"miter"},je,{data:ge})),e(c,{ref:mt,selfRectFunc:te?()=>te:void 0,name:"rendertag-text",x:a.a.x,y:a.a.y,rotation:a.a.rotation,scaleX:Se,scaleY:Se,offsetY:Se>0?-ce/Se:0,width:At,height:Math.max(a.height,Qt),listening:!1,visible:!Rt&&!!(Ht?Gt:qt),editModeEnabled:Rt,opacity:Dt,shadowEnabled:a.shadowEnabled&&!_(a),shadowBlur:ze.shadowBlur,shadowOffsetX:ze.shadowOffsetX,shadowOffsetY:ze.shadowOffsetY,shadowColor:T(a.shadowColor),shadowOpacity:a.shadowOpacity,hideInExport:!a.showInExport,sceneFunc:(t,e)=>{const o=t._context;if(o){try{if(Ht){if(!Gt||!Ut){return}o.save(),o.translate(Zt,-Ut.y),x({layout:Gt,ctx:o}),o.restore()}else{if(!qt){return}m({layout:qt,width:At,ctx:o,pixelRatio:1})}}catch(r){console.error("render-tag draw failed:",r)}}}}),e(d,{ref:pt,name:"element",x:a.a.x,y:a.a.y,listening:a.selectable,rotation:a.a.rotation,width:a.a.width,height:de,visible:!Rt,draggable:me?a.draggable&&vt:a.draggable,preventDefault:!me||vt,opacity:0,hideInExport:!0,id:a.id,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(E(t.target))},onDblClick:t=>{a.contentEditable&&(we.current={x:t.evt.clientX,y:t.evt.clientY},a.toggleEditMode(!0))},onDblTap:t=>{var e;if(a.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];we.current=o?{x:o.clientX,y:o.clientY}:null,a.toggleEditMode(!0)}},onTransformStart:t=>{wt.current=a.height,xe.current=a.width,be.current=null,pe.current=t.target.position(),ye.current=b(t.target),ve.current=a.a.rotation,Oe.current=a.height,yt.current=!a.curveEnabled&&["top-left","top-right","bottom-left","bottom-right"].includes(ye.current)?{html:Nt,width:Jt,boxHeight:se,backgroundPadding:he,backgroundCornerRadius:ue,borderSize:a.borderSize,overhang:$t,shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY}:null,bt(!0)},onTransform:e=>{const o=e.target,r=ye.current,n="middle-left"===r||"middle-right"===r,i="top-center"===r||"bottom-center"===r,l=o.scaleX();if(n){const e=o.scaleX(),r=o.width()*e;let n=Math.max(r,S(xe.current,a.fontSize));const i=a.toJSON();let l=0;if(a.curveEnabled){l=C(Object.assign(Object.assign({},i),{width:n}))}else{const{x:e,y:o,width:r,height:a}=i,s=t(i,["x","y","width","height"]),d=`${JSON.stringify(s)}|${Yt}|${Mt}|${Xt}`;if(be.current&&be.current.signature===d&&n<=be.current.width){n=be.current.width,l=be.current.height}else{const t=t=>{const e=Y(Object.assign(Object.assign({},i),{width:t}),{fontFamily:Yt,color:Xt,forEditor:!0,text:Wt,textIsNormalized:!0});try{return g({html:tt(e),width:t,accuracy:"performance"})}catch(at){return{height:W(e).height,lines:[]}}};let e=t(n);const o=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width-$t));o>n+.5&&o<=xe.current+.5&&(n=Math.min(Math.ceil(o),xe.current),e=t(n),be.current={signature:d,width:n,height:e.height}),l=e.height}}n>r&&pe.current&&o.position(pe.current),o.width(n),o.scaleX(1),o.scaleY(1);let s=z.textVerticalResizeEnabled?Math.max(l,wt.current):l;"ellipsis"===z.textOverflow&&(s=wt.current),o.height(s),a.set({width:o.width(),height:s,x:o.x(),y:o.y()})}else if(i){const t="resize"===z.textOverflow?Ht?(null==Ut?void 0:Ut.height)||a.fontSize:Qt:Kt*a.fontSize,e=o.height()*o.scaleY();if(e<t){if(o.height(t),pe.current){if("top-center"===ye.current){const e=ve.current*Math.PI/180,r=Oe.current-t;o.x(pe.current.x-r*Math.sin(e)),o.y(pe.current.y+r*Math.cos(e))}else{o.position(pe.current)}}}else{o.height(e)}o.scaleY(1),o.scaleX(1),Oe.current=Math.max(t,e),a.set({x:o.x(),y:o.y(),height:o.height(),rotation:o.rotation()})}else{o.scaleX(1),o.scaleY(1),a.set({fontSize:a.fontSize*l,letterSpacing:a.letterSpacing,width:o.width()*l,x:o.x(),y:o.y(),rotation:o.rotation(),height:o.height()*l,shadowBlur:a.shadowBlur*l,shadowOffsetX:a.shadowOffsetX*l,shadowOffsetY:a.shadowOffsetY*l,strokeWidth:a.strokeWidth*l,borderSize:a.borderSize*l,text:K(a.text,l)})}pe.current=e.target.position()},onTransformEnd:t=>{yt.current=null,bt(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},y(a,a.fontSize*e)),{width:O(t.target.width()*e),x:O(t.target.x()),y:O(t.target.y()),rotation:O(t.target.rotation()),text:K(a.text,e)}))}}),Rt&&e(l,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-ce,children:e(f,{divProps:{style:{zIndex:20}},children:e(Q,{element:a,onChange:t=>{const e=V({oldText:H(a.text),newText:H(t),element:a});a.set(Object.assign({text:t},w(a,e)))},onBlur:()=>{u(a)&&(a.set(v(a)),a.toggleEditMode(!1))},clickCoords:we.current})})})]})});
|
|
1
|
+
var t=this&&this.__rest||function(t,e){var o={};for(var r in t){Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(o[r]=t[r])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(r=Object.getOwnPropertySymbols(t);n<r.length;n++){e.indexOf(r[n])<0&&Object.prototype.propertyIsEnumerable.call(t,r[n])&&(o[r[n]]=t[r[n]])}}return o};import{jsx as e,Fragment as o,jsxs as r}from"react/jsx-runtime";import{elementOpacity as n}from"./paint-opacity.js";import i from"react";import{observer as a}from"mobx-react-lite";import{Group as l,Path as s,Rect as c,Shape as d}from"react-konva";import{nodeAutorun as u}from"../utils/mobx.js";import{isAlive as h}from"mobx-state-tree";import{Html as f}from"react-konva-utils";import{drawLayout as g}from"render-tag";import{drawTextOnPathLayout as m}from"render-tag/path";import{activeAnchorOf as b,isCornerAnchor as p,scaledFontSizeAttrs as x,scaledFontSizeBounds as y,roundedFontSizeAttrs as v,roundedTextDecorationAttrs as w,scaledStyleAttrs as z,roundedGeometryPx as E,roundedPosition as O,textMinResizeWidth as j}from"../utils/px.js";import{flags as S}from"../utils/flags.js";import{editorTextSizing as M,textOverflowOf as k,textResizeFloorFontSize as R}from"../utils/text-sizing.js";import{curvedBoxWidth as F,layoutCurvedTextSizing as Y,layoutTextSizing as X,renderTagLayoutAt as C}from"@polotno/core/text-sizing";import{curveLayoutAt as I}from"@polotno/core/curve-glyphs";import{applyFilter as P}from"./apply-filters.js";import{getLineHeight as B,usePrevious as T}from"./text-element.js";import{useFontSpecsLoader as A}from"./text-element/font-specs-loader.js";import{getHtml as H,getCurveTextHeight as N,letterSpacingOverhang as D}from"@polotno/core/text-html";import{useColor as W,withDisplayTextPaints as $}from"./use-color.js";import{toDisplayColor as _}from"@polotno/core/color";import{detectSize as J}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as L,trimTrailingLetterSpace as V}from"@polotno/core/background-shape";import{useFadeIn as q}from"./use-fadein.js";import{incrementLoader as G}from"../utils/loader.js";import{removeTags as K}from"@polotno/core/text";import{shadowCastByBackground as Q}from"@polotno/core/filters";import{applyOpacityToColor as U}from"@polotno/core/svg";import{isTouchDevice as Z}from"../utils/screen.js";import{getLimitedFontSize as tt}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as et,extractFontSpecsFromHtml as ot,scaleRichTextFontSizesInHtml as rt}from"../utils/rich-text-html.js";import{RichEditor as nt}from"./rich-editor.js";import{visiblePlaceholder as it}from"./text-element/placeholder.js";import{wrapHtmlForRenderTag as at}from"@polotno/core/reset-style";import{xmlEscape as lt}from"@polotno/core/xml";import{hasMeasuredHeight as st,resolveTextBoxHeight as ct}from"@polotno/core/text-types";export{at as wrapHtmlForRenderTag};function dt(t,e){return t!==e&&(!S.textVerticalResizeEnabled||t<e)}export const RenderTagElement=a(({element:a,store:at})=>{var ut,ht,ft,gt,mt,bt,pt,xt,yt,vt,wt;const zt=i.useRef(null),Et=i.useRef(null),[Ot,jt]=i.useState(!1),St=i.useRef(a.height),Mt=i.useRef(null),kt=a.isSelected,Rt=T(kt),{textVerticalResizeEnabled:Ft}=S,Yt=S.autoDeleteEmptyText,Xt=T(a.fontFamily),Ct=i.useMemo(()=>et(a.text,{fontSize:a.fontSize,fontFamily:a.fontFamily}),[a.text,a.fontSize,a.fontFamily]),It=i.useMemo(()=>ot(Ct,{fontFamily:a.fontFamily,fontWeight:a.fontWeight,fontStyle:a.fontStyle}),[Ct,a.fontFamily,a.fontWeight,a.fontStyle]),[Pt]=A(at,It,a.text||a.placeholder),Bt=a._editModeEnabled;q(zt);const Tt=Pt?a.fontFamily:Xt&&Xt!==a.fontFamily?Xt:"Arial",At=W(a).fill,Ht=it(a.placeholder),Nt=!!Ht&&!K(a.text),Dt=Nt?lt(Ht):Ct,Wt=Nt?.6:n(a),$t=H($(a),{fontFamily:Tt,color:At,forEditor:!0,text:Dt,textIsNormalized:!0}),_t=a.width||1,Jt=t=>C($(t),{getHtmlOptions:{fontFamily:Tt,color:At,forEditor:!0,text:Dt,textIsNormalized:!0}}),Lt=k(a),Vt=M(a),qt=a.curveEnabled,Gt=JSON.stringify(Vt),Kt="fixed"===Vt.box?a.height:0,Qt=Ot?Mt.current:null,Ut=i.useMemo(()=>{if(Qt){return Qt.sized}const t=Nt?Object.assign(Object.assign({},a),{text:Dt}):a;try{return qt?Object.assign({curved:!0},Y({element:t,sizing:Vt,layoutAt:I($(a),{getHtmlOptions:{fontFamily:Tt,color:At,text:Dt,textIsNormalized:!0}})})):Object.assign({curved:!1},X({element:t,sizing:Vt,layoutAt:Jt(a)}))}catch(e){return console.error("render-tag layout failed:",e),null}},[$t,_t,Pt,qt,Gt,Kt,a.curvePower,a.fontSize,a.width,Qt]),Zt=null!==(ut=null==Qt?void 0:Qt.width)&&void 0!==ut?ut:_t,te=Qt?a.width/Qt.width:1,ee=!1===(null==Ut?void 0:Ut.curved)?Ut.layout:null,oe=(null==Ut?void 0:Ut.curved)?Ut.layout:null,re=null!==(ht=null==Ut?void 0:Ut.fontSize)&&void 0!==ht?ht:a.fontSize,ne=null!==(ft=null==Ut?void 0:Ut.scale)&&void 0!==ft?ft:1,ie=D(a.letterSpacing,re),ae=null!==(gt=null==Qt?void 0:Qt.overhang)&&void 0!==gt?gt:ie,le=B({fontLoaded:Pt,fontFamily:a.fontFamily,fontSize:re,lineHeight:a.lineHeight}),se=qt?0:Math.max(null!==(mt=null==ee?void 0:ee.height)&&void 0!==mt?mt:0,re*le),ce=oe?oe.bounds:null,de=ce?(a.width-ce.width)/2-ce.x:0,ue=i.useMemo(()=>{var t;if(oe&&ce){const t=oe.paintBounds;return Object.assign(Object.assign({},t),{x:t.x+de,y:t.y-ce.y})}return null!==(t=null==ee?void 0:ee.paintBounds)&&void 0!==t?t:null},[ee,oe,ce,de]),he=i.useMemo(()=>ee?ee.lines.map(t=>V({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height},ae)):[],[ee,ae]),fe=i.useRef([]),ge=i.useRef(null),me=i.useRef(!0),[,be]=i.useReducer(t=>t+1,0);i.useEffect(()=>{const t=fe.current;t.length>0&&(fe.current=[],t.forEach(t=>t()))});const pe=i.useCallback((t,e,o,r)=>{const n=G(`text ${a.id}`),i=()=>{at.history.ignore(t,e,o).then(()=>{me.current?(fe.current.push(n),be()):n()})};r?queueMicrotask(i):i()},[a.id,at]),xe=i.useCallback(t=>{const e=null!==ge.current;ge.current=t,e||pe(()=>{const t=ge.current;ge.current=null,h(a)&&dt(a.height,t)&&a.set({height:t})},!1,!0)},[a,pe]),ye=i.useRef(Ut);ye.current=Ut,i.useEffect(()=>{if(!Pt){return}if(!ee){return}if(!st(a)){return void pe(()=>{h(a)&&(st(a)||a.set({height:se}))},void 0,void 0,!0)}if(at.isPlaying){return}const t=dt(a.height,se),e=()=>xe(se);if("change-font-size"!==Lt||Ot||Bt||a.curveEnabled){"resize"===Lt&&t&&!Ot&&e()}else if(Ut&&Ut.fontSize!==a.fontSize){const{fontSize:t,text:e}=Ut;pe(()=>{if(!h(a)){return}if(ye.current!==Ut||"change-font-size"!==k(a)||a._editModeEnabled){return}const o=x(a,t);Nt||e===a.text||(o.text=e),a.set(o)},!1,!0)}else{t&&e()}}),i.useEffect(()=>()=>{me.current=!1;const t=fe.current;t.length>0&&(fe.current=[],t.forEach(t=>t()))},[]),i.useLayoutEffect(()=>{if(!Pt||!a.curveEnabled||Ot){return}if(!ce||!ce.width){return}const t=t=>t&&Ft?Math.max(t,ce.height):ce.height,e=t=>F(Vt,{width:t},ce.width);Math.abs(e(a.width)-a.width)<1&&Math.abs(t(a.height)-a.height)<1||pe(()=>{if(!h(a)){return}const o=e(a.width),r=o-a.width,n=t(a.height),i=n-a.height;if(Math.abs(r)<1&&Math.abs(i)<1){return}const l=a.a.rotation*Math.PI/180,s=-r/2*Math.cos(l),c=-r/2*Math.sin(l);a.set({width:o,height:n,x:a.a.x+s,y:a.a.y+c})},!1,!0,!0)},[Pt,a.curveEnabled,Lt,ce,Ot,Ft,pe]),i.useEffect(()=>{Rt&&!kt&&Yt&&""===K(a.text)&&a.removable&&!it(a.placeholder)&&at.deleteElements([a.id])},[Yt,a.placeholder,a.removable,a.text,kt,Rt]),i.useEffect(()=>u(a,()=>{const t=zt.current;t&&P(t,a)}),[ee,oe,a.a.width,a.a.height,a.shadowColor,a.shadowOffsetX,a.shadowOffsetY,a.shadowOpacity]);const ve=ct(a,se),we=qt||!Pt||Ot||at.isPlaying||"resize"!==Lt?a.a.height:Ft?Math.max(a.a.height,se):se;let ze=0;qt||"middle"!==a.verticalAlign?qt||"bottom"!==a.verticalAlign||(ze=ve-se*te):ze=(ve-se*te)/2;const Ee=null!==(bt=null==Qt?void 0:Qt.backgroundPadding)&&void 0!==bt?bt:a.backgroundPadding*(re*le*.5),Oe=null!==(pt=null==Qt?void 0:Qt.backgroundCornerRadius)&&void 0!==pt?pt:a.backgroundCornerRadius*(re*le*.5),je=a.backgroundEnabled||a.borderSize>0,Se=i.useMemo(()=>!je||a.curveEnabled||a.legacyBackground||0===he.length?"":L({rects:he,padding:Ee,cornerRadius:Oe}),[je,a.curveEnabled,a.legacyBackground,he,Ee,Oe]),Me=Z(),ke=i.useRef(null),Re=i.useRef(a.width),Fe=i.useRef(null),Ye=i.useRef(null),Xe=i.useRef(""),Ce=i.useRef(0),Ie=i.useRef(0),Pe=i.useRef({fontSize:a.fontSize}),Be=a.width>0?a.a.width/a.width:1,Te=Be*te,Ae=null!==(xt=null==Qt?void 0:Qt.shadow)&&void 0!==xt?xt:{shadowBlur:a.shadowBlur*ne,shadowOffsetX:a.shadowOffsetX*ne,shadowOffsetY:a.shadowOffsetY*ne},He=Object.assign(Object.assign({},Ae),{shadowColor:_(a.shadowColor),shadowOpacity:a.shadowOpacity}),Ne=Object.assign(Object.assign({},He),{shadowEnabled:a.shadowEnabled&&je});return r(o,{children:[a.curveEnabled||a.legacyBackground?e(c,Object.assign({x:a.a.x,y:a.a.y,offsetX:Ee/Be,offsetY:Ee/Be,rotation:a.a.rotation,scaleX:Te,scaleY:Te,hideInExport:!a.showInExport,listening:!1,visible:je,opacity:Wt,fillEnabled:a.backgroundEnabled,fill:U(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:_(a.borderColor),strokeWidth:null!==(yt=null==Qt?void 0:Qt.borderSize)&&void 0!==yt?yt:a.borderSize},Ne,{width:Zt+2*Ee,height:(null!==(vt=null==Qt?void 0:Qt.boxHeight)&&void 0!==vt?vt:ve)+2*Ee,cornerRadius:Oe})):e(s,Object.assign({name:"rendertag-background",x:a.a.x,y:a.a.y,offsetY:-ze/Te,rotation:a.a.rotation,scaleX:Te,scaleY:Te,hideInExport:!a.showInExport,listening:!1,visible:je&&Se.length>0,opacity:Wt,fillEnabled:a.backgroundEnabled,fill:U(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:_(a.borderColor),strokeWidth:null!==(wt=null==Qt?void 0:Qt.borderSize)&&void 0!==wt?wt:a.borderSize,lineJoin:Oe>0?"round":"miter"},Ne,{data:Se})),e(d,Object.assign({ref:zt,selfRectFunc:ue?()=>ue:void 0,name:"rendertag-text",x:a.a.x,y:a.a.y,rotation:a.a.rotation,scaleX:Te,scaleY:Te,offsetY:Te>0?-ze/Te:0,width:Zt,height:Math.max(a.height,se),listening:!1,visible:!Bt&&!!(qt?oe:ee),editModeEnabled:Bt,opacity:Wt,shadowEnabled:a.shadowEnabled&&!Q(a)},He,{hideInExport:!a.showInExport,sceneFunc:(t,e)=>{const o=t._context;if(o){try{if(qt){if(!oe||!ce){return}o.save(),o.translate(de,-ce.y),m({layout:oe,ctx:o}),o.restore()}else{if(!ee){return}g({layout:ee,width:Zt,ctx:o,pixelRatio:1})}}catch(r){console.error("render-tag draw failed:",r)}}}})),e(c,{ref:Et,name:"element",x:a.a.x,y:a.a.y,listening:a.selectable,rotation:a.a.rotation,width:a.a.width,height:we,visible:!Bt,draggable:Me?a.draggable&&kt:a.draggable,preventDefault:!Me||kt,opacity:0,hideInExport:!0,id:a.id,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(O(t.target))},onDblClick:t=>{a.contentEditable&&(Ye.current={x:t.evt.clientX,y:t.evt.clientY},a.toggleEditMode(!0))},onDblTap:t=>{var e;if(a.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Ye.current=o?{x:o.clientX,y:o.clientY}:null,a.toggleEditMode(!0)}},onTransformStart:t=>{St.current=a.height,Re.current=a.width,Fe.current=null,ke.current=t.target.position(),Xe.current=b(t.target),Ce.current=a.a.rotation,Ie.current=a.height,Pe.current={fontSize:a.fontSize,minFontSize:a.minFontSize,maxFontSize:a.maxFontSize},Mt.current=!a.curveEnabled&&["top-left","top-right","bottom-left","bottom-right"].includes(Xe.current)?{sized:Ut,width:_t,boxHeight:ve,backgroundPadding:Ee,backgroundCornerRadius:Oe,borderSize:a.borderSize,overhang:ie,shadow:Ae}:null,jt(!0)},onTransform:e=>{const o=e.target,r=Xe.current,n="middle-left"===r||"middle-right"===r,i="top-center"===r||"bottom-center"===r,l=o.scaleX();if(n){const e=o.scaleX(),r=o.width()*e;let n=Math.max(r,j(Re.current,R(a)));const i=a.toJSON();let l=0;if("fit"===Lt){l=St.current}else if(a.curveEnabled){l=N(Object.assign(Object.assign({},i),{width:n}))}else{const{x:e,y:o,width:r,height:a}=i,s=t(i,["x","y","width","height"]),c=`${JSON.stringify(s)}|${Tt}|${Pt}|${At}`;if(Fe.current&&Fe.current.signature===c&&n<=Fe.current.width){n=Fe.current.width,l=Fe.current.height}else{const t=t=>{const e=Object.assign(Object.assign({},i),{width:t});try{return X({element:Object.assign(Object.assign({},e),{text:Dt}),sizing:Object.assign(Object.assign({},Vt),{fit:{mode:"off"},cutAtBox:!1}),layoutAt:Jt(e)}).layout}catch(ut){const o=H(e,{fontFamily:Tt,color:At,forEditor:!0,text:Dt,textIsNormalized:!0});return{height:J(o).height,lines:[]}}};let e=t(n);const o=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width-ie));o>n+.5&&o<=Re.current+.5&&(n=Math.min(Math.ceil(o),Re.current),e=t(n),Fe.current={signature:c,width:n,height:e.height}),l=e.height}}n>r&&ke.current&&o.position(ke.current),o.width(n),o.scaleX(1),o.scaleY(1);let s=S.textVerticalResizeEnabled?Math.max(l,St.current):l;"ellipsis"!==Lt&&"fit"!==Lt||(s=St.current),o.height(s),a.set({width:o.width(),height:s,x:o.x(),y:o.y()})}else if(i){const t="resize"===Lt?qt?(null==ce?void 0:ce.height)||a.fontSize:se:le*("fit"===Vt.fit.mode?Vt.fit.min:a.fontSize),e=o.height()*o.scaleY();if(e<t){if(o.height(t),ke.current){if("top-center"===Xe.current){const e=Ce.current*Math.PI/180,r=Ie.current-t;o.x(ke.current.x-r*Math.sin(e)),o.y(ke.current.y+r*Math.cos(e))}else{o.position(ke.current)}}}else{o.height(e)}o.scaleY(1),o.scaleX(1),Ie.current=Math.max(t,e),a.set({x:o.x(),y:o.y(),height:o.height(),rotation:o.rotation()})}else{o.scaleX(1),o.scaleY(1);const t=a.fontSize*l;a.set(Object.assign(Object.assign(Object.assign({},y(Pe.current,t/Pe.current.fontSize)),{letterSpacing:a.letterSpacing,width:o.width()*l,x:o.x(),y:o.y(),rotation:o.rotation(),height:o.height()*l}),z(a,l)))}ke.current=e.target.position()},onTransformEnd:t=>{Mt.current=null,jt(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1);const o=v(a,a.fontSize*e),r=Pe.current,n=Xe.current;a.set(Object.assign(Object.assign(Object.assign({},o),p(n)&&"rotater"!==n?y(r,o.fontSize/r.fontSize,!0):{}),{width:E(t.target.width()*e),x:E(t.target.x()),y:E(t.target.y()),rotation:E(t.target.rotation()),text:rt(a.text,e)}))}}),Bt&&e(l,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-ze,children:e(f,{divProps:{style:{zIndex:20}},children:e(nt,{element:a,fitScale:ne,fitScaleFor:t=>{if(qt){return ne}try{return X({element:Object.assign(Object.assign({},a),{text:t}),sizing:Vt,layoutAt:C($(a),{getHtmlOptions:{fontFamily:Tt,color:At,forEditor:!0,text:t}})}).scale}catch(ut){return ne}},onChange:t=>{if("fit"===Lt){return void a.set({text:t})}const e=tt({oldText:K(a.text),newText:K(t),element:a});a.set(Object.assign({text:t},x(a,e)))},onBlur:()=>{h(a)&&(a.set(w(a)),a.toggleEditMode(!1))},clickCoords:Ye.current})})})]})});
|
package/canvas/rich-editor.d.ts
CHANGED
|
@@ -41,11 +41,13 @@ type Point = {
|
|
|
41
41
|
x: number;
|
|
42
42
|
y: number;
|
|
43
43
|
} | null;
|
|
44
|
-
export declare const RichEditor: ({ onBlur, onChange, element, clickCoords, }: {
|
|
44
|
+
export declare const RichEditor: ({ onBlur, onChange, element, clickCoords, fitScale, fitScaleFor, }: {
|
|
45
45
|
onBlur: (e?: any) => void;
|
|
46
46
|
onChange: (t: string) => void;
|
|
47
47
|
element: TextElementType;
|
|
48
48
|
clickCoords: Point;
|
|
49
|
+
fitScale?: number;
|
|
50
|
+
fitScaleFor?: (text: string) => number;
|
|
49
51
|
}) => React.JSX.Element;
|
|
50
52
|
export declare function setCursorFromCoords(quill: any, coords: {
|
|
51
53
|
x: number;
|