polotno 3.20.1 → 3.22.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.
Files changed (111) hide show
  1. package/THIRD_PARTY_NOTICES.md +8 -8
  2. package/blueprint/side-panel/select-video.d.ts +74 -0
  3. package/blueprint.polotno.css +165 -230
  4. package/canvas/crop-overlay.d.ts +20 -8
  5. package/canvas/crop-overlay.js +1 -1
  6. package/canvas/element.js +1 -1
  7. package/canvas/fade-overlay.js +1 -1
  8. package/canvas/figure-element.js +1 -1
  9. package/canvas/gif-element.d.ts +28 -4
  10. package/canvas/gif-element.js +1 -1
  11. package/canvas/image-element.js +1 -1
  12. package/canvas/lottie-element.js +1 -1
  13. package/canvas/lottie-player.wasm.js +1 -1
  14. package/canvas/media-resize.d.ts +19 -0
  15. package/canvas/media-resize.js +1 -0
  16. package/canvas/page.js +1 -1
  17. package/canvas/quill-font-formats.d.ts +1 -1
  18. package/canvas/quill-font-formats.js +1 -1
  19. package/canvas/render-tag-element.js +1 -1
  20. package/canvas/rich-editor.js +3 -5
  21. package/canvas/text-element/render-tag-source.d.ts +5 -0
  22. package/canvas/text-element/render-tag-source.js +1 -1
  23. package/canvas/tooltip.js +1 -1
  24. package/canvas/use-color.js +1 -1
  25. package/canvas/use-mediabunny-video.js +1 -1
  26. package/canvas/video-element.js +1 -1
  27. package/canvas/workspace-canvas.js +1 -1
  28. package/config.d.ts +1 -1
  29. package/config.js +1 -1
  30. package/icons/registry.d.ts +7 -0
  31. package/icons/registry.js +1 -1
  32. package/image-tools/host.d.ts +3 -0
  33. package/image-tools/sessions.js +1 -1
  34. package/image-tools/target.d.ts +5 -0
  35. package/image-tools/target.js +1 -1
  36. package/image-tools/types.d.ts +5 -0
  37. package/model/crop-mode.d.ts +3 -0
  38. package/model/crop-mode.js +1 -1
  39. package/model/figure-model.d.ts +29 -0
  40. package/model/gif-model.d.ts +40 -0
  41. package/model/group-model.d.ts +304 -2
  42. package/model/group-model.js +1 -1
  43. package/model/history.js +1 -1
  44. package/model/image-model.d.ts +40 -0
  45. package/model/image-model.js +1 -1
  46. package/model/line-model.d.ts +29 -0
  47. package/model/lottie-model.d.ts +40 -0
  48. package/model/node-model.d.ts +32 -0
  49. package/model/node-model.js +1 -1
  50. package/model/page-model.d.ts +912 -6
  51. package/model/shape-model.d.ts +29 -0
  52. package/model/shape-model.js +1 -1
  53. package/model/store.d.ts +61 -2
  54. package/model/store.js +1 -1
  55. package/model/svg-model.d.ts +27 -0
  56. package/model/table-model.d.ts +37 -1
  57. package/model/table-model.js +1 -1
  58. package/model/text-model.d.ts +29 -1
  59. package/model/text-model.js +1 -1
  60. package/model/video-model.d.ts +40 -0
  61. package/package.json +25 -27
  62. package/polotno.bundle.js +240 -238
  63. package/primitives/autocomplete.d.ts +11 -0
  64. package/primitives/autocomplete.js +1 -0
  65. package/primitives/select.js +1 -1
  66. package/side-panel/index.d.ts +1 -0
  67. package/side-panel/index.js +1 -1
  68. package/side-panel/layers-panel.js +1 -1
  69. package/side-panel/select-image.js +1 -1
  70. package/side-panel/select-video.d.ts +74 -0
  71. package/side-panel/select-video.js +1 -1
  72. package/side-panel/variables-panel.d.ts +10 -0
  73. package/side-panel/variables-panel.js +1 -0
  74. package/side-panel/videos-grid.d.ts +1 -1
  75. package/side-panel/videos-grid.js +1 -1
  76. package/toolbar/color-picker.d.ts +8 -0
  77. package/toolbar/color-picker.js +1 -1
  78. package/toolbar/download-button.js +1 -1
  79. package/toolbar/element-container.d.ts +6 -0
  80. package/toolbar/element-container.js +1 -1
  81. package/toolbar/figure-toolbar.d.ts +8 -9
  82. package/toolbar/figure-toolbar.js +1 -1
  83. package/toolbar/gif-toolbar.d.ts +4 -5
  84. package/toolbar/gif-toolbar.js +1 -1
  85. package/toolbar/group-toolbar.d.ts +2 -3
  86. package/toolbar/group-toolbar.js +1 -1
  87. package/toolbar/image-toolbar.d.ts +11 -12
  88. package/toolbar/image-toolbar.js +1 -1
  89. package/toolbar/index.d.ts +1 -0
  90. package/toolbar/index.js +1 -1
  91. package/toolbar/line-toolbar.js +1 -1
  92. package/toolbar/many-toolbar.d.ts +2 -3
  93. package/toolbar/many-toolbar.js +1 -1
  94. package/toolbar/page-toolbar.d.ts +2 -3
  95. package/toolbar/page-toolbar.js +1 -1
  96. package/toolbar/svg-toolbar.d.ts +2 -3
  97. package/toolbar/svg-toolbar.js +1 -1
  98. package/toolbar/text-toolbar.js +1 -1
  99. package/toolbar/text-variables.d.ts +15 -0
  100. package/toolbar/text-variables.js +1 -0
  101. package/toolbar/toolbar.js +1 -1
  102. package/toolbar/video-toolbar.js +1 -1
  103. package/ui.css +1 -1
  104. package/utils/animations.js +1 -1
  105. package/utils/clipboard.d.ts +54 -2
  106. package/utils/l10n.d.ts +32 -0
  107. package/utils/l10n.js +1 -1
  108. package/utils/selection-transform.js +1 -1
  109. package/utils/use-api.d.ts +1 -1
  110. package/utils/use-api.js +1 -1
  111. package/utils/validate-key.js +1 -1
@@ -29,15 +29,19 @@ interface CropOverlayElement extends CropHost {
29
29
  readonly shadowBlur: number;
30
30
  toggleCropMode(flag?: boolean): void;
31
31
  }
