polotno 3.17.0 → 3.19.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 (77) hide show
  1. package/blueprint/side-panel/select-video.d.ts +18 -16
  2. package/canvas/animated-shadow.d.ts +11 -0
  3. package/canvas/animated-shadow.js +1 -0
  4. package/canvas/apply-filters.d.ts +5 -1
  5. package/canvas/apply-filters.js +1 -1
  6. package/canvas/downscale.js +1 -1
  7. package/canvas/element.js +1 -1
  8. package/canvas/figure-element.js +1 -1
  9. package/canvas/gif-element.js +1 -1
  10. package/canvas/html-element.js +1 -1
  11. package/canvas/image-element.d.ts +1 -0
  12. package/canvas/image-element.js +1 -1
  13. package/canvas/line-element.js +1 -1
  14. package/canvas/page.js +1 -1
  15. package/canvas/render-tag-element.js +1 -1
  16. package/canvas/rich-editor.d.ts +2 -0
  17. package/canvas/rich-editor.js +8 -5
  18. package/canvas/text-element.js +1 -1
  19. package/canvas/use-animated-blur.d.ts +4 -0
  20. package/canvas/use-animated-blur.js +1 -0
  21. package/canvas/use-color.d.ts +1 -1
  22. package/canvas/use-filtered-source.js +1 -1
  23. package/canvas/use-offscreen-canvas.d.ts +2 -0
  24. package/canvas/use-offscreen-canvas.js +1 -0
  25. package/canvas/use-transformer-snap.js +1 -1
  26. package/canvas/video-element.js +1 -1
  27. package/icons/animation-icons.d.ts +30 -7
  28. package/icons/animation-icons.js +1 -1
  29. package/icons/registry.d.ts +22 -0
  30. package/icons/registry.js +1 -1
  31. package/image-tools/raster.js +1 -1
  32. package/model/figure-model.d.ts +10 -9
  33. package/model/gif-model.d.ts +10 -9
  34. package/model/group-model.d.ts +78 -68
  35. package/model/group-model.js +1 -1
  36. package/model/history.d.ts +3 -0
  37. package/model/history.js +1 -1
  38. package/model/image-model.d.ts +10 -9
  39. package/model/line-model.d.ts +10 -9
  40. package/model/node-model.d.ts +8 -8
  41. package/model/page-model.d.ts +228 -204
  42. package/model/shape-model.d.ts +15 -9
  43. package/model/shape-model.js +1 -1
  44. package/model/store.d.ts +31 -17
  45. package/model/store.js +1 -1
  46. package/model/svg-model.d.ts +9 -8
  47. package/model/table-model.d.ts +8 -7
  48. package/model/text-model.d.ts +9 -8
  49. package/model/video-model.d.ts +10 -9
  50. package/package.json +24 -21
  51. package/pages-timeline/element-track.js +1 -1
  52. package/pages-timeline/page-preview.js +1 -1
  53. package/polotno.bundle.js +195 -177
  54. package/side-panel/animations-panel.js +1 -1
  55. package/side-panel/select-video.d.ts +18 -16
  56. package/side-panel/text-panel.js +1 -1
  57. package/side-panel/text-template.js +1 -1
  58. package/toolbar/animations-picker.js +1 -1
  59. package/toolbar/use-copy-style.d.ts +1 -0
  60. package/toolbar/use-copy-style.js +1 -1
  61. package/ui.css +1 -1
  62. package/utils/animations.d.ts +212 -10
  63. package/utils/animations.js +1 -1
  64. package/utils/clipboard.d.ts +17 -15
  65. package/utils/clipboard.js +1 -1
  66. package/utils/duplicate.js +1 -1
  67. package/utils/fonts.js +1 -1
  68. package/utils/from-svg.js +1 -1
  69. package/utils/image-dpi.js +1 -1
  70. package/utils/image-edit.js +1 -1
  71. package/utils/l10n.d.ts +44 -0
  72. package/utils/l10n.js +1 -1
  73. package/utils/px.d.ts +1 -0
  74. package/utils/px.js +1 -1
  75. package/utils/timeline.d.ts +3 -1
  76. package/utils/timeline.js +1 -1
  77. package/utils/validate-key.js +1 -1
