polotno 4.7.0 → 4.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/blueprint/side-panel/text-panel.js +3 -3
- package/blueprint/toolbar/text-toolbar.js +2 -2
- package/canvas/downscale.d.ts +3 -0
- package/canvas/downscale.js +1 -0
- package/canvas/html-element.js +1 -1
- package/canvas/image-element.d.ts +1 -1
- package/canvas/image-element.js +1 -1
- package/canvas/quill-font-formats.js +1 -1
- package/canvas/render-tag-element.js +1 -1
- package/canvas/rich-editor.js +5 -2
- package/canvas/text-element.js +1 -1
- package/canvas/video-element.js +1 -1
- package/model/group-model.d.ts +2 -12
- package/model/page-model.d.ts +2 -12
- package/model/store.d.ts +18 -108
- package/model/table-model.d.ts +2 -12
- package/model/table-model.js +1 -1
- package/model/text-model.js +1 -1
- package/package.json +7 -7
- package/polotno.bundle.js +171 -170
- package/side-panel/images-grid.d.ts +2 -1
- package/side-panel/images-grid.js +1 -1
- package/side-panel/select-svg.js +1 -1
- package/side-panel/text-panel.js +1 -1
- package/side-panel/text-template.d.ts +12 -0
- package/side-panel/text-template.js +1 -0
- package/toolbar/text-toolbar.js +1 -1
- package/ui.css +1 -1
- package/utils/animations.d.ts +7 -1
- package/utils/crop.d.ts +2 -2
- package/utils/flags.d.ts +1 -1
- package/utils/font-metric.js +1 -1
- package/utils/fonts.js +1 -1
- package/utils/from-svg.js +1 -1
- package/utils/html-to-svg/font-inliner.js +1 -1
- package/utils/html2canvas.js +1 -1
- package/utils/loader.d.ts +1 -1
- package/utils/math.d.ts +21 -6
- package/utils/measure-text.js +1 -1
- package/utils/rich-text-html.js +1 -1
- package/utils/text-format-state.js +1 -1
- package/utils/text-format.js +1 -1
- package/utils/unit.d.ts +6 -5
- package/utils/validate-key.d.ts +15 -2
- package/utils/validate-key.js +1 -1
- package/utils/wait.d.ts +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
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 n=0;for(i=Object.getOwnPropertySymbols(t);n<i.length;n++){e.indexOf(i[n])<0&&Object.prototype.propertyIsEnumerable.call(t,i[n])&&(o[i[n]]=t[i[n]])}}return o};import{jsx as e,jsxs as o}from"react/jsx-runtime";import i from"react";import{observer as n}from"mobx-react-lite";import{Button as l,Tab as a,Tabs as r}from"@blueprintjs/core";import s from"swr";import{Upload as c,Trash as d}from"@blueprintjs/icons";import{t as f}from"../../utils/l10n.js";import m from"../../utils/styled.js";import{isMobile as p}from"../../utils/screen.js";import{ImagesGrid as h}from"./images-grid.js";import{textTemplateList as g}from"../../utils/api.js";import{fetcher as y}from"../../utils/use-api.js";import{localFileToURL as x}from"../../utils/file.js";import{
|
|
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 n=0;for(i=Object.getOwnPropertySymbols(t);n<i.length;n++){e.indexOf(i[n])<0&&Object.prototype.propertyIsEnumerable.call(t,i[n])&&(o[i[n]]=t[i[n]])}}return o};import{jsx as e,jsxs as o}from"react/jsx-runtime";import i from"react";import{observer as n}from"mobx-react-lite";import{Button as l,Tab as a,Tabs as r}from"@blueprintjs/core";import s from"swr";import{Upload as c,Trash as d}from"@blueprintjs/icons";import{t as f}from"../../utils/l10n.js";import m from"../../utils/styled.js";import{isMobile as p}from"../../utils/screen.js";import{ImagesGrid as h}from"./images-grid.js";import{textTemplateList as g}from"../../utils/api.js";import{fetcher as y}from"../../utils/use-api.js";import{localFileToURL as x}from"../../utils/file.js";import{cssFontFamily as u}from"@polotno/core/fonts";import{registerNextDomDrop as b}from"../../canvas/page.js";const v=m("div")`
|
|
2
2
|
height: calc(100% - 40px);
|
|
3
3
|
display: flex;
|
|
4
4
|
flex-direction: column;
|
|
@@ -6,7 +6,7 @@ var t=this&&this.__rest||function(t,e){var o={};for(var i in t){Object.prototype
|
|
|
6
6
|
.bp6-dark & .polotno-text-preview-plain {
|
|
7
7
|
filter: invert(1);
|
|
8
8
|
}
|
|
9
|
-
`,
|
|
9
|
+
`,j=m("div")`
|
|
10
10
|
height: 100px;
|
|
11
11
|
cursor: pointer;
|
|
12
12
|
box-shadow: 0 0 5px rgba(16, 22, 26, 0.3);
|
|
@@ -21,4 +21,4 @@ var t=this&&this.__rest||function(t,e){var o={};for(var i in t){Object.prototype
|
|
|
21
21
|
text-align: center;
|
|
22
22
|
color: white;
|
|
23
23
|
margin-bottom: 10px;
|
|
24
|
-
`;let
|
|
24
|
+
`;let w=async t=>x(t);export function setFontUploadFunc(t){w=t}const F=n(({onSelect:t,onRemove:i,font:n})=>o(j,{style:{fontFamily:u(n.fontFamily)},className:"polotno-font-item",onClick:t,children:[n.fontFamily," text",e(l,{style:{position:"absolute",right:0,bottom:0},minimal:!0,icon:e(d,{}),onClick:t=>{t.stopPropagation(),i()}})]})),S=o=>{var{onSelect:i}=o,n=t(o,["onSelect"]);return e(l,Object.assign({},n,{draggable:!0,className:"polotno-close-panel",onClick:()=>i(),onDragStart:()=>{b(({x:t,y:e})=>{i({x:t,y:e})})},onDragEnd:t=>{b(null)}}))};export const TextPanel=n(({store:t})=>{i.useEffect(()=>{t.loadFont("Roboto")},[]);const n=e=>{var o;const i=e.width||t.width/2,n=((null==e?void 0:e.x)||t.width/2)-i/2,l=((null==e?void 0:e.y)||t.height/2)-e.fontSize/2,a=(t.width+t.height)/2160,r=null===(o=t.activePage)||void 0===o?void 0:o.addElement(Object.assign(Object.assign({type:"text",fontFamily:"Roboto"},e),{x:n,y:l,width:i,fontSize:e.fontSize*a}));p()||null==r||r.toggleEditMode(!0)};i.useEffect(()=>{t.fonts.forEach(e=>t.loadFont(e.fontFamily))},[t.fonts]);const{data:d,error:m}=s(g(),y),[x,u]=i.useState("text");return o("div",{style:{height:"100%",display:"flex",flexDirection:"column"},children:[o(r,{id:"text-panel-tabs",large:!0,onChange:t=>u(String(t)),children:[e(a,{id:"text",children:f("sidePanel.text")}),e(a,{id:"font",children:f("sidePanel.myFonts")})]}),"text"===x&&o(v,{children:[e(S,{style:{marginBottom:"5px",width:"100%",fontSize:"25px",fontFamily:"Roboto"},minimal:!0,onSelect:t=>{n(Object.assign(Object.assign({},t),{fontSize:76,text:f("sidePanel.headerText"),fontFamily:"Roboto"}))},children:f("sidePanel.createHeader")}),e(S,{style:{marginBottom:"5px",width:"100%",fontSize:"18px",fontFamily:"Roboto"},minimal:!0,onSelect:t=>{n(Object.assign(Object.assign({},t),{fontSize:44,text:f("sidePanel.subHeaderText"),fontFamily:"Roboto"}))},children:f("sidePanel.createSubHeader")}),e(S,{style:{marginBottom:"5px",width:"100%",fontSize:"14px",fontFamily:"Roboto"},minimal:!0,onSelect:t=>{n(Object.assign(Object.assign({},t),{fontSize:30,text:f("sidePanel.bodyText"),fontFamily:"Roboto"}))},children:f("sidePanel.createBody")}),e(h,{shadowEnabled:!1,images:null==d?void 0:d.items,getPreview:t=>t.preview,getImageClassName:t=>t.json.indexOf("plain")>=0?"polotno-text-preview-plain":"",isLoading:!d,error:m,onSelect:async(e,o)=>{const i=await fetch(e.json),n=await i.json();if(!t.activePage){return}const l=(t.width+t.height)/2160,a=o?o.x-n.width/2*l:t.width/2-n.width/2*l,r=o?o.y-n.height/2*l:t.height/2-n.height/2*l;t.history.transaction(()=>{const e=n.pages[0].children,o=[];e.forEach(e=>{delete e.id;const{id:i}=t.activePage.addElement(Object.assign(Object.assign({},e),{fontSize:e.fontSize*l,x:e.x*l+a,y:e.y*l+r,width:e.width*l,height:e.height*l}));o.push(i)}),t.selectElements(o)})}})]}),"font"===x&&o("div",{style:{display:"flex",flexDirection:"column",height:"calc(100% - 50px)"},children:[o("label",{htmlFor:"polotno-font-upload",children:[e(l,{icon:e(c,{}),style:{width:"100%"},onClick:()=>{var t;null===(t=document.querySelector("#polotno-font-upload"))||void 0===t||t.click()},children:f("sidePanel.uploadFont")}),e("input",{type:"file",accept:".ttf, .otf, .woff, .woff2, .eot",id:"polotno-font-upload",style:{display:"none"},onChange:async e=>{const{target:o}=e;for(const i of o.files){const e=await w(i),o=i.name.split(".")[0].replace(/,/g,"");t.addFont({fontFamily:o,url:e})}o.value=null}})]}),e("div",{style:{paddingTop:"20px",overflow:"auto",height:"100%"},children:t.fonts.map((o,i)=>e(F,{font:o,onSelect:()=>{n({fontSize:80,text:"Cool text",fontFamily:o.fontFamily})},onRemove:()=>{t.find(t=>("text"===t.type&&t.fontFamily===o.fontFamily&&t.set({fontFamily:"Roboto"}),!1)),t.removeFont(o.fontFamily)}},i))})]})]})});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
var t=this&&this.__rest||function(t,e){var n={};for(var o in t){Object.prototype.hasOwnProperty.call(t,o)&&e.indexOf(o)<0&&(n[o]=t[o])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(o=Object.getOwnPropertySymbols(t);i<o.length;i++){e.indexOf(o[i])<0&&Object.prototype.propertyIsEnumerable.call(t,o[i])&&(n[o[i]]=t[o[i]])}}return n};import{jsx as e,jsxs as n}from"react/jsx-runtime";import o from"react";import{observer as i}from"mobx-react-lite";import{Button as r,ButtonGroup as a,Classes as l,InputGroup as s,MenuDivider as c,MenuItem as m,NumericInput as p,Popover as f,Position as d,Slider as h,Tooltip as u}from"@blueprintjs/core";import{List as x}from"react-window";import g from"swr";import{AlignCenter as T,AlignJustify as b,AlignLeft as y,AlignRight as v,Bold as O,CaretDown as F,Italic as S,Search as I,Strikethrough as C,Underline as N}from"@blueprintjs/icons";import{getFontsList as w,globalFonts as A,isGoogleFontChanged as E}from"../../utils/fonts.js";import{getGoogleFontImage as j,getGoogleFontsListAPI as M}from"../../utils/api.js";import L from"./color-picker.js";import _ from"./filters-picker.js";import{AnimationsPicker as V}from"./animations-picker.js";import{ElementContainer as D,extendToolbar as k}from"./element-container.js";import{TextAiWrite as P}from"./text-ai-write.js";import{MdcFormatLineSpacing as R,MdcFormatLetterCase as B,MdcFormatVerticalAlignTop as z,MdcFormatVerticalAlignCenter as G,MdcFormatVerticalAlignBottom as W}from"../../icons/meronex.js";import H from"../../utils/styled.js";import{t as $}from"../../utils/l10n.js";import{flags as U}from"../../utils/flags.js";const
|
|
1
|
+
var t=this&&this.__rest||function(t,e){var n={};for(var o in t){Object.prototype.hasOwnProperty.call(t,o)&&e.indexOf(o)<0&&(n[o]=t[o])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(o=Object.getOwnPropertySymbols(t);i<o.length;i++){e.indexOf(o[i])<0&&Object.prototype.propertyIsEnumerable.call(t,o[i])&&(n[o[i]]=t[o[i]])}}return n};import{jsx as e,jsxs as n}from"react/jsx-runtime";import o from"react";import{observer as i}from"mobx-react-lite";import{Button as r,ButtonGroup as a,Classes as l,InputGroup as s,MenuDivider as c,MenuItem as m,NumericInput as p,Popover as f,Position as d,Slider as h,Tooltip as u}from"@blueprintjs/core";import{List as x}from"react-window";import g from"swr";import{AlignCenter as T,AlignJustify as b,AlignLeft as y,AlignRight as v,Bold as O,CaretDown as F,Italic as S,Search as I,Strikethrough as C,Underline as N}from"@blueprintjs/icons";import{getFontsList as w,globalFonts as A,isGoogleFontChanged as E}from"../../utils/fonts.js";import{getGoogleFontImage as j,getGoogleFontsListAPI as M}from"../../utils/api.js";import L from"./color-picker.js";import _ from"./filters-picker.js";import{AnimationsPicker as V}from"./animations-picker.js";import{ElementContainer as D,extendToolbar as k}from"./element-container.js";import{TextAiWrite as P}from"./text-ai-write.js";import{MdcFormatLineSpacing as R,MdcFormatLetterCase as B,MdcFormatVerticalAlignTop as z,MdcFormatVerticalAlignCenter as G,MdcFormatVerticalAlignBottom as W}from"../../icons/meronex.js";import H from"../../utils/styled.js";import{t as $}from"../../utils/l10n.js";import{flags as U}from"../../utils/flags.js";import{cssFontFamily as q}from"@polotno/core/fonts";const J=H("img")`
|
|
2
2
|
height: 20px;
|
|
3
3
|
|
|
4
4
|
.bp6-dark & {
|
|
5
5
|
filter: invert(1);
|
|
6
6
|
}
|
|
7
|
-
`,
|
|
7
|
+
`,K=w(),Q=({fontFamily:t,handleClick:n,modifiers:i,store:r,isCustom:a})=>{const[l,s]=o.useState(!a);if(o.useEffect(()=>{l||r.loadFont(t)},[t,l]),"_divider"===t){return e("div",{style:{paddingTop:"10px"},children:e(c,{})})}const p=l?e(J,{src:j(t),alt:t,onError:()=>{s(!1)}}):t;return e(m,{text:p,active:i.active,disabled:i.disabled,onClick:n,style:{fontFamily:q(t)}})},X=({index:t,style:n,filteredFonts:o,activeFont:i,onFontSelect:r,store:a})=>{const l=o[t];return e("div",{style:n,children:e(Q,{fontFamily:l,modifiers:{active:i===l},handleClick:()=>r(l),store:a,isCustom:a.fonts.find(t=>t.fontFamily===l)||A.find(t=>t.fontFamily===l)},l)})},Y=({onChange:t,defaultValue:n})=>{const i=o.useRef(null);return o.useEffect(()=>{i.current&&i.current.focus()},[]),e(s,{leftIcon:e(I,{}),inputRef:i,defaultValue:n,onChange:e=>t(e.target.value)})};export const FontMenu=({store:t,fonts:i,activeFont:a,activeFontLabel:s,onFontSelect:c,mixed:m,mixedFonts:p})=>{const[h,g]=o.useState(""),T=i.filter(t=>(null==t?void 0:t.toLowerCase().indexOf(h.toLowerCase()))>=0);return e(f,{content:n("div",{children:[e(Y,{onChange:t=>g(t),defaultValue:h}),e("div",{style:{paddingTop:"5px"},children:e(x,{tagName:"ul",className:l.MENU,style:{height:Math.min(400,30*T.length)+10,width:210},rowCount:T.length,rowHeight:30,rowComponent:X,rowProps:{filteredFonts:T,activeFont:a,onFontSelect:c,store:t}})})]}),children:e(u,{content:m&&p&&p.length>0?`${$("toolbar.fontsInSelection")}: ${p.join(", ")}`:$("toolbar.fontFamily"),position:d.BOTTOM,children:e(r,{text:m?$("toolbar.fontFamilyMixed"):s,rightIcon:e(F,{}),minimal:!0,style:{marginRight:"5px",fontFamily:m?void 0:q(a),fontStyle:m?"italic":void 0,color:m?"#5c7080":void 0,overflow:"hidden",whiteSpace:"nowrap",maxHeight:"30px"},"aria-label":$("toolbar.fontFamily")})})})};const Z={};export const fetcher=t=>Z[t]?Promise.resolve(Z[t]):fetch(t).then(t=>t.json()).then(e=>(Z[t]=e,e));export const TextFontFamily=i(({elements:t,store:n})=>{const{data:i,mutate:r}=g(M(),fetcher,{isPaused:()=>E(),fallbackData:[]});o.useEffect(()=>{r()},[E()]);const a=n.fonts.concat(A).map(t=>t.fontFamily).concat((null==i?void 0:i.length)&&!E()?i:K);let l=t[0].fontFamily;l.length>15&&(l=l.slice(0,15)+"...");const s=[];n.find(t=>("text"===t.type&&s.push(t.fontFamily),!1));const c=[...new Set(s.concat("_divider").concat(a))];return e(FontMenu,{fonts:c,activeFont:t[0].fontFamily,activeFontLabel:l,store:n,onFontSelect:e=>{n.history.transaction(()=>{t.forEach(t=>{t.set({fontFamily:e})})})}})});export const TextFontSize=i(({elements:t,store:n})=>{const[i,r]=o.useState(!1);return e(u,{content:$("toolbar.fontSize"),position:d.BOTTOM,openOnTargetFocus:!1,disabled:i,children:e(NumberInput,{"aria-label":$("toolbar.fontSize"),onFocus:()=>r(!0),onBlur:()=>r(!1),onValueChange:e=>{n.history.transaction(()=>{t.forEach(t=>{t.set({fontSize:e,width:Math.max(e,t.width)})})})},value:Math.round(t[0].fontSize),style:{width:"50px"},min:5,max:4*n.height})})});export const ALIGN_OPTIONS=["left","center","right","justify"];export const VERTICAL_ALIGN_OPTIONS=["top","middle","bottom"];export const VERTICAL_ALIGN_ICONS={top:e("span",{className:"bp6-icon",children:e(z,{})}),middle:e("span",{className:"bp6-icon",children:e(G,{})}),bottom:e("span",{className:"bp6-icon",children:e(W,{})})};export const TextFontVariant=i(({elements:t,store:o})=>{const i=t[0];return n(a,{children:[e(u,{content:$("toolbar.textAlign"),position:d.BOTTOM,children:e(r,{minimal:!0,icon:"left"===i.align?e(y,{}):"center"===i.align?e(T,{}):"right"===i.align?e(v,{}):e(b,{}),onMouseDown:t=>{t.preventDefault()},onClick:()=>{const e=(ALIGN_OPTIONS.indexOf(i.align)+1+ALIGN_OPTIONS.length)%ALIGN_OPTIONS.length,n=ALIGN_OPTIONS[e];o.history.transaction(()=>{t.forEach(t=>{t.set({align:n})})})},"aria-label":$("toolbar.textAlign")})}),U.textVerticalResizeEnabled&&e(u,{content:$("toolbar.verticalAlign"),position:d.BOTTOM,children:e(r,{minimal:!0,icon:VERTICAL_ALIGN_ICONS[i.verticalAlign],onMouseDown:t=>{t.preventDefault()},onClick:()=>{const e=(VERTICAL_ALIGN_OPTIONS.indexOf(i.verticalAlign)+1+VERTICAL_ALIGN_OPTIONS.length)%VERTICAL_ALIGN_OPTIONS.length,n=VERTICAL_ALIGN_OPTIONS[e];o.history.transaction(()=>{t.forEach(t=>{t.set({verticalAlign:n})})})},"aria-label":$("toolbar.verticalAlign")})}),e(u,{content:$("toolbar.bold"),position:d.BOTTOM,children:e(r,{minimal:!0,icon:e(O,{}),active:"bold"===i.fontWeight||"700"===i.fontWeight,onMouseDown:t=>{t.preventDefault()},onClick:()=>{const e="bold"===i.fontWeight||"700"===i.fontWeight;o.history.transaction(()=>{t.forEach(t=>{e?t.set({fontWeight:"normal"}):t.set({fontWeight:"bold"})})})},"aria-label":$("toolbar.bold")})}),e(u,{content:$("toolbar.italic"),position:d.BOTTOM,children:e(r,{minimal:!0,icon:e(S,{}),active:"italic"===i.fontStyle,onMouseDown:t=>{t.preventDefault()},onClick:()=>{const e="italic"===i.fontStyle;o.history.transaction(()=>{t.forEach(t=>{e?t.set({fontStyle:"normal"}):t.set({fontStyle:"italic"})})})},"aria-label":$("toolbar.italic")})}),e(u,{content:$("toolbar.underline"),position:d.BOTTOM,children:e(r,{minimal:!0,icon:e(N,{}),active:i.textDecoration.indexOf("underline")>=0,onMouseDown:t=>{t.preventDefault()},onClick:()=>{let e=i.textDecoration.split(" ");e.indexOf("underline")>=0?e=e.filter(t=>"underline"!==t):e.push("underline"),o.history.transaction(()=>{t.forEach(t=>{t.set({textDecoration:e.join(" ")})})})},"aria-label":$("toolbar.underline")})}),e(u,{content:$("toolbar.strikethrough"),position:d.BOTTOM,children:e(r,{minimal:!0,icon:e(C,{}),active:i.textDecoration.indexOf("line-through")>=0,onMouseDown:t=>{t.preventDefault()},onClick:()=>{let e=i.textDecoration.split(" ");e.indexOf("line-through")>=0?e=e.filter(t=>"line-through"!==t):e.push("line-through"),o.history.transaction(()=>{t.forEach(t=>{t.set({textDecoration:e.join(" ")})})})},"aria-label":$("toolbar.strikethrough")})})]})});export const TextTransform=i(({elements:t,store:n})=>e(a,{children:e(u,{content:$("toolbar.uppercase"),children:e(r,{variant:"minimal",active:"uppercase"==t[0].textTransform,icon:e(B,{className:"bp6-icon"}),onMouseDown:t=>{t.preventDefault()},onClick:()=>{n.history.transaction(()=>{t.forEach(t=>{t.set({textTransform:"uppercase"===t.textTransform?"none":"uppercase"})})})}})})}));export const TextFill=i(({elements:t,store:n})=>e(L,{value:t[0].fill,style:{marginRight:"5px"},gradientEnabled:!0,onChange:e=>n.history.transaction(()=>{t.forEach(t=>{t.set({fill:e})})}),store:n}));export const NumberInput=n=>{var{value:i,onValueChange:r}=n,a=t(n,["value","onValueChange"]);const[l,s]=o.useState(i.toString());o.useEffect(()=>{s(i.toString())},[i]);const{min:c,max:m}=a,f="number"==typeof c||"number"==typeof m;return e(p,Object.assign({value:l,onValueChange:(t,e)=>{s(e),Number.isNaN(t)||"number"==typeof c&&t<c||"number"==typeof m&&t>m||r(t)},clampValueOnBlur:f},a))};export const TextSpacing=i(({elements:t,store:o})=>{const i=t[0],a=e=>{o.history.transaction(()=>{t.forEach(t=>{t.set(e)})})},l="number"==typeof i.lineHeight?100*i.lineHeight:120;return e(f,{position:d.BOTTOM,content:n("div",{style:{padding:"15px",width:"230px"},children:[n("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",paddingTop:"5px",paddingBottom:"5px"},children:[e("div",{children:$("toolbar.lineHeight")}),e("div",{children:e(NumberInput,{value:Math.round(l),onValueChange:t=>{a({lineHeight:t/100})},style:{width:"50px"},min:50,max:250,buttonPosition:"none"})})]}),e(h,{value:Math.round(l),onChange:t=>{a({lineHeight:t/100})},min:50,max:250,stepSize:1,showTrackFill:!1,labelRenderer:!1}),n("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",paddingTop:"5px",paddingBottom:"5px"},children:[e("div",{children:$("toolbar.letterSpacing")}),e("div",{children:e(NumberInput,{value:Math.round(100*i.letterSpacing),onValueChange:t=>{a({letterSpacing:t/100})},style:{width:"50px"},min:-50,max:250,buttonPosition:"none"})})]}),e(h,{value:Math.round(100*i.letterSpacing),onChange:t=>{a({letterSpacing:t/100})},min:-50,max:250,stepSize:1,showTrackFill:!1,labelRenderer:!1})]}),children:e(u,{content:$("toolbar.spacing"),position:d.BOTTOM,children:e(r,{icon:e(R,{className:"bp6-icon",style:{fontSize:"20px"}}),minimal:!0,"aria-label":$("toolbar.spacing")})})})});const tt={TextFontFamily,TextFontSize,TextFontVariant,TextTransform,TextFill,TextSpacing,TextAiWrite:P,TextFilters:_,TextAnimations:V};export const TextToolbar=i(({store:t,components:n})=>{const o=t.selectedElements,i=["TextFill","TextFontFamily","TextFontSize","TextFontVariant","TextSpacing","TextTransform","TextFilters",U.animationsEnabled&&"TextAnimations",U.aiTextEnabled&&"TextAiWrite"],r=k({type:"text",usedItems:i,components:n});return e(D,{items:r,itemRender:i=>{const r=n[i]||tt[i];return e(r,{elements:o,element:o[0],store:t},i)}})});export default TextToolbar;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function downScaleCanvas(t,e){const a=document.createElement("canvas"),{width:n,height:h}=function(t){return t instanceof HTMLVideoElement?{width:t.videoWidth,height:t.videoHeight}:{width:t.width,height:t.height}}(t);a.width=n,a.height=h;const o=a.getContext("2d");o.drawImage(t,0,0,n,h);const r=Math.max(1,Math.floor(n*e)),d=Math.max(1,Math.floor(h*e)),i=function(t,e,a){const n=new ImageData(e,a),h=new Int32Array(t.data.buffer),o=new Int32Array(n.data.buffer),r=e/t.width,d=a/t.height,i=Math.round(1/r),u=Math.round(1/d),c=i*u;for(let f=0;f<a;f++){for(let a=0;a<e;a++){const n=Math.round(a/r)+Math.round(f/d)*t.width;let g=0,w=0,M=0,l=0;for(let e=0;e<u;e++){for(let a=0;a<i;a++){const o=h[n+a+e*t.width],r=o>>>24;g+=(o<<24>>>24)*r,w+=(o<<16>>>24)*r,M+=(o<<8>>>24)*r,l+=r}}const s=l||1;g=Math.round(g/s),w=Math.round(w/s),M=Math.round(M/s),l=Math.round(l/c),o[a+f*e]=l<<24|M<<16|w<<8|g}}return n}(o.getImageData(0,0,n,h),r,d);return a.width=r,a.height=d,o.putImageData(i,0,0),a}
|
package/canvas/html-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,Fragment as e,jsxs as o}from"react/jsx-runtime";import r from"react";import{observer as n}from"mobx-react-lite";import{Group as i,Image as a,Path as s,Rect as l}from"react-konva";import{activeAnchorOf as c,scaledFontSizeAttrs as f,roundedFontSizeAttrs as d,roundedTextDecorationAttrs as h,textMinResizeWidth as u}from"../utils/px.js";import g from"konva";import{isAlive as m}from"mobx-state-tree";import{nodeAutorun as x}from"../utils/mobx.js";import{flags as p}from"../utils/flags.js";import{applyFilter as y}from"./apply-filters.js";import{getLineHeight as w,useFontFamiliesLoader as b,usePrevious as v}from"./text-element.js";import{getCurvePath as E}from"@polotno/core/text-html";import{useColor as S}from"./use-color.js";import{useWorkspaceStyle as O}from"./workspace-style.js";import{detectSize as k,detectLineRects as z,htmlToCanvas as F}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as j}from"@polotno/core/background-shape";import{useFadeIn as M}from"./use-fadein.js";import{Html as R}from"react-konva-utils";import{incrementLoader as X,triggerLoadError as Y}from"../utils/loader.js";import{removeTags as I}from"@polotno/core/text";import*as C from"../utils/fonts.js";import{isTouchDevice as P}from"../utils/screen.js";import{getLimitedFontSize as T}from"./text-element/max-font-size.js";import{visiblePlaceholder as B}from"./text-element/placeholder.js";import{useDelayer as A}from"./use-delayer.js";import{getHtml as W}from"@polotno/core/text-html";import{hasMeasuredHeight as $,resolveTextBoxHeight as D}from"@polotno/core/text-types";import{normalizeRichTextHtml as H,extractFontFamiliesFromHtml as _,scaleRichTextFontSizesInHtml as L}from"../utils/rich-text-html.js";import{findFitFontSize as J}from"./text-element/fit-font-size.js";import{RichEditor as N,RichTextContainer as q,applyOpacityToColor as V,quillRef as G,createQuill as K,setQuillContent as Q,setQuillFormats as U,syncQuillFormatState as Z,setCursorFromCoords as tt}from"./rich-editor.js";export{G as quillRef,K as createQuill,Q as setQuillContent,U as setQuillFormats,Z as syncQuillFormatState,tt as setCursorFromCoords};function et(t){const e=document.createElement("canvas"),o=e.getContext("2d");return null==o||o.drawImage(t,0,0,e.width,e.height),function(t){return!function(t){const e=t.getContext("2d").getImageData(0,0,t.width,t.height).data;for(let o=0;o<e.length;o+=4){if(0!==e[o+3]){return!0}}return!1}(t)}(e)}const ot=/^((?!chrome|android).)*safari/i.test(navigator.userAgent),rt=navigator.userAgent.includes("Firefox");export const HTMLElement=n(({element:n,store:G})=>{var K,Q;const{textOverflowIndicatorStyle:U}=O(),Z=r.useRef(null),[tt,nt]=r.useState(),[it,at]=r.useState(!1),[st,lt]=r.useState(!1),ct=r.useRef(n.height),ft=n.isSelected,dt=v(ft),ht=n.fontSize/3,{textVerticalResizeEnabled:ut}=p,gt=p.autoDeleteEmptyText,mt=v(n.fontFamily),xt=r.useMemo(()=>H(n.text,{fontSize:n.fontSize,fontFamily:n.fontFamily}),[n.text,n.fontSize,n.fontFamily]),pt=r.useMemo(()=>[...new Set([n.fontFamily,..._(xt)])].filter(Boolean),[n.fontFamily,xt]),[yt]=b(G,pt,n.fontStyle,n.fontWeight,n.text||n.placeholder),wt=n._editModeEnabled;M(Z);const bt=yt?n.fontFamily:mt!==n.fontFamily?mt:"Arial",vt=S(n).fill,Et=W(n,{fontFamily:bt,color:vt,textOverflow:p.textOverflow}),St=r.useRef([]),Ot=r.useRef(!0),[,kt]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=St.current;t.length>0&&(St.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{Ot.current=!1;const t=St.current;t.length>0&&(St.current=[],t.forEach(t=>t()))},[]);let{width:zt,height:Ft}=function(t,e,o){return r.useMemo(()=>k(t),[t,e.width,o])}(Et,n,yt);r.useEffect(()=>{if(!yt){return}const t=(t,e,o,r)=>{const i=X(`text ${n.id}`),a=()=>{G.history.ignore(t,e,o).then(()=>{Ot.current?(St.current.push(i),kt()):i()})};r?queueMicrotask(a):a()};if(n.curveEnabled&&Math.abs(zt-n.width)>1&&!it&&t(()=>{if(m(n)&&Math.abs(zt-n.width)>1){const t=n.x+n.width/2;n.set({width:zt,x:t-zt/2})}},!1,!0),!$(n)){return void t(()=>{m(n)&&($(n)||n.set({height:Ft}))},void 0,void 0,!0)}const{textOverflow:e}=p;if("change-font-size"!==e||it||wt||n.curveEnabled){"resize"===e&&(ut&&n.height<Ft&&!it&&t(()=>{m(n)&&n.set({height:Ft})},!1,!0),ut||n.height===Ft||it||t(()=>{m(n)&&n.set({height:Ft})},!1,!0))}else{const e=J(n,{fontFamily:n.fontFamily,textOverflow:p.textOverflow});e.fontSize!==n.fontSize?t(()=>{if(!m(n)){return}const t=f(n,e.fontSize);void 0!==e.text&&e.text!==n.text&&(t.text=e.text),n.set(t)},!1,!0):n.height!==Ft&&(ut&&n.height<Ft?t(()=>{m(n)&&n.set({height:Ft})},!1,!0):ut||t(()=>{m(n)&&n.set({height:Ft})},!1,!0))}});const jt=r.useMemo(()=>{const t={lastArgs:null,lastResult:null};return async function(e){return t.lastArgs&&t.lastResult&&(o=t.lastArgs,r=e,JSON.stringify(o)===JSON.stringify(r))||(t.lastResult=await F(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),Mt=r.useRef(0),Rt=r.useRef(null);r.useEffect(()=>{it||wt||(async()=>{Mt.current++;const t=Mt.current;let e=X(`text ${n.id} ${t}`);Rt.current&&Rt.current(),Rt.current=e,lt(!0);let o=null;const r=ot?5:1;for(let a=0;a<r;a++){const e=a>0?jt:F;try{if(o=await e({skipCache:a>0,html:Et,width:zt||n.width||1,height:D(n,Ft)||1,fontFamily:bt,fontFamilies:_(Et),resolveFont:t=>G.fonts.find(e=>e.fontFamily===t)||C.globalFonts.find(e=>e.fontFamily===t),padding:ht,pixelRatio:G._elementsPixelRatio,font:G.fonts.find(t=>t.fontFamily===bt)||C.globalFonts.find(t=>t.fontFamily===bt)}),t!==Mt.current){return}if((ot||rt)&&o&&et(o)){await new Promise(t=>setTimeout(t,50*(a+1)));continue}break}catch(i){console.error(i),Y(`Error rendering rich text with id ${n.id}`);break}}o?nt(o):e?(e(),e=null):console.error("Finish function is called twice!"),lt(!1)})()},[Et,it,Ft,wt,bt,n.height,G._elementsPixelRatio,yt]);const[Xt,Yt]=A(st,300),[It]=A(it,300,!0),Ct=It||Xt;r.useEffect(()=>{var t;if(!Ct){return x(n,()=>{const t=Z.current;y(t,n)})}null===(t=Z.current)||void 0===t||t.clearCache()},[tt,Ct,n.shadowColor,n.shadowOffsetX,n.shadowOffsetY,n.shadowOpacity]),r.useEffect(()=>{tt&&!st&&Rt.current&&(Rt.current(),Rt.current=null)},[tt,st]),r.useLayoutEffect(()=>{if(!yt){return}if(!n.curveEnabled){return}const t=new g.TextPath({data:E(n.a.width,n.a.height,n.curvePower,n.a.fontSize),text:I(n.text),letterSpacing:n.letterSpacing*n.a.fontSize,fontSize:n.a.fontSize,fontFamily:n.fontFamily,fontWeight:n.fontWeight,fontStyle:n.fontStyle,align:"center",textBaseline:"middle",fill:n.fill}),e=t.getSelfRect().width;if(e){const t=e-n.a.width,o=n.a.rotation*Math.PI/180,r=-t/2*Math.cos(o),i=-t/2*Math.sin(o);queueMicrotask(()=>{m(n)&&n.set({width:e,x:n.a.x+r,y:n.a.y+i})})}t.destroy()},[yt,n.curveEnabled,n.curvePower,n.text,n.fontSize,n.fontFamily,n.fontWeight,n.fontStyle,n.letterSpacing]),r.useEffect(()=>()=>{Rt.current&&Rt.current()},[]),r.useEffect(()=>{dt&&!ft&>&&""===I(n.text)&&n.removable&&!B(n.placeholder)&&G.deleteElements([n.id])},[gt,n.placeholder,n.removable,n.text,ft,dt]);const Pt=D(n,Ft);let Tt=0;"middle"===n.verticalAlign&&(Tt=(Pt-Ft)/2),"bottom"===n.verticalAlign&&(Tt=Pt-Ft);const Bt=w({fontLoaded:yt,fontFamily:n.fontFamily,fontSize:n.fontSize,lineHeight:n.lineHeight}),At=n.backgroundPadding*(n.fontSize*Bt*.5),Wt=n.backgroundCornerRadius*(n.fontSize*Bt*.5),$t=r.useMemo(()=>{if(!n.backgroundEnabled||n.curveEnabled||n.legacyBackground){return""}const t=z(Et);if(0===t.length){return""}const e=t.map(t=>({left:t.left+ht,right:t.right+ht,top:t.top+ht,bottom:t.bottom+ht}));return j({rects:e,padding:At,cornerRadius:Wt})},[n.backgroundEnabled,n.curveEnabled,n.legacyBackground,Et,yt,At,Wt,ht]),Dt=P(),Ht=E(n.a.width,Ft,n.curvePower,n.fontSize),_t=r.useRef(null),Lt=r.useRef(n.width),Jt=r.useRef(""),Nt=r.useRef(0),qt=r.useRef(0),Vt=r.useRef(null),Gt=wt&&n.strokeWidth>0&&!n.curveEnabled,Kt=zt>0?(n.a.width+2*ht)/(zt+2*ht):1;return o(e,{children:[n.curveEnabled||n.legacyBackground?t(l,{x:n.a.x,y:n.a.y,offsetX:At/Kt,offsetY:At/Kt,rotation:n.a.rotation,scaleX:Kt,scaleY:Kt,hideInExport:!n.showInExport,listening:!1,visible:n.backgroundEnabled,opacity:n.backgroundOpacity*n.a.opacity,fill:n.backgroundColor,width:n.width+2*At,height:Pt+2*At,cornerRadius:Wt}):t(s,{name:"html-background",x:n.a.x,y:n.a.y,offsetX:ht/Kt,offsetY:(ht-Tt)/Kt,rotation:n.a.rotation,scaleX:Kt,scaleY:Kt,hideInExport:!n.showInExport,listening:!1,visible:n.backgroundEnabled&&$t.length>0,opacity:n.backgroundOpacity*n.a.opacity,fill:n.backgroundColor,data:$t}),t(s,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,data:Ht,stroke:U.stroke,strokeWidth:1,visible:!1}),t(l,{ref:Z,name:"element",x:n.a.x,y:n.a.y,listening:n.selectable,rotation:n.a.rotation,width:n.a.width,height:n.a.height,visible:!Ct,draggable:Dt?n.draggable&&ft:n.draggable,preventDefault:!Dt||ft,opacity:wt?0:n.a.opacity,hideInExport:!n.showInExport,onDragMove:t=>{n.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{n.set({x:t.target.x(),y:t.target.y()})},id:n.id,onDblClick:t=>{n.contentEditable&&(Vt.current={x:t.evt.clientX,y:t.evt.clientY},n.toggleEditMode(!0))},onDblTap:t=>{var e;if(n.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Vt.current=o?{x:o.clientX,y:o.clientY}:null,n.toggleEditMode(!0)}},onTransformStart:t=>{at(!0),ct.current=n.height,Lt.current=n.width,_t.current=t.target.position(),Jt.current=c(t.target),Nt.current=n.a.rotation,qt.current=n.height},onTransform:t=>{const e=t.target,o=Jt.current,r="middle-left"===o||"middle-right"===o,i="top-center"===o||"bottom-center"===o,a=e.scaleX();if(r){const t=e.scaleX(),o=e.width()*t,r=u(Lt.current,n.fontSize);let i=o;o<r&&(i=r,_t.current&&e.position(_t.current)),e.width(i),e.scaleX(1),e.scaleY(1);const a=W(Object.assign(Object.assign({},n.toJSON()),{width:i}),{fontFamily:bt,color:vt,textOverflow:p.textOverflow}),s=k(a).height,l=p.textVerticalResizeEnabled?Math.max(s,ct.current):s;e.height(l),n.set({width:e.width(),height:l,x:e.x(),y:e.y()})}else if(i){const t="resize"===p.textOverflow,o="number"==typeof n.lineHeight?n.lineHeight:1,r=t?Ft:o*n.fontSize,i=e.height()*e.scaleY();if(i<r){if(e.height(r),_t.current){if("top-center"===Jt.current){const t=Nt.current*Math.PI/180,o=qt.current-r;e.x(_t.current.x-o*Math.sin(t)),e.y(_t.current.y+o*Math.cos(t))}else{e.position(_t.current)}}}else{e.height(i)}e.scaleY(1),e.scaleX(1),qt.current=Math.max(r,i),n.set({x:e.x(),y:e.y(),height:e.height(),rotation:e.rotation()})}else{e.scaleX(1),e.scaleY(1),n.set({fontSize:n.fontSize*a,letterSpacing:n.letterSpacing,width:e.width()*a,x:e.x(),y:e.y(),rotation:e.rotation(),height:e.height()*a,shadowBlur:n.shadowBlur*a,shadowOffsetX:n.shadowOffsetX*a,shadowOffsetY:n.shadowOffsetY*a,strokeWidth:n.strokeWidth*a,text:L(n.text,a)})}_t.current=t.target.position()},onTransformEnd:t=>{at(!1),lt(!0);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),n.set(Object.assign(Object.assign({},d(n,n.fontSize*e)),{width:t.target.width()*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),text:L(n.text,e)}))}}),t(a,{ref:Z,image:tt,x:n.a.x,y:n.a.y,offsetX:ht,offsetY:ht-Tt,listening:!1,rotation:n.a.rotation,width:n.a.width+2*ht,height:(n.a.width+2*ht)*((null!==(K=null==tt?void 0:tt.height)&&void 0!==K?K:0)/((null==tt?void 0:tt.width)||1)||1),visible:!Ct&&!wt,opacity:n.a.opacity,shadowEnabled:n.shadowEnabled,shadowBlur:n.shadowBlur,shadowOffsetX:n.shadowOffsetX,shadowOffsetY:n.shadowOffsetY,shadowColor:n.shadowColor,shadowOpacity:n.shadowOpacity,hideInExport:!n.showInExport,editModeEnabled:n._editModeEnabled||it||Ct}),(Ct&&!wt||Gt)&&t(i,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,offsetY:-Tt,children:t(R,{divProps:{style:{pointerEvents:"none"}},children:t(q,{dangerouslySetInnerHTML:{__html:Et},style:{pointerEvents:"none",opacity:n.a.opacity,textShadow:n.shadowEnabled?`${n.shadowOffsetX}px ${n.shadowOffsetY}px ${n.shadowBlur}px ${V(n.shadowColor,null!==(Q=n.shadowOpacity)&&void 0!==Q?Q:1)}`:void 0}})})}),wt&&t(i,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,offsetY:-Tt,children:t(R,{divProps:{style:{zIndex:20}},children:t(N,{element:n,onChange:t=>{const e=T({oldText:I(n.text),newText:I(t),element:n});n.set(Object.assign({text:t},f(n,e)))},onBlur:t=>{m(n)&&(n.set(h(n)),n.toggleEditMode(!1),Yt(!0))},clickCoords:Vt.current})})})]})});
|
|
1
|
+
import{jsx as t,Fragment as e,jsxs as o}from"react/jsx-runtime";import r from"react";import{observer as n}from"mobx-react-lite";import{Group as i,Image as a,Path as s,Rect as l}from"react-konva";import{activeAnchorOf as c,scaledFontSizeAttrs as f,roundedFontSizeAttrs as d,roundedTextDecorationAttrs as h,textMinResizeWidth as u}from"../utils/px.js";import g from"konva";import{isAlive as m}from"mobx-state-tree";import{nodeAutorun as x}from"../utils/mobx.js";import{flags as p}from"../utils/flags.js";import{applyFilter as y}from"./apply-filters.js";import{getLineHeight as w,useFontFamiliesLoader as b,usePrevious as v}from"./text-element.js";import{getCurvePath as E}from"@polotno/core/text-html";import{useColor as S}from"./use-color.js";import{useWorkspaceStyle as O}from"./workspace-style.js";import{detectSize as k,detectLineRects as z,htmlToCanvas as F}from"../utils/html2canvas.js";import{generateBackgroundShapeFromRects as j}from"@polotno/core/background-shape";import{useFadeIn as M}from"./use-fadein.js";import{Html as R}from"react-konva-utils";import{incrementLoader as X,triggerLoadError as Y}from"../utils/loader.js";import{removeTags as I}from"@polotno/core/text";import{cssFontFamily as C}from"@polotno/core/fonts";import*as P from"../utils/fonts.js";import{isTouchDevice as T}from"../utils/screen.js";import{getLimitedFontSize as B}from"./text-element/max-font-size.js";import{visiblePlaceholder as A}from"./text-element/placeholder.js";import{useDelayer as W}from"./use-delayer.js";import{getHtml as $}from"@polotno/core/text-html";import{hasMeasuredHeight as D,resolveTextBoxHeight as H}from"@polotno/core/text-types";import{normalizeRichTextHtml as _,extractFontFamiliesFromHtml as L,scaleRichTextFontSizesInHtml as J}from"../utils/rich-text-html.js";import{findFitFontSize as N}from"./text-element/fit-font-size.js";import{RichEditor as q,RichTextContainer as V,applyOpacityToColor as G,quillRef as K,createQuill as Q,setQuillContent as U,setQuillFormats as Z,syncQuillFormatState as tt,setCursorFromCoords as et}from"./rich-editor.js";export{K as quillRef,Q as createQuill,U as setQuillContent,Z as setQuillFormats,tt as syncQuillFormatState,et as setCursorFromCoords};function ot(t){const e=document.createElement("canvas"),o=e.getContext("2d");return null==o||o.drawImage(t,0,0,e.width,e.height),function(t){return!function(t){const e=t.getContext("2d").getImageData(0,0,t.width,t.height).data;for(let o=0;o<e.length;o+=4){if(0!==e[o+3]){return!0}}return!1}(t)}(e)}const rt=/^((?!chrome|android).)*safari/i.test(navigator.userAgent),nt=navigator.userAgent.includes("Firefox");export const HTMLElement=n(({element:n,store:K})=>{var Q,U;const{textOverflowIndicatorStyle:Z}=O(),tt=r.useRef(null),[et,it]=r.useState(),[at,st]=r.useState(!1),[lt,ct]=r.useState(!1),ft=r.useRef(n.height),dt=n.isSelected,ht=v(dt),ut=n.fontSize/3,{textVerticalResizeEnabled:gt}=p,mt=p.autoDeleteEmptyText,xt=v(n.fontFamily),pt=r.useMemo(()=>_(n.text,{fontSize:n.fontSize,fontFamily:n.fontFamily}),[n.text,n.fontSize,n.fontFamily]),yt=r.useMemo(()=>[...new Set([n.fontFamily,...L(pt)])].filter(Boolean),[n.fontFamily,pt]),[wt]=b(K,yt,n.fontStyle,n.fontWeight,n.text||n.placeholder),bt=n._editModeEnabled;M(tt);const vt=wt?n.fontFamily:xt!==n.fontFamily?xt:"Arial",Et=S(n).fill,St=$(n,{fontFamily:vt,color:Et,textOverflow:p.textOverflow}),Ot=r.useRef([]),kt=r.useRef(!0),[,zt]=r.useReducer(t=>t+1,0);r.useEffect(()=>{const t=Ot.current;t.length>0&&(Ot.current=[],t.forEach(t=>t()))}),r.useEffect(()=>()=>{kt.current=!1;const t=Ot.current;t.length>0&&(Ot.current=[],t.forEach(t=>t()))},[]);let{width:Ft,height:jt}=function(t,e,o){return r.useMemo(()=>k(t),[t,e.width,o])}(St,n,wt);r.useEffect(()=>{if(!wt){return}const t=(t,e,o,r)=>{const i=X(`text ${n.id}`),a=()=>{K.history.ignore(t,e,o).then(()=>{kt.current?(Ot.current.push(i),zt()):i()})};r?queueMicrotask(a):a()};if(n.curveEnabled&&Math.abs(Ft-n.width)>1&&!at&&t(()=>{if(m(n)&&Math.abs(Ft-n.width)>1){const t=n.x+n.width/2;n.set({width:Ft,x:t-Ft/2})}},!1,!0),!D(n)){return void t(()=>{m(n)&&(D(n)||n.set({height:jt}))},void 0,void 0,!0)}const{textOverflow:e}=p;if("change-font-size"!==e||at||bt||n.curveEnabled){"resize"===e&&(gt&&n.height<jt&&!at&&t(()=>{m(n)&&n.set({height:jt})},!1,!0),gt||n.height===jt||at||t(()=>{m(n)&&n.set({height:jt})},!1,!0))}else{const e=N(n,{fontFamily:n.fontFamily,textOverflow:p.textOverflow});e.fontSize!==n.fontSize?t(()=>{if(!m(n)){return}const t=f(n,e.fontSize);void 0!==e.text&&e.text!==n.text&&(t.text=e.text),n.set(t)},!1,!0):n.height!==jt&&(gt&&n.height<jt?t(()=>{m(n)&&n.set({height:jt})},!1,!0):gt||t(()=>{m(n)&&n.set({height:jt})},!1,!0))}});const Mt=r.useMemo(()=>{const t={lastArgs:null,lastResult:null};return async function(e){return t.lastArgs&&t.lastResult&&(o=t.lastArgs,r=e,JSON.stringify(o)===JSON.stringify(r))||(t.lastResult=await F(e),t.lastArgs=Object.assign({},e)),t.lastResult;var o,r}},[]),Rt=r.useRef(0),Xt=r.useRef(null);r.useEffect(()=>{at||bt||(async()=>{Rt.current++;const t=Rt.current;let e=X(`text ${n.id} ${t}`);Xt.current&&Xt.current(),Xt.current=e,ct(!0);let o=null;const r=rt?5:1;for(let a=0;a<r;a++){const e=a>0?Mt:F;try{if(o=await e({skipCache:a>0,html:St,width:Ft||n.width||1,height:H(n,jt)||1,fontFamily:vt,fontFamilies:L(St),resolveFont:t=>K.fonts.find(e=>e.fontFamily===t)||P.globalFonts.find(e=>e.fontFamily===t),padding:ut,pixelRatio:K._elementsPixelRatio,font:K.fonts.find(t=>t.fontFamily===vt)||P.globalFonts.find(t=>t.fontFamily===vt)}),t!==Rt.current){return}if((rt||nt)&&o&&ot(o)){await new Promise(t=>setTimeout(t,50*(a+1)));continue}break}catch(i){console.error(i),Y(`Error rendering rich text with id ${n.id}`);break}}o?it(o):e?(e(),e=null):console.error("Finish function is called twice!"),ct(!1)})()},[St,at,jt,bt,vt,n.height,K._elementsPixelRatio,wt]);const[Yt,It]=W(lt,300),[Ct]=W(at,300,!0),Pt=Ct||Yt;r.useEffect(()=>{var t;if(!Pt){return x(n,()=>{const t=tt.current;y(t,n)})}null===(t=tt.current)||void 0===t||t.clearCache()},[et,Pt,n.shadowColor,n.shadowOffsetX,n.shadowOffsetY,n.shadowOpacity]),r.useEffect(()=>{et&&!lt&&Xt.current&&(Xt.current(),Xt.current=null)},[et,lt]),r.useLayoutEffect(()=>{if(!wt){return}if(!n.curveEnabled){return}const t=new g.TextPath({data:E(n.a.width,n.a.height,n.curvePower,n.a.fontSize),text:I(n.text),letterSpacing:n.letterSpacing*n.a.fontSize,fontSize:n.a.fontSize,fontFamily:C(n.fontFamily),fontWeight:n.fontWeight,fontStyle:n.fontStyle,align:"center",textBaseline:"middle",fill:n.fill}),e=t.getSelfRect().width;if(e){const t=e-n.a.width,o=n.a.rotation*Math.PI/180,r=-t/2*Math.cos(o),i=-t/2*Math.sin(o);queueMicrotask(()=>{m(n)&&n.set({width:e,x:n.a.x+r,y:n.a.y+i})})}t.destroy()},[wt,n.curveEnabled,n.curvePower,n.text,n.fontSize,n.fontFamily,n.fontWeight,n.fontStyle,n.letterSpacing]),r.useEffect(()=>()=>{Xt.current&&Xt.current()},[]),r.useEffect(()=>{ht&&!dt&&mt&&""===I(n.text)&&n.removable&&!A(n.placeholder)&&K.deleteElements([n.id])},[mt,n.placeholder,n.removable,n.text,dt,ht]);const Tt=H(n,jt);let Bt=0;"middle"===n.verticalAlign&&(Bt=(Tt-jt)/2),"bottom"===n.verticalAlign&&(Bt=Tt-jt);const At=w({fontLoaded:wt,fontFamily:n.fontFamily,fontSize:n.fontSize,lineHeight:n.lineHeight}),Wt=n.backgroundPadding*(n.fontSize*At*.5),$t=n.backgroundCornerRadius*(n.fontSize*At*.5),Dt=r.useMemo(()=>{if(!n.backgroundEnabled||n.curveEnabled||n.legacyBackground){return""}const t=z(St);if(0===t.length){return""}const e=t.map(t=>({left:t.left+ut,right:t.right+ut,top:t.top+ut,bottom:t.bottom+ut}));return j({rects:e,padding:Wt,cornerRadius:$t})},[n.backgroundEnabled,n.curveEnabled,n.legacyBackground,St,wt,Wt,$t,ut]),Ht=T(),_t=E(n.a.width,jt,n.curvePower,n.fontSize),Lt=r.useRef(null),Jt=r.useRef(n.width),Nt=r.useRef(""),qt=r.useRef(0),Vt=r.useRef(0),Gt=r.useRef(null),Kt=bt&&n.strokeWidth>0&&!n.curveEnabled,Qt=Ft>0?(n.a.width+2*ut)/(Ft+2*ut):1;return o(e,{children:[n.curveEnabled||n.legacyBackground?t(l,{x:n.a.x,y:n.a.y,offsetX:Wt/Qt,offsetY:Wt/Qt,rotation:n.a.rotation,scaleX:Qt,scaleY:Qt,hideInExport:!n.showInExport,listening:!1,visible:n.backgroundEnabled,opacity:n.backgroundOpacity*n.a.opacity,fill:n.backgroundColor,width:n.width+2*Wt,height:Tt+2*Wt,cornerRadius:$t}):t(s,{name:"html-background",x:n.a.x,y:n.a.y,offsetX:ut/Qt,offsetY:(ut-Bt)/Qt,rotation:n.a.rotation,scaleX:Qt,scaleY:Qt,hideInExport:!n.showInExport,listening:!1,visible:n.backgroundEnabled&&Dt.length>0,opacity:n.backgroundOpacity*n.a.opacity,fill:n.backgroundColor,data:Dt}),t(s,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,data:_t,stroke:Z.stroke,strokeWidth:1,visible:!1}),t(l,{ref:tt,name:"element",x:n.a.x,y:n.a.y,listening:n.selectable,rotation:n.a.rotation,width:n.a.width,height:n.a.height,visible:!Pt,draggable:Ht?n.draggable&&dt:n.draggable,preventDefault:!Ht||dt,opacity:bt?0:n.a.opacity,hideInExport:!n.showInExport,onDragMove:t=>{n.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{n.set({x:t.target.x(),y:t.target.y()})},id:n.id,onDblClick:t=>{n.contentEditable&&(Gt.current={x:t.evt.clientX,y:t.evt.clientY},n.toggleEditMode(!0))},onDblTap:t=>{var e;if(n.contentEditable){const o=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Gt.current=o?{x:o.clientX,y:o.clientY}:null,n.toggleEditMode(!0)}},onTransformStart:t=>{st(!0),ft.current=n.height,Jt.current=n.width,Lt.current=t.target.position(),Nt.current=c(t.target),qt.current=n.a.rotation,Vt.current=n.height},onTransform:t=>{const e=t.target,o=Nt.current,r="middle-left"===o||"middle-right"===o,i="top-center"===o||"bottom-center"===o,a=e.scaleX();if(r){const t=e.scaleX(),o=e.width()*t,r=u(Jt.current,n.fontSize);let i=o;o<r&&(i=r,Lt.current&&e.position(Lt.current)),e.width(i),e.scaleX(1),e.scaleY(1);const a=$(Object.assign(Object.assign({},n.toJSON()),{width:i}),{fontFamily:vt,color:Et,textOverflow:p.textOverflow}),s=k(a).height,l=p.textVerticalResizeEnabled?Math.max(s,ft.current):s;e.height(l),n.set({width:e.width(),height:l,x:e.x(),y:e.y()})}else if(i){const t="resize"===p.textOverflow,o="number"==typeof n.lineHeight?n.lineHeight:1,r=t?jt:o*n.fontSize,i=e.height()*e.scaleY();if(i<r){if(e.height(r),Lt.current){if("top-center"===Nt.current){const t=qt.current*Math.PI/180,o=Vt.current-r;e.x(Lt.current.x-o*Math.sin(t)),e.y(Lt.current.y+o*Math.cos(t))}else{e.position(Lt.current)}}}else{e.height(i)}e.scaleY(1),e.scaleX(1),Vt.current=Math.max(r,i),n.set({x:e.x(),y:e.y(),height:e.height(),rotation:e.rotation()})}else{e.scaleX(1),e.scaleY(1),n.set({fontSize:n.fontSize*a,letterSpacing:n.letterSpacing,width:e.width()*a,x:e.x(),y:e.y(),rotation:e.rotation(),height:e.height()*a,shadowBlur:n.shadowBlur*a,shadowOffsetX:n.shadowOffsetX*a,shadowOffsetY:n.shadowOffsetY*a,strokeWidth:n.strokeWidth*a,text:J(n.text,a)})}Lt.current=t.target.position()},onTransformEnd:t=>{st(!1),ct(!0);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),n.set(Object.assign(Object.assign({},d(n,n.fontSize*e)),{width:t.target.width()*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),text:J(n.text,e)}))}}),t(a,{ref:tt,image:et,x:n.a.x,y:n.a.y,offsetX:ut,offsetY:ut-Bt,listening:!1,rotation:n.a.rotation,width:n.a.width+2*ut,height:(n.a.width+2*ut)*((null!==(Q=null==et?void 0:et.height)&&void 0!==Q?Q:0)/((null==et?void 0:et.width)||1)||1),visible:!Pt&&!bt,opacity:n.a.opacity,shadowEnabled:n.shadowEnabled,shadowBlur:n.shadowBlur,shadowOffsetX:n.shadowOffsetX,shadowOffsetY:n.shadowOffsetY,shadowColor:n.shadowColor,shadowOpacity:n.shadowOpacity,hideInExport:!n.showInExport,editModeEnabled:n._editModeEnabled||at||Pt}),(Pt&&!bt||Kt)&&t(i,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,offsetY:-Bt,children:t(R,{divProps:{style:{pointerEvents:"none"}},children:t(V,{dangerouslySetInnerHTML:{__html:St},style:{pointerEvents:"none",opacity:n.a.opacity,textShadow:n.shadowEnabled?`${n.shadowOffsetX}px ${n.shadowOffsetY}px ${n.shadowBlur}px ${G(n.shadowColor,null!==(U=n.shadowOpacity)&&void 0!==U?U:1)}`:void 0}})})}),bt&&t(i,{x:n.a.x,y:n.a.y,rotation:n.a.rotation,offsetY:-Bt,children:t(R,{divProps:{style:{zIndex:20}},children:t(q,{element:n,onChange:t=>{const e=B({oldText:I(n.text),newText:I(t),element:n});n.set(Object.assign({text:t},f(n,e)))},onBlur:t=>{m(n)&&(n.set(h(n)),n.toggleEditMode(!1),It(!0))},clickCoords:Gt.current})})})]})});
|
|
@@ -18,7 +18,7 @@ export declare const useCornerRadiusAndCrop: (element: ImageElementType, image:
|
|
|
18
18
|
y: number;
|
|
19
19
|
width: number;
|
|
20
20
|
height: number;
|
|
21
|
-
}, pixelRatio: number, cornerRadius?: number, skipDownscale?: boolean, isVector?: boolean) => HTMLCanvasElement | HTMLImageElement | undefined;
|
|
21
|
+
}, pixelRatio: number, cornerRadius?: number, skipDownscale?: boolean, isVector?: boolean, borderInset?: number) => HTMLCanvasElement | HTMLImageElement | undefined;
|
|
22
22
|
type ImageOrSVGElementType = ImageElementType & {
|
|
23
23
|
maskSrc?: string;
|
|
24
24
|
colorsReplace?: Map<string, string>;
|
package/canvas/image-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import o from"react";import{observer as i}from"mobx-react-lite";import{isAlive as a}from"mobx-state-tree";import{Arc as n,Group as h,Image as s,Rect as c,Text as d,Transformer as g}from"react-konva";import{ROLES as l}from"../model/store.js";import u from"use-image";import p from"konva";import{Portal as m}from"react-konva-utils";import{useWorkspaceStyle as f}from"./workspace-style.js";import{incrementLoader as w,triggerLoadError as x}from"../utils/loader.js";import{polotnoError as M}from"@polotno/core/errors";import{getCropRect as y}from"@polotno/core/image";import*as v from"@polotno/core/svg";import{flags as b}from"../utils/flags.js";import{trySetCanvasSize as E}from"../utils/canvas.js";import{roundEnabledPx as S,activeAnchorOf as I,isCornerAnchor as C,scaledBoxStyleAttrs as R,roundedBoxStyleAttrs as X}from"../utils/px.js";import{useFilteredSource as Y}from"./use-filtered-source.js";import{useFadeIn as O}from"./use-fadein.js";import{isTouchDevice as T}from"../utils/screen.js";import{useDelayer as j}from"./use-delayer.js";import{useColor as D}from"./use-color.js";function W(){return document.createElement("canvas")}const z=t=>t.indexOf("data:image/svg+xml")>=0||t.indexOf(".svg")>=0;export const useSizeFixer=t=>{const[e,r]=o.useState(t);return o.useEffect(()=>{(async()=>{const o=await async function(t){if(!z(t)){return t}const e=await v.urlToString(t),r=v.fixSize(e);return v.svgToURL(r)}(t);o!==e&&r(o)})()},[t]),e};function k(t,e){return{x:(t.x+e.x)/2,y:(t.y+e.y)/2}}const A=(t,e)=>e.width<5||e.height<5?t:e,L={keepRatio:!1,rotateEnabled:!1,boundBoxFunc:A},H={enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],rotateEnabled:!1,borderEnabled:!1,boundBoxFunc:A};export function maskCompositeSize(t,e){const r=y({stretchEnabled:e.stretchEnabled,cropX:0,cropY:0,cropWidth:1,cropHeight:1,width:Math.max(e.width,1),height:Math.max(e.height,1)},t);return{width:Math.max(Math.round(r.width),1),height:Math.max(Math.round(r.height),1)}}const _=(t,e,r)=>Math.max(e,Math.min(r,t));export const useCornerRadiusAndCrop=(t,e,r,i,a=0,n=!1,h=!0)=>{var s,c;const d=Math.floor(_(t.a.width*i,1,1e4)),g=Math.floor(_(t.a.height*i,1,1e4)),l=Math.min(a*i,d/2,g/2),u=Math.max(t.a.width/r.width,t.a.height/r.height)*i,m=(null===(s=t.page)||void 0===s?void 0:s._exportingOrRendering)&&b.imageDownScalingEnabled&&u<1&&!n,f=0===r.x&&0===r.y&&r.width===(null==e?void 0:e.width)&&r.height===(null==e?void 0:e.height),w=o.useMemo(()=>{if(e&&e.width&&e.height){return f&&0===l&&!m?void 0:W()}},[e,l,m,f]);return o.useLayoutEffect(()=>{if(!w||!e){return}E(w,d,g);const t=w.getContext("2d");if(!t){return}l&&(t.beginPath(),t.moveTo(l,0),t.lineTo(d-l,0),t.arc(d-l,l,l,3*Math.PI/2,0,!1),t.lineTo(d,g-l),t.arc(d-l,g-l,l,0,Math.PI/2,!1),t.lineTo(l,g),t.arc(l,g-l,l,Math.PI/2,Math.PI,!1),t.lineTo(0,l),t.arc(l,l,l,Math.PI,3*Math.PI/2,!1),t.clip());const o=m?function(t,e){var r,o;const i=W();i.width=t.width,i.height=t.height;const a=Math.max(1,Math.floor(i.width*e)),n=Math.max(1,Math.floor(i.height*e));null===(r=i.getContext("2d"))||void 0===r||r.drawImage(t,0,0,i.width,i.height);const h=function(t,e,r,o,i,a,n){for(var h=new ImageData(e,r),s=new Int32Array(t.data.buffer),c=t.width,d=new Int32Array(h.data.buffer),g=h.width,l=e/a,u=r/n,p=Math.round(1/l),m=Math.round(1/u),f=p*m,w=0;w<h.height;w++){for(var x=0;x<g;x++){for(var M=0+Math.round(x/l)+(0+Math.round(w/u))*c,y=0,v=0,b=0,E=0,S=0;S<m;S++){for(var I=0;I<p;I++){var C=s[M+I+S*c];y+=C<<24>>>24,v+=C<<16>>>24,b+=C<<8>>>24,E+=C>>>24}}y=Math.round(y/f),v=Math.round(v/f),b=Math.round(b/f),E=Math.round(E/f),d[x+w*g]=E<<24|b<<16|v<<8|y}}return h}(i.getContext("2d").getImageData(0,0,i.width,i.height),a,n,0,0,i.width,i.height);return i.width=a,i.height=n,null===(o=i.getContext("2d"))||void 0===o||o.putImageData(h,0,0),i}(e,u):e,i=m?{x:Math.floor(r.x*u),y:Math.floor(r.y*u),width:Math.floor(r.width*u),height:Math.floor(r.height*u)}:r;t.drawImage(o,i.x,i.y,i.width,i.height,0,0,w.width,w.height)},[w,t.a.width,t.a.height,r.x,r.y,r.width,r.height,a,i,n,null===(c=t.page)||void 0===c?void 0:c._exportingOrRendering,m]),o.useEffect(()=>()=>{w&&"CANVAS"===w.nodeName&&p.Util.releaseCanvas(w)},[w]),w||e};const P=W(),F=i(({element:r})=>{const i=Math.min(30,r.a.width/4,r.a.height/4),a=o.useRef(null);o.useEffect(()=>{const t=a.current;if(!t){return}const e=new p.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}});const{mediaLoadingStyle:s}=f();return e(h,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:s.fill}),t(n,{ref:a,x:r.a.width/2,y:r.a.height/2,fill:s.textFill,outerRadius:Math.abs(i),innerRadius:Math.max(1,i-5),angle:270})]})}),N=i(({element:r})=>{const{mediaErrorStyle:o}=f(),i=Math.max(10,Math.min(30,r.a.width/25));return e(h,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:o.fill}),t(d,{text:"Can not load the image...",fontSize:i,width:r.a.width,height:r.a.height,align:"center",fill:o.textFill,verticalAlign:"middle",padding:5})]})});let B=u;export const setImageLoaderHook=t=>{B=t};export const useImageLoader=(t,e="",r="")=>{const i=o.useRef(void 0),a=()=>{var t;null===(t=i.current)||void 0===t||t.call(i),i.current=void 0};o.useEffect(()=>a,[]),o.useLayoutEffect(()=>{const o=e.slice(0,200),n=`image with id ${r} url: ${o}`;"loading"!==t||i.current||(i.current=w(n)),"loading"!==t&&a(),"failed"===t&&x(M("IMAGE_FAILED",n,{src:o,elementId:r,reason:"load"}))},[t])};export const ImageElement=i(({element:i,store:n})=>{var d,u,w;const x=f(),[M,b]=o.useState(!1),A=o.useRef(null),_=o.useRef(null),U=i.isSelected,[V,$]=(t=>{const[e,r]=o.useReducer(t=>t+1,0),i=o.useRef("loading"),a=o.useRef(t.src),n=o.useRef(t.src);n.current!==t.src&&(n.current=t.src,i.current="loading");const h=o.useMemo(()=>z(t.src)||"svg"===t.type,[t.src,t.type]);return o.useEffect(()=>{if(!h){return}if(!t.src){return}let e=!1;return(async()=>{i.current="loading",r();const o=await v.urlToString(t.src),n=v.fixSize(o);let h;h=t.colorsReplace?v.replaceColors(n,t.colorsReplace||new Map):v.svgToURL(n),e||(a.current=h,i.current="loaded",r())})(),()=>{e=!0}},[t.src,JSON.stringify(t.colorsReplace)]),h?[a.current,i.current]:[t.src,"loaded"]})(i),[q,G]=B(V,"anonymous"),J="svg"!==i.type||"loaded"===$?G:"loading";useImageLoader(J,i.src,i.id);const K=(null===(d=i.page)||void 0===d?void 0:d._exportingOrRendering)?1:Math.max(1,n.scale),Q=n._elementsPixelRatio*K,Z=(({image:t,status:e,type:r})=>{const i=o.useRef(void 0);return o.useEffect(()=>{i.current=t||i.current},[t]),"failed"!==e||"failed"!==e&&"svg"===r?i.current:void 0})({image:q,status:G,type:i.type}),tt=((t,e,r)=>{const i=o.useMemo(()=>{var o,i;const{flipX:a,flipY:n}=t,h="svg"===t.type||t.src.indexOf("data:image/svg+xml")>=0||t.src.indexOf(".svg")>=0,s=navigator.userAgent.toLowerCase().indexOf("firefox")>-1,c=(/^((?!chrome|android).)*safari/i.test(navigator.userAgent)||s)&&h||t.maskSrc;if(!a&&!n&&!c){return e}if(!e||!e.width||!e.height){return null}const d=W();let g=1;"svg"===t.type&&(g=Math.max(t.a.width/e.width*r,t.a.height/e.height*r)),E(d,Math.max(e.width*g,1),Math.max(e.height*g,1));let l=a?-d.width:0,u=n?-d.height:0;return null===(o=d.getContext("2d"))||void 0===o||o.scale(a?-1:1,n?-1:1),null===(i=d.getContext("2d"))||void 0===i||i.drawImage(e,l,u,d.width,d.height),d},[t.maskSrc,t.flipX,t.flipY,e,t.a.width,t.a.height,r]);return o.useEffect(()=>()=>{i&&"CANVAS"===i.nodeName&&p.Util.releaseCanvas(i)},[i]),i})(i,q||Z,Q),et=((t,e)=>{const r=useSizeFixer(t.maskSrc||""),[i,a]=B(r,"anonymous");return useImageLoader(r?a:"loaded",r||"",t.id),o.useMemo(()=>{if(!i){return e}if(!e||!e.width||!e.height){return e}const{width:r,height:o}=maskCompositeSize(e,{width:t.a.width,height:t.a.height,stretchEnabled:t.stretchEnabled}),a=W();a.width=r,a.height=o;const n=a.getContext("2d");if(!n){return e}n.drawImage(e,0,0,r,o,0,0,r,o),n.globalCompositeOperation="source-in";const h=function(t,e){const r=e.width/e.height;let o,i;return r>=t.width/t.height?(o=t.width,i=t.width/r):(o=t.height*r,i=t.height),{x:(t.width-o)/2,y:(t.height-i)/2,width:o,height:i}}(i,t);return n.drawImage(i,h.x,h.y,h.width,h.height,0,0,r,o),a},[e,i,t.a.width,t.a.height,t.stretchEnabled])})(i,tt)||P,[rt=P]=Y(i,et);let{cropX:ot,cropY:it,cropWidth:at,cropHeight:nt}=i.a;"loaded"!==G&&(ot=it=0,at=nt=1);const ht=y({stretchEnabled:i.stretchEnabled,cropX:ot,cropY:it,cropWidth:at,cropHeight:nt,width:i.a.width,height:i.a.height},rt),st=ht.width,ct=ht.height,dt=i.stretchEnabled,gt=null!==(u=i.cornerRadius)&&void 0!==u?u:0,lt=(null===(w=i.page)||void 0===w?void 0:w._exportingOrRendering)?1:Math.min(2,n.scale),ut=n._elementsPixelRatio*lt;let pt=((t,e,r,i)=>{const a=useSizeFixer(t.clipSrc||""),[n,h]=B(a,"anonymous"),s=t.clipSrc?h:"loaded";useImageLoader(s,t.clipSrc,t.id);const c=o.useMemo(()=>{if(e&&n){return W()}},[e,n]);return o.useLayoutEffect(()=>{var o;if(!n){return}if(!e||!e.width||!e.height){return}if(!n||!n.width||!n.height){return}if(!c){return}const i=W(),a=Math.max(t.a.width/n.width*r,t.a.height/n.height*r);i.width=Math.max(n.width*a,1),i.height=Math.max(n.height*a,1),null===(o=i.getContext("2d"))||void 0===o||o.drawImage(n,0,0,i.width,i.height),c.width=Math.max(e.width,1),c.height=Math.max(e.height,1);const h=c.getContext("2d");h&&(h.save(),h.drawImage(i,0,0,e.width,e.height),p.Util.releaseCanvas(i),h.globalCompositeOperation="source-in",h.drawImage(e,0,0,c.width,c.height),h.restore())},[c,e,n,t.a.width,t.a.height,r,...i]),t.clipSrc&&n?c:e})(i,useCornerRadiusAndCrop(i,rt,ht,ut,gt,M||i._cropModeEnabled||"svg"===i.type,!0),ut,[ht,gt,ut]);const mt=Math.max(i.a.width/st,i.a.height/ct);o.useEffect(()=>{var t;if(!i._cropModeEnabled){return}const e=null===(t=A.current)||void 0===t?void 0:t.getStage();function r(t){i._cropModeEnabled&&t.target!==_.current&&i.toggleCropMode(!1)}function o(t){i._cropModeEnabled&&t.target.parentNode!==(null==e?void 0:e.content)&&i.toggleCropMode(!1)}return document.body.addEventListener("click",o),null==e||e.on("click",r),null==e||e.on("tap",r),()=>{document.body.removeEventListener("click",o),document.body.removeEventListener("touchstart",o),null==e||e.off("click",r),null==e||e.off("click",r)}},[i._cropModeEnabled]);const ft=o.useRef(null),wt=o.useRef(null),xt=o.useRef(null);o.useLayoutEffect(()=>{i._cropModeEnabled&&(wt.current.nodes([ft.current]),xt.current.nodes([_.current]))},[i._cropModeEnabled]);var Mt=o.useRef(null),yt=o.useRef(0),vt=o.useRef(!1);const bt=t=>{var e;(null===(e=t.evt.touches)||void 0===e?void 0:e.length)>2&&t.target.stopDrag(),Math.round(t.target.x())>0&&(t.target.x(0),t.target.scaleX(1)),Math.round(t.target.y())>0&&(t.target.y(0),t.target.scaleY(1));const r=t.target.width()*t.target.scaleX(),o=t.target.height()*t.target.scaleY(),a=Math.min(1,st/r),n=Math.min(1,ct/o),h=1-a,s=Math.min(h,Math.max(0,Math.round(-t.target.x())/r)),c=1-n,d=Math.min(c,Math.max(0,Math.round(-t.target.y())/o));t.target.setAttrs({x:-s*rt.width,y:-d*rt.height,scaleX:1,scaleY:1}),i.set({cropX:s,cropY:d,cropWidth:a,cropHeight:n})},Et=()=>{"svg"!==i.type&&i.contentEditable&&(i.stretchEnabled||setTimeout(()=>{a(i)&&i.toggleCropMode(!0)}))},St="svg"===i.type&&Z,It="loading"===G&&!St,[Ct]=j(It,100,!1,!1),Rt="failed"===G,Xt=!Ct&&!Rt,Yt=o.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Ot=o.useRef(!1),Tt=Xt?i.a.opacity:0;O(A,Tt);const jt=i.selectable||n.role===l.ADMIN,Dt=T(),Wt=D(i,i.borderColor,"stroke");return e(r,{children:[Ct&&t(F,{element:i}),Rt&&t(N,{element:i}),t(s,{ref:A,name:"element",id:i.id,image:pt,x:i.a.x,y:i.a.y,width:i.a.width||1,height:i.a.height||1,rotation:i.a.rotation,opacity:Tt,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,customCrop:ht,listening:jt,draggable:Dt?i.draggable&&U:i.draggable,preventDefault:!Dt||U,hideInExport:!i.showInExport,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDblClick:Et,onDblTap:Et,onTransformStart:t=>{b(!0),Ot.current=C(I(t.target)),Yt.current={cropX:i.cropX,cropY:i.cropY,cropWidth:i.cropWidth,cropHeight:i.cropHeight}},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),o=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const a=1-st/rt.width,n=Math.min(a,Math.max(0,i.cropX)),h=1-ct/rt.height,s=Math.min(h,Math.max(0,i.cropY)),c=Ot.current,d=!c&&r<1&&Yt.current.cropHeight>ct/rt.height;let g=c?i.cropWidth:i.cropWidth*r;d&&(g=i.cropWidth);const l=!c&&o<1&&Yt.current.cropWidth>st/rt.width;let u=c?i.cropHeight:i.cropHeight*o;l&&(u=i.cropHeight),dt&&(g=i.cropWidth,u=i.cropHeight);const p=c?Math.min(r,o):1;i.set(Object.assign({cropX:n,cropY:s,x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*o,rotation:t.target.rotation(),cropWidth:Math.min(g,1-n),cropHeight:Math.min(u,1-s)},1!==p&&Object.assign(Object.assign({},R(i,p)),{borderSize:i.borderSize*p})))},onTransformEnd:t=>{const e=t.currentTarget;i.set(Object.assign(Object.assign({width:e.width(),height:e.height(),x:e.x(),y:e.y(),rotation:t.target.rotation(),cropWidth:st/rt.width,cropHeight:ct/rt.height},X(i)),{borderSize:S(i.borderSize)})),b(!1)}}),t(c,Object.assign({x:i.x,y:i.y,width:Math.max(i.a.width-i.borderSize,0),height:Math.max(i.a.height-i.borderSize,0),opacity:Tt,offsetX:-i.borderSize/2,offsetY:-i.borderSize/2},Wt,{strokeWidth:i.borderSize,listening:!1,visible:!!i.borderSize,rotation:i.rotation,cornerRadius:Math.max(0,gt-i.borderSize),hideInExport:!i.showInExport})),i._cropModeEnabled&&e(m,{selector:".page-abs-container",enabled:!0,children:[t(c,{x:-window.innerWidth/n.scale,y:-window.innerWidth/n.scale,width:window.innerWidth/n.scale*3,height:window.innerWidth/n.scale*3,fill:x.cropOverlayFill}),t(s,{listening:!1,image:pt,x:i.x,y:i.y,width:i.a.width,height:i.a.height,rotation:i.rotation,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur}),e(h,{x:i.x,y:i.y,rotation:i.rotation,scaleX:mt,scaleY:mt,children:[t(s,{image:rt,ref:_,opacity:.4,draggable:!0,x:-i.cropX*rt.width,y:-i.cropY*rt.height,onDragMove:bt,onTransform:bt,onTouchMove:t=>{t.evt.preventDefault();const e=t.target.getStage().getPointersPositions();var r=e[0],o=e[1];const i=t.target;if(r&&!o&&!i.isDragging()&&vt.current&&(i.startDrag(),vt.current=!1),r&&o){p.hitOnDragEnabled=!0,i.isDragging()&&(vt.current=!0,i.stopDrag());const e=t.target.getAbsoluteTransform().copy();e.invert();var a={x:r.x,y:r.y},n={x:o.x,y:o.y};if(!Mt.current){return void(Mt.current=k(a,n))}var h=k(a,n),s=function(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}(a,n);yt.current||(yt.current=s);const m=i.position();var c={x:h.x-m.x,y:h.y-m.y},d=s/yt.current;i.scaleX(d),i.scaleY(d);const f=e.point(h),w=e.point(Mt.current);var g=f.x-w.x,l=f.y-w.y,u={x:Math.min(0,h.x-c.x*d+g),y:Math.min(0,h.y-c.y*d+l)};i.position(u),yt.current=s,Mt.current=h,bt(t)}},onTouchEnd:t=>{yt.current=0,Mt.current=null,p.hitOnDragEnabled=!1}}),t(g,Object.assign({ref:xt},H,x.outerImageCropTransformerStyle)),t(c,{width:st,height:ct,ref:ft,listening:!1,onTransform:t=>{if(i.cropX<Math.abs(t.target.x()/rt.width)&&t.target.x()<0&&i.cropX>0){const e=(i.cropWidth+i.cropX)*rt.width;t.target.scaleX(1),t.target.width(e)}if(i.cropY<Math.abs(t.target.y()/rt.height)&&t.target.y()<0&&i.cropY>0){const e=(i.cropHeight+i.cropY)*rt.height;t.target.scaleY(1),t.target.height(e)}t.target.x()<-i.cropX*rt.width-1e-9&&(t.target.x(-i.cropX*rt.width),t.target.scaleX(1)),t.target.y()<-i.cropY*rt.height-1e-9&&(t.target.y(-i.cropY*rt.height),t.target.scaleY(1));const e=Math.min(1,Math.max(0,i.cropX+t.target.x()/rt.width)),r=Math.min(1,Math.max(0,t.target.y()/rt.height+i.cropY)),o=t.target.width()*t.target.scaleX(),a=t.target.height()*t.target.scaleY(),n=Math.min(1-e,o/rt.width),h=Math.min(1-r,a/rt.height),s=t.target.getAbsolutePosition(t.target.getParent().getParent());t.target.scale({x:1,y:1}),t.target.position({x:0,y:0}),i.set({x:s.x,y:s.y,cropX:e,cropY:r,cropWidth:n,cropHeight:h,width:Math.min(o*mt,rt.width*(1-e)*mt),height:Math.min(a*mt,rt.height*(1-r)*mt)})}}),t(g,Object.assign({ref:wt},L,x.innerImageCropTransformerStyle,{visible:i.resizable}))]})]})]})});
|
|
1
|
+
import{jsx as t,jsxs as e,Fragment as r}from"react/jsx-runtime";import o from"react";import{observer as i}from"mobx-react-lite";import{isAlive as a}from"mobx-state-tree";import{Arc as n,Group as h,Image as s,Rect as c,Text as d,Transformer as l}from"react-konva";import{ROLES as g}from"../model/store.js";import u from"use-image";import p from"konva";import{Portal as m}from"react-konva-utils";import{useWorkspaceStyle as f}from"./workspace-style.js";import{incrementLoader as w,triggerLoadError as x}from"../utils/loader.js";import{polotnoError as y}from"@polotno/core/errors";import{getBorderInset as M,getCropRect as b}from"@polotno/core/image";import*as v from"@polotno/core/svg";import{flags as E}from"../utils/flags.js";import{trySetCanvasSize as S}from"../utils/canvas.js";import{roundEnabledPx as R,activeAnchorOf as C,isCornerAnchor as X,scaledBoxStyleAttrs as Y,roundedBoxStyleAttrs as I}from"../utils/px.js";import{useFilteredSource as O}from"./use-filtered-source.js";import{useFadeIn as j}from"./use-fadein.js";import{isTouchDevice as z}from"../utils/screen.js";import{useDelayer as W}from"./use-delayer.js";import{useColor as k}from"./use-color.js";import{downScaleCanvas as D}from"./downscale.js";function L(){return document.createElement("canvas")}export const useSizeFixer=t=>{const[e,r]=o.useState(t);return o.useEffect(()=>{(async()=>{const o=await async function(t){if(!v.isSvgUrl(t)){return t}const e=await v.urlToString(t),r=v.fixSize(e);return v.svgToURL(r)}(t);o!==e&&r(o)})()},[t]),e};function T(t,e){return{x:(t.x+e.x)/2,y:(t.y+e.y)/2}}const A=(t,e)=>e.width<5||e.height<5?t:e,H={keepRatio:!1,rotateEnabled:!1,boundBoxFunc:A},_={enabledAnchors:["top-left","top-right","bottom-left","bottom-right"],rotateEnabled:!1,borderEnabled:!1,boundBoxFunc:A};export function maskCompositeSize(t,e){const r=b({stretchEnabled:e.stretchEnabled,cropX:0,cropY:0,cropWidth:1,cropHeight:1,width:Math.max(e.width,1),height:Math.max(e.height,1)},t);return{width:Math.max(Math.round(r.width),1),height:Math.max(Math.round(r.height),1)}}const F=(t,e,r)=>Math.max(e,Math.min(r,t));export const useCornerRadiusAndCrop=(t,e,r,i,a=0,n=!1,h=!0,s=0)=>{var c,d;const l=Math.floor(F(t.a.width*i,1,1e4)),g=Math.floor(F(t.a.height*i,1,1e4)),u=Math.min(a*i,l/2,g/2),m=Math.min(s*i,l/2,g/2),f=Math.max(t.a.width/r.width,t.a.height/r.height)*i,w=(null===(c=t.page)||void 0===c?void 0:c._exportingOrRendering)&&E.imageDownScalingEnabled&&f<1&&!n,x=0===r.x&&0===r.y&&r.width===(null==e?void 0:e.width)&&r.height===(null==e?void 0:e.height),y=o.useMemo(()=>{if(e&&e.width&&e.height){return!x||0!==u||w||m?L():void 0}},[e,u,w,x,m]);return o.useLayoutEffect(()=>{if(!y||!e){return}S(y,l,g);const t=y.getContext("2d");if(!t){return}(u||m)&&(t.beginPath(),t.roundRect(m,m,l-2*m,g-2*m,Math.max(0,u-m)),t.clip());const o=w?D(e,f):e,i=w?{x:Math.floor(r.x*f),y:Math.floor(r.y*f),width:Math.floor(r.width*f),height:Math.floor(r.height*f)}:r;t.drawImage(o,i.x,i.y,i.width,i.height,0,0,y.width,y.height)},[y,t.a.width,t.a.height,r.x,r.y,r.width,r.height,a,i,n,null===(d=t.page)||void 0===d?void 0:d._exportingOrRendering,w,m]),o.useEffect(()=>()=>{y&&"CANVAS"===y.nodeName&&p.Util.releaseCanvas(y)},[y]),y||e};const P=L(),U=i(({element:r})=>{const i=Math.min(30,r.a.width/4,r.a.height/4),a=o.useRef(null);o.useEffect(()=>{const t=a.current;if(!t){return}const e=new p.Animation(e=>{t.rotate(((null==e?void 0:e.timeDiff)||0)/2)},t.getLayer());return e.start(),()=>{e.stop()}});const{mediaLoadingStyle:s}=f();return e(h,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:s.fill}),t(n,{ref:a,x:r.a.width/2,y:r.a.height/2,fill:s.textFill,outerRadius:Math.abs(i),innerRadius:Math.max(1,i-5),angle:270})]})}),N=i(({element:r})=>{const{mediaErrorStyle:o}=f(),i=Math.max(10,Math.min(30,r.a.width/25));return e(h,{x:r.x,y:r.y,rotation:r.rotation,listening:!1,opacity:r.a.opacity,hideInExport:!r.showInExport,children:[t(c,{width:r.a.width,height:r.a.height,fill:o.fill}),t(d,{text:"Can not load the image...",fontSize:i,width:r.a.width,height:r.a.height,align:"center",fill:o.textFill,verticalAlign:"middle",padding:5})]})});let B=u;export const setImageLoaderHook=t=>{B=t};export const useImageLoader=(t,e="",r="")=>{const i=o.useRef(void 0),a=()=>{var t;null===(t=i.current)||void 0===t||t.call(i),i.current=void 0};o.useEffect(()=>a,[]),o.useLayoutEffect(()=>{const o=e.slice(0,200),n=`image with id ${r} url: ${o}`;"loading"!==t||i.current||(i.current=w(n)),"loading"!==t&&a(),"failed"===t&&x(y("IMAGE_FAILED",n,{src:o,elementId:r,reason:"load"}))},[t])};export const ImageElement=i(({element:i,store:n})=>{var d,u,w;const x=f(),[y,E]=o.useState(!1),D=o.useRef(null),A=o.useRef(null),F=i.isSelected,[V,$]=(t=>{const[e,r]=o.useReducer(t=>t+1,0),i=o.useRef("loading"),a=o.useRef(t.src),n=o.useRef(t.src);n.current!==t.src&&(n.current=t.src,i.current="loading");const h=o.useMemo(()=>v.isSvgUrl(t.src)||"svg"===t.type,[t.src,t.type]);return o.useEffect(()=>{if(!h){return}if(!t.src){return}let e=!1;return(async()=>{i.current="loading",r();const o=await v.urlToString(t.src),n=v.fixSize(o);let h;h=t.colorsReplace?v.replaceColors(n,t.colorsReplace||new Map):v.svgToURL(n),e||(a.current=h,i.current="loaded",r())})(),()=>{e=!0}},[t.src,JSON.stringify(t.colorsReplace)]),h?[a.current,i.current]:[t.src,"loaded"]})(i),[q,G]=B(V,"anonymous"),J="svg"!==i.type||"loaded"===$?G:"loading";useImageLoader(J,i.src,i.id);const K=(null===(d=i.page)||void 0===d?void 0:d._exportingOrRendering)?1:Math.max(1,n.scale),Q=n._elementsPixelRatio*K,Z=(({image:t,status:e,type:r})=>{const i=o.useRef(void 0);return o.useEffect(()=>{i.current=t||i.current},[t]),"failed"!==e||"failed"!==e&&"svg"===r?i.current:void 0})({image:q,status:G,type:i.type}),tt=((t,e,r)=>{const i=o.useMemo(()=>{var o,i;const{flipX:a,flipY:n}=t,h="svg"===t.type||v.isSvgUrl(t.src),s=navigator.userAgent.toLowerCase().indexOf("firefox")>-1,c=(/^((?!chrome|android).)*safari/i.test(navigator.userAgent)||s)&&h||t.maskSrc;if(!a&&!n&&!c){return e}if(!e||!e.width||!e.height){return null}const d=L();let l=1;"svg"===t.type&&(l=Math.max(t.a.width/e.width*r,t.a.height/e.height*r)),S(d,Math.max(e.width*l,1),Math.max(e.height*l,1));let g=a?-d.width:0,u=n?-d.height:0;return null===(o=d.getContext("2d"))||void 0===o||o.scale(a?-1:1,n?-1:1),null===(i=d.getContext("2d"))||void 0===i||i.drawImage(e,g,u,d.width,d.height),d},[t.maskSrc,t.flipX,t.flipY,e,t.a.width,t.a.height,r]);return o.useEffect(()=>()=>{i&&"CANVAS"===i.nodeName&&p.Util.releaseCanvas(i)},[i]),i})(i,q||Z,Q),et=((t,e)=>{const r=useSizeFixer(t.maskSrc||""),[i,a]=B(r,"anonymous");return useImageLoader(r?a:"loaded",r||"",t.id),o.useMemo(()=>{if(!i){return e}if(!e||!e.width||!e.height){return e}const{width:r,height:o}=maskCompositeSize(e,{width:t.a.width,height:t.a.height,stretchEnabled:t.stretchEnabled}),a=L();a.width=r,a.height=o;const n=a.getContext("2d");if(!n){return e}n.drawImage(e,0,0,r,o,0,0,r,o),n.globalCompositeOperation="source-in";const h=function(t,e){const r=e.width/e.height;let o,i;return r>=t.width/t.height?(o=t.width,i=t.width/r):(o=t.height*r,i=t.height),{x:(t.width-o)/2,y:(t.height-i)/2,width:o,height:i}}(i,t);return n.drawImage(i,h.x,h.y,h.width,h.height,0,0,r,o),a},[e,i,t.a.width,t.a.height,t.stretchEnabled])})(i,tt)||P,[rt=P]=O(i,et);let{cropX:ot,cropY:it,cropWidth:at,cropHeight:nt}=i.a;"loaded"!==G&&(ot=it=0,at=nt=1);const ht=b({stretchEnabled:i.stretchEnabled,cropX:ot,cropY:it,cropWidth:at,cropHeight:nt,width:i.a.width,height:i.a.height},rt),st=ht.width,ct=ht.height,dt=i.stretchEnabled,lt=null!==(u=i.cornerRadius)&&void 0!==u?u:0,gt=(null===(w=i.page)||void 0===w?void 0:w._exportingOrRendering)?1:Math.min(2,n.scale),ut=n._elementsPixelRatio*gt;let pt=((t,e,r,i)=>{const a=useSizeFixer(t.clipSrc||""),[n,h]=B(a,"anonymous"),s=t.clipSrc?h:"loaded";useImageLoader(s,t.clipSrc,t.id);const c=o.useMemo(()=>{if(e&&n){return L()}},[e,n]);return o.useLayoutEffect(()=>{var o;if(!n){return}if(!e||!e.width||!e.height){return}if(!n||!n.width||!n.height){return}if(!c){return}const i=L(),a=Math.max(t.a.width/n.width*r,t.a.height/n.height*r);i.width=Math.max(n.width*a,1),i.height=Math.max(n.height*a,1),null===(o=i.getContext("2d"))||void 0===o||o.drawImage(n,0,0,i.width,i.height),c.width=Math.max(e.width,1),c.height=Math.max(e.height,1);const h=c.getContext("2d");h&&(h.save(),h.drawImage(i,0,0,e.width,e.height),p.Util.releaseCanvas(i),h.globalCompositeOperation="source-in",h.drawImage(e,0,0,c.width,c.height),h.restore())},[c,e,n,t.a.width,t.a.height,r,...i]),t.clipSrc&&n?c:e})(i,useCornerRadiusAndCrop(i,rt,ht,ut,lt,y||i._cropModeEnabled||"svg"===i.type,!0,M({borderSize:i.borderSize,borderColor:i.borderColor,width:i.a.width,height:i.a.height})),ut,[ht,lt,ut]);const mt=Math.max(i.a.width/st,i.a.height/ct);o.useEffect(()=>{var t;if(!i._cropModeEnabled){return}const e=null===(t=D.current)||void 0===t?void 0:t.getStage();function r(t){i._cropModeEnabled&&t.target!==A.current&&i.toggleCropMode(!1)}function o(t){i._cropModeEnabled&&t.target.parentNode!==(null==e?void 0:e.content)&&i.toggleCropMode(!1)}return document.body.addEventListener("click",o),null==e||e.on("click",r),null==e||e.on("tap",r),()=>{document.body.removeEventListener("click",o),document.body.removeEventListener("touchstart",o),null==e||e.off("click",r),null==e||e.off("click",r)}},[i._cropModeEnabled]);const ft=o.useRef(null),wt=o.useRef(null),xt=o.useRef(null);o.useLayoutEffect(()=>{i._cropModeEnabled&&(wt.current.nodes([ft.current]),xt.current.nodes([A.current]))},[i._cropModeEnabled]);var yt=o.useRef(null),Mt=o.useRef(0),bt=o.useRef(!1);const vt=t=>{var e;(null===(e=t.evt.touches)||void 0===e?void 0:e.length)>2&&t.target.stopDrag(),Math.round(t.target.x())>0&&(t.target.x(0),t.target.scaleX(1)),Math.round(t.target.y())>0&&(t.target.y(0),t.target.scaleY(1));const r=t.target.width()*t.target.scaleX(),o=t.target.height()*t.target.scaleY(),a=Math.min(1,st/r),n=Math.min(1,ct/o),h=1-a,s=Math.min(h,Math.max(0,Math.round(-t.target.x())/r)),c=1-n,d=Math.min(c,Math.max(0,Math.round(-t.target.y())/o));t.target.setAttrs({x:-s*rt.width,y:-d*rt.height,scaleX:1,scaleY:1}),i.set({cropX:s,cropY:d,cropWidth:a,cropHeight:n})},Et=()=>{"svg"!==i.type&&i.contentEditable&&(i.stretchEnabled||setTimeout(()=>{a(i)&&i.toggleCropMode(!0)}))},St="svg"===i.type&&Z,Rt="loading"===G&&!St,[Ct]=W(Rt,100,!1,!1),Xt="failed"===G,Yt=!Ct&&!Xt,It=o.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Ot=o.useRef(!1),jt=Yt?i.a.opacity:0;j(D,jt);const zt=i.selectable||n.role===g.ADMIN,Wt=z(),kt=k(i,i.borderColor,"stroke");return e(r,{children:[Ct&&t(U,{element:i}),Xt&&t(N,{element:i}),t(s,{ref:D,name:"element",id:i.id,image:pt,x:i.a.x,y:i.a.y,width:i.a.width||1,height:i.a.height||1,rotation:i.a.rotation,opacity:jt,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,customCrop:ht,listening:zt,draggable:Wt?i.draggable&&F:i.draggable,preventDefault:!Wt||F,hideInExport:!i.showInExport,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDblClick:Et,onDblTap:Et,onTransformStart:t=>{E(!0),Ot.current=X(C(t.target)),It.current={cropX:i.cropX,cropY:i.cropY,cropWidth:i.cropWidth,cropHeight:i.cropHeight}},onTransform:t=>{const e=t.currentTarget,r=Math.abs(e.scaleX()-1)<1e-7?1:e.scaleX(),o=Math.abs(e.scaleY()-1)<1e-7?1:e.scaleY();e.scaleX(1),e.scaleY(1);const a=1-st/rt.width,n=Math.min(a,Math.max(0,i.cropX)),h=1-ct/rt.height,s=Math.min(h,Math.max(0,i.cropY)),c=Ot.current,d=!c&&r<1&&It.current.cropHeight>ct/rt.height;let l=c?i.cropWidth:i.cropWidth*r;d&&(l=i.cropWidth);const g=!c&&o<1&&It.current.cropWidth>st/rt.width;let u=c?i.cropHeight:i.cropHeight*o;g&&(u=i.cropHeight),dt&&(l=i.cropWidth,u=i.cropHeight);const p=c?Math.min(r,o):1;i.set(Object.assign({cropX:n,cropY:s,x:e.x(),y:e.y(),width:e.width()*r,height:e.height()*o,rotation:t.target.rotation(),cropWidth:Math.min(l,1-n),cropHeight:Math.min(u,1-s)},1!==p&&Object.assign(Object.assign({},Y(i,p)),{borderSize:i.borderSize*p})))},onTransformEnd:t=>{const e=t.currentTarget;i.set(Object.assign(Object.assign({width:e.width(),height:e.height(),x:e.x(),y:e.y(),rotation:t.target.rotation(),cropWidth:st/rt.width,cropHeight:ct/rt.height},I(i)),{borderSize:R(i.borderSize)})),E(!1)}}),t(c,Object.assign({x:i.x,y:i.y,width:Math.max(i.a.width-i.borderSize,0),height:Math.max(i.a.height-i.borderSize,0),opacity:jt,offsetX:-i.borderSize/2,offsetY:-i.borderSize/2},kt,{strokeWidth:i.borderSize,listening:!1,visible:!!i.borderSize,rotation:i.rotation,cornerRadius:Math.max(0,lt-i.borderSize),hideInExport:!i.showInExport})),i._cropModeEnabled&&e(m,{selector:".page-abs-container",enabled:!0,children:[t(c,{x:-window.innerWidth/n.scale,y:-window.innerWidth/n.scale,width:window.innerWidth/n.scale*3,height:window.innerWidth/n.scale*3,fill:x.cropOverlayFill}),t(s,{listening:!1,image:pt,x:i.x,y:i.y,width:i.a.width,height:i.a.height,rotation:i.rotation,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur}),e(h,{x:i.x,y:i.y,rotation:i.rotation,scaleX:mt,scaleY:mt,children:[t(s,{image:rt,ref:A,opacity:.4,draggable:!0,x:-i.cropX*rt.width,y:-i.cropY*rt.height,onDragMove:vt,onTransform:vt,onTouchMove:t=>{t.evt.preventDefault();const e=t.target.getStage().getPointersPositions();var r=e[0],o=e[1];const i=t.target;if(r&&!o&&!i.isDragging()&&bt.current&&(i.startDrag(),bt.current=!1),r&&o){p.hitOnDragEnabled=!0,i.isDragging()&&(bt.current=!0,i.stopDrag());const e=t.target.getAbsoluteTransform().copy();e.invert();var a={x:r.x,y:r.y},n={x:o.x,y:o.y};if(!yt.current){return void(yt.current=T(a,n))}var h=T(a,n),s=function(t,e){return Math.sqrt(Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2))}(a,n);Mt.current||(Mt.current=s);const m=i.position();var c={x:h.x-m.x,y:h.y-m.y},d=s/Mt.current;i.scaleX(d),i.scaleY(d);const f=e.point(h),w=e.point(yt.current);var l=f.x-w.x,g=f.y-w.y,u={x:Math.min(0,h.x-c.x*d+l),y:Math.min(0,h.y-c.y*d+g)};i.position(u),Mt.current=s,yt.current=h,vt(t)}},onTouchEnd:t=>{Mt.current=0,yt.current=null,p.hitOnDragEnabled=!1}}),t(l,Object.assign({ref:xt},_,x.outerImageCropTransformerStyle)),t(c,{width:st,height:ct,ref:ft,listening:!1,onTransform:t=>{if(i.cropX<Math.abs(t.target.x()/rt.width)&&t.target.x()<0&&i.cropX>0){const e=(i.cropWidth+i.cropX)*rt.width;t.target.scaleX(1),t.target.width(e)}if(i.cropY<Math.abs(t.target.y()/rt.height)&&t.target.y()<0&&i.cropY>0){const e=(i.cropHeight+i.cropY)*rt.height;t.target.scaleY(1),t.target.height(e)}t.target.x()<-i.cropX*rt.width-1e-9&&(t.target.x(-i.cropX*rt.width),t.target.scaleX(1)),t.target.y()<-i.cropY*rt.height-1e-9&&(t.target.y(-i.cropY*rt.height),t.target.scaleY(1));const e=Math.min(1,Math.max(0,i.cropX+t.target.x()/rt.width)),r=Math.min(1,Math.max(0,t.target.y()/rt.height+i.cropY)),o=t.target.width()*t.target.scaleX(),a=t.target.height()*t.target.scaleY(),n=Math.min(1-e,o/rt.width),h=Math.min(1-r,a/rt.height),s=t.target.getAbsolutePosition(t.target.getParent().getParent());t.target.scale({x:1,y:1}),t.target.position({x:0,y:0}),i.set({x:s.x,y:s.y,cropX:e,cropY:r,cropWidth:n,cropHeight:h,width:Math.min(o*mt,rt.width*(1-e)*mt),height:Math.min(a*mt,rt.height*(1-r)*mt)})}}),t(l,Object.assign({ref:wt},H,x.innerImageCropTransformerStyle,{visible:i.resizable}))]})]})]})});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"quill";import{firstStopColor as e}from"@polotno/core/gradient";import{
|
|
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{isClipGradientSpan as i}from"../utils/rich-text-html.js";let l=!1;const s=["background-image","background-size","-webkit-background-clip","background-clip","-webkit-text-fill-color","color"];export function registerQuillUnrestrictedFontFormats(){if(l){return}l=!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 u=t.import("parchment");if((null==u?void 0:u.StyleAttributor)&&(null==u?void 0:u.Scope)){const r=new u.StyleAttributor("font-weight","font-weight",{scope:u.Scope.INLINE});t.register(r,!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",r),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){for(const e of s){t.style.removeProperty(e)}}value(t){return i(t)?t.style.getPropertyValue("background-image"):""}}return new r("gradient","background-image",{scope:t.Scope.INLINE})}(u),!0);const o=t.import("attributors/style/color");if(o){const e=o.value.bind(o);o.value=t=>i(t)?"":e(t),t.register(o,!0)}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var t=this&&this.__rest||function(t,e){var r={};for(var o in t){Object.prototype.hasOwnProperty.call(t,o)&&e.indexOf(o)<0&&(r[o]=t[o])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(o=Object.getOwnPropertySymbols(t);n<o.length;n++){e.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(t,o[n])&&(r[o[n]]=t[o[n]])}}return r};import{jsx as e,Fragment as r,jsxs as o}from"react/jsx-runtime";import n from"react";import{observer as i}from"mobx-react-lite";import{Group as a,Path as s,Rect as l,Shape as c}from"react-konva";import{nodeAutorun as h}from"../utils/mobx.js";import{isAlive as d}from"mobx-state-tree";import{Html as f}from"react-konva-utils";import{layout as u,drawLayout as g}from"render-tag";import{layoutTextOnPath as m,drawTextOnPathLayout as x}from"render-tag/path";import{activeAnchorOf as p,scaledFontSizeAttrs as y,roundedFontSizeAttrs as b,roundedTextDecorationAttrs as w,textMinResizeWidth as v}from"../utils/px.js";import{flags as O}from"../utils/flags.js";import{applyFilter as E}from"./apply-filters.js";import{getLineHeight as j,usePrevious as S}from"./text-element.js";import{useFontSpecsLoader as M}from"./text-element/font-specs-loader.js";import{getHtml as z,getCurvePath as k,getCurveTextHeight as R}from"@polotno/core/text-html";import{useColor as Y}from"./use-color.js";import{isGradient as X}from"@polotno/core/gradient";import{detectSize as F}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as I}from"@polotno/core/background-shape";import{useFadeIn as P}from"./use-fadein.js";import{incrementLoader as C}from"../utils/loader.js";import{removeTags as T}from"@polotno/core/text";import{isTouchDevice as B}from"../utils/screen.js";import{getLimitedFontSize as W}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as D,extractFontSpecsFromHtml as J,scaleRichTextFontSizesInHtml as N}from"../utils/rich-text-html.js";import{RichEditor as $}from"./rich-editor.js";import{findFitFontSize as _}from"./text-element/fit-font-size.js";import{visiblePlaceholder as A}from"./text-element/placeholder.js";import{wrapHtmlForRenderTag as L}from"@polotno/core/reset-style";import{xmlEscape as H}from"@polotno/core/xml";import{hasMeasuredHeight as V,resolveTextBoxHeight as q}from"@polotno/core/text-types";export{L as wrapHtmlForRenderTag};export const RenderTagElement=i(({element:i,store:G})=>{var K;const Q=n.useRef(null),U=n.useRef(null),[Z,tt]=n.useState(!1),et=n.useRef(i.height),rt=i.isSelected,ot=S(rt),{textVerticalResizeEnabled:nt}=O,it=O.autoDeleteEmptyText,at=S(i.fontFamily),st=n.useMemo(()=>D(i.text,{fontSize:i.fontSize,fontFamily:i.fontFamily}),[i.text,i.fontSize,i.fontFamily]),lt=n.useMemo(()=>J(st,{fontFamily:i.fontFamily,fontWeight:i.fontWeight,fontStyle:i.fontStyle}),[st,i.fontFamily,i.fontWeight,i.fontStyle]),[ct]=M(G,lt,i.text||i.placeholder),ht=i._editModeEnabled;P(Q);const dt=ct?i.fontFamily:at&&at!==i.fontFamily?at:"Arial",ft=Y(i).fill,ut=X(i.fill),gt=X(i.stroke)&&!!i.strokeWidth,mt=!!i.strokeWidth&&(gt||ut),xt="ellipsis"===O.textOverflow&&!i.curveEnabled&&V(i)&&!mt,pt=A(i.placeholder),yt=!T(i.text)&&!!pt,bt=yt?H(pt):i.text,wt=yt?.6:i.a.opacity,vt=z(yt?Object.assign(Object.assign({},i.toJSON()),{text:bt}):i,{fontFamily:dt,color:ft,forEditor:!xt,textOverflow:O.textOverflow}),Ot=i.width||1,Et=i.curveEnabled,jt=function(t,e,r,o){return n.useMemo(()=>{if(o){return null}try{return u({html:L(t),width:Math.max(1,e),accuracy:"performance"})}catch(r){return console.error("render-tag layout failed:",r),null}},[t,e,r,o])}(vt,Ot,ct,Et),St=function(t,e,r){return n.useMemo(()=>{if(!e.curveEnabled){return null}const r=e.width||1,o=R(e),n=k(r,o,e.curvePower,e.fontSize);try{return m({html:L(t),path:n,align:"center",textBaseline:"middle"})}catch(i){return console.error("render-tag/path layoutTextOnPath failed:",i),null}},[r,e.curveEnabled,e.curvePower,e.fontSize,e.width,e.height,t])}(vt,i,ct),Mt=j({fontLoaded:ct,fontFamily:i.fontFamily,fontSize:i.fontSize,lineHeight:i.lineHeight}),zt=Et?0:Math.max(null!==(K=null==jt?void 0:jt.height)&&void 0!==K?K:0,i.fontSize*Mt),kt=St?St.bounds:null,Rt=n.useMemo(()=>jt?jt.lines.map(t=>({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height})):[],[jt]),Yt=n.useRef([]),Xt=n.useRef(!0),[,Ft]=n.useReducer(t=>t+1,0);n.useEffect(()=>{const t=Yt.current;t.length>0&&(Yt.current=[],t.forEach(t=>t()))});const It=n.useCallback((t,e,r,o)=>{const n=C(`text ${i.id}`),a=()=>{G.history.ignore(t,e,r).then(()=>{Xt.current?(Yt.current.push(n),Ft()):n()})};o?queueMicrotask(a):a()},[i.id,G]);n.useEffect(()=>{if(!ct){return}if(!jt){return}if(!V(i)){return void It(()=>{d(i)&&(V(i)||i.set({height:zt}))},void 0,void 0,!0)}if(G.isPlaying){return}const{textOverflow:t}=O,e=i.height!==zt&&(!nt||i.height<zt),r=()=>It(()=>{d(i)&&i.set({height:zt})},!1,!0);if("change-font-size"!==t||Z||ht||i.curveEnabled){"resize"===t&&e&&!Z&&r()}else{const t=_(yt?Object.assign(Object.assign({},i.toJSON()),{text:bt,toJSON:()=>Object.assign(Object.assign({},i.toJSON()),{text:bt})}):i,{fontFamily:dt,color:ft,forEditor:!0},t=>{try{return u({html:L(t),width:Ot,accuracy:"performance"}).height}catch(K){return F(t).height}});t.fontSize!==i.fontSize?It(()=>{if(!d(i)){return}const e=y(i,t.fontSize);void 0!==t.text&&t.text!==i.text&&(e.text=t.text),i.set(e)},!1,!0):e&&r()}}),n.useEffect(()=>()=>{Xt.current=!1;const t=Yt.current;t.length>0&&(Yt.current=[],t.forEach(t=>t()))},[]),n.useLayoutEffect(()=>{if(!ct||!i.curveEnabled||Z){return}if(!kt||!kt.width){return}const t=t=>t&&nt?Math.max(t,kt.height):kt.height;Math.abs(kt.width-i.width)<1&&Math.abs(t(i.height)-i.height)<1||It(()=>{if(!d(i)){return}const e=kt.width-i.width,r=t(i.height),o=r-i.height;if(Math.abs(e)<1&&Math.abs(o)<1){return}const n=i.a.rotation*Math.PI/180,a=-e/2*Math.cos(n),s=-e/2*Math.sin(n);i.set({width:kt.width,height:r,x:i.a.x+a,y:i.a.y+s})},!1,!0,!0)},[ct,i.curveEnabled,kt,Z,nt,It]),n.useEffect(()=>{ot&&!rt&&it&&""===T(i.text)&&i.removable&&!A(i.placeholder)&&G.deleteElements([i.id])},[it,i.placeholder,i.removable,i.text,rt,ot]),n.useEffect(()=>h(i,()=>{const t=Q.current;t&&E(t,i)}),[jt,St,i.a.width,i.a.height,i.shadowColor,i.shadowOffsetX,i.shadowOffsetY,i.shadowOpacity]);const Pt=q(i,zt);let Ct=0;Et||"middle"!==i.verticalAlign?Et||"bottom"!==i.verticalAlign||(Ct=Pt-zt):Ct=(Pt-zt)/2;const Tt=i.backgroundPadding*(i.fontSize*Mt*.5),Bt=i.backgroundCornerRadius*(i.fontSize*Mt*.5),Wt=n.useMemo(()=>!i.backgroundEnabled||i.curveEnabled||i.legacyBackground||0===Rt.length?"":I({rects:Rt,padding:Tt,cornerRadius:Bt}),[i.backgroundEnabled,i.curveEnabled,i.legacyBackground,Rt,Tt,Bt]),Dt=B(),Jt=n.useRef(null),Nt=n.useRef(i.width),$t=n.useRef(null),_t=n.useRef(null),At=n.useRef(""),Lt=n.useRef(0),Ht=n.useRef(0),Vt=i.width>0?i.a.width/i.width:1;return o(r,{children:[i.curveEnabled||i.legacyBackground?e(l,{x:i.a.x,y:i.a.y,offsetX:Tt/Vt,offsetY:Tt/Vt,rotation:i.a.rotation,scaleX:Vt,scaleY:Vt,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled,opacity:i.backgroundOpacity*wt,fill:i.backgroundColor,width:i.width+2*Tt,height:Pt+2*Tt,cornerRadius:Bt}):e(s,{name:"rendertag-background",x:i.a.x,y:i.a.y,offsetY:-Ct/Vt,rotation:i.a.rotation,scaleX:Vt,scaleY:Vt,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled&&Wt.length>0,opacity:i.backgroundOpacity*wt,fill:i.backgroundColor,data:Wt}),e(c,{ref:Q,name:"rendertag-text",x:i.a.x,y:i.a.y,rotation:i.a.rotation,scaleX:Vt,scaleY:Vt,offsetY:Vt>0?-Ct/Vt:0,width:i.width,height:Math.max(i.height,zt),listening:!1,visible:!ht&&!!(Et?St:jt),opacity:wt,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,hideInExport:!i.showInExport,sceneFunc:(t,e)=>{const r=t._context;if(r){try{if(Et){if(!St||!kt){return}const t=(i.width-kt.width)/2-kt.x;r.save(),r.translate(t,-kt.y),x({layout:St,ctx:r}),r.restore()}else{if(!jt){return}g({layout:jt,width:Ot,ctx:r,pixelRatio:1})}}catch(o){console.error("render-tag draw failed:",o)}}}}),e(l,{ref:U,name:"element",x:i.a.x,y:i.a.y,listening:i.selectable,rotation:i.a.rotation,width:i.a.width,height:i.a.height,visible:!ht,draggable:Dt?i.draggable&&rt:i.draggable,preventDefault:!Dt||rt,opacity:0,hideInExport:!0,id:i.id,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDblClick:t=>{i.contentEditable&&(_t.current={x:t.evt.clientX,y:t.evt.clientY},i.toggleEditMode(!0))},onDblTap:t=>{var e;if(i.contentEditable){const r=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];_t.current=r?{x:r.clientX,y:r.clientY}:null,i.toggleEditMode(!0)}},onTransformStart:t=>{tt(!0),et.current=i.height,Nt.current=i.width,$t.current=null,Jt.current=t.target.position(),At.current=p(t.target),Lt.current=i.a.rotation,Ht.current=i.height},onTransform:e=>{const r=e.target,o=At.current,n="middle-left"===o||"middle-right"===o,a="top-center"===o||"bottom-center"===o,s=r.scaleX();if(n){const e=r.scaleX(),o=r.width()*e;let n=Math.max(o,v(Nt.current,i.fontSize));const a=i.toJSON();let s=0;if(i.curveEnabled){s=R(Object.assign(Object.assign({},a),{width:n}))}else{const{x:e,y:r,width:o,height:i}=a,l=t(a,["x","y","width","height"]),c=`${JSON.stringify(l)}|${dt}|${ct}|${ft}`;if($t.current&&$t.current.signature===c&&n<=$t.current.width){n=$t.current.width,s=$t.current.height}else{const t=t=>{const e=z(Object.assign(Object.assign({},a),{text:bt,width:t}),{fontFamily:dt,color:ft,forEditor:!0});try{return u({html:L(e),width:t,accuracy:"performance"})}catch(K){return{height:F(e).height,lines:[]}}};let e=t(n);const r=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width));r>n+.5&&r<=Nt.current+.5&&(n=Math.min(Math.ceil(r),Nt.current),e=t(n),$t.current={signature:c,width:n,height:e.height}),s=e.height}}n>o&&Jt.current&&r.position(Jt.current),r.width(n),r.scaleX(1),r.scaleY(1);let l=O.textVerticalResizeEnabled?Math.max(s,et.current):s;"ellipsis"===O.textOverflow&&(l=et.current),r.height(l),i.set({width:r.width(),height:l,x:r.x(),y:r.y()})}else if(a){const t="resize"===O.textOverflow?Et?(null==kt?void 0:kt.height)||i.fontSize:zt:Mt*i.fontSize,e=r.height()*r.scaleY();if(e<t){if(r.height(t),Jt.current){if("top-center"===At.current){const e=Lt.current*Math.PI/180,o=Ht.current-t;r.x(Jt.current.x-o*Math.sin(e)),r.y(Jt.current.y+o*Math.cos(e))}else{r.position(Jt.current)}}}else{r.height(e)}r.scaleY(1),r.scaleX(1),Ht.current=Math.max(t,e),i.set({x:r.x(),y:r.y(),height:r.height(),rotation:r.rotation()})}else{r.scaleX(1),r.scaleY(1),i.set({fontSize:i.fontSize*s,letterSpacing:i.letterSpacing,width:r.width()*s,x:r.x(),y:r.y(),rotation:r.rotation(),height:r.height()*s,shadowBlur:i.shadowBlur*s,shadowOffsetX:i.shadowOffsetX*s,shadowOffsetY:i.shadowOffsetY*s,strokeWidth:i.strokeWidth*s,text:N(i.text,s)})}Jt.current=e.target.position()},onTransformEnd:t=>{tt(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),i.set(Object.assign(Object.assign({},b(i,i.fontSize*e)),{width:t.target.width()*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),text:N(i.text,e)}))}}),ht&&e(a,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,offsetY:-Ct,children:e(f,{divProps:{style:{zIndex:20}},children:e($,{element:i,onChange:t=>{const e=W({oldText:T(i.text),newText:T(t),element:i});i.set(Object.assign({text:t},y(i,e)))},onBlur:()=>{d(i)&&(i.set(w(i)),i.toggleEditMode(!1))},clickCoords:_t.current})})})]})});
|
|
1
|
+
var t=this&&this.__rest||function(t,e){var r={};for(var o in t){Object.prototype.hasOwnProperty.call(t,o)&&e.indexOf(o)<0&&(r[o]=t[o])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(o=Object.getOwnPropertySymbols(t);n<o.length;n++){e.indexOf(o[n])<0&&Object.prototype.propertyIsEnumerable.call(t,o[n])&&(r[o[n]]=t[o[n]])}}return r};import{jsx as e,Fragment as r,jsxs as o}from"react/jsx-runtime";import n from"react";import{observer as i}from"mobx-react-lite";import{Group as a,Path as s,Rect as l,Shape as c}from"react-konva";import{nodeAutorun as h}from"../utils/mobx.js";import{isAlive as d}from"mobx-state-tree";import{Html as u}from"react-konva-utils";import{layout as f,drawLayout as g}from"render-tag";import{layoutTextOnPath as m,drawTextOnPathLayout as x}from"render-tag/path";import{activeAnchorOf as p,scaledFontSizeAttrs as y,roundedFontSizeAttrs as b,roundedTextDecorationAttrs as w,textMinResizeWidth as v}from"../utils/px.js";import{flags as E}from"../utils/flags.js";import{applyFilter as O}from"./apply-filters.js";import{getLineHeight as M,usePrevious as S}from"./text-element.js";import{useFontSpecsLoader as j}from"./text-element/font-specs-loader.js";import{getHtml as z,getCurvePath as k,getCurveTextHeight as R}from"@polotno/core/text-html";import{useColor as Y}from"./use-color.js";import{isGradient as X}from"@polotno/core/gradient";import{detectSize as F}from"@polotno/core/measure-html";import{generateBackgroundShapeFromRects as I}from"@polotno/core/background-shape";import{useFadeIn as P}from"./use-fadein.js";import{incrementLoader as C}from"../utils/loader.js";import{removeTags as T}from"@polotno/core/text";import{isTouchDevice as B}from"../utils/screen.js";import{getLimitedFontSize as W}from"./text-element/max-font-size.js";import{normalizeRichTextHtml as D,extractFontSpecsFromHtml as N,scaleRichTextFontSizesInHtml as $}from"../utils/rich-text-html.js";import{RichEditor as J}from"./rich-editor.js";import{findFitFontSize as _}from"./text-element/fit-font-size.js";import{visiblePlaceholder as A}from"./text-element/placeholder.js";import{wrapHtmlForRenderTag as L}from"@polotno/core/reset-style";import{xmlEscape as V}from"@polotno/core/xml";import{hasMeasuredHeight as H,resolveTextBoxHeight as q}from"@polotno/core/text-types";export{L as wrapHtmlForRenderTag};function G(t,e){return t!==e&&(!E.textVerticalResizeEnabled||t<e)}export const RenderTagElement=i(({element:i,store:K})=>{var Q;const U=n.useRef(null),Z=n.useRef(null),[tt,et]=n.useState(!1),rt=n.useRef(i.height),ot=i.isSelected,nt=S(ot),{textVerticalResizeEnabled:it}=E,at=E.autoDeleteEmptyText,st=S(i.fontFamily),lt=n.useMemo(()=>D(i.text,{fontSize:i.fontSize,fontFamily:i.fontFamily}),[i.text,i.fontSize,i.fontFamily]),ct=n.useMemo(()=>N(lt,{fontFamily:i.fontFamily,fontWeight:i.fontWeight,fontStyle:i.fontStyle}),[lt,i.fontFamily,i.fontWeight,i.fontStyle]),[ht]=j(K,ct,i.text||i.placeholder),dt=i._editModeEnabled;P(U);const ut=ht?i.fontFamily:st&&st!==i.fontFamily?st:"Arial",ft=Y(i).fill,gt=X(i.fill),mt=X(i.stroke)&&!!i.strokeWidth,xt=!!i.strokeWidth&&(mt||gt),pt="ellipsis"===E.textOverflow&&!i.curveEnabled&&H(i)&&!xt,yt=A(i.placeholder),bt=!!yt&&!T(i.text),wt=bt?V(yt):lt,vt=bt?.6:i.a.opacity,Et=z(i,{fontFamily:ut,color:ft,forEditor:!pt,textOverflow:E.textOverflow,text:wt,textIsNormalized:!0}),Ot=i.width||1,Mt=i.curveEnabled,St=function(t,e,r,o){return n.useMemo(()=>{if(o){return null}try{return f({html:L(t),width:Math.max(1,e),accuracy:"performance"})}catch(r){return console.error("render-tag layout failed:",r),null}},[t,e,r,o])}(Et,Ot,ht,Mt),jt=function(t,e,r){return n.useMemo(()=>{if(!e.curveEnabled){return null}const r=e.width||1,o=R(e),n=k(r,o,e.curvePower,e.fontSize);try{return m({html:L(t),path:n,align:"center",textBaseline:"middle"})}catch(i){return console.error("render-tag/path layoutTextOnPath failed:",i),null}},[r,e.curveEnabled,e.curvePower,e.fontSize,e.width,e.height,t])}(Et,i,ht),zt=M({fontLoaded:ht,fontFamily:i.fontFamily,fontSize:i.fontSize,lineHeight:i.lineHeight}),kt=Mt?0:Math.max(null!==(Q=null==St?void 0:St.height)&&void 0!==Q?Q:0,i.fontSize*zt),Rt=jt?jt.bounds:null,Yt=n.useMemo(()=>St?St.lines.map(t=>({left:t.bounds.x,right:t.bounds.x+t.bounds.width,top:t.bounds.y,bottom:t.bounds.y+t.bounds.height})):[],[St]),Xt=n.useRef([]),Ft=n.useRef(null),It=n.useRef(!0),[,Pt]=n.useReducer(t=>t+1,0);n.useEffect(()=>{const t=Xt.current;t.length>0&&(Xt.current=[],t.forEach(t=>t()))});const Ct=n.useCallback((t,e,r,o)=>{const n=C(`text ${i.id}`),a=()=>{K.history.ignore(t,e,r).then(()=>{It.current?(Xt.current.push(n),Pt()):n()})};o?queueMicrotask(a):a()},[i.id,K]),Tt=n.useCallback(t=>{const e=null!==Ft.current;Ft.current=t,e||Ct(()=>{const t=Ft.current;Ft.current=null,d(i)&&G(i.height,t)&&i.set({height:t})},!1,!0)},[i,Ct]);n.useEffect(()=>{if(!ht){return}if(!St){return}if(!H(i)){return void Ct(()=>{d(i)&&(H(i)||i.set({height:kt}))},void 0,void 0,!0)}if(K.isPlaying){return}const{textOverflow:t}=E,e=G(i.height,kt),r=()=>Tt(kt);if("change-font-size"!==t||tt||dt||i.curveEnabled){"resize"===t&&e&&!tt&&r()}else{const t=_(bt?{toJSON:()=>Object.assign(Object.assign({},i.toJSON()),{text:wt})}:i,{fontFamily:ut,color:ft,forEditor:!0},t=>{try{return f({html:L(t),width:Ot,accuracy:"performance"}).height}catch(Q){return F(t).height}});t.fontSize!==i.fontSize?Ct(()=>{if(!d(i)){return}const e=y(i,t.fontSize);void 0!==t.text&&t.text!==i.text&&(e.text=t.text),i.set(e)},!1,!0):e&&r()}}),n.useEffect(()=>()=>{It.current=!1;const t=Xt.current;t.length>0&&(Xt.current=[],t.forEach(t=>t()))},[]),n.useLayoutEffect(()=>{if(!ht||!i.curveEnabled||tt){return}if(!Rt||!Rt.width){return}const t=t=>t&&it?Math.max(t,Rt.height):Rt.height;Math.abs(Rt.width-i.width)<1&&Math.abs(t(i.height)-i.height)<1||Ct(()=>{if(!d(i)){return}const e=Rt.width-i.width,r=t(i.height),o=r-i.height;if(Math.abs(e)<1&&Math.abs(o)<1){return}const n=i.a.rotation*Math.PI/180,a=-e/2*Math.cos(n),s=-e/2*Math.sin(n);i.set({width:Rt.width,height:r,x:i.a.x+a,y:i.a.y+s})},!1,!0,!0)},[ht,i.curveEnabled,Rt,tt,it,Ct]),n.useEffect(()=>{nt&&!ot&&at&&""===T(i.text)&&i.removable&&!A(i.placeholder)&&K.deleteElements([i.id])},[at,i.placeholder,i.removable,i.text,ot,nt]),n.useEffect(()=>h(i,()=>{const t=U.current;t&&O(t,i)}),[St,jt,i.a.width,i.a.height,i.shadowColor,i.shadowOffsetX,i.shadowOffsetY,i.shadowOpacity]);const Bt=q(i,kt),Wt=Mt||!ht||tt||K.isPlaying||"resize"!==E.textOverflow?i.a.height:it?Math.max(i.a.height,kt):kt;let Dt=0;Mt||"middle"!==i.verticalAlign?Mt||"bottom"!==i.verticalAlign||(Dt=Bt-kt):Dt=(Bt-kt)/2;const Nt=i.backgroundPadding*(i.fontSize*zt*.5),$t=i.backgroundCornerRadius*(i.fontSize*zt*.5),Jt=n.useMemo(()=>!i.backgroundEnabled||i.curveEnabled||i.legacyBackground||0===Yt.length?"":I({rects:Yt,padding:Nt,cornerRadius:$t}),[i.backgroundEnabled,i.curveEnabled,i.legacyBackground,Yt,Nt,$t]),_t=B(),At=n.useRef(null),Lt=n.useRef(i.width),Vt=n.useRef(null),Ht=n.useRef(null),qt=n.useRef(""),Gt=n.useRef(0),Kt=n.useRef(0),Qt=i.width>0?i.a.width/i.width:1;return o(r,{children:[i.curveEnabled||i.legacyBackground?e(l,{x:i.a.x,y:i.a.y,offsetX:Nt/Qt,offsetY:Nt/Qt,rotation:i.a.rotation,scaleX:Qt,scaleY:Qt,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled,opacity:i.backgroundOpacity*vt,fill:i.backgroundColor,width:i.width+2*Nt,height:Bt+2*Nt,cornerRadius:$t}):e(s,{name:"rendertag-background",x:i.a.x,y:i.a.y,offsetY:-Dt/Qt,rotation:i.a.rotation,scaleX:Qt,scaleY:Qt,hideInExport:!i.showInExport,listening:!1,visible:i.backgroundEnabled&&Jt.length>0,opacity:i.backgroundOpacity*vt,fill:i.backgroundColor,data:Jt}),e(c,{ref:U,name:"rendertag-text",x:i.a.x,y:i.a.y,rotation:i.a.rotation,scaleX:Qt,scaleY:Qt,offsetY:Qt>0?-Dt/Qt:0,width:i.width,height:Math.max(i.height,kt),listening:!1,visible:!dt&&!!(Mt?jt:St),editModeEnabled:dt,opacity:vt,shadowEnabled:i.shadowEnabled,shadowBlur:i.shadowBlur,shadowOffsetX:i.shadowOffsetX,shadowOffsetY:i.shadowOffsetY,shadowColor:i.shadowColor,shadowOpacity:i.shadowOpacity,hideInExport:!i.showInExport,sceneFunc:(t,e)=>{const r=t._context;if(r){try{if(Mt){if(!jt||!Rt){return}const t=(i.width-Rt.width)/2-Rt.x;r.save(),r.translate(t,-Rt.y),x({layout:jt,ctx:r}),r.restore()}else{if(!St){return}g({layout:St,width:Ot,ctx:r,pixelRatio:1})}}catch(o){console.error("render-tag draw failed:",o)}}}}),e(l,{ref:Z,name:"element",x:i.a.x,y:i.a.y,listening:i.selectable,rotation:i.a.rotation,width:i.a.width,height:Wt,visible:!dt,draggable:_t?i.draggable&&ot:i.draggable,preventDefault:!_t||ot,opacity:0,hideInExport:!0,id:i.id,onDragMove:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{i.set({x:t.target.x(),y:t.target.y()})},onDblClick:t=>{i.contentEditable&&(Ht.current={x:t.evt.clientX,y:t.evt.clientY},i.toggleEditMode(!0))},onDblTap:t=>{var e;if(i.contentEditable){const r=null===(e=t.evt.changedTouches)||void 0===e?void 0:e[0];Ht.current=r?{x:r.clientX,y:r.clientY}:null,i.toggleEditMode(!0)}},onTransformStart:t=>{et(!0),rt.current=i.height,Lt.current=i.width,Vt.current=null,At.current=t.target.position(),qt.current=p(t.target),Gt.current=i.a.rotation,Kt.current=i.height},onTransform:e=>{const r=e.target,o=qt.current,n="middle-left"===o||"middle-right"===o,a="top-center"===o||"bottom-center"===o,s=r.scaleX();if(n){const e=r.scaleX(),o=r.width()*e;let n=Math.max(o,v(Lt.current,i.fontSize));const a=i.toJSON();let s=0;if(i.curveEnabled){s=R(Object.assign(Object.assign({},a),{width:n}))}else{const{x:e,y:r,width:o,height:i}=a,l=t(a,["x","y","width","height"]),c=`${JSON.stringify(l)}|${ut}|${ht}|${ft}`;if(Vt.current&&Vt.current.signature===c&&n<=Vt.current.width){n=Vt.current.width,s=Vt.current.height}else{const t=t=>{const e=z(Object.assign(Object.assign({},a),{width:t}),{fontFamily:ut,color:ft,forEditor:!0,text:wt,textIsNormalized:!0});try{return f({html:L(e),width:t,accuracy:"performance"})}catch(Q){return{height:F(e).height,lines:[]}}};let e=t(n);const r=Math.max(0,...e.lines.map(t=>t.bounds.x+t.bounds.width));r>n+.5&&r<=Lt.current+.5&&(n=Math.min(Math.ceil(r),Lt.current),e=t(n),Vt.current={signature:c,width:n,height:e.height}),s=e.height}}n>o&&At.current&&r.position(At.current),r.width(n),r.scaleX(1),r.scaleY(1);let l=E.textVerticalResizeEnabled?Math.max(s,rt.current):s;"ellipsis"===E.textOverflow&&(l=rt.current),r.height(l),i.set({width:r.width(),height:l,x:r.x(),y:r.y()})}else if(a){const t="resize"===E.textOverflow?Mt?(null==Rt?void 0:Rt.height)||i.fontSize:kt:zt*i.fontSize,e=r.height()*r.scaleY();if(e<t){if(r.height(t),At.current){if("top-center"===qt.current){const e=Gt.current*Math.PI/180,o=Kt.current-t;r.x(At.current.x-o*Math.sin(e)),r.y(At.current.y+o*Math.cos(e))}else{r.position(At.current)}}}else{r.height(e)}r.scaleY(1),r.scaleX(1),Kt.current=Math.max(t,e),i.set({x:r.x(),y:r.y(),height:r.height(),rotation:r.rotation()})}else{r.scaleX(1),r.scaleY(1),i.set({fontSize:i.fontSize*s,letterSpacing:i.letterSpacing,width:r.width()*s,x:r.x(),y:r.y(),rotation:r.rotation(),height:r.height()*s,shadowBlur:i.shadowBlur*s,shadowOffsetX:i.shadowOffsetX*s,shadowOffsetY:i.shadowOffsetY*s,strokeWidth:i.strokeWidth*s,text:$(i.text,s)})}At.current=e.target.position()},onTransformEnd:t=>{et(!1);const e=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),i.set(Object.assign(Object.assign({},b(i,i.fontSize*e)),{width:t.target.width()*e,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),text:$(i.text,e)}))}}),dt&&e(a,{x:i.a.x,y:i.a.y,rotation:i.a.rotation,offsetY:-Dt,children:e(u,{divProps:{style:{zIndex:20}},children:e(J,{element:i,onChange:t=>{const e=W({oldText:T(i.text),newText:T(t),element:i});i.set(Object.assign({text:t},y(i,e)))},onBlur:()=>{d(i)&&(i.set(w(i)),i.toggleEditMode(!1))},clickCoords:Ht.current})})})]})});
|
package/canvas/rich-editor.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(o[n]=e[n])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(e);r<n.length;r++){t.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(e,n[r])&&(o[n[r]]=e[n[r]])}}return o};import{jsx as t}from"react/jsx-runtime";import o from"react";import n from"quill";import*as r from"mobx";import{nodeReaction as i}from"../utils/mobx.js";import{isAlive as l}from"mobx-state-tree";import{getQuillEditorHtml as a,getDir as s}from"@polotno/core/text-html";import{quillHtmlToModelText as d,isQuillFormatMixed as c,isBoldWeight as u}from"../utils/rich-text-html.js";import{resetStyleContent as f,wrapHtmlForRenderTag as p}from"@polotno/core/reset-style";import{layout as m}from"render-tag";import{removeTags as h}from"@polotno/core/text";import{cssFontFamily as g}from"@polotno/core/fonts";import{isGradient as b,firstStopColor as v}from"@polotno/core/gradient";import{getOptimalCaretColor as x}from"./text-element/caret-color.js";import{visiblePlaceholder as y}from"./text-element/placeholder.js";import{registerQuillUnrestrictedFontFormats as w}from"./quill-font-formats.js";import{ALL_QUILL_MIXED_KEYS as S,setActiveTextEditor as k,updateActiveTextFormat as q,clearActiveTextEditor as T,getActiveTextEditor as E}from"../utils/text-format-state.js";import C from"../utils/styled.js";import{applyOpacityToColor as L}from"@polotno/core/svg";export{L as applyOpacityToColor};export const quillRef=r.observable({enabled:!1,currentFormat:{},pendingToolbarInteraction:!1,editor:r.observable.object({instance:null},{},{deep:!1})});const F=S;export function syncQuillFormatState(e,t){const o=t?e.getFormat(t):{},n=function(e,t){const o={};if(!t||t.length<=0){return o}const n=e.getContents(t.index,t.length);for(const r of F){c(n,r)&&(o[r]=!0)}return o}(e,t);r.runInAction(()=>{quillRef.currentFormat=o}),q(o,n,t)}const R="polotno-saved-selection";function $(){const e=window.getSelection();if(!e||0===e.rangeCount||e.isCollapsed){return}if("undefined"==typeof Highlight||!CSS.highlights){return}const t=e.getRangeAt(0).cloneRange();CSS.highlights.set(R,new Highlight(t)),M()}function O(){"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.delete(R)}function M(){setTimeout(()=>{"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.has(R)&&(document.querySelector(".polotno-portal")||I(document.activeElement)?M():O())},300)}if("undefined"!=typeof document&&"undefined"!=typeof CSS&&CSS.highlights&&!document.querySelector("[data-polotno-highlight]")){const D=document.createElement("style");D.setAttribute("data-polotno-highlight",""),D.textContent=`::highlight(${R}) { background-color: Highlight; }`,document.head.appendChild(D)}let j=null;function N(e,t){"text"===e.type&&t.push(e);for(const o of e.children||[]){N(o,t)}}export function computeMarkerVars(e){var t,o,n;const r=e.fontSize;if(!r||r<=0){return null}const i=g(e.fontFamily),l=u(e.fontWeight)?700:400,a="italic"===e.fontStyle?"italic":"normal",s=null!=j?j:j=document.createElement("canvas").getContext("2d");s.fontKerning="normal",s.font=`${r}px ${i}`;const d=s.measureText("M"),c=(null!==(t=d.fontBoundingBoxAscent)&&void 0!==t?t:0)/r,f=(null!==(o=d.fontBoundingBoxDescent)&&void 0!==o?o:0)/r,h=`font-family:${i};font-size:${r}px;font-weight:${l};font-style:${a};line-height:${e.lineHeight};letter-spacing:${e.letterSpacing}em`,b=p(`<div style="${h}"><ul><li>x</li></ul><ol><li>x</li></ol></div>`);let v;try{v=m({html:b,width:Math.max(12*r,600)})}catch(T){return null}const x=[];N(v.layoutRoot,x);const y=x.find(e=>"•"===e.text),w=x.find(e=>/^\d/.test(e.text));if(!y){return null}const S=y.style.fontSize,k=S*(1+c-f)/2,q=2.1*r;return{bulletLeft:y.x,bulletTop:y.y-k,bulletFontSize:S,numberGap:w?Math.max(0,q-w.x-(null!==(n=w.width)&&void 0!==n?n:0)):0}}export const RichTextContainer=C("div",o.forwardRef)`
|
|
2
2
|
.ql-editor {
|
|
3
3
|
outline: none;
|
|
4
4
|
/* anchor for the absolutely-positioned ::before placeholder below */
|
|
@@ -6,6 +6,9 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
|
|
|
6
6
|
/* Element-level underline/strike — text-decoration doesn't inherit, so it is
|
|
7
7
|
applied here on the editable root (mirrors the static getHtml render). */
|
|
8
8
|
text-decoration: var(--polotno-text-decoration, none);
|
|
9
|
+
/* Chrome would otherwise break the underline at every descender; the
|
|
10
|
+
canvas draws one solid band. Mirrors getHtml (@polotno/core). */
|
|
11
|
+
text-decoration-skip-ink: none;
|
|
9
12
|
}
|
|
10
13
|
/* Placeholder for an empty editor. Quill toggles ql-blank + data-placeholder
|
|
11
14
|
but its core stylesheet (which paints it) is not loaded — do it here. Same
|
|
@@ -152,4 +155,4 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
|
|
|
152
155
|
&[style*='--polotno-gradient'] .ql-editor [style*='color:'] {
|
|
153
156
|
-webkit-text-fill-color: currentColor;
|
|
154
157
|
}
|
|
155
|
-
`;let
|
|
158
|
+
`;let z=["background","font-weight","color","gradient","font","italic","size","script","strike","underline","indent","list","direction"];export const setQuillFormats=e=>{z=e};let P=!1;function Q(){P||(P=!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 A(e){return e?"700":"400"}export const createQuill=(t,o)=>{w();const r=new n(t,{toolbar:!1,keyboard:!1,placeholder:(null==o?void 0:o.placeholder)||void 0,clipboard:{matchVisual:!1},formats:z});!function(t){const o=t.format.bind(t),n=t.formatText.bind(t),r=t.getFormat.bind(t);t.format=(e,t,n)=>"bold"===e?(Q(),o("font-weight",A(t),n)):o(e,t,n),t.formatText=(t,o,r,i,l)=>{if(r&&"object"==typeof r){if("bold"in r){Q();const{bold:l}=r,a=e(r,["bold"]);return n(t,o,Object.assign(Object.assign({},a),{"font-weight":A(l)}),i)}return n(t,o,r,i)}return"bold"===r?(Q(),n(t,o,"font-weight",A(i),l)):n(t,o,r,i,l)},t.getFormat=(...e)=>{const t=r(...e);return t&&null!=t["font-weight"]&&!("bold"in t)&&(t.bold=u(t["font-weight"])),t}}(r);const i=n.import("delta"),l=(e,t)=>t.compose((new i).retain(t.length(),{"font-weight":"700"}));return r.clipboard.addMatcher("STRONG",l),r.clipboard.addMatcher("B",l),r};export const setQuillContent=(e,t)=>{var o=e.clipboard.convert({html:"<div class='ql-editor' style='outline: none;'>"+t.replace(/\t/g,"")+"<p><br></p></div>",text:""});o.ops.forEach(e=>{"string"==typeof e.insert&&e.insert.includes("")&&(e.insert=e.insert.split("").join("\t"))}),e.setContents(o),e.history.clear()};export const RichEditor=({onBlur:e,onChange:n,element:c,clickCoords:u})=>{var f;const p=o.useRef(null),m=o.useRef(!1);o.useLayoutEffect(()=>{let t,o=!1,i=null,s=null,f=null,h=null,g=null;const b=()=>{if(!o&&p.current){try{t=createQuill(p.current,{placeholder:y(c.placeholder)}),r.runInAction(()=>{quillRef.editor.instance=t}),quillRef.pendingToolbarInteraction=!1,window.__polotnoQuill=t,k({instance:t,store:c.store,element:c});const o=e=>{e&&syncQuillFormatState(t,e)};t.on("text-change",()=>{const e=E(),r=e&&e.instance===t?e.lastSelection:null;o(t.getSelection()||r),setTimeout(()=>{var e,o;const r=t.root;if(!r||!r.isConnected){return}if(!l(c)){return}const i=r.innerHTML;m.current=!0,n(d(i,{fontSize:c.fontSize,fontFamily:c.fontFamily}));const a=t.getContents(),s=c.fill;let u=null,f=!0;for(const t of a.ops){if("string"!=typeof t.insert||"\n"===t.insert){continue}if(null===(e=t.attributes)||void 0===e?void 0:e.gradient){f=!1;break}const n=(null===(o=t.attributes)||void 0===o?void 0:o.color)||s;if(null===u){u=n}else if(n!==u){f=!1;break}}f&&u&&c.set({fill:u})},10)}),t.on("selection-change",o);const b=a(c,c.fontFamily);setQuillContent(t,b),m.current=!0;const v=p.current.closest(".polotno-workspace-inner"),x=null==v?void 0:v.scrollLeft,w=null==v?void 0:v.scrollTop;u?setCursorFromCoords(t,u):t.setSelection(0,0,"api"),v&&void 0!==x&&void 0!==w&&(v.scrollLeft=x,v.scrollTop=w);const S=t.root;h=t=>{const o=t.relatedTarget;(null==o?void 0:o.classList.contains("ql-clipboard"))||(I(o)||quillRef.pendingToolbarInteraction?$():setTimeout(()=>{quillRef.pendingToolbarInteraction||I(document.activeElement)?$():(O(),e())},0))},g=()=>{O()},S.addEventListener("blur",h),S.addEventListener("focus",g),i=S,s=e=>{var o,n;const r=t.getSelection();if(!r||0===r.length){return}const i=t.getText(r.index,r.length).replace(/\n$/,""),l=window.getSelection();let a="";if(l&&l.rangeCount>0){const e=l.getRangeAt(0);let t=e.cloneContents(),o=e.startContainer;o&&o.nodeType===Node.TEXT_NODE&&(o=o.parentNode);let n=e.endContainer;if(n&&n.nodeType===Node.TEXT_NODE&&(n=n.parentNode),o&&o===n){let e=o;for(;e&&e!==S&&e.nodeType===Node.ELEMENT_NODE;){const o=e.tagName;if("SPAN"!==o&&"EM"!==o&&"STRONG"!==o&&"U"!==o&&"S"!==o&&"A"!==o){break}const n=e.cloneNode(!1);n.appendChild(t),t=n,e=e.parentNode}}const r=document.createElement("div");r.appendChild(t),a=r.innerHTML}if(null===(o=e.clipboardData)||void 0===o||o.setData("text/plain",i),a){const t='<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>\x3c!--StartFragment--\x3e<div style="white-space: pre-wrap; overflow-wrap: break-word;">'+a+"</div>\x3c!--EndFragment--\x3e</body></html>";null===(n=e.clipboardData)||void 0===n||n.setData("text/html",t)}e.preventDefault()},f=e=>{s&&s(e);const o=t.getSelection();o&&o.length>0&&t.deleteText(o.index,o.length)},i.addEventListener("copy",s),i.addEventListener("cut",f)}catch(b){console.error("RichEditor init failed:",b)}}};return p.current?b():queueMicrotask(b),()=>{o=!0,i&&h&&i.removeEventListener("blur",h),i&&g&&i.removeEventListener("focus",g),O(),i&&s&&f&&(i.removeEventListener("copy",s),i.removeEventListener("cut",f)),r.runInAction(()=>{quillRef.editor.instance=null,quillRef.currentFormat={}}),T(t),delete window.__polotnoQuill}},[]),o.useEffect(()=>i(c,()=>`${c.text}\0${c.fontSize}\0${c.fontFamily}`,()=>{const e=quillRef.editor.instance;if(!e){return}if(m.current){return void(m.current=!1)}const t=e.getSelection();setQuillContent(e,a(c,c.fontFamily)),m.current=!0,t&&(e.setSelection(t.index,t.length),syncQuillFormatState(e,e.getSelection()))},{fireImmediately:!0}),[]),o.useEffect(()=>i(c,()=>`${c.fontFamily}\0${c.fontSize}\0${c.fontWeight}\0${c.fontStyle}\0${c.lineHeight}\0${c.letterSpacing}\0${s(h(c.text))}`,()=>{const e=p.current;if(!e){return}const t="ltr"===s(h(c.text)),o=computeMarkerVars(c);o&&e.style.setProperty("--rt-number-gap",`${o.numberGap}px`),o&&t?(e.style.setProperty("--rt-bullet-left",`${o.bulletLeft}px`),e.style.setProperty("--rt-bullet-top",`${o.bulletTop}px`),e.style.setProperty("--rt-bullet-fs",`${o.bulletFontSize}px`),e.classList.add("rt-marker-geom")):e.classList.remove("rt-marker-geom")},{fireImmediately:!0}),[]),o.useEffect(()=>{window.addEventListener("blur",e);const t=t=>{var o;const n=t.target;"number"==typeof(null==n?void 0:n.nodeType)&&(null===(o=p.current)||void 0===o?void 0:o.contains(n))||I(n)||quillRef.pendingToolbarInteraction||(O(),e())};return window.addEventListener("mousedown",t),window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",e),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t)}},[]);const w={color:c.fill};c.fill.indexOf("gradient")>=0&&(w["--polotno-gradient"]=c.fill,w.color="transparent"),c.strokeWidth>0&&(w.WebkitTextStroke=`${c.strokeWidth}px ${v(c.stroke)}`,w.paintOrder="stroke fill");const S=b(c.fill)?v(c.fill):x(c),q=c.a.rotation*Math.PI/180,C=c.shadowOffsetX*Math.cos(q)+c.shadowOffsetY*Math.sin(q),F=-c.shadowOffsetX*Math.sin(q)+c.shadowOffsetY*Math.cos(q);return t(RichTextContainer,{ref:p,style:Object.assign(Object.assign({},w),{fontSize:c.fontSize,fontFamily:g(c.fontFamily),fontWeight:c.fontWeight,fontStyle:c.fontStyle,"--polotno-text-decoration":c.textDecoration||"none",textTransform:c.textTransform,width:c.a.width,lineHeight:c.lineHeight,letterSpacing:c.letterSpacing*c.fontSize+"px",textAlign:c.align,opacity:Math.max(c.a.opacity,.2),textShadow:c.shadowEnabled?`${C}px ${F}px ${c.shadowBlur}px ${L(c.shadowColor,null!==(f=c.shadowOpacity)&&void 0!==f?f:1)}`:void 0,caretColor:S}),dir:s(h(c.text))})};function I(e){const t=e;return!("function"!=typeof(null==t?void 0:t.closest)||!t.closest(".sketch-picker")&&!t.closest("[data-polotno-keep-focus]")&&!t.closest(".polotno-portal")&&!t.closest(".polotno-toolbar"))}export function setCursorFromCoords(e,t){if(!e||!t){return}const{x:o,y:r}=t;try{let t=null;if(document.caretRangeFromPoint){t=document.caretRangeFromPoint(o,r)}else if(document.caretPositionFromPoint){const e=document.caretPositionFromPoint(o,r);e&&(t=document.createRange(),t.setStart(e.offsetNode,e.offset))}if(t){const o=n.find(t.startContainer,!0);if(o){const n=o.offset(e.scroll)+t.startOffset;return void e.setSelection(n,0,"api")}}}catch(i){}e.setSelection(0,0,"api")}
|
package/canvas/text-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as t,Fragment as e,jsxs as r}from"react/jsx-runtime";import n from"react";import{observer as o}from"mobx-react-lite";import{Group as i,Path as a,Text as s,TextPath as l}from"react-konva";import{Html as c}from"react-konva-utils";import{nodeAutorun as u}from"../utils/mobx.js";import d from"konva";import{parsePath as f,roundCommands as h}from"svg-round-corners/lib/index.js";import{generateBackgroundShapeFromRects as g,textLinesToRects as p}from"@polotno/core/background-shape";import{useColor as m}from"./use-color.js";import{useWorkspaceStyle as x}from"./workspace-style.js";import{ROLES as w}from"../model/store.js";import{incrementLoader as v}from"../utils/loader.js";import{activeAnchorOf as y,scaledFontSizeAttrs as b,roundedFontSizeAttrs as S,roundedTextDecorationAttrs as E,textMinResizeWidth as z}from"../utils/px.js";import{isFontLoaded as k}from"../utils/fonts.js";import{flags as O}from"../utils/flags.js";import{removeTags as R}from"@polotno/core/text";import{getDir as F,getCurvePath as A,resolveLineHeight as M,normalizeStrokeLineJoin as j}from"@polotno/core/text-html";import{applyFilter as L}from"./apply-filters.js";import{useFadeIn as C}from"./use-fadein.js";import{isTouchDevice as T}from"../utils/screen.js";import{isAlive as H,getParent as P}from"mobx-state-tree";import{getLimitedFontSize as X}from"./text-element/max-font-size.js";import{getOptimalCaretColor as Y}from"./text-element/caret-color.js";import{visiblePlaceholder as W}from"./text-element/placeholder.js";import{hasMeasuredHeight as D,resolveTextBoxHeight as $}from"@polotno/core/text-types";let B;function I(){return B||(B=document.getElementById("polotno-text-style"),B||(B=document.createElement("style"),B.id="polotno-text-style",document.head.appendChild(B)),B)}d._fixTextRendering=!0;const N={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 J=o(({textNodeRef:e,element:r,onBlur:o,selectAll:i,cursorPosition:a})=>{const[s,l]=n.useState(N),c=e.current;n.useLayoutEffect(()=>{if(!c){return}const t={};t.width=c.width()-2*c.padding()+"px",t.height=c.height()-2*c.padding()+c.fontSize()*c.lineHeight()+"px",t.fontSize=c.fontSize()+"px",t.lineHeight=c.lineHeight()+.01,t.fontFamily=c.fontFamily(),t.textAlign=c.align(),t.color=c.fill(),t.fontWeight=r.fontWeight,t.fontStyle=r.fontStyle,t.letterSpacing=r.letterSpacing+"em",t.opacity=Math.max(r.a.opacity,.2),t.textTransform=r.textTransform,t.caretColor=Y(r);const e=`\n .polotno-input::placeholder {\n color: ${s.color};\n opacity: 0.6;\n }\n `,n=I();n.innerHTML="",n.appendChild(document.createTextNode(e)),JSON.stringify(t)!==JSON.stringify(s)&&l(t)});const u=n.useRef(null);if(n.useLayoutEffect(()=>{var t;const e=u.current;if(!e){return}null===(t=u.current)||void 0===t||t.focus({preventScroll:!0});const r=a||e.value.length;e.selectionStart=e.selectionEnd=r,i&&(null==e||e.select(),document.execCommand("selectAll",!1,void 0))},[]),n.useEffect(()=>{window.addEventListener("blur",o);const t=t=>{var e;(null===(e=u.current)||void 0===e?void 0:e.contains(t.target))||o()};return window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",o),window.removeEventListener("touchstart",t)}},[]),!c){return null}let d=0;const f=c.textArr.length*c.lineHeight()*c.fontSize(),h=$(r.a,f);"middle"===r.verticalAlign&&(d=(h-f)/2),"bottom"===r.verticalAlign&&(d=h-f);const g=R(r.text);return t("textarea",{className:"polotno-input",ref:u,dir:F(g),style:Object.assign(Object.assign(Object.assign({},N),s),{paddingTop:d+"px"}),value:g,onChange:t=>{const e=X({oldText:r.text,newText:t.target.value,element:r});r.set(Object.assign({text:t.target.value},b(r,e)))},placeholder:W(r.placeholder),onBlur:o})}),_=e=>t(c,{children:t(J,Object.assign({},e))});export const useFontLoader=(t,e,r,o,i)=>{const[a,s]=n.useReducer(t=>t+1,0),l=n.useRef(k(e,r,o)),c=n.useRef(null);return n.useLayoutEffect(()=>{if(l.current=k(e,r,o),l.current){return}let n=!0;return(async()=>{l.current=!1,s();const a=v(`text ${e}`);await t.loadFont(e,r,o,i),n?(c.current=a,l.current=!0,s()):a()})(),()=>{c.current&&(c.current(),c.current=null),n=!1}},[e,r,o]),n.useEffect(()=>{l.current&&setTimeout(()=>{c.current&&(c.current(),c.current=null)})},[l.current]),[l.current]};export const useFontFamiliesLoader=(t,e,r,o,i)=>{const[a,s]=n.useReducer(t=>t+1,0),l=e.join("\0"),c=n.useRef(e.every(t=>k(t,r,o))),u=n.useRef(null);return n.useLayoutEffect(()=>{if(c.current=e.every(t=>k(t,r,o)),c.current){return}let n=!0;return(async()=>{c.current=!1,s();const a=v(`text fonts ${l}`);await Promise.all(e.map(e=>t.loadFont(e,r,o,i))),n?(u.current=a,c.current=!0,s()):a()})(),()=>{u.current&&(u.current(),u.current=null),n=!1}},[l,r,o]),n.useEffect(()=>{c.current&&setTimeout(()=>{u.current&&(u.current(),u.current=null)})},[c.current]),[c.current]};export const getLineHeight=({fontLoaded:t,fontFamily:e,fontSize:r,lineHeight:o})=>n.useMemo(()=>M({fontFamily:e,fontSize:r,lineHeight:o}),[t,e,r,o]);export function usePrevious(t){const e=n.useRef(t),r=n.useRef(t);return n.useMemo(()=>{r.current=e.current,e.current=t},[t]),r.current}function V(t,e,r=!1){const n=t.fontSize(),o=t.height(),i=R(e.text);let a=e.a.fontSize;t.height(void 0);const s=Math.round(2*e.a.fontSize)-1;for(let l=1;l<s;l++){const n=D(e.a)&&t.height()>e.a.height;let o=i.split("\n").join(" ").split(/[\s-]+/).reduce((t,e)=>(e=e.toLowerCase(),/[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\uAC00-\uD7AF]/.test(e)?t.concat(e.split("")):t.concat(e)),[]),s=t.textArr.map(t=>t.text.toLowerCase()).join(";");const l=o.find(t=>!s.includes(t)||(s=s.replace(t,""),!1));if(!(n||l&&!r)){break}a-=.5,t.fontSize(a)}return t.fontSize(n),t.height(o),a}export{getCurvePath}from"@polotno/core/text-html";export const TextElement=o(({element:o,store:c})=>{const{textOverflowIndicatorStyle:d}=x(),k=n.useRef(null),M=n.useRef(null),X=n.useRef(null),Y=n.useRef([]),B=n.useRef(!0),[,I]=n.useReducer(t=>t+1,0),{editorEnabled:N,selectAll:J}=(t=>{const[e,r]=n.useState(!1),o=n.useRef(!1);return n.useEffect(()=>{var e=!0;return setTimeout(()=>{e&&(t._editModeEnabled&&(o.current=!0),r(!0),setTimeout(()=>{o.current=!1},50))},50),()=>{e=!1}},[]),{editorEnabled:e&&t._editModeEnabled,selectAll:o.current}})(o),[q,U]=n.useState(!1),[Z,G]=n.useState(!1),K=n.useRef(o.a.height),Q=n.useRef(0),tt=n.useRef(""),et=n.useRef(0),rt=o.isSelected,nt=usePrevious(rt),{textVerticalResizeEnabled:ot}=O,it=O.autoDeleteEmptyText,at=usePrevious(o.fontFamily),[st,lt]=n.useState([]);n.useEffect(()=>{var t,e;const r=null!==(e=null===(t=k.current)||void 0===t?void 0:t.textArr)&&void 0!==e?e:[];JSON.stringify(r)!==JSON.stringify(st)&<(r)}),n.useEffect(()=>{if(o.a.width||"tablecell"===o.type){return}const t=k.current;t.width(600),o.set({width:1.4*t.getTextWidth()})},[]),n.useEffect(()=>{nt&&!rt&&it&&""===o.text&&o.removable&&!W(o.placeholder)&&c.deleteElements([o.id])},[it,o.placeholder,o.removable,o.text,rt,nt]);const ct=()=>{if(o.curveEnabled){const t=k.current;null==t||t.clearCache(),null==t||t.setAttrs({filters:[]});const e=M.current;e&&L(e,o)}else{const t=k.current;t&&L(t,o)}};n.useLayoutEffect(()=>{if(!Z){return u(o,()=>{ct()})}}),n.useEffect(()=>{const t=Y.current;t.length>0&&(Y.current=[],t.forEach(t=>t()))}),n.useEffect(()=>()=>{B.current=!1;const t=Y.current;t.length>0&&(Y.current=[],t.forEach(t=>t()))},[]);let ut=R(o.text);"uppercase"===o.textTransform&&(ut=ut.toUpperCase());const[dt]=useFontLoader(c,o.fontFamily,o.fontStyle,o.fontWeight,o.text||o.placeholder),ft=()=>{if(o.curveEnabled){const t=M.current;return t.getSelfRect().height||t.fontSize()}const t=k.current.clone({height:void 0}),e=Math.ceil(t.fontSize()*t.lineHeight()*t.textArr.length+1);return t.destroy(),e};n.useLayoutEffect(()=>{if(!dt){return}if("tablecell"===o.type){const{textOverflow:t}=O;if("resize"===t){const t=ft(),e=H(o)?P(o,2):null;(null==e?void 0:e.setCellContentHeight)&&e.setCellContentHeight(o.id,t)}else if("change-font-size"===t){const t=k.current;if(t){const e=V(t,o,O.textSplitAllowed);e!==o.fontSize&&c.history.ignore(()=>{o.set({fontSize:e})})}}return}const{textOverflow:t,textSplitAllowed:e}=O,r=(t,e,r,n)=>{const i=v(`text ${o.id}`),a=()=>{c.history.ignore(t,e,r).then(()=>{B.current?(Y.current.push(i),I()):i()})};n?queueMicrotask(a):a()};if(!D(o)){const t=ft();return void r(()=>{D(o)||o.set({height:t})},void 0,void 0,!0)}if(!c.isPlaying){if("change-font-size"!==t||q){if("resize"===t){const t=ft();ot&&o.height<t&&!q&&r(()=>{var e;H(o)&&o.set({height:t}),null===(e=k.current)||void 0===e||e.height(t)},!1,!0),ot||o.height===t||q||r(()=>{var e;H(o)&&o.set({height:t}),null===(e=k.current)||void 0===e||e.height(t)},!1,!0)}}else{const t=k.current,n=o.curveEnabled?o.fontSize:V(t,o,e);if(n!==o.fontSize){return void r(()=>{o.set(b(o,n))},!1,!0)}const i=ft();o.height===i||ot||r(()=>{o.set({height:i})},!1,!0)}}}),n.useLayoutEffect(()=>{var t;if(dt&&o.curveEnabled){const e=null===(t=M.current)||void 0===t?void 0:t.getSelfRect().width;if(e){const t=e-o.a.width,r=o.a.rotation*Math.PI/180,n=-t/2*Math.cos(r),i=-t/2*Math.sin(r);o.set({width:e,x:o.a.x+n,y:o.a.y+i})}}},[dt,o.curveEnabled,o.curvePower,o.text,o.fontSize,o.fontFamily,o.fontWeight,o.fontStyle,o.letterSpacing]),n.useLayoutEffect(()=>{const t=k.current;t&&(t.width(t.width()+1e-8),t.width(t.width()-1e-8),t._setTextData(),ct())},[dt]);const ht=n.useRef(null),gt=n.useRef(o.width),pt=n.useRef(0),mt=t=>{t.evt.preventDefault();const e=c.selectedShapes.find(t=>t===o);e&&o.contentEditable&&(pt.current=function(t){var e;const r=t.target,n=function(t){var e=t.getAbsoluteTransform().copy();e.invert();var r=t.getStage().getPointerPosition();return e.point(r)}(r),o=r.textArr,i=Math.floor(n.y/(r.fontSize()*r.lineHeight())),a=o.slice(0,i).reduce((t,e)=>t+e.text.length,i),s=null!==(e=o[i])&&void 0!==e?e:o[0];let l=0;return"right"===r.align()?l=r.width()-s.width:"center"===r.align()&&(l=r.width()/2-s.width/2),a+Math.round((n.x-l)/s.width*s.text.length)}(t),o.toggleEditMode())},xt=W(o.placeholder),wt=!ut&&xt?.6:o.a.opacity;C(k,wt);const vt=getLineHeight({fontLoaded:dt,fontFamily:o.fontFamily,fontSize:o.a.fontSize,lineHeight:o.lineHeight}),yt=o.selectable||c.role===w.ADMIN,bt=m(o),St=m(o,o.stroke,"stroke"),Et=n.useMemo(()=>o.backgroundEnabled?o.curveEnabled?function({width:t,height:e,padding:r=0,cornerRadius:n=0}){const o=-r,i=-r,a=t+r,s=e+r,l=f(`M ${o} ${i} L ${a} ${i} L ${a} ${s} L ${o} ${s} Z`);return h(l,n).path}({width:o.a.width,height:o.a.height,cornerRadius:o.backgroundCornerRadius*(o.a.fontSize*vt*.5),padding:o.backgroundPadding*(o.a.fontSize*vt*.5)}):function({lines:t,lineHeight:e,width:r,align:n="left",padding:o=0,cornerRadius:i=0}){const a=p({lines:t,lineHeight:e,width:r,align:n});return g({rects:a,padding:o,cornerRadius:i})}({lines:JSON.parse(JSON.stringify(st)),cornerRadius:o.backgroundCornerRadius*(o.a.fontSize*vt*.5),lineHeight:vt*o.a.fontSize,padding:o.backgroundPadding*(o.a.fontSize*vt*.5),width:o.a.width,align:o.align}):"",[o.backgroundEnabled,o.curveEnabled,o.backgroundCornerRadius,o.a.fontSize,o.a.height,vt,o.backgroundPadding,o.a.width,o.align,st]),zt=T();let kt=0;if(!o.curveEnabled){const t=st.length*vt*o.a.fontSize,e=$(o.a,t);"middle"===o.verticalAlign?kt=(e-t)/2:"bottom"===o.verticalAlign&&(kt=e-t)}const Ot=o.curveEnabled?A(o.a.width,o.a.height,o.curvePower,o.a.fontSize):"",Rt=dt?'"'+o.fontFamily+'"':at===o.fontFamily?"Arial":'"'+at+'"',Ft=F(ut);return r(e,{children:[t(a,{ref:X,x:o.a.x,y:o.a.y,rotation:o.a.rotation,hideInExport:!o.showInExport,listening:!1,visible:o.backgroundEnabled,opacity:o.backgroundOpacity*wt,data:Et,fill:o.backgroundColor,offsetY:-kt}),t(a,{data:Ot,stroke:d.stroke,strokeWidth:1,x:o.a.x,y:o.a.y,rotation:o.a.rotation,visible:!1}),t(l,Object.assign({ref:M,visible:o.curveEnabled,data:Ot,text:ut||xt,listening:!1,align:"center",textBaseline:"middle",direction:Ft},bt,St,{strokeWidth:o.strokeWidth,lineJoin:j(o.strokeLineJoin),fillAfterStrokeEnabled:!0,fontSize:o.a.fontSize,fontFamily:`"${o.fontFamily}", "${at}"`,fontStyle:o.fontStyle+" "+o.fontWeight,textDecoration:o.textDecoration.trim(),letterSpacing:o.letterSpacing*o.a.fontSize,x:o.a.x,y:o.a.y,rotation:o.a.rotation,opacity:o._editModeEnabled?.3:wt,hideInExport:!o.showInExport,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity})),t(s,Object.assign({ref:k,id:o.selectable?o.id:void 0,name:o.selectable?"element":"",hideInExport:!o.showInExport,editModeEnabled:o._editModeEnabled,x:o.a.x,y:o.a.y,rotation:o.a.rotation,width:o.a.width,height:D(o.a)?o.a.height:void 0,text:ut||xt,direction:Ft},bt,St,{lineJoin:j(o.strokeLineJoin),strokeWidth:o.strokeWidth,fillAfterStrokeEnabled:!0,fontSize:o.a.fontSize,fontFamily:Rt,fontStyle:o.fontStyle+" "+o.fontWeight,textDecoration:o.textDecoration,align:o.align,verticalAlign:o.verticalAlign,draggable:zt?o.draggable&&rt:o.draggable,preventDefault:!zt||rt,opacity:o.curveEnabled?0:wt,visible:!o._editModeEnabled,ellipsis:"ellipsis"===O.textOverflow,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity,lineHeight:vt,letterSpacing:o.letterSpacing*o.a.fontSize,listening:!!o.selectable&&yt,onDragStart:()=>{G(!0)},onDragMove:t=>{o.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{G(!1),o.set({x:t.target.x(),y:t.target.y()})},onClick:mt,onTap:mt,onTransformStart:()=>{U(!0);const t=k.current;K.current=t.height(),gt.current=o.a.width,ht.current=t.position(),Q.current=t.rotation(),et.current=t.height(),tt.current=y(t)},onTransform:t=>{var e,r;const n=t.target;null===(e=X.current)||void 0===e||e.setAttrs({x:n.x(),y:n.y(),rotation:n.rotation(),scale:n.scale()});const i=tt.current;if("middle-left"===i||"middle-right"===i){const t=n.scaleX(),e=n.width()*t,r=z(gt.current,o.a.fontSize);let i=e;e<r&&(i=r,ht.current&&n.position(ht.current)),n.width(i),n.scaleX(1),n.scaleY(1);let a=ft();if("ellipsis"===O.textOverflow){a=K.current}else{const t=Math.max(a,K.current);n.height(t),o.set({height:n.height()})}const s=O.textVerticalResizeEnabled?Math.max(a,K.current):ft();o.set({x:n.x(),width:n.width(),rotation:n.rotation(),height:s}),n.skewX(0),n.skewY(0),ct()}if("top-center"===i||"bottom-center"===i){const e="resize"===O.textOverflow?ft():vt*o.a.fontSize,r=t.target.height()*t.target.scaleY();if(r<e){if(t.target.height(e),ht.current){if("top-center"===tt.current){const r=Q.current*Math.PI/180,n=et.current-e;t.target.x(ht.current.x-n*Math.sin(r)),t.target.y(ht.current.y+n*Math.cos(r))}else{t.target.position(ht.current)}}}else{t.target.height(r)}t.target.scaleY(1),t.target.scaleX(1),t.target.rotation(Q.current),t.target.skewX(0),t.target.skewY(0),et.current=Math.max(e,r)}ht.current=t.target.position();const a=t.target.scaleX();null===(r=X.current)||void 0===r||r.setAttrs({scaleX:1,scaleY:1}),t.target.scaleX(1),t.target.scaleY(1),o.set({fontSize:o.a.fontSize*a,width:t.target.width()*a,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*a,shadowBlur:o.shadowBlur*a,shadowOffsetX:o.shadowOffsetX*a,shadowOffsetY:o.shadowOffsetY*a,strokeWidth:o.strokeWidth*a})},onTransformEnd:t=>{var e;const r=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),o.set(Object.assign(Object.assign({},S(o,o.fontSize*r)),{width:Math.ceil(t.target.width()*r),x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*r})),null===(e=X.current)||void 0===e||e.setAttrs({scaleX:1,scaleY:1}),U(!1)}})),N&&o.contentEditable&&t(i,{x:o.a.x,y:o.a.y,rotation:o.a.rotation,children:t(_,{textNodeRef:k,element:o,selectAll:J,cursorPosition:pt.current,onBlur:()=>{o.set(E(o)),o.toggleEditMode(!1)}})})]})});
|
|
1
|
+
import{jsx as t,Fragment as e,jsxs as r}from"react/jsx-runtime";import n from"react";import{observer as o}from"mobx-react-lite";import{Group as i,Path as a,Text as s,TextPath as l}from"react-konva";import{Html as c}from"react-konva-utils";import{nodeAutorun as u}from"../utils/mobx.js";import d from"konva";import{parsePath as f,roundCommands as h}from"svg-round-corners/lib/index.js";import{generateBackgroundShapeFromRects as g,textLinesToRects as p}from"@polotno/core/background-shape";import{useColor as m}from"./use-color.js";import{useWorkspaceStyle as x}from"./workspace-style.js";import{ROLES as w}from"../model/store.js";import{incrementLoader as v}from"../utils/loader.js";import{activeAnchorOf as y,scaledFontSizeAttrs as b,roundedFontSizeAttrs as S,roundedTextDecorationAttrs as E,textMinResizeWidth as z}from"../utils/px.js";import{isFontLoaded as k}from"../utils/fonts.js";import{flags as O}from"../utils/flags.js";import{removeTags as R}from"@polotno/core/text";import{getDir as F,getCurvePath as A,resolveLineHeight as M,normalizeStrokeLineJoin as j}from"@polotno/core/text-html";import{cssFontFamily as L}from"@polotno/core/fonts";import{FIT_FONT_SIZE_STEP as C,FIT_HEIGHT_EPSILON as T}from"@polotno/core/fit-text";import{applyFilter as H}from"./apply-filters.js";import{useFadeIn as P}from"./use-fadein.js";import{isTouchDevice as X}from"../utils/screen.js";import{isAlive as Y,getParent as W}from"mobx-state-tree";import{getLimitedFontSize as D}from"./text-element/max-font-size.js";import{getOptimalCaretColor as $}from"./text-element/caret-color.js";import{visiblePlaceholder as B}from"./text-element/placeholder.js";import{hasMeasuredHeight as I,resolveTextBoxHeight as N}from"@polotno/core/text-types";let J;function _(){return J||(J=document.getElementById("polotno-text-style"),J||(J=document.createElement("style"),J.id="polotno-text-style",document.head.appendChild(J)),J)}d._fixTextRendering=!0;const V={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 q=o(({textNodeRef:e,element:r,onBlur:o,selectAll:i,cursorPosition:a})=>{const[s,l]=n.useState(V),c=e.current;n.useLayoutEffect(()=>{if(!c){return}const t={};t.width=c.width()-2*c.padding()+"px",t.height=c.height()-2*c.padding()+c.fontSize()*c.lineHeight()+"px",t.fontSize=c.fontSize()+"px",t.lineHeight=c.lineHeight()+.01,t.fontFamily=c.fontFamily(),t.textAlign=c.align(),t.color=c.fill(),t.fontWeight=r.fontWeight,t.fontStyle=r.fontStyle,t.letterSpacing=r.letterSpacing+"em",t.opacity=Math.max(r.a.opacity,.2),t.textTransform=r.textTransform,t.caretColor=$(r);const e=`\n .polotno-input::placeholder {\n color: ${s.color};\n opacity: 0.6;\n }\n `,n=_();n.innerHTML="",n.appendChild(document.createTextNode(e)),JSON.stringify(t)!==JSON.stringify(s)&&l(t)});const u=n.useRef(null);if(n.useLayoutEffect(()=>{var t;const e=u.current;if(!e){return}null===(t=u.current)||void 0===t||t.focus({preventScroll:!0});const r=a||e.value.length;e.selectionStart=e.selectionEnd=r,i&&(null==e||e.select(),document.execCommand("selectAll",!1,void 0))},[]),n.useEffect(()=>{window.addEventListener("blur",o);const t=t=>{var e;(null===(e=u.current)||void 0===e?void 0:e.contains(t.target))||o()};return window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",o),window.removeEventListener("touchstart",t)}},[]),!c){return null}let d=0;const f=c.textArr.length*c.lineHeight()*c.fontSize(),h=N(r.a,f);"middle"===r.verticalAlign&&(d=(h-f)/2),"bottom"===r.verticalAlign&&(d=h-f);const g=R(r.text);return t("textarea",{className:"polotno-input",ref:u,dir:F(g),style:Object.assign(Object.assign(Object.assign({},V),s),{paddingTop:d+"px"}),value:g,onChange:t=>{const e=D({oldText:r.text,newText:t.target.value,element:r});r.set(Object.assign({text:t.target.value},b(r,e)))},placeholder:B(r.placeholder),onBlur:o})}),U=e=>t(c,{children:t(q,Object.assign({},e))});export const useFontLoader=(t,e,r,o,i)=>{const[a,s]=n.useReducer(t=>t+1,0),l=n.useRef(k(e,r,o)),c=n.useRef(null);return n.useLayoutEffect(()=>{if(l.current=k(e,r,o),l.current){return}let n=!0;return(async()=>{l.current=!1,s();const a=v(`text ${e}`);await t.loadFont(e,r,o,i),n?(c.current=a,l.current=!0,s()):a()})(),()=>{c.current&&(c.current(),c.current=null),n=!1}},[e,r,o]),n.useEffect(()=>{l.current&&setTimeout(()=>{c.current&&(c.current(),c.current=null)})},[l.current]),[l.current]};export const useFontFamiliesLoader=(t,e,r,o,i)=>{const[a,s]=n.useReducer(t=>t+1,0),l=e.join("\0"),c=n.useRef(e.every(t=>k(t,r,o))),u=n.useRef(null);return n.useLayoutEffect(()=>{if(c.current=e.every(t=>k(t,r,o)),c.current){return}let n=!0;return(async()=>{c.current=!1,s();const a=v(`text fonts ${l}`);await Promise.all(e.map(e=>t.loadFont(e,r,o,i))),n?(u.current=a,c.current=!0,s()):a()})(),()=>{u.current&&(u.current(),u.current=null),n=!1}},[l,r,o]),n.useEffect(()=>{c.current&&setTimeout(()=>{u.current&&(u.current(),u.current=null)})},[c.current]),[c.current]};export const getLineHeight=({fontLoaded:t,fontFamily:e,fontSize:r,lineHeight:o})=>n.useMemo(()=>M({fontFamily:e,fontSize:r,lineHeight:o}),[t,e,r,o]);export function usePrevious(t){const e=n.useRef(t),r=n.useRef(t);return n.useMemo(()=>{r.current=e.current,e.current=t},[t]),r.current}function Z(t,e,r=!1){const n=t.fontSize(),o=t.height(),i=R(e.text);let a=e.a.fontSize;t.height(void 0);const s=Math.round(2*e.a.fontSize)-1;for(let l=1;l<s;l++){const n=I(e.a)&&t.height()>e.a.height+T;let o=i.split("\n").join(" ").split(/[\s-]+/).reduce((t,e)=>(e=e.toLowerCase(),/[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\uAC00-\uD7AF]/.test(e)?t.concat(e.split("")):t.concat(e)),[]),s=t.textArr.map(t=>t.text.toLowerCase()).join(";");const l=o.find(t=>!s.includes(t)||(s=s.replace(t,""),!1));if(!(n||l&&!r)){break}a-=C,t.fontSize(a)}return t.fontSize(n),t.height(o),a}export{getCurvePath}from"@polotno/core/text-html";export const TextElement=o(({element:o,store:c})=>{const{textOverflowIndicatorStyle:d}=x(),k=n.useRef(null),M=n.useRef(null),C=n.useRef(null),T=n.useRef([]),D=n.useRef(!0),[,$]=n.useReducer(t=>t+1,0),{editorEnabled:J,selectAll:_}=(t=>{const[e,r]=n.useState(!1),o=n.useRef(!1);return n.useEffect(()=>{var e=!0;return setTimeout(()=>{e&&(t._editModeEnabled&&(o.current=!0),r(!0),setTimeout(()=>{o.current=!1},50))},50),()=>{e=!1}},[]),{editorEnabled:e&&t._editModeEnabled,selectAll:o.current}})(o),[V,q]=n.useState(!1),[G,K]=n.useState(!1),Q=n.useRef(o.a.height),tt=n.useRef(0),et=n.useRef(""),rt=n.useRef(0),nt=o.isSelected,ot=usePrevious(nt),{textVerticalResizeEnabled:it}=O,at=O.autoDeleteEmptyText,st=usePrevious(o.fontFamily),[lt,ct]=n.useState([]);n.useEffect(()=>{var t,e;const r=null!==(e=null===(t=k.current)||void 0===t?void 0:t.textArr)&&void 0!==e?e:[];JSON.stringify(r)!==JSON.stringify(lt)&&ct(r)}),n.useEffect(()=>{if(o.a.width||"tablecell"===o.type){return}const t=k.current;t.width(600),o.set({width:1.4*t.getTextWidth()})},[]),n.useEffect(()=>{ot&&!nt&&at&&""===o.text&&o.removable&&!B(o.placeholder)&&c.deleteElements([o.id])},[at,o.placeholder,o.removable,o.text,nt,ot]);const ut=()=>{if(o.curveEnabled){const t=k.current;null==t||t.clearCache(),null==t||t.setAttrs({filters:[]});const e=M.current;e&&H(e,o)}else{const t=k.current;t&&H(t,o)}};n.useLayoutEffect(()=>{if(!G){return u(o,()=>{ut()})}}),n.useEffect(()=>{const t=T.current;t.length>0&&(T.current=[],t.forEach(t=>t()))}),n.useEffect(()=>()=>{D.current=!1;const t=T.current;t.length>0&&(T.current=[],t.forEach(t=>t()))},[]);let dt=R(o.text);"uppercase"===o.textTransform&&(dt=dt.toUpperCase());const[ft]=useFontLoader(c,o.fontFamily,o.fontStyle,o.fontWeight,o.text||o.placeholder),ht=()=>{if(o.curveEnabled){const t=M.current;return t.getSelfRect().height||t.fontSize()}const t=k.current.clone({height:void 0}),e=Math.ceil(t.fontSize()*t.lineHeight()*t.textArr.length+1);return t.destroy(),e};n.useLayoutEffect(()=>{if(!ft){return}if("tablecell"===o.type){const{textOverflow:t}=O;if("resize"===t){const t=ht(),e=Y(o)?W(o,2):null;(null==e?void 0:e.setCellContentHeight)&&e.setCellContentHeight(o.id,t)}else if("change-font-size"===t){const t=k.current;if(t){const e=Z(t,o,O.textSplitAllowed);e!==o.fontSize&&c.history.ignore(()=>{o.set({fontSize:e})})}}return}const{textOverflow:t,textSplitAllowed:e}=O,r=(t,e,r,n)=>{const i=v(`text ${o.id}`),a=()=>{c.history.ignore(t,e,r).then(()=>{D.current?(T.current.push(i),$()):i()})};n?queueMicrotask(a):a()};if(!I(o)){const t=ht();return void r(()=>{I(o)||o.set({height:t})},void 0,void 0,!0)}if(!c.isPlaying){if("change-font-size"!==t||V){if("resize"===t){const t=ht();it&&o.height<t&&!V&&r(()=>{var e;Y(o)&&o.set({height:t}),null===(e=k.current)||void 0===e||e.height(t)},!1,!0),it||o.height===t||V||r(()=>{var e;Y(o)&&o.set({height:t}),null===(e=k.current)||void 0===e||e.height(t)},!1,!0)}}else{const t=k.current,n=o.curveEnabled?o.fontSize:Z(t,o,e);if(n!==o.fontSize){return void r(()=>{o.set(b(o,n))},!1,!0)}const i=ht();o.height===i||it||r(()=>{o.set({height:i})},!1,!0)}}}),n.useLayoutEffect(()=>{var t;if(ft&&o.curveEnabled){const e=null===(t=M.current)||void 0===t?void 0:t.getSelfRect().width;if(e){const t=e-o.a.width,r=o.a.rotation*Math.PI/180,n=-t/2*Math.cos(r),i=-t/2*Math.sin(r);o.set({width:e,x:o.a.x+n,y:o.a.y+i})}}},[ft,o.curveEnabled,o.curvePower,o.text,o.fontSize,o.fontFamily,o.fontWeight,o.fontStyle,o.letterSpacing]),n.useLayoutEffect(()=>{const t=k.current;t&&(t.width(t.width()+1e-8),t.width(t.width()-1e-8),t._setTextData(),ut())},[ft]);const gt=n.useRef(null),pt=n.useRef(o.width),mt=n.useRef(0),xt=t=>{t.evt.preventDefault();const e=c.selectedShapes.find(t=>t===o);e&&o.contentEditable&&(mt.current=function(t){var e;const r=t.target,n=function(t){var e=t.getAbsoluteTransform().copy();e.invert();var r=t.getStage().getPointerPosition();return e.point(r)}(r),o=r.textArr,i=Math.floor(n.y/(r.fontSize()*r.lineHeight())),a=o.slice(0,i).reduce((t,e)=>t+e.text.length,i),s=null!==(e=o[i])&&void 0!==e?e:o[0];let l=0;return"right"===r.align()?l=r.width()-s.width:"center"===r.align()&&(l=r.width()/2-s.width/2),a+Math.round((n.x-l)/s.width*s.text.length)}(t),o.toggleEditMode())},wt=B(o.placeholder),vt=!dt&&wt?.6:o.a.opacity;P(k,vt);const yt=getLineHeight({fontLoaded:ft,fontFamily:o.fontFamily,fontSize:o.a.fontSize,lineHeight:o.lineHeight}),bt=o.selectable||c.role===w.ADMIN,St=m(o),Et=m(o,o.stroke,"stroke"),zt=n.useMemo(()=>o.backgroundEnabled?o.curveEnabled?function({width:t,height:e,padding:r=0,cornerRadius:n=0}){const o=-r,i=-r,a=t+r,s=e+r,l=f(`M ${o} ${i} L ${a} ${i} L ${a} ${s} L ${o} ${s} Z`);return h(l,n).path}({width:o.a.width,height:o.a.height,cornerRadius:o.backgroundCornerRadius*(o.a.fontSize*yt*.5),padding:o.backgroundPadding*(o.a.fontSize*yt*.5)}):function({lines:t,lineHeight:e,width:r,align:n="left",padding:o=0,cornerRadius:i=0}){const a=p({lines:t,lineHeight:e,width:r,align:n});return g({rects:a,padding:o,cornerRadius:i})}({lines:JSON.parse(JSON.stringify(lt)),cornerRadius:o.backgroundCornerRadius*(o.a.fontSize*yt*.5),lineHeight:yt*o.a.fontSize,padding:o.backgroundPadding*(o.a.fontSize*yt*.5),width:o.a.width,align:o.align}):"",[o.backgroundEnabled,o.curveEnabled,o.backgroundCornerRadius,o.a.fontSize,o.a.height,yt,o.backgroundPadding,o.a.width,o.align,lt]),kt=X();let Ot=0;if(!o.curveEnabled){const t=lt.length*yt*o.a.fontSize,e=N(o.a,t);"middle"===o.verticalAlign?Ot=(e-t)/2:"bottom"===o.verticalAlign&&(Ot=e-t)}const Rt=o.curveEnabled?A(o.a.width,o.a.height,o.curvePower,o.a.fontSize):"",Ft=ft?L(o.fontFamily):st===o.fontFamily?"Arial":L(st),At=F(dt);return r(e,{children:[t(a,{ref:C,x:o.a.x,y:o.a.y,rotation:o.a.rotation,hideInExport:!o.showInExport,listening:!1,visible:o.backgroundEnabled,opacity:o.backgroundOpacity*vt,data:zt,fill:o.backgroundColor,offsetY:-Ot}),t(a,{data:Rt,stroke:d.stroke,strokeWidth:1,x:o.a.x,y:o.a.y,rotation:o.a.rotation,visible:!1}),t(l,Object.assign({ref:M,visible:o.curveEnabled,data:Rt,text:dt||wt,listening:!1,align:"center",textBaseline:"middle",direction:At},St,Et,{strokeWidth:o.strokeWidth,lineJoin:j(o.strokeLineJoin),fillAfterStrokeEnabled:!0,fontSize:o.a.fontSize,fontFamily:`${L(o.fontFamily)}, ${L(st)}`,fontStyle:o.fontStyle+" "+o.fontWeight,textDecoration:o.textDecoration.trim(),letterSpacing:o.letterSpacing*o.a.fontSize,x:o.a.x,y:o.a.y,rotation:o.a.rotation,opacity:o._editModeEnabled?.3:vt,hideInExport:!o.showInExport,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity})),t(s,Object.assign({ref:k,id:o.selectable?o.id:void 0,name:o.selectable?"element":"",hideInExport:!o.showInExport,editModeEnabled:o._editModeEnabled,x:o.a.x,y:o.a.y,rotation:o.a.rotation,width:o.a.width,height:I(o.a)?o.a.height:void 0,text:dt||wt,direction:At},St,Et,{lineJoin:j(o.strokeLineJoin),strokeWidth:o.strokeWidth,fillAfterStrokeEnabled:!0,fontSize:o.a.fontSize,fontFamily:Ft,fontStyle:o.fontStyle+" "+o.fontWeight,textDecoration:o.textDecoration,align:o.align,verticalAlign:o.verticalAlign,draggable:kt?o.draggable&&nt:o.draggable,preventDefault:!kt||nt,opacity:o.curveEnabled?0:vt,visible:!o._editModeEnabled,ellipsis:"ellipsis"===O.textOverflow,shadowEnabled:o.shadowEnabled,shadowBlur:o.shadowBlur,shadowOffsetX:o.shadowOffsetX,shadowOffsetY:o.shadowOffsetY,shadowColor:o.shadowColor,shadowOpacity:o.shadowOpacity,lineHeight:yt,letterSpacing:o.letterSpacing*o.a.fontSize,listening:!!o.selectable&&bt,onDragStart:()=>{K(!0)},onDragMove:t=>{o.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{K(!1),o.set({x:t.target.x(),y:t.target.y()})},onClick:xt,onTap:xt,onTransformStart:()=>{q(!0);const t=k.current;Q.current=t.height(),pt.current=o.a.width,gt.current=t.position(),tt.current=t.rotation(),rt.current=t.height(),et.current=y(t)},onTransform:t=>{var e,r;const n=t.target;null===(e=C.current)||void 0===e||e.setAttrs({x:n.x(),y:n.y(),rotation:n.rotation(),scale:n.scale()});const i=et.current;if("middle-left"===i||"middle-right"===i){const t=n.scaleX(),e=n.width()*t,r=z(pt.current,o.a.fontSize);let i=e;e<r&&(i=r,gt.current&&n.position(gt.current)),n.width(i),n.scaleX(1),n.scaleY(1);let a=ht();if("ellipsis"===O.textOverflow){a=Q.current}else{const t=Math.max(a,Q.current);n.height(t),o.set({height:n.height()})}const s=O.textVerticalResizeEnabled?Math.max(a,Q.current):ht();o.set({x:n.x(),width:n.width(),rotation:n.rotation(),height:s}),n.skewX(0),n.skewY(0),ut()}if("top-center"===i||"bottom-center"===i){const e="resize"===O.textOverflow?ht():yt*o.a.fontSize,r=t.target.height()*t.target.scaleY();if(r<e){if(t.target.height(e),gt.current){if("top-center"===et.current){const r=tt.current*Math.PI/180,n=rt.current-e;t.target.x(gt.current.x-n*Math.sin(r)),t.target.y(gt.current.y+n*Math.cos(r))}else{t.target.position(gt.current)}}}else{t.target.height(r)}t.target.scaleY(1),t.target.scaleX(1),t.target.rotation(tt.current),t.target.skewX(0),t.target.skewY(0),rt.current=Math.max(e,r)}gt.current=t.target.position();const a=t.target.scaleX();null===(r=C.current)||void 0===r||r.setAttrs({scaleX:1,scaleY:1}),t.target.scaleX(1),t.target.scaleY(1),o.set({fontSize:o.a.fontSize*a,width:t.target.width()*a,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*a,shadowBlur:o.shadowBlur*a,shadowOffsetX:o.shadowOffsetX*a,shadowOffsetY:o.shadowOffsetY*a,strokeWidth:o.strokeWidth*a})},onTransformEnd:t=>{var e;const r=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),o.set(Object.assign(Object.assign({},S(o,o.fontSize*r)),{width:Math.ceil(t.target.width()*r),x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*r})),null===(e=C.current)||void 0===e||e.setAttrs({scaleX:1,scaleY:1}),q(!1)}})),J&&o.contentEditable&&t(i,{x:o.a.x,y:o.a.y,rotation:o.a.rotation,children:t(U,{textNodeRef:k,element:o,selectAll:_,cursorPosition:mt.current,onBlur:()=>{o.set(E(o)),o.toggleEditMode(!1)}})})]})});
|