polotno 3.15.3 → 3.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/blueprint/side-panel/select-video.d.ts +2 -0
- package/canvas/element.js +1 -1
- package/canvas/figure-element.js +1 -1
- package/canvas/gif-element.js +1 -1
- package/canvas/html-element.js +1 -1
- package/canvas/image-element.js +1 -1
- package/canvas/page.js +1 -1
- package/canvas/render-tag-element.js +1 -1
- package/canvas/rich-editor.d.ts +3 -1
- package/canvas/rich-editor.js +2 -2
- package/canvas/text-element.js +1 -1
- package/canvas/use-color.d.ts +2 -2
- package/canvas/video-element.js +1 -1
- package/model/figure-model.d.ts +1 -0
- package/model/gif-model.d.ts +1 -0
- package/model/group-model.d.ts +12 -0
- package/model/image-model.d.ts +1 -0
- package/model/line-model.d.ts +1 -0
- package/model/node-model.d.ts +1 -0
- package/model/node-model.js +1 -1
- package/model/page-model.d.ts +36 -0
- package/model/page-model.js +1 -1
- package/model/shape-model.d.ts +1 -0
- package/model/store.d.ts +6 -0
- package/model/store.js +1 -1
- package/model/svg-model.d.ts +1 -0
- package/model/table-model.d.ts +1 -0
- package/model/text-model.d.ts +5 -0
- package/model/text-model.js +1 -1
- package/model/video-model.d.ts +1 -0
- package/package.json +21 -21
- package/polotno.bundle.js +166 -165
- package/primitives/numeric-input-delayed.d.ts +12 -0
- package/primitives/numeric-input-delayed.js +1 -0
- package/primitives/popover.js +1 -1
- package/side-panel/effects-panel.js +1 -1
- package/side-panel/layer-selection.d.ts +26 -0
- package/side-panel/layer-selection.js +1 -0
- package/side-panel/layers-panel.js +1 -1
- package/side-panel/select-video.d.ts +2 -0
- package/side-panel/size-panel.js +1 -1
- package/side-panel/text-effect-presets.d.ts +11 -0
- package/side-panel/text-effect-presets.js +1 -0
- package/toolbar/group-toolbar.js +1 -1
- package/toolbar/image-toolbar.d.ts +3 -2
- package/toolbar/image-toolbar.js +1 -1
- package/toolbar/many-toolbar.js +1 -1
- package/toolbar/opacity-picker.d.ts +3 -2
- package/toolbar/opacity-picker.js +1 -1
- package/toolbar/position-picker.js +1 -1
- package/toolbar/selection-colors.d.ts +18 -0
- package/toolbar/selection-colors.js +1 -0
- package/toolbar/selection-size.d.ts +12 -0
- package/toolbar/selection-size.js +1 -0
- package/toolbar/svg-toolbar.js +1 -1
- package/ui.css +1 -1
- package/utils/alignment.d.ts +16 -0
- package/utils/alignment.js +1 -1
- package/utils/array.d.ts +1 -0
- package/utils/array.js +1 -0
- package/utils/clipboard.d.ts +10 -0
- package/utils/duplicate.d.ts +2 -0
- package/utils/duplicate.js +1 -1
- package/utils/file.d.ts +1 -1
- package/utils/flags.d.ts +1 -1
- package/utils/l10n.d.ts +13 -0
- package/utils/l10n.js +1 -1
- package/utils/measure-text.d.ts +6 -3
- package/utils/measure-text.js +1 -1
- package/utils/px.d.ts +11 -18
- package/utils/px.js +1 -1
- package/utils/rich-text-html.d.ts +5 -0
- package/utils/rich-text-html.js +1 -1
- package/utils/selection-colors.d.ts +15 -0
- package/utils/selection-colors.js +1 -0
- package/utils/selection-transform.d.ts +19 -0
- package/utils/selection-transform.js +1 -0
- package/utils/selection.d.ts +3 -0
- package/utils/selection.js +1 -0
- package/utils/text-sizing.d.ts +5 -0
- package/utils/text-sizing.js +1 -0
- package/utils/use-svg-colors.d.ts +1 -0
- package/utils/use-svg-colors.js +1 -1
- package/utils/validate-key.js +1 -1
- package/canvas/text-element/fit-font-size.d.ts +0 -5
- package/canvas/text-element/fit-font-size.js +0 -1
package/canvas/rich-editor.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(o[n]=t[n])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(t);r<n.length;r++){e.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(t,n[r])&&(o[n[r]]=t[n[r]])}}return o};import{jsx as e}from"react/jsx-runtime";import o from"react";import n from"quill";import*as r from"mobx";import{nodeReaction as i}from"../utils/mobx.js";import{isAlive as l}from"mobx-state-tree";import{getDir as s,getMeasureCtx as a,getQuillEditorHtml as c,textLayoutWidth as d}from"@polotno/core/text-html";import{quillHtmlToModelText as u,isQuillFormatMixed as f,isBoldWeight as p,detachNativeColorsInHtml as m}from"../utils/rich-text-html.js";import{toDisplayColor as h}from"@polotno/core/color";import{resetStyleContent as g,wrapHtmlForRenderTag as b}from"@polotno/core/reset-style";import{layout as v,lineBaselineOffset as y}from"render-tag";import{removeTags as x}from"@polotno/core/text";import{cssFontFamily as w}from"@polotno/core/fonts";import{isGradient as S,firstStopColor as k}from"@polotno/core/gradient";import{getOptimalCaretColor as T}from"./text-element/caret-color.js";import{visiblePlaceholder as q}from"./text-element/placeholder.js";import{registerQuillUnrestrictedFontFormats as E,PRESERVED_INLINE_STYLES as C,PRESERVED_BLOCK_STYLES as O}from"./quill-font-formats.js";import{ALL_QUILL_MIXED_KEYS as R,setActiveTextEditor as F,updateActiveTextFormat as L,clearActiveTextEditor as $,getActiveTextEditor as N}from"../utils/text-format-state.js";import j from"../utils/styled.js";import{applyOpacityToColor as M}from"@polotno/core/svg";import{elementDropShadow as z,shadowCastByBackground as P}from"@polotno/core/filters";export{M as applyOpacityToColor};export const quillRef=r.observable({enabled:!1,currentFormat:{},pendingToolbarInteraction:!1,editor:r.observable.object({instance:null},{},{deep:!1})});const D=R;export function syncQuillFormatState(t,e){const o=e?t.getFormat(e):{},n=function(t,e){const o={};if(!e||e.length<=0){return o}const n=t.getContents(e.index,e.length);for(const r of D){f(n,r)&&(o[r]=!0)}return o}(t,e);r.runInAction(()=>{quillRef.currentFormat=o}),L(o,n,e)}const H="polotno-saved-selection";function I(){const t=window.getSelection();if(!t||0===t.rangeCount||t.isCollapsed){return}if("undefined"==typeof Highlight||!CSS.highlights){return}const e=t.getRangeAt(0).cloneRange();CSS.highlights.set(H,new Highlight(e)),Q()}function A(){"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.delete(H)}function Q(){setTimeout(()=>{"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.has(H)&&(document.querySelector(".polotno-portal")||tt(document.activeElement)?Q():A())},300)}if("undefined"!=typeof document&&"undefined"!=typeof CSS&&CSS.highlights&&!document.querySelector("[data-polotno-highlight]")){const et=document.createElement("style");et.setAttribute("data-polotno-highlight",""),et.textContent=`::highlight(${H}) { background-color: Highlight; }`,document.head.appendChild(et)}function _(t,e){"text"===t.type&&e.push(t);for(const o of t.children||[]){_(o,e)}}export function computeMarkerVars(t){var e,o,n;const r=t.fontSize;if(!r||r<=0){return null}const i=w(t.fontFamily),l=p(t.fontWeight)?700:400,s="italic"===t.fontStyle?"italic":"normal",c=a();c.fontKerning="normal",c.font=`${r}px ${i}`;const d=c.measureText("M"),u=(null!==(e=d.fontBoundingBoxAscent)&&void 0!==e?e:0)/r,f=(null!==(o=d.fontBoundingBoxDescent)&&void 0!==o?o:0)/r,m=`font-family:${i};font-size:${r}px;font-weight:${l};font-style:${s};line-height:${t.lineHeight};letter-spacing:${t.letterSpacing}em`,h=b(`<div style="${m}"><ul><li>x</li></ul><ol><li>x</li></ol></div>`);let g;try{g=v({html:h,width:Math.max(12*r,600)})}catch(E){return null}const x=[];_(g.layoutRoot,x);const S=x.find(t=>"•"===t.text),k=x.find(t=>/^\d/.test(t.text));if(!S){return null}const T=S.style.fontSize,q=2.1*r;return{bulletLeft:S.x,bulletTop:S.y-y(T,T*u,T*f),bulletFontSize:T,numberGap:k?Math.max(0,q-k.x-(null!==(n=k.width)&&void 0!==n?n:0)):0}}export const RichTextContainer=j("div",o.forwardRef)`
|
|
2
2
|
.ql-editor {
|
|
3
3
|
outline: none;
|
|
4
4
|
/* anchor for the absolutely-positioned ::before placeholder below */
|
|
@@ -163,4 +163,4 @@ var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype
|
|
|
163
163
|
.ql-editor [style*='background-clip'] {
|
|
164
164
|
-webkit-text-stroke-width: 0;
|
|
165
165
|
}
|
|
166
|
-
`;const W=[...C,...O,"header"];let B=["background","font-weight","color","gradient","font","italic","size","script","strike","underline","text-transform","indent","list","direction",...W];export const setQuillFormats=
|
|
166
|
+
`;const W=[...C,...O,"header"];let B=["background","font-weight","color","gradient","font","italic","size","script","strike","underline","text-transform","indent","list","direction",...W];export const setQuillFormats=t=>{B=t};let X=!1;function G(){X||(X=!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 V(t){return t?"700":"400"}const Y=n.import("delta"),U="data-polotno-rich-text";export const createQuill=(e,o)=>{E();const r=new n(e,{toolbar:!1,keyboard:!1,placeholder:(null==o?void 0:o.placeholder)||void 0,clipboard:{matchVisual:!1},formats:B});!function(e){const o=e.format.bind(e),n=e.formatText.bind(e),r=e.getFormat.bind(e);e.format=(t,e,n)=>"bold"===t?(G(),o("font-weight",V(e),n)):o(t,e,n),e.formatText=(e,o,r,i,l)=>{if(r&&"object"==typeof r){if("bold"in r){G();const{bold:l}=r,s=t(r,["bold"]);return n(e,o,Object.assign(Object.assign({},s),{"font-weight":V(l)}),i)}return n(e,o,r,i)}return"bold"===r?(G(),n(e,o,"font-weight",V(i),l)):n(e,o,r,i,l)},e.getFormat=(...t)=>{const e=r(...t);return e&&null!=e["font-weight"]&&!("bold"in e)&&(e.bold=p(e["font-weight"])),e}}(r);const i=(t,e)=>e.compose((new Y).retain(e.length(),{"font-weight":"700"}));r.clipboard.addMatcher("STRONG",i),r.clipboard.addMatcher("B",i);const l=r.clipboard,s=l.onPaste.bind(l);return l.onPaste=(t,e)=>{var o;if(null===(o=e.html)||void 0===o?void 0:o.includes(U)){return Z(r,()=>s(t,e))}const n=l.convert;l.convert=(...t)=>((t,e)=>{for(const o of t.ops){if(o.attributes){for(const t of e){delete o.attributes[t]}}}return t})(n.apply(l,t),W);try{return s(t,e)}finally{l.convert=n}},r};const K=(t,e)=>e.insert(t.data),J=(t,e)=>{const o=e.ops[0];return parseFloat(t.style.textIndent)>0&&"string"==typeof(null==o?void 0:o.insert)&&o.insert.startsWith("\t")?e.compose((new Y).delete(1)):e};function Z(t,e){const{matchers:o}=t.clipboard,n=o.findIndex(([t])=>t===Node.TEXT_NODE),r=o[n];-1!==n&&(o[n]=[Node.TEXT_NODE,K]),o.push([Node.ELEMENT_NODE,J]);try{return e()}finally{o.pop(),-1!==n&&(o[n]=r)}}export const setQuillContent=(t,e)=>{const o=Z(t,()=>t.clipboard.convert({html:"<div class='ql-editor' style='outline: none;'>"+e+"<p><br></p></div>",text:""}));t.setContents(o),t.history.clear()};export const editorHtml=t=>c(Object.assign(Object.assign({},t),{text:m(t.text)}),t.fontFamily);export const RichEditor=({onBlur:t,onChange:n,element:a,clickCoords:c,fitScale:f=1,fitScaleFor:p})=>{const m=o.useRef(null),g=o.useRef(!1),b=o.useRef(f);b.current=f;const[v,y]=o.useState(null),E=null!=v?v:f,C=o.useRef(p);C.current=p;const O=t=>({width:d(a.a.width/t,a.letterSpacing,a.fontSize),transform:1!==t?`scale(${t})`:void 0,transformOrigin:1!==t?"0 0":void 0}),R=o.useRef(null),L=o.useRef(void 0),j=()=>`${a.text}\0${a.fontSize}\0${a.fontFamily}`,M=t=>u(t,{fontSize:a.fontSize,fontFamily:a.fontFamily}),D=t=>{setQuillContent(t,editorHtml(a)),R.current=M(t.root.innerHTML),L.current=j()},H=o.useRef(()=>{}),Q=()=>{H.current(),t()};o.useLayoutEffect(()=>{let t,e,o=!1,i=null,s=null,d=null,u=null,f=null;const p=()=>{if(!o&&m.current){try{t=createQuill(m.current,{placeholder:q(a.placeholder)}),r.runInAction(()=>{quillRef.editor.instance=t}),quillRef.pendingToolbarInteraction=!1,window.__polotnoQuill=t,F({instance:t,store:a.store,element:a});const o=e=>{e&&syncQuillFormatState(t,e)},p=()=>{let e=t.root;if(e.querySelector(".ql-cursor")){e=e.cloneNode(!0);const t=e.querySelector(".ql-cursor");t.replaceWith(t.textContent.replace(/\uFEFF/g,""))}return M(e.innerHTML)},h=()=>{const t=C.current;t&&l(a)&&(t=>{var e,o;const n=m.current;if(!n){return}const r=O(t);n.style.width=`${r.width}px`,n.style.transform=null!==(e=r.transform)&&void 0!==e?e:"",n.style.transformOrigin=null!==(o=r.transformOrigin)&&void 0!==o?o:""})(t(p()))},v=()=>{var o,r;if(clearTimeout(e),e=void 0,!l(a)){return}const i=p();if(i===R.current){return}R.current=i,g.current=!0,n(i);const s=t.getContents(),c=a.fill;let d=null,u=!0;for(const t of s.ops){if("string"!=typeof t.insert||"\n"===t.insert){continue}if(null===(o=t.attributes)||void 0===o?void 0:o.gradient){u=!1;break}const e=(null===(r=t.attributes)||void 0===r?void 0:r.color)||c;if(null===d){d=e}else if(e!==d){u=!1;break}}u&&d&&a.set({fill:d})};H.current=()=>{(void 0!==e||t.composition.isComposing)&&v()},t.on("text-change",()=>{const n=N(),r=n&&n.instance===t?n.lastSelection:null;o(t.getSelection()||r),t.composition.isComposing||h(),void 0===e&&(e=setTimeout(v,10))}),t.on("selection-change",o),t.on("composition-start",()=>y(b.current)),t.on("composition-end",()=>{h(),y(null)}),D(t);const x=m.current.closest(".polotno-workspace-inner"),w=null==x?void 0:x.scrollLeft,S=null==x?void 0:x.scrollTop;c?setCursorFromCoords(t,c):t.setSelection(0,0,"api"),x&&void 0!==w&&void 0!==S&&(x.scrollLeft=w,x.scrollTop=S);const k=t.root;u=t=>{const e=t.relatedTarget;(null==e?void 0:e.classList.contains("ql-clipboard"))||(tt(e)||quillRef.pendingToolbarInteraction?I():setTimeout(()=>{quillRef.pendingToolbarInteraction||tt(document.activeElement)?I():(A(),Q())},0))},f=()=>{A()},k.addEventListener("blur",u),k.addEventListener("focus",f),i=k,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 s="";if(l&&l.rangeCount>0){const t=l.getRangeAt(0);let e=t.cloneContents(),o=t.startContainer;o&&o.nodeType===Node.TEXT_NODE&&(o=o.parentNode);let n=t.endContainer;if(n&&n.nodeType===Node.TEXT_NODE&&(n=n.parentNode),o&&o===n){let t=o;for(;t&&t!==k&&t.nodeType===Node.ELEMENT_NODE;){const o=t.tagName;if("SPAN"!==o&&"EM"!==o&&"STRONG"!==o&&"U"!==o&&"S"!==o&&"A"!==o){break}const n=t.cloneNode(!1);n.appendChild(e),e=n,t=t.parentNode}}const r=document.createElement("div");r.appendChild(e),s=r.innerHTML}if(null===(o=e.clipboardData)||void 0===o||o.setData("text/plain",i),s){const t=`<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>\x3c!--StartFragment--\x3e<div ${U} style="white-space: pre-wrap; overflow-wrap: break-word;">`+s+"</div>\x3c!--EndFragment--\x3e</body></html>";null===(n=e.clipboardData)||void 0===n||n.setData("text/html",t)}e.preventDefault()},d=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",d)}catch(p){console.error("RichEditor init failed:",p)}}};return m.current?p():queueMicrotask(p),()=>{o=!0,clearTimeout(e),i&&u&&i.removeEventListener("blur",u),i&&f&&i.removeEventListener("focus",f),A(),i&&s&&d&&(i.removeEventListener("copy",s),i.removeEventListener("cut",d)),r.runInAction(()=>{quillRef.editor.instance=null,quillRef.currentFormat={}}),$(t),delete window.__polotnoQuill}},[]),o.useEffect(()=>i(a,j,t=>{const e=quillRef.editor.instance;if(!e){return}if(g.current){return g.current=!1,void(L.current=t)}if(t===L.current){return}const o=e.getSelection();D(e),o&&(e.setSelection(o.index,o.length),syncQuillFormatState(e,e.getSelection()))},{fireImmediately:!0}),[]),o.useEffect(()=>i(a,()=>`${a.fontFamily}\0${a.fontSize}\0${a.fontWeight}\0${a.fontStyle}\0${a.lineHeight}\0${a.letterSpacing}\0${s(x(a.text))}`,()=>{const t=m.current;if(!t){return}const e="ltr"===s(x(a.text)),o=computeMarkerVars(a);o&&t.style.setProperty("--rt-number-gap",`${o.numberGap}px`),o&&e?(t.style.setProperty("--rt-bullet-left",`${o.bulletLeft}px`),t.style.setProperty("--rt-bullet-top",`${o.bulletTop}px`),t.style.setProperty("--rt-bullet-fs",`${o.bulletFontSize}px`),t.classList.add("rt-marker-geom")):t.classList.remove("rt-marker-geom")},{fireImmediately:!0}),[]),o.useEffect(()=>{window.addEventListener("blur",Q);const t=t=>{var e;const o=t.target;"number"==typeof(null==o?void 0:o.nodeType)&&(null===(e=m.current)||void 0===e?void 0:e.contains(o))||tt(o)||quillRef.pendingToolbarInteraction||(A(),Q())};return window.addEventListener("mousedown",t),window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",Q),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t)}},[]);const _=h(a.fill),W={color:_};_.indexOf("gradient")>=0&&(W["--polotno-gradient"]=_,W.color="transparent"),a.strokeWidth>0&&(W.WebkitTextStroke=`${a.strokeWidth}px ${k(a.stroke)}`,W.paintOrder="stroke fill");const B=S(a.fill)?k(a.fill):T(a),X=a.shadowEnabled&&!P(a);return e(RichTextContainer,{ref:m,style:Object.assign(Object.assign(Object.assign(Object.assign({},W),{fontSize:a.fontSize,fontFamily:w(a.fontFamily),fontWeight:a.fontWeight,fontStyle:a.fontStyle,"--polotno-text-decoration":a.textDecoration||"none",textTransform:a.textTransform}),O(E)),{lineHeight:a.lineHeight,letterSpacing:a.letterSpacing*a.fontSize+"px",textAlign:a.align,opacity:Math.max(a.a.opacity,.2),filter:X?z({shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowBlur:a.shadowBlur,shadowColor:a.shadowColor,shadowOpacity:a.shadowOpacity,rotation:a.a.rotation}):void 0,caretColor:B}),dir:s(x(a.text))})};function tt(t){const e=t;return!("function"!=typeof(null==e?void 0:e.closest)||!e.closest(".sketch-picker")&&!e.closest("[data-polotno-keep-focus]")&&!e.closest(".polotno-portal")&&!e.closest(".polotno-toolbar"))}export function setCursorFromCoords(t,e){if(!t||!e){return}const{x:o,y:r}=e;try{let e=null;if(document.caretRangeFromPoint){e=document.caretRangeFromPoint(o,r)}else if(document.caretPositionFromPoint){const t=document.caretPositionFromPoint(o,r);t&&(e=document.createRange(),e.setStart(t.offsetNode,t.offset))}if(e){const o=n.find(e.startContainer,!0);if(o){const n=o.offset(t.scroll)+e.startOffset;return void t.setSelection(n,0,"api")}}}catch(i){}t.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{elementOpacity as o}from"./paint-opacity.js";import n from"react";import{toDisplayColor as i}from"@polotno/core/color";import{observer as a}from"mobx-react-lite";import{Group as l,Path as s,Text as c,TextPath as u}from"react-konva";import{Html as d}from"react-konva-utils";import{nodeAutorun as f}from"../utils/mobx.js";import h from"konva";import{parsePath as g,roundCommands as m}from"svg-round-corners/lib/index.js";import{generateBackgroundShapeFromRects as p,textLinesToRects as x}from"@polotno/core/background-shape";import{useColor as b}from"./use-color.js";import{useWorkspaceStyle as v}from"./workspace-style.js";import{ROLES as w}from"../model/store.js";import{incrementLoader as S}from"../utils/loader.js";import{activeAnchorOf as y,scaledFontSizeAttrs as E,roundedFontSizeAttrs as z,roundedTextDecorationAttrs as k,roundedGeometryPx as R,roundedPosition as F,textMinResizeWidth as O}from"../utils/px.js";import{isFontLoaded as j}from"../utils/fonts.js";import{flags as A}from"../utils/flags.js";import{removeTags as M}from"@polotno/core/text";import{getDir as L,getCurvePath as C,resolveLineHeight as T,normalizeStrokeLineJoin as H}from"@polotno/core/text-html";import{cssFontFamily as P}from"@polotno/core/fonts";import{shadowCastByBackground as W}from"@polotno/core/filters";import{applyOpacityToColor as X}from"@polotno/core/svg";import{FIT_FONT_SIZE_STEP as D,fitHeightLimit as Y}from"@polotno/core/fit-text";import{applyFilter as $}from"./apply-filters.js";import{useFadeIn as I}from"./use-fadein.js";import{isTouchDevice as J}from"../utils/screen.js";import{isAlive as N,getParent as B}from"mobx-state-tree";import{getLimitedFontSize as _}from"./text-element/max-font-size.js";import{getOptimalCaretColor as V}from"./text-element/caret-color.js";import{visiblePlaceholder as q}from"./text-element/placeholder.js";import{hasMeasuredHeight as U,resolveTextBoxHeight as Z}from"@polotno/core/text-types";let G;function K(){return G||(G=document.getElementById("polotno-text-style"),G||(G=document.createElement("style"),G.id="polotno-text-style",document.head.appendChild(G)),G)}h._fixTextRendering=!0;const Q={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 tt=a(({textNodeRef:e,element:r,onBlur:o,selectAll:i,cursorPosition:a})=>{const[l,s]=n.useState(Q),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=V(r);const e=`\n .polotno-input::placeholder {\n color: ${l.color};\n opacity: 0.6;\n }\n `,o=K();o.innerHTML="",o.appendChild(document.createTextNode(e)),JSON.stringify(t)!==JSON.stringify(l)&&s(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=Z(r.a,f);"middle"===r.verticalAlign&&(d=(h-f)/2),"bottom"===r.verticalAlign&&(d=h-f);const g=M(r.text);return t("textarea",{className:"polotno-input",ref:u,dir:L(g),style:Object.assign(Object.assign(Object.assign({},Q),l),{paddingTop:d+"px"}),value:g,onChange:t=>{const e=_({oldText:r.text,newText:t.target.value,element:r});r.set(Object.assign({text:t.target.value},E(r,e)))},placeholder:q(r.placeholder),onBlur:o})}),et=e=>t(d,{children:t(tt,Object.assign({},e))});export const useFontLoader=(t,e,r,o,i)=>{const[a,l]=n.useReducer(t=>t+1,0),s=n.useRef(j(e,r,o)),c=n.useRef(null);return n.useLayoutEffect(()=>{if(s.current=j(e,r,o),s.current){return}let n=!0;return(async()=>{s.current=!1,l();const a=S(`text ${e}`);await t.loadFont(e,r,o,i),n?(c.current=a,s.current=!0,l()):a()})(),()=>{c.current&&(c.current(),c.current=null),n=!1}},[e,r,o]),n.useEffect(()=>{s.current&&setTimeout(()=>{c.current&&(c.current(),c.current=null)})},[s.current]),[s.current]};export const useFontFamiliesLoader=(t,e,r,o,i)=>{const[a,l]=n.useReducer(t=>t+1,0),s=e.join("\0"),c=n.useRef(e.every(t=>j(t,r,o))),u=n.useRef(null);return n.useLayoutEffect(()=>{if(c.current=e.every(t=>j(t,r,o)),c.current){return}let n=!0;return(async()=>{c.current=!1,l();const a=S(`text fonts ${s}`);await Promise.all(e.map(e=>t.loadFont(e,r,o,i))),n?(u.current=a,c.current=!0,l()):a()})(),()=>{u.current&&(u.current(),u.current=null),n=!1}},[s,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(()=>T({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 rt(t,e,r=!1){const o=t.fontSize(),n=t.height(),i=M(e.text);let a=e.a.fontSize;t.height(void 0);const l=Math.round(2*e.a.fontSize)-1;for(let s=1;s<l;s++){const o=t.height()>Y(e.a);let n=i.split("\n").join(" ").split(/[\s-]+/).reduce((t,e)=>(e=e.toLowerCase(),/[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\uAC00-\uD7AF]/.test(e)?t.concat(e.split("")):t.concat(e)),[]),l=t.textArr.map(t=>t.text.toLowerCase()).join(";");const s=n.find(t=>!l.includes(t)||(l=l.replace(t,""),!1));if(!(o||s&&!r)){break}a-=D,t.fontSize(a)}return t.fontSize(o),t.height(n),a}export{getCurvePath}from"@polotno/core/text-html";export const TextElement=a(({element:a,store:d})=>{const{textOverflowIndicatorStyle:h}=v(),j=n.useRef(null),T=n.useRef(null),D=n.useRef(null),Y=n.useRef([]),_=n.useRef(!0),[,V]=n.useReducer(t=>t+1,0),{editorEnabled:G,selectAll:K}=(t=>{const[e,r]=n.useState(!1),o=n.useRef(!1);return n.useEffect(()=>{var e=!0;return setTimeout(()=>{e&&(t._editModeEnabled&&(o.current=!0),r(!0),setTimeout(()=>{o.current=!1},50))},50),()=>{e=!1}},[]),{editorEnabled:e&&t._editModeEnabled,selectAll:o.current}})(a),[Q,tt]=n.useState(!1),[ot,nt]=n.useState(!1),it=n.useRef(a.a.height),at=n.useRef(0),lt=n.useRef(""),st=n.useRef(0),ct=a.isSelected,ut=usePrevious(ct),{textVerticalResizeEnabled:dt}=A,ft=A.autoDeleteEmptyText,ht=usePrevious(a.fontFamily),[gt,mt]=n.useState([]);n.useEffect(()=>{var t,e;const r=null!==(e=null===(t=j.current)||void 0===t?void 0:t.textArr)&&void 0!==e?e:[];JSON.stringify(r)!==JSON.stringify(gt)&&mt(r)}),n.useEffect(()=>{if(a.a.width||"tablecell"===a.type){return}const t=j.current;t.width(600),a.set({width:1.4*t.getTextWidth()})},[]),n.useEffect(()=>{ut&&!ct&&ft&&""===a.text&&a.removable&&!q(a.placeholder)&&d.deleteElements([a.id])},[ft,a.placeholder,a.removable,a.text,ct,ut]);const pt=()=>{if(a.curveEnabled){const t=j.current;null==t||t.clearCache(),null==t||t.setAttrs({filters:[]});const e=T.current;e&&$(e,a)}else{const t=j.current;t&&$(t,a)}};n.useLayoutEffect(()=>{if(!ot){return f(a,()=>{pt()})}}),n.useEffect(()=>{const t=Y.current;t.length>0&&(Y.current=[],t.forEach(t=>t()))}),n.useEffect(()=>()=>{_.current=!1;const t=Y.current;t.length>0&&(Y.current=[],t.forEach(t=>t()))},[]);let xt=M(a.text);"uppercase"===a.textTransform&&(xt=xt.toUpperCase());const[bt]=useFontLoader(d,a.fontFamily,a.fontStyle,a.fontWeight,a.text||a.placeholder),vt=()=>{if(a.curveEnabled){const t=T.current;return t.getSelfRect().height||t.fontSize()}const t=j.current.clone({height:void 0}),e=Math.ceil(t.fontSize()*t.lineHeight()*t.textArr.length+1);return t.destroy(),e};n.useLayoutEffect(()=>{if(!bt){return}if("tablecell"===a.type){const{textOverflow:t}=A;if("resize"===t){const t=vt(),e=N(a)?B(a,2):null;(null==e?void 0:e.setCellContentHeight)&&e.setCellContentHeight(a.id,t)}else if("change-font-size"===t){const t=j.current;if(t){const e=rt(t,a,A.textSplitAllowed);e!==a.fontSize&&d.history.ignore(()=>{a.set({fontSize:e})})}}return}const{textOverflow:t,textSplitAllowed:e}=A,r=(t,e,r,o)=>{const n=S(`text ${a.id}`),i=()=>{d.history.ignore(t,e,r).then(()=>{_.current?(Y.current.push(n),V()):n()})};o?queueMicrotask(i):i()};if(!U(a)){const t=vt();return void r(()=>{U(a)||a.set({height:t})},void 0,void 0,!0)}if(!d.isPlaying){if("change-font-size"!==t||Q){if("resize"===t){const t=vt();dt&&a.height<t&&!Q&&r(()=>{var e;N(a)&&a.set({height:t}),null===(e=j.current)||void 0===e||e.height(t)},!1,!0),dt||a.height===t||Q||r(()=>{var e;N(a)&&a.set({height:t}),null===(e=j.current)||void 0===e||e.height(t)},!1,!0)}}else{const t=j.current,o=a.curveEnabled?a.fontSize:rt(t,a,e);if(o!==a.fontSize){return void r(()=>{a.set(E(a,o))},!1,!0)}const n=vt();a.height===n||dt||r(()=>{a.set({height:n})},!1,!0)}}}),n.useLayoutEffect(()=>{var t;if(bt&&a.curveEnabled){const e=null===(t=T.current)||void 0===t?void 0:t.getSelfRect().width;if(e){const t=e-a.a.width,r=a.a.rotation*Math.PI/180,o=-t/2*Math.cos(r),n=-t/2*Math.sin(r);a.set({width:e,x:a.a.x+o,y:a.a.y+n})}}},[bt,a.curveEnabled,a.curvePower,a.text,a.fontSize,a.fontFamily,a.fontWeight,a.fontStyle,a.letterSpacing]),n.useLayoutEffect(()=>{const t=j.current;t&&(t.width(t.width()+1e-8),t.width(t.width()-1e-8),t._setTextData(),pt())},[bt]);const wt=n.useRef(null),St=n.useRef(a.width),yt=n.useRef(0),Et=t=>{t.evt.preventDefault();const e=d.selectedShapes.find(t=>t===a);e&&a.contentEditable&&(yt.current=function(t){var e;const r=t.target,o=function(t){var e=t.getAbsoluteTransform().copy();e.invert();var r=t.getStage().getPointerPosition();return e.point(r)}(r),n=r.textArr,i=Math.floor(o.y/(r.fontSize()*r.lineHeight())),a=n.slice(0,i).reduce((t,e)=>t+e.text.length,i),l=null!==(e=n[i])&&void 0!==e?e:n[0];let s=0;return"right"===r.align()?s=r.width()-l.width:"center"===r.align()&&(s=r.width()/2-l.width/2),a+Math.round((o.x-s)/l.width*l.text.length)}(t),a.toggleEditMode())},zt=q(a.placeholder),kt=!xt&&zt?.6:o(a);I(j,kt);const Rt=getLineHeight({fontLoaded:bt,fontFamily:a.fontFamily,fontSize:a.a.fontSize,lineHeight:a.lineHeight}),Ft=a.selectable||d.role===w.ADMIN,Ot=b(a),jt=b(a,a.stroke,"stroke"),At=n.useMemo(()=>a.backgroundEnabled||a.borderSize?a.curveEnabled?function({width:t,height:e,padding:r=0,cornerRadius:o=0}){const n=-r,i=-r,a=t+r,l=e+r,s=g(`M ${n} ${i} L ${a} ${i} L ${a} ${l} L ${n} ${l} Z`);return m(s,o).path}({width:a.a.width,height:a.a.height,cornerRadius:a.backgroundCornerRadius*(a.a.fontSize*Rt*.5),padding:a.backgroundPadding*(a.a.fontSize*Rt*.5)}):function({lines:t,lineHeight:e,width:r,align:o="left",padding:n=0,cornerRadius:i=0}){const a=x({lines:t,lineHeight:e,width:r,align:o});return p({rects:a,padding:n,cornerRadius:i})}({lines:JSON.parse(JSON.stringify(gt)),cornerRadius:a.backgroundCornerRadius*(a.a.fontSize*Rt*.5),lineHeight:Rt*a.a.fontSize,padding:a.backgroundPadding*(a.a.fontSize*Rt*.5),width:a.a.width,align:a.align}):"",[a.backgroundEnabled,a.borderSize,a.curveEnabled,a.backgroundCornerRadius,a.a.fontSize,a.a.height,Rt,a.backgroundPadding,a.a.width,a.align,gt]),Mt=J();let Lt=0;if(!a.curveEnabled){const t=gt.length*Rt*a.a.fontSize,e=Z(a.a,t);"middle"===a.verticalAlign?Lt=(e-t)/2:"bottom"===a.verticalAlign&&(Lt=e-t)}const Ct=a.curveEnabled?C(a.a.width,a.a.height,a.curvePower,a.a.fontSize):"",Tt=bt?P(a.fontFamily):ht===a.fontFamily?"Arial":P(ht),Ht=L(xt),Pt=a.shadowEnabled&&!W(a),Wt={shadowEnabled:a.shadowEnabled&&(W(a)||a.borderSize>0),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:i(a.shadowColor),shadowOpacity:a.shadowOpacity};return r(e,{children:[t(s,Object.assign({ref:D,x:a.a.x,y:a.a.y,rotation:a.a.rotation,hideInExport:!a.showInExport,listening:!1,visible:a.backgroundEnabled||a.borderSize>0,opacity:kt,data:At,fillEnabled:a.backgroundEnabled,fill:X(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,lineJoin:a.backgroundCornerRadius>0?"round":"miter",offsetY:-Lt},Wt)),t(s,{data:Ct,stroke:h.stroke,strokeWidth:1,x:a.a.x,y:a.a.y,rotation:a.a.rotation,visible:!1}),t(u,Object.assign({ref:T,visible:a.curveEnabled,data:Ct,text:xt||zt,listening:!1,align:"center",textBaseline:"middle",direction:Ht},Ot,jt,{strokeWidth:a.strokeWidth,lineJoin:H(a.strokeLineJoin),fillAfterStrokeEnabled:!0,fontSize:a.a.fontSize,fontFamily:`${P(a.fontFamily)}, ${P(ht)}`,fontStyle:a.fontStyle+" "+a.fontWeight,textDecoration:a.textDecoration.trim(),letterSpacing:a.letterSpacing*a.a.fontSize,x:a.a.x,y:a.a.y,rotation:a.a.rotation,opacity:a._editModeEnabled?.3:kt,hideInExport:!a.showInExport},Wt,{shadowEnabled:Pt})),t(c,Object.assign({ref:j,id:a.selectable?a.id:void 0,name:a.selectable?"element":"",hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled,x:a.a.x,y:a.a.y,rotation:a.a.rotation,width:a.a.width,height:U(a.a)?a.a.height:void 0,text:xt||zt,direction:Ht},Ot,jt,{lineJoin:H(a.strokeLineJoin),strokeWidth:a.strokeWidth,fillAfterStrokeEnabled:!0,fontSize:a.a.fontSize,fontFamily:Tt,fontStyle:a.fontStyle+" "+a.fontWeight,textDecoration:a.textDecoration,align:a.align,verticalAlign:a.verticalAlign,draggable:Mt?a.draggable&&ct:a.draggable,preventDefault:!Mt||ct,opacity:a.curveEnabled?0:kt,visible:!a._editModeEnabled,ellipsis:"ellipsis"===A.textOverflow},Wt,{shadowEnabled:Pt,lineHeight:Rt,letterSpacing:a.letterSpacing*a.a.fontSize,listening:!!a.selectable&&Ft,onDragStart:()=>{nt(!0)},onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{nt(!1),a.set(F(t.target))},onClick:Et,onTap:Et,onTransformStart:()=>{tt(!0);const t=j.current;it.current=t.height(),St.current=a.a.width,wt.current=t.position(),at.current=t.rotation(),st.current=t.height(),lt.current=y(t)},onTransform:t=>{var e,r;const o=t.target;null===(e=D.current)||void 0===e||e.setAttrs({x:o.x(),y:o.y(),rotation:o.rotation(),scale:o.scale()});const n=lt.current;if("middle-left"===n||"middle-right"===n){const t=o.scaleX(),e=o.width()*t,r=O(St.current,a.a.fontSize);let n=e;e<r&&(n=r,wt.current&&o.position(wt.current)),o.width(n),o.scaleX(1),o.scaleY(1);let i=vt();if("ellipsis"===A.textOverflow){i=it.current}else{const t=Math.max(i,it.current);o.height(t),a.set({height:o.height()})}const l=A.textVerticalResizeEnabled?Math.max(i,it.current):vt();a.set({x:o.x(),width:o.width(),rotation:o.rotation(),height:l}),o.skewX(0),o.skewY(0),pt()}if("top-center"===n||"bottom-center"===n){const e="resize"===A.textOverflow?vt():Rt*a.a.fontSize,r=t.target.height()*t.target.scaleY();if(r<e){if(t.target.height(e),wt.current){if("top-center"===lt.current){const r=at.current*Math.PI/180,o=st.current-e;t.target.x(wt.current.x-o*Math.sin(r)),t.target.y(wt.current.y+o*Math.cos(r))}else{t.target.position(wt.current)}}}else{t.target.height(r)}t.target.scaleY(1),t.target.scaleX(1),t.target.rotation(at.current),t.target.skewX(0),t.target.skewY(0),st.current=Math.max(e,r)}wt.current=t.target.position();const i=t.target.scaleX();null===(r=D.current)||void 0===r||r.setAttrs({scaleX:1,scaleY:1}),t.target.scaleX(1),t.target.scaleY(1),a.set({fontSize:a.a.fontSize*i,width:t.target.width()*i,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*i,shadowBlur:a.shadowBlur*i,shadowOffsetX:a.shadowOffsetX*i,shadowOffsetY:a.shadowOffsetY*i,strokeWidth:a.strokeWidth*i,borderSize:a.borderSize*i})},onTransformEnd:t=>{var e;const r=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},z(a,a.fontSize*r)),{width:Math.ceil(t.target.width()*r),x:R(t.target.x()),y:R(t.target.y()),rotation:R(t.target.rotation()),height:R(t.target.height()*r)})),null===(e=D.current)||void 0===e||e.setAttrs({scaleX:1,scaleY:1}),tt(!1)}})),G&&a.contentEditable&&t(l,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,children:t(et,{textNodeRef:j,element:a,selectAll:K,cursorPosition:yt.current,onBlur:()=>{a.set(k(a)),a.toggleEditMode(!1)}})})]})});
|
|
1
|
+
import{jsx as t,Fragment as e,jsxs as r}from"react/jsx-runtime";import{elementOpacity as o}from"./paint-opacity.js";import n from"react";import{toDisplayColor as i}from"@polotno/core/color";import{observer as a}from"mobx-react-lite";import{Group as l,Path as s,Text as c,TextPath as u}from"react-konva";import{Html as d}from"react-konva-utils";import{nodeAutorun as f}from"../utils/mobx.js";import h from"konva";import{parsePath as g,roundCommands as m}from"svg-round-corners/lib/index.js";import{generateBackgroundShapeFromRects as p,textLinesToRects as x}from"@polotno/core/background-shape";import{useColor as b}from"./use-color.js";import{useWorkspaceStyle as v}from"./workspace-style.js";import{ROLES as w}from"../model/store.js";import{incrementLoader as S}from"../utils/loader.js";import{activeAnchorOf as y,scaledFontSizeAttrs as E,roundedFontSizeAttrs as z,roundedTextDecorationAttrs as k,roundedGeometryPx as R,roundedPosition as j,textMinResizeWidth as F}from"../utils/px.js";import{isFontLoaded as A}from"../utils/fonts.js";import{flags as M}from"../utils/flags.js";import{textOverflowOf as O}from"../utils/text-sizing.js";import{FIT_FONT_SIZE_STEP as L,FIT_HEIGHT_EPSILON as C,legacyTextOverflow as T}from"@polotno/core/text-sizing";import{removeTags as H}from"@polotno/core/text";import{getDir as P,getCurvePath as W,resolveLineHeight as X,normalizeStrokeLineJoin as D}from"@polotno/core/text-html";import{cssFontFamily as Y}from"@polotno/core/fonts";import{shadowCastByBackground as $}from"@polotno/core/filters";import{applyOpacityToColor as I}from"@polotno/core/svg";import{applyFilter as J}from"./apply-filters.js";import{useFadeIn as N}from"./use-fadein.js";import{isTouchDevice as B}from"../utils/screen.js";import{isAlive as _,getParent as V}from"mobx-state-tree";import{getLimitedFontSize as q}from"./text-element/max-font-size.js";import{getOptimalCaretColor as U}from"./text-element/caret-color.js";import{visiblePlaceholder as Z}from"./text-element/placeholder.js";import{hasMeasuredHeight as G,resolveTextBoxHeight as K}from"@polotno/core/text-types";let Q;function tt(){return Q||(Q=document.getElementById("polotno-text-style"),Q||(Q=document.createElement("style"),Q.id="polotno-text-style",document.head.appendChild(Q)),Q)}h._fixTextRendering=!0;const et={border:"none",padding:"0px",overflow:"hidden",background:"none",outline:"none",resize:"none",overflowWrap:"break-word",whiteSpace:"pre-wrap",userSelect:"text",wordBreak:"normal",textTransform:"none"};export{getDir}from"@polotno/core/text-html";const rt=a(({textNodeRef:e,element:r,onBlur:o,selectAll:i,cursorPosition:a})=>{const[l,s]=n.useState(et),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=U(r);const e=`\n .polotno-input::placeholder {\n color: ${l.color};\n opacity: 0.6;\n }\n `,o=tt();o.innerHTML="",o.appendChild(document.createTextNode(e)),JSON.stringify(t)!==JSON.stringify(l)&&s(t)});const 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=K(r.a,f);"middle"===r.verticalAlign&&(d=(h-f)/2),"bottom"===r.verticalAlign&&(d=h-f);const g=H(r.text);return t("textarea",{className:"polotno-input",ref:u,dir:P(g),style:Object.assign(Object.assign(Object.assign({},et),l),{paddingTop:d+"px"}),value:g,onChange:t=>{const e=q({oldText:r.text,newText:t.target.value,element:r});r.set(Object.assign({text:t.target.value},E(r,e)))},placeholder:Z(r.placeholder),onBlur:o})}),ot=e=>t(d,{children:t(rt,Object.assign({},e))});export const useFontLoader=(t,e,r,o,i)=>{const[a,l]=n.useReducer(t=>t+1,0),s=n.useRef(A(e,r,o)),c=n.useRef(null);return n.useLayoutEffect(()=>{if(s.current=A(e,r,o),s.current){return}let n=!0;return(async()=>{s.current=!1,l();const a=S(`text ${e}`);await t.loadFont(e,r,o,i),n?(c.current=a,s.current=!0,l()):a()})(),()=>{c.current&&(c.current(),c.current=null),n=!1}},[e,r,o]),n.useEffect(()=>{s.current&&setTimeout(()=>{c.current&&(c.current(),c.current=null)})},[s.current]),[s.current]};export const useFontFamiliesLoader=(t,e,r,o,i)=>{const[a,l]=n.useReducer(t=>t+1,0),s=e.join("\0"),c=n.useRef(e.every(t=>A(t,r,o))),u=n.useRef(null);return n.useLayoutEffect(()=>{if(c.current=e.every(t=>A(t,r,o)),c.current){return}let n=!0;return(async()=>{c.current=!1,l();const a=S(`text fonts ${s}`);await Promise.all(e.map(e=>t.loadFont(e,r,o,i))),n?(u.current=a,c.current=!0,l()):a()})(),()=>{u.current&&(u.current(),u.current=null),n=!1}},[s,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(()=>X({fontFamily:e,fontSize:r,lineHeight:o}),[t,e,r,o]);export function usePrevious(t){const e=n.useRef(t),r=n.useRef(t);return n.useMemo(()=>{r.current=e.current,e.current=t},[t]),r.current}function nt(t,e,r=!1){const o=t.fontSize(),n=t.height(),i=H(e.text);let a=e.a.fontSize;t.height(void 0);const l=Math.round(2*e.a.fontSize)-1;for(let s=1;s<l;s++){const o=G(e.a)&&t.height()>e.a.height+C;let n=i.split("\n").join(" ").split(/[\s-]+/).reduce((t,e)=>(e=e.toLowerCase(),/[\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFFEF\u4E00-\u9FAF\uAC00-\uD7AF]/.test(e)?t.concat(e.split("")):t.concat(e)),[]),l=t.textArr.map(t=>t.text.toLowerCase()).join(";");const s=n.find(t=>!l.includes(t)||(l=l.replace(t,""),!1));if(!(o||s&&!r)){break}a-=L,t.fontSize(a)}return t.fontSize(o),t.height(n),a}export{getCurvePath}from"@polotno/core/text-html";export const TextElement=a(({element:a,store:d})=>{const{textOverflowIndicatorStyle:h}=v(),A=n.useRef(null),L=n.useRef(null),C=n.useRef(null),X=n.useRef([]),q=n.useRef(!0),[,U]=n.useReducer(t=>t+1,0),{editorEnabled:Q,selectAll:tt}=(t=>{const[e,r]=n.useState(!1),o=n.useRef(!1);return n.useEffect(()=>{var e=!0;return setTimeout(()=>{e&&(t._editModeEnabled&&(o.current=!0),r(!0),setTimeout(()=>{o.current=!1},50))},50),()=>{e=!1}},[]),{editorEnabled:e&&t._editModeEnabled,selectAll:o.current}})(a),[et,rt]=n.useState(!1),[it,at]=n.useState(!1),lt=n.useRef(a.a.height),st=n.useRef(0),ct=n.useRef(""),ut=n.useRef(0),dt=a.isSelected,ft=usePrevious(dt),{textVerticalResizeEnabled:ht}=M,gt=M.autoDeleteEmptyText,mt=usePrevious(a.fontFamily),[pt,xt]=n.useState([]);n.useEffect(()=>{var t,e;const r=null!==(e=null===(t=A.current)||void 0===t?void 0:t.textArr)&&void 0!==e?e:[];JSON.stringify(r)!==JSON.stringify(pt)&&xt(r)}),n.useEffect(()=>{if(a.a.width||"tablecell"===a.type){return}const t=A.current;t.width(600),a.set({width:1.4*t.getTextWidth()})},[]),n.useEffect(()=>{ft&&!dt&>&&""===a.text&&a.removable&&!Z(a.placeholder)&&d.deleteElements([a.id])},[gt,a.placeholder,a.removable,a.text,dt,ft]);const bt=()=>{if(a.curveEnabled){const t=A.current;null==t||t.clearCache(),null==t||t.setAttrs({filters:[]});const e=L.current;e&&J(e,a)}else{const t=A.current;t&&J(t,a)}};n.useLayoutEffect(()=>{if(!it){return f(a,()=>{bt()})}}),n.useEffect(()=>{const t=X.current;t.length>0&&(X.current=[],t.forEach(t=>t()))}),n.useEffect(()=>()=>{q.current=!1;const t=X.current;t.length>0&&(X.current=[],t.forEach(t=>t()))},[]);let vt=H(a.text);"uppercase"===a.textTransform&&(vt=vt.toUpperCase());const[wt]=useFontLoader(d,a.fontFamily,a.fontStyle,a.fontWeight,a.text||a.placeholder),St=()=>{if(a.curveEnabled){const t=L.current;return t.getSelfRect().height||t.fontSize()}const t=A.current.clone({height:void 0}),e=Math.ceil(t.fontSize()*t.lineHeight()*t.textArr.length+1);return t.destroy(),e};n.useLayoutEffect(()=>{if(!wt){return}if("tablecell"===a.type){const t=T(O(a));if("resize"===t){const t=St(),e=_(a)?V(a,2):null;(null==e?void 0:e.setCellContentHeight)&&e.setCellContentHeight(a.id,t)}else if("change-font-size"===t){const t=A.current;if(t){const e=nt(t,a,M.textSplitAllowed);e!==a.fontSize&&d.history.ignore(()=>{a.set({fontSize:e})})}}return}const t=T(O(a)),{textSplitAllowed:e}=M,r=(t,e,r,o)=>{const n=S(`text ${a.id}`),i=()=>{d.history.ignore(t,e,r).then(()=>{q.current?(X.current.push(n),U()):n()})};o?queueMicrotask(i):i()};if(!G(a)){const t=St();return void r(()=>{G(a)||a.set({height:t})},void 0,void 0,!0)}if(!d.isPlaying){if("change-font-size"!==t||et){if("resize"===t){const t=St();ht&&a.height<t&&!et&&r(()=>{var e;_(a)&&a.set({height:t}),null===(e=A.current)||void 0===e||e.height(t)},!1,!0),ht||a.height===t||et||r(()=>{var e;_(a)&&a.set({height:t}),null===(e=A.current)||void 0===e||e.height(t)},!1,!0)}}else{const t=A.current,o=a.curveEnabled?a.fontSize:nt(t,a,e);if(o!==a.fontSize){return void r(()=>{a.set(E(a,o))},!1,!0)}const n=St();a.height===n||ht||r(()=>{a.set({height:n})},!1,!0)}}}),n.useLayoutEffect(()=>{var t;if(wt&&a.curveEnabled){const e=null===(t=L.current)||void 0===t?void 0:t.getSelfRect().width;if(e){const t=e-a.a.width,r=a.a.rotation*Math.PI/180,o=-t/2*Math.cos(r),n=-t/2*Math.sin(r);a.set({width:e,x:a.a.x+o,y:a.a.y+n})}}},[wt,a.curveEnabled,a.curvePower,a.text,a.fontSize,a.fontFamily,a.fontWeight,a.fontStyle,a.letterSpacing]),n.useLayoutEffect(()=>{const t=A.current;t&&(t.width(t.width()+1e-8),t.width(t.width()-1e-8),t._setTextData(),bt())},[wt]);const yt=n.useRef(null),Et=n.useRef(a.width),zt=n.useRef(0),kt=t=>{t.evt.preventDefault();const e=d.selectedShapes.find(t=>t===a);e&&a.contentEditable&&(zt.current=function(t){var e;const r=t.target,o=function(t){var e=t.getAbsoluteTransform().copy();e.invert();var r=t.getStage().getPointerPosition();return e.point(r)}(r),n=r.textArr,i=Math.floor(o.y/(r.fontSize()*r.lineHeight())),a=n.slice(0,i).reduce((t,e)=>t+e.text.length,i),l=null!==(e=n[i])&&void 0!==e?e:n[0];let s=0;return"right"===r.align()?s=r.width()-l.width:"center"===r.align()&&(s=r.width()/2-l.width/2),a+Math.round((o.x-s)/l.width*l.text.length)}(t),a.toggleEditMode())},Rt=Z(a.placeholder),jt=!vt&&Rt?.6:o(a);N(A,jt);const Ft=getLineHeight({fontLoaded:wt,fontFamily:a.fontFamily,fontSize:a.a.fontSize,lineHeight:a.lineHeight}),At=a.selectable||d.role===w.ADMIN,Mt=b(a),Ot=b(a,a.stroke,"stroke"),Lt=n.useMemo(()=>a.backgroundEnabled||a.borderSize?a.curveEnabled?function({width:t,height:e,padding:r=0,cornerRadius:o=0}){const n=-r,i=-r,a=t+r,l=e+r,s=g(`M ${n} ${i} L ${a} ${i} L ${a} ${l} L ${n} ${l} Z`);return m(s,o).path}({width:a.a.width,height:a.a.height,cornerRadius:a.backgroundCornerRadius*(a.a.fontSize*Ft*.5),padding:a.backgroundPadding*(a.a.fontSize*Ft*.5)}):function({lines:t,lineHeight:e,width:r,align:o="left",padding:n=0,cornerRadius:i=0}){const a=x({lines:t,lineHeight:e,width:r,align:o});return p({rects:a,padding:n,cornerRadius:i})}({lines:JSON.parse(JSON.stringify(pt)),cornerRadius:a.backgroundCornerRadius*(a.a.fontSize*Ft*.5),lineHeight:Ft*a.a.fontSize,padding:a.backgroundPadding*(a.a.fontSize*Ft*.5),width:a.a.width,align:a.align}):"",[a.backgroundEnabled,a.borderSize,a.curveEnabled,a.backgroundCornerRadius,a.a.fontSize,a.a.height,Ft,a.backgroundPadding,a.a.width,a.align,pt]),Ct=B();let Tt=0;if(!a.curveEnabled){const t=pt.length*Ft*a.a.fontSize,e=K(a.a,t);"middle"===a.verticalAlign?Tt=(e-t)/2:"bottom"===a.verticalAlign&&(Tt=e-t)}const Ht=a.curveEnabled?W(a.a.width,a.a.height,a.curvePower,a.a.fontSize):"",Pt=wt?Y(a.fontFamily):mt===a.fontFamily?"Arial":Y(mt),Wt=P(vt),Xt=a.shadowEnabled&&!$(a),Dt={shadowEnabled:a.shadowEnabled&&($(a)||a.borderSize>0),shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:i(a.shadowColor),shadowOpacity:a.shadowOpacity};return r(e,{children:[t(s,Object.assign({ref:C,x:a.a.x,y:a.a.y,rotation:a.a.rotation,hideInExport:!a.showInExport,listening:!1,visible:a.backgroundEnabled||a.borderSize>0,opacity:jt,data:Lt,fillEnabled:a.backgroundEnabled,fill:I(a.backgroundColor,a.backgroundOpacity),strokeEnabled:a.borderSize>0,stroke:i(a.borderColor),strokeWidth:a.borderSize,lineJoin:a.backgroundCornerRadius>0?"round":"miter",offsetY:-Tt},Dt)),t(s,{data:Ht,stroke:h.stroke,strokeWidth:1,x:a.a.x,y:a.a.y,rotation:a.a.rotation,visible:!1}),t(u,Object.assign({ref:L,visible:a.curveEnabled,data:Ht,text:vt||Rt,listening:!1,align:"center",textBaseline:"middle",direction:Wt},Mt,Ot,{strokeWidth:a.strokeWidth,lineJoin:D(a.strokeLineJoin),fillAfterStrokeEnabled:!0,fontSize:a.a.fontSize,fontFamily:`${Y(a.fontFamily)}, ${Y(mt)}`,fontStyle:a.fontStyle+" "+a.fontWeight,textDecoration:a.textDecoration.trim(),letterSpacing:a.letterSpacing*a.a.fontSize,x:a.a.x,y:a.a.y,rotation:a.a.rotation,opacity:a._editModeEnabled?.3:jt,hideInExport:!a.showInExport},Dt,{shadowEnabled:Xt})),t(c,Object.assign({ref:A,id:a.selectable?a.id:void 0,name:a.selectable?"element":"",hideInExport:!a.showInExport,editModeEnabled:a._editModeEnabled,x:a.a.x,y:a.a.y,rotation:a.a.rotation,width:a.a.width,height:G(a.a)?a.a.height:void 0,text:vt||Rt,direction:Wt},Mt,Ot,{lineJoin:D(a.strokeLineJoin),strokeWidth:a.strokeWidth,fillAfterStrokeEnabled:!0,fontSize:a.a.fontSize,fontFamily:Pt,fontStyle:a.fontStyle+" "+a.fontWeight,textDecoration:a.textDecoration,align:a.align,verticalAlign:a.verticalAlign,draggable:Ct?a.draggable&&dt:a.draggable,preventDefault:!Ct||dt,opacity:a.curveEnabled?0:jt,visible:!a._editModeEnabled,ellipsis:"ellipsis"===O(a)},Dt,{shadowEnabled:Xt,lineHeight:Ft,letterSpacing:a.letterSpacing*a.a.fontSize,listening:!!a.selectable&&At,onDragStart:()=>{at(!0)},onDragMove:t=>{a.set({x:t.target.x(),y:t.target.y()})},onDragEnd:t=>{at(!1),a.set(j(t.target))},onClick:kt,onTap:kt,onTransformStart:()=>{rt(!0);const t=A.current;lt.current=t.height(),Et.current=a.a.width,yt.current=t.position(),st.current=t.rotation(),ut.current=t.height(),ct.current=y(t)},onTransform:t=>{var e,r;const o=t.target;null===(e=C.current)||void 0===e||e.setAttrs({x:o.x(),y:o.y(),rotation:o.rotation(),scale:o.scale()});const n=ct.current;if("middle-left"===n||"middle-right"===n){const t=o.scaleX(),e=o.width()*t,r=F(Et.current,a.a.fontSize);let n=e;e<r&&(n=r,yt.current&&o.position(yt.current)),o.width(n),o.scaleX(1),o.scaleY(1);let i=St();if("ellipsis"===O(a)){i=lt.current}else{const t=Math.max(i,lt.current);o.height(t),a.set({height:o.height()})}const l=M.textVerticalResizeEnabled?Math.max(i,lt.current):St();a.set({x:o.x(),width:o.width(),rotation:o.rotation(),height:l}),o.skewX(0),o.skewY(0),bt()}if("top-center"===n||"bottom-center"===n){const e="resize"===O(a)?St():Ft*a.a.fontSize,r=t.target.height()*t.target.scaleY();if(r<e){if(t.target.height(e),yt.current){if("top-center"===ct.current){const r=st.current*Math.PI/180,o=ut.current-e;t.target.x(yt.current.x-o*Math.sin(r)),t.target.y(yt.current.y+o*Math.cos(r))}else{t.target.position(yt.current)}}}else{t.target.height(r)}t.target.scaleY(1),t.target.scaleX(1),t.target.rotation(st.current),t.target.skewX(0),t.target.skewY(0),ut.current=Math.max(e,r)}yt.current=t.target.position();const i=t.target.scaleX();null===(r=C.current)||void 0===r||r.setAttrs({scaleX:1,scaleY:1}),t.target.scaleX(1),t.target.scaleY(1),a.set({fontSize:a.a.fontSize*i,width:t.target.width()*i,x:t.target.x(),y:t.target.y(),rotation:t.target.rotation(),height:t.target.height()*i,shadowBlur:a.shadowBlur*i,shadowOffsetX:a.shadowOffsetX*i,shadowOffsetY:a.shadowOffsetY*i,strokeWidth:a.strokeWidth*i,borderSize:a.borderSize*i})},onTransformEnd:t=>{var e;const r=t.target.scaleX();t.target.scaleX(1),t.target.scaleY(1),a.set(Object.assign(Object.assign({},z(a,a.fontSize*r)),{width:Math.ceil(t.target.width()*r),x:R(t.target.x()),y:R(t.target.y()),rotation:R(t.target.rotation()),height:R(t.target.height()*r)})),null===(e=C.current)||void 0===e||e.setAttrs({scaleX:1,scaleY:1}),rt(!1)}})),Q&&a.contentEditable&&t(l,{x:a.a.x,y:a.a.y,rotation:a.a.rotation,children:t(ot,{textNodeRef:A,element:a,selectAll:tt,cursorPosition:zt.current,onBlur:()=>{a.set(k(a)),a.toggleEditMode(!1)}})})]})});
|
package/canvas/use-color.d.ts
CHANGED
|
@@ -6,8 +6,8 @@ type ColorElement = {
|
|
|
6
6
|
fill?: string;
|
|
7
7
|
};
|
|
8
8
|
export declare const withDisplayTextPaints: <T extends {
|
|
9
|
-
fill
|
|
10
|
-
stroke
|
|
9
|
+
fill?: string;
|
|
10
|
+
stroke?: string;
|
|
11
11
|
}>(element: T) => T;
|
|
12
12
|
export declare const useColor: (element: ColorElement, value?: string, propName?: string) => {
|
|
13
13
|
[x: string]: string;
|
package/canvas/video-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{elementOpacity as i}from"./paint-opacity.js";import o from"react";import{toDisplayColor as n}from"@polotno/core/color";import{observer as a}from"mobx-react-lite";import{isAlive as s}from"mobx-state-tree";import{nodeAutorun as d}from"../utils/mobx.js";import{Arc as c,Group as h,Image as l,Rect as u,Text as m}from"react-konva";import{ROLES as p}from"../model/store.js";import{DEFAULT_RETRIES as g,isDeterministicUrl as f,retryDelayMs as v}from"@polotno/core/retry";import w from"konva";import{useWorkspaceStyle as x}from"./workspace-style.js";import{incrementLoader as E,triggerLoadError as y}from"../utils/loader.js";import{polotnoError as M}from"@polotno/core/errors";import{getCropRect as L}from"@polotno/core/image";import*as b from"@polotno/core/svg";import{roundEnabledPx as T,activeAnchorOf as C,roundedCropFraction as k,roundedGeometryPx as I,roundedPosition as R,isCornerAnchor as j,scaledBoxStyleAttrs as S,roundedBoxStyleAttrs as P}from"../utils/px.js";import{useFilteredSource as H}from"./use-filtered-source.js";import{useEvent as O}from"../hooks/use-event.js";import{useFadeIn as X}from"./use-fadein.js";import{isTouchDevice as Y}from"../utils/screen.js";import{createPlayGate as A,playMedia as W}from"../utils/media.js";import{useMediabunnyVideo as z}from"./use-mediabunny-video.js";import{getElementTimeRange as _,getVideoRestMs as D}from"../utils/timeline.js";import{drawReduced as V,exportKernelEnabled as U,shouldResample as Z,sourceSize as B}from"./downscale.js";import{trySetCanvasSize as F}from"../utils/canvas.js";import{useClipImage as N}from"./image-element.js";import{CropOverlay as $,CROP_CORNER_ANCHORS as q}from"./crop-overlay.js";const G=new window.Image;G.src=b.svgToURL('\n <svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">\n <path opacity="0.6" d="M4 5.49683V18.5032C4 20.05 5.68077 21.0113 7.01404 20.227L18.0694 13.7239C19.384 12.9506 19.384 11.0494 18.0694 10.2761L7.01404 3.77296C5.68077 2.98869 4 3.95 4 5.49683Z" fill="white"/>\n <path d="M4 5.49683V18.5032C4 20.05 5.68077 21.0113 7.01404 20.227L18.0694 13.7239C19.384 12.9506 19.384 11.0494 18.0694 10.2761L7.01404 3.77296C5.68077 2.98869 4 3.95 4 5.49683Z" stroke="#323232" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>\n </svg>\n');const J=new window.Image;J.src=b.svgToURL('\n <svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">\n <path opacity="0.6" d="M14 19L14 5C14 3.89543 14.8954 3 16 3L17 3C18.1046 3 19 3.89543 19 5L19 19C19 20.1046 18.1046 21 17 21L16 21C14.8954 21 14 20.1046 14 19Z" fill="white"/>\n <path opacity="0.6" d="M10 19L10 5C10 3.89543 9.10457 3 8 3L7 3C5.89543 3 5 3.89543 5 5L5 19C5 20.1046 5.89543 21 7 21L8 21C9.10457 21 10 20.1046 10 19Z" fill="white"/>\n <path d="M14 19L14 5C14 3.89543 14.8954 3 16 3L17 3C18.1046 3 19 3.89543 19 5L19 19C19 20.1046 18.1046 21 17 21L16 21C14.8954 21 14 20.1046 14 19Z" stroke="#323232" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>\n <path d="M10 19L10 5C10 3.89543 9.10457 3 8 3L7 3C5.89543 3 5 3.89543 5 5L5 19C5 20.1046 5.89543 21 7 21L8 21C9.10457 21 10 20.1046 10 19Z" stroke="#323232" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>\n </svg>\n');export const useCornerRadiusAndCrop=(e,t,r,i,n=0,a=!1)=>{var s;const d=Math.floor(Math.max(e.width*i,1)),c=Math.floor(Math.max(e.height*i,1)),h=Math.min(n*i,d/2,c/2),l=Math.max(e.width/r.width,e.height/r.height)*i,u=U(e.page)&&!a&&Z(l,d,c),{width:m,height:p}=t?B(t):{},g=0===r.x&&0===r.y&&r.width===m&&r.height===p,f=o.useMemo(()=>{if(t&&m&&p){return g&&0===h&&!u?void 0:document.createElement("canvas")}},[t,h,u,g,m,p]),v=()=>{if(!f||!t){return}f.width=d,f.height=c;const e=f.getContext("2d");e&&(h&&(e.beginPath(),e.moveTo(h,0),e.lineTo(d-h,0),e.arc(d-h,h,h,3*Math.PI/2,0,!1),e.lineTo(d,c-h),e.arc(d-h,c-h,h,0,Math.PI/2,!1),e.lineTo(h,c),e.arc(h,c-h,h,Math.PI/2,Math.PI,!1),e.lineTo(0,h),e.arc(h,h,h,Math.PI,3*Math.PI/2,!1),e.clip()),u?V(e,t,r,f.width,f.height):e.drawImage(t,r.x,r.y,r.width,r.height,0,0,f.width,f.height))};return o.useLayoutEffect(()=>{v()},[f,e.width,e.height,r.x,r.y,r.width,r.height,n,i,a,null===(s=e.page)||void 0===s?void 0:s._exportingOrRendering,u]),o.useEffect(()=>()=>{f&&"CANVAS"===f.nodeName&&w.Util.releaseCanvas(f)},[f]),[f||t,v]};export const useClip=(e,t,r,i)=>{const[n,a]=N(e.clipSrc||""),s=e.clipSrc?a:"loaded";useLoader(s,e.clipSrc,e.id);const d=!!t&&!!n,c=o.useMemo(()=>d?{canvas:document.createElement("canvas"),mask:document.createElement("canvas"),width:0,height:0}:void 0,[d]),h=null==c?void 0:c.canvas;function l(){if(!c||!h||!t){return}const{width:i,height:o}=B(t);if(!i||!o){return}const n=Math.min(1e4,Math.max(i,Math.ceil(e.a.width*r),1)),a=Math.min(1e4,Math.max(o,Math.ceil(e.a.height*r),1));c.width===n&&c.height===a||(F(h,n,a),c.width=n,c.height=a);const s=h.getContext("2d");s&&(s.clearRect(0,0,h.width,h.height),s.save(),s.drawImage(c.mask,0,0,h.width,h.height),s.globalCompositeOperation="source-in",s.drawImage(t,0,0,h.width,h.height),s.restore())}return o.useLayoutEffect(()=>{var t;if(!c||!(null==n?void 0:n.width)||!n.height){return}const i=Math.max(e.a.width/n.width*r,e.a.height/n.height*r);F(c.mask,Math.min(1e4,Math.max(n.width*i,1)),Math.min(1e4,Math.max(n.height*i,1))),null===(t=c.mask.getContext("2d"))||void 0===t||t.drawImage(n,0,0,c.mask.width,c.mask.height)},[c,n,e.a.width,e.a.height,r]),o.useLayoutEffect(()=>{l()},[c,t,n,e.a.width,e.a.height,r,...i]),o.useEffect(()=>()=>{c&&w.Util.releaseCanvas(c.canvas,c.mask)},[c]),[d?h:t,l]};const K=document.createElement("canvas"),Q=a(({element:r})=>{const n=Math.min(30,r.width/4,r.height/4),a=o.useRef(null);o.useEffect(()=>{const e=a.current;if(!e){return}const t=new w.Animation(t=>{e.rotate(((null==t?void 0:t.timeDiff)||0)/2)},e.getLayer());return t.start(),()=>{t.stop()}});const{mediaLoadingStyle:s}=x();return t(h,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:i(r),hideInExport:!r.showInExport,children:[e(u,{width:r.width,height:r.height,fill:s.fill}),e(c,{ref:a,x:r.width/2,y:r.height/2,fill:s.textFill,outerRadius:Math.abs(n),innerRadius:Math.max(1,n-5),angle:270})]})}),ee=a(({element:r})=>{const{mediaErrorStyle:o}=x(),n=Math.max(10,Math.min(30,r.width/25));return t(h,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:i(r),hideInExport:!r.showInExport,children:[e(u,{width:r.width,height:r.height,fill:o.fill}),e(m,{text:"Can not load the video...",fontSize:n,width:r.width,height:r.height,align:"center",fill:o.textFill,verticalAlign:"middle",padding:5})]})});let te=function(e,t,r){const i=o.useRef("loading"),n=o.useRef(void 0),[a,s]=o.useState(0),d=o.useRef(void 0),c=o.useRef(void 0),h=o.useRef(void 0);return d.current===e&&c.current===t&&h.current===r||(i.current="loading",n.current=void 0,d.current=e,c.current=t,h.current=r),o.useLayoutEffect(()=>{if(!e){return}const o=f(e)?1:g;let a,d,c,h=0,l=!1,u=!1;const m=e=>{e&&(e.removeAttribute("src"),e.load())},p=()=>{h+=1;const g=document.createElement("video");function f(){l||(g.removeEventListener("canplay",f),u=!0,i.current="loaded",n.current=g,g.currentTime=0,s(Math.random()))}function w(e){var t;const r=4===g.readyState,c=g.buffered&&g.buffered.length>0&&g.buffered.end(g.buffered.length-1)/g.duration*100>=100;if(r&&c){return}if(l){return}if(!u&&h<o){return null==d||d(),m(g),void(a=setTimeout(p,v(h)))}const f=e.message||(null===(t=g.error)||void 0===t?void 0:t.message)||"Unknown error";console.error(new Error("Video failed to load: "+f)),i.current="failed",n.current=void 0,s(Math.random())}c=g,g.addEventListener("canplay",f),g.addEventListener("error",w),d=()=>{g.removeEventListener("canplay",f),g.removeEventListener("error",w)},t&&(g.crossOrigin=t),r&&(g.referrerPolicy=r),g.src=e,g.load()};return p(),()=>{l=!0,clearTimeout(a),null==d||d(),u||m(c)}},[e,t,r]),[n.current,i.current]};export const setVideoLoaderHook=e=>{te=e};function re(e){return!e.seeking&&e.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA}export const useLoader=(e,t,r)=>{const i=o.useRef(void 0),n=()=>{var e;null===(e=i.current)||void 0===e||e.call(i),i.current=void 0};o.useEffect(()=>n,[]),o.useLayoutEffect(()=>{const o=t.slice(0,200),a=`video with id ${r} url: ${o}`;"loading"!==e||i.current||(i.current=E(a)),"loading"!==e&&n(),"failed"===e&&y(M("VIDEO_FAILED",a,{src:o,elementId:r,reason:"load"}))},[e])};export const VideoElement=a(({element:a,store:c})=>{var m,g,f,v,x;const[y,M]=o.useState(!1),b=o.useRef(null),V=a.isSelected,[U,Z]=o.useState(!1),[F,N]=te(a.src,"anonymous"),[ie,oe]=o.useState(!1),ne=c.activePage===a.page,ae=(null===(m=a.page)||void 0===m?void 0:m._exportingOrRendering)&&"loaded"===N,se=z(a,c,ae);useLoader(N,a.src,a.id),F&&(F.playsInline=!0);const de=ae&&se||F,[ce,he]=((e,t)=>{const{flipX:r,flipY:i}=e,{width:n,height:a}=t?B(t):{},s=e=>{if(!t){return}const o=e.getContext("2d");o&&(o.clearRect(0,0,e.width,e.height),o.save(),o.scale(r?-1:1,i?-1:1),o.drawImage(t,r?-e.width:0,i?-e.height:0,e.width,e.height),o.restore())},d=o.useMemo(()=>{if(!r&&!i||!n||!a){return}const e=document.createElement("canvas");return e.width=n,e.height=a,s(e),e},[r,i,t,n,a]);return o.useEffect(()=>()=>{d&&w.Util.releaseCanvas(d)},[d]),[d||t,()=>{d&&s(d)}]})(a,de),[le=K,ue]=H(a,ce||K);o.useEffect(()=>{F&&c.history.ignore(()=>{a.set({duration:1e3*F.duration})})},[F]);const me=o.useMemo(A,[F]);o.useEffect(()=>{var e;if(!F){return}const t=c.animatedElementsIds,r=(!t.length||t.includes(a.id))&&c.isPlaying;if(!ne||!r&&!U){return void F.pause()}U&&(F.muted=!1,F.currentTime=a.startTime*F.duration,W(F).then(e=>{"refused"===e&&Z(!1)}));const i=new w.Animation(()=>{Pe()},null===(e=b.current)||void 0===e?void 0:e.getLayer());i.start();const o=()=>{Z(!1),F.currentTime=a.startTime*F.duration},n=()=>{F.currentTime>=a.endTime*F.duration&&(F.pause(),F.currentTime=a.startTime*F.duration,Z(!1))};return F.addEventListener("ended",o),F.addEventListener("timeupdate",n),()=>{F.pause(),i.stop(),F.removeEventListener("ended",o),F.removeEventListener("timeupdate",n)}},[F,U,c.isPlaying,ne]),o.useEffect(()=>{F&&(F.volume=a.volume)},[F,a.volume]),o.useEffect(()=>{F&&(F.playbackRate=a.speed,F.preservesPitch=!1)},[F,a.speed]);const pe=o.useRef(null);o.useEffect(()=>{const e=d(a,()=>{var e,t;if(F){if(ae&&se){return Pe(),void c.currentTime}const r=c.animatedElementsIds;if(r.length&&!r.includes(a.id)){return}const i=c.activePage===a.page,o=c.currentTime>=a.page.startTime&&c.currentTime<a.page.startTime+a.page.duration,n=a.startTime*F.duration*1e3,s=F.duration*(a.endTime-a.startTime)*1e3,{start:d,end:h}=_(a.animations,a.page.duration),l=(c.currentTime||a.page.startTime)-a.page.startTime,u=o?D(c.currentTime,a.page.startTime,d,s,n,a.speed):0,m=u/1e3,p=o&&l>=d&&l<h;U||(F.muted=!p,p&&c.isPlaying&&i?F.paused?(F.currentTime=m,me.play(F)):Math.abs(1e3*F.currentTime-u)>500&&(F.currentTime=m):(me.reset(),F.paused||F.pause(),m!==F.currentTime&&(F.currentTime=m),!re(F)&&!c.isPlaying&&!pe.current&&(pe.current=E(`video ${a.id}`)))),Pe(),null===(t=null===(e=b.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()}});return()=>{e(),pe.current&&(pe.current(),pe.current=null)}},[c,F,ae,se,U]),o.useEffect(()=>{var e,t;if(!F){return}if(ae){return null===(e=pe.current)||void 0===e||e.call(pe),void(pe.current=null)}const r=()=>{var e;re(F)&&(Pe(),null===(e=pe.current)||void 0===e||e.call(pe),pe.current=null)};let i;return(null===(t=a.page)||void 0===t?void 0:t._exportingOrRendering)&&(i=setInterval(r,20)),F.addEventListener("timeupdate",r),F.addEventListener("canplay",r),F.addEventListener("seeked",r),()=>{clearInterval(i),F.removeEventListener("timeupdate",r),F.removeEventListener("canplay",r),F.removeEventListener("seeked",r)}},[null===(g=a.page)||void 0===g?void 0:g._exportingOrRendering,F,a.id,E,ae]);let{cropX:ge,cropY:fe,cropWidth:ve,cropHeight:we}=a;"loaded"!==N&&(ge=fe=0,ve=we=1);const{width:xe,height:Ee}=B(le),ye=null!==(f=null==F?void 0:F.videoWidth)&&void 0!==f?f:0,Me=null!==(v=null==F?void 0:F.videoHeight)&&void 0!==v?v:0;o.useLayoutEffect(()=>{"loaded"===N&&ye>0&&Me>0&&a._setSourceSize({width:ye,height:Me},a.src)},[a,a.src,N,ye,Me]);const Le=L({stretchEnabled:a.stretchEnabled,cropX:ge,cropY:fe,cropWidth:ve,cropHeight:we,width:a.a.width,height:a.a.height},{width:xe,height:Ee}),be=Le.width,Te=Le.height,Ce=a.stretchEnabled,ke=null!==(x=a.cornerRadius)&&void 0!==x?x:0,[Ie,Re]=useCornerRadiusAndCrop(a,le,Le,c._elementsPixelRatio,ke,!0);let[je,Se]=useClip(a,Ie,c._elementsPixelRatio,[Le,ke,c._elementsPixelRatio]);const Pe=O(()=>{var e,t;he(),ue(),Re(),Se(),null===(t=null===(e=b.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()});o.useEffect(()=>{Pe()});const He=()=>{a._cropEnabled&&setTimeout(()=>{s(a)&&a.toggleCropMode(!0)})},Oe="loading"===N,Xe="failed"===N,Ye=!Oe&&!Xe,Ae=o.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),We=o.useRef(!1),ze=Ye?i(a):0;X(b,ze);const _e=a.selectable||c.role===p.ADMIN,De=Y();return e(r,{children:t(h,{onMouseEnter:()=>oe(!0),onMouseLeave:()=>oe(!1),children:[Oe&&e(Q,{element:a}),Xe&&e(ee,{element:a}),e(l,{ref:b,name:"element",id:a.id,image:je,x:a.a.x,y:a.a.y,width:a.a.width||1,height:a.a.height||1,rotation:a.a.rotation,opacity:ze,shadowEnabled:a.shadowEnabled,shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:n(a.shadowColor),shadowOpacity:a.shadowOpacity,customCrop:Le,listening:_e,draggable:De?a.draggable&&V:a.draggable,preventDefault:!De||V,hideInExport:!a.showInExport,onDragMove:e=>{a.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{a.set(R(e.target))},onDblClick:He,onDblTap:He,onTransformStart:e=>{M(!0),We.current=j(C(e.target)),Ae.current={cropX:a.cropX,cropY:a.cropY,cropWidth:a.cropWidth,cropHeight:a.cropHeight}},onTransform:e=>{const t=e.currentTarget,r=Math.abs(t.scaleX()-1)<1e-7?1:t.scaleX(),i=Math.abs(t.scaleY()-1)<1e-7?1:t.scaleY();t.scaleX(1),t.scaleY(1);const o=1-be/xe,n=Math.min(o,Math.max(0,a.cropX)),s=1-Te/Ee,d=Math.min(s,Math.max(0,a.cropY)),c=We.current,h=!c&&r<1&&Ae.current.cropHeight>Te/Ee;let l=c?a.cropWidth:a.cropWidth*r;h&&(l=a.cropWidth);const u=!c&&i<1&&Ae.current.cropWidth>be/xe;let m=c?a.cropHeight:a.cropHeight*i;u&&(m=a.cropHeight),Ce&&(l=a.cropWidth,m=a.cropHeight);const p=c?Math.min(r,i):1;a.set(Object.assign({cropX:n,cropY:d,x:t.x(),y:t.y(),width:t.width()*r,height:t.height()*i,rotation:e.target.rotation(),cropWidth:Math.min(l,1-n),cropHeight:Math.min(m,1-d)},1!==p&&Object.assign(Object.assign({},S(a,p)),{borderSize:a.borderSize*p})))},onTransformEnd:e=>{const t=e.currentTarget;a.set(Object.assign(Object.assign({width:I(t.width()),height:I(t.height()),x:I(t.x()),y:I(t.y()),rotation:I(e.target.rotation()),cropX:k(a.cropX),cropY:k(a.cropY),cropWidth:k(be/xe),cropHeight:k(Te/Ee)},P(a)),{borderSize:T(a.borderSize)})),M(!1)}}),e(u,{x:a.a.x,y:a.a.y,width:Math.max(a.a.width-a.borderSize,0),height:Math.max(a.a.height-a.borderSize,0),opacity:ze,offsetX:-a.borderSize/2,offsetY:-a.borderSize/2,stroke:n(a.borderColor),strokeWidth:a.borderSize,listening:!1,visible:!!a.borderSize,rotation:a.rotation,cornerRadius:Math.max(0,ke-a.borderSize),hideInExport:!a.showInExport}),!c.isPlaying&&e(l,{image:U?ie||De?J:void 0:G,x:a.a.x,y:a.a.y,offsetX:-a.a.width/2+15/c.scale,offsetY:-a.a.height/2+15/c.scale,rotation:a.a.rotation,width:30/c.scale,height:30/c.scale,hideInExport:!0,onClick:()=>Z(!U),onTap:()=>Z(!U)}),a._cropModeEnabled&&"loaded"===N&&e($,{element:a,store:c,previewImage:je,sourceImage:le,sourceWidth:xe,sourceHeight:Ee,cropAbsoluteWidth:be,cropAbsoluteHeight:Te,innerAnchors:q})]})})});
|
|
1
|
+
import{jsx as e,jsxs as t,Fragment as r}from"react/jsx-runtime";import{elementOpacity as i}from"./paint-opacity.js";import o from"react";import{toDisplayColor as n}from"@polotno/core/color";import{observer as a}from"mobx-react-lite";import{isAlive as s}from"mobx-state-tree";import{nodeAutorun as d}from"../utils/mobx.js";import{Arc as c,Group as h,Image as l,Rect as u,Text as m}from"react-konva";import{ROLES as p}from"../model/store.js";import{DEFAULT_RETRIES as g,isDeterministicUrl as f,retryDelayMs as v}from"@polotno/core/retry";import w from"konva";import{useWorkspaceStyle as x}from"./workspace-style.js";import{incrementLoader as E,triggerLoadError as y}from"../utils/loader.js";import{polotnoError as M}from"@polotno/core/errors";import{getCropRect as L}from"@polotno/core/image";import*as T from"@polotno/core/svg";import{activeAnchorOf as C,roundedCropFraction as b,roundedGeometryPx as k,roundedPosition as I,isCornerAnchor as R,scaledStyleAttrs as j,roundedStyleAttrs as S}from"../utils/px.js";import{useFilteredSource as P}from"./use-filtered-source.js";import{useEvent as H}from"../hooks/use-event.js";import{useFadeIn as X}from"./use-fadein.js";import{isTouchDevice as Y}from"../utils/screen.js";import{createPlayGate as A,playMedia as W}from"../utils/media.js";import{useMediabunnyVideo as O}from"./use-mediabunny-video.js";import{getElementTimeRange as _,getVideoRestMs as D}from"../utils/timeline.js";import{drawReduced as z,exportKernelEnabled as V,shouldResample as U,sourceSize as Z}from"./downscale.js";import{trySetCanvasSize as B}from"../utils/canvas.js";import{useClipImage as F}from"./image-element.js";import{CropOverlay as N,CROP_CORNER_ANCHORS as $}from"./crop-overlay.js";const q=new window.Image;q.src=T.svgToURL('\n <svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">\n <path opacity="0.6" d="M4 5.49683V18.5032C4 20.05 5.68077 21.0113 7.01404 20.227L18.0694 13.7239C19.384 12.9506 19.384 11.0494 18.0694 10.2761L7.01404 3.77296C5.68077 2.98869 4 3.95 4 5.49683Z" fill="white"/>\n <path d="M4 5.49683V18.5032C4 20.05 5.68077 21.0113 7.01404 20.227L18.0694 13.7239C19.384 12.9506 19.384 11.0494 18.0694 10.2761L7.01404 3.77296C5.68077 2.98869 4 3.95 4 5.49683Z" stroke="#323232" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>\n </svg>\n');const G=new window.Image;G.src=T.svgToURL('\n <svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">\n <path opacity="0.6" d="M14 19L14 5C14 3.89543 14.8954 3 16 3L17 3C18.1046 3 19 3.89543 19 5L19 19C19 20.1046 18.1046 21 17 21L16 21C14.8954 21 14 20.1046 14 19Z" fill="white"/>\n <path opacity="0.6" d="M10 19L10 5C10 3.89543 9.10457 3 8 3L7 3C5.89543 3 5 3.89543 5 5L5 19C5 20.1046 5.89543 21 7 21L8 21C9.10457 21 10 20.1046 10 19Z" fill="white"/>\n <path d="M14 19L14 5C14 3.89543 14.8954 3 16 3L17 3C18.1046 3 19 3.89543 19 5L19 19C19 20.1046 18.1046 21 17 21L16 21C14.8954 21 14 20.1046 14 19Z" stroke="#323232" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>\n <path d="M10 19L10 5C10 3.89543 9.10457 3 8 3L7 3C5.89543 3 5 3.89543 5 5L5 19C5 20.1046 5.89543 21 7 21L8 21C9.10457 21 10 20.1046 10 19Z" stroke="#323232" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>\n </svg>\n');export const useCornerRadiusAndCrop=(e,t,r,i,n=0,a=!1)=>{var s;const d=Math.floor(Math.max(e.width*i,1)),c=Math.floor(Math.max(e.height*i,1)),h=Math.min(n*i,d/2,c/2),l=Math.max(e.width/r.width,e.height/r.height)*i,u=V(e.page)&&!a&&U(l,d,c),{width:m,height:p}=t?Z(t):{},g=0===r.x&&0===r.y&&r.width===m&&r.height===p,f=o.useMemo(()=>{if(t&&m&&p){return g&&0===h&&!u?void 0:document.createElement("canvas")}},[t,h,u,g,m,p]),v=()=>{if(!f||!t){return}f.width=d,f.height=c;const e=f.getContext("2d");e&&(h&&(e.beginPath(),e.moveTo(h,0),e.lineTo(d-h,0),e.arc(d-h,h,h,3*Math.PI/2,0,!1),e.lineTo(d,c-h),e.arc(d-h,c-h,h,0,Math.PI/2,!1),e.lineTo(h,c),e.arc(h,c-h,h,Math.PI/2,Math.PI,!1),e.lineTo(0,h),e.arc(h,h,h,Math.PI,3*Math.PI/2,!1),e.clip()),u?z(e,t,r,f.width,f.height):e.drawImage(t,r.x,r.y,r.width,r.height,0,0,f.width,f.height))};return o.useLayoutEffect(()=>{v()},[f,e.width,e.height,r.x,r.y,r.width,r.height,n,i,a,null===(s=e.page)||void 0===s?void 0:s._exportingOrRendering,u]),o.useEffect(()=>()=>{f&&"CANVAS"===f.nodeName&&w.Util.releaseCanvas(f)},[f]),[f||t,v]};export const useClip=(e,t,r,i)=>{const[n,a]=F(e.clipSrc||""),s=e.clipSrc?a:"loaded";useLoader(s,e.clipSrc,e.id);const d=!!t&&!!n,c=o.useMemo(()=>d?{canvas:document.createElement("canvas"),mask:document.createElement("canvas"),width:0,height:0}:void 0,[d]),h=null==c?void 0:c.canvas;function l(){if(!c||!h||!t){return}const{width:i,height:o}=Z(t);if(!i||!o){return}const n=Math.min(1e4,Math.max(i,Math.ceil(e.a.width*r),1)),a=Math.min(1e4,Math.max(o,Math.ceil(e.a.height*r),1));c.width===n&&c.height===a||(B(h,n,a),c.width=n,c.height=a);const s=h.getContext("2d");s&&(s.clearRect(0,0,h.width,h.height),s.save(),s.drawImage(c.mask,0,0,h.width,h.height),s.globalCompositeOperation="source-in",s.drawImage(t,0,0,h.width,h.height),s.restore())}return o.useLayoutEffect(()=>{var t;if(!c||!(null==n?void 0:n.width)||!n.height){return}const i=Math.max(e.a.width/n.width*r,e.a.height/n.height*r);B(c.mask,Math.min(1e4,Math.max(n.width*i,1)),Math.min(1e4,Math.max(n.height*i,1))),null===(t=c.mask.getContext("2d"))||void 0===t||t.drawImage(n,0,0,c.mask.width,c.mask.height)},[c,n,e.a.width,e.a.height,r]),o.useLayoutEffect(()=>{l()},[c,t,n,e.a.width,e.a.height,r,...i]),o.useEffect(()=>()=>{c&&w.Util.releaseCanvas(c.canvas,c.mask)},[c]),[d?h:t,l]};const J=document.createElement("canvas"),K=a(({element:r})=>{const n=Math.min(30,r.width/4,r.height/4),a=o.useRef(null);o.useEffect(()=>{const e=a.current;if(!e){return}const t=new w.Animation(t=>{e.rotate(((null==t?void 0:t.timeDiff)||0)/2)},e.getLayer());return t.start(),()=>{t.stop()}});const{mediaLoadingStyle:s}=x();return t(h,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:i(r),hideInExport:!r.showInExport,children:[e(u,{width:r.width,height:r.height,fill:s.fill}),e(c,{ref:a,x:r.width/2,y:r.height/2,fill:s.textFill,outerRadius:Math.abs(n),innerRadius:Math.max(1,n-5),angle:270})]})}),Q=a(({element:r})=>{const{mediaErrorStyle:o}=x(),n=Math.max(10,Math.min(30,r.width/25));return t(h,{x:r.a.x,y:r.a.y,rotation:r.a.rotation,listening:!1,opacity:i(r),hideInExport:!r.showInExport,children:[e(u,{width:r.width,height:r.height,fill:o.fill}),e(m,{text:"Can not load the video...",fontSize:n,width:r.width,height:r.height,align:"center",fill:o.textFill,verticalAlign:"middle",padding:5})]})});let ee=function(e,t,r){const i=o.useRef("loading"),n=o.useRef(void 0),[a,s]=o.useState(0),d=o.useRef(void 0),c=o.useRef(void 0),h=o.useRef(void 0);return d.current===e&&c.current===t&&h.current===r||(i.current="loading",n.current=void 0,d.current=e,c.current=t,h.current=r),o.useLayoutEffect(()=>{if(!e){return}const o=f(e)?1:g;let a,d,c,h=0,l=!1,u=!1;const m=e=>{e&&(e.removeAttribute("src"),e.load())},p=()=>{h+=1;const g=document.createElement("video");function f(){l||(g.removeEventListener("canplay",f),u=!0,i.current="loaded",n.current=g,g.currentTime=0,s(Math.random()))}function w(e){var t;const r=4===g.readyState,c=g.buffered&&g.buffered.length>0&&g.buffered.end(g.buffered.length-1)/g.duration*100>=100;if(r&&c){return}if(l){return}if(!u&&h<o){return null==d||d(),m(g),void(a=setTimeout(p,v(h)))}const f=e.message||(null===(t=g.error)||void 0===t?void 0:t.message)||"Unknown error";console.error(new Error("Video failed to load: "+f)),i.current="failed",n.current=void 0,s(Math.random())}c=g,g.addEventListener("canplay",f),g.addEventListener("error",w),d=()=>{g.removeEventListener("canplay",f),g.removeEventListener("error",w)},t&&(g.crossOrigin=t),r&&(g.referrerPolicy=r),g.src=e,g.load()};return p(),()=>{l=!0,clearTimeout(a),null==d||d(),u||m(c)}},[e,t,r]),[n.current,i.current]};export const setVideoLoaderHook=e=>{ee=e};function te(e){return!e.seeking&&e.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA}export const useLoader=(e,t,r)=>{const i=o.useRef(void 0),n=()=>{var e;null===(e=i.current)||void 0===e||e.call(i),i.current=void 0};o.useEffect(()=>n,[]),o.useLayoutEffect(()=>{const o=t.slice(0,200),a=`video with id ${r} url: ${o}`;"loading"!==e||i.current||(i.current=E(a)),"loading"!==e&&n(),"failed"===e&&y(M("VIDEO_FAILED",a,{src:o,elementId:r,reason:"load"}))},[e])};export const VideoElement=a(({element:a,store:c})=>{var m,g,f,v,x;const[y,M]=o.useState(!1),T=o.useRef(null),z=a.isSelected,[V,U]=o.useState(!1),[B,F]=ee(a.src,"anonymous"),[re,ie]=o.useState(!1),oe=c.activePage===a.page,ne=(null===(m=a.page)||void 0===m?void 0:m._exportingOrRendering)&&"loaded"===F,ae=O(a,c,ne);useLoader(F,a.src,a.id),B&&(B.playsInline=!0);const se=ne&&ae||B,[de,ce]=((e,t)=>{const{flipX:r,flipY:i}=e,{width:n,height:a}=t?Z(t):{},s=e=>{if(!t){return}const o=e.getContext("2d");o&&(o.clearRect(0,0,e.width,e.height),o.save(),o.scale(r?-1:1,i?-1:1),o.drawImage(t,r?-e.width:0,i?-e.height:0,e.width,e.height),o.restore())},d=o.useMemo(()=>{if(!r&&!i||!n||!a){return}const e=document.createElement("canvas");return e.width=n,e.height=a,s(e),e},[r,i,t,n,a]);return o.useEffect(()=>()=>{d&&w.Util.releaseCanvas(d)},[d]),[d||t,()=>{d&&s(d)}]})(a,se),[he=J,le]=P(a,de||J);o.useEffect(()=>{B&&c.history.ignore(()=>{a.set({duration:1e3*B.duration})})},[B]);const ue=o.useMemo(A,[B]);o.useEffect(()=>{var e;if(!B){return}const t=c.animatedElementsIds,r=(!t.length||t.includes(a.id))&&c.isPlaying;if(!oe||!r&&!V){return void B.pause()}V&&(B.muted=!1,B.currentTime=a.startTime*B.duration,W(B).then(e=>{"refused"===e&&U(!1)}));const i=new w.Animation(()=>{Se()},null===(e=T.current)||void 0===e?void 0:e.getLayer());i.start();const o=()=>{U(!1),B.currentTime=a.startTime*B.duration},n=()=>{B.currentTime>=a.endTime*B.duration&&(B.pause(),B.currentTime=a.startTime*B.duration,U(!1))};return B.addEventListener("ended",o),B.addEventListener("timeupdate",n),()=>{B.pause(),i.stop(),B.removeEventListener("ended",o),B.removeEventListener("timeupdate",n)}},[B,V,c.isPlaying,oe]),o.useEffect(()=>{B&&(B.volume=a.volume)},[B,a.volume]),o.useEffect(()=>{B&&(B.playbackRate=a.speed,B.preservesPitch=!1)},[B,a.speed]);const me=o.useRef(null);o.useEffect(()=>{const e=d(a,()=>{var e,t;if(B){if(ne&&ae){return Se(),void c.currentTime}const r=c.animatedElementsIds;if(r.length&&!r.includes(a.id)){return}const i=c.activePage===a.page,o=c.currentTime>=a.page.startTime&&c.currentTime<a.page.startTime+a.page.duration,n=a.startTime*B.duration*1e3,s=B.duration*(a.endTime-a.startTime)*1e3,{start:d,end:h}=_(a.animations,a.page.duration),l=(c.currentTime||a.page.startTime)-a.page.startTime,u=o?D(c.currentTime,a.page.startTime,d,s,n,a.speed):0,m=u/1e3,p=o&&l>=d&&l<h;V||(B.muted=!p,p&&c.isPlaying&&i?B.paused?(B.currentTime=m,ue.play(B)):Math.abs(1e3*B.currentTime-u)>500&&(B.currentTime=m):(ue.reset(),B.paused||B.pause(),m!==B.currentTime&&(B.currentTime=m),!te(B)&&!c.isPlaying&&!me.current&&(me.current=E(`video ${a.id}`)))),Se(),null===(t=null===(e=T.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()}});return()=>{e(),me.current&&(me.current(),me.current=null)}},[c,B,ne,ae,V]),o.useEffect(()=>{var e,t;if(!B){return}if(ne){return null===(e=me.current)||void 0===e||e.call(me),void(me.current=null)}const r=()=>{var e;te(B)&&(Se(),null===(e=me.current)||void 0===e||e.call(me),me.current=null)};let i;return(null===(t=a.page)||void 0===t?void 0:t._exportingOrRendering)&&(i=setInterval(r,20)),B.addEventListener("timeupdate",r),B.addEventListener("canplay",r),B.addEventListener("seeked",r),()=>{clearInterval(i),B.removeEventListener("timeupdate",r),B.removeEventListener("canplay",r),B.removeEventListener("seeked",r)}},[null===(g=a.page)||void 0===g?void 0:g._exportingOrRendering,B,a.id,E,ne]);let{cropX:pe,cropY:ge,cropWidth:fe,cropHeight:ve}=a;"loaded"!==F&&(pe=ge=0,fe=ve=1);const{width:we,height:xe}=Z(he),Ee=null!==(f=null==B?void 0:B.videoWidth)&&void 0!==f?f:0,ye=null!==(v=null==B?void 0:B.videoHeight)&&void 0!==v?v:0;o.useLayoutEffect(()=>{"loaded"===F&&Ee>0&&ye>0&&a._setSourceSize({width:Ee,height:ye},a.src)},[a,a.src,F,Ee,ye]);const Me=L({stretchEnabled:a.stretchEnabled,cropX:pe,cropY:ge,cropWidth:fe,cropHeight:ve,width:a.a.width,height:a.a.height},{width:we,height:xe}),Le=Me.width,Te=Me.height,Ce=a.stretchEnabled,be=null!==(x=a.cornerRadius)&&void 0!==x?x:0,[ke,Ie]=useCornerRadiusAndCrop(a,he,Me,c._elementsPixelRatio,be,!0);let[Re,je]=useClip(a,ke,c._elementsPixelRatio,[Me,be,c._elementsPixelRatio]);const Se=H(()=>{var e,t;ce(),le(),Ie(),je(),null===(t=null===(e=T.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()});o.useEffect(()=>{Se()});const Pe=()=>{a._cropEnabled&&setTimeout(()=>{s(a)&&a.toggleCropMode(!0)})},He="loading"===F,Xe="failed"===F,Ye=!He&&!Xe,Ae=o.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),We=o.useRef(!1),Oe=Ye?i(a):0;X(T,Oe);const _e=a.selectable||c.role===p.ADMIN,De=Y();return e(r,{children:t(h,{onMouseEnter:()=>ie(!0),onMouseLeave:()=>ie(!1),children:[He&&e(K,{element:a}),Xe&&e(Q,{element:a}),e(l,{ref:T,name:"element",id:a.id,image:Re,x:a.a.x,y:a.a.y,width:a.a.width||1,height:a.a.height||1,rotation:a.a.rotation,opacity:Oe,shadowEnabled:a.shadowEnabled,shadowBlur:a.shadowBlur,shadowOffsetX:a.shadowOffsetX,shadowOffsetY:a.shadowOffsetY,shadowColor:n(a.shadowColor),shadowOpacity:a.shadowOpacity,customCrop:Me,listening:_e,draggable:De?a.draggable&&z:a.draggable,preventDefault:!De||z,hideInExport:!a.showInExport,onDragMove:e=>{a.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{a.set(I(e.target))},onDblClick:Pe,onDblTap:Pe,onTransformStart:e=>{M(!0),We.current=R(C(e.target)),Ae.current={cropX:a.cropX,cropY:a.cropY,cropWidth:a.cropWidth,cropHeight:a.cropHeight}},onTransform:e=>{const t=e.currentTarget,r=Math.abs(t.scaleX()-1)<1e-7?1:t.scaleX(),i=Math.abs(t.scaleY()-1)<1e-7?1:t.scaleY();t.scaleX(1),t.scaleY(1);const o=1-Le/we,n=Math.min(o,Math.max(0,a.cropX)),s=1-Te/xe,d=Math.min(s,Math.max(0,a.cropY)),c=We.current,h=!c&&r<1&&Ae.current.cropHeight>Te/xe;let l=c?a.cropWidth:a.cropWidth*r;h&&(l=a.cropWidth);const u=!c&&i<1&&Ae.current.cropWidth>Le/we;let m=c?a.cropHeight:a.cropHeight*i;u&&(m=a.cropHeight),Ce&&(l=a.cropWidth,m=a.cropHeight);const p=c?Math.min(r,i):1;a.set(Object.assign({cropX:n,cropY:d,x:t.x(),y:t.y(),width:t.width()*r,height:t.height()*i,rotation:e.target.rotation(),cropWidth:Math.min(l,1-n),cropHeight:Math.min(m,1-d)},1!==p&&j(a,p)))},onTransformEnd:e=>{const t=e.currentTarget;a.set(Object.assign({width:k(t.width()),height:k(t.height()),x:k(t.x()),y:k(t.y()),rotation:k(e.target.rotation()),cropX:b(a.cropX),cropY:b(a.cropY),cropWidth:b(Le/we),cropHeight:b(Te/xe)},S(a))),M(!1)}}),e(u,{x:a.a.x,y:a.a.y,width:Math.max(a.a.width-a.borderSize,0),height:Math.max(a.a.height-a.borderSize,0),opacity:Oe,offsetX:-a.borderSize/2,offsetY:-a.borderSize/2,stroke:n(a.borderColor),strokeWidth:a.borderSize,listening:!1,visible:!!a.borderSize,rotation:a.rotation,cornerRadius:Math.max(0,be-a.borderSize),hideInExport:!a.showInExport}),!c.isPlaying&&e(l,{image:V?re||De?G:void 0:q,x:a.a.x,y:a.a.y,offsetX:-a.a.width/2+15/c.scale,offsetY:-a.a.height/2+15/c.scale,rotation:a.a.rotation,width:30/c.scale,height:30/c.scale,hideInExport:!0,onClick:()=>U(!V),onTap:()=>U(!V)}),a._cropModeEnabled&&"loaded"===F&&e(N,{element:a,store:c,previewImage:Re,sourceImage:he,sourceWidth:we,sourceHeight:xe,cropAbsoluteWidth:Le,cropAbsoluteHeight:Te,innerAnchors:$})]})})});
|
package/model/figure-model.d.ts
CHANGED
package/model/gif-model.d.ts
CHANGED
package/model/group-model.d.ts
CHANGED
|
@@ -195,6 +195,7 @@ export declare const TYPES_MAP: {
|
|
|
195
195
|
showInExport: boolean;
|
|
196
196
|
}>>;
|
|
197
197
|
readonly parent: any;
|
|
198
|
+
readonly isHighlighted: boolean;
|
|
198
199
|
readonly zIndex: any;
|
|
199
200
|
} & {
|
|
200
201
|
toJSON(): {
|
|
@@ -362,6 +363,10 @@ export declare const TYPES_MAP: {
|
|
|
362
363
|
legacyBackground: import("mobx-state-tree").IMaybe<import("mobx-state-tree").ISimpleType<boolean>>;
|
|
363
364
|
curveEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
364
365
|
curvePower: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
366
|
+
textOverflow: import("mobx-state-tree").IMaybe<import("mobx-state-tree").ISimpleType<string>>;
|
|
367
|
+
maxLines: import("mobx-state-tree").IMaybe<import("mobx-state-tree").ISimpleType<number>>;
|
|
368
|
+
minFontSize: import("mobx-state-tree").IMaybe<import("mobx-state-tree").ISimpleType<number>>;
|
|
369
|
+
maxFontSize: import("mobx-state-tree").IMaybe<import("mobx-state-tree").ISimpleType<number>>;
|
|
365
370
|
}, {
|
|
366
371
|
readonly page: any;
|
|
367
372
|
readonly store: any;
|
|
@@ -426,6 +431,7 @@ export declare const TYPES_MAP: {
|
|
|
426
431
|
showInExport: boolean;
|
|
427
432
|
}>>;
|
|
428
433
|
readonly parent: any;
|
|
434
|
+
readonly isHighlighted: boolean;
|
|
429
435
|
readonly zIndex: any;
|
|
430
436
|
} & {
|
|
431
437
|
toJSON(): {
|
|
@@ -636,6 +642,7 @@ export declare const TYPES_MAP: {
|
|
|
636
642
|
showInExport: boolean;
|
|
637
643
|
}>>;
|
|
638
644
|
readonly parent: any;
|
|
645
|
+
readonly isHighlighted: boolean;
|
|
639
646
|
readonly zIndex: any;
|
|
640
647
|
} & {
|
|
641
648
|
toJSON(): {
|
|
@@ -863,6 +870,7 @@ export declare const TYPES_MAP: {
|
|
|
863
870
|
showInExport: boolean;
|
|
864
871
|
}>>;
|
|
865
872
|
readonly parent: any;
|
|
873
|
+
readonly isHighlighted: boolean;
|
|
866
874
|
readonly zIndex: any;
|
|
867
875
|
} & {
|
|
868
876
|
toJSON(): {
|
|
@@ -1088,6 +1096,7 @@ export declare const TYPES_MAP: {
|
|
|
1088
1096
|
showInExport: boolean;
|
|
1089
1097
|
}>>;
|
|
1090
1098
|
readonly parent: any;
|
|
1099
|
+
readonly isHighlighted: boolean;
|
|
1091
1100
|
readonly zIndex: any;
|
|
1092
1101
|
} & {
|
|
1093
1102
|
toJSON(): {
|
|
@@ -1314,6 +1323,7 @@ export declare const TYPES_MAP: {
|
|
|
1314
1323
|
showInExport: boolean;
|
|
1315
1324
|
}>>;
|
|
1316
1325
|
readonly parent: any;
|
|
1326
|
+
readonly isHighlighted: boolean;
|
|
1317
1327
|
readonly zIndex: any;
|
|
1318
1328
|
} & {
|
|
1319
1329
|
toJSON(): {
|
|
@@ -1536,6 +1546,7 @@ export declare const TYPES_MAP: {
|
|
|
1536
1546
|
showInExport: boolean;
|
|
1537
1547
|
}>>;
|
|
1538
1548
|
readonly parent: any;
|
|
1549
|
+
readonly isHighlighted: boolean;
|
|
1539
1550
|
readonly zIndex: any;
|
|
1540
1551
|
} & {
|
|
1541
1552
|
toJSON(): {
|
|
@@ -1911,6 +1922,7 @@ export declare const TYPES_MAP: {
|
|
|
1911
1922
|
showInExport: boolean;
|
|
1912
1923
|
}>>;
|
|
1913
1924
|
readonly parent: any;
|
|
1925
|
+
readonly isHighlighted: boolean;
|
|
1914
1926
|
readonly zIndex: any;
|
|
1915
1927
|
} & {
|
|
1916
1928
|
toJSON(): {
|
package/model/image-model.d.ts
CHANGED
package/model/line-model.d.ts
CHANGED
package/model/node-model.d.ts
CHANGED
package/model/node-model.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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{types as t,getParentOfType as o,getSnapshot as n,hasParentOfType as r,isAlive as
|
|
1
|
+
var e=this&&this.__rest||function(e,t){var o={};for(var n in e){Object.prototype.hasOwnProperty.call(e,n)&&t.indexOf(n)<0&&(o[n]=e[n])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(e);r<n.length;r++){t.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(e,n[r])&&(o[n[r]]=e[n[r]])}}return o};import{types as t,getParentOfType as o,getSnapshot as n,hasParentOfType as r,isAlive as i}from"mobx-state-tree";import{nanoid as l}from"nanoid";import{BLEND_MODES as s}from"@polotno/schema/defaults";import{Store as d}from"./store.js";import{Page as a}from"./page-model.js";import{GroupElement as p,forEveryNode as m}from"./group-model.js";export const Node=t.model("Node",{id:t.identifier,type:"none",name:"",opacity:1,blendMode:t.optional(t.enumeration([...s]),"normal"),custom:t.frozen(),visible:!0,selectable:!0,removable:!0,alwaysOnTop:!1,showInExport:!0}).preProcessSnapshot(e=>{for(var t in e){null===e[t]&&(e[t]=void 0)}return e}).postProcessSnapshot(t=>{const{blendMode:o}=t,n=e(t,["blendMode"]);for(const e in n){"_"===e[0]&&delete n[e]}return o===("group"===n.type?"pass-through":"normal")?n:Object.assign(Object.assign({},n),{blendMode:o})}).views(e=>({get page(){return i(e)?o(e,a):null},get store(){return i(e)?o(e,d):null},get top(){if(!i(e)){return e}let t=e;for(;;){if(!r(t,p)){return t}t=o(t,p)}},get parent(){if(!i(e)){return null}if(r(e,p)){const t=o(e,p);return t&&t.children&&t.children.length,t}if(r(e,a)){const t=o(e,a);return t&&t.children&&t.children.length,t}if(r(e,d)){const t=o(e,d);return t&&t.pages&&t.pages.length,t}return null},get isHighlighted(){var t;return!!i(e)&&(null===(t=e.store)||void 0===t?void 0:t.highlightedElementId)===e.id},get zIndex(){return i(e)?e.parent.children.indexOf(e):-1}})).actions(e=>({toJSON:()=>Object.assign({},n(e))})).actions(e=>({clone(t={},{skipSelect:o=!1}={}){const n=JSON.parse(JSON.stringify(e.toJSON()));t.id=t.id||l(10);const r=new Map;return m(n,e=>{const t=e.id,o=l(10);r.set(t,o),e.id=o}),m(n,e=>{e.mergedInto&&r.has(e.mergedInto)&&(e.mergedInto=r.get(e.mergedInto))}),Object.assign(n,t),e.page.addElement(n,{skipSelect:o})},set(t){Object.assign(e,t)},moveUp(){var t;null===(t=e.parent)||void 0===t||t.moveElementsUp([e.id])},moveTop(){var t;null===(t=e.parent)||void 0===t||t.moveElementsTop([e.id])},moveDown(){var t;null===(t=e.parent)||void 0===t||t.moveElementsDown([e.id])},moveBottom(){var t;null===(t=e.parent)||void 0===t||t.moveElementsBottom([e.id])},setZIndex(t){e.parent.setElementZIndex(e.id,t)},beforeDestroy(){}}));
|