32
- export declare const CropOverlay: (({ element, store, previewImage, sourceImage, sourceWidth, sourceHeight, cropAbsoluteWidth, cropAbsoluteHeight, innerAnchors, onSourceTouchMove, onSourceTouchEnd, }: {
32
+ export declare const CropOverlay: (({ element, store, previewImage, sourceImage, sourceWidth, sourceHeight, crop, innerAnchors, onSourceTouchMove, onSourceTouchEnd, }: {
33
33
  element: CropOverlayElement;
34
34
  store: StoreType;
35
35
  previewImage: CanvasImageSource | undefined;
36
36
  sourceImage: CanvasImageSource | undefined;
37
37
  sourceWidth: number;
38
38
  sourceHeight: number;
39
- cropAbsoluteWidth: number;
40
- cropAbsoluteHeight: number;
39
+ crop: {
40
+ x: number;
41
+ y: number;
42
+ width: number;
43
+ height: number;
44
+ };
41
45
  innerAnchors?: string[];
42
46
  onSourceTouchMove?: (e: Konva.KonvaEventObject<TouchEvent>) => void;
43
47
  onSourceTouchEnd?: (e: Konva.KonvaEventObject<TouchEvent>) => void;
@@ -48,21 +52,29 @@ export declare const CropOverlay: (({ element, store, previewImage, sourceImage,
48
52
  sourceImage: CanvasImageSource | undefined;
49
53
  sourceWidth: number;
50
54
  sourceHeight: number;
51
- cropAbsoluteWidth: number;
52
- cropAbsoluteHeight: number;
55
+ crop: {
56
+ x: number;
57
+ y: number;
58
+ width: number;
59
+ height: number;
60
+ };
53
61
  innerAnchors?: string[];
54
62
  onSourceTouchMove?: (e: Konva.KonvaEventObject<TouchEvent>) => void;
55
63
  onSourceTouchEnd?: (e: Konva.KonvaEventObject<TouchEvent>) => void;
56
64
  }> & {
57
- readonly type: ({ element, store, previewImage, sourceImage, sourceWidth, sourceHeight, cropAbsoluteWidth, cropAbsoluteHeight, innerAnchors, onSourceTouchMove, onSourceTouchEnd, }: {
65
+ readonly type: ({ element, store, previewImage, sourceImage, sourceWidth, sourceHeight, crop, innerAnchors, onSourceTouchMove, onSourceTouchEnd, }: {
58
66
  element: CropOverlayElement;
59
67
  store: StoreType;
60
68
  previewImage: CanvasImageSource | undefined;
61
69
  sourceImage: CanvasImageSource | undefined;
62
70
  sourceWidth: number;
63
71
  sourceHeight: number;
64
- cropAbsoluteWidth: number;
65
- cropAbsoluteHeight: number;
72
+ crop: {
73
+ x: number;
74
+ y: number;
75
+ width: number;
76
+ height: number;
77
+ };
66
78
  innerAnchors?: string[];
67
79
  onSourceTouchMove?: (e: Konva.KonvaEventObject<TouchEvent>) => void;
68
80
  onSourceTouchEnd?: (e: Konva.KonvaEventObject<TouchEvent>) => void;
@@ -1 +1 @@
1
- import{Fragment as o,jsx as t,jsxs as e}from"react/jsx-runtime";import r from"react";import{observer as i}from"mobx-react-lite";import{Group as n,Image as a,Line as c,Rect as l,Transformer as s}from"react-konva";import{Portal as d}from"react-konva-utils";import{useWorkspaceStyle as h}from"./workspace-style.js";import{cropBoundBoxFunc as m}from"./transform-box.js";import{clamp as u}from"../lib/utils.js";import{RectControl as p}from"./rect-control.js";import{ParentFrame as g}from"./parent-frame.js";import{frameBox as f,relativeRect as v}from"../image-tools/geometry.js";import{sessionControls as x}from"../image-tools/sessions.js";export const CROP_CORNER_ANCHORS=["top-left","top-right","bottom-left","bottom-right"];const b={enabledAnchors:CROP_CORNER_ANCHORS,rotateEnabled:!1,borderEnabled:!1},y=({width:e,height:r})=>{const{cropGridStyle:i}=h();return i&&e>0&&r>0?t(o,{children:[[e/3,0,e/3,r],[2*e/3,0,2*e/3,r],[0,r/3,e,r/3],[0,2*r/3,e,2*r/3]].map((o,e)=>t(c,Object.assign({},i,{points:o,strokeScaleEnabled:!1,listening:!1}),e))}):null};export const cropDraftWriter=(o,t)=>{const e=t.activeTool;return r=>{"crop"===(null==e?void 0:e.name)&&e.elementId===o.id&&t.activeTool===e&&x(e).setDraft(r)}};export const commitCropSource=(o,t,e,r,i,n,a)=>{const c=o.target;Math.round(c.x())>0&&(c.x(0),c.scaleX(1)),Math.round(c.y())>0&&(c.y(0),c.scaleY(1));const l=c.width()*c.scaleX(),s=c.height()*c.scaleY(),d=Math.min(1,n/l),h=Math.min(1,a/s),m=u(-c.x()/l,0,1-d),p=u(-c.y()/s,0,1-h);c.setAttrs({x:-m*r,y:-p*i,scaleX:1,scaleY:1}),e({cropX:m,cropY:p,cropWidth:d,cropHeight:h})};export const CropOverlay=i(({element:o,store:i,previewImage:c,sourceImage:u,sourceWidth:x,sourceHeight:w,cropAbsoluteWidth:j,cropAbsoluteHeight:E,innerAnchors:O,onSourceTouchMove:C,onSourceTouchEnd:R})=>{var S,M,T,I,A,X;const Y=h(),H=r.useRef(null),W=r.useRef(null),k=r.useRef(null),D=i.activeTool,B="crop"===(null==D?void 0:D.name)&&D.pending,F=cropDraftWriter(o,i);r.useLayoutEffect(()=>{k.current.nodes([H.current])},[]),r.useEffect(()=>{var t;const e=null===(t=H.current)||void 0===t?void 0:t.getStage(),r=t=>{B||t.target.findAncestor(".image-tool-controls",!0)||o.toggleCropMode(!1)};return null==e||e.on("click",r),null==e||e.on("tap",r),()=>{null==e||e.off("click",r),null==e||e.off("tap",r)}},[o,B]);const N=o=>{var t;B||(C&&(null===(t=o.evt.touches)||void 0===t?void 0:t.length)>2&&o.target.stopDrag(),commitCropSource(o,0,F,x,w,j,E))},_={x:0,y:0,width:j,height:E},P=o.a.width/j,z=o.a.height/E,G=Math.max(window.innerWidth,window.innerHeight,null!==(A=null===(M=null===(S=H.current)||void 0===S?void 0:S.getStage())||void 0===M?void 0:M.width())&&void 0!==A?A:0,null!==(X=null===(I=null===(T=H.current)||void 0===T?void 0:T.getStage())||void 0===I?void 0:I.height())&&void 0!==X?X:0)/i.scale;return t(d,{selector:".page-abs-container",enabled:!0,children:e(g,{elementId:o.id,children:[t(l,{name:"image-tool-dim",hideInExport:!0,x:-G,y:-G,width:3*G,height:3*G,fill:Y.cropOverlayFill}),t(a,{hideInExport:!0,listening:!1,image:c,x:o.a.x,y:o.a.y,width:o.a.width,height:o.a.height,rotation:o.a.rotation,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur}),e(n,{name:"image-tool-controls",hideInExport:!0,x:o.a.x,y:o.a.y,rotation:o.a.rotation,scaleX:P,scaleY:z,children:[t(a,{ref:H,image:u,width:x,height:w,opacity:.4,draggable:!B,x:-o.cropX*x,y:-o.cropY*w,onDragMove:N,onTransform:N,onTouchMove:B?void 0:C,onTouchEnd:B?void 0:R}),t(s,Object.assign({ref:k},b,Y.outerImageCropTransformerStyle,{visible:!B,boundBoxFunc:m(H,W,"cover",Y.outerImageCropTransformerStyle.boundBoxFunc)})),t(p,{crop:!0,nodeRef:W,fixedRef:H,rect:_,disabled:!o.resizable||B,anchors:O,onChange:t=>{var e;const r=o.cropX+t.x/x,i=o.cropY+t.y/w,n=f(Object.assign(Object.assign({},o),{rotation:o.a.rotation}),v(t,_));null===(e=W.current)||void 0===e||e.position({x:0,y:0}),F(Object.assign(Object.assign({},n),{cropX:r,cropY:i,cropWidth:Math.min(t.width/x,1-r),cropHeight:Math.min(t.height/w,1-i)}))}}),t(y,{width:j,height:E})]})]})})});
1
+ import{Fragment as o,jsx as t,jsxs as e}from"react/jsx-runtime";import r from"react";import{observer as i}from"mobx-react-lite";import{Group as n,Image as a,Line as l,Rect as c,Transformer as s}from"react-konva";import{Portal as h}from"react-konva-utils";import{useWorkspaceStyle as d}from"./workspace-style.js";import{cropBoundBoxFunc as m}from"./transform-box.js";import{clamp as g}from"../lib/utils.js";import{RectControl as u}from"./rect-control.js";import{ParentFrame as p}from"./parent-frame.js";import{frameBox as f,relativeRect as v}from"../image-tools/geometry.js";import{sessionControls as x}from"../image-tools/sessions.js";export const CROP_CORNER_ANCHORS=["top-left","top-right","bottom-left","bottom-right"];const b={enabledAnchors:CROP_CORNER_ANCHORS,rotateEnabled:!1,borderEnabled:!1},w=({width:e,height:r})=>{const{cropGridStyle:i}=d();return i&&e>0&&r>0?t(o,{children:[[e/3,0,e/3,r],[2*e/3,0,2*e/3,r],[0,r/3,e,r/3],[0,2*r/3,e,2*r/3]].map((o,e)=>t(l,Object.assign({},i,{points:o,strokeScaleEnabled:!1,listening:!1}),e))}):null};export const cropDraftWriter=(o,t)=>{const e=t.activeTool;return r=>{"crop"===(null==e?void 0:e.name)&&e.elementId===o.id&&t.activeTool===e&&x(e).setDraft(r)}};export const commitCropSource=(o,t,e,r,i,n,a)=>{const l=o.target;Math.round(l.x())>0&&(l.x(0),l.scaleX(1)),Math.round(l.y())>0&&(l.y(0),l.scaleY(1));const c=l.width()*l.scaleX(),s=l.height()*l.scaleY(),h=Math.min(1,n/c),d=Math.min(1,a/s),m=g(-l.x()/c,0,1-h),u=g(-l.y()/s,0,1-d);l.setAttrs({x:-m*r,y:-u*i,scaleX:1,scaleY:1}),e({cropX:m,cropY:u,cropWidth:h,cropHeight:d})};export const CropOverlay=i(({element:o,store:i,previewImage:l,sourceImage:g,sourceWidth:x,sourceHeight:y,crop:j,innerAnchors:E,onSourceTouchMove:O,onSourceTouchEnd:C})=>{var R,S,M,T,I,k;const A=d(),H=r.useRef(null),W=r.useRef(null),X=r.useRef(null),Y=i.activeTool,D="crop"===(null==Y?void 0:Y.name)&&Y.pending,B=cropDraftWriter(o,i);r.useLayoutEffect(()=>{X.current.nodes([H.current])},[]),r.useEffect(()=>{var t;const e=null===(t=H.current)||void 0===t?void 0:t.getStage(),r=t=>{D||t.target.findAncestor(".image-tool-controls",!0)||o.toggleCropMode(!1)};return null==e||e.on("click",r),null==e||e.on("tap",r),()=>{null==e||e.off("click",r),null==e||e.off("tap",r)}},[o,D]);const F=o=>{var t;D||(O&&(null===(t=o.evt.touches)||void 0===t?void 0:t.length)>2&&o.target.stopDrag(),commitCropSource(o,0,B,x,y,j.width,j.height))},N={x:0,y:0,width:j.width,height:j.height},_=o.a.width/j.width,P=o.a.height/j.height,z=Math.max(window.innerWidth,window.innerHeight,null!==(I=null===(S=null===(R=H.current)||void 0===R?void 0:R.getStage())||void 0===S?void 0:S.width())&&void 0!==I?I:0,null!==(k=null===(T=null===(M=H.current)||void 0===M?void 0:M.getStage())||void 0===T?void 0:T.height())&&void 0!==k?k:0)/i.scale;return t(h,{selector:".page-abs-container",enabled:!0,children:e(p,{elementId:o.id,children:[t(c,{name:"image-tool-dim",hideInExport:!0,x:-z,y:-z,width:3*z,height:3*z,fill:A.cropOverlayFill}),t(a,{hideInExport:!0,listening:!1,image:l,x:o.a.x,y:o.a.y,width:o.a.width,height:o.a.height,rotation:o.a.rotation,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur}),e(n,{name:"image-tool-controls",hideInExport:!0,x:o.a.x,y:o.a.y,rotation:o.a.rotation,scaleX:_,scaleY:P,children:[t(a,{ref:H,image:g,width:x,height:y,opacity:.4,draggable:!D,x:-j.x,y:-j.y,onDragMove:F,onTransform:F,onTouchMove:D?void 0:O,onTouchEnd:D?void 0:C}),t(s,Object.assign({ref:X},b,A.outerImageCropTransformerStyle,{visible:!D,boundBoxFunc:m(H,W,"cover",A.outerImageCropTransformerStyle.boundBoxFunc)})),t(u,{crop:!0,nodeRef:W,fixedRef:H,rect:N,disabled:!o.resizable||D,anchors:E,onChange:t=>{var e;const r=(j.x+t.x)/x,i=(j.y+t.y)/y,n=f(Object.assign(Object.assign({},o),{rotation:o.a.rotation}),v(t,N));null===(e=W.current)||void 0===e||e.position({x:0,y:0}),B(Object.assign(Object.assign({},n),{cropX:r,cropY:i,cropWidth:Math.min(t.width/x,1-r),cropHeight:Math.min(t.height/y,1-i)}))}}),t(w,{width:j.width,height:j.height})]})]})})});
package/canvas/element.js CHANGED
@@ -1 +1 @@
1
- import{Fragment as e,jsx as t,jsxs as o}from"react/jsx-runtime";import{createElement as r}from"react";import n from"react";import{observer as i}from"mobx-react-lite";import{ROLES as s}from"../model/store.js";import l from"konva";import{MAIN_TRANSFORMER_NAME as a}from"../utils/px.js";import{TextElement as m}from"./text-element.js";import{ImageElement as d}from"./image-element.js";import{HTMLElement as c}from"./html-element.js";import{RenderTagElement as f}from"./render-tag-element.js";import{LineElement as p}from"./line-element.js";import{VideoElement as u}from"./video-element.js";import{FigureElement as g}from"./figure-element.js";import{GifElement as v}from"./gif-element.js";import{LottieElement as h}from"./lottie-element.js";import{TableElement as x}from"./table-element.js";import{Group as y}from"react-konva";import{Highlighter as b}from"./highlighter.js";import{PlacedNode as j}from"./placed-node.js";import{forEveryChild as w}from"../model/group-model.js";import{flags as E}from"../utils/flags.js";import{blendModeOf as T,isIsolatedGroup as M}from"@polotno/core/page";const R={text:m,image:d,svg:d,line:p,video:u,figure:g,group:i(e=>{var o;const{element:n,store:i}=e,l=n,{children:a}=l,m=n.selectable||i.role===s.ADMIN,d=l.a;return t(j,{as:y,element:l,offsetX:d.offsetX,offsetY:d.offsetY,scaleX:d.scaleX,scaleY:d.scaleY,opacity:d.opacity,isolated:M(l),globalCompositeOperation:null!==(o=T(l))&&void 0!==o?o:"source-over",listening:m,hideInExport:!n.showInExport,id:n.id,clipFunc:d.wipe&&l._restingBbox?C(l._restingBbox,d.wipe):void 0,children:a.map(t=>r(X,Object.assign({},e,{key:t.id,store:i,element:t})))})}),gif:v,lottie:h,table:x};export function registerShapeComponent(e,t){R[e]=t}const S=(e,t)=>{const o=[];w(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 l.Transform;o.translate(e.x,e.y),o.rotate(l.Util.degToRad(e.rotation)),t.forEach(e=>{const t=o.point(e);r.push(t)})});const n=new l.Transform;n.rotate(-l.Util.degToRad(t));let i=1/0,s=1/0,a=-1/0,m=-1/0;r.forEach(e=>{const t=n.point(e);i=Math.min(i,t.x),s=Math.min(s,t.y),a=Math.max(a,t.x),m=Math.max(m,t.y)}),n.invert();const d=n.point({x:i,y:s});return{x:d.x,y:d.y,width:a-i,height:m-s,rotation:t}},I=".polotnoElementHover",O=new WeakMap;const k=1e6,C=(e,t)=>o=>{var r;const n="x"===t.axis?e.width:e.height,i=t.from<=0?-k:t.from*n,s=t.to>=1?k:t.to*n,[a,m,d,c]="x"===t.axis?[i,s,-k,k]:[-k,k,i,s];o.save(),o.translate(e.x,e.y),o.rotate(l.Util.degToRad(null!==(r=e.rotation)&&void 0!==r?r:0)),o.rect(a,d,m-a,c-d),o.restore()},U=(e,t)=>{const o=l.Util.degToRad(e.rotation);return(new l.Transform).translate(e.x,e.y).rotate(o).translate(e.width/2,0).scale(t,1).translate(-e.width/2,0).rotate(-o).translate(-e.x,-e.y).decompose()},X=i(r=>{var i,s;const{element:l,store:m}=r,[d,p]=n.useState(!1),u="group"===(null===(i=l.parent)||void 0===i?void 0:i.type),g="group"!==l.type||u?null:m.selectedElements,v=!!g&&1===g.length&&g[0]!==l&&g[0].top===l,[h,x]=n.useState(!1),j=n.useRef(null),w=l.visible,M=()=>{var e,t;return null!==(t=null===(e=j.current)||void 0===e?void 0:e.getStage())&&void 0!==t?t:null},k=n.useRef(void 0),X=()=>{var e;return k.current||(k.current=null===(e=M())||void 0===e?void 0:e.findOne(`.${a}`)),k.current};n.useEffect(()=>{const e=M();if(!e){return}const t=function(e,t,o,r){var n;const{subscribers:i}=function(e,t){const o=O.get(e);if(o){return o}const r={subscribers:new Map},n=e=>{var t,o;const n=r.hoveredId;r.hoveredId=e,n&&n!==e&&(null===(t=r.subscribers.get(n))||void 0===t||t.forEach(e=>e(!1))),e&&(null===(o=r.subscribers.get(e))||void 0===o||o.forEach(e=>e(!0)))};return e.on(`mouseover${I}`,e=>{var o,r;const i=e.target.findAncestor(".element",!0);n(null===(r=null===(o=t.getElementById(null==i?void 0:i.id()))||void 0===o?void 0:o.top)||void 0===r?void 0:r.id)}),e.on(`mouseleave${I}`,()=>n(void 0)),O.set(e,r),r}(e,t),s=null!==(n=i.get(o))&&void 0!==n?n:new Set;return i.set(o,s),s.add(r),()=>{s.delete(r),s.size||(i.delete(o),i.size||(e.off(I),O.delete(e)))}}(e,m,l.id,p);return()=>{t(),p(!1)}},[w,l.id]),n.useEffect(()=>{if(!v){return}const e=M(),t=X();if(!e||!t){return}x(!1);const o=()=>x(!0),r=()=>x(!1);return e.on("dragstart",o),e.on("dragend",r),t.on("transformstart",o),t.on("transformend",r),()=>{e.off("dragstart",o),e.off("dragend",r),t.off("transformstart",o),t.off("transformend",r)}},[w,v]);let Y=R[r.element.type];if("text"===r.element.type&&(E.renderTagTextEnabled?Y=f:E.htmlRenderEnabled&&(Y=c)),!w){return null}if(!Y){return console.error("Can not find component for "+r.element.type),null}const B="group"===l.type?void 0:T(l),F="group"===l.type?null:l.a,$=null==F?void 0:F.wipe,z=null==F?void 0:F.flipScale;return o(e,{children:[t(y,Object.assign({name:"element-container",ref:j,visible:l.active,listening:l.active,isolated:!!B,opacity:B?l.a.opacity:1,globalCompositeOperation:null!=B?B:"source-over",clipFunc:$?C(F,$):void 0},void 0===z?null:U(F,z),{children:t(Y,Object.assign({},r))})),((d||v)&&!u||l.isHighlighted)&&l.active&&!l.isSelectedDirectly&&!h&&t(b,{element:"group"===l.type?{a:S(l,v?"rotation"in g[0]?g[0].rotation:0:d&&(null===(s=X())||void 0===s?void 0:s.rotation())||0)}:l})]})});export default X;
1
+ import{Fragment as e,jsx as t,jsxs as o}from"react/jsx-runtime";import{createElement as r}from"react";import n from"react";import{observer as i}from"mobx-react-lite";import{ROLES as l}from"../model/store.js";import s from"konva";import{MAIN_TRANSFORMER_NAME as a}from"../utils/px.js";import{TextElement as m}from"./text-element.js";import{ImageElement as d}from"./image-element.js";import{HTMLElement as c}from"./html-element.js";import{RenderTagElement as f}from"./render-tag-element.js";import{LineElement as u}from"./line-element.js";import{VideoElement as p}from"./video-element.js";import{FigureElement as v}from"./figure-element.js";import{GifElement as g}from"./gif-element.js";import{LottieElement as h}from"./lottie-element.js";import{TableElement as x}from"./table-element.js";import{Group as y}from"react-konva";import{Highlighter as b}from"./highlighter.js";import{PlacedNode as j}from"./placed-node.js";import{forEveryChild as w}from"../model/group-model.js";import{flags as E}from"../utils/flags.js";import{blendModeOf as T,isIsolatedGroup as M}from"@polotno/core/page";const R={text:m,image:d,svg:d,line:u,video:p,figure:v,group:i(e=>{var o;const{element:n,store:i}=e,s=n,{children:a}=s,m=n.selectable||i.role===l.ADMIN,d=s.a;return t(j,{as:y,element:s,offsetX:d.offsetX,offsetY:d.offsetY,scaleX:d.scaleX,scaleY:d.scaleY,opacity:d.opacity,isolated:M(s),globalCompositeOperation:null!==(o=T(s))&&void 0!==o?o:"source-over",listening:m,hideInExport:!n.showInExport,id:n.id,clipFunc:d.wipe&&s._restingBbox?C(s._restingBbox,d.wipe):void 0,children:a.map(t=>r(X,Object.assign({},e,{key:t.id,store:i,element:t})))})}),gif:g,lottie:h,table:x};export function registerShapeComponent(e,t){R[e]=t}const S=(e,t)=>{const o=[];w(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 d=n.point({x:i,y:l});return{x:d.x,y:d.y,width:a-i,height:m-l,rotation:t}},I=".polotnoElementHover",O=new WeakMap;const k=1e6,C=(e,t)=>o=>{var r;const n="x"===t.axis?e.width:e.height,i=t.from<=0?-k:t.from*n,l=t.to>=1?k:t.to*n,[a,m,d,c]="x"===t.axis?[i,l,-k,k]:[-k,k,i,l];o.save(),o.translate(e.x,e.y),o.rotate(s.Util.degToRad(null!==(r=e.rotation)&&void 0!==r?r:0)),o.rect(a,d,m-a,c-d),o.restore()},U=(e,t)=>{const o=s.Util.degToRad(e.rotation);return(new s.Transform).translate(e.x,e.y).rotate(o).translate(e.width/2,0).scale(t,1).translate(-e.width/2,0).rotate(-o).translate(-e.x,-e.y).decompose()},X=i(r=>{var i,l,s;const{element:m,store:d}=r,[u,p]=n.useState(!1),v="group"===(null===(i=m.parent)||void 0===i?void 0:i.type),g="group"!==m.type||v?null:d.selectedElements,h=!!g&&1===g.length&&g[0]!==m&&g[0].top===m,[x,j]=n.useState(!1),w=n.useRef(null),M=null!==(s=m.filled.visible)&&void 0!==s?s:m.visible,k=()=>{var e,t;return null!==(t=null===(e=w.current)||void 0===e?void 0:e.getStage())&&void 0!==t?t:null},X=n.useRef(void 0),Y=()=>{var e;return X.current||(X.current=null===(e=k())||void 0===e?void 0:e.findOne(`.${a}`)),X.current};n.useEffect(()=>{const e=k();if(!e){return}const t=function(e,t,o,r){var n;const{subscribers:i}=function(e,t){const o=O.get(e);if(o){return o}const r={subscribers:new Map},n=e=>{var t,o;const n=r.hoveredId;r.hoveredId=e,n&&n!==e&&(null===(t=r.subscribers.get(n))||void 0===t||t.forEach(e=>e(!1))),e&&(null===(o=r.subscribers.get(e))||void 0===o||o.forEach(e=>e(!0)))};return e.on(`mouseover${I}`,e=>{var o,r;const i=e.target.findAncestor(".element",!0);n(null===(r=null===(o=t.getElementById(null==i?void 0:i.id()))||void 0===o?void 0:o.top)||void 0===r?void 0:r.id)}),e.on(`mouseleave${I}`,()=>n(void 0)),O.set(e,r),r}(e,t),l=null!==(n=i.get(o))&&void 0!==n?n:new Set;return i.set(o,l),l.add(r),()=>{l.delete(r),l.size||(i.delete(o),i.size||(e.off(I),O.delete(e)))}}(e,d,m.id,p);return()=>{t(),p(!1)}},[M,m.id]),n.useEffect(()=>{if(!h){return}const e=k(),t=Y();if(!e||!t){return}j(!1);const o=()=>j(!0),r=()=>j(!1);return e.on("dragstart",o),e.on("dragend",r),t.on("transformstart",o),t.on("transformend",r),()=>{e.off("dragstart",o),e.off("dragend",r),t.off("transformstart",o),t.off("transformend",r)}},[M,h]);let B=R[r.element.type];if("text"===r.element.type&&(E.renderTagTextEnabled?B=f:E.htmlRenderEnabled&&(B=c)),!M){return null}if(!B){return console.error("Can not find component for "+r.element.type),null}const F="group"===m.type?void 0:T(m),$="group"===m.type?null:m.a,z=null==$?void 0:$.wipe,A=null==$?void 0:$.flipScale;return o(e,{children:[t(y,Object.assign({name:"element-container",ref:w,visible:m.active,listening:m.active,isolated:!!F,opacity:F?m.a.opacity:1,globalCompositeOperation:null!=F?F:"source-over",clipFunc:z?C($,z):void 0},void 0===A?null:U($,A),{children:t(B,Object.assign({},r))})),((u||h)&&!v||m.isHighlighted)&&m.active&&!m.isSelectedDirectly&&!x&&t(b,{element:"group"===m.type?{a:S(m,h?"rotation"in g[0]?g[0].rotation:0:u&&(null===(l=Y())||void 0===l?void 0:l.rotation())||0)}:m})]})});export default X;
@@ -1 +1 @@
1
- import{Fragment as t,jsx as e,jsxs as n}from"react/jsx-runtime";import r from"react";import{observer as s}from"mobx-react-lite";import{Circle as i,Ellipse as a,Group as o,Line as l,Rect as c}from"react-konva";import{ROLES as d}from"../model/store.js";import{fadeStopColor as u,insertFadeStop as f}from"../utils/fade-gradient.js";import{moveStop as p,safe01 as g}from"../utils/stops.js";import{getFadeStopFocus as v,getSelectedFadeStop as m,setSelectedFadeStop as x}from"../utils/fade-overlay.js";import{useWorkspaceStyle as y}from"./workspace-style.js";import{t as h}from"../utils/l10n.js";import{readClipFade as b,writeClipFade as j}from"../utils/clip-fade.js";import{isAlive as S}from"mobx-state-tree";export const FadeOverlay=s(({store:s,pageId:M})=>{var O,k,E;const w=1===s.selectedElements.length?s.selectedElements[0]:null;let D=!!w;for(let t=w;t;t="group"===(null===(O=t.parent)||void 0===O?void 0:O.type)?t.parent:null){D=D&&t.visible&&t.active}const R=!D||s.role===d.VIEWER||s.isPlaying||(null===(k=null==w?void 0:w.page)||void 0===k?void 0:k.id)!==M||!("clipSrc"in w)||"_cropModeEnabled"in w&&w._cropModeEnabled?null:b(w.clipSrc),F=!!R,P=null==R?void 0:R.fade,A=Math.min(m(s),(null!==(E=null==P?void 0:P.stops.length)&&void 0!==E?E:1)-1),L=t=>{w&&R&&S(w)&&w.set({clipSrc:j(t,R.baseSrc)})},T=t=>{var e,n;x(s,t);const r=v(s);(null==r?void 0:r.elementId)===(null==w?void 0:w.id)&&(null==r?void 0:r.onSelectStop(t))||null===(n=null===(e=W.current)||void 0===e?void 0:e.getStage())||void 0===n||n.container().focus({preventScroll:!0})},{transformerStyle:I}=y(),W=r.useRef(null),z=r.useRef(null),B=r.useRef(!1),V=r.useRef(!1),X=r.useRef(null);if(r.useLayoutEffect(()=>{const t=W.current,e=null==t?void 0:t.getLayer();if(!t||!e||!w){return}const n=w.id,r=()=>{var e;const r=null===(e=t.getStage())||void 0===e?void 0:e.findOne("#"+n),s=t.getParent(),i=!!(null==r?void 0:r.isVisible());if(t.visible(i),!r||!s||!i){return}const a=s.getAbsoluteTransform().copy().invert();a.multiply(r.getAbsoluteTransform()),t.setAttrs(a.decompose())};return r(),e.on("beforeDraw",r),()=>{e.off("beforeDraw",r)}}),r.useLayoutEffect(()=>{var t,e,n;B.current||!F&&!V.current||(V.current=F,z.current=null!==(n=null===(t=W.current)||void 0===t?void 0:t.getLayer())&&void 0!==n?n:z.current,null===(e=z.current)||void 0===e||e.drawHit())}),r.useEffect(()=>{var t,e;if(!F){return}const n=null===(e=null===(t=W.current)||void 0===t?void 0:t.getStage())||void 0===e?void 0:e.container();if(!n){return}const r=n.getAttribute("tabindex");n.tabIndex=-1;const i=t=>{if(document.activeElement!==n||!["Delete","Backspace"].includes(t.key)){return}if(t.preventDefault(),t.stopPropagation(),!w||!S(w)||!("clipSrc"in w)){return}const e=b(w.clipSrc);if(!e||e.fade.stops.length<=2){return}const r=m(s);s.history.transaction(()=>w.set({clipSrc:j(Object.assign(Object.assign({},e.fade),{stops:e.fade.stops.filter((t,e)=>e!==r)}),e.baseSrc)})),x(s,Math.max(0,r-1))};return n.addEventListener("keydown",i),()=>{n.removeEventListener("keydown",i),null===r?n.removeAttribute("tabindex"):n.setAttribute("tabindex",r),x(s,0),n.style.cursor="",n.title="",B.current&&(B.current=!1,s.history.endTransaction())}},[s,null==w?void 0:w.id,F]),!P||!w){return null}const{width:Y,height:_}=w.a;if(Y<=0||_<=0){return null}const H=s.scale||1,q=Math.min(12/H,Y/10,_/10),C=Math.min(5/H,q/2),G=Math.min(24/H,2*q),J=t=>({x:t.x*Y,y:t.y*_}),K=t=>({x:Math.max(q/Y,Math.min(1-q/Y,t.x)),y:Math.max(q/_,Math.min(1-q/_,t.y))}),N=t=>K({x:t.x()/Y,y:t.y()/_}),Q=t=>{var e;const n=((null===(e=W.current)||void 0===e?void 0:e.getAbsoluteRotation())||0)+("y"===t?90:0);return["ew-resize","nwse-resize","ns-resize","nesw-resize"][(Math.round(n/45)%4+4)%4]},U=t=>"function"==typeof t?t():t,Z=(t,e)=>({onMouseEnter:n=>{const r=n.target.getStage().container();r.style.cursor=U(t),r.title=e},onMouseLeave:t=>{if(B.current){return}const e=t.target.getStage().container();e.style.cursor="",e.title=""}}),$=()=>{var t;const e=null===(t=W.current)||void 0===t?void 0:t.getRelativePointerPosition();"radial"===P.shape&&e&&(X.current={position:e,radius:P.radius})},tt=(t,e,n,r="grab")=>Object.assign(Object.assign({draggable:!0,dragDistance:0,stroke:n===A?String(I.anchorStroke):void 0!==n&&P.stops[n].alpha>.5?"#fff":"#222",strokeWidth:2,strokeScaleEnabled:!1,hitStrokeWidth:Math.max(0,G-2*C)},Z(r,t)),{onMouseDown:t=>{t.cancelBubble=!0,$(),void 0!==n&&(t.evt.preventDefault(),T(n))},onTouchStart:t=>{t.cancelBubble=!0,$(),void 0!==n&&(t.evt.preventDefault(),T(n))},onDragStart:t=>{t.target.getStage().container().style.cursor="grab"===r?"grabbing":U(r),B.current=!0,s.history.startTransaction()},onDragMove:t=>e(t.target),onDragEnd:t=>{t.target.getStage().container().style.cursor=U(r),B.current=!1,X.current=null,s.history.endTransaction()}}),et=t=>({fill:u(P.stops[t].alpha)}),nt=[{stroke:"#222"},{stroke:"#fff",dash:[4,4]}],rt=J("linear"===P.shape?P.start:P.center),st=J("linear"===P.shape?P.end:{x:P.center.x-P.radius.x,y:P.center.y}),it=st.x-rt.x,at=st.y-rt.y,ot=t=>({x:rt.x+it*t,y:rt.y+at*t}),lt=t=>g(((t.x-rt.x)*it+(t.y-rt.y)*at)/(it*it+at*at||1)),ct=t=>{var e,n;t.cancelBubble=!0;const r=t.target.getRelativePointerPosition();if(!r){return}const i=f(P.stops,lt(r));null===(n=null===(e=W.current)||void 0===e?void 0:e.getStage())||void 0===n||n.container().focus({preventScroll:!0}),x(s,i.index),s.history.transaction(()=>L(Object.assign(Object.assign({},P),{stops:i.stops})))},dt=n(t,{children:[nt.map((t,n)=>e(l,Object.assign({},t,{listening:1===n,strokeWidth:1.5,name:"fade-axis-guide"},Z("copy",h("toolbar.clipFade.addStop")),{points:[rt.x,rt.y,st.x,st.y],strokeScaleEnabled:!1,hitStrokeWidth:10,onMouseDown:ct,onTouchStart:ct}),n)),P.stops.map((t,n)=>0===t.offset||"linear"===P.shape&&1===t.offset?null:e(i,Object.assign({name:"fade-stop-anchor"},J(K({x:ot(t.offset).x/Y,y:ot(t.offset).y/_})),{radius:C},et(n),tt(h("toolbar.clipFade.stopOpacity"),e=>((t,e)=>{const n=P.stops.findIndex(e=>e.id===t),r=p(P.stops,n,lt(e.position()));e.position(ot(r.stops[r.index].offset)),x(s,r.index),L(Object.assign(Object.assign({},P),{stops:r.stops}))})(t.id,e),n)),t.id))]});let ut;if("linear"===P.shape){const r=(t,e)=>{const n=N(e),r=J("start"===t?P.end:P.start);Math.hypot(n.x*Y-r.x,n.y*_-r.y)<1/H?e.position(J(P[t])):(e.position(J(n)),L(Object.assign(Object.assign({},P),{[t]:n})))};ut=n(t,{children:[dt,e(i,Object.assign({name:"fade-axis-anchor fade-start"},J(K(P.start)),{radius:C},et(0),tt(h("toolbar.clipFade.start"),t=>r("start",t),0))),e(i,Object.assign({name:"fade-axis-anchor fade-end"},J(K(P.end)),{radius:C},et(P.stops.length-1),tt(h("toolbar.clipFade.end"),t=>r("end",t),P.stops.length-1)))]})}else{const r=J(P.center),s=P.radius.x*Y,o=P.radius.y*_,l=t=>{const e=N(t),n={x:Math.max(Math.min(.5,P.radius.x),Math.min(Math.max(.5,1-P.radius.x),e.x)),y:Math.max(Math.min(.5,P.radius.y),Math.min(Math.max(.5,1-P.radius.y),e.y))};t.position(J(n)),L(Object.assign(Object.assign({},P),{center:n}))},d=(t,e)=>{var n;const r="x"===t?Y:_,s=X.current;if(!s){return}const i=null===(n=W.current)||void 0===n?void 0:n.getRelativePointerPosition();if(!i){return}const a=i[t],o=Math.max(.001,Math.min(P.center[t],1-P.center[t])),l=Math.min(o,Math.max(.001,s.radius[t]+(a-s.position[t])/r*("x"===t?1:-1)));e.position(J(K("x"===t?{x:P.center.x+l,y:P.center.y}:{x:P.center.x,y:P.center.y-l}))),L(Object.assign(Object.assign({},P),{radius:Object.assign(Object.assign({},P.radius),{[t]:l})}))};ut=n(t,{children:[nt.map((t,n)=>e(a,Object.assign({},t,{strokeWidth:1.5,x:r.x,y:r.y,radiusX:s,radiusY:o,strokeScaleEnabled:!1,listening:!1}),n)),dt,e(c,Object.assign({name:"fade-center"},J(K(P.center)),{offsetX:C,offsetY:C,width:2*C,height:2*C},et(0),tt(h("toolbar.clipFade.center"),l,0,"move"))),e(i,Object.assign({name:"fade-radius-x"},J(K({x:(r.x+s)/Y,y:P.center.y})),{radius:C,fill:"#fff"},tt(h("toolbar.clipFade.radius"),t=>d("x",t),void 0,()=>Q("x")))),e(i,Object.assign({name:"fade-radius-y"},J(K({x:P.center.x,y:(r.y-o)/_})),{radius:C,fill:"#fff"},tt(h("toolbar.clipFade.radius"),t=>d("y",t),void 0,()=>Q("y"))))]})}return e(o,{ref:W,name:"fade-overlay",hideInExport:!0,children:ut})});
1
+ import{Fragment as e,jsx as t,jsxs as n}from"react/jsx-runtime";import r from"react";import{observer as i}from"mobx-react-lite";import{Circle as s,Ellipse as a,Group as o,Line as l,Rect as c}from"react-konva";import{ROLES as d}from"../model/store.js";import{fadeStopColor as u,insertFadeStop as f}from"../utils/fade-gradient.js";import{moveStop as p,safe01 as g}from"../utils/stops.js";import{getFadeStopFocus as v,getSelectedFadeStop as m,setSelectedFadeStop as x}from"../utils/fade-overlay.js";import{useWorkspaceStyle as y}from"./workspace-style.js";import{t as b}from"../utils/l10n.js";import{readClipFade as h,writeClipFade as j}from"../utils/clip-fade.js";import{isAlive as S}from"mobx-state-tree";export const FadeOverlay=i(({store:i,pageId:M})=>{var O,k,E,w;const D=1===i.selectedElements.length?i.selectedElements[0]:null;let R=!!D;for(let e=D;e;e="group"===(null===(O=e.parent)||void 0===O?void 0:O.type)?e.parent:null){R=R&&(null!==(E=e.filled.visible)&&void 0!==E?E:e.visible)&&e.active}const F=!R||i.role===d.VIEWER||i.isPlaying||(null===(k=null==D?void 0:D.page)||void 0===k?void 0:k.id)!==M||!("clipSrc"in D)||"_cropModeEnabled"in D&&D._cropModeEnabled?null:h(D.clipSrc),P=!!F,A=null==F?void 0:F.fade,L=Math.min(m(i),(null!==(w=null==A?void 0:A.stops.length)&&void 0!==w?w:1)-1),T=e=>{D&&F&&S(D)&&D.set({clipSrc:j(e,F.baseSrc)})},I=e=>{var t,n;x(i,e);const r=v(i);(null==r?void 0:r.elementId)===(null==D?void 0:D.id)&&(null==r?void 0:r.onSelectStop(e))||null===(n=null===(t=z.current)||void 0===t?void 0:t.getStage())||void 0===n||n.container().focus({preventScroll:!0})},{transformerStyle:W}=y(),z=r.useRef(null),B=r.useRef(null),V=r.useRef(!1),X=r.useRef(!1),Y=r.useRef(null);if(r.useLayoutEffect(()=>{const e=z.current,t=null==e?void 0:e.getLayer();if(!e||!t||!D){return}const n=D.id,r=()=>{var t;const r=null===(t=e.getStage())||void 0===t?void 0:t.findOne("#"+n),i=e.getParent(),s=!!(null==r?void 0:r.isVisible());if(e.visible(s),!r||!i||!s){return}const a=i.getAbsoluteTransform().copy().invert();a.multiply(r.getAbsoluteTransform()),e.setAttrs(a.decompose())};return r(),t.on("beforeDraw",r),()=>{t.off("beforeDraw",r)}}),r.useLayoutEffect(()=>{var e,t,n;V.current||!P&&!X.current||(X.current=P,B.current=null!==(n=null===(e=z.current)||void 0===e?void 0:e.getLayer())&&void 0!==n?n:B.current,null===(t=B.current)||void 0===t||t.drawHit())}),r.useEffect(()=>{var e,t;if(!P){return}const n=null===(t=null===(e=z.current)||void 0===e?void 0:e.getStage())||void 0===t?void 0:t.container();if(!n){return}const r=n.getAttribute("tabindex");n.tabIndex=-1;const s=e=>{if(document.activeElement!==n||!["Delete","Backspace"].includes(e.key)){return}if(e.preventDefault(),e.stopPropagation(),!D||!S(D)||!("clipSrc"in D)){return}const t=h(D.clipSrc);if(!t||t.fade.stops.length<=2){return}const r=m(i);i.history.transaction(()=>D.set({clipSrc:j(Object.assign(Object.assign({},t.fade),{stops:t.fade.stops.filter((e,t)=>t!==r)}),t.baseSrc)})),x(i,Math.max(0,r-1))};return n.addEventListener("keydown",s),()=>{n.removeEventListener("keydown",s),null===r?n.removeAttribute("tabindex"):n.setAttribute("tabindex",r),x(i,0),n.style.cursor="",n.title="",V.current&&(V.current=!1,i.history.endTransaction())}},[i,null==D?void 0:D.id,P]),!A||!D){return null}const{width:_,height:H}=D.a;if(_<=0||H<=0){return null}const q=i.scale||1,C=Math.min(12/q,_/10,H/10),G=Math.min(5/q,C/2),J=Math.min(24/q,2*C),K=e=>({x:e.x*_,y:e.y*H}),N=e=>({x:Math.max(C/_,Math.min(1-C/_,e.x)),y:Math.max(C/H,Math.min(1-C/H,e.y))}),Q=e=>N({x:e.x()/_,y:e.y()/H}),U=e=>{var t;const n=((null===(t=z.current)||void 0===t?void 0:t.getAbsoluteRotation())||0)+("y"===e?90:0);return["ew-resize","nwse-resize","ns-resize","nesw-resize"][(Math.round(n/45)%4+4)%4]},Z=e=>"function"==typeof e?e():e,$=(e,t)=>({onMouseEnter:n=>{const r=n.target.getStage().container();r.style.cursor=Z(e),r.title=t},onMouseLeave:e=>{if(V.current){return}const t=e.target.getStage().container();t.style.cursor="",t.title=""}}),ee=()=>{var e;const t=null===(e=z.current)||void 0===e?void 0:e.getRelativePointerPosition();"radial"===A.shape&&t&&(Y.current={position:t,radius:A.radius})},te=(e,t,n,r="grab")=>Object.assign(Object.assign({draggable:!0,dragDistance:0,stroke:n===L?String(W.anchorStroke):void 0!==n&&A.stops[n].alpha>.5?"#fff":"#222",strokeWidth:2,strokeScaleEnabled:!1,hitStrokeWidth:Math.max(0,J-2*G)},$(r,e)),{onMouseDown:e=>{e.cancelBubble=!0,ee(),void 0!==n&&(e.evt.preventDefault(),I(n))},onTouchStart:e=>{e.cancelBubble=!0,ee(),void 0!==n&&(e.evt.preventDefault(),I(n))},onDragStart:e=>{e.target.getStage().container().style.cursor="grab"===r?"grabbing":Z(r),V.current=!0,i.history.startTransaction()},onDragMove:e=>t(e.target),onDragEnd:e=>{e.target.getStage().container().style.cursor=Z(r),V.current=!1,Y.current=null,i.history.endTransaction()}}),ne=e=>({fill:u(A.stops[e].alpha)}),re=[{stroke:"#222"},{stroke:"#fff",dash:[4,4]}],ie=K("linear"===A.shape?A.start:A.center),se=K("linear"===A.shape?A.end:{x:A.center.x-A.radius.x,y:A.center.y}),ae=se.x-ie.x,oe=se.y-ie.y,le=e=>({x:ie.x+ae*e,y:ie.y+oe*e}),ce=e=>g(((e.x-ie.x)*ae+(e.y-ie.y)*oe)/(ae*ae+oe*oe||1)),de=e=>{var t,n;e.cancelBubble=!0;const r=e.target.getRelativePointerPosition();if(!r){return}const s=f(A.stops,ce(r));null===(n=null===(t=z.current)||void 0===t?void 0:t.getStage())||void 0===n||n.container().focus({preventScroll:!0}),x(i,s.index),i.history.transaction(()=>T(Object.assign(Object.assign({},A),{stops:s.stops})))},ue=n(e,{children:[re.map((e,n)=>t(l,Object.assign({},e,{listening:1===n,strokeWidth:1.5,name:"fade-axis-guide"},$("copy",b("toolbar.clipFade.addStop")),{points:[ie.x,ie.y,se.x,se.y],strokeScaleEnabled:!1,hitStrokeWidth:10,onMouseDown:de,onTouchStart:de}),n)),A.stops.map((e,n)=>0===e.offset||"linear"===A.shape&&1===e.offset?null:t(s,Object.assign({name:"fade-stop-anchor"},K(N({x:le(e.offset).x/_,y:le(e.offset).y/H})),{radius:G},ne(n),te(b("toolbar.clipFade.stopOpacity"),t=>((e,t)=>{const n=A.stops.findIndex(t=>t.id===e),r=p(A.stops,n,ce(t.position()));t.position(le(r.stops[r.index].offset)),x(i,r.index),T(Object.assign(Object.assign({},A),{stops:r.stops}))})(e.id,t),n)),e.id))]});let fe;if("linear"===A.shape){const r=(e,t)=>{const n=Q(t),r=K("start"===e?A.end:A.start);Math.hypot(n.x*_-r.x,n.y*H-r.y)<1/q?t.position(K(A[e])):(t.position(K(n)),T(Object.assign(Object.assign({},A),{[e]:n})))};fe=n(e,{children:[ue,t(s,Object.assign({name:"fade-axis-anchor fade-start"},K(N(A.start)),{radius:G},ne(0),te(b("toolbar.clipFade.start"),e=>r("start",e),0))),t(s,Object.assign({name:"fade-axis-anchor fade-end"},K(N(A.end)),{radius:G},ne(A.stops.length-1),te(b("toolbar.clipFade.end"),e=>r("end",e),A.stops.length-1)))]})}else{const r=K(A.center),i=A.radius.x*_,o=A.radius.y*H,l=e=>{const t=Q(e),n={x:Math.max(Math.min(.5,A.radius.x),Math.min(Math.max(.5,1-A.radius.x),t.x)),y:Math.max(Math.min(.5,A.radius.y),Math.min(Math.max(.5,1-A.radius.y),t.y))};e.position(K(n)),T(Object.assign(Object.assign({},A),{center:n}))},d=(e,t)=>{var n;const r="x"===e?_:H,i=Y.current;if(!i){return}const s=null===(n=z.current)||void 0===n?void 0:n.getRelativePointerPosition();if(!s){return}const a=s[e],o=Math.max(.001,Math.min(A.center[e],1-A.center[e])),l=Math.min(o,Math.max(.001,i.radius[e]+(a-i.position[e])/r*("x"===e?1:-1)));t.position(K(N("x"===e?{x:A.center.x+l,y:A.center.y}:{x:A.center.x,y:A.center.y-l}))),T(Object.assign(Object.assign({},A),{radius:Object.assign(Object.assign({},A.radius),{[e]:l})}))};fe=n(e,{children:[re.map((e,n)=>t(a,Object.assign({},e,{strokeWidth:1.5,x:r.x,y:r.y,radiusX:i,radiusY:o,strokeScaleEnabled:!1,listening:!1}),n)),ue,t(c,Object.assign({name:"fade-center"},K(N(A.center)),{offsetX:G,offsetY:G,width:2*G,height:2*G},ne(0),te(b("toolbar.clipFade.center"),l,0,"move"))),t(s,Object.assign({name:"fade-radius-x"},K(N({x:(r.x+i)/_,y:A.center.y})),{radius:G,fill:"#fff"},te(b("toolbar.clipFade.radius"),e=>d("x",e),void 0,()=>U("x")))),t(s,Object.assign({name:"fade-radius-y"},K(N({x:A.center.x,y:(r.y-o)/H})),{radius:G,fill:"#fff"},te(b("toolbar.clipFade.radius"),e=>d("y",e),void 0,()=>U("y"))))]})}return t(o,{ref:z,name:"fade-overlay",hideInExport:!0,children:fe})});
@@ -1 +1 @@
1
- import{Fragment as e,jsx as t,jsxs as r}from"react/jsx-runtime";import a from"react";import{animatedShadow as o}from"./animated-shadow.js";import{observer as s}from"mobx-react-lite";import{Group as i,Rect as n,Shape as h}from"react-konva";import{ROLES as l}from"../model/store.js";import c from"konva";import{getFigureFillClip as d,subTypeToPathDataFunc as m}from"@polotno/core/figure-to-svg";import{isOpaquePaint as g}from"@polotno/core/gradient";import{isTouchDevice as p}from"../utils/screen.js";import{activeAnchorOf as f,isCornerAnchor as u,scaledStyleAttrs as b,roundedStyleAttrs as k,roundedGeometryAttrs as w,roundedPosition as j,transformScale as x}from"../utils/px.js";import{useColor as y}from"./use-color.js";import{PlacedNode as v}from"./placed-node.js";import{elementOpacity as P,paintOpacity as T}from"./paint-opacity.js";const M=(e,t)=>{e.beginPath();for(const{command:r,points:a}of t){switch(r){case"L":e.lineTo(a[0],a[1]);break;case"M":e.moveTo(a[0],a[1]);break;case"C":e.bezierCurveTo(a[0],a[1],a[2],a[3],a[4],a[5]);break;case"Q":e.quadraticCurveTo(a[0],a[1],a[2],a[3]);break;case"A":e.ellipse(a[0],a[1],a[2],a[3],a[6],a[4],a[4]+a[5],!a[7]);break;case"z":e.closePath()}}},O={};export const FigureElement=s(({element:S,store:E})=>{const R=a.useRef(null),D=a.useRef(!1),W=S.selectable||E.role===l.ADMIN,C=p(),F=Math.min(S.strokeWidth,S.width/2,S.height/2),I=y(S,S.fill,"fill"),z=Object.assign(Object.assign(Object.assign({width:S.a.width,height:S.a.height},I),o(S)),{preventDefault:!C||S.isSelected,hideInExport:!S.showInExport}),A=y(S,S.stroke,"stroke"),X=Object.assign(Object.assign({},A),{strokeWidth:2*F,dash:S.dash.map(e=>e*F)}),Y=Math.max(0,10-Math.min(S.a.width,S.a.height)),q=d({subType:S.subType,width:S.a.width,height:S.a.height,cornerRadius:S.cornerRadius,strokeWidth:F,stroke:S.stroke,dash:S.dash,opacity:T(S)}),L=!S.a.shadowEnabled||g(S.fill)?q:null,N=(Q=S.subType,O[Q]||(O[Q]=(B=m(Q),s(({element:o,fillProps:s,strokeProps:i,hitStrokeWidth:l,fillClip:d})=>{const m=B({width:o.a.width,height:o.a.height,cornerRadius:o.cornerRadius}),g=a.useMemo(()=>c.Path.parsePathData(m),[m]),p=a.useMemo(()=>d?c.Path.parsePathData(d):null,[d]),f=!!p;return r(e,{children:[t(n,{width:o.a.width,height:o.a.height,fill:"transparent",hitStrokeWidth:l}),t(h,Object.assign({},s,i,{perfectDrawEnabled:f,shadowForStrokeEnabled:f,selfRectFunc:e=>({x:0,y:0,width:e.width(),height:e.height()}),sceneFunc:(e,t)=>{e.save(),p?(M(e,g),e.clip(),M(e,p),e.clip()):M(e,g),e.fillShape(t),e.restore(),e.save(),M(e,g),e.clip(),e.strokeShape(t),e.restore()}}))]})}))),O[Q]||i);var Q,B;return t(e,{children:t(v,{as:i,nodeRef:R,element:S,id:S.id,opacity:P(S),listening:W,draggable:C?S.draggable&&S.isSelected:S.draggable,name:"element",onDragMove:e=>{S.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{S.set(j(e.target))},onTransformStart:e=>{D.current=u(f(e.target))},onTransform:e=>{const t={x:x(e.target.scaleX()),y:x(e.target.scaleY())};e.target.scaleX(1),e.target.scaleY(1);const r=D.current?Math.min(t.x,t.y):1;S.set(Object.assign({width:S.width*t.x,height:S.height*t.y,x:e.target.x(),y:e.target.y(),rotation:e.target.rotation()},1!==r&&b(S,r)))},onTransformEnd:()=>{S.set(Object.assign(Object.assign({},w(S)),k(S)))},children:t(N,{element:S,fillProps:z,strokeProps:X,hitStrokeWidth:Y,fillClip:L})})})});
1
+ import{Fragment as e,jsx as t,jsxs as r}from"react/jsx-runtime";import a from"react";import{animatedShadow as o}from"./animated-shadow.js";import{observer as s}from"mobx-react-lite";import{Group as i,Rect as n,Shape as h}from"react-konva";import{ROLES as l}from"../model/store.js";import c from"konva";import{getFigureFillClip as d,subTypeToPathDataFunc as m}from"@polotno/core/figure-to-svg";import{isOpaquePaint as g}from"@polotno/core/gradient";import{isTouchDevice as p}from"../utils/screen.js";import{activeAnchorOf as f,isCornerAnchor as u,scaledStyleAttrs as b,roundedStyleAttrs as k,roundedGeometryAttrs as w,roundedPosition as j,transformScale as x}from"../utils/px.js";import{useColor as y}from"./use-color.js";import{PlacedNode as v}from"./placed-node.js";import{elementOpacity as P,paintOpacity as T}from"./paint-opacity.js";const M=(e,t)=>{e.beginPath();for(const{command:r,points:a}of t){switch(r){case"L":e.lineTo(a[0],a[1]);break;case"M":e.moveTo(a[0],a[1]);break;case"C":e.bezierCurveTo(a[0],a[1],a[2],a[3],a[4],a[5]);break;case"Q":e.quadraticCurveTo(a[0],a[1],a[2],a[3]);break;case"A":e.ellipse(a[0],a[1],a[2],a[3],a[6],a[4],a[4]+a[5],!a[7]);break;case"z":e.closePath()}}},O={};export const FigureElement=s(({element:S,store:E})=>{const R=a.useRef(null),D=a.useRef(!1),W=S.selectable||E.role===l.ADMIN,C=p(),F=Math.min(S.strokeWidth,S.width/2,S.height/2),I=y(S,S.a.fill,"fill"),z=Object.assign(Object.assign(Object.assign({width:S.a.width,height:S.a.height},I),o(S)),{preventDefault:!C||S.isSelected,hideInExport:!S.showInExport}),A=y(S,S.stroke,"stroke"),X=Object.assign(Object.assign({},A),{strokeWidth:2*F,dash:S.dash.map(e=>e*F)}),Y=Math.max(0,10-Math.min(S.a.width,S.a.height)),q=d({subType:S.subType,width:S.a.width,height:S.a.height,cornerRadius:S.cornerRadius,strokeWidth:F,stroke:S.stroke,dash:S.dash,opacity:T(S)}),L=!S.a.shadowEnabled||g(S.a.fill)?q:null,N=(Q=S.subType,O[Q]||(O[Q]=(B=m(Q),s(({element:o,fillProps:s,strokeProps:i,hitStrokeWidth:l,fillClip:d})=>{const m=B({width:o.a.width,height:o.a.height,cornerRadius:o.cornerRadius}),g=a.useMemo(()=>c.Path.parsePathData(m),[m]),p=a.useMemo(()=>d?c.Path.parsePathData(d):null,[d]),f=!!p;return r(e,{children:[t(n,{width:o.a.width,height:o.a.height,fill:"transparent",hitStrokeWidth:l}),t(h,Object.assign({},s,i,{perfectDrawEnabled:f,shadowForStrokeEnabled:f,selfRectFunc:e=>({x:0,y:0,width:e.width(),height:e.height()}),sceneFunc:(e,t)=>{e.save(),p?(M(e,g),e.clip(),M(e,p),e.clip()):M(e,g),e.fillShape(t),e.restore(),e.save(),M(e,g),e.clip(),e.strokeShape(t),e.restore()}}))]})}))),O[Q]||i);var Q,B;return t(e,{children:t(v,{as:i,nodeRef:R,element:S,id:S.id,opacity:P(S),listening:W,draggable:C?S.draggable&&S.isSelected:S.draggable,name:"element",onDragMove:e=>{S.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{S.set(j(e.target))},onTransformStart:e=>{D.current=u(f(e.target))},onTransform:e=>{const t={x:x(e.target.scaleX()),y:x(e.target.scaleY())};e.target.scaleX(1),e.target.scaleY(1);const r=D.current?Math.min(t.x,t.y):1;S.set(Object.assign({width:S.width*t.x,height:S.height*t.y,x:e.target.x(),y:e.target.y(),rotation:e.target.rotation()},1!==r&&b(S,r)))},onTransformEnd:()=>{S.set(Object.assign(Object.assign({},w(S)),k(S)))},children:t(N,{element:S,fillProps:z,strokeProps:X,hitStrokeWidth:Y,fillClip:L})})})});
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import { StoreType } from '../model/store.js';
3
- import { VideoElementType } from '../model/video-model.js';
3
+ import { GifElementType } from '../model/gif-model.js';
4
4
  import { ImageElementType } from '../model/image-model.js';
5
5
  import Konva from 'konva';
6
6
  export declare function useRasterPipeline(element: ImageElementType, pixelRatio: number, source: HTMLCanvasElement | undefined, crop: {
@@ -9,12 +9,20 @@ export declare function useRasterPipeline(element: ImageElementType, pixelRatio:
9
9
  width: number;
10
10
  height: number;
11
11
  }, imageRef: React.RefObject<Konva.Image | null>, isTransforming: boolean): readonly [HTMLCanvasElement | HTMLVideoElement | undefined, () => void];
12
- export declare const RasterNode: (({ element, store, nodeRef, image, opacity, onTransformingChange, }: {
12
+ export declare const RasterNode: (({ element, store, nodeRef, image, opacity, crop, source, onTransformingChange, }: {
13
13
  element: ImageElementType;
14
14
  store: StoreType;
15
15
  nodeRef: React.RefObject<Konva.Image | null>;
16
16
  image: HTMLCanvasElement | HTMLVideoElement | undefined;
17
17
  opacity: number;
18
+ crop: {
19
+ width: number;
20
+ height: number;
21
+ };
22
+ source: {
23
+ width: number;
24
+ height: number;
25
+ } | undefined;
18
26
  onTransformingChange: (transforming: boolean) => void;
19
27
  }) => React.JSX.Element) & React.NamedExoticComponent<{
20
28
  element: ImageElementType;
@@ -22,20 +30,36 @@ export declare const RasterNode: (({ element, store, nodeRef, image, opacity, on
22
30
  nodeRef: React.RefObject<Konva.Image | null>;
23
31
  image: HTMLCanvasElement | HTMLVideoElement | undefined;
24
32
  opacity: number;
33
+ crop: {
34
+ width: number;
35
+ height: number;
36
+ };
37
+ source: {
38
+ width: number;
39
+ height: number;
40
+ } | undefined;
25
41
  onTransformingChange: (transforming: boolean) => void;
26
42
  }> & {
27
- readonly type: ({ element, store, nodeRef, image, opacity, onTransformingChange, }: {
43
+ readonly type: ({ element, store, nodeRef, image, opacity, crop, source, onTransformingChange, }: {
28
44
  element: ImageElementType;
29
45
  store: StoreType;
30
46
  nodeRef: React.RefObject<Konva.Image | null>;
31
47
  image: HTMLCanvasElement | HTMLVideoElement | undefined;
32
48
  opacity: number;
49
+ crop: {
50
+ width: number;
51
+ height: number;
52
+ };
53
+ source: {
54
+ width: number;
55
+ height: number;
56
+ } | undefined;
33
57
  onTransformingChange: (transforming: boolean) => void;
34
58
  }) => React.JSX.Element;
35
59
  };
36
60
  type Props = {
37
61
  store: StoreType;
38
- element: VideoElementType;
62
+ element: GifElementType;
39
63
  };
40
64
  export declare const GifElement: (({ element, store }: Props) => React.JSX.Element) & React.NamedExoticComponent<Props> & {
41
65
  readonly type: ({ element, store }: Props) => React.JSX.Element;
@@ -1 +1 @@
1
- import{Fragment as e,jsx as t,jsxs as r}from"react/jsx-runtime";import{elementOpacity as o}from"./paint-opacity.js";import i from"react";import{toDisplayColor as n}from"@polotno/core/color";import{animatedShadow as a}from"./animated-shadow.js";import{observer as s}from"mobx-react-lite";import{isAlive as d}from"mobx-state-tree";import{nodeAutorun as l}from"../utils/mobx.js";import{isHeldByPreview as m}from"../utils/timeline.js";import{Image as c,Rect as h}from"react-konva";import{ROLES as u}from"../model/store.js";import{parseGIF as f,decompressFrames as g}from"gifuct-js";import{useClip as p,useCornerRadiusAndCrop as v,useFlip as w}from"./video-element.js";import{useImageLoader as x}from"./image-element.js";import{fetchBytesWithRetry as y}from"@polotno/core/retry";import{dataUrlToBytes as j}from"@polotno/core/image-bytes";import{ErrorPlaceholder as b,LoadingPlaceholder as R}from"./media-placeholders.js";import{useFilteredSource as E}from"./use-filtered-source.js";import{useOffscreenCanvas as I}from"./use-offscreen-canvas.js";import{useFadeIn as S}from"./use-fadein.js";import{useAnimatedBlur as T}from"./use-animated-blur.js";import{isTouchDevice as z}from"../utils/screen.js";import{activeAnchorOf as C,isCornerAnchor as D,scaledStyleAttrs as M,roundedStyleAttrs as O,roundedGeometryAttrs as k,roundedPosition as F,transformScale as L}from"../utils/px.js";import{useEvent as P}from"../hooks/use-event.js";import{PlacedNode as X}from"./placed-node.js";import{refCountedCache as Y}from"../utils/ref-counted.js";async function _(e){var t;const r=null!==(t=j(e))&&void 0!==t?t:await y(e),o=f(new Uint8Array(r).buffer),i=g(o,!0).map(e=>({patch:new Uint8ClampedArray(e.patch),delay:e.delay,width:e.dims.width,height:e.dims.height,left:e.dims.left,top:e.dims.top,disposalType:e.disposalType}));return{frames:i,duration:i.reduce((e,t)=>e+t.delay,0),dimensions:{width:o.lsd.width,height:o.lsd.height}}}const A=Y(),G=[],N={width:0,height:0};function U(e,t,r){const o=t.getContext("2d"),i=r.getContext("2d");if(!o||!i){return}2===e.disposalType&&o.clearRect(0,0,t.width,t.height),r.width=e.width,r.height=e.height;const n=i.createImageData(e.width,e.height);n.data.set(e.patch),i.putImageData(n,0,0),o.drawImage(r,e.left,e.top)}export function useRasterPipeline(e,t,r,o,n,a){var s;const l=null!==(s=e.cornerRadius)&&void 0!==s?s:0,[m,c]=w(e,r),[h,u]=E(e,m),[f,g]=v(e,h,o,t,l,a||e._cropModeEnabled),[x,y]=p(e,f,t,[o,l]),j=T(n,e,x),b=P(()=>{d(e)&&(c(),u(),g(),y(),j())});return i.useLayoutEffect(()=>{var e,t;null===(t=null===(e=n.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()}),[x,b]}export const RasterNode=s(({element:o,store:s,nodeRef:d,image:l,opacity:m,onTransformingChange:f})=>{var g;const p=i.useRef(!1),v=z();S(d,m);const w=o.selectable||s.role===u.ADMIN,x=null!==(g=o.cornerRadius)&&void 0!==g?g:0;return r(e,{children:[t(X,Object.assign({as:c,nodeRef:d,element:o,name:"element",id:o.id,image:l,width:o.a.width||1,height:o.a.height||1,opacity:m},a(o),{listening:w,draggable:v?o.draggable&&o.isSelected:o.draggable,preventDefault:!v||o.isSelected,hideInExport:!o.showInExport,onDragMove:e=>{o.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{o.set(F(e.target))},onTransformStart:e=>{f(!0),p.current=D(C(e.target))},onTransform:e=>{const t=e.currentTarget,r=L(t.scaleX()),i=L(t.scaleY());t.scaleX(1),t.scaleY(1);const n=p.current?Math.min(r,i):1;o.set(Object.assign({x:t.x(),y:t.y(),width:t.width()*r,height:t.height()*i,rotation:t.rotation()},1!==n&&M(o,n)))},onTransformEnd:()=>{o.set(Object.assign(Object.assign({},k(o)),O(o))),f(!1)}})),!!o.borderSize&&t(X,{as:h,element:o,width:Math.max(o.a.width-o.borderSize,0),height:Math.max(o.a.height-o.borderSize,0),opacity:m,offsetX:-o.borderSize/2,offsetY:-o.borderSize/2,stroke:n(o.borderColor),strokeWidth:o.borderSize,listening:!1,cornerRadius:Math.max(0,x-o.borderSize),hideInExport:!o.showInExport})]})});export const GifElement=s(({element:n,store:a})=>{const[s,d]=i.useState(!1),c=i.useRef(null),[h,u,f,g]=function(e){var t,r,o,n;const[a,s]=i.useState(()=>{var t;return{url:e,gif:null===(t=A.get(e))||void 0===t?void 0:t.value}});i.useEffect(()=>{const[t,r]=function(e){const[t,r]=A.acquire(e,()=>{const t={decoded:_(e)};return t.decoded.then(e=>{t.value=e},()=>A.remove(e,t)),t});return[t.decoded,r]}(e);let o=!0;return t.then(t=>{o&&s({url:e,gif:t})},t=>{o&&(console.error("Failed to load GIF:",t),s({url:e,failed:!0}))}),()=>{o=!1,r()}},[e]);const{gif:d,failed:l}=a.url===e?a:{gif:null===(t=A.get(e))||void 0===t?void 0:t.value,failed:!1},m=d?"loaded":l?"failed":"loading";return[null!==(r=null==d?void 0:d.frames)&&void 0!==r?r:G,null!==(o=null==d?void 0:d.duration)&&void 0!==o?o:0,null!==(n=null==d?void 0:d.dimensions)&&void 0!==n?n:N,m]}(n.src);x(g,n.src,n.id);const p=e=>{e.width=f.width,e.height=f.height,U(h[0],e,document.createElement("canvas"))},v=I(h.length>0,p,[h,f]);i.useEffect(()=>{if(!v){return}const e=document.createElement("canvas");p(v),z();let t=0;const r=r=>{const o=(e=>{const t=e%u;let r=0;for(let o=0;o<h.length;o++){if(r+=h[o].delay,r>t){return o}}return 0})(r);o!==t&&(U(h[o],v,e),z(),c.current.getLayer().draw(),t=o)};let o=0;const i=l(n,()=>{if(m(n)){return}clearInterval(o);const e=n.page._mediaLocalTime;null!==e?r(e):a.isPlaying||n.page._exportingOrRendering?r(0):o=window.setInterval(()=>r(performance.now()),16)});return()=>{i(),clearInterval(o)}},[h,u]),i.useEffect(()=>{u&&a.history.ignore(()=>{n.set({duration:u})})},[u]);let{cropX:w,cropY:y,cropWidth:j,cropHeight:E}=n.a;"loaded"!==g&&(w=y=0,j=E=1);const S={x:f.width*w,y:f.height*y,width:f.width*j,height:f.height*E},[T,z]=useRasterPipeline(n,a._elementsPixelRatio,v,S,c,s),C="loading"===g,D="failed"===g,M=C||D?0:o(n);return r(e,{children:[C&&t(R,{element:n}),D&&t(b,{element:n,text:"Cannot load the GIF..."}),t(RasterNode,{element:n,store:a,nodeRef:c,image:T,opacity:M,onTransformingChange:d})]})});
1
+ import{Fragment as e,jsx as t,jsxs as r}from"react/jsx-runtime";import{elementOpacity as o}from"./paint-opacity.js";import i from"react";import{toDisplayColor as n}from"@polotno/core/color";import{animatedShadow as a}from"./animated-shadow.js";import{observer as s}from"mobx-react-lite";import{isAlive as d}from"mobx-state-tree";import{nodeAutorun as l}from"../utils/mobx.js";import{getVideoRestMs as m,isHeldByPreview as c}from"../utils/timeline.js";import{getCropRect as f}from"@polotno/core/image";import{Image as u,Rect as h}from"react-konva";import{ROLES as p}from"../model/store.js";import{parseGIF as g,decompressFrames as v}from"gifuct-js";import{useClip as w,useCornerRadiusAndCrop as y,useFlip as x}from"./video-element.js";import{useImageLoader as b}from"./image-element.js";import{fetchBytesWithRetry as j}from"@polotno/core/retry";import{dataUrlToBytes as E}from"@polotno/core/image-bytes";import{ErrorPlaceholder as R,LoadingPlaceholder as I}from"./media-placeholders.js";import{useFilteredSource as S}from"./use-filtered-source.js";import{useOffscreenCanvas as z}from"./use-offscreen-canvas.js";import{useFadeIn as M}from"./use-fadein.js";import{useAnimatedBlur as C}from"./use-animated-blur.js";import{isTouchDevice as D}from"../utils/screen.js";import{roundedPosition as T}from"../utils/px.js";import{useMediaResize as _}from"./media-resize.js";import{useEvent as F}from"../hooks/use-event.js";import{PlacedNode as L}from"./placed-node.js";import{refCountedCache as k}from"../utils/ref-counted.js";async function P(e){var t;const r=null!==(t=E(e))&&void 0!==t?t:await j(e),o=g(new Uint8Array(r).buffer),i=v(o,!0).map(e=>({patch:new Uint8ClampedArray(e.patch),delay:e.delay,width:e.dims.width,height:e.dims.height,left:e.dims.left,top:e.dims.top,disposalType:e.disposalType}));return{frames:i,duration:i.reduce((e,t)=>e+t.delay,0),dimensions:{width:o.lsd.width,height:o.lsd.height}}}const A=k(),G=[],N={width:0,height:0};function W(e,t,r){const o=t.getContext("2d"),i=r.getContext("2d");if(!o||!i){return}2===e.disposalType&&o.clearRect(0,0,t.width,t.height),r.width=e.width,r.height=e.height;const n=i.createImageData(e.width,e.height);n.data.set(e.patch),i.putImageData(n,0,0),o.drawImage(r,e.left,e.top)}export function useRasterPipeline(e,t,r,o,n,a){var s;const l=null!==(s=e.cornerRadius)&&void 0!==s?s:0,[m,c]=x(e,r),[f,u]=S(e,m),[h,p]=y(e,f,o,t,l,a||e._cropModeEnabled),[g,v]=w(e,h,t,[o,l]),b=C(n,e,g),j=F(()=>{d(e)&&(c(),u(),p(),v(),b())});return i.useLayoutEffect(()=>{var e,t;null===(t=null===(e=n.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()}),[g,j]}export const RasterNode=s(({element:o,store:i,nodeRef:s,image:d,opacity:l,crop:m,source:c,onTransformingChange:f})=>{var g;const v=_(o,m,c,f),w=D();M(s,l);const y=o.selectable||i.role===p.ADMIN,x=null!==(g=o.cornerRadius)&&void 0!==g?g:0;return r(e,{children:[t(L,Object.assign({as:u,nodeRef:s,element:o,name:"element",id:o.id,image:d,width:o.a.width||1,height:o.a.height||1,opacity:l},a(o),{listening:y,draggable:w?o.draggable&&o.isSelected:o.draggable,preventDefault:!w||o.isSelected,hideInExport:!o.showInExport,onDragMove:e=>{o.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{o.set(T(e.target))}},v)),!!o.borderSize&&t(L,{as:h,element:o,width:Math.max(o.a.width-o.borderSize,0),height:Math.max(o.a.height-o.borderSize,0),opacity:l,offsetX:-o.borderSize/2,offsetY:-o.borderSize/2,stroke:n(o.borderColor),strokeWidth:o.borderSize,listening:!1,cornerRadius:Math.max(0,x-o.borderSize),hideInExport:!o.showInExport})]})});export const GifElement=s(({element:n,store:a})=>{const[s,d]=i.useState(!1),u=i.useRef(null),[h,p,g,v]=function(e){var t,r,o,n;const[a,s]=i.useState(()=>{var t;return{url:e,gif:null===(t=A.get(e))||void 0===t?void 0:t.value}});i.useEffect(()=>{const[t,r]=function(e){const[t,r]=A.acquire(e,()=>{const t={decoded:P(e)};return t.decoded.then(e=>{t.value=e},()=>A.remove(e,t)),t});return[t.decoded,r]}(e);let o=!0;return t.then(t=>{o&&s({url:e,gif:t})},t=>{o&&(console.error("Failed to load GIF:",t),s({url:e,failed:!0}))}),()=>{o=!1,r()}},[e]);const{gif:d,failed:l}=a.url===e?a:{gif:null===(t=A.get(e))||void 0===t?void 0:t.value,failed:!1},m=d?"loaded":l?"failed":"loading";return[null!==(r=null==d?void 0:d.frames)&&void 0!==r?r:G,null!==(o=null==d?void 0:d.duration)&&void 0!==o?o:0,null!==(n=null==d?void 0:d.dimensions)&&void 0!==n?n:N,m]}(n.a.src);b(v,n.a.src,n.id),i.useLayoutEffect(()=>{"loaded"===v&&n._setSourceSize(g,n.a.src)},[n,n.a.src,v,g]);const w=e=>{e.width=g.width,e.height=g.height,W(h[0],e,document.createElement("canvas"))},y=z(h.length>0,w,[h,g]);i.useEffect(()=>{if(!y){return}const e=document.createElement("canvas");w(y),D();let t=0;const r=r=>{const o=(e=>{let t=0;for(let r=0;r<h.length;r++){if(t+=h[r].delay,t>e){return r}}return 0})(r);o!==t&&(W(h[o],y,e),D(),u.current.getLayer().draw(),t=o)};let o=0;const i=l(n,()=>{if(c(n)){return}clearInterval(o);const e=n.page._mediaLocalTime;if(null!==e){const{start:t,end:o}=n.timeRange;r(m(e,0,t,p,0,1,{windowEnd:o,frameMs:a._renderFrameMs,renderEnd:a._renderEndMs-n.page.startTime}))}else{a.isPlaying||n.page._exportingOrRendering?r(0):o=window.setInterval(()=>r(performance.now()%p),16)}});return()=>{i(),clearInterval(o)}},[h,p]),i.useEffect(()=>{p&&a.history.ignore(()=>{n.set({duration:p})})},[p]);let{cropX:x,cropY:j,cropWidth:E,cropHeight:S}=n.a;"loaded"!==v&&(x=j=0,E=S=1);const M=f({fit:n.a.fit,focus:n.focus,stretchEnabled:n.stretchEnabled,cropX:x,cropY:j,cropWidth:E,cropHeight:S,width:n.a.width,height:n.a.height},g),[C,D]=useRasterPipeline(n,a._elementsPixelRatio,y,M,u,s),T="loading"===v,_="failed"===v,F=T||_?0:o(n);return r(e,{children:[T&&t(I,{element:n}),_&&t(R,{element:n,text:"Cannot load the GIF..."}),t(RasterNode,{element:n,store:a,nodeRef:u,image:C,opacity:F,crop:M,source:"loaded"===v?g:void 0,onTransformingChange:d})]})});
@@ -1 +1 @@
1
- import{Fragment as e,jsx as t,jsxs as r}from"react/jsx-runtime";import o from"react";import{toDisplayColor as i}from"@polotno/core/color";import{animatedShadow as n}from"./animated-shadow.js";import{observer as a}from"mobx-react-lite";import{isAlive as h}from"mobx-state-tree";import{Image as s,Rect as c}from"react-konva";import{ROLES as d}from"../model/store.js";import l from"./use-image.js";import g from"konva";import{ErrorPlaceholder as u,LoadingPlaceholder as m}from"./media-placeholders.js";import{incrementLoader as p,triggerLoadError as f}from"../utils/loader.js";import{polotnoError as v}from"@polotno/core/errors";import{getBorderInset as w,getCropRect as x,mayBeUnsizedSvg 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{activeAnchorOf as I,roundedCropFraction as C,roundedGeometryPx as R,roundedPosition as j,isCornerAnchor as W,scaledStyleAttrs as H,roundedStyleAttrs as X,transformScale as Y}from"../utils/px.js";import{useFilteredSource as z}from"./use-filtered-source.js";import{useOffscreenCanvas as T}from"./use-offscreen-canvas.js";import{useFadeIn as D}from"./use-fadein.js";import{useAnimatedBlur as O}from"./use-animated-blur.js";import{PlacedNode as k}from"./placed-node.js";import{isTouchDevice as A}from"../utils/screen.js";import{useDelayer as L}from"./use-delayer.js";import{useColor as _}from"./use-color.js";import{commitCropSource as P,cropDraftWriter as N,CropOverlay as U}from"./crop-overlay.js";import{ImageToolControls as F}from"./image-tool-controls.js";import{drawReduced as G,exportKernelEnabled as $,shouldResample as q}from"./downscale.js";import{alignToPixelGrid as J,coversWholeSource as Q,displayRatio as V,maskCompositeSize as B,MAX_ELEMENT_BUFFER as K,svgSourceRatio as Z}from"@polotno/core/image-buffer";export{B as maskCompositeSize,Z as svgSourceRatio};const ee=(e,t,r)=>V(e.a,t,r),te=new Map,re=new Map;let oe=0;const ie=e=>{const t=te.get(e);return void 0!==t&&(te.delete(e),te.set(e,t)),t},ne=(e,t)=>{const r=e.length+t.length;if(!(r>4e6||te.has(e))){te.set(e,t),oe+=r;for(const[e,t]of te){if(oe<=16e6){break}te.delete(e),oe-=e.length+t.length}}},ae=(e,t)=>e+"\n"+JSON.stringify((null==t?void 0:t.size)?[...t]:[]),he=new Map,se=new Map,ce=e=>S.isSvgUrl(e)||!0===he.get(e),de=e=>"svg"===e.type||ce(e.src||""),le=(e,t,r,i)=>{const[n,a]=o.useState(""),h=!t&&!!r&&"loaded"===i&&!e.startsWith("data:")&&!he.has(e)&&n!==e&&M(r);return o.useEffect(()=>{if(!h){return}let t=!0;return(e=>{let t=se.get(e);return t||(t=S.fetchSvgText(e).then(t=>{null!==t&&ne(ae(e),S.prepareSvgSource(t)),he.set(e,null!==t)}).catch(()=>{}).finally(()=>se.delete(e)),se.set(e,t)),t})(e).then(()=>{t&&a(e)}),()=>{t=!1}},[h,e]),h?"loading":i},ge=(e,t,r)=>{const[,i]=o.useReducer(e=>e+1,0),n=t&&e?ae(e,r):"",a=o.useRef("loading"),h=o.useRef(""),s=o.useRef(n);if(s.current!==n&&(s.current=n,a.current="loading"),n&&"loading"===a.current){const e=ie(n);void 0!==e&&(h.current=e,a.current="loaded")}return o.useEffect(()=>{if(!n||"loaded"===a.current){return}let t=!1;return((e,t)=>{const r=ae(e,t),o=ie(r);if(void 0!==o){return Promise.resolve(o)}let i=re.get(r);if(!i){const o=(null==t?void 0:t.size)?new Map(t):null;i=S.urlToString(e).then(e=>S.prepareSvgSource(e,o)),re.set(r,i),i.then(e=>ne(r,e),()=>{}).finally(()=>re.delete(r))}return i})(e,r).then(e=>{t||(h.current=e,a.current="loaded",i())},()=>{t||(h.current="",a.current="failed",i())}),()=>{t=!0}},[n]),n?[h.current,a.current]:[e,"loaded"]};export const useSizeFixer=e=>ge(e,ce(e))[0]||e;function ue(e,t){return{x:(e.x+t.x)/2,y:(e.y+t.y)/2}}const me=e=>{if(e.flipX||e.flipY||e.maskSrc){return!0}const t=navigator.userAgent.toLowerCase();return(t.indexOf("firefox")>-1||/^((?!chrome|android).)*safari/i.test(t))&&de(e)},pe=(e,t,r)=>Math.max(t,Math.min(r,e));export const useCornerRadiusAndCrop=(e,t,r,i,n=0,a=!1,h=!0,s=0,c)=>{var d;const l=K,u=K,m=c.aligned?pe(c.deviceWidth,1,l):Math.floor(pe(e.a.width*i,1,l)),p=c.aligned?pe(c.deviceHeight,1,u):Math.floor(pe(e.a.height*i,1,u)),f=Math.min(n*i,m/2,p/2),v=Math.min(s*i,m/2,p/2),w=ee(e,r,i),x=$(e.page)&&!a&&(q(w,m,p)||c.aligned),M=Q(r,t),y=o.useMemo(()=>{if(t&&t.width&&t.height){return!M||0!==f||x||v?document.createElement("canvas"):void 0}},[t,f,x,M,v]);return o.useLayoutEffect(()=>{var o;if(!y||!t){return}E(y,m,p);const i=y.getContext("2d");if(i){if((null===(o=e.page)||void 0===o?void 0:o._exportingOrRendering)&&(i.imageSmoothingQuality="high"),(f||v)&&(i.beginPath(),i.roundRect(v,v,m-2*v,p-2*v,Math.max(0,f-v)),i.clip()),x){const e=y.width===m&&y.height===p&&c.aligned;return void G(i,t,r,y.width,y.height,{x:e?c.phaseX:0,y:e?c.phaseY:0,width:e?c.spanX:y.width,height:e?c.spanY:y.height})}i.drawImage(t,r.x,r.y,r.width,r.height,0,0,y.width,y.height)}},[y,e.a.width,e.a.height,r.x,r.y,r.width,r.height,n,i,a,null===(d=e.page)||void 0===d?void 0:d._exportingOrRendering,x,c.phaseX,c.phaseY,v]),o.useEffect(()=>()=>{y&&"CANVAS"===y.nodeName&&g.Util.releaseCanvas(y)},[y]),y||t};const fe=document.createElement("canvas");let ve=l;export const setImageLoaderHook=e=>{ve=e};export const useCanvasImage=(e,t)=>ve(e,t);export const useSizedImage=e=>{const[t,r]=ge(e,ce(e)),[o,i]=useCanvasImage(t,"anonymous"),n=le(e,ce(e),o,i);return[o,"loaded"===r?n:r]};export const useClipImage=e=>{const[t,r]=useSizedImage(e),i=o.useRef(void 0);return e&&"failed"!==r?t&&(i.current=t):i.current=void 0,[e&&"failed"!==r?t||i.current:void 0,r]};export const useImageLoader=(e,t="",r="")=>{const i=o.useRef(void 0),n=()=>{var e;null===(e=i.current)||void 0===e||e.call(i),i.current=void 0};o.useEffect(()=>n,[]),o.useLayoutEffect(()=>{const o=t.slice(0,200),a=`image with id ${r} url: ${o}`;"loading"!==e||i.current||(i.current=p(a)),"loading"!==e&&n(),"failed"===e&&f(v("IMAGE_FAILED",a,{src:o,elementId:r,reason:"load"}))},[e])};export const ImageElement=a(({element:a,store:l})=>{var p,f,v,M,S;const[G,q]=o.useState(!1),V=o.useRef(null),K=A(),[te,re]=ge(a.src,de(a),a.colorsReplace),[oe,ie]=ve(te,"anonymous"),ne="loaded"===re?ie:re,ae=le(a.src,de(a),oe,ne);useImageLoader(ae,a.src,a.id);const he=(null===(p=a.page)||void 0===p?void 0:p._exportingOrRendering)||!me(a)?1:Math.max(1,l.scale),se=l._elementsPixelRatio*he,ce=(({image:e,status:t})=>{const r=o.useRef(void 0);return o.useEffect(()=>{r.current=e||r.current},[e]),"failed"!==t?r.current:void 0})({image:oe,status:ae}),pe=((e,t,r)=>{const o=de(e)&&t?Z({cropWidth:e.a.cropWidth,cropHeight:e.a.cropHeight,width:e.a.width,height:e.a.height},t,r):1,{flipX:i,flipY:n}=e,a=me(e),h=T(a&&!!(t&&t.width&&t.height),e=>{var r,a;if(!t){return}E(e,Math.max(t.width*o,1),Math.max(t.height*o,1));let h=i?-e.width:0,s=n?-e.height:0;null===(r=e.getContext("2d"))||void 0===r||r.scale(i?-1:1,n?-1:1),null===(a=e.getContext("2d"))||void 0===a||a.drawImage(t,h,s,e.width,e.height)},[i,n,t,o]);return a?h:t})(a,oe||ce,se),we=((e,t)=>{const[r,i]=useSizedImage(e.maskSrc||""),n=e.maskSrc?i:"loaded";return useImageLoader(n,e.maskSrc||"",e.id),o.useMemo(()=>{if(!r){return t}if(!t||!t.width||!t.height){return t}const{width:o,height:i}=B(t,{width:e.a.width,height:e.a.height,stretchEnabled:e.stretchEnabled}),n=document.createElement("canvas");n.width=o,n.height=i;const a=n.getContext("2d");if(!a){return t}a.drawImage(t,0,0,o,i,0,0,o,i),a.globalCompositeOperation="source-in";const h=function(e,t){const r=t.width/t.height;let o,i;return r>=e.width/e.height?(o=e.width,i=e.width/r):(o=e.height*r,i=e.height),{x:(e.width-o)/2,y:(e.height-i)/2,width:o,height:i}}(r,e);return a.drawImage(r,h.x,h.y,h.width,h.height,0,0,o,i),n},[t,r,e.a.width,e.a.height,e.stretchEnabled])})(a,pe)||fe,[xe=fe]=z(a,we);o.useLayoutEffect(()=>{"loaded"===ae&&a._setSourceSize({width:xe.width,height:xe.height},a.src)},[a,a.src,ae,xe.width,xe.height]);let{cropX:Me,cropY:ye,cropWidth:be,cropHeight:Se}=a.a;"loaded"!==ie&&(Me=ye=0,be=Se=1);const Ee=x({stretchEnabled:a.stretchEnabled,cropX:Me,cropY:ye,cropWidth:be,cropHeight:Se,width:a.a.width,height:a.a.height},xe),Ie=Ee.width,Ce=Ee.height,Re=a.stretchEnabled,je=null!==(S=a.cornerRadius)&&void 0!==S?S:0,We=w({borderSize:a.borderSize,borderColor:i(a.borderColor),width:a.a.width,height:a.a.height,opacity:b(a)}),He=!(!je&&!We&&!a.clipSrc&&Q(Ee,xe)),Xe=(null===(f=a.page)||void 0===f?void 0:f._exportingOrRendering)||!He?1:Math.min(2,l.scale),Ye=l._elementsPixelRatio*Xe,ze=G||a._cropModeEnabled||"svg"===a.type,Te=$(a.page)&&0===a.a.rotation&&"group"!==(null===(v=a.parent)||void 0===v?void 0:v.type)&&!ze,De=((e,t,r,o)=>J(e.a,t,r,o))(a,Ye,ee(a,Ee,Ye),Te);let Oe=((e,t,r,i)=>{const[n,a]=useClipImage(e.clipSrc||""),h=e.clipSrc?a:"loaded";useImageLoader(h,e.clipSrc,e.id);const s=o.useMemo(()=>{if(t&&n){return document.createElement("canvas")}},[t,n]);return o.useLayoutEffect(()=>{var o;if(!n){return}if(!t||!t.width||!t.height){return}if(!n||!n.width||!n.height){return}if(!s){return}const i=document.createElement("canvas"),a=Math.max(e.a.width/n.width*r,e.a.height/n.height*r);E(i,Math.min(1e4,Math.max(n.width*a,1)),Math.min(1e4,Math.max(n.height*a,1))),null===(o=i.getContext("2d"))||void 0===o||o.drawImage(n,0,0,i.width,i.height),E(s,Math.min(1e4,Math.max(t.width,Math.ceil(e.a.width*r),1)),Math.min(1e4,Math.max(t.height,Math.ceil(e.a.height*r),1)));const h=s.getContext("2d");h&&(h.save(),h.drawImage(i,0,0,s.width,s.height),g.Util.releaseCanvas(i),h.globalCompositeOperation="source-in",h.drawImage(t,0,0,s.width,s.height),h.restore())},[s,t,n,e.a.width,e.a.height,r,...i]),o.useEffect(()=>()=>{s&&g.Util.releaseCanvas(s)},[s]),e.clipSrc&&n?s:t})(a,useCornerRadiusAndCrop(a,xe,Ee,Ye,je,ze,!0,We,De),Ye,[Ee,je,Ye]);var ke=o.useRef(null),Ae=o.useRef(0),Le=o.useRef(!1);const _e=()=>{var e;a._cropEnabled&&!a.pendingEdit&&(null===(e=l.activeTool)||void 0===e?void 0:e.elementId)!==a.id&&setTimeout(()=>{h(a)&&a.toggleCropMode(!0)})},Pe=de(a)&&ce,Ne="loading"===ne&&!Pe,[Ue]=L(Ne,100,!1,!1),Fe="failed"===ae,Ge=!Ue&&!Fe,$e=o.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),qe=o.useRef(!1),Je=Ge?y(a):0;D(V,Je),O(V,a,Oe);const Qe=a.selectable||l.role===d.ADMIN,Ve=_(a,a.borderColor,"stroke");return r(e,{children:[Ue&&t(m,{element:a}),Fe&&t(u,{element:a,text:"Can not load the image..."}),t(k,Object.assign({as:s,nodeRef:V,element:a,name:"element",id:a.id,image:Oe},De.position,{width:De.width||1,height:De.height||1,opacity:Je},n(a),{customCrop:Ee,listening:Qe,draggable:!a.pendingEdit&&(null===(M=l.activeTool)||void 0===M?void 0:M.elementId)!==a.id&&(K?a.draggable&&a.isSelected:a.draggable),preventDefault:!K||a.isSelected,hideInExport:!a.showInExport,onDragMove:e=>{a.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{a.set(j(e.target))},onDblClick:_e,onDblTap:_e,onTransformStart:e=>{q(!0),qe.current=W(I(e.target)),$e.current={cropX:a.cropX,cropY:a.cropY,cropWidth:a.cropWidth,cropHeight:a.cropHeight}},onTransform:e=>{const t=e.currentTarget,r=Y(t.scaleX()),o=Y(t.scaleY());t.scaleX(1),t.scaleY(1);const i=1-Ie/xe.width,n=Math.min(i,Math.max(0,a.cropX)),h=1-Ce/xe.height,s=Math.min(h,Math.max(0,a.cropY)),c=qe.current,d=!c&&r<1&&$e.current.cropHeight>Ce/xe.height;let l=c?a.cropWidth:a.cropWidth*r;d&&(l=a.cropWidth);const g=!c&&o<1&&$e.current.cropWidth>Ie/xe.width;let u=c?a.cropHeight:a.cropHeight*o;g&&(u=a.cropHeight),Re&&(l=a.cropWidth,u=a.cropHeight);const m=c?Math.min(r,o):1;a.set(Object.assign({cropX:n,cropY:s,x:t.x(),y:t.y(),width:t.width()*r,height:t.height()*o,rotation:e.target.rotation(),cropWidth:Math.min(l,1-n),cropHeight:Math.min(u,1-s)},1!==m&&H(a,m)))},onTransformEnd:e=>{const t=e.currentTarget;a.set(Object.assign({width:R(t.width()),height:R(t.height()),x:R(t.x()),y:R(t.y()),rotation:R(e.target.rotation()),cropX:C(a.cropX),cropY:C(a.cropY),cropWidth:C(Ie/xe.width),cropHeight:C(Ce/xe.height)},X(a))),q(!1)}})),!!a.borderSize&&t(k,Object.assign({as:c,element:a},De.position,{width:Math.max(De.width-a.borderSize,0),height:Math.max(De.height-a.borderSize,0),opacity:Je,offsetX:-a.borderSize/2,offsetY:-a.borderSize/2},Ve,{strokeWidth:a.borderSize,listening:!1,cornerRadius:Math.max(0,je-a.borderSize),hideInExport:!a.showInExport})),a._cropModeEnabled&&"loaded"===ae&&t(U,{element:a,store:l,previewImage:Oe,sourceImage:xe,sourceWidth:xe.width,sourceHeight:xe.height,cropAbsoluteWidth:Ie,cropAbsoluteHeight:Ce,onSourceTouchMove:e=>{e.evt.preventDefault();const t=e.target.getStage().getPointersPositions();var r=t[0],o=t[1];const i=e.target;if(r&&!o&&!i.isDragging()&&Le.current&&(i.startDrag(),Le.current=!1),r&&o){g.hitOnDragEnabled=!0,i.isDragging()&&(Le.current=!0,i.stopDrag());const t=e.target.getAbsoluteTransform().copy();t.invert();var n={x:r.x,y:r.y},h={x:o.x,y:o.y};if(!ke.current){return void(ke.current=ue(n,h))}var s=ue(n,h),c=function(e,t){return Math.sqrt(Math.pow(t.x-e.x,2)+Math.pow(t.y-e.y,2))}(n,h);Ae.current||(Ae.current=c);const v=i.position();var d={x:s.x-v.x,y:s.y-v.y},u=c/Ae.current;i.scaleX(u),i.scaleY(u);const w=t.point(s),x=t.point(ke.current);var m=w.x-x.x,p=w.y-x.y,f={x:Math.min(0,s.x-d.x*u+m),y:Math.min(0,s.y-d.y*u+p)};i.position(f),Ae.current=c,ke.current=s,(e=>{P(e,a,N(a,l),xe.width,xe.height,Ie,Ce)})(e)}},onSourceTouchEnd:e=>{Ae.current=0,ke.current=null,g.hitOnDragEnabled=!1}}),"image"===a.type&&t(F,{element:a,store:l})]})});
1
+ import{Fragment as e,jsx as t,jsxs as r}from"react/jsx-runtime";import o from"react";import{toDisplayColor as i}from"@polotno/core/color";import{animatedShadow as a}from"./animated-shadow.js";import{observer as n}from"mobx-react-lite";import{isAlive as s}from"mobx-state-tree";import{Image as d,Rect as h}from"react-konva";import{ROLES as c}from"../model/store.js";import l from"./use-image.js";import u from"konva";import{ErrorPlaceholder as g,LoadingPlaceholder as m}from"./media-placeholders.js";import{incrementLoader as p,triggerLoadError as f}from"../utils/loader.js";import{polotnoError as v}from"@polotno/core/errors";import{getBorderInset as w,getCropRect as x,mayBeUnsizedSvg as y}from"@polotno/core/image";import{elementOpacity as M,paintOpacity as S}from"./paint-opacity.js";import*as b from"@polotno/core/svg";import{trySetCanvasSize as E}from"../utils/canvas.js";import{roundedPosition as I}from"../utils/px.js";import{useMediaResize as C}from"./media-resize.js";import{useFilteredSource as j}from"./use-filtered-source.js";import{useOffscreenCanvas as R}from"./use-offscreen-canvas.js";import{useFadeIn as z}from"./use-fadein.js";import{useAnimatedBlur as D}from"./use-animated-blur.js";import{PlacedNode as O}from"./placed-node.js";import{isTouchDevice as k}from"../utils/screen.js";import{useDelayer as L}from"./use-delayer.js";import{useColor as _}from"./use-color.js";import{commitCropSource as T,cropDraftWriter as A,CropOverlay as X}from"./crop-overlay.js";import{ImageToolControls as Y}from"./image-tool-controls.js";import{drawReduced as W,exportKernelEnabled as H,shouldResample as P}from"./downscale.js";import{alignToPixelGrid as N,coversWholeSource as U,displayRatio as F,maskCompositeSize as G,MAX_ELEMENT_BUFFER as $,svgSourceRatio as q}from"@polotno/core/image-buffer";export{G as maskCompositeSize,q as svgSourceRatio};const J=(e,t,r)=>F(e.a,t,r),Q=new Map,V=new Map;let B=0;const K=e=>{const t=Q.get(e);return void 0!==t&&(Q.delete(e),Q.set(e,t)),t},Z=(e,t)=>{const r=e.length+t.length;if(!(r>4e6||Q.has(e))){Q.set(e,t),B+=r;for(const[e,t]of Q){if(B<=16e6){break}Q.delete(e),B-=e.length+t.length}}},ee=(e,t)=>e+"\n"+JSON.stringify((null==t?void 0:t.size)?[...t]:[]),te=new Map,re=new Map,oe=e=>b.isSvgUrl(e)||!0===te.get(e),ie=e=>"svg"===e.type||oe(e.a.src||""),ae=(e,t,r,i)=>{const[a,n]=o.useState(""),s=!t&&!!r&&"loaded"===i&&!e.startsWith("data:")&&!te.has(e)&&a!==e&&y(r);return o.useEffect(()=>{if(!s){return}let t=!0;return(e=>{let t=re.get(e);return t||(t=b.fetchSvgText(e).then(t=>{null!==t&&Z(ee(e),b.prepareSvgSource(t)),te.set(e,null!==t)}).catch(()=>{}).finally(()=>re.delete(e)),re.set(e,t)),t})(e).then(()=>{t&&n(e)}),()=>{t=!1}},[s,e]),s?"loading":i},ne=(e,t,r)=>{const[,i]=o.useReducer(e=>e+1,0),a=t&&e?ee(e,r):"",n=o.useRef("loading"),s=o.useRef(""),d=o.useRef(a);if(d.current!==a&&(d.current=a,n.current="loading"),a&&"loading"===n.current){const e=K(a);void 0!==e&&(s.current=e,n.current="loaded")}return o.useEffect(()=>{if(!a||"loaded"===n.current){return}let t=!1;return((e,t)=>{const r=ee(e,t),o=K(r);if(void 0!==o){return Promise.resolve(o)}let i=V.get(r);if(!i){const o=(null==t?void 0:t.size)?new Map(t):null;i=b.urlToString(e).then(e=>b.prepareSvgSource(e,o)),V.set(r,i),i.then(e=>Z(r,e),()=>{}).finally(()=>V.delete(r))}return i})(e,r).then(e=>{t||(s.current=e,n.current="loaded",i())},()=>{t||(s.current="",n.current="failed",i())}),()=>{t=!0}},[a]),a?[s.current,n.current]:[e,"loaded"]};export const useSizeFixer=e=>ne(e,oe(e))[0]||e;function se(e,t){return{x:(e.x+t.x)/2,y:(e.y+t.y)/2}}const de=e=>{if(e.flipX||e.flipY||e.maskSrc){return!0}const t=navigator.userAgent.toLowerCase();return(t.indexOf("firefox")>-1||/^((?!chrome|android).)*safari/i.test(t))&&ie(e)},he=(e,t,r)=>Math.max(t,Math.min(r,e));export const useCornerRadiusAndCrop=(e,t,r,i,a=0,n=!1,s=!0,d=0,h)=>{var c;const l=$,g=$,m=h.aligned?he(h.deviceWidth,1,l):Math.floor(he(e.a.width*i,1,l)),p=h.aligned?he(h.deviceHeight,1,g):Math.floor(he(e.a.height*i,1,g)),f=Math.min(a*i,m/2,p/2),v=Math.min(d*i,m/2,p/2),w=J(e,r,i),x=H(e.page)&&!n&&(P(w,m,p)||h.aligned),y=U(r,t),M=o.useMemo(()=>{if(t&&t.width&&t.height){return!y||0!==f||x||v?document.createElement("canvas"):void 0}},[t,f,x,y,v]);return o.useLayoutEffect(()=>{var o;if(!M||!t){return}E(M,m,p);const i=M.getContext("2d");if(i){if((null===(o=e.page)||void 0===o?void 0:o._exportingOrRendering)&&(i.imageSmoothingQuality="high"),(f||v)&&(i.beginPath(),i.roundRect(v,v,m-2*v,p-2*v,Math.max(0,f-v)),i.clip()),x){const e=M.width===m&&M.height===p&&h.aligned;return void W(i,t,r,M.width,M.height,{x:e?h.phaseX:0,y:e?h.phaseY:0,width:e?h.spanX:M.width,height:e?h.spanY:M.height})}i.drawImage(t,r.x,r.y,r.width,r.height,0,0,M.width,M.height)}},[M,e.a.width,e.a.height,r.x,r.y,r.width,r.height,a,i,n,null===(c=e.page)||void 0===c?void 0:c._exportingOrRendering,x,h.phaseX,h.phaseY,v]),o.useEffect(()=>()=>{M&&"CANVAS"===M.nodeName&&u.Util.releaseCanvas(M)},[M]),M||t};const ce=document.createElement("canvas");let le=l;export const setImageLoaderHook=e=>{le=e};export const useCanvasImage=(e,t)=>le(e,t);export const useSizedImage=e=>{const[t,r]=ne(e,oe(e)),[o,i]=useCanvasImage(t,"anonymous"),a=ae(e,oe(e),o,i);return[o,"loaded"===r?a:r]};export const useClipImage=e=>{const[t,r]=useSizedImage(e),i=o.useRef(void 0);return e&&"failed"!==r?t&&(i.current=t):i.current=void 0,[e&&"failed"!==r?t||i.current:void 0,r]};export const useImageLoader=(e,t="",r="")=>{const i=o.useRef(void 0),a=()=>{var e;null===(e=i.current)||void 0===e||e.call(i),i.current=void 0};o.useEffect(()=>a,[]),o.useLayoutEffect(()=>{const o=t.slice(0,200),n=`image with id ${r} url: ${o}`;"loading"!==e||i.current||(i.current=p(n)),"loading"!==e&&a(),"failed"===e&&f(v("IMAGE_FAILED",n,{src:o,elementId:r,reason:"load"}))},[e])};export const ImageElement=n(({element:n,store:l})=>{var p,f,v,y,b;const[W,P]=o.useState(!1),F=o.useRef(null),$=k(),Q=n.a.src,V=ie(n),[B,K]=ne(Q,V,n.colorsReplace),[Z,ee]=le(B,"anonymous"),te="loaded"===K?ee:K,re=ae(Q,V,Z,te),oe=!!Q;useImageLoader(oe?re:"loaded",Q,n.id);const he=(null===(p=n.page)||void 0===p?void 0:p._exportingOrRendering)||!de(n)?1:Math.max(1,l.scale),ue=l._elementsPixelRatio*he,ge=(({image:e,status:t})=>{const r=o.useRef(void 0);return o.useEffect(()=>{r.current=e||r.current},[e]),"failed"!==t?r.current:void 0})({image:Z,status:re}),me=((e,t,r)=>{const o=ie(e)&&t?q({cropWidth:e.a.cropWidth,cropHeight:e.a.cropHeight,width:e.a.width,height:e.a.height},t,r):1,{flipX:i,flipY:a}=e,n=de(e),s=R(n&&!!(t&&t.width&&t.height),e=>{var r,n;if(!t){return}E(e,Math.max(t.width*o,1),Math.max(t.height*o,1));let s=i?-e.width:0,d=a?-e.height:0;null===(r=e.getContext("2d"))||void 0===r||r.scale(i?-1:1,a?-1:1),null===(n=e.getContext("2d"))||void 0===n||n.drawImage(t,s,d,e.width,e.height)},[i,a,t,o]);return n?s:t})(n,Z||ge,ue),pe=((e,t)=>{const[r,i]=useSizedImage(e.maskSrc||""),a=e.maskSrc?i:"loaded";return useImageLoader(a,e.maskSrc||"",e.id),o.useMemo(()=>{if(!r){return t}if(!t||!t.width||!t.height){return t}const{width:o,height:i}=G(t,{width:e.a.width,height:e.a.height,stretchEnabled:e.stretchEnabled}),a=document.createElement("canvas");a.width=o,a.height=i;const n=a.getContext("2d");if(!n){return t}n.drawImage(t,0,0,o,i,0,0,o,i),n.globalCompositeOperation="source-in";const s=function(e,t){const r=t.width/t.height;let o,i;return r>=e.width/e.height?(o=e.width,i=e.width/r):(o=e.height*r,i=e.height),{x:(e.width-o)/2,y:(e.height-i)/2,width:o,height:i}}(r,e);return n.drawImage(r,s.x,s.y,s.width,s.height,0,0,o,i),a},[t,r,e.a.width,e.a.height,e.stretchEnabled])})(n,me)||ce,[fe=ce]=j(n,pe);o.useLayoutEffect(()=>{"loaded"===re&&n._setSourceSize({width:fe.width,height:fe.height},Q)},[n,Q,re,fe.width,fe.height]);let{cropX:ve,cropY:we,cropWidth:xe,cropHeight:ye}=n.a;"loaded"!==ee&&(ve=we=0,xe=ye=1);const Me=x({stretchEnabled:n.stretchEnabled,fit:n.a.fit,focus:n.focus,cropX:ve,cropY:we,cropWidth:xe,cropHeight:ye,width:n.a.width,height:n.a.height},fe),Se=Me.width,be=Me.height,Ee=null!==(b=n.cornerRadius)&&void 0!==b?b:0,Ie=w({borderSize:n.borderSize,borderColor:i(n.borderColor),width:n.a.width,height:n.a.height,opacity:S(n)}),Ce=!(!Ee&&!Ie&&!n.clipSrc&&U(Me,fe)),je=(null===(f=n.page)||void 0===f?void 0:f._exportingOrRendering)||!Ce?1:Math.min(2,l.scale),Re=l._elementsPixelRatio*je,ze=W||n._cropModeEnabled||"svg"===n.type,De=H(n.page)&&0===n.a.rotation&&"group"!==(null===(v=n.parent)||void 0===v?void 0:v.type)&&!ze,Oe=((e,t,r,o)=>N(e.a,t,r,o))(n,Re,J(n,Me,Re),De);let ke=((e,t,r,i)=>{const[a,n]=useClipImage(e.clipSrc||""),s=e.clipSrc?n:"loaded";useImageLoader(s,e.clipSrc,e.id);const d=o.useMemo(()=>{if(t&&a){return document.createElement("canvas")}},[t,a]);return o.useLayoutEffect(()=>{var o;if(!a){return}if(!t||!t.width||!t.height){return}if(!a||!a.width||!a.height){return}if(!d){return}const i=document.createElement("canvas"),n=Math.max(e.a.width/a.width*r,e.a.height/a.height*r);E(i,Math.min(1e4,Math.max(a.width*n,1)),Math.min(1e4,Math.max(a.height*n,1))),null===(o=i.getContext("2d"))||void 0===o||o.drawImage(a,0,0,i.width,i.height),E(d,Math.min(1e4,Math.max(t.width,Math.ceil(e.a.width*r),1)),Math.min(1e4,Math.max(t.height,Math.ceil(e.a.height*r),1)));const s=d.getContext("2d");s&&(s.save(),s.drawImage(i,0,0,d.width,d.height),u.Util.releaseCanvas(i),s.globalCompositeOperation="source-in",s.drawImage(t,0,0,d.width,d.height),s.restore())},[d,t,a,e.a.width,e.a.height,r,...i]),o.useEffect(()=>()=>{d&&u.Util.releaseCanvas(d)},[d]),e.clipSrc&&a?d:t})(n,useCornerRadiusAndCrop(n,fe,Me,Re,Ee,ze,!0,Ie,Oe),Re,[Me,Ee,Re]);var Le=o.useRef(null),_e=o.useRef(0),Te=o.useRef(!1);const Ae=()=>{var e;n._cropEnabled&&!n.pendingEdit&&(null===(e=l.activeTool)||void 0===e?void 0:e.elementId)!==n.id&&setTimeout(()=>{s(n)&&n.toggleCropMode(!0)})},Xe=ie(n)&&ge,Ye=oe&&"loading"===te&&!Xe,[We]=L(Ye,100,!1,!1),He="failed"===re,Pe=!We&&!He,Ne=C(n,Me,"loaded"===ee?fe:void 0,P),Ue=Pe?M(n):0;z(F,Ue),D(F,n,ke);const Fe=n.selectable||l.role===c.ADMIN,Ge=_(n,n.borderColor,"stroke");return r(e,{children:[We&&t(m,{element:n}),He&&t(g,{element:n,text:"Can not load the image..."}),t(O,Object.assign({as:d,nodeRef:F,element:n,name:"element",id:n.id,image:ke},Oe.position,{width:Oe.width||1,height:Oe.height||1,opacity:Ue},a(n),{customCrop:Me,listening:Fe,draggable:!n.pendingEdit&&(null===(y=l.activeTool)||void 0===y?void 0:y.elementId)!==n.id&&($?n.draggable&&n.isSelected:n.draggable),preventDefault:!$||n.isSelected,hideInExport:!n.showInExport,onDragMove:e=>{n.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{n.set(I(e.target))},onDblClick:Ae,onDblTap:Ae},Ne)),!!n.borderSize&&t(O,Object.assign({as:h,element:n},Oe.position,{width:Math.max(Oe.width-n.borderSize,0),height:Math.max(Oe.height-n.borderSize,0),opacity:Ue,offsetX:-n.borderSize/2,offsetY:-n.borderSize/2},Ge,{strokeWidth:n.borderSize,listening:!1,cornerRadius:Math.max(0,Ee-n.borderSize),hideInExport:!n.showInExport})),n._cropModeEnabled&&"loaded"===re&&t(X,{element:n,store:l,previewImage:ke,sourceImage:fe,sourceWidth:fe.width,sourceHeight:fe.height,crop:Me,onSourceTouchMove:e=>{e.evt.preventDefault();const t=e.target.getStage().getPointersPositions();var r=t[0],o=t[1];const i=e.target;if(r&&!o&&!i.isDragging()&&Te.current&&(i.startDrag(),Te.current=!1),r&&o){u.hitOnDragEnabled=!0,i.isDragging()&&(Te.current=!0,i.stopDrag());const t=e.target.getAbsoluteTransform().copy();t.invert();var a={x:r.x,y:r.y},s={x:o.x,y:o.y};if(!Le.current){return void(Le.current=se(a,s))}var d=se(a,s),h=function(e,t){return Math.sqrt(Math.pow(t.x-e.x,2)+Math.pow(t.y-e.y,2))}(a,s);_e.current||(_e.current=h);const v=i.position();var c={x:d.x-v.x,y:d.y-v.y},g=h/_e.current;i.scaleX(g),i.scaleY(g);const w=t.point(d),x=t.point(Le.current);var m=w.x-x.x,p=w.y-x.y,f={x:Math.min(0,d.x-c.x*g+m),y:Math.min(0,d.y-c.y*g+p)};i.position(f),_e.current=h,Le.current=d,(e=>{T(e,n,A(n,l),fe.width,fe.height,Se,be)})(e)}},onSourceTouchEnd:e=>{_e.current=0,Le.current=null,u.hitOnDragEnabled=!1}}),"image"===n.type&&t(Y,{element:n,store:l})]})});
@@ -1 +1 @@
1
- import{Fragment as e,jsx as t,jsxs as i}from"react/jsx-runtime";import{elementOpacity as o}from"./paint-opacity.js";import r from"react";import{getCropRect as n}from"@polotno/core/image";import{observer as a}from"mobx-react-lite";import{nodeAutorun as l}from"../utils/mobx.js";import{svgSourceRatio as m,useImageLoader as s}from"./image-element.js";import{RasterNode as d,useRasterPipeline as h}from"./gif-element.js";import{ErrorPlaceholder as c,LoadingPlaceholder as p}from"./media-placeholders.js";import{getVideoRestMs as u,isHeldByPreview as g}from"../utils/timeline.js";import{closeLottie as f,openLottie as w}from"../utils/lottie.js";export const LottieElement=a(({element:a,store:v})=>{var x;const[j,M]=r.useState(!1),y=r.useRef(null),[E,F]=function(e){const[t,i]=r.useState();r.useEffect(()=>{let t,o=!0;return w(e).then(r=>{if(!o){return f(r)}t=r,i({url:e,loaded:r})},t=>{o&&(console.error("Failed to load Lottie:",t),i({url:e}))}),()=>{o=!1,t&&f(t),i(void 0)}},[e]);const o=(null==t?void 0:t.url)===e?t:void 0,n=o?o.loaded?"loaded":"failed":"loading";return[null==o?void 0:o.loaded,n]}(a.src);s(F,a.src,a.id);const L=!!(null===(x=a.page)||void 0===x?void 0:x._exportingOrRendering),b=v._elementsPixelRatio*(L?1:Math.min(2,v.scale)),{cropX:R,cropY:_,cropWidth:A,cropHeight:H,width:S,height:T}=a.a;let W={width:1,height:1};if(E){const e=E.player.animationSize(),t=m({cropWidth:A,cropHeight:H,width:S,height:T},e,b);W={width:Math.max(1,Math.round(e.width*t)),height:Math.max(1,Math.round(e.height*t))};const{canvas:i,player:o}=E;i.width===W.width&&i.height===W.height||(i.width=W.width,i.height=W.height,o.resize())}const z=n({stretchEnabled:a.stretchEnabled,cropX:R,cropY:_,cropWidth:A,cropHeight:H,width:S,height:T},W),[C,P]=h(a,b,null==E?void 0:E.canvas,z,y,j);r.useLayoutEffect(()=>{if(!E){return}const{player:e}=E,{totalFrames:t}=e,i=1e3*e.duration,o=e=>Math.min(e/i*t,t-1);let r=-1;const n=t=>{var i,o;t!==r&&(r=t,e.setFrame(t),P(),null===(o=null===(i=y.current)||void 0===i?void 0:i.getLayer())||void 0===o||o.batchDraw())};let m=0;const s=()=>{n(Math.floor(o(performance.now()%i))),m=requestAnimationFrame(s)},d=l(a,()=>{if(g(a)){return}cancelAnimationFrame(m);const e=a.page._mediaLocalTime;if(null!==e){const{start:t,end:r}=a.timeRange;n(o(u(e,0,t,i,0,1,{windowEnd:r,frameMs:v._renderFrameMs,renderEnd:v._renderEndMs-a.page.startTime})))}else{v.isPlaying||L?n(t-1):s()}});return()=>{d(),cancelAnimationFrame(m)}},[E,W.width,W.height,L]);const X=E?o(a):0;return i(e,{children:["loading"===F&&t(p,{element:a}),"failed"===F&&t(c,{element:a,text:"Cannot load the Lottie..."}),t(d,{element:a,store:v,nodeRef:y,image:C,opacity:X,onTransformingChange:M})]})});
1
+ import{Fragment as e,jsx as t,jsxs as o}from"react/jsx-runtime";import{elementOpacity as i}from"./paint-opacity.js";import r from"react";import{getCropRect as a}from"@polotno/core/image";import{observer as n}from"mobx-react-lite";import{nodeAutorun as l}from"../utils/mobx.js";import{svgSourceRatio as s,useImageLoader as m}from"./image-element.js";import{RasterNode as d,useRasterPipeline as c}from"./gif-element.js";import{ErrorPlaceholder as h,LoadingPlaceholder as u}from"./media-placeholders.js";import{getVideoRestMs as p,isHeldByPreview as f}from"../utils/timeline.js";import{closeLottie as g,openLottie as v}from"../utils/lottie.js";export const LottieElement=n(({element:n,store:w})=>{var x;const[j,y]=r.useState(!1),M=r.useRef(null),[E,F]=function(e){const[t,o]=r.useState();r.useEffect(()=>{let t,i=!0;return v(e).then(r=>{if(!i){return g(r)}t=r,o({url:e,loaded:r})},t=>{i&&(console.error("Failed to load Lottie:",t),o({url:e}))}),()=>{i=!1,t&&g(t),o(void 0)}},[e]);const i=(null==t?void 0:t.url)===e?t:void 0,a=i?i.loaded?"loaded":"failed":"loading";return[null==i?void 0:i.loaded,a]}(n.a.src);m(F,n.a.src,n.id),r.useLayoutEffect(()=>{E&&n._setSourceSize(E.player.animationSize(),n.a.src)},[n,n.a.src,E]);const L=!!(null===(x=n.page)||void 0===x?void 0:x._exportingOrRendering),S=w._elementsPixelRatio*(L?1:Math.min(2,w.scale)),{cropX:_,cropY:b,cropWidth:R,cropHeight:z,width:A,height:H}=n.a;let T={width:1,height:1};if(E){const e=E.player.animationSize(),t=s({cropWidth:R,cropHeight:z,width:A,height:H},e,S);T={width:Math.max(1,Math.round(e.width*t)),height:Math.max(1,Math.round(e.height*t))};const{canvas:o,player:i}=E;o.width===T.width&&o.height===T.height||(o.width=T.width,o.height=T.height,i.resize())}const W=a({fit:n.a.fit,focus:n.focus,stretchEnabled:n.stretchEnabled,cropX:_,cropY:b,cropWidth:R,cropHeight:z,width:A,height:H},T),[C,P]=c(n,S,null==E?void 0:E.canvas,W,M,j);r.useLayoutEffect(()=>{if(!E){return}const{player:e}=E,{totalFrames:t}=e,o=1e3*e.duration,i=e=>Math.min(e/o*t,t-1);let r=-1;const a=t=>{var o,i;t!==r&&(r=t,e.setFrame(t),P(),null===(i=null===(o=M.current)||void 0===o?void 0:o.getLayer())||void 0===i||i.batchDraw())};let s=0;const m=()=>{a(Math.floor(i(performance.now()%o))),s=requestAnimationFrame(m)},d=l(n,()=>{if(f(n)){return}cancelAnimationFrame(s);const e=n.page._mediaLocalTime;if(null!==e){const{start:t,end:r}=n.timeRange;a(i(p(e,0,t,o,0,1,{windowEnd:r,frameMs:w._renderFrameMs,renderEnd:w._renderEndMs-n.page.startTime})))}else{w.isPlaying||L?a(t-1):m()}});return()=>{d(),cancelAnimationFrame(s)}},[E,T.width,T.height,L]);const X=E?i(n):0;return o(e,{children:["loading"===F&&t(u,{element:n}),"failed"===F&&t(h,{element:n,text:"Cannot load the Lottie..."}),t(d,{element:n,store:w,nodeRef:M,image:C,opacity:X,crop:W,source:E?T:void 0,onTransformingChange:y})]})});