polotno 3.22.0 → 3.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/THIRD_PARTY_NOTICES.md +7 -7
- package/blueprint/toolbar/color-picker.js +2 -2
- package/canvas/element.js +1 -1
- package/canvas/fade-overlay.d.ts +5 -0
- package/canvas/fade-overlay.js +1 -1
- package/canvas/html-element.js +1 -1
- package/canvas/image-element.js +1 -1
- package/canvas/line-element.js +1 -1
- package/canvas/page.js +1 -1
- package/canvas/quill-font-formats.d.ts +1 -0
- package/canvas/quill-font-formats.js +1 -1
- package/canvas/radial-gradient-handle.d.ts +14 -0
- package/canvas/radial-gradient-handle.js +1 -0
- package/canvas/render-tag-element.js +1 -1
- package/canvas/rich-editor.js +12 -6
- package/canvas/text-element/legacy-padding-warning.d.ts +6 -0
- package/canvas/text-element/legacy-padding-warning.js +1 -0
- package/canvas/text-element/max-font-size.js +1 -1
- package/canvas/text-element.js +1 -1
- package/canvas/use-color.d.ts +24 -2
- package/canvas/use-color.js +1 -1
- package/icons/registry.d.ts +1 -0
- package/icons/registry.js +1 -1
- package/model/group-model.d.ts +26 -10
- package/model/group-model.js +1 -1
- package/model/node-model.d.ts +10 -1
- package/model/node-model.js +1 -1
- package/model/page-model.d.ts +75 -33
- package/model/page-model.js +1 -1
- package/model/table-model.d.ts +29 -9
- package/model/table-model.js +1 -1
- package/model/text-model.d.ts +5 -1
- package/model/text-model.js +1 -1
- package/package.json +16 -16
- package/polotno.bundle.js +194 -202
- package/primitives/field-group.js +1 -1
- package/primitives/popover.d.ts +1 -1
- package/primitives/popover.js +1 -1
- package/side-panel/effects-panel.js +1 -1
- package/side-panel/side-panel.d.ts +1 -0
- package/side-panel/side-panel.js +1 -1
- package/side-panel/templates-panel.js +1 -1
- package/toolbar/color-picker.js +1 -1
- package/toolbar/gradient-center-pad.d.ts +11 -0
- package/toolbar/gradient-center-pad.js +1 -0
- package/toolbar/padding-control.d.ts +29 -0
- package/toolbar/padding-control.js +1 -0
- package/toolbar/selection-colors.js +1 -1
- package/toolbar/stop-track.js +1 -1
- package/toolbar/table-toolbar.js +1 -1
- package/toolbar/text-toolbar.d.ts +7 -2
- package/toolbar/text-toolbar.js +1 -1
- package/toolbar/unit-numeric-input.d.ts +5 -0
- package/toolbar/unit-numeric-input.js +1 -0
- package/toolbar/use-copy-style.js +1 -1
- package/ui.css +1 -1
- package/utils/clipboard.d.ts +10 -2
- package/utils/clipboard.js +1 -1
- package/utils/gradient-handle.d.ts +18 -0
- package/utils/gradient-handle.js +1 -0
- package/utils/gradient-stops.d.ts +7 -2
- package/utils/gradient-stops.js +1 -1
- package/utils/history-gesture.d.ts +7 -0
- package/utils/history-gesture.js +1 -0
- package/utils/l10n.d.ts +16 -1
- package/utils/l10n.js +1 -1
- package/utils/measure-text.d.ts +4 -0
- package/utils/measure-text.js +1 -1
- package/utils/px.d.ts +4 -0
- package/utils/px.js +1 -1
- package/utils/rich-text-html.d.ts +1 -0
- package/utils/rich-text-html.js +1 -1
- package/utils/selection-transform.js +1 -1
- package/utils/validate-key.js +1 -1
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
|
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
|
-
`,
|
|
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;
|
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},
|
|
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;
|
package/canvas/fade-overlay.d.ts
CHANGED
|
@@ -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;
|
package/canvas/fade-overlay.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Fragment as
|
|
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/html-element.js
CHANGED
|
@@ -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 A}from"./workspace-style.js";import{detectSize as P,detectLineRects as $,htmlToCanvas as T}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";export{ht as quillRef,ut as createQuill,gt as setQuillContent,mt as setQuillFormats,pt as syncQuillFormatState,xt as setCursorFromCoords};function bt(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),wt=navigator.userAgent.includes("Firefox");export const HTMLElement=a(({element:a,store:ht})=>{var ut,gt,mt;const{textOverflowIndicatorStyle:pt}=A(),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),At=a._editModeEnabled,Pt=At&&!(null===(ut=a.page)||void 0===ut?void 0:ut._exporting);B(xt);const $t=It?a.fontFamily:Yt!==a.fontFamily?Yt:"Arial",Tt=C(a).fill,Wt=tt(I(a),{fontFamily:$t,color:Tt,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(()=>P(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||At||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 T(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),qt=r.useRef(0),Vt=r.useRef(null);r.useEffect(()=>{Et||Pt||(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:T;try{if(o=await e({skipCache:n>0,html:Bt,width:Lt||a.width||1,height:nt(a,Jt)||1,fontFamily:$t,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===$t)||q.globalFonts.find(t=>t.fontFamily===$t)}),t!==qt.current){return}if((yt||wt)&&o&&bt(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,Pt,$t,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=$(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=At&&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:At?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:$t,color:Tt,textOverflow:E(a)}),s=P(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&&!At,opacity:n(a)},pe,{shadowEnabled:me,hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled||Et||Ut})),(Ut&&!At||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}})})}),At&&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})})})]})});
|
package/canvas/image-element.js
CHANGED
|
@@ -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 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",n.borderSize/2);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})]})});
|
package/canvas/line-element.js
CHANGED
|
@@ -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
|
|
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})}}))]})})]})});
|
package/canvas/page.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=this&&this.__rest||function(e,t){var n={};for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++){t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(n[i[o]]=e[i[o]])}}return n};import{Fragment as t,jsx as n,jsxs as i}from"react/jsx-runtime";import o from"react";import{clamp as r}from"../lib/utils.js";import{observer as l,useLocalObservable as s}from"mobx-react-lite";import{action as a,runInAction as c}from"mobx";import{isAlive as d}from"mobx-state-tree";import{ZERO_BLEED as m}from"@polotno/schema";import{Group as u,Image as h,Label as g,Layer as f,Line as p,Rect as x,Shape as v,Stage as b,KonvaRenderer as y,Tag as w,Text as E,Transformer as S}from"react-konva";import{useWorkspaceStyle as X}from"./workspace-style.js";import{ROLES as Y}from"../model/store.js";import k from"konva";import A from"./element.js";import{DrawingLayer as M}from"./drawing-layer.js";import{ensureDragOrder as j,useSnap as I}from"./use-transformer-snap.js";import{useImageLoader as P,useSizedImage as O}from"./image-element.js";import{FadeOverlay as R}from"./fade-overlay.js";import{getCrop as T}from"../utils/crop.js";import{cloneBehind as D}from"../utils/duplicate.js";import{___ as B,____ as C,isCreditVisible as W}from"../utils/validate-key.js";import{getClientRect as G,getCorners as L,getTotalClientRect as V,polygonsIntersect as F}from"../utils/math.js";import{pxToUnitRounded as z,pxToUnitString as _}from"../utils/unit.js";import{isCornerAnchor as U,isRotateAnchor as Z,MAIN_TRANSFORMER_NAME as H}from"../utils/px.js";import{canStretchAxis as J,isAxisAligned as N,leafMinSize as K}from"../utils/selection-transform.js";import{restoreHeldPoint as $}from"./transform-box.js";import{flags as Q}from"../utils/flags.js";import{textOverflowOf as q}from"../utils/text-sizing.js";import{useColor as ee}from"./use-color.js";import{isGradient as te}from"@polotno/core/gradient";import{toDisplayColor as ne}from"@polotno/core/color";import{Html as ie}from"react-konva-utils";import{getParentGroupId as oe,withoutDescendants as re}from"../utils/selection.js";import{nodeReaction as le}from"../utils/mobx.js";import{useEvent as se}from"../hooks/use-event.js";const ae=o.version.startsWith("18.")?o.forwardRef((e,t)=>(o.useLayoutEffect(()=>{y.flushSync()}),n(b,Object.assign({},e,{ref:t})))):b,ce={enabledAnchors:["top-left","top-center","top-right","middle-left","bottom-left","bottom-right","bottom-center","middle-right"],rotateEnabled:!0,rotationSnaps:[0,45,90,135,180,225,270,315],ignoreStroke:!0,flipEnabled:!1,keepRatio:!0},de=e=>{if(!e.length){return null}const t=N(e[0].rotation)?0:e[0].rotation;return e.every(e=>N(e.rotation-t))?t:null},me=e=>e.selectedShapes.filter(t=>{var n;return(null===(n=e.activeTool)||void 0===n?void 0:n.elementId)!==t.id&&!t.pendingEdit}),ue=e=>e.selectedShapes.some(e=>!e.resizable),he=e=>e.selectedShapes.some(e=>!e.draggable),ge=e=>{var t;return 1!==e.selectedElements.length||"group"===(null===(t=e.selectedElements[0])||void 0===t?void 0:t.type)},fe=e=>{var t;const n=me(e),i=ge(e),o=null!==(t=de(n))&&void 0!==t?t:0,r=e=>i&&n.length>0&&n.every(t=>J(t,e,o));return{boxRotation:o,canStretchX:r("x"),canStretchY:r("y")}},pe={text:{enabledAnchors:["top-left","top-right","middle-left","bottom-left","bottom-right","middle-right"]},svg:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},gif:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},lottie:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},line:{enabledAnchors:[],borderEnabled:!1,rotateEnabled:!1},image:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},many:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},group:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},table:{keepRatio:!1,enabledAnchors:["top-left","top-center","top-right","middle-left","middle-right","bottom-left","bottom-center","bottom-right"]}};export function registerTransformerAttrs(e,t){pe[e]=pe[e]||t,Object.assign(pe[e],t)}const xe=e=>n(x,Object.assign({},e,{preventDefault:!1})),ve=t=>{var{url:i}=t,o=e(t,["url"]);const[r,l]=O(i),s=r?T(r,{width:o.width,height:o.height},"center-middle"):{};return P(l,i,"page background"),n(h,Object.assign({image:r},o,s))},be=e=>{const t=ee({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return n(x,Object.assign({},e,t))},ye=t=>{var{background:i,transparencyBackgroundColor:r}=t,l=e(t,["background","transparencyBackgroundColor"]);const s=o.useMemo(()=>{if(te(i)){return!0}const e=k.Util.colorToRGBA(i);return!e||e.a<1},[i]),a=o.useMemo(()=>{if(!s||r){return}const e=document.createElement("canvas"),t=30;e.width=60,e.height=60;const n=e.getContext("2d");return n&&(n.fillStyle="black",n.fillRect(t,0,t,t),n.fillRect(0,t,t,t)),e},[s,r]);return s?n(x,r?Object.assign({fill:r},l,{hideInExport:!0}):Object.assign({fillPatternImage:a},l,{opacity:.1,hideInExport:!0})):null},we=t=>{var{background:i}=t,r=e(t,["background"]);return o.useMemo(()=>!!k.Util.colorToRGBA(i)||te(i),[i])?n(be,Object.assign({fill:i},r)):n(ve,Object.assign({url:i},r))},Ee=l(({selection:e})=>{const{selectionRectStyle:t}=X();return e.visible?n(x,{name:"selection",x:Math.min(e.x1,e.x2),y:Math.min(e.y1,e.y2),width:Math.abs(e.x1-e.x2),height:Math.abs(e.y1-e.y2),fill:t.fill,stroke:t.stroke,strokeWidth:t.strokeWidth}):null});function Se(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const Xe=l(({gesture:e,store:o})=>{const{transformLabelStyle:r}=X(),{x:l,y:s,width:a,height:c,rotation:d,anchor:m}=e.label;if(void 0===l||void 0===s||void 0===a||void 0===c||void 0===d||void 0===m){return null}const u=G({x:l,y:s,width:a,height:c,rotation:k.Util.radToDeg(d)}),h=null!==m&&Z(m),f=(c/2+70)*Math.cos(d-Math.PI/2),p=(c/2+70)*Math.sin(d-Math.PI/2),x=z({unit:o.unit,dpi:o.dpi,px:a/o.scale,precious:"px"===o.unit?0:1})+" x "+z({unit:o.unit,dpi:o.dpi,px:c/o.scale,precious:"px"===o.unit?0:1})+("px"===o.unit?"":" "+o.unit);return i(t,{children:[i(g,{x:(u.minX+u.maxX)/2+f,y:(u.minY+u.maxY)/2+p,offsetX:14,offsetY:14,visible:h,children:[n(w,{cornerRadius:5,fill:r.fill}),n(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:Se(d).toString()+"°"})]}),i(g,{x:(u.minX+u.maxX)/2,y:u.maxY+20,visible:!h,children:[n(w,{cornerRadius:5,fill:r.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),n(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:x})]})]})}),Ye=l(({store:e,gesture:t,transformerStyle:n,onChange:i})=>{var r;const l=e.selectedElements.some(e=>e.curveEnabled),s=e.selectedElements.some(e=>{var t;return!(null!==(t=e.filled.visible)&&void 0!==t?t:e.visible)}),a=JSON.stringify(me(e).map(e=>e.id)),c=!ge(e)&&e.selectedElements[0].keepRatio,d=!ge(e)&&"text"===(null===(r=e.selectedElements[0])||void 0===r?void 0:r.type)&&"fit"===q(e.selectedElements[0]),m=t.transforming?null:fe(e);return o.useLayoutEffect(()=>{i()},[e.selectedShapes,a,ue(e),s,he(e),l,c,d,t.transforming,null==m?void 0:m.canStretchX,null==m?void 0:m.canStretchY,null==m?void 0:m.boxRotation,n]),null}),ke=()=>{console.warn("Polotno warning: onClick callback is deprecated. Just stop using it. Polotno will do selection automatically.")},Ae=l(({page:e})=>{const t=e._exportingOrRendering||e._forceMount;return o.useEffect(()=>{if(t&&d(e)){return e.set({_exportRendered:!0}),()=>{d(e)&&e.set({_exportRendered:!1})}}},[e,t]),null}),Me=l(({elements:e,store:t})=>{const i=e.filter(e=>e.alwaysOnTop),r=e.filter(e=>!e.alwaysOnTop).concat(i);return n(o.Fragment,{children:r.map(e=>n(A,{store:t,element:e,onClick:ke},e.id))})});export const useContextMenu=({store:e})=>{const[t,n]=o.useState(!1),[i,r]=o.useState({x:0,y:0}),l=o.useCallback(e=>{n(!0),r(e)},[]);return{open:l,close:o.useCallback(()=>{n(!1)},[]),props:{isOpen:t,offset:i,setIsOpen:n}}};let je=null;export const registerNextDomDrop=e=>{je=e};const Ie=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),Pe=atob("I2MwMzkyYg=="),Oe=atob("djAuOS4y"),Re=e=>n(t,{children:i(g,{fill:Pe,height:200,children:[n(w,{fill:Pe}),n(E,Object.assign({},e,{fill:"white",text:Ie,height:void 0,padding:10,fontSize:20}))]})}),Te=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),De=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),Be=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),Ce=e=>{const t=1===C(),o=t?De:Te,r=t?230:195;return i(u,Object.assign({},e,{listening:!1,children:[n(x,{fill:Pe,width:r,height:24,cornerRadius:2}),n(E,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),t&&n(E,{text:Be,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default l(({store:t,page:l,width:d,height:h,pageControlsEnabled:b,components:y,altCloneEnabled:A,groupSelectionMode:P="drill",viewportSize:O,layout:T="vertical",tooltipSafeArea:z,transformerSafeArea:Z})=>{const J=X(),ee=J.transformerStyle,te=ce,ve=l.computedBleed,be=t.bleedVisible?ve:m,Se=l.computedWidth+be.left+be.right,ke=l.computedHeight+be.top+be.bottom,Ie=(d-Se*t.scale)/2,Pe=(h-ke*t.scale)/2,Te={x:-ve.left,y:-ve.top,width:l.computedWidth+ve.left+ve.right,height:l.computedHeight+ve.top+ve.bottom,background:ne(l.background),name:"page-background",preventDefault:!1},De={x:-be.left,y:-be.top,width:Se,height:ke},Be=o.useRef(null),We=o.useRef(null),Ge=o.useRef(null),Le=o.useRef(null),Ve=null==b||b,Fe=o.useRef(!1),[ze,_e]=o.useState(null),Ue=s(()=>({transforming:!1,label:{},setLabel(e){this.label=e},setTransforming(e){this.transforming=e}})),[Ze,He]=o.useState(0),Je=useContextMenu({store:t}),Ne=me(t),Ke=ge(t),$e=()=>{var n,i,o;const r=Be.current;if(!r){return[]}const l=r.getStage();if(!l){return[]}const s=me(t),a=new Set(s.map(e=>e.id)),c=new Map;a.size&&l.find(e=>{const t=e.id();return a.has(t)&&!c.has(t)&&c.set(t,e),!1});const d=s.map(e=>c.get(e.id)).filter(e=>!!e),m=1===t.selectedElements.length,u=m&&(null===(n=t.selectedElements[0])||void 0===n?void 0:n.type)||"many",h=pe[u],g=Object.assign(Object.assign({},te),h||{});h&&("svg"!==u&&"image"!==u&&"gif"!==u&&"lottie"!==u||t.selectedElements[0].keepRatio||(g.enabledAnchors=te.enabledAnchors),"text"!==u||!Q.textVerticalResizeEnabled&&"fit"!==q(t.selectedElements[0])||(g.enabledAnchors=null===(i=pe.text.enabledAnchors)||void 0===i?void 0:i.concat(["bottom-center","top-center"])),"text"===u&&m&&t.selectedElements[0].curveEnabled&&(g.enabledAnchors=pe.many.enabledAnchors));const f=fe(t);(f.canStretchX||f.canStretchY)&&(g.enabledAnchors=[...g.enabledAnchors||[],...f.canStretchX?["middle-left","middle-right"]:[],...f.canStretchY?["top-center","bottom-center"]:[]]);const{boundBoxFunc:p}=ee,x=e(ee,["boundBoxFunc"]);r.setAttrs(Object.assign(Object.assign(Object.assign({},g),x),{name:H})),ue(t)&&r.enabledAnchors([]),he(t)&&r.rotateEnabled(!1);const v=r.nodes();if(v.length===d.length&&d.every((e,t)=>v[t]===e)||r.nodes(d),ge(t)&&d.length){const e=de(s);null===e||N(r.rotation()-e)||(r.rotation(e),r.forceUpdate())}return null===(o=r.getLayer())||void 0===o||o.batchDraw(),d},Qe=se(()=>{var e;if(null===(e=Be.current)||void 0===e?void 0:e.isTransforming()){return}const t=$e();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),j())}),qe=()=>{var e,t;const n=Be.current;if(!n){return}if(!n.isTransforming()){return void Ue.setLabel({})}if(!n.nodes().length){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();Ue.setLabel({anchor:o,x:i.x-(null!==(e=null==r?void 0:r.x())&&void 0!==e?e:0),y:i.y-(null!==(t=null==r?void 0:r.y())&&void 0!==t?t:0),rotation:i.rotation,width:i.width,height:i.height})},et=o.useCallback(()=>{var e;if(null===(e=Be.current)||void 0===e?void 0:e.isTransforming()){return}const t=Be.current;if(!t||!t.nodes().length){return void He(0)}const n=t.getClientRect();if(!t.getStage()){return}const i=function(e,t,n,i=0){const o=e.x+e.width/2,r=e.y+e.height/2,l=e.width/2,s=e.height/2,a=i*Math.PI/180,c=e=>{const t=e*Math.PI/180,i=(e%360+360)%360;let c;c=0===i||180===i?s:l;const d=Math.sin(t)*(c+n),m=-Math.cos(t)*(c+n),u=d*Math.cos(a)-m*Math.sin(a),h=d*Math.sin(a)+m*Math.cos(a);return{x:o+u,y:r+h}},d=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(d(c(0))){return 0}for(const m of[180,270,90]){if(d(c(m))){return m}}return 0}(n,{top:(null==Z?void 0:Z.top)||0,bottom:h-((null==Z?void 0:Z.bottom)||0),left:(null==Z?void 0:Z.left)||0,right:d-((null==Z?void 0:Z.right)||0)},ee.rotateAnchorOffset,t.rotation());He(i)},[Z,d,h]);o.useEffect(()=>{var e;null===(e=Be.current)||void 0===e||e.update(),qe(),et()},[t.scale,et]),o.useEffect(()=>{et()},[t.selectedShapes,et]);const tt=s(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),nt=o.useRef(!1),it=e=>{const n=Ge.current;if(!n||t.role===Y.VIEWER||t.activePage!==l||t.isPlaying||"button"in e.evt&&0!==e.evt.button||1!==n.getPointersPositions().length||!e.target.findAncestor(".elements-container")||!e.target.findAncestor(".element",!0)||!t.selectedShapes.length||he(t)){return}const i=n.getPointerPosition();if(!i){return}const o=$e();if(o.some(t=>t===e.target||t.isAncestorOf(e.target))){return}const r=o.find(e=>!!(e.draggable()&&e.isVisible()&&e.isListening())&&(e instanceof k.Shape?[e]:e instanceof k.Container?e.find("Shape"):[]).some(e=>e.intersects(i)));if(r){for(const[t,n]of k.DD._dragElements){"ready"!==n.dragStatus||n.node!==e.target&&!n.node.isAncestorOf(e.target)||k.DD._dragElements.delete(t)}r._createDragElement(e),"touches"in e.evt&&e.evt.cancelable&&r.preventDefault()&&e.evt.preventDefault()}},ot=a(e=>{var n,i,o,r,l,s,a;if(t.role===Y.VIEWER){return}if(nt.current=!1,t.activeTool&&"crop"!==t.activeTool.name){return}const c=e.target.findAncestor(".elements-container"),d=e.target.findAncestor("Transformer"),m=e.target.findAncestor(".page-abs-container");if(c||d||m){return}const u=null===(n=e.target.getStage())||void 0===n?void 0:n.getPointerPosition();u&&(u.x-=null!==(l=null===(i=e.target.getStage())||void 0===i?void 0:i.x())&&void 0!==l?l:0,u.y-=null!==(s=null===(o=e.target.getStage())||void 0===o?void 0:o.y())&&void 0!==s?s:0,tt.visible=!0,tt.x1=u.x,tt.y1=u.y,tt.x2=u.x,tt.y2=u.y,(null!==(a=null===(r=e.target.getStage())||void 0===r?void 0:r.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(tt.visible=!1))});(({stageRef:e,containerRef:t,viewportSize:n})=>{o.useEffect(()=>{var n;const i=null===(n=t.current)||void 0===n?void 0:n.closest(".polotno-workspace-inner");function o(){var n;if(!e.current){return}const o=null===(n=t.current)||void 0===n?void 0:n.getBoundingClientRect(),l=null==i?void 0:i.getBoundingClientRect();if(!o||!l){return}const s=r(l.left-o.left-100,0,Math.max(0,o.width-e.current.width())),a=r(l.top-o.top-100,0,Math.max(0,o.height-e.current.height()));e.current.position({x:-s,y:-a}),e.current.container().style.transform=`translate(${s}px, ${a}px)`}return o(),null==i||i.addEventListener("scroll",o),()=>{null==i||i.removeEventListener("scroll",o)}},[n.width,n.height])})({stageRef:Ge,containerRef:Le,viewportSize:O}),o.useEffect(()=>{const e=a(e=>{var t,n,i,o,r,l;if(!tt.visible){return}null===(t=Ge.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=Ge.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(r=null===(i=Ge.current)||void 0===i?void 0:i.x())&&void 0!==r?r:0,s.y-=null!==(l=null===(o=Ge.current)||void 0===o?void 0:o.y())&&void 0!==l?l:0):s={x:tt.x2,y:tt.y2},tt.x2=s.x,tt.y2=s.y}),n=a(()=>{if(!tt.visible){return}if(!Ge.current){return}const e=Ge.current.findOne(".selection"),n=e?e.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(n.width&&n.height){const e=[{x:n.x,y:n.y},{x:n.x+n.width,y:n.y},{x:n.x+n.width,y:n.y+n.height},{x:n.x,y:n.y+n.height}],i=[];Ge.current.find(".element").forEach(n=>{var o;const r=t.getElementById(n.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=n.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const c=L(r.a).map(e=>a.point(e));F(e,c)&&i.push(r.top.id)});const o=[...new Set(i)];nt.current=!0,t.selectElements(o)}tt.visible=!1});return window.addEventListener("mousemove",e),window.addEventListener("touchmove",e),window.addEventListener("mouseup",n,{capture:!0}),window.addEventListener("touchend",n,{capture:!0}),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("touchmove",e),window.removeEventListener("mouseup",n,{capture:!0}),window.removeEventListener("touchend",n,{capture:!0})}},[]);const rt=o.useCallback(e=>{if(!t.selectedElements[0]){return}if(e&&t.selectedElementsIds.includes(e)){return}const n=V(t.selectedShapes),i=e?t.getElementById(e):{x:0,y:0,width:l.computedWidth,height:l.computedHeight,rotation:0},o=G(i),r=[];n.minX>o.maxX&&r.push({distance:n.minX-o.maxX,box1:n,box2:o,points:[{x:n.minX,y:n.minY+n.height/2},{x:o.maxX,y:n.minY+n.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),n.maxX<o.minX&&r.push({distance:o.minX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:n.minY+n.height/2},{x:o.minX,y:n.minY+n.height/2},{x:o.minX,y:o.minY+o.height/2}]}),n.minY>o.maxY&&r.push({box1:n,box2:o,distance:n.minY-o.maxY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]}),n.maxY<o.minY&&r.push({box1:n,box2:o,distance:o.minY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]});const s=n.minX>=o.minX&&n.maxX<=o.maxX&&n.minY>=o.minY&&n.maxY<=o.maxY;if(s&&(r.push({distance:n.minX-o.minX,box1:n,box2:o,points:[{x:n.minX,y:n.minY+n.height/2},{x:o.minX,y:n.minY+n.height/2},{x:o.minX,y:o.minY+o.height/2}]}),r.push({distance:o.maxX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:n.minY+n.height/2},{x:o.maxX,y:n.minY+n.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),r.push({box1:n,box2:o,distance:n.minY-o.minY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]}),r.push({box1:n,box2:o,distance:o.maxY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]})),0===r.length&&!s){const e=n.minX<o.maxX&&n.maxX>o.minX,t=n.minY<o.maxY&&n.maxY>o.minY;if(e&&t){const e=(Math.max(n.minY,o.minY)+Math.min(n.maxY,o.maxY))/2,t=(Math.max(n.minX,o.minX)+Math.min(n.maxX,o.maxX))/2;n.minX<o.minX?r.push({distance:o.minX-n.minX,box1:n,box2:o,points:[{x:n.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}):r.push({distance:n.minX-o.minX,box1:n,box2:o,points:[{x:n.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}),n.maxX>o.maxX?r.push({distance:n.maxX-o.maxX,box1:n,box2:o,points:[{x:o.maxX,y:e},{x:n.maxX,y:e},{x:o.maxX,y:e}]}):r.push({distance:o.maxX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:e},{x:o.maxX,y:e},{x:o.maxX,y:e}]}),n.minY<o.minY?r.push({box1:n,box2:o,distance:o.minY-n.minY,points:[{x:t,y:n.minY},{x:t,y:o.minY},{x:t,y:o.minY}]}):r.push({box1:n,box2:o,distance:n.minY-o.minY,points:[{x:t,y:n.minY},{x:t,y:o.minY},{x:t,y:o.minY}]}),n.maxY>o.maxY?r.push({box1:n,box2:o,distance:n.maxY-o.maxY,points:[{x:t,y:o.maxY},{x:t,y:n.maxY},{x:t,y:o.maxY}]}):r.push({box1:n,box2:o,distance:o.maxY-n.maxY,points:[{x:t,y:n.maxY},{x:t,y:o.maxY},{x:t,y:o.maxY}]})}}_e(e=>JSON.stringify(e)===JSON.stringify(r)?e:r)},[t,l]),lt=o.useCallback(()=>{t.distanceGuidesVisible&&t.selectedElements[0]?rt():_e(null)},[t,rt]);o.useEffect(lt,[t.distanceGuidesVisible,lt]);const st=t.distanceGuidesVisible||!!ze;o.useEffect(()=>{if(st){return le(l,()=>t.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|"),lt)}},[st,lt]);const at=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=Le.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{at.current=!0,clearTimeout(t),t=setTimeout(()=>{at.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const e=Ge.current;if(!e){return}const n=e.container(),i=new Map,o=e=>{var t,n;i.has(e.target)||[e.target,...null!==(n=null===(t=Be.current)||void 0===t?void 0:t.nodes())&&void 0!==n?n:[]].forEach(e=>{i.has(e)||i.set(e,{x:e.x(),y:e.y()})})},r=e=>{e.target===Be.current?i.clear():i.delete(e.target)};e.on("dragstart",o),e.on("dragend",r);const l=e=>{e.touches.length>=2&&c(()=>{var e,n;const o=new Map(i);(null===(e=Be.current)||void 0===e?void 0:e.isDragging())&&Be.current.stopDrag(),null===(n=Be.current)||void 0===n||n.nodes().forEach(e=>{var n;const i=o.get(e);e.isDragging()&&e.stopDrag(),i&&(e.setAttrs(i),null===(n=t.getElementById(e.id()))||void 0===n||n.set({x:i.x,y:i.y}))})})};return n.addEventListener("touchstart",l,{passive:!0}),n.addEventListener("touchmove",l,{passive:!0}),()=>{e.off("dragstart",o),e.off("dragend",r),n.removeEventListener("touchstart",l),n.removeEventListener("touchmove",l)}},[t]);const ct=e=>{if(t.role===Y.VIEWER){return}if(t.activePage!==l&&l.select(),at.current){return}if(nt.current){return}const n=e.evt.shiftKey,i=e.evt.ctrlKey||e.evt.metaKey,o=n||i,r=e.target.findAncestor(".elements-container"),s=e.target.findAncestor(".page-abs-container"),a=e.target.findAncestor("Transformer"),c=e.target.findAncestor(".page-container",!0),d=!(o||r||a||s||c||tt.visible);if(t.activeTool&&"crop"!==t.activeTool.name&&!r&&!a){return}if(d){return t.selectElements([]),void t.selectPages([])}if(i&&!n){const e=Ge.current,n=null==e?void 0:e.getPointerPosition();if(!e||!n){return}const i=e.getAllIntersections(n),o=new Set,r=[];for(const s of[...i].reverse()){const e=s.findAncestor(".element",!0);if(!e){continue}const n=t.getElementById(e.id()),i=null==n?void 0:n.top;i&&(o.has(i.id)||(o.add(i.id),r.push(i.id)))}if(0===r.length){return}let l;if(1===t.selectedElementsIds.length){const e=t.selectedElementsIds[0],n=r.indexOf(e);l=-1===n?r[0]:r[(n+1)%r.length]}else{l=r[0]}return t.selectElements([l]),void t.selectPages([])}const m=e.target.findAncestor(".element-container",!0),u=(null==m?void 0:m.findOne(".element"))||e.target.findAncestor(".element",!0),h=t.getElementById(null==u?void 0:u.id());h&&"pendingEdit"in h&&h._setImageEditError(null);const g=null==h?void 0:h.top;let f;if("group"===P){f=null==g?void 0:g.id}else if(g){const e=t.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===g.id});f=e?null==h?void 0:h.id:null==g?void 0:g.id}else{f=void 0}const p=null==h?void 0:h.id;if(p&&t.selectedElementsIds.indexOf(p)>=0&&!n){return}const x=!!f&&t.selectedElementsIds.indexOf(f)>=0,v=e.target.findAncestor(".page-container",!0);if(f&&n&&!x){const e=re(t.selectedElementsIds.concat([f]),e=>oe(t,e));t.selectElements(e),t.selectPages([])}else{f&&n&&x?t.selectElements(t.selectedElementsIds.filter(e=>e!==f)):!f||n||x?f&&!n&&x||(v?(n||t.selectElements([]),t.selectPages([l.id])):t.selectPages([])):(t.selectElements([f]),t.selectPages([]))}};I(Be,t);const dt=t.activePage===l,mt=t._selectedPagesIds.includes(l.id),ut=null==y?void 0:y.PageControls,ht=null==y?void 0:y.Tooltip,gt=null==y?void 0:y.ContextMenu,ft=1/t.scale,pt=0/t.scale;return i("div",{ref:Le,onDragOver:e=>e.preventDefault(),onDrop:e=>{if(e.preventDefault(),t.role===Y.VIEWER){return}if(!Ge.current){return}Ge.current.setPointersPositions(e);const n=Ge.current.findOne(".elements-container"),i=null==n?void 0:n.getRelativePointerPosition(),o=Ge.current.getPointerPosition(),r=Ge.current.getAllIntersections(o).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(r.reverse())].map(e=>t.getElementById(e.id())).filter(e=>!!e),a=s[0];je&&i&&(je(i,a,{elements:s,page:l}),je=null)},style:{position:"relative",width:d+"px",height:h+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+(dt?" active-page":""),children:[n(ae,{ref:Ge,eventBatchFunc:c,width:Math.min(d,O.width+200),height:Math.min(O.height+200,h),onClick:ct,onTap:ct,onContextMenu:e=>{if(e.evt.preventDefault(),t.role===Y.VIEWER){return}const n=e.target.findAncestor(".element",!0),i=t.getElementById(null==n?void 0:n.id()),o=null==i?void 0:i.top,r=null==o?void 0:o.id;if(!r){return t.selectElements([]),void Je.close()}t.selectedElementsIds.indexOf(r)>=0||t.selectElements([r]),Je.open({x:e.evt.clientX,y:e.evt.clientY})},onMouseDown:e=>{it(e),ot(e)},onTouchStart:it,onMouseMove:e=>{if(!t.distanceGuidesVisible&&!e.evt.altKey){return void(ze&&_e(null))}const n=e.target.findAncestor(".element",!0),i=null==n?void 0:n.id();rt(i)},onDragStart:e=>{var n;const i=e.target.getStage();if(i&&i.getPointersPositions().length>=2){return void e.target.stopDrag()}const o=e.target.findAncestor(".element",!0);if(o){const i=t.getElementById(null==o?void 0:o.id()),r=null==i?void 0:i.top,l=null==r?void 0:r.id;if(!(i&&t.selectedElementsIds.indexOf(i.id)>=0||l&&t.selectedElementsIds.indexOf(l)>=0)&&l){t.selectElements([l]);const i=$e();e.target.stopDrag(),i.forEach(t=>{t.isDragging()||t.startDrag(e)}),null===(n=Be.current)||void 0===n||n.startDrag(e),j()}}ze&&_e(null)},onDragMove:e=>{const n=Be.current;n&&e.target===n&&n.nodes().forEach(e=>{const n=t.getElementById(e.id());!n||n.x===e.x()&&n.y===e.y()||n.set({x:e.x(),y:e.y()})})},pageId:l.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:i(f,{children:[n(xe,{width:d,height:h,fill:J.workspaceBackgroundColor}),n(u,{x:Ie,y:Pe,scaleX:t.scale,scaleY:t.scale,name:"page-container",children:i(u,{name:"page-container-2",children:[n(u,{x:be.left,y:be.top,children:n(ye,Object.assign({},Te,De,{transparencyBackgroundColor:J.transparencyBackgroundColor}))}),i(u,{isolated:l.hasBlendMode,children:[n(u,{name:"page-background-group",x:be.left,y:be.top,clip:De,children:n(we,Object.assign({},Te))}),i(u,{x:be.left,y:be.top,name:"elements-container",listening:!t.isPlaying&&t.role!==Y.VIEWER,children:[n(x,{name:"elements-area",width:l.computedWidth,height:l.computedHeight,listening:!1}),n(Me,{elements:l.children,store:t}),n(Ae,{page:l})]})]}),n(v,{name:"bleed",fill:J.bleedColor,listening:!1,visible:t.bleedVisible&&(be.top>0||be.right>0||be.bottom>0||be.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:i,bottom:o,left:r}=ve,s=l.computedWidth,a=l.computedHeight,c=e._context;c.beginPath(),c.rect(0,0,r+s+i,n+a+o),c.rect(r,n,s,a),c.fillStyle=t.fill(),c.fill("evenodd")}}),B()===Oe&&n(Re,{name:"hit-detection",x:-ft/2-pt,y:-ft/2-pt,width:Se+ft+2*pt,height:ke+ft+2*pt})]})}),n(p,{name:"workspace-background",points:[0,0,d,0,d,h,0,h,0,0,Ie,Pe,Ie,h-Pe,d-Ie,h-Pe,d-Ie,Pe,Ie,Pe],listening:!1,closed:!0,fill:J.workspaceBackgroundColor}),n(u,{x:Ie,y:Pe,scaleX:t.scale,scaleY:t.scale,children:n(x,{name:"page-highlight",hideInExport:!0,x:-ft/2-pt,y:-ft/2-pt,width:Se+ft+2*pt,height:ke+ft+2*pt,stroke:mt?J.activePageBorderColor:J.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),i(u,{x:Ie+be.left*t.scale,y:Pe+be.top*t.scale,scaleX:t.scale,scaleY:t.scale,name:"page-abs-container",children:[n(S,{ref:Be,name:H,rotateAnchorAngle:Ze,onDragStart:e=>{var n;const i=e.target.getStage();i&&i.getPointersPositions().length>=2?e.target.stopDrag():((null===(n=e.evt)||void 0===n?void 0:n.altKey)&&A&&D(t.selectedElements,l,t),Fe.current||(Fe.current=!0,t.history.startTransaction()))},onDragEnd:()=>{Fe.current&&(t.history.endTransaction(),Fe.current=!1),et()},onTransformStart:()=>{Ue.setTransforming(!0),t.history.startTransaction(),We.current=new Map(Ne.map(e=>[e.id,{width:e.width,height:e.height}])),_e(null)},boundBoxFunc:(e,n)=>{var i,o,r,l;const s=null!==(l=null===(i=ee.boundBoxFunc)||void 0===i?void 0:i.call(ee,e,n))&&void 0!==l?l:n,a=Math.abs(s.width)<1||Math.abs(s.height)<1,c=Math.abs(e.width)<1||Math.abs(e.height)<1;if(a&&!c){return e}const d=t.selectedElements;if(1===d.length&&"table"===(null===(o=d[0])||void 0===o?void 0:o.type)){const t=d[0],n=Math.abs(e.width),i=Math.abs(e.height);if(n>0&&i>0){const o=Math.abs(s.width)/n,r=Math.abs(s.height)/i,l=t.width*o,a=t.height*r,c=Object.assign({},s);return l<t.minWidth&&(c.width=e.width),a<t.minHeight&&(c.height=e.height),$(c,s,e)}}return Ke&&Ne.length>0?((e,t,n,i,o)=>{const r=Math.abs(e.width),l=Math.abs(e.height);if(0===r||0===l){return t}const s=Math.abs(t.width)/r,a=Math.abs(t.height)/l,c=!U(n),d=e=>i.some(t=>{var n;const i=null==o?void 0:o.get(t.id),r=null!==(n="x"===e?null==i?void 0:i.width:null==i?void 0:i.height)&&void 0!==n?n:"x"===e?t.width:t.height,l="x"===e?t.width:t.height,d="x"===e?s:a,m=K(t,e,c,r);return r>=m&&l*d<m}),m=Object.assign({},t),u=t.width*e.width<0,h=t.height*e.height<0,g=u||d("x"),f=h||d("y"),p=f||!c&&g;return(g||!c&&f)&&(m.width=e.width),p&&(m.height=e.height),$(m,t,e)})(e,s,(null===(r=Be.current)||void 0===r?void 0:r.getActiveAnchor())||"",Ne,We.current):s},onTransform:e=>{const t=Be.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{qe()},0)},onTransformEnd:e=>{Ue.setTransforming(!1),Ue.setLabel({}),t.history.endTransaction(),We.current=null,et()},visible:!t.isPlaying}),n(Ye,{store:t,gesture:Ue,transformerStyle:ee,onChange:Qe}),ze&&ze.map(({points:e,distance:o,box1:r,box2:l},s)=>i(u,{name:"distances-container",hideInExport:!0,listening:!1,children:[n(x,Object.assign({},r,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),n(x,Object.assign({},l,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),n(p,{points:[e[0].x,e[0].y,e[1].x,e[1].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(p,{points:[e[1].x,e[1].y,e[2].x,e[2].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),i(g,{x:(e[0].x+e[1].x)/2,y:(e[0].y+e[1].y)/2,offsetY:-10,scaleX:1/t.scale,scaleY:1/t.scale,children:[n(w,{cornerRadius:5,fill:J.distanceGuideStyle.labelFill,pointerDirection:"down"}),n(E,{align:"center",verticalAlign:"middle",fill:J.distanceGuideStyle.labelTextFill,padding:5,text:_({unit:t.unit,dpi:t.dpi,px:o})})]})]},s)),l._rendering&&i(u,{children:[n(x,{width:Se,height:ke,fill:"rgba(255,255,255,0.9)"}),n(E,{text:"Rendering...",fontSize:60,width:Se,height:ke,align:"center",verticalAlign:"middle"})]}),ht&&n(ht,{components:y,store:t,page:l,stageRef:Ge,tooltipSafeArea:z}),gt&&n(ie,{children:n(gt,Object.assign({components:y,store:t},Je.props))}),n(R,{store:t,pageId:l.id})]}),n(M,{store:t,page:l,width:d,height:h,scale:t.scale,xPadding:Ie,yPadding:Pe,bleedLeft:be.left,bleedTop:be.top}),n(Xe,{gesture:Ue,store:t}),n(Ee,{selection:tt}),C()>0&&i(u,{hideInExport:!0,children:[n(Ce,{name:"cache-bounds",x:d-(1===C()?250:210),y:h-(1===C()?95:45)}),n(Ce,{name:"cache-bounds-t",x:10,y:10})]}),W()&&n(E,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:d-170,y:h-18,onMouseEnter:e=>{e.target.getStage().container().style.cursor="pointer"},onMouseLeave:e=>{e.target.getStage().container().style.cursor=""},onTouchStart:e=>{e.cancelBubble=!0},onMouseDown:e=>{e.cancelBubble=!0},onClick:()=>{window.open("https://polotno.com","_blank","noopener")},onTap:()=>{window.open("https://polotno.com","_blank","noopener")}}),n(u,{name:"line-guides"})]})}),Ve&&ut&&n(ut,{store:t,page:l,xPadding:Ie,yPadding:Pe,layout:T})]})});
|
|
1
|
+
var e=this&&this.__rest||function(e,t){var n={};for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++){t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(n[i[o]]=e[i[o]])}}return n};import{Fragment as t,jsx as n,jsxs as i}from"react/jsx-runtime";import o from"react";import{clamp as r}from"../lib/utils.js";import{observer as l,useLocalObservable as s}from"mobx-react-lite";import{action as a,runInAction as c}from"mobx";import{isAlive as d}from"mobx-state-tree";import{ZERO_BLEED as m}from"@polotno/schema";import{Group as u,Image as h,Label as g,Layer as f,Line as p,Rect as x,Shape as v,Stage as b,KonvaRenderer as y,Tag as w,Text as E,Transformer as S}from"react-konva";import{useWorkspaceStyle as X}from"./workspace-style.js";import{ROLES as Y}from"../model/store.js";import k from"konva";import A from"./element.js";import{DrawingLayer as j}from"./drawing-layer.js";import{ensureDragOrder as M,useSnap as I}from"./use-transformer-snap.js";import{useImageLoader as O,useSizedImage as P}from"./image-element.js";import{FadeOverlay as R}from"./fade-overlay.js";import{RadialGradientHandle as T}from"./radial-gradient-handle.js";import{getCrop as D}from"../utils/crop.js";import{cloneBehind as B}from"../utils/duplicate.js";import{___ as C,____ as W,isCreditVisible as G}from"../utils/validate-key.js";import{getClientRect as L,getCorners as V,getTotalClientRect as F,polygonsIntersect as z}from"../utils/math.js";import{pxToUnitRounded as _,pxToUnitString as U}from"../utils/unit.js";import{isCornerAnchor as Z,isRotateAnchor as H,MAIN_TRANSFORMER_NAME as J}from"../utils/px.js";import{canStretchAxis as N,isAxisAligned as K,leafMinSize as $}from"../utils/selection-transform.js";import{restoreHeldPoint as Q}from"./transform-box.js";import{flags as q}from"../utils/flags.js";import{textOverflowOf as ee}from"../utils/text-sizing.js";import{useColor as te}from"./use-color.js";import{isGradient as ne}from"@polotno/core/gradient";import{toDisplayColor as ie}from"@polotno/core/color";import{backgroundPaintBox as oe}from"@polotno/core/page";import{Html as re}from"react-konva-utils";import{getParentGroupId as le,withoutDescendants as se}from"../utils/selection.js";import{nodeReaction as ae}from"../utils/mobx.js";import{useEvent as ce}from"../hooks/use-event.js";const de=o.version.startsWith("18.")?o.forwardRef((e,t)=>(o.useLayoutEffect(()=>{y.flushSync()}),n(b,Object.assign({},e,{ref:t})))):b,me={enabledAnchors:["top-left","top-center","top-right","middle-left","bottom-left","bottom-right","bottom-center","middle-right"],rotateEnabled:!0,rotationSnaps:[0,45,90,135,180,225,270,315],ignoreStroke:!0,flipEnabled:!1,keepRatio:!0},ue=e=>{if(!e.length){return null}const t=K(e[0].rotation)?0:e[0].rotation;return e.every(e=>K(e.rotation-t))?t:null},he=e=>e.selectedShapes.filter(t=>{var n;return(null===(n=e.activeTool)||void 0===n?void 0:n.elementId)!==t.id&&!t.pendingEdit}),ge=e=>e.selectedShapes.some(e=>!e.resizable),fe=e=>e.selectedShapes.some(e=>!e.draggable),pe=e=>{var t;return 1!==e.selectedElements.length||"group"===(null===(t=e.selectedElements[0])||void 0===t?void 0:t.type)},xe=e=>{var t;const n=he(e),i=pe(e),o=null!==(t=ue(n))&&void 0!==t?t:0,r=e=>i&&n.length>0&&n.every(t=>N(t,e,o));return{boxRotation:o,canStretchX:r("x"),canStretchY:r("y")}},ve={text:{enabledAnchors:["top-left","top-right","middle-left","bottom-left","bottom-right","middle-right"]},svg:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},gif:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},lottie:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},line:{enabledAnchors:[],borderEnabled:!1,rotateEnabled:!1},image:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},many:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},group:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},table:{keepRatio:!1,enabledAnchors:["top-left","top-center","top-right","middle-left","middle-right","bottom-left","bottom-center","bottom-right"]}};export function registerTransformerAttrs(e,t){ve[e]=ve[e]||t,Object.assign(ve[e],t)}const be=e=>n(x,Object.assign({},e,{preventDefault:!1})),ye=t=>{var{url:i}=t,o=e(t,["url"]);const[r,l]=P(i),s=r?D(r,{width:o.width,height:o.height},"center-middle"):{};return O(l,i,"page background"),n(h,Object.assign({image:r},o,s))},we=e=>{const t=te({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return n(x,Object.assign({},e,t))},Ee=t=>{var{background:i,transparencyBackgroundColor:r}=t,l=e(t,["background","transparencyBackgroundColor"]);const s=o.useMemo(()=>{if(ne(i)){return!0}const e=k.Util.colorToRGBA(i);return!e||e.a<1},[i]),a=o.useMemo(()=>{if(!s||r){return}const e=document.createElement("canvas"),t=30;e.width=60,e.height=60;const n=e.getContext("2d");return n&&(n.fillStyle="black",n.fillRect(t,0,t,t),n.fillRect(0,t,t,t)),e},[s,r]);return s?n(x,r?Object.assign({fill:r},l,{hideInExport:!0}):Object.assign({fillPatternImage:a},l,{opacity:.1,hideInExport:!0})):null},Se=t=>{var{background:i}=t,r=e(t,["background"]);return o.useMemo(()=>!!k.Util.colorToRGBA(i)||ne(i),[i])?n(we,Object.assign({fill:i},r)):n(ye,Object.assign({url:i},r))},Xe=l(({selection:e})=>{const{selectionRectStyle:t}=X();return e.visible?n(x,{name:"selection",x:Math.min(e.x1,e.x2),y:Math.min(e.y1,e.y2),width:Math.abs(e.x1-e.x2),height:Math.abs(e.y1-e.y2),fill:t.fill,stroke:t.stroke,strokeWidth:t.strokeWidth}):null});function Ye(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const ke=l(({gesture:e,store:o})=>{const{transformLabelStyle:r}=X(),{x:l,y:s,width:a,height:c,rotation:d,anchor:m}=e.label;if(void 0===l||void 0===s||void 0===a||void 0===c||void 0===d||void 0===m){return null}const u=L({x:l,y:s,width:a,height:c,rotation:k.Util.radToDeg(d)}),h=null!==m&&H(m),f=(c/2+70)*Math.cos(d-Math.PI/2),p=(c/2+70)*Math.sin(d-Math.PI/2),x=_({unit:o.unit,dpi:o.dpi,px:a/o.scale,precious:"px"===o.unit?0:1})+" x "+_({unit:o.unit,dpi:o.dpi,px:c/o.scale,precious:"px"===o.unit?0:1})+("px"===o.unit?"":" "+o.unit);return i(t,{children:[i(g,{x:(u.minX+u.maxX)/2+f,y:(u.minY+u.maxY)/2+p,offsetX:14,offsetY:14,visible:h,children:[n(w,{cornerRadius:5,fill:r.fill}),n(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:Ye(d).toString()+"°"})]}),i(g,{x:(u.minX+u.maxX)/2,y:u.maxY+20,visible:!h,children:[n(w,{cornerRadius:5,fill:r.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),n(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:x})]})]})}),Ae=l(({store:e,gesture:t,transformerStyle:n,onChange:i})=>{var r;const l=e.selectedElements.some(e=>e.curveEnabled),s=e.selectedElements.some(e=>{var t;return!(null!==(t=e.filled.visible)&&void 0!==t?t:e.visible)}),a=JSON.stringify(he(e).map(e=>e.id)),c=!pe(e)&&e.selectedElements[0].keepRatio,d=!pe(e)&&"text"===(null===(r=e.selectedElements[0])||void 0===r?void 0:r.type)&&"fit"===ee(e.selectedElements[0]),m=t.transforming?null:xe(e);return o.useLayoutEffect(()=>{i()},[e.selectedShapes,a,ge(e),s,fe(e),l,c,d,t.transforming,null==m?void 0:m.canStretchX,null==m?void 0:m.canStretchY,null==m?void 0:m.boxRotation,n]),null}),je=()=>{console.warn("Polotno warning: onClick callback is deprecated. Just stop using it. Polotno will do selection automatically.")},Me=l(({page:e})=>{const t=e._exportingOrRendering||e._forceMount;return o.useEffect(()=>{if(t&&d(e)){return e.set({_exportRendered:!0}),()=>{d(e)&&e.set({_exportRendered:!1})}}},[e,t]),null}),Ie=l(({elements:e,store:t})=>{const i=e.filter(e=>e.alwaysOnTop),r=e.filter(e=>!e.alwaysOnTop).concat(i);return n(o.Fragment,{children:r.map(e=>n(A,{store:t,element:e,onClick:je},e.id))})});export const useContextMenu=({store:e})=>{const[t,n]=o.useState(!1),[i,r]=o.useState({x:0,y:0}),l=o.useCallback(e=>{n(!0),r(e)},[]);return{open:l,close:o.useCallback(()=>{n(!1)},[]),props:{isOpen:t,offset:i,setIsOpen:n}}};let Oe=null;export const registerNextDomDrop=e=>{Oe=e};const Pe=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),Re=atob("I2MwMzkyYg=="),Te=atob("djAuOS4y"),De=e=>n(t,{children:i(g,{fill:Re,height:200,children:[n(w,{fill:Re}),n(E,Object.assign({},e,{fill:"white",text:Pe,height:void 0,padding:10,fontSize:20}))]})}),Be=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),Ce=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),We=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),Ge=e=>{const t=1===W(),o=t?Ce:Be,r=t?230:195;return i(u,Object.assign({},e,{listening:!1,children:[n(x,{fill:Re,width:r,height:24,cornerRadius:2}),n(E,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),t&&n(E,{text:We,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default l(({store:t,page:l,width:d,height:h,pageControlsEnabled:b,components:y,altCloneEnabled:A,groupSelectionMode:O="drill",viewportSize:P,layout:D="vertical",tooltipSafeArea:_,transformerSafeArea:H})=>{const N=X(),te=N.transformerStyle,ne=me,ye=l.computedBleed,we=t.bleedVisible?ye:m,Ye=l.computedWidth+we.left+we.right,je=l.computedHeight+we.top+we.bottom,Pe=(d-Ye*t.scale)/2,Re=(h-je*t.scale)/2,Be=Object.assign(Object.assign({},oe(ye,l.computedWidth,l.computedHeight)),{background:ie(l.background),name:"page-background",preventDefault:!1}),Ce={x:-we.left,y:-we.top,width:Ye,height:je},We=o.useRef(null),Le=o.useRef(null),Ve=o.useRef(null),Fe=o.useRef(null),ze=null==b||b,_e=o.useRef(!1),[Ue,Ze]=o.useState(null),He=s(()=>({transforming:!1,label:{},setLabel(e){this.label=e},setTransforming(e){this.transforming=e}})),[Je,Ne]=o.useState(0),Ke=useContextMenu({store:t}),$e=he(t),Qe=pe(t),qe=()=>{var n,i,o;const r=We.current;if(!r){return[]}const l=r.getStage();if(!l){return[]}const s=he(t),a=new Set(s.map(e=>e.id)),c=new Map;a.size&&l.find(e=>{const t=e.id();return a.has(t)&&!c.has(t)&&c.set(t,e),!1});const d=s.map(e=>c.get(e.id)).filter(e=>!!e),m=1===t.selectedElements.length,u=m&&(null===(n=t.selectedElements[0])||void 0===n?void 0:n.type)||"many",h=ve[u],g=Object.assign(Object.assign({},ne),h||{});h&&("svg"!==u&&"image"!==u&&"gif"!==u&&"lottie"!==u||t.selectedElements[0].keepRatio||(g.enabledAnchors=ne.enabledAnchors),"text"!==u||!q.textVerticalResizeEnabled&&"fit"!==ee(t.selectedElements[0])||(g.enabledAnchors=null===(i=ve.text.enabledAnchors)||void 0===i?void 0:i.concat(["bottom-center","top-center"])),"text"===u&&m&&t.selectedElements[0].curveEnabled&&(g.enabledAnchors=ve.many.enabledAnchors));const f=xe(t);(f.canStretchX||f.canStretchY)&&(g.enabledAnchors=[...g.enabledAnchors||[],...f.canStretchX?["middle-left","middle-right"]:[],...f.canStretchY?["top-center","bottom-center"]:[]]);const{boundBoxFunc:p}=te,x=e(te,["boundBoxFunc"]);r.setAttrs(Object.assign(Object.assign(Object.assign({},g),x),{name:J})),ge(t)&&r.enabledAnchors([]),fe(t)&&r.rotateEnabled(!1);const v=r.nodes();if(v.length===d.length&&d.every((e,t)=>v[t]===e)||r.nodes(d),pe(t)&&d.length){const e=ue(s);null===e||K(r.rotation()-e)||(r.rotation(e),r.forceUpdate())}return null===(o=r.getLayer())||void 0===o||o.batchDraw(),d},et=ce(()=>{var e;if(null===(e=We.current)||void 0===e?void 0:e.isTransforming()){return}const t=qe();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),M())}),tt=()=>{var e,t;const n=We.current;if(!n){return}if(!n.isTransforming()){return void He.setLabel({})}if(!n.nodes().length){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();He.setLabel({anchor:o,x:i.x-(null!==(e=null==r?void 0:r.x())&&void 0!==e?e:0),y:i.y-(null!==(t=null==r?void 0:r.y())&&void 0!==t?t:0),rotation:i.rotation,width:i.width,height:i.height})},nt=o.useCallback(()=>{var e;if(null===(e=We.current)||void 0===e?void 0:e.isTransforming()){return}const t=We.current;if(!t||!t.nodes().length){return void Ne(0)}const n=t.getClientRect();if(!t.getStage()){return}const i=function(e,t,n,i=0){const o=e.x+e.width/2,r=e.y+e.height/2,l=e.width/2,s=e.height/2,a=i*Math.PI/180,c=e=>{const t=e*Math.PI/180,i=(e%360+360)%360;let c;c=0===i||180===i?s:l;const d=Math.sin(t)*(c+n),m=-Math.cos(t)*(c+n),u=d*Math.cos(a)-m*Math.sin(a),h=d*Math.sin(a)+m*Math.cos(a);return{x:o+u,y:r+h}},d=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(d(c(0))){return 0}for(const m of[180,270,90]){if(d(c(m))){return m}}return 0}(n,{top:(null==H?void 0:H.top)||0,bottom:h-((null==H?void 0:H.bottom)||0),left:(null==H?void 0:H.left)||0,right:d-((null==H?void 0:H.right)||0)},te.rotateAnchorOffset,t.rotation());Ne(i)},[H,d,h]);o.useEffect(()=>{var e;null===(e=We.current)||void 0===e||e.update(),tt(),nt()},[t.scale,nt]),o.useEffect(()=>{nt()},[t.selectedShapes,nt]);const it=s(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),ot=o.useRef(!1),rt=e=>{const n=Ve.current;if(!n||t.role===Y.VIEWER||t.activePage!==l||t.isPlaying||"button"in e.evt&&0!==e.evt.button||1!==n.getPointersPositions().length||!e.target.findAncestor(".elements-container")||!e.target.findAncestor(".element",!0)||!t.selectedShapes.length||fe(t)){return}const i=n.getPointerPosition();if(!i){return}const o=qe();if(o.some(t=>t===e.target||t.isAncestorOf(e.target))){return}const r=o.find(e=>!!(e.draggable()&&e.isVisible()&&e.isListening())&&(e instanceof k.Shape?[e]:e instanceof k.Container?e.find("Shape"):[]).some(e=>e.intersects(i)));if(r){for(const[t,n]of k.DD._dragElements){"ready"!==n.dragStatus||n.node!==e.target&&!n.node.isAncestorOf(e.target)||k.DD._dragElements.delete(t)}r._createDragElement(e),"touches"in e.evt&&e.evt.cancelable&&r.preventDefault()&&e.evt.preventDefault()}},lt=a(e=>{var n,i,o,r,l,s,a;if(t.role===Y.VIEWER){return}if(ot.current=!1,t.activeTool&&"crop"!==t.activeTool.name){return}const c=e.target.findAncestor(".elements-container"),d=e.target.findAncestor("Transformer"),m=e.target.findAncestor(".page-abs-container");if(c||d||m){return}const u=null===(n=e.target.getStage())||void 0===n?void 0:n.getPointerPosition();u&&(u.x-=null!==(l=null===(i=e.target.getStage())||void 0===i?void 0:i.x())&&void 0!==l?l:0,u.y-=null!==(s=null===(o=e.target.getStage())||void 0===o?void 0:o.y())&&void 0!==s?s:0,it.visible=!0,it.x1=u.x,it.y1=u.y,it.x2=u.x,it.y2=u.y,(null!==(a=null===(r=e.target.getStage())||void 0===r?void 0:r.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(it.visible=!1))});(({stageRef:e,containerRef:t,viewportSize:n})=>{o.useEffect(()=>{var n;const i=null===(n=t.current)||void 0===n?void 0:n.closest(".polotno-workspace-inner");function o(){var n;if(!e.current){return}const o=null===(n=t.current)||void 0===n?void 0:n.getBoundingClientRect(),l=null==i?void 0:i.getBoundingClientRect();if(!o||!l){return}const s=r(l.left-o.left-100,0,Math.max(0,o.width-e.current.width())),a=r(l.top-o.top-100,0,Math.max(0,o.height-e.current.height()));e.current.position({x:-s,y:-a}),e.current.container().style.transform=`translate(${s}px, ${a}px)`}return o(),null==i||i.addEventListener("scroll",o),()=>{null==i||i.removeEventListener("scroll",o)}},[n.width,n.height])})({stageRef:Ve,containerRef:Fe,viewportSize:P}),o.useEffect(()=>{const e=a(e=>{var t,n,i,o,r,l;if(!it.visible){return}null===(t=Ve.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=Ve.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(r=null===(i=Ve.current)||void 0===i?void 0:i.x())&&void 0!==r?r:0,s.y-=null!==(l=null===(o=Ve.current)||void 0===o?void 0:o.y())&&void 0!==l?l:0):s={x:it.x2,y:it.y2},it.x2=s.x,it.y2=s.y}),n=a(()=>{if(!it.visible){return}if(!Ve.current){return}const e=Ve.current.findOne(".selection"),n=e?e.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(n.width&&n.height){const e=[{x:n.x,y:n.y},{x:n.x+n.width,y:n.y},{x:n.x+n.width,y:n.y+n.height},{x:n.x,y:n.y+n.height}],i=[];Ve.current.find(".element").forEach(n=>{var o;const r=t.getElementById(n.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=n.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const c=V(r.a).map(e=>a.point(e));z(e,c)&&i.push(r.top.id)});const o=[...new Set(i)];ot.current=!0,t.selectElements(o)}it.visible=!1});return window.addEventListener("mousemove",e),window.addEventListener("touchmove",e),window.addEventListener("mouseup",n,{capture:!0}),window.addEventListener("touchend",n,{capture:!0}),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("touchmove",e),window.removeEventListener("mouseup",n,{capture:!0}),window.removeEventListener("touchend",n,{capture:!0})}},[]);const st=o.useCallback(e=>{if(!t.selectedElements[0]){return}if(e&&t.selectedElementsIds.includes(e)){return}const n=F(t.selectedShapes),i=e?t.getElementById(e):{x:0,y:0,width:l.computedWidth,height:l.computedHeight,rotation:0},o=L(i),r=[];n.minX>o.maxX&&r.push({distance:n.minX-o.maxX,box1:n,box2:o,points:[{x:n.minX,y:n.minY+n.height/2},{x:o.maxX,y:n.minY+n.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),n.maxX<o.minX&&r.push({distance:o.minX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:n.minY+n.height/2},{x:o.minX,y:n.minY+n.height/2},{x:o.minX,y:o.minY+o.height/2}]}),n.minY>o.maxY&&r.push({box1:n,box2:o,distance:n.minY-o.maxY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]}),n.maxY<o.minY&&r.push({box1:n,box2:o,distance:o.minY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]});const s=n.minX>=o.minX&&n.maxX<=o.maxX&&n.minY>=o.minY&&n.maxY<=o.maxY;if(s&&(r.push({distance:n.minX-o.minX,box1:n,box2:o,points:[{x:n.minX,y:n.minY+n.height/2},{x:o.minX,y:n.minY+n.height/2},{x:o.minX,y:o.minY+o.height/2}]}),r.push({distance:o.maxX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:n.minY+n.height/2},{x:o.maxX,y:n.minY+n.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),r.push({box1:n,box2:o,distance:n.minY-o.minY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]}),r.push({box1:n,box2:o,distance:o.maxY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]})),0===r.length&&!s){const e=n.minX<o.maxX&&n.maxX>o.minX,t=n.minY<o.maxY&&n.maxY>o.minY;if(e&&t){const e=(Math.max(n.minY,o.minY)+Math.min(n.maxY,o.maxY))/2,t=(Math.max(n.minX,o.minX)+Math.min(n.maxX,o.maxX))/2;n.minX<o.minX?r.push({distance:o.minX-n.minX,box1:n,box2:o,points:[{x:n.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}):r.push({distance:n.minX-o.minX,box1:n,box2:o,points:[{x:n.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}),n.maxX>o.maxX?r.push({distance:n.maxX-o.maxX,box1:n,box2:o,points:[{x:o.maxX,y:e},{x:n.maxX,y:e},{x:o.maxX,y:e}]}):r.push({distance:o.maxX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:e},{x:o.maxX,y:e},{x:o.maxX,y:e}]}),n.minY<o.minY?r.push({box1:n,box2:o,distance:o.minY-n.minY,points:[{x:t,y:n.minY},{x:t,y:o.minY},{x:t,y:o.minY}]}):r.push({box1:n,box2:o,distance:n.minY-o.minY,points:[{x:t,y:n.minY},{x:t,y:o.minY},{x:t,y:o.minY}]}),n.maxY>o.maxY?r.push({box1:n,box2:o,distance:n.maxY-o.maxY,points:[{x:t,y:o.maxY},{x:t,y:n.maxY},{x:t,y:o.maxY}]}):r.push({box1:n,box2:o,distance:o.maxY-n.maxY,points:[{x:t,y:n.maxY},{x:t,y:o.maxY},{x:t,y:o.maxY}]})}}Ze(e=>JSON.stringify(e)===JSON.stringify(r)?e:r)},[t,l]),at=o.useCallback(()=>{t.distanceGuidesVisible&&t.selectedElements[0]?st():Ze(null)},[t,st]);o.useEffect(at,[t.distanceGuidesVisible,at]);const ct=t.distanceGuidesVisible||!!Ue;o.useEffect(()=>{if(ct){return ae(l,()=>t.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|"),at)}},[ct,at]);const dt=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=Fe.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{dt.current=!0,clearTimeout(t),t=setTimeout(()=>{dt.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const e=Ve.current;if(!e){return}const n=e.container(),i=new Map,o=e=>{var t,n;i.has(e.target)||[e.target,...null!==(n=null===(t=We.current)||void 0===t?void 0:t.nodes())&&void 0!==n?n:[]].forEach(e=>{i.has(e)||i.set(e,{x:e.x(),y:e.y()})})},r=e=>{e.target===We.current?i.clear():i.delete(e.target)};e.on("dragstart",o),e.on("dragend",r);const l=e=>{e.touches.length>=2&&c(()=>{var e,n;const o=new Map(i);(null===(e=We.current)||void 0===e?void 0:e.isDragging())&&We.current.stopDrag(),null===(n=We.current)||void 0===n||n.nodes().forEach(e=>{var n;const i=o.get(e);e.isDragging()&&e.stopDrag(),i&&(e.setAttrs(i),null===(n=t.getElementById(e.id()))||void 0===n||n.set({x:i.x,y:i.y}))})})};return n.addEventListener("touchstart",l,{passive:!0}),n.addEventListener("touchmove",l,{passive:!0}),()=>{e.off("dragstart",o),e.off("dragend",r),n.removeEventListener("touchstart",l),n.removeEventListener("touchmove",l)}},[t]);const mt=e=>{if(t.role===Y.VIEWER){return}if(t.activePage!==l&&l.select(),dt.current){return}if(ot.current){return}const n=e.evt.shiftKey,i=e.evt.ctrlKey||e.evt.metaKey,o=n||i,r=e.target.findAncestor(".elements-container"),s=e.target.findAncestor(".page-abs-container"),a=e.target.findAncestor("Transformer"),c=e.target.findAncestor(".page-container",!0),d=!(o||r||a||s||c||it.visible);if(t.activeTool&&"crop"!==t.activeTool.name&&!r&&!a){return}if(d){return t.selectElements([]),void t.selectPages([])}if(i&&!n){const e=Ve.current,n=null==e?void 0:e.getPointerPosition();if(!e||!n){return}const i=e.getAllIntersections(n),o=new Set,r=[];for(const s of[...i].reverse()){const e=s.findAncestor(".element",!0);if(!e){continue}const n=t.getElementById(e.id()),i=null==n?void 0:n.top;i&&(o.has(i.id)||(o.add(i.id),r.push(i.id)))}if(0===r.length){return}let l;if(1===t.selectedElementsIds.length){const e=t.selectedElementsIds[0],n=r.indexOf(e);l=-1===n?r[0]:r[(n+1)%r.length]}else{l=r[0]}return t.selectElements([l]),void t.selectPages([])}const m=e.target.findAncestor(".element-container",!0),u=(null==m?void 0:m.findOne(".element"))||e.target.findAncestor(".element",!0),h=t.getElementById(null==u?void 0:u.id());h&&"pendingEdit"in h&&h._setImageEditError(null);const g=null==h?void 0:h.top;let f;if("group"===O){f=null==g?void 0:g.id}else if(g){const e=t.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===g.id});f=e?null==h?void 0:h.id:null==g?void 0:g.id}else{f=void 0}const p=null==h?void 0:h.id;if(p&&t.selectedElementsIds.indexOf(p)>=0&&!n){return}const x=!!f&&t.selectedElementsIds.indexOf(f)>=0,v=e.target.findAncestor(".page-container",!0);if(f&&n&&!x){const e=se(t.selectedElementsIds.concat([f]),e=>le(t,e));t.selectElements(e),t.selectPages([])}else{f&&n&&x?t.selectElements(t.selectedElementsIds.filter(e=>e!==f)):!f||n||x?f&&!n&&x||(v?(n||t.selectElements([]),t.selectPages([l.id])):t.selectPages([])):(t.selectElements([f]),t.selectPages([]))}};I(We,t);const ut=t.activePage===l,ht=t._selectedPagesIds.includes(l.id),gt=null==y?void 0:y.PageControls,ft=null==y?void 0:y.Tooltip,pt=null==y?void 0:y.ContextMenu,xt=1/t.scale,vt=0/t.scale;return i("div",{ref:Fe,onDragOver:e=>e.preventDefault(),onDrop:e=>{if(e.preventDefault(),t.role===Y.VIEWER){return}if(!Ve.current){return}Ve.current.setPointersPositions(e);const n=Ve.current.findOne(".elements-container"),i=null==n?void 0:n.getRelativePointerPosition(),o=Ve.current.getPointerPosition(),r=Ve.current.getAllIntersections(o).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(r.reverse())].map(e=>t.getElementById(e.id())).filter(e=>!!e),a=s[0];Oe&&i&&(Oe(i,a,{elements:s,page:l}),Oe=null)},style:{position:"relative",width:d+"px",height:h+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+(ut?" active-page":""),children:[n(de,{ref:Ve,eventBatchFunc:c,width:Math.min(d,P.width+200),height:Math.min(P.height+200,h),onClick:mt,onTap:mt,onContextMenu:e=>{if(e.evt.preventDefault(),t.role===Y.VIEWER){return}const n=e.target.findAncestor(".element",!0),i=t.getElementById(null==n?void 0:n.id()),o=null==i?void 0:i.top,r=null==o?void 0:o.id;if(!r){return t.selectElements([]),void Ke.close()}t.selectedElementsIds.indexOf(r)>=0||t.selectElements([r]),Ke.open({x:e.evt.clientX,y:e.evt.clientY})},onMouseDown:e=>{rt(e),lt(e)},onTouchStart:rt,onMouseMove:e=>{if(!t.distanceGuidesVisible&&!e.evt.altKey){return void(Ue&&Ze(null))}const n=e.target.findAncestor(".element",!0),i=null==n?void 0:n.id();st(i)},onDragStart:e=>{var n;const i=e.target.getStage();if(i&&i.getPointersPositions().length>=2){return void e.target.stopDrag()}const o=e.target.findAncestor(".element",!0);if(o){const i=t.getElementById(null==o?void 0:o.id()),r=null==i?void 0:i.top,l=null==r?void 0:r.id;if(!(i&&t.selectedElementsIds.indexOf(i.id)>=0||l&&t.selectedElementsIds.indexOf(l)>=0)&&l){t.selectElements([l]);const i=qe();e.target.stopDrag(),i.forEach(t=>{t.isDragging()||t.startDrag(e)}),null===(n=We.current)||void 0===n||n.startDrag(e),M()}}Ue&&Ze(null)},onDragMove:e=>{const n=We.current;n&&e.target===n&&n.nodes().forEach(e=>{const n=t.getElementById(e.id());!n||n.x===e.x()&&n.y===e.y()||n.set({x:e.x(),y:e.y()})})},pageId:l.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:i(f,{children:[n(be,{width:d,height:h,fill:N.workspaceBackgroundColor}),n(u,{x:Pe,y:Re,scaleX:t.scale,scaleY:t.scale,name:"page-container",children:i(u,{name:"page-container-2",children:[n(u,{x:we.left,y:we.top,children:n(Ee,Object.assign({},Be,Ce,{transparencyBackgroundColor:N.transparencyBackgroundColor}))}),i(u,{isolated:l.hasBlendMode,children:[n(u,{name:"page-background-group",x:we.left,y:we.top,clip:Ce,children:n(Se,Object.assign({},Be))}),i(u,{x:we.left,y:we.top,name:"elements-container",listening:!t.isPlaying&&t.role!==Y.VIEWER,children:[n(x,{name:"elements-area",width:l.computedWidth,height:l.computedHeight,listening:!1}),n(Ie,{elements:l.children,store:t}),n(Me,{page:l})]})]}),n(v,{name:"bleed",fill:N.bleedColor,listening:!1,visible:t.bleedVisible&&(we.top>0||we.right>0||we.bottom>0||we.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:i,bottom:o,left:r}=ye,s=l.computedWidth,a=l.computedHeight,c=e._context;c.beginPath(),c.rect(0,0,r+s+i,n+a+o),c.rect(r,n,s,a),c.fillStyle=t.fill(),c.fill("evenodd")}}),C()===Te&&n(De,{name:"hit-detection",x:-xt/2-vt,y:-xt/2-vt,width:Ye+xt+2*vt,height:je+xt+2*vt})]})}),n(p,{name:"workspace-background",points:[0,0,d,0,d,h,0,h,0,0,Pe,Re,Pe,h-Re,d-Pe,h-Re,d-Pe,Re,Pe,Re],listening:!1,closed:!0,fill:N.workspaceBackgroundColor}),n(u,{x:Pe,y:Re,scaleX:t.scale,scaleY:t.scale,children:n(x,{name:"page-highlight",hideInExport:!0,x:-xt/2-vt,y:-xt/2-vt,width:Ye+xt+2*vt,height:je+xt+2*vt,stroke:ht?N.activePageBorderColor:N.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),i(u,{x:Pe+we.left*t.scale,y:Re+we.top*t.scale,scaleX:t.scale,scaleY:t.scale,name:"page-abs-container",children:[n(S,{ref:We,name:J,rotateAnchorAngle:Je,onDragStart:e=>{var n;const i=e.target.getStage();i&&i.getPointersPositions().length>=2?e.target.stopDrag():((null===(n=e.evt)||void 0===n?void 0:n.altKey)&&A&&B(t.selectedElements,l,t),_e.current||(_e.current=!0,t.history.startTransaction()))},onDragEnd:()=>{_e.current&&(t.history.endTransaction(),_e.current=!1),nt()},onTransformStart:()=>{He.setTransforming(!0),t.history.startTransaction(),Le.current=new Map($e.map(e=>[e.id,{width:e.width,height:e.height}])),Ze(null)},boundBoxFunc:(e,n)=>{var i,o,r,l;const s=null!==(l=null===(i=te.boundBoxFunc)||void 0===i?void 0:i.call(te,e,n))&&void 0!==l?l:n,a=Math.abs(s.width)<1||Math.abs(s.height)<1,c=Math.abs(e.width)<1||Math.abs(e.height)<1;if(a&&!c){return e}const d=t.selectedElements;if(1===d.length&&"table"===(null===(o=d[0])||void 0===o?void 0:o.type)){const t=d[0],n=Math.abs(e.width),i=Math.abs(e.height);if(n>0&&i>0){const o=Math.abs(s.width)/n,r=Math.abs(s.height)/i,l=t.width*o,a=t.height*r,c=Object.assign({},s);return l<t.minWidth&&(c.width=e.width),a<t.minHeight&&(c.height=e.height),Q(c,s,e)}}return Qe&&$e.length>0?((e,t,n,i,o)=>{const r=Math.abs(e.width),l=Math.abs(e.height);if(0===r||0===l){return t}const s=Math.abs(t.width)/r,a=Math.abs(t.height)/l,c=!Z(n),d=e=>i.some(t=>{var n;const i=null==o?void 0:o.get(t.id),r=null!==(n="x"===e?null==i?void 0:i.width:null==i?void 0:i.height)&&void 0!==n?n:"x"===e?t.width:t.height,l="x"===e?t.width:t.height,d="x"===e?s:a,m=$(t,e,c,r);return r>=m&&l*d<m}),m=Object.assign({},t),u=t.width*e.width<0,h=t.height*e.height<0,g=u||d("x"),f=h||d("y"),p=f||!c&&g;return(g||!c&&f)&&(m.width=e.width),p&&(m.height=e.height),Q(m,t,e)})(e,s,(null===(r=We.current)||void 0===r?void 0:r.getActiveAnchor())||"",$e,Le.current):s},onTransform:e=>{const t=We.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{tt()},0)},onTransformEnd:e=>{He.setTransforming(!1),He.setLabel({}),t.history.endTransaction(),Le.current=null,nt()},visible:!t.isPlaying}),n(Ae,{store:t,gesture:He,transformerStyle:te,onChange:et}),Ue&&Ue.map(({points:e,distance:o,box1:r,box2:l},s)=>i(u,{name:"distances-container",hideInExport:!0,listening:!1,children:[n(x,Object.assign({},r,{stroke:N.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),n(x,Object.assign({},l,{stroke:N.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),n(p,{points:[e[0].x,e[0].y,e[1].x,e[1].y],stroke:N.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(p,{points:[e[1].x,e[1].y,e[2].x,e[2].y],stroke:N.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),i(g,{x:(e[0].x+e[1].x)/2,y:(e[0].y+e[1].y)/2,offsetY:-10,scaleX:1/t.scale,scaleY:1/t.scale,children:[n(w,{cornerRadius:5,fill:N.distanceGuideStyle.labelFill,pointerDirection:"down"}),n(E,{align:"center",verticalAlign:"middle",fill:N.distanceGuideStyle.labelTextFill,padding:5,text:U({unit:t.unit,dpi:t.dpi,px:o})})]})]},s)),l._rendering&&i(u,{children:[n(x,{width:Ye,height:je,fill:"rgba(255,255,255,0.9)"}),n(E,{text:"Rendering...",fontSize:60,width:Ye,height:je,align:"center",verticalAlign:"middle"})]}),ft&&n(ft,{components:y,store:t,page:l,stageRef:Ve,tooltipSafeArea:_}),pt&&n(re,{children:n(pt,Object.assign({components:y,store:t},Ke.props))}),n(R,{store:t,pageId:l.id}),n(T,{store:t,pageId:l.id})]}),n(j,{store:t,page:l,width:d,height:h,scale:t.scale,xPadding:Pe,yPadding:Re,bleedLeft:we.left,bleedTop:we.top}),n(ke,{gesture:He,store:t}),n(Xe,{selection:it}),W()>0&&i(u,{hideInExport:!0,children:[n(Ge,{name:"cache-bounds",x:d-(1===W()?250:210),y:h-(1===W()?95:45)}),n(Ge,{name:"cache-bounds-t",x:10,y:10})]}),G()&&n(E,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:d-170,y:h-18,onMouseEnter:e=>{e.target.getStage().container().style.cursor="pointer"},onMouseLeave:e=>{e.target.getStage().container().style.cursor=""},onTouchStart:e=>{e.cancelBubble=!0},onMouseDown:e=>{e.cancelBubble=!0},onClick:()=>{window.open("https://polotno.com","_blank","noopener")},onTap:()=>{window.open("https://polotno.com","_blank","noopener")}}),n(u,{name:"line-guides"})]})}),ze&>&&n(gt,{store:t,page:l,xPadding:Pe,yPadding:Re,layout:D})]})});
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export declare const VARIABLE_TOKEN_FORMAT = "variable-token";
|
|
1
2
|
export declare const PRESERVED_INLINE_STYLES: readonly ['text-shadow', 'letter-spacing', 'word-spacing', '-webkit-text-stroke', 'font-variant', 'vertical-align', 'text-decoration-color', 'text-decoration-style', 'text-decoration-thickness', 'text-underline-offset', 'text-decoration-skip-ink'];
|
|
2
3
|
export declare const PRESERVED_BLOCK_STYLES: readonly ['text-align', 'line-height', 'margin-top', 'margin-bottom', 'text-indent', 'list-style-type'];
|
|
3
4
|
export declare function registerQuillUnrestrictedFontFormats(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"quill";import{firstStopColor as e}from"@polotno/core/gradient";import{parsePrimaryFontFamily as r}from"@polotno/core/rich-text-html";import{cssFontFamily as o}from"@polotno/core/fonts";import{containsCmykColor as i,isCmykColor as n,toDisplayColor as s}from"@polotno/core/color";import{isClipGradientSpan as l,nativeColorAttribute as c,readNativeColor as a}from"../utils/rich-text-html.js";let
|
|
1
|
+
import t from"quill";import{firstStopColor as e}from"@polotno/core/gradient";import{parsePrimaryFontFamily as r}from"@polotno/core/rich-text-html";import{cssFontFamily as o}from"@polotno/core/fonts";import{containsCmykColor as i,isCmykColor as n,toDisplayColor as s}from"@polotno/core/color";import{isClipGradientSpan as l,nativeColorAttribute as c,readNativeColor as a,VARIABLE_TOKEN_CLASS as u}from"../utils/rich-text-html.js";let p=!1;export const VARIABLE_TOKEN_FORMAT="variable-token";export const PRESERVED_INLINE_STYLES=["text-shadow","letter-spacing","word-spacing","-webkit-text-stroke","font-variant","vertical-align","text-decoration-color","text-decoration-style","text-decoration-thickness","text-underline-offset","text-decoration-skip-ink"];export const PRESERVED_BLOCK_STYLES=["text-align","line-height","margin-top","margin-bottom","text-indent","list-style-type"];function m(t){const e=t.keyName,r=c(e),o=t.add.bind(t),i=t.remove.bind(t),l=t.value.bind(t);t.add=(t,e)=>n(e)?!!o(t,s(e))&&(t.setAttribute(r,e),!0):(t.removeAttribute(r),o(t,e)),t.remove=t=>{t.removeAttribute(r),i(t)},t.value=t=>a(t,e)||l(t)}const d=["background-image","background-size","-webkit-background-clip","background-clip","-webkit-text-fill-color","color"],g=c("background-image");export function registerQuillUnrestrictedFontFormats(){if(p){return}p=!0;const n=t.import("attributors/style/size"),c=t.import("attributors/style/font");n&&(delete n.whitelist,t.register(n,!0)),c&&(delete c.whitelist,c.add=(t,e)=>(t.style.fontFamily=o(e),!0),c.value=t=>r(t.style.fontFamily)||"",t.register(c,!0));const b=t.import("parchment");if((null==b?void 0:b.StyleAttributor)&&(null==b?void 0:b.Scope)){const r=new b.StyleAttributor("font-weight","font-weight",{scope:b.Scope.INLINE});t.register(r,!0),t.register(new b.StyleAttributor("text-transform","text-transform",{scope:b.Scope.INLINE}),!0);const o=(e,r)=>t.register(new b.StyleAttributor(e,e,{scope:r}),!0);PRESERVED_INLINE_STYLES.forEach(t=>o(t,b.Scope.INLINE)),PRESERVED_BLOCK_STYLES.forEach(t=>o(t,b.Scope.BLOCK));const n=t.import("blots/inline");class c extends n{static formats(){return!0}}c.blotName="variable-token",c.className=u,t.register(c,!0),t.register(function(t){class r extends t.StyleAttributor{constructor(t,e,r){super(t,e,r)}add(t,r){if("string"!=typeof r||!r.includes("gradient")){return!1}t.style.setProperty("background-image",s(r)),i(r)?t.setAttribute(g,r):t.removeAttribute(g),t.style.setProperty("background-size","100% 100%"),t.style.setProperty("-webkit-background-clip","text"),t.style.setProperty("background-clip","text"),t.style.setProperty("-webkit-text-fill-color","transparent");try{t.style.setProperty("color",e(r))}catch(o){}return!0}remove(t){t.removeAttribute(g);for(const e of d){t.style.removeProperty(e)}}value(t){return l(t)?a(t,"background-image")||t.style.getPropertyValue("background-image"):""}}return new r("gradient","background-image",{scope:t.Scope.INLINE})}(b),!0);const p=t.import("attributors/style/color");if(p){m(p);const e=p.value.bind(p);p.value=t=>l(t)?"":e(t),t.register(p,!0)}const y=t.import("attributors/style/background");y&&(m(y),t.register(y,!0))}}
|