polotno 4.22.1 → 4.24.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 (91) hide show
  1. package/THIRD_PARTY_NOTICES.md +7 -7
  2. package/blueprint/toolbar/color-picker.js +2 -2
  3. package/canvas/apply-filters.js +1 -1
  4. package/canvas/downscale.d.ts +1 -1
  5. package/canvas/downscale.js +1 -1
  6. package/canvas/element.js +1 -1
  7. package/canvas/fade-overlay.d.ts +5 -0
  8. package/canvas/fade-overlay.js +1 -1
  9. package/canvas/hotkeys.js +1 -1
  10. package/canvas/html-element.js +1 -1
  11. package/canvas/image-element.js +1 -1
  12. package/canvas/line-element.js +1 -1
  13. package/canvas/page.js +1 -1
  14. package/canvas/quill-font-formats.d.ts +1 -0
  15. package/canvas/quill-font-formats.js +1 -1
  16. package/canvas/radial-gradient-handle.d.ts +14 -0
  17. package/canvas/radial-gradient-handle.js +1 -0
  18. package/canvas/render-tag-element.js +1 -1
  19. package/canvas/rich-editor.d.ts +2 -0
  20. package/canvas/rich-editor.js +12 -6
  21. package/canvas/text-element/legacy-padding-warning.d.ts +6 -0
  22. package/canvas/text-element/legacy-padding-warning.js +1 -0
  23. package/canvas/text-element/max-font-size.js +1 -1
  24. package/canvas/text-element.js +1 -1
  25. package/canvas/use-color.d.ts +24 -2
  26. package/canvas/use-color.js +1 -1
  27. package/canvas/video-element.js +1 -1
  28. package/icons/registry.d.ts +1 -0
  29. package/icons/registry.js +1 -1
  30. package/image-tools/raster.js +1 -1
  31. package/model/group-model.d.ts +26 -10
  32. package/model/group-model.js +1 -1
  33. package/model/node-model.d.ts +10 -1
  34. package/model/node-model.js +1 -1
  35. package/model/page-model.d.ts +75 -33
  36. package/model/page-model.js +1 -1
  37. package/model/table-model.d.ts +29 -9
  38. package/model/table-model.js +1 -1
  39. package/model/text-model.d.ts +5 -1
  40. package/model/text-model.js +1 -1
  41. package/package.json +16 -16
  42. package/polotno.bundle.js +194 -202
  43. package/primitives/field-group.js +1 -1
  44. package/primitives/popover.d.ts +1 -1
  45. package/primitives/popover.js +1 -1
  46. package/primitives/slider-field.js +1 -1
  47. package/side-panel/effect-card.js +1 -1
  48. package/side-panel/effects-panel.js +1 -1
  49. package/side-panel/images-grid.js +1 -1
  50. package/side-panel/layers-panel.js +1 -1
  51. package/side-panel/tab-button.js +1 -3
  52. package/side-panel/templates-panel.js +1 -1
  53. package/toolbar/color-picker.js +1 -1
  54. package/toolbar/gradient-center-pad.d.ts +11 -0
  55. package/toolbar/gradient-center-pad.js +1 -0
  56. package/toolbar/html-toolbar.js +2 -2
  57. package/toolbar/padding-control.d.ts +29 -0
  58. package/toolbar/padding-control.js +1 -0
  59. package/toolbar/selection-colors.js +1 -1
  60. package/toolbar/stop-track.js +1 -1
  61. package/toolbar/table-toolbar.js +1 -1
  62. package/toolbar/text-toolbar.d.ts +7 -2
  63. package/toolbar/text-toolbar.js +1 -1
  64. package/toolbar/unit-numeric-input.d.ts +5 -0
  65. package/toolbar/unit-numeric-input.js +1 -0
  66. package/toolbar/use-copy-style.js +1 -1
  67. package/ui.css +1 -1
  68. package/utils/clipboard.d.ts +10 -2
  69. package/utils/clipboard.js +1 -1
  70. package/utils/engine.d.ts +2 -0
  71. package/utils/engine.js +3 -0
  72. package/utils/fonts.js +1 -1
  73. package/utils/gradient-handle.d.ts +18 -0
  74. package/utils/gradient-handle.js +1 -0
  75. package/utils/gradient-stops.d.ts +7 -2
  76. package/utils/gradient-stops.js +1 -1
  77. package/utils/history-gesture.d.ts +7 -0
  78. package/utils/history-gesture.js +1 -0
  79. package/utils/html2canvas.js +1 -1
  80. package/utils/l10n.d.ts +19 -1
  81. package/utils/l10n.js +1 -1
  82. package/utils/measure-text.d.ts +4 -0
  83. package/utils/measure-text.js +1 -1
  84. package/utils/px.d.ts +4 -0
  85. package/utils/px.js +1 -1
  86. package/utils/rich-text-html.d.ts +1 -0
  87. package/utils/rich-text-html.js +1 -1
  88. package/utils/selection-transform.js +1 -1
  89. package/utils/text-format.d.ts +5 -0
  90. package/utils/text-format.js +1 -1
  91. package/utils/validate-key.js +1 -1
@@ -738,7 +738,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
738
738
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
739
739
  SOFTWARE.
740
740
 
741
- ## konva - 10.9.1 (MIT)
741
+ ## konva - 10.11.0 (MIT)
742
742
 
743
743
  MIT License
744
744
 
@@ -913,7 +913,7 @@ maintained libraries used by this software which have their own
913
913
  licenses; we recommend you read them, as their terms may differ from the
914
914
  terms above.
915
915
 
916
- ## lucide-react - 1.75.0 (ISC)
916
+ ## lucide-react - 1.77.0 (ISC)
917
917
 
918
918
  ISC License
919
919
 
@@ -959,7 +959,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
959
959
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
960
960
  SOFTWARE.
961
961
 
962
- ## mediabunny - 1.66.0 (MPL-2.0)
962
+ ## mediabunny - 1.66.2 (MPL-2.0)
963
963
 
964
964
  Mozilla Public License Version 2.0
965
965
  ==================================
@@ -1359,7 +1359,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1359
1359
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1360
1360
  SOFTWARE.
1361
1361
 
1362
- ## mobx-react-lite - 5.1.3 (MIT)
1362
+ ## mobx-react-lite - 5.1.4 (MIT)
1363
1363
 
1364
1364
  The MIT License (MIT)
1365
1365
 
@@ -1657,7 +1657,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1657
1657
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1658
1658
  SOFTWARE.
1659
1659
 
1660
- ## react-window - 2.5.3 (MIT)
1660
+ ## react-window - 2.5.4 (MIT)
1661
1661
 
1662
1662
  The MIT License (MIT)
1663
1663
 
@@ -1705,7 +1705,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1705
1705
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1706
1706
  SOFTWARE.
1707
1707
 
1708
- ## render-tag - 0.4.6 (MIT)
1708
+ ## render-tag - 0.4.7 (MIT)
1709
1709
 
1710
1710
  ## scheduler - 0.28.0 (MIT)
1711
1711
 
@@ -1790,7 +1790,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1790
1790
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1791
1791
  SOFTWARE.
1792
1792
 
1793
- ## tailwind-merge - 3.7.0 (MIT)
1793
+ ## tailwind-merge - 3.7.1 (MIT)
1794
1794
 
1795
1795
  MIT License
1796
1796
 
