polotno 3.13.0 → 3.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/blueprint/side-panel/select-video.d.ts +105 -36
- package/canvas/apply-filters.js +1 -1
- package/canvas/downscale.d.ts +4 -0
- package/canvas/downscale.js +1 -1
- package/canvas/element.d.ts +1 -1
- package/canvas/element.js +1 -1
- package/canvas/figure-element.js +1 -1
- package/canvas/gif-element.js +1 -1
- package/canvas/html-element.js +1 -1
- package/canvas/image-element.js +1 -1
- package/canvas/line-element.js +1 -1
- package/canvas/page.js +1 -1
- package/canvas/paint-opacity.d.ts +12 -0
- package/canvas/paint-opacity.js +1 -0
- package/canvas/render-tag-element.js +1 -1
- package/canvas/rich-editor.js +11 -3
- package/canvas/table-element.js +1 -1
- package/canvas/text-element/font-specs-loader.js +1 -1
- package/canvas/text-element.js +1 -1
- package/canvas/use-filtered-source.js +1 -1
- package/canvas/video-element.js +1 -1
- package/canvas/workspace-canvas.js +1 -1
- package/model/audio-model.d.ts +54 -2
- package/model/figure-model.d.ts +65 -24
- package/model/gif-model.d.ts +65 -24
- package/model/group-model.d.ts +497 -182
- package/model/group-model.js +1 -1
- package/model/history.js +1 -1
- package/model/image-model.d.ts +65 -24
- package/model/line-model.d.ts +65 -24
- package/model/node-model.d.ts +65 -24
- package/model/node-model.js +1 -1
- package/model/page-model.d.ts +4369 -445
- package/model/page-model.js +1 -1
- package/model/shape-model.d.ts +65 -24
- package/model/store.d.ts +480 -18060
- package/model/store.js +1 -1
- package/model/svg-model.d.ts +64 -24
- package/model/table-model.d.ts +40 -12
- package/model/table-model.js +1 -1
- package/model/text-model.d.ts +64 -24
- package/model/text-model.js +1 -1
- package/model/video-model.d.ts +65 -24
- package/package.json +8 -8
- package/polotno.bundle.js +167 -156
- package/primitives/panel-header.js +1 -1
- package/primitives/panel-scroll-area.d.ts +4 -0
- package/primitives/panel-scroll-area.js +1 -0
- package/primitives/select.js +1 -1
- package/side-panel/animations-panel.js +1 -1
- package/side-panel/draw-panel.js +1 -1
- package/side-panel/effects-panel.js +1 -1
- package/side-panel/elements-panel.js +1 -1
- package/side-panel/image-clip-panel.js +1 -1
- package/side-panel/layers-panel.js +1 -1
- package/side-panel/select-video.d.ts +105 -36
- package/side-panel/side-panel.js +1 -1
- package/side-panel/size-panel.js +1 -1
- package/side-panel/text-panel.js +1 -1
- package/toolbar/color-picker.js +1 -1
- package/toolbar/html-toolbar.js +3 -3
- package/toolbar/opacity-picker.js +1 -1
- package/toolbar/text-toolbar.js +1 -1
- package/ui.css +1 -1
- package/utils/canvas.d.ts +0 -3
- package/utils/canvas.js +1 -1
- package/utils/clipboard.d.ts +104 -36
- package/utils/fonts.js +1 -1
- package/utils/from-svg.js +1 -1
- package/utils/l10n.d.ts +21 -0
- package/utils/l10n.js +1 -1
- package/utils/pointer-drag.js +1 -1
- package/utils/text-format-state.d.ts +3 -0
- package/utils/text-format-state.js +1 -1
- package/utils/validate-key.js +1 -1
package/canvas/page.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=this&&this.__rest||function(e,t){var n={};for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++){t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(n[i[o]]=e[i[o]])}}return n};import{jsx as t,jsxs as n,Fragment as i}from"react/jsx-runtime";import o from"react";import{observer as r,useLocalObservable as l}from"mobx-react-lite";import{action as s,runInAction as a}from"mobx";import{ZERO_BLEED as c}from"@polotno/schema";import{Group as d,Image as h,Label as u,Layer as m,Line as g,Rect as f,Shape as p,Stage as x,KonvaRenderer as v,Tag as b,Text as y,Transformer as w}from"react-konva";import{useWorkspaceStyle as E}from"./workspace-style.js";import{ROLES as X}from"../model/store.js";import Y from"konva";import S from"./element.js";import{DrawingLayer as k}from"./drawing-layer.js";import{ensureDragOrder as A,useSnap as M}from"./use-transformer-snap.js";import{useCanvasImage as I,useImageLoader as P}from"./image-element.js";import{FadeOverlay as O}from"./fade-overlay.js";import{getCrop as j}from"../utils/crop.js";import{___ as R,____ as D,isCreditVisible as B}from"../utils/validate-key.js";import{getClientRect as C,getCorners as T,getTotalClientRect as W,polygonsIntersect as G}from"../utils/math.js";import{pxToUnitRounded as L,pxToUnitString as F}from"../utils/unit.js";import{isCornerAnchor as V,MAIN_TRANSFORMER_NAME as z,textMinResizeWidth as U,textMinWidthIsMeasured as Z}from"../utils/px.js";import{restoreHeldPoint as _}from"./transform-box.js";import{flags as H}from"../utils/flags.js";import{useColor as J}from"./use-color.js";import{isGradient as N}from"@polotno/core/gradient";import{Html as K}from"react-konva-utils";const $=o.version.startsWith("18.")?o.forwardRef((e,n)=>(o.useLayoutEffect(()=>{v.flushSync()}),t(x,Object.assign({},e,{ref:n})))):x,Q={enabledAnchors:["top-left","top-center","top-right","middle-left","bottom-left","bottom-right","bottom-center","middle-right"],rotateEnabled:!0,rotationSnaps:[0,45,90,135,180,225,270,315],ignoreStroke:!0,flipEnabled:!1,keepRatio:!0},q=e=>{const t=(e%180+180)%180;return t<.1||180-t<.1},ee=e=>{if(!e.length){return null}const t=q(e[0].rotation)?0:e[0].rotation;return e.every(e=>q(e.rotation-t))?t:null},te=e=>e.selectedShapes.filter(e=>!e._cropModeEnabled),ne={text:{enabledAnchors:["top-left","top-right","middle-left","bottom-left","bottom-right","middle-right"]},svg:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},gif:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},line:{enabledAnchors:[],borderEnabled:!1,rotateEnabled:!1},image:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},many:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},group:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},table:{keepRatio:!1,enabledAnchors:["top-left","top-center","top-right","middle-left","middle-right","bottom-left","bottom-center","bottom-right"]}};export function registerTransformerAttrs(e,t){ne[e]=ne[e]||t,Object.assign(ne[e],t)}const ie=e=>t(f,Object.assign({},e,{preventDefault:!1})),oe=n=>{var{url:i}=n,o=e(n,["url"]);const[r,l]=I(i,"anonymous"),s=r?j(r,{width:o.width,height:o.height},"center-middle"):{};return P(l,i,"page background"),t(h,Object.assign({image:r},o,s))},re=e=>{const n=J({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return t(f,Object.assign({},e,n))},le=i=>{const{background:r,scale:l,borderColor:s,transparencyBackgroundColor:a}=i,c=e(i,["background","scale","borderColor","transparencyBackgroundColor"]),d=o.useMemo(()=>!!Y.Util.colorToRGBA(r)||N(r),[r]),h=o.useMemo(()=>{if(N(r)){return!0}const e=Y.Util.colorToRGBA(r);return!e||e.a<1},[r]),u=o.useMemo(()=>{if(!h||a){return null}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},[h,a]);return n(o.Fragment,{children:[h&&t(f,a?Object.assign({fill:a},c,{hideInExport:!0}):Object.assign({fillPatternImage:u},c,{opacity:.1,hideInExport:!0})),d?t(re,Object.assign({fill:r},c)):t(oe,Object.assign({url:r},c))]})},se=r(({selection:e})=>{const{selectionRectStyle:n}=E();return e.visible?t(f,{name:"selection",x:Math.min(e.x1,e.x2),y:Math.min(e.y1,e.y2),width:Math.abs(e.x1-e.x2),height:Math.abs(e.y1-e.y2),fill:n.fill,stroke:n.stroke,strokeWidth:n.strokeWidth}):null});function ae(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const ce=r(({x:e,y:o,width:r,height:l,rotation:s,anchor:a,store:c})=>{const{transformLabelStyle:d}=E();if(void 0===e||void 0===o||void 0===r||void 0===l||void 0===s||void 0===a){return null}const h=C({x:e,y:o,width:r,height:l,rotation:Y.Util.radToDeg(s)}),m=(l/2+70)*Math.cos(s-Math.PI/2),g=(l/2+70)*Math.sin(s-Math.PI/2),f=L({unit:c.unit,dpi:c.dpi,px:r/c.scale,precious:"px"===c.unit?0:1})+" x "+L({unit:c.unit,dpi:c.dpi,px:l/c.scale,precious:"px"===c.unit?0:1})+("px"===c.unit?"":" "+c.unit);return n(i,{children:[n(u,{x:(h.minX+h.maxX)/2+m,y:(h.minY+h.maxY)/2+g,offsetX:14,offsetY:14,visible:"rotater"===a,children:[t(b,{cornerRadius:5,fill:d.fill}),t(y,{align:"center",verticalAlign:"middle",fill:d.textFill,padding:8,text:ae(s).toString()+"°"})]}),n(u,{x:(h.minX+h.maxX)/2,y:h.maxY+20,visible:"rotater"!==a,children:[t(b,{cornerRadius:5,fill:d.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),t(y,{align:"center",verticalAlign:"middle",fill:d.textFill,padding:8,text:f})]})]})}),de=r(({elements:e,store:n})=>{const i=e.filter(e=>e.alwaysOnTop),r=e.filter(e=>!e.alwaysOnTop).concat(i);return t(o.Fragment,{children:r.map(e=>t(S,{store:n,element:e,onClick:()=>{console.warn("Polotno warning: onClick callback is deprecated. Just stop using it. Polotno will do selection automatically.")}},e.id))})});export const useContextMenu=({store:e})=>{const[t,n]=o.useState(!1),[i,r]=o.useState({x:0,y:0}),l=o.useCallback(e=>{n(!0),r(e)},[]);return{open:l,close:o.useCallback(()=>{n(!1)},[]),props:{isOpen:t,offset:i,setIsOpen:n}}};let he=null;export const registerNextDomDrop=e=>{he=e};const ue=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),me=atob("I2MwMzkyYg=="),ge=atob("djAuOS4y"),fe=e=>t(i,{children:n(u,{fill:me,height:200,children:[t(b,{fill:me}),t(y,Object.assign({},e,{fill:"white",text:ue,height:void 0,padding:10,fontSize:20}))]})}),pe=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),xe=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),ve=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),be=e=>{const i=1===D(),o=i?xe:pe,r=i?230:195;return n(d,Object.assign({},e,{listening:!1,children:[t(f,{fill:me,width:r,height:24,cornerRadius:2}),t(y,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),i&&t(y,{text:ve,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default r(({store:i,page:r,width:h,height:x,pageControlsEnabled:v,components:S,altCloneEnabled:I,groupSelectionMode:P="drill",viewportSize:j,layout:L="vertical",tooltipSafeArea:J,transformerSafeArea:N})=>{var oe,re;const ae=E(),ue=ae.transformerStyle,me=Q,pe=r.computedBleed,xe=i.bleedVisible?pe:c,ve=r.computedWidth+xe.left+xe.right,ye=r.computedHeight+xe.top+xe.bottom,we=(h-ve*i.scale)/2,Ee=(x-ye*i.scale)/2,Xe=o.useRef(null),Ye=o.useRef(null),Se=o.useRef(null),ke=o.useRef(null),Ae=null==v||v,Me=o.useRef(!1),[Ie,Pe]=o.useState(null),[Oe,je]=o.useState({}),[Re,De]=o.useState(0),Be=useContextMenu({store:i}),Ce=i.selectedElements.find(e=>e._cropModeEnabled),Te=i.selectedElements.find(e=>e.curveEnabled),We=i.selectedShapes.filter(e=>!e.resizable).length>0,Ge=i.selectedShapes.filter(e=>!e.draggable).length>0,Le=i.selectedElements.filter(e=>!e.visible).length>0,Fe=te(i),Ve=1!==i.selectedElements.length||"group"===(null===(oe=i.selectedElements[0])||void 0===oe?void 0:oe.type),ze=null!==(re=ee(Fe))&&void 0!==re?re:0,Ue=e=>Ve&&Fe.length>0&&Fe.every(t=>((e,t,n)=>{if(!q(e.rotation-n)){return!1}switch(e.type){case"text":return"x"===t&&!Z(e);case"figure":case"video":case"table":return!0;case"image":case"svg":case"gif":return!e.keepRatio;default:return!1}})(t,e,ze)),Ze=!Ve&&i.selectedElements[0].keepRatio,_e=Ue("x"),He=Ue("y"),Je=()=>{var t,n,o;const r=Xe.current;if(!r){return[]}const l=r.getStage();if(!l){return[]}const s=te(i),a=s.map(e=>l.findOne("#"+e.id)).filter(e=>!!e),c=1===i.selectedElements.length,d=c&&(null===(t=i.selectedElements[0])||void 0===t?void 0:t.type)||"many",h=ne[d],u=Object.assign(Object.assign({},me),h||{});h&&("svg"!==d&&"image"!==d&&"gif"!==d||i.selectedElements[0].keepRatio||(u.enabledAnchors=me.enabledAnchors),"text"===d&&H.textVerticalResizeEnabled&&(u.enabledAnchors=null===(n=ne.text.enabledAnchors)||void 0===n?void 0:n.concat(["bottom-center","top-center"])),"text"===d&&c&&i.selectedElements[0].curveEnabled&&(u.enabledAnchors=ne.many.enabledAnchors)),(_e||He)&&(u.enabledAnchors=[...u.enabledAnchors||[],..._e?["middle-left","middle-right"]:[],...He?["top-center","bottom-center"]:[]]);const{boundBoxFunc:m}=ue,g=e(ue,["boundBoxFunc"]);r.setAttrs(Object.assign(Object.assign(Object.assign({},u),g),{name:z})),We&&r.enabledAnchors([]),Ge&&r.rotateEnabled(!1);const f=r.nodes();if(f.length===a.length&&a.every((e,t)=>f[t]===e)||r.nodes(a),Ve&&a.length){const e=ee(s);null===e||q(r.rotation()-e)||(r.rotation(e),r.forceUpdate())}return null===(o=r.getLayer())||void 0===o||o.batchDraw(),a};o.useLayoutEffect(()=>{var e;if(null===(e=Xe.current)||void 0===e?void 0:e.isTransforming()){return}const t=Je();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),A())},[i.selectedShapes,Ce,We,Le,Ge,Te,_e,He,Ze,ze,ue]);const Ne=()=>{var e,t;const n=Xe.current;if(!n){return}if(!n.isTransforming()){return void je({})}if(!n.nodes().length||!n.isTransforming()){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();je({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})},Ke=o.useCallback(()=>{var e;if(null===(e=Xe.current)||void 0===e?void 0:e.isTransforming()){return}const t=Xe.current;if(!t||!t.nodes().length){return void De(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),h=-Math.cos(t)*(c+n),u=d*Math.cos(a)-h*Math.sin(a),m=d*Math.sin(a)+h*Math.cos(a);return{x:o+u,y:r+m}},d=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(d(c(0))){return 0}for(const h of[180,270,90]){if(d(c(h))){return h}}return 0}(n,{top:(null==N?void 0:N.top)||0,bottom:x-((null==N?void 0:N.bottom)||0),left:(null==N?void 0:N.left)||0,right:h-((null==N?void 0:N.right)||0)},ue.rotateAnchorOffset,t.rotation());De(i)},[N,h,x]);o.useEffect(()=>{var e;null===(e=Xe.current)||void 0===e||e.update(),Ne(),Ke()},[i.scale,Ke]),o.useEffect(()=>{Ke()},[i.selectedShapes,Ke]);const $e=l(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),Qe=o.useRef(!1),qe=e=>{const t=Se.current;if(!t||i.role===X.VIEWER||i.activePage!==r||i.isPlaying||"button"in e.evt&&0!==e.evt.button||1!==t.getPointersPositions().length||!e.target.findAncestor(".elements-container")||!e.target.findAncestor(".element",!0)||!i.selectedShapes.length||i.selectedShapes.some(e=>!e.draggable)){return}const n=t.getPointerPosition();if(!n){return}const o=Je();if(o.some(t=>t===e.target||t.isAncestorOf(e.target))){return}const l=o.find(e=>!!(e.draggable()&&e.isVisible()&&e.isListening())&&(e instanceof Y.Shape?[e]:e instanceof Y.Container?e.find("Shape"):[]).some(e=>e.intersects(n)));if(l){for(const[t,n]of Y.DD._dragElements){"ready"!==n.dragStatus||n.node!==e.target&&!n.node.isAncestorOf(e.target)||Y.DD._dragElements.delete(t)}l._createDragElement(e),"touches"in e.evt&&e.evt.cancelable&&l.preventDefault()&&e.evt.preventDefault()}},et=s(e=>{var t,n,o,r,l,s,a;if(i.role===X.VIEWER){return}Qe.current=!1;const c=e.target.findAncestor(".elements-container"),d=e.target.findAncestor("Transformer"),h=e.target.findAncestor(".page-abs-container");if(c||d||h){return}const u=null===(t=e.target.getStage())||void 0===t?void 0:t.getPointerPosition();u&&(u.x-=null!==(o=null===(n=e.target.getStage())||void 0===n?void 0:n.x())&&void 0!==o?o:0,u.y-=null!==(l=null===(r=e.target.getStage())||void 0===r?void 0:r.y())&&void 0!==l?l:0,$e.visible=!0,$e.x1=u.x,$e.y1=u.y,$e.x2=u.x,$e.y2=u.y,(null!==(a=null===(s=e.target.getStage())||void 0===s?void 0:s.getPointersPositions().length)&&void 0!==a?a:0)>=2&&($e.visible=!1))});(({stageRef:e,containerRef:t,viewportSize:n})=>{o.useEffect(()=>{var n;const i=null===(n=t.current)||void 0===n?void 0:n.closest(".polotno-workspace-inner");function o(){var n;if(!e.current){return}const o=null===(n=t.current)||void 0===n?void 0:n.getBoundingClientRect(),r=null==i?void 0:i.getBoundingClientRect();if(!o||!r){return}const l=Math.max(0,r.left-o.left-100),s=Math.max(0,r.top-o.top-100);e.current.position({x:-l,y:-s}),e.current.container().style.transform=`translate(${l}px, ${s}px)`}return o(),null==i||i.addEventListener("scroll",o),()=>{null==i||i.removeEventListener("scroll",o)}},[n.width,n.height])})({stageRef:Se,containerRef:ke,viewportSize:j}),o.useEffect(()=>{const e=s(e=>{var t,n,i,o,r,l;if(!$e.visible){return}null===(t=Se.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=Se.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(o=null===(i=Se.current)||void 0===i?void 0:i.x())&&void 0!==o?o:0,s.y-=null!==(l=null===(r=Se.current)||void 0===r?void 0:r.y())&&void 0!==l?l:0):s={x:$e.x2,y:$e.y2},$e.x2=s.x,$e.y2=s.y}),t=s(()=>{if(!$e.visible){return}if(!Se.current){return}const e=Se.current.findOne(".selection"),t=e?e.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(t.width&&t.height){const e=[{x:t.x,y:t.y},{x:t.x+t.width,y:t.y},{x:t.x+t.width,y:t.y+t.height},{x:t.x,y:t.y+t.height}],n=[];Se.current.find(".element").forEach(t=>{var o;const r=i.getElementById(t.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=t.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const c=T(r.a).map(e=>a.point(e));G(e,c)&&n.push(r.top.id)});const o=[...new Set(n)];Qe.current=!0,i.selectElements(o)}$e.visible=!1});return window.addEventListener("mousemove",e),window.addEventListener("touchmove",e),window.addEventListener("mouseup",t,{capture:!0}),window.addEventListener("touchend",t,{capture:!0}),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("touchmove",e),window.removeEventListener("mouseup",t),window.removeEventListener("touchend",t)}},[]);const tt=o.useCallback(e=>{if(!i.selectedElements[0]){return}if(e&&i.selectedElementsIds.includes(e)){return}const t=W(i.selectedShapes),n=e?i.getElementById(e):{x:0,y:0,width:r.computedWidth,height:r.computedHeight,rotation:0},o=C(n),l=[];t.minX>o.maxX&&l.push({distance:t.minX-o.maxX,box1:t,box2:o,points:[{x:t.minX,y:t.minY+t.height/2},{x:o.maxX,y:t.minY+t.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),t.maxX<o.minX&&l.push({distance:o.minX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:t.minY+t.height/2},{x:o.minX,y:t.minY+t.height/2},{x:o.minX,y:o.minY+o.height/2}]}),t.minY>o.maxY&&l.push({box1:t,box2:o,distance:t.minY-o.maxY,points:[{x:t.minX+t.width/2,y:t.minY},{x:t.minX+t.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]}),t.maxY<o.minY&&l.push({box1:t,box2:o,distance:o.minY-t.maxY,points:[{x:t.minX+t.width/2,y:t.maxY},{x:t.minX+t.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]});const s=t.minX>=o.minX&&t.maxX<=o.maxX&&t.minY>=o.minY&&t.maxY<=o.maxY;if(s&&(l.push({distance:t.minX-o.minX,box1:t,box2:o,points:[{x:t.minX,y:t.minY+t.height/2},{x:o.minX,y:t.minY+t.height/2},{x:o.minX,y:o.minY+o.height/2}]}),l.push({distance:o.maxX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:t.minY+t.height/2},{x:o.maxX,y:t.minY+t.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),l.push({box1:t,box2:o,distance:t.minY-o.minY,points:[{x:t.minX+t.width/2,y:t.minY},{x:t.minX+t.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]}),l.push({box1:t,box2:o,distance:o.maxY-t.maxY,points:[{x:t.minX+t.width/2,y:t.maxY},{x:t.minX+t.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]})),0===l.length&&!s){const e=t.minX<o.maxX&&t.maxX>o.minX,n=t.minY<o.maxY&&t.maxY>o.minY;if(e&&n){const e=(Math.max(t.minY,o.minY)+Math.min(t.maxY,o.maxY))/2,n=(Math.max(t.minX,o.minX)+Math.min(t.maxX,o.maxX))/2;t.minX<o.minX?l.push({distance:o.minX-t.minX,box1:t,box2:o,points:[{x:t.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}):l.push({distance:t.minX-o.minX,box1:t,box2:o,points:[{x:t.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}),t.maxX>o.maxX?l.push({distance:t.maxX-o.maxX,box1:t,box2:o,points:[{x:o.maxX,y:e},{x:t.maxX,y:e},{x:o.maxX,y:e}]}):l.push({distance:o.maxX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:e},{x:o.maxX,y:e},{x:o.maxX,y:e}]}),t.minY<o.minY?l.push({box1:t,box2:o,distance:o.minY-t.minY,points:[{x:n,y:t.minY},{x:n,y:o.minY},{x:n,y:o.minY}]}):l.push({box1:t,box2:o,distance:t.minY-o.minY,points:[{x:n,y:t.minY},{x:n,y:o.minY},{x:n,y:o.minY}]}),t.maxY>o.maxY?l.push({box1:t,box2:o,distance:t.maxY-o.maxY,points:[{x:n,y:o.maxY},{x:n,y:t.maxY},{x:n,y:o.maxY}]}):l.push({box1:t,box2:o,distance:o.maxY-t.maxY,points:[{x:n,y:t.maxY},{x:n,y:o.maxY},{x:n,y:o.maxY}]})}}JSON.stringify(Ie)!==JSON.stringify(l)&&Pe(l)},[i,r]);o.useEffect(()=>{i.selectedElements[0]&&i.distanceGuidesVisible?tt():Ie&&Pe(null)},[i.distanceGuidesVisible,tt,i.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|")]);const nt=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=ke.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{nt.current=!0,clearTimeout(t),t=setTimeout(()=>{nt.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const e=Se.current;if(!e){return}const t=e.container(),n=new Map,o=e=>{var t,i;n.has(e.target)||[e.target,...null!==(i=null===(t=Xe.current)||void 0===t?void 0:t.nodes())&&void 0!==i?i:[]].forEach(e=>{n.has(e)||n.set(e,{x:e.x(),y:e.y()})})},r=e=>{e.target===Xe.current?n.clear():n.delete(e.target)};e.on("dragstart",o),e.on("dragend",r);const l=e=>{e.touches.length>=2&&a(()=>{var e,t;const o=new Map(n);(null===(e=Xe.current)||void 0===e?void 0:e.isDragging())&&Xe.current.stopDrag(),null===(t=Xe.current)||void 0===t||t.nodes().forEach(e=>{var t;const n=o.get(e);e.isDragging()&&e.stopDrag(),n&&(e.setAttrs(n),null===(t=i.getElementById(e.id()))||void 0===t||t.set({x:n.x,y:n.y}))})})};return t.addEventListener("touchstart",l,{passive:!0}),t.addEventListener("touchmove",l,{passive:!0}),()=>{e.off("dragstart",o),e.off("dragend",r),t.removeEventListener("touchstart",l),t.removeEventListener("touchmove",l)}},[i]);const it=e=>{if(i.role===X.VIEWER){return}if(i.activePage!==r&&r.select(),nt.current){return}if(Qe.current){return}const t=e.evt.shiftKey,n=e.evt.ctrlKey||e.evt.metaKey,o=t||n,l=e.target.findAncestor(".elements-container"),s=e.target.findAncestor(".page-abs-container"),a=e.target.findAncestor("Transformer"),c=e.target.findAncestor(".page-container",!0);if(!(o||l||a||s||c||$e.visible)){return i.selectElements([]),void i.selectPages([])}if(n&&!t){const e=Se.current,t=null==e?void 0:e.getPointerPosition();if(!e||!t){return}const n=e.getAllIntersections(t),o=new Set,r=[];for(const s of[...n].reverse()){const e=s.findAncestor(".element",!0);if(!e){continue}const t=i.getElementById(e.id()),n=null==t?void 0:t.top;n&&(o.has(n.id)||(o.add(n.id),r.push(n.id)))}if(0===r.length){return}let l;if(1===i.selectedElementsIds.length){const e=i.selectedElementsIds[0],t=r.indexOf(e);l=-1===t?r[0]:r[(t+1)%r.length]}else{l=r[0]}return i.selectElements([l]),void i.selectPages([])}const d=e.target.findAncestor(".element-container",!0),h=(null==d?void 0:d.findOne(".element"))||e.target.findAncestor(".element",!0),u=i.getElementById(null==h?void 0:h.id()),m=null==u?void 0:u.top;let g;if("group"===P){g=null==m?void 0:m.id}else if(m){const e=i.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===m.id});g=e?null==u?void 0:u.id:null==m?void 0:m.id}else{g=void 0}const f=null==u?void 0:u.id;if(f&&i.selectedElementsIds.indexOf(f)>=0&&!t){return}const p=!!g&&i.selectedElementsIds.indexOf(g)>=0,x=e.target.findAncestor(".page-container",!0);if(g&&t&&!p){const e=i.selectedElementsIds.concat([g]),t=new Set(e),n=e.filter(e=>{const n=i.getElementById(e);if(!n){return!1}for(let i=n.parent;i;i=null==i?void 0:i.parent){if(t.has(i.id)){return!1}}return!0});i.selectElements(n),i.selectPages([])}else{g&&t&&p?i.selectElements(i.selectedElementsIds.filter(e=>e!==g)):!g||t||p?g&&!t&&p||(x?(t||i.selectElements([]),i.selectPages([r.id])):i.selectPages([])):(i.selectElements([g]),i.selectPages([]))}};M(Xe,i);const ot=i.activePage===r,rt=i._selectedPagesIds.includes(r.id),lt=null==S?void 0:S.PageControls,st=null==S?void 0:S.Tooltip,at=null==S?void 0:S.ContextMenu,ct=1/i.scale,dt=0/i.scale;return n("div",{ref:ke,onDragOver:e=>e.preventDefault(),onDrop:e=>{if(e.preventDefault(),i.role===X.VIEWER){return}if(!Se.current){return}Se.current.setPointersPositions(e);const t=Se.current.findOne(".elements-container"),n=null==t?void 0:t.getRelativePointerPosition(),o=Se.current.getPointerPosition(),l=Se.current.getAllIntersections(o).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(l.reverse())].map(e=>i.getElementById(e.id())).filter(e=>!!e),a=s[0];he&&n&&(he(n,a,{elements:s,page:r}),he=null)},style:{position:"relative",width:h+"px",height:x+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+(ot?" active-page":""),children:[t($,{ref:Se,eventBatchFunc:a,width:Math.min(h,j.width+200),height:Math.min(j.height+200,x),onClick:it,onTap:it,onContextMenu:e=>{if(e.evt.preventDefault(),i.role===X.VIEWER){return}const t=e.target.findAncestor(".element",!0),n=i.getElementById(null==t?void 0:t.id()),o=null==n?void 0:n.top,r=null==o?void 0:o.id;if(!r){return i.selectElements([]),void Be.close()}i.selectedElementsIds.indexOf(r)>=0||i.selectElements([r]),Be.open({x:e.evt.clientX,y:e.evt.clientY})},onMouseDown:e=>{qe(e),et(e)},onTouchStart:qe,onMouseMove:e=>{if(!i.distanceGuidesVisible&&!e.evt.altKey){return void(Ie&&Pe(null))}const t=e.target.findAncestor(".element",!0),n=null==t?void 0:t.id();tt(n)},onDragStart:e=>{var t;const n=e.target.getStage();if(n&&n.getPointersPositions().length>=2){return void e.target.stopDrag()}const o=e.target.findAncestor(".element",!0);if(o){const n=i.getElementById(null==o?void 0:o.id()),r=null==n?void 0:n.top,l=null==r?void 0:r.id;if(!(n&&i.selectedElementsIds.indexOf(n.id)>=0||l&&i.selectedElementsIds.indexOf(l)>=0)&&l){i.selectElements([l]);const n=Je();e.target.stopDrag(),n.forEach(t=>{t.isDragging()||t.startDrag(e)}),null===(t=Xe.current)||void 0===t||t.startDrag(e),A()}}Ie&&Pe(null)},onDragMove:e=>{const t=Xe.current;t&&e.target===t&&t.nodes().forEach(e=>{const t=i.getElementById(e.id());!t||t.x===e.x()&&t.y===e.y()||t.set({x:e.x(),y:e.y()})})},pageId:r.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:n(m,{children:[t(ie,{width:h,height:x,fill:ae.workspaceBackgroundColor}),t(d,{x:we,y:Ee,scaleX:i.scale,scaleY:i.scale,name:"page-container",children:n(d,{name:"page-container-2",children:[t(d,{name:"page-background-group",x:xe.left,y:xe.top,children:t(le,{x:-pe.left,y:-pe.top,width:r.computedWidth+pe.left+pe.right,height:r.computedHeight+pe.top+pe.bottom,background:r.background,transparencyBackgroundColor:ae.transparencyBackgroundColor,name:"page-background",preventDefault:!1,scale:i.scale})}),n(d,{x:xe.left,y:xe.top,name:"elements-container",listening:!i.isPlaying&&i.role!==X.VIEWER,children:[t(f,{name:"elements-area",width:r.computedWidth,height:r.computedHeight,listening:!1}),t(de,{elements:r.children,store:i})]}),t(p,{name:"bleed",fill:ae.bleedColor,listening:!1,visible:i.bleedVisible&&(xe.top>0||xe.right>0||xe.bottom>0||xe.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:i,bottom:o,left:l}=pe,s=r.computedWidth,a=r.computedHeight,c=e._context;c.beginPath(),c.rect(0,0,l+s+i,n+a+o),c.rect(l,n,s,a),c.fillStyle=t.fill(),c.fill("evenodd")}}),R()===ge&&t(fe,{name:"hit-detection",x:-ct/2-dt,y:-ct/2-dt,width:ve+ct+2*dt,height:ye+ct+2*dt})]})}),t(g,{name:"workspace-background",points:[0,0,h,0,h,x,0,x,0,0,we,Ee,we,x-Ee,h-we,x-Ee,h-we,Ee,we,Ee],listening:!1,closed:!0,fill:ae.workspaceBackgroundColor}),t(d,{x:we,y:Ee,scaleX:i.scale,scaleY:i.scale,children:t(f,{name:"page-highlight",hideInExport:!0,x:-ct/2-dt,y:-ct/2-dt,width:ve+ct+2*dt,height:ye+ct+2*dt,stroke:rt?ae.activePageBorderColor:ae.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),n(d,{x:we+xe.left*i.scale,y:Ee+xe.top*i.scale,scaleX:i.scale,scaleY:i.scale,name:"page-abs-container",children:[t(w,{ref:Xe,name:z,rotateAnchorAngle:Re,onDragStart:e=>{var t;const n=e.target.getStage();n&&n.getPointersPositions().length>=2?e.target.stopDrag():((null===(t=e.evt)||void 0===t?void 0:t.altKey)&&I&&i.selectedElements.forEach(e=>{const t=e.clone({},{skipSelect:!0}),n=r.children.indexOf(e);r.setElementZIndex(t.id,n)}),Me.current||(Me.current=!0,i.history.startTransaction()))},onDragEnd:()=>{Me.current&&(i.history.endTransaction(),Me.current=!1),Ke()},onTransformStart:()=>{i.history.startTransaction(),Ye.current=new Map(Fe.map(e=>[e.id,{width:e.width,height:e.height}])),Pe(null)},boundBoxFunc:(e,t)=>{var n,o,r,l;const s=null!==(o=null===(n=ue.boundBoxFunc)||void 0===n?void 0:n.call(ue,e,t))&&void 0!==o?o:t,a=Math.abs(s.width)<1||Math.abs(s.height)<1,c=Math.abs(e.width)<1||Math.abs(e.height)<1;if(a&&!c){return e}const d=i.selectedElements;if(1===d.length&&"table"===(null===(r=d[0])||void 0===r?void 0:r.type)){const t=d[0],n=Math.abs(e.width),i=Math.abs(e.height);if(n>0&&i>0){const o=Math.abs(s.width)/n,r=Math.abs(s.height)/i,l=t.width*o,a=t.height*r,c=Object.assign({},s);return l<t.minWidth&&(c.width=e.width),a<t.minHeight&&(c.height=e.height),_(c,s,e)}}return Ve&&Fe.length>0?((e,t,n,i,o)=>{const r=Math.abs(e.width),l=Math.abs(e.height);if(0===r||0===l){return t}const s=Math.abs(t.width)/r,a=Math.abs(t.height)/l,c=!V(n),d=e=>i.some(t=>{var n;const i=null==o?void 0:o.get(t.id),r=null!==(n="x"===e?null==i?void 0:i.width:null==i?void 0:i.height)&&void 0!==n?n:"x"===e?t.width:t.height,l="x"===e?t.width:t.height,d="x"===e?s:a,h=((e,t,n,i)=>{if("table"===e.type){const n=e;return"x"===t?n.minWidth:n.minHeight}return"text"===e.type&&"x"===t&&n?U(i,e.fontSize):1})(t,e,c,r);return r>=h&&l*d<h}),h=Object.assign({},t),u=t.width*e.width<0,m=t.height*e.height<0,g=u||d("x"),f=m||d("y"),p=f||!c&&g;return(g||!c&&f)&&(h.width=e.width),p&&(h.height=e.height),_(h,t,e)})(e,s,(null===(l=Xe.current)||void 0===l?void 0:l.getActiveAnchor())||"",Fe,Ye.current):s},onTransform:e=>{const t=Xe.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{Ne()},0)},onTransformEnd:e=>{je({}),i.history.endTransaction(),Ye.current=null,Ke(),Je()},visible:!i.isPlaying}),Ie&&Ie.map(({points:e,distance:o,box1:r,box2:l},s)=>n(d,{name:"distances-container",hideInExport:!0,listening:!1,children:[t(f,Object.assign({},r,{stroke:ae.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(f,Object.assign({},l,{stroke:ae.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(g,{points:[e[0].x,e[0].y,e[1].x,e[1].y],stroke:ae.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),t(g,{points:[e[1].x,e[1].y,e[2].x,e[2].y],stroke:ae.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(u,{x:(e[0].x+e[1].x)/2,y:(e[0].y+e[1].y)/2,offsetY:-10,scaleX:1/i.scale,scaleY:1/i.scale,children:[t(b,{cornerRadius:5,fill:ae.distanceGuideStyle.labelFill,pointerDirection:"down"}),t(y,{align:"center",verticalAlign:"middle",fill:ae.distanceGuideStyle.labelTextFill,padding:5,text:F({unit:i.unit,dpi:i.dpi,px:o})})]})]},s)),r._rendering&&n(d,{children:[t(f,{width:ve,height:ye,fill:"rgba(255,255,255,0.9)"}),t(y,{text:"Rendering...",fontSize:60,width:ve,height:ye,align:"center",verticalAlign:"middle"})]}),st&&t(st,{components:S,store:i,page:r,stageRef:Se,tooltipSafeArea:J}),at&&t(K,{children:t(at,Object.assign({components:S,store:i},Be.props))}),t(O,{store:i,pageId:r.id})]}),t(k,{store:i,page:r,width:h,height:x,scale:i.scale,xPadding:we,yPadding:Ee,bleedLeft:xe.left,bleedTop:xe.top}),t(ce,Object.assign({},Oe,{store:i})),t(se,{selection:$e}),D()>0&&n(d,{hideInExport:!0,children:[t(be,{name:"cache-bounds",x:h-(1===D()?250:210),y:x-(1===D()?95:45)}),t(be,{name:"cache-bounds-t",x:10,y:10})]}),B()&&t(y,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:h-170,y:x-18,onMouseEnter:e=>{e.target.getStage().container().style.cursor="pointer"},onMouseLeave:e=>{e.target.getStage().container().style.cursor=""},onTouchStart:e=>{e.cancelBubble=!0},onMouseDown:e=>{e.cancelBubble=!0},onClick:()=>{window.open("https://polotno.com","_blank","noopener")},onTap:()=>{window.open("https://polotno.com","_blank","noopener")}}),t(d,{name:"line-guides"})]})}),Ae&<&&t(lt,{store:i,page:r,xPadding:we,yPadding:Ee,layout:L})]})});
|
|
1
|
+
var e=this&&this.__rest||function(e,t){var n={};for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++){t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(n[i[o]]=e[i[o]])}}return n};import{jsx as t,jsxs as n,Fragment as i}from"react/jsx-runtime";import o from"react";import{observer as r,useLocalObservable as l}from"mobx-react-lite";import{action as s,runInAction as a}from"mobx";import{ZERO_BLEED as d}from"@polotno/schema";import{Group as c,Image as u,Label as h,Layer as m,Line as g,Rect as f,Shape as p,Stage as x,KonvaRenderer as v,Tag as b,Text as y,Transformer as w}from"react-konva";import{useWorkspaceStyle as E}from"./workspace-style.js";import{ROLES as X}from"../model/store.js";import Y from"konva";import S from"./element.js";import{DrawingLayer as k}from"./drawing-layer.js";import{ensureDragOrder as A,useSnap as M}from"./use-transformer-snap.js";import{useCanvasImage as I,useImageLoader as O}from"./image-element.js";import{FadeOverlay as P}from"./fade-overlay.js";import{getCrop as j}from"../utils/crop.js";import{___ as R,____ as B,isCreditVisible as D}from"../utils/validate-key.js";import{getClientRect as T,getCorners as C,getTotalClientRect as W,polygonsIntersect as G}from"../utils/math.js";import{pxToUnitRounded as L,pxToUnitString as F}from"../utils/unit.js";import{isCornerAnchor as V,MAIN_TRANSFORMER_NAME as z,textMinResizeWidth as U,textMinWidthIsMeasured as Z}from"../utils/px.js";import{restoreHeldPoint as _}from"./transform-box.js";import{flags as H}from"../utils/flags.js";import{useColor as J}from"./use-color.js";import{isGradient as N}from"@polotno/core/gradient";import{Html as K}from"react-konva-utils";const $=o.version.startsWith("18.")?o.forwardRef((e,n)=>(o.useLayoutEffect(()=>{v.flushSync()}),t(x,Object.assign({},e,{ref:n})))):x,Q={enabledAnchors:["top-left","top-center","top-right","middle-left","bottom-left","bottom-right","bottom-center","middle-right"],rotateEnabled:!0,rotationSnaps:[0,45,90,135,180,225,270,315],ignoreStroke:!0,flipEnabled:!1,keepRatio:!0},q=e=>{const t=(e%180+180)%180;return t<.1||180-t<.1},ee=e=>{if(!e.length){return null}const t=q(e[0].rotation)?0:e[0].rotation;return e.every(e=>q(e.rotation-t))?t:null},te=e=>e.selectedShapes.filter(e=>!e._cropModeEnabled),ne={text:{enabledAnchors:["top-left","top-right","middle-left","bottom-left","bottom-right","middle-right"]},svg:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},gif:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},line:{enabledAnchors:[],borderEnabled:!1,rotateEnabled:!1},image:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},many:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},group:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},table:{keepRatio:!1,enabledAnchors:["top-left","top-center","top-right","middle-left","middle-right","bottom-left","bottom-center","bottom-right"]}};export function registerTransformerAttrs(e,t){ne[e]=ne[e]||t,Object.assign(ne[e],t)}const ie=e=>t(f,Object.assign({},e,{preventDefault:!1})),oe=n=>{var{url:i}=n,o=e(n,["url"]);const[r,l]=I(i,"anonymous"),s=r?j(r,{width:o.width,height:o.height},"center-middle"):{};return O(l,i,"page background"),t(u,Object.assign({image:r},o,s))},re=e=>{const n=J({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return t(f,Object.assign({},e,n))},le=n=>{var{background:i,transparencyBackgroundColor:r}=n,l=e(n,["background","transparencyBackgroundColor"]);const s=o.useMemo(()=>{if(N(i)){return!0}const e=Y.Util.colorToRGBA(i);return!e||e.a<1},[i]),a=o.useMemo(()=>{if(!s||r){return}const e=document.createElement("canvas"),t=30;e.width=60,e.height=60;const n=e.getContext("2d");return n&&(n.fillStyle="black",n.fillRect(t,0,t,t),n.fillRect(0,t,t,t)),e},[s,r]);return s?t(f,r?Object.assign({fill:r},l,{hideInExport:!0}):Object.assign({fillPatternImage:a},l,{opacity:.1,hideInExport:!0})):null},se=n=>{var{background:i}=n,r=e(n,["background"]);return o.useMemo(()=>!!Y.Util.colorToRGBA(i)||N(i),[i])?t(re,Object.assign({fill:i},r)):t(oe,Object.assign({url:i},r))},ae=r(({selection:e})=>{const{selectionRectStyle:n}=E();return e.visible?t(f,{name:"selection",x:Math.min(e.x1,e.x2),y:Math.min(e.y1,e.y2),width:Math.abs(e.x1-e.x2),height:Math.abs(e.y1-e.y2),fill:n.fill,stroke:n.stroke,strokeWidth:n.strokeWidth}):null});function de(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const ce=r(({x:e,y:o,width:r,height:l,rotation:s,anchor:a,store:d})=>{const{transformLabelStyle:c}=E();if(void 0===e||void 0===o||void 0===r||void 0===l||void 0===s||void 0===a){return null}const u=T({x:e,y:o,width:r,height:l,rotation:Y.Util.radToDeg(s)}),m=(l/2+70)*Math.cos(s-Math.PI/2),g=(l/2+70)*Math.sin(s-Math.PI/2),f=L({unit:d.unit,dpi:d.dpi,px:r/d.scale,precious:"px"===d.unit?0:1})+" x "+L({unit:d.unit,dpi:d.dpi,px:l/d.scale,precious:"px"===d.unit?0:1})+("px"===d.unit?"":" "+d.unit);return n(i,{children:[n(h,{x:(u.minX+u.maxX)/2+m,y:(u.minY+u.maxY)/2+g,offsetX:14,offsetY:14,visible:"rotater"===a,children:[t(b,{cornerRadius:5,fill:c.fill}),t(y,{align:"center",verticalAlign:"middle",fill:c.textFill,padding:8,text:de(s).toString()+"°"})]}),n(h,{x:(u.minX+u.maxX)/2,y:u.maxY+20,visible:"rotater"!==a,children:[t(b,{cornerRadius:5,fill:c.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),t(y,{align:"center",verticalAlign:"middle",fill:c.textFill,padding:8,text:f})]})]})}),ue=r(({elements:e,store:n})=>{const i=e.filter(e=>e.alwaysOnTop),r=e.filter(e=>!e.alwaysOnTop).concat(i);return t(o.Fragment,{children:r.map(e=>t(S,{store:n,element:e,onClick:()=>{console.warn("Polotno warning: onClick callback is deprecated. Just stop using it. Polotno will do selection automatically.")}},e.id))})});export const useContextMenu=({store:e})=>{const[t,n]=o.useState(!1),[i,r]=o.useState({x:0,y:0}),l=o.useCallback(e=>{n(!0),r(e)},[]);return{open:l,close:o.useCallback(()=>{n(!1)},[]),props:{isOpen:t,offset:i,setIsOpen:n}}};let he=null;export const registerNextDomDrop=e=>{he=e};const me=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),ge=atob("I2MwMzkyYg=="),fe=atob("djAuOS4y"),pe=e=>t(i,{children:n(h,{fill:ge,height:200,children:[t(b,{fill:ge}),t(y,Object.assign({},e,{fill:"white",text:me,height:void 0,padding:10,fontSize:20}))]})}),xe=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),ve=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),be=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),ye=e=>{const i=1===B(),o=i?ve:xe,r=i?230:195;return n(c,Object.assign({},e,{listening:!1,children:[t(f,{fill:ge,width:r,height:24,cornerRadius:2}),t(y,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),i&&t(y,{text:be,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default r(({store:i,page:r,width:u,height:x,pageControlsEnabled:v,components:S,altCloneEnabled:I,groupSelectionMode:O="drill",viewportSize:j,layout:L="vertical",tooltipSafeArea:J,transformerSafeArea:N})=>{var oe,re;const de=E(),me=de.transformerStyle,ge=Q,xe=r.computedBleed,ve=i.bleedVisible?xe:d,be=r.computedWidth+ve.left+ve.right,we=r.computedHeight+ve.top+ve.bottom,Ee=(u-be*i.scale)/2,Xe=(x-we*i.scale)/2,Ye={x:-xe.left,y:-xe.top,width:r.computedWidth+xe.left+xe.right,height:r.computedHeight+xe.top+xe.bottom,background:r.background,name:"page-background",preventDefault:!1},Se=o.useRef(null),ke=o.useRef(null),Ae=o.useRef(null),Me=o.useRef(null),Ie=null==v||v,Oe=o.useRef(!1),[Pe,je]=o.useState(null),[Re,Be]=o.useState({}),[De,Te]=o.useState(0),Ce=useContextMenu({store:i}),We=i.selectedElements.find(e=>e._cropModeEnabled),Ge=i.selectedElements.find(e=>e.curveEnabled),Le=i.selectedShapes.filter(e=>!e.resizable).length>0,Fe=i.selectedShapes.filter(e=>!e.draggable).length>0,Ve=i.selectedElements.filter(e=>!e.visible).length>0,ze=te(i),Ue=1!==i.selectedElements.length||"group"===(null===(oe=i.selectedElements[0])||void 0===oe?void 0:oe.type),Ze=null!==(re=ee(ze))&&void 0!==re?re:0,_e=e=>Ue&&ze.length>0&&ze.every(t=>((e,t,n)=>{if(!q(e.rotation-n)){return!1}switch(e.type){case"text":return"x"===t&&!Z(e);case"figure":case"video":case"table":return!0;case"image":case"svg":case"gif":return!e.keepRatio;default:return!1}})(t,e,Ze)),He=!Ue&&i.selectedElements[0].keepRatio,Je=_e("x"),Ne=_e("y"),Ke=()=>{var t,n,o;const r=Se.current;if(!r){return[]}const l=r.getStage();if(!l){return[]}const s=te(i),a=s.map(e=>l.findOne("#"+e.id)).filter(e=>!!e),d=1===i.selectedElements.length,c=d&&(null===(t=i.selectedElements[0])||void 0===t?void 0:t.type)||"many",u=ne[c],h=Object.assign(Object.assign({},ge),u||{});u&&("svg"!==c&&"image"!==c&&"gif"!==c||i.selectedElements[0].keepRatio||(h.enabledAnchors=ge.enabledAnchors),"text"===c&&H.textVerticalResizeEnabled&&(h.enabledAnchors=null===(n=ne.text.enabledAnchors)||void 0===n?void 0:n.concat(["bottom-center","top-center"])),"text"===c&&d&&i.selectedElements[0].curveEnabled&&(h.enabledAnchors=ne.many.enabledAnchors)),(Je||Ne)&&(h.enabledAnchors=[...h.enabledAnchors||[],...Je?["middle-left","middle-right"]:[],...Ne?["top-center","bottom-center"]:[]]);const{boundBoxFunc:m}=me,g=e(me,["boundBoxFunc"]);r.setAttrs(Object.assign(Object.assign(Object.assign({},h),g),{name:z})),Le&&r.enabledAnchors([]),Fe&&r.rotateEnabled(!1);const f=r.nodes();if(f.length===a.length&&a.every((e,t)=>f[t]===e)||r.nodes(a),Ue&&a.length){const e=ee(s);null===e||q(r.rotation()-e)||(r.rotation(e),r.forceUpdate())}return null===(o=r.getLayer())||void 0===o||o.batchDraw(),a};o.useLayoutEffect(()=>{var e;if(null===(e=Se.current)||void 0===e?void 0:e.isTransforming()){return}const t=Ke();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),A())},[i.selectedShapes,We,Le,Ve,Fe,Ge,Je,Ne,He,Ze,me]);const $e=()=>{var e,t;const n=Se.current;if(!n){return}if(!n.isTransforming()){return void Be({})}if(!n.nodes().length||!n.isTransforming()){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();Be({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})},Qe=o.useCallback(()=>{var e;if(null===(e=Se.current)||void 0===e?void 0:e.isTransforming()){return}const t=Se.current;if(!t||!t.nodes().length){return void Te(0)}const n=t.getClientRect();if(!t.getStage()){return}const i=function(e,t,n,i=0){const o=e.x+e.width/2,r=e.y+e.height/2,l=e.width/2,s=e.height/2,a=i*Math.PI/180,d=e=>{const t=e*Math.PI/180,i=(e%360+360)%360;let d;d=0===i||180===i?s:l;const c=Math.sin(t)*(d+n),u=-Math.cos(t)*(d+n),h=c*Math.cos(a)-u*Math.sin(a),m=c*Math.sin(a)+u*Math.cos(a);return{x:o+h,y:r+m}},c=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(c(d(0))){return 0}for(const u of[180,270,90]){if(c(d(u))){return u}}return 0}(n,{top:(null==N?void 0:N.top)||0,bottom:x-((null==N?void 0:N.bottom)||0),left:(null==N?void 0:N.left)||0,right:u-((null==N?void 0:N.right)||0)},me.rotateAnchorOffset,t.rotation());Te(i)},[N,u,x]);o.useEffect(()=>{var e;null===(e=Se.current)||void 0===e||e.update(),$e(),Qe()},[i.scale,Qe]),o.useEffect(()=>{Qe()},[i.selectedShapes,Qe]);const qe=l(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),et=o.useRef(!1),tt=e=>{const t=Ae.current;if(!t||i.role===X.VIEWER||i.activePage!==r||i.isPlaying||"button"in e.evt&&0!==e.evt.button||1!==t.getPointersPositions().length||!e.target.findAncestor(".elements-container")||!e.target.findAncestor(".element",!0)||!i.selectedShapes.length||i.selectedShapes.some(e=>!e.draggable)){return}const n=t.getPointerPosition();if(!n){return}const o=Ke();if(o.some(t=>t===e.target||t.isAncestorOf(e.target))){return}const l=o.find(e=>!!(e.draggable()&&e.isVisible()&&e.isListening())&&(e instanceof Y.Shape?[e]:e instanceof Y.Container?e.find("Shape"):[]).some(e=>e.intersects(n)));if(l){for(const[t,n]of Y.DD._dragElements){"ready"!==n.dragStatus||n.node!==e.target&&!n.node.isAncestorOf(e.target)||Y.DD._dragElements.delete(t)}l._createDragElement(e),"touches"in e.evt&&e.evt.cancelable&&l.preventDefault()&&e.evt.preventDefault()}},nt=s(e=>{var t,n,o,r,l,s,a;if(i.role===X.VIEWER){return}et.current=!1;const d=e.target.findAncestor(".elements-container"),c=e.target.findAncestor("Transformer"),u=e.target.findAncestor(".page-abs-container");if(d||c||u){return}const h=null===(t=e.target.getStage())||void 0===t?void 0:t.getPointerPosition();h&&(h.x-=null!==(o=null===(n=e.target.getStage())||void 0===n?void 0:n.x())&&void 0!==o?o:0,h.y-=null!==(l=null===(r=e.target.getStage())||void 0===r?void 0:r.y())&&void 0!==l?l:0,qe.visible=!0,qe.x1=h.x,qe.y1=h.y,qe.x2=h.x,qe.y2=h.y,(null!==(a=null===(s=e.target.getStage())||void 0===s?void 0:s.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(qe.visible=!1))});(({stageRef:e,containerRef:t,viewportSize:n})=>{o.useEffect(()=>{var n;const i=null===(n=t.current)||void 0===n?void 0:n.closest(".polotno-workspace-inner");function o(){var n;if(!e.current){return}const o=null===(n=t.current)||void 0===n?void 0:n.getBoundingClientRect(),r=null==i?void 0:i.getBoundingClientRect();if(!o||!r){return}const l=Math.max(0,r.left-o.left-100),s=Math.max(0,r.top-o.top-100);e.current.position({x:-l,y:-s}),e.current.container().style.transform=`translate(${l}px, ${s}px)`}return o(),null==i||i.addEventListener("scroll",o),()=>{null==i||i.removeEventListener("scroll",o)}},[n.width,n.height])})({stageRef:Ae,containerRef:Me,viewportSize:j}),o.useEffect(()=>{const e=s(e=>{var t,n,i,o,r,l;if(!qe.visible){return}null===(t=Ae.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=Ae.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(o=null===(i=Ae.current)||void 0===i?void 0:i.x())&&void 0!==o?o:0,s.y-=null!==(l=null===(r=Ae.current)||void 0===r?void 0:r.y())&&void 0!==l?l:0):s={x:qe.x2,y:qe.y2},qe.x2=s.x,qe.y2=s.y}),t=s(()=>{if(!qe.visible){return}if(!Ae.current){return}const e=Ae.current.findOne(".selection"),t=e?e.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(t.width&&t.height){const e=[{x:t.x,y:t.y},{x:t.x+t.width,y:t.y},{x:t.x+t.width,y:t.y+t.height},{x:t.x,y:t.y+t.height}],n=[];Ae.current.find(".element").forEach(t=>{var o;const r=i.getElementById(t.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=t.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const d=C(r.a).map(e=>a.point(e));G(e,d)&&n.push(r.top.id)});const o=[...new Set(n)];et.current=!0,i.selectElements(o)}qe.visible=!1});return window.addEventListener("mousemove",e),window.addEventListener("touchmove",e),window.addEventListener("mouseup",t,{capture:!0}),window.addEventListener("touchend",t,{capture:!0}),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("touchmove",e),window.removeEventListener("mouseup",t),window.removeEventListener("touchend",t)}},[]);const it=o.useCallback(e=>{if(!i.selectedElements[0]){return}if(e&&i.selectedElementsIds.includes(e)){return}const t=W(i.selectedShapes),n=e?i.getElementById(e):{x:0,y:0,width:r.computedWidth,height:r.computedHeight,rotation:0},o=T(n),l=[];t.minX>o.maxX&&l.push({distance:t.minX-o.maxX,box1:t,box2:o,points:[{x:t.minX,y:t.minY+t.height/2},{x:o.maxX,y:t.minY+t.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),t.maxX<o.minX&&l.push({distance:o.minX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:t.minY+t.height/2},{x:o.minX,y:t.minY+t.height/2},{x:o.minX,y:o.minY+o.height/2}]}),t.minY>o.maxY&&l.push({box1:t,box2:o,distance:t.minY-o.maxY,points:[{x:t.minX+t.width/2,y:t.minY},{x:t.minX+t.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]}),t.maxY<o.minY&&l.push({box1:t,box2:o,distance:o.minY-t.maxY,points:[{x:t.minX+t.width/2,y:t.maxY},{x:t.minX+t.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]});const s=t.minX>=o.minX&&t.maxX<=o.maxX&&t.minY>=o.minY&&t.maxY<=o.maxY;if(s&&(l.push({distance:t.minX-o.minX,box1:t,box2:o,points:[{x:t.minX,y:t.minY+t.height/2},{x:o.minX,y:t.minY+t.height/2},{x:o.minX,y:o.minY+o.height/2}]}),l.push({distance:o.maxX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:t.minY+t.height/2},{x:o.maxX,y:t.minY+t.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),l.push({box1:t,box2:o,distance:t.minY-o.minY,points:[{x:t.minX+t.width/2,y:t.minY},{x:t.minX+t.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]}),l.push({box1:t,box2:o,distance:o.maxY-t.maxY,points:[{x:t.minX+t.width/2,y:t.maxY},{x:t.minX+t.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]})),0===l.length&&!s){const e=t.minX<o.maxX&&t.maxX>o.minX,n=t.minY<o.maxY&&t.maxY>o.minY;if(e&&n){const e=(Math.max(t.minY,o.minY)+Math.min(t.maxY,o.maxY))/2,n=(Math.max(t.minX,o.minX)+Math.min(t.maxX,o.maxX))/2;t.minX<o.minX?l.push({distance:o.minX-t.minX,box1:t,box2:o,points:[{x:t.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}):l.push({distance:t.minX-o.minX,box1:t,box2:o,points:[{x:t.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}),t.maxX>o.maxX?l.push({distance:t.maxX-o.maxX,box1:t,box2:o,points:[{x:o.maxX,y:e},{x:t.maxX,y:e},{x:o.maxX,y:e}]}):l.push({distance:o.maxX-t.maxX,box1:t,box2:o,points:[{x:t.maxX,y:e},{x:o.maxX,y:e},{x:o.maxX,y:e}]}),t.minY<o.minY?l.push({box1:t,box2:o,distance:o.minY-t.minY,points:[{x:n,y:t.minY},{x:n,y:o.minY},{x:n,y:o.minY}]}):l.push({box1:t,box2:o,distance:t.minY-o.minY,points:[{x:n,y:t.minY},{x:n,y:o.minY},{x:n,y:o.minY}]}),t.maxY>o.maxY?l.push({box1:t,box2:o,distance:t.maxY-o.maxY,points:[{x:n,y:o.maxY},{x:n,y:t.maxY},{x:n,y:o.maxY}]}):l.push({box1:t,box2:o,distance:o.maxY-t.maxY,points:[{x:n,y:t.maxY},{x:n,y:o.maxY},{x:n,y:o.maxY}]})}}JSON.stringify(Pe)!==JSON.stringify(l)&&je(l)},[i,r]);o.useEffect(()=>{i.selectedElements[0]&&i.distanceGuidesVisible?it():Pe&&je(null)},[i.distanceGuidesVisible,it,i.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|")]);const ot=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=Me.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{ot.current=!0,clearTimeout(t),t=setTimeout(()=>{ot.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const e=Ae.current;if(!e){return}const t=e.container(),n=new Map,o=e=>{var t,i;n.has(e.target)||[e.target,...null!==(i=null===(t=Se.current)||void 0===t?void 0:t.nodes())&&void 0!==i?i:[]].forEach(e=>{n.has(e)||n.set(e,{x:e.x(),y:e.y()})})},r=e=>{e.target===Se.current?n.clear():n.delete(e.target)};e.on("dragstart",o),e.on("dragend",r);const l=e=>{e.touches.length>=2&&a(()=>{var e,t;const o=new Map(n);(null===(e=Se.current)||void 0===e?void 0:e.isDragging())&&Se.current.stopDrag(),null===(t=Se.current)||void 0===t||t.nodes().forEach(e=>{var t;const n=o.get(e);e.isDragging()&&e.stopDrag(),n&&(e.setAttrs(n),null===(t=i.getElementById(e.id()))||void 0===t||t.set({x:n.x,y:n.y}))})})};return t.addEventListener("touchstart",l,{passive:!0}),t.addEventListener("touchmove",l,{passive:!0}),()=>{e.off("dragstart",o),e.off("dragend",r),t.removeEventListener("touchstart",l),t.removeEventListener("touchmove",l)}},[i]);const rt=e=>{if(i.role===X.VIEWER){return}if(i.activePage!==r&&r.select(),ot.current){return}if(et.current){return}const t=e.evt.shiftKey,n=e.evt.ctrlKey||e.evt.metaKey,o=t||n,l=e.target.findAncestor(".elements-container"),s=e.target.findAncestor(".page-abs-container"),a=e.target.findAncestor("Transformer"),d=e.target.findAncestor(".page-container",!0);if(!(o||l||a||s||d||qe.visible)){return i.selectElements([]),void i.selectPages([])}if(n&&!t){const e=Ae.current,t=null==e?void 0:e.getPointerPosition();if(!e||!t){return}const n=e.getAllIntersections(t),o=new Set,r=[];for(const s of[...n].reverse()){const e=s.findAncestor(".element",!0);if(!e){continue}const t=i.getElementById(e.id()),n=null==t?void 0:t.top;n&&(o.has(n.id)||(o.add(n.id),r.push(n.id)))}if(0===r.length){return}let l;if(1===i.selectedElementsIds.length){const e=i.selectedElementsIds[0],t=r.indexOf(e);l=-1===t?r[0]:r[(t+1)%r.length]}else{l=r[0]}return i.selectElements([l]),void i.selectPages([])}const c=e.target.findAncestor(".element-container",!0),u=(null==c?void 0:c.findOne(".element"))||e.target.findAncestor(".element",!0),h=i.getElementById(null==u?void 0:u.id()),m=null==h?void 0:h.top;let g;if("group"===O){g=null==m?void 0:m.id}else if(m){const e=i.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===m.id});g=e?null==h?void 0:h.id:null==m?void 0:m.id}else{g=void 0}const f=null==h?void 0:h.id;if(f&&i.selectedElementsIds.indexOf(f)>=0&&!t){return}const p=!!g&&i.selectedElementsIds.indexOf(g)>=0,x=e.target.findAncestor(".page-container",!0);if(g&&t&&!p){const e=i.selectedElementsIds.concat([g]),t=new Set(e),n=e.filter(e=>{const n=i.getElementById(e);if(!n){return!1}for(let i=n.parent;i;i=null==i?void 0:i.parent){if(t.has(i.id)){return!1}}return!0});i.selectElements(n),i.selectPages([])}else{g&&t&&p?i.selectElements(i.selectedElementsIds.filter(e=>e!==g)):!g||t||p?g&&!t&&p||(x?(t||i.selectElements([]),i.selectPages([r.id])):i.selectPages([])):(i.selectElements([g]),i.selectPages([]))}};M(Se,i);const lt=i.activePage===r,st=i._selectedPagesIds.includes(r.id),at=null==S?void 0:S.PageControls,dt=null==S?void 0:S.Tooltip,ct=null==S?void 0:S.ContextMenu,ut=1/i.scale,ht=0/i.scale;return n("div",{ref:Me,onDragOver:e=>e.preventDefault(),onDrop:e=>{if(e.preventDefault(),i.role===X.VIEWER){return}if(!Ae.current){return}Ae.current.setPointersPositions(e);const t=Ae.current.findOne(".elements-container"),n=null==t?void 0:t.getRelativePointerPosition(),o=Ae.current.getPointerPosition(),l=Ae.current.getAllIntersections(o).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(l.reverse())].map(e=>i.getElementById(e.id())).filter(e=>!!e),a=s[0];he&&n&&(he(n,a,{elements:s,page:r}),he=null)},style:{position:"relative",width:u+"px",height:x+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+(lt?" active-page":""),children:[t($,{ref:Ae,eventBatchFunc:a,width:Math.min(u,j.width+200),height:Math.min(j.height+200,x),onClick:rt,onTap:rt,onContextMenu:e=>{if(e.evt.preventDefault(),i.role===X.VIEWER){return}const t=e.target.findAncestor(".element",!0),n=i.getElementById(null==t?void 0:t.id()),o=null==n?void 0:n.top,r=null==o?void 0:o.id;if(!r){return i.selectElements([]),void Ce.close()}i.selectedElementsIds.indexOf(r)>=0||i.selectElements([r]),Ce.open({x:e.evt.clientX,y:e.evt.clientY})},onMouseDown:e=>{tt(e),nt(e)},onTouchStart:tt,onMouseMove:e=>{if(!i.distanceGuidesVisible&&!e.evt.altKey){return void(Pe&&je(null))}const t=e.target.findAncestor(".element",!0),n=null==t?void 0:t.id();it(n)},onDragStart:e=>{var t;const n=e.target.getStage();if(n&&n.getPointersPositions().length>=2){return void e.target.stopDrag()}const o=e.target.findAncestor(".element",!0);if(o){const n=i.getElementById(null==o?void 0:o.id()),r=null==n?void 0:n.top,l=null==r?void 0:r.id;if(!(n&&i.selectedElementsIds.indexOf(n.id)>=0||l&&i.selectedElementsIds.indexOf(l)>=0)&&l){i.selectElements([l]);const n=Ke();e.target.stopDrag(),n.forEach(t=>{t.isDragging()||t.startDrag(e)}),null===(t=Se.current)||void 0===t||t.startDrag(e),A()}}Pe&&je(null)},onDragMove:e=>{const t=Se.current;t&&e.target===t&&t.nodes().forEach(e=>{const t=i.getElementById(e.id());!t||t.x===e.x()&&t.y===e.y()||t.set({x:e.x(),y:e.y()})})},pageId:r.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:n(m,{children:[t(ie,{width:u,height:x,fill:de.workspaceBackgroundColor}),t(c,{x:Ee,y:Xe,scaleX:i.scale,scaleY:i.scale,name:"page-container",children:n(c,{name:"page-container-2",children:[t(c,{x:ve.left,y:ve.top,children:t(le,Object.assign({},Ye,{transparencyBackgroundColor:de.transparencyBackgroundColor}))}),n(c,{isolated:r.hasBlendMode,children:[t(c,{name:"page-background-group",x:ve.left,y:ve.top,children:t(se,Object.assign({},Ye))}),n(c,{x:ve.left,y:ve.top,name:"elements-container",listening:!i.isPlaying&&i.role!==X.VIEWER,children:[t(f,{name:"elements-area",width:r.computedWidth,height:r.computedHeight,listening:!1}),t(ue,{elements:r.children,store:i})]})]}),t(p,{name:"bleed",fill:de.bleedColor,listening:!1,visible:i.bleedVisible&&(ve.top>0||ve.right>0||ve.bottom>0||ve.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:i,bottom:o,left:l}=xe,s=r.computedWidth,a=r.computedHeight,d=e._context;d.beginPath(),d.rect(0,0,l+s+i,n+a+o),d.rect(l,n,s,a),d.fillStyle=t.fill(),d.fill("evenodd")}}),R()===fe&&t(pe,{name:"hit-detection",x:-ut/2-ht,y:-ut/2-ht,width:be+ut+2*ht,height:we+ut+2*ht})]})}),t(g,{name:"workspace-background",points:[0,0,u,0,u,x,0,x,0,0,Ee,Xe,Ee,x-Xe,u-Ee,x-Xe,u-Ee,Xe,Ee,Xe],listening:!1,closed:!0,fill:de.workspaceBackgroundColor}),t(c,{x:Ee,y:Xe,scaleX:i.scale,scaleY:i.scale,children:t(f,{name:"page-highlight",hideInExport:!0,x:-ut/2-ht,y:-ut/2-ht,width:be+ut+2*ht,height:we+ut+2*ht,stroke:st?de.activePageBorderColor:de.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),n(c,{x:Ee+ve.left*i.scale,y:Xe+ve.top*i.scale,scaleX:i.scale,scaleY:i.scale,name:"page-abs-container",children:[t(w,{ref:Se,name:z,rotateAnchorAngle:De,onDragStart:e=>{var t;const n=e.target.getStage();n&&n.getPointersPositions().length>=2?e.target.stopDrag():((null===(t=e.evt)||void 0===t?void 0:t.altKey)&&I&&i.selectedElements.forEach(e=>{const t=e.clone({},{skipSelect:!0}),n=r.children.indexOf(e);r.setElementZIndex(t.id,n)}),Oe.current||(Oe.current=!0,i.history.startTransaction()))},onDragEnd:()=>{Oe.current&&(i.history.endTransaction(),Oe.current=!1),Qe()},onTransformStart:()=>{i.history.startTransaction(),ke.current=new Map(ze.map(e=>[e.id,{width:e.width,height:e.height}])),je(null)},boundBoxFunc:(e,t)=>{var n,o,r,l;const s=null!==(o=null===(n=me.boundBoxFunc)||void 0===n?void 0:n.call(me,e,t))&&void 0!==o?o:t,a=Math.abs(s.width)<1||Math.abs(s.height)<1,d=Math.abs(e.width)<1||Math.abs(e.height)<1;if(a&&!d){return e}const c=i.selectedElements;if(1===c.length&&"table"===(null===(r=c[0])||void 0===r?void 0:r.type)){const t=c[0],n=Math.abs(e.width),i=Math.abs(e.height);if(n>0&&i>0){const o=Math.abs(s.width)/n,r=Math.abs(s.height)/i,l=t.width*o,a=t.height*r,d=Object.assign({},s);return l<t.minWidth&&(d.width=e.width),a<t.minHeight&&(d.height=e.height),_(d,s,e)}}return Ue&&ze.length>0?((e,t,n,i,o)=>{const r=Math.abs(e.width),l=Math.abs(e.height);if(0===r||0===l){return t}const s=Math.abs(t.width)/r,a=Math.abs(t.height)/l,d=!V(n),c=e=>i.some(t=>{var n;const i=null==o?void 0:o.get(t.id),r=null!==(n="x"===e?null==i?void 0:i.width:null==i?void 0:i.height)&&void 0!==n?n:"x"===e?t.width:t.height,l="x"===e?t.width:t.height,c="x"===e?s:a,u=((e,t,n,i)=>{if("table"===e.type){const n=e;return"x"===t?n.minWidth:n.minHeight}return"text"===e.type&&"x"===t&&n?U(i,e.fontSize):1})(t,e,d,r);return r>=u&&l*c<u}),u=Object.assign({},t),h=t.width*e.width<0,m=t.height*e.height<0,g=h||c("x"),f=m||c("y"),p=f||!d&&g;return(g||!d&&f)&&(u.width=e.width),p&&(u.height=e.height),_(u,t,e)})(e,s,(null===(l=Se.current)||void 0===l?void 0:l.getActiveAnchor())||"",ze,ke.current):s},onTransform:e=>{const t=Se.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{$e()},0)},onTransformEnd:e=>{Be({}),i.history.endTransaction(),ke.current=null,Qe(),Ke()},visible:!i.isPlaying}),Pe&&Pe.map(({points:e,distance:o,box1:r,box2:l},s)=>n(c,{name:"distances-container",hideInExport:!0,listening:!1,children:[t(f,Object.assign({},r,{stroke:de.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(f,Object.assign({},l,{stroke:de.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),t(g,{points:[e[0].x,e[0].y,e[1].x,e[1].y],stroke:de.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),t(g,{points:[e[1].x,e[1].y,e[2].x,e[2].y],stroke:de.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(h,{x:(e[0].x+e[1].x)/2,y:(e[0].y+e[1].y)/2,offsetY:-10,scaleX:1/i.scale,scaleY:1/i.scale,children:[t(b,{cornerRadius:5,fill:de.distanceGuideStyle.labelFill,pointerDirection:"down"}),t(y,{align:"center",verticalAlign:"middle",fill:de.distanceGuideStyle.labelTextFill,padding:5,text:F({unit:i.unit,dpi:i.dpi,px:o})})]})]},s)),r._rendering&&n(c,{children:[t(f,{width:be,height:we,fill:"rgba(255,255,255,0.9)"}),t(y,{text:"Rendering...",fontSize:60,width:be,height:we,align:"center",verticalAlign:"middle"})]}),dt&&t(dt,{components:S,store:i,page:r,stageRef:Ae,tooltipSafeArea:J}),ct&&t(K,{children:t(ct,Object.assign({components:S,store:i},Ce.props))}),t(P,{store:i,pageId:r.id})]}),t(k,{store:i,page:r,width:u,height:x,scale:i.scale,xPadding:Ee,yPadding:Xe,bleedLeft:ve.left,bleedTop:ve.top}),t(ce,Object.assign({},Re,{store:i})),t(ae,{selection:qe}),B()>0&&n(c,{hideInExport:!0,children:[t(ye,{name:"cache-bounds",x:u-(1===B()?250:210),y:x-(1===B()?95:45)}),t(ye,{name:"cache-bounds-t",x:10,y:10})]}),D()&&t(y,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:u-170,y:x-18,onMouseEnter:e=>{e.target.getStage().container().style.cursor="pointer"},onMouseLeave:e=>{e.target.getStage().container().style.cursor=""},onTouchStart:e=>{e.cancelBubble=!0},onMouseDown:e=>{e.cancelBubble=!0},onClick:()=>{window.open("https://polotno.com","_blank","noopener")},onTap:()=>{window.open("https://polotno.com","_blank","noopener")}}),t(c,{name:"line-guides"})]})}),Ie&&at&&t(at,{store:i,page:r,xPadding:Ee,yPadding:Xe,layout:L})]})});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GroupBlendMode } from '@polotno/schema';
|
|
2
|
+
type OpacityNode = {
|
|
3
|
+
type?: string;
|
|
4
|
+
blendMode?: GroupBlendMode;
|
|
5
|
+
a: {
|
|
6
|
+
opacity: number;
|
|
7
|
+
};
|
|
8
|
+
parent?: OpacityNode | null;
|
|
9
|
+
};
|
|
10
|
+
export declare const elementOpacity: (element: OpacityNode) => number;
|
|
11
|
+
export declare const paintOpacity: (element: OpacityNode) => number;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{blendModeOf as t,isIsolatedGroup as o}from"@polotno/core/page";export const elementOpacity=o=>t(o)?1:o.a.opacity;export const paintOpacity=p=>{if(t(p)){return 1}let e=p.a.opacity;for(let t=p.parent;"group"===(null==t?void 0:t.type)&&!o(t);t=t.parent){e*=t.a.opacity}return e};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var t=this&&this.__rest||function(t,e){var o={};for(var r in t){Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(o[r]=t[r])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(r=Object.getOwnPropertySymbols(t);n<r.length;n++){e.indexOf(r[n])<0&&Object.prototype.propertyIsEnumerable.call(t,r[n])&&(o[r[n]]=t[r[n]])}}return o};import{jsx as e,Fragment as o,jsxs as r}from"react/jsx-runtime";import n from"react";import{observer as i}from"mobx-react-lite";import{Group as a,Path as s,Rect as l,Shape as c}from"react-konva";import{nodeAutorun as h}from"../utils/mobx.js";import{isAlive as d}from"mobx-state-tree";import{Html as f}from"react-konva-utils";import{layout as u,drawLayout as g}from"render-tag";import{layoutTextOnPath as m,drawTextOnPathLayout as x}from"render-tag/path";import{activeAnchorOf as p,scaledFontSizeAttrs as w,roundedFontSizeAttrs as y,roundedTextDecorationAttrs as b,roundedGeometryPx as E,roundedPosition as O,textMinResizeWidth as v}from"../utils/px.js";import{flags as S}from"../utils/flags.js";import{applyFilter as M}from"./apply-filters.js";import{getLineHeight as j,usePrevious as z}from"./text-element.js";import{useFontSpecsLoader as k}from"./text-element/font-specs-loader.js";import{getHtml as R,getCurvePath as Y,getCurveTextHeight as X,letterSpacingOverhang as F}from"@polotno/core/text-html";import{useColor as C}from"./use-color.js";import{isGradient as I}from"@polotno/core/gradient";import{detectSize as P}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as B,trimTrailingLetterSpace as T}from"@polotno/core/background-shape";import{useFadeIn as W}from"./use-fadein.js";import{incrementLoader as D}from"../utils/loader.js";import{removeTags as N}from"@polotno/core/text";import{shadowCastByBackground as $}from"@polotno/core/filters";import{isTouchDevice as J}from"../utils/screen.js";import{getLimitedFontSize as _}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as A,extractFontSpecsFromHtml as L,scaleRichTextFontSizesInHtml as V}from"../utils/rich-text-html.js";import{RichEditor as H}from"./rich-editor.js";import{findFitFontSize as q}from"./text-element/fit-font-size.js";import{visiblePlaceholder as G}from"./text-element/placeholder.js";import{wrapHtmlForRenderTag as K}from"@polotno/core/reset-style";import{xmlEscape as Q}from"@polotno/core/xml";import{hasMeasuredHeight as U,resolveTextBoxHeight as Z}from"@polotno/core/text-types";export{K as wrapHtmlForRenderTag};function tt(t,e){return t!==e&&(!S.textVerticalResizeEnabled||t<e)}export const RenderTagElement=i(({element:i,store:et})=>{var ot;const rt=n.useRef(null),nt=n.useRef(null),[it,at]=n.useState(!1),st=n.useRef(i.height),lt=i.isSelected,ct=z(lt),{textVerticalResizeEnabled:ht}=S,dt=S.autoDeleteEmptyText,ft=z(i.fontFamily),ut=n.useMemo(()=>A(i.text,{fontSize:i.fontSize,fontFamily:i.fontFamily}),[i.text,i.fontSize,i.fontFamily]),gt=n.useMemo(()=>L(ut,{fontFamily:i.fontFamily,fontWeight:i.fontWeight,fontStyle:i.fontStyle}),[ut,i.fontFamily,i.fontWeight,i.fontStyle]),[mt]=k(et,gt,i.text||i.placeholder),xt=i._editModeEnabled;W(rt);const pt=mt?i.fontFamily:ft&&ft!==i.fontFamily?ft:"Arial",wt=C(i).fill,yt=I(i.fill),bt=I(i.stroke)&&!!i.strokeWidth,Et=!!i.strokeWidth&&(bt||yt),Ot="ellipsis"===S.textOverflow&&!i.curveEnabled&&U(i)&&!Et,vt=G(i.placeholder),St=!!vt&&!N(i.text),Mt=St?Q(vt):ut,jt=St?.6:i.a.opacity,zt=R(i,{fontFamily:pt,color:wt,forEditor:!Ot,textOverflow:S.textOverflow,text:Mt,textIsNormalized:!0}),kt=i.width||1,Rt=F(i.letterSpacing,i.fontSize),Yt=i.curveEnabled,Xt=function(t,e,o,r){return n.useMemo(()=>{if(r){return null}try{return u({html:K(t),width:Math.max(1,e),accuracy:"performance"})}catch(o){return console.error("render-tag layout failed:",o),null}},[t,e,o,r])}(zt,kt,mt,Yt),Ft=function(t,e,o){return n.useMemo(()=>{if(!e.curveEnabled){return null}const o=e.width||1,r=X(e),n=Y(o,r,e.curvePower,e.fontSize);try{return m({html:K(t),path:n,align:"center",textBaseline:"middle"})}catch(i){return console.error("render-tag/path layoutTextOnPath failed:",i),null}},[o,e.curveEnabled,e.curvePower,e.fontSize,e.width,e.height,t])}(zt,i,mt),Ct=j({fontLoaded:mt,fontFamily:i.fontFamily,fontSize:i.fontSize,lineHeight:i.lineHeight}),It=Yt?0:Math.max(null!==(ot=null==Xt?void 0:Xt.height)&&void 0!==ot?ot:0,i.fontSize*Ct),Pt=Ft?Ft.bounds:null,Bt=n.useMemo(()=>Xt?Xt.lines.map(t=>T({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height},Rt)):[],[Xt,Rt]),Tt=n.useRef([]),Wt=n.useRef(null),Dt=n.useRef(!0),[,Nt]=n.useReducer(t=>t+1,0);n.useEffect(()=>{const t=Tt.current;t.length>0&&(Tt.current=[],t.forEach(t=>t()))});const $t=n.useCallback((t,e,o,r)=>{const n=D(`text ${i.id}`),a=()=>{et.history.ignore(t,e,o).then(()=>{Dt.current?(Tt.current.push(n),Nt()):n()})};r?queueMicrotask(a):a()},[i.id,et]),Jt=n.useCallback(t=>{const e=null!==Wt.current;Wt.current=t,e||$t(()=>{const t=Wt.current;Wt.current=null,d(i)&&tt(i.height,t)&&i.set({height:t})},!1,!0)},[i,$t]);n.useEffect(()=>{if(!mt){return}if(!Xt){return}if(!U(i)){return void $t(()=>{d(i)&&(U(i)||i.set({height:It}))},void 0,void 0,!0)}if(et.isPlaying){return}const{textOverflow:t}=S,e=tt(i.height,It),o=()=>Jt(It);if("change-font-size"!==t||it||xt||i.curveEnabled){"resize"===t&&e&&!it&&o()}else{const t=q(St?{toJSON:()=>Object.assign(Object.assign({},i.toJSON()),{text:Mt})}:i,{fontFamily:pt,color:wt,forEditor:!0},t=>{try{return u({html:K(t),width:kt,accuracy:"performance"}).height}catch(ot){return P(t).height}});t.fontSize!==i.fontSize?$t(()=>{if(!d(i)){return}const e=w(i,t.fontSize);void 0!==t.text&&t.text!==i.text&&(e.text=t.text),i.set(e)},!1,!0):e&&o()}}),n.useEffect(()=>()=>{Dt.current=!1;const t=Tt.current;t.length>0&&(Tt.current=[],t.forEach(t=>t()))},[]),n.useLayoutEffect(()=>{if(!mt||!i.curveEnabled||it){return}if(!Pt||!Pt.width){return}const t=t=>t&&ht?Math.max(t,Pt.height):Pt.height;Math.abs(Pt.width-i.width)<1&&Math.abs(t(i.height)-i.height)<1||$t(()=>{if(!d(i)){return}const e=Pt.width-i.width,o=t(i.height),r=o-i.height;if(Math.abs(e)<1&&Math.abs(r)<1){return}const n=i.a.rotation*Math.PI/180,a=-e/2*Math.cos(n),s=-e/2*Math.sin(n);i.set({width:Pt.width,height:o,x:i.a.x+a,y:i.a.y+s})},!1,!0,!0)},[mt,i.curveEnabled,Pt,it,ht,$t]),n.useEffect(()=>{ct&&!lt&&dt&&""===N(i.text)&&i.removable&&!G(i.placeholder)&&et.deleteElements([i.id])},[dt,i.placeholder,i.removable,i.text,lt,ct]),n.useEffect(()=>h(i,()=>{const t=rt.current;t&&M(t,i)}),[Xt,Ft,i.a.width,i.a.height,i.shadowColor,i.shadowOffsetX,i.shadowOffsetY,i.shadowOpacity]);const _t=Z(i,It),At=Yt||!mt||it||et.isPlaying||"resize"!==S.textOverflow?i.a.height:ht?Math.max(i.a.height,It):It;let Lt=0;Yt||"middle"!==i.verticalAlign?Yt||"bottom"!==i.verticalAlign||(Lt=_t-It):Lt=(_t-It)/2;const Vt=i.backgroundPadding*(i.fontSize*Ct*.5),Ht=i.backgroundCornerRadius*(i.fontSize*Ct*.5),qt=n.useMemo(()=>!i.backgroundEnabled||i.curveEnabled||i.legacyBackground||0===Bt.length?"":B({rects:Bt,padding:Vt,cornerRadius:Ht}),[i.backgroundEnabled,i.curveEnabled,i.legacyBackground,Bt,Vt,Ht]),Gt=J(),Kt=n.useRef(null),Qt=n.useRef(i.width),Ut=n.useRef(null),Zt=n.useRef(null),te=n.useRef(""),ee=n.useRef(0),oe=n.useRef(0),re=i.width>0?i.a.width/i.width:1,ne={shadowEnabled:i.shadowEnabled&&$(i),shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity};return r(o,{children:[i.curveEnabled||i.legacyBackground?e(l,Object.assign({x:i.a.x,y:i.a.y,offsetX:Vt/re,offsetY:Vt/re,rotation:i.a.rotation,scaleX:re,scaleY:re,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled,opacity:i.backgroundOpacity*jt,fill:i.backgroundColor},ne,{width:i.width+2*Vt,height:_t+2*Vt,cornerRadius:Ht})):e(s,Object.assign({name:"rendertag-background",x:i.a.x,y:i.a.y,offsetY:-Lt/re,rotation:i.a.rotation,scaleX:re,scaleY:re,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled&&qt.length>0,opacity:i.backgroundOpacity*jt,fill:i.backgroundColor},ne,{data:qt})),e(c,{ref:rt,name:"rendertag-text",x:i.a.x,y:i.a.y,rotation:i.a.rotation,scaleX:re,scaleY:re,offsetY:re>0?-Lt/re:0,width:i.width,height:Math.max(i.height,It),listening:!1,visible:!xt&&!!(Yt?Ft:Xt),editModeEnabled:xt,opacity:jt,shadowEnabled:i.shadowEnabled&&!ne.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,hideInExport:!i.showInExport,sceneFunc:(t,e)=>{const o=t._context;if(o){try{if(Yt){if(!Ft||!Pt){return}const t=(i.width-Pt.width)/2-Pt.x;o.save(),o.translate(t,-Pt.y),x({layout:Ft,ctx:o}),o.restore()}else{if(!Xt){return}g({layout:Xt,width:kt,ctx:o,pixelRatio:1})}}catch(r){console.error("render-tag draw failed:",r)}}}}),e(l,{ref:nt,name:"element",x:i.a.x,y:i.a.y,listening:i.selectable,rotation:i.a.rotation,width:i.a.width,height:At,visible:!xt,draggable:Gt?i.draggable&<:i.draggable,preventDefault:!Gt||lt,opacity:0,hideInExport:!0,id:i.id,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set(O(t.target))},onDblClick:t=>{i.contentEditable&&(Zt.current={x:t.evt.clientX,y:t.evt.clientY},i.toggleEditMode(!0))},onDblTap:t=>{var e;if(i.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Zt.current=o?{x:o.clientX,y:o.clientY}:null,i.toggleEditMode(!0)}},onTransformStart:t=>{at(!0),st.current=i.height,Qt.current=i.width,Ut.current=null,Kt.current=t.target.position(),te.current=p(t.target),ee.current=i.a.rotation,oe.current=i.height},onTransform:e=>{const o=e.target,r=te.current,n="middle-left"===r||"middle-right"===r,a="top-center"===r||"bottom-center"===r,s=o.scaleX();if(n){const e=o.scaleX(),r=o.width()*e;let n=Math.max(r,v(Qt.current,i.fontSize));const a=i.toJSON();let s=0;if(i.curveEnabled){s=X(Object.assign(Object.assign({},a),{width:n}))}else{const{x:e,y:o,width:r,height:i}=a,l=t(a,["x","y","width","height"]),c=`${JSON.stringify(l)}|${pt}|${mt}|${wt}`;if(Ut.current&&Ut.current.signature===c&&n<=Ut.current.width){n=Ut.current.width,s=Ut.current.height}else{const t=t=>{const e=R(Object.assign(Object.assign({},a),{width:t}),{fontFamily:pt,color:wt,forEditor:!0,text:Mt,textIsNormalized:!0});try{return u({html:K(e),width:t,accuracy:"performance"})}catch(ot){return{height:P(e).height,lines:[]}}};let e=t(n);const o=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width-Rt));o>n+.5&&o<=Qt.current+.5&&(n=Math.min(Math.ceil(o),Qt.current),e=t(n),Ut.current={signature:c,width:n,height:e.height}),s=e.height}}n>r&&Kt.current&&o.position(Kt.current),o.width(n),o.scaleX(1),o.scaleY(1);let l=S.textVerticalResizeEnabled?Math.max(s,st.current):s;"ellipsis"===S.textOverflow&&(l=st.current),o.height(l),i.set({width:o.width(),height:l,x:o.x(),y:o.y()})}else if(a){const t="resize"===S.textOverflow?Yt?(null==Pt?void 0:Pt.height)||i.fontSize:It:Ct*i.fontSize,e=o.height()*o.scaleY();if(e<t){if(o.height(t),Kt.current){if("top-center"===te.current){const e=ee.current*Math.PI/180,r=oe.current-t;o.x(Kt.current.x-r*Math.sin(e)),o.y(Kt.current.y+r*Math.cos(e))}else{o.position(Kt.current)}}}else{o.height(e)}o.scaleY(1),o.scaleX(1),oe.current=Math.max(t,e),i.set({x:o.x(),y:o.y(),height:o.height(),rotation:o.rotation()})}else{o.scaleX(1),o.scaleY(1),i.set({fontSize:i.fontSize*s,letterSpacing:i.letterSpacing,width:o.width()*s,x:o.x(),y:o.y(),rotation:o.rotation(),height:o.height()*s,shadowBlur:i.shadowBlur*s,shadowOffsetX:i.shadowOffsetX*s,shadowOffsetY:i.shadowOffsetY*s,strokeWidth:i.strokeWidth*s,text:V(i.text,s)})}Kt.current=e.target.position()},onTransformEnd:t=>{at(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),i.set(Object.assign(Object.assign({},y(i,i.fontSize*e)),{width:E(t.target.width()*e),x:E(t.target.x()),y:E(t.target.y()),rotation:E(t.target.rotation()),text:V(i.text,e)}))}}),xt&&e(a,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,offsetY:-Lt,children:e(f,{divProps:{style:{zIndex:20}},children:e(H,{element:i,onChange:t=>{const e=_({oldText:N(i.text),newText:N(t),element:i});i.set(Object.assign({text:t},w(i,e)))},onBlur:()=>{d(i)&&(i.set(b(i)),i.toggleEditMode(!1))},clickCoords:Zt.current})})})]})});
|
|
1
|
+
var t=this&&this.__rest||function(t,e){var o={};for(var r in t){Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(o[r]=t[r])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(r=Object.getOwnPropertySymbols(t);n<r.length;n++){e.indexOf(r[n])<0&&Object.prototype.propertyIsEnumerable.call(t,r[n])&&(o[r[n]]=t[r[n]])}}return o};import{jsx as e,Fragment as o,jsxs as r}from"react/jsx-runtime";import{elementOpacity as n}from"./paint-opacity.js";import i from"react";import{observer as a}from"mobx-react-lite";import{Group as s,Path as l,Rect as c,Shape as h}from"react-konva";import{nodeAutorun as d}from"../utils/mobx.js";import{isAlive as f}from"mobx-state-tree";import{Html as u}from"react-konva-utils";import{layout as g,drawLayout as m}from"render-tag";import{layoutTextOnPath as x,drawTextOnPathLayout as p}from"render-tag/path";import{activeAnchorOf as y,scaledFontSizeAttrs as w,roundedFontSizeAttrs as b,roundedTextDecorationAttrs as v,roundedGeometryPx as O,roundedPosition as E,textMinResizeWidth as j}from"../utils/px.js";import{flags as M}from"../utils/flags.js";import{applyFilter as S}from"./apply-filters.js";import{getLineHeight as z,usePrevious as k}from"./text-element.js";import{useFontSpecsLoader as R}from"./text-element/font-specs-loader.js";import{getHtml as Y,getCurvePath as X,getCurveTextHeight as F,letterSpacingOverhang as B}from"@polotno/core/text-html";import{useColor as C}from"./use-color.js";import{isGradient as I}from"@polotno/core/gradient";import{detectSize as P}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as T,trimTrailingLetterSpace as W}from"@polotno/core/background-shape";import{useFadeIn as D}from"./use-fadein.js";import{incrementLoader as N}from"../utils/loader.js";import{removeTags as $}from"@polotno/core/text";import{shadowCastByBackground as J}from"@polotno/core/filters";import{isTouchDevice as _}from"../utils/screen.js";import{getLimitedFontSize as A}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as L,extractFontSpecsFromHtml as V,scaleRichTextFontSizesInHtml as H}from"../utils/rich-text-html.js";import{RichEditor as q}from"./rich-editor.js";import{findFitFontSize as G}from"./text-element/fit-font-size.js";import{visiblePlaceholder as K}from"./text-element/placeholder.js";import{wrapHtmlForRenderTag as Q}from"@polotno/core/reset-style";import{xmlEscape as U}from"@polotno/core/xml";import{hasMeasuredHeight as Z,resolveTextBoxHeight as tt}from"@polotno/core/text-types";export{Q as wrapHtmlForRenderTag};function et(t,e){return t!==e&&(!M.textVerticalResizeEnabled||t<e)}export const RenderTagElement=a(({element:a,store:ot})=>{var rt;const nt=i.useRef(null),it=i.useRef(null),[at,st]=i.useState(!1),lt=i.useRef(a.height),ct=a.isSelected,ht=k(ct),{textVerticalResizeEnabled:dt}=M,ft=M.autoDeleteEmptyText,ut=k(a.fontFamily),gt=i.useMemo(()=>L(a.text,{fontSize:a.fontSize,fontFamily:a.fontFamily}),[a.text,a.fontSize,a.fontFamily]),mt=i.useMemo(()=>V(gt,{fontFamily:a.fontFamily,fontWeight:a.fontWeight,fontStyle:a.fontStyle}),[gt,a.fontFamily,a.fontWeight,a.fontStyle]),[xt]=R(ot,mt,a.text||a.placeholder),pt=a._editModeEnabled;D(nt);const yt=xt?a.fontFamily:ut&&ut!==a.fontFamily?ut:"Arial",wt=C(a).fill,bt=I(a.fill),vt=I(a.stroke)&&!!a.strokeWidth,Ot=!!a.strokeWidth&&(vt||bt),Et="ellipsis"===M.textOverflow&&!a.curveEnabled&&Z(a)&&!Ot,jt=K(a.placeholder),Mt=!!jt&&!$(a.text),St=Mt?U(jt):gt,zt=Mt?.6:n(a),kt=Y(a,{fontFamily:yt,color:wt,forEditor:!Et,textOverflow:M.textOverflow,text:St,textIsNormalized:!0}),Rt=a.width||1,Yt=B(a.letterSpacing,a.fontSize),Xt=a.curveEnabled,Ft=function(t,e,o,r){return i.useMemo(()=>{if(r){return null}try{return g({html:Q(t),width:Math.max(1,e),accuracy:"performance"})}catch(o){return console.error("render-tag layout failed:",o),null}},[t,e,o,r])}(kt,Rt,xt,Xt),Bt=function(t,e,o){return i.useMemo(()=>{if(!e.curveEnabled){return null}const o=e.width||1,r=F(e),n=X(o,r,e.curvePower,e.fontSize);try{return x({html:Q(t),path:n,align:"center",textBaseline:"middle"})}catch(i){return console.error("render-tag/path layoutTextOnPath failed:",i),null}},[o,e.curveEnabled,e.curvePower,e.fontSize,e.width,e.height,t])}(kt,a,xt),Ct=z({fontLoaded:xt,fontFamily:a.fontFamily,fontSize:a.fontSize,lineHeight:a.lineHeight}),It=Xt?0:Math.max(null!==(rt=null==Ft?void 0:Ft.height)&&void 0!==rt?rt:0,a.fontSize*Ct),Pt=Bt?Bt.bounds:null,Tt=Pt?(a.width-Pt.width)/2-Pt.x:0,Wt=i.useMemo(()=>{var t;if(Bt&&Pt){const t=Bt.paintBounds;return Object.assign(Object.assign({},t),{x:t.x+Tt,y:t.y-Pt.y})}return null!==(t=null==Ft?void 0:Ft.paintBounds)&&void 0!==t?t:null},[Ft,Bt,Pt,Tt]),Dt=i.useMemo(()=>Ft?Ft.lines.map(t=>W({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height},Yt)):[],[Ft,Yt]),Nt=i.useRef([]),$t=i.useRef(null),Jt=i.useRef(!0),[,_t]=i.useReducer(t=>t+1,0);i.useEffect(()=>{const t=Nt.current;t.length>0&&(Nt.current=[],t.forEach(t=>t()))});const At=i.useCallback((t,e,o,r)=>{const n=N(`text ${a.id}`),i=()=>{ot.history.ignore(t,e,o).then(()=>{Jt.current?(Nt.current.push(n),_t()):n()})};r?queueMicrotask(i):i()},[a.id,ot]),Lt=i.useCallback(t=>{const e=null!==$t.current;$t.current=t,e||At(()=>{const t=$t.current;$t.current=null,f(a)&&et(a.height,t)&&a.set({height:t})},!1,!0)},[a,At]);i.useEffect(()=>{if(!xt){return}if(!Ft){return}if(!Z(a)){return void At(()=>{f(a)&&(Z(a)||a.set({height:It}))},void 0,void 0,!0)}if(ot.isPlaying){return}const{textOverflow:t}=M,e=et(a.height,It),o=()=>Lt(It);if("change-font-size"!==t||at||pt||a.curveEnabled){"resize"===t&&e&&!at&&o()}else{const t=G(Mt?{toJSON:()=>Object.assign(Object.assign({},a.toJSON()),{text:St})}:a,{fontFamily:yt,color:wt,forEditor:!0},t=>{try{return g({html:Q(t),width:Rt,accuracy:"performance"}).height}catch(rt){return P(t).height}});t.fontSize!==a.fontSize?At(()=>{if(!f(a)){return}const e=w(a,t.fontSize);void 0!==t.text&&t.text!==a.text&&(e.text=t.text),a.set(e)},!1,!0):e&&o()}}),i.useEffect(()=>()=>{Jt.current=!1;const t=Nt.current;t.length>0&&(Nt.current=[],t.forEach(t=>t()))},[]),i.useLayoutEffect(()=>{if(!xt||!a.curveEnabled||at){return}if(!Pt||!Pt.width){return}const t=t=>t&&dt?Math.max(t,Pt.height):Pt.height;Math.abs(Pt.width-a.width)<1&&Math.abs(t(a.height)-a.height)<1||At(()=>{if(!f(a)){return}const e=Pt.width-a.width,o=t(a.height),r=o-a.height;if(Math.abs(e)<1&&Math.abs(r)<1){return}const n=a.a.rotation*Math.PI/180,i=-e/2*Math.cos(n),s=-e/2*Math.sin(n);a.set({width:Pt.width,height:o,x:a.a.x+i,y:a.a.y+s})},!1,!0,!0)},[xt,a.curveEnabled,Pt,at,dt,At]),i.useEffect(()=>{ht&&!ct&&ft&&""===$(a.text)&&a.removable&&!K(a.placeholder)&&ot.deleteElements([a.id])},[ft,a.placeholder,a.removable,a.text,ct,ht]),i.useEffect(()=>d(a,()=>{const t=nt.current;t&&S(t,a)}),[Ft,Bt,a.a.width,a.a.height,a.shadowColor,a.shadowOffsetX,a.shadowOffsetY,a.shadowOpacity]);const Vt=tt(a,It),Ht=Xt||!xt||at||ot.isPlaying||"resize"!==M.textOverflow?a.a.height:dt?Math.max(a.a.height,It):It;let qt=0;Xt||"middle"!==a.verticalAlign?Xt||"bottom"!==a.verticalAlign||(qt=Vt-It):qt=(Vt-It)/2;const Gt=a.backgroundPadding*(a.fontSize*Ct*.5),Kt=a.backgroundCornerRadius*(a.fontSize*Ct*.5),Qt=i.useMemo(()=>!a.backgroundEnabled||a.curveEnabled||a.legacyBackground||0===Dt.length?"":T({rects:Dt,padding:Gt,cornerRadius:Kt}),[a.backgroundEnabled,a.curveEnabled,a.legacyBackground,Dt,Gt,Kt]),Ut=_(),Zt=i.useRef(null),te=i.useRef(a.width),ee=i.useRef(null),oe=i.useRef(null),re=i.useRef(""),ne=i.useRef(0),ie=i.useRef(0),ae=a.width>0?a.a.width/a.width:1,se={shadowEnabled:a.shadowEnabled&&J(a),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:a.shadowColor,shadowOpacity:a.shadowOpacity};return r(o,{children:[a.curveEnabled||a.legacyBackground?e(c,Object.assign({x:a.a.x,y:a.a.y,offsetX:Gt/ae,offsetY:Gt/ae,rotation:a.a.rotation,scaleX:ae,scaleY:ae,hideInExport:!a.showInExport,listening:!1,visible:a.backgroundEnabled,opacity:a.backgroundOpacity*zt,fill:a.backgroundColor},se,{width:a.width+2*Gt,height:Vt+2*Gt,cornerRadius:Kt})):e(l,Object.assign({name:"rendertag-background",x:a.a.x,y:a.a.y,offsetY:-qt/ae,rotation:a.a.rotation,scaleX:ae,scaleY:ae,hideInExport:!a.showInExport,listening:!1,visible:a.backgroundEnabled&&Qt.length>0,opacity:a.backgroundOpacity*zt,fill:a.backgroundColor},se,{data:Qt})),e(h,{ref:nt,selfRectFunc:Wt?()=>Wt:void 0,name:"rendertag-text",x:a.a.x,y:a.a.y,rotation:a.a.rotation,scaleX:ae,scaleY:ae,offsetY:ae>0?-qt/ae:0,width:a.width,height:Math.max(a.height,It),listening:!1,visible:!pt&&!!(Xt?Bt:Ft),editModeEnabled:pt,opacity:zt,shadowEnabled:a.shadowEnabled&&!se.shadowEnabled,shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:a.shadowColor,shadowOpacity:a.shadowOpacity,hideInExport:!a.showInExport,sceneFunc:(t,e)=>{const o=t._context;if(o){try{if(Xt){if(!Bt||!Pt){return}o.save(),o.translate(Tt,-Pt.y),p({layout:Bt,ctx:o}),o.restore()}else{if(!Ft){return}m({layout:Ft,width:Rt,ctx:o,pixelRatio:1})}}catch(r){console.error("render-tag draw failed:",r)}}}}),e(c,{ref:it,name:"element",x:a.a.x,y:a.a.y,listening:a.selectable,rotation:a.a.rotation,width:a.a.width,height:Ht,visible:!pt,draggable:Ut?a.draggable&&ct:a.draggable,preventDefault:!Ut||ct,opacity:0,hideInExport:!0,id:a.id,onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{a.set(E(t.target))},onDblClick:t=>{a.contentEditable&&(oe.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];oe.current=o?{x:o.clientX,y:o.clientY}:null,a.toggleEditMode(!0)}},onTransformStart:t=>{st(!0),lt.current=a.height,te.current=a.width,ee.current=null,Zt.current=t.target.position(),re.current=y(t.target),ne.current=a.a.rotation,ie.current=a.height},onTransform:e=>{const o=e.target,r=re.current,n="middle-left"===r||"middle-right"===r,i="top-center"===r||"bottom-center"===r,s=o.scaleX();if(n){const e=o.scaleX(),r=o.width()*e;let n=Math.max(r,j(te.current,a.fontSize));const i=a.toJSON();let s=0;if(a.curveEnabled){s=F(Object.assign(Object.assign({},i),{width:n}))}else{const{x:e,y:o,width:r,height:a}=i,l=t(i,["x","y","width","height"]),c=`${JSON.stringify(l)}|${yt}|${xt}|${wt}`;if(ee.current&&ee.current.signature===c&&n<=ee.current.width){n=ee.current.width,s=ee.current.height}else{const t=t=>{const e=Y(Object.assign(Object.assign({},i),{width:t}),{fontFamily:yt,color:wt,forEditor:!0,text:St,textIsNormalized:!0});try{return g({html:Q(e),width:t,accuracy:"performance"})}catch(rt){return{height:P(e).height,lines:[]}}};let e=t(n);const o=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width-Yt));o>n+.5&&o<=te.current+.5&&(n=Math.min(Math.ceil(o),te.current),e=t(n),ee.current={signature:c,width:n,height:e.height}),s=e.height}}n>r&&Zt.current&&o.position(Zt.current),o.width(n),o.scaleX(1),o.scaleY(1);let l=M.textVerticalResizeEnabled?Math.max(s,lt.current):s;"ellipsis"===M.textOverflow&&(l=lt.current),o.height(l),a.set({width:o.width(),height:l,x:o.x(),y:o.y()})}else if(i){const t="resize"===M.textOverflow?Xt?(null==Pt?void 0:Pt.height)||a.fontSize:It:Ct*a.fontSize,e=o.height()*o.scaleY();if(e<t){if(o.height(t),Zt.current){if("top-center"===re.current){const e=ne.current*Math.PI/180,r=ie.current-t;o.x(Zt.current.x-r*Math.sin(e)),o.y(Zt.current.y+r*Math.cos(e))}else{o.position(Zt.current)}}}else{o.height(e)}o.scaleY(1),o.scaleX(1),ie.current=Math.max(t,e),a.set({x:o.x(),y:o.y(),height:o.height(),rotation:o.rotation()})}else{o.scaleX(1),o.scaleY(1),a.set({fontSize:a.fontSize*s,letterSpacing:a.letterSpacing,width:o.width()*s,x:o.x(),y:o.y(),rotation:o.rotation(),height:o.height()*s,shadowBlur:a.shadowBlur*s,shadowOffsetX:a.shadowOffsetX*s,shadowOffsetY:a.shadowOffsetY*s,strokeWidth:a.strokeWidth*s,text:H(a.text,s)})}Zt.current=e.target.position()},onTransformEnd:t=>{st(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},b(a,a.fontSize*e)),{width:O(t.target.width()*e),x:O(t.target.x()),y:O(t.target.y()),rotation:O(t.target.rotation()),text:H(a.text,e)}))}}),pt&&e(s,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,offsetY:-qt,children:e(u,{divProps:{style:{zIndex:20}},children:e(q,{element:a,onChange:t=>{const e=A({oldText:$(a.text),newText:$(t),element:a});a.set(Object.assign({text:t},w(a,e)))},onBlur:()=>{f(a)&&(a.set(v(a)),a.toggleEditMode(!1))},clickCoords:oe.current})})})]})});
|
package/canvas/rich-editor.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(o[n]=e[n])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(e);r<n.length;r++){t.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(e,n[r])&&(o[n[r]]=e[n[r]])}}return o};import{jsx as t}from"react/jsx-runtime";import o from"react";import n from"quill";import*as r from"mobx";import{nodeReaction as i}from"../utils/mobx.js";import{isAlive as l}from"mobx-state-tree";import{getDir as
|
|
1
|
+
var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(o[n]=e[n])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(e);r<n.length;r++){t.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(e,n[r])&&(o[n[r]]=e[n[r]])}}return o};import{jsx as t}from"react/jsx-runtime";import o from"react";import n from"quill";import*as r from"mobx";import{nodeReaction as i}from"../utils/mobx.js";import{isAlive as l}from"mobx-state-tree";import{getDir as a,getMeasureCtx as s,getQuillEditorHtml as d,textLayoutWidth as c}from"@polotno/core/text-html";import{quillHtmlToModelText as u,isQuillFormatMixed as f,isBoldWeight as p}from"../utils/rich-text-html.js";import{resetStyleContent as h,wrapHtmlForRenderTag as m}from"@polotno/core/reset-style";import{layout as g,lineBaselineOffset as b}from"render-tag";import{removeTags as v}from"@polotno/core/text";import{cssFontFamily as y}from"@polotno/core/fonts";import{isGradient as x,firstStopColor as w}from"@polotno/core/gradient";import{getOptimalCaretColor as k}from"./text-element/caret-color.js";import{visiblePlaceholder as S}from"./text-element/placeholder.js";import{registerQuillUnrestrictedFontFormats as q}from"./quill-font-formats.js";import{ALL_QUILL_MIXED_KEYS as T,setActiveTextEditor as E,updateActiveTextFormat as C,clearActiveTextEditor as L,getActiveTextEditor as F}from"../utils/text-format-state.js";import O from"../utils/styled.js";import{applyOpacityToColor as R}from"@polotno/core/svg";import{elementDropShadow as $,shadowCastByBackground as j}from"@polotno/core/filters";export{R as applyOpacityToColor};export const quillRef=r.observable({enabled:!1,currentFormat:{},pendingToolbarInteraction:!1,editor:r.observable.object({instance:null},{},{deep:!1})});const N=T;export function syncQuillFormatState(e,t){const o=t?e.getFormat(t):{},n=function(e,t){const o={};if(!t||t.length<=0){return o}const n=e.getContents(t.index,t.length);for(const r of N){f(n,r)&&(o[r]=!0)}return o}(e,t);r.runInAction(()=>{quillRef.currentFormat=o}),C(o,n,t)}const z="polotno-saved-selection";function M(){const e=window.getSelection();if(!e||0===e.rangeCount||e.isCollapsed){return}if("undefined"==typeof Highlight||!CSS.highlights){return}const t=e.getRangeAt(0).cloneRange();CSS.highlights.set(z,new Highlight(t)),Q()}function P(){"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.delete(z)}function Q(){setTimeout(()=>{"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.has(z)&&(document.querySelector(".polotno-portal")||B(document.activeElement)?Q():P())},300)}if("undefined"!=typeof document&&"undefined"!=typeof CSS&&CSS.highlights&&!document.querySelector("[data-polotno-highlight]")){const W=document.createElement("style");W.setAttribute("data-polotno-highlight",""),W.textContent=`::highlight(${z}) { background-color: Highlight; }`,document.head.appendChild(W)}function A(e,t){"text"===e.type&&t.push(e);for(const o of e.children||[]){A(o,t)}}export function computeMarkerVars(e){var t,o,n;const r=e.fontSize;if(!r||r<=0){return null}const i=y(e.fontFamily),l=p(e.fontWeight)?700:400,a="italic"===e.fontStyle?"italic":"normal",d=s();d.fontKerning="normal",d.font=`${r}px ${i}`;const c=d.measureText("M"),u=(null!==(t=c.fontBoundingBoxAscent)&&void 0!==t?t:0)/r,f=(null!==(o=c.fontBoundingBoxDescent)&&void 0!==o?o:0)/r,h=`font-family:${i};font-size:${r}px;font-weight:${l};font-style:${a};line-height:${e.lineHeight};letter-spacing:${e.letterSpacing}em`,v=m(`<div style="${h}"><ul><li>x</li></ul><ol><li>x</li></ol></div>`);let x;try{x=g({html:v,width:Math.max(12*r,600)})}catch(E){return null}const w=[];A(x.layoutRoot,w);const k=w.find(e=>"•"===e.text),S=w.find(e=>/^\d/.test(e.text));if(!k){return null}const q=k.style.fontSize,T=2.1*r;return{bulletLeft:k.x,bulletTop:k.y-b(q,q*u,q*f),bulletFontSize:q,numberGap:S?Math.max(0,T-S.x-(null!==(n=S.width)&&void 0!==n?n:0)):0}}export const RichTextContainer=O("div",o.forwardRef)`
|
|
2
2
|
.ql-editor {
|
|
3
3
|
outline: none;
|
|
4
4
|
/* anchor for the absolutely-positioned ::before placeholder below */
|
|
@@ -25,7 +25,7 @@ var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype
|
|
|
25
25
|
pointer-events: none;
|
|
26
26
|
opacity: 0;
|
|
27
27
|
}
|
|
28
|
-
${
|
|
28
|
+
${h}
|
|
29
29
|
|
|
30
30
|
/* ===== Editor-only list markers (Quill flat-DOM hack) ======================
|
|
31
31
|
Quill emits a FLAT <ol> with per-item data-list + ql-indent classes (not
|
|
@@ -155,4 +155,12 @@ var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype
|
|
|
155
155
|
&[style*='--polotno-gradient'] .ql-editor [style*='color:'] {
|
|
156
156
|
-webkit-text-fill-color: currentColor;
|
|
157
157
|
}
|
|
158
|
-
|
|
158
|
+
/* No stroke under a gradient fill, element-wide or per-span. The gradient is
|
|
159
|
+
a background-clip:text background, which CSS paints before any text, so
|
|
160
|
+
paint-order can't lift it over the stroke — the stroke would cover the
|
|
161
|
+
inner half of every glyph. Showing the clean fill beats showing it wrong. */
|
|
162
|
+
&[style*='--polotno-gradient'] .ql-editor,
|
|
163
|
+
.ql-editor [style*='background-clip'] {
|
|
164
|
+
-webkit-text-stroke-width: 0;
|
|
165
|
+
}
|
|
166
|
+
`;let D=["background","font-weight","color","gradient","font","italic","size","script","strike","underline","indent","list","direction"];export const setQuillFormats=e=>{D=e};let H=!1;function I(){H||(H=!0,console.warn("[polotno] The Quill 'bold' format is deprecated. Use the numeric 'font-weight' format instead, e.g. quill.format('font-weight', '700') or applyTextFormat({ fontWeight: 700 })."))}function _(e){return e?"700":"400"}export const createQuill=(t,o)=>{q();const r=new n(t,{toolbar:!1,keyboard:!1,placeholder:(null==o?void 0:o.placeholder)||void 0,clipboard:{matchVisual:!1},formats:D});!function(t){const o=t.format.bind(t),n=t.formatText.bind(t),r=t.getFormat.bind(t);t.format=(e,t,n)=>"bold"===e?(I(),o("font-weight",_(t),n)):o(e,t,n),t.formatText=(t,o,r,i,l)=>{if(r&&"object"==typeof r){if("bold"in r){I();const{bold:l}=r,a=e(r,["bold"]);return n(t,o,Object.assign(Object.assign({},a),{"font-weight":_(l)}),i)}return n(t,o,r,i)}return"bold"===r?(I(),n(t,o,"font-weight",_(i),l)):n(t,o,r,i,l)},t.getFormat=(...e)=>{const t=r(...e);return t&&null!=t["font-weight"]&&!("bold"in t)&&(t.bold=p(t["font-weight"])),t}}(r);const i=n.import("delta"),l=(e,t)=>t.compose((new i).retain(t.length(),{"font-weight":"700"}));return r.clipboard.addMatcher("STRONG",l),r.clipboard.addMatcher("B",l),r};export const setQuillContent=(e,t)=>{var o=e.clipboard.convert({html:"<div class='ql-editor' style='outline: none;'>"+t.replace(/\t/g,"")+"<p><br></p></div>",text:""});o.ops.forEach(e=>{"string"==typeof e.insert&&e.insert.includes("")&&(e.insert=e.insert.split("").join("\t"))}),e.setContents(o),e.history.clear()};export const RichEditor=({onBlur:e,onChange:n,element:s,clickCoords:f})=>{const p=o.useRef(null),h=o.useRef(!1);o.useLayoutEffect(()=>{let t,o=!1,i=null,a=null,c=null,m=null,g=null;const b=()=>{if(!o&&p.current){try{t=createQuill(p.current,{placeholder:S(s.placeholder)}),r.runInAction(()=>{quillRef.editor.instance=t}),quillRef.pendingToolbarInteraction=!1,window.__polotnoQuill=t,E({instance:t,store:s.store,element:s});const o=e=>{e&&syncQuillFormatState(t,e)};t.on("text-change",()=>{const e=F(),r=e&&e.instance===t?e.lastSelection:null;o(t.getSelection()||r),setTimeout(()=>{var e,o;const r=t.root;if(!r||!r.isConnected){return}if(!l(s)){return}const i=r.innerHTML;h.current=!0,n(u(i,{fontSize:s.fontSize,fontFamily:s.fontFamily}));const a=t.getContents(),d=s.fill;let c=null,f=!0;for(const t of a.ops){if("string"!=typeof t.insert||"\n"===t.insert){continue}if(null===(e=t.attributes)||void 0===e?void 0:e.gradient){f=!1;break}const n=(null===(o=t.attributes)||void 0===o?void 0:o.color)||d;if(null===c){c=n}else if(n!==c){f=!1;break}}f&&c&&s.set({fill:c})},10)}),t.on("selection-change",o);const b=d(s,s.fontFamily);setQuillContent(t,b),h.current=!0;const v=p.current.closest(".polotno-workspace-inner"),y=null==v?void 0:v.scrollLeft,x=null==v?void 0:v.scrollTop;f?setCursorFromCoords(t,f):t.setSelection(0,0,"api"),v&&void 0!==y&&void 0!==x&&(v.scrollLeft=y,v.scrollTop=x);const w=t.root;m=t=>{const o=t.relatedTarget;(null==o?void 0:o.classList.contains("ql-clipboard"))||(B(o)||quillRef.pendingToolbarInteraction?M():setTimeout(()=>{quillRef.pendingToolbarInteraction||B(document.activeElement)?M():(P(),e())},0))},g=()=>{P()},w.addEventListener("blur",m),w.addEventListener("focus",g),i=w,a=e=>{var o,n;const r=t.getSelection();if(!r||0===r.length){return}const i=t.getText(r.index,r.length).replace(/\n$/,""),l=window.getSelection();let a="";if(l&&l.rangeCount>0){const e=l.getRangeAt(0);let t=e.cloneContents(),o=e.startContainer;o&&o.nodeType===Node.TEXT_NODE&&(o=o.parentNode);let n=e.endContainer;if(n&&n.nodeType===Node.TEXT_NODE&&(n=n.parentNode),o&&o===n){let e=o;for(;e&&e!==w&&e.nodeType===Node.ELEMENT_NODE;){const o=e.tagName;if("SPAN"!==o&&"EM"!==o&&"STRONG"!==o&&"U"!==o&&"S"!==o&&"A"!==o){break}const n=e.cloneNode(!1);n.appendChild(t),t=n,e=e.parentNode}}const r=document.createElement("div");r.appendChild(t),a=r.innerHTML}if(null===(o=e.clipboardData)||void 0===o||o.setData("text/plain",i),a){const t='<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>\x3c!--StartFragment--\x3e<div style="white-space: pre-wrap; overflow-wrap: break-word;">'+a+"</div>\x3c!--EndFragment--\x3e</body></html>";null===(n=e.clipboardData)||void 0===n||n.setData("text/html",t)}e.preventDefault()},c=e=>{a&&a(e);const o=t.getSelection();o&&o.length>0&&t.deleteText(o.index,o.length)},i.addEventListener("copy",a),i.addEventListener("cut",c)}catch(b){console.error("RichEditor init failed:",b)}}};return p.current?b():queueMicrotask(b),()=>{o=!0,i&&m&&i.removeEventListener("blur",m),i&&g&&i.removeEventListener("focus",g),P(),i&&a&&c&&(i.removeEventListener("copy",a),i.removeEventListener("cut",c)),r.runInAction(()=>{quillRef.editor.instance=null,quillRef.currentFormat={}}),L(t),delete window.__polotnoQuill}},[]),o.useEffect(()=>i(s,()=>`${s.text}\0${s.fontSize}\0${s.fontFamily}`,()=>{const e=quillRef.editor.instance;if(!e){return}if(h.current){return void(h.current=!1)}const t=e.getSelection();setQuillContent(e,d(s,s.fontFamily)),h.current=!0,t&&(e.setSelection(t.index,t.length),syncQuillFormatState(e,e.getSelection()))},{fireImmediately:!0}),[]),o.useEffect(()=>i(s,()=>`${s.fontFamily}\0${s.fontSize}\0${s.fontWeight}\0${s.fontStyle}\0${s.lineHeight}\0${s.letterSpacing}\0${a(v(s.text))}`,()=>{const e=p.current;if(!e){return}const t="ltr"===a(v(s.text)),o=computeMarkerVars(s);o&&e.style.setProperty("--rt-number-gap",`${o.numberGap}px`),o&&t?(e.style.setProperty("--rt-bullet-left",`${o.bulletLeft}px`),e.style.setProperty("--rt-bullet-top",`${o.bulletTop}px`),e.style.setProperty("--rt-bullet-fs",`${o.bulletFontSize}px`),e.classList.add("rt-marker-geom")):e.classList.remove("rt-marker-geom")},{fireImmediately:!0}),[]),o.useEffect(()=>{window.addEventListener("blur",e);const t=t=>{var o;const n=t.target;"number"==typeof(null==n?void 0:n.nodeType)&&(null===(o=p.current)||void 0===o?void 0:o.contains(n))||B(n)||quillRef.pendingToolbarInteraction||(P(),e())};return window.addEventListener("mousedown",t),window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",e),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t)}},[]);const m={color:s.fill};s.fill.indexOf("gradient")>=0&&(m["--polotno-gradient"]=s.fill,m.color="transparent"),s.strokeWidth>0&&(m.WebkitTextStroke=`${s.strokeWidth}px ${w(s.stroke)}`,m.paintOrder="stroke fill");const g=x(s.fill)?w(s.fill):k(s),b=s.shadowEnabled&&!j(s);return t(RichTextContainer,{ref:p,style:Object.assign(Object.assign({},m),{fontSize:s.fontSize,fontFamily:y(s.fontFamily),fontWeight:s.fontWeight,fontStyle:s.fontStyle,"--polotno-text-decoration":s.textDecoration||"none",textTransform:s.textTransform,width:c(s.a.width,s.letterSpacing,s.fontSize),lineHeight:s.lineHeight,letterSpacing:s.letterSpacing*s.fontSize+"px",textAlign:s.align,opacity:Math.max(s.a.opacity,.2),filter:b?$({shadowOffsetX:s.shadowOffsetX,shadowOffsetY:s.shadowOffsetY,shadowBlur:s.shadowBlur,shadowColor:s.shadowColor,shadowOpacity:s.shadowOpacity,rotation:s.a.rotation}):void 0,caretColor:g}),dir:a(v(s.text))})};function B(e){const t=e;return!("function"!=typeof(null==t?void 0:t.closest)||!t.closest(".sketch-picker")&&!t.closest("[data-polotno-keep-focus]")&&!t.closest(".polotno-portal")&&!t.closest(".polotno-toolbar"))}export function setCursorFromCoords(e,t){if(!e||!t){return}const{x:o,y:r}=t;try{let t=null;if(document.caretRangeFromPoint){t=document.caretRangeFromPoint(o,r)}else if(document.caretPositionFromPoint){const e=document.caretPositionFromPoint(o,r);e&&(t=document.createRange(),t.setStart(e.offsetNode,e.offset))}if(t){const o=n.find(t.startContainer,!0);if(o){const n=o.offset(e.scroll)+t.startOffset;return void e.setSelection(n,0,"api")}}}catch(i){}e.setSelection(0,0,"api")}
|
package/canvas/table-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,Fragment as e,jsxs as o}from"react/jsx-runtime";import i from"react";import{observer as
|
|
1
|
+
import{jsx as t,Fragment as e,jsxs as o}from"react/jsx-runtime";import{elementOpacity as i}from"./paint-opacity.js";import n from"react";import{observer as r}from"mobx-react-lite";import{observable as l,runInAction as s}from"mobx";import{nodeAutorun as h}from"../utils/mobx.js";import{Group as a,Rect as c,Line as d,Circle as g}from"react-konva";import{Html as u}from"react-konva-utils";import{ROLES as w}from"../model/store.js";import{isTouchDevice as p}from"../utils/screen.js";import{roundedGeometryPx as f,roundedPosition as x}from"../utils/px.js";import{TextElement as y}from"./text-element.js";const v="#0d83dd",m=(t,e)=>{const o=t.target.getStage();o&&(o.container().style.cursor=e)},b=r(({axis:i,hovered:n,pillX:r,pillY:l,pillW:s,pillH:h,hitX:a,hitY:d,hitW:g,hitH:u,tint:w,hoverKind:p,idx:f,onSelect:x,onHoverChange:y})=>o(e,{children:[n&&t(c,{x:w.x,y:w.y,width:w.width,height:w.height,fill:"rgba(13, 131, 221, 0.08)",listening:!1}),t(c,{x:r,y:l,width:s,height:h,cornerRadius:Math.min(s,h)/2,fill:n?v:"#9ca3af",opacity:n?.55:.18,listening:!1}),t(c,{x:a,y:d,width:g,height:u,fill:"transparent",onMouseEnter:t=>{y({kind:p,idx:f}),m(t,"pointer")},onMouseLeave:t=>{y(null),m(t,"")},onClick:x})]})),C=r(({x:i,y:n,hovered:r,hoverKind:l,idx:s,isTouch:h,sizes:u,onInsert:w,onHoverChange:p})=>{const{hit:f,r:x,halo:y,stroke:b}=u,C=r||h;return o(a,{x:i,y:n,onMouseEnter:t=>{p({kind:l,idx:s}),m(t,"pointer")},onMouseLeave:t=>{p(null),m(t,"")},onClick:t=>{t.cancelBubble=!0,w()},children:[t(c,{x:-f/2,y:-f/2,width:f,height:f,fill:"transparent"}),C&&o(e,{children:[t(g,{radius:x+y,fill:"white",listening:!1}),t(g,{radius:x,fill:v,listening:!1}),t(d,{points:[.55*-x,0,.55*x,0],stroke:"white",strokeWidth:b,lineCap:"round",listening:!1}),t(d,{points:[0,.55*-x,0,.55*x],stroke:"white",strokeWidth:b,lineCap:"round",listening:!1})]})]})}),k=r(({element:o,store:i,inv:n,isTouch:r,hover:l,setHover:s})=>{const h=16*n,a=2*n,c=10*n,d={hit:22*n,r:7*n,halo:2*n,stroke:2*n},g=(t,e)=>null!==l&&l.kind===t&&l.idx===e,u=(t,e)=>i=>{i.cancelBubble=!0;const n=o._anchorCellId?o.cells.find(t=>t.id===o._anchorCellId):void 0;i.evt.shiftKey&&n?"col"===t?o.selectColumnRange(n.col,e):o.selectRowRange(n.row,e):"col"===t?o.selectColumn(e):o.selectRow(e)},w=[0],p=[0];for(let t=0;t<o.cols;t++){w.push(w[t]+o.colWidths[t]*o.a.width)}for(let t=0;t<o.rows;t++){p.push(p[t]+o.rowHeights[t]*o.a.height)}const f=[];for(let e=0;e<o.cols;e++){const i=Math.max(0,w[e+1]-w[e]-6);i<=0||f.push(t(b,{axis:"col",hovered:g("colSelect",e),pillX:w[e]+3,pillY:-a-c,pillW:i,pillH:a,hitX:w[e]+1,hitY:-h-c+a,hitW:i+4,hitH:h,tint:{x:w[e],y:0,width:w[e+1]-w[e],height:o.a.height},hoverKind:"colSelect",idx:e,onSelect:u("col",e),onHoverChange:s},`col-pill-${e}`))}for(let e=0;e<=o.cols;e++){f.push(t(C,{x:w[e],y:-c-a/2,hovered:g("colInsert",e),hoverKind:"colInsert",idx:e,isTouch:r,sizes:d,onInsert:()=>i.history.transaction(()=>o.addColumn(e)),onHoverChange:s},`col-insert-${e}`))}for(let e=0;e<o.rows;e++){const i=Math.max(0,p[e+1]-p[e]-6);i<=0||f.push(t(b,{axis:"row",hovered:g("rowSelect",e),pillX:-a-c,pillY:p[e]+3,pillW:a,pillH:i,hitX:-h-c+a,hitY:p[e]+1,hitW:h,hitH:i+4,tint:{x:0,y:p[e],width:o.a.width,height:p[e+1]-p[e]},hoverKind:"rowSelect",idx:e,onSelect:u("row",e),onHoverChange:s},`row-pill-${e}`))}for(let e=0;e<=o.rows;e++){f.push(t(C,{x:-c-a/2,y:p[e],hovered:g("rowInsert",e),hoverKind:"rowInsert",idx:e,isTouch:r,sizes:d,onInsert:()=>i.history.transaction(()=>o.addRow(e)),onHoverChange:s},`row-insert-${e}`))}return t(e,{children:f})});export const TableElement=r(({element:r,store:g})=>{const v=n.useRef(null),m=n.useRef(null),b=r.selectable||g.role===w.ADMIN,C=p(),S=r.isSelected,E=r._focusedCellIds.length>0,I=r.editingCell,H=1/Math.max(.01,g.scale),[M,R]=n.useState(null);n.useEffect(()=>{S||(r.clearCellFocus(),R(null))},[S]);const W=null==I?void 0:I._editModeEnabled;n.useEffect(()=>{r._editingCellId&&I&&!I._editModeEnabled&&r.exitCellEdit()},[W]),n.useEffect(()=>{E&&!r._editingCellId&&setTimeout(()=>{var t;return null===(t=m.current)||void 0===t?void 0:t.focus()},0)},[E,r._editingCellId]);const T=n.useMemo(()=>l.box(!1),[]);n.useEffect(()=>h(r,()=>{T.get()||r._fitRowsToContent&&g.history.ignore(()=>{r._applyFitRowsToContent()})}),[r,g,T]);const B=t=>{const e=t.target.getStage();if(!e){return}const o=e.getPointerPosition();if(!o){return}const i=v.current;if(!i){return}const n=i.getAbsoluteTransform().copy().invert().point(o);for(const l of r.visibleCells){const t=r.getCellRect(l.row,l.col,l.rowSpan,l.colSpan);if(n.x>=t.x&&n.x<=t.x+t.width&&n.y>=t.y&&n.y<=t.y+t.height){return l}}},$=(t,e)=>"dashed"===t?[4*e,2*e]:"dotted"===t?[e,e]:void 0,_=[];for(const e of r.visibleCells){const o=r.getCellRect(e.row,e.col,e.rowSpan,e.colSpan),i=0===e.row,n=e.row+e.rowSpan===r.rows,l=0===e.col,s=e.col+e.colSpan===r.cols;if(0===e.row){const i=e.getEffectiveBorder("top");if("none"!==i.style&&i.width>0){const n=o.y+i.width/2,r=o.x-(l?0:i.width/2),h=o.x+o.width+(s?0:i.width/2);_.push(t(d,{points:[r,n,h,n],stroke:i.color,strokeWidth:i.width,dash:$(i.style,i.width),listening:!1},`bt-${e.id}`))}}if(0===e.col){const r=e.getEffectiveBorder("left");if("none"!==r.style&&r.width>0){const l=o.x+r.width/2,s=o.y-(i?0:r.width/2),h=o.y+o.height+(n?0:r.width/2);_.push(t(d,{points:[l,s,l,h],stroke:r.color,strokeWidth:r.width,dash:$(r.style,r.width),listening:!1},`bl-${e.id}`))}}{const i=e.getEffectiveBorder("bottom");if("none"!==i.style&&i.width>0){const r=n?o.y+o.height-i.width/2:o.y+o.height,h=o.x-(l?0:i.width/2),a=o.x+o.width+(s?0:i.width/2);_.push(t(d,{points:[h,r,a,r],stroke:i.color,strokeWidth:i.width,dash:$(i.style,i.width),listening:!1},`bb-${e.id}`))}}{const r=e.getEffectiveBorder("right");if("none"!==r.style&&r.width>0){const l=s?o.x+o.width-r.width/2:o.x+o.width,h=o.y-(i?0:r.width/2),a=o.y+o.height+(n?0:r.width/2);_.push(t(d,{points:[l,h,l,a],stroke:r.color,strokeWidth:r.width,dash:$(r.style,r.width),listening:!1},`br-${e.id}`))}}}return o(e,{children:[t(c,{id:r.id,name:"element",x:r.a.x,y:r.a.y,rotation:r.a.rotation,width:r.a.width,height:r.a.height,fill:"transparent",opacity:i(r),hideInExport:!r.showInExport,listening:b,draggable:C?r.draggable&&S:r.draggable,onClick:t=>{if(!S){return}if(r.locked||!r.contentEditable){return}const e=B(t);e&&(t.evt.shiftKey&&E?(t.cancelBubble=!0,r.focusCellRange(e.row,e.col)):r.focusCell(e.id))},onDblClick:t=>{if(!S){return}if(r.locked||!r.contentEditable){return}const e=B(t);e&&r.enterCellEdit(e.id)},onDragMove:t=>{var e;const o=t.target;r.set({x:o.x(),y:o.y()}),null===(e=v.current)||void 0===e||e.position({x:o.x(),y:o.y()})},onDragEnd:t=>{r.set(x(t.target))},onTransformStart:()=>{s(()=>T.set(!0))},onTransformEnd:()=>{r.set({x:f(r.x),y:f(r.y),width:f(r.width),rotation:f(r.rotation)}),r._resetBaseRowHeights(),s(()=>T.set(!1))},onTransform:t=>{var e,o;const i=t.target,n=i.scaleX(),l=i.scaleY();i.scaleX(1),i.scaleY(1);const s=r.width*n,h=r.height*l;i.width(s),i.height(h);const a=i.x(),c=i.y(),d=i.rotation();null===(e=v.current)||void 0===e||e.position({x:a,y:c}),null===(o=v.current)||void 0===o||o.rotation(d),r.set({width:s,x:a,y:c,rotation:d}),r.setHeightRedistribute(h)}}),o(a,{ref:v,x:r.a.x,y:r.a.y,rotation:r.a.rotation,opacity:i(r),hideInExport:!r.showInExport,scaleX:r.a.width/r.width,scaleY:r.a.height/r.height,children:[r.visibleCells.map(e=>{const o=r.getCellRect(e.row,e.col,e.rowSpan,e.colSpan);return"transparent"===e.cellBackground?null:t(c,{x:o.x,y:o.y,width:o.width,height:o.height,fill:e.cellBackground,listening:!1},`bg-${e.id}`)}),_,r.visibleCells.map(e=>t(y,{element:e,store:g},`text-${e.id}`)),S&&r._focusedCellIds.map(e=>{const o=r.cells.find(t=>t.id===e);if(!o){return null}const i=r.getCellRect(o.row,o.col,o.rowSpan,o.colSpan),n=2*H;return t(c,{x:i.x,y:i.y,width:i.width,height:i.height,fill:"rgba(13, 131, 221, 0.1)",stroke:"#0d83dd",strokeWidth:n,listening:!1},`focus-${e}`)}),E&&!I&&t(u,{children:t("div",{ref:m,tabIndex:-1,style:{position:"absolute",opacity:0,width:"1px",height:"1px",pointerEvents:"none",outline:"none"}})}),S&&!I&&!g.isPlaying&&(()=>{const e=[];let o=0;for(let i=0;i<r.cols-1;i++){o+=r.colWidths[i]*r.a.width;const n=o,l=i;e.push(t(d,{x:n,points:[0,0,0,r.a.height],stroke:"transparent",strokeWidth:1,hitStrokeWidth:16,draggable:!0,onMouseEnter:t=>{var e;const o=null===(e=t.target.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor="col-resize"),R({kind:"colInsert",idx:l+1})},onMouseLeave:t=>{var e;const o=null===(e=t.target.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor=""),R(null)},onDragStart:t=>{t.cancelBubble=!0,g.history.startTransaction()},onDragMove:t=>{t.cancelBubble=!0;const e=t.target,o=(e.x()-n)/r.width;e.x(n),r.resizeColumn(l,o)},onDragEnd:t=>{t.cancelBubble=!0,t.target.x(n),g.history.endTransaction()}},`col-resize-${i}`))}return e})(),S&&!I&&!g.isPlaying&&(()=>{const e=[];let o=0;for(let i=0;i<r.rows-1;i++){o+=r.rowHeights[i]*r.a.height;const n=o,l=i;e.push(t(d,{y:n,points:[0,0,r.a.width,0],stroke:"transparent",strokeWidth:1,hitStrokeWidth:16,draggable:!0,onMouseEnter:t=>{var e;const o=null===(e=t.target.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor="row-resize"),R({kind:"rowInsert",idx:l+1})},onMouseLeave:t=>{var e;const o=null===(e=t.target.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor=""),R(null)},onDragStart:t=>{t.cancelBubble=!0,s(()=>T.set(!0)),g.history.startTransaction()},onDragMove:t=>{t.cancelBubble=!0;const e=t.target,o=(e.y()-n)/r.height;e.y(n),r.resizeRow(l,o)},onDragEnd:t=>{t.cancelBubble=!0,t.target.y(n),r._resetBaseRowHeights(),s(()=>T.set(!1)),g.history.endTransaction()}},`row-resize-${i}`))}return e})(),S&&!I&&!g.isPlaying&&t(k,{element:r,store:g,inv:H,isTouch:C,hover:M,setHover:R})]})]})});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import r from"react";import{isFontLoaded as t}from"../../utils/fonts.js";import{incrementLoader as e}from"../../utils/loader.js";export const useFontSpecsLoader=(n,u,c)=>{const[,s]=r.useReducer(r=>r+1,0),o=u.map(r=>JSON.stringify([r,n.fonts.find(t=>t.fontFamily===r.family)])).join("|"),f=r=>r.every(r=>t(r.family,r.style,r.weight)),i=r.useRef(f(u)),l=r.useRef(null);return r.useLayoutEffect(()=>{if(i.current=f(u),i.current){return}let r=!0;return(async()=>{i.current=!1,s();const t=e(`text font specs ${o}`);await Promise.all(u.map(r=>n.loadFont(r.family,r.style,r.weight,c))),r?(l.current=t,i.current=!0,s()):t()})(),()=>{l.current&&(l.current(),l.current=null),r=!1}},[o]),r.useEffect(()=>{i.current&&setTimeout(()=>{l.current&&(l.current(),l.current=null)})},[i.current]),[i.current]};
|
package/canvas/text-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,Fragment as e,jsxs as r}from"react/jsx-runtime";import n from"react";import{observer as o}from"mobx-react-lite";import{Group as i,Path as a,Text as s,TextPath as l}from"react-konva";import{Html as c}from"react-konva-utils";import{nodeAutorun as u}from"../utils/mobx.js";import d from"konva";import{parsePath as f,roundCommands as h}from"svg-round-corners/lib/index.js";import{generateBackgroundShapeFromRects as g,textLinesToRects as p}from"@polotno/core/background-shape";import{useColor as m}from"./use-color.js";import{useWorkspaceStyle as x}from"./workspace-style.js";import{ROLES as w}from"../model/store.js";import{incrementLoader as v}from"../utils/loader.js";import{activeAnchorOf as y,scaledFontSizeAttrs as b,roundedFontSizeAttrs as S,roundedTextDecorationAttrs as E,roundedGeometryPx as z,roundedPosition as k,textMinResizeWidth as O}from"../utils/px.js";import{isFontLoaded as R}from"../utils/fonts.js";import{flags as F}from"../utils/flags.js";import{removeTags as A}from"@polotno/core/text";import{getDir as M,getCurvePath as j,resolveLineHeight as L,normalizeStrokeLineJoin as C}from"@polotno/core/text-html";import{cssFontFamily as T}from"@polotno/core/fonts";import{FIT_FONT_SIZE_STEP as H,FIT_HEIGHT_EPSILON as P}from"@polotno/core/fit-text";import{applyFilter as X}from"./apply-filters.js";import{useFadeIn as Y}from"./use-fadein.js";import{isTouchDevice as W}from"../utils/screen.js";import{isAlive as D,getParent as $}from"mobx-state-tree";import{getLimitedFontSize as B}from"./text-element/max-font-size.js";import{getOptimalCaretColor as I}from"./text-element/caret-color.js";import{visiblePlaceholder as N}from"./text-element/placeholder.js";import{hasMeasuredHeight as J,resolveTextBoxHeight as _}from"@polotno/core/text-types";let V;function q(){return V||(V=document.getElementById("polotno-text-style"),V||(V=document.createElement("style"),V.id="polotno-text-style",document.head.appendChild(V)),V)}d._fixTextRendering=!0;const U={border:"none",padding:"0px",overflow:"hidden",background:"none",outline:"none",resize:"none",overflowWrap:"break-word",whiteSpace:"pre-wrap",userSelect:"text",wordBreak:"normal",textTransform:"none"};export{getDir}from"@polotno/core/text-html";const Z=o(({textNodeRef:e,element:r,onBlur:o,selectAll:i,cursorPosition:a})=>{const[s,l]=n.useState(U),c=e.current;n.useLayoutEffect(()=>{if(!c){return}const t={};t.width=c.width()-2*c.padding()+"px",t.height=c.height()-2*c.padding()+c.fontSize()*c.lineHeight()+"px",t.fontSize=c.fontSize()+"px",t.lineHeight=c.lineHeight()+.01,t.fontFamily=c.fontFamily(),t.textAlign=c.align(),t.color=c.fill(),t.fontWeight=r.fontWeight,t.fontStyle=r.fontStyle,t.letterSpacing=r.letterSpacing+"em",t.opacity=Math.max(r.a.opacity,.2),t.textTransform=r.textTransform,t.caretColor=I(r);const e=`\n .polotno-input::placeholder {\n color: ${s.color};\n opacity: 0.6;\n }\n `,n=q();n.innerHTML="",n.appendChild(document.createTextNode(e)),JSON.stringify(t)!==JSON.stringify(s)&&l(t)});const u=n.useRef(null);if(n.useLayoutEffect(()=>{var t;const e=u.current;if(!e){return}null===(t=u.current)||void 0===t||t.focus({preventScroll:!0});const r=a||e.value.length;e.selectionStart=e.selectionEnd=r,i&&(null==e||e.select(),document.execCommand("selectAll",!1,void 0))},[]),n.useEffect(()=>{window.addEventListener("blur",o);const t=t=>{var e;(null===(e=u.current)||void 0===e?void 0:e.contains(t.target))||o()};return window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",o),window.removeEventListener("touchstart",t)}},[]),!c){return null}let d=0;const f=c.textArr.length*c.lineHeight()*c.fontSize(),h=_(r.a,f);"middle"===r.verticalAlign&&(d=(h-f)/2),"bottom"===r.verticalAlign&&(d=h-f);const g=A(r.text);return t("textarea",{className:"polotno-input",ref:u,dir:M(g),style:Object.assign(Object.assign(Object.assign({},U),s),{paddingTop:d+"px"}),value:g,onChange:t=>{const e=B({oldText:r.text,newText:t.target.value,element:r});r.set(Object.assign({text:t.target.value},b(r,e)))},placeholder:N(r.placeholder),onBlur:o})}),G=e=>t(c,{children:t(Z,Object.assign({},e))});export const useFontLoader=(t,e,r,o,i)=>{const[a,s]=n.useReducer(t=>t+1,0),l=n.useRef(R(e,r,o)),c=n.useRef(null);return n.useLayoutEffect(()=>{if(l.current=R(e,r,o),l.current){return}let n=!0;return(async()=>{l.current=!1,s();const a=v(`text ${e}`);await t.loadFont(e,r,o,i),n?(c.current=a,l.current=!0,s()):a()})(),()=>{c.current&&(c.current(),c.current=null),n=!1}},[e,r,o]),n.useEffect(()=>{l.current&&setTimeout(()=>{c.current&&(c.current(),c.current=null)})},[l.current]),[l.current]};export const useFontFamiliesLoader=(t,e,r,o,i)=>{const[a,s]=n.useReducer(t=>t+1,0),l=e.join("\0"),c=n.useRef(e.every(t=>R(t,r,o))),u=n.useRef(null);return n.useLayoutEffect(()=>{if(c.current=e.every(t=>R(t,r,o)),c.current){return}let n=!0;return(async()=>{c.current=!1,s();const a=v(`text fonts ${l}`);await Promise.all(e.map(e=>t.loadFont(e,r,o,i))),n?(u.current=a,c.current=!0,s()):a()})(),()=>{u.current&&(u.current(),u.current=null),n=!1}},[l,r,o]),n.useEffect(()=>{c.current&&setTimeout(()=>{u.current&&(u.current(),u.current=null)})},[c.current]),[c.current]};export const getLineHeight=({fontLoaded:t,fontFamily:e,fontSize:r,lineHeight:o})=>n.useMemo(()=>L({fontFamily:e,fontSize:r,lineHeight:o}),[t,e,r,o]);export function usePrevious(t){const e=n.useRef(t),r=n.useRef(t);return n.useMemo(()=>{r.current=e.current,e.current=t},[t]),r.current}function K(t,e,r=!1){const n=t.fontSize(),o=t.height(),i=A(e.text);let a=e.a.fontSize;t.height(void 0);const s=Math.round(2*e.a.fontSize)-1;for(let l=1;l<s;l++){const n=J(e.a)&&t.height()>e.a.height+P;let o=i.split("\n").join(" ").split(/[\s-]+/).reduce((t,e)=>(e=e.toLowerCase(),/[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\uAC00-\uD7AF]/.test(e)?t.concat(e.split("")):t.concat(e)),[]),s=t.textArr.map(t=>t.text.toLowerCase()).join(";");const l=o.find(t=>!s.includes(t)||(s=s.replace(t,""),!1));if(!(n||l&&!r)){break}a-=H,t.fontSize(a)}return t.fontSize(n),t.height(o),a}export{getCurvePath}from"@polotno/core/text-html";export const TextElement=o(({element:o,store:c})=>{const{textOverflowIndicatorStyle:d}=x(),R=n.useRef(null),L=n.useRef(null),H=n.useRef(null),P=n.useRef([]),B=n.useRef(!0),[,I]=n.useReducer(t=>t+1,0),{editorEnabled:V,selectAll:q}=(t=>{const[e,r]=n.useState(!1),o=n.useRef(!1);return n.useEffect(()=>{var e=!0;return setTimeout(()=>{e&&(t._editModeEnabled&&(o.current=!0),r(!0),setTimeout(()=>{o.current=!1},50))},50),()=>{e=!1}},[]),{editorEnabled:e&&t._editModeEnabled,selectAll:o.current}})(o),[U,Z]=n.useState(!1),[Q,tt]=n.useState(!1),et=n.useRef(o.a.height),rt=n.useRef(0),nt=n.useRef(""),ot=n.useRef(0),it=o.isSelected,at=usePrevious(it),{textVerticalResizeEnabled:st}=F,lt=F.autoDeleteEmptyText,ct=usePrevious(o.fontFamily),[ut,dt]=n.useState([]);n.useEffect(()=>{var t,e;const r=null!==(e=null===(t=R.current)||void 0===t?void 0:t.textArr)&&void 0!==e?e:[];JSON.stringify(r)!==JSON.stringify(ut)&&dt(r)}),n.useEffect(()=>{if(o.a.width||"tablecell"===o.type){return}const t=R.current;t.width(600),o.set({width:1.4*t.getTextWidth()})},[]),n.useEffect(()=>{at&&!it&<&&""===o.text&&o.removable&&!N(o.placeholder)&&c.deleteElements([o.id])},[lt,o.placeholder,o.removable,o.text,it,at]);const ft=()=>{if(o.curveEnabled){const t=R.current;null==t||t.clearCache(),null==t||t.setAttrs({filters:[]});const e=L.current;e&&X(e,o)}else{const t=R.current;t&&X(t,o)}};n.useLayoutEffect(()=>{if(!Q){return u(o,()=>{ft()})}}),n.useEffect(()=>{const t=P.current;t.length>0&&(P.current=[],t.forEach(t=>t()))}),n.useEffect(()=>()=>{B.current=!1;const t=P.current;t.length>0&&(P.current=[],t.forEach(t=>t()))},[]);let ht=A(o.text);"uppercase"===o.textTransform&&(ht=ht.toUpperCase());const[gt]=useFontLoader(c,o.fontFamily,o.fontStyle,o.fontWeight,o.text||o.placeholder),pt=()=>{if(o.curveEnabled){const t=L.current;return t.getSelfRect().height||t.fontSize()}const t=R.current.clone({height:void 0}),e=Math.ceil(t.fontSize()*t.lineHeight()*t.textArr.length+1);return t.destroy(),e};n.useLayoutEffect(()=>{if(!gt){return}if("tablecell"===o.type){const{textOverflow:t}=F;if("resize"===t){const t=pt(),e=D(o)?$(o,2):null;(null==e?void 0:e.setCellContentHeight)&&e.setCellContentHeight(o.id,t)}else if("change-font-size"===t){const t=R.current;if(t){const e=K(t,o,F.textSplitAllowed);e!==o.fontSize&&c.history.ignore(()=>{o.set({fontSize:e})})}}return}const{textOverflow:t,textSplitAllowed:e}=F,r=(t,e,r,n)=>{const i=v(`text ${o.id}`),a=()=>{c.history.ignore(t,e,r).then(()=>{B.current?(P.current.push(i),I()):i()})};n?queueMicrotask(a):a()};if(!J(o)){const t=pt();return void r(()=>{J(o)||o.set({height:t})},void 0,void 0,!0)}if(!c.isPlaying){if("change-font-size"!==t||U){if("resize"===t){const t=pt();st&&o.height<t&&!U&&r(()=>{var e;D(o)&&o.set({height:t}),null===(e=R.current)||void 0===e||e.height(t)},!1,!0),st||o.height===t||U||r(()=>{var e;D(o)&&o.set({height:t}),null===(e=R.current)||void 0===e||e.height(t)},!1,!0)}}else{const t=R.current,n=o.curveEnabled?o.fontSize:K(t,o,e);if(n!==o.fontSize){return void r(()=>{o.set(b(o,n))},!1,!0)}const i=pt();o.height===i||st||r(()=>{o.set({height:i})},!1,!0)}}}),n.useLayoutEffect(()=>{var t;if(gt&&o.curveEnabled){const e=null===(t=L.current)||void 0===t?void 0:t.getSelfRect().width;if(e){const t=e-o.a.width,r=o.a.rotation*Math.PI/180,n=-t/2*Math.cos(r),i=-t/2*Math.sin(r);o.set({width:e,x:o.a.x+n,y:o.a.y+i})}}},[gt,o.curveEnabled,o.curvePower,o.text,o.fontSize,o.fontFamily,o.fontWeight,o.fontStyle,o.letterSpacing]),n.useLayoutEffect(()=>{const t=R.current;t&&(t.width(t.width()+1e-8),t.width(t.width()-1e-8),t._setTextData(),ft())},[gt]);const mt=n.useRef(null),xt=n.useRef(o.width),wt=n.useRef(0),vt=t=>{t.evt.preventDefault();const e=c.selectedShapes.find(t=>t===o);e&&o.contentEditable&&(wt.current=function(t){var e;const r=t.target,n=function(t){var e=t.getAbsoluteTransform().copy();e.invert();var r=t.getStage().getPointerPosition();return e.point(r)}(r),o=r.textArr,i=Math.floor(n.y/(r.fontSize()*r.lineHeight())),a=o.slice(0,i).reduce((t,e)=>t+e.text.length,i),s=null!==(e=o[i])&&void 0!==e?e:o[0];let l=0;return"right"===r.align()?l=r.width()-s.width:"center"===r.align()&&(l=r.width()/2-s.width/2),a+Math.round((n.x-l)/s.width*s.text.length)}(t),o.toggleEditMode())},yt=N(o.placeholder),bt=!ht&&yt?.6:o.a.opacity;Y(R,bt);const St=getLineHeight({fontLoaded:gt,fontFamily:o.fontFamily,fontSize:o.a.fontSize,lineHeight:o.lineHeight}),Et=o.selectable||c.role===w.ADMIN,zt=m(o),kt=m(o,o.stroke,"stroke"),Ot=n.useMemo(()=>o.backgroundEnabled?o.curveEnabled?function({width:t,height:e,padding:r=0,cornerRadius:n=0}){const o=-r,i=-r,a=t+r,s=e+r,l=f(`M ${o} ${i} L ${a} ${i} L ${a} ${s} L ${o} ${s} Z`);return h(l,n).path}({width:o.a.width,height:o.a.height,cornerRadius:o.backgroundCornerRadius*(o.a.fontSize*St*.5),padding:o.backgroundPadding*(o.a.fontSize*St*.5)}):function({lines:t,lineHeight:e,width:r,align:n="left",padding:o=0,cornerRadius:i=0}){const a=p({lines:t,lineHeight:e,width:r,align:n});return g({rects:a,padding:o,cornerRadius:i})}({lines:JSON.parse(JSON.stringify(ut)),cornerRadius:o.backgroundCornerRadius*(o.a.fontSize*St*.5),lineHeight:St*o.a.fontSize,padding:o.backgroundPadding*(o.a.fontSize*St*.5),width:o.a.width,align:o.align}):"",[o.backgroundEnabled,o.curveEnabled,o.backgroundCornerRadius,o.a.fontSize,o.a.height,St,o.backgroundPadding,o.a.width,o.align,ut]),Rt=W();let Ft=0;if(!o.curveEnabled){const t=ut.length*St*o.a.fontSize,e=_(o.a,t);"middle"===o.verticalAlign?Ft=(e-t)/2:"bottom"===o.verticalAlign&&(Ft=e-t)}const At=o.curveEnabled?j(o.a.width,o.a.height,o.curvePower,o.a.fontSize):"",Mt=gt?T(o.fontFamily):ct===o.fontFamily?"Arial":T(ct),jt=M(ht);return r(e,{children:[t(a,{ref:H,x:o.a.x,y:o.a.y,rotation:o.a.rotation,hideInExport:!o.showInExport,listening:!1,visible:o.backgroundEnabled,opacity:o.backgroundOpacity*bt,data:Ot,fill:o.backgroundColor,offsetY:-Ft}),t(a,{data:At,stroke:d.stroke,strokeWidth:1,x:o.a.x,y:o.a.y,rotation:o.a.rotation,visible:!1}),t(l,Object.assign({ref:L,visible:o.curveEnabled,data:At,text:ht||yt,listening:!1,align:"center",textBaseline:"middle",direction:jt},zt,kt,{strokeWidth:o.strokeWidth,lineJoin:C(o.strokeLineJoin),fillAfterStrokeEnabled:!0,fontSize:o.a.fontSize,fontFamily:`${T(o.fontFamily)}, ${T(ct)}`,fontStyle:o.fontStyle+" "+o.fontWeight,textDecoration:o.textDecoration.trim(),letterSpacing:o.letterSpacing*o.a.fontSize,x:o.a.x,y:o.a.y,rotation:o.a.rotation,opacity:o._editModeEnabled?.3:bt,hideInExport:!o.showInExport,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity})),t(s,Object.assign({ref:R,id:o.selectable?o.id:void 0,name:o.selectable?"element":"",hideInExport:!o.showInExport,editModeEnabled:o._editModeEnabled,x:o.a.x,y:o.a.y,rotation:o.a.rotation,width:o.a.width,height:J(o.a)?o.a.height:void 0,text:ht||yt,direction:jt},zt,kt,{lineJoin:C(o.strokeLineJoin),strokeWidth:o.strokeWidth,fillAfterStrokeEnabled:!0,fontSize:o.a.fontSize,fontFamily:Mt,fontStyle:o.fontStyle+" "+o.fontWeight,textDecoration:o.textDecoration,align:o.align,verticalAlign:o.verticalAlign,draggable:Rt?o.draggable&&it:o.draggable,preventDefault:!Rt||it,opacity:o.curveEnabled?0:bt,visible:!o._editModeEnabled,ellipsis:"ellipsis"===F.textOverflow,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity,lineHeight:St,letterSpacing:o.letterSpacing*o.a.fontSize,listening:!!o.selectable&&Et,onDragStart:()=>{tt(!0)},onDragMove:t=>{o.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{tt(!1),o.set(k(t.target))},onClick:vt,onTap:vt,onTransformStart:()=>{Z(!0);const t=R.current;et.current=t.height(),xt.current=o.a.width,mt.current=t.position(),rt.current=t.rotation(),ot.current=t.height(),nt.current=y(t)},onTransform:t=>{var e,r;const n=t.target;null===(e=H.current)||void 0===e||e.setAttrs({x:n.x(),y:n.y(),rotation:n.rotation(),scale:n.scale()});const i=nt.current;if("middle-left"===i||"middle-right"===i){const t=n.scaleX(),e=n.width()*t,r=O(xt.current,o.a.fontSize);let i=e;e<r&&(i=r,mt.current&&n.position(mt.current)),n.width(i),n.scaleX(1),n.scaleY(1);let a=pt();if("ellipsis"===F.textOverflow){a=et.current}else{const t=Math.max(a,et.current);n.height(t),o.set({height:n.height()})}const s=F.textVerticalResizeEnabled?Math.max(a,et.current):pt();o.set({x:n.x(),width:n.width(),rotation:n.rotation(),height:s}),n.skewX(0),n.skewY(0),ft()}if("top-center"===i||"bottom-center"===i){const e="resize"===F.textOverflow?pt():St*o.a.fontSize,r=t.target.height()*t.target.scaleY();if(r<e){if(t.target.height(e),mt.current){if("top-center"===nt.current){const r=rt.current*Math.PI/180,n=ot.current-e;t.target.x(mt.current.x-n*Math.sin(r)),t.target.y(mt.current.y+n*Math.cos(r))}else{t.target.position(mt.current)}}}else{t.target.height(r)}t.target.scaleY(1),t.target.scaleX(1),t.target.rotation(rt.current),t.target.skewX(0),t.target.skewY(0),ot.current=Math.max(e,r)}mt.current=t.target.position();const a=t.target.scaleX();null===(r=H.current)||void 0===r||r.setAttrs({scaleX:1,scaleY:1}),t.target.scaleX(1),t.target.scaleY(1),o.set({fontSize:o.a.fontSize*a,width:t.target.width()*a,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*a,shadowBlur:o.shadowBlur*a,shadowOffsetX:o.shadowOffsetX*a,shadowOffsetY:o.shadowOffsetY*a,strokeWidth:o.strokeWidth*a})},onTransformEnd:t=>{var e;const r=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),o.set(Object.assign(Object.assign({},S(o,o.fontSize*r)),{width:Math.ceil(t.target.width()*r),x:z(t.target.x()),y:z(t.target.y()),rotation:z(t.target.rotation()),height:z(t.target.height()*r)})),null===(e=H.current)||void 0===e||e.setAttrs({scaleX:1,scaleY:1}),Z(!1)}})),V&&o.contentEditable&&t(i,{x:o.a.x,y:o.a.y,rotation:o.a.rotation,children:t(G,{textNodeRef:R,element:o,selectAll:q,cursorPosition:wt.current,onBlur:()=>{o.set(E(o)),o.toggleEditMode(!1)}})})]})});
|
|
1
|
+
import{jsx as t,Fragment as e,jsxs as r}from"react/jsx-runtime";import{elementOpacity as n}from"./paint-opacity.js";import o from"react";import{observer as i}from"mobx-react-lite";import{Group as a,Path as s,Text as l,TextPath as c}from"react-konva";import{Html as u}from"react-konva-utils";import{nodeAutorun as d}from"../utils/mobx.js";import f from"konva";import{parsePath as h,roundCommands as g}from"svg-round-corners/lib/index.js";import{generateBackgroundShapeFromRects as p,textLinesToRects as m}from"@polotno/core/background-shape";import{useColor as x}from"./use-color.js";import{useWorkspaceStyle as w}from"./workspace-style.js";import{ROLES as v}from"../model/store.js";import{incrementLoader as y}from"../utils/loader.js";import{activeAnchorOf as b,scaledFontSizeAttrs as S,roundedFontSizeAttrs as E,roundedTextDecorationAttrs as z,roundedGeometryPx as k,roundedPosition as O,textMinResizeWidth as R}from"../utils/px.js";import{isFontLoaded as F}from"../utils/fonts.js";import{flags as A}from"../utils/flags.js";import{removeTags as j}from"@polotno/core/text";import{getDir as M,getCurvePath as L,resolveLineHeight as C,normalizeStrokeLineJoin as T}from"@polotno/core/text-html";import{cssFontFamily as H}from"@polotno/core/fonts";import{FIT_FONT_SIZE_STEP as P,FIT_HEIGHT_EPSILON as X}from"@polotno/core/fit-text";import{applyFilter as Y}from"./apply-filters.js";import{useFadeIn as W}from"./use-fadein.js";import{isTouchDevice as D}from"../utils/screen.js";import{isAlive as $,getParent as B}from"mobx-state-tree";import{getLimitedFontSize as I}from"./text-element/max-font-size.js";import{getOptimalCaretColor as N}from"./text-element/caret-color.js";import{visiblePlaceholder as J}from"./text-element/placeholder.js";import{hasMeasuredHeight as _,resolveTextBoxHeight as V}from"@polotno/core/text-types";let q;function U(){return q||(q=document.getElementById("polotno-text-style"),q||(q=document.createElement("style"),q.id="polotno-text-style",document.head.appendChild(q)),q)}f._fixTextRendering=!0;const Z={border:"none",padding:"0px",overflow:"hidden",background:"none",outline:"none",resize:"none",overflowWrap:"break-word",whiteSpace:"pre-wrap",userSelect:"text",wordBreak:"normal",textTransform:"none"};export{getDir}from"@polotno/core/text-html";const G=i(({textNodeRef:e,element:r,onBlur:n,selectAll:i,cursorPosition:a})=>{const[s,l]=o.useState(Z),c=e.current;o.useLayoutEffect(()=>{if(!c){return}const t={};t.width=c.width()-2*c.padding()+"px",t.height=c.height()-2*c.padding()+c.fontSize()*c.lineHeight()+"px",t.fontSize=c.fontSize()+"px",t.lineHeight=c.lineHeight()+.01,t.fontFamily=c.fontFamily(),t.textAlign=c.align(),t.color=c.fill(),t.fontWeight=r.fontWeight,t.fontStyle=r.fontStyle,t.letterSpacing=r.letterSpacing+"em",t.opacity=Math.max(r.a.opacity,.2),t.textTransform=r.textTransform,t.caretColor=N(r);const e=`\n .polotno-input::placeholder {\n color: ${s.color};\n opacity: 0.6;\n }\n `,n=U();n.innerHTML="",n.appendChild(document.createTextNode(e)),JSON.stringify(t)!==JSON.stringify(s)&&l(t)});const u=o.useRef(null);if(o.useLayoutEffect(()=>{var t;const e=u.current;if(!e){return}null===(t=u.current)||void 0===t||t.focus({preventScroll:!0});const r=a||e.value.length;e.selectionStart=e.selectionEnd=r,i&&(null==e||e.select(),document.execCommand("selectAll",!1,void 0))},[]),o.useEffect(()=>{window.addEventListener("blur",n);const t=t=>{var e;(null===(e=u.current)||void 0===e?void 0:e.contains(t.target))||n()};return window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",n),window.removeEventListener("touchstart",t)}},[]),!c){return null}let d=0;const f=c.textArr.length*c.lineHeight()*c.fontSize(),h=V(r.a,f);"middle"===r.verticalAlign&&(d=(h-f)/2),"bottom"===r.verticalAlign&&(d=h-f);const g=j(r.text);return t("textarea",{className:"polotno-input",ref:u,dir:M(g),style:Object.assign(Object.assign(Object.assign({},Z),s),{paddingTop:d+"px"}),value:g,onChange:t=>{const e=I({oldText:r.text,newText:t.target.value,element:r});r.set(Object.assign({text:t.target.value},S(r,e)))},placeholder:J(r.placeholder),onBlur:n})}),K=e=>t(u,{children:t(G,Object.assign({},e))});export const useFontLoader=(t,e,r,n,i)=>{const[a,s]=o.useReducer(t=>t+1,0),l=o.useRef(F(e,r,n)),c=o.useRef(null);return o.useLayoutEffect(()=>{if(l.current=F(e,r,n),l.current){return}let o=!0;return(async()=>{l.current=!1,s();const a=y(`text ${e}`);await t.loadFont(e,r,n,i),o?(c.current=a,l.current=!0,s()):a()})(),()=>{c.current&&(c.current(),c.current=null),o=!1}},[e,r,n]),o.useEffect(()=>{l.current&&setTimeout(()=>{c.current&&(c.current(),c.current=null)})},[l.current]),[l.current]};export const useFontFamiliesLoader=(t,e,r,n,i)=>{const[a,s]=o.useReducer(t=>t+1,0),l=e.join("\0"),c=o.useRef(e.every(t=>F(t,r,n))),u=o.useRef(null);return o.useLayoutEffect(()=>{if(c.current=e.every(t=>F(t,r,n)),c.current){return}let o=!0;return(async()=>{c.current=!1,s();const a=y(`text fonts ${l}`);await Promise.all(e.map(e=>t.loadFont(e,r,n,i))),o?(u.current=a,c.current=!0,s()):a()})(),()=>{u.current&&(u.current(),u.current=null),o=!1}},[l,r,n]),o.useEffect(()=>{c.current&&setTimeout(()=>{u.current&&(u.current(),u.current=null)})},[c.current]),[c.current]};export const getLineHeight=({fontLoaded:t,fontFamily:e,fontSize:r,lineHeight:n})=>o.useMemo(()=>C({fontFamily:e,fontSize:r,lineHeight:n}),[t,e,r,n]);export function usePrevious(t){const e=o.useRef(t),r=o.useRef(t);return o.useMemo(()=>{r.current=e.current,e.current=t},[t]),r.current}function Q(t,e,r=!1){const n=t.fontSize(),o=t.height(),i=j(e.text);let a=e.a.fontSize;t.height(void 0);const s=Math.round(2*e.a.fontSize)-1;for(let l=1;l<s;l++){const n=_(e.a)&&t.height()>e.a.height+X;let o=i.split("\n").join(" ").split(/[\s-]+/).reduce((t,e)=>(e=e.toLowerCase(),/[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\uAC00-\uD7AF]/.test(e)?t.concat(e.split("")):t.concat(e)),[]),s=t.textArr.map(t=>t.text.toLowerCase()).join(";");const l=o.find(t=>!s.includes(t)||(s=s.replace(t,""),!1));if(!(n||l&&!r)){break}a-=P,t.fontSize(a)}return t.fontSize(n),t.height(o),a}export{getCurvePath}from"@polotno/core/text-html";export const TextElement=i(({element:i,store:u})=>{const{textOverflowIndicatorStyle:f}=w(),F=o.useRef(null),C=o.useRef(null),P=o.useRef(null),X=o.useRef([]),I=o.useRef(!0),[,N]=o.useReducer(t=>t+1,0),{editorEnabled:q,selectAll:U}=(t=>{const[e,r]=o.useState(!1),n=o.useRef(!1);return o.useEffect(()=>{var e=!0;return setTimeout(()=>{e&&(t._editModeEnabled&&(n.current=!0),r(!0),setTimeout(()=>{n.current=!1},50))},50),()=>{e=!1}},[]),{editorEnabled:e&&t._editModeEnabled,selectAll:n.current}})(i),[Z,G]=o.useState(!1),[tt,et]=o.useState(!1),rt=o.useRef(i.a.height),nt=o.useRef(0),ot=o.useRef(""),it=o.useRef(0),at=i.isSelected,st=usePrevious(at),{textVerticalResizeEnabled:lt}=A,ct=A.autoDeleteEmptyText,ut=usePrevious(i.fontFamily),[dt,ft]=o.useState([]);o.useEffect(()=>{var t,e;const r=null!==(e=null===(t=F.current)||void 0===t?void 0:t.textArr)&&void 0!==e?e:[];JSON.stringify(r)!==JSON.stringify(dt)&&ft(r)}),o.useEffect(()=>{if(i.a.width||"tablecell"===i.type){return}const t=F.current;t.width(600),i.set({width:1.4*t.getTextWidth()})},[]),o.useEffect(()=>{st&&!at&&ct&&""===i.text&&i.removable&&!J(i.placeholder)&&u.deleteElements([i.id])},[ct,i.placeholder,i.removable,i.text,at,st]);const ht=()=>{if(i.curveEnabled){const t=F.current;null==t||t.clearCache(),null==t||t.setAttrs({filters:[]});const e=C.current;e&&Y(e,i)}else{const t=F.current;t&&Y(t,i)}};o.useLayoutEffect(()=>{if(!tt){return d(i,()=>{ht()})}}),o.useEffect(()=>{const t=X.current;t.length>0&&(X.current=[],t.forEach(t=>t()))}),o.useEffect(()=>()=>{I.current=!1;const t=X.current;t.length>0&&(X.current=[],t.forEach(t=>t()))},[]);let gt=j(i.text);"uppercase"===i.textTransform&&(gt=gt.toUpperCase());const[pt]=useFontLoader(u,i.fontFamily,i.fontStyle,i.fontWeight,i.text||i.placeholder),mt=()=>{if(i.curveEnabled){const t=C.current;return t.getSelfRect().height||t.fontSize()}const t=F.current.clone({height:void 0}),e=Math.ceil(t.fontSize()*t.lineHeight()*t.textArr.length+1);return t.destroy(),e};o.useLayoutEffect(()=>{if(!pt){return}if("tablecell"===i.type){const{textOverflow:t}=A;if("resize"===t){const t=mt(),e=$(i)?B(i,2):null;(null==e?void 0:e.setCellContentHeight)&&e.setCellContentHeight(i.id,t)}else if("change-font-size"===t){const t=F.current;if(t){const e=Q(t,i,A.textSplitAllowed);e!==i.fontSize&&u.history.ignore(()=>{i.set({fontSize:e})})}}return}const{textOverflow:t,textSplitAllowed:e}=A,r=(t,e,r,n)=>{const o=y(`text ${i.id}`),a=()=>{u.history.ignore(t,e,r).then(()=>{I.current?(X.current.push(o),N()):o()})};n?queueMicrotask(a):a()};if(!_(i)){const t=mt();return void r(()=>{_(i)||i.set({height:t})},void 0,void 0,!0)}if(!u.isPlaying){if("change-font-size"!==t||Z){if("resize"===t){const t=mt();lt&&i.height<t&&!Z&&r(()=>{var e;$(i)&&i.set({height:t}),null===(e=F.current)||void 0===e||e.height(t)},!1,!0),lt||i.height===t||Z||r(()=>{var e;$(i)&&i.set({height:t}),null===(e=F.current)||void 0===e||e.height(t)},!1,!0)}}else{const t=F.current,n=i.curveEnabled?i.fontSize:Q(t,i,e);if(n!==i.fontSize){return void r(()=>{i.set(S(i,n))},!1,!0)}const o=mt();i.height===o||lt||r(()=>{i.set({height:o})},!1,!0)}}}),o.useLayoutEffect(()=>{var t;if(pt&&i.curveEnabled){const e=null===(t=C.current)||void 0===t?void 0:t.getSelfRect().width;if(e){const t=e-i.a.width,r=i.a.rotation*Math.PI/180,n=-t/2*Math.cos(r),o=-t/2*Math.sin(r);i.set({width:e,x:i.a.x+n,y:i.a.y+o})}}},[pt,i.curveEnabled,i.curvePower,i.text,i.fontSize,i.fontFamily,i.fontWeight,i.fontStyle,i.letterSpacing]),o.useLayoutEffect(()=>{const t=F.current;t&&(t.width(t.width()+1e-8),t.width(t.width()-1e-8),t._setTextData(),ht())},[pt]);const xt=o.useRef(null),wt=o.useRef(i.width),vt=o.useRef(0),yt=t=>{t.evt.preventDefault();const e=u.selectedShapes.find(t=>t===i);e&&i.contentEditable&&(vt.current=function(t){var e;const r=t.target,n=function(t){var e=t.getAbsoluteTransform().copy();e.invert();var r=t.getStage().getPointerPosition();return e.point(r)}(r),o=r.textArr,i=Math.floor(n.y/(r.fontSize()*r.lineHeight())),a=o.slice(0,i).reduce((t,e)=>t+e.text.length,i),s=null!==(e=o[i])&&void 0!==e?e:o[0];let l=0;return"right"===r.align()?l=r.width()-s.width:"center"===r.align()&&(l=r.width()/2-s.width/2),a+Math.round((n.x-l)/s.width*s.text.length)}(t),i.toggleEditMode())},bt=J(i.placeholder),St=!gt&&bt?.6:n(i);W(F,St);const Et=getLineHeight({fontLoaded:pt,fontFamily:i.fontFamily,fontSize:i.a.fontSize,lineHeight:i.lineHeight}),zt=i.selectable||u.role===v.ADMIN,kt=x(i),Ot=x(i,i.stroke,"stroke"),Rt=o.useMemo(()=>i.backgroundEnabled?i.curveEnabled?function({width:t,height:e,padding:r=0,cornerRadius:n=0}){const o=-r,i=-r,a=t+r,s=e+r,l=h(`M ${o} ${i} L ${a} ${i} L ${a} ${s} L ${o} ${s} Z`);return g(l,n).path}({width:i.a.width,height:i.a.height,cornerRadius:i.backgroundCornerRadius*(i.a.fontSize*Et*.5),padding:i.backgroundPadding*(i.a.fontSize*Et*.5)}):function({lines:t,lineHeight:e,width:r,align:n="left",padding:o=0,cornerRadius:i=0}){const a=m({lines:t,lineHeight:e,width:r,align:n});return p({rects:a,padding:o,cornerRadius:i})}({lines:JSON.parse(JSON.stringify(dt)),cornerRadius:i.backgroundCornerRadius*(i.a.fontSize*Et*.5),lineHeight:Et*i.a.fontSize,padding:i.backgroundPadding*(i.a.fontSize*Et*.5),width:i.a.width,align:i.align}):"",[i.backgroundEnabled,i.curveEnabled,i.backgroundCornerRadius,i.a.fontSize,i.a.height,Et,i.backgroundPadding,i.a.width,i.align,dt]),Ft=D();let At=0;if(!i.curveEnabled){const t=dt.length*Et*i.a.fontSize,e=V(i.a,t);"middle"===i.verticalAlign?At=(e-t)/2:"bottom"===i.verticalAlign&&(At=e-t)}const jt=i.curveEnabled?L(i.a.width,i.a.height,i.curvePower,i.a.fontSize):"",Mt=pt?H(i.fontFamily):ut===i.fontFamily?"Arial":H(ut),Lt=M(gt);return r(e,{children:[t(s,{ref:P,x:i.a.x,y:i.a.y,rotation:i.a.rotation,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled,opacity:i.backgroundOpacity*St,data:Rt,fill:i.backgroundColor,offsetY:-At}),t(s,{data:jt,stroke:f.stroke,strokeWidth:1,x:i.a.x,y:i.a.y,rotation:i.a.rotation,visible:!1}),t(c,Object.assign({ref:C,visible:i.curveEnabled,data:jt,text:gt||bt,listening:!1,align:"center",textBaseline:"middle",direction:Lt},kt,Ot,{strokeWidth:i.strokeWidth,lineJoin:T(i.strokeLineJoin),fillAfterStrokeEnabled:!0,fontSize:i.a.fontSize,fontFamily:`${H(i.fontFamily)}, ${H(ut)}`,fontStyle:i.fontStyle+" "+i.fontWeight,textDecoration:i.textDecoration.trim(),letterSpacing:i.letterSpacing*i.a.fontSize,x:i.a.x,y:i.a.y,rotation:i.a.rotation,opacity:i._editModeEnabled?.3:St,hideInExport:!i.showInExport,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity})),t(l,Object.assign({ref:F,id:i.selectable?i.id:void 0,name:i.selectable?"element":"",hideInExport:!i.showInExport,editModeEnabled:i._editModeEnabled,x:i.a.x,y:i.a.y,rotation:i.a.rotation,width:i.a.width,height:_(i.a)?i.a.height:void 0,text:gt||bt,direction:Lt},kt,Ot,{lineJoin:T(i.strokeLineJoin),strokeWidth:i.strokeWidth,fillAfterStrokeEnabled:!0,fontSize:i.a.fontSize,fontFamily:Mt,fontStyle:i.fontStyle+" "+i.fontWeight,textDecoration:i.textDecoration,align:i.align,verticalAlign:i.verticalAlign,draggable:Ft?i.draggable&&at:i.draggable,preventDefault:!Ft||at,opacity:i.curveEnabled?0:St,visible:!i._editModeEnabled,ellipsis:"ellipsis"===A.textOverflow,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,lineHeight:Et,letterSpacing:i.letterSpacing*i.a.fontSize,listening:!!i.selectable&&zt,onDragStart:()=>{et(!0)},onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{et(!1),i.set(O(t.target))},onClick:yt,onTap:yt,onTransformStart:()=>{G(!0);const t=F.current;rt.current=t.height(),wt.current=i.a.width,xt.current=t.position(),nt.current=t.rotation(),it.current=t.height(),ot.current=b(t)},onTransform:t=>{var e,r;const n=t.target;null===(e=P.current)||void 0===e||e.setAttrs({x:n.x(),y:n.y(),rotation:n.rotation(),scale:n.scale()});const o=ot.current;if("middle-left"===o||"middle-right"===o){const t=n.scaleX(),e=n.width()*t,r=R(wt.current,i.a.fontSize);let o=e;e<r&&(o=r,xt.current&&n.position(xt.current)),n.width(o),n.scaleX(1),n.scaleY(1);let a=mt();if("ellipsis"===A.textOverflow){a=rt.current}else{const t=Math.max(a,rt.current);n.height(t),i.set({height:n.height()})}const s=A.textVerticalResizeEnabled?Math.max(a,rt.current):mt();i.set({x:n.x(),width:n.width(),rotation:n.rotation(),height:s}),n.skewX(0),n.skewY(0),ht()}if("top-center"===o||"bottom-center"===o){const e="resize"===A.textOverflow?mt():Et*i.a.fontSize,r=t.target.height()*t.target.scaleY();if(r<e){if(t.target.height(e),xt.current){if("top-center"===ot.current){const r=nt.current*Math.PI/180,n=it.current-e;t.target.x(xt.current.x-n*Math.sin(r)),t.target.y(xt.current.y+n*Math.cos(r))}else{t.target.position(xt.current)}}}else{t.target.height(r)}t.target.scaleY(1),t.target.scaleX(1),t.target.rotation(nt.current),t.target.skewX(0),t.target.skewY(0),it.current=Math.max(e,r)}xt.current=t.target.position();const a=t.target.scaleX();null===(r=P.current)||void 0===r||r.setAttrs({scaleX:1,scaleY:1}),t.target.scaleX(1),t.target.scaleY(1),i.set({fontSize:i.a.fontSize*a,width:t.target.width()*a,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*a,shadowBlur:i.shadowBlur*a,shadowOffsetX:i.shadowOffsetX*a,shadowOffsetY:i.shadowOffsetY*a,strokeWidth:i.strokeWidth*a})},onTransformEnd:t=>{var e;const r=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),i.set(Object.assign(Object.assign({},E(i,i.fontSize*r)),{width:Math.ceil(t.target.width()*r),x:k(t.target.x()),y:k(t.target.y()),rotation:k(t.target.rotation()),height:k(t.target.height()*r)})),null===(e=P.current)||void 0===e||e.setAttrs({scaleX:1,scaleY:1}),G(!1)}})),q&&i.contentEditable&&t(a,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,children:t(K,{textNodeRef:F,element:i,selectAll:U,cursorPosition:vt.current,onBlur:()=>{i.set(z(i)),i.toggleEditMode(!1)}})})]})});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import t from"react";import e from"konva";import{isAlive as r}from"mobx-state-tree";import{applyFiltersToCanvas as o,hasActiveFilters as s,getFiltersDepsKey as n}from"./apply-filters.js";import{trySetCanvasSize as m}from"../utils/canvas.js";import{sourceSize as i}from"./downscale.js";export const useFilteredSource=(a,c)=>{const f=n(a),u=r(a)&&s(a),p=t=>{if(!c||!r(a)){return}const{width:e,height:s}=i(c);if(!e||!s){return}m(t,e,s);const n=t.getContext("2d");n&&(n.drawImage(c,0,0),o(t,a))},l=t.useMemo(()=>{if(!u||!c){return}const t=document.createElement("canvas");return p(t),t},[c,u,f]);return t.useEffect(()=>()=>{l&&e.Util.releaseCanvas(l)},[l]),[l||c||void 0,()=>{l&&p(l)}]};
|