polotno 4.22.0 → 4.22.1
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/canvas/render-tag-element.js +1 -1
- package/package.json +1 -1
- package/polotno.bundle.js +148 -148
- package/side-panel/side-panel.d.ts +1 -0
- package/side-panel/side-panel.js +1 -1
- package/utils/validate-key.js +1 -1
|
@@ -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 y,scaledFontSizeBounds as v,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,yt,vt,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=At?Object.assign(Object.assign({},s),{text:Ht}):s;try{return qt?Object.assign({curved:!0},F({element:t,sizing:Vt,layoutAt:B(N(s),{getHtmlOptions:{fontFamily:Tt,color:Bt,text:Ht,textIsNormalized:!0}})})):Object.assign({curved:!1},I({element:t,sizing:Vt,layoutAt:Nt(s)}))}catch(e){return console.error("render-tag layout failed:",e),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!==(yt=null==Kt?void 0:Kt.overhang)&&void 0!==yt?yt:ue,fe=H({fontLoaded:It,fontFamily:s.fontFamily,fontSize:ce,lineHeight:s.lineHeight}),ge=qt?0:Math.max(null!==(vt=null==ee?void 0:ee.height)&&void 0!==vt?vt: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]),ye=n.useRef([]),ve=n.useRef(null),we=n.useRef(!0),[,je]=n.useReducer(t=>t+1,0);n.useEffect(()=>{const t=ye.current;t.length>0&&(ye.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?(ye.current.push(i),je()):i()})};r?queueMicrotask(n):n()},[s.id,lt]),ze=n.useCallback(t=>{const e=null!==ve.current;ve.current=t,e||Oe(()=>{const t=ve.current;ve.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=y(s,t);At||e===s.text||(o.text=e),s.set(o)},!1,!0)}else{t&&e()}}}),n.useEffect(()=>()=>{we.current=!1;const t=ye.current;t.length>0&&(ye.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({},v($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)?v(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},y(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 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})})})]})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "polotno",
|
|
3
|
-
"version": "4.22.
|
|
3
|
+
"version": "4.22.1",
|
|
4
4
|
"description": "Design editor SDK: embed a Canva-like editor, generate designs from templates and data, export PNG, PDF, and video in the browser or on a server.",
|
|
5
5
|
"author": "Anton Lavrenov",
|
|
6
6
|
"keywords": [
|