polotno 3.20.1 → 3.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/THIRD_PARTY_NOTICES.md +6 -6
- package/blueprint.polotno.css +75 -50
- package/canvas/gif-element.d.ts +28 -4
- package/canvas/gif-element.js +1 -1
- package/canvas/image-element.js +1 -1
- package/canvas/lottie-element.js +1 -1
- package/canvas/lottie-player.wasm.js +1 -1
- package/canvas/media-resize.d.ts +19 -0
- package/canvas/media-resize.js +1 -0
- package/canvas/page.js +1 -1
- package/canvas/quill-font-formats.d.ts +1 -1
- package/canvas/quill-font-formats.js +1 -1
- package/canvas/rich-editor.js +4 -4
- package/canvas/use-color.js +1 -1
- package/canvas/video-element.js +1 -1
- package/config.d.ts +1 -1
- package/config.js +1 -1
- package/model/group-model.d.ts +2 -1
- package/model/page-model.d.ts +6 -3
- package/model/shape-model.js +1 -1
- package/model/store.js +1 -1
- package/model/text-model.d.ts +2 -1
- package/model/text-model.js +1 -1
- package/package.json +21 -23
- package/polotno.bundle.js +208 -205
- package/side-panel/videos-grid.d.ts +1 -1
- package/side-panel/videos-grid.js +1 -1
- package/toolbar/color-picker.d.ts +8 -0
- package/toolbar/color-picker.js +1 -1
- package/toolbar/download-button.js +1 -1
- package/toolbar/video-toolbar.js +1 -1
- package/utils/clipboard.d.ts +4 -2
- package/utils/selection-transform.js +1 -1
- package/utils/use-api.d.ts +1 -1
- package/utils/use-api.js +1 -1
- package/utils/validate-key.js +1 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { KonvaNodeEvents } from 'react-konva';
|
|
2
|
+
import type { ImageElementType } from '../model/image-model.js';
|
|
3
|
+
type Size = {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
};
|
|
7
|
+
type Crop = {
|
|
8
|
+
cropX: number;
|
|
9
|
+
cropY: number;
|
|
10
|
+
cropWidth: number;
|
|
11
|
+
cropHeight: number;
|
|
12
|
+
};
|
|
13
|
+
export declare const sideResizeCrop: (element: Crop & {
|
|
14
|
+
stretchEnabled: boolean;
|
|
15
|
+
width: number;
|
|
16
|
+
height: number;
|
|
17
|
+
}, source: Size, scaleX: number, scaleY: number) => Crop;
|
|
18
|
+
export declare const useMediaResize: (element: ImageElementType, crop: Size, source: Size | undefined, onTransformingChange: (transforming: boolean) => void) => Pick<KonvaNodeEvents, 'onTransformStart' | 'onTransform' | 'onTransformEnd'>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import t from"react";import{getCropRect as r}from"@polotno/core/image";import{activeAnchorOf as o,isCornerAnchor as e,roundedCropFraction as i,roundedGeometryPx as h,roundedStyleAttrs as c,scaledStyleAttrs as n,transformScale as s}from"../utils/px.js";const p=(t,r,o,e,i)=>{const h=Math.min(1-i.width,Math.max(0,t.cropX)),c=Math.min(1-i.height,Math.max(0,t.cropY)),n=!r.corner&&!t.stretchEnabled,s=!n||o<1&&r.cropHeight>i.height?t.cropWidth:t.cropWidth*o,p=!n||e<1&&r.cropWidth>i.width?t.cropHeight:t.cropHeight*e;return{cropX:h,cropY:c,cropWidth:Math.min(s,1-h),cropHeight:Math.min(p,1-c)}},a=(t,r)=>({width:t.width/r.width,height:t.height/r.height}),g=(t,r,o)=>({cropX:i(t),cropY:i(r),cropWidth:i(o.width),cropHeight:i(o.height)});export const sideResizeCrop=(t,o,e,i)=>{const{stretchEnabled:h,width:c,height:n,cropWidth:s,cropHeight:d}=t,m=a(r(t,o),o),w=p(t,{corner:!1,cropWidth:s,cropHeight:d},e,i,m),u=Object.assign(Object.assign({},w),{stretchEnabled:h,width:c*e,height:n*i});return g(w.cropX,w.cropY,a(r(u,o),o))};export const useMediaResize=(r,i,d,m)=>{const w=t.useRef({corner:!1,cropWidth:0,cropHeight:0}),u=d&&d.width&&d.height?a(i,d):void 0;return{onTransformStart:t=>{m(!0),w.current={corner:e(o(t.target)),cropWidth:r.cropWidth,cropHeight:r.cropHeight}},onTransform:t=>{const o=t.currentTarget,e=s(o.scaleX()),i=s(o.scaleY());o.scaleX(1),o.scaleY(1);const h=w.current.corner?Math.min(e,i):1;r.set(Object.assign(Object.assign({x:o.x(),y:o.y(),width:o.width()*e,height:o.height()*i,rotation:o.rotation()},u&&p(r,w.current,e,i,u)),1!==h&&n(r,h)))},onTransformEnd:t=>{const o=t.currentTarget;r.set(Object.assign(Object.assign({width:h(o.width()),height:h(o.height()),x:h(o.x()),y:h(o.y()),rotation:h(o.rotation())},u&&g(r.cropX,r.cropY,u)),c(r))),m(!1)}}};
|
package/canvas/page.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e=this&&this.__rest||function(e,t){var n={};for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++){t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(n[i[o]]=e[i[o]])}}return n};import{Fragment as t,jsx as n,jsxs as i}from"react/jsx-runtime";import o from"react";import{clamp as r}from"../lib/utils.js";import{observer as l,useLocalObservable as s}from"mobx-react-lite";import{action as a,runInAction as c}from"mobx";import{isAlive as d}from"mobx-state-tree";import{ZERO_BLEED as m}from"@polotno/schema";import{Group as u,Image as h,Label as g,Layer as f,Line as p,Rect as x,Shape as v,Stage as b,KonvaRenderer as y,Tag as w,Text as E,Transformer as S}from"react-konva";import{useWorkspaceStyle as X}from"./workspace-style.js";import{ROLES as Y}from"../model/store.js";import k from"konva";import A from"./element.js";import{DrawingLayer as M}from"./drawing-layer.js";import{ensureDragOrder as j,useSnap as I}from"./use-transformer-snap.js";import{useImageLoader as P,useSizedImage as O}from"./image-element.js";import{FadeOverlay as R}from"./fade-overlay.js";import{getCrop as T}from"../utils/crop.js";import{cloneBehind as D}from"../utils/duplicate.js";import{___ as B,____ as C,isCreditVisible as W}from"../utils/validate-key.js";import{getClientRect as G,getCorners as L,getTotalClientRect as V,polygonsIntersect as F}from"../utils/math.js";import{pxToUnitRounded as z,pxToUnitString as _}from"../utils/unit.js";import{isCornerAnchor as U,isRotateAnchor as Z,MAIN_TRANSFORMER_NAME as H}from"../utils/px.js";import{canStretchAxis as J,isAxisAligned as N,leafMinSize as K}from"../utils/selection-transform.js";import{restoreHeldPoint as $}from"./transform-box.js";import{flags as Q}from"../utils/flags.js";import{textOverflowOf as q}from"../utils/text-sizing.js";import{useColor as ee}from"./use-color.js";import{isGradient as te}from"@polotno/core/gradient";import{toDisplayColor as ne}from"@polotno/core/color";import{Html as ie}from"react-konva-utils";import{getParentGroupId as oe,withoutDescendants as re}from"../utils/selection.js";import{nodeReaction as le}from"../utils/mobx.js";import{useEvent as se}from"../hooks/use-event.js";const ae=o.version.startsWith("18.")?o.forwardRef((e,t)=>(o.useLayoutEffect(()=>{y.flushSync()}),n(b,Object.assign({},e,{ref:t})))):b,ce={enabledAnchors:["top-left","top-center","top-right","middle-left","bottom-left","bottom-right","bottom-center","middle-right"],rotateEnabled:!0,rotationSnaps:[0,45,90,135,180,225,270,315],ignoreStroke:!0,flipEnabled:!1,keepRatio:!0},de=e=>{if(!e.length){return null}const t=N(e[0].rotation)?0:e[0].rotation;return e.every(e=>N(e.rotation-t))?t:null},me=e=>e.selectedShapes.filter(t=>{var n;return(null===(n=e.activeTool)||void 0===n?void 0:n.elementId)!==t.id&&!t.pendingEdit}),ue=e=>e.selectedShapes.some(e=>!e.resizable),he=e=>e.selectedShapes.some(e=>!e.draggable),ge=e=>{var t;return 1!==e.selectedElements.length||"group"===(null===(t=e.selectedElements[0])||void 0===t?void 0:t.type)},fe=e=>{var t;const n=me(e),i=ge(e),o=null!==(t=de(n))&&void 0!==t?t:0,r=e=>i&&n.length>0&&n.every(t=>J(t,e,o));return{boxRotation:o,canStretchX:r("x"),canStretchY:r("y")}},pe={text:{enabledAnchors:["top-left","top-right","middle-left","bottom-left","bottom-right","middle-right"]},svg:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},gif:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},lottie:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},line:{enabledAnchors:[],borderEnabled:!1,rotateEnabled:!1},image:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},many:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},group:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},table:{keepRatio:!1,enabledAnchors:["top-left","top-center","top-right","middle-left","middle-right","bottom-left","bottom-center","bottom-right"]}};export function registerTransformerAttrs(e,t){pe[e]=pe[e]||t,Object.assign(pe[e],t)}const xe=e=>n(x,Object.assign({},e,{preventDefault:!1})),ve=t=>{var{url:i}=t,o=e(t,["url"]);const[r,l]=O(i),s=r?T(r,{width:o.width,height:o.height},"center-middle"):{};return P(l,i,"page background"),n(h,Object.assign({image:r},o,s))},be=e=>{const t=ee({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return n(x,Object.assign({},e,t))},ye=t=>{var{background:i,transparencyBackgroundColor:r}=t,l=e(t,["background","transparencyBackgroundColor"]);const s=o.useMemo(()=>{if(te(i)){return!0}const e=k.Util.colorToRGBA(i);return!e||e.a<1},[i]),a=o.useMemo(()=>{if(!s||r){return}const e=document.createElement("canvas"),t=30;e.width=60,e.height=60;const n=e.getContext("2d");return n&&(n.fillStyle="black",n.fillRect(t,0,t,t),n.fillRect(0,t,t,t)),e},[s,r]);return s?n(x,r?Object.assign({fill:r},l,{hideInExport:!0}):Object.assign({fillPatternImage:a},l,{opacity:.1,hideInExport:!0})):null},we=t=>{var{background:i}=t,r=e(t,["background"]);return o.useMemo(()=>!!k.Util.colorToRGBA(i)||te(i),[i])?n(be,Object.assign({fill:i},r)):n(ve,Object.assign({url:i},r))},Ee=l(({selection:e})=>{const{selectionRectStyle:t}=X();return e.visible?n(x,{name:"selection",x:Math.min(e.x1,e.x2),y:Math.min(e.y1,e.y2),width:Math.abs(e.x1-e.x2),height:Math.abs(e.y1-e.y2),fill:t.fill,stroke:t.stroke,strokeWidth:t.strokeWidth}):null});function Se(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const Xe=l(({gesture:e,store:o})=>{const{transformLabelStyle:r}=X(),{x:l,y:s,width:a,height:c,rotation:d,anchor:m}=e.label;if(void 0===l||void 0===s||void 0===a||void 0===c||void 0===d||void 0===m){return null}const u=G({x:l,y:s,width:a,height:c,rotation:k.Util.radToDeg(d)}),h=null!==m&&Z(m),f=(c/2+70)*Math.cos(d-Math.PI/2),p=(c/2+70)*Math.sin(d-Math.PI/2),x=z({unit:o.unit,dpi:o.dpi,px:a/o.scale,precious:"px"===o.unit?0:1})+" x "+z({unit:o.unit,dpi:o.dpi,px:c/o.scale,precious:"px"===o.unit?0:1})+("px"===o.unit?"":" "+o.unit);return i(t,{children:[i(g,{x:(u.minX+u.maxX)/2+f,y:(u.minY+u.maxY)/2+p,offsetX:14,offsetY:14,visible:h,children:[n(w,{cornerRadius:5,fill:r.fill}),n(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:Se(d).toString()+"°"})]}),i(g,{x:(u.minX+u.maxX)/2,y:u.maxY+20,visible:!h,children:[n(w,{cornerRadius:5,fill:r.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),n(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:x})]})]})}),Ye=l(({store:e,gesture:t,transformerStyle:n,onChange:i})=>{var r;const l=e.selectedElements.some(e=>e.curveEnabled),s=e.selectedElements.some(e=>!e.visible),a=JSON.stringify(me(e).map(e=>e.id)),c=!ge(e)&&e.selectedElements[0].keepRatio,d=!ge(e)&&"text"===(null===(r=e.selectedElements[0])||void 0===r?void 0:r.type)&&"fit"===q(e.selectedElements[0]),m=t.transforming?null:fe(e);return o.useLayoutEffect(()=>{i()},[e.selectedShapes,a,ue(e),s,he(e),l,c,d,t.transforming,null==m?void 0:m.canStretchX,null==m?void 0:m.canStretchY,null==m?void 0:m.boxRotation,n]),null}),ke=()=>{console.warn("Polotno warning: onClick callback is deprecated. Just stop using it. Polotno will do selection automatically.")},Ae=l(({page:e})=>{const t=e._exportingOrRendering||e._forceMount;return o.useEffect(()=>{if(t&&d(e)){return e.set({_exportRendered:!0}),()=>{d(e)&&e.set({_exportRendered:!1})}}},[e,t]),null}),Me=l(({elements:e,store:t})=>{const i=e.filter(e=>e.alwaysOnTop),r=e.filter(e=>!e.alwaysOnTop).concat(i);return n(o.Fragment,{children:r.map(e=>n(A,{store:t,element:e,onClick:ke},e.id))})});export const useContextMenu=({store:e})=>{const[t,n]=o.useState(!1),[i,r]=o.useState({x:0,y:0}),l=o.useCallback(e=>{n(!0),r(e)},[]);return{open:l,close:o.useCallback(()=>{n(!1)},[]),props:{isOpen:t,offset:i,setIsOpen:n}}};let je=null;export const registerNextDomDrop=e=>{je=e};const Ie=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),Pe=atob("I2MwMzkyYg=="),Oe=atob("djAuOS4y"),Re=e=>n(t,{children:i(g,{fill:Pe,height:200,children:[n(w,{fill:Pe}),n(E,Object.assign({},e,{fill:"white",text:Ie,height:void 0,padding:10,fontSize:20}))]})}),Te=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),De=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),Be=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),Ce=e=>{const t=1===C(),o=t?De:Te,r=t?230:195;return i(u,Object.assign({},e,{listening:!1,children:[n(x,{fill:Pe,width:r,height:24,cornerRadius:2}),n(E,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),t&&n(E,{text:Be,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default l(({store:t,page:l,width:d,height:h,pageControlsEnabled:b,components:y,altCloneEnabled:A,groupSelectionMode:P="drill",viewportSize:O,layout:T="vertical",tooltipSafeArea:z,transformerSafeArea:Z})=>{const J=X(),ee=J.transformerStyle,te=ce,ve=l.computedBleed,be=t.bleedVisible?ve:m,Se=l.computedWidth+be.left+be.right,ke=l.computedHeight+be.top+be.bottom,Ie=(d-Se*t.scale)/2,Pe=(h-ke*t.scale)/2,Te={x:-ve.left,y:-ve.top,width:l.computedWidth+ve.left+ve.right,height:l.computedHeight+ve.top+ve.bottom,background:ne(l.background),name:"page-background",preventDefault:!1},De={x:-be.left,y:-be.top,width:Se,height:ke},Be=o.useRef(null),We=o.useRef(null),Ge=o.useRef(null),Le=o.useRef(null),Ve=null==b||b,Fe=o.useRef(!1),[ze,_e]=o.useState(null),Ue=s(()=>({transforming:!1,label:{},setLabel(e){this.label=e},setTransforming(e){this.transforming=e}})),[Ze,He]=o.useState(0),Je=useContextMenu({store:t}),Ne=me(t),Ke=ge(t),$e=()=>{var n,i,o;const r=Be.current;if(!r){return[]}const l=r.getStage();if(!l){return[]}const s=me(t),a=new Set(s.map(e=>e.id)),c=new Map;a.size&&l.find(e=>{const t=e.id();return a.has(t)&&!c.has(t)&&c.set(t,e),!1});const d=s.map(e=>c.get(e.id)).filter(e=>!!e),m=1===t.selectedElements.length,u=m&&(null===(n=t.selectedElements[0])||void 0===n?void 0:n.type)||"many",h=pe[u],g=Object.assign(Object.assign({},te),h||{});h&&("svg"!==u&&"image"!==u&&"gif"!==u&&"lottie"!==u||t.selectedElements[0].keepRatio||(g.enabledAnchors=te.enabledAnchors),"text"!==u||!Q.textVerticalResizeEnabled&&"fit"!==q(t.selectedElements[0])||(g.enabledAnchors=null===(i=pe.text.enabledAnchors)||void 0===i?void 0:i.concat(["bottom-center","top-center"])),"text"===u&&m&&t.selectedElements[0].curveEnabled&&(g.enabledAnchors=pe.many.enabledAnchors));const f=fe(t);(f.canStretchX||f.canStretchY)&&(g.enabledAnchors=[...g.enabledAnchors||[],...f.canStretchX?["middle-left","middle-right"]:[],...f.canStretchY?["top-center","bottom-center"]:[]]);const{boundBoxFunc:p}=ee,x=e(ee,["boundBoxFunc"]);r.setAttrs(Object.assign(Object.assign(Object.assign({},g),x),{name:H})),ue(t)&&r.enabledAnchors([]),he(t)&&r.rotateEnabled(!1);const v=r.nodes();if(v.length===d.length&&d.every((e,t)=>v[t]===e)||r.nodes(d),ge(t)&&d.length){const e=de(s);null===e||N(r.rotation()-e)||(r.rotation(e),r.forceUpdate())}return null===(o=r.getLayer())||void 0===o||o.batchDraw(),d},Qe=se(()=>{var e;if(null===(e=Be.current)||void 0===e?void 0:e.isTransforming()){return}const t=$e();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),j())}),qe=()=>{var e,t;const n=Be.current;if(!n){return}if(!n.isTransforming()){return void Ue.setLabel({})}if(!n.nodes().length){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();Ue.setLabel({anchor:o,x:i.x-(null!==(e=null==r?void 0:r.x())&&void 0!==e?e:0),y:i.y-(null!==(t=null==r?void 0:r.y())&&void 0!==t?t:0),rotation:i.rotation,width:i.width,height:i.height})},et=o.useCallback(()=>{var e;if(null===(e=Be.current)||void 0===e?void 0:e.isTransforming()){return}const t=Be.current;if(!t||!t.nodes().length){return void He(0)}const n=t.getClientRect();if(!t.getStage()){return}const i=function(e,t,n,i=0){const o=e.x+e.width/2,r=e.y+e.height/2,l=e.width/2,s=e.height/2,a=i*Math.PI/180,c=e=>{const t=e*Math.PI/180,i=(e%360+360)%360;let c;c=0===i||180===i?s:l;const d=Math.sin(t)*(c+n),m=-Math.cos(t)*(c+n),u=d*Math.cos(a)-m*Math.sin(a),h=d*Math.sin(a)+m*Math.cos(a);return{x:o+u,y:r+h}},d=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(d(c(0))){return 0}for(const m of[180,270,90]){if(d(c(m))){return m}}return 0}(n,{top:(null==Z?void 0:Z.top)||0,bottom:h-((null==Z?void 0:Z.bottom)||0),left:(null==Z?void 0:Z.left)||0,right:d-((null==Z?void 0:Z.right)||0)},ee.rotateAnchorOffset,t.rotation());He(i)},[Z,d,h]);o.useEffect(()=>{var e;null===(e=Be.current)||void 0===e||e.update(),qe(),et()},[t.scale,et]),o.useEffect(()=>{et()},[t.selectedShapes,et]);const tt=s(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),nt=o.useRef(!1),it=e=>{const n=Ge.current;if(!n||t.role===Y.VIEWER||t.activePage!==l||t.isPlaying||"button"in e.evt&&0!==e.evt.button||1!==n.getPointersPositions().length||!e.target.findAncestor(".elements-container")||!e.target.findAncestor(".element",!0)||!t.selectedShapes.length||he(t)){return}const i=n.getPointerPosition();if(!i){return}const o=$e();if(o.some(t=>t===e.target||t.isAncestorOf(e.target))){return}const r=o.find(e=>!!(e.draggable()&&e.isVisible()&&e.isListening())&&(e instanceof k.Shape?[e]:e instanceof k.Container?e.find("Shape"):[]).some(e=>e.intersects(i)));if(r){for(const[t,n]of k.DD._dragElements){"ready"!==n.dragStatus||n.node!==e.target&&!n.node.isAncestorOf(e.target)||k.DD._dragElements.delete(t)}r._createDragElement(e),"touches"in e.evt&&e.evt.cancelable&&r.preventDefault()&&e.evt.preventDefault()}},ot=a(e=>{var n,i,o,r,l,s,a;if(t.role===Y.VIEWER){return}if(nt.current=!1,t.activeTool&&"crop"!==t.activeTool.name){return}const c=e.target.findAncestor(".elements-container"),d=e.target.findAncestor("Transformer"),m=e.target.findAncestor(".page-abs-container");if(c||d||m){return}const u=null===(n=e.target.getStage())||void 0===n?void 0:n.getPointerPosition();u&&(u.x-=null!==(l=null===(i=e.target.getStage())||void 0===i?void 0:i.x())&&void 0!==l?l:0,u.y-=null!==(s=null===(o=e.target.getStage())||void 0===o?void 0:o.y())&&void 0!==s?s:0,tt.visible=!0,tt.x1=u.x,tt.y1=u.y,tt.x2=u.x,tt.y2=u.y,(null!==(a=null===(r=e.target.getStage())||void 0===r?void 0:r.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(tt.visible=!1))});(({stageRef:e,containerRef:t,viewportSize:n})=>{o.useEffect(()=>{var n;const i=null===(n=t.current)||void 0===n?void 0:n.closest(".polotno-workspace-inner");function o(){var n;if(!e.current){return}const o=null===(n=t.current)||void 0===n?void 0:n.getBoundingClientRect(),l=null==i?void 0:i.getBoundingClientRect();if(!o||!l){return}const s=r(l.left-o.left-100,0,Math.max(0,o.width-e.current.width())),a=r(l.top-o.top-100,0,Math.max(0,o.height-e.current.height()));e.current.position({x:-s,y:-a}),e.current.container().style.transform=`translate(${s}px, ${a}px)`}return o(),null==i||i.addEventListener("scroll",o),()=>{null==i||i.removeEventListener("scroll",o)}},[n.width,n.height])})({stageRef:Ge,containerRef:Le,viewportSize:O}),o.useEffect(()=>{const e=a(e=>{var t,n,i,o,r,l;if(!tt.visible){return}null===(t=Ge.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=Ge.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(r=null===(i=Ge.current)||void 0===i?void 0:i.x())&&void 0!==r?r:0,s.y-=null!==(l=null===(o=Ge.current)||void 0===o?void 0:o.y())&&void 0!==l?l:0):s={x:tt.x2,y:tt.y2},tt.x2=s.x,tt.y2=s.y}),n=a(()=>{if(!tt.visible){return}if(!Ge.current){return}const e=Ge.current.findOne(".selection"),n=e?e.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(n.width&&n.height){const e=[{x:n.x,y:n.y},{x:n.x+n.width,y:n.y},{x:n.x+n.width,y:n.y+n.height},{x:n.x,y:n.y+n.height}],i=[];Ge.current.find(".element").forEach(n=>{var o;const r=t.getElementById(n.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=n.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const c=L(r.a).map(e=>a.point(e));F(e,c)&&i.push(r.top.id)});const o=[...new Set(i)];nt.current=!0,t.selectElements(o)}tt.visible=!1});return window.addEventListener("mousemove",e),window.addEventListener("touchmove",e),window.addEventListener("mouseup",n,{capture:!0}),window.addEventListener("touchend",n,{capture:!0}),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("touchmove",e),window.removeEventListener("mouseup",n),window.removeEventListener("touchend",n)}},[]);const rt=o.useCallback(e=>{if(!t.selectedElements[0]){return}if(e&&t.selectedElementsIds.includes(e)){return}const n=V(t.selectedShapes),i=e?t.getElementById(e):{x:0,y:0,width:l.computedWidth,height:l.computedHeight,rotation:0},o=G(i),r=[];n.minX>o.maxX&&r.push({distance:n.minX-o.maxX,box1:n,box2:o,points:[{x:n.minX,y:n.minY+n.height/2},{x:o.maxX,y:n.minY+n.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),n.maxX<o.minX&&r.push({distance:o.minX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:n.minY+n.height/2},{x:o.minX,y:n.minY+n.height/2},{x:o.minX,y:o.minY+o.height/2}]}),n.minY>o.maxY&&r.push({box1:n,box2:o,distance:n.minY-o.maxY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]}),n.maxY<o.minY&&r.push({box1:n,box2:o,distance:o.minY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]});const s=n.minX>=o.minX&&n.maxX<=o.maxX&&n.minY>=o.minY&&n.maxY<=o.maxY;if(s&&(r.push({distance:n.minX-o.minX,box1:n,box2:o,points:[{x:n.minX,y:n.minY+n.height/2},{x:o.minX,y:n.minY+n.height/2},{x:o.minX,y:o.minY+o.height/2}]}),r.push({distance:o.maxX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:n.minY+n.height/2},{x:o.maxX,y:n.minY+n.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),r.push({box1:n,box2:o,distance:n.minY-o.minY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]}),r.push({box1:n,box2:o,distance:o.maxY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]})),0===r.length&&!s){const e=n.minX<o.maxX&&n.maxX>o.minX,t=n.minY<o.maxY&&n.maxY>o.minY;if(e&&t){const e=(Math.max(n.minY,o.minY)+Math.min(n.maxY,o.maxY))/2,t=(Math.max(n.minX,o.minX)+Math.min(n.maxX,o.maxX))/2;n.minX<o.minX?r.push({distance:o.minX-n.minX,box1:n,box2:o,points:[{x:n.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}):r.push({distance:n.minX-o.minX,box1:n,box2:o,points:[{x:n.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}),n.maxX>o.maxX?r.push({distance:n.maxX-o.maxX,box1:n,box2:o,points:[{x:o.maxX,y:e},{x:n.maxX,y:e},{x:o.maxX,y:e}]}):r.push({distance:o.maxX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:e},{x:o.maxX,y:e},{x:o.maxX,y:e}]}),n.minY<o.minY?r.push({box1:n,box2:o,distance:o.minY-n.minY,points:[{x:t,y:n.minY},{x:t,y:o.minY},{x:t,y:o.minY}]}):r.push({box1:n,box2:o,distance:n.minY-o.minY,points:[{x:t,y:n.minY},{x:t,y:o.minY},{x:t,y:o.minY}]}),n.maxY>o.maxY?r.push({box1:n,box2:o,distance:n.maxY-o.maxY,points:[{x:t,y:o.maxY},{x:t,y:n.maxY},{x:t,y:o.maxY}]}):r.push({box1:n,box2:o,distance:o.maxY-n.maxY,points:[{x:t,y:n.maxY},{x:t,y:o.maxY},{x:t,y:o.maxY}]})}}_e(e=>JSON.stringify(e)===JSON.stringify(r)?e:r)},[t,l]),lt=o.useCallback(()=>{t.distanceGuidesVisible&&t.selectedElements[0]?rt():_e(null)},[t,rt]);o.useEffect(lt,[t.distanceGuidesVisible,lt]);const st=t.distanceGuidesVisible||!!ze;o.useEffect(()=>{if(st){return le(l,()=>t.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|"),lt)}},[st,lt]);const at=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=Le.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{at.current=!0,clearTimeout(t),t=setTimeout(()=>{at.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const e=Ge.current;if(!e){return}const n=e.container(),i=new Map,o=e=>{var t,n;i.has(e.target)||[e.target,...null!==(n=null===(t=Be.current)||void 0===t?void 0:t.nodes())&&void 0!==n?n:[]].forEach(e=>{i.has(e)||i.set(e,{x:e.x(),y:e.y()})})},r=e=>{e.target===Be.current?i.clear():i.delete(e.target)};e.on("dragstart",o),e.on("dragend",r);const l=e=>{e.touches.length>=2&&c(()=>{var e,n;const o=new Map(i);(null===(e=Be.current)||void 0===e?void 0:e.isDragging())&&Be.current.stopDrag(),null===(n=Be.current)||void 0===n||n.nodes().forEach(e=>{var n;const i=o.get(e);e.isDragging()&&e.stopDrag(),i&&(e.setAttrs(i),null===(n=t.getElementById(e.id()))||void 0===n||n.set({x:i.x,y:i.y}))})})};return n.addEventListener("touchstart",l,{passive:!0}),n.addEventListener("touchmove",l,{passive:!0}),()=>{e.off("dragstart",o),e.off("dragend",r),n.removeEventListener("touchstart",l),n.removeEventListener("touchmove",l)}},[t]);const ct=e=>{if(t.role===Y.VIEWER){return}if(t.activePage!==l&&l.select(),at.current){return}if(nt.current){return}const n=e.evt.shiftKey,i=e.evt.ctrlKey||e.evt.metaKey,o=n||i,r=e.target.findAncestor(".elements-container"),s=e.target.findAncestor(".page-abs-container"),a=e.target.findAncestor("Transformer"),c=e.target.findAncestor(".page-container",!0),d=!(o||r||a||s||c||tt.visible);if(t.activeTool&&"crop"!==t.activeTool.name&&!r&&!a){return}if(d){return t.selectElements([]),void t.selectPages([])}if(i&&!n){const e=Ge.current,n=null==e?void 0:e.getPointerPosition();if(!e||!n){return}const i=e.getAllIntersections(n),o=new Set,r=[];for(const s of[...i].reverse()){const e=s.findAncestor(".element",!0);if(!e){continue}const n=t.getElementById(e.id()),i=null==n?void 0:n.top;i&&(o.has(i.id)||(o.add(i.id),r.push(i.id)))}if(0===r.length){return}let l;if(1===t.selectedElementsIds.length){const e=t.selectedElementsIds[0],n=r.indexOf(e);l=-1===n?r[0]:r[(n+1)%r.length]}else{l=r[0]}return t.selectElements([l]),void t.selectPages([])}const m=e.target.findAncestor(".element-container",!0),u=(null==m?void 0:m.findOne(".element"))||e.target.findAncestor(".element",!0),h=t.getElementById(null==u?void 0:u.id());h&&"pendingEdit"in h&&h._setImageEditError(null);const g=null==h?void 0:h.top;let f;if("group"===P){f=null==g?void 0:g.id}else if(g){const e=t.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===g.id});f=e?null==h?void 0:h.id:null==g?void 0:g.id}else{f=void 0}const p=null==h?void 0:h.id;if(p&&t.selectedElementsIds.indexOf(p)>=0&&!n){return}const x=!!f&&t.selectedElementsIds.indexOf(f)>=0,v=e.target.findAncestor(".page-container",!0);if(f&&n&&!x){const e=re(t.selectedElementsIds.concat([f]),e=>oe(t,e));t.selectElements(e),t.selectPages([])}else{f&&n&&x?t.selectElements(t.selectedElementsIds.filter(e=>e!==f)):!f||n||x?f&&!n&&x||(v?(n||t.selectElements([]),t.selectPages([l.id])):t.selectPages([])):(t.selectElements([f]),t.selectPages([]))}};I(Be,t);const dt=t.activePage===l,mt=t._selectedPagesIds.includes(l.id),ut=null==y?void 0:y.PageControls,ht=null==y?void 0:y.Tooltip,gt=null==y?void 0:y.ContextMenu,ft=1/t.scale,pt=0/t.scale;return i("div",{ref:Le,onDragOver:e=>e.preventDefault(),onDrop:e=>{if(e.preventDefault(),t.role===Y.VIEWER){return}if(!Ge.current){return}Ge.current.setPointersPositions(e);const n=Ge.current.findOne(".elements-container"),i=null==n?void 0:n.getRelativePointerPosition(),o=Ge.current.getPointerPosition(),r=Ge.current.getAllIntersections(o).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(r.reverse())].map(e=>t.getElementById(e.id())).filter(e=>!!e),a=s[0];je&&i&&(je(i,a,{elements:s,page:l}),je=null)},style:{position:"relative",width:d+"px",height:h+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+(dt?" active-page":""),children:[n(ae,{ref:Ge,eventBatchFunc:c,width:Math.min(d,O.width+200),height:Math.min(O.height+200,h),onClick:ct,onTap:ct,onContextMenu:e=>{if(e.evt.preventDefault(),t.role===Y.VIEWER){return}const n=e.target.findAncestor(".element",!0),i=t.getElementById(null==n?void 0:n.id()),o=null==i?void 0:i.top,r=null==o?void 0:o.id;if(!r){return t.selectElements([]),void Je.close()}t.selectedElementsIds.indexOf(r)>=0||t.selectElements([r]),Je.open({x:e.evt.clientX,y:e.evt.clientY})},onMouseDown:e=>{it(e),ot(e)},onTouchStart:it,onMouseMove:e=>{if(!t.distanceGuidesVisible&&!e.evt.altKey){return void(ze&&_e(null))}const n=e.target.findAncestor(".element",!0),i=null==n?void 0:n.id();rt(i)},onDragStart:e=>{var n;const i=e.target.getStage();if(i&&i.getPointersPositions().length>=2){return void e.target.stopDrag()}const o=e.target.findAncestor(".element",!0);if(o){const i=t.getElementById(null==o?void 0:o.id()),r=null==i?void 0:i.top,l=null==r?void 0:r.id;if(!(i&&t.selectedElementsIds.indexOf(i.id)>=0||l&&t.selectedElementsIds.indexOf(l)>=0)&&l){t.selectElements([l]);const i=$e();e.target.stopDrag(),i.forEach(t=>{t.isDragging()||t.startDrag(e)}),null===(n=Be.current)||void 0===n||n.startDrag(e),j()}}ze&&_e(null)},onDragMove:e=>{const n=Be.current;n&&e.target===n&&n.nodes().forEach(e=>{const n=t.getElementById(e.id());!n||n.x===e.x()&&n.y===e.y()||n.set({x:e.x(),y:e.y()})})},pageId:l.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:i(f,{children:[n(xe,{width:d,height:h,fill:J.workspaceBackgroundColor}),n(u,{x:Ie,y:Pe,scaleX:t.scale,scaleY:t.scale,name:"page-container",children:i(u,{name:"page-container-2",children:[n(u,{x:be.left,y:be.top,children:n(ye,Object.assign({},Te,De,{transparencyBackgroundColor:J.transparencyBackgroundColor}))}),i(u,{isolated:l.hasBlendMode,children:[n(u,{name:"page-background-group",x:be.left,y:be.top,clip:De,children:n(we,Object.assign({},Te))}),i(u,{x:be.left,y:be.top,name:"elements-container",listening:!t.isPlaying&&t.role!==Y.VIEWER,children:[n(x,{name:"elements-area",width:l.computedWidth,height:l.computedHeight,listening:!1}),n(Me,{elements:l.children,store:t}),n(Ae,{page:l})]})]}),n(v,{name:"bleed",fill:J.bleedColor,listening:!1,visible:t.bleedVisible&&(be.top>0||be.right>0||be.bottom>0||be.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:i,bottom:o,left:r}=ve,s=l.computedWidth,a=l.computedHeight,c=e._context;c.beginPath(),c.rect(0,0,r+s+i,n+a+o),c.rect(r,n,s,a),c.fillStyle=t.fill(),c.fill("evenodd")}}),B()===Oe&&n(Re,{name:"hit-detection",x:-ft/2-pt,y:-ft/2-pt,width:Se+ft+2*pt,height:ke+ft+2*pt})]})}),n(p,{name:"workspace-background",points:[0,0,d,0,d,h,0,h,0,0,Ie,Pe,Ie,h-Pe,d-Ie,h-Pe,d-Ie,Pe,Ie,Pe],listening:!1,closed:!0,fill:J.workspaceBackgroundColor}),n(u,{x:Ie,y:Pe,scaleX:t.scale,scaleY:t.scale,children:n(x,{name:"page-highlight",hideInExport:!0,x:-ft/2-pt,y:-ft/2-pt,width:Se+ft+2*pt,height:ke+ft+2*pt,stroke:mt?J.activePageBorderColor:J.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),i(u,{x:Ie+be.left*t.scale,y:Pe+be.top*t.scale,scaleX:t.scale,scaleY:t.scale,name:"page-abs-container",children:[n(S,{ref:Be,name:H,rotateAnchorAngle:Ze,onDragStart:e=>{var n;const i=e.target.getStage();i&&i.getPointersPositions().length>=2?e.target.stopDrag():((null===(n=e.evt)||void 0===n?void 0:n.altKey)&&A&&D(t.selectedElements,l,t),Fe.current||(Fe.current=!0,t.history.startTransaction()))},onDragEnd:()=>{Fe.current&&(t.history.endTransaction(),Fe.current=!1),et()},onTransformStart:()=>{Ue.setTransforming(!0),t.history.startTransaction(),We.current=new Map(Ne.map(e=>[e.id,{width:e.width,height:e.height}])),_e(null)},boundBoxFunc:(e,n)=>{var i,o,r,l;const s=null!==(l=null===(i=ee.boundBoxFunc)||void 0===i?void 0:i.call(ee,e,n))&&void 0!==l?l:n,a=Math.abs(s.width)<1||Math.abs(s.height)<1,c=Math.abs(e.width)<1||Math.abs(e.height)<1;if(a&&!c){return e}const d=t.selectedElements;if(1===d.length&&"table"===(null===(o=d[0])||void 0===o?void 0:o.type)){const t=d[0],n=Math.abs(e.width),i=Math.abs(e.height);if(n>0&&i>0){const o=Math.abs(s.width)/n,r=Math.abs(s.height)/i,l=t.width*o,a=t.height*r,c=Object.assign({},s);return l<t.minWidth&&(c.width=e.width),a<t.minHeight&&(c.height=e.height),$(c,s,e)}}return Ke&&Ne.length>0?((e,t,n,i,o)=>{const r=Math.abs(e.width),l=Math.abs(e.height);if(0===r||0===l){return t}const s=Math.abs(t.width)/r,a=Math.abs(t.height)/l,c=!U(n),d=e=>i.some(t=>{var n;const i=null==o?void 0:o.get(t.id),r=null!==(n="x"===e?null==i?void 0:i.width:null==i?void 0:i.height)&&void 0!==n?n:"x"===e?t.width:t.height,l="x"===e?t.width:t.height,d="x"===e?s:a,m=K(t,e,c,r);return r>=m&&l*d<m}),m=Object.assign({},t),u=t.width*e.width<0,h=t.height*e.height<0,g=u||d("x"),f=h||d("y"),p=f||!c&&g;return(g||!c&&f)&&(m.width=e.width),p&&(m.height=e.height),$(m,t,e)})(e,s,(null===(r=Be.current)||void 0===r?void 0:r.getActiveAnchor())||"",Ne,We.current):s},onTransform:e=>{const t=Be.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{qe()},0)},onTransformEnd:e=>{Ue.setTransforming(!1),Ue.setLabel({}),t.history.endTransaction(),We.current=null,et()},visible:!t.isPlaying}),n(Ye,{store:t,gesture:Ue,transformerStyle:ee,onChange:Qe}),ze&&ze.map(({points:e,distance:o,box1:r,box2:l},s)=>i(u,{name:"distances-container",hideInExport:!0,listening:!1,children:[n(x,Object.assign({},r,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),n(x,Object.assign({},l,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),n(p,{points:[e[0].x,e[0].y,e[1].x,e[1].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(p,{points:[e[1].x,e[1].y,e[2].x,e[2].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),i(g,{x:(e[0].x+e[1].x)/2,y:(e[0].y+e[1].y)/2,offsetY:-10,scaleX:1/t.scale,scaleY:1/t.scale,children:[n(w,{cornerRadius:5,fill:J.distanceGuideStyle.labelFill,pointerDirection:"down"}),n(E,{align:"center",verticalAlign:"middle",fill:J.distanceGuideStyle.labelTextFill,padding:5,text:_({unit:t.unit,dpi:t.dpi,px:o})})]})]},s)),l._rendering&&i(u,{children:[n(x,{width:Se,height:ke,fill:"rgba(255,255,255,0.9)"}),n(E,{text:"Rendering...",fontSize:60,width:Se,height:ke,align:"center",verticalAlign:"middle"})]}),ht&&n(ht,{components:y,store:t,page:l,stageRef:Ge,tooltipSafeArea:z}),gt&&n(ie,{children:n(gt,Object.assign({components:y,store:t},Je.props))}),n(R,{store:t,pageId:l.id})]}),n(M,{store:t,page:l,width:d,height:h,scale:t.scale,xPadding:Ie,yPadding:Pe,bleedLeft:be.left,bleedTop:be.top}),n(Xe,{gesture:Ue,store:t}),n(Ee,{selection:tt}),C()>0&&i(u,{hideInExport:!0,children:[n(Ce,{name:"cache-bounds",x:d-(1===C()?250:210),y:h-(1===C()?95:45)}),n(Ce,{name:"cache-bounds-t",x:10,y:10})]}),W()&&n(E,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:d-170,y:h-18,onMouseEnter:e=>{e.target.getStage().container().style.cursor="pointer"},onMouseLeave:e=>{e.target.getStage().container().style.cursor=""},onTouchStart:e=>{e.cancelBubble=!0},onMouseDown:e=>{e.cancelBubble=!0},onClick:()=>{window.open("https://polotno.com","_blank","noopener")},onTap:()=>{window.open("https://polotno.com","_blank","noopener")}}),n(u,{name:"line-guides"})]})}),Ve&&ut&&n(ut,{store:t,page:l,xPadding:Ie,yPadding:Pe,layout:T})]})});
|
|
1
|
+
var e=this&&this.__rest||function(e,t){var n={};for(var i in e){Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(n[i]=e[i])}if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++){t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(n[i[o]]=e[i[o]])}}return n};import{Fragment as t,jsx as n,jsxs as i}from"react/jsx-runtime";import o from"react";import{clamp as r}from"../lib/utils.js";import{observer as l,useLocalObservable as s}from"mobx-react-lite";import{action as a,runInAction as c}from"mobx";import{isAlive as d}from"mobx-state-tree";import{ZERO_BLEED as m}from"@polotno/schema";import{Group as u,Image as h,Label as g,Layer as f,Line as p,Rect as x,Shape as v,Stage as b,KonvaRenderer as y,Tag as w,Text as E,Transformer as S}from"react-konva";import{useWorkspaceStyle as X}from"./workspace-style.js";import{ROLES as Y}from"../model/store.js";import k from"konva";import A from"./element.js";import{DrawingLayer as M}from"./drawing-layer.js";import{ensureDragOrder as j,useSnap as I}from"./use-transformer-snap.js";import{useImageLoader as P,useSizedImage as O}from"./image-element.js";import{FadeOverlay as R}from"./fade-overlay.js";import{getCrop as T}from"../utils/crop.js";import{cloneBehind as D}from"../utils/duplicate.js";import{___ as B,____ as C,isCreditVisible as W}from"../utils/validate-key.js";import{getClientRect as G,getCorners as L,getTotalClientRect as V,polygonsIntersect as F}from"../utils/math.js";import{pxToUnitRounded as z,pxToUnitString as _}from"../utils/unit.js";import{isCornerAnchor as U,isRotateAnchor as Z,MAIN_TRANSFORMER_NAME as H}from"../utils/px.js";import{canStretchAxis as J,isAxisAligned as N,leafMinSize as K}from"../utils/selection-transform.js";import{restoreHeldPoint as $}from"./transform-box.js";import{flags as Q}from"../utils/flags.js";import{textOverflowOf as q}from"../utils/text-sizing.js";import{useColor as ee}from"./use-color.js";import{isGradient as te}from"@polotno/core/gradient";import{toDisplayColor as ne}from"@polotno/core/color";import{Html as ie}from"react-konva-utils";import{getParentGroupId as oe,withoutDescendants as re}from"../utils/selection.js";import{nodeReaction as le}from"../utils/mobx.js";import{useEvent as se}from"../hooks/use-event.js";const ae=o.version.startsWith("18.")?o.forwardRef((e,t)=>(o.useLayoutEffect(()=>{y.flushSync()}),n(b,Object.assign({},e,{ref:t})))):b,ce={enabledAnchors:["top-left","top-center","top-right","middle-left","bottom-left","bottom-right","bottom-center","middle-right"],rotateEnabled:!0,rotationSnaps:[0,45,90,135,180,225,270,315],ignoreStroke:!0,flipEnabled:!1,keepRatio:!0},de=e=>{if(!e.length){return null}const t=N(e[0].rotation)?0:e[0].rotation;return e.every(e=>N(e.rotation-t))?t:null},me=e=>e.selectedShapes.filter(t=>{var n;return(null===(n=e.activeTool)||void 0===n?void 0:n.elementId)!==t.id&&!t.pendingEdit}),ue=e=>e.selectedShapes.some(e=>!e.resizable),he=e=>e.selectedShapes.some(e=>!e.draggable),ge=e=>{var t;return 1!==e.selectedElements.length||"group"===(null===(t=e.selectedElements[0])||void 0===t?void 0:t.type)},fe=e=>{var t;const n=me(e),i=ge(e),o=null!==(t=de(n))&&void 0!==t?t:0,r=e=>i&&n.length>0&&n.every(t=>J(t,e,o));return{boxRotation:o,canStretchX:r("x"),canStretchY:r("y")}},pe={text:{enabledAnchors:["top-left","top-right","middle-left","bottom-left","bottom-right","middle-right"]},svg:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},gif:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},lottie:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},line:{enabledAnchors:[],borderEnabled:!1,rotateEnabled:!1},image:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},many:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},group:{enabledAnchors:["top-left","top-right","bottom-left","bottom-right"]},table:{keepRatio:!1,enabledAnchors:["top-left","top-center","top-right","middle-left","middle-right","bottom-left","bottom-center","bottom-right"]}};export function registerTransformerAttrs(e,t){pe[e]=pe[e]||t,Object.assign(pe[e],t)}const xe=e=>n(x,Object.assign({},e,{preventDefault:!1})),ve=t=>{var{url:i}=t,o=e(t,["url"]);const[r,l]=O(i),s=r?T(r,{width:o.width,height:o.height},"center-middle"):{};return P(l,i,"page background"),n(h,Object.assign({image:r},o,s))},be=e=>{const t=ee({a:{width:e.width,height:e.height},width:e.width,height:e.height},e.fill);return n(x,Object.assign({},e,t))},ye=t=>{var{background:i,transparencyBackgroundColor:r}=t,l=e(t,["background","transparencyBackgroundColor"]);const s=o.useMemo(()=>{if(te(i)){return!0}const e=k.Util.colorToRGBA(i);return!e||e.a<1},[i]),a=o.useMemo(()=>{if(!s||r){return}const e=document.createElement("canvas"),t=30;e.width=60,e.height=60;const n=e.getContext("2d");return n&&(n.fillStyle="black",n.fillRect(t,0,t,t),n.fillRect(0,t,t,t)),e},[s,r]);return s?n(x,r?Object.assign({fill:r},l,{hideInExport:!0}):Object.assign({fillPatternImage:a},l,{opacity:.1,hideInExport:!0})):null},we=t=>{var{background:i}=t,r=e(t,["background"]);return o.useMemo(()=>!!k.Util.colorToRGBA(i)||te(i),[i])?n(be,Object.assign({fill:i},r)):n(ve,Object.assign({url:i},r))},Ee=l(({selection:e})=>{const{selectionRectStyle:t}=X();return e.visible?n(x,{name:"selection",x:Math.min(e.x1,e.x2),y:Math.min(e.y1,e.y2),width:Math.abs(e.x1-e.x2),height:Math.abs(e.y1-e.y2),fill:t.fill,stroke:t.stroke,strokeWidth:t.strokeWidth}):null});function Se(e){let t=180*e/Math.PI;return t%=360,t>180?t-=360:t<-180&&(t+=360),Math.round(t)}const Xe=l(({gesture:e,store:o})=>{const{transformLabelStyle:r}=X(),{x:l,y:s,width:a,height:c,rotation:d,anchor:m}=e.label;if(void 0===l||void 0===s||void 0===a||void 0===c||void 0===d||void 0===m){return null}const u=G({x:l,y:s,width:a,height:c,rotation:k.Util.radToDeg(d)}),h=null!==m&&Z(m),f=(c/2+70)*Math.cos(d-Math.PI/2),p=(c/2+70)*Math.sin(d-Math.PI/2),x=z({unit:o.unit,dpi:o.dpi,px:a/o.scale,precious:"px"===o.unit?0:1})+" x "+z({unit:o.unit,dpi:o.dpi,px:c/o.scale,precious:"px"===o.unit?0:1})+("px"===o.unit?"":" "+o.unit);return i(t,{children:[i(g,{x:(u.minX+u.maxX)/2+f,y:(u.minY+u.maxY)/2+p,offsetX:14,offsetY:14,visible:h,children:[n(w,{cornerRadius:5,fill:r.fill}),n(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:Se(d).toString()+"°"})]}),i(g,{x:(u.minX+u.maxX)/2,y:u.maxY+20,visible:!h,children:[n(w,{cornerRadius:5,fill:r.fill,pointerDirection:"up",pointerHeight:0,pointerWidth:0}),n(E,{align:"center",verticalAlign:"middle",fill:r.textFill,padding:8,text:x})]})]})}),Ye=l(({store:e,gesture:t,transformerStyle:n,onChange:i})=>{var r;const l=e.selectedElements.some(e=>e.curveEnabled),s=e.selectedElements.some(e=>!e.visible),a=JSON.stringify(me(e).map(e=>e.id)),c=!ge(e)&&e.selectedElements[0].keepRatio,d=!ge(e)&&"text"===(null===(r=e.selectedElements[0])||void 0===r?void 0:r.type)&&"fit"===q(e.selectedElements[0]),m=t.transforming?null:fe(e);return o.useLayoutEffect(()=>{i()},[e.selectedShapes,a,ue(e),s,he(e),l,c,d,t.transforming,null==m?void 0:m.canStretchX,null==m?void 0:m.canStretchY,null==m?void 0:m.boxRotation,n]),null}),ke=()=>{console.warn("Polotno warning: onClick callback is deprecated. Just stop using it. Polotno will do selection automatically.")},Ae=l(({page:e})=>{const t=e._exportingOrRendering||e._forceMount;return o.useEffect(()=>{if(t&&d(e)){return e.set({_exportRendered:!0}),()=>{d(e)&&e.set({_exportRendered:!1})}}},[e,t]),null}),Me=l(({elements:e,store:t})=>{const i=e.filter(e=>e.alwaysOnTop),r=e.filter(e=>!e.alwaysOnTop).concat(i);return n(o.Fragment,{children:r.map(e=>n(A,{store:t,element:e,onClick:ke},e.id))})});export const useContextMenu=({store:e})=>{const[t,n]=o.useState(!1),[i,r]=o.useState({x:0,y:0}),l=o.useCallback(e=>{n(!0),r(e)},[]);return{open:l,close:o.useCallback(()=>{n(!1)},[]),props:{isOpen:t,offset:i,setIsOpen:n}}};let je=null;export const registerNextDomDrop=e=>{je=e};const Ie=atob("UG9sb3RubyBmcmVlIGxpY2Vuc2UgbGltaXRhdGlvbiBleGNlZWRlZCAtIFBsZWFzZSB1cGdyYWRlIHlvdXIgYWNjb3VudC4="),Pe=atob("I2MwMzkyYg=="),Oe=atob("djAuOS4y"),Re=e=>n(t,{children:i(g,{fill:Pe,height:200,children:[n(w,{fill:Pe}),n(E,Object.assign({},e,{fill:"white",text:Ie,height:void 0,padding:10,fontSize:20}))]})}),Te=atob("RlJFRSAgVFJJQUwgIExJQ0VOU0U="),De=(atob("UG93ZXJlZCBieSBQb2xvdG5v"),atob("TElDRU5TRSAgS0VZICBJUyAgTUlTU0lORw==")),Be=atob("UGxlYXNlIGFjdGl2YXRlIHlvdXIgbGljZW5zZS4KTGljZW5zZSByZWxhdGVkIHF1ZXN0aW9uczoKcG9sb3Ruby5jb20vY29udGFjdA=="),Ce=e=>{const t=1===C(),o=t?De:Te,r=t?230:195;return i(u,Object.assign({},e,{listening:!1,children:[n(x,{fill:Pe,width:r,height:24,cornerRadius:2}),n(E,{text:o,fill:"white",fontSize:13,fontFamily:"monospace",width:r,align:"center",y:5,listening:!1}),t&&n(E,{text:Be,fill:"rgba(0,0,0,0.6)",fontSize:14,y:30,listening:!1})]}))};export default l(({store:t,page:l,width:d,height:h,pageControlsEnabled:b,components:y,altCloneEnabled:A,groupSelectionMode:P="drill",viewportSize:O,layout:T="vertical",tooltipSafeArea:z,transformerSafeArea:Z})=>{const J=X(),ee=J.transformerStyle,te=ce,ve=l.computedBleed,be=t.bleedVisible?ve:m,Se=l.computedWidth+be.left+be.right,ke=l.computedHeight+be.top+be.bottom,Ie=(d-Se*t.scale)/2,Pe=(h-ke*t.scale)/2,Te={x:-ve.left,y:-ve.top,width:l.computedWidth+ve.left+ve.right,height:l.computedHeight+ve.top+ve.bottom,background:ne(l.background),name:"page-background",preventDefault:!1},De={x:-be.left,y:-be.top,width:Se,height:ke},Be=o.useRef(null),We=o.useRef(null),Ge=o.useRef(null),Le=o.useRef(null),Ve=null==b||b,Fe=o.useRef(!1),[ze,_e]=o.useState(null),Ue=s(()=>({transforming:!1,label:{},setLabel(e){this.label=e},setTransforming(e){this.transforming=e}})),[Ze,He]=o.useState(0),Je=useContextMenu({store:t}),Ne=me(t),Ke=ge(t),$e=()=>{var n,i,o;const r=Be.current;if(!r){return[]}const l=r.getStage();if(!l){return[]}const s=me(t),a=new Set(s.map(e=>e.id)),c=new Map;a.size&&l.find(e=>{const t=e.id();return a.has(t)&&!c.has(t)&&c.set(t,e),!1});const d=s.map(e=>c.get(e.id)).filter(e=>!!e),m=1===t.selectedElements.length,u=m&&(null===(n=t.selectedElements[0])||void 0===n?void 0:n.type)||"many",h=pe[u],g=Object.assign(Object.assign({},te),h||{});h&&("svg"!==u&&"image"!==u&&"gif"!==u&&"lottie"!==u||t.selectedElements[0].keepRatio||(g.enabledAnchors=te.enabledAnchors),"text"!==u||!Q.textVerticalResizeEnabled&&"fit"!==q(t.selectedElements[0])||(g.enabledAnchors=null===(i=pe.text.enabledAnchors)||void 0===i?void 0:i.concat(["bottom-center","top-center"])),"text"===u&&m&&t.selectedElements[0].curveEnabled&&(g.enabledAnchors=pe.many.enabledAnchors));const f=fe(t);(f.canStretchX||f.canStretchY)&&(g.enabledAnchors=[...g.enabledAnchors||[],...f.canStretchX?["middle-left","middle-right"]:[],...f.canStretchY?["top-center","bottom-center"]:[]]);const{boundBoxFunc:p}=ee,x=e(ee,["boundBoxFunc"]);r.setAttrs(Object.assign(Object.assign(Object.assign({},g),x),{name:H})),ue(t)&&r.enabledAnchors([]),he(t)&&r.rotateEnabled(!1);const v=r.nodes();if(v.length===d.length&&d.every((e,t)=>v[t]===e)||r.nodes(d),ge(t)&&d.length){const e=de(s);null===e||N(r.rotation()-e)||(r.rotation(e),r.forceUpdate())}return null===(o=r.getLayer())||void 0===o||o.batchDraw(),d},Qe=se(()=>{var e;if(null===(e=Be.current)||void 0===e?void 0:e.isTransforming()){return}const t=$e();t.find(e=>null==e?void 0:e.isDragging())&&(t.forEach(e=>{e.isDragging()||null==e||e.startDrag()}),j())}),qe=()=>{var e,t;const n=Be.current;if(!n){return}if(!n.isTransforming()){return void Ue.setLabel({})}if(!n.nodes().length){return}const i=n.__getNodeRect(),o=null==n?void 0:n.getActiveAnchor(),r=n.getStage();Ue.setLabel({anchor:o,x:i.x-(null!==(e=null==r?void 0:r.x())&&void 0!==e?e:0),y:i.y-(null!==(t=null==r?void 0:r.y())&&void 0!==t?t:0),rotation:i.rotation,width:i.width,height:i.height})},et=o.useCallback(()=>{var e;if(null===(e=Be.current)||void 0===e?void 0:e.isTransforming()){return}const t=Be.current;if(!t||!t.nodes().length){return void He(0)}const n=t.getClientRect();if(!t.getStage()){return}const i=function(e,t,n,i=0){const o=e.x+e.width/2,r=e.y+e.height/2,l=e.width/2,s=e.height/2,a=i*Math.PI/180,c=e=>{const t=e*Math.PI/180,i=(e%360+360)%360;let c;c=0===i||180===i?s:l;const d=Math.sin(t)*(c+n),m=-Math.cos(t)*(c+n),u=d*Math.cos(a)-m*Math.sin(a),h=d*Math.sin(a)+m*Math.cos(a);return{x:o+u,y:r+h}},d=e=>e.x>=t.left&&e.x<=t.right&&e.y>=t.top&&e.y<=t.bottom;if(d(c(0))){return 0}for(const m of[180,270,90]){if(d(c(m))){return m}}return 0}(n,{top:(null==Z?void 0:Z.top)||0,bottom:h-((null==Z?void 0:Z.bottom)||0),left:(null==Z?void 0:Z.left)||0,right:d-((null==Z?void 0:Z.right)||0)},ee.rotateAnchorOffset,t.rotation());He(i)},[Z,d,h]);o.useEffect(()=>{var e;null===(e=Be.current)||void 0===e||e.update(),qe(),et()},[t.scale,et]),o.useEffect(()=>{et()},[t.selectedShapes,et]);const tt=s(()=>({visible:!1,x1:0,y1:0,x2:0,y2:0})),nt=o.useRef(!1),it=e=>{const n=Ge.current;if(!n||t.role===Y.VIEWER||t.activePage!==l||t.isPlaying||"button"in e.evt&&0!==e.evt.button||1!==n.getPointersPositions().length||!e.target.findAncestor(".elements-container")||!e.target.findAncestor(".element",!0)||!t.selectedShapes.length||he(t)){return}const i=n.getPointerPosition();if(!i){return}const o=$e();if(o.some(t=>t===e.target||t.isAncestorOf(e.target))){return}const r=o.find(e=>!!(e.draggable()&&e.isVisible()&&e.isListening())&&(e instanceof k.Shape?[e]:e instanceof k.Container?e.find("Shape"):[]).some(e=>e.intersects(i)));if(r){for(const[t,n]of k.DD._dragElements){"ready"!==n.dragStatus||n.node!==e.target&&!n.node.isAncestorOf(e.target)||k.DD._dragElements.delete(t)}r._createDragElement(e),"touches"in e.evt&&e.evt.cancelable&&r.preventDefault()&&e.evt.preventDefault()}},ot=a(e=>{var n,i,o,r,l,s,a;if(t.role===Y.VIEWER){return}if(nt.current=!1,t.activeTool&&"crop"!==t.activeTool.name){return}const c=e.target.findAncestor(".elements-container"),d=e.target.findAncestor("Transformer"),m=e.target.findAncestor(".page-abs-container");if(c||d||m){return}const u=null===(n=e.target.getStage())||void 0===n?void 0:n.getPointerPosition();u&&(u.x-=null!==(l=null===(i=e.target.getStage())||void 0===i?void 0:i.x())&&void 0!==l?l:0,u.y-=null!==(s=null===(o=e.target.getStage())||void 0===o?void 0:o.y())&&void 0!==s?s:0,tt.visible=!0,tt.x1=u.x,tt.y1=u.y,tt.x2=u.x,tt.y2=u.y,(null!==(a=null===(r=e.target.getStage())||void 0===r?void 0:r.getPointersPositions().length)&&void 0!==a?a:0)>=2&&(tt.visible=!1))});(({stageRef:e,containerRef:t,viewportSize:n})=>{o.useEffect(()=>{var n;const i=null===(n=t.current)||void 0===n?void 0:n.closest(".polotno-workspace-inner");function o(){var n;if(!e.current){return}const o=null===(n=t.current)||void 0===n?void 0:n.getBoundingClientRect(),l=null==i?void 0:i.getBoundingClientRect();if(!o||!l){return}const s=r(l.left-o.left-100,0,Math.max(0,o.width-e.current.width())),a=r(l.top-o.top-100,0,Math.max(0,o.height-e.current.height()));e.current.position({x:-s,y:-a}),e.current.container().style.transform=`translate(${s}px, ${a}px)`}return o(),null==i||i.addEventListener("scroll",o),()=>{null==i||i.removeEventListener("scroll",o)}},[n.width,n.height])})({stageRef:Ge,containerRef:Le,viewportSize:O}),o.useEffect(()=>{const e=a(e=>{var t,n,i,o,r,l;if(!tt.visible){return}null===(t=Ge.current)||void 0===t||t.setPointersPositions(e);let s=null===(n=Ge.current)||void 0===n?void 0:n.getPointerPosition();s?(s.x-=null!==(r=null===(i=Ge.current)||void 0===i?void 0:i.x())&&void 0!==r?r:0,s.y-=null!==(l=null===(o=Ge.current)||void 0===o?void 0:o.y())&&void 0!==l?l:0):s={x:tt.x2,y:tt.y2},tt.x2=s.x,tt.y2=s.y}),n=a(()=>{if(!tt.visible){return}if(!Ge.current){return}const e=Ge.current.findOne(".selection"),n=e?e.getClientRect({skipStroke:!0}):{width:0,height:0,x:0,y:0};if(n.width&&n.height){const e=[{x:n.x,y:n.y},{x:n.x+n.width,y:n.y},{x:n.x+n.width,y:n.y+n.height},{x:n.x,y:n.y+n.height}],i=[];Ge.current.find(".element").forEach(n=>{var o;const r=t.getElementById(n.id()),l=null==r?void 0:r.draggable,s=null==r?void 0:r.selectable;if(!l||!s){return}const a=null===(o=n.getParent())||void 0===o?void 0:o.getAbsoluteTransform();if(!a){return}const c=L(r.a).map(e=>a.point(e));F(e,c)&&i.push(r.top.id)});const o=[...new Set(i)];nt.current=!0,t.selectElements(o)}tt.visible=!1});return window.addEventListener("mousemove",e),window.addEventListener("touchmove",e),window.addEventListener("mouseup",n,{capture:!0}),window.addEventListener("touchend",n,{capture:!0}),()=>{window.removeEventListener("mousemove",e),window.removeEventListener("touchmove",e),window.removeEventListener("mouseup",n,{capture:!0}),window.removeEventListener("touchend",n,{capture:!0})}},[]);const rt=o.useCallback(e=>{if(!t.selectedElements[0]){return}if(e&&t.selectedElementsIds.includes(e)){return}const n=V(t.selectedShapes),i=e?t.getElementById(e):{x:0,y:0,width:l.computedWidth,height:l.computedHeight,rotation:0},o=G(i),r=[];n.minX>o.maxX&&r.push({distance:n.minX-o.maxX,box1:n,box2:o,points:[{x:n.minX,y:n.minY+n.height/2},{x:o.maxX,y:n.minY+n.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),n.maxX<o.minX&&r.push({distance:o.minX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:n.minY+n.height/2},{x:o.minX,y:n.minY+n.height/2},{x:o.minX,y:o.minY+o.height/2}]}),n.minY>o.maxY&&r.push({box1:n,box2:o,distance:n.minY-o.maxY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]}),n.maxY<o.minY&&r.push({box1:n,box2:o,distance:o.minY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]});const s=n.minX>=o.minX&&n.maxX<=o.maxX&&n.minY>=o.minY&&n.maxY<=o.maxY;if(s&&(r.push({distance:n.minX-o.minX,box1:n,box2:o,points:[{x:n.minX,y:n.minY+n.height/2},{x:o.minX,y:n.minY+n.height/2},{x:o.minX,y:o.minY+o.height/2}]}),r.push({distance:o.maxX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:n.minY+n.height/2},{x:o.maxX,y:n.minY+n.height/2},{x:o.maxX,y:o.minY+o.height/2}]}),r.push({box1:n,box2:o,distance:n.minY-o.minY,points:[{x:n.minX+n.width/2,y:n.minY},{x:n.minX+n.width/2,y:o.minY},{x:o.minX+o.width/2,y:o.minY}]}),r.push({box1:n,box2:o,distance:o.maxY-n.maxY,points:[{x:n.minX+n.width/2,y:n.maxY},{x:n.minX+n.width/2,y:o.maxY},{x:o.minX+o.width/2,y:o.maxY}]})),0===r.length&&!s){const e=n.minX<o.maxX&&n.maxX>o.minX,t=n.minY<o.maxY&&n.maxY>o.minY;if(e&&t){const e=(Math.max(n.minY,o.minY)+Math.min(n.maxY,o.maxY))/2,t=(Math.max(n.minX,o.minX)+Math.min(n.maxX,o.maxX))/2;n.minX<o.minX?r.push({distance:o.minX-n.minX,box1:n,box2:o,points:[{x:n.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}):r.push({distance:n.minX-o.minX,box1:n,box2:o,points:[{x:n.minX,y:e},{x:o.minX,y:e},{x:o.minX,y:e}]}),n.maxX>o.maxX?r.push({distance:n.maxX-o.maxX,box1:n,box2:o,points:[{x:o.maxX,y:e},{x:n.maxX,y:e},{x:o.maxX,y:e}]}):r.push({distance:o.maxX-n.maxX,box1:n,box2:o,points:[{x:n.maxX,y:e},{x:o.maxX,y:e},{x:o.maxX,y:e}]}),n.minY<o.minY?r.push({box1:n,box2:o,distance:o.minY-n.minY,points:[{x:t,y:n.minY},{x:t,y:o.minY},{x:t,y:o.minY}]}):r.push({box1:n,box2:o,distance:n.minY-o.minY,points:[{x:t,y:n.minY},{x:t,y:o.minY},{x:t,y:o.minY}]}),n.maxY>o.maxY?r.push({box1:n,box2:o,distance:n.maxY-o.maxY,points:[{x:t,y:o.maxY},{x:t,y:n.maxY},{x:t,y:o.maxY}]}):r.push({box1:n,box2:o,distance:o.maxY-n.maxY,points:[{x:t,y:n.maxY},{x:t,y:o.maxY},{x:t,y:o.maxY}]})}}_e(e=>JSON.stringify(e)===JSON.stringify(r)?e:r)},[t,l]),lt=o.useCallback(()=>{t.distanceGuidesVisible&&t.selectedElements[0]?rt():_e(null)},[t,rt]);o.useEffect(lt,[t.distanceGuidesVisible,lt]);const st=t.distanceGuidesVisible||!!ze;o.useEffect(()=>{if(st){return le(l,()=>t.selectedShapes.map(e=>`${e.x},${e.y},${e.width},${e.height},${e.rotation}`).join("|"),lt)}},[st,lt]);const at=o.useRef(!1);o.useEffect(()=>{var e;let t;const n=null===(e=Le.current)||void 0===e?void 0:e.closest(".polotno-workspace-inner"),i=()=>{at.current=!0,clearTimeout(t),t=setTimeout(()=>{at.current=!1},300)};return null==n||n.addEventListener("scroll",i),()=>{clearTimeout(t),null==n||n.removeEventListener("scroll",i)}},[]),o.useEffect(()=>{const e=Ge.current;if(!e){return}const n=e.container(),i=new Map,o=e=>{var t,n;i.has(e.target)||[e.target,...null!==(n=null===(t=Be.current)||void 0===t?void 0:t.nodes())&&void 0!==n?n:[]].forEach(e=>{i.has(e)||i.set(e,{x:e.x(),y:e.y()})})},r=e=>{e.target===Be.current?i.clear():i.delete(e.target)};e.on("dragstart",o),e.on("dragend",r);const l=e=>{e.touches.length>=2&&c(()=>{var e,n;const o=new Map(i);(null===(e=Be.current)||void 0===e?void 0:e.isDragging())&&Be.current.stopDrag(),null===(n=Be.current)||void 0===n||n.nodes().forEach(e=>{var n;const i=o.get(e);e.isDragging()&&e.stopDrag(),i&&(e.setAttrs(i),null===(n=t.getElementById(e.id()))||void 0===n||n.set({x:i.x,y:i.y}))})})};return n.addEventListener("touchstart",l,{passive:!0}),n.addEventListener("touchmove",l,{passive:!0}),()=>{e.off("dragstart",o),e.off("dragend",r),n.removeEventListener("touchstart",l),n.removeEventListener("touchmove",l)}},[t]);const ct=e=>{if(t.role===Y.VIEWER){return}if(t.activePage!==l&&l.select(),at.current){return}if(nt.current){return}const n=e.evt.shiftKey,i=e.evt.ctrlKey||e.evt.metaKey,o=n||i,r=e.target.findAncestor(".elements-container"),s=e.target.findAncestor(".page-abs-container"),a=e.target.findAncestor("Transformer"),c=e.target.findAncestor(".page-container",!0),d=!(o||r||a||s||c||tt.visible);if(t.activeTool&&"crop"!==t.activeTool.name&&!r&&!a){return}if(d){return t.selectElements([]),void t.selectPages([])}if(i&&!n){const e=Ge.current,n=null==e?void 0:e.getPointerPosition();if(!e||!n){return}const i=e.getAllIntersections(n),o=new Set,r=[];for(const s of[...i].reverse()){const e=s.findAncestor(".element",!0);if(!e){continue}const n=t.getElementById(e.id()),i=null==n?void 0:n.top;i&&(o.has(i.id)||(o.add(i.id),r.push(i.id)))}if(0===r.length){return}let l;if(1===t.selectedElementsIds.length){const e=t.selectedElementsIds[0],n=r.indexOf(e);l=-1===n?r[0]:r[(n+1)%r.length]}else{l=r[0]}return t.selectElements([l]),void t.selectPages([])}const m=e.target.findAncestor(".element-container",!0),u=(null==m?void 0:m.findOne(".element"))||e.target.findAncestor(".element",!0),h=t.getElementById(null==u?void 0:u.id());h&&"pendingEdit"in h&&h._setImageEditError(null);const g=null==h?void 0:h.top;let f;if("group"===P){f=null==g?void 0:g.id}else if(g){const e=t.selectedElements.some(e=>{var t;return(null===(t=e.top)||void 0===t?void 0:t.id)===g.id});f=e?null==h?void 0:h.id:null==g?void 0:g.id}else{f=void 0}const p=null==h?void 0:h.id;if(p&&t.selectedElementsIds.indexOf(p)>=0&&!n){return}const x=!!f&&t.selectedElementsIds.indexOf(f)>=0,v=e.target.findAncestor(".page-container",!0);if(f&&n&&!x){const e=re(t.selectedElementsIds.concat([f]),e=>oe(t,e));t.selectElements(e),t.selectPages([])}else{f&&n&&x?t.selectElements(t.selectedElementsIds.filter(e=>e!==f)):!f||n||x?f&&!n&&x||(v?(n||t.selectElements([]),t.selectPages([l.id])):t.selectPages([])):(t.selectElements([f]),t.selectPages([]))}};I(Be,t);const dt=t.activePage===l,mt=t._selectedPagesIds.includes(l.id),ut=null==y?void 0:y.PageControls,ht=null==y?void 0:y.Tooltip,gt=null==y?void 0:y.ContextMenu,ft=1/t.scale,pt=0/t.scale;return i("div",{ref:Le,onDragOver:e=>e.preventDefault(),onDrop:e=>{if(e.preventDefault(),t.role===Y.VIEWER){return}if(!Ge.current){return}Ge.current.setPointersPositions(e);const n=Ge.current.findOne(".elements-container"),i=null==n?void 0:n.getRelativePointerPosition(),o=Ge.current.getPointerPosition(),r=Ge.current.getAllIntersections(o).map(e=>e.findAncestor(".element",!0)).filter(Boolean),s=[...new Set(r.reverse())].map(e=>t.getElementById(e.id())).filter(e=>!!e),a=s[0];je&&i&&(je(i,a,{elements:s,page:l}),je=null)},style:{position:"relative",width:d+"px",height:h+"px",overflow:"hidden",flexShrink:0},className:"polotno-page-container"+(dt?" active-page":""),children:[n(ae,{ref:Ge,eventBatchFunc:c,width:Math.min(d,O.width+200),height:Math.min(O.height+200,h),onClick:ct,onTap:ct,onContextMenu:e=>{if(e.evt.preventDefault(),t.role===Y.VIEWER){return}const n=e.target.findAncestor(".element",!0),i=t.getElementById(null==n?void 0:n.id()),o=null==i?void 0:i.top,r=null==o?void 0:o.id;if(!r){return t.selectElements([]),void Je.close()}t.selectedElementsIds.indexOf(r)>=0||t.selectElements([r]),Je.open({x:e.evt.clientX,y:e.evt.clientY})},onMouseDown:e=>{it(e),ot(e)},onTouchStart:it,onMouseMove:e=>{if(!t.distanceGuidesVisible&&!e.evt.altKey){return void(ze&&_e(null))}const n=e.target.findAncestor(".element",!0),i=null==n?void 0:n.id();rt(i)},onDragStart:e=>{var n;const i=e.target.getStage();if(i&&i.getPointersPositions().length>=2){return void e.target.stopDrag()}const o=e.target.findAncestor(".element",!0);if(o){const i=t.getElementById(null==o?void 0:o.id()),r=null==i?void 0:i.top,l=null==r?void 0:r.id;if(!(i&&t.selectedElementsIds.indexOf(i.id)>=0||l&&t.selectedElementsIds.indexOf(l)>=0)&&l){t.selectElements([l]);const i=$e();e.target.stopDrag(),i.forEach(t=>{t.isDragging()||t.startDrag(e)}),null===(n=Be.current)||void 0===n||n.startDrag(e),j()}}ze&&_e(null)},onDragMove:e=>{const n=Be.current;n&&e.target===n&&n.nodes().forEach(e=>{const n=t.getElementById(e.id());!n||n.x===e.x()&&n.y===e.y()||n.set({x:e.x(),y:e.y()})})},pageId:l.id,style:{position:"absolute",overflow:"hidden",top:0,left:0},children:i(f,{children:[n(xe,{width:d,height:h,fill:J.workspaceBackgroundColor}),n(u,{x:Ie,y:Pe,scaleX:t.scale,scaleY:t.scale,name:"page-container",children:i(u,{name:"page-container-2",children:[n(u,{x:be.left,y:be.top,children:n(ye,Object.assign({},Te,De,{transparencyBackgroundColor:J.transparencyBackgroundColor}))}),i(u,{isolated:l.hasBlendMode,children:[n(u,{name:"page-background-group",x:be.left,y:be.top,clip:De,children:n(we,Object.assign({},Te))}),i(u,{x:be.left,y:be.top,name:"elements-container",listening:!t.isPlaying&&t.role!==Y.VIEWER,children:[n(x,{name:"elements-area",width:l.computedWidth,height:l.computedHeight,listening:!1}),n(Me,{elements:l.children,store:t}),n(Ae,{page:l})]})]}),n(v,{name:"bleed",fill:J.bleedColor,listening:!1,visible:t.bleedVisible&&(be.top>0||be.right>0||be.bottom>0||be.left>0),hideInExport:!0,sceneFunc:(e,t)=>{const{top:n,right:i,bottom:o,left:r}=ve,s=l.computedWidth,a=l.computedHeight,c=e._context;c.beginPath(),c.rect(0,0,r+s+i,n+a+o),c.rect(r,n,s,a),c.fillStyle=t.fill(),c.fill("evenodd")}}),B()===Oe&&n(Re,{name:"hit-detection",x:-ft/2-pt,y:-ft/2-pt,width:Se+ft+2*pt,height:ke+ft+2*pt})]})}),n(p,{name:"workspace-background",points:[0,0,d,0,d,h,0,h,0,0,Ie,Pe,Ie,h-Pe,d-Ie,h-Pe,d-Ie,Pe,Ie,Pe],listening:!1,closed:!0,fill:J.workspaceBackgroundColor}),n(u,{x:Ie,y:Pe,scaleX:t.scale,scaleY:t.scale,children:n(x,{name:"page-highlight",hideInExport:!0,x:-ft/2-pt,y:-ft/2-pt,width:Se+ft+2*pt,height:ke+ft+2*pt,stroke:mt?J.activePageBorderColor:J.pageBorderColor,strokeWidth:2,listening:!1,strokeScaleEnabled:!1})}),i(u,{x:Ie+be.left*t.scale,y:Pe+be.top*t.scale,scaleX:t.scale,scaleY:t.scale,name:"page-abs-container",children:[n(S,{ref:Be,name:H,rotateAnchorAngle:Ze,onDragStart:e=>{var n;const i=e.target.getStage();i&&i.getPointersPositions().length>=2?e.target.stopDrag():((null===(n=e.evt)||void 0===n?void 0:n.altKey)&&A&&D(t.selectedElements,l,t),Fe.current||(Fe.current=!0,t.history.startTransaction()))},onDragEnd:()=>{Fe.current&&(t.history.endTransaction(),Fe.current=!1),et()},onTransformStart:()=>{Ue.setTransforming(!0),t.history.startTransaction(),We.current=new Map(Ne.map(e=>[e.id,{width:e.width,height:e.height}])),_e(null)},boundBoxFunc:(e,n)=>{var i,o,r,l;const s=null!==(l=null===(i=ee.boundBoxFunc)||void 0===i?void 0:i.call(ee,e,n))&&void 0!==l?l:n,a=Math.abs(s.width)<1||Math.abs(s.height)<1,c=Math.abs(e.width)<1||Math.abs(e.height)<1;if(a&&!c){return e}const d=t.selectedElements;if(1===d.length&&"table"===(null===(o=d[0])||void 0===o?void 0:o.type)){const t=d[0],n=Math.abs(e.width),i=Math.abs(e.height);if(n>0&&i>0){const o=Math.abs(s.width)/n,r=Math.abs(s.height)/i,l=t.width*o,a=t.height*r,c=Object.assign({},s);return l<t.minWidth&&(c.width=e.width),a<t.minHeight&&(c.height=e.height),$(c,s,e)}}return Ke&&Ne.length>0?((e,t,n,i,o)=>{const r=Math.abs(e.width),l=Math.abs(e.height);if(0===r||0===l){return t}const s=Math.abs(t.width)/r,a=Math.abs(t.height)/l,c=!U(n),d=e=>i.some(t=>{var n;const i=null==o?void 0:o.get(t.id),r=null!==(n="x"===e?null==i?void 0:i.width:null==i?void 0:i.height)&&void 0!==n?n:"x"===e?t.width:t.height,l="x"===e?t.width:t.height,d="x"===e?s:a,m=K(t,e,c,r);return r>=m&&l*d<m}),m=Object.assign({},t),u=t.width*e.width<0,h=t.height*e.height<0,g=u||d("x"),f=h||d("y"),p=f||!c&&g;return(g||!c&&f)&&(m.width=e.width),p&&(m.height=e.height),$(m,t,e)})(e,s,(null===(r=Be.current)||void 0===r?void 0:r.getActiveAnchor())||"",Ne,We.current):s},onTransform:e=>{const t=Be.current.nodes(),n=t[t.length-1];e.target===n&&setTimeout(()=>{qe()},0)},onTransformEnd:e=>{Ue.setTransforming(!1),Ue.setLabel({}),t.history.endTransaction(),We.current=null,et()},visible:!t.isPlaying}),n(Ye,{store:t,gesture:Ue,transformerStyle:ee,onChange:Qe}),ze&&ze.map(({points:e,distance:o,box1:r,box2:l},s)=>i(u,{name:"distances-container",hideInExport:!0,listening:!1,children:[n(x,Object.assign({},r,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),n(x,Object.assign({},l,{stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1})),n(p,{points:[e[0].x,e[0].y,e[1].x,e[1].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),n(p,{points:[e[1].x,e[1].y,e[2].x,e[2].y],stroke:J.distanceGuideStyle.stroke,strokeWidth:1,strokeScaleEnabled:!1}),i(g,{x:(e[0].x+e[1].x)/2,y:(e[0].y+e[1].y)/2,offsetY:-10,scaleX:1/t.scale,scaleY:1/t.scale,children:[n(w,{cornerRadius:5,fill:J.distanceGuideStyle.labelFill,pointerDirection:"down"}),n(E,{align:"center",verticalAlign:"middle",fill:J.distanceGuideStyle.labelTextFill,padding:5,text:_({unit:t.unit,dpi:t.dpi,px:o})})]})]},s)),l._rendering&&i(u,{children:[n(x,{width:Se,height:ke,fill:"rgba(255,255,255,0.9)"}),n(E,{text:"Rendering...",fontSize:60,width:Se,height:ke,align:"center",verticalAlign:"middle"})]}),ht&&n(ht,{components:y,store:t,page:l,stageRef:Ge,tooltipSafeArea:z}),gt&&n(ie,{children:n(gt,Object.assign({components:y,store:t},Je.props))}),n(R,{store:t,pageId:l.id})]}),n(M,{store:t,page:l,width:d,height:h,scale:t.scale,xPadding:Ie,yPadding:Pe,bleedLeft:be.left,bleedTop:be.top}),n(Xe,{gesture:Ue,store:t}),n(Ee,{selection:tt}),C()>0&&i(u,{hideInExport:!0,children:[n(Ce,{name:"cache-bounds",x:d-(1===C()?250:210),y:h-(1===C()?95:45)}),n(Ce,{name:"cache-bounds-t",x:10,y:10})]}),W()&&n(E,{text:"Powered by polotno.com",fontSize:14,fill:"rgba(0,0,0,0.6)",x:d-170,y:h-18,onMouseEnter:e=>{e.target.getStage().container().style.cursor="pointer"},onMouseLeave:e=>{e.target.getStage().container().style.cursor=""},onTouchStart:e=>{e.cancelBubble=!0},onMouseDown:e=>{e.cancelBubble=!0},onClick:()=>{window.open("https://polotno.com","_blank","noopener")},onTap:()=>{window.open("https://polotno.com","_blank","noopener")}}),n(u,{name:"line-guides"})]})}),Ve&&ut&&n(ut,{store:t,page:l,xPadding:Ie,yPadding:Pe,layout:T})]})});
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export declare const PRESERVED_INLINE_STYLES: readonly ['text-shadow', 'letter-spacing', 'word-spacing', '-webkit-text-stroke', 'font-variant', 'vertical-align'];
|
|
1
|
+
export declare const PRESERVED_INLINE_STYLES: readonly ['text-shadow', 'letter-spacing', 'word-spacing', '-webkit-text-stroke', 'font-variant', 'vertical-align', 'text-decoration-color', 'text-decoration-style', 'text-decoration-thickness', 'text-underline-offset'];
|
|
2
2
|
export declare const PRESERVED_BLOCK_STYLES: readonly ['text-align', 'line-height', 'margin-top', 'margin-bottom', 'text-indent', 'list-style-type'];
|
|
3
3
|
export declare function registerQuillUnrestrictedFontFormats(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"quill";import{firstStopColor as e}from"@polotno/core/gradient";import{parsePrimaryFontFamily as r}from"@polotno/core/rich-text-html";import{cssFontFamily as o}from"@polotno/core/fonts";import{containsCmykColor as i,isCmykColor as n,toDisplayColor as s}from"@polotno/core/color";import{isClipGradientSpan as l,nativeColorAttribute as c,readNativeColor as a}from"../utils/rich-text-html.js";let u=!1;export const PRESERVED_INLINE_STYLES=["text-shadow","letter-spacing","word-spacing","-webkit-text-stroke","font-variant","vertical-align"];export const PRESERVED_BLOCK_STYLES=["text-align","line-height","margin-top","margin-bottom","text-indent","list-style-type"];function p(t){const e=t.keyName,r=c(e),o=t.add.bind(t),i=t.remove.bind(t),l=t.value.bind(t);t.add=(t,e)=>n(e)?!!o(t,s(e))&&(t.setAttribute(r,e),!0):(t.removeAttribute(r),o(t,e)),t.remove=t=>{t.removeAttribute(r),i(t)},t.value=t=>a(t,e)||l(t)}const m=["background-image","background-size","-webkit-background-clip","background-clip","-webkit-text-fill-color","color"],
|
|
1
|
+
import t from"quill";import{firstStopColor as e}from"@polotno/core/gradient";import{parsePrimaryFontFamily as r}from"@polotno/core/rich-text-html";import{cssFontFamily as o}from"@polotno/core/fonts";import{containsCmykColor as i,isCmykColor as n,toDisplayColor as s}from"@polotno/core/color";import{isClipGradientSpan as l,nativeColorAttribute as c,readNativeColor as a}from"../utils/rich-text-html.js";let u=!1;export const PRESERVED_INLINE_STYLES=["text-shadow","letter-spacing","word-spacing","-webkit-text-stroke","font-variant","vertical-align","text-decoration-color","text-decoration-style","text-decoration-thickness","text-underline-offset"];export const PRESERVED_BLOCK_STYLES=["text-align","line-height","margin-top","margin-bottom","text-indent","list-style-type"];function p(t){const e=t.keyName,r=c(e),o=t.add.bind(t),i=t.remove.bind(t),l=t.value.bind(t);t.add=(t,e)=>n(e)?!!o(t,s(e))&&(t.setAttribute(r,e),!0):(t.removeAttribute(r),o(t,e)),t.remove=t=>{t.removeAttribute(r),i(t)},t.value=t=>a(t,e)||l(t)}const m=["background-image","background-size","-webkit-background-clip","background-clip","-webkit-text-fill-color","color"],d=c("background-image");export function registerQuillUnrestrictedFontFormats(){if(u){return}u=!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 g=t.import("parchment");if((null==g?void 0:g.StyleAttributor)&&(null==g?void 0:g.Scope)){const r=new g.StyleAttributor("font-weight","font-weight",{scope:g.Scope.INLINE});t.register(r,!0),t.register(new g.StyleAttributor("text-transform","text-transform",{scope:g.Scope.INLINE}),!0);const o=(e,r)=>t.register(new g.StyleAttributor(e,e,{scope:r}),!0);PRESERVED_INLINE_STYLES.forEach(t=>o(t,g.Scope.INLINE)),PRESERVED_BLOCK_STYLES.forEach(t=>o(t,g.Scope.BLOCK)),t.register(function(t){class r extends t.StyleAttributor{constructor(t,e,r){super(t,e,r)}add(t,r){if("string"!=typeof r||!r.includes("gradient")){return!1}t.style.setProperty("background-image",s(r)),i(r)?t.setAttribute(d,r):t.removeAttribute(d),t.style.setProperty("background-size","100% 100%"),t.style.setProperty("-webkit-background-clip","text"),t.style.setProperty("background-clip","text"),t.style.setProperty("-webkit-text-fill-color","transparent");try{t.style.setProperty("color",e(r))}catch(o){}return!0}remove(t){t.removeAttribute(d);for(const e of m){t.style.removeProperty(e)}}value(t){return l(t)?a(t,"background-image")||t.style.getPropertyValue("background-image"):""}}return new r("gradient","background-image",{scope:t.Scope.INLINE})}(g),!0);const n=t.import("attributors/style/color");if(n){p(n);const e=n.value.bind(n);n.value=t=>l(t)?"":e(t),t.register(n,!0)}const c=t.import("attributors/style/background");c&&(p(c),t.register(c,!0))}}
|
package/canvas/rich-editor.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(o[n]=t[n])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(t);r<n.length;r++){e.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(t,n[r])&&(o[n[r]]=t[n[r]])}}return o};import{jsx as e}from"react/jsx-runtime";import o from"react";import n from"quill";import*as r from"mobx";import{nodeReaction as i}from"../utils/mobx.js";import{isAlive as l}from"mobx-state-tree";import{geckoRtlOverhangCss as s,getDir as a,getMeasureCtx as c,getQuillEditorHtml as d,letterSpacingOverhang as u,OVERHANG_PROPERTY as f,textLayoutWidth as p}from"@polotno/core/text-html";import{quillHtmlToModelText as m,isQuillFormatMixed as h,isBoldWeight as g,detachNativeColorsInHtml as b}from"../utils/rich-text-html.js";import{toDisplayColor as
|
|
1
|
+
var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(o[n]=t[n])}if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(n=Object.getOwnPropertySymbols(t);r<n.length;r++){e.indexOf(n[r])<0&&Object.prototype.propertyIsEnumerable.call(t,n[r])&&(o[n[r]]=t[n[r]])}}return o};import{jsx as e}from"react/jsx-runtime";import o from"react";import n from"quill";import*as r from"mobx";import{nodeReaction as i}from"../utils/mobx.js";import{isAlive as l}from"mobx-state-tree";import{geckoRtlOverhangCss as s,getDir as a,getMeasureCtx as c,getQuillEditorHtml as d,letterSpacingOverhang as u,OVERHANG_PROPERTY as f,textLayoutWidth as p}from"@polotno/core/text-html";import{quillHtmlToModelText as m,isQuillFormatMixed as h,isBoldWeight as g,detachNativeColorsInHtml as b}from"../utils/rich-text-html.js";import{toDisplayColor as y}from"@polotno/core/color";import{LIST_INDENT_EM as v}from"@polotno/core/css-values";import{resetStyleContent as x,wrapHtmlForRenderTag as w}from"@polotno/core/reset-style";import{layout as S,lineBaselineOffset as k}from"render-tag";import{removeTags as T}from"@polotno/core/text";import{cssFontFamily as E}from"@polotno/core/fonts";import{isGradient as q,firstStopColor as C}from"@polotno/core/gradient";import{getOptimalCaretColor as O}from"./text-element/caret-color.js";import{visiblePlaceholder as R}from"./text-element/placeholder.js";import{registerQuillUnrestrictedFontFormats as L,PRESERVED_INLINE_STYLES as F,PRESERVED_BLOCK_STYLES as $}from"./quill-font-formats.js";import{ALL_QUILL_MIXED_KEYS as N,setActiveTextEditor as z,updateActiveTextFormat as j,clearActiveTextEditor as M,getActiveTextEditor as P}from"../utils/text-format-state.js";import D from"../utils/styled.js";import{applyOpacityToColor as H}from"@polotno/core/svg";import{elementDropShadow as A,shadowCastByBackground as I}from"@polotno/core/filters";export{H as applyOpacityToColor};export const quillRef=r.observable({enabled:!1,currentFormat:{},pendingToolbarInteraction:!1,editor:r.observable.object({instance:null},{},{deep:!1})});const Q=N;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 Q){h(n,r)&&(o[r]=!0)}return o}(t,e);r.runInAction(()=>{quillRef.currentFormat=o}),j(o,n,e)}const _="polotno-saved-selection";function W(){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(_,new Highlight(e)),V()}function B(){"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.delete(_)}function V(){setTimeout(()=>{"undefined"!=typeof CSS&&CSS.highlights&&CSS.highlights.has(_)&&(document.querySelector(".polotno-portal")||it(document.activeElement)?V():B())},300)}if("undefined"!=typeof document&&"undefined"!=typeof CSS&&CSS.highlights&&!document.querySelector("[data-polotno-highlight]")){const lt=document.createElement("style");lt.setAttribute("data-polotno-highlight",""),lt.textContent=`::highlight(${_}) { background-color: Highlight; }`,document.head.appendChild(lt)}export function collectRTText(t,e){"text"===t.type?e.push(t):t.children.forEach(t=>collectRTText(t,e))}export function computeMarkerVars(t){var e,o,n;const r=t.fontSize;if(!r||r<=0){return null}const i=E(t.fontFamily),l=g(t.fontWeight)?700:400,s="italic"===t.fontStyle?"italic":"normal",a=c();a.fontKerning="normal",a.font=`${r}px ${i}`;const d=a.measureText("M"),u=(null!==(e=d.fontBoundingBoxAscent)&&void 0!==e?e:0)/r,f=(null!==(o=d.fontBoundingBoxDescent)&&void 0!==o?o:0)/r,p=`font-family:${i};font-size:${r}px;font-weight:${l};font-style:${s};line-height:${t.lineHeight};letter-spacing:${t.letterSpacing}em`,m=w(`<div style="${p}"><ul><li>x</li></ul><ol><li>x</li></ol></div>`);let h;try{h=S({html:m,width:Math.max(12*r,600)})}catch(R){return null}const b=[];collectRTText(h.layoutRoot,b);const y=b.find(t=>"•"===t.text),x=b.find(t=>/^\d/.test(t.text));if(!y){return null}const T=y.style.fontSize,q=y.x,C=y.y-k(T,T*u,T*f),O=v*r;return{bulletLeft:q,bulletTop:C,bulletFontSize:T,numberGap:x?Math.max(0,O-x.x-(null!==(n=x.width)&&void 0!==n?n:0)):0}}export const RichTextContainer=D("div",o.forwardRef)`
|
|
2
2
|
.ql-editor {
|
|
3
3
|
outline: none;
|
|
4
4
|
/* anchor for the absolutely-positioned ::before placeholder below */
|
|
@@ -62,7 +62,7 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
|
|
|
62
62
|
}
|
|
63
63
|
.ql-editor li {
|
|
64
64
|
position: relative; /* anchor for the absolutely-positioned ::before */
|
|
65
|
-
padding-inline-start: ${
|
|
65
|
+
padding-inline-start: ${v}em; /* marker space + per-level indent (start side) */
|
|
66
66
|
}
|
|
67
67
|
.ql-editor li[data-list='ordered'] {
|
|
68
68
|
counter-increment: list-0;
|
|
@@ -89,7 +89,7 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
|
|
|
89
89
|
inset-inline-start: 0;
|
|
90
90
|
top: 0;
|
|
91
91
|
box-sizing: border-box;
|
|
92
|
-
width: ${
|
|
92
|
+
width: ${v}em;
|
|
93
93
|
text-align: end;
|
|
94
94
|
}
|
|
95
95
|
.ql-editor li[data-list='ordered']::before {
|
|
@@ -166,4 +166,4 @@ var t=this&&this.__rest||function(t,e){var o={};for(var n in t){Object.prototype
|
|
|
166
166
|
.ql-editor [style*='background-clip'] {
|
|
167
167
|
-webkit-text-stroke-width: 0;
|
|
168
168
|
}
|
|
169
|
-
`;const G=[...O,...$,"header"];let V=["background","font-weight","color","gradient","font","italic","size","script","strike","underline","text-transform","indent","list","direction",...G];export const setQuillFormats=t=>{V=t};let Y=!1;function U(){Y||(Y=!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 K(t){return t?"700":"400"}const J=n.import("delta"),Z="data-polotno-rich-text";export const createQuill=(e,o)=>{L();const r=new n(e,{toolbar:!1,keyboard:!1,placeholder:(null==o?void 0:o.placeholder)||void 0,clipboard:{matchVisual:!1},formats:V});!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?(U(),o("font-weight",K(e),n)):o(t,e,n),e.formatText=(e,o,r,i,l)=>{if(r&&"object"==typeof r){if("bold"in r){U();const{bold:l}=r,s=t(r,["bold"]);return n(e,o,Object.assign(Object.assign({},s),{"font-weight":K(l)}),i)}return n(e,o,r,i)}return"bold"===r?(U(),n(e,o,"font-weight",K(i),l)):n(e,o,r,i,l)},e.getFormat=(...t)=>{const e=r(...t);return e&&null!=e["font-weight"]&&!("bold"in e)&&(e.bold=g(e["font-weight"])),e}}(r);const i=(t,e)=>e.compose((new J).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(Z)){return ot(r,()=>s(t,e))}const n=l.convert;l.convert=(...t)=>((t,e)=>{for(const o of t.ops){if(o.attributes){for(const t of e){delete o.attributes[t]}}}return t})(n.apply(l,t),G);try{return s(t,e)}finally{l.convert=n}},r};const tt=(t,e)=>e.insert(t.data),et=(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 J).delete(1)):e};function ot(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,tt]),o.push([Node.ELEMENT_NODE,et]);try{return e()}finally{o.pop(),-1!==n&&(o[n]=r)}}export const setQuillContent=(t,e)=>{const o=ot(t,()=>t.clipboard.convert({html:"<div class='ql-editor' style='outline: none;'>"+e+"<p><br></p></div>",text:""}));t.setContents(o),t.history.clear()};export const editorHtml=t=>d({text:b(t.text),width:t.width,fontSize:t.fontSize,fontFamily:t.fontFamily},t.fontFamily);export const RichEditor=({onBlur:t,onChange:n,element:s,clickCoords:c,initialCaret:d="start",fitScale:h=1,fitScaleFor:g})=>{const b=o.useRef(null),y=o.useRef(!1),x=o.useRef(h);x.current=h;const[w,S]=o.useState(null),k=null!=w?w:h,L=o.useRef(g);L.current=g;const O=t=>({width:p(s.a.width/t,s.letterSpacing,s.fontSize),zoom:1!==t?t:void 0}),$=o.useRef(null),N=o.useRef(void 0),j=()=>`${s.text}\0${s.fontSize}\0${s.fontFamily}`,D=t=>m(t,{fontSize:s.fontSize,fontFamily:s.fontFamily}),H=t=>{setQuillContent(t,editorHtml(s)),$.current=D(t.root.innerHTML),N.current=j()},Q=o.useRef(()=>{}),_=()=>{Q.current(),t()};o.useLayoutEffect(()=>{let t,e,o=!1,i=null,a=null,u=null,f=null,p=null;const m=()=>{if(!o&&b.current){try{t=createQuill(b.current,{placeholder:F(s.placeholder)}),r.runInAction(()=>{quillRef.editor.instance=t}),quillRef.pendingToolbarInteraction=!1,window.__polotnoQuill=t,z({instance:t,store:s.store,element:s});const o=e=>{e&&syncQuillFormatState(t,e)},m=()=>{let e=t.root;if(e.querySelector(".ql-cursor")){e=e.cloneNode(!0);const t=e.querySelector(".ql-cursor");t.replaceWith(t.textContent.replace(/\uFEFF/g,""))}return D(e.innerHTML)},h=()=>{const t=L.current;t&&l(s)&&(t=>{const e=b.current;if(!e){return}const o=O(t);e.style.width=`${o.width}px`,e.style.zoom=void 0===o.zoom?"":String(o.zoom)})(t(m()))},g=()=>{var o,r;if(clearTimeout(e),e=void 0,!l(s)){return}const i=m();if(i===$.current){return}$.current=i,y.current=!0,n(i);const a=t.getContents(),c=s.fill;let d=null,u=!0;for(const t of a.ops){if("string"!=typeof t.insert||"\n"===t.insert){continue}if(null===(o=t.attributes)||void 0===o?void 0:o.gradient){u=!1;break}const e=(null===(r=t.attributes)||void 0===r?void 0:r.color)||c;if(null===d){d=e}else if(e!==d){u=!1;break}}u&&d&&d!==s.fill&&s.set({fill:d})};Q.current=()=>{(void 0!==e||t.composition.isComposing)&&g()},t.on("text-change",()=>{const n=P(),r=n&&n.instance===t?n.lastSelection:null;o(t.getSelection()||r),t.composition.isComposing||h(),void 0===e&&(e=setTimeout(g,10))}),t.on("selection-change",o),t.on("composition-start",()=>S(x.current)),t.on("composition-end",()=>{h(),S(null)}),H(t);const v=b.current.closest(".polotno-workspace-inner"),w=null==v?void 0:v.scrollLeft,k=null==v?void 0:v.scrollTop,T="end"===d?t.getLength()-1:0;c?setCursorFromCoords(t,c,T):t.setSelection(T,0,"api"),v&&void 0!==w&&void 0!==k&&(v.scrollLeft=w,v.scrollTop=k);const q=t.root;f=t=>{const e=t.relatedTarget;(null==e?void 0:e.classList.contains("ql-clipboard"))||(nt(e)||quillRef.pendingToolbarInteraction?W():setTimeout(()=>{quillRef.pendingToolbarInteraction||nt(document.activeElement)?W():(B(),_())},0))},p=()=>{B()},q.addEventListener("blur",f),q.addEventListener("focus",p),i=q,a=e=>{var o,n;const r=t.getSelection();if(!r||0===r.length){return}const i=t.getText(r.index,r.length).replace(/\n$/,""),l=window.getSelection();let s="";if(l&&l.rangeCount>0){const t=l.getRangeAt(0);let e=t.cloneContents(),o=t.startContainer;o&&o.nodeType===Node.TEXT_NODE&&(o=o.parentNode);let n=t.endContainer;if(n&&n.nodeType===Node.TEXT_NODE&&(n=n.parentNode),o&&o===n){let t=o;for(;t&&t!==q&&t.nodeType===Node.ELEMENT_NODE;){const o=t.tagName;if("SPAN"!==o&&"EM"!==o&&"STRONG"!==o&&"U"!==o&&"S"!==o&&"A"!==o){break}const n=t.cloneNode(!1);n.appendChild(e),e=n,t=t.parentNode}}const r=document.createElement("div");r.appendChild(e),s=r.innerHTML}if(null===(o=e.clipboardData)||void 0===o||o.setData("text/plain",i),s){const t=`<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>\x3c!--StartFragment--\x3e<div ${Z} style="white-space: pre-wrap; overflow-wrap: break-word;">`+s+"</div>\x3c!--EndFragment--\x3e</body></html>";null===(n=e.clipboardData)||void 0===n||n.setData("text/html",t)}e.preventDefault()},u=e=>{a&&a(e);const o=t.getSelection();o&&o.length>0&&t.deleteText(o.index,o.length)},i.addEventListener("copy",a),i.addEventListener("cut",u)}catch(m){console.error("RichEditor init failed:",m)}}};return b.current?m():queueMicrotask(m),()=>{o=!0,Q.current(),clearTimeout(e),i&&f&&i.removeEventListener("blur",f),i&&p&&i.removeEventListener("focus",p),B(),i&&a&&u&&(i.removeEventListener("copy",a),i.removeEventListener("cut",u)),r.runInAction(()=>{quillRef.editor.instance=null,quillRef.currentFormat={}}),M(t),delete window.__polotnoQuill}},[]),o.useEffect(()=>i(s,j,t=>{const e=quillRef.editor.instance;if(!e){return}if(y.current){return y.current=!1,void(N.current=t)}if(t===N.current){return}const o=e.getSelection();H(e),o&&(e.setSelection(o.index,o.length),syncQuillFormatState(e,e.getSelection()))},{fireImmediately:!0}),[]),o.useEffect(()=>i(s,()=>`${s.fontFamily}\0${s.fontSize}\0${s.fontWeight}\0${s.fontStyle}\0${s.lineHeight}\0${s.letterSpacing}\0${a(T(s.text))}`,()=>{const t=b.current;if(!t){return}const e="ltr"===a(T(s.text)),o=computeMarkerVars(s);o&&t.style.setProperty("--rt-number-gap",`${o.numberGap}px`),o&&e?(t.style.setProperty("--rt-bullet-left",`${o.bulletLeft}px`),t.style.setProperty("--rt-bullet-top",`${o.bulletTop}px`),t.style.setProperty("--rt-bullet-fs",`${o.bulletFontSize}px`),t.classList.add("rt-marker-geom")):t.classList.remove("rt-marker-geom")},{fireImmediately:!0}),[]),o.useEffect(()=>{window.addEventListener("blur",_);const t=t=>{var e;const o=t.target;"number"==typeof(null==o?void 0:o.nodeType)&&(null===(e=b.current)||void 0===e?void 0:e.contains(o))||nt(o)||quillRef.pendingToolbarInteraction||(B(),_())};return window.addEventListener("mousedown",t),window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",_),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t)}},[]);const X=v(s.fill),G={color:X};X.indexOf("gradient")>=0&&(G["--polotno-gradient"]=X,G.color="transparent"),s.strokeWidth>0&&(G.WebkitTextStroke=`${s.strokeWidth}px ${C(s.stroke)}`,G.paintOrder="stroke fill");const V=E(s.fill)?C(s.fill):R(s),Y=s.shadowEnabled&&!I(s);return e(RichTextContainer,{ref:b,style:Object.assign(Object.assign(Object.assign(Object.assign({},G),{fontSize:s.fontSize,fontFamily:q(s.fontFamily),fontWeight:s.fontWeight,fontStyle:s.fontStyle,"--polotno-text-decoration":s.textDecoration||"none",textTransform:s.textTransform}),O(k)),{[f]:u(s.letterSpacing,s.fontSize)+"px",lineHeight:s.lineHeight,letterSpacing:s.letterSpacing*s.fontSize+"px",textAlign:s.align,opacity:Math.max(s.a.opacity,.2),filter:Y?A({shadowOffsetX:s.shadowOffsetX,shadowOffsetY:s.shadowOffsetY,shadowBlur:s.shadowBlur,shadowColor:s.shadowColor,shadowOpacity:s.shadowOpacity,rotation:s.a.rotation}):void 0,caretColor:V}),dir:a(T(s.text))})};function nt(t){const e=t;return!("function"!=typeof(null==e?void 0:e.closest)||!e.closest(".sketch-picker")&&!e.closest("[data-polotno-keep-focus]")&&!e.closest(".polotno-portal")&&!e.closest(".polotno-toolbar"))}export function setCursorFromCoords(t,e,o=0){if(!t||!e){return}const{x:r,y:i}=e;try{let e=null;if(document.caretRangeFromPoint){e=document.caretRangeFromPoint(r,i)}else if(document.caretPositionFromPoint){const t=document.caretPositionFromPoint(r,i);t&&(e=document.createRange(),e.setStart(t.offsetNode,t.offset))}if(e){const o=n.find(e.startContainer,!0);if(o){const n=o.offset(t.scroll)+e.startOffset;return void t.setSelection(n,0,"api")}}}catch(l){}t.setSelection(o,0,"api")}
|
|
169
|
+
`;const X=[...F,...$,"header"];let G=["background","font-weight","color","gradient","font","italic","size","script","strike","underline","text-transform","indent","list","direction",...X];export const setQuillFormats=t=>{G=t};let Y=!1;function U(){Y||(Y=!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 K(t){return t?"700":"400"}const J=n.import("delta"),Z="data-polotno-rich-text",tt=["text-decoration-color","text-decoration-style","text-decoration-thickness"],et=(t,e)=>{const{style:o}=t,n=o.textDecorationLine,r={};if(n.includes("underline")&&(r.underline=!0),n.includes("line-through")&&(r.strike=!0),!r.underline&&!r.strike){return e}for(const i of tt){const t=o.getPropertyValue(i);t&&!/^(initial|currentcolor|solid|auto)$/i.test(t)&&(r[i]=t)}return e.ops.reduce((t,e)=>void 0===e.insert?t:t.insert(e.insert,Object.assign(Object.assign({},r),e.attributes)),new J)};export const createQuill=(e,o)=>{L();const r=new n(e,{toolbar:!1,keyboard:!1,placeholder:(null==o?void 0:o.placeholder)||void 0,clipboard:{matchVisual:!1},formats:G});!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?(U(),o("font-weight",K(e),n)):o(t,e,n),e.formatText=(e,o,r,i,l)=>{if(r&&"object"==typeof r){if("bold"in r){U();const{bold:l}=r,s=t(r,["bold"]);return n(e,o,Object.assign(Object.assign({},s),{"font-weight":K(l)}),i)}return n(e,o,r,i)}return"bold"===r?(U(),n(e,o,"font-weight",K(i),l)):n(e,o,r,i,l)},e.getFormat=(...t)=>{const e=r(...t);return e&&null!=e["font-weight"]&&!("bold"in e)&&(e.bold=g(e["font-weight"])),e}}(r);const i=(t,e)=>e.compose((new J).retain(e.length(),{"font-weight":"700"}));r.clipboard.addMatcher("STRONG",i),r.clipboard.addMatcher("B",i),r.clipboard.addMatcher(Node.ELEMENT_NODE,et);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(Z)){return rt(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),X);try{return s(t,e)}finally{l.convert=n}},r};const ot=(t,e)=>e.insert(t.data),nt=(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 J).delete(1)):e};function rt(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,ot]),o.push([Node.ELEMENT_NODE,nt]);try{return e()}finally{o.pop(),-1!==n&&(o[n]=r)}}export const setQuillContent=(t,e)=>{const o=rt(t,()=>t.clipboard.convert({html:"<div class='ql-editor' style='outline: none;'>"+e+"<p><br></p></div>",text:""}));t.setContents(o),t.history.clear()};export const editorHtml=t=>d({text:b(t.text),width:t.width,fontSize:t.fontSize,fontFamily:t.fontFamily},t.fontFamily);export const RichEditor=({onBlur:t,onChange:n,element:s,clickCoords:c,initialCaret:d="start",fitScale:h=1,fitScaleFor:g})=>{const b=o.useRef(null),v=o.useRef(!1),x=o.useRef(h);x.current=h;const[w,S]=o.useState(null),k=null!=w?w:h,L=o.useRef(g);L.current=g;const F=t=>({width:p(s.a.width/t,s.letterSpacing,s.fontSize),zoom:1!==t?t:void 0}),$=o.useRef(null),N=o.useRef(void 0),j=()=>`${s.text}\0${s.fontSize}\0${s.fontFamily}`,D=t=>m(t,{fontSize:s.fontSize,fontFamily:s.fontFamily}),H=t=>{setQuillContent(t,editorHtml(s)),$.current=D(t.root.innerHTML),N.current=j()},Q=o.useRef(()=>{}),_=()=>{Q.current(),t()};o.useLayoutEffect(()=>{let t,e,o=!1,i=null,a=null,u=null,f=null,p=null;const m=()=>{if(!o&&b.current){try{t=createQuill(b.current,{placeholder:R(s.placeholder)}),r.runInAction(()=>{quillRef.editor.instance=t}),quillRef.pendingToolbarInteraction=!1,window.__polotnoQuill=t,z({instance:t,store:s.store,element:s});const o=e=>{e&&syncQuillFormatState(t,e)},m=()=>{let e=t.root;if(e.querySelector(".ql-cursor")){e=e.cloneNode(!0);const t=e.querySelector(".ql-cursor");t.replaceWith(t.textContent.replace(/\uFEFF/g,""))}return D(e.innerHTML)},h=()=>{const t=L.current;t&&l(s)&&(t=>{const e=b.current;if(!e){return}const o=F(t);e.style.width=`${o.width}px`,e.style.zoom=void 0===o.zoom?"":String(o.zoom)})(t(m()))},g=()=>{var o,r;if(clearTimeout(e),e=void 0,!l(s)){return}const i=m();if(i===$.current){return}$.current=i,v.current=!0,n(i);const a=t.getContents(),c=s.fill;let d=null,u=!0;for(const t of a.ops){if("string"!=typeof t.insert||"\n"===t.insert){continue}if(null===(o=t.attributes)||void 0===o?void 0:o.gradient){u=!1;break}const e=(null===(r=t.attributes)||void 0===r?void 0:r.color)||c;if(null===d){d=e}else if(e!==d){u=!1;break}}u&&d&&d!==s.fill&&s.set({fill:d})};Q.current=()=>{(void 0!==e||t.composition.isComposing)&&g()},t.on("text-change",()=>{const n=P(),r=n&&n.instance===t?n.lastSelection:null;o(t.getSelection()||r),t.composition.isComposing||h(),void 0===e&&(e=setTimeout(g,10))}),t.on("selection-change",o),t.on("composition-start",()=>S(x.current)),t.on("composition-end",()=>{h(),S(null)}),H(t);const y=b.current.closest(".polotno-workspace-inner"),w=null==y?void 0:y.scrollLeft,k=null==y?void 0:y.scrollTop,T="end"===d?t.getLength()-1:0;c?setCursorFromCoords(t,c,T):t.setSelection(T,0,"api"),y&&void 0!==w&&void 0!==k&&(y.scrollLeft=w,y.scrollTop=k);const E=t.root;f=t=>{const e=t.relatedTarget;(null==e?void 0:e.classList.contains("ql-clipboard"))||(it(e)||quillRef.pendingToolbarInteraction?W():setTimeout(()=>{quillRef.pendingToolbarInteraction||it(document.activeElement)?W():(B(),_())},0))},p=()=>{B()},E.addEventListener("blur",f),E.addEventListener("focus",p),i=E,a=e=>{var o,n;const r=t.getSelection();if(!r||0===r.length){return}const i=t.getText(r.index,r.length).replace(/\n$/,""),l=window.getSelection();let s="";if(l&&l.rangeCount>0){const t=l.getRangeAt(0);let e=t.cloneContents(),o=t.startContainer;o&&o.nodeType===Node.TEXT_NODE&&(o=o.parentNode);let n=t.endContainer;if(n&&n.nodeType===Node.TEXT_NODE&&(n=n.parentNode),o&&o===n){let t=o;for(;t&&t!==E&&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 ${Z} style="white-space: pre-wrap; overflow-wrap: break-word;">`+s+"</div>\x3c!--EndFragment--\x3e</body></html>";null===(n=e.clipboardData)||void 0===n||n.setData("text/html",t)}e.preventDefault()},u=e=>{a&&a(e);const o=t.getSelection();o&&o.length>0&&t.deleteText(o.index,o.length)},i.addEventListener("copy",a),i.addEventListener("cut",u)}catch(m){console.error("RichEditor init failed:",m)}}};return b.current?m():queueMicrotask(m),()=>{o=!0,Q.current(),clearTimeout(e),i&&f&&i.removeEventListener("blur",f),i&&p&&i.removeEventListener("focus",p),B(),i&&a&&u&&(i.removeEventListener("copy",a),i.removeEventListener("cut",u)),r.runInAction(()=>{quillRef.editor.instance=null,quillRef.currentFormat={}}),M(t),delete window.__polotnoQuill}},[]),o.useEffect(()=>i(s,j,t=>{const e=quillRef.editor.instance;if(!e){return}if(v.current){return v.current=!1,void(N.current=t)}if(t===N.current){return}const o=e.getSelection();H(e),o&&(e.setSelection(o.index,o.length),syncQuillFormatState(e,e.getSelection()))},{fireImmediately:!0}),[]),o.useEffect(()=>i(s,()=>`${s.fontFamily}\0${s.fontSize}\0${s.fontWeight}\0${s.fontStyle}\0${s.lineHeight}\0${s.letterSpacing}\0${a(T(s.text))}`,()=>{const t=b.current;if(!t){return}const e="ltr"===a(T(s.text)),o=computeMarkerVars(s);o&&t.style.setProperty("--rt-number-gap",`${o.numberGap}px`),o&&e?(t.style.setProperty("--rt-bullet-left",`${o.bulletLeft}px`),t.style.setProperty("--rt-bullet-top",`${o.bulletTop}px`),t.style.setProperty("--rt-bullet-fs",`${o.bulletFontSize}px`),t.classList.add("rt-marker-geom")):t.classList.remove("rt-marker-geom")},{fireImmediately:!0}),[]),o.useEffect(()=>{window.addEventListener("blur",_);const t=t=>{var e;const o=t.target;"number"==typeof(null==o?void 0:o.nodeType)&&(null===(e=b.current)||void 0===e?void 0:e.contains(o))||it(o)||quillRef.pendingToolbarInteraction||(B(),_())};return window.addEventListener("mousedown",t),window.addEventListener("touchstart",t),()=>{window.removeEventListener("blur",_),window.removeEventListener("mousedown",t),window.removeEventListener("touchstart",t)}},[]);const V=y(s.fill),X={color:V};V.indexOf("gradient")>=0&&(X["--polotno-gradient"]=V,X.color="transparent"),s.strokeWidth>0&&(X.WebkitTextStroke=`${s.strokeWidth}px ${C(s.stroke)}`,X.paintOrder="stroke fill");const G=q(s.fill)?C(s.fill):O(s),Y=s.shadowEnabled&&!I(s);return e(RichTextContainer,{ref:b,style:Object.assign(Object.assign(Object.assign(Object.assign({},X),{fontSize:s.fontSize,fontFamily:E(s.fontFamily),fontWeight:s.fontWeight,fontStyle:s.fontStyle,"--polotno-text-decoration":s.textDecoration||"none",textTransform:s.textTransform}),F(k)),{[f]:u(s.letterSpacing,s.fontSize)+"px",lineHeight:s.lineHeight,letterSpacing:s.letterSpacing*s.fontSize+"px",textAlign:s.align,opacity:Math.max(s.a.opacity,.2),filter:Y?A({shadowOffsetX:s.shadowOffsetX,shadowOffsetY:s.shadowOffsetY,shadowBlur:s.shadowBlur,shadowColor:s.shadowColor,shadowOpacity:s.shadowOpacity,rotation:s.a.rotation}):void 0,caretColor:G}),dir:a(T(s.text))})};function it(t){const e=t;return!("function"!=typeof(null==e?void 0:e.closest)||!e.closest(".sketch-picker")&&!e.closest("[data-polotno-keep-focus]")&&!e.closest(".polotno-portal")&&!e.closest(".polotno-toolbar"))}export function setCursorFromCoords(t,e,o=0){if(!t||!e){return}const{x:r,y:i}=e;try{let e=null;if(document.caretRangeFromPoint){e=document.caretRangeFromPoint(r,i)}else if(document.caretPositionFromPoint){const t=document.caretPositionFromPoint(r,i);t&&(e=document.createRange(),e.setStart(t.offsetNode,t.offset))}if(e){const o=n.find(e.startContainer,!0);if(o){const n=o.offset(t.scroll)+e.startOffset;return void t.setSelection(n,0,"api")}}}catch(l){}t.setSelection(o,0,"api")}
|
package/canvas/use-color.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"react";import*as
|
|
1
|
+
import t from"react";import*as i from"@polotno/core/gradient";import{toDisplayColor as o}from"@polotno/core/color";const r=Object.keys({id:!0,text:!0,width:!0,height:!0,fontSize:!0,fontFamily:!0,fontWeight:!0,fontStyle:!0,lineHeight:!0,letterSpacing:!0,textTransform:!0,textDecoration:!0,align:!0,fill:!0,stroke:!0,strokeWidth:!0,strokeLineJoin:!0,curveEnabled:!0,curvePower:!0,shadowBlur:!0,shadowOffsetX:!0,shadowOffsetY:!0,textOverflow:!0,maxLines:!0,minFontSize:!0,maxFontSize:!0,a:!0});export const withDisplayTextPaints=t=>{const i={};for(const e of r){e in t&&(i[e]="fill"===e||"stroke"===e?o(t[e]):t[e])}return i};export const useColor=(r,e,n)=>{var a;return void 0===e&&(e=null!==(a=r.fill)&&void 0!==a?a:""),void 0===n&&(n="fill"),t.useMemo(()=>{if(!i.isGradient(e)){return{[n]:o(e)}}const{stops:t,rotation:a}=i.parseColor(e),{start:l,end:s}=i.canvasGradientPoints(a,r.a.width,r.a.height),d=[];return t.forEach(({offset:t,color:i})=>{d.push(t,i)}),"fill"===n?{fillLinearGradientStartPointX:l.x,fillLinearGradientStartPointY:l.y,fillLinearGradientColorStops:d,fillLinearGradientEndPointX:s.x,fillLinearGradientEndPointY:s.y,fill:t[1].color,fillPriority:"linear-gradient"}:{strokeLinearGradientStartPointX:l.x,strokeLinearGradientStartPointY:l.y,strokeLinearGradientColorStops:d,strokeLinearGradientEndPointX:s.x,strokeLinearGradientEndPointY:s.y}},[e,r.width,r.height])};
|
package/canvas/video-element.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Fragment as e,jsx as t,jsxs as r}from"react/jsx-runtime";import{elementOpacity as o}from"./paint-opacity.js";import i from"react";import{toDisplayColor as n}from"@polotno/core/color";import{animatedShadow as a}from"./animated-shadow.js";import{observer as s}from"mobx-react-lite";import{isAlive as d}from"mobx-state-tree";import{nodeAutorun as c}from"../utils/mobx.js";import{Group as h,Image as l,Rect as u}from"react-konva";import{ROLES as m}from"../model/store.js";import{DEFAULT_RETRIES as p,isDeterministicUrl as g,retryDelayMs as f}from"@polotno/core/retry";import v from"konva";import{ErrorPlaceholder as w,LoadingPlaceholder as x}from"./media-placeholders.js";import{incrementLoader as E,triggerLoadError as M}from"../utils/loader.js";import{polotnoError as L}from"@polotno/core/errors";import{getCropRect as y}from"@polotno/core/image";import*as C from"@polotno/core/svg";import{activeAnchorOf as b,roundedCropFraction as T,roundedGeometryPx as k,roundedPosition as j,isCornerAnchor as R,scaledStyleAttrs as I,roundedStyleAttrs as S,transformScale as P}from"../utils/px.js";import{useFilteredSource as H}from"./use-filtered-source.js";import{useOffscreenCanvas as W}from"./use-offscreen-canvas.js";import{useEvent as _}from"../hooks/use-event.js";import{useFadeIn as A}from"./use-fadein.js";import{useAnimatedBlur as X}from"./use-animated-blur.js";import{isTouchDevice as Y}from"../utils/screen.js";import{createPlayGate as V,playMedia as D}from"../utils/media.js";import{useMediabunnyVideo as O}from"./use-mediabunny-video.js";import{getElementTimeRange as F,getVideoRestMs as z,isHeldByPreview as U}from"../utils/timeline.js";import{drawReduced as Z,exportKernelEnabled as N,shouldResample as q,sourceSize as $}from"./downscale.js";import{trySetCanvasSize as B}from"../utils/canvas.js";import{useClipImage as G}from"./image-element.js";import{CropOverlay as J,CROP_CORNER_ANCHORS as K}from"./crop-overlay.js";const Q=new window.Image;Q.src=C.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 ee=new window.Image;ee.src=C.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 useFlip=(e,t)=>{const{flipX:r,flipY:o}=e,{width:i,height:n}=t?$(t):{},a=e=>{if(!t){return}const i=e.getContext("2d");i&&(i.clearRect(0,0,e.width,e.height),i.save(),i.scale(r?-1:1,o?-1:1),i.drawImage(t,r?-e.width:0,o?-e.height:0,e.width,e.height),i.restore())},s=W((r||o)&&!!i&&!!n,e=>{i&&n&&(e.width=i,e.height=n,a(e))},[r,o,t,i,n]);return[s||t,()=>{s&&a(s)}]};export const useCornerRadiusAndCrop=(e,t,r,o,n=0,a=!1)=>{var s;const d=Math.floor(Math.max(e.a.width*o,1)),c=Math.floor(Math.max(e.a.height*o,1)),h=Math.min(n*o,d/2,c/2),l=Math.max(e.a.width/r.width,e.a.height/r.height)*o,u=N(e.page)&&!a&&q(l,d,c),{width:m,height:p}=t?$(t):{},g=0===r.x&&0===r.y&&r.width===m&&r.height===p,f=i.useMemo(()=>{if(t&&m&&p){return g&&0===h&&!u?void 0:document.createElement("canvas")}},[t,h,u,g,m,p]),w=()=>{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 i.useLayoutEffect(()=>{w()},[f,e.a.width,e.a.height,r.x,r.y,r.width,r.height,n,o,a,null===(s=e.page)||void 0===s?void 0:s._exportingOrRendering,u]),i.useEffect(()=>()=>{f&&"CANVAS"===f.nodeName&&v.Util.releaseCanvas(f)},[f]),[f||t,w]};export const useClip=(e,t,r,o)=>{const[n,a]=G(e.clipSrc||""),s=e.clipSrc?a:"loaded";useLoader(s,e.clipSrc,e.id);const d=!!t&&!!n,c=i.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:o,height:i}=$(t);if(!o||!i){return}const n=Math.min(1e4,Math.max(o,Math.ceil(e.a.width*r),1)),a=Math.min(1e4,Math.max(i,Math.ceil(e.a.height*r),1));h.width&&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(t,0,0,h.width,h.height),s.globalCompositeOperation="destination-in",s.drawImage(c.mask,0,0,h.width,h.height),s.restore())}return i.useLayoutEffect(()=>{var t;if(!c||!(null==n?void 0:n.width)||!n.height){return}const o=Math.max(e.a.width/n.width*r,e.a.height/n.height*r);B(c.mask,Math.min(1e4,Math.max(n.width*o,1)),Math.min(1e4,Math.max(n.height*o,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]),i.useLayoutEffect(()=>{l()},[c,t,n,e.a.width,e.a.height,r,...o]),i.useEffect(()=>()=>{c&&v.Util.releaseCanvas(c.canvas,c.mask)},[c]),[d?h:t,l]};const te=document.createElement("canvas");let re=function(e,t,r){const o=i.useRef("loading"),n=i.useRef(void 0),[a,s]=i.useState(0),d=i.useRef(void 0),c=i.useRef(void 0),h=i.useRef(void 0);return d.current===e&&c.current===t&&h.current===r||(o.current="loading",n.current=void 0,d.current=e,c.current=t,h.current=r),i.useLayoutEffect(()=>{if(!e){return}const i=g(e)?1:p;let a,d,c,h=0,l=!1,u=!1;const m=e=>{e&&(e.removeAttribute("src"),e.load())},v=()=>{h+=1;const p=document.createElement("video");function g(){l||(p.removeEventListener("canplay",g),u=!0,o.current="loaded",n.current=p,p.currentTime=0,s(Math.random()))}function w(e){var t;const r=4===p.readyState,c=p.buffered&&p.buffered.length>0&&p.buffered.end(p.buffered.length-1)/p.duration*100>=100;if(r&&c){return}if(l){return}if(!u&&h<i){return null==d||d(),m(p),void(a=setTimeout(v,f(h)))}const g=e.message||(null===(t=p.error)||void 0===t?void 0:t.message)||"Unknown error";console.error(new Error("Video failed to load: "+g)),o.current="failed",n.current=void 0,s(Math.random())}c=p,p.addEventListener("canplay",g),p.addEventListener("error",w),d=()=>{p.removeEventListener("canplay",g),p.removeEventListener("error",w)},t&&(p.crossOrigin=t),r&&(p.referrerPolicy=r),p.src=e,p.load()};return v(),()=>{l=!0,clearTimeout(a),null==d||d(),u||m(c)}},[e,t,r]),[n.current,o.current]};export const setVideoLoaderHook=e=>{re=e};function oe(e){return!e.seeking&&e.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA}export const useLoader=(e,t,r)=>{const o=i.useRef(void 0),n=()=>{var e;null===(e=o.current)||void 0===e||e.call(o),o.current=void 0};i.useEffect(()=>n,[]),i.useLayoutEffect(()=>{const i=t.slice(0,200),a=`video with id ${r} url: ${i}`;"loading"!==e||o.current||(o.current=E(a)),"loading"!==e&&n(),"failed"===e&&M(L("VIDEO_FAILED",a,{src:i,elementId:r,reason:"load"}))},[e])};export const VideoElement=s(({element:s,store:p})=>{var g,f,M,L,C;const[W,Z]=i.useState(!1),N=i.useRef(null),q=Y(),[B,G]=i.useState(!1),[ie,ne]=re(s.src,"anonymous"),[ae,se]=i.useState(!1),de=p.activePage===s.page,ce=(null===(g=s.page)||void 0===g?void 0:g._exportingOrRendering)&&"loaded"===ne,he=O(s,p,ce);useLoader(ne,s.src,s.id),ie&&(ie.playsInline=!0);const le=ce&&he||ie,[ue,me]=useFlip(s,le),[pe=te,ge]=H(s,ue||te);i.useEffect(()=>{ie&&p.history.ignore(()=>{s.set({duration:1e3*ie.duration})})},[ie]);const fe=i.useMemo(V,[ie]);i.useEffect(()=>{var e;if(!ie){return}const t=!U(s)&&p.isPlaying;if(!de||!t&&!B){return void ie.pause()}B&&(ie.muted=!1,ie.currentTime=s.startTime*ie.duration,D(ie).then(e=>{"refused"===e&&G(!1)}));const r=new v.Animation(()=>{Ae()},null===(e=N.current)||void 0===e?void 0:e.getLayer());r.start();const o=()=>{G(!1),ie.currentTime=s.startTime*ie.duration},i=()=>{ie.currentTime>=s.endTime*ie.duration&&(ie.pause(),ie.currentTime=s.startTime*ie.duration,G(!1))};return ie.addEventListener("ended",o),ie.addEventListener("timeupdate",i),()=>{ie.pause(),r.stop(),ie.removeEventListener("ended",o),ie.removeEventListener("timeupdate",i)}},[ie,B,p.isPlaying,de]),i.useEffect(()=>{ie&&(ie.volume=s.volume)},[ie,s.volume]),i.useEffect(()=>{ie&&(ie.playbackRate=s.speed,ie.preservesPitch=!1)},[ie,s.speed]);const ve=i.useRef(null);i.useEffect(()=>{const e=c(s,()=>{var e,t;if(ie){if(ce&&he){return Ae(),void p.currentTime}if(U(s)){return}const r=p.activePage===s.page,o=s.page._mediaLocalTime,i=s.startTime*ie.duration*1e3,n=ie.duration*(s.endTime-s.startTime)*1e3,{start:a,end:d}=F(s.animations,s.page.duration),c=z(null!=o?o:0,0,a,n,i,s.speed,{windowEnd:d,frameMs:p._renderFrameMs,renderEnd:p._renderEndMs-s.page.startTime}),h=c/1e3,l=null!==o&&o>=a&&o<d;B||(ie.muted=!l,l&&p.isPlaying&&r?ie.paused?(ie.currentTime=h,fe.play(ie)):Math.abs(1e3*ie.currentTime-c)>500&&(ie.currentTime=h):(fe.reset(),ie.paused||ie.pause(),h!==ie.currentTime&&(ie.currentTime=h),!oe(ie)&&!p.isPlaying&&!ve.current&&(ve.current=E(`video ${s.id}`)))),Ae(),null===(t=null===(e=N.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()}});return()=>{e(),ve.current&&(ve.current(),ve.current=null)}},[p,ie,ce,he,B]),i.useEffect(()=>{var e,t;if(!ie){return}if(ce){return null===(e=ve.current)||void 0===e||e.call(ve),void(ve.current=null)}const r=()=>{var e;null===(e=ve.current)||void 0===e||e.call(ve),ve.current=null},o=()=>{oe(ie)&&(Ae(),r())};let i,n;(null===(t=s.page)||void 0===t?void 0:t._exportingOrRendering)&&(i=setInterval(o,20)),ie.addEventListener("timeupdate",o),ie.addEventListener("canplay",o),ie.addEventListener("seeked",o);const a=()=>{ie.paused&&(Ae(),oe(ie)&&r()),n=ie.requestVideoFrameCallback(a)};return"requestVideoFrameCallback"in ie&&(n=ie.requestVideoFrameCallback(a)),()=>{clearInterval(i),void 0!==n&&ie.cancelVideoFrameCallback(n),ie.removeEventListener("timeupdate",o),ie.removeEventListener("canplay",o),ie.removeEventListener("seeked",o)}},[null===(f=s.page)||void 0===f?void 0:f._exportingOrRendering,ie,s.id,E,ce]);let{cropX:we,cropY:xe,cropWidth:Ee,cropHeight:Me}=s.a;"loaded"!==ne&&(we=xe=0,Ee=Me=1);const{width:Le,height:ye}=$(pe),Ce=null!==(M=null==ie?void 0:ie.videoWidth)&&void 0!==M?M:0,be=null!==(L=null==ie?void 0:ie.videoHeight)&&void 0!==L?L:0;i.useLayoutEffect(()=>{"loaded"===ne&&Ce>0&&be>0&&s._setSourceSize({width:Ce,height:be},s.src)},[s,s.src,ne,Ce,be]);const Te=y({stretchEnabled:s.stretchEnabled,cropX:we,cropY:xe,cropWidth:Ee,cropHeight:Me,width:s.a.width,height:s.a.height},{width:Le,height:ye}),ke=Te.width,je=Te.height,Re=s.stretchEnabled,Ie=null!==(C=s.cornerRadius)&&void 0!==C?C:0,[Se,Pe]=useCornerRadiusAndCrop(s,pe,Te,p._elementsPixelRatio,Ie,!0);let[He,We]=useClip(s,Se,p._elementsPixelRatio,[Te,Ie,p._elementsPixelRatio]);const _e=X(N,s,He),Ae=_(()=>{var e,t;me(),ge(),Pe(),We(),_e(),null===(t=null===(e=N.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()});i.useEffect(()=>{Ae()});const Xe=()=>{s._cropEnabled&&setTimeout(()=>{d(s)&&s.toggleCropMode(!0)})},Ye="loading"===ne,Ve="failed"===ne,De=!Ye&&!Ve,Oe=i.useRef({cropX:0,cropY:0,cropWidth:0,cropHeight:0}),Fe=i.useRef(!1),ze=De?o(s):0;A(N,ze);const Ue=s.selectable||p.role===m.ADMIN;return t(e,{children:r(h,{onMouseEnter:()=>se(!0),onMouseLeave:()=>se(!1),children:[Ye&&t(x,{element:s}),Ve&&t(w,{element:s,text:"Can not load the video..."}),t(l,Object.assign({ref:N,name:"element",id:s.id,image:He,x:s.a.x,y:s.a.y,width:s.a.width||1,height:s.a.height||1,rotation:s.a.rotation,opacity:ze},a(s),{customCrop:Te,listening:Ue,draggable:q?s.draggable&&s.isSelected:s.draggable,preventDefault:!q||s.isSelected,hideInExport:!s.showInExport,onDragMove:e=>{s.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{s.set(j(e.target))},onDblClick:Xe,onDblTap:Xe,onTransformStart:e=>{Z(!0),Fe.current=R(b(e.target)),Oe.current={cropX:s.cropX,cropY:s.cropY,cropWidth:s.cropWidth,cropHeight:s.cropHeight}},onTransform:e=>{const t=e.currentTarget,r=P(t.scaleX()),o=P(t.scaleY());t.scaleX(1),t.scaleY(1);const i=1-ke/Le,n=Math.min(i,Math.max(0,s.cropX)),a=1-je/ye,d=Math.min(a,Math.max(0,s.cropY)),c=Fe.current,h=!c&&r<1&&Oe.current.cropHeight>je/ye;let l=c?s.cropWidth:s.cropWidth*r;h&&(l=s.cropWidth);const u=!c&&o<1&&Oe.current.cropWidth>ke/Le;let m=c?s.cropHeight:s.cropHeight*o;u&&(m=s.cropHeight),Re&&(l=s.cropWidth,m=s.cropHeight);const p=c?Math.min(r,o):1;s.set(Object.assign({cropX:n,cropY:d,x:t.x(),y:t.y(),width:t.width()*r,height:t.height()*o,rotation:e.target.rotation(),cropWidth:Math.min(l,1-n),cropHeight:Math.min(m,1-d)},1!==p&&I(s,p)))},onTransformEnd:e=>{const t=e.currentTarget;s.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:T(s.cropX),cropY:T(s.cropY),cropWidth:T(ke/Le),cropHeight:T(je/ye)},S(s))),Z(!1)}})),!!s.borderSize&&t(u,{x:s.a.x,y:s.a.y,width:Math.max(s.a.width-s.borderSize,0),height:Math.max(s.a.height-s.borderSize,0),opacity:ze,offsetX:-s.borderSize/2,offsetY:-s.borderSize/2,stroke:n(s.borderColor),strokeWidth:s.borderSize,listening:!1,rotation:s.a.rotation,cornerRadius:Math.max(0,Ie-s.borderSize),hideInExport:!s.showInExport}),!p.isPlaying&&t(l,{image:B?ae||q?ee:void 0:Q,x:s.a.x,y:s.a.y,offsetX:-s.a.width/2+15/p.scale,offsetY:-s.a.height/2+15/p.scale,rotation:s.a.rotation,width:30/p.scale,height:30/p.scale,hideInExport:!0,onClick:()=>G(!B),onTap:()=>G(!B)}),s._cropModeEnabled&&"loaded"===ne&&t(J,{element:s,store:p,previewImage:He,sourceImage:pe,sourceWidth:Le,sourceHeight:ye,cropAbsoluteWidth:ke,cropAbsoluteHeight:je,innerAnchors:K})]})})});
|
|
1
|
+
import{Fragment as e,jsx as t,jsxs as r}from"react/jsx-runtime";import{elementOpacity as o}from"./paint-opacity.js";import i from"react";import{toDisplayColor as n}from"@polotno/core/color";import{animatedShadow as a}from"./animated-shadow.js";import{observer as s}from"mobx-react-lite";import{isAlive as d}from"mobx-state-tree";import{nodeAutorun as l}from"../utils/mobx.js";import{Group as c,Image as u,Rect as h}from"react-konva";import{ROLES as m}from"../model/store.js";import{DEFAULT_RETRIES as p,isDeterministicUrl as g,retryDelayMs as f}from"@polotno/core/retry";import v from"konva";import{ErrorPlaceholder as w,LoadingPlaceholder as x}from"./media-placeholders.js";import{incrementLoader as E,triggerLoadError as L}from"../utils/loader.js";import{polotnoError as M}from"@polotno/core/errors";import{getCropRect as y}from"@polotno/core/image";import*as C from"@polotno/core/svg";import{roundedPosition as b}from"../utils/px.js";import{useMediaResize as k}from"./media-resize.js";import{useFilteredSource as T}from"./use-filtered-source.js";import{useOffscreenCanvas as j}from"./use-offscreen-canvas.js";import{useEvent as I}from"../hooks/use-event.js";import{useFadeIn as R}from"./use-fadein.js";import{useAnimatedBlur as S}from"./use-animated-blur.js";import{isTouchDevice as P}from"../utils/screen.js";import{createPlayGate as _,playMedia as A}from"../utils/media.js";import{useMediabunnyVideo as V}from"./use-mediabunny-video.js";import{getElementTimeRange as D,getVideoRestMs as z,isHeldByPreview as F}from"../utils/timeline.js";import{drawReduced as H,exportKernelEnabled as O,shouldResample as U,sourceSize as W}from"./downscale.js";import{trySetCanvasSize as Z}from"../utils/canvas.js";import{useClipImage as X}from"./image-element.js";import{CropOverlay as Y,CROP_CORNER_ANCHORS as N}from"./crop-overlay.js";const q=new window.Image;q.src=C.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 $=new window.Image;$.src=C.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 useFlip=(e,t)=>{const{flipX:r,flipY:o}=e,{width:i,height:n}=t?W(t):{},a=e=>{if(!t){return}const i=e.getContext("2d");i&&(i.clearRect(0,0,e.width,e.height),i.save(),i.scale(r?-1:1,o?-1:1),i.drawImage(t,r?-e.width:0,o?-e.height:0,e.width,e.height),i.restore())},s=j((r||o)&&!!i&&!!n,e=>{i&&n&&(e.width=i,e.height=n,a(e))},[r,o,t,i,n]);return[s||t,()=>{s&&a(s)}]};export const useCornerRadiusAndCrop=(e,t,r,o,n=0,a=!1)=>{var s;const d=Math.floor(Math.max(e.a.width*o,1)),l=Math.floor(Math.max(e.a.height*o,1)),c=Math.min(n*o,d/2,l/2),u=Math.max(e.a.width/r.width,e.a.height/r.height)*o,h=O(e.page)&&!a&&U(u,d,l),{width:m,height:p}=t?W(t):{},g=0===r.x&&0===r.y&&r.width===m&&r.height===p,f=i.useMemo(()=>{if(t&&m&&p){return g&&0===c&&!h?void 0:document.createElement("canvas")}},[t,c,h,g,m,p]),w=()=>{if(!f||!t){return}f.width=d,f.height=l;const e=f.getContext("2d");e&&(c&&(e.beginPath(),e.moveTo(c,0),e.lineTo(d-c,0),e.arc(d-c,c,c,3*Math.PI/2,0,!1),e.lineTo(d,l-c),e.arc(d-c,l-c,c,0,Math.PI/2,!1),e.lineTo(c,l),e.arc(c,l-c,c,Math.PI/2,Math.PI,!1),e.lineTo(0,c),e.arc(c,c,c,Math.PI,3*Math.PI/2,!1),e.clip()),h?H(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 i.useLayoutEffect(()=>{w()},[f,e.a.width,e.a.height,r.x,r.y,r.width,r.height,n,o,a,null===(s=e.page)||void 0===s?void 0:s._exportingOrRendering,h]),i.useEffect(()=>()=>{f&&"CANVAS"===f.nodeName&&v.Util.releaseCanvas(f)},[f]),[f||t,w]};export const useClip=(e,t,r,o)=>{const[n,a]=X(e.clipSrc||""),s=e.clipSrc?a:"loaded";useLoader(s,e.clipSrc,e.id);const d=!!t&&!!n,l=i.useMemo(()=>d?{canvas:document.createElement("canvas"),mask:document.createElement("canvas"),width:0,height:0}:void 0,[d]),c=null==l?void 0:l.canvas;function u(){if(!l||!c||!t){return}const{width:o,height:i}=W(t);if(!o||!i){return}const n=Math.min(1e4,Math.max(o,Math.ceil(e.a.width*r),1)),a=Math.min(1e4,Math.max(i,Math.ceil(e.a.height*r),1));c.width&&l.width===n&&l.height===a||(Z(c,n,a),l.width=n,l.height=a);const s=c.getContext("2d");s&&(s.clearRect(0,0,c.width,c.height),s.save(),s.drawImage(t,0,0,c.width,c.height),s.globalCompositeOperation="destination-in",s.drawImage(l.mask,0,0,c.width,c.height),s.restore())}return i.useLayoutEffect(()=>{var t;if(!l||!(null==n?void 0:n.width)||!n.height){return}const o=Math.max(e.a.width/n.width*r,e.a.height/n.height*r);Z(l.mask,Math.min(1e4,Math.max(n.width*o,1)),Math.min(1e4,Math.max(n.height*o,1))),null===(t=l.mask.getContext("2d"))||void 0===t||t.drawImage(n,0,0,l.mask.width,l.mask.height)},[l,n,e.a.width,e.a.height,r]),i.useLayoutEffect(()=>{u()},[l,t,n,e.a.width,e.a.height,r,...o]),i.useEffect(()=>()=>{l&&v.Util.releaseCanvas(l.canvas,l.mask)},[l]),[d?c:t,u]};const B=document.createElement("canvas");let G=function(e,t,r){const o=i.useRef("loading"),n=i.useRef(void 0),[a,s]=i.useState(0),d=i.useRef(void 0),l=i.useRef(void 0),c=i.useRef(void 0);return d.current===e&&l.current===t&&c.current===r||(o.current="loading",n.current=void 0,d.current=e,l.current=t,c.current=r),i.useLayoutEffect(()=>{if(!e){return}const i=g(e)?1:p;let a,d,l,c=0,u=!1,h=!1;const m=e=>{e&&(e.removeAttribute("src"),e.load())},v=()=>{c+=1;const p=document.createElement("video");function g(){u||(p.removeEventListener("canplay",g),h=!0,o.current="loaded",n.current=p,p.currentTime=0,s(Math.random()))}function w(e){var t;const r=4===p.readyState,l=p.buffered&&p.buffered.length>0&&p.buffered.end(p.buffered.length-1)/p.duration*100>=100;if(r&&l){return}if(u){return}if(!h&&c<i){return null==d||d(),m(p),void(a=setTimeout(v,f(c)))}const g=e.message||(null===(t=p.error)||void 0===t?void 0:t.message)||"Unknown error";console.error(new Error("Video failed to load: "+g)),o.current="failed",n.current=void 0,s(Math.random())}l=p,p.addEventListener("canplay",g),p.addEventListener("error",w),d=()=>{p.removeEventListener("canplay",g),p.removeEventListener("error",w)},t&&(p.crossOrigin=t),r&&(p.referrerPolicy=r),p.src=e,p.load()};return v(),()=>{u=!0,clearTimeout(a),null==d||d(),h||m(l)}},[e,t,r]),[n.current,o.current]};export const setVideoLoaderHook=e=>{G=e};function J(e){return!e.seeking&&e.readyState>=HTMLMediaElement.HAVE_CURRENT_DATA}export const useLoader=(e,t,r)=>{const o=i.useRef(void 0),n=()=>{var e;null===(e=o.current)||void 0===e||e.call(o),o.current=void 0};i.useEffect(()=>n,[]),i.useLayoutEffect(()=>{const i=t.slice(0,200),a=`video with id ${r} url: ${i}`;"loading"!==e||o.current||(o.current=E(a)),"loading"!==e&&n(),"failed"===e&&L(M("VIDEO_FAILED",a,{src:i,elementId:r,reason:"load"}))},[e])};export const VideoElement=s(({element:s,store:p})=>{var g,f,L,M,C;const[j,H]=i.useState(!1),O=i.useRef(null),U=P(),[Z,X]=i.useState(!1),[K,Q]=G(s.src,"anonymous"),[ee,te]=i.useState(!1),re=p.activePage===s.page,oe=(null===(g=s.page)||void 0===g?void 0:g._exportingOrRendering)&&"loaded"===Q,ie=V(s,p,oe);useLoader(Q,s.src,s.id),K&&(K.playsInline=!0);const ne=oe&&ie||K,[ae,se]=useFlip(s,ne),[de=B,le]=T(s,ae||B);i.useEffect(()=>{K&&p.history.ignore(()=>{s.set({duration:1e3*K.duration})})},[K]);const ce=i.useMemo(_,[K]);i.useEffect(()=>{var e;if(!K){return}const t=!F(s)&&p.isPlaying;if(!re||!t&&!Z){return void K.pause()}Z&&(K.muted=!1,K.currentTime=s.startTime*K.duration,A(K).then(e=>{"refused"===e&&X(!1)}));const r=new v.Animation(()=>{Ie()},null===(e=O.current)||void 0===e?void 0:e.getLayer());r.start();const o=()=>{X(!1),K.currentTime=s.startTime*K.duration},i=()=>{K.currentTime>=s.endTime*K.duration&&(K.pause(),K.currentTime=s.startTime*K.duration,X(!1))};return K.addEventListener("ended",o),K.addEventListener("timeupdate",i),()=>{K.pause(),r.stop(),K.removeEventListener("ended",o),K.removeEventListener("timeupdate",i)}},[K,Z,p.isPlaying,re]),i.useEffect(()=>{K&&(K.volume=s.volume)},[K,s.volume]),i.useEffect(()=>{K&&(K.playbackRate=s.speed,K.preservesPitch=!1)},[K,s.speed]);const ue=i.useRef(null);i.useEffect(()=>{const e=l(s,()=>{var e,t;if(K){if(oe&&ie){return Ie(),void p.currentTime}if(F(s)){return}const r=p.activePage===s.page,o=s.page._mediaLocalTime,i=s.startTime*K.duration*1e3,n=K.duration*(s.endTime-s.startTime)*1e3,{start:a,end:d}=D(s.animations,s.page.duration),l=z(null!=o?o:0,0,a,n,i,s.speed,{windowEnd:d,frameMs:p._renderFrameMs,renderEnd:p._renderEndMs-s.page.startTime}),c=l/1e3,u=null!==o&&o>=a&&o<d;Z||(K.muted=!u,u&&p.isPlaying&&r?K.paused?(K.currentTime=c,ce.play(K)):Math.abs(1e3*K.currentTime-l)>500&&(K.currentTime=c):(ce.reset(),K.paused||K.pause(),c!==K.currentTime&&(K.currentTime=c),!J(K)&&!p.isPlaying&&!ue.current&&(ue.current=E(`video ${s.id}`)))),Ie(),null===(t=null===(e=O.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()}});return()=>{e(),ue.current&&(ue.current(),ue.current=null)}},[p,K,oe,ie,Z]),i.useEffect(()=>{var e,t;if(!K){return}if(oe){return null===(e=ue.current)||void 0===e||e.call(ue),void(ue.current=null)}const r=()=>{var e;null===(e=ue.current)||void 0===e||e.call(ue),ue.current=null},o=()=>{J(K)&&(Ie(),r())};let i,n;(null===(t=s.page)||void 0===t?void 0:t._exportingOrRendering)&&(i=setInterval(o,20)),K.addEventListener("timeupdate",o),K.addEventListener("canplay",o),K.addEventListener("seeked",o);const a=()=>{K.paused&&(Ie(),J(K)&&r()),n=K.requestVideoFrameCallback(a)};return"requestVideoFrameCallback"in K&&(n=K.requestVideoFrameCallback(a)),()=>{clearInterval(i),void 0!==n&&K.cancelVideoFrameCallback(n),K.removeEventListener("timeupdate",o),K.removeEventListener("canplay",o),K.removeEventListener("seeked",o)}},[null===(f=s.page)||void 0===f?void 0:f._exportingOrRendering,K,s.id,E,oe]);let{cropX:he,cropY:me,cropWidth:pe,cropHeight:ge}=s.a;"loaded"!==Q&&(he=me=0,pe=ge=1);const{width:fe,height:ve}=W(de),we=null!==(L=null==K?void 0:K.videoWidth)&&void 0!==L?L:0,xe=null!==(M=null==K?void 0:K.videoHeight)&&void 0!==M?M:0;i.useLayoutEffect(()=>{"loaded"===Q&&we>0&&xe>0&&s._setSourceSize({width:we,height:xe},s.src)},[s,s.src,Q,we,xe]);const Ee=y({stretchEnabled:s.stretchEnabled,cropX:he,cropY:me,cropWidth:pe,cropHeight:ge,width:s.a.width,height:s.a.height},{width:fe,height:ve}),Le=Ee.width,Me=Ee.height,ye=null!==(C=s.cornerRadius)&&void 0!==C?C:0,[Ce,be]=useCornerRadiusAndCrop(s,de,Ee,p._elementsPixelRatio,ye,!0);let[ke,Te]=useClip(s,Ce,p._elementsPixelRatio,[Ee,ye,p._elementsPixelRatio]);const je=S(O,s,ke),Ie=I(()=>{var e,t;se(),le(),be(),Te(),je(),null===(t=null===(e=O.current)||void 0===e?void 0:e.getLayer())||void 0===t||t.batchDraw()});i.useEffect(()=>{Ie()});const Re=()=>{s._cropEnabled&&setTimeout(()=>{d(s)&&s.toggleCropMode(!0)})},Se="loading"===Q,Pe="failed"===Q,_e=!Se&&!Pe,Ae=k(s,Ee,"loaded"===Q?{width:fe,height:ve}:void 0,H),Ve=_e?o(s):0;R(O,Ve);const De=s.selectable||p.role===m.ADMIN;return t(e,{children:r(c,{onMouseEnter:()=>te(!0),onMouseLeave:()=>te(!1),children:[Se&&t(x,{element:s}),Pe&&t(w,{element:s,text:"Can not load the video..."}),t(u,Object.assign({ref:O,name:"element",id:s.id,image:ke,x:s.a.x,y:s.a.y,width:s.a.width||1,height:s.a.height||1,rotation:s.a.rotation,opacity:Ve},a(s),{customCrop:Ee,listening:De,draggable:U?s.draggable&&s.isSelected:s.draggable,preventDefault:!U||s.isSelected,hideInExport:!s.showInExport,onDragMove:e=>{s.set({x:e.target.x(),y:e.target.y()})},onDragEnd:e=>{s.set(b(e.target))},onDblClick:Re,onDblTap:Re},Ae)),!!s.borderSize&&t(h,{x:s.a.x,y:s.a.y,width:Math.max(s.a.width-s.borderSize,0),height:Math.max(s.a.height-s.borderSize,0),opacity:Ve,offsetX:-s.borderSize/2,offsetY:-s.borderSize/2,stroke:n(s.borderColor),strokeWidth:s.borderSize,listening:!1,rotation:s.a.rotation,cornerRadius:Math.max(0,ye-s.borderSize),hideInExport:!s.showInExport}),!p.isPlaying&&t(u,{image:Z?ee||U?$:void 0:q,x:s.a.x,y:s.a.y,offsetX:-s.a.width/2+15/p.scale,offsetY:-s.a.height/2+15/p.scale,rotation:s.a.rotation,width:30/p.scale,height:30/p.scale,hideInExport:!0,onClick:()=>X(!Z),onTap:()=>X(!Z)}),s._cropModeEnabled&&"loaded"===Q&&t(Y,{element:s,store:p,previewImage:ke,sourceImage:de,sourceWidth:fe,sourceHeight:ve,cropAbsoluteWidth:Le,cropAbsoluteHeight:Me,innerAnchors:N})]})})});
|
package/config.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export { setTranslations, getTranslations } from './utils/l10n.js';
|
|
|
16
16
|
export { setIcons, type IconName } from './icons/registry.js';
|
|
17
17
|
export { registerNextDomDrop as unstable_registerNextDomDrop } from './canvas/page.js';
|
|
18
18
|
export { getGoogleFontsListAPI, getGoogleFontImage, setAPI } from './utils/api.js';
|
|
19
|
-
export { setColorsPresetFunc } from './toolbar/color-picker.js';
|
|
19
|
+
export { setColorsPresetFunc, setColorPickerContent, type ColorPickerContentProps, } from './toolbar/color-picker.js';
|
|
20
20
|
export { setQuillFormats as unstable_setQuillFormats } from './canvas/rich-editor.js';
|
|
21
21
|
export { useDownScaling, setDownScalingEnabled } from './utils/flags.js';
|
|
22
22
|
export { useRemoveBackground as unstable_setRemoveBackgroundEnabled, setRemoveBackgroundEnabled, } from './utils/flags.js';
|
package/config.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{setGoogleFonts,addGlobalFont,removeGlobalFont,replaceGlobalFonts,setGoogleFontsVariants}from"./utils/fonts.js";export{setUploadFunc}from"./side-panel/upload-panel.js";export{setExpandFunc,setReplaceObjectFunc,setRemoveObjectFunc,setUpscaleFunc,setRemoveBackgroundFunc}from"./utils/api.js";export{setFontUploadFunc}from"./side-panel/text-panel.js";export{toggleFadeInAnimation}from"./canvas/use-fadein.js";export{setTransformerStyle,setHighlighterStyle,setInnerImageCropTransformerStyle,setOuterImageCropTransformerStyle}from"./canvas/workspace-style.js";export{setImageLoaderHook as unstable_setImageLoaderHook}from"./canvas/image-element.js";export{registerShapeModel as unstable_registerShapeModel}from"./model/group-model.js";export{registerShapeComponent as unstable_registerShapeComponent}from"./canvas/element.js";export{registerTransformerAttrs as unstable_registerTransformerAttrs}from"./canvas/page.js";export{registerToolbarComponent as unstable_registerToolbarComponent}from"./toolbar/toolbar.js";export{onLoadError,setAssetLoadTimeout,setFontLoadTimeout}from"./utils/loader.js";export{setSnapFilterFunc as unstable_setSnapFilterFunc}from"./canvas/use-transformer-snap.js";export{setTranslations,getTranslations}from"./utils/l10n.js";export{setIcons}from"./icons/registry.js";export{registerNextDomDrop as unstable_registerNextDomDrop}from"./canvas/page.js";export{getGoogleFontsListAPI,getGoogleFontImage,setAPI}from"./utils/api.js";export{setColorsPresetFunc}from"./toolbar/color-picker.js";export{setQuillFormats as unstable_setQuillFormats}from"./canvas/rich-editor.js";export{useDownScaling,setDownScalingEnabled}from"./utils/flags.js";export{useRemoveBackground as unstable_setRemoveBackgroundEnabled,setRemoveBackgroundEnabled}from"./utils/flags.js";export{useHtmlTextRender as unstable_useHtmlTextRender,setRichTextEnabled}from"./utils/flags.js";export{setLegacyRichTextEnabled}from"./utils/flags.js";export{setForceTextFit as unstable_setForceTextFit}from"./utils/flags.js";export{setTextSplitAllowed as unstable_setTextSplitAllowed}from"./utils/flags.js";export{setTextOverflow as unstable_setTextOverflow,setTextOverflow}from"./utils/flags.js";export{setTextVerticalResizeEnabled as unstable_setTextVerticalResizeEnabled,setTextVerticalResizeEnabled}from"./utils/flags.js";export{setAnimationsEnabled as unstable_setAnimationsEnabled,setAnimationsEnabled}from"./utils/flags.js";export{setAiTextEnabled}from"./utils/flags.js";export{setCmykInputEnabled}from"./utils/flags.js";export{setCmykPreview}from"@polotno/core/color";export{setAutoDeleteEmptyText}from"./utils/flags.js";
|
|
1
|
+
export{setGoogleFonts,addGlobalFont,removeGlobalFont,replaceGlobalFonts,setGoogleFontsVariants}from"./utils/fonts.js";export{setUploadFunc}from"./side-panel/upload-panel.js";export{setExpandFunc,setReplaceObjectFunc,setRemoveObjectFunc,setUpscaleFunc,setRemoveBackgroundFunc}from"./utils/api.js";export{setFontUploadFunc}from"./side-panel/text-panel.js";export{toggleFadeInAnimation}from"./canvas/use-fadein.js";export{setTransformerStyle,setHighlighterStyle,setInnerImageCropTransformerStyle,setOuterImageCropTransformerStyle}from"./canvas/workspace-style.js";export{setImageLoaderHook as unstable_setImageLoaderHook}from"./canvas/image-element.js";export{registerShapeModel as unstable_registerShapeModel}from"./model/group-model.js";export{registerShapeComponent as unstable_registerShapeComponent}from"./canvas/element.js";export{registerTransformerAttrs as unstable_registerTransformerAttrs}from"./canvas/page.js";export{registerToolbarComponent as unstable_registerToolbarComponent}from"./toolbar/toolbar.js";export{onLoadError,setAssetLoadTimeout,setFontLoadTimeout}from"./utils/loader.js";export{setSnapFilterFunc as unstable_setSnapFilterFunc}from"./canvas/use-transformer-snap.js";export{setTranslations,getTranslations}from"./utils/l10n.js";export{setIcons}from"./icons/registry.js";export{registerNextDomDrop as unstable_registerNextDomDrop}from"./canvas/page.js";export{getGoogleFontsListAPI,getGoogleFontImage,setAPI}from"./utils/api.js";export{setColorsPresetFunc,setColorPickerContent}from"./toolbar/color-picker.js";export{setQuillFormats as unstable_setQuillFormats}from"./canvas/rich-editor.js";export{useDownScaling,setDownScalingEnabled}from"./utils/flags.js";export{useRemoveBackground as unstable_setRemoveBackgroundEnabled,setRemoveBackgroundEnabled}from"./utils/flags.js";export{useHtmlTextRender as unstable_useHtmlTextRender,setRichTextEnabled}from"./utils/flags.js";export{setLegacyRichTextEnabled}from"./utils/flags.js";export{setForceTextFit as unstable_setForceTextFit}from"./utils/flags.js";export{setTextSplitAllowed as unstable_setTextSplitAllowed}from"./utils/flags.js";export{setTextOverflow as unstable_setTextOverflow,setTextOverflow}from"./utils/flags.js";export{setTextVerticalResizeEnabled as unstable_setTextVerticalResizeEnabled,setTextVerticalResizeEnabled}from"./utils/flags.js";export{setAnimationsEnabled as unstable_setAnimationsEnabled,setAnimationsEnabled}from"./utils/flags.js";export{setAiTextEnabled}from"./utils/flags.js";export{setCmykInputEnabled}from"./utils/flags.js";export{setCmykPreview}from"@polotno/core/color";export{setAutoDeleteEmptyText}from"./utils/flags.js";
|
package/model/group-model.d.ts
CHANGED
|
@@ -367,7 +367,6 @@ export declare const TYPES_MAP: {
|
|
|
367
367
|
strokeLineJoin: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
368
368
|
lineHeight: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree/dist/internal").IUnionType<[import("mobx-state-tree").ISimpleType<number>, import("mobx-state-tree").ISimpleType<string>]>, [undefined]>;
|
|
369
369
|
letterSpacing: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
370
|
-
_editModeEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
371
370
|
backgroundEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
372
371
|
backgroundColor: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
373
372
|
backgroundOpacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
@@ -498,6 +497,8 @@ export declare const TYPES_MAP: {
|
|
|
498
497
|
} & {
|
|
499
498
|
setAnimation(type: 'enter' | 'exit' | 'loop', attrs: AnimationAttrs): void;
|
|
500
499
|
setFilter(type: any, value: any): void;
|
|
500
|
+
} & {
|
|
501
|
+
_editModeEnabled: boolean;
|
|
501
502
|
} & {
|
|
502
503
|
set(attrs: any): void;
|
|
503
504
|
toggleEditMode(editing?: boolean): void;
|
package/model/page-model.d.ts
CHANGED
|
@@ -292,7 +292,6 @@ declare const PageImpl: import("mobx-state-tree").IModelType<{
|
|
|
292
292
|
strokeLineJoin: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
293
293
|
lineHeight: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree/dist/internal").IUnionType<[import("mobx-state-tree").ISimpleType<number>, import("mobx-state-tree").ISimpleType<string>]>, [undefined]>;
|
|
294
294
|
letterSpacing: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
295
|
-
_editModeEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
296
295
|
backgroundEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
297
296
|
backgroundColor: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
298
297
|
backgroundOpacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
@@ -423,6 +422,8 @@ declare const PageImpl: import("mobx-state-tree").IModelType<{
|
|
|
423
422
|
} & {
|
|
424
423
|
setAnimation(type: 'enter' | 'exit' | 'loop', attrs: import("../utils/animations.js").AnimationAttrs): void;
|
|
425
424
|
setFilter(type: any, value: any): void;
|
|
425
|
+
} & {
|
|
426
|
+
_editModeEnabled: boolean;
|
|
426
427
|
} & {
|
|
427
428
|
set(attrs: any): void;
|
|
428
429
|
toggleEditMode(editing?: boolean): void;
|
|
@@ -2600,7 +2601,6 @@ declare const PageImpl: import("mobx-state-tree").IModelType<{
|
|
|
2600
2601
|
strokeLineJoin: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
2601
2602
|
lineHeight: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree/dist/internal").IUnionType<[import("mobx-state-tree").ISimpleType<number>, import("mobx-state-tree").ISimpleType<string>]>, [undefined]>;
|
|
2602
2603
|
letterSpacing: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
2603
|
-
_editModeEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
2604
2604
|
backgroundEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
2605
2605
|
backgroundColor: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
2606
2606
|
backgroundOpacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
@@ -2731,6 +2731,8 @@ declare const PageImpl: import("mobx-state-tree").IModelType<{
|
|
|
2731
2731
|
} & {
|
|
2732
2732
|
setAnimation(type: 'enter' | 'exit' | 'loop', attrs: import("../utils/animations.js").AnimationAttrs): void;
|
|
2733
2733
|
setFilter(type: any, value: any): void;
|
|
2734
|
+
} & {
|
|
2735
|
+
_editModeEnabled: boolean;
|
|
2734
2736
|
} & {
|
|
2735
2737
|
set(attrs: any): void;
|
|
2736
2738
|
toggleEditMode(editing?: boolean): void;
|
|
@@ -4905,7 +4907,6 @@ declare const PageImpl: import("mobx-state-tree").IModelType<{
|
|
|
4905
4907
|
strokeLineJoin: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
4906
4908
|
lineHeight: import("mobx-state-tree").IOptionalIType<import("mobx-state-tree/dist/internal").IUnionType<[import("mobx-state-tree").ISimpleType<number>, import("mobx-state-tree").ISimpleType<string>]>, [undefined]>;
|
|
4907
4909
|
letterSpacing: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
4908
|
-
_editModeEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
4909
4910
|
backgroundEnabled: import("mobx-state-tree").IType<boolean | undefined, boolean, boolean>;
|
|
4910
4911
|
backgroundColor: import("mobx-state-tree").IType<string | undefined, string, string>;
|
|
4911
4912
|
backgroundOpacity: import("mobx-state-tree").IType<number | undefined, number, number>;
|
|
@@ -5036,6 +5037,8 @@ declare const PageImpl: import("mobx-state-tree").IModelType<{
|
|
|
5036
5037
|
} & {
|
|
5037
5038
|
setAnimation(type: 'enter' | 'exit' | 'loop', attrs: import("../utils/animations.js").AnimationAttrs): void;
|
|
5038
5039
|
setFilter(type: any, value: any): void;
|
|
5040
|
+
} & {
|
|
5041
|
+
_editModeEnabled: boolean;
|
|
5039
5042
|
} & {
|
|
5040
5043
|
set(attrs: any): void;
|
|
5041
5044
|
toggleEditMode(editing?: boolean): void;
|
package/model/shape-model.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{types as t,isAlive as e}from"mobx-state-tree";import{animate as i,isBuiltIn as n,loopWindow as o,playedAnimations as a,setAnimationOf as r}from"../utils/animations.js";import{computed as s}from"mobx";import{transitionWindows as l}from"@polotno/core/animation-presets";import{
|
|
1
|
+
import{types as t,isAlive as e}from"mobx-state-tree";import{animate as i,isBuiltIn as n,loopWindow as o,playedAnimations as a,setAnimationOf as r}from"../utils/animations.js";import{computed as s}from"mobx";import{transitionWindows as l}from"@polotno/core/animation-presets";import{clampElementSize as d}from"@polotno/schema/defaults";import{Node as c}from"./node-model.js";import{getElementTimeRange as h,getPlayheadLocalTime as p,isHeldByPreview as m}from"../utils/timeline.js";import{degToRad as f,getCenter as u}from"../utils/math.js";export const Animation=t.model("Animation",{delay:0,duration:500,enabled:!0,type:t.enumeration("Type",["enter","exit","loop"]),name:"none",data:t.frozen({}),easing:t.maybe(t.string)});export const getDiff=(t,e)=>{const i={};for(const n in e){if("number"==typeof t[n]&&"number"==typeof e[n]){const o=e[n]-t[n];0!==o&&(i[n]=o)}}return i};const b=["x","y","width","height","rotation"];export const ShapeFilter=t.model("ShapeFilter",{intensity:1});export const INDEPENDENT_FILTERS=["temperature","contrast","highlights","shadows","white","black","saturation","vibrance"];export const Shape=c.named("Shape").props({x:0,y:0,width:100,height:100,rotation:0,opacity:1,animations:t.array(Animation),blurEnabled:!1,blurRadius:10,brightnessEnabled:!1,brightness:0,sepiaEnabled:!1,grayscaleEnabled:!1,filters:t.map(ShapeFilter),shadowEnabled:!1,shadowBlur:5,shadowOffsetX:0,shadowOffsetY:0,shadowColor:"black",shadowOpacity:1,visible:!0,draggable:!0,resizable:!0,selectable:!0,contentEditable:!0,styleEditable:!0,alwaysOnTop:!1,showInExport:!0}).preProcessSnapshot(t=>{const e=Object.assign(Object.assign({},t),{x:"x"in t&&t.x||0,y:"y"in t&&t.y||0,filters:"filters"in t?Array.isArray(t.filters)?{}:t.filters:void 0});return"width"in t&&(e.width=d(e.width)),"height"in t&&(e.height=d(e.height)),"locked"in t&&t.locked&&(e.draggable=!1,e.contentEditable=!1,e.styleEditable=!1,e.resizable=!1,e.removable=!1),e}).views(t=>{var r,d,c,g,y,x;const w=["x","y","width","height","rotation","opacity","color","fontSize","cropX","cropY","cropWidth","cropHeight","blurEnabled","blurRadius","shadowEnabled","shadowBlur","shadowOffsetX","shadowOffsetY","shadowColor","shadowOpacity","text"],E=t,v={x:null!==(r=E.x)&&void 0!==r?r:0,y:null!==(d=E.y)&&void 0!==d?d:0,width:null!==(c=E.width)&&void 0!==c?c:0,height:null!==(g=E.height)&&void 0!==g?g:0,rotation:null!==(y=E.rotation)&&void 0!==y?y:0,opacity:null!==(x=E.opacity)&&void 0!==x?x:1};for(const e of w){e in t&&(v[e]=E[e])}v.wipe=v.flipScale=v.textUnits=void 0;const S=(t,e)=>{const i=getDiff(E,e);for(const n in i){const e=t[n];"number"==typeof e&&(t[n]=e+i[n])}for(const n in e){n in t&&"number"!=typeof t[n]&&void 0!==e[n]&&(t[n]=e[n])}},O=t=>{var e;const i={};for(const n of b){const o=t[n];i[n]="number"==typeof o?o:null!==(e=E[n])&&void 0!==e?e:0}return i},T=t=>b.some(e=>"number"==typeof t[e]&&t[e]!==E[e]),j=s(()=>{if(!e(t)){return v}const r=Object.assign({},v);for(const t in r){r[t]=E[t]}const s=p(t);if(null===s){return r}const d=(t.store.isEditingPaused||m(t))&&t.isSelected,c=a(t),h=c.find(t=>"enter"===t.type),b=(null==h?void 0:h.enabled)&&s<h.delay;b&&(r.opacity=0);const g=l(c,t.page.duration),y=g.enter&&s>=g.enter.start&&s<=g.enter.end;if(y&&!d){const e=(s-g.enter.start)/g.pace;S(r,i({element:t,animation:h,dTime:e}))}const x=c.find(t=>"exit"===t.type);if(!b&&!y&&g.exit&&s>=g.exit.start&&s<=g.exit.end&&!d){const e=(s-g.exit.start)/g.pace;S(r,i({element:t,animation:x,dTime:e}))}(null==x?void 0:x.enabled)&&s>=t.page.duration-x.delay&&(r.opacity=0);const w=c.find(t=>"loop"===t.type),j=(null==w?void 0:w.enabled)&&o(t,w,c);if(j&&s>=j.start&&!d){const e=s-j.start,o=i({element:t,animation:j.animation,dTime:e});r.textUnits&&delete o.textUnits;const a=n(j.animation.name)&&T(r)&&T(o)?O(r):null;S(r,o),a&&Object.assign(r,((t,e,i)=>{const n=t.width?i.width*e.width/t.width:i.width,o=t.height?i.height*e.height/t.height:i.height,a=i.rotation+e.rotation-t.rotation,r=u(i),s=u(e),l=u(t),d=r.x+s.x-l.x,c=r.y+s.y-l.y,h=f(a);return{width:n,height:o,rotation:a,x:d-n/2*Math.cos(h)+o/2*Math.sin(h),y:c-o/2*Math.cos(h)-n/2*Math.sin(h)}})(O(E),O(o),a))}return r.opacity=Math.min(1,Math.max(0,r.opacity)),r}),D={};for(const e in v){const t=s(()=>j.get()[e]);Object.defineProperty(D,e,{enumerable:!0,get:()=>t.get()})}return{get locked(){return!(t.draggable||t.contentEditable||t.styleEditable||t.resizable)},get a(){return D},animated(t){return this.a[t]},get timeRange(){return e(t)&&t.page?h(t.animations,t.page.duration):{start:0,end:0}},get active(){if(!e(t)){return!0}const i=p(t);if(null===i){return!0}const{start:n,end:o}=this.timeRange;return i>=n&&i<o},get isSelected(){return!!e(t)&&t.store.selectedShapes.indexOf(t)>=0&&t.selectable},get isSelectedDirectly(){return!!e(t)&&t.store.selectedElements.indexOf(t)>=0&&t.selectable}}}).actions(t=>({setAnimation(e,i){r(t.animations,e,i)},setFilter(e,i){INDEPENDENT_FILTERS.includes(e)||t.filters.forEach((e,i)=>{INDEPENDENT_FILTERS.includes(i.toString())||t.filters.delete(i.toString())}),null==i?t.filters.delete(e):t.filters.set(e,{intensity:i})}}));
|