package/canvas/page.js CHANGED
@@ -1 +1 @@
1
- var e=this&&this.__rest||function(e,t){var n={};for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++){t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(n[i[o]]=e[i[o]])}}return n};import{jsx as t,jsxs as n,Fragment as i}from"react/jsx-runtime";import o from"react";import{clamp as r}from"../lib/utils.js";import{observer as s,useLocalObservable as l}from"mobx-react-lite";import{action as a,runInAction as c}from"mobx";import{isAlive as d}from"mobx-state-tree";import{ZERO_BLEED as m}from"@polotno/schema";import{Group as u,Image as h,Label as g,Layer as f,Line as p,Rect as x,Shape as v,Stage as b,KonvaRenderer as y,Tag as w,Text as E,Transformer as S}from"react-konva";import{useWorkspaceStyle as X}from"./workspace-style.js";import{ROLES as Y}from"../model/store.js";import k from"konva";import A from"./element.js";import{DrawingLayer as M}from"./drawing-layer.js";import{ensureDragOrder as j,useSnap as I}from"./use-transformer-snap.js";import{useCanvasImage as P,useImageLoader as O}from"./image-element.js";import{FadeOverlay as R}from"./fade-overlay.js";import{getCrop as T}from"../utils/crop.js";import{cloneBehind as D}from"../utils/duplicate.js";import{___ as B,____ as C,isCreditVisible as W}from"../utils/validate-key.js";import{getClientRect as G,getCorners as L,getTotalClientRect as V,polygonsIntersect as F}from"../utils/math.js";import{pxToUnitRounded as z,pxToUnitString as _}from"../utils/unit.js";import{isCornerAnchor as U,MAIN_TRANSFORMER_NAME as Z}from"../utils/px.js";import{canStretchAxis as H,isAxisAligned as J,leafMinSize as N}from"../utils/selection-transform.js";import{restoreHeldPoint as K}from"./transform-box.js";import{flags as $}from"../utils/flags.js";import{textOverflowOf as Q}from"../utils/text-sizing.js";import{useColor as q}from"./use-color.js";import{isGradient as ee}from"@polotno/core/gradient";import{toDisplayColor as te}from"@polotno/core/color";import{Html as ne}from"react-konva-utils";import{getParentGroupId as ie,withoutDescendants as oe}from"../utils/selection.js";import{nodeReaction as re}from"../utils/mobx.js";import{useEvent as se}from"../hooks/use-event.js";const le=o.version.startsWith("18.")?o.forwardRef((e,n)=>(o.useLayoutEffect(()=>{y.flushSync()}),t(b,Object.assign({},e,{ref:n})))):b,ae={enabledAnchors:["top-left","top-center","top-right","middle-left","bottom-left","bottom-right","bottom-center","middle-right"],rotateEnabled:!0,rotationSnaps:[0,45,90,135,180,225,270,315],ignoreStroke:!0,flipEnabled:!1,keepRatio:!0},ce=e=>{if(!e.length){return null}const t=J(e[0].rotation)?0:e[0].rotation;return e.every(e=>J(e.rotation-t))?t:null},de=e=>e.selectedShapes.filter(t=>{var n;return(null===(n=e.activeTool)||void 0===n?void 0:n.elementId)!==t.id&&!t.pendingEdit}),me=e=>e.selectedShapes.some(e=>!e.resizable),ue=e=>e.selectedShapes.some(e=>!e.draggable),he=e=>{var t;return 1!==e.selectedElements.length||"group"===(null===(t=e.selectedElements[0])||void 0===t?void 0:t.type)},ge=e=>{var t;const n=de(e),i=he(e),o=null!==(t=ce(n))&&void 0!==t?t:0,r=e=>i&&n.length>0&&n.every(t=>H(t,e,o));return{boxRotation:o,canStretchX:r("x"),canStretchY:r("y")}},fe={text:{enabledAnchors:["top-left","top-right","middle-left","bottom-left","bottom-right","middle-right"]},svg:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},gif:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},line:{enabledAnchors:[],borderEnabled:!1,rotateEnabled:!1},image:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},many:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},group:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},table:{keepRatio:!1,enabledAnchors:["top-left","top-center","top-right","middle-left","middle-right","bottom-left","bottom-center","bottom-right"]}};export function registerTransformerAttrs(e,t){fe[e]=fe[e]||t,Object.assign(fe[e],t)}const pe=e=>t(x,Object.assign({},e,{preventDefault:!1})),xe=n=>{var{url:i}=n,o=e(n,["url"]);const[r,s]=P(i,"anonymous"),l=r?T(r,{width:o.width,height:o.height},"center-middle"):{};return O(s,i,"page background"),t(h,Object.assign({image:r},o,l))},ve=e=>{const n=q({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return t(x,Object.assign({},e,n))},be=n=>{var{background:i,transparencyBackgroundColor:r}=n,s=e(n,["background","transparencyBackgroundColor"]);const l=o.useMemo(()=>{if(ee(i)){return!0}const e=k.Util.colorToRGBA(i);return!e||e.a<1},[i]),a=o.useMemo(()=>{if(!l||r){return}const e=document.createElement("canvas"),t=30;e.width=60,e.height=60;const n=e.getContext("2d");return n&&(n.fillStyle="black",n.fillRect(t,0,t,t),n.fillRect(0,t,t,t)),e},[l,r]);return l?t(x,r?Object.assign({fill:r},s,{hideInExport:!0}):Object.assign({fillPatternImage:a},s,{opacity:.1,hideInExport:!0})):null},ye=n=>{var{background:i}=n,r=e(n,["background"]);return o.useMemo(()=>!!k.Util.colorToRGBA(i)||ee(i),[i])?t(ve,Object.assign({fill:i},r)):t(xe,Object.assign({url:i},r))},we=s(({selection:e})=>{const{selectionRectStyle:n}=X();return e.visible?t(x,{name:"selection",x:Math.min(e.x1,e.x2),y:Math.min(e.y1,e.y2),width:Math.abs(e.x1-e.x2),height:Math.abs(e.y1-e.y2),fill:n.fill,stroke:n.stroke,strokeWidth:n.strokeWidth}):null});function Ee(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const Se=s(({gesture:e,store:o})=>{const{transformLabelStyle:r}=X(),{x:s,y:l,width:a,height:c,rotation:d,anchor:m}=e.label;if(void 0===s||void 0===l||void 0===a||void 0===c||void 0===d||void 0===m){return null}const u=G({x:s,y:l,width:a,height:c,rotation:k.Util.radToDeg(d)}),h=(c/2+70)*Math.cos(d-Math.PI/2),f=(c/2+70)*Math.sin(d-Math.PI/2),p=z({unit:o.unit,dpi:o.dpi,px:a/o.scale,precious:"px"===o.unit?0:1})+" x "+z({unit:o.unit,dpi:o.dpi,px:c/o.scale,precious:"px"===o.unit?0:1})+("px"===o.unit?"":" "+o.unit);return n(i,{children:[n(g,{x:(u.minX+u.maxX)/2+h,y:(u.minY+u.maxY)/2+f,offsetX:14,offsetY:14,visible:"rotater"===m,children:[t(w,{cornerRadius:5,fill:r.fill}),t(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:Ee(d).toString()+"°"})]}),n(g,{x:(u.minX+u.maxX)/2,y:u.maxY+20,visible:"rotater"!==m,children:[t(w,{cornerRadius:5,fill:r.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),t(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:p})]})]})}),Xe=s(({store:e,gesture:t,transformerStyle:n,onChange:i})=>{var r;const s=e.selectedElements.some(e=>e.curveEnabled),l=e.selectedElements.some(e=>!e.visible),a=JSON.stringify(de(e).map(e=>e.id)),c=!he(e)&&e.selectedElements[0].keepRatio,d=!he(e)&&"text"===(null===(r=e.selectedElements[0])||void 0===r?void 0:r.type)&&"fit"===Q(e.selectedElements[0]),m=t.transforming?null:ge(e);return o.useLayoutEffect(()=>{i()},[e.selectedShapes,a,me(e),l,ue(e),s,c,d,t.transforming,null==m?void 0:m.canStretchX,null==m?void 0:m.canStretchY,null==m?void 0:m.boxRotation,n]),null}),Ye=()=>{console.warn("Polotno warning: onClick callback is deprecated. Just stop using it. Polotno will do selection automatically.")},ke=s(({page:e})=>{const t=e._exportingOrRendering||e._forceMount;return o.useEffect(()=>{if(t&&d(e)){return e.set({_exportRendered:!0}),()=>{d(e)&&e.set({_exportRendered:!1})}}},[e,t]),null}),Ae=s(({elements:e,store:n})=>{const i=e.filter(e=>e.alwaysOnTop),r=e.filter(e=>!e.alwaysOnTop).concat(i);return t(o.Fragment,{children:r.map(e=>t(A,{store:n,element:e,onClick:Ye},e.id))})});export const useContextMenu=({store:e})=>{const[t,n]=o.useState(!1),[i,r]=o.useState({x:0,y:0}),s=o.useCallback(e=>{n(!0),r(e)},[]);return{open:s,close:o.useCallback(()=>{n(!1)},[]),props:{isOpen:t,offset:i,setIsOpen:n}}};let Me=null;export const registerNextDomDrop=e=>{Me=e};const je=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),Ie=atob("I2MwMzkyYg=="),Pe=atob("djAuOS4y"),Oe=e=>t(i,{children:n(g,{fill:Ie,height:200,children:[t(w,{fill:Ie}),t(E,Object.assign({},e,{fill:"white",text:je,height:void 0,padding:10,fontSize:20}))]})}),Re=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),Te=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),De=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),Be=e=>{const i=1===C(),o=i?Te:Re,r=i?230:195;return n(u,Object.assign({},e,{listening:!1,children:[t(x,{fill:Ie,width:r,height:24,cornerRadius:2}),t(E,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),i&&t(E,{text:De,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default s(({store:i,page:s,width:d,height:h,pageControlsEnabled:b,components:y,altCloneEnabled:A,groupSelectionMode:P="drill",viewportSize:O,layout:T="vertical",tooltipSafeArea:z,transformerSafeArea:H})=>{const q=X(),ee=q.transformerStyle,xe=ae,ve=s.computedBleed,Ee=i.bleedVisible?ve:m,Ye=s.computedWidth+Ee.left+Ee.right,je=s.computedHeight+Ee.top+Ee.bottom,Ie=(d-Ye*i.scale)/2,Re=(h-je*i.scale)/2,Te={x:-ve.left,y:-ve.top,width:s.computedWidth+ve.left+ve.right,height:s.computedHeight+ve.top+ve.bottom,background:te(s.background),name:"page-background",preventDefault:!1},De={x:-Ee.left,y:-Ee.top,width:Ye,height:je},Ce=o.useRef(null),We=o.useRef(null),Ge=o.useRef(null),Le=o.useRef(null),Ve=null==b||b,Fe=o.useRef(!1),[ze,_e]=o.useState(null),Ue=l(()=>({transforming:!1,label:{},setLabel(e){this.label=e},setTransforming(e){this.transforming=e}})),[Ze,He]=o.useState(0),Je=useContextMenu({store:i}),Ne=de(i),Ke=he(i),$e=()=>{var t,n,o;const r=Ce.current;if(!r){return[]}const s=r.getStage();if(!s){return[]}const l=de(i),a=new Set(l.map(e=>e.id)),c=new Map;a.size&&s.find(e=>{const t=e.id();return a.has(t)&&!c.has(t)&&c.set(t,e),!1});const d=l.map(e=>c.get(e.id)).filter(e=>!!e),m=1===i.selectedElements.length,u=m&&(null===(t=i.selectedElements[0])||void 0===t?void 0:t.type)||"many",h=fe[u],g=Object.assign(Object.assign({},xe),h||{});h&&("svg"!==u&&"image"!==u&&"gif"!==u||i.selectedElements[0].keepRatio||(g.enabledAnchors=xe.enabledAnchors),"text"!==u||!$.textVerticalResizeEnabled&&"fit"!==Q(i.selectedElements[0])||(g.enabledAnchors=null===(n=fe.text.enabledAnchors)||void 0===n?void 0:n.concat(["bottom-center","top-center"])),"text"===u&&m&&i.selectedElements[0].curveEnabled&&(g.enabledAnchors=fe.many.enabledAnchors));const f=ge(i);(f.canStretchX||f.canStretchY)&&(g.enabledAnchors=[...g.enabledAnchors||[],...f.canStretchX?["middle-left","middle-right"]:[],...f.canStretchY?["top-center","bottom-center"]:[]]);const{boundBoxFunc:p}=ee,x=e(ee,["boundBoxFunc"]);r.setAttrs(Object.assign(Object.assign(Object.assign({},g),x),{name:Z})),me(i)&&r.enabledAnchors([]),ue(i)&&r.rotateEnabled(!1);const v=r.nodes();if(v.length===d.length&&d.every((e,t)=>v[t]===e)||r.nodes(d),he(i)&&d.length){const e=ce(l);null===e||J(r.rotation()-e)||(r.rotation(e),r.forceUpdate())}return null===(o=r.getLayer())||void 0===o||o.batchDraw(),d},Qe=se(()=>{var e;if(null===(e=Ce.current)||void 0===e?void 0:e.isTransforming()){return}const t=$e();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),j())}),qe=()=>{var e,t;const n=Ce.current;if(!n){return}if(!n.isTransforming()){return void Ue.setLabel({})}if(!n.nodes().length){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();Ue.setLabel({anchor:o,x:i.x-(null!==(e=null==r?void 0:r.x())&&void 0!==e?e:0),y:i.y-(null!==(t=null==r?void 0:r.y())&&void 0!==t?t:0),rotation:i.rotation,width:i.width,height:i.height})},et=o.useCallback(()=>{var e;if(null===(e=Ce.current)||void 0===e?void 0:e.isTransforming()){return}const t=Ce.current;if(!t||!t.nodes().length){return void He(0)}const n=t.getClientRect();if(!t.getStage()){return}const i=function(e,t,n,i=0){const o=e.x+e.width/2,r=e.y+e.height/2,s=e.width/2,l=e.height/2,a=i*Math.PI/180,c=e=>{const t=e*Math.PI/180,i=(e%360+360)%360;let c;c=0===i||180===i?l:s;const d=Math.sin(t)*(c+n),m=-Math.cos(t)*(c+n),u=d*Math.cos(a)-m*Math.sin(a),h=d*Math.sin(a)+m*Math.cos(a);return{x:o+u,y:r+h}},d=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(d(c(0))){return 0}for(const m of[180,270,90]){if(d(c(m))){return m}}return 0}(n,{top:(null==H?void 0:H.top)||0,bottom:h-((null==H?void 0:H.bottom)||0),left:(null==H?void 0:H.left)||0,right:d-((null==H?void 0:H.right)||0)},ee.rotateAnchorOffset,t.rotation());He(i)},[H,d,h]);o.useEffect(()=>{var e;null===(e=Ce.current)||void 0===e||e.update(),qe(),et()},[i.scale,et]),o.useEffect(()=>{et()},[i.selectedShapes,et]);const tt=l(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),nt=o.useRef(!1),it=e=>{const t=Ge.current;if(!t||i.role===Y.VIEWER||i.activePage!==s||i.isPlaying||"button"in e.evt&&0!==e.evt.button||1!==t.getPointersPositions().length||!e.target.findAncestor(".elements-container")||!e.target.findAncestor(".element",!0)||!i.selectedShapes.length||ue(i)){return}const n=t.getPointerPosition();if(!n){return}const o=$e();if(o.some(t=>t===e.target||t.isAncestorOf(e.target))){return}const r=o.find(e=>!!(e.draggable()&&e.isVisible()&&e.isListening())&&(e instanceof k.Shape?[e]:e instanceof k.Container?e.find("Shape"):[]).some(e=>e.intersects(n)));if(r){for(const[t,n]of k.DD._dragElements){"ready"!==n.dragStatus||n.node!==e.target&&!n.node.isAncestorOf(e.target)||k.DD._dragElements.delete(t)}r._createDragElement(e),"touches"in e.evt&&e.evt.cancelable&&r.preventDefault()&&e.evt.preventDefault()}},ot=a(e=>{var t,n,o,r,s,l,a;if(i.role===Y.VIEWER){return}if(nt.current=!1,i.activeTool&&"crop"!==i.activeTool.name){return}const c=e.target.findAncestor(".elements-container"),d=e.target.findAncestor("Transformer"),m=e.target.findAncestor(".page-abs-container");if(c||d||m){return}const u=null===(t=e.target.getStage())||void 0===t?void 0:t.getPointerPosition();u&&(u.x-=null!==(o=null===(n=e.target.getStage())||void 0===n?void 0:n.x())&&void 0!==o?o:0,u.y-=null!==(s=null===(r=e.target.getStage())||void 0===r?void 0:r.y())&&void 0!==s?s:0,tt.visible=!0,tt.x1=u.x,tt.y1=u.y,tt.x2=u.x,tt.y2=u.y,(null!==(a=null===(l=e.target.getStage())||void 0===l?void 0:l.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(tt.visible=!1))});(({stageRef:e,containerRef:t,viewportSize:n})=>{o.useEffect(()=>{var n;const i=null===(n=t.current)||void 0===n?void 0:n.closest(".polotno-workspace-inner");function o(){var n;if(!e.current){return}const o=null===(n=t.current)||void 0===n?void 0:n.getBoundingClientRect(),s=null==i?void 0:i.getBoundingClientRect();if(!o||!s){return}const l=r(s.left-o.left-100,0,Math.max(0,o.width-e.current.width())),a=r(s.top-o.top-100,0,Math.max(0,o.height-e.current.height()));e.current.position({x:-l,y:-a}),e.current.container().style.transform=`translate(${l}px, ${a}px)`}return o(),null==i||i.addEventListener("scroll",o),()=>{null==i||i.removeEventListener("scroll",o)}},[n.width,n.height])})({stageRef:Ge,containerRef:Le,viewportSize:O}),o.useEffect(()=>{const e=a(e=>{var t,n,i,o,r,s;if(!tt.visible){return}null===(t=Ge.current)||void 0===t||t.setPointersPositions(e);let l=null===(n=Ge.current)||void 0===n?void 0:n.getPointerPosition();l?(l.x-=null!==(o=null===(i=Ge.current)||void 0===i?void 0:i.x())&&void 0!==o?o:0,l.y-=null!==(s=null===(r=Ge.current)||void 0===r?void 0:r.y())&&void 0!==s?s:0):l={x:tt.x2,y:tt.y2},tt.x2=l.x,tt.y2=l.y}),t=a(()=>{if(!tt.visible){return}if(!Ge.current){return}const e=Ge.current.findOne(".selection"),t=e?e.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(t.width&&t.height){const e=[{x:t.x,y:t.y},{x:t.x+t.width,y:t.y},{x:t.x+t.width,y:t.y+t.height},{x:t.x,y:t.y+t.height}],n=[];Ge.current.find(".element").forEach(t=>{var o;const r=i.getElementById(t.id()),s=null==r?void 0:r.draggable,l=null==r?void 0:r.selectable;if(!s||!l){return}const a=null===(o=t.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const c=L(r.a).map(e=>a.point(e));F(e,c)&&n.push(r.top.id)});const o=[...new Set(n)];nt.current=!0,i.selectElements(o)}tt.visible=!1});return window.addEventListener("mousemove",e),window.addEventListener("touchmove",e),window.addEventListener("mouseup",t,{capture:!0}),window.addEventListener("touchend",t,{capture:!0}),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("touchmove",e),window.removeEventListener("mouseup",t),window.removeEventListener("touchend",t)}},[]);const rt=o.useCallback(e=>{if(!i.selectedElements[0]){return}if(e&&i.selectedElementsIds.includes(e)){return}const t=V(i.selectedShapes),n=e?i.getElementById(e):{x:0,y:0,width:s.computedWidth,height:s.computedHeight,rotation:0},o=G(n),r=[];t.minX>o.maxX&&r.push({distance:t.minX-o.maxX,box1:t,box2:o,points:[{x:t.minX,y:t.minY+t.height/2},{x:o.maxX,y:t.minY+t.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),t.maxX<o.minX&&r.push({distance:o.minX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:t.minY+t.height/2},{x:o.minX,y:t.minY+t.height/2},{x:o.minX,y:o.minY+o.height/2}]}),t.minY>o.maxY&&r.push({box1:t,box2:o,distance:t.minY-o.maxY,points:[{x:t.minX+t.width/2,y:t.minY},{x:t.minX+t.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]}),t.maxY<o.minY&&r.push({box1:t,box2:o,distance:o.minY-t.maxY,points:[{x:t.minX+t.width/2,y:t.maxY},{x:t.minX+t.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]});const l=t.minX>=o.minX&&t.maxX<=o.maxX&&t.minY>=o.minY&&t.maxY<=o.maxY;if(l&&(r.push({distance:t.minX-o.minX,box1:t,box2:o,points:[{x:t.minX,y:t.minY+t.height/2},{x:o.minX,y:t.minY+t.height/2},{x:o.minX,y:o.minY+o.height/2}]}),r.push({distance:o.maxX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:t.minY+t.height/2},{x:o.maxX,y:t.minY+t.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),r.push({box1:t,box2:o,distance:t.minY-o.minY,points:[{x:t.minX+t.width/2,y:t.minY},{x:t.minX+t.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]}),r.push({box1:t,box2:o,distance:o.maxY-t.maxY,points:[{x:t.minX+t.width/2,y:t.maxY},{x:t.minX+t.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]})),0===r.length&&!l){const e=t.minX<o.maxX&&t.maxX>o.minX,n=t.minY<o.maxY&&t.maxY>o.minY;if(e&&n){const e=(Math.max(t.minY,o.minY)+Math.min(t.maxY,o.maxY))/2,n=(Math.max(t.minX,o.minX)+Math.min(t.maxX,o.maxX))/2;t.minX<o.minX?r.push({distance:o.minX-t.minX,box1:t,box2:o,points:[{x:t.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}):r.push({distance:t.minX-o.minX,box1:t,box2:o,points:[{x:t.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}),t.maxX>o.maxX?r.push({distance:t.maxX-o.maxX,box1:t,box2:o,points:[{x:o.maxX,y:e},{x:t.maxX,y:e},{x:o.maxX,y:e}]}):r.push({distance:o.maxX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:e},{x:o.maxX,y:e},{x:o.maxX,y:e}]}),t.minY<o.minY?r.push({box1:t,box2:o,distance:o.minY-t.minY,points:[{x:n,y:t.minY},{x:n,y:o.minY},{x:n,y:o.minY}]}):r.push({box1:t,box2:o,distance:t.minY-o.minY,points:[{x:n,y:t.minY},{x:n,y:o.minY},{x:n,y:o.minY}]}),t.maxY>o.maxY?r.push({box1:t,box2:o,distance:t.maxY-o.maxY,points:[{x:n,y:o.maxY},{x:n,y:t.maxY},{x:n,y:o.maxY}]}):r.push({box1:t,box2:o,distance:o.maxY-t.maxY,points:[{x:n,y:t.maxY},{x:n,y:o.maxY},{x:n,y:o.maxY}]})}}_e(e=>JSON.stringify(e)===JSON.stringify(r)?e:r)},[i,s]),st=o.useCallback(()=>{i.distanceGuidesVisible&&i.selectedElements[0]?rt():_e(null)},[i,rt]);o.useEffect(st,[i.distanceGuidesVisible,st]);const lt=i.distanceGuidesVisible||!!ze;o.useEffect(()=>{if(lt){return re(s,()=>i.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|"),st)}},[lt,st]);const at=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=Le.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{at.current=!0,clearTimeout(t),t=setTimeout(()=>{at.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const e=Ge.current;if(!e){return}const t=e.container(),n=new Map,o=e=>{var t,i;n.has(e.target)||[e.target,...null!==(i=null===(t=Ce.current)||void 0===t?void 0:t.nodes())&&void 0!==i?i:[]].forEach(e=>{n.has(e)||n.set(e,{x:e.x(),y:e.y()})})},r=e=>{e.target===Ce.current?n.clear():n.delete(e.target)};e.on("dragstart",o),e.on("dragend",r);const s=e=>{e.touches.length>=2&&c(()=>{var e,t;const o=new Map(n);(null===(e=Ce.current)||void 0===e?void 0:e.isDragging())&&Ce.current.stopDrag(),null===(t=Ce.current)||void 0===t||t.nodes().forEach(e=>{var t;const n=o.get(e);e.isDragging()&&e.stopDrag(),n&&(e.setAttrs(n),null===(t=i.getElementById(e.id()))||void 0===t||t.set({x:n.x,y:n.y}))})})};return t.addEventListener("touchstart",s,{passive:!0}),t.addEventListener("touchmove",s,{passive:!0}),()=>{e.off("dragstart",o),e.off("dragend",r),t.removeEventListener("touchstart",s),t.removeEventListener("touchmove",s)}},[i]);const ct=e=>{if(i.role===Y.VIEWER){return}if(i.activePage!==s&&s.select(),at.current){return}if(nt.current){return}const t=e.evt.shiftKey,n=e.evt.ctrlKey||e.evt.metaKey,o=t||n,r=e.target.findAncestor(".elements-container"),l=e.target.findAncestor(".page-abs-container"),a=e.target.findAncestor("Transformer"),c=e.target.findAncestor(".page-container",!0),d=!(o||r||a||l||c||tt.visible);if(i.activeTool&&"crop"!==i.activeTool.name&&!r&&!a){return}if(d){return i.selectElements([]),void i.selectPages([])}if(n&&!t){const e=Ge.current,t=null==e?void 0:e.getPointerPosition();if(!e||!t){return}const n=e.getAllIntersections(t),o=new Set,r=[];for(const l of[...n].reverse()){const e=l.findAncestor(".element",!0);if(!e){continue}const t=i.getElementById(e.id()),n=null==t?void 0:t.top;n&&(o.has(n.id)||(o.add(n.id),r.push(n.id)))}if(0===r.length){return}let s;if(1===i.selectedElementsIds.length){const e=i.selectedElementsIds[0],t=r.indexOf(e);s=-1===t?r[0]:r[(t+1)%r.length]}else{s=r[0]}return i.selectElements([s]),void i.selectPages([])}const m=e.target.findAncestor(".element-container",!0),u=(null==m?void 0:m.findOne(".element"))||e.target.findAncestor(".element",!0),h=i.getElementById(null==u?void 0:u.id());h&&"pendingEdit"in h&&h._setImageEditError(null);const g=null==h?void 0:h.top;let f;if("group"===P){f=null==g?void 0:g.id}else if(g){const e=i.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===g.id});f=e?null==h?void 0:h.id:null==g?void 0:g.id}else{f=void 0}const p=null==h?void 0:h.id;if(p&&i.selectedElementsIds.indexOf(p)>=0&&!t){return}const x=!!f&&i.selectedElementsIds.indexOf(f)>=0,v=e.target.findAncestor(".page-container",!0);if(f&&t&&!x){const e=oe(i.selectedElementsIds.concat([f]),e=>ie(i,e));i.selectElements(e),i.selectPages([])}else{f&&t&&x?i.selectElements(i.selectedElementsIds.filter(e=>e!==f)):!f||t||x?f&&!t&&x||(v?(t||i.selectElements([]),i.selectPages([s.id])):i.selectPages([])):(i.selectElements([f]),i.selectPages([]))}};I(Ce,i);const dt=i.activePage===s,mt=i._selectedPagesIds.includes(s.id),ut=null==y?void 0:y.PageControls,ht=null==y?void 0:y.Tooltip,gt=null==y?void 0:y.ContextMenu,ft=1/i.scale,pt=0/i.scale;return n("div",{ref:Le,onDragOver:e=>e.preventDefault(),onDrop:e=>{if(e.preventDefault(),i.role===Y.VIEWER){return}if(!Ge.current){return}Ge.current.setPointersPositions(e);const t=Ge.current.findOne(".elements-container"),n=null==t?void 0:t.getRelativePointerPosition(),o=Ge.current.getPointerPosition(),r=Ge.current.getAllIntersections(o).map(e=>e.findAncestor(".element",!0)).filter(Boolean),l=[...new Set(r.reverse())].map(e=>i.getElementById(e.id())).filter(e=>!!e),a=l[0];Me&&n&&(Me(n,a,{elements:l,page:s}),Me=null)},style:{position:"relative",width:d+"px",height:h+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+(dt?" active-page":""),children:[t(le,{ref:Ge,eventBatchFunc:c,width:Math.min(d,O.width+200),height:Math.min(O.height+200,h),onClick:ct,onTap:ct,onContextMenu:e=>{if(e.evt.preventDefault(),i.role===Y.VIEWER){return}const t=e.target.findAncestor(".element",!0),n=i.getElementById(null==t?void 0:t.id()),o=null==n?void 0:n.top,r=null==o?void 0:o.id;if(!r){return i.selectElements([]),void Je.close()}i.selectedElementsIds.indexOf(r)>=0||i.selectElements([r]),Je.open({x:e.evt.clientX,y:e.evt.clientY})},onMouseDown:e=>{it(e),ot(e)},onTouchStart:it,onMouseMove:e=>{if(!i.distanceGuidesVisible&&!e.evt.altKey){return void(ze&&_e(null))}const t=e.target.findAncestor(".element",!0),n=null==t?void 0:t.id();rt(n)},onDragStart:e=>{var t;const n=e.target.getStage();if(n&&n.getPointersPositions().length>=2){return void e.target.stopDrag()}const o=e.target.findAncestor(".element",!0);if(o){const n=i.getElementById(null==o?void 0:o.id()),r=null==n?void 0:n.top,s=null==r?void 0:r.id;if(!(n&&i.selectedElementsIds.indexOf(n.id)>=0||s&&i.selectedElementsIds.indexOf(s)>=0)&&s){i.selectElements([s]);const n=$e();e.target.stopDrag(),n.forEach(t=>{t.isDragging()||t.startDrag(e)}),null===(t=Ce.current)||void 0===t||t.startDrag(e),j()}}ze&&_e(null)},onDragMove:e=>{const t=Ce.current;t&&e.target===t&&t.nodes().forEach(e=>{const t=i.getElementById(e.id());!t||t.x===e.x()&&t.y===e.y()||t.set({x:e.x(),y:e.y()})})},pageId:s.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:n(f,{children:[t(pe,{width:d,height:h,fill:q.workspaceBackgroundColor}),t(u,{x:Ie,y:Re,scaleX:i.scale,scaleY:i.scale,name:"page-container",children:n(u,{name:"page-container-2",children:[t(u,{x:Ee.left,y:Ee.top,children:t(be,Object.assign({},Te,De,{transparencyBackgroundColor:q.transparencyBackgroundColor}))}),n(u,{isolated:s.hasBlendMode,children:[t(u,{name:"page-background-group",x:Ee.left,y:Ee.top,clip:De,children:t(ye,Object.assign({},Te))}),n(u,{x:Ee.left,y:Ee.top,name:"elements-container",listening:!i.isPlaying&&i.role!==Y.VIEWER,children:[t(x,{name:"elements-area",width:s.computedWidth,height:s.computedHeight,listening:!1}),t(Ae,{elements:s.children,store:i}),t(ke,{page:s})]})]}),t(v,{name:"bleed",fill:q.bleedColor,listening:!1,visible:i.bleedVisible&&(Ee.top>0||Ee.right>0||Ee.bottom>0||Ee.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:i,bottom:o,left:r}=ve,l=s.computedWidth,a=s.computedHeight,c=e._context;c.beginPath(),c.rect(0,0,r+l+i,n+a+o),c.rect(r,n,l,a),c.fillStyle=t.fill(),c.fill("evenodd")}}),B()===Pe&&t(Oe,{name:"hit-detection",x:-ft/2-pt,y:-ft/2-pt,width:Ye+ft+2*pt,height:je+ft+2*pt})]})}),t(p,{name:"workspace-background",points:[0,0,d,0,d,h,0,h,0,0,Ie,Re,Ie,h-Re,d-Ie,h-Re,d-Ie,Re,Ie,Re],listening:!1,closed:!0,fill:q.workspaceBackgroundColor}),t(u,{x:Ie,y:Re,scaleX:i.scale,scaleY:i.scale,children:t(x,{name:"page-highlight",hideInExport:!0,x:-ft/2-pt,y:-ft/2-pt,width:Ye+ft+2*pt,height:je+ft+2*pt,stroke:mt?q.activePageBorderColor:q.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),n(u,{x:Ie+Ee.left*i.scale,y:Re+Ee.top*i.scale,scaleX:i.scale,scaleY:i.scale,name:"page-abs-container",children:[t(S,{ref:Ce,name:Z,rotateAnchorAngle:Ze,onDragStart:e=>{var t;const n=e.target.getStage();n&&n.getPointersPositions().length>=2?e.target.stopDrag():((null===(t=e.evt)||void 0===t?void 0:t.altKey)&&A&&D(i.selectedElements,s,i),Fe.current||(Fe.current=!0,i.history.startTransaction()))},onDragEnd:()=>{Fe.current&&(i.history.endTransaction(),Fe.current=!1),et()},onTransformStart:()=>{Ue.setTransforming(!0),i.history.startTransaction(),We.current=new Map(Ne.map(e=>[e.id,{width:e.width,height:e.height}])),_e(null)},boundBoxFunc:(e,t)=>{var n,o,r,s;const l=null!==(o=null===(n=ee.boundBoxFunc)||void 0===n?void 0:n.call(ee,e,t))&&void 0!==o?o:t,a=Math.abs(l.width)<1||Math.abs(l.height)<1,c=Math.abs(e.width)<1||Math.abs(e.height)<1;if(a&&!c){return e}const d=i.selectedElements;if(1===d.length&&"table"===(null===(r=d[0])||void 0===r?void 0:r.type)){const t=d[0],n=Math.abs(e.width),i=Math.abs(e.height);if(n>0&&i>0){const o=Math.abs(l.width)/n,r=Math.abs(l.height)/i,s=t.width*o,a=t.height*r,c=Object.assign({},l);return s<t.minWidth&&(c.width=e.width),a<t.minHeight&&(c.height=e.height),K(c,l,e)}}return Ke&&Ne.length>0?((e,t,n,i,o)=>{const r=Math.abs(e.width),s=Math.abs(e.height);if(0===r||0===s){return t}const l=Math.abs(t.width)/r,a=Math.abs(t.height)/s,c=!U(n),d=e=>i.some(t=>{var n;const i=null==o?void 0:o.get(t.id),r=null!==(n="x"===e?null==i?void 0:i.width:null==i?void 0:i.height)&&void 0!==n?n:"x"===e?t.width:t.height,s="x"===e?t.width:t.height,d="x"===e?l:a,m=N(t,e,c,r);return r>=m&&s*d<m}),m=Object.assign({},t),u=t.width*e.width<0,h=t.height*e.height<0,g=u||d("x"),f=h||d("y"),p=f||!c&&g;return(g||!c&&f)&&(m.width=e.width),p&&(m.height=e.height),K(m,t,e)})(e,l,(null===(s=Ce.current)||void 0===s?void 0:s.getActiveAnchor())||"",Ne,We.current):l},onTransform:e=>{const t=Ce.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{qe()},0)},onTransformEnd:e=>{Ue.setTransforming(!1),Ue.setLabel({}),i.history.endTransaction(),We.current=null,et()},visible:!i.isPlaying}),t(Xe,{store:i,gesture:Ue,transformerStyle:ee,onChange:Qe}),ze&&ze.map(({points:e,distance:o,box1:r,box2:s},l)=>n(u,{name:"distances-container",hideInExport:!0,listening:!1,children:[t(x,Object.assign({},r,{stroke:q.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(x,Object.assign({},s,{stroke:q.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(p,{points:[e[0].x,e[0].y,e[1].x,e[1].y],stroke:q.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),t(p,{points:[e[1].x,e[1].y,e[2].x,e[2].y],stroke:q.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(g,{x:(e[0].x+e[1].x)/2,y:(e[0].y+e[1].y)/2,offsetY:-10,scaleX:1/i.scale,scaleY:1/i.scale,children:[t(w,{cornerRadius:5,fill:q.distanceGuideStyle.labelFill,pointerDirection:"down"}),t(E,{align:"center",verticalAlign:"middle",fill:q.distanceGuideStyle.labelTextFill,padding:5,text:_({unit:i.unit,dpi:i.dpi,px:o})})]})]},l)),s._rendering&&n(u,{children:[t(x,{width:Ye,height:je,fill:"rgba(255,255,255,0.9)"}),t(E,{text:"Rendering...",fontSize:60,width:Ye,height:je,align:"center",verticalAlign:"middle"})]}),ht&&t(ht,{components:y,store:i,page:s,stageRef:Ge,tooltipSafeArea:z}),gt&&t(ne,{children:t(gt,Object.assign({components:y,store:i},Je.props))}),t(R,{store:i,pageId:s.id})]}),t(M,{store:i,page:s,width:d,height:h,scale:i.scale,xPadding:Ie,yPadding:Re,bleedLeft:Ee.left,bleedTop:Ee.top}),t(Se,{gesture:Ue,store:i}),t(we,{selection:tt}),C()>0&&n(u,{hideInExport:!0,children:[t(Be,{name:"cache-bounds",x:d-(1===C()?250:210),y:h-(1===C()?95:45)}),t(Be,{name:"cache-bounds-t",x:10,y:10})]}),W()&&t(E,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:d-170,y:h-18,onMouseEnter:e=>{e.target.getStage().container().style.cursor="pointer"},onMouseLeave:e=>{e.target.getStage().container().style.cursor=""},onTouchStart:e=>{e.cancelBubble=!0},onMouseDown:e=>{e.cancelBubble=!0},onClick:()=>{window.open("https://polotno.com","_blank","noopener")},onTap:()=>{window.open("https://polotno.com","_blank","noopener")}}),t(u,{name:"line-guides"})]})}),Ve&&ut&&t(ut,{store:i,page:s,xPadding:Ie,yPadding:Re,layout:T})]})});
1
+ var e=this&&this.__rest||function(e,t){var n={};for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++){t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(n[i[o]]=e[i[o]])}}return n};import{jsx as t,jsxs as n,Fragment as i}from"react/jsx-runtime";import o from"react";import{clamp as r}from"../lib/utils.js";import{observer as l,useLocalObservable as s}from"mobx-react-lite";import{action as a,runInAction as c}from"mobx";import{isAlive as d}from"mobx-state-tree";import{ZERO_BLEED as m}from"@polotno/schema";import{Group as u,Image as h,Label as g,Layer as f,Line as p,Rect as x,Shape as v,Stage as b,KonvaRenderer as y,Tag as w,Text as E,Transformer as S}from"react-konva";import{useWorkspaceStyle as X}from"./workspace-style.js";import{ROLES as Y}from"../model/store.js";import k from"konva";import A from"./element.js";import{DrawingLayer as M}from"./drawing-layer.js";import{ensureDragOrder as j,useSnap as I}from"./use-transformer-snap.js";import{useImageLoader as P,useSizedImage as O}from"./image-element.js";import{FadeOverlay as R}from"./fade-overlay.js";import{getCrop as T}from"../utils/crop.js";import{cloneBehind as D}from"../utils/duplicate.js";import{___ as B,____ as C,isCreditVisible as W}from"../utils/validate-key.js";import{getClientRect as G,getCorners as L,getTotalClientRect as V,polygonsIntersect as F}from"../utils/math.js";import{pxToUnitRounded as z,pxToUnitString as _}from"../utils/unit.js";import{isCornerAnchor as U,isRotateAnchor as Z,MAIN_TRANSFORMER_NAME as H}from"../utils/px.js";import{canStretchAxis as J,isAxisAligned as N,leafMinSize as K}from"../utils/selection-transform.js";import{restoreHeldPoint as $}from"./transform-box.js";import{flags as Q}from"../utils/flags.js";import{textOverflowOf as q}from"../utils/text-sizing.js";import{useColor as ee}from"./use-color.js";import{isGradient as te}from"@polotno/core/gradient";import{toDisplayColor as ne}from"@polotno/core/color";import{Html as ie}from"react-konva-utils";import{getParentGroupId as oe,withoutDescendants as re}from"../utils/selection.js";import{nodeReaction as le}from"../utils/mobx.js";import{useEvent as se}from"../hooks/use-event.js";const ae=o.version.startsWith("18.")?o.forwardRef((e,n)=>(o.useLayoutEffect(()=>{y.flushSync()}),t(b,Object.assign({},e,{ref:n})))):b,ce={enabledAnchors:["top-left","top-center","top-right","middle-left","bottom-left","bottom-right","bottom-center","middle-right"],rotateEnabled:!0,rotationSnaps:[0,45,90,135,180,225,270,315],ignoreStroke:!0,flipEnabled:!1,keepRatio:!0},de=e=>{if(!e.length){return null}const t=N(e[0].rotation)?0:e[0].rotation;return e.every(e=>N(e.rotation-t))?t:null},me=e=>e.selectedShapes.filter(t=>{var n;return(null===(n=e.activeTool)||void 0===n?void 0:n.elementId)!==t.id&&!t.pendingEdit}),ue=e=>e.selectedShapes.some(e=>!e.resizable),he=e=>e.selectedShapes.some(e=>!e.draggable),ge=e=>{var t;return 1!==e.selectedElements.length||"group"===(null===(t=e.selectedElements[0])||void 0===t?void 0:t.type)},fe=e=>{var t;const n=me(e),i=ge(e),o=null!==(t=de(n))&&void 0!==t?t:0,r=e=>i&&n.length>0&&n.every(t=>J(t,e,o));return{boxRotation:o,canStretchX:r("x"),canStretchY:r("y")}},pe={text:{enabledAnchors:["top-left","top-right","middle-left","bottom-left","bottom-right","middle-right"]},svg:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},gif:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},line:{enabledAnchors:[],borderEnabled:!1,rotateEnabled:!1},image:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},many:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},group:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},table:{keepRatio:!1,enabledAnchors:["top-left","top-center","top-right","middle-left","middle-right","bottom-left","bottom-center","bottom-right"]}};export function registerTransformerAttrs(e,t){pe[e]=pe[e]||t,Object.assign(pe[e],t)}const xe=e=>t(x,Object.assign({},e,{preventDefault:!1})),ve=n=>{var{url:i}=n,o=e(n,["url"]);const[r,l]=O(i),s=r?T(r,{width:o.width,height:o.height},"center-middle"):{};return P(l,i,"page background"),t(h,Object.assign({image:r},o,s))},be=e=>{const n=ee({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return t(x,Object.assign({},e,n))},ye=n=>{var{background:i,transparencyBackgroundColor:r}=n,l=e(n,["background","transparencyBackgroundColor"]);const s=o.useMemo(()=>{if(te(i)){return!0}const e=k.Util.colorToRGBA(i);return!e||e.a<1},[i]),a=o.useMemo(()=>{if(!s||r){return}const e=document.createElement("canvas"),t=30;e.width=60,e.height=60;const n=e.getContext("2d");return n&&(n.fillStyle="black",n.fillRect(t,0,t,t),n.fillRect(0,t,t,t)),e},[s,r]);return s?t(x,r?Object.assign({fill:r},l,{hideInExport:!0}):Object.assign({fillPatternImage:a},l,{opacity:.1,hideInExport:!0})):null},we=n=>{var{background:i}=n,r=e(n,["background"]);return o.useMemo(()=>!!k.Util.colorToRGBA(i)||te(i),[i])?t(be,Object.assign({fill:i},r)):t(ve,Object.assign({url:i},r))},Ee=l(({selection:e})=>{const{selectionRectStyle:n}=X();return e.visible?t(x,{name:"selection",x:Math.min(e.x1,e.x2),y:Math.min(e.y1,e.y2),width:Math.abs(e.x1-e.x2),height:Math.abs(e.y1-e.y2),fill:n.fill,stroke:n.stroke,strokeWidth:n.strokeWidth}):null});function Se(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const Xe=l(({gesture:e,store:o})=>{const{transformLabelStyle:r}=X(),{x:l,y:s,width:a,height:c,rotation:d,anchor:m}=e.label;if(void 0===l||void 0===s||void 0===a||void 0===c||void 0===d||void 0===m){return null}const u=G({x:l,y:s,width:a,height:c,rotation:k.Util.radToDeg(d)}),h=null!==m&&Z(m),f=(c/2+70)*Math.cos(d-Math.PI/2),p=(c/2+70)*Math.sin(d-Math.PI/2),x=z({unit:o.unit,dpi:o.dpi,px:a/o.scale,precious:"px"===o.unit?0:1})+" x "+z({unit:o.unit,dpi:o.dpi,px:c/o.scale,precious:"px"===o.unit?0:1})+("px"===o.unit?"":" "+o.unit);return n(i,{children:[n(g,{x:(u.minX+u.maxX)/2+f,y:(u.minY+u.maxY)/2+p,offsetX:14,offsetY:14,visible:h,children:[t(w,{cornerRadius:5,fill:r.fill}),t(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:Se(d).toString()+"°"})]}),n(g,{x:(u.minX+u.maxX)/2,y:u.maxY+20,visible:!h,children:[t(w,{cornerRadius:5,fill:r.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),t(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:x})]})]})}),Ye=l(({store:e,gesture:t,transformerStyle:n,onChange:i})=>{var r;const l=e.selectedElements.some(e=>e.curveEnabled),s=e.selectedElements.some(e=>!e.visible),a=JSON.stringify(me(e).map(e=>e.id)),c=!ge(e)&&e.selectedElements[0].keepRatio,d=!ge(e)&&"text"===(null===(r=e.selectedElements[0])||void 0===r?void 0:r.type)&&"fit"===q(e.selectedElements[0]),m=t.transforming?null:fe(e);return o.useLayoutEffect(()=>{i()},[e.selectedShapes,a,ue(e),s,he(e),l,c,d,t.transforming,null==m?void 0:m.canStretchX,null==m?void 0:m.canStretchY,null==m?void 0:m.boxRotation,n]),null}),ke=()=>{console.warn("Polotno warning: onClick callback is deprecated. Just stop using it. Polotno will do selection automatically.")},Ae=l(({page:e})=>{const t=e._exportingOrRendering||e._forceMount;return o.useEffect(()=>{if(t&&d(e)){return e.set({_exportRendered:!0}),()=>{d(e)&&e.set({_exportRendered:!1})}}},[e,t]),null}),Me=l(({elements:e,store:n})=>{const i=e.filter(e=>e.alwaysOnTop),r=e.filter(e=>!e.alwaysOnTop).concat(i);return t(o.Fragment,{children:r.map(e=>t(A,{store:n,element:e,onClick:ke},e.id))})});export const useContextMenu=({store:e})=>{const[t,n]=o.useState(!1),[i,r]=o.useState({x:0,y:0}),l=o.useCallback(e=>{n(!0),r(e)},[]);return{open:l,close:o.useCallback(()=>{n(!1)},[]),props:{isOpen:t,offset:i,setIsOpen:n}}};let je=null;export const registerNextDomDrop=e=>{je=e};const Ie=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),Pe=atob("I2MwMzkyYg=="),Oe=atob("djAuOS4y"),Re=e=>t(i,{children:n(g,{fill:Pe,height:200,children:[t(w,{fill:Pe}),t(E,Object.assign({},e,{fill:"white",text:Ie,height:void 0,padding:10,fontSize:20}))]})}),Te=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),De=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),Be=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),Ce=e=>{const i=1===C(),o=i?De:Te,r=i?230:195;return n(u,Object.assign({},e,{listening:!1,children:[t(x,{fill:Pe,width:r,height:24,cornerRadius:2}),t(E,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),i&&t(E,{text:Be,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default l(({store:i,page:l,width:d,height:h,pageControlsEnabled:b,components:y,altCloneEnabled:A,groupSelectionMode:P="drill",viewportSize:O,layout:T="vertical",tooltipSafeArea:z,transformerSafeArea:Z})=>{const J=X(),ee=J.transformerStyle,te=ce,ve=l.computedBleed,be=i.bleedVisible?ve:m,Se=l.computedWidth+be.left+be.right,ke=l.computedHeight+be.top+be.bottom,Ie=(d-Se*i.scale)/2,Pe=(h-ke*i.scale)/2,Te={x:-ve.left,y:-ve.top,width:l.computedWidth+ve.left+ve.right,height:l.computedHeight+ve.top+ve.bottom,background:ne(l.background),name:"page-background",preventDefault:!1},De={x:-be.left,y:-be.top,width:Se,height:ke},Be=o.useRef(null),We=o.useRef(null),Ge=o.useRef(null),Le=o.useRef(null),Ve=null==b||b,Fe=o.useRef(!1),[ze,_e]=o.useState(null),Ue=s(()=>({transforming:!1,label:{},setLabel(e){this.label=e},setTransforming(e){this.transforming=e}})),[Ze,He]=o.useState(0),Je=useContextMenu({store:i}),Ne=me(i),Ke=ge(i),$e=()=>{var t,n,o;const r=Be.current;if(!r){return[]}const l=r.getStage();if(!l){return[]}const s=me(i),a=new Set(s.map(e=>e.id)),c=new Map;a.size&&l.find(e=>{const t=e.id();return a.has(t)&&!c.has(t)&&c.set(t,e),!1});const d=s.map(e=>c.get(e.id)).filter(e=>!!e),m=1===i.selectedElements.length,u=m&&(null===(t=i.selectedElements[0])||void 0===t?void 0:t.type)||"many",h=pe[u],g=Object.assign(Object.assign({},te),h||{});h&&("svg"!==u&&"image"!==u&&"gif"!==u||i.selectedElements[0].keepRatio||(g.enabledAnchors=te.enabledAnchors),"text"!==u||!Q.textVerticalResizeEnabled&&"fit"!==q(i.selectedElements[0])||(g.enabledAnchors=null===(n=pe.text.enabledAnchors)||void 0===n?void 0:n.concat(["bottom-center","top-center"])),"text"===u&&m&&i.selectedElements[0].curveEnabled&&(g.enabledAnchors=pe.many.enabledAnchors));const f=fe(i);(f.canStretchX||f.canStretchY)&&(g.enabledAnchors=[...g.enabledAnchors||[],...f.canStretchX?["middle-left","middle-right"]:[],...f.canStretchY?["top-center","bottom-center"]:[]]);const{boundBoxFunc:p}=ee,x=e(ee,["boundBoxFunc"]);r.setAttrs(Object.assign(Object.assign(Object.assign({},g),x),{name:H})),ue(i)&&r.enabledAnchors([]),he(i)&&r.rotateEnabled(!1);const v=r.nodes();if(v.length===d.length&&d.every((e,t)=>v[t]===e)||r.nodes(d),ge(i)&&d.length){const e=de(s);null===e||N(r.rotation()-e)||(r.rotation(e),r.forceUpdate())}return null===(o=r.getLayer())||void 0===o||o.batchDraw(),d},Qe=se(()=>{var e;if(null===(e=Be.current)||void 0===e?void 0:e.isTransforming()){return}const t=$e();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),j())}),qe=()=>{var e,t;const n=Be.current;if(!n){return}if(!n.isTransforming()){return void Ue.setLabel({})}if(!n.nodes().length){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();Ue.setLabel({anchor:o,x:i.x-(null!==(e=null==r?void 0:r.x())&&void 0!==e?e:0),y:i.y-(null!==(t=null==r?void 0:r.y())&&void 0!==t?t:0),rotation:i.rotation,width:i.width,height:i.height})},et=o.useCallback(()=>{var e;if(null===(e=Be.current)||void 0===e?void 0:e.isTransforming()){return}const t=Be.current;if(!t||!t.nodes().length){return void He(0)}const n=t.getClientRect();if(!t.getStage()){return}const i=function(e,t,n,i=0){const o=e.x+e.width/2,r=e.y+e.height/2,l=e.width/2,s=e.height/2,a=i*Math.PI/180,c=e=>{const t=e*Math.PI/180,i=(e%360+360)%360;let c;c=0===i||180===i?s:l;const d=Math.sin(t)*(c+n),m=-Math.cos(t)*(c+n),u=d*Math.cos(a)-m*Math.sin(a),h=d*Math.sin(a)+m*Math.cos(a);return{x:o+u,y:r+h}},d=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(d(c(0))){return 0}for(const m of[180,270,90]){if(d(c(m))){return m}}return 0}(n,{top:(null==Z?void 0:Z.top)||0,bottom:h-((null==Z?void 0:Z.bottom)||0),left:(null==Z?void 0:Z.left)||0,right:d-((null==Z?void 0:Z.right)||0)},ee.rotateAnchorOffset,t.rotation());He(i)},[Z,d,h]);o.useEffect(()=>{var e;null===(e=Be.current)||void 0===e||e.update(),qe(),et()},[i.scale,et]),o.useEffect(()=>{et()},[i.selectedShapes,et]);const tt=s(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),nt=o.useRef(!1),it=e=>{const t=Ge.current;if(!t||i.role===Y.VIEWER||i.activePage!==l||i.isPlaying||"button"in e.evt&&0!==e.evt.button||1!==t.getPointersPositions().length||!e.target.findAncestor(".elements-container")||!e.target.findAncestor(".element",!0)||!i.selectedShapes.length||he(i)){return}const n=t.getPointerPosition();if(!n){return}const o=$e();if(o.some(t=>t===e.target||t.isAncestorOf(e.target))){return}const r=o.find(e=>!!(e.draggable()&&e.isVisible()&&e.isListening())&&(e instanceof k.Shape?[e]:e instanceof k.Container?e.find("Shape"):[]).some(e=>e.intersects(n)));if(r){for(const[t,n]of k.DD._dragElements){"ready"!==n.dragStatus||n.node!==e.target&&!n.node.isAncestorOf(e.target)||k.DD._dragElements.delete(t)}r._createDragElement(e),"touches"in e.evt&&e.evt.cancelable&&r.preventDefault()&&e.evt.preventDefault()}},ot=a(e=>{var t,n,o,r,l,s,a;if(i.role===Y.VIEWER){return}if(nt.current=!1,i.activeTool&&"crop"!==i.activeTool.name){return}const c=e.target.findAncestor(".elements-container"),d=e.target.findAncestor("Transformer"),m=e.target.findAncestor(".page-abs-container");if(c||d||m){return}const u=null===(t=e.target.getStage())||void 0===t?void 0:t.getPointerPosition();u&&(u.x-=null!==(o=null===(n=e.target.getStage())||void 0===n?void 0:n.x())&&void 0!==o?o:0,u.y-=null!==(l=null===(r=e.target.getStage())||void 0===r?void 0:r.y())&&void 0!==l?l:0,tt.visible=!0,tt.x1=u.x,tt.y1=u.y,tt.x2=u.x,tt.y2=u.y,(null!==(a=null===(s=e.target.getStage())||void 0===s?void 0:s.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(tt.visible=!1))});(({stageRef:e,containerRef:t,viewportSize:n})=>{o.useEffect(()=>{var n;const i=null===(n=t.current)||void 0===n?void 0:n.closest(".polotno-workspace-inner");function o(){var n;if(!e.current){return}const o=null===(n=t.current)||void 0===n?void 0:n.getBoundingClientRect(),l=null==i?void 0:i.getBoundingClientRect();if(!o||!l){return}const s=r(l.left-o.left-100,0,Math.max(0,o.width-e.current.width())),a=r(l.top-o.top-100,0,Math.max(0,o.height-e.current.height()));e.current.position({x:-s,y:-a}),e.current.container().style.transform=`translate(${s}px, ${a}px)`}return o(),null==i||i.addEventListener("scroll",o),()=>{null==i||i.removeEventListener("scroll",o)}},[n.width,n.height])})({stageRef:Ge,containerRef:Le,viewportSize:O}),o.useEffect(()=>{const e=a(e=>{var t,n,i,o,r,l;if(!tt.visible){return}null===(t=Ge.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=Ge.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(o=null===(i=Ge.current)||void 0===i?void 0:i.x())&&void 0!==o?o:0,s.y-=null!==(l=null===(r=Ge.current)||void 0===r?void 0:r.y())&&void 0!==l?l:0):s={x:tt.x2,y:tt.y2},tt.x2=s.x,tt.y2=s.y}),t=a(()=>{if(!tt.visible){return}if(!Ge.current){return}const e=Ge.current.findOne(".selection"),t=e?e.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(t.width&&t.height){const e=[{x:t.x,y:t.y},{x:t.x+t.width,y:t.y},{x:t.x+t.width,y:t.y+t.height},{x:t.x,y:t.y+t.height}],n=[];Ge.current.find(".element").forEach(t=>{var o;const r=i.getElementById(t.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=t.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const c=L(r.a).map(e=>a.point(e));F(e,c)&&n.push(r.top.id)});const o=[...new Set(n)];nt.current=!0,i.selectElements(o)}tt.visible=!1});return window.addEventListener("mousemove",e),window.addEventListener("touchmove",e),window.addEventListener("mouseup",t,{capture:!0}),window.addEventListener("touchend",t,{capture:!0}),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("touchmove",e),window.removeEventListener("mouseup",t),window.removeEventListener("touchend",t)}},[]);const rt=o.useCallback(e=>{if(!i.selectedElements[0]){return}if(e&&i.selectedElementsIds.includes(e)){return}const t=V(i.selectedShapes),n=e?i.getElementById(e):{x:0,y:0,width:l.computedWidth,height:l.computedHeight,rotation:0},o=G(n),r=[];t.minX>o.maxX&&r.push({distance:t.minX-o.maxX,box1:t,box2:o,points:[{x:t.minX,y:t.minY+t.height/2},{x:o.maxX,y:t.minY+t.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),t.maxX<o.minX&&r.push({distance:o.minX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:t.minY+t.height/2},{x:o.minX,y:t.minY+t.height/2},{x:o.minX,y:o.minY+o.height/2}]}),t.minY>o.maxY&&r.push({box1:t,box2:o,distance:t.minY-o.maxY,points:[{x:t.minX+t.width/2,y:t.minY},{x:t.minX+t.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]}),t.maxY<o.minY&&r.push({box1:t,box2:o,distance:o.minY-t.maxY,points:[{x:t.minX+t.width/2,y:t.maxY},{x:t.minX+t.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]});const s=t.minX>=o.minX&&t.maxX<=o.maxX&&t.minY>=o.minY&&t.maxY<=o.maxY;if(s&&(r.push({distance:t.minX-o.minX,box1:t,box2:o,points:[{x:t.minX,y:t.minY+t.height/2},{x:o.minX,y:t.minY+t.height/2},{x:o.minX,y:o.minY+o.height/2}]}),r.push({distance:o.maxX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:t.minY+t.height/2},{x:o.maxX,y:t.minY+t.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),r.push({box1:t,box2:o,distance:t.minY-o.minY,points:[{x:t.minX+t.width/2,y:t.minY},{x:t.minX+t.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]}),r.push({box1:t,box2:o,distance:o.maxY-t.maxY,points:[{x:t.minX+t.width/2,y:t.maxY},{x:t.minX+t.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]})),0===r.length&&!s){const e=t.minX<o.maxX&&t.maxX>o.minX,n=t.minY<o.maxY&&t.maxY>o.minY;if(e&&n){const e=(Math.max(t.minY,o.minY)+Math.min(t.maxY,o.maxY))/2,n=(Math.max(t.minX,o.minX)+Math.min(t.maxX,o.maxX))/2;t.minX<o.minX?r.push({distance:o.minX-t.minX,box1:t,box2:o,points:[{x:t.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}):r.push({distance:t.minX-o.minX,box1:t,box2:o,points:[{x:t.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}),t.maxX>o.maxX?r.push({distance:t.maxX-o.maxX,box1:t,box2:o,points:[{x:o.maxX,y:e},{x:t.maxX,y:e},{x:o.maxX,y:e}]}):r.push({distance:o.maxX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:e},{x:o.maxX,y:e},{x:o.maxX,y:e}]}),t.minY<o.minY?r.push({box1:t,box2:o,distance:o.minY-t.minY,points:[{x:n,y:t.minY},{x:n,y:o.minY},{x:n,y:o.minY}]}):r.push({box1:t,box2:o,distance:t.minY-o.minY,points:[{x:n,y:t.minY},{x:n,y:o.minY},{x:n,y:o.minY}]}),t.maxY>o.maxY?r.push({box1:t,box2:o,distance:t.maxY-o.maxY,points:[{x:n,y:o.maxY},{x:n,y:t.maxY},{x:n,y:o.maxY}]}):r.push({box1:t,box2:o,distance:o.maxY-t.maxY,points:[{x:n,y:t.maxY},{x:n,y:o.maxY},{x:n,y:o.maxY}]})}}_e(e=>JSON.stringify(e)===JSON.stringify(r)?e:r)},[i,l]),lt=o.useCallback(()=>{i.distanceGuidesVisible&&i.selectedElements[0]?rt():_e(null)},[i,rt]);o.useEffect(lt,[i.distanceGuidesVisible,lt]);const st=i.distanceGuidesVisible||!!ze;o.useEffect(()=>{if(st){return le(l,()=>i.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|"),lt)}},[st,lt]);const at=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=Le.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{at.current=!0,clearTimeout(t),t=setTimeout(()=>{at.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const e=Ge.current;if(!e){return}const t=e.container(),n=new Map,o=e=>{var t,i;n.has(e.target)||[e.target,...null!==(i=null===(t=Be.current)||void 0===t?void 0:t.nodes())&&void 0!==i?i:[]].forEach(e=>{n.has(e)||n.set(e,{x:e.x(),y:e.y()})})},r=e=>{e.target===Be.current?n.clear():n.delete(e.target)};e.on("dragstart",o),e.on("dragend",r);const l=e=>{e.touches.length>=2&&c(()=>{var e,t;const o=new Map(n);(null===(e=Be.current)||void 0===e?void 0:e.isDragging())&&Be.current.stopDrag(),null===(t=Be.current)||void 0===t||t.nodes().forEach(e=>{var t;const n=o.get(e);e.isDragging()&&e.stopDrag(),n&&(e.setAttrs(n),null===(t=i.getElementById(e.id()))||void 0===t||t.set({x:n.x,y:n.y}))})})};return t.addEventListener("touchstart",l,{passive:!0}),t.addEventListener("touchmove",l,{passive:!0}),()=>{e.off("dragstart",o),e.off("dragend",r),t.removeEventListener("touchstart",l),t.removeEventListener("touchmove",l)}},[i]);const ct=e=>{if(i.role===Y.VIEWER){return}if(i.activePage!==l&&l.select(),at.current){return}if(nt.current){return}const t=e.evt.shiftKey,n=e.evt.ctrlKey||e.evt.metaKey,o=t||n,r=e.target.findAncestor(".elements-container"),s=e.target.findAncestor(".page-abs-container"),a=e.target.findAncestor("Transformer"),c=e.target.findAncestor(".page-container",!0),d=!(o||r||a||s||c||tt.visible);if(i.activeTool&&"crop"!==i.activeTool.name&&!r&&!a){return}if(d){return i.selectElements([]),void i.selectPages([])}if(n&&!t){const e=Ge.current,t=null==e?void 0:e.getPointerPosition();if(!e||!t){return}const n=e.getAllIntersections(t),o=new Set,r=[];for(const s of[...n].reverse()){const e=s.findAncestor(".element",!0);if(!e){continue}const t=i.getElementById(e.id()),n=null==t?void 0:t.top;n&&(o.has(n.id)||(o.add(n.id),r.push(n.id)))}if(0===r.length){return}let l;if(1===i.selectedElementsIds.length){const e=i.selectedElementsIds[0],t=r.indexOf(e);l=-1===t?r[0]:r[(t+1)%r.length]}else{l=r[0]}return i.selectElements([l]),void i.selectPages([])}const m=e.target.findAncestor(".element-container",!0),u=(null==m?void 0:m.findOne(".element"))||e.target.findAncestor(".element",!0),h=i.getElementById(null==u?void 0:u.id());h&&"pendingEdit"in h&&h._setImageEditError(null);const g=null==h?void 0:h.top;let f;if("group"===P){f=null==g?void 0:g.id}else if(g){const e=i.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===g.id});f=e?null==h?void 0:h.id:null==g?void 0:g.id}else{f=void 0}const p=null==h?void 0:h.id;if(p&&i.selectedElementsIds.indexOf(p)>=0&&!t){return}const x=!!f&&i.selectedElementsIds.indexOf(f)>=0,v=e.target.findAncestor(".page-container",!0);if(f&&t&&!x){const e=re(i.selectedElementsIds.concat([f]),e=>oe(i,e));i.selectElements(e),i.selectPages([])}else{f&&t&&x?i.selectElements(i.selectedElementsIds.filter(e=>e!==f)):!f||t||x?f&&!t&&x||(v?(t||i.selectElements([]),i.selectPages([l.id])):i.selectPages([])):(i.selectElements([f]),i.selectPages([]))}};I(Be,i);const dt=i.activePage===l,mt=i._selectedPagesIds.includes(l.id),ut=null==y?void 0:y.PageControls,ht=null==y?void 0:y.Tooltip,gt=null==y?void 0:y.ContextMenu,ft=1/i.scale,pt=0/i.scale;return n("div",{ref:Le,onDragOver:e=>e.preventDefault(),onDrop:e=>{if(e.preventDefault(),i.role===Y.VIEWER){return}if(!Ge.current){return}Ge.current.setPointersPositions(e);const t=Ge.current.findOne(".elements-container"),n=null==t?void 0:t.getRelativePointerPosition(),o=Ge.current.getPointerPosition(),r=Ge.current.getAllIntersections(o).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(r.reverse())].map(e=>i.getElementById(e.id())).filter(e=>!!e),a=s[0];je&&n&&(je(n,a,{elements:s,page:l}),je=null)},style:{position:"relative",width:d+"px",height:h+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+(dt?" active-page":""),children:[t(ae,{ref:Ge,eventBatchFunc:c,width:Math.min(d,O.width+200),height:Math.min(O.height+200,h),onClick:ct,onTap:ct,onContextMenu:e=>{if(e.evt.preventDefault(),i.role===Y.VIEWER){return}const t=e.target.findAncestor(".element",!0),n=i.getElementById(null==t?void 0:t.id()),o=null==n?void 0:n.top,r=null==o?void 0:o.id;if(!r){return i.selectElements([]),void Je.close()}i.selectedElementsIds.indexOf(r)>=0||i.selectElements([r]),Je.open({x:e.evt.clientX,y:e.evt.clientY})},onMouseDown:e=>{it(e),ot(e)},onTouchStart:it,onMouseMove:e=>{if(!i.distanceGuidesVisible&&!e.evt.altKey){return void(ze&&_e(null))}const t=e.target.findAncestor(".element",!0),n=null==t?void 0:t.id();rt(n)},onDragStart:e=>{var t;const n=e.target.getStage();if(n&&n.getPointersPositions().length>=2){return void e.target.stopDrag()}const o=e.target.findAncestor(".element",!0);if(o){const n=i.getElementById(null==o?void 0:o.id()),r=null==n?void 0:n.top,l=null==r?void 0:r.id;if(!(n&&i.selectedElementsIds.indexOf(n.id)>=0||l&&i.selectedElementsIds.indexOf(l)>=0)&&l){i.selectElements([l]);const n=$e();e.target.stopDrag(),n.forEach(t=>{t.isDragging()||t.startDrag(e)}),null===(t=Be.current)||void 0===t||t.startDrag(e),j()}}ze&&_e(null)},onDragMove:e=>{const t=Be.current;t&&e.target===t&&t.nodes().forEach(e=>{const t=i.getElementById(e.id());!t||t.x===e.x()&&t.y===e.y()||t.set({x:e.x(),y:e.y()})})},pageId:l.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:n(f,{children:[t(xe,{width:d,height:h,fill:J.workspaceBackgroundColor}),t(u,{x:Ie,y:Pe,scaleX:i.scale,scaleY:i.scale,name:"page-container",children:n(u,{name:"page-container-2",children:[t(u,{x:be.left,y:be.top,children:t(ye,Object.assign({},Te,De,{transparencyBackgroundColor:J.transparencyBackgroundColor}))}),n(u,{isolated:l.hasBlendMode,children:[t(u,{name:"page-background-group",x:be.left,y:be.top,clip:De,children:t(we,Object.assign({},Te))}),n(u,{x:be.left,y:be.top,name:"elements-container",listening:!i.isPlaying&&i.role!==Y.VIEWER,children:[t(x,{name:"elements-area",width:l.computedWidth,height:l.computedHeight,listening:!1}),t(Me,{elements:l.children,store:i}),t(Ae,{page:l})]})]}),t(v,{name:"bleed",fill:J.bleedColor,listening:!1,visible:i.bleedVisible&&(be.top>0||be.right>0||be.bottom>0||be.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:i,bottom:o,left:r}=ve,s=l.computedWidth,a=l.computedHeight,c=e._context;c.beginPath(),c.rect(0,0,r+s+i,n+a+o),c.rect(r,n,s,a),c.fillStyle=t.fill(),c.fill("evenodd")}}),B()===Oe&&t(Re,{name:"hit-detection",x:-ft/2-pt,y:-ft/2-pt,width:Se+ft+2*pt,height:ke+ft+2*pt})]})}),t(p,{name:"workspace-background",points:[0,0,d,0,d,h,0,h,0,0,Ie,Pe,Ie,h-Pe,d-Ie,h-Pe,d-Ie,Pe,Ie,Pe],listening:!1,closed:!0,fill:J.workspaceBackgroundColor}),t(u,{x:Ie,y:Pe,scaleX:i.scale,scaleY:i.scale,children:t(x,{name:"page-highlight",hideInExport:!0,x:-ft/2-pt,y:-ft/2-pt,width:Se+ft+2*pt,height:ke+ft+2*pt,stroke:mt?J.activePageBorderColor:J.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),n(u,{x:Ie+be.left*i.scale,y:Pe+be.top*i.scale,scaleX:i.scale,scaleY:i.scale,name:"page-abs-container",children:[t(S,{ref:Be,name:H,rotateAnchorAngle:Ze,onDragStart:e=>{var t;const n=e.target.getStage();n&&n.getPointersPositions().length>=2?e.target.stopDrag():((null===(t=e.evt)||void 0===t?void 0:t.altKey)&&A&&D(i.selectedElements,l,i),Fe.current||(Fe.current=!0,i.history.startTransaction()))},onDragEnd:()=>{Fe.current&&(i.history.endTransaction(),Fe.current=!1),et()},onTransformStart:()=>{Ue.setTransforming(!0),i.history.startTransaction(),We.current=new Map(Ne.map(e=>[e.id,{width:e.width,height:e.height}])),_e(null)},boundBoxFunc:(e,t)=>{var n,o,r,l;const s=null!==(o=null===(n=ee.boundBoxFunc)||void 0===n?void 0:n.call(ee,e,t))&&void 0!==o?o:t,a=Math.abs(s.width)<1||Math.abs(s.height)<1,c=Math.abs(e.width)<1||Math.abs(e.height)<1;if(a&&!c){return e}const d=i.selectedElements;if(1===d.length&&"table"===(null===(r=d[0])||void 0===r?void 0:r.type)){const t=d[0],n=Math.abs(e.width),i=Math.abs(e.height);if(n>0&&i>0){const o=Math.abs(s.width)/n,r=Math.abs(s.height)/i,l=t.width*o,a=t.height*r,c=Object.assign({},s);return l<t.minWidth&&(c.width=e.width),a<t.minHeight&&(c.height=e.height),$(c,s,e)}}return Ke&&Ne.length>0?((e,t,n,i,o)=>{const r=Math.abs(e.width),l=Math.abs(e.height);if(0===r||0===l){return t}const s=Math.abs(t.width)/r,a=Math.abs(t.height)/l,c=!U(n),d=e=>i.some(t=>{var n;const i=null==o?void 0:o.get(t.id),r=null!==(n="x"===e?null==i?void 0:i.width:null==i?void 0:i.height)&&void 0!==n?n:"x"===e?t.width:t.height,l="x"===e?t.width:t.height,d="x"===e?s:a,m=K(t,e,c,r);return r>=m&&l*d<m}),m=Object.assign({},t),u=t.width*e.width<0,h=t.height*e.height<0,g=u||d("x"),f=h||d("y"),p=f||!c&&g;return(g||!c&&f)&&(m.width=e.width),p&&(m.height=e.height),$(m,t,e)})(e,s,(null===(l=Be.current)||void 0===l?void 0:l.getActiveAnchor())||"",Ne,We.current):s},onTransform:e=>{const t=Be.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{qe()},0)},onTransformEnd:e=>{Ue.setTransforming(!1),Ue.setLabel({}),i.history.endTransaction(),We.current=null,et()},visible:!i.isPlaying}),t(Ye,{store:i,gesture:Ue,transformerStyle:ee,onChange:Qe}),ze&&ze.map(({points:e,distance:o,box1:r,box2:l},s)=>n(u,{name:"distances-container",hideInExport:!0,listening:!1,children:[t(x,Object.assign({},r,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(x,Object.assign({},l,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(p,{points:[e[0].x,e[0].y,e[1].x,e[1].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),t(p,{points:[e[1].x,e[1].y,e[2].x,e[2].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(g,{x:(e[0].x+e[1].x)/2,y:(e[0].y+e[1].y)/2,offsetY:-10,scaleX:1/i.scale,scaleY:1/i.scale,children:[t(w,{cornerRadius:5,fill:J.distanceGuideStyle.labelFill,pointerDirection:"down"}),t(E,{align:"center",verticalAlign:"middle",fill:J.distanceGuideStyle.labelTextFill,padding:5,text:_({unit:i.unit,dpi:i.dpi,px:o})})]})]},s)),l._rendering&&n(u,{children:[t(x,{width:Se,height:ke,fill:"rgba(255,255,255,0.9)"}),t(E,{text:"Rendering...",fontSize:60,width:Se,height:ke,align:"center",verticalAlign:"middle"})]}),ht&&t(ht,{components:y,store:i,page:l,stageRef:Ge,tooltipSafeArea:z}),gt&&t(ie,{children:t(gt,Object.assign({components:y,store:i},Je.props))}),t(R,{store:i,pageId:l.id})]}),t(M,{store:i,page:l,width:d,height:h,scale:i.scale,xPadding:Ie,yPadding:Pe,bleedLeft:be.left,bleedTop:be.top}),t(Xe,{gesture:Ue,store:i}),t(Ee,{selection:tt}),C()>0&&n(u,{hideInExport:!0,children:[t(Ce,{name:"cache-bounds",x:d-(1===C()?250:210),y:h-(1===C()?95:45)}),t(Ce,{name:"cache-bounds-t",x:10,y:10})]}),W()&&t(E,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:d-170,y:h-18,onMouseEnter:e=>{e.target.getStage().container().style.cursor="pointer"},onMouseLeave:e=>{e.target.getStage().container().style.cursor=""},onTouchStart:e=>{e.cancelBubble=!0},onMouseDown:e=>{e.cancelBubble=!0},onClick:()=>{window.open("https://polotno.com","_blank","noopener")},onTap:()=>{window.open("https://polotno.com","_blank","noopener")}}),t(u,{name:"line-guides"})]})}),Ve&&ut&&t(ut,{store:i,page:l,xPadding:Ie,yPadding:Pe,layout:T})]})});
@@ -1 +1 @@
1
- var t=this&&this.__rest||function(t,e){var o={};for(var r in t){Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(o[r]=t[r])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(r=Object.getOwnPropertySymbols(t);n<r.length;n++){e.indexOf(r[n])<0&&Object.prototype.propertyIsEnumerable.call(t,r[n])&&(o[r[n]]=t[r[n]])}}return o};import{jsx as e,Fragment as o,jsxs as r}from"react/jsx-runtime";import{elementOpacity as n}from"./paint-opacity.js";import i from"react";import{observer as l}from"mobx-react-lite";import{Group as s,Path as a,Rect as c,Shape as d}from"react-konva";import{nodeAutorun as u}from"../utils/mobx.js";import{isAlive as h}from"mobx-state-tree";import{Html as f}from"react-konva-utils";import{drawLayout as g}from"render-tag";import{drawTextOnPathLayout as m}from"render-tag/path";import{activeAnchorOf as b,isCornerAnchor as p,scaledFontSizeAttrs as x,scaledFontSizeBounds as y,roundedFontSizeAttrs as w,roundedTextDecorationAttrs as v,scaledStyleAttrs as z,roundedGeometryPx as O,roundedPosition as j,textMinResizeWidth as E,transformScale as S}from"../utils/px.js";import{flags as M}from"../utils/flags.js";import{editorTextSizing as k,textOverflowOf as R,textResizeFloorFontSize as F}from"../utils/text-sizing.js";import{curvedBoxWidth as Y,layoutCurvedTextSizing as C,layoutTextSizing as I,renderTagLayoutAt as X}from"@polotno/core/text-sizing";import{curveLayoutAt as P}from"@polotno/core/curve-glyphs";import{applyFilter as B}from"./apply-filters.js";import{getLineHeight as A,usePrevious as T}from"./text-element.js";import{useFontSpecsLoader as H}from"./text-element/font-specs-loader.js";import{getHtml as N,getCurveTextHeight as D,letterSpacingOverhang as W}from"@polotno/core/text-html";import{useColor as $,withDisplayTextPaints as _}from"./use-color.js";import{toDisplayColor as J}from"@polotno/core/color";import{detectSize as L}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as V,trimTrailingLetterSpace as q}from"@polotno/core/background-shape";import{useFadeIn as G}from"./use-fadein.js";import{incrementLoader as K}from"../utils/loader.js";import{removeTags as Q}from"@polotno/core/text";import{shadowCastByBackground as U}from"@polotno/core/filters";import{applyOpacityToColor as Z}from"@polotno/core/svg";import{isTouchDevice as tt}from"../utils/screen.js";import{getLimitedFontSize as et}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as ot,extractFontSpecsFromHtml as rt,scaleRichTextFontSizesInHtml as nt}from"../utils/rich-text-html.js";import{RichEditor as it}from"./rich-editor.js";import{visiblePlaceholder as lt}from"./text-element/placeholder.js";import{PlacedNode as st}from"./placed-node.js";import{AutoDeleteEmptyText as at}from"./text-element/auto-delete-empty-text.js";import{wrapHtmlForRenderTag as ct}from"@polotno/core/reset-style";import{xmlEscape as dt}from"@polotno/core/xml";import{hasMeasuredHeight as ut,resolveTextBoxHeight as ht}from"@polotno/core/text-types";export{ct as wrapHtmlForRenderTag};function ft(t,e){return t!==e&&(!M.textVerticalResizeEnabled||t<e)}export const RenderTagElement=l(({element:l,store:ct})=>{var gt,mt,bt,pt,xt,yt,wt,vt,zt,Ot,jt;const Et=i.useRef(null),St=i.useRef(null),[Mt,kt]=i.useState(!1),Rt=i.useRef(l.height),Ft=i.useRef(null),Yt=tt(),{textVerticalResizeEnabled:Ct}=M,It=T(l.fontFamily),Xt=i.useMemo(()=>ot(l.text,{fontSize:l.fontSize,fontFamily:l.fontFamily}),[l.text,l.fontSize,l.fontFamily]),Pt=i.useMemo(()=>rt(Xt,{fontFamily:l.fontFamily,fontWeight:l.fontWeight,fontStyle:l.fontStyle}),[Xt,l.fontFamily,l.fontWeight,l.fontStyle]),[Bt]=H(ct,Pt,l.text||l.placeholder),At=l._editModeEnabled;G(Et);const Tt=Bt?l.fontFamily:It&&It!==l.fontFamily?It:"Arial",Ht=$(l).fill,Nt=lt(l.placeholder),Dt=!!Nt&&!Q(l.text),Wt=Dt?dt(Nt):Xt,$t=Dt?.6:n(l),_t=N(_(l),{fontFamily:Tt,color:Ht,forEditor:!0,text:Wt,textIsNormalized:!0}),Jt=l.width||1,Lt=t=>X(_(t),{getHtmlOptions:{fontFamily:Tt,color:Ht,forEditor:!0,text:Wt,textIsNormalized:!0}}),Vt=R(l),qt=k(l),Gt=l.curveEnabled,Kt=JSON.stringify(qt),Qt="fixed"===qt.box?l.height:0,Ut=Mt?Ft.current:null,Zt=i.useMemo(()=>{if(Ut){return Ut.sized}const t=Dt?Object.assign(Object.assign({},l),{text:Wt}):l;try{return Gt?Object.assign({curved:!0},C({element:t,sizing:qt,layoutAt:P(_(l),{getHtmlOptions:{fontFamily:Tt,color:Ht,text:Wt,textIsNormalized:!0}})})):Object.assign({curved:!1},I({element:t,sizing:qt,layoutAt:Lt(l)}))}catch(e){return console.error("render-tag layout failed:",e),null}},[_t,Jt,Bt,Gt,Kt,Qt,l.curvePower,l.fontSize,l.width,Ut]),te=null!==(gt=null==Ut?void 0:Ut.width)&&void 0!==gt?gt:Jt,ee=Ut?l.width/Ut.width:1,oe=!1===(null==Zt?void 0:Zt.curved)?Zt.layout:null,re=(null==Zt?void 0:Zt.curved)?Zt.layout:null,ne=null!==(mt=null==Zt?void 0:Zt.fontSize)&&void 0!==mt?mt:l.fontSize,ie=null!==(bt=null==Zt?void 0:Zt.scale)&&void 0!==bt?bt:1,le=W(l.letterSpacing,ne),se=null!==(pt=null==Ut?void 0:Ut.overhang)&&void 0!==pt?pt:le,ae=A({fontLoaded:Bt,fontFamily:l.fontFamily,fontSize:ne,lineHeight:l.lineHeight}),ce=Gt?0:Math.max(null!==(xt=null==oe?void 0:oe.height)&&void 0!==xt?xt:0,ne*ae),de=re?re.bounds:null,ue=de?(l.width-de.width)/2-de.x:0,he=i.useMemo(()=>{var t;if(re&&de){const t=re.paintBounds;return Object.assign(Object.assign({},t),{x:t.x+ue,y:t.y-de.y})}return null!==(t=null==oe?void 0:oe.paintBounds)&&void 0!==t?t:null},[oe,re,de,ue]),fe=i.useMemo(()=>oe?oe.lines.map(t=>q({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height},se)):[],[oe,se]),ge=i.useRef([]),me=i.useRef(null),be=i.useRef(!0),[,pe]=i.useReducer(t=>t+1,0);i.useEffect(()=>{const t=ge.current;t.length>0&&(ge.current=[],t.forEach(t=>t()))});const xe=i.useCallback((t,e,o,r)=>{const n=K(`text ${l.id}`),i=()=>{ct.history.ignore(t,e,o).then(()=>{be.current?(ge.current.push(n),pe()):n()})};r?queueMicrotask(i):i()},[l.id,ct]),ye=i.useCallback(t=>{const e=null!==me.current;me.current=t,e||xe(()=>{const t=me.current;me.current=null,h(l)&&ft(l.height,t)&&l.set({height:t})},!1,!0)},[l,xe]),we=i.useRef(Zt);we.current=Zt,i.useEffect(()=>{if(!Bt){return}if(!oe){return}if(!ut(l)){return void xe(()=>{h(l)&&(ut(l)||l.set({height:ce}))},void 0,void 0,!0)}if(ct.isPlaying){return}const t=ft(l.height,ce),e=()=>ye(ce);if("change-font-size"!==Vt||Mt||At||l.curveEnabled){"resize"===Vt&&t&&!Mt&&e()}else if(Zt&&Zt.fontSize!==l.fontSize){const{fontSize:t,text:e}=Zt;xe(()=>{if(!h(l)){return}if(we.current!==Zt||"change-font-size"!==R(l)||l._editModeEnabled){return}const o=x(l,t);Dt||e===l.text||(o.text=e),l.set(o)},!1,!0)}else{t&&e()}}),i.useEffect(()=>()=>{be.current=!1;const t=ge.current;t.length>0&&(ge.current=[],t.forEach(t=>t()))},[]),i.useLayoutEffect(()=>{if(!Bt||!l.curveEnabled||Mt){return}if(!de||!de.width){return}const t=t=>t&&Ct?Math.max(t,de.height):de.height,e=t=>Y(qt,{width:t},de.width);Math.abs(e(l.width)-l.width)<1&&Math.abs(t(l.height)-l.height)<1||xe(()=>{if(!h(l)){return}const o=e(l.width),r=o-l.width,n=t(l.height),i=n-l.height;if(Math.abs(r)<1&&Math.abs(i)<1){return}const s=l.a.rotation*Math.PI/180,a=-r/2*Math.cos(s),c=-r/2*Math.sin(s);l.set({width:o,height:n,x:l.a.x+a,y:l.a.y+c})},!1,!0,!0)},[Bt,l.curveEnabled,Vt,de,Mt,Ct,xe]),i.useEffect(()=>u(l,()=>{const t=Et.current;t&&B(t,l)}),[oe,re,l.a.width,l.a.height,l.shadowColor,l.shadowOffsetX,l.shadowOffsetY,l.shadowOpacity]);const ve=ht(l,ce),ze=Gt||!Bt||Mt||ct.isPlaying||"resize"!==Vt?l.a.height:Ct?Math.max(l.a.height,ce):ce;let Oe=0;Gt||"middle"!==l.verticalAlign?Gt||"bottom"!==l.verticalAlign||(Oe=ve-ce*ee):Oe=(ve-ce*ee)/2;const je=null!==(yt=null==Ut?void 0:Ut.backgroundPadding)&&void 0!==yt?yt:l.backgroundPadding*(ne*ae*.5),Ee=null!==(wt=null==Ut?void 0:Ut.backgroundCornerRadius)&&void 0!==wt?wt:l.backgroundCornerRadius*(ne*ae*.5),Se=l.backgroundEnabled||l.borderSize>0,Me=i.useMemo(()=>!Se||l.curveEnabled||l.legacyBackground||0===fe.length?"":V({rects:fe,padding:je,cornerRadius:Ee}),[Se,l.curveEnabled,l.legacyBackground,fe,je,Ee]),ke=i.useRef(null),Re=i.useRef(l.width),Fe=i.useRef(null),Ye=i.useRef(null),Ce=i.useRef(""),Ie=i.useRef(0),Xe=i.useRef(0),Pe=i.useRef({fontSize:l.fontSize}),Be=l.width>0?l.a.width/l.width:1,Ae=Be*ee,Te=null!==(vt=null==Ut?void 0:Ut.shadow)&&void 0!==vt?vt:{shadowBlur:l.shadowBlur*ie,shadowOffsetX:l.shadowOffsetX*ie,shadowOffsetY:l.shadowOffsetY*ie},He=Object.assign(Object.assign({},Te),{shadowColor:J(l.shadowColor),shadowOpacity:l.shadowOpacity}),Ne=Object.assign(Object.assign({},He),{shadowEnabled:l.shadowEnabled&&Se});return r(o,{children:[e(at,{element:l,store:ct}),Se?l.curveEnabled||l.legacyBackground?e(st,Object.assign({as:c,element:l,offsetX:je/Be,offsetY:je/Be,scaleX:Ae,scaleY:Ae,hideInExport:!l.showInExport,listening:!1,opacity:$t,fillEnabled:l.backgroundEnabled,fill:Z(l.backgroundColor,l.backgroundOpacity),strokeEnabled:l.borderSize>0,stroke:J(l.borderColor),strokeWidth:null!==(zt=null==Ut?void 0:Ut.borderSize)&&void 0!==zt?zt:l.borderSize},Ne,{width:te+2*je,height:(null!==(Ot=null==Ut?void 0:Ut.boxHeight)&&void 0!==Ot?Ot:ve)+2*je,cornerRadius:Ee})):e(st,Object.assign({as:a,element:l,name:"rendertag-background",offsetY:-Oe/Ae,scaleX:Ae,scaleY:Ae,hideInExport:!l.showInExport,listening:!1,visible:Me.length>0,opacity:$t,fillEnabled:l.backgroundEnabled,fill:Z(l.backgroundColor,l.backgroundOpacity),strokeEnabled:l.borderSize>0,stroke:J(l.borderColor),strokeWidth:null!==(jt=null==Ut?void 0:Ut.borderSize)&&void 0!==jt?jt:l.borderSize,lineJoin:Ee>0?"round":"miter"},Ne,{data:Me})):null,e(st,Object.assign({as:d,nodeRef:Et,element:l,selfRectFunc:he?()=>he:void 0,name:"rendertag-text",scaleX:Ae,scaleY:Ae,offsetY:Ae>0?-Oe/Ae:0,width:te,height:Math.max(l.height,ce),listening:!1,visible:!At&&!!(Gt?re:oe),editModeEnabled:At,opacity:$t,shadowEnabled:l.shadowEnabled&&!U(l)},He,{hideInExport:!l.showInExport,sceneFunc:(t,e)=>{const o=t._context;if(o){try{if(Gt){if(!re||!de){return}o.save(),o.translate(ue,-de.y),m({layout:re,ctx:o}),o.restore()}else{if(!oe){return}g({layout:oe,width:te,ctx:o,pixelRatio:1})}}catch(r){console.error("render-tag draw failed:",r)}}}})),e(st,{as:c,nodeRef:St,element:l,name:"element",listening:l.selectable,width:l.a.width,height:ze,visible:!At,draggable:Yt?l.draggable&&l.isSelected:l.draggable,preventDefault:!Yt||l.isSelected,opacity:0,hideInExport:!0,id:l.id,onDragMove:t=>{l.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{l.set(j(t.target))},onDblClick:t=>{l.contentEditable&&(Ye.current={x:t.evt.clientX,y:t.evt.clientY},l.toggleEditMode(!0))},onDblTap:t=>{var e;if(l.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Ye.current=o?{x:o.clientX,y:o.clientY}:null,l.toggleEditMode(!0)}},onTransformStart:t=>{Rt.current=l.height,Re.current=l.width,Fe.current=null,ke.current=t.target.position(),Ce.current=b(t.target),Ie.current=l.a.rotation,Xe.current=l.height,Pe.current={fontSize:l.fontSize,minFontSize:l.minFontSize,maxFontSize:l.maxFontSize},Ft.current=!l.curveEnabled&&["top-left","top-right","bottom-left","bottom-right"].includes(Ce.current)?{sized:Zt,width:Jt,boxHeight:ve,backgroundPadding:je,backgroundCornerRadius:Ee,borderSize:l.borderSize,overhang:le,shadow:Te}:null,kt(!0)},onTransform:e=>{const o=e.target,r=Ce.current,n="middle-left"===r||"middle-right"===r,i="top-center"===r||"bottom-center"===r,s=S(o.scaleX());if(n){const e=o.width()*s;let r=Math.max(e,E(Re.current,F(l)));const n=l.toJSON();let i=0;if("fit"===Vt){i=Rt.current}else if(l.curveEnabled){i=D(Object.assign(Object.assign({},n),{width:r}))}else{const{x:e,y:o,width:l,height:s}=n,a=t(n,["x","y","width","height"]),c=`${JSON.stringify(a)}|${Tt}|${Bt}|${Ht}`;if(Fe.current&&Fe.current.signature===c&&r<=Fe.current.width){r=Fe.current.width,i=Fe.current.height}else{const t=t=>{const e=Object.assign(Object.assign({},n),{width:t});try{return I({element:Object.assign(Object.assign({},e),{text:Wt}),sizing:Object.assign(Object.assign({},qt),{fit:{mode:"off"},cutAtBox:!1}),layoutAt:Lt(e)}).layout}catch(gt){const o=N(e,{fontFamily:Tt,color:Ht,forEditor:!0,text:Wt,textIsNormalized:!0});return{height:L(o).height,lines:[]}}};let e=t(r);const o=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width-le));o>r+.5&&o<=Re.current+.5&&(r=Math.min(Math.ceil(o),Re.current),e=t(r),Fe.current={signature:c,width:r,height:e.height}),i=e.height}}r>e&&ke.current&&o.position(ke.current),o.width(r),o.scaleX(1),o.scaleY(1);let a=M.textVerticalResizeEnabled?Math.max(i,Rt.current):i;"ellipsis"!==Vt&&"fit"!==Vt||(a=Rt.current),o.height(a),l.set({width:o.width(),height:a,x:o.x(),y:o.y()})}else if(i){const t="resize"===Vt?Gt?(null==de?void 0:de.height)||l.fontSize:ce:ae*("fit"===qt.fit.mode?qt.fit.min:l.fontSize),e=o.height()*o.scaleY();if(e<t){if(o.height(t),ke.current){if("top-center"===Ce.current){const e=Ie.current*Math.PI/180,r=Xe.current-t;o.x(ke.current.x-r*Math.sin(e)),o.y(ke.current.y+r*Math.cos(e))}else{o.position(ke.current)}}}else{o.height(e)}o.scaleY(1),o.scaleX(1),Xe.current=Math.max(t,e),l.set({x:o.x(),y:o.y(),height:o.height(),rotation:o.rotation()})}else{o.scaleX(1),o.scaleY(1);const t=l.fontSize*s;l.set(Object.assign(Object.assign(Object.assign({},y(Pe.current,t/Pe.current.fontSize)),{letterSpacing:l.letterSpacing,width:o.width()*s,x:o.x(),y:o.y(),rotation:o.rotation(),height:o.height()*s}),z(l,s)))}ke.current=e.target.position()},onTransformEnd:t=>{Ft.current=null,kt(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1);const o=w(l,l.fontSize*e),r=Pe.current,n=Ce.current;l.set(Object.assign(Object.assign(Object.assign({},o),p(n)&&"rotater"!==n?y(r,o.fontSize/r.fontSize,!0):{}),{width:O(t.target.width()*e),x:O(t.target.x()),y:O(t.target.y()),rotation:O(t.target.rotation()),text:nt(l.text,e)}))}}),At&&e(st,{as:s,element:l,offsetY:-Oe,children:e(f,{divProps:{style:{zIndex:20}},children:e(it,{element:l,fitScale:ie,fitScaleFor:t=>{if(Gt){return ie}try{return I({element:Object.assign(Object.assign({},l),{text:t}),sizing:qt,layoutAt:X(_(l),{getHtmlOptions:{fontFamily:Tt,color:Ht,forEditor:!0,text:t}})}).scale}catch(gt){return ie}},onChange:t=>{if("fit"===Vt){return void l.set({text:t})}const e=et({oldText:Q(l.text),newText:Q(t),element:l});l.set(Object.assign({text:t},x(l,e)))},onBlur:()=>{h(l)&&(l.set(v(l)),l.toggleEditMode(!1))},clickCoords:Ye.current})})})]})});
1
+ var t=this&&this.__rest||function(t,e){var o={};for(var r in t){Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(o[r]=t[r])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(r=Object.getOwnPropertySymbols(t);n<r.length;n++){e.indexOf(r[n])<0&&Object.prototype.propertyIsEnumerable.call(t,r[n])&&(o[r[n]]=t[r[n]])}}return o};import{jsx as e,Fragment as o,jsxs as r}from"react/jsx-runtime";import{elementOpacity as n}from"./paint-opacity.js";import i from"react";import{observer as s}from"mobx-react-lite";import{Group as l,Path as a,Rect as c,Shape as d}from"react-konva";import{nodeAutorun as u}from"../utils/mobx.js";import{isAlive as h}from"mobx-state-tree";import{Html as f}from"react-konva-utils";import{drawLayout as g}from"render-tag";import{drawTextOnPathLayout as m}from"render-tag/path";import{activeAnchorOf as b,isCornerAnchor as p,isRotateAnchor as x,scaledFontSizeAttrs as y,scaledFontSizeBounds as w,roundedFontSizeAttrs as v,roundedTextDecorationAttrs as j,scaledStyleAttrs as z,roundedGeometryPx as O,roundedPosition as E,textMinResizeWidth as S,transformScale as M}from"../utils/px.js";import{flags as k}from"../utils/flags.js";import{editorTextSizing as R,textOverflowOf as F,textResizeFloorFontSize as Y}from"../utils/text-sizing.js";import{curvedBoxWidth as C,layoutAsSized as I,layoutCurvedTextSizing as X,layoutTextSizing as P,renderTagLayoutAt as A}from"@polotno/core/text-sizing";import{curveLayoutAt as T}from"@polotno/core/curve-glyphs";import{applyFilter as B}from"./apply-filters.js";import{getLineHeight as H,usePrevious as N}from"./text-element.js";import{useFontSpecsLoader as $}from"./text-element/font-specs-loader.js";import{getHtml as D,getCurveTextHeight as W,letterSpacingOverhang as _}from"@polotno/core/text-html";import{useColor as J,withDisplayTextPaints as U}from"./use-color.js";import{toDisplayColor as L}from"@polotno/core/color";import{animatedShadow as V}from"./animated-shadow.js";import{detectSize as q}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as G,trimTrailingLetterSpace as K}from"@polotno/core/background-shape";import{useFadeIn as Q}from"./use-fadein.js";import{incrementLoader as Z}from"../utils/loader.js";import{removeTags as tt}from"@polotno/core/text";import{shadowCastByBackground as et}from"@polotno/core/filters";import{applyOpacityToColor as ot}from"@polotno/core/svg";import{isTouchDevice as rt}from"../utils/screen.js";import{getLimitedFontSize as nt}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as it,extractFontSpecsFromHtml as st,scaleRichTextFontSizesInHtml as lt}from"../utils/rich-text-html.js";import{RichEditor as at}from"./rich-editor.js";import{visiblePlaceholder as ct}from"./text-element/placeholder.js";import{PlacedNode as dt}from"./placed-node.js";import{AutoDeleteEmptyText as ut}from"./text-element/auto-delete-empty-text.js";import{wrapHtmlForRenderTag as ht}from"@polotno/core/reset-style";import{xmlEscape as ft}from"@polotno/core/xml";import{hasMeasuredHeight as gt,resolveTextBoxHeight as mt}from"@polotno/core/text-types";import{TEXT_PRESETS as bt}from"@polotno/core/animation-presets";import{textUnitState as pt}from"../utils/animations.js";export{ht as wrapHtmlForRenderTag};function xt(t,e){return t!==e&&(!k.textVerticalResizeEnabled||t<e)}const yt=({opacity:t,y:e,scale:o,blur:r,from:n,to:i})=>t>=1&&!e&&1===o&&!r&&n<=0&&i>=1;export const RenderTagElement=s(({element:s,store:ht})=>{var wt,vt,jt,zt,Ot,Et,St,Mt,kt,Rt,Ft;const Yt=i.useRef(null),Ct=i.useRef(null),[It,Xt]=i.useState(!1),Pt=i.useRef(s.height),At=i.useRef(null),Tt=rt(),{textVerticalResizeEnabled:Bt}=k,Ht=N(s.fontFamily),Nt=i.useMemo(()=>it(s.text,{fontSize:s.fontSize,fontFamily:s.fontFamily}),[s.text,s.fontSize,s.fontFamily]),$t=i.useMemo(()=>st(Nt,{fontFamily:s.fontFamily,fontWeight:s.fontWeight,fontStyle:s.fontStyle}),[Nt,s.fontFamily,s.fontWeight,s.fontStyle]),[Dt]=$(ht,$t,s.text||s.placeholder),Wt=s._editModeEnabled;Q(Yt);const _t=Dt?s.fontFamily:Ht&&Ht!==s.fontFamily?Ht:"Arial",Jt=J(s).fill,Ut=ct(s.placeholder),Lt=!!Ut&&!tt(s.text),Vt=Lt?ft(Ut):Nt,qt=Lt?.6:n(s),Gt=D(U(s),{fontFamily:_t,color:Jt,forEditor:!0,text:Vt,textIsNormalized:!0}),Kt=s.width||1,Qt=(t,e)=>A(U(t),{getHtmlOptions:{fontFamily:_t,color:Jt,forEditor:!0,text:Vt,textIsNormalized:!0},textUnits:e}),Zt=F(s),te=R(s),ee=s.curveEnabled,oe=JSON.stringify(te),re="fixed"===te.box?s.height:0,ne=It?At.current:null,ie=i.useMemo(()=>{if(ne){return ne.sized}const t=Lt?Object.assign(Object.assign({},s),{text:Vt}):s;try{return ee?Object.assign({curved:!0},X({element:t,sizing:te,layoutAt:T(U(s),{getHtmlOptions:{fontFamily:_t,color:Jt,text:Vt,textIsNormalized:!0}})})):Object.assign({curved:!1},P({element:t,sizing:te,layoutAt:Qt(s)}))}catch(e){return console.error("render-tag layout failed:",e),null}},[Gt,Kt,Dt,ee,oe,re,s.curvePower,s.fontSize,s.width,ne]),se=null!==(wt=null==ne?void 0:ne.width)&&void 0!==wt?wt:Kt,le=ne?s.width/ne.width:1,ae=!1===(null==ie?void 0:ie.curved)?ie.layout:null,ce=(null==ie?void 0:ie.curved)?ie.layout:null,de=s.a.text,ue=s.a.textUnits,he=ue?bt[ue.animation.name].unit:void 0,fe=i.useMemo(()=>{const t="string"==typeof de&&de!==s.text;if(!1!==(null==ie?void 0:ie.curved)||Lt||!t&&!he){return ae}try{return I({element:t?Object.assign(Object.assign({},s),{text:de}):s,sized:ie,sizing:te,layoutAt:Qt(s,he)})}catch(e){return console.error("render-tag layout failed:",e),ae}},[ae,de,he]),ge=ue&&(null==fe?void 0:fe.textUnits),me=i.useMemo(()=>{if(!ue||!ge){return null}const t=ge.map((t,e)=>pt(ue,e,ge.length)),e=e=>!t[e]||yt(t[e]);return t.map((t,o)=>({state:t,slice:yt(t)&&e(o-1)&&e(o+1)}))},[ue,ge]),be=null!==(vt=null==ie?void 0:ie.fontSize)&&void 0!==vt?vt:s.fontSize,pe=null!==(jt=null==ie?void 0:ie.scale)&&void 0!==jt?jt:1,xe=_(s.letterSpacing,be),ye=null!==(zt=null==ne?void 0:ne.overhang)&&void 0!==zt?zt:xe,we=H({fontLoaded:Dt,fontFamily:s.fontFamily,fontSize:be,lineHeight:s.lineHeight}),ve=ee?0:Math.max(null!==(Ot=null==ae?void 0:ae.height)&&void 0!==Ot?Ot:0,be*we),je=ce?ce.bounds:null,ze=je?(s.width-je.width)/2-je.x:0,Oe=i.useMemo(()=>{var t;if(ce&&je){const t=ce.paintBounds;return Object.assign(Object.assign({},t),{x:t.x+ze,y:t.y-je.y})}const e=null!==(t=null==fe?void 0:fe.paintBounds)&&void 0!==t?t:null;return e&&ge?{x:e.x-be,y:e.y-be,width:e.width+2*be,height:e.height+2*be}:e},[fe,ce,je,ze,ge,be]),Ee=i.useMemo(()=>fe?fe.lines.map(t=>K({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height},ye)):[],[fe,ye]),Se=i.useRef([]),Me=i.useRef(null),ke=i.useRef(!0),[,Re]=i.useReducer(t=>t+1,0);i.useEffect(()=>{const t=Se.current;t.length>0&&(Se.current=[],t.forEach(t=>t()))});const Fe=i.useCallback((t,e,o,r)=>{const n=Z(`text ${s.id}`),i=()=>{ht.history.ignore(t,e,o).then(()=>{ke.current?(Se.current.push(n),Re()):n()})};r?queueMicrotask(i):i()},[s.id,ht]),Ye=i.useCallback(t=>{const e=null!==Me.current;Me.current=t,e||Fe(()=>{const t=Me.current;Me.current=null,h(s)&&xt(s.height,t)&&s.set({height:t})},!1,!0)},[s,Fe]),Ce=i.useRef(ie);Ce.current=ie,i.useEffect(()=>{if(!Dt){return}if(!ae){return}if(!gt(s)){return void Fe(()=>{h(s)&&(gt(s)||s.set({height:ve}))},void 0,void 0,!0)}if(ht.isPlaying){return}const t=xt(s.height,ve),e=()=>Ye(ve);if("change-font-size"!==Zt||It||Wt||s.curveEnabled){"resize"===Zt&&t&&!It&&e()}else if(ie&&ie.fontSize!==s.fontSize){const{fontSize:t,text:e}=ie;Fe(()=>{if(!h(s)){return}if(Ce.current!==ie||"change-font-size"!==F(s)||s._editModeEnabled){return}const o=y(s,t);Lt||e===s.text||(o.text=e),s.set(o)},!1,!0)}else{t&&e()}}),i.useEffect(()=>()=>{ke.current=!1;const t=Se.current;t.length>0&&(Se.current=[],t.forEach(t=>t()))},[]),i.useLayoutEffect(()=>{if(!Dt||!s.curveEnabled||It){return}if(!je||!je.width){return}const t=t=>t&&Bt?Math.max(t,je.height):je.height,e=t=>C(te,{width:t},je.width);Math.abs(e(s.width)-s.width)<1&&Math.abs(t(s.height)-s.height)<1||Fe(()=>{if(!h(s)){return}const o=e(s.width),r=o-s.width,n=t(s.height),i=n-s.height;if(Math.abs(r)<1&&Math.abs(i)<1){return}const l=s.a.rotation*Math.PI/180,a=-r/2*Math.cos(l),c=-r/2*Math.sin(l);s.set({width:o,height:n,x:s.a.x+a,y:s.a.y+c})},!1,!0,!0)},[Dt,s.curveEnabled,Zt,je,It,Bt,Fe]),i.useEffect(()=>u(s,()=>{const t=Yt.current;t&&B(t,s)}),[fe,ce,s.a.width,s.a.height,ue,s.a.shadowColor,s.a.shadowOffsetX,s.a.shadowOffsetY,s.a.shadowOpacity]);const Ie=mt(s,ve),Xe=ee||!Dt||It||ht.isPlaying||"resize"!==Zt?s.a.height:Bt?Math.max(s.a.height,ve):ve;let Pe=0;ee||"middle"!==s.verticalAlign?ee||"bottom"!==s.verticalAlign||(Pe=Ie-ve*le):Pe=(Ie-ve*le)/2;const Ae=null!==(Et=null==ne?void 0:ne.backgroundPadding)&&void 0!==Et?Et:s.backgroundPadding*(be*we*.5),Te=null!==(St=null==ne?void 0:ne.backgroundCornerRadius)&&void 0!==St?St:s.backgroundCornerRadius*(be*we*.5),Be=s.backgroundEnabled||s.borderSize>0,He=i.useMemo(()=>!Be||s.curveEnabled||s.legacyBackground||0===Ee.length?"":G({rects:Ee,padding:Ae,cornerRadius:Te}),[Be,s.curveEnabled,s.legacyBackground,Ee,Ae,Te]),Ne=i.useRef(null),$e=i.useRef(s.width),De=i.useRef(null),We=i.useRef(null),_e=i.useRef(""),Je=i.useRef(0),Ue=i.useRef(0),Le=i.useRef({fontSize:s.fontSize}),Ve=s.width>0?s.a.width/s.width:1,qe=Ve*le,Ge=V(s),Ke=null!==(Mt=null==ne?void 0:ne.shadow)&&void 0!==Mt?Mt:{shadowBlur:Ge.shadowBlur*pe,shadowOffsetX:Ge.shadowOffsetX*pe,shadowOffsetY:Ge.shadowOffsetY*pe},Qe=Object.assign(Object.assign({},Ke),{shadowColor:Ge.shadowColor,shadowOpacity:Ge.shadowOpacity}),Ze=Object.assign(Object.assign({},Qe),{shadowEnabled:Ge.shadowEnabled&&Be});return r(o,{children:[e(ut,{element:s,store:ht}),Be?s.curveEnabled||s.legacyBackground?e(dt,Object.assign({as:c,element:s,offsetX:Ae/Ve,offsetY:Ae/Ve,scaleX:qe,scaleY:qe,hideInExport:!s.showInExport,listening:!1,opacity:qt,fillEnabled:s.backgroundEnabled,fill:ot(s.backgroundColor,s.backgroundOpacity),strokeEnabled:s.borderSize>0,stroke:L(s.borderColor),strokeWidth:null!==(kt=null==ne?void 0:ne.borderSize)&&void 0!==kt?kt:s.borderSize},Ze,{width:se+2*Ae,height:(null!==(Rt=null==ne?void 0:ne.boxHeight)&&void 0!==Rt?Rt:Ie)+2*Ae,cornerRadius:Te})):e(dt,Object.assign({as:a,element:s,name:"rendertag-background",offsetY:-Pe/qe,scaleX:qe,scaleY:qe,hideInExport:!s.showInExport,listening:!1,visible:He.length>0,opacity:qt,fillEnabled:s.backgroundEnabled,fill:ot(s.backgroundColor,s.backgroundOpacity),strokeEnabled:s.borderSize>0,stroke:L(s.borderColor),strokeWidth:null!==(Ft=null==ne?void 0:ne.borderSize)&&void 0!==Ft?Ft:s.borderSize,lineJoin:Te>0?"round":"miter"},Ze,{data:He})):null,e(dt,Object.assign({as:d,nodeRef:Yt,element:s,selfRectFunc:Oe?()=>Oe:void 0,name:"rendertag-text",scaleX:qe,scaleY:qe,offsetY:qe>0?-Pe/qe:0,width:se,height:Math.max(s.height,ve),listening:!1,visible:!Wt&&!!(ee?ce:ae),editModeEnabled:Wt,opacity:qt,shadowEnabled:Ge.shadowEnabled&&!et(s)},Qe,{hideInExport:!s.showInExport,sceneFunc:(t,e)=>{const o=t._context;if(o){try{if(ee){if(!ce||!je){return}o.save(),o.translate(ze,-je.y),m({layout:ce,ctx:o}),o.restore()}else{if(!fe){return}g({layout:fe,width:se,ctx:o,pixelRatio:1,paintTextUnit:me?t=>{const{state:e,slice:o}=me[t.unit.index];(({unit:t,ctx:e,draw:o},{opacity:r,y:n,scale:i,blur:s,from:l,to:a},c,d)=>{var u;if(!(r<=0||i<=0||a<=l)){if(l>0||a<1){const o="rtl"===(null===(u=t.fragments[0])||void 0===u?void 0:u.direction),[r,n]=o?[1-a,1-l]:[l,a],i=r<=0?t.x-c:t.x+r*t.width,s=n>=1?t.x+t.width+c:t.x+n*t.width;e.beginPath(),e.rect(i,t.y-c,s-i,t.height+2*c),e.clip()}if(e.globalAlpha*=r,n&&e.translate(0,n*c),1!==i){const o=t.x+t.width/2,r=t.y+t.height/2;e.translate(o,r),e.scale(i,i),e.translate(-o,-r)}if(s>0){const t=e.getTransform(),o=Math.sqrt(Math.abs(t.a*t.d-t.b*t.c));e.filter=`blur(${s*c*o}px)`}o({slice:d})}})(t,e,be,o)}:void 0})}}catch(r){console.error("render-tag draw failed:",r)}}}})),e(dt,{as:c,nodeRef:Ct,element:s,name:"element",listening:s.selectable,width:s.a.width,height:Xe,visible:!Wt,draggable:Tt?s.draggable&&s.isSelected:s.draggable,preventDefault:!Tt||s.isSelected,opacity:0,hideInExport:!0,id:s.id,onDragMove:t=>{s.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{s.set(E(t.target))},onDblClick:t=>{s.contentEditable&&(We.current={x:t.evt.clientX,y:t.evt.clientY},s.toggleEditMode(!0))},onDblTap:t=>{var e;if(s.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];We.current=o?{x:o.clientX,y:o.clientY}:null,s.toggleEditMode(!0)}},onTransformStart:t=>{Pt.current=s.height,$e.current=s.width,De.current=null,Ne.current=t.target.position(),_e.current=b(t.target),Je.current=s.a.rotation,Ue.current=s.height,Le.current={fontSize:s.fontSize,minFontSize:s.minFontSize,maxFontSize:s.maxFontSize},At.current=!s.curveEnabled&&["top-left","top-right","bottom-left","bottom-right"].includes(_e.current)?{sized:ie,width:Kt,boxHeight:Ie,backgroundPadding:Ae,backgroundCornerRadius:Te,borderSize:s.borderSize,overhang:xe,shadow:Ke}:null,Xt(!0)},onTransform:e=>{const o=e.target,r=_e.current,n="middle-left"===r||"middle-right"===r,i="top-center"===r||"bottom-center"===r,l=M(o.scaleX());if(n){const e=o.width()*l;let r=Math.max(e,S($e.current,Y(s)));const n=s.toJSON();let i=0;if("fit"===Zt){i=Pt.current}else if(s.curveEnabled){i=W(Object.assign(Object.assign({},n),{width:r}))}else{const{x:e,y:o,width:s,height:l}=n,a=t(n,["x","y","width","height"]),c=`${JSON.stringify(a)}|${_t}|${Dt}|${Jt}`;if(De.current&&De.current.signature===c&&r<=De.current.width){r=De.current.width,i=De.current.height}else{const t=t=>{const e=Object.assign(Object.assign({},n),{width:t});try{return P({element:Object.assign(Object.assign({},e),{text:Vt}),sizing:Object.assign(Object.assign({},te),{fit:{mode:"off"},cutAtBox:!1}),layoutAt:Qt(e)}).layout}catch(wt){const o=D(e,{fontFamily:_t,color:Jt,forEditor:!0,text:Vt,textIsNormalized:!0});return{height:q(o).height,lines:[]}}};let e=t(r);const o=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width-xe));o>r+.5&&o<=$e.current+.5&&(r=Math.min(Math.ceil(o),$e.current),e=t(r),De.current={signature:c,width:r,height:e.height}),i=e.height}}r>e&&Ne.current&&o.position(Ne.current),o.width(r),o.scaleX(1),o.scaleY(1);let a=k.textVerticalResizeEnabled?Math.max(i,Pt.current):i;"ellipsis"!==Zt&&"fit"!==Zt||(a=Pt.current),o.height(a),s.set({width:o.width(),height:a,x:o.x(),y:o.y()})}else if(i){const t="resize"===Zt?ee?(null==je?void 0:je.height)||s.fontSize:ve:we*("fit"===te.fit.mode?te.fit.min:s.fontSize),e=o.height()*o.scaleY();if(e<t){if(o.height(t),Ne.current){if("top-center"===_e.current){const e=Je.current*Math.PI/180,r=Ue.current-t;o.x(Ne.current.x-r*Math.sin(e)),o.y(Ne.current.y+r*Math.cos(e))}else{o.position(Ne.current)}}}else{o.height(e)}o.scaleY(1),o.scaleX(1),Ue.current=Math.max(t,e),s.set({x:o.x(),y:o.y(),height:o.height(),rotation:o.rotation()})}else{o.scaleX(1),o.scaleY(1);const t=s.fontSize*l;s.set(Object.assign(Object.assign(Object.assign({},w(Le.current,t/Le.current.fontSize)),{letterSpacing:s.letterSpacing,width:o.width()*l,x:o.x(),y:o.y(),rotation:o.rotation(),height:o.height()*l}),z(s,l)))}Ne.current=e.target.position()},onTransformEnd:t=>{At.current=null,Xt(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1);const o=v(s,s.fontSize*e),r=Le.current,n=_e.current;s.set(Object.assign(Object.assign(Object.assign({},o),p(n)&&!x(n)?w(r,o.fontSize/r.fontSize,!0):{}),{width:O(t.target.width()*e),x:O(t.target.x()),y:O(t.target.y()),rotation:O(t.target.rotation()),text:lt(s.text,e)}))}}),Wt&&e(dt,{as:l,element:s,offsetY:-Pe,children:e(f,{divProps:{style:{zIndex:20}},children:e(at,{element:s,fitScale:pe,fitScaleFor:t=>{if(ee){return pe}try{return P({element:Object.assign(Object.assign({},s),{text:t}),sizing:te,layoutAt:A(U(s),{getHtmlOptions:{fontFamily:_t,color:Jt,forEditor:!0,text:t}})}).scale}catch(wt){return pe}},onChange:t=>{if("fit"===Zt){return void s.set({text:t})}const e=nt({oldText:tt(s.text),newText:tt(t),element:s});s.set(Object.assign({text:t},y(s,e)))},onBlur:()=>{h(s)&&(s.set(j(s)),s.toggleEditMode(!1))},clickCoords:We.current})})})]})});
@@ -1,6 +1,7 @@
1
1
  import React from 'react';
2
2
  import Quill from 'quill';
3
3
  import { TextElementType } from '../model/text-model.js';
4
+ import { type LayoutNode, type LayoutText } from 'render-tag';
4
5
  import { applyOpacityToColor } from '@polotno/core/svg';
5
6
  export { applyOpacityToColor };
6
7
  export declare const quillRef: {
@@ -15,6 +16,7 @@ export declare function syncQuillFormatState(quill: any, range: {
15
16
  index: number;
16
17
  length: number;
17
18
  } | null | undefined): void;
19
+ export declare function collectRTText(node: LayoutNode, out: LayoutText[]): void;
18
20
  type MarkerVars = {
19
21
  bulletLeft: number;
20
22
  bulletTop: number;
@@ -1,4 +1,4 @@
1
- var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(o[n]=t[n])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(t);r<n.length;r++){e.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(t,n[r])&&(o[n[r]]=t[n[r]])}}return o};import{jsx as e}from"react/jsx-runtime";import o from"react";import n from"quill";import*as r from"mobx";import{nodeReaction as i}from"../utils/mobx.js";import{isAlive as l}from"mobx-state-tree";import{getDir as s,getMeasureCtx as a,getQuillEditorHtml as c,textLayoutWidth as d}from"@polotno/core/text-html";import{quillHtmlToModelText as u,isQuillFormatMixed as f,isBoldWeight as p,detachNativeColorsInHtml as m}from"../utils/rich-text-html.js";import{toDisplayColor as h}from"@polotno/core/color";import{resetStyleContent as g,wrapHtmlForRenderTag as b}from"@polotno/core/reset-style";import{layout as v,lineBaselineOffset as y}from"render-tag";import{removeTags as x}from"@polotno/core/text";import{cssFontFamily as w}from"@polotno/core/fonts";import{isGradient as S,firstStopColor as k}from"@polotno/core/gradient";import{getOptimalCaretColor as T}from"./text-element/caret-color.js";import{visiblePlaceholder as q}from"./text-element/placeholder.js";import{registerQuillUnrestrictedFontFormats as E,PRESERVED_INLINE_STYLES as C,PRESERVED_BLOCK_STYLES as O}from"./quill-font-formats.js";import{ALL_QUILL_MIXED_KEYS as R,setActiveTextEditor as F,updateActiveTextFormat as L,clearActiveTextEditor as $,getActiveTextEditor as N}from"../utils/text-format-state.js";import j from"../utils/styled.js";import{applyOpacityToColor as z}from"@polotno/core/svg";import{elementDropShadow as M,shadowCastByBackground as P}from"@polotno/core/filters";export{z as applyOpacityToColor};export const quillRef=r.observable({enabled:!1,currentFormat:{},pendingToolbarInteraction:!1,editor:r.observable.object({instance:null},{},{deep:!1})});const D=R;export function syncQuillFormatState(t,e){const o=e?t.getFormat(e):{},n=function(t,e){const o={};if(!e||e.length<=0){return o}const n=t.getContents(e.index,e.length);for(const r of D){f(n,r)&&(o[r]=!0)}return o}(t,e);r.runInAction(()=>{quillRef.currentFormat=o}),L(o,n,e)}const H="polotno-saved-selection";function I(){const t=window.getSelection();if(!t||0===t.rangeCount||t.isCollapsed){return}if("undefined"==typeof Highlight||!CSS.highlights){return}const e=t.getRangeAt(0).cloneRange();CSS.highlights.set(H,new Highlight(e)),Q()}function A(){"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.delete(H)}function Q(){setTimeout(()=>{"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.has(H)&&(document.querySelector(".polotno-portal")||tt(document.activeElement)?Q():A())},300)}if("undefined"!=typeof document&&"undefined"!=typeof CSS&&CSS.highlights&&!document.querySelector("[data-polotno-highlight]")){const et=document.createElement("style");et.setAttribute("data-polotno-highlight",""),et.textContent=`::highlight(${H}) { background-color: Highlight; }`,document.head.appendChild(et)}function _(t,e){"text"===t.type&&e.push(t);for(const o of t.children||[]){_(o,e)}}export function computeMarkerVars(t){var e,o,n;const r=t.fontSize;if(!r||r<=0){return null}const i=w(t.fontFamily),l=p(t.fontWeight)?700:400,s="italic"===t.fontStyle?"italic":"normal",c=a();c.fontKerning="normal",c.font=`${r}px ${i}`;const d=c.measureText("M"),u=(null!==(e=d.fontBoundingBoxAscent)&&void 0!==e?e:0)/r,f=(null!==(o=d.fontBoundingBoxDescent)&&void 0!==o?o:0)/r,m=`font-family:${i};font-size:${r}px;font-weight:${l};font-style:${s};line-height:${t.lineHeight};letter-spacing:${t.letterSpacing}em`,h=b(`<div style="${m}"><ul><li>x</li></ul><ol><li>x</li></ol></div>`);let g;try{g=v({html:h,width:Math.max(12*r,600)})}catch(E){return null}const x=[];_(g.layoutRoot,x);const S=x.find(t=>"•"===t.text),k=x.find(t=>/^\d/.test(t.text));if(!S){return null}const T=S.style.fontSize,q=2.1*r;return{bulletLeft:S.x,bulletTop:S.y-y(T,T*u,T*f),bulletFontSize:T,numberGap:k?Math.max(0,q-k.x-(null!==(n=k.width)&&void 0!==n?n:0)):0}}export const RichTextContainer=j("div",o.forwardRef)`
1
+ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(o[n]=t[n])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(t);r<n.length;r++){e.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(t,n[r])&&(o[n[r]]=t[n[r]])}}return o};import{jsx as e}from"react/jsx-runtime";import o from"react";import n from"quill";import*as r from"mobx";import{nodeReaction as i}from"../utils/mobx.js";import{isAlive as l}from"mobx-state-tree";import{geckoRtlOverhangCss as s,getDir as a,getMeasureCtx as c,getQuillEditorHtml as d,letterSpacingOverhang as u,OVERHANG_PROPERTY as f,textLayoutWidth as p}from"@polotno/core/text-html";import{quillHtmlToModelText as m,isQuillFormatMixed as h,isBoldWeight as g,detachNativeColorsInHtml as b}from"../utils/rich-text-html.js";import{toDisplayColor as v}from"@polotno/core/color";import{resetStyleContent as y,wrapHtmlForRenderTag as x}from"@polotno/core/reset-style";import{layout as w,lineBaselineOffset as S}from"render-tag";import{removeTags as k}from"@polotno/core/text";import{cssFontFamily as T}from"@polotno/core/fonts";import{isGradient as q,firstStopColor as E}from"@polotno/core/gradient";import{getOptimalCaretColor as C}from"./text-element/caret-color.js";import{visiblePlaceholder as R}from"./text-element/placeholder.js";import{registerQuillUnrestrictedFontFormats as O,PRESERVED_INLINE_STYLES as F,PRESERVED_BLOCK_STYLES as L}from"./quill-font-formats.js";import{ALL_QUILL_MIXED_KEYS as $,setActiveTextEditor as N,updateActiveTextFormat as j,clearActiveTextEditor as z,getActiveTextEditor as M}from"../utils/text-format-state.js";import P from"../utils/styled.js";import{applyOpacityToColor as D}from"@polotno/core/svg";import{elementDropShadow as H,shadowCastByBackground as A}from"@polotno/core/filters";export{D as applyOpacityToColor};export const quillRef=r.observable({enabled:!1,currentFormat:{},pendingToolbarInteraction:!1,editor:r.observable.object({instance:null},{},{deep:!1})});const I=$;export function syncQuillFormatState(t,e){const o=e?t.getFormat(e):{},n=function(t,e){const o={};if(!e||e.length<=0){return o}const n=t.getContents(e.index,e.length);for(const r of I){h(n,r)&&(o[r]=!0)}return o}(t,e);r.runInAction(()=>{quillRef.currentFormat=o}),j(o,n,e)}const Q="polotno-saved-selection";function _(){const t=window.getSelection();if(!t||0===t.rangeCount||t.isCollapsed){return}if("undefined"==typeof Highlight||!CSS.highlights){return}const e=t.getRangeAt(0).cloneRange();CSS.highlights.set(Q,new Highlight(e)),B()}function W(){"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.delete(Q)}function B(){setTimeout(()=>{"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.has(Q)&&(document.querySelector(".polotno-portal")||ot(document.activeElement)?B():W())},300)}if("undefined"!=typeof document&&"undefined"!=typeof CSS&&CSS.highlights&&!document.querySelector("[data-polotno-highlight]")){const nt=document.createElement("style");nt.setAttribute("data-polotno-highlight",""),nt.textContent=`::highlight(${Q}) { background-color: Highlight; }`,document.head.appendChild(nt)}export function collectRTText(t,e){"text"===t.type?e.push(t):t.children.forEach(t=>collectRTText(t,e))}export function computeMarkerVars(t){var e,o,n;const r=t.fontSize;if(!r||r<=0){return null}const i=T(t.fontFamily),l=g(t.fontWeight)?700:400,s="italic"===t.fontStyle?"italic":"normal",a=c();a.fontKerning="normal",a.font=`${r}px ${i}`;const d=a.measureText("M"),u=(null!==(e=d.fontBoundingBoxAscent)&&void 0!==e?e:0)/r,f=(null!==(o=d.fontBoundingBoxDescent)&&void 0!==o?o:0)/r,p=`font-family:${i};font-size:${r}px;font-weight:${l};font-style:${s};line-height:${t.lineHeight};letter-spacing:${t.letterSpacing}em`,m=x(`<div style="${p}"><ul><li>x</li></ul><ol><li>x</li></ol></div>`);let h;try{h=w({html:m,width:Math.max(12*r,600)})}catch(E){return null}const b=[];collectRTText(h.layoutRoot,b);const v=b.find(t=>"•"===t.text),y=b.find(t=>/^\d/.test(t.text));if(!v){return null}const k=v.style.fontSize,q=2.1*r;return{bulletLeft:v.x,bulletTop:v.y-S(k,k*u,k*f),bulletFontSize:k,numberGap:y?Math.max(0,q-y.x-(null!==(n=y.width)&&void 0!==n?n:0)):0}}export const RichTextContainer=P("div",o.forwardRef)`
2
2
  .ql-editor {
3
3
  outline: none;
4
4
  /* anchor for the absolutely-positioned ::before placeholder below */
@@ -25,7 +25,7 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
25
25
  pointer-events: none;
26
26
  opacity: 0;
27
27
  }
28
- ${g}
28
+ ${y}
29
29
 
30
30
  /* ===== Editor-only list markers (Quill flat-DOM hack) ======================
31
31
  Quill emits a FLAT <ol> with per-item data-list + ql-indent classes (not
@@ -91,8 +91,6 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
91
91
  box-sizing: border-box;
92
92
  width: 2.1em;
93
93
  text-align: end;
94
- font-weight: normal;
95
- font-style: normal;
96
94
  }
97
95
  .ql-editor li[data-list='ordered']::before {
98
96
  content: counter(list-0) '.';
@@ -109,8 +107,12 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
109
107
  .ql-editor li.ql-indent-3[data-list='ordered']::before {
110
108
  content: counter(list-3) '.';
111
109
  }
110
+ /* A number takes the item's weight and style; a bullet is always regular,
111
+ as render-tag (and Chrome) draw them. */
112
112
  .ql-editor li[data-list='bullet']::before {
113
113
  content: '•';
114
+ font-weight: normal;
115
+ font-style: normal;
114
116
  padding-inline-end: 0.5em; /* fallback gap (RTL, or before vars are set) */
115
117
  }
116
118
  /* LTR: pixel-match render-tag's synthetic disc. The canvas scales the '•'
@@ -134,6 +136,7 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
134
136
  .ql-direction-rtl {
135
137
  direction: rtl;
136
138
  }
139
+ ${s("&[dir]")}
137
140
  /* Apply gradient fill to all text-bearing elements when --polotno-gradient is set */
138
141
  &[style*='--polotno-gradient'] .ql-editor,
139
142
  &[style*='--polotno-gradient'] .ql-editor p,
@@ -163,4 +166,4 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
163
166
  .ql-editor [style*='background-clip'] {
164
167
  -webkit-text-stroke-width: 0;
165
168
  }
166
- `;const W=[...C,...O,"header"];let B=["background","font-weight","color","gradient","font","italic","size","script","strike","underline","text-transform","indent","list","direction",...W];export const setQuillFormats=t=>{B=t};let X=!1;function G(){X||(X=!0,console.warn("[polotno] The Quill 'bold' format is deprecated. Use the numeric 'font-weight' format instead, e.g. quill.format('font-weight', '700') or applyTextFormat({ fontWeight: 700 })."))}function V(t){return t?"700":"400"}const Y=n.import("delta"),U="data-polotno-rich-text";export const createQuill=(e,o)=>{E();const r=new n(e,{toolbar:!1,keyboard:!1,placeholder:(null==o?void 0:o.placeholder)||void 0,clipboard:{matchVisual:!1},formats:B});!function(e){const o=e.format.bind(e),n=e.formatText.bind(e),r=e.getFormat.bind(e);e.format=(t,e,n)=>"bold"===t?(G(),o("font-weight",V(e),n)):o(t,e,n),e.formatText=(e,o,r,i,l)=>{if(r&&"object"==typeof r){if("bold"in r){G();const{bold:l}=r,s=t(r,["bold"]);return n(e,o,Object.assign(Object.assign({},s),{"font-weight":V(l)}),i)}return n(e,o,r,i)}return"bold"===r?(G(),n(e,o,"font-weight",V(i),l)):n(e,o,r,i,l)},e.getFormat=(...t)=>{const e=r(...t);return e&&null!=e["font-weight"]&&!("bold"in e)&&(e.bold=p(e["font-weight"])),e}}(r);const i=(t,e)=>e.compose((new Y).retain(e.length(),{"font-weight":"700"}));r.clipboard.addMatcher("STRONG",i),r.clipboard.addMatcher("B",i);const l=r.clipboard,s=l.onPaste.bind(l);return l.onPaste=(t,e)=>{var o;if(null===(o=e.html)||void 0===o?void 0:o.includes(U)){return Z(r,()=>s(t,e))}const n=l.convert;l.convert=(...t)=>((t,e)=>{for(const o of t.ops){if(o.attributes){for(const t of e){delete o.attributes[t]}}}return t})(n.apply(l,t),W);try{return s(t,e)}finally{l.convert=n}},r};const K=(t,e)=>e.insert(t.data),J=(t,e)=>{const o=e.ops[0];return parseFloat(t.style.textIndent)>0&&"string"==typeof(null==o?void 0:o.insert)&&o.insert.startsWith("\t")?e.compose((new Y).delete(1)):e};function Z(t,e){const{matchers:o}=t.clipboard,n=o.findIndex(([t])=>t===Node.TEXT_NODE),r=o[n];-1!==n&&(o[n]=[Node.TEXT_NODE,K]),o.push([Node.ELEMENT_NODE,J]);try{return e()}finally{o.pop(),-1!==n&&(o[n]=r)}}export const setQuillContent=(t,e)=>{const o=Z(t,()=>t.clipboard.convert({html:"<div class='ql-editor' style='outline: none;'>"+e+"<p><br></p></div>",text:""}));t.setContents(o),t.history.clear()};export const editorHtml=t=>c(Object.assign(Object.assign({},t),{text:m(t.text)}),t.fontFamily);export const RichEditor=({onBlur:t,onChange:n,element:a,clickCoords:c,fitScale:f=1,fitScaleFor:p})=>{const m=o.useRef(null),g=o.useRef(!1),b=o.useRef(f);b.current=f;const[v,y]=o.useState(null),E=null!=v?v:f,C=o.useRef(p);C.current=p;const O=t=>({width:d(a.a.width/t,a.letterSpacing,a.fontSize),zoom:1!==t?t:void 0}),R=o.useRef(null),L=o.useRef(void 0),j=()=>`${a.text}\0${a.fontSize}\0${a.fontFamily}`,z=t=>u(t,{fontSize:a.fontSize,fontFamily:a.fontFamily}),D=t=>{setQuillContent(t,editorHtml(a)),R.current=z(t.root.innerHTML),L.current=j()},H=o.useRef(()=>{}),Q=()=>{H.current(),t()};o.useLayoutEffect(()=>{let t,e,o=!1,i=null,s=null,d=null,u=null,f=null;const p=()=>{if(!o&&m.current){try{t=createQuill(m.current,{placeholder:q(a.placeholder)}),r.runInAction(()=>{quillRef.editor.instance=t}),quillRef.pendingToolbarInteraction=!1,window.__polotnoQuill=t,F({instance:t,store:a.store,element:a});const o=e=>{e&&syncQuillFormatState(t,e)},p=()=>{let e=t.root;if(e.querySelector(".ql-cursor")){e=e.cloneNode(!0);const t=e.querySelector(".ql-cursor");t.replaceWith(t.textContent.replace(/\uFEFF/g,""))}return z(e.innerHTML)},h=()=>{const t=C.current;t&&l(a)&&(t=>{const e=m.current;if(!e){return}const o=O(t);e.style.width=`${o.width}px`,e.style.zoom=void 0===o.zoom?"":String(o.zoom)})(t(p()))},v=()=>{var o,r;if(clearTimeout(e),e=void 0,!l(a)){return}const i=p();if(i===R.current){return}R.current=i,g.current=!0,n(i);const s=t.getContents(),c=a.fill;let d=null,u=!0;for(const t of s.ops){if("string"!=typeof t.insert||"\n"===t.insert){continue}if(null===(o=t.attributes)||void 0===o?void 0:o.gradient){u=!1;break}const e=(null===(r=t.attributes)||void 0===r?void 0:r.color)||c;if(null===d){d=e}else if(e!==d){u=!1;break}}u&&d&&a.set({fill:d})};H.current=()=>{(void 0!==e||t.composition.isComposing)&&v()},t.on("text-change",()=>{const n=N(),r=n&&n.instance===t?n.lastSelection:null;o(t.getSelection()||r),t.composition.isComposing||h(),void 0===e&&(e=setTimeout(v,10))}),t.on("selection-change",o),t.on("composition-start",()=>y(b.current)),t.on("composition-end",()=>{h(),y(null)}),D(t);const x=m.current.closest(".polotno-workspace-inner"),w=null==x?void 0:x.scrollLeft,S=null==x?void 0:x.scrollTop;c?setCursorFromCoords(t,c):t.setSelection(0,0,"api"),x&&void 0!==w&&void 0!==S&&(x.scrollLeft=w,x.scrollTop=S);const k=t.root;u=t=>{const e=t.relatedTarget;(null==e?void 0:e.classList.contains("ql-clipboard"))||(tt(e)||quillRef.pendingToolbarInteraction?I():setTimeout(()=>{quillRef.pendingToolbarInteraction||tt(document.activeElement)?I():(A(),Q())},0))},f=()=>{A()},k.addEventListener("blur",u),k.addEventListener("focus",f),i=k,s=e=>{var o,n;const r=t.getSelection();if(!r||0===r.length){return}const i=t.getText(r.index,r.length).replace(/\n$/,""),l=window.getSelection();let s="";if(l&&l.rangeCount>0){const t=l.getRangeAt(0);let e=t.cloneContents(),o=t.startContainer;o&&o.nodeType===Node.TEXT_NODE&&(o=o.parentNode);let n=t.endContainer;if(n&&n.nodeType===Node.TEXT_NODE&&(n=n.parentNode),o&&o===n){let t=o;for(;t&&t!==k&&t.nodeType===Node.ELEMENT_NODE;){const o=t.tagName;if("SPAN"!==o&&"EM"!==o&&"STRONG"!==o&&"U"!==o&&"S"!==o&&"A"!==o){break}const n=t.cloneNode(!1);n.appendChild(e),e=n,t=t.parentNode}}const r=document.createElement("div");r.appendChild(e),s=r.innerHTML}if(null===(o=e.clipboardData)||void 0===o||o.setData("text/plain",i),s){const t=`<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>\x3c!--StartFragment--\x3e<div ${U} style="white-space: pre-wrap; overflow-wrap: break-word;">`+s+"</div>\x3c!--EndFragment--\x3e</body></html>";null===(n=e.clipboardData)||void 0===n||n.setData("text/html",t)}e.preventDefault()},d=e=>{s&&s(e);const o=t.getSelection();o&&o.length>0&&t.deleteText(o.index,o.length)},i.addEventListener("copy",s),i.addEventListener("cut",d)}catch(p){console.error("RichEditor init failed:",p)}}};return m.current?p():queueMicrotask(p),()=>{o=!0,clearTimeout(e),i&&u&&i.removeEventListener("blur",u),i&&f&&i.removeEventListener("focus",f),A(),i&&s&&d&&(i.removeEventListener("copy",s),i.removeEventListener("cut",d)),r.runInAction(()=>{quillRef.editor.instance=null,quillRef.currentFormat={}}),$(t),delete window.__polotnoQuill}},[]),o.useEffect(()=>i(a,j,t=>{const e=quillRef.editor.instance;if(!e){return}if(g.current){return g.current=!1,void(L.current=t)}if(t===L.current){return}const o=e.getSelection();D(e),o&&(e.setSelection(o.index,o.length),syncQuillFormatState(e,e.getSelection()))},{fireImmediately:!0}),[]),o.useEffect(()=>i(a,()=>`${a.fontFamily}\0${a.fontSize}\0${a.fontWeight}\0${a.fontStyle}\0${a.lineHeight}\0${a.letterSpacing}\0${s(x(a.text))}`,()=>{const t=m.current;if(!t){return}const e="ltr"===s(x(a.text)),o=computeMarkerVars(a);o&&t.style.setProperty("--rt-number-gap",`${o.numberGap}px`),o&&e?(t.style.setProperty("--rt-bullet-left",`${o.bulletLeft}px`),t.style.setProperty("--rt-bullet-top",`${o.bulletTop}px`),t.style.setProperty("--rt-bullet-fs",`${o.bulletFontSize}px`),t.classList.add("rt-marker-geom")):t.classList.remove("rt-marker-geom")},{fireImmediately:!0}),[]),o.useEffect(()=>{window.addEventListener("blur",Q);const t=t=>{var e;const o=t.target;"number"==typeof(null==o?void 0:o.nodeType)&&(null===(e=m.current)||void 0===e?void 0:e.contains(o))||tt(o)||quillRef.pendingToolbarInteraction||(A(),Q())};return window.addEventListener("mousedown",t),window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",Q),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t)}},[]);const _=h(a.fill),W={color:_};_.indexOf("gradient")>=0&&(W["--polotno-gradient"]=_,W.color="transparent"),a.strokeWidth>0&&(W.WebkitTextStroke=`${a.strokeWidth}px ${k(a.stroke)}`,W.paintOrder="stroke fill");const B=S(a.fill)?k(a.fill):T(a),X=a.shadowEnabled&&!P(a);return e(RichTextContainer,{ref:m,style:Object.assign(Object.assign(Object.assign(Object.assign({},W),{fontSize:a.fontSize,fontFamily:w(a.fontFamily),fontWeight:a.fontWeight,fontStyle:a.fontStyle,"--polotno-text-decoration":a.textDecoration||"none",textTransform:a.textTransform}),O(E)),{lineHeight:a.lineHeight,letterSpacing:a.letterSpacing*a.fontSize+"px",textAlign:a.align,opacity:Math.max(a.a.opacity,.2),filter:X?M({shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowBlur:a.shadowBlur,shadowColor:a.shadowColor,shadowOpacity:a.shadowOpacity,rotation:a.a.rotation}):void 0,caretColor:B}),dir:s(x(a.text))})};function tt(t){const e=t;return!("function"!=typeof(null==e?void 0:e.closest)||!e.closest(".sketch-picker")&&!e.closest("[data-polotno-keep-focus]")&&!e.closest(".polotno-portal")&&!e.closest(".polotno-toolbar"))}export function setCursorFromCoords(t,e){if(!t||!e){return}const{x:o,y:r}=e;try{let e=null;if(document.caretRangeFromPoint){e=document.caretRangeFromPoint(o,r)}else if(document.caretPositionFromPoint){const t=document.caretPositionFromPoint(o,r);t&&(e=document.createRange(),e.setStart(t.offsetNode,t.offset))}if(e){const o=n.find(e.startContainer,!0);if(o){const n=o.offset(t.scroll)+e.startOffset;return void t.setSelection(n,0,"api")}}}catch(i){}t.setSelection(0,0,"api")}
169
+ `;const X=[...F,...L,"header"];let G=["background","font-weight","color","gradient","font","italic","size","script","strike","underline","text-transform","indent","list","direction",...X];export const setQuillFormats=t=>{G=t};let V=!1;function Y(){V||(V=!0,console.warn("[polotno] The Quill 'bold' format is deprecated. Use the numeric 'font-weight' format instead, e.g. quill.format('font-weight', '700') or applyTextFormat({ fontWeight: 700 })."))}function U(t){return t?"700":"400"}const K=n.import("delta"),J="data-polotno-rich-text";export const createQuill=(e,o)=>{O();const r=new n(e,{toolbar:!1,keyboard:!1,placeholder:(null==o?void 0:o.placeholder)||void 0,clipboard:{matchVisual:!1},formats:G});!function(e){const o=e.format.bind(e),n=e.formatText.bind(e),r=e.getFormat.bind(e);e.format=(t,e,n)=>"bold"===t?(Y(),o("font-weight",U(e),n)):o(t,e,n),e.formatText=(e,o,r,i,l)=>{if(r&&"object"==typeof r){if("bold"in r){Y();const{bold:l}=r,s=t(r,["bold"]);return n(e,o,Object.assign(Object.assign({},s),{"font-weight":U(l)}),i)}return n(e,o,r,i)}return"bold"===r?(Y(),n(e,o,"font-weight",U(i),l)):n(e,o,r,i,l)},e.getFormat=(...t)=>{const e=r(...t);return e&&null!=e["font-weight"]&&!("bold"in e)&&(e.bold=g(e["font-weight"])),e}}(r);const i=(t,e)=>e.compose((new K).retain(e.length(),{"font-weight":"700"}));r.clipboard.addMatcher("STRONG",i),r.clipboard.addMatcher("B",i);const l=r.clipboard,s=l.onPaste.bind(l);return l.onPaste=(t,e)=>{var o;if(null===(o=e.html)||void 0===o?void 0:o.includes(J)){return et(r,()=>s(t,e))}const n=l.convert;l.convert=(...t)=>((t,e)=>{for(const o of t.ops){if(o.attributes){for(const t of e){delete o.attributes[t]}}}return t})(n.apply(l,t),X);try{return s(t,e)}finally{l.convert=n}},r};const Z=(t,e)=>e.insert(t.data),tt=(t,e)=>{const o=e.ops[0];return parseFloat(t.style.textIndent)>0&&"string"==typeof(null==o?void 0:o.insert)&&o.insert.startsWith("\t")?e.compose((new K).delete(1)):e};function et(t,e){const{matchers:o}=t.clipboard,n=o.findIndex(([t])=>t===Node.TEXT_NODE),r=o[n];-1!==n&&(o[n]=[Node.TEXT_NODE,Z]),o.push([Node.ELEMENT_NODE,tt]);try{return e()}finally{o.pop(),-1!==n&&(o[n]=r)}}export const setQuillContent=(t,e)=>{const o=et(t,()=>t.clipboard.convert({html:"<div class='ql-editor' style='outline: none;'>"+e+"<p><br></p></div>",text:""}));t.setContents(o),t.history.clear()};export const editorHtml=t=>d(Object.assign(Object.assign({},t),{text:b(t.text)}),t.fontFamily);export const RichEditor=({onBlur:t,onChange:n,element:s,clickCoords:c,fitScale:d=1,fitScaleFor:h})=>{const g=o.useRef(null),b=o.useRef(!1),y=o.useRef(d);y.current=d;const[x,w]=o.useState(null),S=null!=x?x:d,O=o.useRef(h);O.current=h;const F=t=>({width:p(s.a.width/t,s.letterSpacing,s.fontSize),zoom:1!==t?t:void 0}),L=o.useRef(null),$=o.useRef(void 0),j=()=>`${s.text}\0${s.fontSize}\0${s.fontFamily}`,P=t=>m(t,{fontSize:s.fontSize,fontFamily:s.fontFamily}),D=t=>{setQuillContent(t,editorHtml(s)),L.current=P(t.root.innerHTML),$.current=j()},I=o.useRef(()=>{}),Q=()=>{I.current(),t()};o.useLayoutEffect(()=>{let t,e,o=!1,i=null,a=null,d=null,u=null,f=null;const p=()=>{if(!o&&g.current){try{t=createQuill(g.current,{placeholder:R(s.placeholder)}),r.runInAction(()=>{quillRef.editor.instance=t}),quillRef.pendingToolbarInteraction=!1,window.__polotnoQuill=t,N({instance:t,store:s.store,element:s});const o=e=>{e&&syncQuillFormatState(t,e)},p=()=>{let e=t.root;if(e.querySelector(".ql-cursor")){e=e.cloneNode(!0);const t=e.querySelector(".ql-cursor");t.replaceWith(t.textContent.replace(/\uFEFF/g,""))}return P(e.innerHTML)},m=()=>{const t=O.current;t&&l(s)&&(t=>{const e=g.current;if(!e){return}const o=F(t);e.style.width=`${o.width}px`,e.style.zoom=void 0===o.zoom?"":String(o.zoom)})(t(p()))},h=()=>{var o,r;if(clearTimeout(e),e=void 0,!l(s)){return}const i=p();if(i===L.current){return}L.current=i,b.current=!0,n(i);const a=t.getContents(),c=s.fill;let d=null,u=!0;for(const t of a.ops){if("string"!=typeof t.insert||"\n"===t.insert){continue}if(null===(o=t.attributes)||void 0===o?void 0:o.gradient){u=!1;break}const e=(null===(r=t.attributes)||void 0===r?void 0:r.color)||c;if(null===d){d=e}else if(e!==d){u=!1;break}}u&&d&&s.set({fill:d})};I.current=()=>{(void 0!==e||t.composition.isComposing)&&h()},t.on("text-change",()=>{const n=M(),r=n&&n.instance===t?n.lastSelection:null;o(t.getSelection()||r),t.composition.isComposing||m(),void 0===e&&(e=setTimeout(h,10))}),t.on("selection-change",o),t.on("composition-start",()=>w(y.current)),t.on("composition-end",()=>{m(),w(null)}),D(t);const v=g.current.closest(".polotno-workspace-inner"),x=null==v?void 0:v.scrollLeft,S=null==v?void 0:v.scrollTop;c?setCursorFromCoords(t,c):t.setSelection(0,0,"api"),v&&void 0!==x&&void 0!==S&&(v.scrollLeft=x,v.scrollTop=S);const k=t.root;u=t=>{const e=t.relatedTarget;(null==e?void 0:e.classList.contains("ql-clipboard"))||(ot(e)||quillRef.pendingToolbarInteraction?_():setTimeout(()=>{quillRef.pendingToolbarInteraction||ot(document.activeElement)?_():(W(),Q())},0))},f=()=>{W()},k.addEventListener("blur",u),k.addEventListener("focus",f),i=k,a=e=>{var o,n;const r=t.getSelection();if(!r||0===r.length){return}const i=t.getText(r.index,r.length).replace(/\n$/,""),l=window.getSelection();let s="";if(l&&l.rangeCount>0){const t=l.getRangeAt(0);let e=t.cloneContents(),o=t.startContainer;o&&o.nodeType===Node.TEXT_NODE&&(o=o.parentNode);let n=t.endContainer;if(n&&n.nodeType===Node.TEXT_NODE&&(n=n.parentNode),o&&o===n){let t=o;for(;t&&t!==k&&t.nodeType===Node.ELEMENT_NODE;){const o=t.tagName;if("SPAN"!==o&&"EM"!==o&&"STRONG"!==o&&"U"!==o&&"S"!==o&&"A"!==o){break}const n=t.cloneNode(!1);n.appendChild(e),e=n,t=t.parentNode}}const r=document.createElement("div");r.appendChild(e),s=r.innerHTML}if(null===(o=e.clipboardData)||void 0===o||o.setData("text/plain",i),s){const t=`<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>\x3c!--StartFragment--\x3e<div ${J} style="white-space: pre-wrap; overflow-wrap: break-word;">`+s+"</div>\x3c!--EndFragment--\x3e</body></html>";null===(n=e.clipboardData)||void 0===n||n.setData("text/html",t)}e.preventDefault()},d=e=>{a&&a(e);const o=t.getSelection();o&&o.length>0&&t.deleteText(o.index,o.length)},i.addEventListener("copy",a),i.addEventListener("cut",d)}catch(p){console.error("RichEditor init failed:",p)}}};return g.current?p():queueMicrotask(p),()=>{o=!0,clearTimeout(e),i&&u&&i.removeEventListener("blur",u),i&&f&&i.removeEventListener("focus",f),W(),i&&a&&d&&(i.removeEventListener("copy",a),i.removeEventListener("cut",d)),r.runInAction(()=>{quillRef.editor.instance=null,quillRef.currentFormat={}}),z(t),delete window.__polotnoQuill}},[]),o.useEffect(()=>i(s,j,t=>{const e=quillRef.editor.instance;if(!e){return}if(b.current){return b.current=!1,void($.current=t)}if(t===$.current){return}const o=e.getSelection();D(e),o&&(e.setSelection(o.index,o.length),syncQuillFormatState(e,e.getSelection()))},{fireImmediately:!0}),[]),o.useEffect(()=>i(s,()=>`${s.fontFamily}\0${s.fontSize}\0${s.fontWeight}\0${s.fontStyle}\0${s.lineHeight}\0${s.letterSpacing}\0${a(k(s.text))}`,()=>{const t=g.current;if(!t){return}const e="ltr"===a(k(s.text)),o=computeMarkerVars(s);o&&t.style.setProperty("--rt-number-gap",`${o.numberGap}px`),o&&e?(t.style.setProperty("--rt-bullet-left",`${o.bulletLeft}px`),t.style.setProperty("--rt-bullet-top",`${o.bulletTop}px`),t.style.setProperty("--rt-bullet-fs",`${o.bulletFontSize}px`),t.classList.add("rt-marker-geom")):t.classList.remove("rt-marker-geom")},{fireImmediately:!0}),[]),o.useEffect(()=>{window.addEventListener("blur",Q);const t=t=>{var e;const o=t.target;"number"==typeof(null==o?void 0:o.nodeType)&&(null===(e=g.current)||void 0===e?void 0:e.contains(o))||ot(o)||quillRef.pendingToolbarInteraction||(W(),Q())};return window.addEventListener("mousedown",t),window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",Q),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t)}},[]);const B=v(s.fill),X={color:B};B.indexOf("gradient")>=0&&(X["--polotno-gradient"]=B,X.color="transparent"),s.strokeWidth>0&&(X.WebkitTextStroke=`${s.strokeWidth}px ${E(s.stroke)}`,X.paintOrder="stroke fill");const G=q(s.fill)?E(s.fill):C(s),V=s.shadowEnabled&&!A(s);return e(RichTextContainer,{ref:g,style:Object.assign(Object.assign(Object.assign(Object.assign({},X),{fontSize:s.fontSize,fontFamily:T(s.fontFamily),fontWeight:s.fontWeight,fontStyle:s.fontStyle,"--polotno-text-decoration":s.textDecoration||"none",textTransform:s.textTransform}),F(S)),{[f]:u(s.letterSpacing,s.fontSize)+"px",lineHeight:s.lineHeight,letterSpacing:s.letterSpacing*s.fontSize+"px",textAlign:s.align,opacity:Math.max(s.a.opacity,.2),filter:V?H({shadowOffsetX:s.shadowOffsetX,shadowOffsetY:s.shadowOffsetY,shadowBlur:s.shadowBlur,shadowColor:s.shadowColor,shadowOpacity:s.shadowOpacity,rotation:s.a.rotation}):void 0,caretColor:G}),dir:a(k(s.text))})};function ot(t){const e=t;return!("function"!=typeof(null==e?void 0:e.closest)||!e.closest(".sketch-picker")&&!e.closest("[data-polotno-keep-focus]")&&!e.closest(".polotno-portal")&&!e.closest(".polotno-toolbar"))}export function setCursorFromCoords(t,e){if(!t||!e){return}const{x:o,y:r}=e;try{let e=null;if(document.caretRangeFromPoint){e=document.caretRangeFromPoint(o,r)}else if(document.caretPositionFromPoint){const t=document.caretPositionFromPoint(o,r);t&&(e=document.createRange(),e.setStart(t.offsetNode,t.offset))}if(e){const o=n.find(e.startContainer,!0);if(o){const n=o.offset(t.scroll)+e.startOffset;return void t.setSelection(n,0,"api")}}}catch(i){}t.setSelection(0,0,"api")}
@@ -1 +1 @@
1
- import{jsx as t,Fragment as e,jsxs as r}from"react/jsx-runtime";import{elementOpacity as o}from"./paint-opacity.js";import n from"react";import{toDisplayColor as i}from"@polotno/core/color";import{observer as a}from"mobx-react-lite";import{Group as l,Path as s,Text as c,TextPath as d}from"react-konva";import{Html as u}from"react-konva-utils";import{nodeAutorun as f}from"../utils/mobx.js";import h from"konva";import{parsePath as g,roundCommands as m}from"svg-round-corners/lib/index.js";import{generateBackgroundShapeFromRects as p,textLinesToRects as x}from"@polotno/core/background-shape";import{useColor as b}from"./use-color.js";import{ROLES as v}from"../model/store.js";import{incrementLoader as S}from"../utils/loader.js";import{activeAnchorOf as w,scaledFontSizeAttrs as y,roundedFontSizeAttrs as E,roundedTextDecorationAttrs as z,roundedGeometryPx as k,roundedPosition as R,textMinResizeWidth as F,transformScale as j}from"../utils/px.js";import{isFontLoaded as A}from"../utils/fonts.js";import{flags as M}from"../utils/flags.js";import{textOverflowOf as L}from"../utils/text-sizing.js";import{FIT_FONT_SIZE_STEP as O,FIT_HEIGHT_EPSILON as C,legacyTextOverflow as T}from"@polotno/core/text-sizing";import{removeTags as H}from"@polotno/core/text";import{getDir as P,getCurvePath as W,resolveLineHeight as X,normalizeStrokeLineJoin as D}from"@polotno/core/text-html";import{cssFontFamily as Y}from"@polotno/core/fonts";import{shadowCastByBackground as $}from"@polotno/core/filters";import{applyOpacityToColor as J}from"@polotno/core/svg";import{applyFilter as N}from"./apply-filters.js";import{useFadeIn as B}from"./use-fadein.js";import{isTouchDevice as I}from"../utils/screen.js";import{isAlive as _,getParent as V}from"mobx-state-tree";import{getLimitedFontSize as q}from"./text-element/max-font-size.js";import{getOptimalCaretColor as U}from"./text-element/caret-color.js";import{visiblePlaceholder as Z}from"./text-element/placeholder.js";import{hasMeasuredHeight as G,resolveTextBoxHeight as K}from"@polotno/core/text-types";let Q;function tt(){return Q||(Q=document.getElementById("polotno-text-style"),Q||(Q=document.createElement("style"),Q.id="polotno-text-style",document.head.appendChild(Q)),Q)}h._fixTextRendering=!0;const et={border:"none",padding:"0px",overflow:"hidden",background:"none",outline:"none",resize:"none",overflowWrap:"break-word",whiteSpace:"pre-wrap",userSelect:"text",wordBreak:"normal",textTransform:"none"};export{getDir}from"@polotno/core/text-html";const rt=a(({textNodeRef:e,element:r,onBlur:o,selectAll:i,cursorPosition:a})=>{const[l,s]=n.useState(et),c=e.current;n.useLayoutEffect(()=>{if(!c){return}const t={};t.width=c.width()-2*c.padding()+"px",t.height=c.height()-2*c.padding()+c.fontSize()*c.lineHeight()+"px",t.fontSize=c.fontSize()+"px",t.lineHeight=c.lineHeight()+.01,t.fontFamily=c.fontFamily(),t.textAlign=c.align(),t.color=c.fill(),t.fontWeight=r.fontWeight,t.fontStyle=r.fontStyle,t.letterSpacing=r.letterSpacing+"em",t.opacity=Math.max(r.a.opacity,.2),t.textTransform=r.textTransform,t.caretColor=U(r);const e=`\n .polotno-input::placeholder {\n color: ${l.color};\n opacity: 0.6;\n }\n `,o=tt();o.innerHTML="",o.appendChild(document.createTextNode(e)),JSON.stringify(t)!==JSON.stringify(l)&&s(t)});const d=n.useRef(null);if(n.useLayoutEffect(()=>{var t;const e=d.current;if(!e){return}null===(t=d.current)||void 0===t||t.focus({preventScroll:!0});const r=a||e.value.length;e.selectionStart=e.selectionEnd=r,i&&(null==e||e.select(),document.execCommand("selectAll",!1,void 0))},[]),n.useEffect(()=>{window.addEventListener("blur",o);const t=t=>{var e;(null===(e=d.current)||void 0===e?void 0:e.contains(t.target))||o()};return window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",o),window.removeEventListener("touchstart",t)}},[]),!c){return null}let u=0;const f=c.textArr.length*c.lineHeight()*c.fontSize(),h=K(r.a,f);"middle"===r.verticalAlign&&(u=(h-f)/2),"bottom"===r.verticalAlign&&(u=h-f);const g=H(r.text);return t("textarea",{className:"polotno-input",ref:d,dir:P(g),style:Object.assign(Object.assign(Object.assign({},et),l),{paddingTop:u+"px"}),value:g,onChange:t=>{const e=q({oldText:r.text,newText:t.target.value,element:r});r.set(Object.assign({text:t.target.value},y(r,e)))},placeholder:Z(r.placeholder),onBlur:o})}),ot=e=>t(u,{children:t(rt,Object.assign({},e))});export const useFontLoader=(t,e,r,o,n)=>useFontFamiliesLoader(t,[e],r,o,n);export const useFontFamiliesLoader=(t,e,r,o,i)=>{const[a,l]=n.useReducer(t=>t+1,0),s=e.join("\0"),c=n.useRef(e.every(t=>A(t,r,o))),d=n.useRef(null);return n.useLayoutEffect(()=>{const n=c.current;if(c.current=e.every(t=>A(t,r,o)),c.current){return void(n||l())}let a=!0;return(async()=>{n&&l();const u=S(`fonts ${s}`);await Promise.all(e.map(e=>t.loadFont(e,r,o,i))),a?(d.current=u,c.current=!0,l()):u()})(),()=>{d.current&&(d.current(),d.current=null),a=!1}},[s,r,o]),n.useEffect(()=>{var t;c.current&&(null===(t=d.current)||void 0===t||t.call(d),d.current=null)},[c.current]),[c.current]};export const getLineHeight=({fontLoaded:t,fontFamily:e,fontSize:r,lineHeight:o})=>n.useMemo(()=>X({fontFamily:e,fontSize:r,lineHeight:o}),[t,e,r,o]);export function usePrevious(t){const e=n.useRef(t),r=n.useRef(t);return n.useMemo(()=>{r.current=e.current,e.current=t},[t]),r.current}function nt(t,e,r=!1){const o=t.fontSize(),n=t.height(),i=H(e.text);let a=e.a.fontSize;t.height(void 0);const l=Math.round(2*e.a.fontSize)-1;for(let s=1;s<l;s++){const o=G(e.a)&&t.height()>e.a.height+C;let n=i.split("\n").join(" ").split(/[\s-]+/).reduce((t,e)=>(e=e.toLowerCase(),/[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\uAC00-\uD7AF]/.test(e)?t.concat(e.split("")):t.concat(e)),[]),l=t.textArr.map(t=>t.text.toLowerCase()).join(";");const s=n.find(t=>!l.includes(t)||(l=l.replace(t,""),!1));if(!(o||s&&!r)){break}a-=O,t.fontSize(a)}return t.fontSize(o),t.height(n),a}export{getCurvePath}from"@polotno/core/text-html";export const TextElement=a(({element:a,store:u})=>{const h=n.useRef(null),A=n.useRef(null),O=n.useRef(null),C=n.useRef([]),X=n.useRef(!0),[,q]=n.useReducer(t=>t+1,0),{editorEnabled:U,selectAll:Q}=(t=>{const[e,r]=n.useState(!1),o=n.useRef(!1);return n.useEffect(()=>{var e=!0;return setTimeout(()=>{e&&(t._editModeEnabled&&(o.current=!0),r(!0),setTimeout(()=>{o.current=!1},50))},50),()=>{e=!1}},[]),{editorEnabled:e&&t._editModeEnabled,selectAll:o.current}})(a),[tt,et]=n.useState(!1),[rt,it]=n.useState(!1),at=n.useRef(a.a.height),lt=n.useRef(0),st=n.useRef(""),ct=n.useRef(0),dt=a.isSelected,ut=usePrevious(dt),{textVerticalResizeEnabled:ft}=M,ht=M.autoDeleteEmptyText,gt=usePrevious(a.fontFamily),[mt,pt]=n.useState([]),xt=a.backgroundEnabled||a.borderSize>0;n.useLayoutEffect(()=>{var t,e;if(!xt){return}const r=null!==(e=null===(t=h.current)||void 0===t?void 0:t.textArr)&&void 0!==e?e:[];JSON.stringify(r)!==JSON.stringify(mt)&&pt(r)}),n.useEffect(()=>{if(a.a.width||"tablecell"===a.type){return}const t=h.current;t.width(600),a.set({width:1.4*t.getTextWidth()})},[]),n.useEffect(()=>{ut&&!dt&&ht&&""===a.text&&a.removable&&!Z(a.placeholder)&&u.deleteElements([a.id])},[ht,a.placeholder,a.removable,a.text,dt,ut]);const bt=()=>{if(a.curveEnabled){const t=h.current;null==t||t.clearCache(),null==t||t.setAttrs({filters:[]});const e=A.current;e&&N(e,a)}else{const t=h.current;t&&N(t,a)}};n.useLayoutEffect(()=>{if(!rt){return f(a,()=>{bt()})}}),n.useEffect(()=>{const t=C.current;t.length>0&&(C.current=[],t.forEach(t=>t()))}),n.useEffect(()=>()=>{X.current=!1;const t=C.current;t.length>0&&(C.current=[],t.forEach(t=>t()))},[]);let vt=H(a.text);"uppercase"===a.textTransform&&(vt=vt.toUpperCase());const[St]=useFontLoader(u,a.fontFamily,a.fontStyle,a.fontWeight,a.text||a.placeholder),wt=()=>{if(a.curveEnabled){const t=A.current;return t.getSelfRect().height||t.fontSize()}const t=h.current.clone({height:void 0}),e=Math.ceil(t.fontSize()*t.lineHeight()*t.textArr.length+1);return t.destroy(),e};n.useLayoutEffect(()=>{if(!St){return}if("tablecell"===a.type){const t=T(L(a));if("resize"===t){const t=wt(),e=_(a)?V(a,2):null;(null==e?void 0:e.setCellContentHeight)&&e.setCellContentHeight(a.id,t)}else if("change-font-size"===t){const t=h.current;if(t){const e=nt(t,a,M.textSplitAllowed);e!==a.fontSize&&u.history.ignore(()=>{a.set({fontSize:e})})}}return}const t=T(L(a)),{textSplitAllowed:e}=M,r=(t,e,r,o)=>{const n=S(`text ${a.id}`),i=()=>{u.history.ignore(t,e,r).then(()=>{X.current?(C.current.push(n),q()):n()})};o?queueMicrotask(i):i()};if(!G(a)){const t=wt();return void r(()=>{G(a)||a.set({height:t})},void 0,void 0,!0)}if(!u.isPlaying){if("change-font-size"!==t||tt){if("resize"===t){const t=wt();ft&&a.height<t&&!tt&&r(()=>{var e;_(a)&&a.set({height:t}),null===(e=h.current)||void 0===e||e.height(t)},!1,!0),ft||a.height===t||tt||r(()=>{var e;_(a)&&a.set({height:t}),null===(e=h.current)||void 0===e||e.height(t)},!1,!0)}}else{const t=h.current,o=a.curveEnabled?a.fontSize:nt(t,a,e);if(o!==a.fontSize){return void r(()=>{a.set(y(a,o))},!1,!0)}const n=wt();a.height===n||ft||r(()=>{a.set({height:n})},!1,!0)}}}),n.useLayoutEffect(()=>{var t;if(St&&a.curveEnabled){const e=null===(t=A.current)||void 0===t?void 0:t.getSelfRect().width;if(e){const t=e-a.a.width,r=a.a.rotation*Math.PI/180,o=-t/2*Math.cos(r),n=-t/2*Math.sin(r);a.set({width:e,x:a.a.x+o,y:a.a.y+n})}}},[St,a.curveEnabled,a.curvePower,a.text,a.fontSize,a.fontFamily,a.fontWeight,a.fontStyle,a.letterSpacing]),n.useLayoutEffect(()=>{const t=h.current;t&&(t.width(t.width()+1e-8),t.width(t.width()-1e-8),t._setTextData(),bt())},[St]);const yt=n.useRef(null),Et=n.useRef(a.width),zt=n.useRef(0),kt=t=>{t.evt.preventDefault();const e=u.selectedShapes.find(t=>t===a);e&&a.contentEditable&&(zt.current=function(t){var e;const r=t.target,o=function(t){var e=t.getAbsoluteTransform().copy();e.invert();var r=t.getStage().getPointerPosition();return e.point(r)}(r),n=r.textArr,i=Math.floor(o.y/(r.fontSize()*r.lineHeight())),a=n.slice(0,i).reduce((t,e)=>t+e.text.length,i),l=null!==(e=n[i])&&void 0!==e?e:n[0];let s=0;return"right"===r.align()?s=r.width()-l.width:"center"===r.align()&&(s=r.width()/2-l.width/2),a+Math.round((o.x-s)/l.width*l.text.length)}(t),a.toggleEditMode())},Rt=Z(a.placeholder),Ft=!vt&&Rt?.6:o(a);B(h,Ft);const jt=getLineHeight({fontLoaded:St,fontFamily:a.fontFamily,fontSize:a.a.fontSize,lineHeight:a.lineHeight}),At=a.selectable||u.role===v.ADMIN,Mt=b(a),Lt=b(a,a.stroke,"stroke"),Ot=n.useMemo(()=>a.backgroundEnabled||a.borderSize?a.curveEnabled?function({width:t,height:e,padding:r=0,cornerRadius:o=0}){const n=-r,i=-r,a=t+r,l=e+r,s=g(`M ${n} ${i} L ${a} ${i} L ${a} ${l} L ${n} ${l} Z`);return m(s,o).path}({width:a.a.width,height:a.a.height,cornerRadius:a.backgroundCornerRadius*(a.a.fontSize*jt*.5),padding:a.backgroundPadding*(a.a.fontSize*jt*.5)}):function({lines:t,lineHeight:e,width:r,align:o="left",padding:n=0,cornerRadius:i=0}){const a=x({lines:t,lineHeight:e,width:r,align:o});return p({rects:a,padding:n,cornerRadius:i})}({lines:JSON.parse(JSON.stringify(mt)),cornerRadius:a.backgroundCornerRadius*(a.a.fontSize*jt*.5),lineHeight:jt*a.a.fontSize,padding:a.backgroundPadding*(a.a.fontSize*jt*.5),width:a.a.width,align:a.align}):"",[a.backgroundEnabled,a.borderSize,a.curveEnabled,a.backgroundCornerRadius,a.a.fontSize,a.a.height,jt,a.backgroundPadding,a.a.width,a.align,mt]),Ct=I();let Tt=0;if(!a.curveEnabled){const t=mt.length*jt*a.a.fontSize,e=K(a.a,t);"middle"===a.verticalAlign?Tt=(e-t)/2:"bottom"===a.verticalAlign&&(Tt=e-t)}const Ht=a.curveEnabled?W(a.a.width,a.a.height,a.curvePower,a.a.fontSize):"",Pt=St?Y(a.fontFamily):gt===a.fontFamily?"Arial":Y(gt),Wt=P(vt),Xt=a.shadowEnabled&&!$(a),Dt={shadowEnabled:a.shadowEnabled&&($(a)||a.borderSize>0),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:i(a.shadowColor),shadowOpacity:a.shadowOpacity};return r(e,{children:[xt&&t(s,Object.assign({ref:O,x:a.a.x,y:a.a.y,rotation:a.a.rotation,hideInExport:!a.showInExport,listening:!1,opacity:Ft,data:Ot,fillEnabled:a.backgroundEnabled,fill:J(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,lineJoin:a.backgroundCornerRadius>0?"round":"miter",offsetY:-Tt},Dt)),a.curveEnabled&&t(d,Object.assign({ref:A,data:Ht,text:vt||Rt,listening:!1,align:"center",textBaseline:"middle",direction:Wt},Mt,Lt,{strokeWidth:a.strokeWidth,lineJoin:D(a.strokeLineJoin),fillAfterStrokeEnabled:!0,fontSize:a.a.fontSize,fontFamily:`${Y(a.fontFamily)}, ${Y(gt)}`,fontStyle:a.fontStyle+" "+a.fontWeight,textDecoration:a.textDecoration.trim(),letterSpacing:a.letterSpacing*a.a.fontSize,x:a.a.x,y:a.a.y,rotation:a.a.rotation,opacity:a._editModeEnabled?.3:Ft,hideInExport:!a.showInExport},Dt,{shadowEnabled:Xt})),t(c,Object.assign({ref:h,id:a.selectable?a.id:void 0,name:a.selectable?"element":"",hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled,x:a.a.x,y:a.a.y,rotation:a.a.rotation,width:a.a.width,height:G(a.a)?a.a.height:void 0,text:vt||Rt,direction:Wt},Mt,Lt,{lineJoin:D(a.strokeLineJoin),strokeWidth:a.strokeWidth,fillAfterStrokeEnabled:!0,fontSize:a.a.fontSize,fontFamily:Pt,fontStyle:a.fontStyle+" "+a.fontWeight,textDecoration:a.textDecoration,align:a.align,verticalAlign:a.verticalAlign,draggable:Ct?a.draggable&&dt:a.draggable,preventDefault:!Ct||dt,opacity:a.curveEnabled?0:Ft,visible:!a._editModeEnabled,ellipsis:"ellipsis"===L(a)},Dt,{shadowEnabled:Xt,lineHeight:jt,letterSpacing:a.letterSpacing*a.a.fontSize,listening:!!a.selectable&&At,onDragStart:()=>{it(!0)},onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{it(!1),a.set(R(t.target))},onClick:kt,onTap:kt,onTransformStart:()=>{et(!0);const t=h.current;at.current=t.height(),Et.current=a.a.width,yt.current=t.position(),lt.current=t.rotation(),ct.current=t.height(),st.current=w(t)},onTransform:t=>{var e,r;const o=t.target;null===(e=O.current)||void 0===e||e.setAttrs({x:o.x(),y:o.y(),rotation:o.rotation(),scale:o.scale()});const n=st.current;if("middle-left"===n||"middle-right"===n){const t=o.scaleX(),e=o.width()*t,r=F(Et.current,a.a.fontSize);let n=e;e<r&&(n=r,yt.current&&o.position(yt.current)),o.width(n),o.scaleX(1),o.scaleY(1);let i=wt();if("ellipsis"===L(a)){i=at.current}else{const t=Math.max(i,at.current);o.height(t),a.set({height:o.height()})}const l=M.textVerticalResizeEnabled?Math.max(i,at.current):wt();a.set({x:o.x(),width:o.width(),rotation:o.rotation(),height:l}),o.skewX(0),o.skewY(0),bt()}if("top-center"===n||"bottom-center"===n){const e="resize"===L(a)?wt():jt*a.a.fontSize,r=t.target.height()*t.target.scaleY();if(r<e){if(t.target.height(e),yt.current){if("top-center"===st.current){const r=lt.current*Math.PI/180,o=ct.current-e;t.target.x(yt.current.x-o*Math.sin(r)),t.target.y(yt.current.y+o*Math.cos(r))}else{t.target.position(yt.current)}}}else{t.target.height(r)}t.target.scaleY(1),t.target.scaleX(1),t.target.rotation(lt.current),t.target.skewX(0),t.target.skewY(0),ct.current=Math.max(e,r)}yt.current=t.target.position();const i=j(t.target.scaleX());null===(r=O.current)||void 0===r||r.setAttrs({scaleX:1,scaleY:1}),t.target.scaleX(1),t.target.scaleY(1),a.set({fontSize:a.a.fontSize*i,width:t.target.width()*i,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*i,shadowBlur:a.shadowBlur*i,shadowOffsetX:a.shadowOffsetX*i,shadowOffsetY:a.shadowOffsetY*i,strokeWidth:a.strokeWidth*i,borderSize:a.borderSize*i})},onTransformEnd:t=>{var e;const r=j(t.target.scaleX());t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},E(a,a.fontSize*r)),{width:Math.ceil(t.target.width()*r),x:k(t.target.x()),y:k(t.target.y()),rotation:k(t.target.rotation()),height:k(t.target.height()*r)})),null===(e=O.current)||void 0===e||e.setAttrs({scaleX:1,scaleY:1}),et(!1)}})),U&&a.contentEditable&&t(l,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,children:t(ot,{textNodeRef:h,element:a,selectAll:Q,cursorPosition:zt.current,onBlur:()=>{a.set(z(a)),a.toggleEditMode(!1)}})})]})});
1
+ import{jsx as t,Fragment as e,jsxs as r}from"react/jsx-runtime";import{elementOpacity as o}from"./paint-opacity.js";import n from"react";import{toDisplayColor as i}from"@polotno/core/color";import{observer as a}from"mobx-react-lite";import{Group as l,Path as s,Text as c,TextPath as d}from"react-konva";import{Html as u}from"react-konva-utils";import{nodeAutorun as f}from"../utils/mobx.js";import h from"konva";import{parsePath as g,roundCommands as p}from"svg-round-corners/lib/index.js";import{generateBackgroundShapeFromRects as m,textLinesToRects as x}from"@polotno/core/background-shape";import{useColor as b}from"./use-color.js";import{ROLES as v}from"../model/store.js";import{incrementLoader as S}from"../utils/loader.js";import{activeAnchorOf as w,scaledFontSizeAttrs as y,roundedFontSizeAttrs as E,roundedTextDecorationAttrs as z,roundedGeometryPx as k,roundedPosition as R,textMinResizeWidth as F,transformScale as j}from"../utils/px.js";import{isFontLoaded as A}from"../utils/fonts.js";import{flags as M}from"../utils/flags.js";import{textOverflowOf as L}from"../utils/text-sizing.js";import{FIT_FONT_SIZE_STEP as O,FIT_HEIGHT_EPSILON as C,legacyTextOverflow as T}from"@polotno/core/text-sizing";import{removeTags as H}from"@polotno/core/text";import{getDir as P,getCurvePath as W,resolveLineHeight as X,normalizeStrokeLineJoin as D}from"@polotno/core/text-html";import{cssFontFamily as Y}from"@polotno/core/fonts";import{shadowCastByBackground as $}from"@polotno/core/filters";import{applyOpacityToColor as J}from"@polotno/core/svg";import{applyFilter as N}from"./apply-filters.js";import{useFadeIn as B}from"./use-fadein.js";import{isTouchDevice as I}from"../utils/screen.js";import{isAlive as _,getParent as V}from"mobx-state-tree";import{getLimitedFontSize as q}from"./text-element/max-font-size.js";import{getOptimalCaretColor as U}from"./text-element/caret-color.js";import{visiblePlaceholder as Z}from"./text-element/placeholder.js";import{hasMeasuredHeight as G,resolveTextBoxHeight as K}from"@polotno/core/text-types";let Q;function tt(){return Q||(Q=document.getElementById("polotno-text-style"),Q||(Q=document.createElement("style"),Q.id="polotno-text-style",document.head.appendChild(Q)),Q)}h._fixTextRendering=!0;const et={border:"none",padding:"0px",overflow:"hidden",background:"none",outline:"none",resize:"none",overflowWrap:"break-word",whiteSpace:"pre-wrap",userSelect:"text",wordBreak:"normal",textTransform:"none"};export{getDir}from"@polotno/core/text-html";const rt=a(({textNodeRef:e,element:r,onBlur:o,selectAll:i,cursorPosition:a})=>{const[l,s]=n.useState(et),c=e.current;n.useLayoutEffect(()=>{if(!c){return}const t={};t.width=c.width()-2*c.padding()+"px",t.height=c.height()-2*c.padding()+c.fontSize()*c.lineHeight()+"px",t.fontSize=c.fontSize()+"px",t.lineHeight=c.lineHeight()+.01,t.fontFamily=c.fontFamily(),t.textAlign=c.align(),t.color=c.fill(),t.fontWeight=r.fontWeight,t.fontStyle=r.fontStyle,t.letterSpacing=r.letterSpacing+"em",t.opacity=Math.max(r.a.opacity,.2),t.textTransform=r.textTransform,t.caretColor=U(r);const e=`\n .polotno-input::placeholder {\n color: ${l.color};\n opacity: 0.6;\n }\n `,o=tt();o.innerHTML="",o.appendChild(document.createTextNode(e)),JSON.stringify(t)!==JSON.stringify(l)&&s(t)});const d=n.useRef(null);if(n.useLayoutEffect(()=>{var t;const e=d.current;if(!e){return}null===(t=d.current)||void 0===t||t.focus({preventScroll:!0});const r=a||e.value.length;e.selectionStart=e.selectionEnd=r,i&&e.setSelectionRange(0,e.value.length)},[]),n.useEffect(()=>{window.addEventListener("blur",o);const t=t=>{var e;(null===(e=d.current)||void 0===e?void 0:e.contains(t.target))||o()};return window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",o),window.removeEventListener("touchstart",t)}},[]),!c){return null}let u=0;const f=c.textArr.length*c.lineHeight()*c.fontSize(),h=K(r.a,f);"middle"===r.verticalAlign&&(u=(h-f)/2),"bottom"===r.verticalAlign&&(u=h-f);const g=H(r.text);return t("textarea",{className:"polotno-input",ref:d,dir:P(g),style:Object.assign(Object.assign(Object.assign({},et),l),{paddingTop:u+"px"}),value:g,onChange:t=>{const e=q({oldText:r.text,newText:t.target.value,element:r});r.set(Object.assign({text:t.target.value},y(r,e)))},placeholder:Z(r.placeholder),onBlur:o})}),ot=e=>t(u,{children:t(rt,Object.assign({},e))});export const useFontLoader=(t,e,r,o,n)=>useFontFamiliesLoader(t,[e],r,o,n);export const useFontFamiliesLoader=(t,e,r,o,i)=>{const[a,l]=n.useReducer(t=>t+1,0),s=e.join("\0"),c=n.useRef(e.every(t=>A(t,r,o))),d=n.useRef(null);return n.useLayoutEffect(()=>{const n=c.current;if(c.current=e.every(t=>A(t,r,o)),c.current){return void(n||l())}let a=!0;return(async()=>{n&&l();const u=S(`fonts ${s}`);await Promise.all(e.map(e=>t.loadFont(e,r,o,i))),a?(d.current=u,c.current=!0,l()):u()})(),()=>{d.current&&(d.current(),d.current=null),a=!1}},[s,r,o]),n.useEffect(()=>{var t;c.current&&(null===(t=d.current)||void 0===t||t.call(d),d.current=null)},[c.current]),[c.current]};export const getLineHeight=({fontLoaded:t,fontFamily:e,fontSize:r,lineHeight:o})=>n.useMemo(()=>X({fontFamily:e,fontSize:r,lineHeight:o}),[t,e,r,o]);export function usePrevious(t){const e=n.useRef(t),r=n.useRef(t);return n.useMemo(()=>{r.current=e.current,e.current=t},[t]),r.current}function nt(t,e,r=!1){const o=t.fontSize(),n=t.height(),i=H(e.text);let a=e.a.fontSize;t.height(void 0);const l=Math.round(2*e.a.fontSize)-1;for(let s=1;s<l;s++){const o=G(e.a)&&t.height()>e.a.height+C;let n=i.split("\n").join(" ").split(/[\s-]+/).reduce((t,e)=>(e=e.toLowerCase(),/[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\uAC00-\uD7AF]/.test(e)?t.concat(e.split("")):t.concat(e)),[]),l=t.textArr.map(t=>t.text.toLowerCase()).join(";");const s=n.find(t=>!l.includes(t)||(l=l.replace(t,""),!1));if(!(o||s&&!r)){break}a-=O,t.fontSize(a)}return t.fontSize(o),t.height(n),a}export{getCurvePath}from"@polotno/core/text-html";export const TextElement=a(({element:a,store:u})=>{const h=n.useRef(null),A=n.useRef(null),O=n.useRef(null),C=n.useRef([]),X=n.useRef(!0),[,q]=n.useReducer(t=>t+1,0),{editorEnabled:U,selectAll:Q}=(t=>{const[e,r]=n.useState(!1),o=n.useRef(!1);return n.useEffect(()=>{var e=!0;return setTimeout(()=>{e&&(t._editModeEnabled&&(o.current=!0),r(!0),setTimeout(()=>{o.current=!1},50))},50),()=>{e=!1}},[]),{editorEnabled:e&&t._editModeEnabled,selectAll:o.current}})(a),[tt,et]=n.useState(!1),[rt,it]=n.useState(!1),at=n.useRef(a.a.height),lt=n.useRef(0),st=n.useRef(""),ct=n.useRef(0),dt=a.isSelected,ut=usePrevious(dt),{textVerticalResizeEnabled:ft}=M,ht=M.autoDeleteEmptyText,gt=usePrevious(a.fontFamily),[pt,mt]=n.useState([]),xt=a.backgroundEnabled||a.borderSize>0;n.useLayoutEffect(()=>{var t,e;if(!xt){return}const r=null!==(e=null===(t=h.current)||void 0===t?void 0:t.textArr)&&void 0!==e?e:[];JSON.stringify(r)!==JSON.stringify(pt)&&mt(r)}),n.useEffect(()=>{if(a.a.width||"tablecell"===a.type){return}const t=h.current;t.width(600),a.set({width:1.4*t.getTextWidth()})},[]),n.useEffect(()=>{ut&&!dt&&ht&&""===a.text&&a.removable&&!Z(a.placeholder)&&u.deleteElements([a.id])},[ht,a.placeholder,a.removable,a.text,dt,ut]);const bt=()=>{if(a.curveEnabled){const t=h.current;null==t||t.clearCache(),null==t||t.setAttrs({filters:[]});const e=A.current;e&&N(e,a)}else{const t=h.current;t&&N(t,a)}};n.useLayoutEffect(()=>{if(!rt){return f(a,()=>{bt()})}}),n.useEffect(()=>{const t=C.current;t.length>0&&(C.current=[],t.forEach(t=>t()))}),n.useEffect(()=>()=>{X.current=!1;const t=C.current;t.length>0&&(C.current=[],t.forEach(t=>t()))},[]);let vt=H(a.text);"uppercase"===a.textTransform&&(vt=vt.toUpperCase());const[St]=useFontLoader(u,a.fontFamily,a.fontStyle,a.fontWeight,a.text||a.placeholder),wt=()=>{if(a.curveEnabled){const t=A.current;return t.getSelfRect().height||t.fontSize()}const t=h.current.clone({height:void 0}),e=Math.ceil(t.fontSize()*t.lineHeight()*t.textArr.length+1);return t.destroy(),e};n.useLayoutEffect(()=>{if(!St){return}if("tablecell"===a.type){const t=T(L(a));if("resize"===t){const t=wt(),e=_(a)?V(a,2):null;(null==e?void 0:e.setCellContentHeight)&&e.setCellContentHeight(a.id,t)}else if("change-font-size"===t){const t=h.current;if(t){const e=nt(t,a,M.textSplitAllowed);e!==a.fontSize&&u.history.ignore(()=>{a.set({fontSize:e})})}}return}const t=T(L(a)),{textSplitAllowed:e}=M,r=(t,e,r,o)=>{const n=S(`text ${a.id}`),i=()=>{u.history.ignore(t,e,r).then(()=>{X.current?(C.current.push(n),q()):n()})};o?queueMicrotask(i):i()};if(!G(a)){const t=wt();return void r(()=>{G(a)||a.set({height:t})},void 0,void 0,!0)}if(!u.isPlaying){if("change-font-size"!==t||tt){if("resize"===t){const t=wt();ft&&a.height<t&&!tt&&r(()=>{var e;_(a)&&a.set({height:t}),null===(e=h.current)||void 0===e||e.height(t)},!1,!0),ft||a.height===t||tt||r(()=>{var e;_(a)&&a.set({height:t}),null===(e=h.current)||void 0===e||e.height(t)},!1,!0)}}else{const t=h.current,o=a.curveEnabled?a.fontSize:nt(t,a,e);if(o!==a.fontSize){return void r(()=>{a.set(y(a,o))},!1,!0)}const n=wt();a.height===n||ft||r(()=>{a.set({height:n})},!1,!0)}}}),n.useLayoutEffect(()=>{var t;if(St&&a.curveEnabled){const e=null===(t=A.current)||void 0===t?void 0:t.getSelfRect().width;if(e){const t=e-a.a.width,r=a.a.rotation*Math.PI/180,o=-t/2*Math.cos(r),n=-t/2*Math.sin(r);a.set({width:e,x:a.a.x+o,y:a.a.y+n})}}},[St,a.curveEnabled,a.curvePower,a.text,a.fontSize,a.fontFamily,a.fontWeight,a.fontStyle,a.letterSpacing]),n.useLayoutEffect(()=>{const t=h.current;t&&(t.width(t.width()+1e-8),t.width(t.width()-1e-8),t._setTextData(),bt())},[St]);const yt=n.useRef(null),Et=n.useRef(a.width),zt=n.useRef(0),kt=t=>{t.evt.preventDefault();const e=u.selectedShapes.find(t=>t===a);e&&a.contentEditable&&(zt.current=function(t){var e;const r=t.target,o=function(t){var e=t.getAbsoluteTransform().copy();e.invert();var r=t.getStage().getPointerPosition();return e.point(r)}(r),n=r.textArr,i=Math.floor(o.y/(r.fontSize()*r.lineHeight())),a=n.slice(0,i).reduce((t,e)=>t+e.text.length,i),l=null!==(e=n[i])&&void 0!==e?e:n[0];let s=0;return"right"===r.align()?s=r.width()-l.width:"center"===r.align()&&(s=r.width()/2-l.width/2),a+Math.round((o.x-s)/l.width*l.text.length)}(t),a.toggleEditMode())},Rt=Z(a.placeholder),Ft=!vt&&Rt?.6:o(a);B(h,Ft);const jt=getLineHeight({fontLoaded:St,fontFamily:a.fontFamily,fontSize:a.a.fontSize,lineHeight:a.lineHeight}),At=a.selectable||u.role===v.ADMIN,Mt=b(a),Lt=b(a,a.stroke,"stroke"),Ot=n.useMemo(()=>a.backgroundEnabled||a.borderSize?a.curveEnabled?function({width:t,height:e,padding:r=0,cornerRadius:o=0}){const n=-r,i=-r,a=t+r,l=e+r,s=g(`M ${n} ${i} L ${a} ${i} L ${a} ${l} L ${n} ${l} Z`);return p(s,o).path}({width:a.a.width,height:a.a.height,cornerRadius:a.backgroundCornerRadius*(a.a.fontSize*jt*.5),padding:a.backgroundPadding*(a.a.fontSize*jt*.5)}):function({lines:t,lineHeight:e,width:r,align:o="left",padding:n=0,cornerRadius:i=0}){const a=x({lines:t,lineHeight:e,width:r,align:o});return m({rects:a,padding:n,cornerRadius:i})}({lines:JSON.parse(JSON.stringify(pt)),cornerRadius:a.backgroundCornerRadius*(a.a.fontSize*jt*.5),lineHeight:jt*a.a.fontSize,padding:a.backgroundPadding*(a.a.fontSize*jt*.5),width:a.a.width,align:a.align}):"",[a.backgroundEnabled,a.borderSize,a.curveEnabled,a.backgroundCornerRadius,a.a.fontSize,a.a.height,jt,a.backgroundPadding,a.a.width,a.align,pt]),Ct=I();let Tt=0;if(!a.curveEnabled){const t=pt.length*jt*a.a.fontSize,e=K(a.a,t);"middle"===a.verticalAlign?Tt=(e-t)/2:"bottom"===a.verticalAlign&&(Tt=e-t)}const Ht=a.curveEnabled?W(a.a.width,a.a.height,a.curvePower,a.a.fontSize):"",Pt=St?Y(a.fontFamily):gt===a.fontFamily?"Arial":Y(gt),Wt=P(vt),Xt=a.shadowEnabled&&!$(a),Dt={shadowEnabled:a.shadowEnabled&&($(a)||a.borderSize>0),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:i(a.shadowColor),shadowOpacity:a.shadowOpacity};return r(e,{children:[xt&&t(s,Object.assign({ref:O,x:a.a.x,y:a.a.y,rotation:a.a.rotation,hideInExport:!a.showInExport,listening:!1,opacity:Ft,data:Ot,fillEnabled:a.backgroundEnabled,fill:J(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,lineJoin:a.backgroundCornerRadius>0?"round":"miter",offsetY:-Tt},Dt)),a.curveEnabled&&t(d,Object.assign({ref:A,data:Ht,text:vt||Rt,listening:!1,align:"center",textBaseline:"middle",direction:Wt},Mt,Lt,{strokeWidth:a.strokeWidth,lineJoin:D(a.strokeLineJoin),fillAfterStrokeEnabled:!0,fontSize:a.a.fontSize,fontFamily:`${Y(a.fontFamily)}, ${Y(gt)}`,fontStyle:a.fontStyle+" "+a.fontWeight,textDecoration:a.textDecoration.trim(),letterSpacing:a.letterSpacing*a.a.fontSize,x:a.a.x,y:a.a.y,rotation:a.a.rotation,opacity:a._editModeEnabled?.3:Ft,hideInExport:!a.showInExport},Dt,{shadowEnabled:Xt})),t(c,Object.assign({ref:h,id:a.selectable?a.id:void 0,name:a.selectable?"element":"",hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled,x:a.a.x,y:a.a.y,rotation:a.a.rotation,width:a.a.width,height:G(a.a)?a.a.height:void 0,text:vt||Rt,direction:Wt},Mt,Lt,{lineJoin:D(a.strokeLineJoin),strokeWidth:a.strokeWidth,fillAfterStrokeEnabled:!0,fontSize:a.a.fontSize,fontFamily:Pt,fontStyle:a.fontStyle+" "+a.fontWeight,textDecoration:a.textDecoration,align:a.align,verticalAlign:a.verticalAlign,draggable:Ct?a.draggable&&dt:a.draggable,preventDefault:!Ct||dt,opacity:a.curveEnabled?0:Ft,visible:!a._editModeEnabled,ellipsis:"ellipsis"===L(a)},Dt,{shadowEnabled:Xt,lineHeight:jt,letterSpacing:a.letterSpacing*a.a.fontSize,listening:!!a.selectable&&At,onDragStart:()=>{it(!0)},onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{it(!1),a.set(R(t.target))},onClick:kt,onTap:kt,onTransformStart:()=>{et(!0);const t=h.current;at.current=t.height(),Et.current=a.a.width,yt.current=t.position(),lt.current=t.rotation(),ct.current=t.height(),st.current=w(t)},onTransform:t=>{var e,r;const o=t.target;null===(e=O.current)||void 0===e||e.setAttrs({x:o.x(),y:o.y(),rotation:o.rotation(),scale:o.scale()});const n=st.current;if("middle-left"===n||"middle-right"===n){const t=o.scaleX(),e=o.width()*t,r=F(Et.current,a.a.fontSize);let n=e;e<r&&(n=r,yt.current&&o.position(yt.current)),o.width(n),o.scaleX(1),o.scaleY(1);let i=wt();if("ellipsis"===L(a)){i=at.current}else{const t=Math.max(i,at.current);o.height(t),a.set({height:o.height()})}const l=M.textVerticalResizeEnabled?Math.max(i,at.current):wt();a.set({x:o.x(),width:o.width(),rotation:o.rotation(),height:l}),o.skewX(0),o.skewY(0),bt()}if("top-center"===n||"bottom-center"===n){const e="resize"===L(a)?wt():jt*a.a.fontSize,r=t.target.height()*t.target.scaleY();if(r<e){if(t.target.height(e),yt.current){if("top-center"===st.current){const r=lt.current*Math.PI/180,o=ct.current-e;t.target.x(yt.current.x-o*Math.sin(r)),t.target.y(yt.current.y+o*Math.cos(r))}else{t.target.position(yt.current)}}}else{t.target.height(r)}t.target.scaleY(1),t.target.scaleX(1),t.target.rotation(lt.current),t.target.skewX(0),t.target.skewY(0),ct.current=Math.max(e,r)}yt.current=t.target.position();const i=j(t.target.scaleX());null===(r=O.current)||void 0===r||r.setAttrs({scaleX:1,scaleY:1}),t.target.scaleX(1),t.target.scaleY(1),a.set({fontSize:a.a.fontSize*i,width:t.target.width()*i,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*i,shadowBlur:a.shadowBlur*i,shadowOffsetX:a.shadowOffsetX*i,shadowOffsetY:a.shadowOffsetY*i,strokeWidth:a.strokeWidth*i,borderSize:a.borderSize*i})},onTransformEnd:t=>{var e;const r=j(t.target.scaleX());t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},E(a,a.fontSize*r)),{width:Math.ceil(t.target.width()*r),x:k(t.target.x()),y:k(t.target.y()),rotation:k(t.target.rotation()),height:k(t.target.height()*r)})),null===(e=O.current)||void 0===e||e.setAttrs({scaleX:1,scaleY:1}),et(!1)}})),U&&a.contentEditable&&t(l,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,children:t(ot,{textNodeRef:h,element:a,selectAll:Q,cursorPosition:zt.current,onBlur:()=>{a.set(z(a)),a.toggleEditMode(!1)}})})]})});
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ import Konva from 'konva';
3
+ import type { ShapeType } from '../model/shape-model.js';
4
+ export declare function useAnimatedBlur(ref: React.RefObject<Konva.Node | null>, element: ShapeType, source: unknown): () => void;
@@ -0,0 +1 @@
1
+ import r from"react";import{getElementBlurRadius as t}from"@polotno/core/filters";import{nodeAutorun as e}from"../utils/mobx.js";const i=(r,e,i)=>e?t({blurRadius:i,width:r.a.width,height:r.a.height,cropWidth:r.a.cropWidth,cropHeight:r.a.cropHeight})/2:0;export function useAnimatedBlur(t,o,a){const c=r.useRef(!1);return r.useEffect(()=>e(o,()=>{const r=i(o,o.a.blurEnabled,o.a.blurRadius),e=i(o,o.blurEnabled,o.blurRadius),a=t.current;if(!a){return}const l=Math.sqrt(Math.max(0,r**2-e**2));l>.01?(a.filters([`blur(${l}px)`]),a.cache({pixelRatio:o.store._elementsPixelRatio}),c.current=!0):c.current&&(a.filters([]),a.clearCache(),c.current=!1)}),[o,a]),r.useCallback(()=>{var r;c.current&&(null===(r=t.current)||void 0===r||r.cache({pixelRatio:o.store._elementsPixelRatio}))},[o])}
@@ -27,7 +27,7 @@ export declare const useColor: (element: ColorElement, value?: string, propName?
27
27
  fillLinearGradientColorStops: (string | number)[];
28
28
  fillLinearGradientEndPointX: number;
29
29
  fillLinearGradientEndPointY: number;
30
- fill: any;
30
+ fill: string;
31
31
  fillPriority: string;
32
32
  strokeLinearGradientStartPointX?: undefined;
33
33
  strokeLinearGradientStartPointY?: undefined;
@@ -1 +1 @@
1
- import t from"react";import e from"konva";import{isAlive as r}from"mobx-state-tree";import{applyFiltersToCanvas as o,hasActiveFilters as s,getFiltersDepsKey as n}from"./apply-filters.js";import{trySetCanvasSize as m}from"../utils/canvas.js";import{sourceSize as i}from"./downscale.js";export const useFilteredSource=(a,c)=>{const f=n(a),u=r(a)&&s(a),p=t=>{if(!c||!r(a)){return}const{width:e,height:s}=i(c);if(!e||!s){return}m(t,e,s);const n=t.getContext("2d");n&&(n.drawImage(c,0,0),o(t,a))},l=t.useMemo(()=>{if(!u||!c){return}const t=document.createElement("canvas");return p(t),t},[c,u,f]);return t.useEffect(()=>()=>{l&&e.Util.releaseCanvas(l)},[l]),[l||c||void 0,()=>{l&&p(l)}]};
1
+ import{isAlive as t}from"mobx-state-tree";import{applyFiltersToCanvas as r,hasActiveFilters as o,getFiltersDepsKey as e}from"./apply-filters.js";import{trySetCanvasSize as s}from"../utils/canvas.js";import{sourceSize as i}from"./downscale.js";import{useOffscreenCanvas as m}from"./use-offscreen-canvas.js";export const useFilteredSource=(n,f)=>{const a=e(n),c=t(n)&&o(n),p=o=>{if(!f||!t(n)){return}const{width:e,height:m}=i(f);if(!e||!m){return}s(o,e,m);const a=o.getContext("2d");a&&(a.drawImage(f,0,0),r(o,n))},u=m(c&&!!f,p,[f,a]);return[u||f||void 0,()=>{u&&p(u)}]};
@@ -0,0 +1,2 @@
1
+ import React from 'react';
2
+ export declare const useOffscreenCanvas: (enabled: boolean, paint: (canvas: HTMLCanvasElement) => void, deps: React.DependencyList) => HTMLCanvasElement | undefined;
@@ -0,0 +1 @@
1
+ import e from"react";import t from"konva";export const useOffscreenCanvas=(r,n,o)=>{const a=e.useMemo(()=>{if(!r){return}const e=document.createElement("canvas");return n(e),e},[r,...o]);return e.useLayoutEffect(()=>{if(a){return a.width&&a.height||n(a),()=>t.Util.releaseCanvas(a)}},[a]),a};
@@ -1 +1 @@
1
- import e from"react";import t from"konva";import{getClientRect as n}from"../utils/math.js";import{useWorkspaceStyle as o}from"./workspace-style.js";let r=({targetKonvaNodes:e,guideKonvaNode:t,distance:n,snapDirection:o})=>n<5;export const setSnapFilterFunc=e=>{r=e};function s(e,t){var n=[],o=[];e.vertical.forEach(e=>{t.vertical.forEach(t=>{var o=Math.abs(e.offset-t.guide);r({targetKonvaNodes:t.nodes,guideKonvaNode:e.node,distance:o,snapDirection:e.snap})&&n.push({lineGuide:e.offset,diff:o,snap:t.snap,offset:t.offset})})}),e.horizontal.forEach(e=>{t.horizontal.forEach(t=>{var n=Math.abs(e.offset-t.guide);r({targetKonvaNodes:t.nodes,guideKonvaNode:e.node,distance:n,snapDirection:e.snap})&&o.push({lineGuide:e.offset,diff:n,snap:t.snap,offset:t.offset})})});var s=[];const i=n.sort((e,t)=>e.diff-t.diff),a=o.sort((e,t)=>e.diff-t.diff);var d=i[0],c=a[0];return d&&i.filter(e=>Math.abs(e.diff-d.diff)<.1).forEach(e=>{s.push(Object.assign({orientation:"V"},e))}),c&&a.filter(e=>Math.abs(e.diff-c.diff)<.1).forEach(e=>{s.push(Object.assign({orientation:"H"},e))}),s}export const ensureDragOrder=()=>{if(0===t.DD._dragElements.size){return}const e=[...t.DD._dragElements.entries()],n=e.find(([e,n])=>n.node instanceof t.Transformer);n&&(e.splice(e.indexOf(n),1),e.unshift(n),t.DD._dragElements.clear(),e.forEach(([e,n])=>{t.DD._dragElements.set(e,n)}))};function i(e,n,o){return new t.Line({x:-e.x(),y:-e.y(),points:o,stroke:n.stroke,strokeWidth:n.strokeWidth,name:"guid-line",dash:n.dash})}function a(e,t,n){const o=null==e?void 0:e.children.find(e=>"line-guides"===e.name()),r=e.getStage();n.forEach(e=>{"H"===e.orientation?null==o||o.add(i(r,t,[-6e3,e.lineGuide,6e3,e.lineGuide])):"V"===e.orientation&&(null==o||o.add(i(r,t,[e.lineGuide,-6e3,e.lineGuide,6e3])))})}export function useSnap(r,f){const u=e.useRef(null),l=e.useRef(null),h=e.useRef(new Map),g=e.useRef(null),{snapGuideStyle:y}=o(),p=e.useRef(y);p.current=y;const x=e=>e.hasName("element")||e.hasName("page-background")||!u.current&&e.hasName("elements-area");function v(e){var t;const n=null===(t=r.current)||void 0===t?void 0:t.getStage(),o=[],s=[];return n.find(x).forEach(t=>{if(!(e.indexOf(t)>=0)){var n=t.getClientRect({skipShadow:!0,skipStroke:!0});o.push({offset:n.x,node:t,snap:"start"},{offset:n.x+n.width,node:t,snap:"end"},{offset:n.x+n.width/2,node:t,snap:"center"}),s.push({offset:n.y,node:t,snap:"start"},{offset:n.y+n.height,node:t,snap:"end"},{offset:n.y+n.height/2,node:t,snap:"center"})}}),{vertical:o,horizontal:s}}function m(e){var t;a(null===(t=r.current)||void 0===t?void 0:t.getLayer(),p.current,e)}const b=e=>{var o,r;const a=null===(o=e.target)||void 0===o?void 0:o.getLayer();if(!a){return}const d=a.children.find(e=>"line-guides"===e.name());null==d||d.destroyChildren();const c=l.current;if((null===(r=e.evt)||void 0===r?void 0:r.shiftKey)&&c){const t=e.target.getAbsolutePosition();let n=t.x-c.x,o=t.y-c.y;const r=Math.abs(n),s=Math.abs(o);if(r>2.414*s){o=0}else if(s>2.414*r){n=0}else{const e=(r+s)/2;n=Math.sign(n)*e,o=Math.sign(o)*e}e.target.nodes().forEach(e=>{const t=h.current.get(e);t&&e.setAbsolutePosition({x:t.x+n,y:t.y+o})});const f=g.current;if(f&&(r>0||s>0)){const e=Math.sign(n),t=Math.sign(o);null==d||d.add(i(a.getStage(),p.current,[f.x-6e3*e,f.y-6e3*t,f.x+6e3*e,f.y+6e3*t]))}return}var f=v(e.target.nodes()),u=function(e){const o=e.__getNodeRect(),r=n(Object.assign(Object.assign({},o),{rotation:t.Util.radToDeg(o.rotation)})),s=e.getAbsolutePosition();return{vertical:[{guide:r.x,offset:s.x-r.x,snap:"start",nodes:e.nodes()},{guide:r.x+r.width/2,offset:s.x-r.x-r.width/2,snap:"center",nodes:e.nodes()},{guide:r.x+r.width,offset:s.x-r.x-r.width,snap:"end",nodes:e.nodes()}],horizontal:[{guide:r.y,offset:s.y-r.y,snap:"start",nodes:e.nodes()},{guide:r.y+r.height/2,offset:s.y-r.y-r.height/2,snap:"center",nodes:e.nodes()},{guide:r.y+r.height,offset:s.y-r.y-r.height,snap:"end",nodes:e.nodes()}]}}(e.target),y=s(f,u);if(!y.length){return}m(y);const x=e.target.getAbsolutePosition(),b=Object.assign({},x);y.forEach(e=>{switch(e.snap){case"start":case"center":case"end":switch(e.orientation){case"V":b.x=e.lineGuide+e.offset;break;case"H":b.y=e.lineGuide+e.offset}}});const w=b.x-x.x,M=b.y-x.y;e.evt.ctrlKey||e.evt.metaKey||e.target.nodes().forEach(e=>{const t=e.getAbsolutePosition();e.setAbsolutePosition({x:t.x+w,y:t.y+M})})},w=(e,t,n)=>{var o;const i=r.current;if(!i){return t}const a=i.getLayer();if(!a){return t}const f=a.children.find(e=>"line-guides"===e.name());if(null==f||f.destroyChildren(),"rotater"===i.getActiveAnchor()){return t}const u=i.getActiveAnchor(),l=c[u],h=null===(o=i.findOne(`.${l}`))||void 0===o?void 0:o.getAbsolutePosition();if(!h){return t}const g={x:e.x-h.x,y:e.y-h.y},y=function(e,t){const n=d(e,t)/d(t,t);return{x:n*t.x,y:n*t.y}}({x:t.x-e.x,y:t.y-e.y},g);if(Math.sqrt(Math.pow(y.x,2)+Math.pow(y.y,2))>5){return t}const p={x:e.x+y.x,y:e.y+y.y};var x=s(v(i.nodes()),{vertical:[{guide:p.x,offset:0,snap:"start",nodes:i.nodes()}],horizontal:[{guide:p.y,offset:0,snap:"start",nodes:i.nodes()}]});if(!x.length){return t}if(m(x),n.ctrlKey||n.metaKey){return t}const b=[];if(x.forEach(e=>{const t=function(e,t,n){if("V"===n.orientation){const o=n.lineGuide;if(Math.abs(t.x-e.x)<1e-4){return null}const r=(t.y-e.y)/(t.x-e.x);return{x:o,y:r*o+(e.y-r*e.x)}}{const o=n.lineGuide;if(Math.abs(t.y-e.y)<1e-4){return null}const r=(t.y-e.y)/(t.x-e.x);return{x:(o-e.y)/r+e.x,y:o}}}(p,h,{orientation:e.orientation,lineGuide:e.lineGuide});t&&b.push(t)}),b.length>0){let e=b[0],t=Math.sqrt(Math.pow(p.x-e.x,2)+Math.pow(p.y-e.y,2));if(b.forEach(n=>{const o=Math.sqrt(Math.pow(p.x-n.x,2)+Math.pow(p.y-n.y,2));o<t&&(t=o,e=n)}),t<10){return e}}return p},M=e=>{if(l.current=null,h.current=new Map,g.current=null,!e.target){return}const t=e.target.getLayer();if(!t){return}const n=t.children.find(e=>"line-guides"===e.name());null==n||n.destroyChildren(),t.batchDraw()};e.useEffect(()=>{r.current&&(r.current.anchorDragBoundFunc(w),r.current.on("dragstart",e=>{const t=r.current;if(t){l.current=t.getAbsolutePosition(),h.current=new Map(t.nodes().map(e=>[e,e.getAbsolutePosition()]));const e=t.getClientRect();g.current={x:e.x+e.width/2,y:e.y+e.height/2}}setTimeout(()=>{ensureDragOrder()})}),r.current.on("dragmove",b),r.current.on("dragend",M),r.current.on("transformend",M),r.current.on("transform",e=>{var t,n;e.evt.ctrlKey||e.evt.metaKey?null===(t=r.current)||void 0===t||t.rotationSnapTolerance(0):null===(n=r.current)||void 0===n||n.rotationSnapTolerance(5)}))},[])}export function useAnchorSnap(t,n,r){const{snapGuideStyle:i}=o(),d=e.useRef(i);d.current=i;const c=e=>e.hasName("element")||e.hasName("line-anchor")||e.hasName("page-background")||e.hasName("elements-area"),f=e=>{var o;const r=null===(o=e.target)||void 0===o?void 0:o.getLayer();if(!r){return}const i=r.children.find(e=>"line-guides"===e.name());null==i||i.destroyChildren();var f,u=function(e){var n;const o=null===(n=t.current)||void 0===n?void 0:n.getStage();var r=[],s=[];return o.find(c).forEach(t=>{if(!(e.indexOf(t)>=0)){var n=t.getClientRect({skipShadow:!0,skipStroke:!0});t.hasName("line-anchor")&&(n={x:t.absolutePosition().x,y:t.absolutePosition().y,width:0,height:0}),r.push({offset:n.x,node:t,snap:"start"},{offset:n.x+n.width,node:t,snap:"end"},{offset:n.x+n.width/2,node:t,snap:"center"}),s.push({offset:n.y,node:t,snap:"start"},{offset:n.y+n.height,node:t,snap:"end"},{offset:n.y+n.height/2,node:t,snap:"center"})}}),{vertical:r,horizontal:s}}([e.target,...n.map(e=>e.current)]),l=s(u,{vertical:[{guide:(f=e.target).absolutePosition().x,offset:0,snap:"center",nodes:[f]}],horizontal:[{guide:f.absolutePosition().y,offset:0,snap:"center",nodes:[f]}]});if(!l.length){return}!function(e){var n;a(null===(n=t.current)||void 0===n?void 0:n.getLayer(),d.current,e)}(l);const h=e.target.getAbsolutePosition(),g=Object.assign({},h);l.forEach(e=>{switch(e.snap){case"start":case"center":case"end":switch(e.orientation){case"V":g.x=e.lineGuide+e.offset;break;case"H":g.y=e.lineGuide+e.offset}}}),e.evt.ctrlKey||e.evt.metaKey||e.target.absolutePosition(g)},u=e=>{if(!e.target){return}const t=e.target.getLayer();if(!t){return}const n=t.children.find(e=>"line-guides"===e.name());null==n||n.destroyChildren(),t.batchDraw()};e.useEffect(()=>{t.current&&(t.current.on("dragmove",f),t.current.on("dragend",u))},r)}function d(e,t){return e.x*t.x+e.y*t.y}const c={"top-left":"bottom-right","top-center":"bottom-center","top-right":"bottom-left","middle-right":"middle-left","bottom-right":"top-left","bottom-center":"top-center","bottom-left":"top-right","middle-left":"middle-right"};
1
+ import e from"react";import t from"konva";import{getClientRect as n}from"../utils/math.js";import{isRotateAnchor as o}from"../utils/px.js";import{useWorkspaceStyle as r}from"./workspace-style.js";let s=({targetKonvaNodes:e,guideKonvaNode:t,distance:n,snapDirection:o})=>n<5;export const setSnapFilterFunc=e=>{s=e};function i(e,t){var n=[],o=[];e.vertical.forEach(e=>{t.vertical.forEach(t=>{var o=Math.abs(e.offset-t.guide);s({targetKonvaNodes:t.nodes,guideKonvaNode:e.node,distance:o,snapDirection:e.snap})&&n.push({lineGuide:e.offset,diff:o,snap:t.snap,offset:t.offset})})}),e.horizontal.forEach(e=>{t.horizontal.forEach(t=>{var n=Math.abs(e.offset-t.guide);s({targetKonvaNodes:t.nodes,guideKonvaNode:e.node,distance:n,snapDirection:e.snap})&&o.push({lineGuide:e.offset,diff:n,snap:t.snap,offset:t.offset})})});var r=[];const i=n.sort((e,t)=>e.diff-t.diff),a=o.sort((e,t)=>e.diff-t.diff);var d=i[0],c=a[0];return d&&i.filter(e=>Math.abs(e.diff-d.diff)<.1).forEach(e=>{r.push(Object.assign({orientation:"V"},e))}),c&&a.filter(e=>Math.abs(e.diff-c.diff)<.1).forEach(e=>{r.push(Object.assign({orientation:"H"},e))}),r}export const ensureDragOrder=()=>{if(0===t.DD._dragElements.size){return}const e=[...t.DD._dragElements.entries()],n=e.find(([e,n])=>n.node instanceof t.Transformer);n&&(e.splice(e.indexOf(n),1),e.unshift(n),t.DD._dragElements.clear(),e.forEach(([e,n])=>{t.DD._dragElements.set(e,n)}))};function a(e,n,o){return new t.Line({x:-e.x(),y:-e.y(),points:o,stroke:n.stroke,strokeWidth:n.strokeWidth,name:"guid-line",dash:n.dash})}function d(e,t,n){const o=null==e?void 0:e.children.find(e=>"line-guides"===e.name()),r=e.getStage();n.forEach(e=>{"H"===e.orientation?null==o||o.add(a(r,t,[-6e3,e.lineGuide,6e3,e.lineGuide])):"V"===e.orientation&&(null==o||o.add(a(r,t,[e.lineGuide,-6e3,e.lineGuide,6e3])))})}export function useSnap(s,u){const l=e.useRef(null),h=e.useRef(null),g=e.useRef(new Map),y=e.useRef(null),{snapGuideStyle:p}=r(),x=e.useRef(p);x.current=p;const v=e=>e.hasName("element")||e.hasName("page-background")||!l.current&&e.hasName("elements-area");function m(e){var t;const n=null===(t=s.current)||void 0===t?void 0:t.getStage(),o=[],r=[];return n.find(v).forEach(t=>{if(!(e.indexOf(t)>=0)){var n=t.getClientRect({skipShadow:!0,skipStroke:!0});o.push({offset:n.x,node:t,snap:"start"},{offset:n.x+n.width,node:t,snap:"end"},{offset:n.x+n.width/2,node:t,snap:"center"}),r.push({offset:n.y,node:t,snap:"start"},{offset:n.y+n.height,node:t,snap:"end"},{offset:n.y+n.height/2,node:t,snap:"center"})}}),{vertical:o,horizontal:r}}function b(e){var t;d(null===(t=s.current)||void 0===t?void 0:t.getLayer(),x.current,e)}const w=e=>{var o,r;const s=null===(o=e.target)||void 0===o?void 0:o.getLayer();if(!s){return}const d=s.children.find(e=>"line-guides"===e.name());null==d||d.destroyChildren();const c=h.current;if((null===(r=e.evt)||void 0===r?void 0:r.shiftKey)&&c){const t=e.target.getAbsolutePosition();let n=t.x-c.x,o=t.y-c.y;const r=Math.abs(n),i=Math.abs(o);if(r>2.414*i){o=0}else if(i>2.414*r){n=0}else{const e=(r+i)/2;n=Math.sign(n)*e,o=Math.sign(o)*e}e.target.nodes().forEach(e=>{const t=g.current.get(e);t&&e.setAbsolutePosition({x:t.x+n,y:t.y+o})});const f=y.current;if(f&&(r>0||i>0)){const e=Math.sign(n),t=Math.sign(o);null==d||d.add(a(s.getStage(),x.current,[f.x-6e3*e,f.y-6e3*t,f.x+6e3*e,f.y+6e3*t]))}return}var f=m(e.target.nodes()),u=function(e){const o=e.__getNodeRect(),r=n(Object.assign(Object.assign({},o),{rotation:t.Util.radToDeg(o.rotation)})),s=e.getAbsolutePosition();return{vertical:[{guide:r.x,offset:s.x-r.x,snap:"start",nodes:e.nodes()},{guide:r.x+r.width/2,offset:s.x-r.x-r.width/2,snap:"center",nodes:e.nodes()},{guide:r.x+r.width,offset:s.x-r.x-r.width,snap:"end",nodes:e.nodes()}],horizontal:[{guide:r.y,offset:s.y-r.y,snap:"start",nodes:e.nodes()},{guide:r.y+r.height/2,offset:s.y-r.y-r.height/2,snap:"center",nodes:e.nodes()},{guide:r.y+r.height,offset:s.y-r.y-r.height,snap:"end",nodes:e.nodes()}]}}(e.target),l=i(f,u);if(!l.length){return}b(l);const p=e.target.getAbsolutePosition(),v=Object.assign({},p);l.forEach(e=>{switch(e.snap){case"start":case"center":case"end":switch(e.orientation){case"V":v.x=e.lineGuide+e.offset;break;case"H":v.y=e.lineGuide+e.offset}}});const w=v.x-p.x,M=v.y-p.y;e.evt.ctrlKey||e.evt.metaKey||e.target.nodes().forEach(e=>{const t=e.getAbsolutePosition();e.setAbsolutePosition({x:t.x+w,y:t.y+M})})},M=(e,t,n)=>{var r;const a=s.current;if(!a){return t}const d=a.getLayer();if(!d){return t}const u=d.children.find(e=>"line-guides"===e.name());null==u||u.destroyChildren();const l=a.getActiveAnchor();if(o(l)){return t}const h=f[l],g=null===(r=a.findOne(`.${h}`))||void 0===r?void 0:r.getAbsolutePosition();if(!g){return t}const y={x:e.x-g.x,y:e.y-g.y},p=function(e,t){const n=c(e,t)/c(t,t);return{x:n*t.x,y:n*t.y}}({x:t.x-e.x,y:t.y-e.y},y);if(Math.sqrt(Math.pow(p.x,2)+Math.pow(p.y,2))>5){return t}const x={x:e.x+p.x,y:e.y+p.y};var v=i(m(a.nodes()),{vertical:[{guide:x.x,offset:0,snap:"start",nodes:a.nodes()}],horizontal:[{guide:x.y,offset:0,snap:"start",nodes:a.nodes()}]});if(!v.length){return t}if(b(v),n.ctrlKey||n.metaKey){return t}const w=[];if(v.forEach(e=>{const t=function(e,t,n){if("V"===n.orientation){const o=n.lineGuide;if(Math.abs(t.x-e.x)<1e-4){return null}const r=(t.y-e.y)/(t.x-e.x);return{x:o,y:r*o+(e.y-r*e.x)}}{const o=n.lineGuide;if(Math.abs(t.y-e.y)<1e-4){return null}const r=(t.y-e.y)/(t.x-e.x);return{x:(o-e.y)/r+e.x,y:o}}}(x,g,{orientation:e.orientation,lineGuide:e.lineGuide});t&&w.push(t)}),w.length>0){let e=w[0],t=Math.sqrt(Math.pow(x.x-e.x,2)+Math.pow(x.y-e.y,2));if(w.forEach(n=>{const o=Math.sqrt(Math.pow(x.x-n.x,2)+Math.pow(x.y-n.y,2));o<t&&(t=o,e=n)}),t<10){return e}}return x},E=e=>{if(h.current=null,g.current=new Map,y.current=null,!e.target){return}const t=e.target.getLayer();if(!t){return}const n=t.children.find(e=>"line-guides"===e.name());null==n||n.destroyChildren(),t.batchDraw()};e.useEffect(()=>{s.current&&(s.current.anchorDragBoundFunc(M),s.current.on("dragstart",e=>{const t=s.current;if(t){h.current=t.getAbsolutePosition(),g.current=new Map(t.nodes().map(e=>[e,e.getAbsolutePosition()]));const e=t.getClientRect();y.current={x:e.x+e.width/2,y:e.y+e.height/2}}setTimeout(()=>{ensureDragOrder()})}),s.current.on("dragmove",w),s.current.on("dragend",E),s.current.on("transformend",E),s.current.on("transform",e=>{var t,n;e.evt.ctrlKey||e.evt.metaKey?null===(t=s.current)||void 0===t||t.rotationSnapTolerance(0):null===(n=s.current)||void 0===n||n.rotationSnapTolerance(5)}))},[])}export function useAnchorSnap(t,n,o){const{snapGuideStyle:s}=r(),a=e.useRef(s);a.current=s;const c=e=>e.hasName("element")||e.hasName("line-anchor")||e.hasName("page-background")||e.hasName("elements-area"),f=e=>{var o;const r=null===(o=e.target)||void 0===o?void 0:o.getLayer();if(!r){return}const s=r.children.find(e=>"line-guides"===e.name());null==s||s.destroyChildren();var f,u=function(e){var n;const o=null===(n=t.current)||void 0===n?void 0:n.getStage();var r=[],s=[];return o.find(c).forEach(t=>{if(!(e.indexOf(t)>=0)){var n=t.getClientRect({skipShadow:!0,skipStroke:!0});t.hasName("line-anchor")&&(n={x:t.absolutePosition().x,y:t.absolutePosition().y,width:0,height:0}),r.push({offset:n.x,node:t,snap:"start"},{offset:n.x+n.width,node:t,snap:"end"},{offset:n.x+n.width/2,node:t,snap:"center"}),s.push({offset:n.y,node:t,snap:"start"},{offset:n.y+n.height,node:t,snap:"end"},{offset:n.y+n.height/2,node:t,snap:"center"})}}),{vertical:r,horizontal:s}}([e.target,...n.map(e=>e.current)]),l=i(u,{vertical:[{guide:(f=e.target).absolutePosition().x,offset:0,snap:"center",nodes:[f]}],horizontal:[{guide:f.absolutePosition().y,offset:0,snap:"center",nodes:[f]}]});if(!l.length){return}!function(e){var n;d(null===(n=t.current)||void 0===n?void 0:n.getLayer(),a.current,e)}(l);const h=e.target.getAbsolutePosition(),g=Object.assign({},h);l.forEach(e=>{switch(e.snap){case"start":case"center":case"end":switch(e.orientation){case"V":g.x=e.lineGuide+e.offset;break;case"H":g.y=e.lineGuide+e.offset}}}),e.evt.ctrlKey||e.evt.metaKey||e.target.absolutePosition(g)},u=e=>{if(!e.target){return}const t=e.target.getLayer();if(!t){return}const n=t.children.find(e=>"line-guides"===e.name());null==n||n.destroyChildren(),t.batchDraw()};e.useEffect(()=>{t.current&&(t.current.on("dragmove",f),t.current.on("dragend",u))},o)}function c(e,t){return e.x*t.x+e.y*t.y}const f={"top-left":"bottom-right","top-center":"bottom-center","top-right":"bottom-left","middle-right":"middle-left","bottom-right":"top-left","bottom-center":"top-center","bottom-left":"top-right","middle-left":"middle-right"};