polotno 4.22.1 → 4.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/THIRD_PARTY_NOTICES.md +7 -7
- package/blueprint/toolbar/color-picker.js +2 -2
- package/canvas/apply-filters.js +1 -1
- package/canvas/downscale.d.ts +1 -1
- package/canvas/downscale.js +1 -1
- package/canvas/element.js +1 -1
- package/canvas/fade-overlay.d.ts +5 -0
- package/canvas/fade-overlay.js +1 -1
- package/canvas/hotkeys.js +1 -1
- package/canvas/html-element.js +1 -1
- package/canvas/image-element.js +1 -1
- package/canvas/line-element.js +1 -1
- package/canvas/page.js +1 -1
- package/canvas/quill-font-formats.d.ts +1 -0
- package/canvas/quill-font-formats.js +1 -1
- package/canvas/radial-gradient-handle.d.ts +14 -0
- package/canvas/radial-gradient-handle.js +1 -0
- package/canvas/render-tag-element.js +1 -1
- package/canvas/rich-editor.d.ts +2 -0
- package/canvas/rich-editor.js +12 -6
- package/canvas/text-element/legacy-padding-warning.d.ts +6 -0
- package/canvas/text-element/legacy-padding-warning.js +1 -0
- package/canvas/text-element/max-font-size.js +1 -1
- package/canvas/text-element.js +1 -1
- package/canvas/use-color.d.ts +24 -2
- package/canvas/use-color.js +1 -1
- package/canvas/video-element.js +1 -1
- package/icons/registry.d.ts +1 -0
- package/icons/registry.js +1 -1
- package/image-tools/raster.js +1 -1
- package/model/group-model.d.ts +26 -10
- package/model/group-model.js +1 -1
- package/model/node-model.d.ts +10 -1
- package/model/node-model.js +1 -1
- package/model/page-model.d.ts +75 -33
- package/model/page-model.js +1 -1
- package/model/table-model.d.ts +29 -9
- package/model/table-model.js +1 -1
- package/model/text-model.d.ts +5 -1
- package/model/text-model.js +1 -1
- package/package.json +16 -16
- package/polotno.bundle.js +194 -202
- package/primitives/field-group.js +1 -1
- package/primitives/popover.d.ts +1 -1
- package/primitives/popover.js +1 -1
- package/primitives/slider-field.js +1 -1
- package/side-panel/effect-card.js +1 -1
- package/side-panel/effects-panel.js +1 -1
- package/side-panel/images-grid.js +1 -1
- package/side-panel/layers-panel.js +1 -1
- package/side-panel/tab-button.js +1 -3
- package/side-panel/templates-panel.js +1 -1
- package/toolbar/color-picker.js +1 -1
- package/toolbar/gradient-center-pad.d.ts +11 -0
- package/toolbar/gradient-center-pad.js +1 -0
- package/toolbar/html-toolbar.js +2 -2
- package/toolbar/padding-control.d.ts +29 -0
- package/toolbar/padding-control.js +1 -0
- package/toolbar/selection-colors.js +1 -1
- package/toolbar/stop-track.js +1 -1
- package/toolbar/table-toolbar.js +1 -1
- package/toolbar/text-toolbar.d.ts +7 -2
- package/toolbar/text-toolbar.js +1 -1
- package/toolbar/unit-numeric-input.d.ts +5 -0
- package/toolbar/unit-numeric-input.js +1 -0
- package/toolbar/use-copy-style.js +1 -1
- package/ui.css +1 -1
- package/utils/clipboard.d.ts +10 -2
- package/utils/clipboard.js +1 -1
- package/utils/engine.d.ts +2 -0
- package/utils/engine.js +3 -0
- package/utils/fonts.js +1 -1
- package/utils/gradient-handle.d.ts +18 -0
- package/utils/gradient-handle.js +1 -0
- package/utils/gradient-stops.d.ts +7 -2
- package/utils/gradient-stops.js +1 -1
- package/utils/history-gesture.d.ts +7 -0
- package/utils/history-gesture.js +1 -0
- package/utils/html2canvas.js +1 -1
- package/utils/l10n.d.ts +19 -1
- package/utils/l10n.js +1 -1
- package/utils/measure-text.d.ts +4 -0
- package/utils/measure-text.js +1 -1
- package/utils/px.d.ts +4 -0
- package/utils/px.js +1 -1
- package/utils/rich-text-html.d.ts +1 -0
- package/utils/rich-text-html.js +1 -1
- package/utils/selection-transform.js +1 -1
- package/utils/text-format.d.ts +5 -0
- package/utils/text-format.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{Fragment as t,jsx as n,jsxs as i}from"react/jsx-runtime";import o from"react";import{clamp as r}from"../lib/utils.js";import{observer as l,useLocalObservable as s}from"mobx-react-lite";import{action as a,runInAction as c}from"mobx";import{isAlive as d}from"mobx-state-tree";import{ZERO_BLEED as m}from"@polotno/schema";import{Group as u,Image as h,Label as g,Layer as f,Line as p,Rect as x,Shape as v,Stage as b,KonvaRenderer as y,Tag as w,Text as E,Transformer as S}from"react-konva";import{useWorkspaceStyle as X}from"./workspace-style.js";import{ROLES as Y}from"../model/store.js";import k from"konva";import A from"./element.js";import{DrawingLayer as M}from"./drawing-layer.js";import{ensureDragOrder as j,useSnap as I}from"./use-transformer-snap.js";import{useImageLoader as P,useSizedImage as O}from"./image-element.js";import{FadeOverlay as R}from"./fade-overlay.js";import{getCrop as T}from"../utils/crop.js";import{cloneBehind as D}from"../utils/duplicate.js";import{___ as B,____ as C,isCreditVisible as W}from"../utils/validate-key.js";import{getClientRect as G,getCorners as L,getTotalClientRect as V,polygonsIntersect as F}from"../utils/math.js";import{pxToUnitRounded as z,pxToUnitString as _}from"../utils/unit.js";import{isCornerAnchor as U,isRotateAnchor as Z,MAIN_TRANSFORMER_NAME as H}from"../utils/px.js";import{canStretchAxis as J,isAxisAligned as N,leafMinSize as K}from"../utils/selection-transform.js";import{restoreHeldPoint as $}from"./transform-box.js";import{flags as Q}from"../utils/flags.js";import{textOverflowOf as q}from"../utils/text-sizing.js";import{useColor as ee}from"./use-color.js";import{isGradient as te}from"@polotno/core/gradient";import{toDisplayColor as ne}from"@polotno/core/color";import{Html as ie}from"react-konva-utils";import{getParentGroupId as oe,withoutDescendants as re}from"../utils/selection.js";import{nodeReaction as le}from"../utils/mobx.js";import{useEvent as se}from"../hooks/use-event.js";const ae=o.version.startsWith("18.")?o.forwardRef((e,t)=>(o.useLayoutEffect(()=>{y.flushSync()}),n(b,Object.assign({},e,{ref:t})))):b,ce={enabledAnchors:["top-left","top-center","top-right","middle-left","bottom-left","bottom-right","bottom-center","middle-right"],rotateEnabled:!0,rotationSnaps:[0,45,90,135,180,225,270,315],ignoreStroke:!0,flipEnabled:!1,keepRatio:!0},de=e=>{if(!e.length){return null}const t=N(e[0].rotation)?0:e[0].rotation;return e.every(e=>N(e.rotation-t))?t:null},me=e=>e.selectedShapes.filter(t=>{var n;return(null===(n=e.activeTool)||void 0===n?void 0:n.elementId)!==t.id&&!t.pendingEdit}),ue=e=>e.selectedShapes.some(e=>!e.resizable),he=e=>e.selectedShapes.some(e=>!e.draggable),ge=e=>{var t;return 1!==e.selectedElements.length||"group"===(null===(t=e.selectedElements[0])||void 0===t?void 0:t.type)},fe=e=>{var t;const n=me(e),i=ge(e),o=null!==(t=de(n))&&void 0!==t?t:0,r=e=>i&&n.length>0&&n.every(t=>J(t,e,o));return{boxRotation:o,canStretchX:r("x"),canStretchY:r("y")}},pe={text:{enabledAnchors:["top-left","top-right","middle-left","bottom-left","bottom-right","middle-right"]},svg:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},gif:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},lottie:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},line:{enabledAnchors:[],borderEnabled:!1,rotateEnabled:!1},image:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},many:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},group:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},table:{keepRatio:!1,enabledAnchors:["top-left","top-center","top-right","middle-left","middle-right","bottom-left","bottom-center","bottom-right"]}};export function registerTransformerAttrs(e,t){pe[e]=pe[e]||t,Object.assign(pe[e],t)}const xe=e=>n(x,Object.assign({},e,{preventDefault:!1})),ve=t=>{var{url:i}=t,o=e(t,["url"]);const[r,l]=O(i),s=r?T(r,{width:o.width,height:o.height},"center-middle"):{};return P(l,i,"page background"),n(h,Object.assign({image:r},o,s))},be=e=>{const t=ee({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return n(x,Object.assign({},e,t))},ye=t=>{var{background:i,transparencyBackgroundColor:r}=t,l=e(t,["background","transparencyBackgroundColor"]);const s=o.useMemo(()=>{if(te(i)){return!0}const e=k.Util.colorToRGBA(i);return!e||e.a<1},[i]),a=o.useMemo(()=>{if(!s||r){return}const e=document.createElement("canvas"),t=30;e.width=60,e.height=60;const n=e.getContext("2d");return n&&(n.fillStyle="black",n.fillRect(t,0,t,t),n.fillRect(0,t,t,t)),e},[s,r]);return s?n(x,r?Object.assign({fill:r},l,{hideInExport:!0}):Object.assign({fillPatternImage:a},l,{opacity:.1,hideInExport:!0})):null},we=t=>{var{background:i}=t,r=e(t,["background"]);return o.useMemo(()=>!!k.Util.colorToRGBA(i)||te(i),[i])?n(be,Object.assign({fill:i},r)):n(ve,Object.assign({url:i},r))},Ee=l(({selection:e})=>{const{selectionRectStyle:t}=X();return e.visible?n(x,{name:"selection",x:Math.min(e.x1,e.x2),y:Math.min(e.y1,e.y2),width:Math.abs(e.x1-e.x2),height:Math.abs(e.y1-e.y2),fill:t.fill,stroke:t.stroke,strokeWidth:t.strokeWidth}):null});function Se(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const Xe=l(({gesture:e,store:o})=>{const{transformLabelStyle:r}=X(),{x:l,y:s,width:a,height:c,rotation:d,anchor:m}=e.label;if(void 0===l||void 0===s||void 0===a||void 0===c||void 0===d||void 0===m){return null}const u=G({x:l,y:s,width:a,height:c,rotation:k.Util.radToDeg(d)}),h=null!==m&&Z(m),f=(c/2+70)*Math.cos(d-Math.PI/2),p=(c/2+70)*Math.sin(d-Math.PI/2),x=z({unit:o.unit,dpi:o.dpi,px:a/o.scale,precious:"px"===o.unit?0:1})+" x "+z({unit:o.unit,dpi:o.dpi,px:c/o.scale,precious:"px"===o.unit?0:1})+("px"===o.unit?"":" "+o.unit);return i(t,{children:[i(g,{x:(u.minX+u.maxX)/2+f,y:(u.minY+u.maxY)/2+p,offsetX:14,offsetY:14,visible:h,children:[n(w,{cornerRadius:5,fill:r.fill}),n(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:Se(d).toString()+"°"})]}),i(g,{x:(u.minX+u.maxX)/2,y:u.maxY+20,visible:!h,children:[n(w,{cornerRadius:5,fill:r.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),n(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:x})]})]})}),Ye=l(({store:e,gesture:t,transformerStyle:n,onChange:i})=>{var r;const l=e.selectedElements.some(e=>e.curveEnabled),s=e.selectedElements.some(e=>{var t;return!(null!==(t=e.filled.visible)&&void 0!==t?t:e.visible)}),a=JSON.stringify(me(e).map(e=>e.id)),c=!ge(e)&&e.selectedElements[0].keepRatio,d=!ge(e)&&"text"===(null===(r=e.selectedElements[0])||void 0===r?void 0:r.type)&&"fit"===q(e.selectedElements[0]),m=t.transforming?null:fe(e);return o.useLayoutEffect(()=>{i()},[e.selectedShapes,a,ue(e),s,he(e),l,c,d,t.transforming,null==m?void 0:m.canStretchX,null==m?void 0:m.canStretchY,null==m?void 0:m.boxRotation,n]),null}),ke=()=>{console.warn("Polotno warning: onClick callback is deprecated. Just stop using it. Polotno will do selection automatically.")},Ae=l(({page:e})=>{const t=e._exportingOrRendering||e._forceMount;return o.useEffect(()=>{if(t&&d(e)){return e.set({_exportRendered:!0}),()=>{d(e)&&e.set({_exportRendered:!1})}}},[e,t]),null}),Me=l(({elements:e,store:t})=>{const i=e.filter(e=>e.alwaysOnTop),r=e.filter(e=>!e.alwaysOnTop).concat(i);return n(o.Fragment,{children:r.map(e=>n(A,{store:t,element:e,onClick:ke},e.id))})});export const useContextMenu=({store:e})=>{const[t,n]=o.useState(!1),[i,r]=o.useState({x:0,y:0}),l=o.useCallback(e=>{n(!0),r(e)},[]);return{open:l,close:o.useCallback(()=>{n(!1)},[]),props:{isOpen:t,offset:i,setIsOpen:n}}};let je=null;export const registerNextDomDrop=e=>{je=e};const Ie=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),Pe=atob("I2MwMzkyYg=="),Oe=atob("djAuOS4y"),Re=e=>n(t,{children:i(g,{fill:Pe,height:200,children:[n(w,{fill:Pe}),n(E,Object.assign({},e,{fill:"white",text:Ie,height:void 0,padding:10,fontSize:20}))]})}),Te=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),De=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),Be=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),Ce=e=>{const t=1===C(),o=t?De:Te,r=t?230:195;return i(u,Object.assign({},e,{listening:!1,children:[n(x,{fill:Pe,width:r,height:24,cornerRadius:2}),n(E,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),t&&n(E,{text:Be,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default l(({store:t,page:l,width:d,height:h,pageControlsEnabled:b,components:y,altCloneEnabled:A,groupSelectionMode:P="drill",viewportSize:O,layout:T="vertical",tooltipSafeArea:z,transformerSafeArea:Z})=>{const J=X(),ee=J.transformerStyle,te=ce,ve=l.computedBleed,be=t.bleedVisible?ve:m,Se=l.computedWidth+be.left+be.right,ke=l.computedHeight+be.top+be.bottom,Ie=(d-Se*t.scale)/2,Pe=(h-ke*t.scale)/2,Te={x:-ve.left,y:-ve.top,width:l.computedWidth+ve.left+ve.right,height:l.computedHeight+ve.top+ve.bottom,background:ne(l.background),name:"page-background",preventDefault:!1},De={x:-be.left,y:-be.top,width:Se,height:ke},Be=o.useRef(null),We=o.useRef(null),Ge=o.useRef(null),Le=o.useRef(null),Ve=null==b||b,Fe=o.useRef(!1),[ze,_e]=o.useState(null),Ue=s(()=>({transforming:!1,label:{},setLabel(e){this.label=e},setTransforming(e){this.transforming=e}})),[Ze,He]=o.useState(0),Je=useContextMenu({store:t}),Ne=me(t),Ke=ge(t),$e=()=>{var n,i,o;const r=Be.current;if(!r){return[]}const l=r.getStage();if(!l){return[]}const s=me(t),a=new Set(s.map(e=>e.id)),c=new Map;a.size&&l.find(e=>{const t=e.id();return a.has(t)&&!c.has(t)&&c.set(t,e),!1});const d=s.map(e=>c.get(e.id)).filter(e=>!!e),m=1===t.selectedElements.length,u=m&&(null===(n=t.selectedElements[0])||void 0===n?void 0:n.type)||"many",h=pe[u],g=Object.assign(Object.assign({},te),h||{});h&&("svg"!==u&&"image"!==u&&"gif"!==u&&"lottie"!==u||t.selectedElements[0].keepRatio||(g.enabledAnchors=te.enabledAnchors),"text"!==u||!Q.textVerticalResizeEnabled&&"fit"!==q(t.selectedElements[0])||(g.enabledAnchors=null===(i=pe.text.enabledAnchors)||void 0===i?void 0:i.concat(["bottom-center","top-center"])),"text"===u&&m&&t.selectedElements[0].curveEnabled&&(g.enabledAnchors=pe.many.enabledAnchors));const f=fe(t);(f.canStretchX||f.canStretchY)&&(g.enabledAnchors=[...g.enabledAnchors||[],...f.canStretchX?["middle-left","middle-right"]:[],...f.canStretchY?["top-center","bottom-center"]:[]]);const{boundBoxFunc:p}=ee,x=e(ee,["boundBoxFunc"]);r.setAttrs(Object.assign(Object.assign(Object.assign({},g),x),{name:H})),ue(t)&&r.enabledAnchors([]),he(t)&&r.rotateEnabled(!1);const v=r.nodes();if(v.length===d.length&&d.every((e,t)=>v[t]===e)||r.nodes(d),ge(t)&&d.length){const e=de(s);null===e||N(r.rotation()-e)||(r.rotation(e),r.forceUpdate())}return null===(o=r.getLayer())||void 0===o||o.batchDraw(),d},Qe=se(()=>{var e;if(null===(e=Be.current)||void 0===e?void 0:e.isTransforming()){return}const t=$e();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),j())}),qe=()=>{var e,t;const n=Be.current;if(!n){return}if(!n.isTransforming()){return void Ue.setLabel({})}if(!n.nodes().length){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();Ue.setLabel({anchor:o,x:i.x-(null!==(e=null==r?void 0:r.x())&&void 0!==e?e:0),y:i.y-(null!==(t=null==r?void 0:r.y())&&void 0!==t?t:0),rotation:i.rotation,width:i.width,height:i.height})},et=o.useCallback(()=>{var e;if(null===(e=Be.current)||void 0===e?void 0:e.isTransforming()){return}const t=Be.current;if(!t||!t.nodes().length){return void He(0)}const n=t.getClientRect();if(!t.getStage()){return}const i=function(e,t,n,i=0){const o=e.x+e.width/2,r=e.y+e.height/2,l=e.width/2,s=e.height/2,a=i*Math.PI/180,c=e=>{const t=e*Math.PI/180,i=(e%360+360)%360;let c;c=0===i||180===i?s:l;const d=Math.sin(t)*(c+n),m=-Math.cos(t)*(c+n),u=d*Math.cos(a)-m*Math.sin(a),h=d*Math.sin(a)+m*Math.cos(a);return{x:o+u,y:r+h}},d=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(d(c(0))){return 0}for(const m of[180,270,90]){if(d(c(m))){return m}}return 0}(n,{top:(null==Z?void 0:Z.top)||0,bottom:h-((null==Z?void 0:Z.bottom)||0),left:(null==Z?void 0:Z.left)||0,right:d-((null==Z?void 0:Z.right)||0)},ee.rotateAnchorOffset,t.rotation());He(i)},[Z,d,h]);o.useEffect(()=>{var e;null===(e=Be.current)||void 0===e||e.update(),qe(),et()},[t.scale,et]),o.useEffect(()=>{et()},[t.selectedShapes,et]);const tt=s(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),nt=o.useRef(!1),it=e=>{const n=Ge.current;if(!n||t.role===Y.VIEWER||t.activePage!==l||t.isPlaying||"button"in e.evt&&0!==e.evt.button||1!==n.getPointersPositions().length||!e.target.findAncestor(".elements-container")||!e.target.findAncestor(".element",!0)||!t.selectedShapes.length||he(t)){return}const i=n.getPointerPosition();if(!i){return}const o=$e();if(o.some(t=>t===e.target||t.isAncestorOf(e.target))){return}const r=o.find(e=>!!(e.draggable()&&e.isVisible()&&e.isListening())&&(e instanceof k.Shape?[e]:e instanceof k.Container?e.find("Shape"):[]).some(e=>e.intersects(i)));if(r){for(const[t,n]of k.DD._dragElements){"ready"!==n.dragStatus||n.node!==e.target&&!n.node.isAncestorOf(e.target)||k.DD._dragElements.delete(t)}r._createDragElement(e),"touches"in e.evt&&e.evt.cancelable&&r.preventDefault()&&e.evt.preventDefault()}},ot=a(e=>{var n,i,o,r,l,s,a;if(t.role===Y.VIEWER){return}if(nt.current=!1,t.activeTool&&"crop"!==t.activeTool.name){return}const c=e.target.findAncestor(".elements-container"),d=e.target.findAncestor("Transformer"),m=e.target.findAncestor(".page-abs-container");if(c||d||m){return}const u=null===(n=e.target.getStage())||void 0===n?void 0:n.getPointerPosition();u&&(u.x-=null!==(l=null===(i=e.target.getStage())||void 0===i?void 0:i.x())&&void 0!==l?l:0,u.y-=null!==(s=null===(o=e.target.getStage())||void 0===o?void 0:o.y())&&void 0!==s?s:0,tt.visible=!0,tt.x1=u.x,tt.y1=u.y,tt.x2=u.x,tt.y2=u.y,(null!==(a=null===(r=e.target.getStage())||void 0===r?void 0:r.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(tt.visible=!1))});(({stageRef:e,containerRef:t,viewportSize:n})=>{o.useEffect(()=>{var n;const i=null===(n=t.current)||void 0===n?void 0:n.closest(".polotno-workspace-inner");function o(){var n;if(!e.current){return}const o=null===(n=t.current)||void 0===n?void 0:n.getBoundingClientRect(),l=null==i?void 0:i.getBoundingClientRect();if(!o||!l){return}const s=r(l.left-o.left-100,0,Math.max(0,o.width-e.current.width())),a=r(l.top-o.top-100,0,Math.max(0,o.height-e.current.height()));e.current.position({x:-s,y:-a}),e.current.container().style.transform=`translate(${s}px, ${a}px)`}return o(),null==i||i.addEventListener("scroll",o),()=>{null==i||i.removeEventListener("scroll",o)}},[n.width,n.height])})({stageRef:Ge,containerRef:Le,viewportSize:O}),o.useEffect(()=>{const e=a(e=>{var t,n,i,o,r,l;if(!tt.visible){return}null===(t=Ge.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=Ge.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(r=null===(i=Ge.current)||void 0===i?void 0:i.x())&&void 0!==r?r:0,s.y-=null!==(l=null===(o=Ge.current)||void 0===o?void 0:o.y())&&void 0!==l?l:0):s={x:tt.x2,y:tt.y2},tt.x2=s.x,tt.y2=s.y}),n=a(()=>{if(!tt.visible){return}if(!Ge.current){return}const e=Ge.current.findOne(".selection"),n=e?e.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(n.width&&n.height){const e=[{x:n.x,y:n.y},{x:n.x+n.width,y:n.y},{x:n.x+n.width,y:n.y+n.height},{x:n.x,y:n.y+n.height}],i=[];Ge.current.find(".element").forEach(n=>{var o;const r=t.getElementById(n.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=n.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const c=L(r.a).map(e=>a.point(e));F(e,c)&&i.push(r.top.id)});const o=[...new Set(i)];nt.current=!0,t.selectElements(o)}tt.visible=!1});return window.addEventListener("mousemove",e),window.addEventListener("touchmove",e),window.addEventListener("mouseup",n,{capture:!0}),window.addEventListener("touchend",n,{capture:!0}),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("touchmove",e),window.removeEventListener("mouseup",n,{capture:!0}),window.removeEventListener("touchend",n,{capture:!0})}},[]);const rt=o.useCallback(e=>{if(!t.selectedElements[0]){return}if(e&&t.selectedElementsIds.includes(e)){return}const n=V(t.selectedShapes),i=e?t.getElementById(e):{x:0,y:0,width:l.computedWidth,height:l.computedHeight,rotation:0},o=G(i),r=[];n.minX>o.maxX&&r.push({distance:n.minX-o.maxX,box1:n,box2:o,points:[{x:n.minX,y:n.minY+n.height/2},{x:o.maxX,y:n.minY+n.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),n.maxX<o.minX&&r.push({distance:o.minX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:n.minY+n.height/2},{x:o.minX,y:n.minY+n.height/2},{x:o.minX,y:o.minY+o.height/2}]}),n.minY>o.maxY&&r.push({box1:n,box2:o,distance:n.minY-o.maxY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]}),n.maxY<o.minY&&r.push({box1:n,box2:o,distance:o.minY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]});const s=n.minX>=o.minX&&n.maxX<=o.maxX&&n.minY>=o.minY&&n.maxY<=o.maxY;if(s&&(r.push({distance:n.minX-o.minX,box1:n,box2:o,points:[{x:n.minX,y:n.minY+n.height/2},{x:o.minX,y:n.minY+n.height/2},{x:o.minX,y:o.minY+o.height/2}]}),r.push({distance:o.maxX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:n.minY+n.height/2},{x:o.maxX,y:n.minY+n.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),r.push({box1:n,box2:o,distance:n.minY-o.minY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]}),r.push({box1:n,box2:o,distance:o.maxY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]})),0===r.length&&!s){const e=n.minX<o.maxX&&n.maxX>o.minX,t=n.minY<o.maxY&&n.maxY>o.minY;if(e&&t){const e=(Math.max(n.minY,o.minY)+Math.min(n.maxY,o.maxY))/2,t=(Math.max(n.minX,o.minX)+Math.min(n.maxX,o.maxX))/2;n.minX<o.minX?r.push({distance:o.minX-n.minX,box1:n,box2:o,points:[{x:n.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}):r.push({distance:n.minX-o.minX,box1:n,box2:o,points:[{x:n.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}),n.maxX>o.maxX?r.push({distance:n.maxX-o.maxX,box1:n,box2:o,points:[{x:o.maxX,y:e},{x:n.maxX,y:e},{x:o.maxX,y:e}]}):r.push({distance:o.maxX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:e},{x:o.maxX,y:e},{x:o.maxX,y:e}]}),n.minY<o.minY?r.push({box1:n,box2:o,distance:o.minY-n.minY,points:[{x:t,y:n.minY},{x:t,y:o.minY},{x:t,y:o.minY}]}):r.push({box1:n,box2:o,distance:n.minY-o.minY,points:[{x:t,y:n.minY},{x:t,y:o.minY},{x:t,y:o.minY}]}),n.maxY>o.maxY?r.push({box1:n,box2:o,distance:n.maxY-o.maxY,points:[{x:t,y:o.maxY},{x:t,y:n.maxY},{x:t,y:o.maxY}]}):r.push({box1:n,box2:o,distance:o.maxY-n.maxY,points:[{x:t,y:n.maxY},{x:t,y:o.maxY},{x:t,y:o.maxY}]})}}_e(e=>JSON.stringify(e)===JSON.stringify(r)?e:r)},[t,l]),lt=o.useCallback(()=>{t.distanceGuidesVisible&&t.selectedElements[0]?rt():_e(null)},[t,rt]);o.useEffect(lt,[t.distanceGuidesVisible,lt]);const st=t.distanceGuidesVisible||!!ze;o.useEffect(()=>{if(st){return le(l,()=>t.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|"),lt)}},[st,lt]);const at=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=Le.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{at.current=!0,clearTimeout(t),t=setTimeout(()=>{at.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const e=Ge.current;if(!e){return}const n=e.container(),i=new Map,o=e=>{var t,n;i.has(e.target)||[e.target,...null!==(n=null===(t=Be.current)||void 0===t?void 0:t.nodes())&&void 0!==n?n:[]].forEach(e=>{i.has(e)||i.set(e,{x:e.x(),y:e.y()})})},r=e=>{e.target===Be.current?i.clear():i.delete(e.target)};e.on("dragstart",o),e.on("dragend",r);const l=e=>{e.touches.length>=2&&c(()=>{var e,n;const o=new Map(i);(null===(e=Be.current)||void 0===e?void 0:e.isDragging())&&Be.current.stopDrag(),null===(n=Be.current)||void 0===n||n.nodes().forEach(e=>{var n;const i=o.get(e);e.isDragging()&&e.stopDrag(),i&&(e.setAttrs(i),null===(n=t.getElementById(e.id()))||void 0===n||n.set({x:i.x,y:i.y}))})})};return n.addEventListener("touchstart",l,{passive:!0}),n.addEventListener("touchmove",l,{passive:!0}),()=>{e.off("dragstart",o),e.off("dragend",r),n.removeEventListener("touchstart",l),n.removeEventListener("touchmove",l)}},[t]);const ct=e=>{if(t.role===Y.VIEWER){return}if(t.activePage!==l&&l.select(),at.current){return}if(nt.current){return}const n=e.evt.shiftKey,i=e.evt.ctrlKey||e.evt.metaKey,o=n||i,r=e.target.findAncestor(".elements-container"),s=e.target.findAncestor(".page-abs-container"),a=e.target.findAncestor("Transformer"),c=e.target.findAncestor(".page-container",!0),d=!(o||r||a||s||c||tt.visible);if(t.activeTool&&"crop"!==t.activeTool.name&&!r&&!a){return}if(d){return t.selectElements([]),void t.selectPages([])}if(i&&!n){const e=Ge.current,n=null==e?void 0:e.getPointerPosition();if(!e||!n){return}const i=e.getAllIntersections(n),o=new Set,r=[];for(const s of[...i].reverse()){const e=s.findAncestor(".element",!0);if(!e){continue}const n=t.getElementById(e.id()),i=null==n?void 0:n.top;i&&(o.has(i.id)||(o.add(i.id),r.push(i.id)))}if(0===r.length){return}let l;if(1===t.selectedElementsIds.length){const e=t.selectedElementsIds[0],n=r.indexOf(e);l=-1===n?r[0]:r[(n+1)%r.length]}else{l=r[0]}return t.selectElements([l]),void t.selectPages([])}const m=e.target.findAncestor(".element-container",!0),u=(null==m?void 0:m.findOne(".element"))||e.target.findAncestor(".element",!0),h=t.getElementById(null==u?void 0:u.id());h&&"pendingEdit"in h&&h._setImageEditError(null);const g=null==h?void 0:h.top;let f;if("group"===P){f=null==g?void 0:g.id}else if(g){const e=t.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===g.id});f=e?null==h?void 0:h.id:null==g?void 0:g.id}else{f=void 0}const p=null==h?void 0:h.id;if(p&&t.selectedElementsIds.indexOf(p)>=0&&!n){return}const x=!!f&&t.selectedElementsIds.indexOf(f)>=0,v=e.target.findAncestor(".page-container",!0);if(f&&n&&!x){const e=re(t.selectedElementsIds.concat([f]),e=>oe(t,e));t.selectElements(e),t.selectPages([])}else{f&&n&&x?t.selectElements(t.selectedElementsIds.filter(e=>e!==f)):!f||n||x?f&&!n&&x||(v?(n||t.selectElements([]),t.selectPages([l.id])):t.selectPages([])):(t.selectElements([f]),t.selectPages([]))}};I(Be,t);const dt=t.activePage===l,mt=t._selectedPagesIds.includes(l.id),ut=null==y?void 0:y.PageControls,ht=null==y?void 0:y.Tooltip,gt=null==y?void 0:y.ContextMenu,ft=1/t.scale,pt=0/t.scale;return i("div",{ref:Le,onDragOver:e=>e.preventDefault(),onDrop:e=>{if(e.preventDefault(),t.role===Y.VIEWER){return}if(!Ge.current){return}Ge.current.setPointersPositions(e);const n=Ge.current.findOne(".elements-container"),i=null==n?void 0:n.getRelativePointerPosition(),o=Ge.current.getPointerPosition(),r=Ge.current.getAllIntersections(o).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(r.reverse())].map(e=>t.getElementById(e.id())).filter(e=>!!e),a=s[0];je&&i&&(je(i,a,{elements:s,page:l}),je=null)},style:{position:"relative",width:d+"px",height:h+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+(dt?" active-page":""),children:[n(ae,{ref:Ge,eventBatchFunc:c,width:Math.min(d,O.width+200),height:Math.min(O.height+200,h),onClick:ct,onTap:ct,onContextMenu:e=>{if(e.evt.preventDefault(),t.role===Y.VIEWER){return}const n=e.target.findAncestor(".element",!0),i=t.getElementById(null==n?void 0:n.id()),o=null==i?void 0:i.top,r=null==o?void 0:o.id;if(!r){return t.selectElements([]),void Je.close()}t.selectedElementsIds.indexOf(r)>=0||t.selectElements([r]),Je.open({x:e.evt.clientX,y:e.evt.clientY})},onMouseDown:e=>{it(e),ot(e)},onTouchStart:it,onMouseMove:e=>{if(!t.distanceGuidesVisible&&!e.evt.altKey){return void(ze&&_e(null))}const n=e.target.findAncestor(".element",!0),i=null==n?void 0:n.id();rt(i)},onDragStart:e=>{var n;const i=e.target.getStage();if(i&&i.getPointersPositions().length>=2){return void e.target.stopDrag()}const o=e.target.findAncestor(".element",!0);if(o){const i=t.getElementById(null==o?void 0:o.id()),r=null==i?void 0:i.top,l=null==r?void 0:r.id;if(!(i&&t.selectedElementsIds.indexOf(i.id)>=0||l&&t.selectedElementsIds.indexOf(l)>=0)&&l){t.selectElements([l]);const i=$e();e.target.stopDrag(),i.forEach(t=>{t.isDragging()||t.startDrag(e)}),null===(n=Be.current)||void 0===n||n.startDrag(e),j()}}ze&&_e(null)},onDragMove:e=>{const n=Be.current;n&&e.target===n&&n.nodes().forEach(e=>{const n=t.getElementById(e.id());!n||n.x===e.x()&&n.y===e.y()||n.set({x:e.x(),y:e.y()})})},pageId:l.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:i(f,{children:[n(xe,{width:d,height:h,fill:J.workspaceBackgroundColor}),n(u,{x:Ie,y:Pe,scaleX:t.scale,scaleY:t.scale,name:"page-container",children:i(u,{name:"page-container-2",children:[n(u,{x:be.left,y:be.top,children:n(ye,Object.assign({},Te,De,{transparencyBackgroundColor:J.transparencyBackgroundColor}))}),i(u,{isolated:l.hasBlendMode,children:[n(u,{name:"page-background-group",x:be.left,y:be.top,clip:De,children:n(we,Object.assign({},Te))}),i(u,{x:be.left,y:be.top,name:"elements-container",listening:!t.isPlaying&&t.role!==Y.VIEWER,children:[n(x,{name:"elements-area",width:l.computedWidth,height:l.computedHeight,listening:!1}),n(Me,{elements:l.children,store:t}),n(Ae,{page:l})]})]}),n(v,{name:"bleed",fill:J.bleedColor,listening:!1,visible:t.bleedVisible&&(be.top>0||be.right>0||be.bottom>0||be.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:i,bottom:o,left:r}=ve,s=l.computedWidth,a=l.computedHeight,c=e._context;c.beginPath(),c.rect(0,0,r+s+i,n+a+o),c.rect(r,n,s,a),c.fillStyle=t.fill(),c.fill("evenodd")}}),B()===Oe&&n(Re,{name:"hit-detection",x:-ft/2-pt,y:-ft/2-pt,width:Se+ft+2*pt,height:ke+ft+2*pt})]})}),n(p,{name:"workspace-background",points:[0,0,d,0,d,h,0,h,0,0,Ie,Pe,Ie,h-Pe,d-Ie,h-Pe,d-Ie,Pe,Ie,Pe],listening:!1,closed:!0,fill:J.workspaceBackgroundColor}),n(u,{x:Ie,y:Pe,scaleX:t.scale,scaleY:t.scale,children:n(x,{name:"page-highlight",hideInExport:!0,x:-ft/2-pt,y:-ft/2-pt,width:Se+ft+2*pt,height:ke+ft+2*pt,stroke:mt?J.activePageBorderColor:J.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),i(u,{x:Ie+be.left*t.scale,y:Pe+be.top*t.scale,scaleX:t.scale,scaleY:t.scale,name:"page-abs-container",children:[n(S,{ref:Be,name:H,rotateAnchorAngle:Ze,onDragStart:e=>{var n;const i=e.target.getStage();i&&i.getPointersPositions().length>=2?e.target.stopDrag():((null===(n=e.evt)||void 0===n?void 0:n.altKey)&&A&&D(t.selectedElements,l,t),Fe.current||(Fe.current=!0,t.history.startTransaction()))},onDragEnd:()=>{Fe.current&&(t.history.endTransaction(),Fe.current=!1),et()},onTransformStart:()=>{Ue.setTransforming(!0),t.history.startTransaction(),We.current=new Map(Ne.map(e=>[e.id,{width:e.width,height:e.height}])),_e(null)},boundBoxFunc:(e,n)=>{var i,o,r,l;const s=null!==(l=null===(i=ee.boundBoxFunc)||void 0===i?void 0:i.call(ee,e,n))&&void 0!==l?l:n,a=Math.abs(s.width)<1||Math.abs(s.height)<1,c=Math.abs(e.width)<1||Math.abs(e.height)<1;if(a&&!c){return e}const d=t.selectedElements;if(1===d.length&&"table"===(null===(o=d[0])||void 0===o?void 0:o.type)){const t=d[0],n=Math.abs(e.width),i=Math.abs(e.height);if(n>0&&i>0){const o=Math.abs(s.width)/n,r=Math.abs(s.height)/i,l=t.width*o,a=t.height*r,c=Object.assign({},s);return l<t.minWidth&&(c.width=e.width),a<t.minHeight&&(c.height=e.height),$(c,s,e)}}return Ke&&Ne.length>0?((e,t,n,i,o)=>{const r=Math.abs(e.width),l=Math.abs(e.height);if(0===r||0===l){return t}const s=Math.abs(t.width)/r,a=Math.abs(t.height)/l,c=!U(n),d=e=>i.some(t=>{var n;const i=null==o?void 0:o.get(t.id),r=null!==(n="x"===e?null==i?void 0:i.width:null==i?void 0:i.height)&&void 0!==n?n:"x"===e?t.width:t.height,l="x"===e?t.width:t.height,d="x"===e?s:a,m=K(t,e,c,r);return r>=m&&l*d<m}),m=Object.assign({},t),u=t.width*e.width<0,h=t.height*e.height<0,g=u||d("x"),f=h||d("y"),p=f||!c&&g;return(g||!c&&f)&&(m.width=e.width),p&&(m.height=e.height),$(m,t,e)})(e,s,(null===(r=Be.current)||void 0===r?void 0:r.getActiveAnchor())||"",Ne,We.current):s},onTransform:e=>{const t=Be.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{qe()},0)},onTransformEnd:e=>{Ue.setTransforming(!1),Ue.setLabel({}),t.history.endTransaction(),We.current=null,et()},visible:!t.isPlaying}),n(Ye,{store:t,gesture:Ue,transformerStyle:ee,onChange:Qe}),ze&&ze.map(({points:e,distance:o,box1:r,box2:l},s)=>i(u,{name:"distances-container",hideInExport:!0,listening:!1,children:[n(x,Object.assign({},r,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),n(x,Object.assign({},l,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),n(p,{points:[e[0].x,e[0].y,e[1].x,e[1].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(p,{points:[e[1].x,e[1].y,e[2].x,e[2].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),i(g,{x:(e[0].x+e[1].x)/2,y:(e[0].y+e[1].y)/2,offsetY:-10,scaleX:1/t.scale,scaleY:1/t.scale,children:[n(w,{cornerRadius:5,fill:J.distanceGuideStyle.labelFill,pointerDirection:"down"}),n(E,{align:"center",verticalAlign:"middle",fill:J.distanceGuideStyle.labelTextFill,padding:5,text:_({unit:t.unit,dpi:t.dpi,px:o})})]})]},s)),l._rendering&&i(u,{children:[n(x,{width:Se,height:ke,fill:"rgba(255,255,255,0.9)"}),n(E,{text:"Rendering...",fontSize:60,width:Se,height:ke,align:"center",verticalAlign:"middle"})]}),ht&&n(ht,{components:y,store:t,page:l,stageRef:Ge,tooltipSafeArea:z}),gt&&n(ie,{children:n(gt,Object.assign({components:y,store:t},Je.props))}),n(R,{store:t,pageId:l.id})]}),n(M,{store:t,page:l,width:d,height:h,scale:t.scale,xPadding:Ie,yPadding:Pe,bleedLeft:be.left,bleedTop:be.top}),n(Xe,{gesture:Ue,store:t}),n(Ee,{selection:tt}),C()>0&&i(u,{hideInExport:!0,children:[n(Ce,{name:"cache-bounds",x:d-(1===C()?250:210),y:h-(1===C()?95:45)}),n(Ce,{name:"cache-bounds-t",x:10,y:10})]}),W()&&n(E,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:d-170,y:h-18,onMouseEnter:e=>{e.target.getStage().container().style.cursor="pointer"},onMouseLeave:e=>{e.target.getStage().container().style.cursor=""},onTouchStart:e=>{e.cancelBubble=!0},onMouseDown:e=>{e.cancelBubble=!0},onClick:()=>{window.open("https://polotno.com","_blank","noopener")},onTap:()=>{window.open("https://polotno.com","_blank","noopener")}}),n(u,{name:"line-guides"})]})}),Ve&&ut&&n(ut,{store:t,page:l,xPadding:Ie,yPadding:Pe,layout:T})]})});
|
|
1
|
+
var e=this&&this.__rest||function(e,t){var n={};for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++){t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(n[i[o]]=e[i[o]])}}return n};import{Fragment as t,jsx as n,jsxs as i}from"react/jsx-runtime";import o from"react";import{clamp as r}from"../lib/utils.js";import{observer as l,useLocalObservable as s}from"mobx-react-lite";import{action as a,runInAction as c}from"mobx";import{isAlive as d}from"mobx-state-tree";import{ZERO_BLEED as m}from"@polotno/schema";import{Group as u,Image as h,Label as g,Layer as f,Line as p,Rect as x,Shape as v,Stage as b,KonvaRenderer as y,Tag as w,Text as E,Transformer as S}from"react-konva";import{useWorkspaceStyle as X}from"./workspace-style.js";import{ROLES as Y}from"../model/store.js";import k from"konva";import A from"./element.js";import{DrawingLayer as j}from"./drawing-layer.js";import{ensureDragOrder as M,useSnap as I}from"./use-transformer-snap.js";import{useImageLoader as O,useSizedImage as P}from"./image-element.js";import{FadeOverlay as R}from"./fade-overlay.js";import{RadialGradientHandle as T}from"./radial-gradient-handle.js";import{getCrop as D}from"../utils/crop.js";import{cloneBehind as B}from"../utils/duplicate.js";import{___ as C,____ as W,isCreditVisible as G}from"../utils/validate-key.js";import{getClientRect as L,getCorners as V,getTotalClientRect as F,polygonsIntersect as z}from"../utils/math.js";import{pxToUnitRounded as _,pxToUnitString as U}from"../utils/unit.js";import{isCornerAnchor as Z,isRotateAnchor as H,MAIN_TRANSFORMER_NAME as J}from"../utils/px.js";import{canStretchAxis as N,isAxisAligned as K,leafMinSize as $}from"../utils/selection-transform.js";import{restoreHeldPoint as Q}from"./transform-box.js";import{flags as q}from"../utils/flags.js";import{textOverflowOf as ee}from"../utils/text-sizing.js";import{useColor as te}from"./use-color.js";import{isGradient as ne}from"@polotno/core/gradient";import{toDisplayColor as ie}from"@polotno/core/color";import{backgroundPaintBox as oe}from"@polotno/core/page";import{Html as re}from"react-konva-utils";import{getParentGroupId as le,withoutDescendants as se}from"../utils/selection.js";import{nodeReaction as ae}from"../utils/mobx.js";import{useEvent as ce}from"../hooks/use-event.js";const de=o.version.startsWith("18.")?o.forwardRef((e,t)=>(o.useLayoutEffect(()=>{y.flushSync()}),n(b,Object.assign({},e,{ref:t})))):b,me={enabledAnchors:["top-left","top-center","top-right","middle-left","bottom-left","bottom-right","bottom-center","middle-right"],rotateEnabled:!0,rotationSnaps:[0,45,90,135,180,225,270,315],ignoreStroke:!0,flipEnabled:!1,keepRatio:!0},ue=e=>{if(!e.length){return null}const t=K(e[0].rotation)?0:e[0].rotation;return e.every(e=>K(e.rotation-t))?t:null},he=e=>e.selectedShapes.filter(t=>{var n;return(null===(n=e.activeTool)||void 0===n?void 0:n.elementId)!==t.id&&!t.pendingEdit}),ge=e=>e.selectedShapes.some(e=>!e.resizable),fe=e=>e.selectedShapes.some(e=>!e.draggable),pe=e=>{var t;return 1!==e.selectedElements.length||"group"===(null===(t=e.selectedElements[0])||void 0===t?void 0:t.type)},xe=e=>{var t;const n=he(e),i=pe(e),o=null!==(t=ue(n))&&void 0!==t?t:0,r=e=>i&&n.length>0&&n.every(t=>N(t,e,o));return{boxRotation:o,canStretchX:r("x"),canStretchY:r("y")}},ve={text:{enabledAnchors:["top-left","top-right","middle-left","bottom-left","bottom-right","middle-right"]},svg:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},gif:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},lottie:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},line:{enabledAnchors:[],borderEnabled:!1,rotateEnabled:!1},image:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},many:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},group:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},table:{keepRatio:!1,enabledAnchors:["top-left","top-center","top-right","middle-left","middle-right","bottom-left","bottom-center","bottom-right"]}};export function registerTransformerAttrs(e,t){ve[e]=ve[e]||t,Object.assign(ve[e],t)}const be=e=>n(x,Object.assign({},e,{preventDefault:!1})),ye=t=>{var{url:i}=t,o=e(t,["url"]);const[r,l]=P(i),s=r?D(r,{width:o.width,height:o.height},"center-middle"):{};return O(l,i,"page background"),n(h,Object.assign({image:r},o,s))},we=e=>{const t=te({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return n(x,Object.assign({},e,t))},Ee=t=>{var{background:i,transparencyBackgroundColor:r}=t,l=e(t,["background","transparencyBackgroundColor"]);const s=o.useMemo(()=>{if(ne(i)){return!0}const e=k.Util.colorToRGBA(i);return!e||e.a<1},[i]),a=o.useMemo(()=>{if(!s||r){return}const e=document.createElement("canvas"),t=30;e.width=60,e.height=60;const n=e.getContext("2d");return n&&(n.fillStyle="black",n.fillRect(t,0,t,t),n.fillRect(0,t,t,t)),e},[s,r]);return s?n(x,r?Object.assign({fill:r},l,{hideInExport:!0}):Object.assign({fillPatternImage:a},l,{opacity:.1,hideInExport:!0})):null},Se=t=>{var{background:i}=t,r=e(t,["background"]);return o.useMemo(()=>!!k.Util.colorToRGBA(i)||ne(i),[i])?n(we,Object.assign({fill:i},r)):n(ye,Object.assign({url:i},r))},Xe=l(({selection:e})=>{const{selectionRectStyle:t}=X();return e.visible?n(x,{name:"selection",x:Math.min(e.x1,e.x2),y:Math.min(e.y1,e.y2),width:Math.abs(e.x1-e.x2),height:Math.abs(e.y1-e.y2),fill:t.fill,stroke:t.stroke,strokeWidth:t.strokeWidth}):null});function Ye(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const ke=l(({gesture:e,store:o})=>{const{transformLabelStyle:r}=X(),{x:l,y:s,width:a,height:c,rotation:d,anchor:m}=e.label;if(void 0===l||void 0===s||void 0===a||void 0===c||void 0===d||void 0===m){return null}const u=L({x:l,y:s,width:a,height:c,rotation:k.Util.radToDeg(d)}),h=null!==m&&H(m),f=(c/2+70)*Math.cos(d-Math.PI/2),p=(c/2+70)*Math.sin(d-Math.PI/2),x=_({unit:o.unit,dpi:o.dpi,px:a/o.scale,precious:"px"===o.unit?0:1})+" x "+_({unit:o.unit,dpi:o.dpi,px:c/o.scale,precious:"px"===o.unit?0:1})+("px"===o.unit?"":" "+o.unit);return i(t,{children:[i(g,{x:(u.minX+u.maxX)/2+f,y:(u.minY+u.maxY)/2+p,offsetX:14,offsetY:14,visible:h,children:[n(w,{cornerRadius:5,fill:r.fill}),n(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:Ye(d).toString()+"°"})]}),i(g,{x:(u.minX+u.maxX)/2,y:u.maxY+20,visible:!h,children:[n(w,{cornerRadius:5,fill:r.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),n(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:x})]})]})}),Ae=l(({store:e,gesture:t,transformerStyle:n,onChange:i})=>{var r;const l=e.selectedElements.some(e=>e.curveEnabled),s=e.selectedElements.some(e=>{var t;return!(null!==(t=e.filled.visible)&&void 0!==t?t:e.visible)}),a=JSON.stringify(he(e).map(e=>e.id)),c=!pe(e)&&e.selectedElements[0].keepRatio,d=!pe(e)&&"text"===(null===(r=e.selectedElements[0])||void 0===r?void 0:r.type)&&"fit"===ee(e.selectedElements[0]),m=t.transforming?null:xe(e);return o.useLayoutEffect(()=>{i()},[e.selectedShapes,a,ge(e),s,fe(e),l,c,d,t.transforming,null==m?void 0:m.canStretchX,null==m?void 0:m.canStretchY,null==m?void 0:m.boxRotation,n]),null}),je=()=>{console.warn("Polotno warning: onClick callback is deprecated. Just stop using it. Polotno will do selection automatically.")},Me=l(({page:e})=>{const t=e._exportingOrRendering||e._forceMount;return o.useEffect(()=>{if(t&&d(e)){return e.set({_exportRendered:!0}),()=>{d(e)&&e.set({_exportRendered:!1})}}},[e,t]),null}),Ie=l(({elements:e,store:t})=>{const i=e.filter(e=>e.alwaysOnTop),r=e.filter(e=>!e.alwaysOnTop).concat(i);return n(o.Fragment,{children:r.map(e=>n(A,{store:t,element:e,onClick:je},e.id))})});export const useContextMenu=({store:e})=>{const[t,n]=o.useState(!1),[i,r]=o.useState({x:0,y:0}),l=o.useCallback(e=>{n(!0),r(e)},[]);return{open:l,close:o.useCallback(()=>{n(!1)},[]),props:{isOpen:t,offset:i,setIsOpen:n}}};let Oe=null;export const registerNextDomDrop=e=>{Oe=e};const Pe=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),Re=atob("I2MwMzkyYg=="),Te=atob("djAuOS4y"),De=e=>n(t,{children:i(g,{fill:Re,height:200,children:[n(w,{fill:Re}),n(E,Object.assign({},e,{fill:"white",text:Pe,height:void 0,padding:10,fontSize:20}))]})}),Be=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),Ce=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),We=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),Ge=e=>{const t=1===W(),o=t?Ce:Be,r=t?230:195;return i(u,Object.assign({},e,{listening:!1,children:[n(x,{fill:Re,width:r,height:24,cornerRadius:2}),n(E,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),t&&n(E,{text:We,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default l(({store:t,page:l,width:d,height:h,pageControlsEnabled:b,components:y,altCloneEnabled:A,groupSelectionMode:O="drill",viewportSize:P,layout:D="vertical",tooltipSafeArea:_,transformerSafeArea:H})=>{const N=X(),te=N.transformerStyle,ne=me,ye=l.computedBleed,we=t.bleedVisible?ye:m,Ye=l.computedWidth+we.left+we.right,je=l.computedHeight+we.top+we.bottom,Pe=(d-Ye*t.scale)/2,Re=(h-je*t.scale)/2,Be=Object.assign(Object.assign({},oe(ye,l.computedWidth,l.computedHeight)),{background:ie(l.background),name:"page-background",preventDefault:!1}),Ce={x:-we.left,y:-we.top,width:Ye,height:je},We=o.useRef(null),Le=o.useRef(null),Ve=o.useRef(null),Fe=o.useRef(null),ze=null==b||b,_e=o.useRef(!1),[Ue,Ze]=o.useState(null),He=s(()=>({transforming:!1,label:{},setLabel(e){this.label=e},setTransforming(e){this.transforming=e}})),[Je,Ne]=o.useState(0),Ke=useContextMenu({store:t}),$e=he(t),Qe=pe(t),qe=()=>{var n,i,o;const r=We.current;if(!r){return[]}const l=r.getStage();if(!l){return[]}const s=he(t),a=new Set(s.map(e=>e.id)),c=new Map;a.size&&l.find(e=>{const t=e.id();return a.has(t)&&!c.has(t)&&c.set(t,e),!1});const d=s.map(e=>c.get(e.id)).filter(e=>!!e),m=1===t.selectedElements.length,u=m&&(null===(n=t.selectedElements[0])||void 0===n?void 0:n.type)||"many",h=ve[u],g=Object.assign(Object.assign({},ne),h||{});h&&("svg"!==u&&"image"!==u&&"gif"!==u&&"lottie"!==u||t.selectedElements[0].keepRatio||(g.enabledAnchors=ne.enabledAnchors),"text"!==u||!q.textVerticalResizeEnabled&&"fit"!==ee(t.selectedElements[0])||(g.enabledAnchors=null===(i=ve.text.enabledAnchors)||void 0===i?void 0:i.concat(["bottom-center","top-center"])),"text"===u&&m&&t.selectedElements[0].curveEnabled&&(g.enabledAnchors=ve.many.enabledAnchors));const f=xe(t);(f.canStretchX||f.canStretchY)&&(g.enabledAnchors=[...g.enabledAnchors||[],...f.canStretchX?["middle-left","middle-right"]:[],...f.canStretchY?["top-center","bottom-center"]:[]]);const{boundBoxFunc:p}=te,x=e(te,["boundBoxFunc"]);r.setAttrs(Object.assign(Object.assign(Object.assign({},g),x),{name:J})),ge(t)&&r.enabledAnchors([]),fe(t)&&r.rotateEnabled(!1);const v=r.nodes();if(v.length===d.length&&d.every((e,t)=>v[t]===e)||r.nodes(d),pe(t)&&d.length){const e=ue(s);null===e||K(r.rotation()-e)||(r.rotation(e),r.forceUpdate())}return null===(o=r.getLayer())||void 0===o||o.batchDraw(),d},et=ce(()=>{var e;if(null===(e=We.current)||void 0===e?void 0:e.isTransforming()){return}const t=qe();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),M())}),tt=()=>{var e,t;const n=We.current;if(!n){return}if(!n.isTransforming()){return void He.setLabel({})}if(!n.nodes().length){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();He.setLabel({anchor:o,x:i.x-(null!==(e=null==r?void 0:r.x())&&void 0!==e?e:0),y:i.y-(null!==(t=null==r?void 0:r.y())&&void 0!==t?t:0),rotation:i.rotation,width:i.width,height:i.height})},nt=o.useCallback(()=>{var e;if(null===(e=We.current)||void 0===e?void 0:e.isTransforming()){return}const t=We.current;if(!t||!t.nodes().length){return void Ne(0)}const n=t.getClientRect();if(!t.getStage()){return}const i=function(e,t,n,i=0){const o=e.x+e.width/2,r=e.y+e.height/2,l=e.width/2,s=e.height/2,a=i*Math.PI/180,c=e=>{const t=e*Math.PI/180,i=(e%360+360)%360;let c;c=0===i||180===i?s:l;const d=Math.sin(t)*(c+n),m=-Math.cos(t)*(c+n),u=d*Math.cos(a)-m*Math.sin(a),h=d*Math.sin(a)+m*Math.cos(a);return{x:o+u,y:r+h}},d=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(d(c(0))){return 0}for(const m of[180,270,90]){if(d(c(m))){return m}}return 0}(n,{top:(null==H?void 0:H.top)||0,bottom:h-((null==H?void 0:H.bottom)||0),left:(null==H?void 0:H.left)||0,right:d-((null==H?void 0:H.right)||0)},te.rotateAnchorOffset,t.rotation());Ne(i)},[H,d,h]);o.useEffect(()=>{var e;null===(e=We.current)||void 0===e||e.update(),tt(),nt()},[t.scale,nt]),o.useEffect(()=>{nt()},[t.selectedShapes,nt]);const it=s(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),ot=o.useRef(!1),rt=e=>{const n=Ve.current;if(!n||t.role===Y.VIEWER||t.activePage!==l||t.isPlaying||"button"in e.evt&&0!==e.evt.button||1!==n.getPointersPositions().length||!e.target.findAncestor(".elements-container")||!e.target.findAncestor(".element",!0)||!t.selectedShapes.length||fe(t)){return}const i=n.getPointerPosition();if(!i){return}const o=qe();if(o.some(t=>t===e.target||t.isAncestorOf(e.target))){return}const r=o.find(e=>!!(e.draggable()&&e.isVisible()&&e.isListening())&&(e instanceof k.Shape?[e]:e instanceof k.Container?e.find("Shape"):[]).some(e=>e.intersects(i)));if(r){for(const[t,n]of k.DD._dragElements){"ready"!==n.dragStatus||n.node!==e.target&&!n.node.isAncestorOf(e.target)||k.DD._dragElements.delete(t)}r._createDragElement(e),"touches"in e.evt&&e.evt.cancelable&&r.preventDefault()&&e.evt.preventDefault()}},lt=a(e=>{var n,i,o,r,l,s,a;if(t.role===Y.VIEWER){return}if(ot.current=!1,t.activeTool&&"crop"!==t.activeTool.name){return}const c=e.target.findAncestor(".elements-container"),d=e.target.findAncestor("Transformer"),m=e.target.findAncestor(".page-abs-container");if(c||d||m){return}const u=null===(n=e.target.getStage())||void 0===n?void 0:n.getPointerPosition();u&&(u.x-=null!==(l=null===(i=e.target.getStage())||void 0===i?void 0:i.x())&&void 0!==l?l:0,u.y-=null!==(s=null===(o=e.target.getStage())||void 0===o?void 0:o.y())&&void 0!==s?s:0,it.visible=!0,it.x1=u.x,it.y1=u.y,it.x2=u.x,it.y2=u.y,(null!==(a=null===(r=e.target.getStage())||void 0===r?void 0:r.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(it.visible=!1))});(({stageRef:e,containerRef:t,viewportSize:n})=>{o.useEffect(()=>{var n;const i=null===(n=t.current)||void 0===n?void 0:n.closest(".polotno-workspace-inner");function o(){var n;if(!e.current){return}const o=null===(n=t.current)||void 0===n?void 0:n.getBoundingClientRect(),l=null==i?void 0:i.getBoundingClientRect();if(!o||!l){return}const s=r(l.left-o.left-100,0,Math.max(0,o.width-e.current.width())),a=r(l.top-o.top-100,0,Math.max(0,o.height-e.current.height()));e.current.position({x:-s,y:-a}),e.current.container().style.transform=`translate(${s}px, ${a}px)`}return o(),null==i||i.addEventListener("scroll",o),()=>{null==i||i.removeEventListener("scroll",o)}},[n.width,n.height])})({stageRef:Ve,containerRef:Fe,viewportSize:P}),o.useEffect(()=>{const e=a(e=>{var t,n,i,o,r,l;if(!it.visible){return}null===(t=Ve.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=Ve.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(r=null===(i=Ve.current)||void 0===i?void 0:i.x())&&void 0!==r?r:0,s.y-=null!==(l=null===(o=Ve.current)||void 0===o?void 0:o.y())&&void 0!==l?l:0):s={x:it.x2,y:it.y2},it.x2=s.x,it.y2=s.y}),n=a(()=>{if(!it.visible){return}if(!Ve.current){return}const e=Ve.current.findOne(".selection"),n=e?e.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(n.width&&n.height){const e=[{x:n.x,y:n.y},{x:n.x+n.width,y:n.y},{x:n.x+n.width,y:n.y+n.height},{x:n.x,y:n.y+n.height}],i=[];Ve.current.find(".element").forEach(n=>{var o;const r=t.getElementById(n.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=n.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const c=V(r.a).map(e=>a.point(e));z(e,c)&&i.push(r.top.id)});const o=[...new Set(i)];ot.current=!0,t.selectElements(o)}it.visible=!1});return window.addEventListener("mousemove",e),window.addEventListener("touchmove",e),window.addEventListener("mouseup",n,{capture:!0}),window.addEventListener("touchend",n,{capture:!0}),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("touchmove",e),window.removeEventListener("mouseup",n,{capture:!0}),window.removeEventListener("touchend",n,{capture:!0})}},[]);const st=o.useCallback(e=>{if(!t.selectedElements[0]){return}if(e&&t.selectedElementsIds.includes(e)){return}const n=F(t.selectedShapes),i=e?t.getElementById(e):{x:0,y:0,width:l.computedWidth,height:l.computedHeight,rotation:0},o=L(i),r=[];n.minX>o.maxX&&r.push({distance:n.minX-o.maxX,box1:n,box2:o,points:[{x:n.minX,y:n.minY+n.height/2},{x:o.maxX,y:n.minY+n.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),n.maxX<o.minX&&r.push({distance:o.minX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:n.minY+n.height/2},{x:o.minX,y:n.minY+n.height/2},{x:o.minX,y:o.minY+o.height/2}]}),n.minY>o.maxY&&r.push({box1:n,box2:o,distance:n.minY-o.maxY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]}),n.maxY<o.minY&&r.push({box1:n,box2:o,distance:o.minY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]});const s=n.minX>=o.minX&&n.maxX<=o.maxX&&n.minY>=o.minY&&n.maxY<=o.maxY;if(s&&(r.push({distance:n.minX-o.minX,box1:n,box2:o,points:[{x:n.minX,y:n.minY+n.height/2},{x:o.minX,y:n.minY+n.height/2},{x:o.minX,y:o.minY+o.height/2}]}),r.push({distance:o.maxX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:n.minY+n.height/2},{x:o.maxX,y:n.minY+n.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),r.push({box1:n,box2:o,distance:n.minY-o.minY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]}),r.push({box1:n,box2:o,distance:o.maxY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]})),0===r.length&&!s){const e=n.minX<o.maxX&&n.maxX>o.minX,t=n.minY<o.maxY&&n.maxY>o.minY;if(e&&t){const e=(Math.max(n.minY,o.minY)+Math.min(n.maxY,o.maxY))/2,t=(Math.max(n.minX,o.minX)+Math.min(n.maxX,o.maxX))/2;n.minX<o.minX?r.push({distance:o.minX-n.minX,box1:n,box2:o,points:[{x:n.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}):r.push({distance:n.minX-o.minX,box1:n,box2:o,points:[{x:n.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}),n.maxX>o.maxX?r.push({distance:n.maxX-o.maxX,box1:n,box2:o,points:[{x:o.maxX,y:e},{x:n.maxX,y:e},{x:o.maxX,y:e}]}):r.push({distance:o.maxX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:e},{x:o.maxX,y:e},{x:o.maxX,y:e}]}),n.minY<o.minY?r.push({box1:n,box2:o,distance:o.minY-n.minY,points:[{x:t,y:n.minY},{x:t,y:o.minY},{x:t,y:o.minY}]}):r.push({box1:n,box2:o,distance:n.minY-o.minY,points:[{x:t,y:n.minY},{x:t,y:o.minY},{x:t,y:o.minY}]}),n.maxY>o.maxY?r.push({box1:n,box2:o,distance:n.maxY-o.maxY,points:[{x:t,y:o.maxY},{x:t,y:n.maxY},{x:t,y:o.maxY}]}):r.push({box1:n,box2:o,distance:o.maxY-n.maxY,points:[{x:t,y:n.maxY},{x:t,y:o.maxY},{x:t,y:o.maxY}]})}}Ze(e=>JSON.stringify(e)===JSON.stringify(r)?e:r)},[t,l]),at=o.useCallback(()=>{t.distanceGuidesVisible&&t.selectedElements[0]?st():Ze(null)},[t,st]);o.useEffect(at,[t.distanceGuidesVisible,at]);const ct=t.distanceGuidesVisible||!!Ue;o.useEffect(()=>{if(ct){return ae(l,()=>t.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|"),at)}},[ct,at]);const dt=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=Fe.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{dt.current=!0,clearTimeout(t),t=setTimeout(()=>{dt.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const e=Ve.current;if(!e){return}const n=e.container(),i=new Map,o=e=>{var t,n;i.has(e.target)||[e.target,...null!==(n=null===(t=We.current)||void 0===t?void 0:t.nodes())&&void 0!==n?n:[]].forEach(e=>{i.has(e)||i.set(e,{x:e.x(),y:e.y()})})},r=e=>{e.target===We.current?i.clear():i.delete(e.target)};e.on("dragstart",o),e.on("dragend",r);const l=e=>{e.touches.length>=2&&c(()=>{var e,n;const o=new Map(i);(null===(e=We.current)||void 0===e?void 0:e.isDragging())&&We.current.stopDrag(),null===(n=We.current)||void 0===n||n.nodes().forEach(e=>{var n;const i=o.get(e);e.isDragging()&&e.stopDrag(),i&&(e.setAttrs(i),null===(n=t.getElementById(e.id()))||void 0===n||n.set({x:i.x,y:i.y}))})})};return n.addEventListener("touchstart",l,{passive:!0}),n.addEventListener("touchmove",l,{passive:!0}),()=>{e.off("dragstart",o),e.off("dragend",r),n.removeEventListener("touchstart",l),n.removeEventListener("touchmove",l)}},[t]);const mt=e=>{if(t.role===Y.VIEWER){return}if(t.activePage!==l&&l.select(),dt.current){return}if(ot.current){return}const n=e.evt.shiftKey,i=e.evt.ctrlKey||e.evt.metaKey,o=n||i,r=e.target.findAncestor(".elements-container"),s=e.target.findAncestor(".page-abs-container"),a=e.target.findAncestor("Transformer"),c=e.target.findAncestor(".page-container",!0),d=!(o||r||a||s||c||it.visible);if(t.activeTool&&"crop"!==t.activeTool.name&&!r&&!a){return}if(d){return t.selectElements([]),void t.selectPages([])}if(i&&!n){const e=Ve.current,n=null==e?void 0:e.getPointerPosition();if(!e||!n){return}const i=e.getAllIntersections(n),o=new Set,r=[];for(const s of[...i].reverse()){const e=s.findAncestor(".element",!0);if(!e){continue}const n=t.getElementById(e.id()),i=null==n?void 0:n.top;i&&(o.has(i.id)||(o.add(i.id),r.push(i.id)))}if(0===r.length){return}let l;if(1===t.selectedElementsIds.length){const e=t.selectedElementsIds[0],n=r.indexOf(e);l=-1===n?r[0]:r[(n+1)%r.length]}else{l=r[0]}return t.selectElements([l]),void t.selectPages([])}const m=e.target.findAncestor(".element-container",!0),u=(null==m?void 0:m.findOne(".element"))||e.target.findAncestor(".element",!0),h=t.getElementById(null==u?void 0:u.id());h&&"pendingEdit"in h&&h._setImageEditError(null);const g=null==h?void 0:h.top;let f;if("group"===O){f=null==g?void 0:g.id}else if(g){const e=t.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===g.id});f=e?null==h?void 0:h.id:null==g?void 0:g.id}else{f=void 0}const p=null==h?void 0:h.id;if(p&&t.selectedElementsIds.indexOf(p)>=0&&!n){return}const x=!!f&&t.selectedElementsIds.indexOf(f)>=0,v=e.target.findAncestor(".page-container",!0);if(f&&n&&!x){const e=se(t.selectedElementsIds.concat([f]),e=>le(t,e));t.selectElements(e),t.selectPages([])}else{f&&n&&x?t.selectElements(t.selectedElementsIds.filter(e=>e!==f)):!f||n||x?f&&!n&&x||(v?(n||t.selectElements([]),t.selectPages([l.id])):t.selectPages([])):(t.selectElements([f]),t.selectPages([]))}};I(We,t);const ut=t.activePage===l,ht=t._selectedPagesIds.includes(l.id),gt=null==y?void 0:y.PageControls,ft=null==y?void 0:y.Tooltip,pt=null==y?void 0:y.ContextMenu,xt=1/t.scale,vt=0/t.scale;return i("div",{ref:Fe,onDragOver:e=>e.preventDefault(),onDrop:e=>{if(e.preventDefault(),t.role===Y.VIEWER){return}if(!Ve.current){return}Ve.current.setPointersPositions(e);const n=Ve.current.findOne(".elements-container"),i=null==n?void 0:n.getRelativePointerPosition(),o=Ve.current.getPointerPosition(),r=Ve.current.getAllIntersections(o).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(r.reverse())].map(e=>t.getElementById(e.id())).filter(e=>!!e),a=s[0];Oe&&i&&(Oe(i,a,{elements:s,page:l}),Oe=null)},style:{position:"relative",width:d+"px",height:h+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+(ut?" active-page":""),children:[n(de,{ref:Ve,eventBatchFunc:c,width:Math.min(d,P.width+200),height:Math.min(P.height+200,h),onClick:mt,onTap:mt,onContextMenu:e=>{if(e.evt.preventDefault(),t.role===Y.VIEWER){return}const n=e.target.findAncestor(".element",!0),i=t.getElementById(null==n?void 0:n.id()),o=null==i?void 0:i.top,r=null==o?void 0:o.id;if(!r){return t.selectElements([]),void Ke.close()}t.selectedElementsIds.indexOf(r)>=0||t.selectElements([r]),Ke.open({x:e.evt.clientX,y:e.evt.clientY})},onMouseDown:e=>{rt(e),lt(e)},onTouchStart:rt,onMouseMove:e=>{if(!t.distanceGuidesVisible&&!e.evt.altKey){return void(Ue&&Ze(null))}const n=e.target.findAncestor(".element",!0),i=null==n?void 0:n.id();st(i)},onDragStart:e=>{var n;const i=e.target.getStage();if(i&&i.getPointersPositions().length>=2){return void e.target.stopDrag()}const o=e.target.findAncestor(".element",!0);if(o){const i=t.getElementById(null==o?void 0:o.id()),r=null==i?void 0:i.top,l=null==r?void 0:r.id;if(!(i&&t.selectedElementsIds.indexOf(i.id)>=0||l&&t.selectedElementsIds.indexOf(l)>=0)&&l){t.selectElements([l]);const i=qe();e.target.stopDrag(),i.forEach(t=>{t.isDragging()||t.startDrag(e)}),null===(n=We.current)||void 0===n||n.startDrag(e),M()}}Ue&&Ze(null)},onDragMove:e=>{const n=We.current;n&&e.target===n&&n.nodes().forEach(e=>{const n=t.getElementById(e.id());!n||n.x===e.x()&&n.y===e.y()||n.set({x:e.x(),y:e.y()})})},pageId:l.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:i(f,{children:[n(be,{width:d,height:h,fill:N.workspaceBackgroundColor}),n(u,{x:Pe,y:Re,scaleX:t.scale,scaleY:t.scale,name:"page-container",children:i(u,{name:"page-container-2",children:[n(u,{x:we.left,y:we.top,children:n(Ee,Object.assign({},Be,Ce,{transparencyBackgroundColor:N.transparencyBackgroundColor}))}),i(u,{isolated:l.hasBlendMode,children:[n(u,{name:"page-background-group",x:we.left,y:we.top,clip:Ce,children:n(Se,Object.assign({},Be))}),i(u,{x:we.left,y:we.top,name:"elements-container",listening:!t.isPlaying&&t.role!==Y.VIEWER,children:[n(x,{name:"elements-area",width:l.computedWidth,height:l.computedHeight,listening:!1}),n(Ie,{elements:l.children,store:t}),n(Me,{page:l})]})]}),n(v,{name:"bleed",fill:N.bleedColor,listening:!1,visible:t.bleedVisible&&(we.top>0||we.right>0||we.bottom>0||we.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:i,bottom:o,left:r}=ye,s=l.computedWidth,a=l.computedHeight,c=e._context;c.beginPath(),c.rect(0,0,r+s+i,n+a+o),c.rect(r,n,s,a),c.fillStyle=t.fill(),c.fill("evenodd")}}),C()===Te&&n(De,{name:"hit-detection",x:-xt/2-vt,y:-xt/2-vt,width:Ye+xt+2*vt,height:je+xt+2*vt})]})}),n(p,{name:"workspace-background",points:[0,0,d,0,d,h,0,h,0,0,Pe,Re,Pe,h-Re,d-Pe,h-Re,d-Pe,Re,Pe,Re],listening:!1,closed:!0,fill:N.workspaceBackgroundColor}),n(u,{x:Pe,y:Re,scaleX:t.scale,scaleY:t.scale,children:n(x,{name:"page-highlight",hideInExport:!0,x:-xt/2-vt,y:-xt/2-vt,width:Ye+xt+2*vt,height:je+xt+2*vt,stroke:ht?N.activePageBorderColor:N.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),i(u,{x:Pe+we.left*t.scale,y:Re+we.top*t.scale,scaleX:t.scale,scaleY:t.scale,name:"page-abs-container",children:[n(S,{ref:We,name:J,rotateAnchorAngle:Je,onDragStart:e=>{var n;const i=e.target.getStage();i&&i.getPointersPositions().length>=2?e.target.stopDrag():((null===(n=e.evt)||void 0===n?void 0:n.altKey)&&A&&B(t.selectedElements,l,t),_e.current||(_e.current=!0,t.history.startTransaction()))},onDragEnd:()=>{_e.current&&(t.history.endTransaction(),_e.current=!1),nt()},onTransformStart:()=>{He.setTransforming(!0),t.history.startTransaction(),Le.current=new Map($e.map(e=>[e.id,{width:e.width,height:e.height}])),Ze(null)},boundBoxFunc:(e,n)=>{var i,o,r,l;const s=null!==(l=null===(i=te.boundBoxFunc)||void 0===i?void 0:i.call(te,e,n))&&void 0!==l?l:n,a=Math.abs(s.width)<1||Math.abs(s.height)<1,c=Math.abs(e.width)<1||Math.abs(e.height)<1;if(a&&!c){return e}const d=t.selectedElements;if(1===d.length&&"table"===(null===(o=d[0])||void 0===o?void 0:o.type)){const t=d[0],n=Math.abs(e.width),i=Math.abs(e.height);if(n>0&&i>0){const o=Math.abs(s.width)/n,r=Math.abs(s.height)/i,l=t.width*o,a=t.height*r,c=Object.assign({},s);return l<t.minWidth&&(c.width=e.width),a<t.minHeight&&(c.height=e.height),Q(c,s,e)}}return Qe&&$e.length>0?((e,t,n,i,o)=>{const r=Math.abs(e.width),l=Math.abs(e.height);if(0===r||0===l){return t}const s=Math.abs(t.width)/r,a=Math.abs(t.height)/l,c=!Z(n),d=e=>i.some(t=>{var n;const i=null==o?void 0:o.get(t.id),r=null!==(n="x"===e?null==i?void 0:i.width:null==i?void 0:i.height)&&void 0!==n?n:"x"===e?t.width:t.height,l="x"===e?t.width:t.height,d="x"===e?s:a,m=$(t,e,c,r);return r>=m&&l*d<m}),m=Object.assign({},t),u=t.width*e.width<0,h=t.height*e.height<0,g=u||d("x"),f=h||d("y"),p=f||!c&&g;return(g||!c&&f)&&(m.width=e.width),p&&(m.height=e.height),Q(m,t,e)})(e,s,(null===(r=We.current)||void 0===r?void 0:r.getActiveAnchor())||"",$e,Le.current):s},onTransform:e=>{const t=We.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{tt()},0)},onTransformEnd:e=>{He.setTransforming(!1),He.setLabel({}),t.history.endTransaction(),Le.current=null,nt()},visible:!t.isPlaying}),n(Ae,{store:t,gesture:He,transformerStyle:te,onChange:et}),Ue&&Ue.map(({points:e,distance:o,box1:r,box2:l},s)=>i(u,{name:"distances-container",hideInExport:!0,listening:!1,children:[n(x,Object.assign({},r,{stroke:N.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),n(x,Object.assign({},l,{stroke:N.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),n(p,{points:[e[0].x,e[0].y,e[1].x,e[1].y],stroke:N.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(p,{points:[e[1].x,e[1].y,e[2].x,e[2].y],stroke:N.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),i(g,{x:(e[0].x+e[1].x)/2,y:(e[0].y+e[1].y)/2,offsetY:-10,scaleX:1/t.scale,scaleY:1/t.scale,children:[n(w,{cornerRadius:5,fill:N.distanceGuideStyle.labelFill,pointerDirection:"down"}),n(E,{align:"center",verticalAlign:"middle",fill:N.distanceGuideStyle.labelTextFill,padding:5,text:U({unit:t.unit,dpi:t.dpi,px:o})})]})]},s)),l._rendering&&i(u,{children:[n(x,{width:Ye,height:je,fill:"rgba(255,255,255,0.9)"}),n(E,{text:"Rendering...",fontSize:60,width:Ye,height:je,align:"center",verticalAlign:"middle"})]}),ft&&n(ft,{components:y,store:t,page:l,stageRef:Ve,tooltipSafeArea:_}),pt&&n(re,{children:n(pt,Object.assign({components:y,store:t},Ke.props))}),n(R,{store:t,pageId:l.id}),n(T,{store:t,pageId:l.id})]}),n(j,{store:t,page:l,width:d,height:h,scale:t.scale,xPadding:Pe,yPadding:Re,bleedLeft:we.left,bleedTop:we.top}),n(ke,{gesture:He,store:t}),n(Xe,{selection:it}),W()>0&&i(u,{hideInExport:!0,children:[n(Ge,{name:"cache-bounds",x:d-(1===W()?250:210),y:h-(1===W()?95:45)}),n(Ge,{name:"cache-bounds-t",x:10,y:10})]}),G()&&n(E,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:d-170,y:h-18,onMouseEnter:e=>{e.target.getStage().container().style.cursor="pointer"},onMouseLeave:e=>{e.target.getStage().container().style.cursor=""},onTouchStart:e=>{e.cancelBubble=!0},onMouseDown:e=>{e.cancelBubble=!0},onClick:()=>{window.open("https://polotno.com","_blank","noopener")},onTap:()=>{window.open("https://polotno.com","_blank","noopener")}}),n(u,{name:"line-guides"})]})}),ze&>&&n(gt,{store:t,page:l,xPadding:Pe,yPadding:Re,layout:D})]})});
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export declare const VARIABLE_TOKEN_FORMAT = "variable-token";
|
|
1
2
|
export declare const PRESERVED_INLINE_STYLES: readonly ['text-shadow', 'letter-spacing', 'word-spacing', '-webkit-text-stroke', 'font-variant', 'vertical-align', 'text-decoration-color', 'text-decoration-style', 'text-decoration-thickness', 'text-underline-offset', 'text-decoration-skip-ink'];
|
|
2
3
|
export declare const PRESERVED_BLOCK_STYLES: readonly ['text-align', 'line-height', 'margin-top', 'margin-bottom', 'text-indent', 'list-style-type'];
|
|
3
4
|
export declare function registerQuillUnrestrictedFontFormats(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"quill";import{firstStopColor as e}from"@polotno/core/gradient";import{parsePrimaryFontFamily as r}from"@polotno/core/rich-text-html";import{cssFontFamily as o}from"@polotno/core/fonts";import{containsCmykColor as i,isCmykColor as n,toDisplayColor as s}from"@polotno/core/color";import{isClipGradientSpan as l,nativeColorAttribute as c,readNativeColor as a}from"../utils/rich-text-html.js";let
|
|
1
|
+
import t from"quill";import{firstStopColor as e}from"@polotno/core/gradient";import{parsePrimaryFontFamily as r}from"@polotno/core/rich-text-html";import{cssFontFamily as o}from"@polotno/core/fonts";import{containsCmykColor as i,isCmykColor as n,toDisplayColor as s}from"@polotno/core/color";import{isClipGradientSpan as l,nativeColorAttribute as c,readNativeColor as a,VARIABLE_TOKEN_CLASS as u}from"../utils/rich-text-html.js";let p=!1;export const VARIABLE_TOKEN_FORMAT="variable-token";export const PRESERVED_INLINE_STYLES=["text-shadow","letter-spacing","word-spacing","-webkit-text-stroke","font-variant","vertical-align","text-decoration-color","text-decoration-style","text-decoration-thickness","text-underline-offset","text-decoration-skip-ink"];export const PRESERVED_BLOCK_STYLES=["text-align","line-height","margin-top","margin-bottom","text-indent","list-style-type"];function m(t){const e=t.keyName,r=c(e),o=t.add.bind(t),i=t.remove.bind(t),l=t.value.bind(t);t.add=(t,e)=>n(e)?!!o(t,s(e))&&(t.setAttribute(r,e),!0):(t.removeAttribute(r),o(t,e)),t.remove=t=>{t.removeAttribute(r),i(t)},t.value=t=>a(t,e)||l(t)}const d=["background-image","background-size","-webkit-background-clip","background-clip","-webkit-text-fill-color","color"],g=c("background-image");export function registerQuillUnrestrictedFontFormats(){if(p){return}p=!0;const n=t.import("attributors/style/size"),c=t.import("attributors/style/font");n&&(delete n.whitelist,t.register(n,!0)),c&&(delete c.whitelist,c.add=(t,e)=>(t.style.fontFamily=o(e),!0),c.value=t=>r(t.style.fontFamily)||"",t.register(c,!0));const b=t.import("parchment");if((null==b?void 0:b.StyleAttributor)&&(null==b?void 0:b.Scope)){const r=new b.StyleAttributor("font-weight","font-weight",{scope:b.Scope.INLINE});t.register(r,!0),t.register(new b.StyleAttributor("text-transform","text-transform",{scope:b.Scope.INLINE}),!0);const o=(e,r)=>t.register(new b.StyleAttributor(e,e,{scope:r}),!0);PRESERVED_INLINE_STYLES.forEach(t=>o(t,b.Scope.INLINE)),PRESERVED_BLOCK_STYLES.forEach(t=>o(t,b.Scope.BLOCK));const n=t.import("blots/inline");class c extends n{static formats(){return!0}}c.blotName="variable-token",c.className=u,t.register(c,!0),t.register(function(t){class r extends t.StyleAttributor{constructor(t,e,r){super(t,e,r)}add(t,r){if("string"!=typeof r||!r.includes("gradient")){return!1}t.style.setProperty("background-image",s(r)),i(r)?t.setAttribute(g,r):t.removeAttribute(g),t.style.setProperty("background-size","100% 100%"),t.style.setProperty("-webkit-background-clip","text"),t.style.setProperty("background-clip","text"),t.style.setProperty("-webkit-text-fill-color","transparent");try{t.style.setProperty("color",e(r))}catch(o){}return!0}remove(t){t.removeAttribute(g);for(const e of d){t.style.removeProperty(e)}}value(t){return l(t)?a(t,"background-image")||t.style.getPropertyValue("background-image"):""}}return new r("gradient","background-image",{scope:t.Scope.INLINE})}(b),!0);const p=t.import("attributors/style/color");if(p){m(p);const e=p.value.bind(p);p.value=t=>l(t)?"":e(t),t.register(p,!0)}const y=t.import("attributors/style/background");y&&(m(y),t.register(y,!0))}}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type StoreType } from '../model/store.js';
|
|
3
|
+
export declare const RadialGradientHandle: (({ store, pageId }: {
|
|
4
|
+
store: StoreType;
|
|
5
|
+
pageId: string;
|
|
6
|
+
}) => React.JSX.Element | null) & React.NamedExoticComponent<{
|
|
7
|
+
store: StoreType;
|
|
8
|
+
pageId: string;
|
|
9
|
+
}> & {
|
|
10
|
+
readonly type: ({ store, pageId }: {
|
|
11
|
+
store: StoreType;
|
|
12
|
+
pageId: string;
|
|
13
|
+
}) => React.JSX.Element | null;
|
|
14
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{jsx as t}from"react/jsx-runtime";import{observer as e}from"mobx-react-lite";import{Circle as o,Group as r}from"react-konva";import{isAlive as n}from"mobx-state-tree";import{ROLES as i}from"../model/store.js";import{beginRadialHandlePress as a,getRadialHandle as l}from"../utils/gradient-handle.js";import{readGradient as s,writeGradient as d}from"../utils/gradient-stops.js";import{isRadialGradient as c}from"@polotno/core/gradient";import{backgroundPaintBox as g}from"@polotno/core/page";import{safe01 as m}from"../utils/stops.js";import{useHistoryGesture as u}from"../utils/history-gesture.js";import{useWorkspaceStyle as p}from"./workspace-style.js";import{t as f}from"../utils/l10n.js";function h(t){var e;const o=null===(e=t.getStage())||void 0===e?void 0:e.container();o&&(o.style.cursor="",o.title="")}export const RadialGradientHandle=e(({store:e,pageId:y})=>{var b,v,x;const j=l(e),{transformerStyle:w}=p(),S=u(e);if(!j||!n(j.node)||e.role===i.VIEWER||e.isPlaying){return null}const k=e.selectedElements,B=e.activePage;let D,E;if(1===k.length&&k[0]===j.node){const t=j.node,{x:e,y:o,rotation:r,width:n,height:i}=t.a;if((null===(b=t.page)||void 0===b?void 0:b.id)!==y||"group"===(null===(v=t.parent)||void 0===v?void 0:v.type)||!t.visible||!(n>0&&i>0)){return null}const a=j.border?(t.borderSize||0)/2:0;E={x:a,y:a,width:n,height:i},D={x:e,y:o,rotation:r}}else{if(0!==k.length||!B||B!==j.node||B.id!==y){return null}E=g(B.computedBleed,B.computedWidth,B.computedHeight),D={x:0,y:0,rotation:0}}const P=j.read();if(!P||!c(P)){return null}const{center:W}=s(P),M=e.scale||1,O=t=>({x:E.x+t.x*E.width,y:E.y+t.y*E.height}),C=t=>({x:m((t.x()-E.x)/E.width),y:m((t.y()-E.y)/E.height)}),H=t=>{const o=l(e),r=null==o?void 0:o.read();o&&r&&o.onChange(d(Object.assign(Object.assign({},s(r)),{type:"radial",center:t})))};return t(r,{x:D.x,y:D.y,rotation:D.rotation,children:t(o,Object.assign({name:"radial-gradient-handle"},O(W),{radius:6/M,fill:"#ffffff",stroke:String(null!==(x=w.anchorStroke)&&void 0!==x?x:"#0d83cd"),strokeWidth:2/M,hitStrokeWidth:12/M,shadowColor:"rgba(0,0,0,0.4)",shadowBlur:2/M,draggable:!0,dragDistance:0,onPointerDown:a,onMouseEnter:t=>{const e=t.target.getStage().container();e.style.cursor="move",e.title=f("toolbar.colorPicker.center")},onMouseLeave:t=>{S.active()||h(t.target)},onDragStart:t=>{t.cancelBubble=!0,S.begin()},onDragMove:t=>{t.cancelBubble=!0;const e=C(t.target);t.target.position(O(e)),H(e)},onDragEnd:t=>{t.cancelBubble=!0,H(C(t.target)),S.end(),h(t.target)}}))})});
|
|
@@ -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 i=0;for(r=Object.getOwnPropertySymbols(t);i<r.length;i++){e.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(t,r[i])&&(o[r[i]]=t[r[i]])}}return o};import{Fragment as e,jsx as o,jsxs as r}from"react/jsx-runtime";import{elementOpacity as i}from"./paint-opacity.js";import n from"react";import{observer as s}from"mobx-react-lite";import{Group as a,Path as l,Rect as c,Shape as d}from"react-konva";import{nodeAutorun as u}from"../utils/mobx.js";import{isAlive as h}from"mobx-state-tree";import{Html as f}from"react-konva-utils";import{drawLayout as g}from"render-tag";import{drawTextOnPathLayout as m}from"render-tag/path";import{activeAnchorOf as b,isCornerAnchor as p,isRotateAnchor as x,scaledFontSizeAttrs as v,scaledFontSizeBounds as y,roundedFontSizeAttrs as w,roundedTextDecorationAttrs as j,scaledStyleAttrs as O,roundedGeometryPx as z,roundedPosition as E,textMinResizeWidth as S,transformScale as M}from"../utils/px.js";import{flags as k}from"../utils/flags.js";import{editorTextSizing as R,textOverflowOf as Y,textResizeFloorFontSize as C}from"../utils/text-sizing.js";import{curvedBoxWidth as X,layoutAsSized as P,layoutCurvedTextSizing as F,layoutTextSizing as I,renderTagLayoutAt as T}from"@polotno/core/text-sizing";import{curveLayoutAt as B}from"@polotno/core/curve-glyphs";import{applyFilter as A}from"./apply-filters.js";import{getLineHeight as H}from"./text-element.js";import{liveFitScale as $,useRenderTagSource as D}from"./text-element/render-tag-source.js";import{getHtml as L,getCurveTextHeight as _,letterSpacingOverhang as J}from"@polotno/core/text-html";import{withDisplayTextPaints as N}from"./use-color.js";import{toDisplayColor as U}from"@polotno/core/color";import{animatedShadow as V}from"./animated-shadow.js";import{detectSize as q}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as W,trimTrailingLetterSpace as G}from"@polotno/core/background-shape";import{useFadeIn as K}from"./use-fadein.js";import{incrementLoader as Q}from"../utils/loader.js";import{removeTags as Z}from"@polotno/core/text";import{shadowCastByBackground as tt}from"@polotno/core/filters";import{applyOpacityToColor as et}from"@polotno/core/svg";import{isTouchDevice as ot}from"../utils/screen.js";import{getLimitedFontSize as rt}from"./text-element/max-font-size.js";import{scaleRichTextFontSizesInHtml as it}from"../utils/rich-text-html.js";import{RichEditor as nt}from"./rich-editor.js";import{PlacedNode as st}from"./placed-node.js";import{AutoDeleteEmptyText as at}from"./text-element/auto-delete-empty-text.js";import{wrapHtmlForRenderTag as lt}from"@polotno/core/reset-style";import{hasMeasuredHeight as ct,resolveTextBoxHeight as dt}from"@polotno/core/text-types";import{TEXT_PRESETS as ut,WIPE_DIRECTION as ht}from"@polotno/core/animation-presets";import{textUnitState as ft}from"../utils/animations.js";export{lt as wrapHtmlForRenderTag};function gt(t,e){return t!==e&&(!k.textVerticalResizeEnabled||t<e)}const mt=({opacity:t,y:e,scale:o,blur:r,from:i,to:n,shift:s})=>t>=1&&!e&&1===o&&!r&&i<=0&&n>=1&&!s;export const RenderTagElement=s(({element:s,store:lt})=>{var bt,pt,xt,vt,yt,wt,jt,Ot,zt,Et,St;const Mt=n.useRef(null),kt=n.useRef(null),[Rt,Yt]=n.useState(!1),Ct=n.useRef(s.height),Xt=n.useRef(null),Pt=ot(),{textVerticalResizeEnabled:Ft}=k,{fontLoaded:It,fontFamily:Tt,color:Bt,isPlaceholder:At,normalizedSourceText:Ht,getHtmlOptions:$t}=D(s,lt),Dt=s._editModeEnabled;K(Mt);const Lt=At?.6:i(s),_t=L(N(s),$t),Jt=s.width||1,Nt=(t,e)=>T(N(t),{getHtmlOptions:$t,textUnits:e}),Ut=Y(s),Vt=R(s),qt=s.curveEnabled,Wt=JSON.stringify(Vt),Gt="fixed"===Vt.box?s.height:0,Kt=Rt?Xt.current:null,Qt=n.useMemo(()=>{if(Kt){return Kt.sized}const t=s.filled.text,e=At?Object.assign(Object.assign({},s),{text:Ht}):void 0!==t&&t!==s.text?Object.assign(Object.assign({},s),{text:t}):s;try{return qt?Object.assign({curved:!0},F({element:e,sizing:Vt,layoutAt:B(N(s),{getHtmlOptions:{fontFamily:Tt,color:Bt,text:Ht,textIsNormalized:!0}})})):Object.assign({curved:!1},I({element:e,sizing:Vt,layoutAt:Nt(s)}))}catch(o){return console.error("render-tag layout failed:",o),null}},[_t,Jt,It,qt,Wt,Gt,s.curvePower,s.fontSize,s.width,Kt]),Zt=null!==(bt=null==Kt?void 0:Kt.width)&&void 0!==bt?bt:Jt,te=Kt?s.width/Kt.width:1,ee=!1===(null==Qt?void 0:Qt.curved)?Qt.layout:null,oe=(null==Qt?void 0:Qt.curved)?Qt.layout:null,re=s.a.text,ie=s.a.textUnits,ne=ie?ut[ie.animation.name].unit:void 0,se=n.useMemo(()=>{var t;const e="string"==typeof re&&re!==(null!==(t=s.filled.text)&&void 0!==t?t:s.text);if(!1!==(null==Qt?void 0:Qt.curved)||At||!e&&!ne){return ee}try{return P({element:e?Object.assign(Object.assign({},s),{text:re}):s,sized:Qt,sizing:Vt,layoutAt:Nt(s,ne)})}catch(o){return console.error("render-tag layout failed:",o),ee}},[ee,re,ne]),ae=ie&&(null==se?void 0:se.textUnits),le=n.useMemo(()=>{if(!ie||!ae){return null}const t=ae.map((t,e)=>ft(ie,e,ae.length)),e=e=>!t[e]||mt(t[e]);return t.map((t,o)=>({state:t,slice:mt(t)&&e(o-1)&&e(o+1)}))},[ie,ae]),ce=null!==(pt=null==Qt?void 0:Qt.fontSize)&&void 0!==pt?pt:s.fontSize,de=null!==(xt=null==Qt?void 0:Qt.scale)&&void 0!==xt?xt:1,ue=J(s.letterSpacing,ce),he=null!==(vt=null==Kt?void 0:Kt.overhang)&&void 0!==vt?vt:ue,fe=H({fontLoaded:It,fontFamily:s.fontFamily,fontSize:ce,lineHeight:s.lineHeight}),ge=qt?0:Math.max(null!==(yt=null==ee?void 0:ee.height)&&void 0!==yt?yt:0,ce*fe),me=oe?oe.bounds:null,be=me?(s.width-me.width)/2-me.x:0,pe=n.useMemo(()=>{var t;if(oe&&me){const t=oe.paintBounds;return Object.assign(Object.assign({},t),{x:t.x+be,y:t.y-me.y})}const e=null!==(t=null==se?void 0:se.paintBounds)&&void 0!==t?t:null;return e&&ae?{x:e.x-ce,y:e.y-ce,width:e.width+2*ce,height:e.height+2*ce}:e},[se,oe,me,be,ae,ce]),xe=n.useMemo(()=>se?se.lines.map(t=>G({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height},he)):[],[se,he]),ve=n.useRef([]),ye=n.useRef(null),we=n.useRef(!0),[,je]=n.useReducer(t=>t+1,0);n.useEffect(()=>{const t=ve.current;t.length>0&&(ve.current=[],t.forEach(t=>t()))});const Oe=n.useCallback((t,e,o,r)=>{const i=Q(`text ${s.id}`),n=()=>{lt.history.ignore(t,e,o).then(()=>{we.current?(ve.current.push(i),je()):i()})};r?queueMicrotask(n):n()},[s.id,lt]),ze=n.useCallback(t=>{const e=null!==ye.current;ye.current=t,e||Oe(()=>{const t=ye.current;ye.current=null,h(s)&>(s.height,t)&&s.set({height:t})},!1,!0)},[s,Oe]),Ee=n.useRef(Qt);Ee.current=Qt,n.useEffect(()=>{if(!It){return}if(!ee){return}if(!ct(s)){return void Oe(()=>{h(s)&&(ct(s)||s.set({height:ge}))},void 0,void 0,!0)}if(lt.isPlaying){return}const t=gt(s.height,ge),e=()=>ze(ge);if(void 0===s.filled.text||"change-font-size"!==Ut){if("change-font-size"!==Ut||Rt||Dt||s.curveEnabled){"resize"===Ut&&t&&!Rt&&e()}else if(Qt&&Qt.fontSize!==s.fontSize){const{fontSize:t,text:e}=Qt;Oe(()=>{if(!h(s)){return}if(Ee.current!==Qt||"change-font-size"!==Y(s)||s._editModeEnabled){return}const o=v(s,t);At||e===s.text||(o.text=e),s.set(o)},!1,!0)}else{t&&e()}}}),n.useEffect(()=>()=>{we.current=!1;const t=ve.current;t.length>0&&(ve.current=[],t.forEach(t=>t()))},[]),n.useLayoutEffect(()=>{if(!It||!s.curveEnabled||Rt){return}if(!me||!me.width){return}const t=t=>t&&Ft?Math.max(t,me.height):me.height,e=t=>X(Vt,{width:t},me.width);Math.abs(e(s.width)-s.width)<1&&Math.abs(t(s.height)-s.height)<1||Oe(()=>{if(!h(s)){return}const o=e(s.width),r=o-s.width,i=t(s.height),n=i-s.height;if(Math.abs(r)<1&&Math.abs(n)<1){return}const a=s.a.rotation*Math.PI/180,l=-r/2*Math.cos(a),c=-r/2*Math.sin(a);s.set({width:o,height:i,x:s.a.x+l,y:s.a.y+c})},!1,!0,!0)},[It,s.curveEnabled,Ut,me,Rt,Ft,Oe]),n.useEffect(()=>u(s,()=>{const t=Mt.current;t&&A(t,s)}),[se,oe,s.a.width,s.a.height,ie,s.a.shadowColor,s.a.shadowOffsetX,s.a.shadowOffsetY,s.a.shadowOpacity]);const Se=dt(s,ge),Me=qt||!It||Rt||lt.isPlaying||"resize"!==Ut?s.a.height:Ft?Math.max(s.a.height,ge):ge;let ke=0;qt||"middle"!==s.verticalAlign?qt||"bottom"!==s.verticalAlign||(ke=Se-ge*te):ke=(Se-ge*te)/2;const Re=null!==(wt=null==Kt?void 0:Kt.backgroundPadding)&&void 0!==wt?wt:s.backgroundPadding*(ce*fe*.5),Ye=null!==(jt=null==Kt?void 0:Kt.backgroundCornerRadius)&&void 0!==jt?jt:s.backgroundCornerRadius*(ce*fe*.5),Ce=s.backgroundEnabled||s.borderSize>0,Xe=n.useMemo(()=>!Ce||s.curveEnabled||s.legacyBackground||0===xe.length?"":W({rects:xe,padding:Re,cornerRadius:Ye}),[Ce,s.curveEnabled,s.legacyBackground,xe,Re,Ye]),Pe=n.useRef(null),Fe=n.useRef(s.width),Ie=n.useRef(null),Te=n.useRef(null),Be=n.useRef(""),Ae=n.useRef(0),He=n.useRef(0),$e=n.useRef({fontSize:s.fontSize}),De=s.width>0?s.a.width/s.width:1,Le=De*te,_e=V(s),Je=null!==(Ot=null==Kt?void 0:Kt.shadow)&&void 0!==Ot?Ot:{shadowBlur:_e.shadowBlur*de,shadowOffsetX:_e.shadowOffsetX*de,shadowOffsetY:_e.shadowOffsetY*de},Ne=Object.assign(Object.assign({},Je),{shadowColor:_e.shadowColor,shadowOpacity:_e.shadowOpacity}),Ue=Object.assign(Object.assign({},Ne),{shadowEnabled:_e.shadowEnabled&&Ce});return r(e,{children:[o(at,{element:s,store:lt}),Ce?s.curveEnabled||s.legacyBackground?o(st,Object.assign({as:c,element:s,offsetX:Re/De,offsetY:Re/De,scaleX:Le,scaleY:Le,hideInExport:!s.showInExport,listening:!1,opacity:Lt,fillEnabled:s.backgroundEnabled,fill:et(s.backgroundColor,s.backgroundOpacity),strokeEnabled:s.borderSize>0,stroke:U(s.borderColor),strokeWidth:null!==(zt=null==Kt?void 0:Kt.borderSize)&&void 0!==zt?zt:s.borderSize},Ue,{width:Zt+2*Re,height:(null!==(Et=null==Kt?void 0:Kt.boxHeight)&&void 0!==Et?Et:Se)+2*Re,cornerRadius:Ye})):o(st,Object.assign({as:l,element:s,name:"rendertag-background",offsetY:-ke/Le,scaleX:Le,scaleY:Le,hideInExport:!s.showInExport,listening:!1,visible:Xe.length>0,opacity:Lt,fillEnabled:s.backgroundEnabled,fill:et(s.backgroundColor,s.backgroundOpacity),strokeEnabled:s.borderSize>0,stroke:U(s.borderColor),strokeWidth:null!==(St=null==Kt?void 0:Kt.borderSize)&&void 0!==St?St:s.borderSize,lineJoin:Ye>0?"round":"miter"},Ue,{data:Xe})):null,o(st,Object.assign({as:d,nodeRef:Mt,element:s,selfRectFunc:pe?()=>pe:void 0,name:"rendertag-text",scaleX:Le,scaleY:Le,offsetY:Le>0?-ke/Le:0,width:Zt,height:Math.max(s.height,ge),listening:!1,visible:!Dt&&!!(qt?oe:ee),editModeEnabled:Dt,opacity:Lt,shadowEnabled:_e.shadowEnabled&&!tt(s)},Ne,{hideInExport:!s.showInExport,sceneFunc:(t,e)=>{const o=t._context;if(o){try{if(qt){if(!oe||!me){return}o.save(),o.translate(be,-me.y),m({layout:oe,ctx:o}),o.restore()}else{if(!se){return}g({layout:se,width:Zt,ctx:o,pixelRatio:1,paintTextUnit:le?t=>{const{state:e,slice:o}=le[t.unit.index];(({unit:t,ctx:e,draw:o},{opacity:r,y:i,scale:n,blur:s,from:a,to:l,direction:c,shift:d},u,h)=>{if(!(r<=0||n<=0||l<=a)){if(a>0||l<1||d){const{vertical:o,low:r,high:i,forward:n}=((t,e,o)=>{var r;const i=e?ht[e]:void 0,n="y"===(null==i?void 0:i.axis);let s=n?t.y:t.x,a=s+(n?t.height:t.width);if(n||o){for(const l of t.fragments){s=Math.min(s,n?l.paintTop:l.paintLeft),a=Math.max(a,n?l.paintBottom:l.paintRight)}}return{vertical:n,low:s,high:a,forward:i?i.forward:"rtl"!==(null===(r=t.fragments[0])||void 0===r?void 0:r.direction)}})(t,c,0!==d),s=i-r,[h,f]=n?[a,l]:[1-l,1-a],g=h<=0&&!d?r-u:r+h*s,m=f>=1&&!d?i+u:r+f*s;e.beginPath(),o?e.rect(t.x-u,g,t.width+2*u,m-g):e.rect(g,t.y-u,m-g,t.height+2*u),e.clip();const b=d*s*(n?1:-1);o?e.translate(0,b):e.translate(b,0)}if(e.globalAlpha*=r,i&&e.translate(0,i*u),1!==n){const o=t.x+t.width/2,r=t.y+t.height/2;e.translate(o,r),e.scale(n,n),e.translate(-o,-r)}if(s>0){const t=e.getTransform(),o=Math.sqrt(Math.abs(t.a*t.d-t.b*t.c));e.filter=`blur(${s*u*o}px)`}o({slice:h})}})(t,e,ce,o)}:void 0})}}catch(r){console.error("render-tag draw failed:",r)}}}})),o(st,{as:c,nodeRef:kt,element:s,name:"element",listening:s.selectable,width:s.a.width,height:Me,visible:!Dt,draggable:Pt?s.draggable&&s.isSelected:s.draggable,preventDefault:!Pt||s.isSelected,opacity:0,hideInExport:!0,id:s.id,onDragMove:t=>{s.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{s.set(E(t.target))},onDblClick:t=>{s.contentEditable&&(Te.current={x:t.evt.clientX,y:t.evt.clientY},s.toggleEditMode(!0))},onDblTap:t=>{var e;if(s.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Te.current=o?{x:o.clientX,y:o.clientY}:null,s.toggleEditMode(!0)}},onTransformStart:t=>{Ct.current=s.height,Fe.current=s.width,Ie.current=null,Pe.current=t.target.position(),Be.current=b(t.target),Ae.current=s.a.rotation,He.current=s.height,$e.current={fontSize:s.fontSize,minFontSize:s.minFontSize,maxFontSize:s.maxFontSize},Xt.current=!s.curveEnabled&&["top-left","top-right","bottom-left","bottom-right"].includes(Be.current)?{sized:Qt,width:Jt,boxHeight:Se,backgroundPadding:Re,backgroundCornerRadius:Ye,borderSize:s.borderSize,overhang:ue,shadow:Je}:null,Yt(!0)},onTransform:e=>{const o=e.target,r=Be.current,i="middle-left"===r||"middle-right"===r,n="top-center"===r||"bottom-center"===r,a=M(o.scaleX());if(i){const e=o.width()*a;let r=Math.max(e,S(Fe.current,C(s)));const i=s.toJSON();let n=0;if("fit"===Ut){n=Ct.current}else if(s.curveEnabled){n=_(Object.assign(Object.assign({},i),{width:r}))}else{const{x:e,y:o,width:s,height:a}=i,l=t(i,["x","y","width","height"]),c=`${JSON.stringify(l)}|${Tt}|${It}|${Bt}`;if(Ie.current&&Ie.current.signature===c&&r<=Ie.current.width){r=Ie.current.width,n=Ie.current.height}else{const t=t=>{const e=Object.assign(Object.assign({},i),{width:t});try{return I({element:Object.assign(Object.assign({},e),{text:Ht}),sizing:Object.assign(Object.assign({},Vt),{fit:{mode:"off"},cutAtBox:!1}),layoutAt:Nt(e)}).layout}catch(bt){const o=L(e,$t);return{height:q(o).height,lines:[]}}};let e=t(r);const o=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width-ue));o>r+.5&&o<=Fe.current+.5&&(r=Math.min(Math.ceil(o),Fe.current),e=t(r),Ie.current={signature:c,width:r,height:e.height}),n=e.height}}r>e&&Pe.current&&o.position(Pe.current),o.width(r),o.scaleX(1),o.scaleY(1);let l=k.textVerticalResizeEnabled?Math.max(n,Ct.current):n;"ellipsis"!==Ut&&"fit"!==Ut||(l=Ct.current),o.height(l),s.set({width:o.width(),height:l,x:o.x(),y:o.y()})}else if(n){const t="resize"===Ut?qt?(null==me?void 0:me.height)||s.fontSize:ge:fe*("fit"===Vt.fit.mode?Vt.fit.min:s.fontSize),e=o.height()*o.scaleY();if(e<t){if(o.height(t),Pe.current){if("top-center"===Be.current){const e=Ae.current*Math.PI/180,r=He.current-t;o.x(Pe.current.x-r*Math.sin(e)),o.y(Pe.current.y+r*Math.cos(e))}else{o.position(Pe.current)}}}else{o.height(e)}o.scaleY(1),o.scaleX(1),He.current=Math.max(t,e),s.set({x:o.x(),y:o.y(),height:o.height(),rotation:o.rotation()})}else{o.scaleX(1),o.scaleY(1);const t=s.fontSize*a;s.set(Object.assign(Object.assign(Object.assign({},y($e.current,t/$e.current.fontSize)),{letterSpacing:s.letterSpacing,width:o.width()*a,x:o.x(),y:o.y(),rotation:o.rotation(),height:o.height()*a}),O(s,a)))}Pe.current=e.target.position()},onTransformEnd:t=>{Xt.current=null,Yt(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1);const o=w(s,s.fontSize*e),r=$e.current,i=Be.current;s.set(Object.assign(Object.assign(Object.assign({},o),p(i)&&!x(i)?y(r,o.fontSize/r.fontSize,!0):{}),{width:z(t.target.width()*e),x:z(t.target.x()),y:z(t.target.y()),rotation:z(t.target.rotation()),text:it(s.text,e)}))}}),Dt&&o(st,{as:a,element:s,offsetY:-ke,children:o(f,{divProps:{style:{zIndex:20}},children:o(nt,{element:s,fitScale:de,fitScaleFor:t=>qt?de:$(s,Vt,$t,t,de),onChange:t=>{if("fit"===Ut){return void s.set({text:t})}const e=rt({oldText:Z(s.text),newText:Z(t),element:s});s.set(Object.assign({text:t},v(s,e)))},onBlur:()=>{h(s)&&(s.set(j(s)),s.toggleEditMode(!1))},clickCoords:Te.current})})})]})});
|
|
1
|
+
var t=this&&this.__rest||function(t,e){var o={};for(var i in t){Object.prototype.hasOwnProperty.call(t,i)&&e.indexOf(i)<0&&(o[i]=t[i])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(i=Object.getOwnPropertySymbols(t);r<i.length;r++){e.indexOf(i[r])<0&&Object.prototype.propertyIsEnumerable.call(t,i[r])&&(o[i[r]]=t[i[r]])}}return o};import{Fragment as e,jsx as o,jsxs as i}from"react/jsx-runtime";import{elementOpacity as r}from"./paint-opacity.js";import n from"react";import{observer as s}from"mobx-react-lite";import{Group as a,Path as l,Rect as c,Shape as d}from"react-konva";import{nodeAutorun as u}from"../utils/mobx.js";import{isAlive as h}from"mobx-state-tree";import{Html as f}from"react-konva-utils";import{drawLayout as g}from"render-tag";import{drawTextOnPathLayout as m}from"render-tag/path";import{activeAnchorOf as p,isCornerAnchor as b,isRotateAnchor as x,scaledFontSizeAttrs as v,scaledFontSizeBounds as y,roundedFontSizeAttrs as w,roundedTextDecorationAttrs as j,scaledStyleAttrs as O,roundedGeometryPx as z,roundedPosition as E,paddedTextMinResizeWidth as S,roundedTextPaddingAttrs as M,transformScale as R}from"../utils/px.js";import{flags as k}from"../utils/flags.js";import{editorTextSizing as X,textOverflowOf as Y,textResizeFloorFontSize as C}from"../utils/text-sizing.js";import{curvedBoxWidth as P,curveOrigin as T,layoutAsSized as F,layoutCurvedTextSizing as I,layoutTextSizing as A,renderTagLayoutAt as B}from"@polotno/core/text-sizing";import{curveLayoutAt as H}from"@polotno/core/curve-glyphs";import{applyFilter as L}from"./apply-filters.js";import{getLineHeight as $}from"./text-element.js";import{liveFitScale as D,useRenderTagSource as _}from"./text-element/render-tag-source.js";import{getHtml as J,getCurveTextHeight as N,letterSpacingOverhang as U}from"@polotno/core/text-html";import{withDisplayTextPaints as V}from"./use-color.js";import{toDisplayColor as q}from"@polotno/core/color";import{animatedShadow as W}from"./animated-shadow.js";import{detectSize as G}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as K,trimTrailingLetterSpace as Q}from"@polotno/core/background-shape";import{useFadeIn as Z}from"./use-fadein.js";import{incrementLoader as tt}from"../utils/loader.js";import{removeTags as et}from"@polotno/core/text";import{shadowCastByBackground as ot}from"@polotno/core/filters";import{applyOpacityToColor as it}from"@polotno/core/svg";import{isTouchDevice as rt}from"../utils/screen.js";import{getLimitedFontSize as nt}from"./text-element/max-font-size.js";import{scaleRichTextFontSizesInHtml as st}from"../utils/rich-text-html.js";import{RichEditor as at}from"./rich-editor.js";import{PlacedNode as lt}from"./placed-node.js";import{AutoDeleteEmptyText as ct}from"./text-element/auto-delete-empty-text.js";import{wrapHtmlForRenderTag as dt}from"@polotno/core/reset-style";import{hasMeasuredHeight as ut,resolveTextBoxHeight as ht,textBackgroundRadius as ft,textBackgroundSpread as gt,textContentElement as mt,textOuterHeight as pt,textPadding as bt}from"@polotno/core/text-types";import{TEXT_PRESETS as xt,WIPE_DIRECTION as vt}from"@polotno/core/animation-presets";import{textUnitState as yt}from"../utils/animations.js";export{dt as wrapHtmlForRenderTag};function wt(t,e){return t!==e&&(!k.textVerticalResizeEnabled||t<e)}const jt=({opacity:t,y:e,scale:o,blur:i,from:r,to:n,shift:s})=>t>=1&&!e&&1===o&&!i&&r<=0&&n>=1&&!s;export const RenderTagElement=s(({element:s,store:dt})=>{var Ot,zt,Et,St,Mt,Rt,kt,Xt,Yt,Ct,Pt;const Tt=n.useRef(null),Ft=n.useRef(null),[It,At]=n.useState(!1),Bt=n.useRef(s.height),Ht=n.useRef(null),Lt=rt(),{textVerticalResizeEnabled:$t}=k,{fontLoaded:Dt,fontFamily:_t,color:Jt,isPlaceholder:Nt,normalizedSourceText:Ut,getHtmlOptions:Vt}=_(s,dt),qt=s._editModeEnabled;Z(Tt);const Wt=Nt?.6:r(s),Gt=J(V(s),Vt),Kt=s.width||1,Qt=(t,e)=>B(V(t),{getHtmlOptions:Vt,textUnits:e}),Zt=Y(s),te=X(s),ee=s.curveEnabled,oe=bt(s),ie=JSON.stringify(te),re="fixed"===te.box?s.height:0,ne=It?Ht.current:null,se=n.useMemo(()=>{if(ne){return ne.sized}const t=s.filled.text,e=Nt?Object.assign(Object.assign({},s),{text:Ut}):void 0!==t&&t!==s.text?Object.assign(Object.assign({},s),{text:t}):s;try{return ee?Object.assign({curved:!0},I({element:e,sizing:te,layoutAt:H(V(s),{getHtmlOptions:{fontFamily:_t,color:Jt,text:Ut,textIsNormalized:!0}})})):Object.assign({curved:!1},A({element:mt(e),sizing:te,layoutAt:Qt(mt(s))}))}catch(o){return console.error("render-tag layout failed:",o),null}},[Gt,Kt,Dt,ee,ie,re,s.curvePower,s.fontSize,s.width,s.paddingTop,s.paddingRight,s.paddingBottom,s.paddingLeft,ne]),ae=null!==(Ot=null==ne?void 0:ne.width)&&void 0!==Ot?Ot:Kt,le=ne?s.width/ne.width:1,ce=!1===(null==se?void 0:se.curved)?se.layout:null,de=(null==se?void 0:se.curved)?se.layout:null,ue=s.a.text,he=s.a.textUnits,fe=he?xt[he.animation.name].unit:void 0,ge=n.useMemo(()=>{var t;const e="string"==typeof ue&&ue!==(null!==(t=s.filled.text)&&void 0!==t?t:s.text);if(!1!==(null==se?void 0:se.curved)||Nt||!e&&!fe){return ce}try{const t=mt(s);return F({element:e?Object.assign(Object.assign({},t),{text:ue}):t,sized:se,sizing:te,layoutAt:Qt(t,fe)})}catch(o){return console.error("render-tag layout failed:",o),ce}},[ce,ue,fe]),me=he&&(null==ge?void 0:ge.textUnits),pe=n.useMemo(()=>{if(!he||!me){return null}const t=me.map((t,e)=>yt(he,e,me.length)),e=e=>!t[e]||jt(t[e]);return t.map((t,o)=>({state:t,slice:jt(t)&&e(o-1)&&e(o+1)}))},[he,me]),be=null!==(zt=null==se?void 0:se.fontSize)&&void 0!==zt?zt:s.fontSize,xe=null!==(Et=null==se?void 0:se.scale)&&void 0!==Et?Et:1,ve=U(s.letterSpacing,be),ye=null!==(St=null==ne?void 0:ne.overhang)&&void 0!==St?St:ve,we=$({fontLoaded:Dt,fontFamily:s.fontFamily,fontSize:be,lineHeight:s.lineHeight}),je=ee?0:Math.max(null!==(Mt=null==ce?void 0:ce.height)&&void 0!==Mt?Mt:0,be*we),Oe=ee?je:pt(s,je),ze=de?de.bounds:null,{x:Ee,y:Se}=ze?T(s.width,ze):{x:0,y:0},Me=n.useMemo(()=>{var t;if(de&&ze){const t=de.paintBounds;return Object.assign(Object.assign({},t),{x:t.x+Ee,y:t.y+Se})}const e=null!==(t=null==ge?void 0:ge.paintBounds)&&void 0!==t?t:null;return e&&me?{x:e.x-be,y:e.y-be,width:e.width+2*be,height:e.height+2*be}:e},[ge,de,ze,Ee,Se,me,be]),Re=n.useMemo(()=>ge?ge.lines.map(t=>Q({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height},ye)):[],[ge,ye]),ke=n.useRef([]),Xe=n.useRef(null),Ye=n.useRef(!0),[,Ce]=n.useReducer(t=>t+1,0);n.useEffect(()=>{const t=ke.current;t.length>0&&(ke.current=[],t.forEach(t=>t()))});const Pe=n.useCallback((t,e,o,i)=>{const r=tt(`text ${s.id}`),n=()=>{dt.history.ignore(t,e,o).then(()=>{Ye.current?(ke.current.push(r),Ce()):r()})};i?queueMicrotask(n):n()},[s.id,dt]),Te=n.useCallback(t=>{const e=null!==Xe.current;Xe.current=t,e||Pe(()=>{const t=Xe.current;Xe.current=null,h(s)&&wt(s.height,t)&&s.set({height:t})},!1,!0)},[s,Pe]),Fe=n.useRef(se);Fe.current=se,n.useEffect(()=>{if(!Dt){return}if(!ce){return}if(!ut(s)){return void Pe(()=>{h(s)&&(ut(s)||s.set({height:Oe}))},void 0,void 0,!0)}if(dt.isPlaying){return}const t=wt(s.height,Oe),e=()=>Te(Oe);if(void 0===s.filled.text||"change-font-size"!==Zt){if("change-font-size"!==Zt||It||qt||s.curveEnabled){"resize"===Zt&&t&&!It&&e()}else if(se&&se.fontSize!==s.fontSize){const{fontSize:t,text:e}=se;Pe(()=>{if(!h(s)){return}if(Fe.current!==se||"change-font-size"!==Y(s)||s._editModeEnabled){return}const o=v(s,t);Nt||e===s.text||(o.text=e),s.set(o)},!1,!0)}else{t&&e()}}}),n.useEffect(()=>()=>{Ye.current=!1;const t=ke.current;t.length>0&&(ke.current=[],t.forEach(t=>t()))},[]),n.useLayoutEffect(()=>{if(!Dt||!s.curveEnabled||It){return}if(!ze||!ze.width){return}const t=t=>t&&$t?Math.max(t,ze.height):ze.height,e=t=>P(te,{width:t},ze.width);Math.abs(e(s.width)-s.width)<1&&Math.abs(t(s.height)-s.height)<1||Pe(()=>{if(!h(s)){return}const o=e(s.width),i=o-s.width,r=t(s.height),n=r-s.height;if(Math.abs(i)<1&&Math.abs(n)<1){return}const a=s.a.rotation*Math.PI/180,l=-i/2*Math.cos(a),c=-i/2*Math.sin(a);s.set({width:o,height:r,x:s.a.x+l,y:s.a.y+c})},!1,!0,!0)},[Dt,s.curveEnabled,Zt,ze,It,$t,Pe]),n.useEffect(()=>u(s,()=>{const t=Tt.current;t&&L(t,s)}),[ge,de,s.a.width,s.a.height,he,s.a.shadowColor,s.a.shadowOffsetX,s.a.shadowOffsetY,s.a.shadowOpacity]);const Ie=ht(s,Oe),Ae=ee||!Dt||It||dt.isPlaying||"resize"!==Zt?s.a.height:$t?Math.max(s.a.height,Oe):Oe,Be=ht(mt(s),je);let He=0;ee||"middle"!==s.verticalAlign?ee||"bottom"!==s.verticalAlign||(He=Be-je*le):He=(Be-je*le)/2;const Le=null!==(Rt=null==ne?void 0:ne.backgroundPadding)&&void 0!==Rt?Rt:gt(s,we),$e=null!==(kt=null==ne?void 0:ne.backgroundCornerRadius)&&void 0!==kt?kt:ft(s,we),De=s.backgroundEnabled||s.borderSize>0,_e="box"===s.backgroundShape,Je=n.useMemo(()=>!De||s.curveEnabled||_e||0===Re.length?"":K({rects:Re,padding:Le,cornerRadius:$e}),[De,s.curveEnabled,_e,Re,Le,$e]),Ne=n.useRef(null),Ue=n.useRef(s.width),Ve=n.useRef(null),qe=n.useRef(null),We=n.useRef(""),Ge=n.useRef(0),Ke=n.useRef(0),Qe=n.useRef({fontSize:s.fontSize}),Ze=s.width>0?s.a.width/s.width:1,to=Ze*le,eo=oe.left&&le>0?oe.left/le:0,oo=oe.top&&le>0?oe.top/le:0,io=W(s),ro=null!==(Xt=null==ne?void 0:ne.shadow)&&void 0!==Xt?Xt:{shadowBlur:io.shadowBlur*xe,shadowOffsetX:io.shadowOffsetX*xe,shadowOffsetY:io.shadowOffsetY*xe},no=Object.assign(Object.assign({},ro),{shadowColor:io.shadowColor,shadowOpacity:io.shadowOpacity}),so=Object.assign(Object.assign({},no),{shadowEnabled:io.shadowEnabled&&De});return i(e,{children:[o(ct,{element:s,store:dt}),De?s.curveEnabled||_e?o(lt,Object.assign({as:c,element:s,offsetX:Le/Ze,offsetY:Le/Ze,scaleX:to,scaleY:to,hideInExport:!s.showInExport,listening:!1,opacity:Wt,fillEnabled:s.backgroundEnabled,fill:it(s.backgroundColor,s.backgroundOpacity),strokeEnabled:s.borderSize>0,stroke:q(s.borderColor),strokeWidth:null!==(Yt=null==ne?void 0:ne.borderSize)&&void 0!==Yt?Yt:s.borderSize},so,{width:ae+2*Le,height:(null!==(Ct=null==ne?void 0:ne.boxHeight)&&void 0!==Ct?Ct:Ie)+2*Le,cornerRadius:$e})):o(lt,Object.assign({as:l,element:s,name:"rendertag-background",offsetX:eo?-eo:void 0,offsetY:-(oo+He/to),scaleX:to,scaleY:to,hideInExport:!s.showInExport,listening:!1,visible:Je.length>0,opacity:Wt,fillEnabled:s.backgroundEnabled,fill:it(s.backgroundColor,s.backgroundOpacity),strokeEnabled:s.borderSize>0,stroke:q(s.borderColor),strokeWidth:null!==(Pt=null==ne?void 0:ne.borderSize)&&void 0!==Pt?Pt:s.borderSize,lineJoin:$e>0?"round":"miter"},so,{data:Je})):null,o(lt,Object.assign({as:d,nodeRef:Tt,element:s,selfRectFunc:Me?()=>Me:void 0,name:"rendertag-text",scaleX:to,scaleY:to,offsetX:eo&&to>0?-eo:void 0,offsetY:to>0?-(oo+He/to):0,width:ae,height:Math.max(s.height,je),listening:!1,visible:!qt&&!!(ee?de:ce),editModeEnabled:qt,opacity:Wt,shadowEnabled:io.shadowEnabled&&!ot(s)},no,{hideInExport:!s.showInExport,sceneFunc:(t,e)=>{const o=t._context;if(o){try{if(ee){if(!de||!ze){return}o.save(),o.translate(Ee,Se),m({layout:de,ctx:o}),o.restore()}else{if(!ge){return}g({layout:ge,width:ae,ctx:o,pixelRatio:1,paintTextUnit:pe?t=>{const{state:e,slice:o}=pe[t.unit.index];(({unit:t,ctx:e,draw:o},{opacity:i,y:r,scale:n,blur:s,from:a,to:l,direction:c,shift:d},u,h)=>{if(!(i<=0||n<=0||l<=a)){if(a>0||l<1||d){const{vertical:o,low:i,high:r,forward:n}=((t,e,o)=>{var i;const r=e?vt[e]:void 0,n="y"===(null==r?void 0:r.axis);let s=n?t.y:t.x,a=s+(n?t.height:t.width);if(n||o){for(const l of t.fragments){s=Math.min(s,n?l.paintTop:l.paintLeft),a=Math.max(a,n?l.paintBottom:l.paintRight)}}return{vertical:n,low:s,high:a,forward:r?r.forward:"rtl"!==(null===(i=t.fragments[0])||void 0===i?void 0:i.direction)}})(t,c,0!==d),s=r-i,[h,f]=n?[a,l]:[1-l,1-a],g=h<=0&&!d?i-u:i+h*s,m=f>=1&&!d?r+u:i+f*s;e.beginPath(),o?e.rect(t.x-u,g,t.width+2*u,m-g):e.rect(g,t.y-u,m-g,t.height+2*u),e.clip();const p=d*s*(n?1:-1);o?e.translate(0,p):e.translate(p,0)}if(e.globalAlpha*=i,r&&e.translate(0,r*u),1!==n){const o=t.x+t.width/2,i=t.y+t.height/2;e.translate(o,i),e.scale(n,n),e.translate(-o,-i)}if(s>0){const t=e.getTransform(),o=Math.sqrt(Math.abs(t.a*t.d-t.b*t.c));e.filter=`blur(${s*u*o}px)`}o({slice:h})}})(t,e,be,o)}:void 0})}}catch(i){console.error("render-tag draw failed:",i)}}}})),o(lt,{as:c,nodeRef:Ft,element:s,name:"element",listening:s.selectable,width:s.a.width,height:Ae,visible:!qt,draggable:Lt?s.draggable&&s.isSelected:s.draggable,preventDefault:!Lt||s.isSelected,opacity:0,hideInExport:!0,id:s.id,onDragMove:t=>{s.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{s.set(E(t.target))},onDblClick:t=>{s.contentEditable&&(qe.current={x:t.evt.clientX,y:t.evt.clientY},s.toggleEditMode(!0))},onDblTap:t=>{var e;if(s.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];qe.current=o?{x:o.clientX,y:o.clientY}:null,s.toggleEditMode(!0)}},onTransformStart:t=>{Bt.current=s.height,Ue.current=s.width,Ve.current=null,Ne.current=t.target.position(),We.current=p(t.target),Ge.current=s.a.rotation,Ke.current=s.height,Qe.current={fontSize:s.fontSize,minFontSize:s.minFontSize,maxFontSize:s.maxFontSize},Ht.current=!s.curveEnabled&&["top-left","top-right","bottom-left","bottom-right"].includes(We.current)?{sized:se,width:Kt,boxHeight:Ie,backgroundPadding:Le,backgroundCornerRadius:$e,borderSize:s.borderSize,overhang:ve,shadow:ro}:null,At(!0)},onTransform:e=>{const o=e.target,i=We.current,r="middle-left"===i||"middle-right"===i,n="top-center"===i||"bottom-center"===i,a=R(o.scaleX());if(r){const e=o.width()*a;let i=Math.max(e,S(ee?{}:s,Ue.current,C(s)));const r=s.toJSON();let n=0;if("fit"===Zt){n=Bt.current}else if(s.curveEnabled){n=N(Object.assign(Object.assign({},r),{width:i}))}else{const{x:e,y:o,width:s,height:a}=r,l=t(r,["x","y","width","height"]),c=`${JSON.stringify(l)}|${_t}|${Dt}|${Jt}`;if(Ve.current&&Ve.current.signature===c&&i<=Ve.current.width){i=Ve.current.width,n=Ve.current.height}else{const t=t=>{const e=mt(Object.assign(Object.assign({},r),{width:t}));try{return A({element:Object.assign(Object.assign({},e),{text:Ut}),sizing:Object.assign(Object.assign({},te),{fit:{mode:"off"},cutAtBox:!1}),layoutAt:Qt(e)}).layout}catch(Ot){const o=J(e,Vt);return{height:G(o).height,lines:[]}}};let e=t(i);const o=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width-ve)),s=oe.left+oe.right;o+s>i+.5&&o+s<=Ue.current+.5&&(i=Math.min(Math.ceil(o)+s,Ue.current),e=t(i),Ve.current={signature:c,width:i,height:e.height}),n=e.height}n+=oe.top+oe.bottom}i>e&&Ne.current&&o.position(Ne.current),o.width(i),o.scaleX(1),o.scaleY(1);let l=k.textVerticalResizeEnabled?Math.max(n,Bt.current):n;"ellipsis"!==Zt&&"fit"!==Zt||(l=Bt.current),o.height(l),s.set({width:o.width(),height:l,x:o.x(),y:o.y()})}else if(n){const t="resize"===Zt?ee?(null==ze?void 0:ze.height)||s.fontSize:Oe:we*("fit"===te.fit.mode?te.fit.min:s.fontSize)+oe.top+oe.bottom,e=o.height()*o.scaleY();if(e<t){if(o.height(t),Ne.current){if("top-center"===We.current){const e=Ge.current*Math.PI/180,i=Ke.current-t;o.x(Ne.current.x-i*Math.sin(e)),o.y(Ne.current.y+i*Math.cos(e))}else{o.position(Ne.current)}}}else{o.height(e)}o.scaleY(1),o.scaleX(1),Ke.current=Math.max(t,e),s.set({x:o.x(),y:o.y(),height:o.height(),rotation:o.rotation()})}else{o.scaleX(1),o.scaleY(1);const t=s.fontSize*a;s.set(Object.assign(Object.assign(Object.assign({},y(Qe.current,t/Qe.current.fontSize)),{letterSpacing:s.letterSpacing,width:o.width()*a,x:o.x(),y:o.y(),rotation:o.rotation(),height:o.height()*a}),O(s,a)))}Ne.current=e.target.position()},onTransformEnd:t=>{Ht.current=null,At(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1);const o=w(s,s.fontSize*e),i=Qe.current,r=We.current;s.set(Object.assign(Object.assign(Object.assign({},o),b(r)&&!x(r)?Object.assign(Object.assign({},y(i,o.fontSize/i.fontSize,!0)),M(s)):{}),{width:z(t.target.width()*e),x:z(t.target.x()),y:z(t.target.y()),rotation:z(t.target.rotation()),text:st(s.text,e)}))}}),qt&&o(lt,{as:a,element:s,offsetX:oe.left?-oe.left:void 0,offsetY:-(oe.top+He),children:o(f,{divProps:{style:{zIndex:20}},children:o(at,{element:s,fitScale:xe,fitScaleFor:t=>ee?xe:D(mt(s),te,Vt,t,xe),onChange:t=>{if("fit"===Zt){return void s.set({text:t})}const e=nt({oldText:et(s.text),newText:et(t),element:s});s.set(Object.assign({text:t},v(s,e)))},onBlur:()=>{h(s)&&(s.set(j(s)),s.toggleEditMode(!1))},clickCoords:qe.current})})})]})});
|
package/canvas/rich-editor.d.ts
CHANGED
|
@@ -35,8 +35,10 @@ export type MarkerFont = {
|
|
|
35
35
|
export declare function computeMarkerVars(element: MarkerFont): MarkerVars | null;
|
|
36
36
|
export declare const RichTextContainer: any;
|
|
37
37
|
export declare const setQuillFormats: (formats: string[]) => void;
|
|
38
|
+
type FormatShortcut = 'bold' | 'italic' | 'underline';
|
|
38
39
|
export declare const createQuill: (node: any, options?: {
|
|
39
40
|
placeholder?: string;
|
|
41
|
+
onFormatShortcut?: (toggle: FormatShortcut) => void;
|
|
40
42
|
}) => Quill;
|
|
41
43
|
export declare const setQuillContent: (quill: any, html: any) => void;
|
|
42
44
|
export type RichEditorElement = TextElementType | TableCellType;
|
package/canvas/rich-editor.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(o[n]=t[n])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(t);r<n.length;r++){e.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(t,n[r])&&(o[n[r]]=t[n[r]])}}return o};import{jsx as e}from"react/jsx-runtime";import o from"react";import n from"quill";import*as r from"mobx";import{nodeReaction as i}from"../utils/mobx.js";import{isAlive as l}from"mobx-state-tree";import{geckoRtlOverhangCss as s,getDir as a,getMeasureCtx as c,getQuillEditorHtml as d,letterSpacingOverhang as u,OVERHANG_PROPERTY as f,textLayoutWidth as p}from"@polotno/core/text-html";import{quillHtmlToModelText as
|
|
1
|
+
var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(o[n]=t[n])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(t);r<n.length;r++){e.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(t,n[r])&&(o[n[r]]=t[n[r]])}}return o};import{jsx as e}from"react/jsx-runtime";import o from"react";import n from"quill";import*as r from"mobx";import{nodeReaction as i}from"../utils/mobx.js";import{isAlive as l}from"mobx-state-tree";import{geckoRtlOverhangCss as s,getDir as a,getMeasureCtx as c,getQuillEditorHtml as d,letterSpacingOverhang as u,OVERHANG_PROPERTY as f,textLayoutWidth as p}from"@polotno/core/text-html";import{textContentBox as m}from"@polotno/core/text-types";import{quillHtmlToModelText as h,isQuillFormatMixed as g,isBoldWeight as b,detachNativeColorsInHtml as v,VARIABLE_TOKEN_CLASS as y}from"../utils/rich-text-html.js";import{variableTokens as x}from"@polotno/schema/variables";import{toDisplayColor as w}from"@polotno/core/color";import{LIST_INDENT_EM as k}from"@polotno/core/css-values";import{resetStyleContent as S,linkStyle as T,wrapHtmlForRenderTag as E}from"@polotno/core/reset-style";import{layout as q,lineBaselineOffset as C}from"render-tag";import{removeTags as O}from"@polotno/core/text";import{cssFontFamily as L}from"@polotno/core/fonts";import{isGradient as R,firstStopColor as F}from"@polotno/core/gradient";import{getOptimalCaretColor as $}from"./text-element/caret-color.js";import{visiblePlaceholder as N}from"./text-element/placeholder.js";import{registerQuillUnrestrictedFontFormats as j,PRESERVED_INLINE_STYLES as z,PRESERVED_BLOCK_STYLES as M,VARIABLE_TOKEN_FORMAT as D}from"./quill-font-formats.js";import{ALL_QUILL_MIXED_KEYS as P,setActiveTextEditor as A,updateActiveTextFormat as H,clearActiveTextEditor as I,getActiveTextEditor as Q,getTextFormatState as _}from"../utils/text-format-state.js";import{formatToggle as W}from"../utils/text-format.js";import B from"../utils/styled.js";import{applyOpacityToColor as V}from"@polotno/core/svg";import{elementDropShadow as X,shadowCastByBackground as G}from"@polotno/core/filters";export{V as applyOpacityToColor};export const quillRef=r.observable({enabled:!1,currentFormat:{},pendingToolbarInteraction:!1,editor:r.observable.object({instance:null},{},{deep:!1})});const Y=P;export function syncQuillFormatState(t,e){const o=e?t.getFormat(e):{},n=function(t,e){const o={};if(!e||e.length<=0){return o}const n=t.getContents(e.index,e.length);for(const r of Y){g(n,r)&&(o[r]=!0)}return o}(t,e);r.runInAction(()=>{quillRef.currentFormat=o}),H(o,n,e)}const U="polotno-saved-selection";function K(){const t=window.getSelection();if(!t||0===t.rangeCount||t.isCollapsed){return}if("undefined"==typeof Highlight||!CSS.highlights){return}const e=t.getRangeAt(0).cloneRange();CSS.highlights.set(U,new Highlight(e)),Z()}function J(){"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.delete(U)}function Z(){setTimeout(()=>{"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.has(U)&&(document.querySelector(".polotno-portal")||pt(document.activeElement)?Z():J())},300)}if("undefined"!=typeof document&&"undefined"!=typeof CSS&&CSS.highlights&&!document.querySelector("[data-polotno-highlight]")){const mt=document.createElement("style");mt.setAttribute("data-polotno-highlight",""),mt.textContent=`::highlight(${U}) { background-color: Highlight; }`,document.head.appendChild(mt)}export function collectRTText(t,e){"text"===t.type?e.push(t):t.children.forEach(t=>collectRTText(t,e))}export function computeMarkerVars(t){var e,o,n;const r=t.fontSize;if(!r||r<=0){return null}const i=L(t.fontFamily),l=`font-family:${i};font-size:${r}px;font-weight:${b(t.fontWeight)?700:400};font-style:${"italic"===t.fontStyle?"italic":"normal"};line-height:${t.lineHeight};letter-spacing:${t.letterSpacing}em`,s=E(`<div style="${l}"><ul><li>x</li></ul><ol><li>x</li></ol></div>`);let a;try{a=q({html:s,width:Math.max(12*r,600)})}catch(x){return null}const d=[];collectRTText(a.layoutRoot,d);const u=d.find(t=>"•"===t.text),f=d.find(t=>/^\d/.test(t.text));if(!u){return null}const p=u.style.fontSize,m=u.x,h=c();h.fontKerning="normal",h.font=`${p}px ${i}`;const g=h.measureText("M"),v=u.y-C(p,null!==(e=g.fontBoundingBoxAscent)&&void 0!==e?e:0,null!==(o=g.fontBoundingBoxDescent)&&void 0!==o?o:0),y=k*r;return{bulletLeft:m,bulletTop:v,bulletFontSize:p,numberGap:f?Math.max(0,y-f.x-(null!==(n=f.width)&&void 0!==n?n:0)):0}}export const RichTextContainer=B("div",o.forwardRef)`
|
|
2
2
|
.ql-editor {
|
|
3
3
|
outline: none;
|
|
4
4
|
/* anchor for the absolutely-positioned ::before placeholder below */
|
|
@@ -22,8 +22,8 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
|
|
|
22
22
|
pointer-events: none;
|
|
23
23
|
opacity: 0;
|
|
24
24
|
}
|
|
25
|
-
${
|
|
26
|
-
${
|
|
25
|
+
${S}
|
|
26
|
+
${T}
|
|
27
27
|
|
|
28
28
|
/* ===== Editor-only list markers (Quill flat-DOM hack) ======================
|
|
29
29
|
Quill emits a FLAT <ol> with per-item data-list + ql-indent classes (not
|
|
@@ -60,7 +60,7 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
|
|
|
60
60
|
}
|
|
61
61
|
.ql-editor li {
|
|
62
62
|
position: relative; /* anchor for the absolutely-positioned ::before */
|
|
63
|
-
padding-inline-start: ${
|
|
63
|
+
padding-inline-start: ${k}em; /* marker space + per-level indent (start side) */
|
|
64
64
|
}
|
|
65
65
|
.ql-editor li[data-list='ordered'] {
|
|
66
66
|
counter-increment: list-0;
|
|
@@ -87,7 +87,7 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
|
|
|
87
87
|
inset-inline-start: 0;
|
|
88
88
|
top: 0;
|
|
89
89
|
box-sizing: border-box;
|
|
90
|
-
width: ${
|
|
90
|
+
width: ${k}em;
|
|
91
91
|
text-align: end;
|
|
92
92
|
}
|
|
93
93
|
.ql-editor li[data-list='ordered']::before {
|
|
@@ -128,6 +128,12 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
|
|
|
128
128
|
text-align: left;
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
+
/* A variable token shows as stored (markVariableTokens), whatever case the
|
|
132
|
+
element or a span asks for: its name is case-sensitive. */
|
|
133
|
+
.ql-editor .${y}, .ql-editor .${y} * {
|
|
134
|
+
text-transform: none !important;
|
|
135
|
+
}
|
|
136
|
+
|
|
131
137
|
strong {
|
|
132
138
|
font-weight: 700;
|
|
133
139
|
}
|
|
@@ -164,4 +170,4 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
|
|
|
164
170
|
.ql-editor [style*='background-clip'] {
|
|
165
171
|
-webkit-text-stroke-width: 0;
|
|
166
172
|
}
|
|
167
|
-
`;const G=[...$,...N,"header"];let Y=["link","background","font-weight","color","gradient","font","italic","size","script","strike","underline","text-transform","indent","list","direction",...G];export const setQuillFormats=t=>{Y=t};let U=!1;function K(){U||(U=!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 J(t){return t?"700":"400"}const Z=n.import("delta"),tt="data-polotno-rich-text",et=["text-decoration-color","text-decoration-style","text-decoration-thickness"],ot=(t,e)=>{const{style:o}=t,n=o.textDecorationLine,r={};if(n.includes("underline")&&(r.underline=!0),n.includes("line-through")&&(r.strike=!0),!r.underline&&!r.strike){return e}for(const i of et){const t=o.getPropertyValue(i);t&&!/^(initial|currentcolor|solid|auto)$/i.test(t)&&(r[i]=t)}return e.ops.reduce((t,e)=>void 0===e.insert?t:t.insert(e.insert,Object.assign(Object.assign({},r),e.attributes)),new Z)};export const createQuill=(e,o)=>{F();const r=new n(e,{toolbar:!1,keyboard:!1,placeholder:(null==o?void 0:o.placeholder)||void 0,clipboard:{matchVisual:!1},formats:Y});r.root.addEventListener("click",t=>{t.target instanceof Element&&t.target.closest("a")&&t.preventDefault()}),function(e){const o=e.format.bind(e),n=e.formatText.bind(e),r=e.getFormat.bind(e);e.format=(t,e,n)=>"bold"===t?(K(),o("font-weight",J(e),n)):o(t,e,n),e.formatText=(e,o,r,i,l)=>{if(r&&"object"==typeof r){if("bold"in r){K();const{bold:l}=r,s=t(r,["bold"]);return n(e,o,Object.assign(Object.assign({},s),{"font-weight":J(l)}),i)}return n(e,o,r,i)}return"bold"===r?(K(),n(e,o,"font-weight",J(i),l)):n(e,o,r,i,l)},e.getFormat=(...t)=>{const e=r(...t);return e&&null!=e["font-weight"]&&!("bold"in e)&&(e.bold=g(e["font-weight"])),e}}(r);const i=(t,e)=>e.compose((new Z).retain(e.length(),{"font-weight":"700"}));r.clipboard.addMatcher("STRONG",i),r.clipboard.addMatcher("B",i),r.clipboard.addMatcher(Node.ELEMENT_NODE,ot);const l=r.clipboard,s=l.onPaste.bind(l);return l.onPaste=(t,e)=>{var o;if(null===(o=e.html)||void 0===o?void 0:o.includes(tt)){return it(r,()=>s(t,e))}const n=l.convert;l.convert=(...t)=>((t,e)=>{for(const o of t.ops){if(o.attributes){for(const t of e){delete o.attributes[t]}}}return t})(n.apply(l,t),G);try{return s(t,e)}finally{l.convert=n}},r};const nt=(t,e)=>e.insert(t.data),rt=(t,e)=>{const o=e.ops[0];return parseFloat(t.style.textIndent)>0&&"string"==typeof(null==o?void 0:o.insert)&&o.insert.startsWith("\t")?e.compose((new Z).delete(1)):e};function it(t,e){const{matchers:o}=t.clipboard,n=o.findIndex(([t])=>t===Node.TEXT_NODE),r=o[n];-1!==n&&(o[n]=[Node.TEXT_NODE,nt]),o.push([Node.ELEMENT_NODE,rt]);try{return e()}finally{o.pop(),-1!==n&&(o[n]=r)}}export const setQuillContent=(t,e)=>{const o=it(t,()=>t.clipboard.convert({html:"<div class='ql-editor' style='outline: none;'>"+e+"<p><br></p></div>",text:""}));t.setContents(o),t.history.clear()};export const editorHtml=t=>d({text:b(t.text),width:t.width,fontSize:t.fontSize,fontFamily:t.fontFamily},t.fontFamily);export const RichEditor=({onBlur:t,onChange:n,element:s,clickCoords:c,initialCaret:d="start",fitScale:h=1,fitScaleFor:g})=>{const b=o.useRef(null),y=o.useRef(!1),x=o.useRef(h);x.current=h;const[w,S]=o.useState(null),k=null!=w?w:h,T=o.useRef(g);T.current=g;const F=t=>({width:p(s.a.width/t,s.letterSpacing,s.fontSize),zoom:1!==t?t:void 0}),$=o.useRef(null),N=o.useRef(void 0),z=()=>`${s.text}\0${s.fontSize}\0${s.fontFamily}`,M=t=>m(t,{fontSize:s.fontSize,fontFamily:s.fontFamily}),A=t=>{setQuillContent(t,editorHtml(s)),$.current=M(t.root.innerHTML),N.current=z()},H=o.useRef(()=>{}),_=()=>{H.current(),t()};o.useLayoutEffect(()=>{let t,e,o=!1,i=null,a=null,u=null,f=null,p=null;const m=()=>{if(!o&&b.current){try{t=createQuill(b.current,{placeholder:R(s.placeholder)}),r.runInAction(()=>{quillRef.editor.instance=t}),quillRef.pendingToolbarInteraction=!1,window.__polotnoQuill=t,j({instance:t,store:s.store,element:s});const o=e=>{e&&syncQuillFormatState(t,e)},m=()=>{let e=t.root;if(e.querySelector(".ql-cursor")){e=e.cloneNode(!0);const t=e.querySelector(".ql-cursor");t.replaceWith(t.textContent.replace(/\uFEFF/g,""))}return M(e.innerHTML)},h=()=>{const t=T.current;t&&l(s)&&(t=>{const e=b.current;if(!e){return}const o=F(t);e.style.width=`${o.width}px`,e.style.zoom=void 0===o.zoom?"":String(o.zoom)})(t(m()))},g=()=>{var o,r;if(clearTimeout(e),e=void 0,!l(s)){return}const i=m();if(i===$.current){return}$.current=i,y.current=!0,n(i);const a=t.getContents(),c=s.fill;let d=null,u=!0;for(const t of a.ops){if("string"!=typeof t.insert||"\n"===t.insert){continue}if(null===(o=t.attributes)||void 0===o?void 0:o.gradient){u=!1;break}const e=(null===(r=t.attributes)||void 0===r?void 0:r.color)||c;if(null===d){d=e}else if(e!==d){u=!1;break}}u&&d&&d!==s.fill&&s.set({fill:d})};H.current=()=>{(void 0!==e||t.composition.isComposing)&&g()},t.on("text-change",()=>{const n=P(),r=n&&n.instance===t?n.lastSelection:null;o(t.getSelection()||r),t.composition.isComposing||h(),void 0===e&&(e=setTimeout(g,10))}),t.on("selection-change",o),t.on("composition-start",()=>S(x.current)),t.on("composition-end",()=>{h(),S(null)}),A(t);const v=b.current.closest(".polotno-workspace-inner"),w=null==v?void 0:v.scrollLeft,k=null==v?void 0:v.scrollTop,E="end"===d?t.getLength()-1:0;c?setCursorFromCoords(t,c,E):t.setSelection(E,0,"api"),v&&void 0!==w&&void 0!==k&&(v.scrollLeft=w,v.scrollTop=k);const q=t.root;f=t=>{const e=t.relatedTarget;(null==e?void 0:e.classList.contains("ql-clipboard"))||(lt(e)||quillRef.pendingToolbarInteraction?B():setTimeout(()=>{quillRef.pendingToolbarInteraction||lt(document.activeElement)?B():(V(),_())},0))},p=()=>{V()},q.addEventListener("blur",f),q.addEventListener("focus",p),i=q,a=e=>{var o,n;const r=t.getSelection();if(!r||0===r.length){return}const i=t.getText(r.index,r.length).replace(/\n$/,""),l=window.getSelection();let s="";if(l&&l.rangeCount>0){const t=l.getRangeAt(0);let e=t.cloneContents(),o=t.startContainer;o&&o.nodeType===Node.TEXT_NODE&&(o=o.parentNode);let n=t.endContainer;if(n&&n.nodeType===Node.TEXT_NODE&&(n=n.parentNode),o&&o===n){let t=o;for(;t&&t!==q&&t.nodeType===Node.ELEMENT_NODE;){const o=t.tagName;if("SPAN"!==o&&"EM"!==o&&"STRONG"!==o&&"U"!==o&&"S"!==o&&"A"!==o){break}const n=t.cloneNode(!1);n.appendChild(e),e=n,t=t.parentNode}}const r=document.createElement("div");r.appendChild(e),s=r.innerHTML}if(null===(o=e.clipboardData)||void 0===o||o.setData("text/plain",i),s){const t=`<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>\x3c!--StartFragment--\x3e<div ${tt} style="white-space: pre-wrap; overflow-wrap: break-word;">`+s+"</div>\x3c!--EndFragment--\x3e</body></html>";null===(n=e.clipboardData)||void 0===n||n.setData("text/html",t)}e.preventDefault()},u=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",u)}catch(m){console.error("RichEditor init failed:",m)}}};return b.current?m():queueMicrotask(m),()=>{o=!0,H.current(),clearTimeout(e),i&&f&&i.removeEventListener("blur",f),i&&p&&i.removeEventListener("focus",p),V(),i&&a&&u&&(i.removeEventListener("copy",a),i.removeEventListener("cut",u)),r.runInAction(()=>{quillRef.editor.instance=null,quillRef.currentFormat={}}),D(t),delete window.__polotnoQuill}},[]),o.useEffect(()=>i(s,z,t=>{const e=quillRef.editor.instance;if(!e){return}if(y.current){return y.current=!1,void(N.current=t)}if(t===N.current){return}const o=e.getSelection();A(e),o&&(e.setSelection(o.index,o.length),syncQuillFormatState(e,e.getSelection()))},{fireImmediately:!0}),[]),o.useEffect(()=>i(s,()=>`${s.fontFamily}\0${s.fontSize}\0${s.fontWeight}\0${s.fontStyle}\0${s.lineHeight}\0${s.letterSpacing}\0${a(E(s.text))}`,()=>{const t=b.current;if(!t){return}const e="ltr"===a(E(s.text)),o=computeMarkerVars(s);o&&t.style.setProperty("--rt-number-gap",`${o.numberGap}px`),o&&e?(t.style.setProperty("--rt-bullet-left",`${o.bulletLeft}px`),t.style.setProperty("--rt-bullet-top",`${o.bulletTop}px`),t.style.setProperty("--rt-bullet-fs",`${o.bulletFontSize}px`),t.classList.add("rt-marker-geom")):t.classList.remove("rt-marker-geom")},{fireImmediately:!0}),[]),o.useEffect(()=>{window.addEventListener("blur",_);const t=t=>{var e;const o=t.target;"number"==typeof(null==o?void 0:o.nodeType)&&(null===(e=b.current)||void 0===e?void 0:e.contains(o))||lt(o)||quillRef.pendingToolbarInteraction||(V(),_())};return window.addEventListener("mousedown",t),window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",_),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t)}},[]);const W=v(s.fill),X={color:W};W.indexOf("gradient")>=0&&(X["--polotno-gradient"]=W,X.color="transparent"),s.strokeWidth>0&&(X.WebkitTextStroke=`${s.strokeWidth}px ${L(s.stroke)}`,X.paintOrder="stroke fill");const G=C(s.fill)?L(s.fill):O(s),Y=s.shadowEnabled&&!Q(s);return e(RichTextContainer,{ref:b,style:Object.assign(Object.assign(Object.assign(Object.assign({},X),{fontSize:s.fontSize,fontFamily:q(s.fontFamily),fontWeight:s.fontWeight,fontStyle:s.fontStyle,"--polotno-text-decoration":s.textDecoration||"none",textTransform:s.textTransform}),F(k)),{[f]:u(s.letterSpacing,s.fontSize)+"px",lineHeight:s.lineHeight,letterSpacing:s.letterSpacing*s.fontSize+"px",textAlign:s.align,opacity:Math.max(s.a.opacity,.2),filter:Y?I({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(E(s.text))})};function lt(t){const e=t;return!("function"!=typeof(null==e?void 0:e.closest)||!e.closest(".sketch-picker")&&!e.closest("[data-polotno-keep-focus]")&&!e.closest(".polotno-portal")&&!e.closest(".polotno-toolbar"))}export function setCursorFromCoords(t,e,o=0){if(!t||!e){return}const{x:r,y:i}=e;try{let e=null;if(document.caretRangeFromPoint){e=document.caretRangeFromPoint(r,i)}else if(document.caretPositionFromPoint){const t=document.caretPositionFromPoint(r,i);t&&(e=document.createRange(),e.setStart(t.offsetNode,t.offset))}if(e){const o=n.find(e.startContainer,!0);if(o){const n=o.offset(t.scroll)+e.startOffset;return void t.setSelection(n,0,"api")}}}catch(l){}t.setSelection(o,0,"api")}
|
|
173
|
+
`;const tt=[...z,...M,"header"];let et=["link","background","font-weight","color","gradient","font","italic","size","script","strike","underline","text-transform","indent","list","direction",...tt];export const setQuillFormats=t=>{et=t};let ot=!1;function nt(){ot||(ot=!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 rt(t){return t?"700":"400"}const it=n.import("delta"),lt="data-polotno-rich-text",st=["text-decoration-color","text-decoration-style","text-decoration-thickness"],at=(t,e)=>{const{style:o}=t,n=o.textDecorationLine,r={};if(n.includes("underline")&&(r.underline=!0),n.includes("line-through")&&(r.strike=!0),!r.underline&&!r.strike){return e}for(const i of st){const t=o.getPropertyValue(i);t&&!/^(initial|currentcolor|solid|auto)$/i.test(t)&&(r[i]=t)}return e.ops.reduce((t,e)=>void 0===e.insert?t:t.insert(e.insert,Object.assign(Object.assign({},r),e.attributes)),new it)};export const createQuill=(e,o)=>{j();const r=null==o?void 0:o.onFormatShortcut,i=new n(e,Object.assign(Object.assign({},r&&{modules:{keyboard:{bindings:Object.fromEntries(["bold","italic","underline"].map(t=>[t,{key:t[0],shortKey:!0,handler:()=>(r(t),!1)}]))}}}),{placeholder:(null==o?void 0:o.placeholder)||void 0,clipboard:{matchVisual:!1},formats:[...et,D]}));i.root.addEventListener("click",t=>{t.target instanceof Element&&t.target.closest("a")&&t.preventDefault()}),function(e){const o=e.format.bind(e),n=e.formatText.bind(e),r=e.getFormat.bind(e);e.format=(t,e,n)=>"bold"===t?(nt(),o("font-weight",rt(e),n)):o(t,e,n),e.formatText=(e,o,r,i,l)=>{if(r&&"object"==typeof r){if("bold"in r){nt();const{bold:l}=r,s=t(r,["bold"]);return n(e,o,Object.assign(Object.assign({},s),{"font-weight":rt(l)}),i)}return n(e,o,r,i)}return"bold"===r?(nt(),n(e,o,"font-weight",rt(i),l)):n(e,o,r,i,l)},e.getFormat=(...t)=>{const e=r(...t);return e&&null!=e["font-weight"]&&!("bold"in e)&&(e.bold=b(e["font-weight"])),e}}(i);const l=(t,e)=>e.compose((new it).retain(e.length(),{"font-weight":"700"}));i.clipboard.addMatcher("STRONG",l),i.clipboard.addMatcher("B",l),i.clipboard.addMatcher(Node.ELEMENT_NODE,at);const s=i.clipboard,a=s.onPaste.bind(s);return s.onPaste=(t,e)=>{var o;if(null===(o=e.html)||void 0===o?void 0:o.includes(lt)){return ut(i,()=>a(t,e))}const n=s.convert;s.convert=(...t)=>((t,e)=>{for(const o of t.ops){if(o.attributes){for(const t of e){delete o.attributes[t]}}}return t})(n.apply(s,t),tt);try{return a(t,e)}finally{s.convert=n}},i};const ct=(t,e)=>e.insert(t.data),dt=(t,e)=>{const o=e.ops[0];return parseFloat(t.style.textIndent)>0&&"string"==typeof(null==o?void 0:o.insert)&&o.insert.startsWith("\t")?e.compose((new it).delete(1)):e};function ut(t,e){const{matchers:o}=t.clipboard,n=o.findIndex(([t])=>t===Node.TEXT_NODE),r=o[n];-1!==n&&(o[n]=[Node.TEXT_NODE,ct]),o.push([Node.ELEMENT_NODE,dt]);try{return e()}finally{o.pop(),-1!==n&&(o[n]=r)}}function ft(t){var e;const{ops:o}=t.getContents(),n=o.map(t=>"string"==typeof t.insert?t.insert:"").join(""),r=new Uint8Array(n.length);for(const{start:s,end:a}of x(n)){r.fill(1,s,a)}const i=[];let l=0;for(const s of o){const t="string"==typeof s.insert?s.insert.length:1,o=!!(null===(e=s.attributes)||void 0===e?void 0:e[D]);for(let e=l;e<l+t;e++){const t=1===r[e];if(t===o){continue}const n=i[i.length-1];n&&n.index+n.length===e&&n.mark===t?n.length++:i.push({index:e,length:1,mark:t})}l+=t}for(const s of i){t.formatText(s.index,s.length,D,s.mark,"silent")}}export const setQuillContent=(t,e)=>{const o=ut(t,()=>t.clipboard.convert({html:"<div class='ql-editor' style='outline: none;'>"+e+"<p><br></p></div>",text:""}));t.setContents(o),t.history.clear()};export const editorHtml=t=>d({text:v(t.text),width:t.width,fontSize:t.fontSize,fontFamily:t.fontFamily},t.fontFamily);export const RichEditor=({onBlur:t,onChange:n,element:s,clickCoords:c,initialCaret:d="start",fitScale:g=1,fitScaleFor:b})=>{const v=o.useRef(null),y=o.useRef(!1),x=o.useRef(g);x.current=g;const[k,S]=o.useState(null),T=null!=k?k:g,E=o.useRef(b);E.current=b;const q=t=>({width:p(m({width:s.a.width,paddingLeft:s.paddingLeft,paddingRight:s.paddingRight,curveEnabled:s.curveEnabled}).width/t,s.letterSpacing,s.fontSize),zoom:1!==t?t:void 0}),C=o.useRef(null),j=o.useRef(void 0),z=()=>`${s.text}\0${s.fontSize}\0${s.fontFamily}`,M=t=>h(t,{fontSize:s.fontSize,fontFamily:s.fontFamily}),D=t=>{setQuillContent(t,editorHtml(s)),C.current=M(t.root.innerHTML),j.current=z()},P=o.useRef(()=>{}),H=()=>{P.current(),t()};o.useLayoutEffect(()=>{let t,e,o=!1,i=null,a=null,u=null,f=null,p=null;const m=()=>{if(!o&&v.current){try{t=createQuill(v.current,{placeholder:N(s.placeholder),onFormatShortcut:t=>{const e=_(s.store),{attrs:o,opts:n}=W(e.format,t);e.applyTextFormat(o,n)}}),r.runInAction(()=>{quillRef.editor.instance=t}),quillRef.pendingToolbarInteraction=!1,window.__polotnoQuill=t,A({instance:t,store:s.store,element:s});const o=e=>{e&&syncQuillFormatState(t,e)},m=()=>{let e=t.root;if(e.querySelector(".ql-cursor")){e=e.cloneNode(!0);const t=e.querySelector(".ql-cursor");t.replaceWith(t.textContent.replace(/\uFEFF/g,""))}return M(e.innerHTML)},h=()=>{const t=E.current;t&&l(s)&&(t=>{const e=v.current;if(!e){return}const o=q(t);e.style.width=`${o.width}px`,e.style.zoom=void 0===o.zoom?"":String(o.zoom)})(t(m()))},g=()=>{var o,r;if(clearTimeout(e),e=void 0,!l(s)){return}const i=m();if(i===C.current){return}C.current=i,y.current=!0,n(i);const a=t.getContents(),c=s.fill;let d=null,u=!0;for(const t of a.ops){if("string"!=typeof t.insert||"\n"===t.insert){continue}if(null===(o=t.attributes)||void 0===o?void 0:o.gradient){u=!1;break}const e=(null===(r=t.attributes)||void 0===r?void 0:r.color)||c;if(null===d){d=e}else if(e!==d){u=!1;break}}u&&d&&d!==s.fill&&s.set({fill:d})};P.current=()=>{(void 0!==e||t.composition.isComposing)&&g()},t.on("text-change",()=>{const n=Q(),r=n&&n.instance===t?n.lastSelection:null;o(t.getSelection()||r),t.composition.isComposing||(ft(t),h()),void 0===e&&(e=setTimeout(g,10))}),t.on("selection-change",o),t.on("composition-start",()=>S(x.current)),t.on("composition-end",()=>{ft(t),h(),S(null)}),D(t);const b=v.current.closest(".polotno-workspace-inner"),w=null==b?void 0:b.scrollLeft,k=null==b?void 0:b.scrollTop,T="end"===d?t.getLength()-1:0;c?setCursorFromCoords(t,c,T):t.setSelection(T,0,"api"),b&&void 0!==w&&void 0!==k&&(b.scrollLeft=w,b.scrollTop=k);const O=t.root;f=t=>{const e=t.relatedTarget;(null==e?void 0:e.classList.contains("ql-clipboard"))||(pt(e)||quillRef.pendingToolbarInteraction?K():setTimeout(()=>{quillRef.pendingToolbarInteraction||pt(document.activeElement)?K():(J(),H())},0))},p=()=>{J()},O.addEventListener("blur",f),O.addEventListener("focus",p),i=O,a=e=>{var o,n;const r=t.getSelection();if(!r||0===r.length){return}const i=t.getText(r.index,r.length).replace(/\n$/,""),l=window.getSelection();let s="";if(l&&l.rangeCount>0){const t=l.getRangeAt(0);let e=t.cloneContents(),o=t.startContainer;o&&o.nodeType===Node.TEXT_NODE&&(o=o.parentNode);let n=t.endContainer;if(n&&n.nodeType===Node.TEXT_NODE&&(n=n.parentNode),o&&o===n){let t=o;for(;t&&t!==O&&t.nodeType===Node.ELEMENT_NODE;){const o=t.tagName;if("SPAN"!==o&&"EM"!==o&&"STRONG"!==o&&"U"!==o&&"S"!==o&&"A"!==o){break}const n=t.cloneNode(!1);n.appendChild(e),e=n,t=t.parentNode}}const r=document.createElement("div");r.appendChild(e),s=r.innerHTML}if(null===(o=e.clipboardData)||void 0===o||o.setData("text/plain",i),s){const t=`<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>\x3c!--StartFragment--\x3e<div ${lt} style="white-space: pre-wrap; overflow-wrap: break-word;">`+s+"</div>\x3c!--EndFragment--\x3e</body></html>";null===(n=e.clipboardData)||void 0===n||n.setData("text/html",t)}e.preventDefault()},u=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",u)}catch(m){console.error("RichEditor init failed:",m)}}};return v.current?m():queueMicrotask(m),()=>{o=!0,P.current(),clearTimeout(e),i&&f&&i.removeEventListener("blur",f),i&&p&&i.removeEventListener("focus",p),J(),i&&a&&u&&(i.removeEventListener("copy",a),i.removeEventListener("cut",u)),r.runInAction(()=>{quillRef.editor.instance=null,quillRef.currentFormat={}}),I(t),delete window.__polotnoQuill}},[]),o.useEffect(()=>i(s,z,t=>{const e=quillRef.editor.instance;if(!e){return}if(y.current){return y.current=!1,void(j.current=t)}if(t===j.current){return}const o=e.getSelection();D(e),o&&(e.setSelection(o.index,o.length),syncQuillFormatState(e,e.getSelection()))},{fireImmediately:!0}),[]),o.useEffect(()=>i(s,()=>`${s.fontFamily}\0${s.fontSize}\0${s.fontWeight}\0${s.fontStyle}\0${s.lineHeight}\0${s.letterSpacing}\0${a(O(s.text))}`,()=>{const t=v.current;if(!t){return}const e="ltr"===a(O(s.text)),o=computeMarkerVars(s);o&&t.style.setProperty("--rt-number-gap",`${o.numberGap}px`),o&&e?(t.style.setProperty("--rt-bullet-left",`${o.bulletLeft}px`),t.style.setProperty("--rt-bullet-top",`${o.bulletTop}px`),t.style.setProperty("--rt-bullet-fs",`${o.bulletFontSize}px`),t.classList.add("rt-marker-geom")):t.classList.remove("rt-marker-geom")},{fireImmediately:!0}),[]),o.useEffect(()=>{window.addEventListener("blur",H);const t=t=>{var e;const o=t.target;"number"==typeof(null==o?void 0:o.nodeType)&&(null===(e=v.current)||void 0===e?void 0:e.contains(o))||pt(o)||quillRef.pendingToolbarInteraction||(J(),H())};return window.addEventListener("mousedown",t),window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",H),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t)}},[]);const B=w(s.fill),V={color:B};B.indexOf("gradient")>=0&&(V["--polotno-gradient"]=B,V.color="transparent"),s.strokeWidth>0&&(V.WebkitTextStroke=`${s.strokeWidth}px ${F(s.stroke)}`,V.paintOrder="stroke fill");const Y=R(s.fill)?F(s.fill):$(s),U=s.shadowEnabled&&!G(s);return e(RichTextContainer,{ref:v,style:Object.assign(Object.assign(Object.assign(Object.assign({},V),{fontSize:s.fontSize,fontFamily:L(s.fontFamily),fontWeight:s.fontWeight,fontStyle:s.fontStyle,"--polotno-text-decoration":s.textDecoration||"none",textTransform:s.textTransform}),q(T)),{[f]:u(s.letterSpacing,s.fontSize)+"px",lineHeight:s.lineHeight,letterSpacing:s.letterSpacing*s.fontSize+"px",textAlign:s.align,opacity:Math.max(s.a.opacity,.2),filter:U?X({shadowOffsetX:s.shadowOffsetX,shadowOffsetY:s.shadowOffsetY,shadowBlur:s.shadowBlur,shadowColor:s.shadowColor,shadowOpacity:s.shadowOpacity,rotation:s.a.rotation}):void 0,caretColor:Y}),dir:a(O(s.text))})};function pt(t){const e=t;return!("function"!=typeof(null==e?void 0:e.closest)||!e.closest(".sketch-picker")&&!e.closest("[data-polotno-keep-focus]")&&!e.closest(".polotno-portal")&&!e.closest(".polotno-toolbar"))}export function setCursorFromCoords(t,e,o=0){if(!t||!e){return}const{x:r,y:i}=e;try{let e=null;if(document.caretRangeFromPoint){e=document.caretRangeFromPoint(r,i)}else if(document.caretPositionFromPoint){const t=document.caretPositionFromPoint(r,i);t&&(e=document.createRange(),e.setStart(t.offsetNode,t.offset))}if(e){const o=n.find(e.startContainer,!0);if(o){const n=o.offset(t.scroll)+e.startOffset;return void t.setSelection(n,0,"api")}}}catch(l){}t.setSelection(o,0,"api")}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import t from"react";import{textPadding as e}from"@polotno/core/text-types";import{warnOnceDeprecated as o}from"../../utils/deprecation.js";export function useLegacyPaddingWarning(r){const{top:d,right:n,bottom:i,left:p}=e(r),a=d+n+i+p>0;t.useEffect(()=>{a&&o("text-padding-legacy-renderer","Polotno: the deprecated text renderer does not draw text padding (paddingTop/Right/Bottom/Left); the default renderer does.")},[a])}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export function getLimitedFontSize({oldText:
|
|
1
|
+
import{textContentBox as t}from"@polotno/core/text-types";export function getLimitedFontSize({oldText:e,newText:n,element:o}){if(!(n.length/Math.max(20,e.length)>=4)){return o.fontSize}const i=t(o).width*o.page.computedHeight/n.length,h=1.5*Math.sqrt(i),r=o.fontSize;return Math.min(r,Math.max(5,Math.round(h)))}
|
package/canvas/text-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Fragment as t,jsx as e,jsxs as r}from"react/jsx-runtime";import{elementOpacity as o}from"./paint-opacity.js";import n from"react";import{toDisplayColor as i}from"@polotno/core/color";import{observer as a}from"mobx-react-lite";import{Group as l,Path as s,Text as c,TextPath as d}from"react-konva";import{Html as u}from"react-konva-utils";import{nodeAutorun as f}from"../utils/mobx.js";import h from"konva";import{parsePath as g,roundCommands as m}from"svg-round-corners/lib/index.js";import{generateBackgroundShapeFromRects as p,textLinesToRects as x}from"@polotno/core/background-shape";import{useColor as b}from"./use-color.js";import{ROLES as v}from"../model/store.js";import{incrementLoader as w}from"../utils/loader.js";import{activeAnchorOf as S,scaledFontSizeAttrs as y,roundedFontSizeAttrs as E,roundedTextDecorationAttrs as z,roundedGeometryPx as k,roundedPosition as R,textMinResizeWidth as F,transformScale as j}from"../utils/px.js";import{isFontLoaded as M}from"../utils/fonts.js";import{flags as A}from"../utils/flags.js";import{textOverflowOf as L}from"../utils/text-sizing.js";import{FIT_FONT_SIZE_STEP as O,FIT_HEIGHT_EPSILON as T,legacyTextOverflow as C}from"@polotno/core/text-sizing";import{removeTags as H}from"@polotno/core/text";import{getDir as P,getCurvePath as W,resolveLineHeight as X,normalizeStrokeLineJoin as D}from"@polotno/core/text-html";import{cssFontFamily as Y}from"@polotno/core/fonts";import{shadowCastByBackground as $}from"@polotno/core/filters";import{applyOpacityToColor as J}from"@polotno/core/svg";import{applyFilter as N}from"./apply-filters.js";import{useFadeIn as B}from"./use-fadein.js";import{isTouchDevice as I}from"../utils/screen.js";import{isAlive as _}from"mobx-state-tree";import{getLimitedFontSize as V}from"./text-element/max-font-size.js";import{getOptimalCaretColor as q}from"./text-element/caret-color.js";import{visiblePlaceholder as U}from"./text-element/placeholder.js";import{hasMeasuredHeight as Z,resolveTextBoxHeight as G}from"@polotno/core/text-types";let K;function Q(){return K||(K=document.getElementById("polotno-text-style"),K||(K=document.createElement("style"),K.id="polotno-text-style",document.head.appendChild(K)),K)}h._fixTextRendering=!0;const tt={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 et=a(({textNodeRef:t,element:r,onBlur:o,selectAll:i,cursorPosition:a})=>{const[l,s]=n.useState(tt),c=t.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=q(r);const e=`\n .polotno-input::placeholder {\n color: ${l.color};\n opacity: 0.6;\n }\n `,o=Q();o.innerHTML="",o.appendChild(document.createTextNode(e)),JSON.stringify(t)!==JSON.stringify(l)&&s(t)});const d=n.useRef(null);if(n.useLayoutEffect(()=>{var t;const e=d.current;if(!e){return}null===(t=d.current)||void 0===t||t.focus({preventScroll:!0});const r=a||e.value.length;e.selectionStart=e.selectionEnd=r,i&&e.setSelectionRange(0,e.value.length)},[]),n.useEffect(()=>{window.addEventListener("blur",o);const t=t=>{var e;(null===(e=d.current)||void 0===e?void 0:e.contains(t.target))||o()};return window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",o),window.removeEventListener("touchstart",t)}},[]),!c){return null}let u=0;const f=c.textArr.length*c.lineHeight()*c.fontSize(),h=G(r.a,f);"middle"===r.verticalAlign&&(u=(h-f)/2),"bottom"===r.verticalAlign&&(u=h-f);const g=H(r.text);return e("textarea",{className:"polotno-input",ref:d,dir:P(g),style:Object.assign(Object.assign(Object.assign({},tt),l),{paddingTop:u+"px"}),value:g,onChange:t=>{const e=V({oldText:r.text,newText:t.target.value,element:r});r.set(Object.assign({text:t.target.value},y(r,e)))},placeholder:U(r.placeholder),onBlur:o})}),rt=t=>e(u,{children:e(et,Object.assign({},t))});export const useFontLoader=(t,e,r,o,n)=>useFontFamiliesLoader(t,[e],r,o,n);export const useFontFamiliesLoader=(t,e,r,o,i)=>{const[a,l]=n.useReducer(t=>t+1,0),s=e.join("\0"),c=n.useRef(e.every(t=>M(t,r,o))),d=n.useRef(null);return n.useLayoutEffect(()=>{const n=c.current;if(c.current=e.every(t=>M(t,r,o)),c.current){return void(n||l())}let a=!0;return(async()=>{n&&l();const u=w(`fonts ${s}`);await Promise.all(e.map(e=>t.loadFont(e,r,o,i))),a?(d.current=u,c.current=!0,l()):u()})(),()=>{d.current&&(d.current(),d.current=null),a=!1}},[s,r,o]),n.useEffect(()=>{var t;c.current&&(null===(t=d.current)||void 0===t||t.call(d),d.current=null)},[c.current]),[c.current]};export const getLineHeight=({fontLoaded:t,fontFamily:e,fontSize:r,lineHeight:o})=>n.useMemo(()=>X({fontFamily:e,fontSize:r,lineHeight:o}),[t,e,r,o]);export function usePrevious(t){const e=n.useRef(t),r=n.useRef(t);return n.useMemo(()=>{r.current=e.current,e.current=t},[t]),r.current}export{getCurvePath}from"@polotno/core/text-html";export const TextElement=a(({element:a,store:u})=>{const h=n.useRef(null),M=n.useRef(null),X=n.useRef(null),V=n.useRef([]),q=n.useRef(!0),[,K]=n.useReducer(t=>t+1,0),{editorEnabled:Q,selectAll:tt}=(t=>{const[e,r]=n.useState(!1),o=n.useRef(!1);return n.useEffect(()=>{var e=!0;return setTimeout(()=>{e&&(t._editModeEnabled&&(o.current=!0),r(!0),setTimeout(()=>{o.current=!1},50))},50),()=>{e=!1}},[]),{editorEnabled:e&&t._editModeEnabled,selectAll:o.current}})(a),[et,ot]=n.useState(!1),[nt,it]=n.useState(!1),at=n.useRef(a.a.height),lt=n.useRef(0),st=n.useRef(""),ct=n.useRef(0),dt=a.isSelected,ut=usePrevious(dt),{textVerticalResizeEnabled:ft}=A,ht=A.autoDeleteEmptyText,gt=usePrevious(a.fontFamily),[mt,pt]=n.useState([]),xt=a.backgroundEnabled||a.borderSize>0;n.useLayoutEffect(()=>{var t,e;if(!xt){return}const r=null!==(e=null===(t=h.current)||void 0===t?void 0:t.textArr)&&void 0!==e?e:[];JSON.stringify(r)!==JSON.stringify(mt)&&pt(r)}),n.useEffect(()=>{if(a.a.width){return}const t=h.current;t.width(600),a.set({width:1.4*t.getTextWidth()})},[]),n.useEffect(()=>{ut&&!dt&&ht&&""===a.text&&a.removable&&!U(a.placeholder)&&u.deleteElements([a.id])},[ht,a.placeholder,a.removable,a.text,dt,ut]);const bt=()=>{if(a.curveEnabled){const t=h.current;null==t||t.clearCache(),null==t||t.setAttrs({filters:[]});const e=M.current;e&&N(e,a)}else{const t=h.current;t&&N(t,a)}};n.useLayoutEffect(()=>{if(!nt){return f(a,()=>{bt()})}}),n.useEffect(()=>{const t=V.current;t.length>0&&(V.current=[],t.forEach(t=>t()))}),n.useEffect(()=>()=>{q.current=!1;const t=V.current;t.length>0&&(V.current=[],t.forEach(t=>t()))},[]);let vt=H(a.text);"uppercase"===a.textTransform&&(vt=vt.toUpperCase());const[wt]=useFontLoader(u,a.fontFamily,a.fontStyle,a.fontWeight,a.text||a.placeholder),St=()=>{if(a.curveEnabled){const t=M.current;return t.getSelfRect().height||t.fontSize()}const t=h.current.clone({height:void 0}),e=Math.ceil(t.fontSize()*t.lineHeight()*t.textArr.length+1);return t.destroy(),e};n.useLayoutEffect(()=>{if(!wt){return}const t=C(L(a)),{textSplitAllowed:e}=A,r=(t,e,r,o)=>{const n=w(`text ${a.id}`),i=()=>{u.history.ignore(t,e,r).then(()=>{q.current?(V.current.push(n),K()):n()})};o?queueMicrotask(i):i()};if(!Z(a)){const t=St();return void r(()=>{Z(a)||a.set({height:t})},void 0,void 0,!0)}if(!u.isPlaying){if("change-font-size"!==t||et){if("resize"===t){const t=St();ft&&a.height<t&&!et&&r(()=>{var e;_(a)&&a.set({height:t}),null===(e=h.current)||void 0===e||e.height(t)},!1,!0),ft||a.height===t||et||r(()=>{var e;_(a)&&a.set({height:t}),null===(e=h.current)||void 0===e||e.height(t)},!1,!0)}}else{const t=h.current,o=a.curveEnabled?a.fontSize:function(t,e,r=!1){const o=t.fontSize(),n=t.height(),i=H(e.text);let a=e.a.fontSize;t.height(void 0);const l=Math.round(2*e.a.fontSize)-1;for(let s=1;s<l;s++){const o=Z(e.a)&&t.height()>e.a.height+T;let n=i.split("\n").join(" ").split(/[\s-]+/).reduce((t,e)=>(e=e.toLowerCase(),/[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\uAC00-\uD7AF]/.test(e)?t.concat(e.split("")):t.concat(e)),[]),l=t.textArr.map(t=>t.text.toLowerCase()).join(";");const s=n.find(t=>!l.includes(t)||(l=l.replace(t,""),!1));if(!(o||s&&!r)){break}a-=O,t.fontSize(a)}return t.fontSize(o),t.height(n),a}(t,a,e);if(o!==a.fontSize){return void r(()=>{a.set(y(a,o))},!1,!0)}const n=St();a.height===n||ft||r(()=>{a.set({height:n})},!1,!0)}}}),n.useLayoutEffect(()=>{var t;if(wt&&a.curveEnabled){const e=null===(t=M.current)||void 0===t?void 0:t.getSelfRect().width;if(e){const t=e-a.a.width,r=a.a.rotation*Math.PI/180,o=-t/2*Math.cos(r),n=-t/2*Math.sin(r);a.set({width:e,x:a.a.x+o,y:a.a.y+n})}}},[wt,a.curveEnabled,a.curvePower,a.text,a.fontSize,a.fontFamily,a.fontWeight,a.fontStyle,a.letterSpacing]),n.useLayoutEffect(()=>{const t=h.current;t&&(t.width(t.width()+1e-8),t.width(t.width()-1e-8),t._setTextData(),bt())},[wt]);const yt=n.useRef(null),Et=n.useRef(a.width),zt=n.useRef(0),kt=t=>{t.evt.preventDefault();const e=u.selectedShapes.find(t=>t===a);e&&a.contentEditable&&(zt.current=function(t){var e;const r=t.target,o=function(t){var e=t.getAbsoluteTransform().copy();e.invert();var r=t.getStage().getPointerPosition();return e.point(r)}(r),n=r.textArr,i=Math.floor(o.y/(r.fontSize()*r.lineHeight())),a=n.slice(0,i).reduce((t,e)=>t+e.text.length,i),l=null!==(e=n[i])&&void 0!==e?e:n[0];let s=0;return"right"===r.align()?s=r.width()-l.width:"center"===r.align()&&(s=r.width()/2-l.width/2),a+Math.round((o.x-s)/l.width*l.text.length)}(t),a.toggleEditMode())},Rt=U(a.placeholder),Ft=!vt&&Rt?.6:o(a);B(h,Ft);const jt=getLineHeight({fontLoaded:wt,fontFamily:a.fontFamily,fontSize:a.a.fontSize,lineHeight:a.lineHeight}),Mt=a.selectable||u.role===v.ADMIN,At=b(a),Lt=b(a,a.stroke,"stroke"),Ot=n.useMemo(()=>a.backgroundEnabled||a.borderSize?a.curveEnabled?function({width:t,height:e,padding:r=0,cornerRadius:o=0}){const n=-r,i=-r,a=t+r,l=e+r,s=g(`M ${n} ${i} L ${a} ${i} L ${a} ${l} L ${n} ${l} Z`);return m(s,o).path}({width:a.a.width,height:a.a.height,cornerRadius:a.backgroundCornerRadius*(a.a.fontSize*jt*.5),padding:a.backgroundPadding*(a.a.fontSize*jt*.5)}):function({lines:t,lineHeight:e,width:r,align:o="left",padding:n=0,cornerRadius:i=0}){const a=x({lines:t,lineHeight:e,width:r,align:o});return p({rects:a,padding:n,cornerRadius:i})}({lines:JSON.parse(JSON.stringify(mt)),cornerRadius:a.backgroundCornerRadius*(a.a.fontSize*jt*.5),lineHeight:jt*a.a.fontSize,padding:a.backgroundPadding*(a.a.fontSize*jt*.5),width:a.a.width,align:a.align}):"",[a.backgroundEnabled,a.borderSize,a.curveEnabled,a.backgroundCornerRadius,a.a.fontSize,a.a.height,jt,a.backgroundPadding,a.a.width,a.align,mt]),Tt=I();let Ct=0;if(!a.curveEnabled){const t=mt.length*jt*a.a.fontSize,e=G(a.a,t);"middle"===a.verticalAlign?Ct=(e-t)/2:"bottom"===a.verticalAlign&&(Ct=e-t)}const Ht=a.curveEnabled?W(a.a.width,a.a.height,a.curvePower,a.a.fontSize):"",Pt=wt?Y(a.fontFamily):gt===a.fontFamily?"Arial":Y(gt),Wt=P(vt),Xt=a.shadowEnabled&&!$(a),Dt={shadowEnabled:a.shadowEnabled&&($(a)||a.borderSize>0),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:i(a.shadowColor),shadowOpacity:a.shadowOpacity};return r(t,{children:[xt&&e(s,Object.assign({ref:X,x:a.a.x,y:a.a.y,rotation:a.a.rotation,hideInExport:!a.showInExport,listening:!1,opacity:Ft,data:Ot,fillEnabled:a.backgroundEnabled,fill:J(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,lineJoin:a.backgroundCornerRadius>0?"round":"miter",offsetY:-Ct},Dt)),a.curveEnabled&&e(d,Object.assign({ref:M,data:Ht,text:vt||Rt,listening:!1,align:"center",textBaseline:"middle",direction:Wt},At,Lt,{strokeWidth:a.strokeWidth,lineJoin:D(a.strokeLineJoin),fillAfterStrokeEnabled:!0,fontSize:a.a.fontSize,fontFamily:`${Y(a.fontFamily)}, ${Y(gt)}`,fontStyle:a.fontStyle+" "+a.fontWeight,textDecoration:a.textDecoration.trim(),letterSpacing:a.letterSpacing*a.a.fontSize,x:a.a.x,y:a.a.y,rotation:a.a.rotation,opacity:a._editModeEnabled?.3:Ft,hideInExport:!a.showInExport},Dt,{shadowEnabled:Xt})),e(c,Object.assign({ref:h,id:a.selectable?a.id:void 0,name:a.selectable?"element":"",hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled,x:a.a.x,y:a.a.y,rotation:a.a.rotation,width:a.a.width,height:Z(a.a)?a.a.height:void 0,text:vt||Rt,direction:Wt},At,Lt,{lineJoin:D(a.strokeLineJoin),strokeWidth:a.strokeWidth,fillAfterStrokeEnabled:!0,fontSize:a.a.fontSize,fontFamily:Pt,fontStyle:a.fontStyle+" "+a.fontWeight,textDecoration:a.textDecoration,align:a.align,verticalAlign:a.verticalAlign,draggable:Tt?a.draggable&&dt:a.draggable,preventDefault:!Tt||dt,opacity:a.curveEnabled?0:Ft,visible:!a._editModeEnabled,ellipsis:"ellipsis"===L(a)},Dt,{shadowEnabled:Xt,lineHeight:jt,letterSpacing:a.letterSpacing*a.a.fontSize,listening:!!a.selectable&&Mt,onDragStart:()=>{it(!0)},onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{it(!1),a.set(R(t.target))},onClick:kt,onTap:kt,onTransformStart:()=>{ot(!0);const t=h.current;at.current=t.height(),Et.current=a.a.width,yt.current=t.position(),lt.current=t.rotation(),ct.current=t.height(),st.current=S(t)},onTransform:t=>{var e,r;const o=t.target;null===(e=X.current)||void 0===e||e.setAttrs({x:o.x(),y:o.y(),rotation:o.rotation(),scale:o.scale()});const n=st.current;if("middle-left"===n||"middle-right"===n){const t=o.scaleX(),e=o.width()*t,r=F(Et.current,a.a.fontSize);let n=e;e<r&&(n=r,yt.current&&o.position(yt.current)),o.width(n),o.scaleX(1),o.scaleY(1);let i=St();if("ellipsis"===L(a)){i=at.current}else{const t=Math.max(i,at.current);o.height(t),a.set({height:o.height()})}const l=A.textVerticalResizeEnabled?Math.max(i,at.current):St();a.set({x:o.x(),width:o.width(),rotation:o.rotation(),height:l}),o.skewX(0),o.skewY(0),bt()}if("top-center"===n||"bottom-center"===n){const e="resize"===L(a)?St():jt*a.a.fontSize,r=t.target.height()*t.target.scaleY();if(r<e){if(t.target.height(e),yt.current){if("top-center"===st.current){const r=lt.current*Math.PI/180,o=ct.current-e;t.target.x(yt.current.x-o*Math.sin(r)),t.target.y(yt.current.y+o*Math.cos(r))}else{t.target.position(yt.current)}}}else{t.target.height(r)}t.target.scaleY(1),t.target.scaleX(1),t.target.rotation(lt.current),t.target.skewX(0),t.target.skewY(0),ct.current=Math.max(e,r)}yt.current=t.target.position();const i=j(t.target.scaleX());null===(r=X.current)||void 0===r||r.setAttrs({scaleX:1,scaleY:1}),t.target.scaleX(1),t.target.scaleY(1),a.set({fontSize:a.a.fontSize*i,width:t.target.width()*i,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*i,shadowBlur:a.shadowBlur*i,shadowOffsetX:a.shadowOffsetX*i,shadowOffsetY:a.shadowOffsetY*i,strokeWidth:a.strokeWidth*i,borderSize:a.borderSize*i})},onTransformEnd:t=>{var e;const r=j(t.target.scaleX());t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},E(a,a.fontSize*r)),{width:Math.ceil(t.target.width()*r),x:k(t.target.x()),y:k(t.target.y()),rotation:k(t.target.rotation()),height:k(t.target.height()*r)})),null===(e=X.current)||void 0===e||e.setAttrs({scaleX:1,scaleY:1}),ot(!1)}})),Q&&a.contentEditable&&e(l,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,children:e(rt,{textNodeRef:h,element:a,selectAll:tt,cursorPosition:zt.current,onBlur:()=>{a.set(z(a)),a.toggleEditMode(!1)}})})]})});
|
|
1
|
+
import{Fragment as t,jsx as e,jsxs as r}from"react/jsx-runtime";import{elementOpacity as o}from"./paint-opacity.js";import n from"react";import{toDisplayColor as i}from"@polotno/core/color";import{observer as a}from"mobx-react-lite";import{Group as l,Path as s,Text as c,TextPath as d}from"react-konva";import{Html as u}from"react-konva-utils";import{nodeAutorun as f}from"../utils/mobx.js";import h from"konva";import{parsePath as g,roundCommands as p}from"svg-round-corners/lib/index.js";import{generateBackgroundShapeFromRects as m,textLinesToRects as x}from"@polotno/core/background-shape";import{useColor as b}from"./use-color.js";import{ROLES as S}from"../model/store.js";import{incrementLoader as v}from"../utils/loader.js";import{activeAnchorOf as w,scaledFontSizeAttrs as y,roundedFontSizeAttrs as E,roundedTextDecorationAttrs as z,roundedGeometryPx as k,roundedPosition as R,textMinResizeWidth as F,transformScale as j}from"../utils/px.js";import{isFontLoaded as M}from"../utils/fonts.js";import{flags as A}from"../utils/flags.js";import{textOverflowOf as L}from"../utils/text-sizing.js";import{FIT_FONT_SIZE_STEP as O,FIT_HEIGHT_EPSILON as T,legacyTextOverflow as C}from"@polotno/core/text-sizing";import{removeTags as H}from"@polotno/core/text";import{getDir as P,getCurvePath as W,resolveLineHeight as X,normalizeStrokeLineJoin as D}from"@polotno/core/text-html";import{cssFontFamily as Y}from"@polotno/core/fonts";import{shadowCastByBackground as $}from"@polotno/core/filters";import{applyOpacityToColor as J}from"@polotno/core/svg";import{applyFilter as N}from"./apply-filters.js";import{useFadeIn as B}from"./use-fadein.js";import{isTouchDevice as I}from"../utils/screen.js";import{isAlive as _}from"mobx-state-tree";import{getLimitedFontSize as V}from"./text-element/max-font-size.js";import{getOptimalCaretColor as q}from"./text-element/caret-color.js";import{visiblePlaceholder as U}from"./text-element/placeholder.js";import{useLegacyPaddingWarning as Z}from"./text-element/legacy-padding-warning.js";import{hasMeasuredHeight as G,resolveTextBoxHeight as K}from"@polotno/core/text-types";let Q;function tt(){return Q||(Q=document.getElementById("polotno-text-style"),Q||(Q=document.createElement("style"),Q.id="polotno-text-style",document.head.appendChild(Q)),Q)}h._fixTextRendering=!0;const et={border:"none",padding:"0px",overflow:"hidden",background:"none",outline:"none",resize:"none",overflowWrap:"break-word",whiteSpace:"pre-wrap",userSelect:"text",wordBreak:"normal",textTransform:"none"};export{getDir}from"@polotno/core/text-html";const rt=a(({textNodeRef:t,element:r,onBlur:o,selectAll:i,cursorPosition:a})=>{const[l,s]=n.useState(et),c=t.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=q(r);const e=`\n .polotno-input::placeholder {\n color: ${l.color};\n opacity: 0.6;\n }\n `,o=tt();o.innerHTML="",o.appendChild(document.createTextNode(e)),JSON.stringify(t)!==JSON.stringify(l)&&s(t)});const d=n.useRef(null);if(n.useLayoutEffect(()=>{var t;const e=d.current;if(!e){return}null===(t=d.current)||void 0===t||t.focus({preventScroll:!0});const r=a||e.value.length;e.selectionStart=e.selectionEnd=r,i&&e.setSelectionRange(0,e.value.length)},[]),n.useEffect(()=>{window.addEventListener("blur",o);const t=t=>{var e;(null===(e=d.current)||void 0===e?void 0:e.contains(t.target))||o()};return window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",o),window.removeEventListener("touchstart",t)}},[]),!c){return null}let u=0;const f=c.textArr.length*c.lineHeight()*c.fontSize(),h=K(r.a,f);"middle"===r.verticalAlign&&(u=(h-f)/2),"bottom"===r.verticalAlign&&(u=h-f);const g=H(r.text);return e("textarea",{className:"polotno-input",ref:d,dir:P(g),style:Object.assign(Object.assign(Object.assign({},et),l),{paddingTop:u+"px"}),value:g,onChange:t=>{const e=V({oldText:r.text,newText:t.target.value,element:r});r.set(Object.assign({text:t.target.value},y(r,e)))},placeholder:U(r.placeholder),onBlur:o})}),ot=t=>e(u,{children:e(rt,Object.assign({},t))});export const useFontLoader=(t,e,r,o,n)=>useFontFamiliesLoader(t,[e],r,o,n);export const useFontFamiliesLoader=(t,e,r,o,i)=>{const[a,l]=n.useReducer(t=>t+1,0),s=e.join("\0"),c=n.useRef(e.every(t=>M(t,r,o))),d=n.useRef(null);return n.useLayoutEffect(()=>{const n=c.current;if(c.current=e.every(t=>M(t,r,o)),c.current){return void(n||l())}let a=!0;return(async()=>{n&&l();const u=v(`fonts ${s}`);await Promise.all(e.map(e=>t.loadFont(e,r,o,i))),a?(d.current=u,c.current=!0,l()):u()})(),()=>{d.current&&(d.current(),d.current=null),a=!1}},[s,r,o]),n.useEffect(()=>{var t;c.current&&(null===(t=d.current)||void 0===t||t.call(d),d.current=null)},[c.current]),[c.current]};export const getLineHeight=({fontLoaded:t,fontFamily:e,fontSize:r,lineHeight:o})=>n.useMemo(()=>X({fontFamily:e,fontSize:r,lineHeight:o}),[t,e,r,o]);export function usePrevious(t){const e=n.useRef(t),r=n.useRef(t);return n.useMemo(()=>{r.current=e.current,e.current=t},[t]),r.current}export{getCurvePath}from"@polotno/core/text-html";export const TextElement=a(({element:a,store:u})=>{Z(a);const h=n.useRef(null),M=n.useRef(null),X=n.useRef(null),V=n.useRef([]),q=n.useRef(!0),[,Q]=n.useReducer(t=>t+1,0),{editorEnabled:tt,selectAll:et}=(t=>{const[e,r]=n.useState(!1),o=n.useRef(!1);return n.useEffect(()=>{var e=!0;return setTimeout(()=>{e&&(t._editModeEnabled&&(o.current=!0),r(!0),setTimeout(()=>{o.current=!1},50))},50),()=>{e=!1}},[]),{editorEnabled:e&&t._editModeEnabled,selectAll:o.current}})(a),[rt,nt]=n.useState(!1),[it,at]=n.useState(!1),lt=n.useRef(a.a.height),st=n.useRef(0),ct=n.useRef(""),dt=n.useRef(0),ut=a.isSelected,ft=usePrevious(ut),{textVerticalResizeEnabled:ht}=A,gt=A.autoDeleteEmptyText,pt=usePrevious(a.fontFamily),[mt,xt]=n.useState([]),bt=a.backgroundEnabled||a.borderSize>0;n.useLayoutEffect(()=>{var t,e;if(!bt){return}const r=null!==(e=null===(t=h.current)||void 0===t?void 0:t.textArr)&&void 0!==e?e:[];JSON.stringify(r)!==JSON.stringify(mt)&&xt(r)}),n.useEffect(()=>{if(a.a.width){return}const t=h.current;t.width(600),a.set({width:1.4*t.getTextWidth()})},[]),n.useEffect(()=>{ft&&!ut&>&&""===a.text&&a.removable&&!U(a.placeholder)&&u.deleteElements([a.id])},[gt,a.placeholder,a.removable,a.text,ut,ft]);const St=()=>{if(a.curveEnabled){const t=h.current;null==t||t.clearCache(),null==t||t.setAttrs({filters:[]});const e=M.current;e&&N(e,a)}else{const t=h.current;t&&N(t,a)}};n.useLayoutEffect(()=>{if(!it){return f(a,()=>{St()})}}),n.useEffect(()=>{const t=V.current;t.length>0&&(V.current=[],t.forEach(t=>t()))}),n.useEffect(()=>()=>{q.current=!1;const t=V.current;t.length>0&&(V.current=[],t.forEach(t=>t()))},[]);let vt=H(a.text);"uppercase"===a.textTransform&&(vt=vt.toUpperCase());const[wt]=useFontLoader(u,a.fontFamily,a.fontStyle,a.fontWeight,a.text||a.placeholder),yt=()=>{if(a.curveEnabled){const t=M.current;return t.getSelfRect().height||t.fontSize()}const t=h.current.clone({height:void 0}),e=Math.ceil(t.fontSize()*t.lineHeight()*t.textArr.length+1);return t.destroy(),e};n.useLayoutEffect(()=>{if(!wt){return}const t=C(L(a)),{textSplitAllowed:e}=A,r=(t,e,r,o)=>{const n=v(`text ${a.id}`),i=()=>{u.history.ignore(t,e,r).then(()=>{q.current?(V.current.push(n),Q()):n()})};o?queueMicrotask(i):i()};if(!G(a)){const t=yt();return void r(()=>{G(a)||a.set({height:t})},void 0,void 0,!0)}if(!u.isPlaying){if("change-font-size"!==t||rt){if("resize"===t){const t=yt();ht&&a.height<t&&!rt&&r(()=>{var e;_(a)&&a.set({height:t}),null===(e=h.current)||void 0===e||e.height(t)},!1,!0),ht||a.height===t||rt||r(()=>{var e;_(a)&&a.set({height:t}),null===(e=h.current)||void 0===e||e.height(t)},!1,!0)}}else{const t=h.current,o=a.curveEnabled?a.fontSize:function(t,e,r=!1){const o=t.fontSize(),n=t.height(),i=H(e.text);let a=e.a.fontSize;t.height(void 0);const l=Math.round(2*e.a.fontSize)-1;for(let s=1;s<l;s++){const o=G(e.a)&&t.height()>e.a.height+T;let n=i.split("\n").join(" ").split(/[\s-]+/).reduce((t,e)=>(e=e.toLowerCase(),/[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\uAC00-\uD7AF]/.test(e)?t.concat(e.split("")):t.concat(e)),[]),l=t.textArr.map(t=>t.text.toLowerCase()).join(";");const s=n.find(t=>!l.includes(t)||(l=l.replace(t,""),!1));if(!(o||s&&!r)){break}a-=O,t.fontSize(a)}return t.fontSize(o),t.height(n),a}(t,a,e);if(o!==a.fontSize){return void r(()=>{a.set(y(a,o))},!1,!0)}const n=yt();a.height===n||ht||r(()=>{a.set({height:n})},!1,!0)}}}),n.useLayoutEffect(()=>{var t;if(wt&&a.curveEnabled){const e=null===(t=M.current)||void 0===t?void 0:t.getSelfRect().width;if(e){const t=e-a.a.width,r=a.a.rotation*Math.PI/180,o=-t/2*Math.cos(r),n=-t/2*Math.sin(r);a.set({width:e,x:a.a.x+o,y:a.a.y+n})}}},[wt,a.curveEnabled,a.curvePower,a.text,a.fontSize,a.fontFamily,a.fontWeight,a.fontStyle,a.letterSpacing]),n.useLayoutEffect(()=>{const t=h.current;t&&(t.width(t.width()+1e-8),t.width(t.width()-1e-8),t._setTextData(),St())},[wt]);const Et=n.useRef(null),zt=n.useRef(a.width),kt=n.useRef(0),Rt=t=>{t.evt.preventDefault();const e=u.selectedShapes.find(t=>t===a);e&&a.contentEditable&&(kt.current=function(t){var e;const r=t.target,o=function(t){var e=t.getAbsoluteTransform().copy();e.invert();var r=t.getStage().getPointerPosition();return e.point(r)}(r),n=r.textArr,i=Math.floor(o.y/(r.fontSize()*r.lineHeight())),a=n.slice(0,i).reduce((t,e)=>t+e.text.length,i),l=null!==(e=n[i])&&void 0!==e?e:n[0];let s=0;return"right"===r.align()?s=r.width()-l.width:"center"===r.align()&&(s=r.width()/2-l.width/2),a+Math.round((o.x-s)/l.width*l.text.length)}(t),a.toggleEditMode())},Ft=U(a.placeholder),jt=!vt&&Ft?.6:o(a);B(h,jt);const Mt=getLineHeight({fontLoaded:wt,fontFamily:a.fontFamily,fontSize:a.a.fontSize,lineHeight:a.lineHeight}),At=a.selectable||u.role===S.ADMIN,Lt=b(a),Ot=b(a,a.stroke,"stroke"),Tt=n.useMemo(()=>a.backgroundEnabled||a.borderSize?a.curveEnabled||"box"===a.backgroundShape?function({width:t,height:e,padding:r=0,cornerRadius:o=0}){const n=-r,i=-r,a=t+r,l=e+r,s=g(`M ${n} ${i} L ${a} ${i} L ${a} ${l} L ${n} ${l} Z`);return p(s,o).path}({width:a.a.width,height:a.a.height,cornerRadius:a.backgroundCornerRadius*(a.a.fontSize*Mt*.5),padding:a.backgroundPadding*(a.a.fontSize*Mt*.5)}):function({lines:t,lineHeight:e,width:r,align:o="left",padding:n=0,cornerRadius:i=0}){const a=x({lines:t,lineHeight:e,width:r,align:o});return m({rects:a,padding:n,cornerRadius:i})}({lines:JSON.parse(JSON.stringify(mt)),cornerRadius:a.backgroundCornerRadius*(a.a.fontSize*Mt*.5),lineHeight:Mt*a.a.fontSize,padding:a.backgroundPadding*(a.a.fontSize*Mt*.5),width:a.a.width,align:a.align}):"",[a.backgroundEnabled,a.borderSize,a.curveEnabled,a.backgroundShape,a.backgroundCornerRadius,a.a.fontSize,a.a.height,Mt,a.backgroundPadding,a.a.width,a.align,mt]),Ct=I();let Ht=0;if(!a.curveEnabled&&"box"!==a.backgroundShape){const t=mt.length*Mt*a.a.fontSize,e=K(a.a,t);"middle"===a.verticalAlign?Ht=(e-t)/2:"bottom"===a.verticalAlign&&(Ht=e-t)}const Pt=a.curveEnabled?W(a.a.width,a.a.height,a.curvePower,a.a.fontSize):"",Wt=wt?Y(a.fontFamily):pt===a.fontFamily?"Arial":Y(pt),Xt=P(vt),Dt=a.shadowEnabled&&!$(a),Yt={shadowEnabled:a.shadowEnabled&&($(a)||a.borderSize>0),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:i(a.shadowColor),shadowOpacity:a.shadowOpacity};return r(t,{children:[bt&&e(s,Object.assign({ref:X,x:a.a.x,y:a.a.y,rotation:a.a.rotation,hideInExport:!a.showInExport,listening:!1,opacity:jt,data:Tt,fillEnabled:a.backgroundEnabled,fill:J(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,lineJoin:a.backgroundCornerRadius>0?"round":"miter",offsetY:-Ht},Yt)),a.curveEnabled&&e(d,Object.assign({ref:M,data:Pt,text:vt||Ft,listening:!1,align:"center",textBaseline:"middle",direction:Xt},Lt,Ot,{strokeWidth:a.strokeWidth,lineJoin:D(a.strokeLineJoin),fillAfterStrokeEnabled:!0,fontSize:a.a.fontSize,fontFamily:`${Y(a.fontFamily)}, ${Y(pt)}`,fontStyle:a.fontStyle+" "+a.fontWeight,textDecoration:a.textDecoration.trim(),letterSpacing:a.letterSpacing*a.a.fontSize,x:a.a.x,y:a.a.y,rotation:a.a.rotation,opacity:a._editModeEnabled?.3:jt,hideInExport:!a.showInExport},Yt,{shadowEnabled:Dt})),e(c,Object.assign({ref:h,id:a.selectable?a.id:void 0,name:a.selectable?"element":"",hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled,x:a.a.x,y:a.a.y,rotation:a.a.rotation,width:a.a.width,height:G(a.a)?a.a.height:void 0,text:vt||Ft,direction:Xt},Lt,Ot,{lineJoin:D(a.strokeLineJoin),strokeWidth:a.strokeWidth,fillAfterStrokeEnabled:!0,fontSize:a.a.fontSize,fontFamily:Wt,fontStyle:a.fontStyle+" "+a.fontWeight,textDecoration:a.textDecoration,align:a.align,verticalAlign:a.verticalAlign,draggable:Ct?a.draggable&&ut:a.draggable,preventDefault:!Ct||ut,opacity:a.curveEnabled?0:jt,visible:!a._editModeEnabled,ellipsis:"ellipsis"===L(a)},Yt,{shadowEnabled:Dt,lineHeight:Mt,letterSpacing:a.letterSpacing*a.a.fontSize,listening:!!a.selectable&&At,onDragStart:()=>{at(!0)},onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{at(!1),a.set(R(t.target))},onClick:Rt,onTap:Rt,onTransformStart:()=>{nt(!0);const t=h.current;lt.current=t.height(),zt.current=a.a.width,Et.current=t.position(),st.current=t.rotation(),dt.current=t.height(),ct.current=w(t)},onTransform:t=>{var e,r;const o=t.target;null===(e=X.current)||void 0===e||e.setAttrs({x:o.x(),y:o.y(),rotation:o.rotation(),scale:o.scale()});const n=ct.current;if("middle-left"===n||"middle-right"===n){const t=o.scaleX(),e=o.width()*t,r=F(zt.current,a.a.fontSize);let n=e;e<r&&(n=r,Et.current&&o.position(Et.current)),o.width(n),o.scaleX(1),o.scaleY(1);let i=yt();if("ellipsis"===L(a)){i=lt.current}else{const t=Math.max(i,lt.current);o.height(t),a.set({height:o.height()})}const l=A.textVerticalResizeEnabled?Math.max(i,lt.current):yt();a.set({x:o.x(),width:o.width(),rotation:o.rotation(),height:l}),o.skewX(0),o.skewY(0),St()}if("top-center"===n||"bottom-center"===n){const e="resize"===L(a)?yt():Mt*a.a.fontSize,r=t.target.height()*t.target.scaleY();if(r<e){if(t.target.height(e),Et.current){if("top-center"===ct.current){const r=st.current*Math.PI/180,o=dt.current-e;t.target.x(Et.current.x-o*Math.sin(r)),t.target.y(Et.current.y+o*Math.cos(r))}else{t.target.position(Et.current)}}}else{t.target.height(r)}t.target.scaleY(1),t.target.scaleX(1),t.target.rotation(st.current),t.target.skewX(0),t.target.skewY(0),dt.current=Math.max(e,r)}Et.current=t.target.position();const i=j(t.target.scaleX());null===(r=X.current)||void 0===r||r.setAttrs({scaleX:1,scaleY:1}),t.target.scaleX(1),t.target.scaleY(1),a.set({fontSize:a.a.fontSize*i,width:t.target.width()*i,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*i,shadowBlur:a.shadowBlur*i,shadowOffsetX:a.shadowOffsetX*i,shadowOffsetY:a.shadowOffsetY*i,strokeWidth:a.strokeWidth*i,borderSize:a.borderSize*i})},onTransformEnd:t=>{var e;const r=j(t.target.scaleX());t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},E(a,a.fontSize*r)),{width:Math.ceil(t.target.width()*r),x:k(t.target.x()),y:k(t.target.y()),rotation:k(t.target.rotation()),height:k(t.target.height()*r)})),null===(e=X.current)||void 0===e||e.setAttrs({scaleX:1,scaleY:1}),nt(!1)}})),tt&&a.contentEditable&&e(l,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,children:e(ot,{textNodeRef:h,element:a,selectAll:et,cursorPosition:kt.current,onBlur:()=>{a.set(z(a)),a.toggleEditMode(!1)}})})]})});
|
package/canvas/use-color.d.ts
CHANGED
|
@@ -5,10 +5,32 @@ type ColorElement = {
|
|
|
5
5
|
width: number;
|
|
6
6
|
height: number;
|
|
7
7
|
fill?: string;
|
|
8
|
+
strokeWidth?: number;
|
|
8
9
|
};
|
|
9
10
|
export declare const withDisplayTextPaints: (element: TextElementLike) => TextElementLike;
|
|
10
|
-
export declare const useColor: (element: ColorElement, value?: string, propName?: string) => {
|
|
11
|
-
|
|
11
|
+
export declare const useColor: (element: ColorElement, value?: string, propName?: string, strokeReach?: number) => {
|
|
12
|
+
fillRadialGradientStartPointX: number;
|
|
13
|
+
fillRadialGradientStartPointY: number;
|
|
14
|
+
fillRadialGradientStartRadius: number;
|
|
15
|
+
fillRadialGradientEndPointX: number;
|
|
16
|
+
fillRadialGradientEndPointY: number;
|
|
17
|
+
fillRadialGradientEndRadius: number;
|
|
18
|
+
fillRadialGradientColorStops: (string | number)[];
|
|
19
|
+
fill: string;
|
|
20
|
+
fillPriority: string;
|
|
21
|
+
fillRadialGradientScaleY?: number | undefined;
|
|
22
|
+
fillLinearGradientStartPointX?: undefined;
|
|
23
|
+
fillLinearGradientStartPointY?: undefined;
|
|
24
|
+
fillLinearGradientColorStops?: undefined;
|
|
25
|
+
fillLinearGradientEndPointX?: undefined;
|
|
26
|
+
fillLinearGradientEndPointY?: undefined;
|
|
27
|
+
strokeLinearGradientStartPointX?: undefined;
|
|
28
|
+
strokeLinearGradientStartPointY?: undefined;
|
|
29
|
+
strokeLinearGradientColorStops?: undefined;
|
|
30
|
+
strokeLinearGradientEndPointX?: undefined;
|
|
31
|
+
strokeLinearGradientEndPointY?: undefined;
|
|
32
|
+
} | {
|
|
33
|
+
[x: string]: string | CanvasGradient | CanvasPattern;
|
|
12
34
|
fillLinearGradientStartPointX?: undefined;
|
|
13
35
|
fillLinearGradientStartPointY?: undefined;
|
|
14
36
|
fillLinearGradientColorStops?: undefined;
|