@@ -1,4 +1,4 @@
1
- var e=this&&this.__rest||function(e,t){var r={};for(var o in e){Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(r[o]=e[o])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(o=Object.getOwnPropertySymbols(e);n<o.length;n++){t.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(e,o[n])&&(r[o[n]]=e[o[n]])}}return r};import{jsx as t,jsxs as r}from"react/jsx-runtime";import o from"react";import{Tab as n,Tabs as i,Popover as s,NumericInput as l}from"@blueprintjs/core";import{Slider as a}from"@blueprintjs/core";import{observer as c}from"mobx-react-lite";import{t as d}from"../../utils/l10n.js";import p from"./sketch.js";import u from"../../utils/styled.js";import{parseColor as g,isGradient as h}from"../../utils/gradient.js";import{sameColors as f}from"../../utils/svg.js";export const DEFAULT_COLORS=["#D0021B","#F5A623","#F8E71C","#8B572A","#7ED321","#417505","#BD10E0","#9013FE","#4A90E2","#50E3C2","#B8E986","#000000","#4A4A4A","#9B9B9B","#FFFFFF"].reverse();export const getUsedColors=e=>{const t=[];e.pages.forEach(e=>{e.children.forEach(e=>{e.shadowEnabled&&t.push(e.shadowColor),"text"===e.type&&(t.push(e.fill),e.backgroundEnabled&&t.push(e.backgroundColor),e.strokeWidth&&t.push(e.stroke)),"svg"===e.type&&t.push(...e.colorsReplace.values()),"figure"===e.type&&(t.push(e.fill),e.strokeWidth&&t.push(e.stroke)),"line"===e.type&&t.push(e.color)})});const r=[];return t.forEach(e=>{if(h(e)){const{stops:t}=g(e);r.push(...t.map(e=>e.color))}else{r.push(e)}}),[...new Set(r)]};const m=u("div")`
1
+ var e=this&&this.__rest||function(e,t){var r={};for(var o in e){Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(r[o]=e[o])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(o=Object.getOwnPropertySymbols(e);n<o.length;n++){t.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(e,o[n])&&(r[o[n]]=e[o[n]])}}return r};import{jsx as t,jsxs as r}from"react/jsx-runtime";import o from"react";import{Tab as n,Tabs as i,Popover as s,NumericInput as l}from"@blueprintjs/core";import{Slider as a}from"@blueprintjs/core";import{observer as c}from"mobx-react-lite";import{t as d}from"../../utils/l10n.js";import p from"./sketch.js";import u from"../../utils/styled.js";import{parseColor as g,isGradient as h,isRadialGradient as f,serializeGradient as m}from"../../utils/gradient.js";import{sameColors as b}from"../../utils/svg.js";export const DEFAULT_COLORS=["#D0021B","#F5A623","#F8E71C","#8B572A","#7ED321","#417505","#BD10E0","#9013FE","#4A90E2","#50E3C2","#B8E986","#000000","#4A4A4A","#9B9B9B","#FFFFFF"].reverse();export const getUsedColors=e=>{const t=[];e.pages.forEach(e=>{e.children.forEach(e=>{e.shadowEnabled&&t.push(e.shadowColor),"text"===e.type&&(t.push(e.fill),e.backgroundEnabled&&t.push(e.backgroundColor),e.strokeWidth&&t.push(e.stroke)),"svg"===e.type&&t.push(...e.colorsReplace.values()),"figure"===e.type&&(t.push(e.fill),e.strokeWidth&&t.push(e.stroke)),"line"===e.type&&t.push(e.color)})});const r=[];return t.forEach(e=>{if(h(e)){const{stops:t}=g(e);r.push(...t.map(e=>e.color))}else{r.push(e)}}),[...new Set(r)]};const v=u("div")`
2
2
  & .sketch-picker {
3
3
  padding: 0px !important;
4
4
  box-shadow: none !important;
@@ -16,4 +16,4 @@ var e=this&&this.__rest||function(e,t){var r={};for(var o in e){Object.prototype
16
16
  .bp6-dark & .sketch-picker label {
17
17
  color: #f5f8fa !important;
18
18
  }
19
- `,b=r=>{var{style:n,color:i,onChange:s}=r,l=e(r,["style","color","onChange"]);const[a,c]=o.useState(i);o.useEffect(()=>{c(i)},[i]);const d=o.useRef(void 0),u=o.useRef(void 0);return t(m,{style:n,onMouseDown:e=>{"INPUT"!==e.target.tagName&&e.preventDefault()},children:t(p,Object.assign({color:a},l,{onChange:e=>{const{r:t,g:r,b:o,a:n}=e.rgb,i=`rgba(${t},${r},${o},${n})`;c(i),u.current=i,d.current||(d.current=window.setTimeout(()=>{d.current=0,s(i)},50))}}))})},v=e=>e.map(e=>`${e.color} ${100*e.offset}%`).join(","),x=({value:e,onChange:n,store:i,preset:s})=>{const{rotation:c,stops:p}=g(e),u=o.useRef(p);u.current=p;const h=o.useRef(c);h.current=c;const[f,m]=o.useState(0),[x,y]=o.useState(-1),w=o.useRef(null),k=o.useRef(!1),E=e=>{k.current=!0};return o.useEffect(()=>{x>-1&&(u.current.splice(x,1),n(`linear-gradient(${h.current}deg, ${v(u.current)})`),y(-1))},[x]),o.useEffect(()=>{var e;null===(e=w.current)||void 0===e||e.focus()},[]),o.useEffect(()=>{const e=e=>{if(k.current){const t=w.current.getBoundingClientRect(),r=e.clientX-t.left,o=r/t.width,i=o<-.5||o>1.5;if(p.length>2&&i){return m(0),k.current=!1,void y(f)}const s=Math.min(1,Math.max(0,r/t.width)),l=[...u.current];l[f].offset=s,n(`linear-gradient(${h.current}deg, ${v(l)})`)}},t=()=>{k.current=!1};return window.addEventListener("mousemove",e),window.addEventListener("mouseup",t),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("mouseup",t)}},[f,u]),r("div",{children:[r("div",{style:{width:"calc(100% - 35px)",height:"60px",position:"relative"},children:[t("div",{ref:w,tabIndex:0,style:{position:"absolute",top:0,left:0,width:"100%",marginLeft:"15px",height:"100%",zIndex:0,outline:"none"},onKeyDownCapture:e=>{if("Backspace"===e.key){if(e.preventDefault(),e.stopPropagation(),u.current.length<=2){return}u.current.splice(f,1),f>=u.current.length&&m(u.current.length-1),n(`linear-gradient(${h.current}deg, ${v(u.current)})`)}},onMouseDown:e=>{const t=e.currentTarget.getBoundingClientRect(),r=e.clientX-t.left,o=Math.min(1,Math.max(0,r/t.width)),i=p[0].color;p.push({color:i,offset:o}),p.sort((e,t)=>e.offset-t.offset);const s=p.findIndex(e=>e.offset===o);m(s),E(),n(`linear-gradient(${c}deg, ${v(p)})`)}}),p.map(({offset:e,color:r},o)=>t("div",{style:{position:"absolute",top:"10px",left:100*e+"%",border:o===f?"2px solid rgb(0, 161, 255)":"2px solid rgb(0, 0, 0, 0)",padding:"2px"},onMouseDown:e=>{e.stopPropagation(),m(o),E()},children:t(C,{background:r,style:{margin:0}})},o))]}),t("div",{style:{width:"calc(100% - 30px)",height:"10px",marginLeft:"15px",background:`linear-gradient(90deg,${v(p)})`}}),r("div",{style:{padding:"10px"},children:[r("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",paddingTop:"5px",paddingBottom:"5px"},children:[t("div",{children:d("toolbar.colorPicker.angle")}),t("div",{children:t(l,{value:c,onValueChange:e=>{const t=Math.min(360,Math.max(0,e));n(`linear-gradient(${t}deg,${v(p)})`)},buttonPosition:"none",style:{width:"50px"},min:0,max:360})})]}),t(a,{min:0,max:360,value:c,showTrackFill:!1,labelRenderer:!1,onChange:e=>{n(`linear-gradient(${e}deg,${v(p)})`)}})]}),t(b,{color:p[f].color,presetColors:s,onChange:e=>{const t=[...p];t[f].color=e,n(`linear-gradient(${c}deg, ${v(t)})`)}})]})},y=c(({value:e,onChange:s,preset:l,store:a})=>{const[c,p]=o.useState(h(e)?"gradient":"solid");return t("div",{style:{padding:"5px"},children:r(i,{id:"TabsExample",selectedTabId:c,onChange:e=>p(e),renderActiveTabPanelOnly:!0,children:[t(n,{id:"solid",title:d("toolbar.colorPicker.solid"),panel:t(b,{color:e,onChange:s,presetColors:l,style:{padding:"0"}})}),t(n,{id:"gradient",title:d("toolbar.colorPicker.linear"),panel:t(x,{value:e,onChange:s,store:a,preset:l}),panelClassName:"ember-panel"})]})})}),C=r=>{var{size:o=30,background:n,style:i={},children:s=null}=r,l=e(r,["size","background","style","children"]);return t("div",Object.assign({role:"button",tabIndex:0,"aria-label":"Pick color",onMouseDown:e=>e.preventDefault(),style:Object.assign({width:o+"px",height:o+"px",background:"url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='15' height='15' viewBox='0 0 8 8'%3E%3Cg fill='rgba(112, 112, 116, 1)' fill-opacity='1'%3E%3Cpath fill-rule='evenodd' d='M0 0h4v4H0V0zm4 4h4v4H4V4z'/%3E%3C/g%3E%3C/svg%3E\")",borderRadius:"2px",boxShadow:"0 0 2px grey",marginBottom:"-4px"},i)},l,{children:t("div",{style:{width:o+"px",height:o+"px",background:n},children:s})}))};let w=e=>{const t=getUsedColors(e).slice(0,8).filter(e=>!DEFAULT_COLORS.find(t=>f(t,e)));return t.concat(DEFAULT_COLORS.slice(0,16-t.length).reverse())};export function setColorsPresetFunc(e){w=e}export const ColorPicker=({value:e,onChange:r,size:o,store:n,gradientEnabled:i,children:l,style:a,position:c="auto",onOpen:d,onClose:p})=>{const u=w(n);return t(s,{autoFocus:!1,enforceFocus:!0,interactionKind:"click",hasBackdrop:!0,position:c,onOpening:d,onClosed:p,content:i?t(y,{preset:u,value:e,onChange:r,store:n}):t("div",{style:{padding:"5px"},children:t(b,{color:e,onChange:r,presetColors:u})}),children:t(C,{size:o,background:e,style:a,children:l})})};export default ColorPicker;
19
+ `,x=r=>{var{style:n,color:i,onChange:s}=r,l=e(r,["style","color","onChange"]);const[a,c]=o.useState(i);o.useEffect(()=>{c(i)},[i]);const d=o.useRef(void 0),u=o.useRef(void 0);return t(v,{style:n,onMouseDown:e=>{"INPUT"!==e.target.tagName&&e.preventDefault()},children:t(p,Object.assign({color:a},l,{onChange:e=>{const{r:t,g:r,b:o,a:n}=e.rgb,i=`rgba(${t},${r},${o},${n})`;c(i),u.current=i,d.current||(d.current=window.setTimeout(()=>{d.current=0,s(i)},50))}}))})},y=e=>e.map(e=>`${e.color} ${100*e.offset}%`).join(","),C=(e,t)=>r=>{if(!f(e)||!h(r)){return t(r)}const{center:o,ending:n}=g(e);t(m({type:"radial",center:o,ending:n,stops:g(r).stops}))},w=({value:e,onChange:n,store:i,preset:s})=>{const{rotation:c,stops:p}=g(e),u=o.useRef(p);u.current=p;const h=o.useRef(c);h.current=c;const[f,m]=o.useState(0),[b,v]=o.useState(-1),C=o.useRef(null),w=o.useRef(!1),k=e=>{w.current=!0};return o.useEffect(()=>{b>-1&&(u.current.splice(b,1),n(`linear-gradient(${h.current}deg, ${y(u.current)})`),v(-1))},[b]),o.useEffect(()=>{var e;null===(e=C.current)||void 0===e||e.focus()},[]),o.useEffect(()=>{const e=e=>{if(w.current){const t=C.current.getBoundingClientRect(),r=e.clientX-t.left,o=r/t.width,i=o<-.5||o>1.5;if(p.length>2&&i){return m(0),w.current=!1,void v(f)}const s=Math.min(1,Math.max(0,r/t.width)),l=[...u.current];l[f].offset=s,n(`linear-gradient(${h.current}deg, ${y(l)})`)}},t=()=>{w.current=!1};return window.addEventListener("mousemove",e),window.addEventListener("mouseup",t),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("mouseup",t)}},[f,u]),r("div",{children:[r("div",{style:{width:"calc(100% - 35px)",height:"60px",position:"relative"},children:[t("div",{ref:C,tabIndex:0,style:{position:"absolute",top:0,left:0,width:"100%",marginLeft:"15px",height:"100%",zIndex:0,outline:"none"},onKeyDownCapture:e=>{if("Backspace"===e.key){if(e.preventDefault(),e.stopPropagation(),u.current.length<=2){return}u.current.splice(f,1),f>=u.current.length&&m(u.current.length-1),n(`linear-gradient(${h.current}deg, ${y(u.current)})`)}},onMouseDown:e=>{const t=e.currentTarget.getBoundingClientRect(),r=e.clientX-t.left,o=Math.min(1,Math.max(0,r/t.width)),i=p[0].color;p.push({color:i,offset:o}),p.sort((e,t)=>e.offset-t.offset);const s=p.findIndex(e=>e.offset===o);m(s),k(),n(`linear-gradient(${c}deg, ${y(p)})`)}}),p.map(({offset:e,color:r},o)=>t("div",{style:{position:"absolute",top:"10px",left:100*e+"%",border:o===f?"2px solid rgb(0, 161, 255)":"2px solid rgb(0, 0, 0, 0)",padding:"2px"},onMouseDown:e=>{e.stopPropagation(),m(o),k()},children:t(E,{background:r,style:{margin:0}})},o))]}),t("div",{style:{width:"calc(100% - 30px)",height:"10px",marginLeft:"15px",background:`linear-gradient(90deg,${y(p)})`}}),r("div",{style:{padding:"10px"},children:[r("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",paddingTop:"5px",paddingBottom:"5px"},children:[t("div",{children:d("toolbar.colorPicker.angle")}),t("div",{children:t(l,{value:c,onValueChange:e=>{const t=Math.min(360,Math.max(0,e));n(`linear-gradient(${t}deg,${y(p)})`)},buttonPosition:"none",style:{width:"50px"},min:0,max:360})})]}),t(a,{min:0,max:360,value:c,showTrackFill:!1,labelRenderer:!1,onChange:e=>{n(`linear-gradient(${e}deg,${y(p)})`)}})]}),t(x,{color:p[f].color,presetColors:s,onChange:e=>{const t=[...p];t[f].color=e,n(`linear-gradient(${c}deg, ${y(t)})`)}})]})},k=c(({value:e,onChange:s,preset:l,store:a})=>{const[c,p]=o.useState(h(e)?"gradient":"solid");return t("div",{style:{padding:"5px"},children:r(i,{id:"TabsExample",selectedTabId:c,onChange:e=>p(e),renderActiveTabPanelOnly:!0,children:[t(n,{id:"solid",title:d("toolbar.colorPicker.solid"),panel:t(x,{color:e,onChange:s,presetColors:l,style:{padding:"0"}})}),t(n,{id:"gradient",title:d("toolbar.colorPicker.linear"),panel:t(w,{value:e,onChange:C(e,s),store:a,preset:l}),panelClassName:"ember-panel"})]})})}),E=r=>{var{size:o=30,background:n,style:i={},children:s=null}=r,l=e(r,["size","background","style","children"]);return t("div",Object.assign({role:"button",tabIndex:0,"aria-label":"Pick color",onMouseDown:e=>e.preventDefault(),style:Object.assign({width:o+"px",height:o+"px",background:"url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='15' height='15' viewBox='0 0 8 8'%3E%3Cg fill='rgba(112, 112, 116, 1)' fill-opacity='1'%3E%3Cpath fill-rule='evenodd' d='M0 0h4v4H0V0zm4 4h4v4H4V4z'/%3E%3C/g%3E%3C/svg%3E\")",borderRadius:"2px",boxShadow:"0 0 2px grey",marginBottom:"-4px"},i)},l,{children:t("div",{style:{width:o+"px",height:o+"px",background:n},children:s})}))};let O=e=>{const t=getUsedColors(e).slice(0,8).filter(e=>!DEFAULT_COLORS.find(t=>b(t,e)));return t.concat(DEFAULT_COLORS.slice(0,16-t.length).reverse())};export function setColorsPresetFunc(e){O=e}export const ColorPicker=({value:e,onChange:r,size:o,store:n,gradientEnabled:i,children:l,style:a,position:c="auto",onOpen:d,onClose:p})=>{const u=O(n);return t(s,{autoFocus:!1,enforceFocus:!0,interactionKind:"click",hasBackdrop:!0,position:c,onOpening:d,onClosed:p,content:i?t(k,{preset:u,value:e,onChange:r,store:n}):t("div",{style:{padding:"5px"},children:t(x,{color:e,onChange:r,presetColors:u})}),children:t(E,{size:o,background:e,style:a,children:l})})};export default ColorPicker;
@@ -1 +1 @@
1
- import{isAlive as e}from"mobx-state-tree";import{applyFiltersToCanvas as t,getFilterAttrs as r}from"@polotno/core/filters";import{hasPixelFilters as i}from"@polotno/core/pixel-filters";export{getEffectiveBlurRadius,getElementBlurRadius}from"@polotno/core/filters";export function getFilterAttrs(e,t={enabled:e.blurEnabled,radius:e.blurRadius}){return r(e,e.filters.entries(),t)}export function hasActiveFilters(e){return i(e,e.filters.keys())}export function applyFiltersToCanvas(r,i){e(i)&&t(r,i,i.filters.entries())}export function getFiltersDepsKey(t){if(!e(t)){return""}const r=[];return t.filters.forEach((e,t)=>{r.push(t+":"+e.intensity)}),[t.brightnessEnabled&&t.brightness,t.blurEnabled&&t.blurRadius,t.sepiaEnabled,t.grayscaleEnabled,...r].join("|")}export function applyFilter(t,r){var i;if(!t||!e(r)){return}const s={enabled:r.a.blurEnabled,radius:r.a.blurRadius},l=getFilterAttrs(r,s),n={};s.enabled&&"text"===r.type&&r.lineHeight<1&&(n.offset=r.fontSize),t.setAttrs(l),l.filters.length?t.cache(Object.assign(Object.assign({},n),{pixelRatio:r.store._elementsPixelRatio})):t.clearCache(),null===(i=t.getLayer())||void 0===i||i.batchDraw()}
1
+ import{isAlive as e}from"mobx-state-tree";import{applyFiltersToCanvas as t,getFilterAttrs as r,textFilterCacheOffset as i}from"@polotno/core/filters";import{hasPixelFilters as l}from"@polotno/core/pixel-filters";export{getEffectiveBlurRadius,getElementBlurRadius}from"@polotno/core/filters";export function getFilterAttrs(e,t={enabled:e.blurEnabled,radius:e.blurRadius}){return r(e,e.filters.entries(),t)}export function hasActiveFilters(e){return l(e,e.filters.keys())}export function applyFiltersToCanvas(r,i){e(i)&&t(r,i,i.filters.entries())}export function getFiltersDepsKey(t){if(!e(t)){return""}const r=[];return t.filters.forEach((e,t)=>{r.push(t+":"+e.intensity)}),[t.brightnessEnabled&&t.brightness,t.blurEnabled&&t.blurRadius,t.sepiaEnabled,t.grayscaleEnabled,...r].join("|")}export function applyFilter(t,r){var l;if(!t||!e(r)){return}const s={enabled:r.a.blurEnabled,radius:r.a.blurRadius},n=getFilterAttrs(r,s),a="text"===r.type?r:null,o=a?i({blurEnabled:s.enabled,blurRadius:s.radius,lineHeight:a.lineHeight,fontSize:a.fontSize}):0;t.setAttrs(n),n.filters.length?t.cache({offset:o,pixelRatio:r.store._elementsPixelRatio}):t.clearCache(),null===(l=t.getLayer())||void 0===l||l.batchDraw()}
@@ -1,4 +1,4 @@
1
- export { BAND_ROWS, drawReduced, forceDownscaleEngine, loadDownscaleKernel, planBands, setDownscaleBandRows, resampleCanvas, shouldResample, sourceSize, RESAMPLE_PIXEL_BUDGET, type AxisFilter, type DownscaleEngine, type SourceRegion, } from '@polotno/core/downscale';
1
+ export { BAND_ROWS, drawImageWindow, drawReduced, forceDownscaleEngine, loadDownscaleKernel, planBands, setDownscaleBandRows, resampleCanvas, shouldResample, sourceSize, RESAMPLE_PIXEL_BUDGET, type AxisFilter, type DownscaleEngine, type SourceRegion, } from '@polotno/core/downscale';
2
2
  export declare function exportKernelEnabled(page: {
3
3
  _exporting?: boolean;
4
4
  _rendering?: boolean;
@@ -1 +1 @@
1
- import{flags as e}from"../utils/flags.js";export{BAND_ROWS,drawReduced,forceDownscaleEngine,loadDownscaleKernel,planBands,setDownscaleBandRows,resampleCanvas,shouldResample,sourceSize,RESAMPLE_PIXEL_BUDGET}from"@polotno/core/downscale";export function exportKernelEnabled(n){return Boolean(null==n?void 0:n._exporting)&&!(null==n?void 0:n._rendering)&&e.imageDownScalingEnabled}
1
+ import{flags as e}from"../utils/flags.js";export{BAND_ROWS,drawImageWindow,drawReduced,forceDownscaleEngine,loadDownscaleKernel,planBands,setDownscaleBandRows,resampleCanvas,shouldResample,sourceSize,RESAMPLE_PIXEL_BUDGET}from"@polotno/core/downscale";export function exportKernelEnabled(n){return Boolean(null==n?void 0:n._exporting)&&!(null==n?void 0:n._rendering)&&e.imageDownScalingEnabled}
package/canvas/element.js CHANGED
@@ -1 +1 @@
1
- import{Fragment as e,jsx as t,jsxs as o}from"react/jsx-runtime";import{createElement as r}from"react";import n from"react";import{observer as i}from"mobx-react-lite";import{ROLES as l}from"../model/store.js";import s from"konva";import{MAIN_TRANSFORMER_NAME as a}from"../utils/px.js";import{TextElement as m}from"./text-element.js";import{ImageElement as d}from"./image-element.js";import{HTMLElement as c}from"./html-element.js";import{RenderTagElement as f}from"./render-tag-element.js";import{LineElement as u}from"./line-element.js";import{VideoElement as p}from"./video-element.js";import{FigureElement as v}from"./figure-element.js";import{GifElement as g}from"./gif-element.js";import{LottieElement as h}from"./lottie-element.js";import{TableElement as x}from"./table-element.js";import{Group as y}from"react-konva";import{Highlighter as b}from"./highlighter.js";import{PlacedNode as j}from"./placed-node.js";import{forEveryChild as w}from"../model/group-model.js";import{flags as E}from"../utils/flags.js";import{blendModeOf as T,isIsolatedGroup as M}from"@polotno/core/page";const R={text:m,image:d,svg:d,line:u,video:p,figure:v,group:i(e=>{var o;const{element:n,store:i}=e,s=n,{children:a}=s,m=n.selectable||i.role===l.ADMIN,d=s.a;return t(j,{as:y,element:s,offsetX:d.offsetX,offsetY:d.offsetY,scaleX:d.scaleX,scaleY:d.scaleY,opacity:d.opacity,isolated:M(s),globalCompositeOperation:null!==(o=T(s))&&void 0!==o?o:"source-over",listening:m,hideInExport:!n.showInExport,id:n.id,clipFunc:d.wipe&&s._restingBbox?C(s._restingBbox,d.wipe):void 0,children:a.map(t=>r(X,Object.assign({},e,{key:t.id,store:i,element:t})))})}),gif:g,lottie:h,table:x};export function registerShapeComponent(e,t){R[e]=t}const S=(e,t)=>{const o=[];w(e,e=>{"group"!==e.type&&o.push(e.a)});const r=[];o.forEach(e=>{const t=[{x:0,y:0},{x:0+e.width,y:0},{x:0+e.width,y:0+e.height},{x:0,y:0+e.height}],o=new s.Transform;o.translate(e.x,e.y),o.rotate(s.Util.degToRad(e.rotation)),t.forEach(e=>{const t=o.point(e);r.push(t)})});const n=new s.Transform;n.rotate(-s.Util.degToRad(t));let i=1/0,l=1/0,a=-1/0,m=-1/0;r.forEach(e=>{const t=n.point(e);i=Math.min(i,t.x),l=Math.min(l,t.y),a=Math.max(a,t.x),m=Math.max(m,t.y)}),n.invert();const d=n.point({x:i,y:l});return{x:d.x,y:d.y,width:a-i,height:m-l,rotation:t}},I=".polotnoElementHover",O=new WeakMap;const k=1e6,C=(e,t)=>o=>{var r;const n="x"===t.axis?e.width:e.height,i=t.from<=0?-k:t.from*n,l=t.to>=1?k:t.to*n,[a,m,d,c]="x"===t.axis?[i,l,-k,k]:[-k,k,i,l];o.save(),o.translate(e.x,e.y),o.rotate(s.Util.degToRad(null!==(r=e.rotation)&&void 0!==r?r:0)),o.rect(a,d,m-a,c-d),o.restore()},U=(e,t)=>{const o=s.Util.degToRad(e.rotation);return(new s.Transform).translate(e.x,e.y).rotate(o).translate(e.width/2,0).scale(t,1).translate(-e.width/2,0).rotate(-o).translate(-e.x,-e.y).decompose()},X=i(r=>{var i,l,s;const{element:m,store:d}=r,[u,p]=n.useState(!1),v="group"===(null===(i=m.parent)||void 0===i?void 0:i.type),g="group"!==m.type||v?null:d.selectedElements,h=!!g&&1===g.length&&g[0]!==m&&g[0].top===m,[x,j]=n.useState(!1),w=n.useRef(null),M=null!==(s=m.filled.visible)&&void 0!==s?s:m.visible,k=()=>{var e,t;return null!==(t=null===(e=w.current)||void 0===e?void 0:e.getStage())&&void 0!==t?t:null},X=n.useRef(void 0),Y=()=>{var e;return X.current||(X.current=null===(e=k())||void 0===e?void 0:e.findOne(`.${a}`)),X.current};n.useEffect(()=>{const e=k();if(!e){return}const t=function(e,t,o,r){var n;const{subscribers:i}=function(e,t){const o=O.get(e);if(o){return o}const r={subscribers:new Map},n=e=>{var t,o;const n=r.hoveredId;r.hoveredId=e,n&&n!==e&&(null===(t=r.subscribers.get(n))||void 0===t||t.forEach(e=>e(!1))),e&&(null===(o=r.subscribers.get(e))||void 0===o||o.forEach(e=>e(!0)))};return e.on(`mouseover${I}`,e=>{var o,r;const i=e.target.findAncestor(".element",!0);n(null===(r=null===(o=t.getElementById(null==i?void 0:i.id()))||void 0===o?void 0:o.top)||void 0===r?void 0:r.id)}),e.on(`mouseleave${I}`,()=>n(void 0)),O.set(e,r),r}(e,t),l=null!==(n=i.get(o))&&void 0!==n?n:new Set;return i.set(o,l),l.add(r),()=>{l.delete(r),l.size||(i.delete(o),i.size||(e.off(I),O.delete(e)))}}(e,d,m.id,p);return()=>{t(),p(!1)}},[M,m.id]),n.useEffect(()=>{if(!h){return}const e=k(),t=Y();if(!e||!t){return}j(!1);const o=()=>j(!0),r=()=>j(!1);return e.on("dragstart",o),e.on("dragend",r),t.on("transformstart",o),t.on("transformend",r),()=>{e.off("dragstart",o),e.off("dragend",r),t.off("transformstart",o),t.off("transformend",r)}},[M,h]);let B=R[r.element.type];if("text"===r.element.type&&(E.renderTagTextEnabled?B=f:E.htmlRenderEnabled&&(B=c)),!M){return null}if(!B){return console.error("Can not find component for "+r.element.type),null}const F="group"===m.type?void 0:T(m),$="group"===m.type?null:m.a,z=null==$?void 0:$.wipe,A=null==$?void 0:$.flipScale;return o(e,{children:[t(y,Object.assign({name:"element-container",ref:w,visible:m.active,listening:m.active,isolated:!!F,opacity:F?m.a.opacity:1,globalCompositeOperation:null!=F?F:"source-over",clipFunc:z?C($,z):void 0},void 0===A?null:U($,A),{children:t(B,Object.assign({},r))})),((u||h)&&!v||m.isHighlighted)&&m.active&&!m.isSelectedDirectly&&!x&&t(b,{element:"group"===m.type?{a:S(m,h?"rotation"in g[0]?g[0].rotation:0:u&&(null===(l=Y())||void 0===l?void 0:l.rotation())||0)}:m})]})});export default X;
1
+ import{Fragment as e,jsx as t,jsxs as o}from"react/jsx-runtime";import{createElement as r}from"react";import n from"react";import{observer as i}from"mobx-react-lite";import{ROLES as l}from"../model/store.js";import s from"konva";import{MAIN_TRANSFORMER_NAME as a}from"../utils/px.js";import{TextElement as m}from"./text-element.js";import{ImageElement as d}from"./image-element.js";import{HTMLElement as c}from"./html-element.js";import{RenderTagElement as f}from"./render-tag-element.js";import{LineElement as u}from"./line-element.js";import{VideoElement as p}from"./video-element.js";import{FigureElement as v}from"./figure-element.js";import{GifElement as g}from"./gif-element.js";import{LottieElement as h}from"./lottie-element.js";import{TableElement as x}from"./table-element.js";import{Group as y}from"react-konva";import{Highlighter as b}from"./highlighter.js";import{PlacedNode as j}from"./placed-node.js";import{forEveryChild as w}from"../model/group-model.js";import{flags as E}from"../utils/flags.js";import{blendModeOf as T,isIsolatedGroup as M}from"@polotno/core/page";const R={text:m,image:d,svg:d,line:u,video:p,figure:v,group:i(e=>{var o;const{element:n,store:i}=e,s=n,{children:a}=s,m=n.selectable||i.role===l.ADMIN,d=s.a;return t(j,{as:y,element:s,offsetX:d.offsetX,offsetY:d.offsetY,scaleX:d.scaleX,scaleY:d.scaleY,opacity:d.opacity,isolated:M(s),globalCompositeOperation:null!==(o=T(s))&&void 0!==o?o:"source-over",listening:m,hideInExport:!n.showInExport,id:n.id,clipFunc:d.wipe&&s._restingBbox?C(s._restingBbox,d.wipe):void 0,children:a.map(t=>r(X,Object.assign({},e,{key:t.id,store:i,element:t})))})}),gif:g,lottie:h,table:x};export function registerShapeComponent(e,t){R[e]=t}const S=(e,t)=>{const o=[];w(e,e=>{"group"!==e.type&&o.push(e.a)});const r=[];o.forEach(e=>{const t=[{x:0,y:0},{x:0+e.width,y:0},{x:0+e.width,y:0+e.height},{x:0,y:0+e.height}],o=new s.Transform;o.translate(e.x,e.y),o.rotate(s.Util.degToRad(e.rotation)),t.forEach(e=>{const t=o.point(e);r.push(t)})});const n=new s.Transform;n.rotate(-s.Util.degToRad(t));let i=1/0,l=1/0,a=-1/0,m=-1/0;r.forEach(e=>{const t=n.point(e);i=Math.min(i,t.x),l=Math.min(l,t.y),a=Math.max(a,t.x),m=Math.max(m,t.y)}),n.invert();const d=n.point({x:i,y:l});return{x:d.x,y:d.y,width:a-i,height:m-l,rotation:t}},I=".polotnoElementHover",O=new WeakMap;const k=1e6,C=(e,t)=>o=>{var r;const n="x"===t.axis?e.width:e.height,i=t.from<=0?-k:t.from*n,l=t.to>=1?k:t.to*n,[a,m,d,c]="x"===t.axis?[i,l,-k,k]:[-k,k,i,l];o.save(),o.translate(e.x,e.y),o.rotate(s.Util.degToRad(null!==(r=e.rotation)&&void 0!==r?r:0)),o.rect(a,d,m-a,c-d),o.restore()},U=(e,t)=>{const o=s.Util.degToRad(e.rotation);return(new s.Transform).translate(e.x,e.y).rotate(o).translate(e.width/2,0).scale(t,1).translate(-e.width/2,0).rotate(-o).translate(-e.x,-e.y).decompose()},X=i(r=>{var i,l,s;const{element:m,store:d}=r,[u,p]=n.useState(!1),v="group"===(null===(i=m.parent)||void 0===i?void 0:i.type),g="group"!==m.type||v?null:d.selectedElements,h=!!g&&1===g.length&&g[0]!==m&&g[0].top===m,[x,j]=n.useState(!1),w=n.useRef(null),M=null!==(s=m.filled.visible)&&void 0!==s?s:m.visible,k=M||((e,t)=>{var o;let r=e;for(;r;){if(t.selectedElements.includes(r)){return!0}r="group"===(null===(o=r.parent)||void 0===o?void 0:o.type)?r.parent:null}return!1})(m,d),X=()=>{var e,t;return null!==(t=null===(e=w.current)||void 0===e?void 0:e.getStage())&&void 0!==t?t:null},Y=n.useRef(void 0),B=()=>{var e;return Y.current||(Y.current=null===(e=X())||void 0===e?void 0:e.findOne(`.${a}`)),Y.current};n.useEffect(()=>{const e=X();if(!e){return}const t=function(e,t,o,r){var n;const{subscribers:i}=function(e,t){const o=O.get(e);if(o){return o}const r={subscribers:new Map},n=e=>{var t,o;const n=r.hoveredId;r.hoveredId=e,n&&n!==e&&(null===(t=r.subscribers.get(n))||void 0===t||t.forEach(e=>e(!1))),e&&(null===(o=r.subscribers.get(e))||void 0===o||o.forEach(e=>e(!0)))};return e.on(`mouseover${I}`,e=>{var o,r;const i=e.target.findAncestor(".element",!0);n(null===(r=null===(o=t.getElementById(null==i?void 0:i.id()))||void 0===o?void 0:o.top)||void 0===r?void 0:r.id)}),e.on(`mouseleave${I}`,()=>n(void 0)),O.set(e,r),r}(e,t),l=null!==(n=i.get(o))&&void 0!==n?n:new Set;return i.set(o,l),l.add(r),()=>{l.delete(r),l.size||(i.delete(o),i.size||(e.off(I),O.delete(e)))}}(e,d,m.id,p);return()=>{t(),p(!1)}},[k,m.id]),n.useEffect(()=>{if(!h){return}const e=X(),t=B();if(!e||!t){return}j(!1);const o=()=>j(!0),r=()=>j(!1);return e.on("dragstart",o),e.on("dragend",r),t.on("transformstart",o),t.on("transformend",r),()=>{e.off("dragstart",o),e.off("dragend",r),t.off("transformstart",o),t.off("transformend",r)}},[k,h]);let F=R[r.element.type];if("text"===r.element.type&&(E.renderTagTextEnabled?F=f:E.htmlRenderEnabled&&(F=c)),!k){return null}if(!F){return console.error("Can not find component for "+r.element.type),null}const $="group"===m.type?void 0:T(m),z="group"===m.type?null:m.a,A=null==z?void 0:z.wipe,D=null==z?void 0:z.flipScale;return o(e,{children:[t(y,Object.assign({name:"element-container",ref:w,visible:m.active&&M,listening:m.active&&M,isolated:!!$,opacity:$?m.a.opacity:1,globalCompositeOperation:null!=$?$:"source-over",clipFunc:A?C(z,A):void 0},void 0===D?null:U(z,D),{children:t(F,Object.assign({},r))})),((u||h)&&!v||m.isHighlighted)&&m.active&&!m.isSelectedDirectly&&!x&&t(b,{element:"group"===m.type?{a:S(m,h?"rotation"in g[0]?g[0].rotation:0:u&&(null===(l=B())||void 0===l?void 0:l.rotation())||0)}:m})]})});export default X;
@@ -1,5 +1,10 @@
1
1
  import React from 'react';
2
2
  import { type StoreType } from '../model/store.js';
3
+ import { FadePoint } from '../utils/fade-gradient.js';
4
+ export declare const radialTrackEnd: ({ center, radius }: {
5
+ center: FadePoint;
6
+ radius: FadePoint;
7
+ }, width: number, height: number, gap: number) => FadePoint;
3
8
  export declare const FadeOverlay: (({ store, pageId }: {
4
9
  store: StoreType;
5
10
  pageId: string;
@@ -1 +1 @@
1
- import{Fragment as e,jsx as t,jsxs as n}from"react/jsx-runtime";import r from"react";import{observer as i}from"mobx-react-lite";import{Circle as s,Ellipse as a,Group as o,Line as l,Rect as c}from"react-konva";import{ROLES as d}from"../model/store.js";import{fadeStopColor as u,insertFadeStop as f}from"../utils/fade-gradient.js";import{moveStop as p,safe01 as g}from"../utils/stops.js";import{getFadeStopFocus as v,getSelectedFadeStop as m,setSelectedFadeStop as x}from"../utils/fade-overlay.js";import{useWorkspaceStyle as y}from"./workspace-style.js";import{t as b}from"../utils/l10n.js";import{readClipFade as h,writeClipFade as j}from"../utils/clip-fade.js";import{isAlive as S}from"mobx-state-tree";export const FadeOverlay=i(({store:i,pageId:M})=>{var O,k,E,w;const D=1===i.selectedElements.length?i.selectedElements[0]:null;let R=!!D;for(let e=D;e;e="group"===(null===(O=e.parent)||void 0===O?void 0:O.type)?e.parent:null){R=R&&(null!==(E=e.filled.visible)&&void 0!==E?E:e.visible)&&e.active}const F=!R||i.role===d.VIEWER||i.isPlaying||(null===(k=null==D?void 0:D.page)||void 0===k?void 0:k.id)!==M||!("clipSrc"in D)||"_cropModeEnabled"in D&&D._cropModeEnabled?null:h(D.clipSrc),P=!!F,A=null==F?void 0:F.fade,L=Math.min(m(i),(null!==(w=null==A?void 0:A.stops.length)&&void 0!==w?w:1)-1),T=e=>{D&&F&&S(D)&&D.set({clipSrc:j(e,F.baseSrc)})},I=e=>{var t,n;x(i,e);const r=v(i);(null==r?void 0:r.elementId)===(null==D?void 0:D.id)&&(null==r?void 0:r.onSelectStop(e))||null===(n=null===(t=z.current)||void 0===t?void 0:t.getStage())||void 0===n||n.container().focus({preventScroll:!0})},{transformerStyle:W}=y(),z=r.useRef(null),B=r.useRef(null),V=r.useRef(!1),X=r.useRef(!1),Y=r.useRef(null);if(r.useLayoutEffect(()=>{const e=z.current,t=null==e?void 0:e.getLayer();if(!e||!t||!D){return}const n=D.id,r=()=>{var t;const r=null===(t=e.getStage())||void 0===t?void 0:t.findOne("#"+n),i=e.getParent(),s=!!(null==r?void 0:r.isVisible());if(e.visible(s),!r||!i||!s){return}const a=i.getAbsoluteTransform().copy().invert();a.multiply(r.getAbsoluteTransform()),e.setAttrs(a.decompose())};return r(),t.on("beforeDraw",r),()=>{t.off("beforeDraw",r)}}),r.useLayoutEffect(()=>{var e,t,n;V.current||!P&&!X.current||(X.current=P,B.current=null!==(n=null===(e=z.current)||void 0===e?void 0:e.getLayer())&&void 0!==n?n:B.current,null===(t=B.current)||void 0===t||t.drawHit())}),r.useEffect(()=>{var e,t;if(!P){return}const n=null===(t=null===(e=z.current)||void 0===e?void 0:e.getStage())||void 0===t?void 0:t.container();if(!n){return}const r=n.getAttribute("tabindex");n.tabIndex=-1;const s=e=>{if(document.activeElement!==n||!["Delete","Backspace"].includes(e.key)){return}if(e.preventDefault(),e.stopPropagation(),!D||!S(D)||!("clipSrc"in D)){return}const t=h(D.clipSrc);if(!t||t.fade.stops.length<=2){return}const r=m(i);i.history.transaction(()=>D.set({clipSrc:j(Object.assign(Object.assign({},t.fade),{stops:t.fade.stops.filter((e,t)=>t!==r)}),t.baseSrc)})),x(i,Math.max(0,r-1))};return n.addEventListener("keydown",s),()=>{n.removeEventListener("keydown",s),null===r?n.removeAttribute("tabindex"):n.setAttribute("tabindex",r),x(i,0),n.style.cursor="",n.title="",V.current&&(V.current=!1,i.history.endTransaction())}},[i,null==D?void 0:D.id,P]),!A||!D){return null}const{width:_,height:H}=D.a;if(_<=0||H<=0){return null}const q=i.scale||1,C=Math.min(12/q,_/10,H/10),G=Math.min(5/q,C/2),J=Math.min(24/q,2*C),K=e=>({x:e.x*_,y:e.y*H}),N=e=>({x:Math.max(C/_,Math.min(1-C/_,e.x)),y:Math.max(C/H,Math.min(1-C/H,e.y))}),Q=e=>N({x:e.x()/_,y:e.y()/H}),U=e=>{var t;const n=((null===(t=z.current)||void 0===t?void 0:t.getAbsoluteRotation())||0)+("y"===e?90:0);return["ew-resize","nwse-resize","ns-resize","nesw-resize"][(Math.round(n/45)%4+4)%4]},Z=e=>"function"==typeof e?e():e,$=(e,t)=>({onMouseEnter:n=>{const r=n.target.getStage().container();r.style.cursor=Z(e),r.title=t},onMouseLeave:e=>{if(V.current){return}const t=e.target.getStage().container();t.style.cursor="",t.title=""}}),ee=()=>{var e;const t=null===(e=z.current)||void 0===e?void 0:e.getRelativePointerPosition();"radial"===A.shape&&t&&(Y.current={position:t,radius:A.radius})},te=(e,t,n,r="grab")=>Object.assign(Object.assign({draggable:!0,dragDistance:0,stroke:n===L?String(W.anchorStroke):void 0!==n&&A.stops[n].alpha>.5?"#fff":"#222",strokeWidth:2,strokeScaleEnabled:!1,hitStrokeWidth:Math.max(0,J-2*G)},$(r,e)),{onMouseDown:e=>{e.cancelBubble=!0,ee(),void 0!==n&&(e.evt.preventDefault(),I(n))},onTouchStart:e=>{e.cancelBubble=!0,ee(),void 0!==n&&(e.evt.preventDefault(),I(n))},onDragStart:e=>{e.target.getStage().container().style.cursor="grab"===r?"grabbing":Z(r),V.current=!0,i.history.startTransaction()},onDragMove:e=>t(e.target),onDragEnd:e=>{e.target.getStage().container().style.cursor=Z(r),V.current=!1,Y.current=null,i.history.endTransaction()}}),ne=e=>({fill:u(A.stops[e].alpha)}),re=[{stroke:"#222"},{stroke:"#fff",dash:[4,4]}],ie=K("linear"===A.shape?A.start:A.center),se=K("linear"===A.shape?A.end:{x:A.center.x-A.radius.x,y:A.center.y}),ae=se.x-ie.x,oe=se.y-ie.y,le=e=>({x:ie.x+ae*e,y:ie.y+oe*e}),ce=e=>g(((e.x-ie.x)*ae+(e.y-ie.y)*oe)/(ae*ae+oe*oe||1)),de=e=>{var t,n;e.cancelBubble=!0;const r=e.target.getRelativePointerPosition();if(!r){return}const s=f(A.stops,ce(r));null===(n=null===(t=z.current)||void 0===t?void 0:t.getStage())||void 0===n||n.container().focus({preventScroll:!0}),x(i,s.index),i.history.transaction(()=>T(Object.assign(Object.assign({},A),{stops:s.stops})))},ue=n(e,{children:[re.map((e,n)=>t(l,Object.assign({},e,{listening:1===n,strokeWidth:1.5,name:"fade-axis-guide"},$("copy",b("toolbar.clipFade.addStop")),{points:[ie.x,ie.y,se.x,se.y],strokeScaleEnabled:!1,hitStrokeWidth:10,onMouseDown:de,onTouchStart:de}),n)),A.stops.map((e,n)=>0===e.offset||"linear"===A.shape&&1===e.offset?null:t(s,Object.assign({name:"fade-stop-anchor"},K(N({x:le(e.offset).x/_,y:le(e.offset).y/H})),{radius:G},ne(n),te(b("toolbar.clipFade.stopOpacity"),t=>((e,t)=>{const n=A.stops.findIndex(t=>t.id===e),r=p(A.stops,n,ce(t.position()));t.position(le(r.stops[r.index].offset)),x(i,r.index),T(Object.assign(Object.assign({},A),{stops:r.stops}))})(e.id,t),n)),e.id))]});let fe;if("linear"===A.shape){const r=(e,t)=>{const n=Q(t),r=K("start"===e?A.end:A.start);Math.hypot(n.x*_-r.x,n.y*H-r.y)<1/q?t.position(K(A[e])):(t.position(K(n)),T(Object.assign(Object.assign({},A),{[e]:n})))};fe=n(e,{children:[ue,t(s,Object.assign({name:"fade-axis-anchor fade-start"},K(N(A.start)),{radius:G},ne(0),te(b("toolbar.clipFade.start"),e=>r("start",e),0))),t(s,Object.assign({name:"fade-axis-anchor fade-end"},K(N(A.end)),{radius:G},ne(A.stops.length-1),te(b("toolbar.clipFade.end"),e=>r("end",e),A.stops.length-1)))]})}else{const r=K(A.center),i=A.radius.x*_,o=A.radius.y*H,l=e=>{const t=Q(e),n={x:Math.max(Math.min(.5,A.radius.x),Math.min(Math.max(.5,1-A.radius.x),t.x)),y:Math.max(Math.min(.5,A.radius.y),Math.min(Math.max(.5,1-A.radius.y),t.y))};e.position(K(n)),T(Object.assign(Object.assign({},A),{center:n}))},d=(e,t)=>{var n;const r="x"===e?_:H,i=Y.current;if(!i){return}const s=null===(n=z.current)||void 0===n?void 0:n.getRelativePointerPosition();if(!s){return}const a=s[e],o=Math.max(.001,Math.min(A.center[e],1-A.center[e])),l=Math.min(o,Math.max(.001,i.radius[e]+(a-i.position[e])/r*("x"===e?1:-1)));t.position(K(N("x"===e?{x:A.center.x+l,y:A.center.y}:{x:A.center.x,y:A.center.y-l}))),T(Object.assign(Object.assign({},A),{radius:Object.assign(Object.assign({},A.radius),{[e]:l})}))};fe=n(e,{children:[re.map((e,n)=>t(a,Object.assign({},e,{strokeWidth:1.5,x:r.x,y:r.y,radiusX:i,radiusY:o,strokeScaleEnabled:!1,listening:!1}),n)),ue,t(c,Object.assign({name:"fade-center"},K(N(A.center)),{offsetX:G,offsetY:G,width:2*G,height:2*G},ne(0),te(b("toolbar.clipFade.center"),l,0,"move"))),t(s,Object.assign({name:"fade-radius-x"},K(N({x:(r.x+i)/_,y:A.center.y})),{radius:G,fill:"#fff"},te(b("toolbar.clipFade.radius"),e=>d("x",e),void 0,()=>U("x")))),t(s,Object.assign({name:"fade-radius-y"},K(N({x:A.center.x,y:(r.y-o)/H})),{radius:G,fill:"#fff"},te(b("toolbar.clipFade.radius"),e=>d("y",e),void 0,()=>U("y"))))]})}return t(o,{ref:z,name:"fade-overlay",hideInExport:!0,children:fe})});
1
+ import{Fragment as t,jsx as e,jsxs as n}from"react/jsx-runtime";import r from"react";import{observer as a}from"mobx-react-lite";import{Circle as s,Ellipse as o,Group as i,Line as l,Rect as c}from"react-konva";import{ROLES as d}from"../model/store.js";import{fadeStopColor as u,insertFadeStop as f}from"../utils/fade-gradient.js";import{moveStop as p,safe01 as g}from"../utils/stops.js";import{getFadeStopFocus as v,getSelectedFadeStop as h,setSelectedFadeStop as y}from"../utils/fade-overlay.js";import{useWorkspaceStyle as m}from"./workspace-style.js";import{t as x}from"../utils/l10n.js";import{readClipFade as b,writeClipFade as M}from"../utils/clip-fade.js";import{isAlive as j}from"mobx-state-tree";const S=t=>({x:t.x<=.5?1:-1,y:t.y>=.5?-1:1});export const radialTrackEnd=({center:t,radius:e},n,r,a)=>{const s=Math.max(e.x*n,1e-6),o=Math.max(e.y*r,1e-6),[i,l]=.5===t.x&&.5===t.y?[-1,0]:[(.5-t.x)*n/s,(.5-t.y)*r/o],c=Math.hypot(i,l);let[d,u]=[i/c,l/c];const f=S(t),p=(t,e)=>{const n=Math.min(a/e,Math.PI/4),r=Math.atan2(u,d),s=Math.atan2(Math.sin(r-t),Math.cos(r-t));if(Math.abs(s)>=n){return}const o=t+(s<0?-n:n);[d,u]=[Math.cos(o),Math.sin(o)]};return p(f.x>0?0:Math.PI,o),p(f.y*Math.PI/2,s),{x:t.x*n+s*d,y:t.y*r+o*u}};export const FadeOverlay=a(({store:a,pageId:O})=>{var k,E,w,D;const P=1===a.selectedElements.length?a.selectedElements[0]:null;let R=!!P;for(let t=P;t;t="group"===(null===(k=t.parent)||void 0===k?void 0:k.type)?t.parent:null){R=R&&(null!==(w=t.filled.visible)&&void 0!==w?w:t.visible)&&t.active}const F=!R||a.role===d.VIEWER||a.isPlaying||(null===(E=null==P?void 0:P.page)||void 0===E?void 0:E.id)!==O||!("clipSrc"in P)||"_cropModeEnabled"in P&&P._cropModeEnabled?null:b(P.clipSrc),I=!!F,T=null==F?void 0:F.fade,A=Math.min(h(a),(null!==(D=null==T?void 0:T.stops.length)&&void 0!==D?D:1)-1),L=t=>{P&&F&&j(P)&&P.set({clipSrc:M(t,F.baseSrc)})},W=t=>{var e,n;y(a,t);const r=v(a);(null==r?void 0:r.elementId)===(null==P?void 0:P.id)&&(null==r?void 0:r.onSelectStop(t))||null===(n=null===(e=B.current)||void 0===e?void 0:e.getStage())||void 0===n||n.container().focus({preventScroll:!0})},{transformerStyle:z}=m(),B=r.useRef(null),V=r.useRef(null),X=r.useRef(!1),Y=r.useRef(!1),_=r.useRef(null);if(r.useLayoutEffect(()=>{const t=B.current,e=null==t?void 0:t.getLayer();if(!t||!e||!P){return}const n=P.id,r=()=>{var e;const r=null===(e=t.getStage())||void 0===e?void 0:e.findOne("#"+n),a=t.getParent(),s=!!(null==r?void 0:r.isVisible());if(t.visible(s),!r||!a||!s){return}const o=a.getAbsoluteTransform().copy().invert();o.multiply(r.getAbsoluteTransform()),t.setAttrs(o.decompose())};return r(),e.on("beforeDraw",r),()=>{e.off("beforeDraw",r)}}),r.useLayoutEffect(()=>{var t,e,n;X.current||!I&&!Y.current||(Y.current=I,V.current=null!==(n=null===(t=B.current)||void 0===t?void 0:t.getLayer())&&void 0!==n?n:V.current,null===(e=V.current)||void 0===e||e.drawHit())}),r.useEffect(()=>{var t,e;if(!I){return}const n=null===(e=null===(t=B.current)||void 0===t?void 0:t.getStage())||void 0===e?void 0:e.container();if(!n){return}const r=n.getAttribute("tabindex");n.tabIndex=-1;const s=t=>{if(document.activeElement!==n||!["Delete","Backspace"].includes(t.key)){return}if(t.preventDefault(),t.stopPropagation(),!P||!j(P)||!("clipSrc"in P)){return}const e=b(P.clipSrc);if(!e||e.fade.stops.length<=2){return}const r=h(a);a.history.transaction(()=>P.set({clipSrc:M(Object.assign(Object.assign({},e.fade),{stops:e.fade.stops.filter((t,e)=>e!==r)}),e.baseSrc)})),y(a,Math.max(0,r-1))};return n.addEventListener("keydown",s),()=>{n.removeEventListener("keydown",s),null===r?n.removeAttribute("tabindex"):n.setAttribute("tabindex",r),y(a,0),n.style.cursor="",n.title="",X.current&&(X.current=!1,a.history.endTransaction())}},[a,null==P?void 0:P.id,I]),!T||!P){return null}const{width:H,height:q}=P.a;if(H<=0||q<=0){return null}const C=a.scale||1,G=Math.min(12/C,H/10,q/10),J=Math.min(5/C,G/2),K=Math.min(24/C,2*G),N=t=>({x:t.x*H,y:t.y*q}),Q=t=>({x:Math.max(G/H,Math.min(1-G/H,t.x)),y:Math.max(G/q,Math.min(1-G/q,t.y))}),U=t=>{var e;const n=((null===(e=B.current)||void 0===e?void 0:e.getAbsoluteRotation())||0)+("y"===t?90:0);return["ew-resize","nwse-resize","ns-resize","nesw-resize"][(Math.round(n/45)%4+4)%4]},Z=t=>"function"==typeof t?t():t,$=(t,e)=>({onMouseEnter:n=>{const r=n.target.getStage().container();r.style.cursor=Z(t),r.title=e},onMouseLeave:t=>{if(X.current){return}const e=t.target.getStage().container();e.style.cursor="",e.title=""}}),tt=()=>{var t;const e=null===(t=B.current)||void 0===t?void 0:t.getRelativePointerPosition();"radial"===T.shape&&e&&(_.current={position:e,radius:T.radius})},et=(t,e,n,r="grab")=>Object.assign(Object.assign({draggable:!0,dragDistance:0,stroke:n===A?String(z.anchorStroke):void 0!==n&&T.stops[n].alpha>.5?"#fff":"#222",strokeWidth:2,strokeScaleEnabled:!1,hitStrokeWidth:Math.max(0,K-2*J)},$(r,t)),{onMouseDown:t=>{t.cancelBubble=!0,tt(),void 0!==n&&(t.evt.preventDefault(),W(n))},onTouchStart:t=>{t.cancelBubble=!0,tt(),void 0!==n&&(t.evt.preventDefault(),W(n))},onDragStart:t=>{t.target.getStage().container().style.cursor="grab"===r?"grabbing":Z(r),X.current=!0,a.history.startTransaction()},onDragMove:t=>e(t.target),onDragEnd:t=>{t.target.getStage().container().style.cursor=Z(r),X.current=!1,_.current=null,a.history.endTransaction()}}),nt=t=>({fill:u(T.stops[t].alpha)}),rt=[{stroke:"#222"},{stroke:"#fff",dash:[4,4]}],at=N("linear"===T.shape?T.start:T.center),st="linear"===T.shape?N(T.end):radialTrackEnd(T,H,q,3*J),ot=st.x-at.x,it=st.y-at.y,lt=t=>({x:at.x+ot*t,y:at.y+it*t}),ct=t=>g(((t.x-at.x)*ot+(t.y-at.y)*it)/(ot*ot+it*it||1)),dt=t=>{var e,n;t.cancelBubble=!0;const r=t.target.getRelativePointerPosition();if(!r){return}const s=f(T.stops,ct(r));null===(n=null===(e=B.current)||void 0===e?void 0:e.getStage())||void 0===n||n.container().focus({preventScroll:!0}),y(a,s.index),a.history.transaction(()=>L(Object.assign(Object.assign({},T),{stops:s.stops})))},ut=n(t,{children:[rt.map((t,n)=>e(l,Object.assign({},t,{listening:1===n,strokeWidth:1.5,name:"fade-axis-guide"},$("copy",x("toolbar.clipFade.addStop")),{points:[at.x,at.y,st.x,st.y],strokeScaleEnabled:!1,hitStrokeWidth:10,onMouseDown:dt,onTouchStart:dt}),n)),T.stops.map((t,n)=>0===t.offset||"linear"===T.shape&&1===t.offset?null:e(s,Object.assign({name:"fade-stop-anchor"},(t=>{const e=lt(t);return"linear"===T.shape?N(Q({x:e.x/H,y:e.y/q})):e})(t.offset),{radius:J},nt(n),et(x("toolbar.clipFade.stopOpacity"),e=>((t,e)=>{const n=T.stops.findIndex(e=>e.id===t),r=p(T.stops,n,ct(e.position()));e.position(lt(r.stops[r.index].offset)),y(a,r.index),L(Object.assign(Object.assign({},T),{stops:r.stops}))})(t.id,e),n)),t.id))]});let ft;if("linear"===T.shape){const r=(t,e)=>{const n=(t=>Q({x:t.x()/H,y:t.y()/q}))(e),r=N("start"===t?T.end:T.start);Math.hypot(n.x*H-r.x,n.y*q-r.y)<1/C?e.position(N(T[t])):(e.position(N(n)),L(Object.assign(Object.assign({},T),{[t]:n})))};ft=n(t,{children:[ut,e(s,Object.assign({name:"fade-axis-anchor fade-start"},N(Q(T.start)),{radius:J},nt(0),et(x("toolbar.clipFade.start"),t=>r("start",t),0))),e(s,Object.assign({name:"fade-axis-anchor fade-end"},N(Q(T.end)),{radius:J},nt(T.stops.length-1),et(x("toolbar.clipFade.end"),t=>r("end",t),T.stops.length-1)))]})}else{const r=N(T.center),a=T.radius.x*H,i=T.radius.y*q,l=S(T.center),d=(t,e)=>{const n=6/C/e,r=[0,.5,1].find(e=>Math.abs(t-e)<=n);return Math.max(-1,Math.min(2,null!=r?r:t))},u=t=>{const e={x:d(t.x()/H,H),y:d(t.y()/q,q)};t.position(N(e)),L(Object.assign(Object.assign({},T),{center:e}))},f=(t,e)=>N("x"===t?{x:T.center.x+l.x*e,y:T.center.y}:{x:T.center.x,y:T.center.y+l.y*e}),p=(t,e)=>{var n;const r="x"===t?H:q,a=_.current;if(!a){return}const s=null===(n=B.current)||void 0===n?void 0:n.getRelativePointerPosition();if(!s){return}const o=T.center[t],i=Math.max(a.radius[t],l[t]>0?2-o:o- -1),c=Math.min(i,Math.max(.001,a.radius[t]+(s[t]-a.position[t])/r*l[t]));e.position(f(t,c)),L(Object.assign(Object.assign({},T),{radius:Object.assign(Object.assign({},T.radius),{[t]:c})}))};ft=n(t,{children:[rt.map((t,n)=>e(o,Object.assign({},t,{strokeWidth:1.5,x:r.x,y:r.y,radiusX:a,radiusY:i,strokeScaleEnabled:!1,listening:!1}),n)),ut,e(c,Object.assign({name:"fade-center"},r,{offsetX:J,offsetY:J,width:2*J,height:2*J},nt(0),et(x("toolbar.clipFade.center"),u,0,"move"))),e(s,Object.assign({name:"fade-radius-x"},f("x",T.radius.x),{radius:J,fill:"#fff"},et(x("toolbar.clipFade.radius"),t=>p("x",t),void 0,()=>U("x")))),e(s,Object.assign({name:"fade-radius-y"},f("y",T.radius.y),{radius:J,fill:"#fff"},et(x("toolbar.clipFade.radius"),t=>p("y",t),void 0,()=>U("y"))))]})}return e(i,{ref:B,name:"fade-overlay",hideInExport:!0,children:ft})});
package/canvas/hotkeys.js CHANGED
@@ -1 +1 @@
1
- import{ROLES as e}from"../model/store.js";import{copyToEvent as t,cutToEvent as o,escapeText as l,pasteFromEvent as n,insertTextElement as r}from"../utils/clipboard.js";import{duplicateElements as i}from"../utils/duplicate.js";import{alignBottom as c,alignCenter as a,alignLeft as s,alignMiddle as d,alignRight as u,alignTop as f}from"../utils/alignment.js";import{richTextToPlainText as p}from"@polotno/core/text";const v="rgba(191, 191, 191, 100)";function y(e){if(1!==e.selectedElements.length){return null}const t=e.selectedElements[0];return"table"!==t.type?null:t}function h(e,t,o){const l=e.visibleCellAt(t,o);l&&e.focusCell(l.id)}function m(e,t,o){const l=e.rows*e.cols,n=t.row*e.cols+t.col;for(let r=1;r<=l;r++){const t=((n+o*r)%l+l)%l,i=e.getCell(Math.floor(t/e.cols),t%e.cols);if(i&&!i.mergedInto){return i}}}function g(e=document.querySelector(".polotno-workspace-container")){var t;const o=document.activeElement,l=null==o?void 0:o.closest(".polotno-workspace-container"),n=!!l&&l!==e,r="INPUT"===(null==o?void 0:o.tagName)||"TEXTAREA"===(null==o?void 0:o.tagName)||!!(null==o?void 0:o.isContentEditable),i=!(!o||!(null==e?void 0:e.contains(o))),c=document.activeElement===e||i;return{workspaceContainer:e,otherWorkspace:n,activeIsEditable:r,activeIsInsideWorkspace:i,isWorkspaceActive:c,hasOutsideTextSelection:!c&&((null===(t=window.getSelection())||void 0===t?void 0:t.toString().length)||0)>0}}export function getWorkspaceEventScope(t,o){if(t.role===e.VIEWER){return{handle:!1,table:null}}const l=g(o);if(l.otherWorkspace||l.activeIsEditable||l.hasOutsideTextSelection){return{handle:!1,table:null}}const n=y(t);return{handle:!0,table:n&&n._focusedCellIds.length>0&&!n._editingCellId?n:null}}export function handleHotkey(t,o,l){var n,p,w,E,k,D,C,x,K,b,A;if(o.role===e.VIEWER){return}const R=g(l);if(R.otherWorkspace){return}const T=R.activeIsEditable&&!R.activeIsInsideWorkspace,S=y(o);if(!T&&S&&function(e,t,o){const l=t._focusedCellIds.length>0,n=!!t._editingCellId;if(!l&&!n){return!1}if(n){if("Escape"===e.key){return e.preventDefault(),t.exitCellEdit(),!0}if("Tab"===e.key){e.preventDefault();const o=t.editingCell;t.exitCellEdit();const l=o&&m(t,o,e.shiftKey?-1:1);return l&&t.enterCellEdit(l.id),!0}return!0}if("Escape"===e.key){return e.preventDefault(),t.clearCellFocus(),!0}const r=t.focusedCells[0],i=r&&t.visibleCellAt(r.row,r.col);if(!i){return!1}const{row:c,col:a}=i;if("Enter"===e.key){return e.preventDefault(),t.enterCellEdit(i.id),!0}if("Tab"===e.key){e.preventDefault();const o=m(t,i,e.shiftKey?-1:1);return o&&t.focusCell(o.id),!0}if("ArrowRight"===e.key||"ArrowLeft"===e.key||"ArrowDown"===e.key||"ArrowUp"===e.key){if(e.preventDefault(),e.shiftKey){const o=function(e){const t=e.cells.find(t=>t.id===e._anchorCellId);if(!t){return null}const o=e.focusedCells;if(o.length<=1){return{row:t.row,col:t.col}}let l=1/0,n=-1,r=1/0,i=-1;for(const c of o){c.row<l&&(l=c.row),c.row>n&&(n=c.row),c.col<r&&(r=c.col),c.col>i&&(i=c.col)}return{row:t.row===l?n:l,col:t.col===r?i:r}}(t)||{row:c,col:a};let l=o.row,n=o.col;"ArrowRight"===e.key&&(n=Math.min(o.col+1,t.cols-1)),"ArrowLeft"===e.key&&(n=Math.max(o.col-1,0)),"ArrowDown"===e.key&&(l=Math.min(o.row+1,t.rows-1)),"ArrowUp"===e.key&&(l=Math.max(o.row-1,0)),t.focusCellRange(l,n)}else{"ArrowRight"===e.key&&h(t,c,Math.min(a+i.colSpan,t.cols-1)),"ArrowLeft"===e.key&&h(t,c,Math.max(a-1,0)),"ArrowDown"===e.key&&h(t,Math.min(c+i.rowSpan,t.rows-1),a),"ArrowUp"===e.key&&h(t,Math.max(c-1,0),a)}return!0}const s=e.ctrlKey||e.metaKey;if(!(s||e.shiftKey||e.altKey||"Delete"!==e.key&&"Backspace"!==e.key)){return e.preventDefault(),t.focusedCells.forEach(e=>{e.set({text:""})}),!0}if(s&&e.shiftKey&&!e.altKey&&"Equal"===e.code){e.preventDefault();const l=t.getFocusedCellRange(),n=l?l.maxRow+1:c+1;return o.history.transaction(()=>{t.addRow(n)}),!0}if(s&&e.shiftKey&&e.altKey&&"Equal"===e.code){e.preventDefault();const l=t.getFocusedCellRange(),n=l?l.maxCol+1:a+1;return o.history.transaction(()=>{t.addColumn(n)}),!0}if(s&&!e.shiftKey&&"Backspace"===e.code){const l=t.getFocusedCellRange();if(l&&0===l.minCol&&l.maxCol===t.cols-1&&t.rows>1){return e.preventDefault(),o.history.transaction(()=>{t.removeRowRange(l.minRow,l.maxRow)}),!0}}if(s&&e.shiftKey&&"Backspace"===e.code){const l=t.getFocusedCellRange();if(l&&0===l.minRow&&l.maxRow===t.rows-1&&t.cols>1){return e.preventDefault(),o.history.transaction(()=>{t.removeColumnRange(l.minCol,l.maxCol)}),!0}}return s&&"KeyA"===e.code?(e.preventDefault(),t.cells.forEach(e=>{t.focusCell(e.id,!0)}),!0):!s||"z"!==e.key.toLowerCase()&&"y"!==e.key.toLowerCase()}(t,S,o)){return}if(R.activeIsEditable){return}if(R.hasOutsideTextSelection){return}if(o.activeTool&&"crop"!==o.activeTool.name&&"Escape"===t.key&&R.isWorkspaceActive){return t.preventDefault(),void o._closeImageTool("discard")}if("Alt"===t.key){const e=R.workspaceContainer;return void((R.isWorkspaceActive||o.distanceGuidesVisible||e&&e.matches(":hover"))&&t.preventDefault())}if("Escape"===t.key||"Enter"===t.key){const e=o._hasCroppedImages;if(e&&("Escape"===t.key||R.isWorkspaceActive)){return t.preventDefault(),void("Escape"===t.key?e.cancelCropMode():(null===(n=o.activeTool)||void 0===n?void 0:n.pending)||e.toggleCropMode(!1))}}const W=o.selectedElements.filter(e=>e.removable).map(e=>e.id),I=!!o.activeTool&&"crop"!==o.activeTool.name;46!==t.keyCode&&8!==t.keyCode||I||o.deleteElements(W);const M=t.ctrlKey||t.metaKey,L=t.shiftKey,P=t.altKey;if("Escape"===t.key){return t.preventDefault(),void("draw"===o.tool?o.setTool("selection"):o.selectElements([]))}if(M&&!L&&"z"===t.key.toLowerCase()){return t.preventDefault(),void o.history.undo()}if(M&&(L&&"z"===t.key.toLowerCase()||"y"===t.key.toLowerCase())){return t.preventDefault(),void o.history.redo()}if(M&&"KeyA"===t.code){t.preventDefault();const e=null===(p=o.activePage)||void 0===p?void 0:p.children.filter(e=>e.selectable),l=(null==e?void 0:e.map(e=>e.id))||[];o.selectElements(l)}if(M&&("Equal"===t.code||"Minus"===t.code)){return t.preventDefault(),void o.setScale(o.scale+("Equal"===t.code?.1:-.1))}if(!I&&!o.selectedShapes.some(e=>"pendingEdit"in e&&e.pendingEdit)){if("ArrowDown"===t.code){t.preventDefault();const e=t.shiftKey?10:1;o.selectedShapes.forEach(t=>{t.draggable&&t.set({y:t.y+e})})}if("ArrowUp"===t.code){t.preventDefault();const e=t.shiftKey?10:1;o.selectedShapes.forEach(t=>{t.draggable&&t.set({y:t.y-e})})}if("ArrowLeft"===t.code){t.preventDefault();const e=t.shiftKey?10:1;o.selectedShapes.forEach(t=>{t.draggable&&t.set({x:t.x-e})})}if("ArrowRight"===t.code){t.preventDefault();const e=t.shiftKey?10:1;o.selectedShapes.forEach(t=>{t.draggable&&t.set({x:t.x+e})})}if(M&&"KeyG"===t.code){t.preventDefault();const e=o.selectedElements[0];if(e&&"group"===e.type){const t=e;o.ungroupElements([t.id])}else{o.groupElements(o.selectedElements.map(e=>e.id))}}if(M&&"KeyD"===t.code&&(t.preventDefault(),i(o.selectedElements,o)),"KeyT"!==t.code||M||(t.preventDefault(),r(o,"Sample Text",{fontFamily:"Roboto"})),"KeyR"===t.code&&!M){t.preventDefault();const e={type:"figure",x:o.width/4,y:o.height/4,width:300,height:300,fill:v,stroke:"#0c0c0c",strokeWidth:0,subType:"rect"};null===(w=o.activePage)||void 0===w||w.addElement(e)}if("KeyL"===t.code&&!M){t.preventDefault();const e=(null!==(K=null===(E=o.activePage)||void 0===E?void 0:E.computedWidth)&&void 0!==K?K:0)/3,l={type:"line",x:(null!==(b=null===(k=o.activePage)||void 0===k?void 0:k.computedWidth)&&void 0!==b?b:0)/2-e/2,y:(null!==(A=null===(D=o.activePage)||void 0===D?void 0:D.computedHeight)&&void 0!==A?A:0)/2,width:e,color:v};null===(C=o.activePage)||void 0===C||C.addElement(l)}if("KeyO"===t.code&&!M){t.preventDefault(),t.preventDefault();const e={type:"figure",x:o.width/4,y:o.height/4,width:300,height:300,fill:v,stroke:"#0c0c0c",strokeWidth:0,subType:"circle"};null===(x=o.activePage)||void 0===x||x.addElement(e)}P&&("KeyA"===t.code&&(t.preventDefault(),s(o)),"KeyD"===t.code&&(t.preventDefault(),u(o)),"KeyS"===t.code&&(t.preventDefault(),c(o)),"KeyW"===t.code&&(t.preventDefault(),f(o)),"KeyV"===t.code&&(t.preventDefault(),d(o)),"KeyH"===t.code&&(t.preventDefault(),a(o))),"BracketRight"===t.code&&(t.preventDefault(),o.selectedElements.forEach(e=>M?e.moveUp():e.moveTop())),"BracketLeft"===t.code&&(t.preventDefault(),o.selectedElements.forEach(e=>M?e.moveDown():e.moveBottom()))}}function w(e,t){const o=e.focusedCells.map(e=>p(e.text)).join("\n");t.clipboardData.setData("text/plain",o||" "),t.preventDefault()}export function handleCopyEvent(e,o,l){const n=getWorkspaceEventScope(o,l);n.handle&&e.clipboardData&&(n.table?w(n.table,e):t(o,e))}export function handleCutEvent(e,t,l){const n=getWorkspaceEventScope(t,l);if(n.handle&&e.clipboardData){if(n.table){const o=n.table;return w(o,e),void t.history.transaction(()=>{o.focusedCells.forEach(e=>e.set({text:""}))})}o(t,e)}}export function handlePasteEvent(e,t,o){const r=getWorkspaceEventScope(t,o);if(!r.handle){return}const i=r.table;n(t,e,{onText:i?e=>{var t;return null===(t=i.focusedCells[0])||void 0===t?void 0:t.set({text:l(e)})}:void 0})}
1
+ import{ROLES as e}from"../model/store.js";import{copyToEvent as t,cutToEvent as o,escapeText as l,pasteFromEvent as n,insertTextElement as r}from"../utils/clipboard.js";import{duplicateElements as a}from"../utils/duplicate.js";import{alignBottom as c,alignCenter as i,alignLeft as s,alignMiddle as d,alignRight as u,alignTop as f}from"../utils/alignment.js";import{richTextToPlainText as p}from"@polotno/core/text";const v="rgba(191, 191, 191, 100)";function y(e){if(1!==e.selectedElements.length){return null}const t=e.selectedElements[0];return"table"!==t.type?null:t}function h(e,t,o){const l=e.visibleCellAt(t,o);l&&e.focusCell(l.id)}function m(e,t,o){const l=e.rows*e.cols,n=t.row*e.cols+t.col;for(let r=1;r<=l;r++){const t=((n+o*r)%l+l)%l,a=e.getCell(Math.floor(t/e.cols),t%e.cols);if(a&&!a.mergedInto){return a}}}function g(e=document.querySelector(".polotno-workspace-container")){var t;const o=document.activeElement,l=null==o?void 0:o.closest(".polotno-workspace-container"),n=!!l&&l!==e,r="INPUT"===(null==o?void 0:o.tagName)||"TEXTAREA"===(null==o?void 0:o.tagName)||!!(null==o?void 0:o.isContentEditable),a=!(!o||!(null==e?void 0:e.contains(o))),c=document.activeElement===e||a;return{workspaceContainer:e,otherWorkspace:n,activeIsEditable:r,activeIsInsideWorkspace:a,isWorkspaceActive:c,hasOutsideTextSelection:!c&&((null===(t=window.getSelection())||void 0===t?void 0:t.toString().length)||0)>0}}export function getWorkspaceEventScope(t,o){if(t.role===e.VIEWER){return{handle:!1,table:null}}const l=g(o);if(l.otherWorkspace||l.activeIsEditable||l.hasOutsideTextSelection){return{handle:!1,table:null}}const n=y(t);return{handle:!0,table:n&&n._focusedCellIds.length>0&&!n._editingCellId?n:null}}export function handleHotkey(t,o,l){var n,p,w,k,E,D,C,x,K,b,A;if(o.role===e.VIEWER){return}const R=g(l);if(R.otherWorkspace){return}const T=R.activeIsEditable&&!R.activeIsInsideWorkspace,S=y(o);if(!T&&S&&function(e,t,o){const l=t._focusedCellIds.length>0,n=!!t._editingCellId;if(!l&&!n){return!1}if(n){if("Escape"===e.key){return e.preventDefault(),t.exitCellEdit(),!0}if("Tab"===e.key){e.preventDefault();const o=t.editingCell;t.exitCellEdit();const l=o&&m(t,o,e.shiftKey?-1:1);return l&&t.enterCellEdit(l.id),!0}return!0}if("Escape"===e.key){return e.preventDefault(),t.clearCellFocus(),!0}const r=t.focusedCells[0],a=r&&t.visibleCellAt(r.row,r.col);if(!a){return!1}const{row:c,col:i}=a;if("Enter"===e.key){return e.preventDefault(),t.enterCellEdit(a.id),!0}if("Tab"===e.key){e.preventDefault();const o=m(t,a,e.shiftKey?-1:1);return o&&t.focusCell(o.id),!0}if("ArrowRight"===e.key||"ArrowLeft"===e.key||"ArrowDown"===e.key||"ArrowUp"===e.key){if(e.preventDefault(),e.shiftKey){const o=function(e){const t=e.cells.find(t=>t.id===e._anchorCellId);if(!t){return null}const o=e.focusedCells;if(o.length<=1){return{row:t.row,col:t.col}}let l=1/0,n=-1,r=1/0,a=-1;for(const c of o){c.row<l&&(l=c.row),c.row>n&&(n=c.row),c.col<r&&(r=c.col),c.col>a&&(a=c.col)}return{row:t.row===l?n:l,col:t.col===r?a:r}}(t)||{row:c,col:i};let l=o.row,n=o.col;"ArrowRight"===e.key&&(n=Math.min(o.col+1,t.cols-1)),"ArrowLeft"===e.key&&(n=Math.max(o.col-1,0)),"ArrowDown"===e.key&&(l=Math.min(o.row+1,t.rows-1)),"ArrowUp"===e.key&&(l=Math.max(o.row-1,0)),t.focusCellRange(l,n)}else{"ArrowRight"===e.key&&h(t,c,Math.min(i+a.colSpan,t.cols-1)),"ArrowLeft"===e.key&&h(t,c,Math.max(i-1,0)),"ArrowDown"===e.key&&h(t,Math.min(c+a.rowSpan,t.rows-1),i),"ArrowUp"===e.key&&h(t,Math.max(c-1,0),i)}return!0}const s=e.ctrlKey||e.metaKey;if(!(s||e.shiftKey||e.altKey||"Delete"!==e.key&&"Backspace"!==e.key)){return e.preventDefault(),t.focusedCells.forEach(e=>{e.set({text:""})}),!0}if(s&&e.shiftKey&&!e.altKey&&"Equal"===e.code){e.preventDefault();const l=t.getFocusedCellRange(),n=l?l.maxRow+1:c+1;return o.history.transaction(()=>{t.addRow(n)}),!0}if(s&&e.shiftKey&&e.altKey&&"Equal"===e.code){e.preventDefault();const l=t.getFocusedCellRange(),n=l?l.maxCol+1:i+1;return o.history.transaction(()=>{t.addColumn(n)}),!0}if(s&&!e.shiftKey&&"Backspace"===e.code){const l=t.getFocusedCellRange();if(l&&0===l.minCol&&l.maxCol===t.cols-1&&t.rows>1){return e.preventDefault(),o.history.transaction(()=>{t.removeRowRange(l.minRow,l.maxRow)}),!0}}if(s&&e.shiftKey&&"Backspace"===e.code){const l=t.getFocusedCellRange();if(l&&0===l.minRow&&l.maxRow===t.rows-1&&t.cols>1){return e.preventDefault(),o.history.transaction(()=>{t.removeColumnRange(l.minCol,l.maxCol)}),!0}}return s&&"KeyA"===e.code?(e.preventDefault(),t.cells.forEach(e=>{t.focusCell(e.id,!0)}),!0):!s||"z"!==e.key.toLowerCase()&&"y"!==e.key.toLowerCase()}(t,S,o)){return}if(R.activeIsEditable){return}if(R.hasOutsideTextSelection){return}if(o.activeTool&&"crop"!==o.activeTool.name&&"Escape"===t.key&&R.isWorkspaceActive){return t.preventDefault(),void o._closeImageTool("discard")}if("Alt"===t.key){const e=R.workspaceContainer;return void((R.isWorkspaceActive||o.distanceGuidesVisible||e&&e.matches(":hover"))&&t.preventDefault())}if("Escape"===t.key||"Enter"===t.key){const e=o._hasCroppedImages;if(e&&("Escape"===t.key||R.isWorkspaceActive)){return t.preventDefault(),void("Escape"===t.key?e.cancelCropMode():(null===(n=o.activeTool)||void 0===n?void 0:n.pending)||e.toggleCropMode(!1))}}const W=o.selectedElements.filter(e=>e.removable).map(e=>e.id),I=!!o.activeTool&&"crop"!==o.activeTool.name;46!==t.keyCode&&8!==t.keyCode||I||o.deleteElements(W);const M=t.ctrlKey||t.metaKey,L=t.shiftKey,P=t.altKey;if("Escape"===t.key){return t.preventDefault(),void("draw"===o.tool?o.setTool("selection"):o.selectElements([]))}if(M&&!L&&"z"===t.key.toLowerCase()){return t.preventDefault(),void o.history.undo()}if(M&&(L&&"z"===t.key.toLowerCase()||"y"===t.key.toLowerCase())){return t.preventDefault(),void o.history.redo()}if(M&&"KeyA"===t.code){t.preventDefault();const e=null===(p=o.activePage)||void 0===p?void 0:p.children.filter(e=>e.selectable),l=(null==e?void 0:e.map(e=>e.id))||[];o.selectElements(l)}const F="Equal"===t.code||"NumpadAdd"===t.code||"="===t.key||"+"===t.key,_="Minus"===t.code||"NumpadSubtract"===t.code||"-"===t.key;if(M&&(F||_)){t.preventDefault();const e=Math.max(5,o.scaleToFit),l=Math.max(Math.min(.1,o.scaleToFit),.01),n=o.scale+(F?.1:-.1);return void o.setScale(Math.min(e,Math.max(l,n)))}if(!I&&!o.selectedShapes.some(e=>"pendingEdit"in e&&e.pendingEdit)){if("ArrowDown"===t.code){t.preventDefault();const e=t.shiftKey?10:1;o.selectedShapes.forEach(t=>{t.draggable&&t.set({y:t.y+e})})}if("ArrowUp"===t.code){t.preventDefault();const e=t.shiftKey?10:1;o.selectedShapes.forEach(t=>{t.draggable&&t.set({y:t.y-e})})}if("ArrowLeft"===t.code){t.preventDefault();const e=t.shiftKey?10:1;o.selectedShapes.forEach(t=>{t.draggable&&t.set({x:t.x-e})})}if("ArrowRight"===t.code){t.preventDefault();const e=t.shiftKey?10:1;o.selectedShapes.forEach(t=>{t.draggable&&t.set({x:t.x+e})})}if(M&&"KeyG"===t.code){t.preventDefault();const e=o.selectedElements[0];if(e&&"group"===e.type){const t=e;o.ungroupElements([t.id])}else{o.groupElements(o.selectedElements.map(e=>e.id))}}if(M&&"KeyD"===t.code&&(t.preventDefault(),a(o.selectedElements,o)),"KeyT"!==t.code||M||(t.preventDefault(),r(o,"Sample Text",{fontFamily:"Roboto"})),"KeyR"===t.code&&!M){t.preventDefault();const e={type:"figure",x:o.width/4,y:o.height/4,width:300,height:300,fill:v,stroke:"#0c0c0c",strokeWidth:0,subType:"rect"};null===(w=o.activePage)||void 0===w||w.addElement(e)}if("KeyL"===t.code&&!M){t.preventDefault();const e=(null!==(K=null===(k=o.activePage)||void 0===k?void 0:k.computedWidth)&&void 0!==K?K:0)/3,l={type:"line",x:(null!==(b=null===(E=o.activePage)||void 0===E?void 0:E.computedWidth)&&void 0!==b?b:0)/2-e/2,y:(null!==(A=null===(D=o.activePage)||void 0===D?void 0:D.computedHeight)&&void 0!==A?A:0)/2,width:e,color:v};null===(C=o.activePage)||void 0===C||C.addElement(l)}if("KeyO"===t.code&&!M){t.preventDefault(),t.preventDefault();const e={type:"figure",x:o.width/4,y:o.height/4,width:300,height:300,fill:v,stroke:"#0c0c0c",strokeWidth:0,subType:"circle"};null===(x=o.activePage)||void 0===x||x.addElement(e)}P&&("KeyA"===t.code&&(t.preventDefault(),s(o)),"KeyD"===t.code&&(t.preventDefault(),u(o)),"KeyS"===t.code&&(t.preventDefault(),c(o)),"KeyW"===t.code&&(t.preventDefault(),f(o)),"KeyV"===t.code&&(t.preventDefault(),d(o)),"KeyH"===t.code&&(t.preventDefault(),i(o))),"BracketRight"===t.code&&(t.preventDefault(),o.selectedElements.forEach(e=>M?e.moveUp():e.moveTop())),"BracketLeft"===t.code&&(t.preventDefault(),o.selectedElements.forEach(e=>M?e.moveDown():e.moveBottom()))}}function w(e,t){const o=e.focusedCells.map(e=>p(e.text)).join("\n");t.clipboardData.setData("text/plain",o||" "),t.preventDefault()}export function handleCopyEvent(e,o,l){const n=getWorkspaceEventScope(o,l);n.handle&&e.clipboardData&&(n.table?w(n.table,e):t(o,e))}export function handleCutEvent(e,t,l){const n=getWorkspaceEventScope(t,l);if(n.handle&&e.clipboardData){if(n.table){const o=n.table;return w(o,e),void t.history.transaction(()=>{o.focusedCells.forEach(e=>e.set({text:""}))})}o(t,e)}}export function handlePasteEvent(e,t,o){const r=getWorkspaceEventScope(t,o);if(!r.handle){return}const a=r.table;n(t,e,{onText:a?e=>{var t;return null===(t=a.focusedCells[0])||void 0===t?void 0:t.set({text:l(e)})}:void 0})}
@@ -1 +1 @@
1
- import{Fragment as t,jsx as e,jsxs as o}from"react/jsx-runtime";import r from"react";import{toDisplayColor as i}from"@polotno/core/color";import{elementOpacity as n}from"./paint-opacity.js";import{observer as a}from"mobx-react-lite";import{Group as s,Image as l,Path as c,Rect as d}from"react-konva";import{activeAnchorOf as f,scaledFontSizeAttrs as h,roundedFontSizeAttrs as u,roundedTextDecorationAttrs as g,roundedGeometryPx as m,roundedPosition as p,textMinResizeWidth as x,transformScale as y}from"../utils/px.js";import b from"konva";import{isAlive as w}from"mobx-state-tree";import{nodeAutorun as S}from"../utils/mobx.js";import{flags as v}from"../utils/flags.js";import{textOverflowOf as E}from"../utils/text-sizing.js";import{layoutTextSizing as z,legacyTextOverflow as k,renderTagLayoutAt as O,resolveTextSizing as j}from"@polotno/core/text-sizing";import{applyFilter as F}from"./apply-filters.js";import{getLineHeight as M,useFontFamiliesLoader as R,usePrevious as Y}from"./text-element.js";import{getCurvePath as X}from"@polotno/core/text-html";import{useColor as C,withDisplayTextPaints as I}from"./use-color.js";import{useWorkspaceStyle as A}from"./workspace-style.js";import{detectSize as P,detectLineRects as $,htmlToCanvas as B}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as T}from"@polotno/core/background-shape";import{useFadeIn as W}from"./use-fadein.js";import{Html as H}from"react-konva-utils";import{incrementLoader as _,triggerLoadError as D}from"../utils/loader.js";import{removeTags as L}from"@polotno/core/text";import{cssFontFamily as J}from"@polotno/core/fonts";import{shadowCastByBackground as N}from"@polotno/core/filters";import*as q from"../utils/fonts.js";import{isTouchDevice as V}from"../utils/screen.js";import{getLimitedFontSize as G}from"./text-element/max-font-size.js";import{AutoDeleteEmptyText as K}from"./text-element/auto-delete-empty-text.js";import{useDelayer as Q}from"./use-delayer.js";import{geckoRtlOverhangCss as U,getHtml as Z,letterSpacingOverhang as tt,OVERHANG_PROPERTY as et,textLayoutWidth as ot}from"@polotno/core/text-html";import{hasMeasuredHeight as rt,resolveTextBoxHeight as it}from"@polotno/core/text-types";import{normalizeRichTextHtml as nt,extractFontFamiliesFromHtml as at,scaleRichTextFontSizesInHtml as st}from"../utils/rich-text-html.js";import{RichEditor as lt,RichTextContainer as ct,applyOpacityToColor as dt,quillRef as ft,createQuill as ht,setQuillContent as ut,setQuillFormats as gt,syncQuillFormatState as mt,setCursorFromCoords as pt}from"./rich-editor.js";export{ft as quillRef,ht as createQuill,ut as setQuillContent,gt as setQuillFormats,mt as syncQuillFormatState,pt as setCursorFromCoords};function xt(t){const e=document.createElement("canvas"),o=e.getContext("2d");return null==o||o.drawImage(t,0,0,e.width,e.height),function(t){return!function(t){const e=t.getContext("2d").getImageData(0,0,t.width,t.height).data;for(let o=0;o<e.length;o+=4){if(0!==e[o+3]){return!0}}return!1}(t)}(e)}const yt=/^((?!chrome|android).)*safari/i.test(navigator.userAgent),bt=navigator.userAgent.includes("Firefox");export const HTMLElement=a(({element:a,store:ft})=>{var ht,ut,gt;const{textOverflowIndicatorStyle:mt}=A(),pt=r.useRef(null),[wt,St]=r.useState(),[vt,Et]=r.useState(!1),[zt,kt]=r.useState(!1),Ot=r.useRef(a.height),jt=a.fontSize/3,Ft=ot(a.a.width,a.letterSpacing,a.fontSize)+2*jt,{textVerticalResizeEnabled:Mt}=v,Rt=Y(a.fontFamily),Yt=r.useMemo(()=>nt(a.text,{fontSize:a.fontSize,fontFamily:a.fontFamily}),[a.text,a.fontSize,a.fontFamily]),Xt=r.useMemo(()=>[...new Set([a.fontFamily,...at(Yt)])].filter(Boolean),[a.fontFamily,Yt]),[Ct]=R(ft,Xt,a.fontStyle,a.fontWeight,a.text||a.placeholder),It=a._editModeEnabled,At=It&&!(null===(ht=a.page)||void 0===ht?void 0:ht._exporting);W(pt);const Pt=Ct?a.fontFamily:Rt!==a.fontFamily?Rt:"Arial",$t=C(a).fill,Bt=Z(I(a),{fontFamily:Pt,color:$t,textOverflow:E(a)}),Tt=function(t,e){return e&&/dir="rtl"|direction: rtl/.test(t)?`<div class="polotno-overhang" style="${et}: ${e}px">${t}</div><style>${U(".polotno-overhang [dir]:not(.polotno-overhang [dir] [dir])")}</style>`:t}(Bt,tt(a.letterSpacing,a.fontSize)),Wt=r.useRef([]),Ht=r.useRef(!0),[,_t]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=Wt.current;t.length>0&&(Wt.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{Ht.current=!1;const t=Wt.current;t.length>0&&(Wt.current=[],t.forEach(t=>t()))},[]);let{width:Dt,height:Lt}=function(t,e,o){return r.useMemo(()=>P(t),[t,e.width,o])}(Tt,a,Ct);r.useEffect(()=>{if(!Ct){return}const t=(t,e,o,r)=>{const i=_(`text ${a.id}`),n=()=>{ft.history.ignore(t,e,o).then(()=>{Ht.current?(Wt.current.push(i),_t()):i()})};r?queueMicrotask(n):n()};if(a.curveEnabled&&Math.abs(Dt-a.width)>1&&!vt&&t(()=>{if(w(a)&&Math.abs(Dt-a.width)>1){const t=a.x+a.width/2;a.set({width:Dt,x:t-Dt/2})}},!1,!0),!rt(a)){return void t(()=>{w(a)&&(rt(a)||a.set({height:Lt}))},void 0,void 0,!0)}const e=k(E(a));if("change-font-size"!==e||vt||It||a.curveEnabled){"resize"===e&&(Mt&&a.height<Lt&&!vt&&t(()=>{w(a)&&a.set({height:Lt})},!1,!0),Mt||a.height===Lt||vt||t(()=>{w(a)&&a.set({height:Lt})},!1,!0))}else{const o=z({element:a,sizing:j({text:a.text,width:a.width,height:a.height,fontSize:a.fontSize,maxLines:a.maxLines,curveEnabled:a.curveEnabled},{textOverflow:e,textSplitAllowed:v.textSplitAllowed}),layoutAt:O(a,{getHtmlOptions:{fontFamily:a.fontFamily}})});o.fontSize!==a.fontSize?t(()=>{if(!w(a)){return}const t=h(a,o.fontSize);o.text!==a.text&&(t.text=o.text),a.set(t)},!1,!0):a.height!==Lt&&(Mt&&a.height<Lt?t(()=>{w(a)&&a.set({height:Lt})},!1,!0):Mt||t(()=>{w(a)&&a.set({height:Lt})},!1,!0))}});const Jt=r.useMemo(()=>{const t={lastArgs:null,lastResult:null};return async function(e){return t.lastArgs&&t.lastResult&&(o=t.lastArgs,r=e,JSON.stringify(o)===JSON.stringify(r))||(t.lastResult=await B(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),Nt=r.useRef(0),qt=r.useRef(null);r.useEffect(()=>{vt||At||(async()=>{Nt.current++;const t=Nt.current;let e=_(`text ${a.id} ${t}`);qt.current&&qt.current(),qt.current=e,kt(!0);let o=null;const r=yt?5:1;for(let n=0;n<r;n++){const e=n>0?Jt:B;try{if(o=await e({skipCache:n>0,html:Tt,width:Dt||a.width||1,height:it(a,Lt)||1,fontFamily:Pt,fontFamilies:at(Tt),resolveFont:t=>ft.fonts.find(e=>e.fontFamily===t)||q.globalFonts.find(e=>e.fontFamily===t),padding:jt,pixelRatio:ft._elementsPixelRatio,font:ft.fonts.find(t=>t.fontFamily===Pt)||q.globalFonts.find(t=>t.fontFamily===Pt)}),t!==Nt.current){return}if((yt||bt)&&o&&xt(o)){await new Promise(t=>setTimeout(t,50*(n+1)));continue}break}catch(i){console.error(i),D(`Error rendering rich text with id ${a.id}`);break}}o?St(o):e?(e(),e=null):console.error("Finish function is called twice!"),kt(!1)})()},[Tt,vt,Lt,At,Pt,a.height,ft._elementsPixelRatio,Ct]);const[Vt,Gt]=Q(zt,300),[Kt]=Q(vt,300,!0),Qt=Kt||Vt;r.useEffect(()=>{var t;if(!Qt){return S(a,()=>{const t=pt.current;F(t,a)})}null===(t=pt.current)||void 0===t||t.clearCache()},[wt,Qt,a.shadowColor,a.shadowOffsetX,a.shadowOffsetY,a.shadowOpacity]),r.useEffect(()=>{wt&&!zt&&qt.current&&(qt.current(),qt.current=null)},[wt,zt]),r.useLayoutEffect(()=>{if(!Ct){return}if(!a.curveEnabled){return}const t=new b.TextPath({data:X(a.a.width,a.a.height,a.curvePower,a.a.fontSize),text:L(a.text),letterSpacing:a.letterSpacing*a.a.fontSize,fontSize:a.a.fontSize,fontFamily:J(a.fontFamily),fontWeight:a.fontWeight,fontStyle:a.fontStyle,align:"center",textBaseline:"middle",fill:a.fill}),e=t.getSelfRect().width;if(e){const t=e-a.a.width,o=a.a.rotation*Math.PI/180,r=-t/2*Math.cos(o),i=-t/2*Math.sin(o);queueMicrotask(()=>{w(a)&&a.set({width:e,x:a.a.x+r,y:a.a.y+i})})}t.destroy()},[Ct,a.curveEnabled,a.curvePower,a.text,a.fontSize,a.fontFamily,a.fontWeight,a.fontStyle,a.letterSpacing]),r.useEffect(()=>()=>{qt.current&&qt.current()},[]);const Ut=it(a,Lt);let Zt=0;"middle"===a.verticalAlign&&(Zt=(Ut-Lt)/2),"bottom"===a.verticalAlign&&(Zt=Ut-Lt);const te=M({fontLoaded:Ct,fontFamily:a.fontFamily,fontSize:a.fontSize,lineHeight:a.lineHeight}),ee=a.backgroundPadding*(a.fontSize*te*.5),oe=a.backgroundCornerRadius*(a.fontSize*te*.5),re=r.useMemo(()=>{if(!a.backgroundEnabled&&!a.borderSize||a.curveEnabled||a.legacyBackground){return""}const t=$(Bt);if(0===t.length){return""}const e=t.map(t=>({left:t.left+jt,right:t.right+jt,top:t.top+jt,bottom:t.bottom+jt}));return T({rects:e,padding:ee,cornerRadius:oe})},[a.backgroundEnabled,a.borderSize,a.curveEnabled,a.legacyBackground,Bt,Ct,ee,oe,jt]),ie=V(),ne=X(a.a.width,Lt,a.curvePower,a.fontSize),ae=r.useRef(null),se=r.useRef(a.width),le=r.useRef(""),ce=r.useRef(0),de=r.useRef(0),fe=r.useRef(null),he=It&&a.strokeWidth>0&&!a.curveEnabled,ue=Dt>0?Ft/(Dt+2*jt):1,ge=a.shadowEnabled&&!N(a),me={shadowEnabled:a.shadowEnabled&&(N(a)||a.borderSize>0),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:i(a.shadowColor),shadowOpacity:a.shadowOpacity};return o(t,{children:[e(K,{element:a,store:ft}),a.curveEnabled||a.legacyBackground?e(d,Object.assign({x:a.a.x,y:a.a.y,offsetX:ee/ue,offsetY:ee/ue,rotation:a.a.rotation,scaleX:ue,scaleY:ue,hideInExport:!a.showInExport,listening:!1,visible:a.backgroundEnabled||a.borderSize>0,opacity:n(a),fillEnabled:a.backgroundEnabled,fill:dt(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,width:a.width+2*ee,height:Ut+2*ee,cornerRadius:oe},me)):e(c,Object.assign({name:"html-background",x:a.a.x,y:a.a.y,offsetX:jt/ue,offsetY:(jt-Zt)/ue,rotation:a.a.rotation,scaleX:ue,scaleY:ue,hideInExport:!a.showInExport,listening:!1,visible:(a.backgroundEnabled||a.borderSize>0)&&re.length>0,opacity:n(a),fillEnabled:a.backgroundEnabled,fill:dt(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,lineJoin:oe>0?"round":"miter",data:re},me)),e(c,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,data:ne,stroke:mt.stroke,strokeWidth:1,visible:!1}),e(d,{ref:pt,name:"element",x:a.a.x,y:a.a.y,listening:a.selectable,rotation:a.a.rotation,width:a.a.width,height:a.a.height,visible:!Qt,draggable:ie?a.draggable&&a.isSelected:a.draggable,preventDefault:!ie||a.isSelected,opacity:It?0:n(a),hideInExport:!a.showInExport,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(p(t.target))},id:a.id,onDblClick:t=>{a.contentEditable&&(fe.current={x:t.evt.clientX,y:t.evt.clientY},a.toggleEditMode(!0))},onDblTap:t=>{var e;if(a.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];fe.current=o?{x:o.clientX,y:o.clientY}:null,a.toggleEditMode(!0)}},onTransformStart:t=>{Et(!0),Ot.current=a.height,se.current=a.width,ae.current=t.target.position(),le.current=f(t.target),ce.current=a.a.rotation,de.current=a.height},onTransform:t=>{const e=t.target,o=le.current,r="middle-left"===o||"middle-right"===o,i="top-center"===o||"bottom-center"===o,n=y(e.scaleX());if(r){const t=e.width()*n,o=x(se.current,a.fontSize);let r=t;t<o&&(r=o,ae.current&&e.position(ae.current)),e.width(r),e.scaleX(1),e.scaleY(1);const i=Z(Object.assign(Object.assign({},a.toJSON()),{width:r}),{fontFamily:Pt,color:$t,textOverflow:E(a)}),s=P(i).height,l=v.textVerticalResizeEnabled?Math.max(s,Ot.current):s;e.height(l),a.set({width:e.width(),height:l,x:e.x(),y:e.y()})}else if(i){const t="resize"===E(a),o="number"==typeof a.lineHeight?a.lineHeight:1,r=t?Lt:o*a.fontSize,i=e.height()*e.scaleY();if(i<r){if(e.height(r),ae.current){if("top-center"===le.current){const t=ce.current*Math.PI/180,o=de.current-r;e.x(ae.current.x-o*Math.sin(t)),e.y(ae.current.y+o*Math.cos(t))}else{e.position(ae.current)}}}else{e.height(i)}e.scaleY(1),e.scaleX(1),de.current=Math.max(r,i),a.set({x:e.x(),y:e.y(),height:e.height(),rotation:e.rotation()})}else{e.scaleX(1),e.scaleY(1),a.set({fontSize:a.fontSize*n,letterSpacing:a.letterSpacing,width:e.width()*n,x:e.x(),y:e.y(),rotation:e.rotation(),height:e.height()*n,shadowBlur:a.shadowBlur*n,shadowOffsetX:a.shadowOffsetX*n,shadowOffsetY:a.shadowOffsetY*n,strokeWidth:a.strokeWidth*n,borderSize:a.borderSize*n,text:st(a.text,n)})}ae.current=t.target.position()},onTransformEnd:t=>{Et(!1),kt(!0);const e=y(t.target.scaleX());t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},u(a,a.fontSize*e)),{width:m(t.target.width()*e),x:m(t.target.x()),y:m(t.target.y()),rotation:m(t.target.rotation()),text:st(a.text,e)}))}}),e(l,Object.assign({ref:pt,image:wt,x:a.a.x,y:a.a.y,offsetX:jt,offsetY:jt-Zt,listening:!1,rotation:a.a.rotation,width:Ft,height:Ft*((null!==(ut=null==wt?void 0:wt.height)&&void 0!==ut?ut:0)/((null==wt?void 0:wt.width)||1)||1),visible:!Qt&&!It,opacity:n(a)},me,{shadowEnabled:ge,hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled||vt||Qt})),(Qt&&!It||he)&&e(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-Zt,children:e(H,{divProps:{style:{pointerEvents:"none"}},children:e(ct,{dangerouslySetInnerHTML:{__html:Tt},style:{pointerEvents:"none",opacity:a.a.opacity,textShadow:ge?`${a.shadowOffsetX}px ${a.shadowOffsetY}px ${a.shadowBlur}px ${dt(a.shadowColor,null!==(gt=a.shadowOpacity)&&void 0!==gt?gt:1)}`:void 0}})})}),It&&e(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-Zt,children:e(H,{divProps:{style:{zIndex:20}},children:e(lt,{element:a,onChange:t=>{const e=G({oldText:L(a.text),newText:L(t),element:a});a.set(Object.assign({text:t},h(a,e)))},onBlur:t=>{w(a)&&(a.set(g(a)),a.toggleEditMode(!1),Gt(!0))},clickCoords:fe.current})})})]})});
1
+ import{Fragment as t,jsx as e,jsxs as o}from"react/jsx-runtime";import r from"react";import{toDisplayColor as i}from"@polotno/core/color";import{elementOpacity as n}from"./paint-opacity.js";import{observer as a}from"mobx-react-lite";import{Group as s,Image as l,Path as c,Rect as d}from"react-konva";import{activeAnchorOf as f,scaledFontSizeAttrs as h,roundedFontSizeAttrs as u,roundedTextDecorationAttrs as g,roundedGeometryPx as m,roundedPosition as p,textMinResizeWidth as x,transformScale as b}from"../utils/px.js";import y from"konva";import{isAlive as w}from"mobx-state-tree";import{nodeAutorun as S}from"../utils/mobx.js";import{flags as v}from"../utils/flags.js";import{textOverflowOf as E}from"../utils/text-sizing.js";import{layoutTextSizing as z,legacyTextOverflow as k,renderTagLayoutAt as O,resolveTextSizing as j}from"@polotno/core/text-sizing";import{applyFilter as F}from"./apply-filters.js";import{getLineHeight as M,useFontFamiliesLoader as R,usePrevious as Y}from"./text-element.js";import{getCurvePath as X}from"@polotno/core/text-html";import{useColor as C,withDisplayTextPaints as I}from"./use-color.js";import{useWorkspaceStyle as P}from"./workspace-style.js";import{detectSize as $,detectLineRects as T,htmlToCanvas as A}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as W}from"@polotno/core/background-shape";import{useFadeIn as B}from"./use-fadein.js";import{Html as H}from"react-konva-utils";import{incrementLoader as _,triggerLoadError as D}from"../utils/loader.js";import{removeTags as L}from"@polotno/core/text";import{cssFontFamily as J}from"@polotno/core/fonts";import{shadowCastByBackground as N}from"@polotno/core/filters";import*as q from"../utils/fonts.js";import{isTouchDevice as V}from"../utils/screen.js";import{getLimitedFontSize as G}from"./text-element/max-font-size.js";import{AutoDeleteEmptyText as K}from"./text-element/auto-delete-empty-text.js";import{useLegacyPaddingWarning as Q}from"./text-element/legacy-padding-warning.js";import{useDelayer as U}from"./use-delayer.js";import{geckoRtlOverhangCss as Z,getHtml as tt,letterSpacingOverhang as et,OVERHANG_PROPERTY as ot,textLayoutWidth as rt}from"@polotno/core/text-html";import{hasMeasuredHeight as it,resolveTextBoxHeight as nt}from"@polotno/core/text-types";import{normalizeRichTextHtml as at,extractFontFamiliesFromHtml as st,scaleRichTextFontSizesInHtml as lt}from"../utils/rich-text-html.js";import{RichEditor as ct,RichTextContainer as dt,applyOpacityToColor as ft,quillRef as ht,createQuill as ut,setQuillContent as gt,setQuillFormats as mt,syncQuillFormatState as pt,setCursorFromCoords as xt}from"./rich-editor.js";import{isFirefox as bt,isWebKit as yt}from"../utils/engine.js";export{ht as quillRef,ut as createQuill,gt as setQuillContent,mt as setQuillFormats,pt as syncQuillFormatState,xt as setCursorFromCoords};function wt(t){const e=document.createElement("canvas"),o=e.getContext("2d");return null==o||o.drawImage(t,0,0,e.width,e.height),function(t){return!function(t){const e=t.getContext("2d").getImageData(0,0,t.width,t.height).data;for(let o=0;o<e.length;o+=4){if(0!==e[o+3]){return!0}}return!1}(t)}(e)}export const HTMLElement=a(({element:a,store:ht})=>{var ut,gt,mt;const{textOverflowIndicatorStyle:pt}=P(),xt=r.useRef(null),[St,vt]=r.useState(),[Et,zt]=r.useState(!1),[kt,Ot]=r.useState(!1),jt=r.useRef(a.height);Q(a);const Ft=a.fontSize/3,Mt=rt(a.a.width,a.letterSpacing,a.fontSize)+2*Ft,{textVerticalResizeEnabled:Rt}=v,Yt=Y(a.fontFamily),Xt=r.useMemo(()=>at(a.text,{fontSize:a.fontSize,fontFamily:a.fontFamily}),[a.text,a.fontSize,a.fontFamily]),Ct=r.useMemo(()=>[...new Set([a.fontFamily,...st(Xt)])].filter(Boolean),[a.fontFamily,Xt]),[It]=R(ht,Ct,a.fontStyle,a.fontWeight,a.text||a.placeholder),Pt=a._editModeEnabled,$t=Pt&&!(null===(ut=a.page)||void 0===ut?void 0:ut._exporting);B(xt);const Tt=It?a.fontFamily:Yt!==a.fontFamily?Yt:"Arial",At=C(a).fill,Wt=tt(I(a),{fontFamily:Tt,color:At,textOverflow:E(a)}),Bt=function(t,e){return e&&/dir="rtl"|direction: rtl/.test(t)?`<div class="polotno-overhang" style="${ot}: ${e}px">${t}</div><style>${Z(".polotno-overhang [dir]:not(.polotno-overhang [dir] [dir])")}</style>`:t}(Wt,et(a.letterSpacing,a.fontSize)),Ht=r.useRef([]),_t=r.useRef(!0),[,Dt]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=Ht.current;t.length>0&&(Ht.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{_t.current=!1;const t=Ht.current;t.length>0&&(Ht.current=[],t.forEach(t=>t()))},[]);let{width:Lt,height:Jt}=function(t,e,o){return r.useMemo(()=>$(t),[t,e.width,o])}(Bt,a,It);r.useEffect(()=>{if(!It){return}const t=(t,e,o,r)=>{const i=_(`text ${a.id}`),n=()=>{ht.history.ignore(t,e,o).then(()=>{_t.current?(Ht.current.push(i),Dt()):i()})};r?queueMicrotask(n):n()};if(a.curveEnabled&&Math.abs(Lt-a.width)>1&&!Et&&t(()=>{if(w(a)&&Math.abs(Lt-a.width)>1){const t=a.x+a.width/2;a.set({width:Lt,x:t-Lt/2})}},!1,!0),!it(a)){return void t(()=>{w(a)&&(it(a)||a.set({height:Jt}))},void 0,void 0,!0)}const e=k(E(a));if("change-font-size"!==e||Et||Pt||a.curveEnabled){"resize"===e&&(Rt&&a.height<Jt&&!Et&&t(()=>{w(a)&&a.set({height:Jt})},!1,!0),Rt||a.height===Jt||Et||t(()=>{w(a)&&a.set({height:Jt})},!1,!0))}else{const o=z({element:a,sizing:j({text:a.text,width:a.width,height:a.height,fontSize:a.fontSize,maxLines:a.maxLines,curveEnabled:a.curveEnabled},{textOverflow:e,textSplitAllowed:v.textSplitAllowed}),layoutAt:O(a,{getHtmlOptions:{fontFamily:a.fontFamily}})});o.fontSize!==a.fontSize?t(()=>{if(!w(a)){return}const t=h(a,o.fontSize);o.text!==a.text&&(t.text=o.text),a.set(t)},!1,!0):a.height!==Jt&&(Rt&&a.height<Jt?t(()=>{w(a)&&a.set({height:Jt})},!1,!0):Rt||t(()=>{w(a)&&a.set({height:Jt})},!1,!0))}});const Nt=r.useMemo(()=>{const t={lastArgs:null,lastResult:null};return async function(e){return t.lastArgs&&t.lastResult&&(o=t.lastArgs,r=e,JSON.stringify(o)===JSON.stringify(r))||(t.lastResult=await A(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),qt=r.useRef(0),Vt=r.useRef(null);r.useEffect(()=>{Et||$t||(async()=>{qt.current++;const t=qt.current;let e=_(`text ${a.id} ${t}`);Vt.current&&Vt.current(),Vt.current=e,Ot(!0);let o=null;const r=yt()?5:1;for(let n=0;n<r;n++){const e=n>0?Nt:A;try{if(o=await e({skipCache:n>0,html:Bt,width:Lt||a.width||1,height:nt(a,Jt)||1,fontFamily:Tt,fontFamilies:st(Bt),resolveFont:t=>ht.fonts.find(e=>e.fontFamily===t)||q.globalFonts.find(e=>e.fontFamily===t),padding:Ft,pixelRatio:ht._elementsPixelRatio,font:ht.fonts.find(t=>t.fontFamily===Tt)||q.globalFonts.find(t=>t.fontFamily===Tt)}),t!==qt.current){return}if((yt()||bt())&&o&&wt(o)){await new Promise(t=>setTimeout(t,50*(n+1)));continue}break}catch(i){console.error(i),D(`Error rendering rich text with id ${a.id}`);break}}o?vt(o):e?(e(),e=null):console.error("Finish function is called twice!"),Ot(!1)})()},[Bt,Et,Jt,$t,Tt,a.height,ht._elementsPixelRatio,It]);const[Gt,Kt]=U(kt,300),[Qt]=U(Et,300,!0),Ut=Qt||Gt;r.useEffect(()=>{var t;if(!Ut){return S(a,()=>{const t=xt.current;F(t,a)})}null===(t=xt.current)||void 0===t||t.clearCache()},[St,Ut,a.shadowColor,a.shadowOffsetX,a.shadowOffsetY,a.shadowOpacity]),r.useEffect(()=>{St&&!kt&&Vt.current&&(Vt.current(),Vt.current=null)},[St,kt]),r.useLayoutEffect(()=>{if(!It){return}if(!a.curveEnabled){return}const t=new y.TextPath({data:X(a.a.width,a.a.height,a.curvePower,a.a.fontSize),text:L(a.text),letterSpacing:a.letterSpacing*a.a.fontSize,fontSize:a.a.fontSize,fontFamily:J(a.fontFamily),fontWeight:a.fontWeight,fontStyle:a.fontStyle,align:"center",textBaseline:"middle",fill:a.fill}),e=t.getSelfRect().width;if(e){const t=e-a.a.width,o=a.a.rotation*Math.PI/180,r=-t/2*Math.cos(o),i=-t/2*Math.sin(o);queueMicrotask(()=>{w(a)&&a.set({width:e,x:a.a.x+r,y:a.a.y+i})})}t.destroy()},[It,a.curveEnabled,a.curvePower,a.text,a.fontSize,a.fontFamily,a.fontWeight,a.fontStyle,a.letterSpacing]),r.useEffect(()=>()=>{Vt.current&&Vt.current()},[]);const Zt=nt(a,Jt);let te=0;"middle"===a.verticalAlign&&(te=(Zt-Jt)/2),"bottom"===a.verticalAlign&&(te=Zt-Jt);const ee=M({fontLoaded:It,fontFamily:a.fontFamily,fontSize:a.fontSize,lineHeight:a.lineHeight}),oe=a.backgroundPadding*(a.fontSize*ee*.5),re=a.backgroundCornerRadius*(a.fontSize*ee*.5),ie=r.useMemo(()=>{if(!a.backgroundEnabled&&!a.borderSize||a.curveEnabled||"box"===a.backgroundShape){return""}const t=T(Wt);if(0===t.length){return""}const e=t.map(t=>({left:t.left+Ft,right:t.right+Ft,top:t.top+Ft,bottom:t.bottom+Ft}));return W({rects:e,padding:oe,cornerRadius:re})},[a.backgroundEnabled,a.borderSize,a.curveEnabled,a.backgroundShape,Wt,It,oe,re,Ft]),ne=V(),ae=X(a.a.width,Jt,a.curvePower,a.fontSize),se=r.useRef(null),le=r.useRef(a.width),ce=r.useRef(""),de=r.useRef(0),fe=r.useRef(0),he=r.useRef(null),ue=Pt&&a.strokeWidth>0&&!a.curveEnabled,ge=Lt>0?Mt/(Lt+2*Ft):1,me=a.shadowEnabled&&!N(a),pe={shadowEnabled:a.shadowEnabled&&(N(a)||a.borderSize>0),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:i(a.shadowColor),shadowOpacity:a.shadowOpacity};return o(t,{children:[e(K,{element:a,store:ht}),a.curveEnabled||"box"===a.backgroundShape?e(d,Object.assign({x:a.a.x,y:a.a.y,offsetX:oe/ge,offsetY:oe/ge,rotation:a.a.rotation,scaleX:ge,scaleY:ge,hideInExport:!a.showInExport,listening:!1,visible:a.backgroundEnabled||a.borderSize>0,opacity:n(a),fillEnabled:a.backgroundEnabled,fill:ft(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,width:a.width+2*oe,height:Zt+2*oe,cornerRadius:re},pe)):e(c,Object.assign({name:"html-background",x:a.a.x,y:a.a.y,offsetX:Ft/ge,offsetY:(Ft-te)/ge,rotation:a.a.rotation,scaleX:ge,scaleY:ge,hideInExport:!a.showInExport,listening:!1,visible:(a.backgroundEnabled||a.borderSize>0)&&ie.length>0,opacity:n(a),fillEnabled:a.backgroundEnabled,fill:ft(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,lineJoin:re>0?"round":"miter",data:ie},pe)),e(c,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,data:ae,stroke:pt.stroke,strokeWidth:1,visible:!1}),e(d,{ref:xt,name:"element",x:a.a.x,y:a.a.y,listening:a.selectable,rotation:a.a.rotation,width:a.a.width,height:a.a.height,visible:!Ut,draggable:ne?a.draggable&&a.isSelected:a.draggable,preventDefault:!ne||a.isSelected,opacity:Pt?0:n(a),hideInExport:!a.showInExport,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(p(t.target))},id:a.id,onDblClick:t=>{a.contentEditable&&(he.current={x:t.evt.clientX,y:t.evt.clientY},a.toggleEditMode(!0))},onDblTap:t=>{var e;if(a.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];he.current=o?{x:o.clientX,y:o.clientY}:null,a.toggleEditMode(!0)}},onTransformStart:t=>{zt(!0),jt.current=a.height,le.current=a.width,se.current=t.target.position(),ce.current=f(t.target),de.current=a.a.rotation,fe.current=a.height},onTransform:t=>{const e=t.target,o=ce.current,r="middle-left"===o||"middle-right"===o,i="top-center"===o||"bottom-center"===o,n=b(e.scaleX());if(r){const t=e.width()*n,o=x(le.current,a.fontSize);let r=t;t<o&&(r=o,se.current&&e.position(se.current)),e.width(r),e.scaleX(1),e.scaleY(1);const i=tt(Object.assign(Object.assign({},a.toJSON()),{width:r}),{fontFamily:Tt,color:At,textOverflow:E(a)}),s=$(i).height,l=v.textVerticalResizeEnabled?Math.max(s,jt.current):s;e.height(l),a.set({width:e.width(),height:l,x:e.x(),y:e.y()})}else if(i){const t="resize"===E(a),o="number"==typeof a.lineHeight?a.lineHeight:1,r=t?Jt:o*a.fontSize,i=e.height()*e.scaleY();if(i<r){if(e.height(r),se.current){if("top-center"===ce.current){const t=de.current*Math.PI/180,o=fe.current-r;e.x(se.current.x-o*Math.sin(t)),e.y(se.current.y+o*Math.cos(t))}else{e.position(se.current)}}}else{e.height(i)}e.scaleY(1),e.scaleX(1),fe.current=Math.max(r,i),a.set({x:e.x(),y:e.y(),height:e.height(),rotation:e.rotation()})}else{e.scaleX(1),e.scaleY(1),a.set({fontSize:a.fontSize*n,letterSpacing:a.letterSpacing,width:e.width()*n,x:e.x(),y:e.y(),rotation:e.rotation(),height:e.height()*n,shadowBlur:a.shadowBlur*n,shadowOffsetX:a.shadowOffsetX*n,shadowOffsetY:a.shadowOffsetY*n,strokeWidth:a.strokeWidth*n,borderSize:a.borderSize*n,text:lt(a.text,n)})}se.current=t.target.position()},onTransformEnd:t=>{zt(!1),Ot(!0);const e=b(t.target.scaleX());t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},u(a,a.fontSize*e)),{width:m(t.target.width()*e),x:m(t.target.x()),y:m(t.target.y()),rotation:m(t.target.rotation()),text:lt(a.text,e)}))}}),e(l,Object.assign({ref:xt,image:St,x:a.a.x,y:a.a.y,offsetX:Ft,offsetY:Ft-te,listening:!1,rotation:a.a.rotation,width:Mt,height:Mt*((null!==(gt=null==St?void 0:St.height)&&void 0!==gt?gt:0)/((null==St?void 0:St.width)||1)||1),visible:!Ut&&!Pt,opacity:n(a)},pe,{shadowEnabled:me,hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled||Et||Ut})),(Ut&&!Pt||ue)&&e(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-te,children:e(H,{divProps:{style:{pointerEvents:"none"}},children:e(dt,{dangerouslySetInnerHTML:{__html:Bt},style:{pointerEvents:"none",opacity:a.a.opacity,textShadow:me?`${a.shadowOffsetX}px ${a.shadowOffsetY}px ${a.shadowBlur}px ${ft(a.shadowColor,null!==(mt=a.shadowOpacity)&&void 0!==mt?mt:1)}`:void 0}})})}),Pt&&e(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-te,children:e(H,{divProps:{style:{zIndex:20}},children:e(ct,{element:a,onChange:t=>{const e=G({oldText:L(a.text),newText:L(t),element:a});a.set(Object.assign({text:t},h(a,e)))},onBlur:t=>{w(a)&&(a.set(g(a)),a.toggleEditMode(!1),Kt(!0))},clickCoords:he.current})})})]})});
@@ -1 +1 @@
1
- import{Fragment as e,jsx as t,jsxs as r}from"react/jsx-runtime";import o from"react";import{toDisplayColor as i}from"@polotno/core/color";import{animatedShadow as a}from"./animated-shadow.js";import{observer as n}from"mobx-react-lite";import{isAlive as s}from"mobx-state-tree";import{Image as d,Rect as h}from"react-konva";import{ROLES as c}from"../model/store.js";import l from"./use-image.js";import u from"konva";import{ErrorPlaceholder as g,LoadingPlaceholder as m}from"./media-placeholders.js";import{incrementLoader as p,triggerLoadError as f}from"../utils/loader.js";import{polotnoError as v}from"@polotno/core/errors";import{getBorderInset as w,getCropRect as x,mayBeUnsizedSvg as y}from"@polotno/core/image";import{elementOpacity as M,paintOpacity as S}from"./paint-opacity.js";import*as b from"@polotno/core/svg";import{trySetCanvasSize as E}from"../utils/canvas.js";import{roundedPosition as I}from"../utils/px.js";import{useMediaResize as C}from"./media-resize.js";import{useFilteredSource as j}from"./use-filtered-source.js";import{useOffscreenCanvas as R}from"./use-offscreen-canvas.js";import{useFadeIn as z}from"./use-fadein.js";import{useAnimatedBlur as D}from"./use-animated-blur.js";import{PlacedNode as O}from"./placed-node.js";import{isTouchDevice as k}from"../utils/screen.js";import{useDelayer as L}from"./use-delayer.js";import{useColor as _}from"./use-color.js";import{commitCropSource as T,cropDraftWriter as A,CropOverlay as X}from"./crop-overlay.js";import{ImageToolControls as Y}from"./image-tool-controls.js";import{drawReduced as W,exportKernelEnabled as H,shouldResample as P}from"./downscale.js";import{alignToPixelGrid as N,coversWholeSource as U,displayRatio as F,maskCompositeSize as G,MAX_ELEMENT_BUFFER as $,svgSourceRatio as q}from"@polotno/core/image-buffer";export{G as maskCompositeSize,q as svgSourceRatio};const J=(e,t,r)=>F(e.a,t,r),Q=new Map,V=new Map;let B=0;const K=e=>{const t=Q.get(e);return void 0!==t&&(Q.delete(e),Q.set(e,t)),t},Z=(e,t)=>{const r=e.length+t.length;if(!(r>4e6||Q.has(e))){Q.set(e,t),B+=r;for(const[e,t]of Q){if(B<=16e6){break}Q.delete(e),B-=e.length+t.length}}},ee=(e,t)=>e+"\n"+JSON.stringify((null==t?void 0:t.size)?[...t]:[]),te=new Map,re=new Map,oe=e=>b.isSvgUrl(e)||!0===te.get(e),ie=e=>"svg"===e.type||oe(e.a.src||""),ae=(e,t,r,i)=>{const[a,n]=o.useState(""),s=!t&&!!r&&"loaded"===i&&!e.startsWith("data:")&&!te.has(e)&&a!==e&&y(r);return o.useEffect(()=>{if(!s){return}let t=!0;return(e=>{let t=re.get(e);return t||(t=b.fetchSvgText(e).then(t=>{null!==t&&Z(ee(e),b.prepareSvgSource(t)),te.set(e,null!==t)}).catch(()=>{}).finally(()=>re.delete(e)),re.set(e,t)),t})(e).then(()=>{t&&n(e)}),()=>{t=!1}},[s,e]),s?"loading":i},ne=(e,t,r)=>{const[,i]=o.useReducer(e=>e+1,0),a=t&&e?ee(e,r):"",n=o.useRef("loading"),s=o.useRef(""),d=o.useRef(a);if(d.current!==a&&(d.current=a,n.current="loading"),a&&"loading"===n.current){const e=K(a);void 0!==e&&(s.current=e,n.current="loaded")}return o.useEffect(()=>{if(!a||"loaded"===n.current){return}let t=!1;return((e,t)=>{const r=ee(e,t),o=K(r);if(void 0!==o){return Promise.resolve(o)}let i=V.get(r);if(!i){const o=(null==t?void 0:t.size)?new Map(t):null;i=b.urlToString(e).then(e=>b.prepareSvgSource(e,o)),V.set(r,i),i.then(e=>Z(r,e),()=>{}).finally(()=>V.delete(r))}return i})(e,r).then(e=>{t||(s.current=e,n.current="loaded",i())},()=>{t||(s.current="",n.current="failed",i())}),()=>{t=!0}},[a]),a?[s.current,n.current]:[e,"loaded"]};export const useSizeFixer=e=>ne(e,oe(e))[0]||e;function se(e,t){return{x:(e.x+t.x)/2,y:(e.y+t.y)/2}}const de=e=>{if(e.flipX||e.flipY||e.maskSrc){return!0}const t=navigator.userAgent.toLowerCase();return(t.indexOf("firefox")>-1||/^((?!chrome|android).)*safari/i.test(t))&&ie(e)},he=(e,t,r)=>Math.max(t,Math.min(r,e));export const useCornerRadiusAndCrop=(e,t,r,i,a=0,n=!1,s=!0,d=0,h)=>{var c;const l=$,g=$,m=h.aligned?he(h.deviceWidth,1,l):Math.floor(he(e.a.width*i,1,l)),p=h.aligned?he(h.deviceHeight,1,g):Math.floor(he(e.a.height*i,1,g)),f=Math.min(a*i,m/2,p/2),v=Math.min(d*i,m/2,p/2),w=J(e,r,i),x=H(e.page)&&!n&&(P(w,m,p)||h.aligned),y=U(r,t),M=o.useMemo(()=>{if(t&&t.width&&t.height){return!y||0!==f||x||v?document.createElement("canvas"):void 0}},[t,f,x,y,v]);return o.useLayoutEffect(()=>{var o;if(!M||!t){return}E(M,m,p);const i=M.getContext("2d");if(i){if((null===(o=e.page)||void 0===o?void 0:o._exportingOrRendering)&&(i.imageSmoothingQuality="high"),(f||v)&&(i.beginPath(),i.roundRect(v,v,m-2*v,p-2*v,Math.max(0,f-v)),i.clip()),x){const e=M.width===m&&M.height===p&&h.aligned;return void W(i,t,r,M.width,M.height,{x:e?h.phaseX:0,y:e?h.phaseY:0,width:e?h.spanX:M.width,height:e?h.spanY:M.height})}i.drawImage(t,r.x,r.y,r.width,r.height,0,0,M.width,M.height)}},[M,e.a.width,e.a.height,r.x,r.y,r.width,r.height,a,i,n,null===(c=e.page)||void 0===c?void 0:c._exportingOrRendering,x,h.phaseX,h.phaseY,v]),o.useEffect(()=>()=>{M&&"CANVAS"===M.nodeName&&u.Util.releaseCanvas(M)},[M]),M||t};const ce=document.createElement("canvas");let le=l;export const setImageLoaderHook=e=>{le=e};export const useCanvasImage=(e,t)=>le(e,t);export const useSizedImage=e=>{const[t,r]=ne(e,oe(e)),[o,i]=useCanvasImage(t,"anonymous"),a=ae(e,oe(e),o,i);return[o,"loaded"===r?a:r]};export const useClipImage=e=>{const[t,r]=useSizedImage(e),i=o.useRef(void 0);return e&&"failed"!==r?t&&(i.current=t):i.current=void 0,[e&&"failed"!==r?t||i.current:void 0,r]};export const useImageLoader=(e,t="",r="")=>{const i=o.useRef(void 0),a=()=>{var e;null===(e=i.current)||void 0===e||e.call(i),i.current=void 0};o.useEffect(()=>a,[]),o.useLayoutEffect(()=>{const o=t.slice(0,200),n=`image with id ${r} url: ${o}`;"loading"!==e||i.current||(i.current=p(n)),"loading"!==e&&a(),"failed"===e&&f(v("IMAGE_FAILED",n,{src:o,elementId:r,reason:"load"}))},[e])};export const ImageElement=n(({element:n,store:l})=>{var p,f,v,y,b;const[W,P]=o.useState(!1),F=o.useRef(null),$=k(),Q=n.a.src,V=ie(n),[B,K]=ne(Q,V,n.colorsReplace),[Z,ee]=le(B,"anonymous"),te="loaded"===K?ee:K,re=ae(Q,V,Z,te),oe=!!Q;useImageLoader(oe?re:"loaded",Q,n.id);const he=(null===(p=n.page)||void 0===p?void 0:p._exportingOrRendering)||!de(n)?1:Math.max(1,l.scale),ue=l._elementsPixelRatio*he,ge=(({image:e,status:t})=>{const r=o.useRef(void 0);return o.useEffect(()=>{r.current=e||r.current},[e]),"failed"!==t?r.current:void 0})({image:Z,status:re}),me=((e,t,r)=>{const o=ie(e)&&t?q({cropWidth:e.a.cropWidth,cropHeight:e.a.cropHeight,width:e.a.width,height:e.a.height},t,r):1,{flipX:i,flipY:a}=e,n=de(e),s=R(n&&!!(t&&t.width&&t.height),e=>{var r,n;if(!t){return}E(e,Math.max(t.width*o,1),Math.max(t.height*o,1));let s=i?-e.width:0,d=a?-e.height:0;null===(r=e.getContext("2d"))||void 0===r||r.scale(i?-1:1,a?-1:1),null===(n=e.getContext("2d"))||void 0===n||n.drawImage(t,s,d,e.width,e.height)},[i,a,t,o]);return n?s:t})(n,Z||ge,ue),pe=((e,t)=>{const[r,i]=useSizedImage(e.maskSrc||""),a=e.maskSrc?i:"loaded";return useImageLoader(a,e.maskSrc||"",e.id),o.useMemo(()=>{if(!r){return t}if(!t||!t.width||!t.height){return t}const{width:o,height:i}=G(t,{width:e.a.width,height:e.a.height,stretchEnabled:e.stretchEnabled}),a=document.createElement("canvas");a.width=o,a.height=i;const n=a.getContext("2d");if(!n){return t}n.drawImage(t,0,0,o,i,0,0,o,i),n.globalCompositeOperation="source-in";const s=function(e,t){const r=t.width/t.height;let o,i;return r>=e.width/e.height?(o=e.width,i=e.width/r):(o=e.height*r,i=e.height),{x:(e.width-o)/2,y:(e.height-i)/2,width:o,height:i}}(r,e);return n.drawImage(r,s.x,s.y,s.width,s.height,0,0,o,i),a},[t,r,e.a.width,e.a.height,e.stretchEnabled])})(n,me)||ce,[fe=ce]=j(n,pe);o.useLayoutEffect(()=>{"loaded"===re&&n._setSourceSize({width:fe.width,height:fe.height},Q)},[n,Q,re,fe.width,fe.height]);let{cropX:ve,cropY:we,cropWidth:xe,cropHeight:ye}=n.a;"loaded"!==ee&&(ve=we=0,xe=ye=1);const Me=x({stretchEnabled:n.stretchEnabled,fit:n.a.fit,focus:n.focus,cropX:ve,cropY:we,cropWidth:xe,cropHeight:ye,width:n.a.width,height:n.a.height},fe),Se=Me.width,be=Me.height,Ee=null!==(b=n.cornerRadius)&&void 0!==b?b:0,Ie=w({borderSize:n.borderSize,borderColor:i(n.borderColor),width:n.a.width,height:n.a.height,opacity:S(n)}),Ce=!(!Ee&&!Ie&&!n.clipSrc&&U(Me,fe)),je=(null===(f=n.page)||void 0===f?void 0:f._exportingOrRendering)||!Ce?1:Math.min(2,l.scale),Re=l._elementsPixelRatio*je,ze=W||n._cropModeEnabled||"svg"===n.type,De=H(n.page)&&0===n.a.rotation&&"group"!==(null===(v=n.parent)||void 0===v?void 0:v.type)&&!ze,Oe=((e,t,r,o)=>N(e.a,t,r,o))(n,Re,J(n,Me,Re),De);let ke=((e,t,r,i)=>{const[a,n]=useClipImage(e.clipSrc||""),s=e.clipSrc?n:"loaded";useImageLoader(s,e.clipSrc,e.id);const d=o.useMemo(()=>{if(t&&a){return document.createElement("canvas")}},[t,a]);return o.useLayoutEffect(()=>{var o;if(!a){return}if(!t||!t.width||!t.height){return}if(!a||!a.width||!a.height){return}if(!d){return}const i=document.createElement("canvas"),n=Math.max(e.a.width/a.width*r,e.a.height/a.height*r);E(i,Math.min(1e4,Math.max(a.width*n,1)),Math.min(1e4,Math.max(a.height*n,1))),null===(o=i.getContext("2d"))||void 0===o||o.drawImage(a,0,0,i.width,i.height),E(d,Math.min(1e4,Math.max(t.width,Math.ceil(e.a.width*r),1)),Math.min(1e4,Math.max(t.height,Math.ceil(e.a.height*r),1)));const s=d.getContext("2d");s&&(s.save(),s.drawImage(i,0,0,d.width,d.height),u.Util.releaseCanvas(i),s.globalCompositeOperation="source-in",s.drawImage(t,0,0,d.width,d.height),s.restore())},[d,t,a,e.a.width,e.a.height,r,...i]),o.useEffect(()=>()=>{d&&u.Util.releaseCanvas(d)},[d]),e.clipSrc&&a?d:t})(n,useCornerRadiusAndCrop(n,fe,Me,Re,Ee,ze,!0,Ie,Oe),Re,[Me,Ee,Re]);var Le=o.useRef(null),_e=o.useRef(0),Te=o.useRef(!1);const Ae=()=>{var e;n._cropEnabled&&!n.pendingEdit&&(null===(e=l.activeTool)||void 0===e?void 0:e.elementId)!==n.id&&setTimeout(()=>{s(n)&&n.toggleCropMode(!0)})},Xe=ie(n)&&ge,Ye=oe&&"loading"===te&&!Xe,[We]=L(Ye,100,!1,!1),He="failed"===re,Pe=!We&&!He,Ne=C(n,Me,"loaded"===ee?fe:void 0,P),Ue=Pe?M(n):0;z(F,Ue),D(F,n,ke);const Fe=n.selectable||l.role===c.ADMIN,Ge=_(n,n.borderColor,"stroke");return r(e,{children:[We&&t(m,{element:n}),He&&t(g,{element:n,text:"Can not load the image..."}),t(O,Object.assign({as:d,nodeRef:F,element:n,name:"element",id:n.id,image:ke},Oe.position,{width:Oe.width||1,height:Oe.height||1,opacity:Ue},a(n),{customCrop:Me,listening:Fe,draggable:!n.pendingEdit&&(null===(y=l.activeTool)||void 0===y?void 0:y.elementId)!==n.id&&($?n.draggable&&n.isSelected:n.draggable),preventDefault:!$||n.isSelected,hideInExport:!n.showInExport,onDragMove:e=>{n.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{n.set(I(e.target))},onDblClick:Ae,onDblTap:Ae},Ne)),!!n.borderSize&&t(O,Object.assign({as:h,element:n},Oe.position,{width:Math.max(Oe.width-n.borderSize,0),height:Math.max(Oe.height-n.borderSize,0),opacity:Ue,offsetX:-n.borderSize/2,offsetY:-n.borderSize/2},Ge,{strokeWidth:n.borderSize,listening:!1,cornerRadius:Math.max(0,Ee-n.borderSize),hideInExport:!n.showInExport})),n._cropModeEnabled&&"loaded"===re&&t(X,{element:n,store:l,previewImage:ke,sourceImage:fe,sourceWidth:fe.width,sourceHeight:fe.height,crop:Me,onSourceTouchMove:e=>{e.evt.preventDefault();const t=e.target.getStage().getPointersPositions();var r=t[0],o=t[1];const i=e.target;if(r&&!o&&!i.isDragging()&&Te.current&&(i.startDrag(),Te.current=!1),r&&o){u.hitOnDragEnabled=!0,i.isDragging()&&(Te.current=!0,i.stopDrag());const t=e.target.getAbsoluteTransform().copy();t.invert();var a={x:r.x,y:r.y},s={x:o.x,y:o.y};if(!Le.current){return void(Le.current=se(a,s))}var d=se(a,s),h=function(e,t){return Math.sqrt(Math.pow(t.x-e.x,2)+Math.pow(t.y-e.y,2))}(a,s);_e.current||(_e.current=h);const v=i.position();var c={x:d.x-v.x,y:d.y-v.y},g=h/_e.current;i.scaleX(g),i.scaleY(g);const w=t.point(d),x=t.point(Le.current);var m=w.x-x.x,p=w.y-x.y,f={x:Math.min(0,d.x-c.x*g+m),y:Math.min(0,d.y-c.y*g+p)};i.position(f),_e.current=h,Le.current=d,(e=>{T(e,n,A(n,l),fe.width,fe.height,Se,be)})(e)}},onSourceTouchEnd:e=>{_e.current=0,Le.current=null,u.hitOnDragEnabled=!1}}),"image"===n.type&&t(Y,{element:n,store:l})]})});
1
+ import{Fragment as e,jsx as t,jsxs as r}from"react/jsx-runtime";import o from"react";import{toDisplayColor as i}from"@polotno/core/color";import{animatedShadow as a}from"./animated-shadow.js";import{observer as n}from"mobx-react-lite";import{isAlive as s}from"mobx-state-tree";import{Image as d,Rect as h}from"react-konva";import{ROLES as c}from"../model/store.js";import l from"./use-image.js";import u from"konva";import{ErrorPlaceholder as g,LoadingPlaceholder as m}from"./media-placeholders.js";import{incrementLoader as p,triggerLoadError as f}from"../utils/loader.js";import{polotnoError as v}from"@polotno/core/errors";import{getBorderInset as w,getCropRect as x,mayBeUnsizedSvg as y}from"@polotno/core/image";import{elementOpacity as M,paintOpacity as S}from"./paint-opacity.js";import*as b from"@polotno/core/svg";import{trySetCanvasSize as E}from"../utils/canvas.js";import{roundedPosition as I}from"../utils/px.js";import{useMediaResize as C}from"./media-resize.js";import{useFilteredSource as j}from"./use-filtered-source.js";import{useOffscreenCanvas as R}from"./use-offscreen-canvas.js";import{useFadeIn as z}from"./use-fadein.js";import{useAnimatedBlur as D}from"./use-animated-blur.js";import{PlacedNode as k}from"./placed-node.js";import{isTouchDevice as O}from"../utils/screen.js";import{useDelayer as _}from"./use-delayer.js";import{useColor as L}from"./use-color.js";import{commitCropSource as T,cropDraftWriter as X,CropOverlay as Y}from"./crop-overlay.js";import{ImageToolControls as A}from"./image-tool-controls.js";import{drawImageWindow as W,drawReduced as H,exportKernelEnabled as P,shouldResample as N}from"./downscale.js";import{alignToPixelGrid as U,coversWholeSource as F,displayRatio as G,maskCompositeSize as $,MAX_ELEMENT_BUFFER as q,svgSourceRatio as J}from"@polotno/core/image-buffer";import{isFirefox as Q,isWebKit as V}from"../utils/engine.js";export{$ as maskCompositeSize,J as svgSourceRatio};const B=(e,t,r)=>G(e.a,t,r),K=new Map,Z=new Map;let ee=0;const te=e=>{const t=K.get(e);return void 0!==t&&(K.delete(e),K.set(e,t)),t},re=(e,t)=>{const r=e.length+t.length;if(!(r>4e6||K.has(e))){K.set(e,t),ee+=r;for(const[e,t]of K){if(ee<=16e6){break}K.delete(e),ee-=e.length+t.length}}},oe=(e,t)=>e+"\n"+JSON.stringify((null==t?void 0:t.size)?[...t]:[]),ie=new Map,ae=new Map,ne=e=>b.isSvgUrl(e)||!0===ie.get(e),se=e=>"svg"===e.type||ne(e.a.src||""),de=(e,t,r,i)=>{const[a,n]=o.useState(""),s=!t&&!!r&&"loaded"===i&&!e.startsWith("data:")&&!ie.has(e)&&a!==e&&y(r);return o.useEffect(()=>{if(!s){return}let t=!0;return(e=>{let t=ae.get(e);return t||(t=b.fetchSvgText(e).then(t=>{null!==t&&re(oe(e),b.prepareSvgSource(t)),ie.set(e,null!==t)}).catch(()=>{}).finally(()=>ae.delete(e)),ae.set(e,t)),t})(e).then(()=>{t&&n(e)}),()=>{t=!1}},[s,e]),s?"loading":i},he=(e,t,r)=>{const[,i]=o.useReducer(e=>e+1,0),a=t&&e?oe(e,r):"",n=o.useRef("loading"),s=o.useRef(""),d=o.useRef(a);if(d.current!==a&&(d.current=a,n.current="loading"),a&&"loading"===n.current){const e=te(a);void 0!==e&&(s.current=e,n.current="loaded")}return o.useEffect(()=>{if(!a||"loaded"===n.current){return}let t=!1;return((e,t)=>{const r=oe(e,t),o=te(r);if(void 0!==o){return Promise.resolve(o)}let i=Z.get(r);if(!i){const o=(null==t?void 0:t.size)?new Map(t):null;i=b.urlToString(e).then(e=>b.prepareSvgSource(e,o)),Z.set(r,i),i.then(e=>re(r,e),()=>{}).finally(()=>Z.delete(r))}return i})(e,r).then(e=>{t||(s.current=e,n.current="loaded",i())},()=>{t||(s.current="",n.current="failed",i())}),()=>{t=!0}},[a]),a?[s.current,n.current]:[e,"loaded"]};export const useSizeFixer=e=>he(e,ne(e))[0]||e;function ce(e,t){return{x:(e.x+t.x)/2,y:(e.y+t.y)/2}}const le=e=>!!(e.flipX||e.flipY||e.maskSrc)||(Q()||V())&&se(e),ue=(e,t,r)=>Math.max(t,Math.min(r,e));export const useCornerRadiusAndCrop=(e,t,r,i,a=0,n=!1,s=!0,d=0,h)=>{var c;const l=q,g=q,m=h.aligned?ue(h.deviceWidth,1,l):Math.floor(ue(e.a.width*i,1,l)),p=h.aligned?ue(h.deviceHeight,1,g):Math.floor(ue(e.a.height*i,1,g)),f=Math.min(a*i,m/2,p/2),v=Math.min(d*i,m/2,p/2),w=B(e,r,i),x=P(e.page)&&!n&&(N(w,m,p)||h.aligned),y=F(r,t),M=o.useMemo(()=>{if(t&&t.width&&t.height){return!y||0!==f||x||v?document.createElement("canvas"):void 0}},[t,f,x,y,v]);return o.useLayoutEffect(()=>{var o;if(!M||!t){return}E(M,m,p);const i=M.getContext("2d");if(i){if((null===(o=e.page)||void 0===o?void 0:o._exportingOrRendering)&&(i.imageSmoothingQuality="high"),(f||v)&&(i.beginPath(),i.roundRect(v,v,m-2*v,p-2*v,Math.max(0,f-v)),i.clip()),x){const e=M.width===m&&M.height===p&&h.aligned;return void H(i,t,r,M.width,M.height,{x:e?h.phaseX:0,y:e?h.phaseY:0,width:e?h.spanX:M.width,height:e?h.spanY:M.height})}W(i,t,r,{x:0,y:0,width:M.width,height:M.height})}},[M,e.a.width,e.a.height,r.x,r.y,r.width,r.height,a,i,n,null===(c=e.page)||void 0===c?void 0:c._exportingOrRendering,x,h.phaseX,h.phaseY,v]),o.useEffect(()=>()=>{M&&"CANVAS"===M.nodeName&&u.Util.releaseCanvas(M)},[M]),M||t};const ge=document.createElement("canvas");let me=l;export const setImageLoaderHook=e=>{me=e};export const useCanvasImage=(e,t)=>me(e,t);export const useSizedImage=e=>{const[t,r]=he(e,ne(e)),[o,i]=useCanvasImage(t,"anonymous"),a=de(e,ne(e),o,i);return[o,"loaded"===r?a:r]};export const useClipImage=e=>{const[t,r]=useSizedImage(e),i=o.useRef(void 0);return e&&"failed"!==r?t&&(i.current=t):i.current=void 0,[e&&"failed"!==r?t||i.current:void 0,r]};export const useImageLoader=(e,t="",r="")=>{const i=o.useRef(void 0),a=()=>{var e;null===(e=i.current)||void 0===e||e.call(i),i.current=void 0};o.useEffect(()=>a,[]),o.useLayoutEffect(()=>{const o=t.slice(0,200),n=`image with id ${r} url: ${o}`;"loading"!==e||i.current||(i.current=p(n)),"loading"!==e&&a(),"failed"===e&&f(v("IMAGE_FAILED",n,{src:o,elementId:r,reason:"load"}))},[e])};export const ImageElement=n(({element:n,store:l})=>{var p,f,v,y,b;const[W,H]=o.useState(!1),N=o.useRef(null),G=O(),q=n.a.src,Q=se(n),[V,K]=he(q,Q,n.colorsReplace),[Z,ee]=me(V,"anonymous"),te="loaded"===K?ee:K,re=de(q,Q,Z,te),oe=!!q;useImageLoader(oe?re:"loaded",q,n.id);const ie=(null===(p=n.page)||void 0===p?void 0:p._exportingOrRendering)||!le(n)?1:Math.max(1,l.scale),ae=l._elementsPixelRatio*ie,ne=(({image:e,status:t})=>{const r=o.useRef(void 0);return o.useEffect(()=>{r.current=e||r.current},[e]),"failed"!==t?r.current:void 0})({image:Z,status:re}),ue=((e,t,r)=>{const o=se(e)&&t?J({cropWidth:e.a.cropWidth,cropHeight:e.a.cropHeight,width:e.a.width,height:e.a.height},t,r):1,{flipX:i,flipY:a}=e,n=le(e),s=R(n&&!!(t&&t.width&&t.height),e=>{var r,n;if(!t){return}E(e,Math.max(t.width*o,1),Math.max(t.height*o,1));let s=i?-e.width:0,d=a?-e.height:0;null===(r=e.getContext("2d"))||void 0===r||r.scale(i?-1:1,a?-1:1),null===(n=e.getContext("2d"))||void 0===n||n.drawImage(t,s,d,e.width,e.height)},[i,a,t,o]);return n?s:t})(n,Z||ne,ae),pe=((e,t)=>{const[r,i]=useSizedImage(e.maskSrc||""),a=e.maskSrc?i:"loaded";return useImageLoader(a,e.maskSrc||"",e.id),o.useMemo(()=>{if(!r){return t}if(!t||!t.width||!t.height){return t}const{width:o,height:i}=$(t,{width:e.a.width,height:e.a.height,stretchEnabled:e.stretchEnabled}),a=document.createElement("canvas");a.width=o,a.height=i;const n=a.getContext("2d");if(!n){return t}n.drawImage(t,0,0,o,i,0,0,o,i),n.globalCompositeOperation="source-in";const s=function(e,t){const r=t.width/t.height;let o,i;return r>=e.width/e.height?(o=e.width,i=e.width/r):(o=e.height*r,i=e.height),{x:(e.width-o)/2,y:(e.height-i)/2,width:o,height:i}}(r,e);return n.drawImage(r,s.x,s.y,s.width,s.height,0,0,o,i),a},[t,r,e.a.width,e.a.height,e.stretchEnabled])})(n,ue)||ge,[fe=ge]=j(n,pe);o.useLayoutEffect(()=>{"loaded"===re&&n._setSourceSize({width:fe.width,height:fe.height},q)},[n,q,re,fe.width,fe.height]);let{cropX:ve,cropY:we,cropWidth:xe,cropHeight:ye}=n.a;"loaded"!==ee&&(ve=we=0,xe=ye=1);const Me=x({stretchEnabled:n.stretchEnabled,fit:n.a.fit,focus:n.focus,cropX:ve,cropY:we,cropWidth:xe,cropHeight:ye,width:n.a.width,height:n.a.height},fe),Se=Me.width,be=Me.height,Ee=null!==(b=n.cornerRadius)&&void 0!==b?b:0,Ie=w({borderSize:n.borderSize,borderColor:i(n.borderColor),width:n.a.width,height:n.a.height,opacity:S(n)}),Ce=!(!Ee&&!Ie&&!n.clipSrc&&F(Me,fe)),je=(null===(f=n.page)||void 0===f?void 0:f._exportingOrRendering)||!Ce?1:Math.min(2,l.scale),Re=l._elementsPixelRatio*je,ze=W||n._cropModeEnabled||"svg"===n.type,De=P(n.page)&&0===n.a.rotation&&"group"!==(null===(v=n.parent)||void 0===v?void 0:v.type)&&!ze,ke=((e,t,r,o)=>U(e.a,t,r,o))(n,Re,B(n,Me,Re),De);let Oe=((e,t,r,i)=>{const[a,n]=useClipImage(e.clipSrc||""),s=e.clipSrc?n:"loaded";useImageLoader(s,e.clipSrc,e.id);const d=o.useMemo(()=>{if(t&&a){return document.createElement("canvas")}},[t,a]);return o.useLayoutEffect(()=>{var o;if(!a){return}if(!t||!t.width||!t.height){return}if(!a||!a.width||!a.height){return}if(!d){return}const i=document.createElement("canvas"),n=Math.max(e.a.width/a.width*r,e.a.height/a.height*r);E(i,Math.min(1e4,Math.max(a.width*n,1)),Math.min(1e4,Math.max(a.height*n,1))),null===(o=i.getContext("2d"))||void 0===o||o.drawImage(a,0,0,i.width,i.height),E(d,Math.min(1e4,Math.max(t.width,Math.ceil(e.a.width*r),1)),Math.min(1e4,Math.max(t.height,Math.ceil(e.a.height*r),1)));const s=d.getContext("2d");s&&(s.save(),s.drawImage(i,0,0,d.width,d.height),u.Util.releaseCanvas(i),s.globalCompositeOperation="source-in",s.drawImage(t,0,0,d.width,d.height),s.restore())},[d,t,a,e.a.width,e.a.height,r,...i]),o.useEffect(()=>()=>{d&&u.Util.releaseCanvas(d)},[d]),e.clipSrc&&a?d:t})(n,useCornerRadiusAndCrop(n,fe,Me,Re,Ee,ze,!0,Ie,ke),Re,[Me,Ee,Re]);var _e=o.useRef(null),Le=o.useRef(0),Te=o.useRef(!1);const Xe=()=>{var e;n._cropEnabled&&!n.pendingEdit&&(null===(e=l.activeTool)||void 0===e?void 0:e.elementId)!==n.id&&setTimeout(()=>{s(n)&&n.toggleCropMode(!0)})},Ye=se(n)&&ne,Ae=oe&&"loading"===te&&!Ye,[We]=_(Ae,100,!1,!1),He="failed"===re,Pe=!We&&!He,Ne=C(n,Me,"loaded"===ee?fe:void 0,H),Ue=Pe?M(n):0;z(N,Ue),D(N,n,Oe);const Fe=n.selectable||l.role===c.ADMIN,Ge=L(n,n.borderColor,"stroke",n.borderSize/2);return r(e,{children:[We&&t(m,{element:n}),He&&t(g,{element:n,text:"Can not load the image..."}),t(k,Object.assign({as:d,nodeRef:N,element:n,name:"element",id:n.id,image:Oe},ke.position,{width:ke.width||1,height:ke.height||1,opacity:Ue},a(n),{customCrop:Me,listening:Fe,draggable:!n.pendingEdit&&(null===(y=l.activeTool)||void 0===y?void 0:y.elementId)!==n.id&&(G?n.draggable&&n.isSelected:n.draggable),preventDefault:!G||n.isSelected,hideInExport:!n.showInExport,onDragMove:e=>{n.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{n.set(I(e.target))},onDblClick:Xe,onDblTap:Xe},Ne)),!!n.borderSize&&t(k,Object.assign({as:h,element:n},ke.position,{width:Math.max(ke.width-n.borderSize,0),height:Math.max(ke.height-n.borderSize,0),opacity:Ue,offsetX:-n.borderSize/2,offsetY:-n.borderSize/2},Ge,{strokeWidth:n.borderSize,listening:!1,cornerRadius:Math.max(0,Ee-n.borderSize),hideInExport:!n.showInExport})),n._cropModeEnabled&&"loaded"===re&&t(Y,{element:n,store:l,previewImage:Oe,sourceImage:fe,sourceWidth:fe.width,sourceHeight:fe.height,crop:Me,onSourceTouchMove:e=>{e.evt.preventDefault();const t=e.target.getStage().getPointersPositions();var r=t[0],o=t[1];const i=e.target;if(r&&!o&&!i.isDragging()&&Te.current&&(i.startDrag(),Te.current=!1),r&&o){u.hitOnDragEnabled=!0,i.isDragging()&&(Te.current=!0,i.stopDrag());const t=e.target.getAbsoluteTransform().copy();t.invert();var a={x:r.x,y:r.y},s={x:o.x,y:o.y};if(!_e.current){return void(_e.current=ce(a,s))}var d=ce(a,s),h=function(e,t){return Math.sqrt(Math.pow(t.x-e.x,2)+Math.pow(t.y-e.y,2))}(a,s);Le.current||(Le.current=h);const v=i.position();var c={x:d.x-v.x,y:d.y-v.y},g=h/Le.current;i.scaleX(g),i.scaleY(g);const w=t.point(d),x=t.point(_e.current);var m=w.x-x.x,p=w.y-x.y,f={x:Math.min(0,d.x-c.x*g+m),y:Math.min(0,d.y-c.y*g+p)};i.position(f),Le.current=h,_e.current=d,(e=>{T(e,n,X(n,l),fe.width,fe.height,Se,be)})(e)}},onSourceTouchEnd:e=>{Le.current=0,_e.current=null,u.hitOnDragEnabled=!1}}),"image"===n.type&&t(A,{element:n,store:l})]})});
@@ -1 +1 @@
1
- import{Fragment as t,jsx as e,jsxs as o}from"react/jsx-runtime";import{elementOpacity as r}from"./paint-opacity.js";import i from"react";import{animatedShadow as n}from"./animated-shadow.js";import{computed as a}from"mobx";import{observer as s}from"mobx-react-lite";import{Group as l,Line as h,Rect as c,Circle as d}from"react-konva";import{ROLES as g}from"../model/store.js";import{useColor as m}from"./use-color.js";import{getLineHeadGeometry as p}from"@polotno/core/line-head";import{Portal as x}from"react-konva-utils";import{useAnchorSnap as f}from"./use-transformer-snap.js";import{useWorkspaceStyle as u}from"./workspace-style.js";import{isTouchDevice as y}from"../utils/screen.js";import{roundedGeometryAttrs as M,roundedPosition as b,transformScale as v}from"../utils/px.js";export function getMiddlePoints(t){const{x:e,y:o,width:r,height:i,rotation:n}=t.a,a=n*Math.PI/180,s={x:e+i/2*Math.cos(a+Math.PI/2),y:o+i/2*Math.sin(a+Math.PI/2)};return{middleLeft:s,middleRight:{x:s.x+r*Math.cos(a),y:s.y+r*Math.sin(a)}}}export function getLinePositionFromMiddlePoints(t,e,o){const r=Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),i=Math.atan2(e.y-t.y,e.x-t.x),n=180*i/Math.PI;return{x:t.x+Math.cos(i-Math.PI/2)*o/2,y:t.y+Math.sin(i-Math.PI/2)*o/2,width:r,height:o,rotation:n}}const j=(t,e,o)=>{const r=o.anchorSize;return{offsetX:r/2/t,offsetY:r/2/t,width:r/t,height:r/t,cornerRadius:o.anchorCornerRadius/t,fill:o.anchorFill,stroke:o.anchorStroke,strokeWidth:o.anchorStrokeWidth,strokeScaleEnabled:!1,draggable:!0,dragDistance:0,onMouseEnter:t=>{t.target.getStage().container().style.cursor="crosshair"},onMouseLeave:t=>{t.target.getStage().container().style.cursor=""},onDragStart:t=>{e.history.startTransaction()},onDragEnd:t=>{e.history.endTransaction()}}},k=s(({element:t,type:o})=>{const i=m(t,t.a.color,"fill"),n=m(t,t.a.color,"stroke"),a=p(o,t.height);if(!a){return null}const s=Object.assign(Object.assign(Object.assign({strokeWidth:t.height,lineCap:"round",lineJoin:"round"},n),i),{opacity:r(t),hideInExport:!t.showInExport});return"circle"===a.kind?e(d,Object.assign({x:a.cx,y:a.cy,radius:a.r},s)):e(h,Object.assign({points:a.points.flat(),closed:a.filled},s))});export const LineElement=s(({element:s,store:d})=>{const{transformerStyle:w}=u(),E=i.useRef(null),P=i.useRef(null),I=i.useRef(null),S=s.selectable||d.role===g.ADMIN,D=y(),O=i.useMemo(()=>a(()=>1===d.selectedElements.length&&d.selectedElements[0]===s),[d,s]).get(),{middleLeft:L,middleRight:R}=getMiddlePoints(s);f(P,[E],[O]),f(I,[E],[O]);const T=m(s,s.a.color,"stroke");return o(t,{children:[e(h,Object.assign({ref:E,name:"element",id:s.id,x:s.a.x,y:s.a.y,points:[0,0,s.a.width,0],offsetY:-s.a.height/2,strokeWidth:s.a.height,hitStrokeWidth:Math.max(s.a.height,20),dash:s.dash.map(t=>t*s.a.height)},T,{rotation:s.a.rotation,opacity:r(s)},n(s),{listening:S,draggable:D?s.draggable&&s.isSelected:s.draggable,preventDefault:!D||s.isSelected,hideInExport:!s.showInExport,onDragMove:t=>{s.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{s.set(b(t.target))},onTransform:t=>{const e=v(t.target.scaleX());t.target.scaleX(1),t.target.scaleY(1),s.set({height:s.height*e,width:s.width*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation()})},onTransformEnd:()=>{s.set(M(s))}})),p(s.startHead,s.height)&&e(l,{x:L.x,y:L.y,rotation:s.rotation,hideInExport:!s.showInExport,onClick:t=>{var e;return null===(e=E.current)||void 0===e?void 0:e.fire("click",t)},onTap:t=>{var e;return null===(e=E.current)||void 0===e?void 0:e.fire("tap",t)},children:e(k,{element:s,type:s.startHead})}),p(s.endHead,s.height)&&e(l,{x:R.x,y:R.y,rotation:s.rotation+180,onClick:t=>{var e;return null===(e=E.current)||void 0===e?void 0:e.fire("click",t)},onTap:t=>{var e;return null===(e=E.current)||void 0===e?void 0:e.fire("tap",t)},children:e(k,{element:s,type:s.endHead})}),O&&s.resizable&&e(x,{selector:".page-abs-container",enabled:!0,children:o(l,{visible:O,children:[e(c,Object.assign({x:L.x,y:L.y,ref:P,name:"line-anchor"},j(d.scale,d,w),{onDragMove:t=>{const e=getLinePositionFromMiddlePoints(t.target.position(),R,s.height);s.set({x:e.x,y:e.y,width:e.width,rotation:e.rotation})}})),e(c,Object.assign({x:R.x,y:R.y,ref:I,name:"line-anchor"},j(d.scale,d,w),{onDragMove:t=>{const e=getLinePositionFromMiddlePoints(L,t.target.position(),s.height);s.set({x:e.x,y:e.y,width:e.width,rotation:e.rotation})}}))]})})]})});
1
+ import{Fragment as t,jsx as e,jsxs as o}from"react/jsx-runtime";import{elementOpacity as r}from"./paint-opacity.js";import i from"react";import{animatedShadow as n}from"./animated-shadow.js";import{computed as a}from"mobx";import{observer as s}from"mobx-react-lite";import{Group as l,Line as h,Rect as c,Circle as d}from"react-konva";import{ROLES as g}from"../model/store.js";import{useColor as m}from"./use-color.js";import{getLineHeadGeometry as p,lineHeadReach as x}from"@polotno/core/line-head";import{Portal as f}from"react-konva-utils";import{useAnchorSnap as u}from"./use-transformer-snap.js";import{useWorkspaceStyle as y}from"./workspace-style.js";import{isTouchDevice as M}from"../utils/screen.js";import{roundedGeometryAttrs as b,roundedPosition as v,transformScale as j}from"../utils/px.js";export function getMiddlePoints(t){const{x:e,y:o,width:r,height:i,rotation:n}=t.a,a=n*Math.PI/180,s={x:e+i/2*Math.cos(a+Math.PI/2),y:o+i/2*Math.sin(a+Math.PI/2)};return{middleLeft:s,middleRight:{x:s.x+r*Math.cos(a),y:s.y+r*Math.sin(a)}}}export function getLinePositionFromMiddlePoints(t,e,o){const r=Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)),i=Math.atan2(e.y-t.y,e.x-t.x),n=180*i/Math.PI;return{x:t.x+Math.cos(i-Math.PI/2)*o/2,y:t.y+Math.sin(i-Math.PI/2)*o/2,width:r,height:o,rotation:n}}const k=(t,e,o)=>{const r=o.anchorSize;return{offsetX:r/2/t,offsetY:r/2/t,width:r/t,height:r/t,cornerRadius:o.anchorCornerRadius/t,fill:o.anchorFill,stroke:o.anchorStroke,strokeWidth:o.anchorStrokeWidth,strokeScaleEnabled:!1,draggable:!0,dragDistance:0,onMouseEnter:t=>{t.target.getStage().container().style.cursor="crosshair"},onMouseLeave:t=>{t.target.getStage().container().style.cursor=""},onDragStart:t=>{e.history.startTransaction()},onDragEnd:t=>{e.history.endTransaction()}}},w=s(({element:t,type:o})=>{const i=m(t,t.a.color,"fill"),n=m(t,t.a.color,"stroke",x(o,t.height)),a=p(o,t.height);if(!a){return null}const s=Object.assign(Object.assign(Object.assign({strokeWidth:t.height,lineCap:"round",lineJoin:"round"},n),i),{opacity:r(t),hideInExport:!t.showInExport});return"circle"===a.kind?e(d,Object.assign({x:a.cx,y:a.cy,radius:a.r},s)):e(h,Object.assign({points:a.points.flat(),closed:a.filled},s))});export const LineElement=s(({element:s,store:d})=>{const{transformerStyle:x}=y(),E=i.useRef(null),P=i.useRef(null),I=i.useRef(null),S=s.selectable||d.role===g.ADMIN,D=M(),O=i.useMemo(()=>a(()=>1===d.selectedElements.length&&d.selectedElements[0]===s),[d,s]).get(),{middleLeft:L,middleRight:R}=getMiddlePoints(s);u(P,[E],[O]),u(I,[E],[O]);const T=m(s,s.a.color,"stroke",s.a.height/2);return o(t,{children:[e(h,Object.assign({ref:E,name:"element",id:s.id,x:s.a.x,y:s.a.y,points:[0,0,s.a.width,0],offsetY:-s.a.height/2,strokeWidth:s.a.height,hitStrokeWidth:Math.max(s.a.height,20),dash:s.dash.map(t=>t*s.a.height)},T,{rotation:s.a.rotation,opacity:r(s)},n(s),{listening:S,draggable:D?s.draggable&&s.isSelected:s.draggable,preventDefault:!D||s.isSelected,hideInExport:!s.showInExport,onDragMove:t=>{s.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{s.set(v(t.target))},onTransform:t=>{const e=j(t.target.scaleX());t.target.scaleX(1),t.target.scaleY(1),s.set({height:s.height*e,width:s.width*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation()})},onTransformEnd:()=>{s.set(b(s))}})),p(s.startHead,s.height)&&e(l,{x:L.x,y:L.y,rotation:s.rotation,hideInExport:!s.showInExport,onClick:t=>{var e;return null===(e=E.current)||void 0===e?void 0:e.fire("click",t)},onTap:t=>{var e;return null===(e=E.current)||void 0===e?void 0:e.fire("tap",t)},children:e(w,{element:s,type:s.startHead})}),p(s.endHead,s.height)&&e(l,{x:R.x,y:R.y,rotation:s.rotation+180,onClick:t=>{var e;return null===(e=E.current)||void 0===e?void 0:e.fire("click",t)},onTap:t=>{var e;return null===(e=E.current)||void 0===e?void 0:e.fire("tap",t)},children:e(w,{element:s,type:s.endHead})}),O&&s.resizable&&e(f,{selector:".page-abs-container",enabled:!0,children:o(l,{visible:O,children:[e(c,Object.assign({x:L.x,y:L.y,ref:P,name:"line-anchor"},k(d.scale,d,x),{onDragMove:t=>{const e=getLinePositionFromMiddlePoints(t.target.position(),R,s.height);s.set({x:e.x,y:e.y,width:e.width,rotation:e.rotation})}})),e(c,Object.assign({x:R.x,y:R.y,ref:I,name:"line-anchor"},k(d.scale,d,x),{onDragMove:t=>{const e=getLinePositionFromMiddlePoints(L,t.target.position(),s.height);s.set({x:e.x,y:e.y,width:e.width,rotation:e.rotation})}}))]})})]})});