Files
xueguang_web_dist/assets/index-BVzr2LGs-BlZuKJ8g.js
zhutao abc955d3bc 1
2025-11-28 15:25:17 +08:00

2 lines
194 KiB
JavaScript

import{U as y,b as tt,l as Ht,D as K,p as ne,R as z,q as x,V as Y,x as M,T as S,F as Be,I as st,a as Ce,B as ze,_ as Ge,c as V,W as fe,z as A,d as je,M as Ie,r as ee,S as _t,f as Ye,o as ve,e as qt,J as Q,g as Vt,h as Zt,i as ue,j,k as U,m as mt,n as He,v as _e,s as ft,t as $,C as O,u as ot,Y as C,w as Kt,A as rt,y as ce,E as We,G as yt,H as qe,K as F,L as ye,N as De,O as Qt,P as Jt,Q as wt,X as es,Z as Ve,$ as kt,a0 as ts,a1 as ss,a2 as we,a3 as ke,a4 as re,a5 as os,a6 as rs,a7 as is,a8 as as,a9 as ns,aa as ls,ab as cs,ac as hs,ad as ps,ae as ds,af as us,ag as ms}from"./appliance-plugin-BKualpMO.js";import{l as ae}from"./index-D4mISQVM.js";import"./eventemitter2-BZbyr5yF.js";import"./index-BbYcdrlh.js";var fs=Object.defineProperty,ys=(v,e,t)=>e in v?fs(v,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):v[e]=t,m=(v,e,t)=>ys(v,typeof e!="symbol"?e+"":e,t),ws=kt,ks=Ve,gs="[object Number]";function vs(v){return typeof v=="number"||ks(v)&&ws(v)==gs}var Ss=vs;const q=je(Ss);function me(v,e=!0){const t=v.length;if(t<2)return"";let s=v[0],o=v[1];if(t===2)return`M${we(s)}L${we(o)}`;let r="";for(let i=2,a=t-1;i<a;i++)s=v[i],o=v[i+1],r+=ke(s,o);return e?`M${ke(v[0],v[1])}Q${we(v[1])}${ke(v[1],v[2])}T${r}${ke(v[t-1],v[0])}${ke(v[0],v[1])}Z`:`M${we(v[0])}Q${we(v[1])}${ke(v[1],v[2])}${v.length>3?"T":""}${r}L${we(v[t-1])}`}const gt=class te{constructor(e){m(this,"maxImageWidth",K.pencilEraser.maxImageWidth),m(this,"maxImageHeight",K.pencilEraser.maxImageHeight),m(this,"syncUnitTime",K.syncOpt.interval),m(this,"vNodes"),m(this,"drawLayer"),m(this,"fullLayer"),m(this,"workId"),m(this,"isDelete",!1);const{vNodes:t,fullLayer:s,drawLayer:o,workId:r,toolsOpt:i}=e;this.vNodes=t,this.fullLayer=s,this.drawLayer=o,this.workId=r,this.syncUnitTime=i.syncUnitTime||this.syncUnitTime}get baseConsumeResult(){return{workId:this.workId,toolsType:this.toolsType,opt:this.workOptions}}filterSamePoints(e,t=.01){return e.reduce((s,o)=>{const r=s[s.length-1];return(o&&!r||o&&r&&!o.isNear(r,t))&&s.push(o),s},[])}setWorkId(e){this.workId=e}getWorkId(){return this.workId}getWorkOptions(){return this.workOptions}setWorkOptions(e){var t,s,o;this.workOptions=e,this.syncUnitTime=e.syncUnitTime||this.syncUnitTime;const r=(t=this.workId)==null?void 0:t.toString(),i=r&&((s=this.vNodes)==null?void 0:s.get(r))||void 0;r&&i&&(i.opt=e,(o=this.vNodes)==null||o.setInfo(r,i))}updataOptService(e){var t,s;let o;const r=(t=this.workId)==null?void 0:t.toString();if(r&&e){const i=this.fullLayer.getElementsByName(r)||this.drawLayer&&this.drawLayer.getElementsByName(r)||[];if(i.length!==1)return;const a=i[0],{pos:l,zIndex:n,scale:c,angle:h,translate:p}=e,d={};q(n)&&(d.zIndex=n),l&&(d.pos=[l[0],l[1]]),c&&(d.scale=c),h&&(d.rotate=h),p&&(d.translate=p),a.attr(d);const u=a?.getBoundingClientRect();return u&&(o=A(o,{x:Math.floor(u.x-te.SafeBorderPadding),y:Math.floor(u.y-te.SafeBorderPadding),w:Math.floor(u.width+te.SafeBorderPadding*2),h:Math.floor(u.height+te.SafeBorderPadding*2)})),(s=this.vNodes)==null||s.setInfo(r,{rect:o,centerPos:l}),o}}drawEraserlines(e,t){const{group:s,eraserlines:o,pos:r,layer:i}=e,a=i.parent;if(t)try{const l=(i.renderer.glRenderer||i.renderer.canvasRenderer).options.displayRatio,n=s.getBoundingClientRect(),c=n.width*l*i.worldScaling[0],h=n.height*l*i.worldScaling[1];let p=1;(c>this.maxImageWidth||h>this.maxImageHeight)&&(p=Math.min(this.maxImageWidth/c,this.maxImageHeight/h));let d=i.getAttribute("scale");d=[d[0]*p,d[1]*p];const u=i.parent.parent,f=i.getAttribute("translate"),w=this.createVmRenderNode(`${this.workId}_bitMapLayer`,u,{offscreen:a.offscreen,width:a.width,height:a.height,contextType:"2d",autoRender:!1,bufferSize:500},d,f),k=s.cloneNode(!0);if(!o||!o.length)return;this.addEraserlines(k,o,r,!1),w.appendChild(k);const g=w.parent;g.render();const I=w.renderer.canvasRenderer,T=I.context;if(!T)return;const W=I.options.displayRatio,N=k.getBoundingClientRect(),R={x:N.x,y:N.y,w:N.width,h:N.height},P={x:0,y:0,w:u.width,h:u.height};if(Ce(R,P)===ze.outside){Z(k,g),u.removeChild(g),this.isDelete=!0;return}const b=ee(R);b.x=Math.floor(Math.max(R.x,0)),b.y=Math.floor(Math.max(R.y,0)),b.w=Math.min(R.x+R.w,u.width)-b.x,b.h=Math.min(R.y+R.h,u.height)-b.y;const D=this.getGroupRect(b,W),B=D.x,E=D.y,X=D.w,_=D.h;let G=T.getImageData(B,E,X,_);if(this.isTransparentRectByCanvas(G)){Z(k,g),u.removeChild(g),this.isDelete=!0,G=null;return}Z(s,a);const J=this.createSpriteNode({imageData:G,safariRect:b,originRect:R,worldScaling:w.worldScaling});J&&s.append(J),Z(k,g),u.removeChild(g),a.deleteTexture(G),G=null}catch(l){console.error("[BaseShapeTool] drawEraserlines error:",l);return}}createVmRenderNode(e,t,s,o,r){const{width:i,height:a}=s,l=`bitMap-${e}`,n=t.layer(l,s),c=new V({anchor:[.5,.5],pos:[i*.5,a*.5],size:[i,a],name:"viewport",id:e});return c.setAttribute("scale",o),c.setAttribute("translate",r),n.append(c),c}getGroupRect(e,t){const s=Math.floor(e.x*t),o=Math.floor(e.y*t),r=Math.floor(e.w*t),i=Math.floor(e.h*t);return{x:s,y:o,w:r,h:i}}addEraserlines(e,t,s,o=!0){for(const r of t){const{thickness:i,op:a}=r;for(const l of a){const n=l.map((u,f)=>f%2?u-s[1]:u-s[0]),c=this.computEraserPoints(n,i),h=me(c,!0),p={pos:[0,0],d:h,fillColor:"rgba(0,0,0,1)"},d=new ye(p);d.addEventListener("beforerender",({detail:u})=>{const f=u.context;o?f.blendFuncSeparate(f.ZERO,f.ZERO,f.ZERO,f.ZERO):f.globalCompositeOperation="destination-out"}),d.addEventListener("afterrender",({detail:u})=>{const f=u.context;o?f.blendFuncSeparate(f.SRC_ALPHA,f.ONE_MINUS_SRC_ALPHA,f.ONE,f.ONE_MINUS_SRC_ALPHA):f.globalCompositeOperation="source-over"}),e.append(d)}}}isTransparentRectByCanvas(e){let t=!0;for(let s=0;s<e.data.length;s+=4)if(e.data[s+3]!==0){t=!1;break}return t}createSpriteNode(e){const{imageData:t,worldScaling:s,safariRect:o,originRect:r}=e;let i;if(typeof OffscreenCanvas=="function"){i=new OffscreenCanvas(t.width,t.height);const c=i.getContext("2d");c&&c.putImageData(t,0,0)}else if(typeof document<"u"){i=document.createElement("canvas"),i.width=t.width,i.height=t.height;const c=i.getContext("2d");c&&c.putImageData(t,0,0)}if(!i)return console.warn("Failed to create imageEraserBitmap Sprite"),null;const a=[Math.floor(r.w/s[0]),Math.floor(r.h/s[1])],l=[Math.floor((o.x-r.x)/s[0]),Math.floor((o.y-r.y)/s[1]),Math.floor(o.w/s[0]),Math.floor(o.h/s[1])],n={name:"eraserTexture",anchor:[.5,.5],pos:[0,0],size:a,texture:i,textureRect:l};return new mt(n)}computEraserPoints(e,t){const s=Math.ceil(t/2);return e.length===2?this.computDot(e,s):this.computLine(e,s)}computDot(e,t){const s=new O(e[0],e[1]);return O.GetDotStroke(s,t,8)}computLine(e,t){const s=[],o=[];let r,i;for(let a=0;a<e.length;a+=2){const l=new O(e[a],e[a+1]);let n;if(a==e.length-2){const c=new O(e[a-2],e[a-1]);n=C.Sub(l,c).uni(),r=l}else{a===0&&(i=l);const c=new O(e[a+2],e[a+3]);n=C.Sub(c,l).uni()}if(n){const c=C.Per(n).mul(t);s.push(O.Sub(l,c)),o.push(O.Add(l,c))}}if(r&&i){const a=O.GetSemicircleStroke(r,s[s.length-1],-1,8),l=O.GetSemicircleStroke(i,o[0],-1,8);return s.concat(a,o.reverse(),l)}return[]}replace(e,t,s){var o;if(!t){s&&e.append(s);return}const r=e.getElementsByName(t);if(r.length)for(const i of r)s?oo(s,i,e):(i.remove(),H(i,e.parent));else s&&e.append(s);this.fullLayer!==this.drawLayer&&(this.fullLayer===e?(o=this.drawLayer)==null||o.getElementsByName(t).forEach(i=>{var a;i.remove(),H(i,(a=this.drawLayer)==null?void 0:a.parent)}):this.fullLayer.getElementsByName(t).forEach(i=>{i.remove(),H(i,this.fullLayer.parent)}))}removeDrawCountNodes(e,t){const s=[];e.getElementsByName(this.workId).forEach(o=>{o.id&&t&&Number(o.id)<t&&s.push(o)});for(const o of s)o.remove(),H(o,e.parent)}static updateNodeOpt(e){var t;const{node:s,opt:o,vNodes:r,willSerializeData:i,targetNode:a}=e,{zIndex:l,translate:n,angle:c,originPoint:h,scenePoint:p,scale:d,pointMap:u,thickness:f}=o;let w;const k=a&&ee(a)||r.get(s.name);if(!k)return;q(l)&&(s.setAttribute("zIndex",l),k.opt.zIndex=l);const g=s.parent;if(g){if(h&&d&&a){const I=[k.op[0],k.op[1]];He(k.op,p,d,n);const T=[k.op[0],k.op[1]],W=[T[0]-I[0],T[1]-I[1]];if(k.centerPos=[k.centerPos[0]+W[0],k.centerPos[1]+W[1]],k.opt.translate=void 0,k.opt.scale=void 0,k.opt.eraserlines)for(let N=0;N<k.opt.eraserlines.length;N++){const{op:R,thickness:P}=k.opt.eraserlines[N];k.opt.eraserlines[N].thickness=Math.round(P*Math.max(d[0],d[1]));for(let b=0;b<R.length;b++)_e(R[b],p,d,n)}}else if(n)if(s.setAttribute("translate",n),k.opt.translate=n,a){const I=[n[0]*g.worldScaling[0],n[1]*g.worldScaling[1]];w=We(k.rect,I),k.rect=w}else{const I=te.getRectFromLayer(g,s.name);k.rect=I||k.rect}else if(q(c))if(s.setAttribute("rotate",c),k.opt.rotate=c,a)w=ft(k.rect,c),k.rect=w;else{const I=te.getRectFromLayer(g,s.name);k.rect=I||k.rect}if(u){const I=u.get(s.name);if(I)for(let T=0,W=0;T<k.op.length;T+=3,W++)k.op[T]=I[W][0],k.op[T+1]=I[W][1]}if(f&&(t=k?.opt)!=null&&t.thickness&&(k.opt.thickness=f),i&&!(h&&d&&a)){if(n){const I=k.op.map((T,W)=>{const N=W%3;return N===0?T+n[0]:N===1?T+n[1]:T});if(k.op=I,k.centerPos=[k.centerPos[0]+n[0],k.centerPos[1]+n[1]],k!=null&&k.opt&&(k.opt.translate=void 0),k.opt.eraserlines)for(let T=0;T<k.opt.eraserlines.length;T++){const{op:W}=k.opt.eraserlines[T];for(let N=0;N<W.length;N++){const R=W[N].map((P,b)=>b%2?P+n[1]:P+n[0]);k.opt.eraserlines[T].op[N]=R}}}else if(q(c)){const I=k.op;if(ts(I,k.centerPos,c),k.op=I,k!=null&&k.opt&&(k.opt.rotate=void 0),k.opt.eraserlines)for(let T=0;T<k.opt.eraserlines.length;T++){const{op:W}=k.opt.eraserlines[T];for(let N=0;N<W.length;N++)ss(W[N],k.centerPos,c)}}}k&&r.setInfo(s.name,k)}}static getCenterPos(e,t){const{worldPosition:s,worldScaling:o}=t;return[(e.x+e.w/2-s[0])/o[0],(e.y+e.h/2-s[1])/o[1]]}static getRectFromLayer(e,t){const s=e.getElementsByName(t)[0];if(s){const o=s.getBoundingClientRect();return{x:Math.floor(o.x-te.SafeBorderPadding),y:Math.floor(o.y-te.SafeBorderPadding),w:Math.floor(o.width+te.SafeBorderPadding*2),h:Math.floor(o.height+te.SafeBorderPadding*2)}}}static isWillRefresh(e){const{toolsType:t,opt:s,node:o,updateOpt:r,willSerializeData:i}=e;return!!(i&&(r.angle||r.translate)||r.thickness&&s.thickness&&s.thickness!==r.thickness||r.strokeType&&s.strokeType&&s.strokeType!==r.strokeType||r.originPoint&&r.scenePoint&&r.scale||r.pointMap&&r.pointMap.has(o.name)||t===S.Text&&(r.fontSize||r.translate||r.textInfos&&r.textInfos.get(o.name))||t===S.Image&&(r.angle||r.translate||r.scale||r.strokeColor&&r.type===Be.Iconify)||t===r.toolsType&&r.willRefresh||s.eraserlines&&s.eraserlines.length&&(r.strokeColor||r.fillColor))}};m(gt,"SafeBorderPadding",10);let L=gt;const Ze=Object.freeze([Object.freeze({width:18,height:26}),Object.freeze({width:26,height:34}),Object.freeze({width:34,height:50}),Object.freeze({width:48,height:74})]);function vt(v,e,t=.01){return Math.abs(v[0]-e[0])<t&&Math.abs(v[1]-e[1])<t}function Ts(v,e=.01){if(v.length===0)return[];const t=[[v[0][0],v[0][1]]];for(let s=1;s<v.length;s++)vt([v[s][0],v[s][1]],t[t.length-1],e)||t.push([v[s][0],v[s][1]]);return t}function Is(v,e=.01){if(v.length<=2)return v;const t=v[0],s=v[v.length-1];return vt(t,s,e)?v.slice(0,-1):v}function Ps(v){const e=v.reduce((s,o)=>s+o[0],0)/v.length,t=v.reduce((s,o)=>s+o[1],0)/v.length;return[e,t]}function it(v,e){return Math.atan2(e[1]-v[1],e[0]-v[0])}function Cs(v,e=.01){if(v.length<=2)return v;const t=[];for(const o of v)t.some(r=>Math.abs(r[0]-o[0])<e&&Math.abs(r[1]-o[1])<e)||t.push([o[0],o[1]]);const s=Ps(t);return t.sort((o,r)=>{const i=it(s,o),a=it(s,r);return i-a})}function Ws(v,e){const t=e[0].x<e[e.length-1].x;return v.map(s=>{const o=s.map(r=>{let i=-1,a=1/0,l=0;for(let n=0;n<e.length;n++){const c=e[n],h=Math.sqrt(Math.pow(c.x-r[0],2)+Math.pow(c.y-r[1],2));h<a&&(a=h,i=n,l=c.z)}return{point:r,index:i,bestZ:l}}).sort((r,i)=>r.index-i.index).map(r=>[...r.point,r.bestZ]);return t&&o[0][0]>o[o.length-1][0]||!t&&o[0][0]<o[o.length-1][0]?o.reverse():o}).sort((s,o)=>{const r=s[0][0],i=o[0][0];return t?r-i:i-r})}function Ls(v,e,t=.01){const s=v.map(c=>[c.x,c.y]),o=(c,h)=>c.map(([p,d])=>({X:Math.round(p*h),Y:Math.round(d*h)})),r=new re.ClipperOffset;r.AddPath(o(s,1e3),re.JoinType.jtRound,re.EndType.etOpenButt);const i=[];r.Execute(i,1);const a=new re.Clipper;a.AddPaths(i,re.PolyType.ptSubject,!0),e.forEach(c=>{a.AddPath(o(c,1e3),re.PolyType.ptClip,!0)});const l=new re.Paths;a.Execute(re.ClipType.ctDifference,l,re.PolyFillType.pftNonZero,re.PolyFillType.pftNonZero);const n=l.map(c=>Is(Cs(Ts(c.map(h=>[h.X/1e3,h.Y/1e3]),t),t),t)).filter(c=>c.length>=2);return Ws(n,v)}class St extends L{constructor(e){super(e),m(this,"canRotate",!0),m(this,"scaleType",U.all),m(this,"toolsType",S.Pencil),m(this,"syncTimestamp"),m(this,"syncIndex",0),m(this,"tmpPoints",[]),m(this,"MAX_REPEAR",10),m(this,"uniThickness"),m(this,"workOptions"),m(this,"centerPos",[0,0]),this.workOptions=e.toolsOpt,this.uniThickness=this.MAX_REPEAR/this.workOptions.thickness/10,this.syncTimestamp=0}combineConsume(){var e;const t=(e=this.workId)==null?void 0:e.toString();if(this.tmpPoints.length<2)return{type:y.None};const s=this.transformDataAll(!0),o={name:t};let r;const i=this.drawLayer||this.fullLayer;return s.length&&(r=this.draw({attrs:o,tasks:s,replaceId:t,layer:i})),{rect:r,type:y.DrawWork,dataType:x.Local}}setWorkOptions(e){super.setWorkOptions(e),this.syncTimestamp=Date.now()}consume(e){const{data:t,isFullWork:s,isSubWorker:o,isMainThread:r,drawCount:i,removeDrawCount:a,isSimpleWorker:l}=e,{workId:n,syncUnitTime:c}=t;c&&(this.syncUnitTime=c);const{tasks:h,effects:p,consumeIndex:d}=this.transformData(t,!1);this.syncIndex=Math.min(this.syncIndex,d,Math.max(0,this.tmpPoints.length-2));const u={name:n,id:q(i)&&i.toString()||void 0};let f,w=!1;const k=this.syncIndex;if(this.syncTimestamp===0&&(this.syncTimestamp=Date.now()),h.length&&(h[0].taskId-this.syncTimestamp>this.syncUnitTime&&(w=!0,this.syncTimestamp=h[0].taskId,this.syncIndex=this.tmpPoints.length),o||r||l)){const I=s?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({attrs:u,tasks:h,effects:p,layer:I,removeDrawCount:a})}if(l){const I=[];return this.tmpPoints.slice(k).forEach(T=>{I.push(T.x,T.y,this.computRadius(T.z,this.workOptions.thickness))}),{...this.baseConsumeResult,type:y.DrawWork,dataType:x.Local,op:w?I:void 0,index:w?k*3:void 0,rect:f,updateNodeOpt:{useAnimation:!0}}}if(o)return d>10&&this.tmpPoints.splice(0,d-10),{rect:f,type:y.DrawWork,dataType:x.Local};const g=[];return this.tmpPoints.slice(k).forEach(I=>{g.push(I.x,I.y,this.computRadius(I.z,this.workOptions.thickness))}),{...this.baseConsumeResult,type:y.DrawWork,dataType:x.Local,rect:f,op:w?g:void 0,index:w?k*3:void 0,updateNodeOpt:{useAnimation:!0}}}consumeAll(e){var t;const s=this.workId;if(e.data){const{op:c,workState:h}=e.data;c!=null&&c.length&&h===M.Done&&this.workOptions.strokeType===F.Stroke&&this.updateTempPointsWithPressureWhenDone(c)}const o=this.transformDataAll(!0),r={name:s};let i;const a=this.fullLayer;if(o.length&&(i=this.draw({attrs:r,tasks:o,replaceId:s,layer:a})),this.tmpPoints.length<2)return this.replace(a,s),{type:y.RemoveNode,removeIds:[s],rect:i};const l=[];this.tmpPoints.map(c=>{l.push(c.x,c.y,c.z)}),this.syncTimestamp=0,delete this.workOptions.syncUnitTime;const n=Q(l);return(t=this.vNodes)==null||t.setInfo(s,{rect:i,op:l,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:i&&L.getCenterPos(i,a)}),{...this.baseConsumeResult,rect:i,type:y.FullWork,dataType:x.Local,ops:n,updateNodeOpt:{pos:this.centerPos,useAnimation:!0}}}clearTmpPoints(){this.tmpPoints.length=0,this.syncTimestamp=0,this.syncIndex=0}consumeService(e){var t,s;const{op:o,isFullWork:r,replaceId:i,workState:a=M.Done}=e;this.tmpPoints.length=0;for(let p=0;p<o.length;p+=3){const d=new O(o[p],o[p+1],o[p+2]);if(this.tmpPoints.length>0){const u=this.tmpPoints[this.tmpPoints.length-1],f=C.Sub(d,u).uni();d.setv(f)}this.tmpPoints.push(d)}if(this.tmpPoints.length<2)return;const l=this.transformDataAll(!0),n=(t=this.workId)==null?void 0:t.toString(),c={name:n};let h;if(n&&l.length){const p=r?this.fullLayer:this.drawLayer||this.fullLayer;h=this.draw({attrs:c,tasks:l,replaceId:i,layer:p,isDrawEraserlines:a===M.Done}),(s=this.vNodes)==null||s.setInfo(n,{rect:h,op:o,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:h&&L.getCenterPos(h,p)})}return h}computPencilPoints(e){const t=[],{op:s,eraserPolylines:o,eraserThickness:r}=e;this.tmpPoints.length=0;for(let a=0;a<s.length;a+=3){const l=new O(s[a],s[a+1],s[a+2]);if(this.tmpPoints.length>0){const n=this.tmpPoints[this.tmpPoints.length-1],c=C.Sub(l,n).uni();l.setv(c)}this.tmpPoints.push(l)}if(this.tmpPoints.length<2)return;const i=this.transformDataAll(!0);if(i.length)for(let a=0;a<i.length;a++){const{pos:l,points:n}=i[a],c=n.map(p=>p.point.addXY(l[0],l[1])),h=[];for(const p of o){const d=[];for(let f=0;f<p.length;f+=2){const w=new O(p[f],p[f+1]);if(d.length>0){const k=d[d.length-1].point,g=C.Sub(w,k).uni();w.setv(g)}d.push({point:w,radius:r})}const{ps:u}=this.computStroke(d,!1);h.push(u.map(f=>f.XY))}Ls(c,h,.01).forEach(p=>{t.push(p.map(d=>[d[0],d[1],d[2]]).flat(1))})}return t}transformDataAll(e=!0){return this.getTaskPoints(this.tmpPoints,e&&this.workOptions.thickness||void 0)}draw(e){const{attrs:t,tasks:s,replaceId:o,effects:r,layer:i,removeDrawCount:a,isDrawEraserlines:l=!0}=e,{strokeColor:n,strokeType:c,thickness:h,zIndex:p,scale:d,rotate:u,translate:f,eraserlines:w,lineCap:k,lineDash:g}=this.workOptions;r!=null&&r.size&&(r.forEach(P=>{var b;(b=i.getElementById(P+""))==null||b.remove()}),r.clear()),a&&this.removeDrawCountNodes(i,a);let I;const T=[],W=i.worldPosition,N=i.worldScaling;for(let P=0;P<s.length;P++){const{pos:b,points:D}=s[P],{ps:B,rect:E}=this.computDrawPoints(D);let X;const _=D.length===1;c===F.Stroke||_?X=me(B,!0):X=me(B,!1);const G={pos:b,d:X,fillColor:c===F.Stroke||_?n:void 0,lineDash:_?void 0:c===F.Dotted?[g&&g[0]||1,(g&&g[1]||2)*h]:c===F.LongDotted?[(g&&g[0]||1)*h,(g&&g[1]||2)*h]:void 0,strokeColor:n,lineCap:_?void 0:k,lineWidth:c===F.Stroke||_?0:h};I=A(I,{x:Math.floor((E.x+b[0])*N[0]+W[0]-L.SafeBorderPadding),y:Math.floor((E.y+b[1])*N[1]+W[1]-L.SafeBorderPadding),w:Math.floor(E.w*N[0]+2*L.SafeBorderPadding),h:Math.floor(E.h*N[1]+2*L.SafeBorderPadding)}),T.push(G)}d&&(t.scale=d),u&&(t.rotate=u),f&&(t.translate=f);const R=new V;if(I){this.centerPos=L.getCenterPos(I,i);const P=c===F.Stroke&&!w;R.attr({...t,normalize:!0,anchor:[.5,.5],bgcolor:P?n:void 0,pos:this.centerPos,size:[(I.w-2*L.SafeBorderPadding)/N[0],(I.h-2*L.SafeBorderPadding)/N[1]],zIndex:p});const b=T.map(D=>(D.pos=[D.pos[0]-this.centerPos[0],D.pos[1]-this.centerPos[1]],new ye(D)));R.append(...b),P&&R.seal(),w&&(this.scaleType=U.proportional,this.drawEraserlines({group:R,eraserlines:w,pos:this.centerPos,layer:i},l)),this.replace(i,o,R)}if(d||u||f){const P=R?.getBoundingClientRect();P&&(I={x:Math.floor(P.x-L.SafeBorderPadding),y:Math.floor(P.y-L.SafeBorderPadding),w:Math.floor(P.width+L.SafeBorderPadding*2),h:Math.floor(P.height+L.SafeBorderPadding*2)})}return this.isDelete&&R.setAttribute("opacity",0),I}computDrawPoints(e){return this.workOptions.strokeType===F.Stroke||e.length===1?this.computStroke(e):this.computNomal(e)}computNomal(e){let t=this.workOptions.thickness;const s=e.map(o=>(t=Math.max(t,o.radius),o.point));return{ps:s,rect:$(s,t)}}computStroke(e,t=!0){return e.length===1?this.computDotStroke(e[0]):this.computLineStroke(e,t)}computLineStroke(e,t=!0){const s=[],o=[];for(let a=0;a<e.length;a++){const{point:l,radius:n}=e[a];let c=l.v;a===0&&e.length>1&&(c=e[a+1].point.v);const h=C.Per(c).mul(n);s.push(O.Sub(l,h)),o.push(O.Add(l,h))}const r=e[e.length-1];if(t){const a=O.GetSemicircleStroke(r.point,s[s.length-1],-1,8),l=O.GetSemicircleStroke(e[0].point,o[0],-1,8),n=s.concat(a,o.reverse(),l);return{ps:n,rect:$(n)}}const i=s.concat(o.reverse());return{ps:i,rect:$(i)}}computDotStroke(e){const{point:t,radius:s}=e,o={x:t.x-s,y:t.y-s,w:s*2,h:s*2};return{ps:O.GetDotStroke(t,s,8),rect:o}}transformData(e,t){const{op:s,workState:o}=e;let r=this.tmpPoints.length-1,i=[];if(s!=null&&s.length&&o){const{strokeType:a,thickness:l}=this.workOptions,n=new Set;r=a===F.Stroke?this.updateTempPointsWithPressure(s,l,n):this.updateTempPoints(s,l,n);const c=t?this.tmpPoints:this.tmpPoints.slice(r);return i=this.getTaskPoints(c,l),{tasks:i,effects:n,consumeIndex:r}}return{tasks:i,consumeIndex:r}}computRadius(e,t){return e*.03*t+t*.5}getMinZ(e,t){return((t||Math.max(1,Math.floor(e*.3)))-e*.5)*100/e/3}getTaskPoints(e,t){var s;const o=[];if(e.length===0)return[];let r=0,i=e[0].x,a=e[0].y,l=[i,a],n=[],c=e[0].t;for(;r<e.length;){const h=e[r],p=h.x-i,d=h.y-a,u=h.z,f=t?this.computRadius(u,t):u;if(n.push({point:new O(p,d,u,e[r].v),radius:f}),r>0&&r<e.length-1){const w=e[r].getAngleByPoints(e[r-1],e[r+1]);if(w<60||w>300){const k=(s=n.pop())==null?void 0:s.point.clone();k&&o.push({taskId:c,pos:l,points:[...n,{point:k,radius:f}]}),i=e[r].x,a=e[r].y,l=[i,a];const g=h.x-i,I=h.y-a;n=[{point:new O(g,I,u),radius:f}],c=Date.now()}}r++}return o.push({taskId:c,pos:l,points:n}),o}updateTempPointsWithPressure(e,t,s){const o=Date.now(),r=this.tmpPoints.length;let i=r;for(let l=0;l<e.length;l+=2){i=Math.min(i,r);const n=this.tmpPoints.length,c=new O(e[l],e[l+1]);if(n===0){this.tmpPoints.push(c);continue}const h=n-1,p=this.tmpPoints[h],d=C.Sub(c,p).uni();if(c.isNear(p,t)){if(p.z<this.MAX_REPEAR){if(p.setz(Math.min(p.z+1,this.MAX_REPEAR)),i=Math.min(i,h),n>1){let w=n-1;for(;w>0;){const k=this.tmpPoints[w].distance(this.tmpPoints[w-1]),g=Math.max(this.tmpPoints[w].z-this.uniThickness*k,0);if(this.tmpPoints[w-1].z>=g)break;this.tmpPoints[w-1].setz(g),i=Math.min(i,w-1),w--}}}else i=1/0;continue}c.setv(d);const u=c.distance(p),f=Math.max(p.z-this.uniThickness*u,0);n>1&&C.Equals(d,p.v,.02)&&(f>0||p.z<=0)&&(s&&p.t&&s.add(p.t),this.tmpPoints.pop(),i=Math.min(h,i)),c.setz(f),this.tmpPoints.push(c)}if(i===1/0)return this.tmpPoints.length;let a=r;if(i===r){a=Math.max(a-1,0);const l=this.tmpPoints[a].t;l&&s?.add(l)}else{let l=r-1;for(a=i;l>=0;){const n=this.tmpPoints[l].t;if(n&&(s?.add(n),l<=i)){a=l,l=-1;break}l--}}return this.tmpPoints[a].setT(o),a}updateTempPoints(e,t,s){var o;const r=Date.now(),i=this.tmpPoints.length;let a=i;for(let n=0;n<e.length;n+=2){const c=this.tmpPoints.length,h=new O(e[n],e[n+1]);if(c===0){this.tmpPoints.push(h);continue}const p=c-1,d=this.tmpPoints[p],u=C.Sub(h,d).uni();if(h.isNear(d,t/2)){a=Math.min(p,a);continue}C.Equals(u,d.v,.02)&&(s&&d.t&&s.add(d.t),this.tmpPoints.pop(),a=Math.min(p,a)),h.setv(u),this.tmpPoints.push(h)}let l=i;if(a===i){l=Math.max(l-1,0);const n=this.tmpPoints[l].t;n&&s?.add(n)}else{let n=Math.min(i-1,a);for(l=a;n>=0;){const c=(o=this.tmpPoints[n])==null?void 0:o.t;if(c&&(s?.add(c),n<=a)){l=n,n=-1;break}n--}}return this.tmpPoints[l].setT(r),l}updateTempPointsWithPressureWhenDone(e){const{thickness:t}=this.workOptions,s=e.length,o=this.getMinZ(t);for(let r=0;r<s;r+=2){const i=this.tmpPoints.length,a=new O(e[r],e[r+1]);if(i===0){this.tmpPoints.push(a);continue}const l=i-1,n=this.tmpPoints[l],c=C.Sub(a,n).uni(),h=a.distance(n);if(i>1&&n.z===o)break;if(a.isNear(n,t/2)){if(s<3&&n.z<this.MAX_REPEAR&&(n.setz(Math.min(n.z+1,this.MAX_REPEAR)),i>1)){let d=i-1;for(;d>0;){const u=this.tmpPoints[d].distance(this.tmpPoints[d-1]),f=Math.max(this.tmpPoints[d].z-this.uniThickness*u,-t/4);if(this.tmpPoints[d-1].z>=f)break;this.tmpPoints[d-1].setz(f),d--}}continue}a.setv(c);const p=Math.max(n.z-this.uniThickness*h,o);i>1&&C.Equals(c,n.v,.02)&&n.z<=0&&this.tmpPoints.pop(),a.setz(p),this.tmpPoints.push(a)}}static updateNodeOpt(e){var t,s;const{node:o,opt:r,vNodes:i}=e,{strokeColor:a,strokeType:l}=r,n=i.get(o.name);return a&&(o.tagName==="GROUP"?so(o)?o.setAttribute("bgcolor",a):o.children.forEach(c=>{c.setAttribute("strokeColor",a),c.getAttribute("fillColor")&&c.setAttribute("fillColor",a)}):(o.setAttribute("strokeColor",a),o.setAttribute("fillColor",a)),(t=n?.opt)!=null&&t.strokeColor&&(n.opt.strokeColor=a)),l&&n!=null&&n.opt&&(s=n.opt)!=null&&s.strokeType&&(n.opt.strokeType=l),n&&i.setInfo(o.name,n),L.updateNodeOpt(e)}}class Tt extends L{constructor(e){super(e),m(this,"toolsType",S.LaserPen),m(this,"canRotate",!1),m(this,"scaleType",U.none),m(this,"syncTimestamp"),m(this,"syncIndex",0),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"consumeIndex",0),this.workOptions=e.toolsOpt,this.syncTimestamp=0}combineConsume(){}setWorkOptions(e){super.setWorkOptions(e),this.syncTimestamp=Date.now()}consume(e){const{data:t,isSubWorker:s}=e,{workId:o,op:r,syncUnitTime:i}=t;if(r?.length===0)return{type:y.None};if(i&&(this.syncUnitTime=i),this.updateTempPoints(r||[]),this.consumeIndex>this.tmpPoints.length-4)return{type:y.None};const{strokeColor:a,thickness:l,strokeType:n,lineDash:c,lineCap:h}=this.workOptions,p=$(this.tmpPoints,l);let d=!1;const u=this.syncIndex,f=this.tmpPoints.slice(this.consumeIndex);this.consumeIndex=this.tmpPoints.length-1,this.syncTimestamp===0&&(this.syncTimestamp=Date.now());const w={name:o?.toString(),opacity:1,lineDash:n===F.Dotted?[c&&c[0]||1,(c&&c[1]||2)*l]:n===F.LongDotted?[(c&&c[0]||1)*l,(c&&c[1]||2)*l]:void 0,strokeColor:a,lineCap:h,lineWidth:l,anchor:[.5,.5]},k=this.getTaskPoints(f);if(k.length){const I=Date.now();I-this.syncTimestamp>this.syncUnitTime&&(d=!0,this.syncTimestamp=I,this.syncIndex=this.tmpPoints.length),s&&this.draw({attrs:w,tasks:k,isDot:!1,layer:this.drawLayer||this.fullLayer})}const g=[];return this.tmpPoints.slice(u).forEach(I=>{g.push(I.x,I.y)}),{rect:{x:p.x*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],y:p.y*this.fullLayer.worldScaling[1]+this.fullLayer.worldPosition[1],w:p.w*this.fullLayer.worldScaling[0],h:p.h*this.fullLayer.worldScaling[1]},type:y.DrawWork,dataType:x.Local,op:d?g:void 0,index:d?u*2:void 0,...this.baseConsumeResult}}consumeAll(){var e;const t=(e=this.workId)==null?void 0:e.toString();let s;if(this.tmpPoints.length-1>this.consumeIndex){let i=this.tmpPoints.slice(this.consumeIndex);const a=i.length===1,{strokeColor:l,thickness:n,strokeType:c}=this.workOptions;if(a){const d=this.computDotStroke({point:i[0],radius:n/2});i=d.ps,s=d.rect}else s=$(this.tmpPoints,n);const h={name:t?.toString(),fillColor:a?l:void 0,opacity:1,lineDash:c===F.Dotted&&!a?[1,n*2]:c===F.LongDotted&&!a?[n,n*2]:void 0,strokeColor:l,lineCap:a?void 0:"round",lineWidth:a?0:n,anchor:[.5,.5]},p=this.getTaskPoints(i);p.length&&this.draw({attrs:h,tasks:p,isDot:a,layer:this.drawLayer||this.fullLayer})}const o=[];this.tmpPoints.forEach(i=>{o.push(i.x,i.y)});const r=Q(o);return{rect:s&&{x:s.x*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],y:s.y*this.fullLayer.worldScaling[1]+this.fullLayer.worldPosition[1],w:s.w*this.fullLayer.worldScaling[0],h:s.h*this.fullLayer.worldScaling[1]},type:y.FullWork,dataType:x.Local,ops:r,index:this.syncIndex*2,...this.baseConsumeResult}}clearTmpPoints(){this.tmpPoints.length=0,this.syncTimestamp=0,this.syncIndex=0}consumeService(e){var t;const{op:s,replaceId:o,isFullWork:r}=e,{strokeColor:i,thickness:a,strokeType:l}=this.workOptions;if(!s.length){const f=$(this.tmpPoints,a);return{x:f.x*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],y:f.y*this.fullLayer.worldScaling[1]+this.fullLayer.worldPosition[1],w:f.w*this.fullLayer.worldScaling[0],h:f.h*this.fullLayer.worldScaling[1]}}const n=Math.max(0,this.tmpPoints.length-1);this.updateTempPoints(s||[]);let c,h=this.tmpPoints.slice(n);const p=h.length===1;if(p){const f=this.computDotStroke({point:h[0],radius:a/2});h=f.ps,c=f.rect}else c=$(this.tmpPoints,a);const d={name:(t=this.workId)==null?void 0:t.toString(),fillColor:p?i:void 0,opacity:1,lineDash:l===F.Dotted&&!p?[1,a*2]:l===F.LongDotted&&!p?[a,a*2]:void 0,strokeColor:i,lineCap:p?void 0:"round",lineWidth:p?0:a,anchor:[.5,.5]},u=this.getTaskPoints(h);if(u.length){const f=r?this.fullLayer:this.drawLayer||this.fullLayer;this.draw({attrs:d,tasks:u,isDot:p,replaceId:o,layer:f})}return{x:c.x*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],y:c.y*this.fullLayer.worldScaling[1]+this.fullLayer.worldPosition[1],w:c.w*this.fullLayer.worldScaling[0],h:c.h*this.fullLayer.worldScaling[1]}}computDotStroke(e){const{point:t,radius:s}=e,o={x:t.x-s,y:t.y-s,w:s*2,h:s*2};return{ps:O.GetDotStroke(t,s,8),rect:o}}updateTempPoints(e){const t=this.tmpPoints.length;for(let s=0;s<e.length;s+=2){if(t){const o=this.tmpPoints.slice(-1)[0];o&&o.x===e[s]&&o.y===e[s+1]&&this.tmpPoints.pop()}this.tmpPoints.push(new O(e[s],e[s+1]))}}async draw(e){const{attrs:t,tasks:s,isDot:o,layer:r}=e,{duration:i}=this.workOptions;for(const a of s){const l=new ye,{pos:n,points:c}=a;let h;o?h=me(c,!0):h=me(c,!1),l.attr({...t,pos:n,d:h});const{vertex:p,fragment:d}=this.workOptions;if(p&&d){const u=r.renderer.createProgram({vertex:p,fragment:d}),{width:f,height:w}=r.getResolution();l.setUniforms({u_time:0,u_resolution:[f,w]}),l.setProgram(u)}r.appendChild(l),l.transition(i).attr({scale:o?[.1,.1]:[1,1],lineWidth:o?0:1}).then(()=>{l.remove()})}}getTaskPoints(e){var t;const s=[];if(e.length===0)return[];let o=0,r=e[0].x,i=e[0].y,a=[r,i],l=[];for(;o<e.length;){const n=e[o],c=n.x-r,h=n.y-i;if(l.push(new O(c,h)),o>0&&o<e.length-1){const p=e[o].getAngleByPoints(e[o-1],e[o+1]);if(p<60||p>300){const d=(t=l.pop())==null?void 0:t.clone();d&&s.push({pos:a,points:[...l,d]}),r=e[o].x,i=e[o].y,a=[r,i];const u=n.x-r,f=n.y-i;l=[new O(u,f)]}}o++}return s.push({pos:a,points:l}),s}removeLocal(){}removeService(e){let t;const s=[];return this.fullLayer.getElementsByName(e).forEach(o=>{if(o.name===e){const r=o.getBoundingClientRect();t=A(t,{x:r.x,y:r.y,w:r.width,h:r.height}),s.push(o)}}),s.length&&s.forEach(o=>o.remove()),t}}const It=class Me extends L{constructor(e,t){super(e),m(this,"canRotate",!1),m(this,"scaleType",U.none),m(this,"toolsType",S.Eraser),m(this,"serviceWork"),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"worldPosition"),m(this,"worldScaling"),m(this,"eraserRect"),m(this,"eraserPolyline"),this.serviceWork=t,this.workOptions=e.toolsOpt,this.worldPosition=this.fullLayer.worldPosition,this.worldScaling=this.fullLayer.worldScaling}combineConsume(){}consumeService(){}setWorkOptions(e){super.setWorkOptions(e)}createEraserRect(e){const t=e[0]*this.worldScaling[0]+this.worldPosition[0],s=e[1]*this.worldScaling[1]+this.worldPosition[1],{width:o,height:r}=Me.eraserSizes[this.workOptions.thickness];this.eraserRect={x:t-o*.5,y:s-r*.5,w:o,h:r},this.eraserPolyline=[this.eraserRect.x,this.eraserRect.y,this.eraserRect.x+this.eraserRect.w,this.eraserRect.y+this.eraserRect.h]}computRectCenterPoints(){const e=this.tmpPoints.slice(-2);if(this.tmpPoints.length===4){const t=new C(this.tmpPoints[0],this.tmpPoints[1]),s=new C(this.tmpPoints[2],this.tmpPoints[3]),o=C.Sub(s,t).uni(),r=C.Dist(t,s),{width:i,height:a}=Me.eraserSizes[this.workOptions.thickness],l=Math.min(i,a),n=Math.round(r/l);if(n>1){const c=[];for(let h=0;h<n;h++){const p=C.Mul(o,h*l);c.push(this.tmpPoints[0]+p.x,this.tmpPoints[1]+p.y)}return c.concat(e)}}return e}isNear(e,t){const s=new C(e[0],e[1]),o=new C(t[0],t[1]),{width:r,height:i}=Me.eraserSizes[this.workOptions.thickness];return C.Dist(s,o)<Math.hypot(r,i)*.5}remove(e){const{curNodeMap:t,removeIds:s}=e;let o;for(const r of t.values())if(r.rect&&this.eraserRect&&this.eraserPolyline&&Ye(this.eraserRect,r.rect)){const{op:i}=r,a=[],l=[];for(let c=0;c<i.length;c+=3){const h=new C(i[c]*this.worldScaling[0]+this.worldPosition[0],i[c+1]*this.worldScaling[1]+this.worldPosition[1],i[c+2]);l.push(h),a.push(new O(h.x,h.y))}const n=a.length&&$(a)||r.rect;Ye(n,this.eraserRect)&&(l.length>1?qe.polyline(l.map(c=>c.XY),this.eraserPolyline).length&&s.add(r.name):s.add(r.name),o=A(o,r.rect||n))}return s.forEach(r=>{var i;const a=this.fullLayer.getElementsByName(r);a[0]&&(a[0].remove(),H(a[0],this.fullLayer.parent),(i=this.vNodes)==null||i.delete(r))}),o&&(o.x-=L.SafeBorderPadding,o.y-=L.SafeBorderPadding,o.w+=L.SafeBorderPadding*2,o.h+=L.SafeBorderPadding*2),o}consume(e){const{op:t,disableEraseImage:s,disableEraseText:o}=e.data;if(!t||t.length===0)return{type:y.None,...this.baseConsumeResult};const r=this.tmpPoints.length;if(r>1&&this.isNear([t[0],t[1]],[this.tmpPoints[r-2],this.tmpPoints[r-1]]))return{type:y.None,...this.baseConsumeResult};r<3?this.tmpPoints.push(t[0],t[1]):this.tmpPoints.splice(2,2,t[0],t[1]);const i=this.computRectCenterPoints();let a;const l=new Set;if(!this.vNodes)return{type:y.None,...this.baseConsumeResult};const n=this.getCanEraserNodeMap(this.vNodes.getCanEraserNodes(this.vNodes.curNodeMap,{disableEraseImage:s,disableEraseText:o}));for(let c=0;c<i.length-1;c+=2){this.createEraserRect(i.slice(c,c+2));const h=this.remove({curNodeMap:n,removeIds:l});a=A(a,h)}return a&&l.size?{type:y.RemoveNode,rect:a,removeIds:[...l]}:{type:y.None,...this.baseConsumeResult}}consumeAll(e){return this.consume(e)}clearTmpPoints(){this.tmpPoints.length=0}getCanEraserNodeMap(e){var t;if(this.serviceWork){const s=new Map(e),o=this.serviceWork.selectorWorkShapes,r=this.serviceWork.workShapes;for(const i of o.values())if((t=i.selectIds)!=null&&t.length)for(const a of i.selectIds)s.delete(a);for(const i of r.keys())s.delete(i);return s}return e}};m(It,"eraserSizes",Ze);let Pt=It;var at=ms,bs=1,Ns=Object.prototype,Rs=Ns.hasOwnProperty;function xs(v,e,t,s,o,r){var i=t&bs,a=at(v),l=a.length,n=at(e),c=n.length;if(l!=c&&!i)return!1;for(var h=l;h--;){var p=a[h];if(!(i?p in e:Rs.call(e,p)))return!1}var d=r.get(v),u=r.get(e);if(d&&u)return d==e&&u==v;var f=!0;r.set(v,e),r.set(e,v);for(var w=i;++h<l;){p=a[h];var k=v[p],g=e[p];if(s)var I=i?s(g,k,p,e,v,r):s(k,g,p,v,e,r);if(!(I===void 0?k===g||o(k,g,t,s,r):I)){f=!1;break}w||(w=p=="constructor")}if(f&&!w){var T=v.constructor,W=e.constructor;T!=W&&"constructor"in v&&"constructor"in e&&!(typeof T=="function"&&T instanceof T&&typeof W=="function"&&W instanceof W)&&(f=!1)}return r.delete(v),r.delete(e),f}var Ds=xs,Ue=hs,Os=ls,Ms=cs,As=Ds,nt=as,lt=us,ct=ns,Es=ps,Bs=1,ht="[object Arguments]",pt="[object Array]",Oe="[object Object]",zs=Object.prototype,dt=zs.hasOwnProperty;function Fs(v,e,t,s,o,r){var i=lt(v),a=lt(e),l=i?pt:nt(v),n=a?pt:nt(e);l=l==ht?Oe:l,n=n==ht?Oe:n;var c=l==Oe,h=n==Oe,p=l==n;if(p&&ct(v)){if(!ct(e))return!1;i=!0,c=!1}if(p&&!c)return r||(r=new Ue),i||Es(v)?Os(v,e,t,s,o,r):Ms(v,e,l,t,s,o,r);if(!(t&Bs)){var d=c&&dt.call(v,"__wrapped__"),u=h&&dt.call(e,"__wrapped__");if(d||u){var f=d?v.value():v,w=u?e.value():e;return r||(r=new Ue),o(f,w,t,s,r)}}return p?(r||(r=new Ue),As(v,e,t,s,o,r)):!1}var Us=Fs,Ys=Us,ut=Ve;function Ct(v,e,t,s,o){return v===e?!0:v==null||e==null||!ut(v)&&!ut(e)?v!==v&&e!==e:Ys(v,e,t,s,Ct,o)}var Xs=Ct,$s=Xs;function Gs(v,e){return $s(v,e)}var js=Gs;const le=je(js),Wt=class Lt extends L{constructor(e){super(e),m(this,"toolsType",S.Selector),m(this,"tmpPoints",[]),m(this,"subTmpPoints",[]),m(this,"workOptions"),m(this,"vNodes"),m(this,"selectIds"),m(this,"selectorColor"),m(this,"strokeColor"),m(this,"fillColor"),m(this,"oldSelectRect"),m(this,"oldSubSelectRect"),m(this,"canRotate",!1),m(this,"canTextEdit",!1),m(this,"canLock",!1),m(this,"scaleType",U.all),m(this,"toolsTypes"),m(this,"shapeOpt"),m(this,"textOpt"),m(this,"isLocked"),m(this,"thickness"),m(this,"strokeType"),m(this,"useStroke"),this.workOptions=e.toolsOpt,this.vNodes=e.vNodes}computSelector(e=!0){const t=$(this.tmpPoints);if(t.w===0||t.h===0)return{selectIds:[],intersectRect:void 0,subNodeMap:new Map};const{rectRange:s,nodeRange:o}=this.vNodes.getRectIntersectRange(t,e);return{selectIds:[...o.keys()],intersectRect:s,subNodeMap:o}}updateTempPoints(e){const t=this.tmpPoints.length,s=e.length;if(s>1){const o=new O(e[s-2]*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],e[s-1]*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[1]);t===2?this.tmpPoints.splice(1,1,o):this.tmpPoints.push(o)}}drawSelector(e){const{drawRect:t,subNodeMap:s,selectorId:o,layer:r,isService:i}=e,a=new V({pos:[t.x,t.y],anchor:[0,0],size:[t.w,t.h],id:o,name:o,zIndex:9999}),l=[];if(i){const n=new ot({normalize:!0,pos:[t.w/2,t.h/2],lineWidth:1,strokeColor:this.selectorColor||this.workOptions.strokeColor,width:t.w,height:t.h,name:Lt.selectorBorderId});l.push(n)}s.forEach((n,c)=>{const h=[n.rect.x+n.rect.w/2-t.x,n.rect.y+n.rect.h/2-t.y],p=new ot({normalize:!0,pos:h,lineWidth:1,strokeColor:s.size>1?this.selectorColor||this.workOptions.strokeColor:void 0,width:n.rect.w,height:n.rect.h,id:`selector-${c}`,name:`selector-${c}`});l.push(p)}),l&&a.append(...l),(r?.parent).appendChild(a)}draw(e,t,s,o=!1){var r,i;const{intersectRect:a,subNodeMap:l}=s;(i=(r=t.parent)==null?void 0:r.getElementById(e))==null||i.remove(),a&&this.drawSelector({drawRect:a,subNodeMap:l,selectorId:e,layer:t,isService:o})}getSelecteorInfo(e){this.scaleType=U.all,this.canRotate=!1,this.textOpt=void 0,this.strokeColor=void 0,this.fillColor=void 0,this.canTextEdit=!1,this.canLock=!1,this.isLocked=!1,this.toolsTypes=void 0,this.shapeOpt=void 0,this.thickness=void 0,this.strokeType=void 0,this.useStroke=!1;const t=new Set;let s,o=!0;for(const r of e.values()){const{opt:i,canRotate:a,scaleType:l,toolsType:n}=r;this.selectorColor=this.workOptions.strokeColor,i.strokeColor&&(this.strokeColor=i.strokeColor),i.fillColor&&(this.fillColor=i.fillColor),i.textOpt&&(this.textOpt=i.textOpt),i.thickness&&(this.thickness=i.thickness),n!==S.Pencil&&(o=!1),i.strokeType&&(this.strokeType=i.strokeType),n===S.SpeechBalloon&&(t.add(n),this.shapeOpt||(this.shapeOpt={}),this.shapeOpt.placement=i.placement),n===S.Polygon&&(t.add(n),this.shapeOpt||(this.shapeOpt={}),this.shapeOpt.vertices=i.vertices),n===S.Star&&(t.add(n),this.shapeOpt||(this.shapeOpt={}),this.shapeOpt.vertices=i.vertices,this.shapeOpt.innerRatio=i.innerRatio,this.shapeOpt.innerVerticeStep=i.innerVerticeStep),n===S.Text&&(this.textOpt=i),e.size===1&&(this.textOpt&&(this.canTextEdit=!0),this.canRotate=a,this.scaleType=l),n===S.Image&&(s=r),(l===U.proportional&&this.scaleType!==U.none||l===U.none)&&(this.scaleType=l)}o&&(this.useStroke=!0),t.size&&(this.toolsTypes=[...t]),s&&(e.size===1?(this.canLock=!0,s.opt.locked&&(this.isLocked=!0,this.scaleType=U.none,this.canRotate=!1,this.textOpt=void 0,this.fillColor=void 0,this.selectorColor="rgb(177,177,177)",this.strokeColor=void 0,this.canTextEdit=!1,this.thickness=void 0,this.strokeType=void 0,this.useStroke=void 0)):e.size>1&&!s.opt.locked&&(this.canLock=!1,this.canRotate=!1))}getChildrenPoints(){var e,t;if(this.scaleType===U.both&&((e=this.selectIds)==null?void 0:e.length)===1){const s=this.selectIds[0],o=(t=this.vNodes.get(s))==null?void 0:t.op;if(o){const r=[];for(let i=0;i<o.length;i+=3)r.push([o[i],o[i+1]]);return r}}}consume(e){if(e.isSubWorker)return this.subWorkerConsume(e);let t={type:y.Select,dataType:x.Local,...this.baseConsumeResult};if(e.isSimpleWorker){const n=this.subWorkerConsume(e);t.subRect=n.rect}const{op:s,workState:o}=e.data;let r=this.oldSelectRect;if(o===M.Start&&(r=this.unSelectedAllIds()),!(s!=null&&s.length)||!this.vNodes.curNodeMap.size)return e.isSimpleWorker?t:{type:y.None};this.updateTempPoints(s);const i=this.computSelector();if(this.selectIds&&le(this.selectIds,i.selectIds))return e.isSimpleWorker?t:{type:y.None};this.selectIds=i.selectIds;const a=i.intersectRect;this.getSelecteorInfo(i.subNodeMap),this.draw(Y,this.fullLayer,i),this.oldSelectRect=a;const l=this.getChildrenPoints();return t={...t,rect:A(a,r),selectIds:i.selectIds,selectRect:a,selectorColor:this.selectorColor,strokeColor:this.strokeColor,fillColor:this.fillColor,textOpt:this.textOpt,canTextEdit:this.canTextEdit,canRotate:this.canRotate,canLock:this.canLock,scaleType:this.scaleType,willSyncService:!0,points:l,isLocked:this.isLocked,toolsTypes:this.toolsTypes,shapeOpt:this.shapeOpt,thickness:this.thickness,useStroke:this.useStroke,strokeType:this.strokeType},t}consumeAll(){var e,t;let s=this.oldSelectRect;if(!((e=this.selectIds)!=null&&e.length)&&this.tmpPoints[0]&&this.selectSingleTool(this.tmpPoints[0].XY,Y,!1),(t=this.selectIds)!=null&&t.length&&(s=this.selectedByIds(this.selectIds)),s){const o=this.getChildrenPoints();return{type:y.Select,dataType:x.Local,rect:this.oldSelectRect,selectIds:this.selectIds,selectorColor:this.selectorColor,selectRect:this.oldSelectRect,strokeColor:this.strokeColor,fillColor:this.fillColor,textOpt:this.textOpt,canTextEdit:this.canTextEdit,canRotate:this.canRotate,canLock:this.canLock,scaleType:this.scaleType,willSyncService:!0,points:o,isLocked:this.isLocked,toolsTypes:this.toolsTypes,shapeOpt:this.shapeOpt,thickness:this.thickness,useStroke:this.useStroke,strokeType:this.strokeType,...this.baseConsumeResult}}return{type:y.None}}consumeService(){}updateTempPointsForSubWorker(e){const t=e.slice(-2),s=new O(t[0],t[1]);if(this.subTmpPoints[0].isNear(s,1))return!1;if(this.subTmpPoints.length===2){if(s.isNear(this.subTmpPoints[1],1))return!1;this.subTmpPoints[1]=s}else this.subTmpPoints.push(s);return!0}computDrawPoints(e){const{thickness:t}=this.workOptions,s=[];for(const i of e)s.push(new C(...i));const o=$(s,t),r=[o.x+o.w/2,o.y+o.h/2];return{rect:o,pos:r,points:s.map(i=>i.XY).flat(1)}}drawForSubWorker(e){const{workId:t,layer:s,ps:o}=e,{strokeColor:r,scale:i,rotate:a,translate:l}=this.workOptions,n=s.worldPosition,c=s.worldScaling,{points:h,rect:p,pos:d}=this.computDrawPoints(o),u=1/s.worldScaling[0],f=r&&Kt(r)||[0,0,0,0],w={close:!0,normalize:!0,points:h,lineWidth:u,fillColor:rt(f[0],f[1],f[2],.1),strokeColor:rt(f[0],f[1],f[2],1),lineJoin:"round",lineCap:"round"};let k={x:Math.floor(p.x*c[0]+n[0]-L.SafeBorderPadding),y:Math.floor(p.y*c[1]+n[1]-L.SafeBorderPadding),w:Math.floor(p.w*c[0]+2*L.SafeBorderPadding),h:Math.floor(p.h*c[0]+2*L.SafeBorderPadding)};const g=new V({name:t,id:t,pos:d,anchor:[.5,.5],size:[p.w,p.h],scale:i,rotate:a,translate:l}),I=new ce({...w,pos:[0,0]});if(g.appendChild(I),this.replace(s,t,g),i||a||l){const T=g.getBoundingClientRect();k={x:Math.floor(T.x-L.SafeBorderPadding),y:Math.floor(T.y-L.SafeBorderPadding),w:Math.floor(T.width+2*L.SafeBorderPadding),h:Math.floor(T.height+2*L.SafeBorderPadding)}}return k}transformData(e){const t=$(e);return[[t.x,t.y,0],[t.x+t.w,t.y,0],[t.x+t.w,t.y+t.h,0],[t.x,t.y+t.h,0]]}subWorkerConsume(e){const{data:t,isFullWork:s}=e,{op:o,syncUnitTime:r}=t;r&&(this.syncUnitTime=r);const i=o?.length;if(!i||i<2)return{type:y.None,rect:void 0};let a;if(this.subTmpPoints.length===0?(this.subTmpPoints=[new O(o[0],o[1])],a=!1):a=this.updateTempPointsForSubWorker(o),!a)return{type:y.None,rect:void 0};const l=this.transformData(this.subTmpPoints),n=s?this.fullLayer:this.drawLayer||this.fullLayer,c=this.drawForSubWorker({ps:l,workId:this.workId,layer:n}),h=A(c,this.oldSubSelectRect);return this.oldSubSelectRect=c,{rect:h,type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult}}subWorkerConsumeAll(e){const{isFullWork:t}=e,s=t?this.fullLayer:this.drawLayer||this.fullLayer;return this.replace(s,this.workId),{rect:this.oldSelectRect,type:y.FullWork,dataType:x.Local,...this.baseConsumeResult}}clearSubTmpPoints(){this.subTmpPoints.length=0}clearTmpPoints(){this.tmpPoints.length=0,this.clearSubTmpPoints()}clearSubSelectData(){this.oldSubSelectRect=void 0}clearSelectData(){this.selectIds=void 0,this.oldSelectRect=void 0,this.clearSubSelectData()}selectSingleTool(e,t=Y,s=!1){if(e.length===2){const o=e[0],r=e[1];let i;const{nodeRange:a}=this.vNodes.getRectIntersectRange({x:o,y:r,w:0,h:0},!1),l=[...a.values()].sort((n,c)=>(c.opt.zIndex||0)-(n.opt.zIndex||0));for(const n of l){const c=this.fullLayer.getElementsByName(n.name);if(Xt(c).find(h=>h.isPointCollision(o,r))){i=n;break}}if(i){const n=i.name;if(!le(this.oldSelectRect,i.rect)){const c=new Map([[n,i]]);this.getSelecteorInfo(c),this.draw(t,this.fullLayer,{intersectRect:i.rect,subNodeMap:c,selectIds:this.selectIds||[]},s)}this.selectIds=[n],this.oldSelectRect=i.rect}}}unSelectedAllIds(){let e;for(const[t,s]of this.vNodes.curNodeMap.entries())s.isSelected&&(e=A(e,s.rect),this.vNodes.unSelected(t));return e}unSelectedByIds(e){let t;for(const s of e){const o=this.vNodes.get(s);o&&o.isSelected&&(t=A(t,o.rect),this.vNodes.unSelected(s))}return t}selectedByIds(e){let t;for(const s of e){const o=this.vNodes.get(s);o&&(t=A(t,o.rect),this.vNodes.selected(s))}return t}getSelectorRect(e,t){var s;let o;const r=(s=e.parent)==null?void 0:s.getElementById(t),i=r?.getBoundingClientRect();return i&&(o=A(o,{x:Math.floor(i.x),y:Math.floor(i.y),w:Math.floor(i.width+1),h:Math.floor(i.height+1)})),o}isCanFillColor(e){return e===S.Ellipse||e===S.Triangle||e===S.Rectangle||e===S.Polygon||e===S.Star||e===S.SpeechBalloon}async updateSelector(e){const{updateSelectorOpt:t,selectIds:s,vNodes:o,willSerializeData:r,worker:i,offset:a}=e,l=this.fullLayer;if(!l)return;let n;const c=new Map,{originPoint:h,workState:p,angle:d,translate:u,dir:f,scale:w}=t;a&&(u?t.translate=[u[0]+a[0],u[1]+a[1]]:t.translate=a);let k;if(h||u||q(d)){if(p===M.Start&&s)return o.setTargetAssignKeys(s),{type:y.Select,dataType:x.Local,selectRect:this.oldSelectRect,rect:this.oldSelectRect};if(k=o.getLastTarget(),!k)return}if(s)for(const T of s){const W=o.get(T);if(W){const{toolsType:N,opt:R}=W,P=(l?.getElementsByName(T))[0];if(P){const b={...t};let D;const B=L.isWillRefresh({toolsType:N,opt:R,updateOpt:b,vNodes:o,node:P,willSerializeData:r});if(N){D=k?.get(T);const E=Yt(N);if(E?.updateNodeOpt({node:P,opt:b,vNodes:o,willSerializeData:r,targetNode:D}),W&&i&&B){const X=i.createWorkShapeNode({workId:T,toolsType:N,toolsOpt:W.opt});X?.setWorkId(T);let _;if(N===S.Image)_=await X.consumeServiceAsync({isFullWork:!0,replaceId:T,worker:i});else if(N===S.Text)_=await X.consumeServiceAsync({isFullWork:!0,replaceId:T,isDrawLabel:!0});else try{_=X?.consumeService({op:W.op,isFullWork:!0,replaceId:T,workState:p})}catch(G){console.error("consumeService error",G);continue}_&&(W.rect=_)}W&&(c.set(T,W),n=A(n,W.rect))}}}}k&&p===M.Done&&(o.deleteLastTarget(),k=void 0);const g=n;if(h&&u&&w&&f&&g&&!a){const T=[[g.x,g.y],[g.x+g.w,g.y],[g.x+g.w,g.y+g.h],[g.x,g.y+g.h]];let W;switch(f){case"top":case"topLeft":case"left":u[0]>0&&u[1]>0?W=T[0]:u[0]>0?W=T[3]:u[1]>0?W=T[1]:W=T[2];break;case"topRight":u[0]<0&&u[1]>0?W=T[1]:u[0]<0?W=T[2]:u[1]>0?W=T[0]:W=T[3];break;case"right":case"bottomRight":case"bottom":u[0]<0&&u[1]<0?W=T[2]:u[0]<0?W=T[1]:u[1]<0?W=T[3]:W=T[0];break;case"bottomLeft":u[0]>0&&u[1]<0?W=T[3]:u[0]>0?W=T[0]:u[1]<0?W=T[2]:W=T[1];break}const N=W&&[h[0]-W[0],h[1]-W[1]]||[0,0];if(!le(N,[0,0]))return await this.updateSelector({...e,updateSelectorOpt:{workState:p},offset:N})}this.getSelecteorInfo(c),this.draw(Y,l,{selectIds:s||[],subNodeMap:c,intersectRect:g});const I=A(this.oldSelectRect,g);return this.oldSelectRect=g,{type:y.Select,dataType:x.Local,selectRect:g,renderRect:n,rect:A(I,g),selectIds:s}}blurSelector(){const e=this.unSelectedAllIds();return{type:y.Select,dataType:x.Local,rect:e,selectIds:[],willSyncService:!0}}getRightServiceId(e){return e.replace(ve,"-")}selectServiceNode(e,t,s){const{selectIds:o}=t,r=this.getRightServiceId(e),i=this.getSelectorRect(this.fullLayer,r);let a;const l=new Map;return o?.forEach(n=>{const c=this.vNodes.get(n);c&&(a=A(a,c.rect),l.set(n,c))}),this.getSelecteorInfo(l),this.draw(r,this.fullLayer,{intersectRect:a,selectIds:o||[],subNodeMap:l},s),A(a,i)}reRenderSelector(){var e;let t;const s=new Map;return(e=this.selectIds)==null||e.forEach(o=>{const r=this.vNodes.get(o);r&&(t=A(t,r.rect),s.set(o,r))},this),this.getSelecteorInfo(s),this.draw(Y,this.fullLayer,{intersectRect:t,subNodeMap:s,selectIds:this.selectIds||[]}),this.oldSelectRect=t,t}updateSelectIds(e){var t;let s;const o=(t=this.selectIds)==null?void 0:t.filter(i=>!e.includes(i));if(o!=null&&o.length&&(s=this.unSelectedByIds(o)),e.length){const i=this.selectedByIds(e);s=A(s,i)}this.selectIds=e;const r=this.reRenderSelector();return{bgRect:s,selectRect:r}}cursorHover(e){var t,s;const o=this.oldSelectRect;this.selectIds=[];const r=(t=this.workId)==null?void 0:t.toString(),i=[e[0]*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],e[1]*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[1]];if(this.selectSingleTool(i,r,!0),this.oldSelectRect&&!le(o,this.oldSelectRect))return{type:y.CursorHover,dataType:x.Local,rect:A(o,this.oldSelectRect),selectorColor:this.selectorColor,willSyncService:!1};if((s=this.selectIds)!=null&&s.length||(this.oldSelectRect=void 0),o&&!this.oldSelectRect)return this.cursorBlur(),{type:y.CursorHover,dataType:x.Local,rect:o,selectorColor:this.selectorColor,willSyncService:!1}}cursorBlur(){var e,t;this.selectIds=[];const s=(e=this.workId)==null?void 0:e.toString();((t=this.fullLayer)==null?void 0:t.parent).children.forEach(o=>{o.name===s&&o.remove()})}};m(Wt,"selectorBorderId","selector-border");let bt=Wt;class Nt extends L{constructor(e){super(e),m(this,"canRotate",!1),m(this,"scaleType",U.both),m(this,"toolsType",S.Arrow),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"oldRect"),m(this,"arrowTipWidth"),m(this,"syncTimestamp"),this.workOptions=e.toolsOpt,this.arrowTipWidth=this.workOptions.thickness*4,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:s,isSubWorker:o,isMainThread:r,smoothSync:i,isSimpleWorker:a}=e,l=this.workId,{op:n,syncUnitTime:c}=t;c&&(this.syncUnitTime=c);const h=n?.length;if(!h||h<2)return{type:y.None};let p;if(this.tmpPoints.length===0?(this.tmpPoints=[new O(n[0],n[1])],p=!1):p=this.updateTempPoints(n),!p)return{type:y.None};let d;if(o||r||a){const f=s?this.fullLayer:this.drawLayer||this.fullLayer;d=this.draw({workId:l,layer:f})}if(a){const f=A(d,this.oldRect);this.oldRect=d;const w={rect:f,type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult};if(i){const k=Date.now();k-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=k,w.op=this.tmpPoints.map(g=>[...g.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!o&&i){const f=Date.now();return f-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=f,{...this.baseConsumeResult,type:y.DrawWork,dataType:x.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0}):{type:y.None}}const u=A(d,this.oldRect);return this.oldRect=d,{rect:u,...this.baseConsumeResult,type:y.DrawWork,dataType:x.Local}}consumeAll(){var e;const t=this.workId;if(this.tmpPoints.length<2)return{type:y.RemoveNode,removeIds:[t]};const s=this.fullLayer,o=this.draw({workId:t,layer:s});this.oldRect=o;const r=this.tmpPoints.map(a=>[...a.XY,0]).flat(1),i=Q(r);return(e=this.vNodes)==null||e.setInfo(t,{rect:o,op:r,opt:this.workOptions,toolsType:this.toolsType,canRotate:this.canRotate,scaleType:this.scaleType,centerPos:L.getCenterPos(o,s)}),{rect:o,...this.baseConsumeResult,type:y.FullWork,dataType:x.Local,ops:i,isSync:!0,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:s,isDrawEraserlines:o}=e,{strokeColor:r,thickness:i,zIndex:a,scale:l,rotate:n,translate:c,strokeType:h,eraserlines:p,lineDash:d,lineCap:u}=this.workOptions,f=s.worldPosition,w=s.worldScaling,{points:k,pos:g,rect:I,isTriangle:T,trianglePoints:W,trianglePos:N}=this.computDrawPoints(i),R=[I.x+I.w/2,I.y+I.h/2],P={pos:R,name:t,id:t,zIndex:a,anchor:[.5,.5],size:[I.w,I.h]};l&&(P.scale=l),n&&(P.rotate=n),c&&(P.translate=c);const b=new V(P),D={points:W,pos:[N[0]-R[0],N[1]-R[1]],fillColor:r,strokeColor:r,lineWidth:0,normalize:!1},B=new ce(D);if(b.append(B),!T&&k&&g){const X={points:k,pos:[g[0]-R[0],g[1]-R[1]],fillColor:r,strokeColor:r,lineDash:h===F.Dotted?[d&&d[0]||1,(d&&d[1]||2)*i]:h===F.LongDotted?[(d&&d[0]||1)*i,(d&&d[1]||2)*i]:void 0,lineCap:h===F.Normal?void 0:u,lineWidth:i,normalize:!1},_=new ce(X);b.append(_)}p&&(this.scaleType=U.proportional,this.drawEraserlines({group:b,eraserlines:p,pos:R,layer:s},o)),this.replace(s,t,b);let E={x:Math.floor(I.x*w[0]+f[0]-L.SafeBorderPadding),y:Math.floor(I.y*w[1]+f[1]-L.SafeBorderPadding),w:Math.floor(I.w*w[0]+2*L.SafeBorderPadding),h:Math.floor(I.h*w[1]+2*L.SafeBorderPadding)};if(l||n||c){const X=b.getBoundingClientRect();E={x:Math.floor(X.x-L.SafeBorderPadding),y:Math.floor(X.y-L.SafeBorderPadding),w:Math.floor(X.width+L.SafeBorderPadding*2),h:Math.floor(X.height+L.SafeBorderPadding*2)}}return this.isDelete&&b.setAttribute("opacity",0),E}computDrawPoints(e){return this.tmpPoints[1].distance(this.tmpPoints[0])>this.arrowTipWidth?this.computFullArrowPoints(e):this.computTrianglePoints()}computFullArrowPoints(e){const t=C.Sub(this.tmpPoints[1],this.tmpPoints[0]).uni(),s=C.Per(t).mul(e/2),o=O.Sub(this.tmpPoints[0],s),r=O.Add(this.tmpPoints[0],s),i=C.Mul(t,this.arrowTipWidth),a=C.Sub(this.tmpPoints[1],i),l=O.Sub(a,s),n=O.Add(a,s),c=C.Per(t).mul(e*1.5),h=O.Sub(a,c),p=O.Add(a,c),d=[this.tmpPoints[0],a],u=[h,this.tmpPoints[1],p],f=[o,r,...u,l,n];return{trianglePoints:u.map(w=>O.Sub(w,this.tmpPoints[1]).XY).flat(1),trianglePos:this.tmpPoints[1].XY,points:d.map(w=>O.Sub(w,this.tmpPoints[0]).XY).flat(1),rect:$(f),isTriangle:!1,pos:this.tmpPoints[0].XY}}computTrianglePoints(){const e=C.Sub(this.tmpPoints[1],this.tmpPoints[0]).uni(),t=this.tmpPoints[1].distance(this.tmpPoints[0]),s=C.Per(e).mul(Math.floor(t*3/8)),o=O.Sub(this.tmpPoints[0],s),r=O.Add(this.tmpPoints[0],s),i=[o,this.tmpPoints[1],r];return{trianglePoints:i.map(a=>O.Sub(a,this.tmpPoints[1]).XY).flat(1),trianglePos:this.tmpPoints[1].XY,rect:$(i),isTriangle:!0}}updateTempPoints(e){const t=e.slice(-2),s=new O(t[0],t[1]),o=this.tmpPoints[0],{thickness:r}=this.workOptions;if(o.isNear(s,r))return!1;if(this.tmpPoints.length===2){if(s.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=s}else this.tmpPoints.push(s);return!0}consumeService(e){var t,s;const{op:o,isFullWork:r,workState:i=M.Done}=e,a=(t=this.workId)==null?void 0:t.toString();if(!a)return;this.tmpPoints.length=0;for(let c=0;c<o.length;c+=3)this.tmpPoints.push(new O(o[c],o[c+1],o[c+2]));const l=r?this.fullLayer:this.drawLayer||this.fullLayer,n=this.draw({workId:a,layer:l,isDrawEraserlines:i===M.Done});return this.oldRect=n,(s=this.vNodes)==null||s.setInfo(a,{rect:n,op:o,opt:this.workOptions,toolsType:this.toolsType,canRotate:this.canRotate,scaleType:this.scaleType,centerPos:L.getCenterPos(n,l)}),n}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var t,s;const{node:o,opt:r,vNodes:i}=e,{strokeColor:a,strokeType:l}=r,n=i.get(o.name);return a&&(o.tagName==="GROUP"?o.children.forEach(c=>{c.setAttribute("strokeColor",a),c.getAttribute("fillColor")&&c.setAttribute("fillColor",a)}):(o.setAttribute("strokeColor",a),o.setAttribute("fillColor",a)),(t=n?.opt)!=null&&t.strokeColor&&(n.opt.strokeColor=a)),l&&n!=null&&n.opt&&(s=n.opt)!=null&&s.strokeType&&(n.opt.strokeType=l),n&&i.setInfo(o.name,n),L.updateNodeOpt(e)}}class Rt extends L{constructor(e){super(e),m(this,"canRotate",!1),m(this,"scaleType",U.all),m(this,"toolsType",S.Ellipse),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"oldRect"),m(this,"syncTimestamp"),this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:s,isSubWorker:o,isMainThread:r,smoothSync:i,isSimpleWorker:a}=e,l=this.workId,{op:n,syncUnitTime:c}=t;c&&(this.syncUnitTime=c);const h=n?.length;if(!h||h<2)return{type:y.None};let p;if(this.tmpPoints.length===0?(this.tmpPoints=[new O(n[0],n[1])],p=!1):p=this.updateTempPoints(n),!p)return{type:y.None};let d;if(o||r||a){const u=s?this.fullLayer:this.drawLayer||this.fullLayer;d=this.draw({workId:l,layer:u,isDrawing:!0});const f=u.parent.parent,w={x:0,y:0,w:Math.floor(f.width),h:Math.floor(f.height)},k={type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult};if(Ce(d,w)!==ze.outside){const g=A(d,this.oldRect);if(this.oldRect=d,k.rect=g,!a)return k}if(a){const g=Date.now();return g-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=g,k.op=this.tmpPoints.map(I=>[...I.XY,0]).flat(1),k.index=0,k.isSync=!0),k}return{type:y.None}}if(!o&&!r&&!a&&i){const u=Date.now();return u-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=u,{type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult,op:this.tmpPoints.map(f=>[...f.XY,0]).flat(1),isSync:!0,index:0}):{type:y.None}}return{type:y.None}}consumeAll(){var e;const t=this.workId;if(this.tmpPoints.length<2)return{type:y.RemoveNode,removeIds:[t]};const s=this.fullLayer,o=this.draw({workId:t,layer:s,isDrawing:!1});this.oldRect=o;const r=this.tmpPoints.map(a=>[...a.XY,0]).flat(1),i=Q(r);return(e=this.vNodes)==null||e.setInfo(t,{rect:o,op:r,opt:this.workOptions,toolsType:this.toolsType,canRotate:this.canRotate,scaleType:this.scaleType,centerPos:o&&L.getCenterPos(o,s)}),{rect:o,type:y.FullWork,dataType:x.Local,...this.baseConsumeResult,ops:i,isSync:!0,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:s,isDrawing:o,isDrawEraserlines:r}=e,{strokeColor:i,fillColor:a,thickness:l,zIndex:n,scale:c,rotate:h,translate:p,strokeType:d,eraserlines:u,lineDash:f,lineCap:w}=this.workOptions,k=s.worldScaling,{radius:g,rect:I,pos:T}=this.computDrawPoints(l),W={closeType:"normal",radius:g,lineWidth:l,fillColor:a!=="transparent"&&a||void 0,strokeColor:i,normalize:!0,lineCap:w,lineDash:d===F.Dotted?[f&&f[0]||1,(f&&f[1]||2)*l]:d===F.LongDotted?[(f&&f[0]||1)*l,(f&&f[1]||2)*l]:void 0},N={name:t,id:t,zIndex:n,pos:T,anchor:[.5,.5],size:[I.w,I.h]};c&&(N.scale=c),h&&(N.rotate=h),p&&(N.translate=p);const R=new V(N);if(o){const D=new ye({d:"M-4,0H4M0,-4V4",normalize:!0,pos:[0,0],strokeColor:i,lineWidth:1,scale:[1/k[0],1/k[1]]});R.append(D)}const P=new es({...W,pos:[0,0]});R.append(P),u&&(this.scaleType=U.proportional,this.drawEraserlines({group:R,eraserlines:u,pos:T,layer:s},r)),this.replace(s,t,R);const b=R.getBoundingClientRect();return this.isDelete&&R.setAttribute("opacity",0),{x:Math.floor(b.x-L.SafeBorderPadding),y:Math.floor(b.y-L.SafeBorderPadding),w:Math.floor(b.width+L.SafeBorderPadding*2),h:Math.floor(b.height+L.SafeBorderPadding*2)}}computDrawPoints(e){const t=$(this.tmpPoints),s=$(this.tmpPoints,e),o=[Math.floor(t.x+t.w/2),Math.floor(t.y+t.h/2)];return{rect:s,pos:o,radius:[Math.floor(t.w/2),Math.floor(t.h/2)]}}updateTempPoints(e){const t=e.slice(-2),s=new O(t[0],t[1]),o=this.tmpPoints[0],{thickness:r}=this.workOptions;if(o.isNear(s,r))return!1;if(this.tmpPoints.length===2){if(s.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=s}else this.tmpPoints.push(s);return!0}consumeService(e){var t,s;const{op:o,isFullWork:r,workState:i=M.Done}=e,a=(t=this.workId)==null?void 0:t.toString();if(!a)return;this.tmpPoints.length=0;for(let c=0;c<o.length;c+=3)this.tmpPoints.push(new O(o[c],o[c+1],o[c+2]));const l=r?this.fullLayer:this.drawLayer||this.fullLayer,n=this.draw({workId:a,layer:l,isDrawing:!1,isDrawEraserlines:i===M.Done});return this.oldRect=n,(s=this.vNodes)==null||s.setInfo(a,{rect:n,op:o,opt:this.workOptions,toolsType:this.toolsType,canRotate:this.canRotate,scaleType:this.scaleType,centerPos:L.getCenterPos(n,l)}),n}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var t,s,o;const{node:r,opt:i,vNodes:a}=e,{strokeColor:l,fillColor:n,strokeType:c}=i,h=a.get(r.name);let p=r;return r.tagName==="GROUP"&&(p=r.children[0]),l&&(p.setAttribute("strokeColor",l),(t=h?.opt)!=null&&t.strokeColor&&(h.opt.strokeColor=l)),n&&(n==="transparent"?p.setAttribute("fillColor","rgba(0,0,0,0)"):p.setAttribute("fillColor",n),(s=h?.opt)!=null&&s.fillColor&&(h.opt.fillColor=n)),c&&h!=null&&h.opt&&(o=h.opt)!=null&&o.strokeType&&(h.opt.strokeType=c),h&&a.setInfo(r.name,h),L.updateNodeOpt(e)}}class xt extends L{constructor(e){super(e),m(this,"canRotate",!0),m(this,"scaleType",U.all),m(this,"toolsType",S.Rectangle),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"oldRect"),m(this,"syncTimestamp"),this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}transformData(){const e=$(this.tmpPoints);return[[e.x,e.y,0],[e.x+e.w,e.y,0],[e.x+e.w,e.y+e.h,0],[e.x,e.y+e.h,0]]}computDrawPoints(e){const{thickness:t}=this.workOptions,s=[];for(const i of e)s.push(new C(...i));const o=$(s,t),r=[o.x+o.w/2,o.y+o.h/2];return{rect:o,pos:r,points:s.map(i=>i.XY).flat(1)}}consume(e){const{data:t,isFullWork:s,isSubWorker:o,isMainThread:r,smoothSync:i,isSimpleWorker:a}=e,l=this.workId,{op:n}=t,c=n?.length;if(!c||c<2)return{type:y.None};let h;if(this.tmpPoints.length===0?(this.tmpPoints=[new O(n[0],n[1])],h=!1):h=this.updateTempPoints(n),!h)return{type:y.None};const p=this.transformData();let d;if(o||r||a){const f=s?this.fullLayer:this.drawLayer||this.fullLayer;d=this.draw({ps:p,workId:l,layer:f,isDrawing:!0})}if(a){const f=A(d,this.oldRect);this.oldRect=d;const w={rect:f,type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult};if(i){const k=Date.now();k-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=k,w.op=p.flat(1),w.index=0,w.isSync=!0)}return w}if(!o&&i){const f=Date.now();return f-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=f,{type:y.DrawWork,dataType:x.Local,op:p.flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:y.None}}const u=A(d,this.oldRect);return this.oldRect=d,{rect:u,type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult}}consumeAll(){var e;const t=this.workId;if(this.tmpPoints.length<2)return{type:y.RemoveNode,removeIds:[t]};const s=this.transformData(),o=this.fullLayer,r=this.draw({ps:s,workId:t,layer:o,isDrawing:!1});this.oldRect=r;const i=s.flat(1),a=Q(i);return(e=this.vNodes)==null||e.setInfo(t,{rect:r,op:i,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:r&&L.getCenterPos(r,o)}),{rect:r,type:y.FullWork,dataType:x.Local,ops:a,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:s,isDrawing:o,ps:r,replaceId:i,isDrawEraserlines:a=!0}=e,{strokeColor:l,fillColor:n,thickness:c,zIndex:h,scale:p,rotate:d,translate:u,strokeType:f,eraserlines:w,lineCap:k,lineDash:g}=this.workOptions,I=s.worldPosition,T=s.worldScaling,{points:W,rect:N,pos:R}=this.computDrawPoints(r),P={close:!0,normalize:!0,points:W,lineWidth:c,fillColor:n!=="transparent"&&n||void 0,strokeColor:l,lineCap:k,lineDash:f===F.Dotted?[g&&g[0]||1,(g&&g[1]||2)*c]:f===F.LongDotted?[(g&&g[0]||1)*c,(g&&g[1]||2)*c]:void 0};let b={x:Math.floor(N.x*T[0]+I[0]-L.SafeBorderPadding),y:Math.floor(N.y*T[1]+I[1]-L.SafeBorderPadding),w:Math.floor(N.w*T[0]+2*L.SafeBorderPadding),h:Math.floor(N.h*T[0]+2*L.SafeBorderPadding)};const D=new V({name:t,id:t,zIndex:h,pos:R,anchor:[.5,.5],size:[N.w,N.h],scale:p,rotate:d,translate:u}),B=new ce({...P,pos:[0,0]});if(D.appendChild(B),o){const E=new ye({d:"M-4,0H4M0,-4V4",normalize:!0,pos:[0,0],strokeColor:l,lineWidth:1,scale:[1/T[0],1/T[1]]});D.appendChild(E)}if(w&&(this.scaleType=U.proportional,this.drawEraserlines({group:D,eraserlines:w,pos:R,layer:s},a)),this.replace(s,i||t,D),p||d||u){const E=D.getBoundingClientRect();b={x:Math.floor(E.x-L.SafeBorderPadding),y:Math.floor(E.y-L.SafeBorderPadding),w:Math.floor(E.width+2*L.SafeBorderPadding),h:Math.floor(E.height+2*L.SafeBorderPadding)}}return this.isDelete&&D.setAttribute("opacity",0),b}updateTempPoints(e){const t=e.slice(-2),s=new O(t[0],t[1]),o=this.tmpPoints[0],{thickness:r}=this.workOptions;if(o.isNear(s,r))return!1;if(this.tmpPoints.length===2){if(s.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=s}else this.tmpPoints.push(s);return!0}consumeService(e){var t,s;const{op:o,isFullWork:r,replaceId:i,workState:a=M.Done}=e,l=(t=this.workId)==null?void 0:t.toString();if(!l)return;const n=[];for(let p=0;p<o.length;p+=3)n.push([o[p],o[p+1],o[p+2]]);const c=r?this.fullLayer:this.drawLayer||this.fullLayer,h=this.draw({ps:n,workId:l,layer:c,isDrawing:!1,replaceId:i,isDrawEraserlines:a===M.Done});return this.oldRect=h,(s=this.vNodes)==null||s.setInfo(l,{rect:h,op:o,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:h&&L.getCenterPos(h,c)}),h}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var t,s,o;const{node:r,opt:i,vNodes:a}=e,{strokeColor:l,fillColor:n,strokeType:c}=i,h=a.get(r.name);let p=r;return r.tagName==="GROUP"&&(p=r.children[0]),l&&(p.setAttribute("strokeColor",l),(t=h?.opt)!=null&&t.strokeColor&&(h.opt.strokeColor=l)),n&&(n==="transparent"?p.setAttribute("fillColor","rgba(0,0,0,0)"):p.setAttribute("fillColor",n),(s=h?.opt)!=null&&s.fillColor&&(h.opt.fillColor=n)),c&&h!=null&&h.opt&&(o=h.opt)!=null&&o.strokeType&&(h.opt.strokeType=c),h&&a.setInfo(r.name,h),L.updateNodeOpt(e)}}class Dt extends L{constructor(e){super(e),m(this,"canRotate",!1),m(this,"scaleType",U.all),m(this,"toolsType",S.Star),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"oldRect"),m(this,"syncTimestamp"),this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:s,isSubWorker:o,isMainThread:r,smoothSync:i,isSimpleWorker:a}=e,l=this.workId,{op:n,syncUnitTime:c}=t,h=n?.length;if(!h||h<2)return{type:y.None};c&&(this.syncUnitTime=c);let p;if(this.tmpPoints.length===0?(this.tmpPoints=[new O(n[0],n[1])],p=!1):p=this.updateTempPoints(n),!p)return{type:y.None};let d;if(o||r||a){const f=s?this.fullLayer:this.drawLayer||this.fullLayer;d=this.draw({workId:l,layer:f,isDrawing:!0})}if(a){const f=A(d,this.oldRect);this.oldRect=d;const w={rect:f,type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult};if(i){const k=Date.now();k-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=k,w.op=this.tmpPoints.map(g=>[...g.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!o&&i){const f=Date.now();return f-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=f,{type:y.DrawWork,dataType:x.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:y.None}}const u=A(d,this.oldRect);return this.oldRect=d,{rect:u,type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult}}consumeAll(){var e;const t=this.workId;if(this.tmpPoints.length<2)return{type:y.RemoveNode,removeIds:[t]};const s=this.fullLayer,o=this.draw({workId:t,layer:s,isDrawing:!1});this.oldRect=o;const r=this.tmpPoints.map(a=>[...a.XY,0]).flat(1),i=Q(r);return(e=this.vNodes)==null||e.setInfo(t,{rect:o,op:r,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:o&&L.getCenterPos(o,s)}),{rect:o,type:y.FullWork,dataType:x.Local,ops:i,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:s,isDrawing:o,isDrawEraserlines:r=!0}=e,{strokeColor:i,fillColor:a,thickness:l,zIndex:n,vertices:c,innerVerticeStep:h,innerRatio:p,scale:d,rotate:u,translate:f,strokeType:w,eraserlines:k,lineDash:g,lineCap:I}=this.workOptions,T=s.worldScaling,{rect:W,pos:N,points:R}=this.computDrawPoints(l,c,h,p),P={close:!0,points:R,lineWidth:l,fillColor:a!=="transparent"&&a||void 0,strokeColor:i,normalize:!0,lineCap:I,lineDash:w===F.Dotted?[g&&g[0]||1,(g&&g[1]||2)*l]:w===F.LongDotted?[(g&&g[0]||1)*l,(g&&g[1]||2)*l]:void 0},b={name:t,id:t,zIndex:n,pos:N,anchor:[.5,.5],size:[W.w,W.h]};d&&(b.scale=d),u&&(b.rotate=u),f&&(b.translate=f);const D=new V(b);if(o){const X=new ye({d:"M-4,0H4M0,-4V4",normalize:!0,pos:[0,0],strokeColor:i,lineWidth:1,scale:[1/T[0],1/T[1]]});D.append(X)}const B=new ce({...P,pos:[0,0]});D.append(B),k&&(this.scaleType=U.proportional,this.drawEraserlines({group:D,eraserlines:k,pos:N,layer:s},r)),this.replace(s,t,D);const E=D.getBoundingClientRect();return this.isDelete&&D.setAttribute("opacity",0),{x:Math.floor(E.x-L.SafeBorderPadding),y:Math.floor(E.y-L.SafeBorderPadding),w:Math.floor(E.width+L.SafeBorderPadding*2),h:Math.floor(E.height+L.SafeBorderPadding*2)}}computDrawPoints(e,t,s,o){const r=$(this.tmpPoints),i=[Math.floor(r.x+r.w/2),Math.floor(r.y+r.h/2)],a=wt(r.w,r.h),l=Math.floor(Math.min(r.w,r.h)/2),n=o*l,c=[],h=2*Math.PI/t;for(let p=0;p<t;p++){const d=p*h-.5*Math.PI;let u,f;p%s===1?(u=n*a[0]*Math.cos(d),f=n*a[1]*Math.sin(d)):(u=l*a[0]*Math.cos(d),f=l*a[1]*Math.sin(d),c.push(u,f)),c.push(u,f)}return{rect:$(this.tmpPoints,e),pos:i,points:c}}updateTempPoints(e){const t=e.slice(-2),s=new O(t[0],t[1]),o=this.tmpPoints[0],{thickness:r}=this.workOptions;if(o.isNear(s,r)||O.Sub(o,s).XY.includes(0))return!1;if(this.tmpPoints.length===2){if(s.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=s}else this.tmpPoints.push(s);return!0}consumeService(e){var t,s;const{op:o,isFullWork:r,workState:i=M.Done}=e,a=(t=this.workId)==null?void 0:t.toString();if(!a)return;this.tmpPoints.length=0;for(let c=0;c<o.length;c+=3)this.tmpPoints.push(new O(o[c],o[c+1],o[c+2]));const l=r?this.fullLayer:this.drawLayer||this.fullLayer,n=this.draw({workId:a,layer:l,isDrawing:!1,isDrawEraserlines:i===M.Done});return this.oldRect=n,(s=this.vNodes)==null||s.setInfo(a,{rect:n,op:o,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:n&&L.getCenterPos(n,l)}),n}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var t;const{node:s,opt:o,vNodes:r}=e,{strokeColor:i,fillColor:a,toolsType:l,vertices:n,innerVerticeStep:c,innerRatio:h,strokeType:p}=o,d=r.get(s.name),u=d?.opt;let f=s;return s.tagName==="GROUP"&&(f=s.children[0]),i&&(f.setAttribute("strokeColor",i),u!=null&&u.strokeColor&&(u.strokeColor=i)),a&&(a==="transparent"?f.setAttribute("fillColor","rgba(0,0,0,0)"):f.setAttribute("fillColor",a),u!=null&&u.fillColor&&(u.fillColor=a)),l===S.Star&&(n&&(u.vertices=n),c&&(u.innerVerticeStep=c),h&&(u.innerRatio=h)),p&&d!=null&&d.opt&&(t=d.opt)!=null&&t.strokeType&&(d.opt.strokeType=p),d&&r.setInfo(s.name,{...d,opt:u}),L.updateNodeOpt(e)}}class Ot extends L{constructor(e){super(e),m(this,"canRotate",!1),m(this,"scaleType",U.all),m(this,"toolsType",S.Polygon),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"oldRect"),m(this,"syncTimestamp"),this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:s,isSubWorker:o,isMainThread:r,smoothSync:i,isSimpleWorker:a}=e,{op:l,syncUnitTime:n}=t;n&&(this.syncUnitTime=n);const c=this.workId,h=l?.length;if(!h||h<2)return{type:y.None};let p;if(this.tmpPoints.length===0?(this.tmpPoints=[new O(l[0],l[1])],p=!1):p=this.updateTempPoints(l),!p)return{type:y.None};let d;if(o||r||a){const f=s?this.fullLayer:this.drawLayer||this.fullLayer;d=this.draw({workId:c,layer:f,isDrawing:!0})}if(a){const f=A(d,this.oldRect);this.oldRect=d;const w={type:y.DrawWork,rect:f,dataType:x.Local,...this.baseConsumeResult};if(i){const k=Date.now();k-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=k,w.op=this.tmpPoints.map(g=>[...g.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!o&&i){const f=Date.now();return f-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=f,{type:y.DrawWork,dataType:x.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:y.None}}const u=A(d,this.oldRect);return this.oldRect=d,{rect:u,type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult}}consumeAll(){var e;const t=this.workId;if(this.tmpPoints.length<2)return{type:y.RemoveNode,removeIds:[t]};const s=this.fullLayer,o=this.draw({workId:t,layer:s,isDrawing:!1});this.oldRect=o;const r=this.tmpPoints.map(a=>[...a.XY,0]).flat(1),i=Q(r);return(e=this.vNodes)==null||e.setInfo(t,{rect:o,op:r,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:o&&L.getCenterPos(o,s)}),{rect:o,type:y.FullWork,dataType:x.Local,ops:i,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:s,isDrawing:o,isDrawEraserlines:r=!0}=e,{strokeColor:i,fillColor:a,thickness:l,zIndex:n,vertices:c,scale:h,rotate:p,translate:d,strokeType:u,eraserlines:f,lineCap:w,lineDash:k}=this.workOptions,g=s.worldScaling,{rect:I,pos:T,points:W}=this.computDrawPoints(l,c),N={close:!0,points:W,lineWidth:l,fillColor:a!=="transparent"&&a||void 0,strokeColor:i,normalize:!0,lineCap:w,lineDash:u===F.Dotted?[k&&k[0]||1,(k&&k[1]||2)*l]:u===F.LongDotted?[(k&&k[0]||1)*l,(k&&k[1]||2)*l]:void 0},R={name:t,id:t,zIndex:n,pos:T,anchor:[.5,.5],size:[I.w,I.h]};h&&(R.scale=h),p&&(R.rotate=p),d&&(R.translate=d);const P=new V(R);if(o){const B=new ye({d:"M-4,0H4M0,-4V4",normalize:!0,pos:[0,0],strokeColor:i,lineWidth:1,scale:[1/g[0],1/g[1]]});P.append(B)}const b=new ce({...N,pos:[0,0]});P.append(b),f&&(this.scaleType=U.proportional,this.drawEraserlines({group:P,eraserlines:f,pos:T,layer:s},r)),this.replace(s,t,P);const D=P.getBoundingClientRect();return this.isDelete&&P.setAttribute("opacity",0),{x:Math.floor(D.x-L.SafeBorderPadding),y:Math.floor(D.y-L.SafeBorderPadding),w:Math.floor(D.width+L.SafeBorderPadding*2),h:Math.floor(D.height+L.SafeBorderPadding*2)}}computDrawPoints(e,t){const s=$(this.tmpPoints),o=[Math.floor(s.x+s.w/2),Math.floor(s.y+s.h/2)],r=wt(s.w,s.h),i=Math.floor(Math.min(s.w,s.h)/2),a=[],l=2*Math.PI/t;for(let n=0;n<t;n++){const c=n*l-.5*Math.PI,h=i*r[0]*Math.cos(c),p=i*r[1]*Math.sin(c);a.push(h,p)}return{rect:$(this.tmpPoints,e),pos:o,points:a}}updateTempPoints(e){const t=e.slice(-2),s=new O(t[0],t[1]),o=this.tmpPoints[0],{thickness:r}=this.workOptions;if(o.isNear(s,r)||O.Sub(o,s).XY.includes(0))return!1;if(this.tmpPoints.length===2){if(s.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=s}else this.tmpPoints.push(s);return!0}consumeService(e){var t,s;const{op:o,isFullWork:r,workState:i=M.Done}=e,a=(t=this.workId)==null?void 0:t.toString();if(!a)return;this.tmpPoints.length=0;for(let c=0;c<o.length;c+=3)this.tmpPoints.push(new O(o[c],o[c+1],o[c+2]));const l=r?this.fullLayer:this.drawLayer||this.fullLayer,n=this.draw({workId:a,layer:l,isDrawing:!1,isDrawEraserlines:i===M.Done});return this.oldRect=n,(s=this.vNodes)==null||s.setInfo(a,{rect:n,op:o,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:n&&L.getCenterPos(n,l)}),n}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var t;const{node:s,opt:o,vNodes:r}=e,{strokeColor:i,fillColor:a,toolsType:l,vertices:n,strokeType:c}=o,h=r.get(s.name),p=h?.opt;let d=s;return s.tagName==="GROUP"&&(d=s.children[0]),i&&(d.setAttribute("strokeColor",i),p!=null&&p.strokeColor&&(p.strokeColor=i)),a&&(a==="transparent"?d.setAttribute("fillColor","rgba(0,0,0,0)"):d.setAttribute("fillColor",a),p!=null&&p.fillColor&&(p.fillColor=a)),l===S.Polygon&&n&&(p.vertices=n),c&&h!=null&&h.opt&&(t=h.opt)!=null&&t.strokeType&&(h.opt.strokeType=c),h&&r.setInfo(s.name,{...h,opt:p}),L.updateNodeOpt(e)}}class se{static bezier(e,t){const s=[];for(let o=0;o<t.length;o+=4){const r=t[o],i=t[o+1],a=t[o+2],l=t[o+3];r&&i&&a&&l?s.push(...se.getBezierPoints(e,r,i,a,l)):r&&i&&a?s.push(...se.getBezierPoints(e,r,i,a)):r&&i?s.push(...se.getBezierPoints(e,r,i)):r&&s.push(r)}return s}static getBezierPoints(e=10,t,s,o,r){let i=null;const a=[];!o&&!r?i=se.oneBezier:o&&!r?i=se.twoBezier:o&&r&&(i=se.threeBezier);for(let l=0;l<e;l++)i&&a.push(i(l/e,t,s,o,r));return r?a.push(r):o&&a.push(o),a}static oneBezier(e,t,s){const o=t.x+(s.x-t.x)*e,r=t.y+(s.y-t.y)*e;return new C(o,r)}static twoBezier(e,t,s,o){const r=(1-e)*(1-e)*t.x+2*e*(1-e)*s.x+e*e*o.x,i=(1-e)*(1-e)*t.y+2*e*(1-e)*s.y+e*e*o.y;return new C(r,i)}static threeBezier(e,t,s,o,r){const i=t.x*(1-e)*(1-e)*(1-e)+3*s.x*e*(1-e)*(1-e)+3*o.x*e*e*(1-e)+r.x*e*e*e,a=t.y*(1-e)*(1-e)*(1-e)+3*s.y*e*(1-e)*(1-e)+3*o.y*e*e*(1-e)+r.y*e*e*e;return new C(i,a)}}class Mt extends L{constructor(e){super(e),m(this,"canRotate",!1),m(this,"scaleType",U.all),m(this,"toolsType",S.SpeechBalloon),m(this,"ratio",.8),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"oldRect"),m(this,"syncTimestamp"),this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){var t;const{data:s,isFullWork:o,isSubWorker:r,isMainThread:i,smoothSync:a,isSimpleWorker:l}=e,n=(t=s?.workId)==null?void 0:t.toString();if(!n)return{type:y.None};const{op:c}=s,h=c?.length;if(!h||h<2)return{type:y.None};let p;if(this.tmpPoints.length===0?(this.tmpPoints=[new O(c[0],c[1])],p=!1):p=this.updateTempPoints(c),!p)return{type:y.None};let d;if(r||i||l){const f=o?this.fullLayer:this.drawLayer||this.fullLayer;d=this.draw({workId:n,layer:f,isDrawing:!0})}if(l){const f=A(d,this.oldRect);this.oldRect=d;const w={rect:f,type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult};if(a){const k=Date.now();k-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=k,w.op=this.tmpPoints.map(g=>[...g.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!r&&a){const f=Date.now();return f-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=f,{type:y.DrawWork,dataType:x.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:y.None}}const u=A(d,this.oldRect);return this.oldRect=d,{rect:u,type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult}}consumeAll(){var e;const t=this.workId;if(this.tmpPoints.length<2)return{type:y.RemoveNode,removeIds:[t]};const s=this.fullLayer,o=this.draw({workId:t,layer:s,isDrawing:!1});this.oldRect=o;const r=this.tmpPoints.map(a=>[...a.XY,0]).flat(1),i=Q(r);return(e=this.vNodes)==null||e.setInfo(t,{rect:o,op:r,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:o&&L.getCenterPos(o,s)}),{rect:o,type:y.FullWork,dataType:x.Local,ops:i,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:s,isDrawEraserlines:o=!0}=e,{strokeColor:r,fillColor:i,thickness:a,zIndex:l,placement:n,scale:c,rotate:h,translate:p,strokeType:d,eraserlines:u,lineDash:f,lineCap:w}=this.workOptions,{rect:k,pos:g,points:I}=this.computDrawPoints(a,n),T={points:I.map(b=>b.XY),lineWidth:a,fillColor:i!=="transparent"&&i||void 0,strokeColor:r,normalize:!0,className:`${g[0]},${g[1]}`,close:!0,lineCap:w,lineDash:d===F.Dotted?[f&&f[0]||1,(f&&f[1]||2)*a]:d===F.LongDotted?[(f&&f[0]||1)*a,(f&&f[1]||2)*a]:void 0},W={name:t,id:t,zIndex:l,pos:g,anchor:[.5,.5],size:[k.w,k.h]};c&&(W.scale=c),h&&(W.rotate=h),p&&(W.translate=p);const N=new V(W),R=new ce({...T,pos:[0,0]});N.append(R),u&&(this.scaleType=U.proportional,this.drawEraserlines({group:N,eraserlines:u,pos:g,layer:s},o)),this.replace(s,t,N);const P=N.getBoundingClientRect();return this.isDelete&&N.setAttribute("opacity",0),{x:Math.floor(P.x-L.SafeBorderPadding),y:Math.floor(P.y-L.SafeBorderPadding),w:Math.floor(P.width+L.SafeBorderPadding*2),h:Math.floor(P.height+L.SafeBorderPadding*2)}}transformControlPoints(e){const t=$(this.tmpPoints);switch(e){case"bottom":case"bottomLeft":case"bottomRight":{const s=t.y+t.h*this.ratio;return[new C(t.x,t.y,0),new C(t.x+t.w,t.y,0),new C(t.x+t.w,s,0),new C(t.x,s,0)]}case"top":case"topLeft":case"topRight":{const s=t.y+t.h*(1-this.ratio);return[new C(t.x,s,0),new C(t.x+t.w,s,0),new C(t.x+t.w,t.y+t.h,0),new C(t.x,t.y+t.h,0)]}case"left":case"leftBottom":case"leftTop":{const s=t.x+t.w*(1-this.ratio);return[new C(s,t.y,0),new C(t.x+t.w,t.y,0),new C(t.x+t.w,t.y+t.h,0),new C(s,t.y+t.h,0)]}case"right":case"rightBottom":case"rightTop":{const s=t.x+t.w*this.ratio;return[new C(t.x,t.y,0),new C(s,t.y,0),new C(s,t.y+t.h,0),new C(t.x,t.y+t.h,0)]}}}computDrawPoints(e,t){const s=$(this.tmpPoints),o=this.transformControlPoints(t),r=Math.floor(s.w*.1),i=Math.floor(s.h*.1),a=[],l=C.Add(o[0],new C(0,i,0)),n=C.Add(o[0],new C(r,0,0)),c=se.getBezierPoints(10,l,o[0],n),h=C.Sub(o[1],new C(r,0,0)),p=C.Add(o[1],new C(0,i,0)),d=se.getBezierPoints(10,h,o[1],p),u=C.Sub(o[2],new C(0,i,0)),f=C.Sub(o[2],new C(r,0,0)),w=se.getBezierPoints(10,u,o[2],f),k=C.Add(o[3],new C(r,0,0)),g=C.Sub(o[3],new C(0,i,0)),I=se.getBezierPoints(10,k,o[3],g),T=r*(1-this.ratio)*10,W=i*(1-this.ratio)*10;switch(t){case"bottom":{const P=C.Sub(o[2],new C(r*5-T/2,0,0)),b=C.Sub(o[2],new C(r*5,-W,0)),D=C.Sub(o[2],new C(r*5+T/2,0,0));a.push(b,D,...I,...c,...d,...w,P);break}case"bottomRight":{const P=C.Sub(o[2],new C(r*1.1,0,0)),b=C.Sub(o[2],new C(r*1.1+T/2,-W,0)),D=C.Sub(o[2],new C(r*1.1+T,0,0));a.push(b,D,...I,...c,...d,...w,P);break}case"bottomLeft":{const P=C.Add(o[3],new C(r*1.1+T,0,0)),b=C.Add(o[3],new C(r*1.1+T/2,W,0)),D=C.Add(o[3],new C(r*1.1,0,0));a.push(b,D,...I,...c,...d,...w,P);break}case"top":{const P=C.Sub(o[1],new C(r*5-T/2,0,0)),b=C.Sub(o[1],new C(r*5,W,0)),D=C.Sub(o[1],new C(r*5+T/2,0,0));a.push(b,P,...d,...w,...I,...c,D);break}case"topRight":{const P=C.Sub(o[1],new C(r*1.1,0,0)),b=C.Sub(o[1],new C(r*1.1+T/2,W,0)),D=C.Sub(o[1],new C(r*1.1+T,0,0));a.push(b,P,...d,...w,...I,...c,D);break}case"topLeft":{const P=C.Add(o[0],new C(r*1.1+T,0,0)),b=C.Add(o[0],new C(r*1.1+T/2,-W,0)),D=C.Add(o[0],new C(r*1.1,0,0));a.push(b,P,...d,...w,...I,...c,D);break}case"left":{const P=C.Add(o[0],new C(0,i*5-W/2,0)),b=C.Add(o[0],new C(-T,i*5,0)),D=C.Add(o[0],new C(0,i*5+W/2,0));a.push(b,P,...c,...d,...w,...I,D);break}case"leftTop":{const P=C.Add(o[0],new C(0,i*1.1,0)),b=C.Add(o[0],new C(-T,i*1.1+W/2,0)),D=C.Add(o[0],new C(0,i*1.1+W,0));a.push(b,P,...c,...d,...w,...I,D);break}case"leftBottom":{const P=C.Sub(o[3],new C(0,i*1.1+W,0)),b=C.Sub(o[3],new C(T,i*1.1+W/2,0)),D=C.Sub(o[3],new C(0,i*1.1,0));a.push(b,P,...c,...d,...w,...I,D);break}case"right":{const P=C.Add(o[1],new C(0,i*5-W/2,0)),b=C.Add(o[1],new C(T,i*5,0)),D=C.Add(o[1],new C(0,i*5+W/2,0));a.push(b,D,...w,...I,...c,...d,P);break}case"rightTop":{const P=C.Add(o[1],new C(0,i*1.1,0)),b=C.Add(o[1],new C(T,i*1.1+W/2,0)),D=C.Add(o[1],new C(0,i*1.1+W,0));a.push(b,D,...w,...I,...c,...d,P);break}case"rightBottom":{const P=C.Sub(o[2],new C(0,i*1.1+W,0)),b=C.Sub(o[2],new C(-T,i*1.1+W/2,0)),D=C.Sub(o[2],new C(0,i*1.1,0));a.push(b,D,...w,...I,...c,...d,P);break}}const N=$(this.tmpPoints,e),R=[Math.floor(N.x+N.w/2),Math.floor(N.y+N.h/2)];return{rect:N,pos:R,points:a}}updateTempPoints(e){const t=e.slice(-2),s=new O(t[0],t[1]),o=this.tmpPoints[0],{thickness:r}=this.workOptions;if(o.isNear(s,r)||O.Sub(o,s).XY.includes(0))return!1;if(this.tmpPoints.length===2){if(s.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=s}else this.tmpPoints.push(s);return!0}consumeService(e){var t,s;const{op:o,isFullWork:r,workState:i=M.Done}=e,a=(t=this.workId)==null?void 0:t.toString();if(!a)return;this.tmpPoints.length=0;for(let c=0;c<o.length;c+=3)this.tmpPoints.push(new O(o[c],o[c+1],o[c+2]));const l=r?this.fullLayer:this.drawLayer||this.fullLayer,n=this.draw({workId:a,layer:l,isDrawing:!1,isDrawEraserlines:i===M.Done});return this.oldRect=n,(s=this.vNodes)==null||s.setInfo(a,{rect:n,op:o,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:n&&L.getCenterPos(n,l)}),n}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var t;const{node:s,opt:o,vNodes:r}=e,{strokeColor:i,fillColor:a,toolsType:l,placement:n,strokeType:c}=o,h=r.get(s.name),p=h?.opt;let d=s;return s.tagName==="GROUP"&&(d=s.children[0]),i&&(d.setAttribute("strokeColor",i),p!=null&&p.strokeColor&&(p.strokeColor=i)),a&&(a==="transparent"?d.setAttribute("fillColor","rgba(0,0,0,0)"):d.setAttribute("fillColor",a),p!=null&&p.fillColor&&(p.fillColor=a)),l===S.SpeechBalloon&&n&&(p.placement=n),c&&h!=null&&h.opt&&(t=h.opt)!=null&&t.strokeType&&(h.opt.strokeType=c),h&&r.setInfo(s.name,{...h,opt:p}),L.updateNodeOpt(e)}}var Hs=kt,_s=Ve,qs="[object Boolean]";function Vs(v){return v===!0||v===!1||_s(v)&&Hs(v)==qs}var Zs=Vs;const ge=je(Zs);class At extends L{constructor(e){super(e),m(this,"canRotate",!1),m(this,"scaleType",U.both),m(this,"toolsType",S.Straight),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"oldRect"),m(this,"straightTipWidth"),m(this,"syncTimestamp"),this.workOptions=e.toolsOpt,this.straightTipWidth=this.workOptions.thickness/2,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:s,isSubWorker:o,isMainThread:r,smoothSync:i,isSimpleWorker:a}=e,l=this.workId,{op:n,syncUnitTime:c}=t,h=n?.length;if(!h||h<2)return{type:y.None};c&&(this.syncUnitTime=c);let p;if(this.tmpPoints.length===0?(this.tmpPoints=[new O(n[0],n[1])],p=!1):p=this.updateTempPoints(n),!p)return{type:y.None};let d;if(o||r||a){const f=s?this.fullLayer:this.drawLayer||this.fullLayer;d=this.draw({workId:l,layer:f})}if(a){const f=A(d,this.oldRect);this.oldRect=d;const w={rect:f,type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult};if(i){const k=Date.now();k-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=k,w.op=this.tmpPoints.map(g=>[...g.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!o&&i){const f=Date.now();return f-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=f,{type:y.DrawWork,dataType:x.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:y.None}}const u=A(d,this.oldRect);return this.oldRect=d,{rect:u,type:y.DrawWork,dataType:x.Local,...this.baseConsumeResult}}consumeAll(){var e;const t=this.workId;if(this.tmpPoints.length<2)return{type:y.RemoveNode,removeIds:[t]};const s=this.fullLayer,o=this.draw({workId:t,layer:s});this.oldRect=o;const r=this.tmpPoints.map(a=>[...a.XY,0]).flat(1),i=Q(r);return(e=this.vNodes)==null||e.setInfo(t,{rect:o,op:r,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:o&&L.getCenterPos(o,s)}),{rect:o,type:y.FullWork,dataType:x.Local,ops:i,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:s,isDrawEraserlines:o=!0}=e,{strokeColor:r,thickness:i,zIndex:a,scale:l,rotate:n,translate:c,strokeType:h,eraserlines:p,lineCap:d,lineDash:u}=this.workOptions,f=s.worldPosition,w=s.worldScaling,{d:k,rect:g,isDot:I}=this.computDrawPoints(i,h),T=[g.x+g.w/2,g.y+g.h/2],W=new V({pos:T,anchor:[.5,.5],size:[g.w,g.h],name:t,id:t,normalize:!0,zIndex:a}),N={pos:[0,0],normalize:!0,d:k,fillColor:r,strokeColor:r,lineDash:I?void 0:h===F.Dotted?[u&&u[0]||1,(u&&u[1]||2)*i]:h===F.LongDotted?[(u&&u[0]||1)*i,(u&&u[1]||2)*i]:void 0,lineCap:I?void 0:d,lineWidth:h===F.Normal||I?0:i};l&&(N.scale=l),n&&(N.rotate=n),c&&(N.translate=c);const R=new ye(N);W.append(R),p&&(this.scaleType=U.proportional,this.drawEraserlines({group:W,eraserlines:p,pos:T,layer:s},o)),this.replace(s,t,W);let P={x:Math.floor(g.x*w[0]+f[0]-L.SafeBorderPadding),y:Math.floor(g.y*w[1]+f[1]-L.SafeBorderPadding),w:Math.floor(g.w*w[0]+2*L.SafeBorderPadding),h:Math.floor(g.h*w[1]+2*L.SafeBorderPadding)};if(n||l||c){const b=R.getBoundingClientRect();P={x:Math.floor(b.x-L.SafeBorderPadding),y:Math.floor(b.y-L.SafeBorderPadding),w:Math.floor(b.width+L.SafeBorderPadding*2),h:Math.floor(b.height+L.SafeBorderPadding*2)}}return this.isDelete&&W.setAttribute("opacity",0),P}computDrawPoints(e,t){return this.tmpPoints[1].distance(this.tmpPoints[0])>this.straightTipWidth?this.computFullPoints(e,t):this.computDotPoints(e)}computFullPoints(e,t){const s=C.Sub(this.tmpPoints[1],this.tmpPoints[0]).uni(),o=C.Per(s).mul(e/2),r=O.Sub(this.tmpPoints[0],o),i=O.Add(this.tmpPoints[0],o),a=O.Sub(this.tmpPoints[1],o),l=O.Add(this.tmpPoints[1],o),n=O.GetSemicircleStroke(this.tmpPoints[1],a,-1,8),c=O.GetSemicircleStroke(this.tmpPoints[0],i,-1,8),h=[r,a,...n,l,i,...c];let p;return t!==F.Normal?p=me(this.tmpPoints,!1):p=me(h,!0),{d:p,rect:$(h),isDot:!1,pos:this.tmpPoints[0].XY}}computDotPoints(e){const t=O.GetDotStroke(this.tmpPoints[0],e/2,8);return{d:me(t,!0),rect:$(t),isDot:!0,pos:this.tmpPoints[0].XY}}updateTempPoints(e){const t=e.slice(-2),s=new O(t[0],t[1]),o=this.tmpPoints[0],{thickness:r}=this.workOptions;if(o.isNear(s,r))return!1;if(this.tmpPoints.length===2){if(s.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=s}else this.tmpPoints.push(s);return!0}consumeService(e){var t,s;const{op:o,isFullWork:r,workState:i=M.Done}=e,a=(t=this.workId)==null?void 0:t.toString();if(!a)return;this.tmpPoints.length=0;for(let c=0;c<o.length;c+=3)this.tmpPoints.push(new O(o[c],o[c+1],o[c+2]));const l=r?this.fullLayer:this.drawLayer||this.fullLayer,n=this.draw({workId:a,layer:l,isDrawEraserlines:i===M.Done});return this.oldRect=n,(s=this.vNodes)==null||s.setInfo(a,{rect:n,op:o,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:n&&L.getCenterPos(n,l)}),n}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var t,s;const{node:o,opt:r,vNodes:i}=e,{strokeColor:a,strokeType:l}=r,n=i.get(o.name);return a&&(o.tagName==="GROUP"?o.children.forEach(c=>{c.setAttribute("strokeColor",a),c.getAttribute("fillColor")&&c.setAttribute("fillColor",a)}):(o.setAttribute("strokeColor",a),o.setAttribute("fillColor",a)),(t=n?.opt)!=null&&t.strokeColor&&(n.opt.strokeColor=a)),l&&n!=null&&n.opt&&(s=n.opt)!=null&&s.strokeType&&(n.opt.strokeType=l),n&&i.setInfo(o.name,n),L.updateNodeOpt(e)}}const Xe=class Pe extends L{constructor(e){super(e),m(this,"canRotate",!1),m(this,"scaleType",U.proportional),m(this,"toolsType",S.Text),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"oldRect"),this.workOptions=e.toolsOpt}consume(){return{type:y.None}}consumeAll(){return{type:y.None}}consumeService(){}async draw(e){const{workId:t,layer:s,isDrawLabel:o,boxRect:r}=e,{boxSize:i,boxPoint:a,zIndex:l}=this.workOptions,n=s.worldPosition,c=s.worldScaling;if(!a||!i)return;const h={name:t,id:t,pos:[a[0]-De,a[1]-De],anchor:[0,0],size:i,zIndex:l},p=new V(h),d={x:a[0]-De,y:a[1]-De,w:i[0],h:i[1]},u={x:Math.floor(d.x*c[0]+n[0]),y:Math.floor(d.y*c[1]+n[1]),w:Math.floor(d.w*c[0])+2,h:Math.floor(d.h*c[1])+2};this.replace(s,t,p);let f;if(r&&(f=Ce(u,r)),(o||r&&f!==ze.outside)&&s&&this.workOptions.text){const w=await Pe.createLabels(this.workOptions,s,u),{labels:k,maxWidth:g}=w;p.append(...k),u.w=Math.ceil(Math.max(g*s.worldScaling[0],u.w))}return u}async consumeServiceAsync(e){var t,s,o,r;const i=(t=this.workId)==null?void 0:t.toString();if(!i)return;const{isFullWork:a,replaceId:l,isDrawLabel:n,boxRect:c}=e;this.oldRect=l&&((o=(s=this.vNodes)==null?void 0:s.get(l))==null?void 0:o.rect)||void 0;const h=a?this.fullLayer:this.drawLayer||this.fullLayer,p=await this.draw({workId:i,layer:h,isDrawLabel:typeof n>"u"&&this.workOptions.workState===M.Done||n,boxRect:c});return(r=this.vNodes)==null||r.setInfo(i,{rect:p,op:[],opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:p&&L.getCenterPos(p,h)}),p}updataOptService(){}async updataOptServiceAsync(e,t,s){var o,r;if(!this.workId)return;const i=this.workId.toString(),{fontColor:a,fontBgColor:l,bold:n,italic:c,lineThrough:h,underline:p,zIndex:d}=e,u=(o=this.vNodes)==null?void 0:o.get(i);if(!u)return;a&&(u.opt.fontColor=a),l&&(u.opt.fontBgColor=l),n&&(u.opt.bold=n),c&&(u.opt.italic=c),ge(h)&&(u.opt.lineThrough=h),ge(p)&&(u.opt.underline=p),q(d)&&(u.opt.zIndex=d),this.oldRect=u.rect;const f=await this.draw({workId:i,layer:this.fullLayer,isDrawLabel:typeof t>"u"&&this.workOptions.workState===M.Done||t,boxRect:s});return(r=this.vNodes)==null||r.setInfo(i,{rect:f,op:[],opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:f&&L.getCenterPos(f,this.fullLayer)}),f}clearTmpPoints(){this.tmpPoints.length=0}static getSafetySnippetRatio(e){const t=(e?.parent).displayRatio||1,s=Math.ceil(e.worldScaling[0]*10)/10;let o=s;return s<=2?o=s*t*2:s>2&&s<=3?o=s*t*1.6:s>3&&s<=4?o=s*t*1.2:s>4&&(o=s*t),Math.floor(o*1e3)/1e3}static getSafetySnippetFontLength(e){return Math.floor(Pe.textImageSnippetSize*3/4/e)||1}static async createLabels(e,t,s){var o;const r=[],{x:i,y:a}=s,{width:l,height:n}=(o=t.parent)==null?void 0:o.parent,c=fe(e.text),h=c.length,{fontSize:p,lineHeight:d,bold:u,textAlign:f,italic:w,fontFamily:k,verticalAlign:g,fontColor:I,fontBgColor:T,underline:W,lineThrough:N}=e,R=Pe.getSafetySnippetRatio(t)||1,P=Math.floor(p*R),b=Pe.getSafetySnippetFontLength(P);let D=0;for(let B=0;B<h;B++){const E=c[B],X=d||P*1.5;if(E){const _=Qt(E),G=[0,0],J=[0,p*1.2];g==="middle"&&(G[1]=Math.floor(B*p*1.2+6+p*(1.1-1)));const he=[0,Math.floor(-p*.15)];G[0]=6;const be=Math.sin(Math.PI/180*20);let pe=0;const Ne=[];let Te=0;for(;Te<_;){f==="left"&&(he[0]=pe),Te===0&&w==="italic"&&(he[0]=he[0]-be/2*p);const Fe=E.slice(Te,Te+b),Re={anchor:[0,0],pos:he,text:Fe,fontFamily:k,fontSize:P,lineHeight:X,strokeColor:I,fontWeight:u,fillColor:I,textAlign:f,fontStyle:w,scale:[1/R,1/R]},de=new Jt(Re),ie=await de.textImageReady;let xe=!0;if(ie){const Qe=ie.rect&&ie.rect[2],Je=ie.rect&&ie.rect[3];if(Qe&&Je){const et=Qe/R,jt=Je/R;pe=et+pe,w==="italic"&&(u==="bold"?pe=pe-be*p*1.2:pe=pe-be*p),((he[0]+G[0]+et)*t.worldScaling[0]+i<=0||(he[0]+G[0])*t.worldScaling[0]+i>=l||(he[1]+G[1]+jt)*t.worldScaling[1]+a<=0||(he[1]+G[1])*t.worldScaling[1]+a>=n)&&(de.disconnect(),xe=!1),xe&&Ne.push(de)}}Te+=b}J[0]=pe,w==="italic"&&(J[0]=J[0]+be*p),D=Math.max(D,J[0]);let Ke=!0;if(((G[0]+J[0])*t.worldScaling[0]+i<=0||G[0]*t.worldScaling[0]+i>=l||(G[1]+J[1])*t.worldScaling[0]+a<=0||G[1]*t.worldScaling[1]+a>=n)&&(Ke=!1),Ke){if(W){const de=Math.floor(p/10),ie={normalize:!1,pos:[0,p*1.1+de/2],lineWidth:de,points:[0,0,Math.ceil(J[0]),0],strokeColor:I,className:"underline"},xe=new ce(ie);Ne.push(xe)}if(N){const de={normalize:!1,pos:[0,p*1.2/2],lineWidth:Math.floor(p/10),points:[0,0,Math.ceil(J[0]),0],strokeColor:I,className:"lineThrough"},ie=new ce(de);Ne.push(ie)}const Fe={pos:G,anchor:[0,0],size:J,bgcolor:T},Re=new V(Fe);Re.append(...Ne),r.push(Re)}}}return{labels:r,maxWidth:D}}static updateNodeOpt(e){const{node:t,opt:s,vNodes:o,targetNode:r}=e,{fontBgColor:i,fontColor:a,translate:l,originPoint:n,scenePoint:c,scale:h,bold:p,italic:d,lineThrough:u,underline:f,fontSize:w,textInfos:k,zIndex:g}=s,I=r&&ee(r)||o.get(t.name);if(!I||!t.parent)return;const T=I.opt;if(q(g)&&(t.setAttribute("zIndex",g),I.opt.zIndex=g),a&&T.fontColor&&(T.fontColor=a,t.children.forEach(W=>{W.tagName==="GROUP"&&W.children.forEach(N=>{N.tagName==="LABEL"?(N.setAttribute("fillColor",a),N.setAttribute("strokeColor",a)):N.tagName==="POLYLINE"&&N.setAttribute("strokeColor",a)})})),i&&T.fontBgColor&&(T.fontBgColor=i,t.children.forEach(W=>{W.tagName==="GROUP"&&W.setAttribute("bgcolor",i)})),p&&(T.bold=p),d&&(T.italic=d),ge(u)&&(T.lineThrough=u),ge(f)&&(T.underline=f),w&&(T.fontSize=w),n&&c&&h&&r&&T.boxPoint){const W=k?.get(t.name);if(W){const{fontSize:b,boxSize:D}=W;T.boxSize=D||T.boxSize,T.fontSize=b||T.fontSize}const N=[T.boxPoint[0],T.boxPoint[1]];He(T.boxPoint,c,h,l);const R=[I.op[0],I.op[1]],P=[R[0]-N[0],R[1]-N[1]];if(I.centerPos=[I.centerPos[0]+P[0],I.centerPos[1]+P[1]],I.opt.eraserlines)for(const b of I.opt.eraserlines){const{op:D,thickness:B}=b;b.thickness=Math.round(B*Math.max(h[0],h[1]));for(let E=0;E<D.length;E++)_e(D[E],c,h,l)}}else if(l&&T.boxPoint&&(T.boxPoint=[Math.round(T.boxPoint[0]+l[0]),Math.round(T.boxPoint[1]+l[1])],I.centerPos=[I.centerPos[0]+l[0],I.centerPos[1]+l[1]],I.opt.eraserlines))for(const W of I.opt.eraserlines){const{op:N}=W;for(let R=0;R<N.length;R++){const P=N[R].map((b,D)=>D%2?b+l[1]:b+l[0]);W.op[R]=P}}return I&&o.setInfo(t.name,I),I?.rect}static getRectFromLayer(e,t){const s=e.getElementsByName(t)[0];if(s){const o=s.getBoundingClientRect();let r={x:Math.floor(o.x),y:Math.floor(o.y),w:Math.floor(o.width+2),h:Math.floor(o.height+2)};return s.children.forEach(i=>{if(i.tagName==="GROUP"){const a=s.getBoundingClientRect();r=A(r,{x:Math.floor(a.x),y:Math.floor(a.y),w:Math.floor(a.width+2),h:Math.floor(a.height+2)})}}),r}}};m(Xe,"textImageSnippetSize",1024*4),m(Xe,"SafeBorderPadding",30);let Et=Xe;const Bt=class Ae extends L{constructor(e){super(e),m(this,"canRotate",!1),m(this,"scaleType",U.none),m(this,"toolsType",S.PencilEraser),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"worldPosition"),m(this,"worldScaling"),m(this,"eraserRect"),m(this,"eraserPolyline"),this.workOptions=e.toolsOpt,this.worldPosition=this.fullLayer.worldPosition,this.worldScaling=this.fullLayer.worldScaling}combineConsume(e){const{workerEngine:t}=e;if(this.tmpPoints.length<2)return{type:y.None};const{thickness:s}=this.workOptions,{width:o,height:r}=Ae.eraserSizes[s],i=Math.round(Math.max(o,r)/this.worldScaling[0]),{rect:a,willNewNodes:l,willDeleteNodes:n}=this.getChangeNodes(i,t);return{...this.baseConsumeResult,type:y.DrawWork,rect:a,willNewNodes:l,willDeleteNodes:n}}consumeService(){}updateTempPoints(e,t){let s=this.tmpPoints.length-1;const o=Date.now();for(let r=0;r<e.length;r+=2){const i=this.tmpPoints.length,a=new O(e[r],e[r+1]);if(i===0){this.tmpPoints.push(a);continue}const l=i-1,n=this.tmpPoints[l],c=C.Sub(a,n).uni();a.isNear(n,t/2)||(C.Equals(c,n.v,.02)&&(this.tmpPoints.pop(),s--),a.setv(c),a.setT(o),this.tmpPoints.push(a))}return Math.max(s,0)}getChangeNodes(e,t){const s=new Map,o=new Set;let r;if(!this.vNodes)return{willDeleteNodes:o,willNewNodes:s,rect:r};const i=this.worldPosition;let a=$(this.tmpPoints,e);a=We(a,i),a=yt(a,this.worldScaling,i);const{nodeRange:l}=this.vNodes.getRectIntersectRange(a);if(!l.size)return{willDeleteNodes:o,willNewNodes:s,rect:r};const n=this.computEraserPointLines(this.tmpPoints,90);for(const[c,h]of l.entries()){if(h.toolsType!==S.Pencil)continue;let p=We(h.rect,[-i[0],-i[1]]);p={x:p.x/this.worldScaling[0],y:p.y/this.worldScaling[1],w:p.w/this.worldScaling[0],h:p.h/this.worldScaling[1]};const d=[p.x,p.y,p.x+p.w,p.y+p.h],u=[],f=[];for(const w of n)qe.polyline(w,d,u);if(u.length&&h.opt)for(const w of u){const k=[];for(let g=0;g<w.length;g++)g!==0&&le(w[g],w[g-1])||k.push(...w[g]);f.push(k)}if(f.length&&t){const w=t.createWorkShapeNode({workId:c,toolsType:S.Pencil,toolsOpt:h.opt});w.setWorkId(c);const k=w?.computPencilPoints({op:h.op,eraserPolylines:f,eraserThickness:e});if(r=A(r,h.rect),o.add(c),t.removeNode(c),k.length)for(let g=0;g<k.length;g++){const I=`${c}_${Date.now()}_${g}`,T=I,W=t.setFullWork({workId:T,opt:h.opt,toolsType:S.Pencil}).consumeService({op:k[g],isFullWork:!0,replaceId:T});t.clearWorkShapeNodeCache(T),r=A(r,W),s.set(I,{...h,toolsType:S.Pencil,rect:W??h.rect,name:I,op:k[g]})}}}return{willNewNodes:s,willDeleteNodes:o,rect:r}}consume(e){const{data:t}=e,{op:s}=t;if(!s||s.length===0)return{type:y.None,...this.baseConsumeResult};const{thickness:o}=this.workOptions,{width:r,height:i}=Ae.eraserSizes[o],a=Math.max(r,i)/this.worldScaling[0];return this.updateTempPoints(s,a),{type:y.None,...this.baseConsumeResult}}computEraserPointLines(e,t=60){const s=[];let o=0;const r=360-t;for(let i=1;i<e.length;i++){const a=e[i-1],l=e[i];if(i===1&&o===0&&(s[o]=[a.XY]),s[o].push(l.XY),i<e.length-1){const n=l.getAngleByPoints(a,e[i+1]);(n<t||n>r)&&(o++,s[o]=[l.XY])}}return e.length===1&&s.length===0&&s.push([e[0].XY,e[0].XY]),s}consumeAll(e){const{workerEngine:t}=e;if(this.replace(this.fullLayer,this.workId),!this.tmpPoints.length)return{type:y.None,...this.baseConsumeResult};const{thickness:s}=this.workOptions,{width:o,height:r}=Ae.eraserSizes[s],i=Math.max(o,r)/this.worldScaling[0],{willDeleteNodes:a,willNewNodes:l,rect:n}=this.getChangeNodes(i,t);if(a.size===0&&l.size===0)return{type:y.FullWork,...this.baseConsumeResult,rect:n};const c=[];this.tmpPoints.map(p=>{c.push(p.x,p.y,i/2)});const h=Q(c);return{...this.baseConsumeResult,type:y.FullWork,dataType:x.Local,rect:n,ops:h,willNewNodes:l,willDeleteNodes:a}}clearTmpPoints(){this.tmpPoints.length=0}};m(Bt,"eraserSizes",Ze);let zt=Bt;const Ft=class Ee extends L{constructor(e){super(e),m(this,"canRotate",!1),m(this,"scaleType",U.none),m(this,"toolsType",S.BitMapEraser),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"worldPosition"),m(this,"worldScaling"),m(this,"eraserRect"),m(this,"eraserPolyline"),m(this,"oldRect"),this.workOptions=e.toolsOpt,this.worldPosition=this.fullLayer.worldPosition,this.worldScaling=this.fullLayer.worldScaling}combineConsume(e){const{workerEngine:t}=e;if(this.tmpPoints.length<2)return{type:y.None};const{thickness:s}=this.workOptions,{width:o,height:r}=Ee.eraserSizes[s],i=Math.round(Math.max(o,r)/this.worldScaling[0]),{willDeleteNodes:a,rect:l}=this.getChangeNodes(i,t);return{...this.baseConsumeResult,type:y.DrawWork,rect:l,willDeleteNodes:a}}consumeService(){}updateTempPoints(e,t){let s=this.tmpPoints.length-1;const o=Date.now();for(let r=0;r<e.length;r+=2){const i=this.tmpPoints.length,a=new O(e[r],e[r+1]);if(i===0){this.tmpPoints.push(a);continue}const l=i-1,n=this.tmpPoints[l],c=C.Sub(a,n).uni();a.isNear(n,t/2)||(C.Equals(c,n.v,.02)&&(this.tmpPoints.pop(),s--),a.setv(c),a.setT(o),this.tmpPoints.push(a))}return Math.max(s,0)}getChangeNodes(e,t){const s=new Map,o=new Set;let r;if(!this.vNodes)return{willDeleteNodes:o,willUpdateNodes:s,rect:r};const i=this.worldPosition;let a=$(this.tmpPoints,e);a=We(a,i),a=yt(a,this.worldScaling,i);const{nodeRange:l}=this.vNodes.getRectIntersectRange(a);if(!l.size)return{willDeleteNodes:o,willUpdateNodes:s,rect:r};const n=this.computEraserPointLines(this.tmpPoints,120);for(const[c,h]of l.entries()){if(h.toolsType===S.Text||h.toolsType===S.Image)continue;let p=We(h.rect,[-i[0],-i[1]]);p={x:p.x/this.worldScaling[0],y:p.y/this.worldScaling[1],w:p.w/this.worldScaling[0],h:p.h/this.worldScaling[1]};const d=[p.x,p.y,p.x+p.w,p.y+p.h],u=[],f=[];for(const g of n)qe.polyline(g,d,u);if(u.length&&h.opt)for(const g of u){const I=[];for(let T=0;T<g.length&&!(isNaN(g[T][0])||isNaN(g[T][1]));T++){const W=g[T].map(N=>Math.round(N*100)/100);if(T!==0){const N=g[T-1].map(R=>Math.round(R*100)/100);if(le(W,N))continue}I.push(...W)}I.length!==0&&f.push(I)}if(!f.length)continue;const w=h.opt.eraserlines?h.opt.eraserlines:[],k=w.find(g=>g.workId===this.workId);if(k?k.op=f:w.push({workId:this.workId,op:f,thickness:e}),h.opt.eraserlines=w,t){const{toolsType:g}=h,I=t.createWorkShapeNode({workId:c,toolsType:g,toolsOpt:h.opt});I?.setWorkId(c);const T=I?.consumeService({op:h.op,isFullWork:!0,replaceId:c});if(r=A(r,T),I!=null&&I.isDelete){t.removeNode(c),o.add(c);continue}}s.set(c,h)}return{willUpdateNodes:s,willDeleteNodes:o,rect:r}}consume(e){const{data:t}=e,{op:s}=t;if(!s||s.length===0)return{type:y.None,...this.baseConsumeResult};const{thickness:o}=this.workOptions,{width:r,height:i}=Ee.eraserSizes[o],a=Math.round(Math.max(r,i)/this.worldScaling[0]);return this.updateTempPoints(s,a),{type:y.None,...this.baseConsumeResult}}computEraserPointLines(e,t=60){const s=[];let o=0;const r=360-t;for(let i=1;i<e.length;i++){const a=e[i-1],l=e[i];if(i===1&&o===0&&(s[o]=[a.XY]),s[o].push(l.XY),i<e.length-1){const n=l.getAngleByPoints(a,e[i+1]);(n<t||n>r)&&(o++,s[o]=[l.XY])}}return e.length===1&&s.length===0&&s.push([e[0].XY,e[0].XY]),s}consumeAll(e){const{workerEngine:t}=e;if(this.replace(this.fullLayer,this.workId),!this.tmpPoints.length)return{type:y.None,...this.baseConsumeResult};const{thickness:s}=this.workOptions,{width:o,height:r}=Ee.eraserSizes[s],i=Math.round(Math.max(o,r)/this.worldScaling[0]),{willDeleteNodes:a,willUpdateNodes:l,rect:n}=this.getChangeNodes(i,t);if(a.size===0&&l.size===0)return{type:y.FullWork,...this.baseConsumeResult,rect:n};const c=[];this.tmpPoints.map(p=>{c.push(p.x,p.y,i/2)});const h=Q(c);return{...this.baseConsumeResult,type:y.FullWork,dataType:x.Local,rect:n,ops:h,willUpdateNodes:l,willDeleteNodes:a}}clearTmpPoints(){this.tmpPoints.length=0}};m(Ft,"eraserSizes",Ze);let Ut=Ft;function Yt(v){switch(v){case S.Arrow:return Nt;case S.Pencil:return St;case S.Straight:return At;case S.Ellipse:return Rt;case S.Polygon:case S.Triangle:return Ot;case S.Star:case S.Rhombus:return Dt;case S.Rectangle:return xt;case S.SpeechBalloon:return Mt;case S.Text:return Et;case S.LaserPen:return Tt;case S.Eraser:return Pt;case S.PencilEraser:return zt;case S.BitMapEraser:return Ut;case S.Selector:return bt;case S.Image:return Le}}function Se(v,e){const{toolsType:t,...s}=v;switch(t){case S.Arrow:return new Nt(s);case S.Pencil:return new St(s);case S.Straight:return new At(s);case S.Ellipse:return new Rt(s);case S.Polygon:case S.Triangle:return new Ot(s);case S.Star:case S.Rhombus:return new Dt(s);case S.Rectangle:return new xt(s);case S.SpeechBalloon:return new Mt(s);case S.Text:return new Et(s);case S.LaserPen:return new Tt(s);case S.Eraser:return new Pt(s,e);case S.BitMapEraser:return new Ut(s);case S.PencilEraser:return new zt(s);case S.Selector:return s.vNodes?new bt({...s,vNodes:s.vNodes,drawLayer:s.fullLayer}):void 0;case S.Image:return new Le(s);default:return}}function Xt(v){const e=[],t=["PATH","SPRITE","POLYLINE","RECT","ELLIPSE","LABEL"];for(const s of v){if(s.tagName==="GROUP"&&s.children.length){const o=Xt(s.children);e.push(...o)}s.tagName&&t.includes(s.tagName)&&e.push(s)}return e}class $t{constructor(e,t){m(this,"viewId"),m(this,"scene"),m(this,"fullLayer"),m(this,"curNodeMap",new Map),m(this,"targetNodeMap",[]),m(this,"highLevelIds"),m(this,"canClearUids"),m(this,"localUid"),this.viewId=e,this.scene=t}init(e){this.fullLayer=e}get(e){return this.curNodeMap.get(e)}setLocalUid(e){this.localUid=e}getLocalUid(){return this.localUid}setCanClearUids(e){this.canClearUids=e}getCanClearUids(){return this.canClearUids}getCanEraserNodes(e,t){const s=new Map;for(const[o,r]of e.entries())r.toolsType===S.Image&&(r.opt.locked||t&&t.disableEraseImage)||r.toolsType===S.Text&&(r.opt.workState===M.Doing||r.opt.workState===M.Start||t&&t.disableEraseText)||this.isCanClearWorkId(o)&&s.set(o,r);return s}getNodesByType(e){const t=new Map;return this.curNodeMap.forEach((s,o)=>{s.toolsType===e&&t.set(o,s)}),t}gethasEraserNodes(){const e=new Map;return this.curNodeMap.forEach((t,s)=>{var o;(o=t.opt.eraserlines)!=null&&o.length&&e.set(s,t)}),e}hasRenderNodes(){return!0}has(e){return this.curNodeMap.has(e)}setInfo(e,t){const s=this.curNodeMap.get(e)||{name:e,rect:t.rect};t.rect&&(s.rect=ee(t.rect)),t.op&&_t(t.op)&&(s.op=ee(t.op)),t.canRotate&&(s.canRotate=t.canRotate),t.scaleType&&(s.scaleType=t.scaleType),t.opt&&(s.opt=ee(t.opt)),t.toolsType&&(s.toolsType=t.toolsType),t.centerPos&&(s.centerPos=ee(t.centerPos)),ge(t.isSelected)&&(s.isSelected=t.isSelected),s.rect?this.curNodeMap.set(e,s):this.curNodeMap.delete(e)}selected(e){this.setInfo(e,{isSelected:!0})}unSelected(e){this.setInfo(e,{isSelected:!1})}delete(e){this.curNodeMap.delete(e)}clear(){this.curNodeMap.clear(),this.targetNodeMap.length=0}getRectIntersectRange(e,t=!0,s=!0){let o;const r=new Map;for(const[i,a]of this.curNodeMap.entries())if(Ye(e,a.rect)){if(t&&a.toolsType===S.Image&&a.opt.locked||s&&a.toolsType===S.Text&&(a.opt.workState===M.Doing||a.opt.workState===M.Start))continue;o=A(o,a.rect),r.set(i,a)}return{rectRange:o,nodeRange:r}}getNodeRectFormShape(e,t){const s=Yt(t.toolsType);return this.fullLayer&&s?.getRectFromLayer(this.fullLayer,e)}updateNodeRect(e){const t=this.curNodeMap.get(e);if(t){const s=this.getNodeRectFormShape(e,t);if(!s){this.curNodeMap.delete(e);return}t.rect=s,this.curNodeMap.set(e,t)}}updateHighLevelNodesRect(e){this.highLevelIds=e;for(const t of this.highLevelIds.keys())this.updateNodeRect(t)}updateLowLevelNodesRect(){var e;for(const t of this.curNodeMap.keys())(e=this.highLevelIds)!=null&&e.has(t)||this.updateNodeRect(t)}clearHighLevelIds(){this.highLevelIds=void 0}setTargetAssignKeys(e){const t=new Map;for(const s of e){const o=this.curNodeMap.get(s);o&&t.set(s,ee(o))}return this.targetNodeMap.push(ee(t)),this.targetNodeMap.length-1}setTarget(){return this.targetNodeMap.push(ee(this.curNodeMap)),this.targetNodeMap.length-1}getLastTarget(){return this.targetNodeMap[this.targetNodeMap.length-1]}deleteLastTarget(){this.targetNodeMap.length&&(this.targetNodeMap.length=this.targetNodeMap.length-1)}getTarget(e){return this.targetNodeMap[e]}deleteTarget(e){this.targetNodeMap.length=e}clearTarget(){this.targetNodeMap.length=0}isLocalWorkId(e){return e.split(ve).length===1}isCanClearWorkId(e){if(this.canClearUids===void 0||this.canClearUids===!0)return!0;if(qt(this.canClearUids)){const t=e.split(ve);if(t.length===1)return this.canClearUids.has("localSelf")||this.localUid&&this.canClearUids.has(this.localUid);if(t.length===2)return this.canClearUids.has(t[0])}return!1}}class Ks{constructor(e,t,s){m(this,"viewId"),m(this,"type"),m(this,"scene"),m(this,"fullLayer"),m(this,"vNodes"),m(this,"dpr"),m(this,"contextType"),m(this,"opt"),m(this,"cameraOpt"),m(this,"isSafari",!1),m(this,"combinePostMsg",new Set),m(this,"workerTaskId"),m(this,"protectedTask"),m(this,"delayPostDoneResolve"),m(this,"cacheImages",new Map),m(this,"imageResolveMap",new Map),m(this,"taskUpdateCameraId"),m(this,"debounceUpdateCameraId");var o,r;if(this.viewId=e,this.type=s,this.opt=t,this.dpr=t.dpr,this.contextType=this.getSupportContextType(s,(o=t?.offscreenCanvasOpt)==null?void 0:o.contextType),!this.contextType)throw new Error("Sorry, your browser doesn't support canvas context type 2d or webgl");try{this.scene=this.createScene(t),this.createRenderLayer(this.scene,t)}catch(i){if(this.contextType!=="2d")this.contextType="2d",(r=this.scene)==null||r.disconnect(),this.scene=this.createScene(t),this.createRenderLayer(this.scene,t);else throw i}this.vNodes=new $t(e,this.scene)}createRenderLayer(e,t){const s=K.bufferSize.full,o=K.bufferSize.sub;this.fullLayer=this.createLayer("fullLayer",e,{...t.layerOpt,bufferSize:this.viewId===ne?s:o*2})}async updateDpr(e){this.dpr=e,this.scene.displayRatio=e}getCachedImages(e){var t;return(t=this.cacheImages.get(e))==null?void 0:t.imageBitmap}getCachedImagesByWorkId(e){for(const[t,s]of this.cacheImages.entries())if(t===e&&s.imageBitmap)return s.imageBitmap}deleteCachedImagesByWorkId(e,t){for(const[s,o]of this.cacheImages.entries())t&&s===t||o.workId===e&&(o.imageBitmap.close(),this.cacheImages.delete(s))}clearCacheImages(){this.cacheImages.forEach(e=>e.imageBitmap.close()),this.cacheImages.clear()}clearImageResolveMap(){this.imageResolveMap.forEach(({timer:e})=>{e&&clearTimeout(e)}),this.imageResolveMap.clear()}setIsSafari(e){this.isSafari=e}async on(e){const{msgType:t,toolsType:s,opt:o,imageSrc:r,imageBitmap:i,workId:a,dpr:l}=e;switch(t){case y.UpdateDpr:q(l)&&await this.updateDpr(l);break;case y.UpdateCamera:await this.updateCamera(e);break;case y.Destroy:this.destroy();break;case y.Clear:await this.clearAll();break;case y.UpdateTools:if(s&&o){const n={toolsType:s,toolsOpt:o,combineUnitTime:e.combineUnitTime,maxCombineEraserTime:e.maxCombineEraserTime};this.localWork.setToolsOpt(n)}break;case y.GetImageBitMap:if(r&&i&&a){const n=a.toString();this.cacheImages.set(r,{imageBitmap:i,workId:n});const c=this.imageResolveMap.get(r);if(c){const{resolve:h,timer:p}=c;p&&clearTimeout(p),h&&h(r)}this.deleteCachedImagesByWorkId(n,r)}break}}getIconSize(e,t,s){const o=e*s,r=t*s;return o<=50||r<=50?[50,50]:o<=100||r<=100?[100,100]:o<=200||r<=200?[200,200]:o<=400||r<=400?[400,400]:o<=800||r<=800?[800,800]:[1600,1600]}async loadImageBitMap(e){const{toolsType:t,opt:s,workId:o,isSubWorker:r}=e;if(t===S.Image&&s&&o){const i=o.toString(),{src:a,type:l,width:n,height:c,strokeColor:h}=s;if(!a||!l||!n||!c)return;let p=a;if(l===Be.Iconify){const[u,f]=this.getIconSize(n,c,this.dpr);p=`${a}?width=${u}&height=${f}&color=${h}`}if(this.cacheImages.has(p)){const u=this.getCachedImages(p);if(u)return u}if(this.imageResolveMap.has(p)){const u=this.getCachedImagesByWorkId(i);if(u)return u}const d=await new Promise(u=>{const f=this.imageResolveMap.get(p)||{resolve:void 0,timer:void 0};f.timer&&clearTimeout(f.timer),f.resolve=u,f.timer=setTimeout(()=>{const w=this.imageResolveMap.get(p);w!=null&&w.resolve&&w.resolve(p)},5e3),this.imageResolveMap.set(p,f),this._post({sp:[{imageSrc:p,workId:i,viewId:this.viewId,isgl:!!this.fullLayer.parent.gl,isSubWorker:r,type:y.GetImageBitMap}]})});return this.imageResolveMap.delete(d),this.getCachedImages(p)}}createLocalWork(e){const{workId:t,opt:s,toolsType:o}=e;t&&s&&(!this.localWork.getToolsOpt()&&o&&this.setToolsOpt({toolsType:o,toolsOpt:s}),this.setWorkOpt({workId:t,toolsOpt:s}))}updateScene(e,t,s){e.attr({...t});const{width:o,height:r}=t;e.container.width=o,e.container.height=r,e.width=o,e.height=r,this.updateLayer({width:o,height:r},s)}updateLayer(e,t){const{width:s,height:o}=e,r=t||this.fullLayer;r&&(r.parent.setAttribute("width",s),r.parent.setAttribute("height",o),r.setAttribute("size",[s,o]),r.setAttribute("pos",[s*.5,o*.5]))}getSupportContextType(e,t){const s=new OffscreenCanvas(100,100);let o=["2d"];e===$e.Full&&this.viewId===ne&&(o=["webgl2","webgl","2d"],t&&o.unshift(t));for(const r of o)try{if(s.getContext(r))return r}catch(i){throw i}}createScene(e){const{offscreenCanvasOpt:t}=e,{width:s,height:o}=t,r=new OffscreenCanvas(s,o);t.contextType&&delete t.contextType;const i=new Ge({container:r,displayRatio:this.dpr,depth:!1,desynchronized:!0,failIfMajorPerformanceCaveat:!0,...t,contextType:this.contextType,id:this.viewId});return i.setAttribute("id",this.viewId),i}createLayer(e,t,s,o){const r=`offscreen-${e}`,{width:i,height:a}=s;o&&(s.offscreen=!0,s.canvas=o);const l=t.layer(r,s),n=new V({anchor:[.5,.5],pos:[i*.5,a*.5],size:[i,a],name:"viewport",id:e});return l.append(n),n}async clearAll(){var e;this.fullLayer&&(this.fullLayer.parent.children.forEach(t=>{t.name!=="viewport"&&t.remove()}),Z(this.fullLayer,this.fullLayer.parent)),this.taskUpdateCameraId&&(clearTimeout(this.taskUpdateCameraId),this.taskUpdateCameraId=void 0),this.debounceUpdateCameraId&&(clearTimeout(this.debounceUpdateCameraId),this.debounceUpdateCameraId=void 0),this.clearCacheImages(),this.clearImageResolveMap(),this.localWork.destroy(),(e=this.serviceWork)==null||e.destroy()}setToolsOpt(e){this.localWork.setToolsOpt(e)}setWorkOpt(e){const{workId:t,toolsOpt:s}=e;t&&s&&this.localWork.setWorkOptions(t.toString(),s)}destroy(){var e;this.vNodes.clear(),this.fullLayer.remove(),H(this.fullLayer,this.fullLayer.parent),this.clearCacheImages(),this.clearImageResolveMap(),this.scene.remove(),this.localWork.destroy(),(e=this.serviceWork)==null||e.destroy()}async post(e){this.combinePostMsg.add(e),await this.runBatchPostData()}async runBatchPostData(){this.workerTaskId||(this.workerTaskId=setTimeout(()=>{this.workerTaskId=void 0,this.combinePost()},16)),this.type===$e.Full&&!this.delayPostDoneResolve&&await new Promise(e=>{this.delayPostDoneResolve=e})&&(this.delayPostDoneResolve=void 0)}combinePostData(){var e,t;this.workerTaskId=void 0;const s=[],o=[];let r,i,a;const l=new Set;for(const n of this.combinePostMsg.values()){if((e=n.render)!=null&&e.length)for(const c of n.render){let h=!1;if(c.workId&&l.add(c.workId),c.isClearAll&&(c.rect=this.getSceneRect(),c.isClear=!0,delete c.isClearAll),c.drawCanvas){const p=this.getLayer(c.drawCanvas);if(!p||!(p.parent instanceof os))continue;if(p.parent.render(),c.isDrawAll){const d=this.getSceneRect();c.rect=d,delete c.isDrawAll}}for(const p of s)if(c.viewId===p.viewId){c.isClear&&p.clearCanvas&&p.isClear&&p.clearCanvas===c.clearCanvas&&(p.rect=A(p.rect,c.rect),h=!0),p.drawCanvas&&p.drawCanvas===c.drawCanvas&&(p.rect=A(p.rect,c.rect),h=!0);continue}h||(c.isClear&&!c.drawCanvas?s.unshift(c):s.push(c))}if((t=n.sp)!=null&&t.length)for(const c of n.sp){let h=!1;for(const p of o)if(le(c,p)){h=!0;break}h||o.push(c)}q(n.fullWorkerDrawCount)&&(r=n.fullWorkerDrawCount),q(n.subWorkerDrawCount)&&(i=n.subWorkerDrawCount),q(n.consumeCount)&&(a=n.consumeCount)}return this.combinePostMsg.clear(),{render:s,sp:o,fullWorkerDrawCount:r,subWorkerDrawCount:i,consumeCount:a,workIds:l,viewId:this.viewId}}getSceneRect(){const{width:e,height:t}=this.scene;return{x:0,y:0,w:Math.floor(e),h:Math.floor(t)}}}class Gt{constructor(e){m(this,"viewId"),m(this,"vNodes"),m(this,"thread"),m(this,"fullLayer"),m(this,"drawLayer"),m(this,"_post"),m(this,"tmpOpt"),m(this,"workShapes",new Map),m(this,"drawCount",0),m(this,"consumeCount",0),m(this,"syncUnitTime",K.syncOpt.interval),m(this,"combineUnitTime",K.bezier.combineUnitTime),m(this,"maxCombineEraserTime",K.pencilEraser.maxCombineTime),this.thread=e.thread,this.viewId=e.viewId,this.vNodes=e.vNodes,this.fullLayer=e.fullLayer,this.drawLayer=e.drawLayer,this._post=this.thread.post.bind(e.thread)}setmaxCombineEraserTime(e){this.maxCombineEraserTime=e}setCombineUnitTime(e){this.combineUnitTime=e}setSyncUnitTime(e){this.syncUnitTime=e}destroy(){this.workShapes.clear()}getWorkShapes(){return this.workShapes}getWorkShape(e){return this.workShapes.get(e)}createWorkShape(e,t){if(e&&this.tmpOpt){const s={toolsType:this.tmpOpt.toolsType,toolsOpt:t||this.tmpOpt.toolsOpt},o=this.createWorkShapeNode({...s,workId:e});o&&this.workShapes.set(e,o)}}setWorkOptions(e,t){const s=this.getWorkShape(e);s||this.createWorkShape(e,t),s?.setWorkOptions(t)}createWorkShapeNode(e){var t;return Se({...e,vNodes:this.vNodes,fullLayer:this.fullLayer,drawLayer:this.drawLayer},(t=this.thread)==null?void 0:t.serviceWork)}setToolsOpt(e){var t,s,o;((t=this.tmpOpt)==null?void 0:t.toolsType)!==e.toolsType&&(s=this.tmpOpt)!=null&&s.toolsType&&this.clearAllWorkShapesCache(),this.tmpOpt=e,(o=e.toolsOpt)!=null&&o.syncUnitTime&&(this.syncUnitTime=e.toolsOpt.syncUnitTime),e.combineUnitTime&&(this.combineUnitTime=e.combineUnitTime),e.maxCombineEraserTime&&(this.maxCombineEraserTime=e.maxCombineEraserTime)}getToolsOpt(){return this.tmpOpt}clearWorkShapeNodeCache(e){var t;(t=this.getWorkShape(e))==null||t.clearTmpPoints(),this.workShapes.delete(e)}clearAllWorkShapesCache(){this.workShapes.forEach(e=>e.clearTmpPoints()),this.workShapes.clear()}setFullWork(e){const{workId:t,opt:s,toolsType:o}=e;if(t&&s&&o){const r=t.toString();let i;return t&&this.workShapes.has(r)?(i=this.workShapes.get(r),i?.setWorkOptions(s)):i=this.createWorkShapeNode({toolsOpt:s,toolsType:o,workId:r}),i?(this.workShapes.set(r,i),i):void 0}}}class Qs extends Gt{constructor(e){super(e),m(this,"drawWorkActiveId")}runSelectWork(e){var t;const s=this.setFullWork(e);s&&(t=e.selectIds)!=null&&t.length&&e.workId&&s.selectServiceNode(e.workId.toString(),{selectIds:e.selectIds},!1)}workShapesDone(){for(const e of this.workShapes.keys())this.clearWorkShapeNodeCache(e);Z(this.fullLayer,this.fullLayer.parent)}async consumeDraw(e){const{workId:t,fullWorkerDrawCount:s,postCount:o}=e,r=t?.toString(),i=r&&this.workShapes.get(r);if(!i)return;this.drawWorkActiveId&&this.drawWorkActiveId!==r&&(await this.consumeDrawAll({workId:this.drawWorkActiveId,viewId:this.viewId,msgType:y.DrawWork,dataType:x.Local}),this.drawWorkActiveId=void 0),!this.drawWorkActiveId&&r!==Y&&(this.drawWorkActiveId=r);const a=i.toolsType;q(o)&&(this.consumeCount=o);const l=i.consume({data:e,drawCount:this.drawCount,isFullWork:!0,isSubWorker:!0,removeDrawCount:s});switch(a){case S.Selector:l&&(this.drawCount++,await this.drawSelector(l));break;case S.Ellipse:case S.Arrow:case S.Straight:case S.Rectangle:case S.Star:case S.Polygon:case S.SpeechBalloon:{l&&(this.drawCount++,await this.drawShape(l));break}case S.Pencil:{l&&(this.drawCount++,await this.drawPencil(l,t?.toString()));break}case S.BitMapEraser:case S.PencilEraser:{l&&(this.drawCount++,await this._post({subWorkerDrawCount:this.drawCount,consumeCount:this.consumeCount}));break}}}async consumeDrawAll(e){const{workId:t}=e;if(t){const s=t.toString();this.drawWorkActiveId===s&&(this.drawWorkActiveId=void 0);const o=this.workShapes.get(s);if(!o)return;switch(o.toolsType){case S.Selector:this.drawCount=0,Z(this.fullLayer,this.fullLayer.parent),this.clearWorkShapeNodeCache(s),this._post({render:[{isClearAll:!0,clearCanvas:j.Float,viewId:this.viewId}]});break;case S.Arrow:case S.Straight:case S.Ellipse:case S.Pencil:case S.Rectangle:case S.Star:case S.Polygon:case S.SpeechBalloon:case S.BitMapEraser:case S.PencilEraser:this.drawCount=0,Z(this.fullLayer,this.fullLayer.parent),this.clearWorkShapeNodeCache(s);break}}}async removeWork(e){const{workId:t}=e,s=t?.toString();if(s){const o=this.removeNode(s);if(o){const r=[];r.push({rect:ue(o),clearCanvas:j.Float,isClear:!0,viewId:this.viewId},{rect:ue(o),drawCanvas:j.Float,viewId:this.viewId}),await this._post({render:r})}}}removeNode(e){const t=this.workShapes.has(e);let s;return t&&(this.fullLayer.getElementsByName(e).forEach(o=>{const r=o.getBoundingClientRect();s=A(s,{x:r.x-L.SafeBorderPadding,y:r.y-L.SafeBorderPadding,w:r.width+L.SafeBorderPadding*2,h:r.height+L.SafeBorderPadding*2}),o.remove(),H(o,this.fullLayer.parent)}),s&&this.clearWorkShapeNodeCache(e)),s}async drawPencil(e,t){await this._post({subWorkerDrawCount:this.drawCount,consumeCount:this.consumeCount,render:[{rect:e?.rect,workId:t,drawCanvas:j.Float,viewId:this.viewId}],sp:e?.op&&[e]})}async drawShape(e){await this._post({subWorkerDrawCount:this.drawCount,consumeCount:this.consumeCount,render:[{rect:e?.rect&&ue(e.rect),isClear:!0,clearCanvas:j.Float,viewId:this.viewId},{rect:e?.rect&&ue(e.rect),drawCanvas:j.Float,viewId:this.viewId}]})}async drawSelector(e){await this._post({subWorkerDrawCount:this.drawCount,consumeCount:this.consumeCount,render:[{rect:e?.rect&&ue(e.rect),isClear:!0,clearCanvas:j.Float,viewId:this.viewId},{rect:e?.rect&&ue(e.rect),drawCanvas:j.Float,viewId:this.viewId}]})}}class Js{constructor(e){m(this,"viewId"),m(this,"vNodes"),m(this,"topLayer"),m(this,"thread"),m(this,"post"),m(this,"serviceWorkShapes",new Map),m(this,"localWorkShapes",new Map),m(this,"tmpOpt"),m(this,"syncUnitTime",K.syncOpt.interval),m(this,"animationId"),this.viewId=e.viewId,this.vNodes=e.vNodes,this.topLayer=e.topLayer,this.thread=e.thread,this.post=e.thread.post.bind(e.thread)}canUseTopLayer(e){return e===S.LaserPen}getWorkShape(e){return this.localWorkShapes.get(e)}createWorkShape(e,t){if(e&&this.tmpOpt){const s={toolsType:this.tmpOpt.toolsType,toolsOpt:t||this.tmpOpt.toolsOpt},o=this.createWorkShapeNode({...s,workId:e});return o&&this.localWorkShapes.set(e,{node:o,toolsType:o.toolsType,workState:M.Start}),o}}setWorkOptions(e,t){var s;const o=(s=this.localWorkShapes.get(e))==null?void 0:s.node;o||this.createWorkShape(e,t),o?.setWorkOptions(t)}createWorkShapeNode(e){const{toolsType:t}=e;if(t===S.LaserPen)return Se({...e,vNodes:this.vNodes,fullLayer:this.topLayer,drawLayer:this.topLayer})}clearAllWorkShapesCache(){this.localWorkShapes.forEach(e=>{var t;return(t=e.node)==null?void 0:t.clearTmpPoints()}),this.localWorkShapes.clear()}setToolsOpt(e){var t;this.tmpOpt=e,(t=e.toolsOpt)!=null&&t.syncUnitTime&&(this.syncUnitTime=e.toolsOpt.syncUnitTime)}getToolsOpt(){return this.tmpOpt}consumeDraw(e){const{workId:t,dataType:s}=e;if(s===x.Service)this.activeServiceWorkShape(e);else{const o=t?.toString(),r=o&&this.localWorkShapes.get(o);if(!r)return;const i=r.node.consume({data:e,isFullWork:!1,isSubWorker:!0});i.rect&&(r.totalRect=A(i.rect,r.totalRect),r.result=i,r.workState=M.Doing,o&&this.localWorkShapes.set(o,r))}this.runAnimation()}consumeDrawAll(e){const{workId:t,dataType:s}=e;if(s===x.Service)this.activeServiceWorkShape(e);else{const o=t?.toString(),r=o&&this.localWorkShapes.get(o);if(!r)return;const i=r.node.consumeAll({data:e});r.totalRect=A(i.rect,r.totalRect),r.result=i,r.workState=M.Done,o&&this.localWorkShapes.set(o,r)}this.runAnimation()}destroy(){this.serviceWorkShapes.clear(),this.localWorkShapes.clear()}setNodeKey(e,t,s,o){return t.toolsType=s,t.node=this.createWorkShapeNode({workId:e,toolsType:s,toolsOpt:o}),t}activeServiceWorkShape(e){var t,s;const{workId:o,opt:r,toolsType:i,type:a,updateNodeOpt:l,ops:n,op:c}=e;if(!o)return;const h=o.toString(),p=(t=this.vNodes.get(h))==null?void 0:t.rect;if(!((s=this.serviceWorkShapes)!=null&&s.has(h))){let u={toolsType:i,animationWorkData:c||[],animationIndex:0,type:a,updateNodeOpt:l,ops:n,oldRect:p};i&&r&&(u=this.setNodeKey(h,u,i,r)),this.serviceWorkShapes.set(h,u)}const d=this.serviceWorkShapes.get(h);a&&(d.type=a),n&&(d.animationWorkData=fe(n),d.ops=n),l&&(d.updateNodeOpt=l),c&&(d.animationWorkData=c),d.node&&d.node.getWorkId()!==h&&d.node.setWorkId(h),p&&(d.oldRect=p),i&&r&&(d.toolsType!==i&&i&&r&&this.setNodeKey(h,d,i,r),d.node&&d.node.setWorkOptions(r))}computNextAnimationIndex(e,t){const s=Math.floor((e.animationWorkData||[]).slice(e.animationIndex).length*32/t/this.syncUnitTime)*t;return Math.min((e.animationIndex||0)+(s||t),(e.animationWorkData||[]).length)}async animationDraw(){var e,t,s,o;this.animationId=void 0;let r=!1;const i=new Map,a=[],l=[],n=[];for(const[c,h]of this.serviceWorkShapes.entries())switch(h.toolsType){case S.LaserPen:{const p=this.computNextAnimationIndex(h,8),d=Math.max(0,h.animationIndex||0),u=(h.animationWorkData||[]).slice(d,p);if((h.animationIndex||0)<p){const f=(e=h.node)==null?void 0:e.consumeService({op:u,isFullWork:!1});h.totalRect=A(h.totalRect,f),h.animationIndex=p,u.length&&i.set(c,{workState:d===0?M.Start:p===((t=h.animationWorkData)==null?void 0:t.length)?M.Done:M.Doing,op:u.slice(-2)})}if(l.push({isClear:!0,rect:h.totalRect,clearCanvas:j.TopFloat,viewId:this.viewId}),a.push({rect:h.totalRect,drawCanvas:j.TopFloat,viewId:this.viewId}),h.isDel){(s=h.node)==null||s.clearTmpPoints(),this.serviceWorkShapes.delete(c);break}h.ops&&h.animationIndex===((o=h.animationWorkData)==null?void 0:o.length)&&!h.isDel&&(this.topLayer.getElementsByName(c.toString())[0]||(h.isDel=!0,this.serviceWorkShapes.set(c,h))),r=!0;break}}for(const[c,h]of this.localWorkShapes.entries()){const{result:p,toolsType:d,totalRect:u,isDel:f,workState:w}=h;switch(d){case S.LaserPen:{if(u&&(l.push({isClear:!0,rect:u,clearCanvas:j.TopFloat,viewId:this.viewId}),a.push({rect:u,drawCanvas:j.TopFloat,viewId:this.viewId})),f){h.node.clearTmpPoints(),this.localWorkShapes.delete(c),n.push({removeIds:[c.toString()],viewId:this.viewId,type:y.RemoveNode});break}p&&((p.op||p.ops)&&n.push(p),h.result=void 0),!this.topLayer.getElementsByName(c.toString())[0]&&w===M.Done&&(h.isDel=!0,this.localWorkShapes.set(c,h)),r=!0;break}}}r&&this.runAnimation(),i.size&&i.forEach((c,h)=>{n.push({type:y.Cursor,uid:h.split(ve)[0],op:c.op,workState:c.workState,viewId:this.viewId})}),(a.length||l.length||n.length)&&this.post({render:[...l,...a],sp:n})}runAnimation(){this.animationId||(this.animationId=setTimeout(()=>{this.animationId=void 0,this.animationDraw()},16))}}class eo extends Gt{constructor(e){super(e),m(this,"opt"),m(this,"scene"),m(this,"cameraOpt"),m(this,"vNodes"),this.opt=e,this.vNodes=void 0}createSnapshotFullLayer(e,t){this.scene=this.opt.createScene(e),this.fullLayer=this.opt.createLayer("snapshotFullLayer",this.scene,t)}destroySnapshotFullLayer(){var e,t,s;Z(this.fullLayer,this.fullLayer.parent),(e=this.fullLayer)==null||e.disconnect(),(t=this.fullLayer)==null||t.remove(),(s=this.scene)==null||s.remove(),this.scene=void 0}updateScene(e){if(!this.scene)throw new Error("SnapshotWork scene is not initialized");this.scene.attr({...e});const{width:t,height:s}=e;this.scene.width=t,this.scene.height=s,this.updateLayer({width:t,height:s})}updateLayer(e){if(!this.fullLayer)throw new Error("SnapshotWork snapshotFullLayer is not initialized");const{width:t,height:s}=e;this.fullLayer.parent.setAttribute("width",t),this.fullLayer.parent.setAttribute("height",s),this.fullLayer.setAttribute("size",[t,s]),this.fullLayer.setAttribute("pos",[t*.5,s*.5])}setCameraOpt(e){this.cameraOpt=e;const{scale:t,centerX:s,centerY:o,width:r,height:i}=e;if(!this.scene)throw new Error("SnapshotWork scene is not initialized");if(!this.fullLayer)throw new Error("SnapshotWork snapshotFullLayer is not initialized");(r!==this.scene.width||i!==this.scene.height)&&this.updateScene({width:r,height:i}),this.fullLayer.setAttribute("scale",[t,t]),this.fullLayer.setAttribute("translate",[-s,-o])}getOffscreen(){var e;return((e=this.fullLayer)==null?void 0:e.parent).canvas}getRectImageBitmap(e,t){const{rect:s}=e,o=this.thread.dpr,r=Math.floor(s.x*o),i=Math.floor(s.y*o),a=Math.floor(s.w*o),l=Math.floor(s.h*o);return createImageBitmap(this.getOffscreen(),r,i,a,l,t)}async getSnapshot(e){const{scenePath:t,scenes:s,cameraOpt:o,w:r,h:i}=e;if(t&&s&&o&&this.fullLayer){this.setCameraOpt(o);let a;for(const[n,c]of Object.entries(s))if(c!=null&&c.type)switch(c?.type){case y.UpdateNode:case y.FullWork:{const{opt:h}=c,p={...c,opt:h,workId:n,msgType:y.FullWork,dataType:x.Service,viewId:this.viewId},d=await this.runFullWork(p);a=A(a,d);break}}let l;r&&i&&(l={resizeWidth:r,resizeHeight:i}),await this.getSnapshotRender({scenePath:t,options:l})}}async runFullWork(e){var t;const s=this.setFullWork(e),o=e.ops&&fe(e.ops);if(s){let r,i;const a=(t=s.getWorkId())==null?void 0:t.toString();return s.toolsType===S.Image?r=await s.consumeServiceAsync({isFullWork:!0,worker:this.thread}):s.toolsType===S.Text?r=await s.consumeServiceAsync({isFullWork:!0,replaceId:a,isDrawLabel:!0}):(r=s.consumeService({op:o,isFullWork:!0,replaceId:a}),i=e?.updateNodeOpt&&s.updataOptService(e.updateNodeOpt)),e.workId&&this.workShapes.delete(e.workId.toString()),A(r,i)}}getSceneRect(){if(!this.scene)throw new Error("SnapshotWork scene is not initialized");const{width:e,height:t}=this.scene;return{x:0,y:0,w:Math.floor(e),h:Math.floor(t)}}async getSnapshotRender(e){var t;const{scenePath:s,options:o}=e;((t=this.fullLayer)==null?void 0:t.parent).render();const r=await this.getRectImageBitmap({rect:this.getSceneRect(),drawCanvas:j.None},o);r&&(this.thread._post({sp:[{type:y.Snapshot,scenePath:s,imageBitmap:r,viewId:this.viewId}]},[r]),r.close())}async getBoundingRect(e){const{scenePath:t,scenes:s,cameraOpt:o}=e;if(t&&s&&o&&this.fullLayer){this.setCameraOpt(o);let r;for(const[i,a]of Object.entries(s))if(a!=null&&a.type)switch(a?.type){case y.UpdateNode:case y.FullWork:{const l=await this.runFullWork({...a,workId:i,msgType:y.FullWork,dataType:x.Service,viewId:this.viewId});r=A(r,l);break}}r&&this.thread._post({sp:[{type:y.BoundingBox,scenePath:t,rect:r,viewId:this.viewId}]})}}}var $e=(v=>(v.Full="full",v.Sub="sub",v))($e||{});class to extends Ks{constructor(e,t,s){super(e,t,"sub"),m(this,"type","sub"),m(this,"_post"),m(this,"topLayer"),m(this,"serviceWork"),m(this,"localWork"),m(this,"topLayerWork"),m(this,"snapshotWork"),this._post=s;const o=K.bufferSize.sub;this.topLayer=t.isUseSimple?void 0:this.createLayer("topLayer",this.scene,{...t.layerOpt,bufferSize:o,contextType:"2d"});const r={thread:this,viewId:this.viewId,vNodes:this.vNodes,fullLayer:this.fullLayer,topLayer:this.topLayer,isUseSimple:t.isUseSimple};this.localWork=new Qs(r),t.isUseSimple||(this.topLayerWork=new Js(r)),this.snapshotWork=new eo({...r,createScene:this.createScene.bind(this),createLayer:this.createLayer.bind(this)}),this.vNodes.init(this.fullLayer)}async combinePost(){var e,t;const{render:s,...o}=this.combinePostData();let r;if(s!=null&&s.length){const a=[];for(const l of s)if(l.rect){if(l.rect=ro(ee(l.rect),this.scene,this.dpr),!l.rect)continue;if(l.drawCanvas&&l.rect&&l.rect.w>0&&l.rect.h>0){const n=await this.getRectImageBitmap(l);l.imageBitmap=n,r||(r=[]),r.push(n)}a.push(l)}o.render=a}const i=(e=o.sp)==null?void 0:e.filter(a=>a.type!==y.None||a.isLockSentEventCursor);i!=null&&i.length?o.sp=i.map(a=>({...a,viewId:this.viewId})):delete o.sp,o.consumeCount===void 0&&delete o.consumeCount,o.subWorkerDrawCount===void 0&&delete o.subWorkerDrawCount,(i!=null&&i.length||o.consumeCount||o.subWorkerDrawCount||(t=o?.render)!=null&&t.length)&&this._post(o,r),this.delayPostDoneResolve&&this.delayPostDoneResolve(!0)}async on(e){var t,s;const{msgType:o,toolsType:r,opt:i,dataType:a,workState:l,isLockSentEventCursor:n}=e;switch(o){case y.UpdateTools:if(r&&(t=this.topLayerWork)!=null&&t.canUseTopLayer(r)&&i){const c={toolsType:r,toolsOpt:i};this.topLayerWork.setToolsOpt(c);return}break;case y.CreateWork:this.createLocalWork(e);return;case y.DrawWork:l===M.Done&&a===x.Local?(await this.consumeDrawAll(a,e),r===S.LaserPen&&n&&this.post({sp:[{type:y.None,isLockSentEventCursor:n}]})):this.consumeDraw(a,e);return;case y.RemoveNode:await this.removeNode(e);return;case y.FullWork:r&&(s=this.topLayerWork)!=null&&s.canUseTopLayer(r)&&await this.consumeDrawAll(a,e);return;case y.Snapshot:this.snapshotWork.createSnapshotFullLayer(this.opt,{...this.opt.layerOpt,bufferSize:this.viewId===ne?6e3:3e3,contextType:"2d"}),await this.snapshotWork.getSnapshot(e),this.snapshotWork.destroySnapshotFullLayer();return;case y.BoundingBox:this.snapshotWork.createSnapshotFullLayer(this.opt,{...this.opt.layerOpt,bufferSize:this.viewId===ne?6e3:3e3,contextType:"2d"}),await this.snapshotWork.getBoundingRect(e),this.snapshotWork.destroySnapshotFullLayer();return}await super.on(e)}createLocalWork(e){var t;const{workId:s,toolsType:o,opt:r}=e;if(o&&(t=this.topLayerWork)!=null&&t.canUseTopLayer(o)&&s&&r){this.topLayerWork.getToolsOpt()||this.topLayerWork.setToolsOpt({toolsType:o,toolsOpt:r}),this.topLayerWork.setWorkOptions(s.toString(),r);return}o&&super.createLocalWork(e)}async removeNode(e){const{dataType:t}=e;t===x.Local&&await this.localWork.removeWork(e)}getLayer(e){switch(e){case j.TopFloat:return this.topLayer||this.fullLayer;default:return this.fullLayer}}getOffscreen(e){var t;return((t=this.getLayer(e))==null?void 0:t.parent).canvas}async consumeDraw(e,t){var s;const{workId:o,toolsType:r}=t;if(o){if(r&&(s=this.topLayerWork)!=null&&s.canUseTopLayer(r)){e===x.Local&&(this.topLayerWork.getWorkShape(o.toString())||this.createLocalWork(t)),this.topLayerWork.consumeDraw(t);return}r&&(this.localWork.getWorkShape(o.toString())||this.createLocalWork(t),await this.localWork.consumeDraw(t));return}}async consumeDrawAll(e,t){var s;const{workId:o,toolsType:r,dataType:i}=t;if(o){const a=o.toString();if(r&&(s=this.topLayerWork)!=null&&s.canUseTopLayer(r)){i===x.Local&&(this.topLayerWork.getWorkShape(a)||this.createLocalWork(t)),this.topLayerWork.consumeDrawAll(t);return}r&&(this.localWork.getWorkShape(a)||super.createLocalWork(t),this.localWork.consumeDrawAll(t));return}}async clearAll(){this.vNodes.clear(),super.clearAll(),this.topLayer&&(this.topLayer.parent.children.forEach(e=>{e.name!=="viewport"&&e.remove()}),Z(this.topLayer,this.topLayer.parent)),await this.post({render:[{isClearAll:!0,clearCanvas:j.TopFloat,viewId:this.viewId}],sp:[{type:y.Clear}]})}getRectImageBitmap(e,t){const{rect:s,drawCanvas:o}=e,r=Math.floor(s.x*this.dpr),i=Math.floor(s.y*this.dpr),a=Math.floor(s.w*this.dpr||1),l=Math.floor(s.h*this.dpr||1);return createImageBitmap(this.getOffscreen(o),r,i,a,l,t)}updateLayer(e){const{width:t,height:s}=e;super.updateLayer(e),this.topLayer&&(this.topLayer.parent.setAttribute("width",t),this.topLayer.parent.setAttribute("height",s),this.topLayer.setAttribute("size",[t,s]),this.topLayer.setAttribute("pos",[t*.5,s*.5]))}async updateDpr(e){super.updateDpr(e);const t=this.topLayerWork&&!!this.topLayerWork.localWorkShapes.size||!1,s=!!this.localWork.getWorkShapes().size,o=[];s&&o.push({isClearAll:!0,clearCanvas:j.Float,viewId:this.viewId}),t&&o.push({isClearAll:!0,clearCanvas:j.TopFloat,viewId:this.viewId}),o.length&&await this.post({render:o})}async updateCamera(e){var t;const s=[],{cameraOpt:o}=e;if(o&&!le(this.cameraOpt,o)){const r=this.topLayerWork&&!!this.topLayerWork.localWorkShapes.size||!1,i=!!this.localWork.getWorkShapes().size;if(i&&this.localWork.workShapesDone(),this.setCameraOpt(o),i&&s.push({isClearAll:!0,clearCanvas:j.Float,viewId:this.viewId}),r&&this.topLayerWork){s.push({isClearAll:!0,clearCanvas:j.TopFloat,viewId:this.viewId});for(const[a,l]of this.topLayerWork.localWorkShapes.entries()||[])if(l.totalRect){let n;(t=this.topLayer)==null||t.getElementsByName(a.toString()).forEach(c=>{const h=c.getBoundingClientRect(),p=ue({x:h.x,y:h.y,w:h.width,h:h.height});n=A(n,p)}),l.totalRect=n,this.topLayerWork.localWorkShapes.set(a,l)}}s.length&&await this.post({render:s})}}setCameraOpt(e,t){var s,o;this.cameraOpt=e;const{scale:r,centerX:i,centerY:a,width:l,height:n}=e;(l!==this.scene.width||n!==this.scene.height)&&this.updateScene(this.scene,{width:l,height:n}),t?(t.setAttribute("scale",[r,r]),t.setAttribute("translate",[-i,-a])):(this.fullLayer.setAttribute("scale",[r,r]),this.fullLayer.setAttribute("translate",[-i,-a]),(s=this.topLayer)==null||s.setAttribute("scale",[r,r]),(o=this.topLayer)==null||o.setAttribute("translate",[-i,-a]))}}class Le extends L{constructor(e){super(e),m(this,"canRotate",!0),m(this,"scaleType",U.all),m(this,"toolsType",S.Image),m(this,"tmpPoints",[]),m(this,"workOptions"),m(this,"oldRect"),this.workOptions=e.toolsOpt,this.scaleType=Le.getScaleType(this.workOptions)}consume(){return{type:y.None}}consumeAll(){return{type:y.None}}draw(e){const{layer:t,workId:s,replaceId:o,imageBitmap:r}=e,{centerX:i,centerY:a,width:l,height:n,rotate:c,zIndex:h,eraserlines:p}=this.workOptions,d=new V({anchor:[.5,.5],pos:[i,a],name:s,size:[l,n],zIndex:h,rotate:c}),u={anchor:[.5,.5],pos:[0,0],size:[l,n],texture:r};r||(u.bgcolor="rgba(0,0,0,0.3)");const f=new mt(u);d.append(f),p&&this.drawEraserlines({group:d,eraserlines:p,pos:[i,a],layer:t}),this.replace(t,o||s,d);const w=d.getBoundingClientRect();if(w)return{x:Math.floor(w.x-L.SafeBorderPadding),y:Math.floor(w.y-L.SafeBorderPadding),w:Math.floor(w.width+L.SafeBorderPadding*2),h:Math.floor(w.height+L.SafeBorderPadding*2)}}consumeService(){}async consumeServiceAsync(e){var t,s,o,r;const{isFullWork:i,replaceId:a,worker:l}=e,{src:n,uuid:c}=this.workOptions,h=((t=this.workId)==null?void 0:t.toString())||c,p=i?this.fullLayer:this.drawLayer||this.fullLayer;if(n){const d=await l.loadImageBitMap({toolsType:this.toolsType,opt:this.workOptions,workId:h,isSubWorker:l instanceof to});if(d){const u=this.draw({workId:h,layer:p,replaceId:a,imageBitmap:d});return this.oldRect=h&&((o=(s=this.vNodes)==null?void 0:s.get(h))==null?void 0:o.rect)||void 0,(r=this.vNodes)==null||r.setInfo(h,{rect:u,op:[],opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:u&&L.getCenterPos(u,p)}),u}}}clearTmpPoints(){this.tmpPoints.length=0}static getScaleType(e){const{uniformScale:t,rotate:s}=e;return t!==!1?U.proportional:s&&Math.abs(s)%90>0?U.proportional:U.all}static updateNodeOpt(e){const{node:t,opt:s,vNodes:o,targetNode:r}=e,{translate:i,originPoint:a,scenePoint:l,scale:n,angle:c,isLocked:h,zIndex:p,strokeColor:d}=s,u=r&&ee(r)||o.get(t.name);if(!u)return;const f=t.parent;if(f){if(d&&(u.opt.strokeColor=d),q(p)&&(t.setAttribute("zIndex",p),u.opt.zIndex=p),ge(h)&&(u.opt.locked=h),a&&l&&n&&i){const{centerX:w,centerY:k,width:g,height:I,uniformScale:T}=u.opt,W=T!==!1?[n[0],n[0]]:n,N=[w,k],R=[w,k];He(R,l,W,i);const P=[R[0]-N[0],R[1]-N[1]];if(u.centerPos=[u.centerPos[0]+P[0],u.centerPos[1]+P[1]],u.opt.width=Math.round(g*W[0]),u.opt.height=Math.round(I*W[1]),u.opt.centerX=R[0],u.opt.centerY=R[1],u.opt.eraserlines)for(const b of u.opt.eraserlines){const{op:D,thickness:B}=b;b.thickness=Math.round(B*Math.max(n[0],n[1]));for(let E=0;E<D.length;E++)_e(D[E],l,n,i)}}else if(i){if(u.opt.centerX=u.opt.centerX+i[0],u.opt.centerY=u.opt.centerY+i[1],u.centerPos=[u.centerPos[0]+i[0],u.centerPos[1]+i[1]],u.opt.eraserlines)for(const w of u.opt.eraserlines){const{op:k}=w;for(let g=0;g<k.length;g++){const I=k[g].map((T,W)=>W%2?T+i[1]:T+i[0]);w.op[g]=I}}}else if(q(c))if(u.opt.rotate=c,u.scaleType=Le.getScaleType(u.opt),r){const w=ft(u.rect,c);u.rect=w}else{const w=L.getRectFromLayer(f,t.name);u.rect=w||u.rect}return u&&o.setInfo(t.name,u),u?.rect}}}const so=v=>{if(v.tagName==="GROUP"){const e=Object.getOwnPropertySymbols(v).find(t=>t.toString()==="Symbol(sealed)");if(e&&v[e])return!0}return!1},H=(v,e)=>{if(v.mesh&&v.mesh.texture&&v.name==="eraserTexture"){const t=v.mesh.texture.image;e.deleteTexture(t)}else if(v.tagName==="GROUP"&&v&&v.children)for(const t of v.children)H(t,e)},Z=(v,e)=>{H(v,e),v.removeAllChildren()},oo=(v,e,t)=>{const s=t.parent;if(e){const o=e.children;if(o){for(const r of o)if(H(r,s),r.tagName==="GROUP")for(const i of r.children)H(i,s)}}e.parent.replaceChild(v,e)},ro=(v,e,t)=>{if(v.w+v.x<=0||v.h+v.y<=0||v.w<=0||v.h<=0)return;const s=e.width,o=e.height,r={x:Math.floor(Math.max(0,v.x)),y:Math.floor(Math.max(0,v.y)),w:Math.floor(Math.min(s,v.w)),h:Math.floor(Math.min(o,v.h))};if(r.x+r.w>s&&(r.w=Math.floor(s-r.x)),r.y+r.h>o&&(r.h=Math.floor(o-r.y)),rs(t)){const i=is(t),a=Math.pow(10,i),l=ds(a,t*a),n=r.x%l,c=r.x-n;c>=0?(r.x=c,r.w=r.w+n):(r.x=0,r.w=r.w+n-c);const h=r.y%l,p=r.y-h;p>=0?(r.y=p,r.h=r.h+h):(r.y=0,r.h=r.h+h-p)}return r};class io{constructor(e){m(this,"vNodes"),m(this,"thread"),m(this,"serviceWorkShapes",new Map),m(this,"localWorkShapes",new Map),m(this,"tmpOpt"),m(this,"animationId"),m(this,"syncUnitTime",K.syncOpt.interval),this.vNodes=e.vNodes,this.thread=e.thread}createLocalWork(e){const{workId:t,opt:s,toolsType:o}=e;if(t&&s){const r=t.toString();!this.getToolsOpt()&&o&&this.setToolsOpt({toolsType:o,toolsOpt:s}),this.setWorkOptions(r,s)}}getLocalWorkShape(e){return this.localWorkShapes.get(e)}createLocalWorkShape(e,t){if(e&&this.tmpOpt){const s={toolsType:this.tmpOpt.toolsType,toolsOpt:t||this.tmpOpt.toolsOpt},o=this.createWorkShapeNode({...s,workId:e});return o&&this.localWorkShapes.set(e,{node:o,toolsType:o.toolsType,workState:M.Start}),o}}canUseTopLayer(e){return e===S.LaserPen}destroy(){this.clearAll()}clearAll(){this.thread.topLayer.children.length&&(this.thread.topLayer.parent.children.forEach(e=>{e.name!=="viewport"&&e.remove()}),Z(this.thread.serviceLayer,this.thread.serviceLayer.parent)),this.serviceWorkShapes.clear(),this.localWorkShapes.clear()}consumeDraw(e){const{workId:t,dataType:s}=e;if(s===x.Service)this.activeServiceWorkShape(e);else{const o=t?.toString(),r=o&&this.localWorkShapes.get(o);if(!r)return;const i=r.node.consume({data:e,isFullWork:!1,isSubWorker:!0});i.rect&&(r.result=i,r.workState=M.Doing,o&&this.localWorkShapes.set(o,r))}this.runAnimation()}setToolsOpt(e){var t;this.tmpOpt=e,(t=e.toolsOpt)!=null&&t.syncUnitTime&&(this.syncUnitTime=e.toolsOpt.syncUnitTime)}getToolsOpt(){return this.tmpOpt}createWorkShapeNode(e){const{toolsType:t}=e;if(t===S.LaserPen)return Se({...e,vNodes:this.vNodes,fullLayer:this.thread.topLayer,drawLayer:this.thread.topLayer})}setNodeKey(e,t,s,o){return t.toolsType=s,t.node=this.createWorkShapeNode({workId:e,toolsType:s,toolsOpt:o}),t}activeServiceWorkShape(e){var t,s;const{workId:o,opt:r,toolsType:i,type:a,updateNodeOpt:l,ops:n,op:c}=e;if(!o)return;const h=o.toString(),p=(t=this.vNodes.get(h))==null?void 0:t.rect;if(!((s=this.serviceWorkShapes)!=null&&s.has(h))){let u={toolsType:i,animationWorkData:c||[],animationIndex:0,type:a,updateNodeOpt:l,ops:n,oldRect:p};i&&r&&(u=this.setNodeKey(h,u,i,r)),this.serviceWorkShapes.set(h,u)}const d=this.serviceWorkShapes.get(h);a&&(d.type=a),n&&(d.animationWorkData=fe(n),d.ops=n),l&&(d.updateNodeOpt=l),c&&(d.animationWorkData=c),d.node&&d.node.getWorkId()!==h&&d.node.setWorkId(h),p&&(d.oldRect=p),i&&r&&(d.toolsType!==i&&i&&r&&this.setNodeKey(h,d,i,r),d.node&&d.node.setWorkOptions(r))}computNextAnimationIndex(e,t){var s;const o=((s=e.node)==null?void 0:s.syncUnitTime)||this.syncUnitTime,r=Math.floor((e.animationWorkData||[]).slice(e.animationIndex).length*32/t/o)*t;return Math.min((e.animationIndex||0)+(r||t),(e.animationWorkData||[]).length)}animationDraw(){var e,t,s,o;this.animationId=void 0;let r=!1;const i=new Map,a=[];for(const[l,n]of this.serviceWorkShapes.entries())switch(n.toolsType){case S.LaserPen:{const c=this.computNextAnimationIndex(n,8),h=Math.max(0,n.animationIndex||0),p=(n.animationWorkData||[]).slice(h,c);if((n.animationIndex||0)<c&&((e=n.node)==null||e.consumeService({op:p,isFullWork:!1}),n.animationIndex=c,p.length&&i.set(l,{workState:h===0?M.Start:c===((t=n.animationWorkData)==null?void 0:t.length)?M.Done:M.Doing,op:p.slice(-2)})),n.isDel){(s=n.node)==null||s.clearTmpPoints(),this.serviceWorkShapes.delete(l);break}n.ops&&n.animationIndex===((o=n.animationWorkData)==null?void 0:o.length)&&!n.isDel&&(this.thread.topLayer.getElementsByName(l.toString())[0]||(n.isDel=!0,this.serviceWorkShapes.set(l,n))),r=!0;break}}for(const[l,n]of this.localWorkShapes.entries()){const{result:c,toolsType:h,isDel:p,workState:d}=n;switch(h){case S.LaserPen:{if(p){n.node.clearTmpPoints(),this.localWorkShapes.delete(l),a.push({removeIds:[l.toString()],type:y.RemoveNode});break}c&&((c.op||c.ops)&&a.push(c),n.result=void 0),!this.thread.topLayer.getElementsByName(l.toString())[0]&&d===M.Done&&(n.isDel=!0,this.localWorkShapes.set(l,n)),r=!0;break}}}r&&this.runAnimation(),i.size&&i.forEach((l,n)=>{a.push({type:y.Cursor,uid:n.split(ve)[0],op:l.op,workState:l.workState,viewId:this.thread.viewId})}),a.length&&this.thread.post({sp:a})}runAnimation(){this.animationId||(this.animationId=requestAnimationFrame(this.animationDraw.bind(this)))}setWorkOptions(e,t){var s;let o=(s=this.localWorkShapes.get(e))==null?void 0:s.node;if(!o&&this.tmpOpt){const{toolsType:r}=this.tmpOpt;this.tmpOpt.toolsOpt=t,o=this.createWorkShapeNode({workId:e,toolsType:r,toolsOpt:t}),o&&this.localWorkShapes.set(e,{node:o,toolsType:r,workState:M.Start}),this.setToolsOpt(this.tmpOpt)}t!=null&&t.syncUnitTime||(t.syncUnitTime=this.syncUnitTime),o&&o.setWorkOptions(t)}consumeDrawAll(e){const{workId:t,dataType:s}=e;if(s===x.Service)this.activeServiceWorkShape(e);else{const o=t?.toString(),r=o&&this.localWorkShapes.get(o);if(!r)return;const i=r.node.consumeAll({data:e});r.result=i,r.workState=M.Done,o&&this.localWorkShapes.set(o,r)}this.runAnimation()}}class ao{constructor(e){m(this,"vNodes"),m(this,"thread"),m(this,"workShapes",new Map),m(this,"effectSelectNodeData",new Set),m(this,"batchEraserRemoveNodes",new Set),m(this,"batchEraserWorks",new Set),m(this,"tmpOpt"),m(this,"syncUnitTime",K.syncOpt.interval),m(this,"fullWorkerDrawCount",0),m(this,"drawWorkActiveId"),m(this,"consumeCount",0),m(this,"combineTimerId"),m(this,"combineDrawResolve"),m(this,"combineDrawActiveId"),this.vNodes=e.vNodes,this.thread=e.thread}async loadImageBitMap(e){return await this.thread.loadImageBitMap(e)}createLocalWork(e){const{workId:t,opt:s,toolsType:o}=e;if(t&&s){const r=t.toString();!this.getToolsOpt()&&o&&this.setToolsOpt({toolsType:o,toolsOpt:s}),this.setWorkOptions(r,s)}}async updateSelector(e){var t;const s=this.workShapes.get(Y);if(!((t=s?.selectIds)!=null&&t.length))return;const{callback:o,...r}=e,{updateSelectorOpt:i,willSerializeData:a,smoothSync:l}=r,n=await s?.updateSelector({updateSelectorOpt:i,selectIds:ae.cloneDeep(s.selectIds),vNodes:this.vNodes,willSerializeData:a,worker:this}),c=new Map;let h;n!=null&&n.selectIds&&(h=ae.xor(s.selectIds,n.selectIds),n.selectIds.forEach(u=>{const f=this.vNodes.get(u);if(f){const{toolsType:w,op:k,opt:g}=f;c.set(u,{opt:g,toolsType:w,ops:k?.length&&Q(k)||void 0})}}),s.selectIds=n.selectIds);const p=[],d=o&&o({res:n,workShapeNode:s,param:r,postData:{sp:p},newServiceStore:c,smoothSync:l})||{sp:p};h&&d.sp.push({type:y.RemoveNode,removeIds:h,viewId:this.thread.viewId}),d.sp.length&&this.thread.post(d)}destroy(){this.clearAll()}clearAll(){if(this.thread.localLayer.children.length&&(this.thread.topLayer.parent.children.forEach(e=>{e.name!=="viewport"&&e.remove()}),Z(this.thread.localLayer,this.thread.localLayer.parent)),this.workShapes.get(Y)){const e=[];e.push({type:y.Select,dataType:x.Local,selectIds:[],willSyncService:!1}),this.thread.post({sp:e})}this.workShapes.clear(),this.effectSelectNodeData.clear(),this.batchEraserWorks.clear(),this.batchEraserRemoveNodes.clear()}async checkTextActive(e){const{op:t,viewId:s,dataType:o}=e;if(t!=null&&t.length){let r;for(const i of this.vNodes.curNodeMap.values()){const{rect:a,name:l,toolsType:n,opt:c}=i,h=t[0]*this.thread.fullLayer.worldScaling[0]+this.thread.fullLayer.worldPosition[0],p=t[1]*this.thread.fullLayer.worldScaling[1]+this.thread.fullLayer.worldPosition[1];if(n===S.Text&&Vt([h,p],a)&&c.workState===M.Done){r=l;break}}r&&(await this.blurSelector({viewId:s,msgType:y.Select,dataType:o,isSync:!0}),this.thread.post({sp:[{type:y.GetTextActive,toolsType:S.Text,workId:r}]}))}}cursorHover(e){const{opt:t,toolsType:s,point:o}=e,r=this.setFullWork({workId:Ie,toolsType:s,opt:t});r&&o&&r.cursorHover(o)}cursorBlur(){var e;const t=this.getWorkShape(Ie);t&&(e=t.selectIds)!=null&&e.length&&(t.cursorBlur(),this.clearWorkShapeNodeCache(Ie)),this.thread.fullLayer.parent.children.forEach(s=>{s.name==="Cursor_Hover_Id"&&s.remove()})}updateFullSelectWork(e){var t,s,o,r,i;const a=this.workShapes.get(Y),{selectIds:l}=e;if(!(l!=null&&l.length)){this.blurSelector(e);return}if(!a){const n=this.setFullWork(e);!n&&e.workId&&this.tmpOpt&&((t=this.tmpOpt)==null?void 0:t.toolsType)===S.Selector&&this.setWorkOptions(e.workId.toString(),e.opt||this.tmpOpt.toolsOpt),n&&this.updateFullSelectWork(e);return}if(a&&l!=null&&l.length){const{selectRect:n}=a.updateSelectIds(l),c=[{...e,selectorColor:((s=e.opt)==null?void 0:s.strokeColor)||a.selectorColor,strokeColor:((o=e.opt)==null?void 0:o.strokeColor)||a.strokeColor,fillColor:((r=e.opt)==null?void 0:r.fillColor)||a.fillColor,textOpt:((i=e.opt)==null?void 0:i.textOpt)||a.textOpt,canTextEdit:a.canTextEdit,canRotate:a.canRotate,scaleType:a.scaleType,type:y.Select,selectRect:n,points:a.getChildrenPoints(),willSyncService:e?.willSyncService||!1,opt:e?.willSyncService&&a.getWorkOptions()||void 0,canLock:a.canLock,isLocked:a.isLocked,toolsTypes:a.toolsTypes,shapeOpt:a.shapeOpt,thickness:a.thickness,useStroke:a.useStroke,strokeType:a.strokeType}];this.thread.post({sp:c})}}commandDeleteText(e){const t=this.vNodes.get(e);if(t&&t.toolsType===S.Text)return{type:y.TextUpdate,toolsType:S.Text,workId:e,dataType:x.Local}}async removeSelector(e){const{willSyncService:t}=e,s=[],o=[],r=this.workShapes.get(Y);if(!r)return;const i=r.selectIds&&[...r.selectIds]||[];for(const a of i){if(this.vNodes.get(a)){const l=this.commandDeleteText(a);l&&s.push(l)}this.removeNode(a),o.push(a)}o.length&&s.push({type:y.RemoveNode,removeIds:o}),s.push({type:y.Select,selectIds:[],willSyncService:t}),await this.blurSelector(),s.length&&this.thread.post({sp:s})}removeWork(e){const{workId:t}=e,s=t?.toString();s&&this.removeNode(s)}removeNode(e){var t;this.vNodes.get(e)&&((t=this.thread.fullLayer)==null||t.getElementsByName(e).forEach(s=>{s.remove(),H(s,this.thread.fullLayer.parent)}),this.vNodes.delete(e)),this.workShapes.has(e)&&(this.thread.localLayer.getElementsByName(e).forEach(s=>{s.remove(),H(s,this.thread.localLayer.parent)}),this.clearWorkShapeNodeCache(e))}setFullWork(e){const{workId:t,opt:s,toolsType:o}=e;if(t&&s&&o){const r=t.toString();let i;return t&&this.workShapes.has(r)?(i=this.workShapes.get(r),i?.setWorkOptions(s)):i=this.createWorkShapeNode({toolsOpt:s,toolsType:o,workId:r}),i?(this.workShapes.set(r,i),i):void 0}}async consumeFull(e){var t;const s=this.setFullWork(e),o=e.ops&&fe(e.ops);if(s){const r=(t=e.workId)==null?void 0:t.toString();s.toolsType===S.Image?await s.consumeServiceAsync({isFullWork:!0,replaceId:r,worker:this}):s.toolsType===S.Text?await s.consumeServiceAsync({isFullWork:!0,replaceId:r,boxRect:this.thread.getSceneRect()}):s.consumeService({op:o,isFullWork:!0,replaceId:r}),e!=null&&e.updateNodeOpt&&s.updataOptService(e.updateNodeOpt);const i=[];e.workId&&this.workShapes.delete(e.workId.toString()),e.willSyncService&&i.push({opt:e.opt,toolsType:e.toolsType,type:y.FullWork,workId:e.workId,ops:e.ops,updateNodeOpt:e.updateNodeOpt,viewId:this.thread.viewId}),i.length&&this.thread.post({sp:i})}}async colloctEffectSelectWork(e){const t=this.workShapes.get(Y),{workId:s,msgType:o}=e;if(t&&s&&t.selectIds&&t.selectIds.includes(s.toString())){o===y.RemoveNode?t.selectIds=t.selectIds.filter(r=>r!==s.toString()):this.effectSelectNodeData.add(e),await new Promise(r=>{setTimeout(()=>{r(!0)},0)}),await this.runEffectSelectWork(!0).then(()=>{var r;(r=this.effectSelectNodeData)==null||r.clear()});return}return e}async runEffectSelectWork(e){var t;for(const s of this.effectSelectNodeData.values()){const o=this.setFullWork(s);if(o){const r=(t=s.workId)==null?void 0:t.toString();if(o.toolsType===S.Image)await o.consumeServiceAsync({isFullWork:!0,replaceId:r,worker:this});else if(o.toolsType===S.Text)await o.consumeServiceAsync({isFullWork:!0,replaceId:r,boxRect:this.thread.getSceneRect()});else{const i=s.ops&&fe(s.ops);o.consumeService({op:i,isFullWork:!0,replaceId:r}),s!=null&&s.updateNodeOpt&&o.updataOptService(s.updateNodeOpt)}s.workId&&this.workShapes.delete(s.workId.toString())}}this.reRenderSelector(e)}hasSelector(){return this.workShapes.has(Y)}getSelector(){return this.workShapes.get(Y)}reRenderSelector(e=!1){var t;const s=this.workShapes.get(Y);if(!s)return;if(s&&!((t=s.selectIds)!=null&&t.length))return this.blurSelector();const o=s.reRenderSelector();o&&this.thread.post({sp:[{type:y.Select,selectIds:s.selectIds,selectRect:o,willSyncService:e,viewId:this.thread.viewId,points:s.getChildrenPoints(),textOpt:s.textOpt,selectorColor:s.selectorColor,strokeColor:s.strokeColor,fillColor:s.fillColor,canTextEdit:s.canTextEdit,canRotate:s.canRotate,scaleType:s.scaleType,opt:s.getWorkOptions()||void 0,canLock:s.canLock,isLocked:s.isLocked,toolsTypes:s.toolsTypes,shapeOpt:s.shapeOpt,thickness:s.thickness,useStroke:s.useStroke,strokeType:s.strokeType}]})}async blurSelector(e){var t;const s=this.workShapes.get(Y),o=s?.blurSelector();if(this.clearWorkShapeNodeCache(Y),((t=this.thread.fullLayer)==null?void 0:t.parent).children.forEach(r=>{r.name===Y&&r.remove()}),o){const r=[];r.push({...o,isSync:e?.isSync}),this.thread.post({sp:r})}}clearWorkShapeNodeCache(e){var t;(t=this.getWorkShape(e))==null||t.clearTmpPoints(),this.workShapes.delete(e)}async drawBitMapEraserFull(e,t,s){const{willUpdateNodes:o,willDeleteNodes:r}=t,i=e.getWorkId(),a=[{type:y.RemoveNode,removeIds:[i],viewId:this.thread.viewId}];if(s&&a.push({type:y.None,isLockSentEventCursor:s}),o!=null&&o.size||r!=null&&r.size){if(o!=null&&o.size)for(const[l,n]of o)a.push({type:y.UpdateNode,dataType:x.Local,opt:n.opt,workId:l,updateNodeOpt:{useAnimation:!1}});r!=null&&r.size&&a.push({type:y.RemoveNode,removeIds:[...r],viewId:this.thread.viewId})}a.length&&this.thread.post({sp:a})}drawPencilEraserFull(e,t,s){const{willNewNodes:o,willDeleteNodes:r}=t,i=e.getWorkId(),a=[{type:y.RemoveNode,removeIds:[i],viewId:this.thread.viewId}];if(s&&a.push({type:y.None,isLockSentEventCursor:s}),o!=null&&o.size||r!=null&&r.size){if(o!=null&&o.size)for(const[l,n]of o)a.push({type:y.FullWork,dataType:x.Local,toolsType:n.toolsType,ops:Q(n.op),opt:n.opt,workId:l,updateNodeOpt:{useAnimation:!1}});r!=null&&r.size&&a.push({type:y.RemoveNode,removeIds:[...r],viewId:this.thread.viewId})}a.length&&this.thread.post({sp:a})}drawEraser(e,t){const s=[];e.removeIds&&s.push(e),t&&s.push({type:y.None,isLockSentEventCursor:t}),this.thread.post({sp:s,consumeCount:this.consumeCount})}getWorkShape(e){return this.workShapes.get(e)}getWorkShapes(){return this.workShapes}consumeDraw(e,t){const{op:s,workId:o,scenePath:r,postCount:i,smoothSync:a}=e;if(s!=null&&s.length&&o){const l=o.toString(),n=this.workShapes.get(l);if(!n)return;const c=n.toolsType;if(c===S.LaserPen)return;switch(this.combineDrawActiveId&&this.combineDrawActiveId!==l&&(this.combineTimerId&&(clearTimeout(this.combineTimerId),this.combineTimerId=void 0,this.combineDrawResolve&&this.combineDrawResolve(!1),this.combineDrawActiveId=void 0),this.consumeDrawAll({workId:this.combineDrawActiveId,scenePath:r,viewId:this.thread.viewId,msgType:y.DrawWork,dataType:x.Local},t)),this.drawWorkActiveId&&this.drawWorkActiveId!==l&&(this.consumeDrawAll({workId:this.drawWorkActiveId,scenePath:r,viewId:this.thread.viewId,msgType:y.DrawWork,dataType:x.Local},t),this.drawWorkActiveId=void 0),!this.drawWorkActiveId&&l!==Y&&(this.drawWorkActiveId=l),ae.isNumber(i)&&(this.consumeCount=i),c){case S.Selector:{const h=n.consume({data:e,isFullWork:!0,isMainThread:!0});this.fullWorkerDrawCount++;const p=[];h.type===y.Select&&(h.selectIds&&t.runReverseSelectWork(h.selectIds),p.push(h)),this.thread.post({consumeCount:this.consumeCount,fullWorkerDrawCount:this.fullWorkerDrawCount,sp:p});break}case S.PencilEraser:case S.BitMapEraser:{n.consume({data:e,isFullWork:!1,isMainThread:!0}),this.fullWorkerDrawCount++,this.thread.post({sp:void 0,consumeCount:this.consumeCount,fullWorkerDrawCount:this.fullWorkerDrawCount}),this.combineTimerId||new Promise(h=>{this.combineDrawActiveId=l,this.combineDrawResolve=h,this.combineTimerId=Zt(()=>{this.combineTimerId=void 0,this.combineDrawResolve&&this.combineDrawResolve(!0)},this.thread.master.maxCombineEraserTime,this.thread.master.control.hasPolyfillMethod("requestIdleCallback"))}).then(h=>{h&&this.drawEraserCombine(l),this.combineDrawResolve=void 0});break}case S.Eraser:{const h=n.consume({data:e,isFullWork:!0});this.drawEraser(h)}break;case S.Arrow:case S.Straight:case S.Ellipse:case S.Rectangle:case S.Star:case S.Polygon:case S.SpeechBalloon:case S.Pencil:{const h=n.consume({data:e,isFullWork:!1,isMainThread:!0,smoothSync:a});h&&(this.fullWorkerDrawCount++,this.thread.post({consumeCount:this.consumeCount,fullWorkerDrawCount:this.fullWorkerDrawCount,sp:h.op&&[{...h,scenePath:r}]||void 0}))}break}}}drawEraserCombine(e){var t,s,o,r;const i=(t=this.workShapes.get(e))==null?void 0:t.combineConsume({workerEngine:this});if(i){const{willDeleteNodes:a,willNewNodes:l}=i,n={render:[],sp:[]};if(i!=null&&i.rect){const c=ue(i.rect);(s=n.render)==null||s.push({rect:c,isClear:!0,clearCanvas:j.Bg,viewId:this.thread.viewId},{rect:c,drawCanvas:j.Bg,viewId:this.thread.viewId})}if(a!=null&&a.size&&((o=n.sp)==null||o.push({type:y.RemoveNode,removeIds:[...a],viewId:this.thread.viewId})),l!=null&&l.size)for(const[c,h]of l)(r=n.sp)==null||r.push({type:y.FullWork,dataType:x.Local,toolsType:h.toolsType,ops:Q(h.op),opt:h.opt,workId:c,updateNodeOpt:{useAnimation:!1}});this.thread.post(n)}}consumeDrawAll(e,t){var s,o,r;const{workId:i,scenePath:a,isLockSentEventCursor:l}=e;if(i){this.combineTimerId&&(clearTimeout(this.combineTimerId),this.combineTimerId=void 0,this.combineDrawResolve&&this.combineDrawResolve(!1),this.combineDrawActiveId=void 0);const n=i.toString();this.drawWorkActiveId===n&&(this.drawWorkActiveId=void 0);const c=this.workShapes.get(n);if(!c)return;const h=c.toolsType;if(h===S.LaserPen)return;const p=this.workShapes.get(Ie),d=(s=p?.selectIds)==null?void 0:s[0],u=c.consumeAll({data:e,workerEngine:this});switch(h){case S.Selector:{u.selectIds&&d&&(o=u.selectIds)!=null&&o.includes(d)&&p.cursorBlur();const f=[];l&&f.push({type:y.None,isLockSentEventCursor:l}),u.type===y.Select&&(u.selectIds&&t.runReverseSelectWork(u.selectIds),f.push({...u,scenePath:a})),f.length&&this.thread.post({sp:f}),(r=c.selectIds)!=null&&r.length?c.clearTmpPoints():this.clearWorkShapeNodeCache(n)}break;case S.PencilEraser:this.drawPencilEraserFull(c,u,l),this.fullWorkerDrawCount=0,this.clearWorkShapeNodeCache(n);break;case S.BitMapEraser:this.drawBitMapEraserFull(c,u,l),this.fullWorkerDrawCount=0,this.clearWorkShapeNodeCache(n);break;case S.Eraser:this.drawEraser({...u,scenePath:a},l),c.clearTmpPoints();break;case S.Arrow:case S.Straight:case S.Ellipse:case S.Rectangle:case S.Star:case S.Polygon:case S.SpeechBalloon:case S.Pencil:{const f=[];l&&f.push({type:y.None,isLockSentEventCursor:l}),u&&(f.push(u),this.fullWorkerDrawCount=0,this.thread.post({fullWorkerDrawCount:this.fullWorkerDrawCount,sp:f})),this.clearWorkShapeNodeCache(n);break}}}}getToolsOpt(){return this.tmpOpt}setToolsOpt(e){var t;this.tmpOpt=e,(t=e.toolsOpt)!=null&&t.syncUnitTime&&(this.syncUnitTime=e.toolsOpt.syncUnitTime)}setWorkOptions(e,t){let s=this.workShapes.get(e);if(!s&&this.tmpOpt){const{toolsType:o}=this.tmpOpt;this.tmpOpt.toolsOpt=t,s=this.createWorkShapeNode({workId:e,toolsType:o,toolsOpt:t}),s&&this.workShapes.set(e,s),this.setToolsOpt(this.tmpOpt)}t.syncUnitTime||(t.syncUnitTime=this.syncUnitTime),s?.setWorkOptions(t)}createWorkShapeNode(e){return Se({...e,vNodes:this.vNodes,fullLayer:this.thread.fullLayer,drawLayer:this.thread.localLayer},this.thread.serviceWork)}}class no{constructor(e){m(this,"vNodes"),m(this,"thread"),m(this,"workShapes",new Map),m(this,"selectorWorkShapes",new Map),m(this,"willRunEffectSelectorIds",new Set),m(this,"runEffectId"),m(this,"animationId"),m(this,"syncUnitTime",K.syncOpt.interval),this.vNodes=e.vNodes,this.thread=e.thread}async loadImageBitMap(e){return await this.thread.loadImageBitMap(e)}destroy(){this.clearAll()}clearAll(){this.thread.serviceLayer.children.length&&(this.thread.serviceLayer.parent.children.forEach(e=>{e.name!=="viewport"&&e.remove()}),Z(this.thread.serviceLayer,this.thread.serviceLayer.parent)),this.workShapes.clear(),this.selectorWorkShapes.clear(),this.willRunEffectSelectorIds.clear()}runEffect(){this.runEffectId||(this.runEffectId=setTimeout(this.effectRunSelector.bind(this),0))}effectRunSelector(){this.runEffectId=void 0,this.willRunEffectSelectorIds.forEach(e=>{var t,s;const o=this.selectorWorkShapes.get(e);o&&o.selectIds&&((t=o.node)==null||t.selectServiceNode(e,o,!0)),(s=o?.selectIds)!=null&&s.length||this.selectorWorkShapes.delete(e)}),this.willRunEffectSelectorIds.clear()}runSelectWork(e){this.activeSelectorShape(e);const{workId:t}=e,s=t?.toString();s&&this.willRunEffectSelectorIds.add(s),this.runEffect()}removeWork(e){const{workId:t}=e,s=t?.toString();if(s){if(this.workShapes.get(s)){this.workShapes.delete(s),this.removeNode(s,e);return}this.removeNode(s,e)}}consumeFull(e){this.activeWorkShape(e),this.runAnimation()}runReverseSelectWork(e){e.forEach(t=>{this.selectorWorkShapes.forEach((s,o)=>{var r;if((r=s.selectIds)!=null&&r.length){const i=s.selectIds.indexOf(t);i>-1&&(s.selectIds.splice(i,1),this.willRunEffectSelectorIds.add(o))}})}),this.willRunEffectSelectorIds.size&&this.runEffect()}consumeDraw(e){this.activeWorkShape(e),this.runAnimation()}computNextAnimationIndex(e,t){const s=Math.floor((e.animationWorkData||[]).slice(e.animationIndex).length*32/t/this.syncUnitTime)*t;return Math.min((e.animationIndex||0)+(s||t),(e.animationWorkData||[]).length)}async animationDraw(){var e,t,s,o,r,i,a,l,n,c,h,p,d,u,f,w,k,g,I,T;this.animationId=void 0;let W=!1;const N=new Map;for(const[R,P]of this.workShapes.entries())switch(P.toolsType){case S.Image:{await((e=P.node)==null?void 0:e.consumeServiceAsync({isFullWork:!0,worker:this})),this.selectorWorkShapes.forEach((b,D)=>{var B;(B=b.selectIds)!=null&&B.includes(R)&&(this.willRunEffectSelectorIds.add(D),this.runEffect())}),this.workShapes.delete(R);break}case S.Text:{P.node&&(await((t=P.node)==null?void 0:t.consumeServiceAsync({isFullWork:!0,replaceId:R,boxRect:this.thread.getSceneRect()})),this.selectorWorkShapes.forEach((b,D)=>{var B;(B=b.selectIds)!=null&&B.includes(R)&&(this.willRunEffectSelectorIds.add(D),this.runEffect())}),(s=P.node)==null||s.clearTmpPoints(),this.workShapes.delete(R));break}case S.Arrow:case S.Straight:case S.Rectangle:case S.Ellipse:case S.Star:case S.Polygon:case S.SpeechBalloon:{const b=!!P.ops;if((o=P.animationWorkData)!=null&&o.length){const D=P.oldRect;(r=P.node)==null||r.consumeService({op:P.animationWorkData,isFullWork:b}),b&&(this.selectorWorkShapes.forEach((B,E)=>{var X;(X=B.selectIds)!=null&&X.includes(R)&&(this.willRunEffectSelectorIds.add(E),this.runEffect())}),(i=P.node)==null||i.clearTmpPoints(),this.workShapes.delete(R)),P.isEnableCursor?N.set(R,{workState:D?P.ops?M.Done:M.Doing:M.Start,op:P.animationWorkData.slice(-3,-1)}):b&&!P.useAnimation&&(a=P.updateNodeOpt)!=null&&a.useAnimation&&N.set(R,{workState:M.Done,op:P.animationWorkData.slice(-3,-1),uid:(l=P.updateNodeOpt)==null?void 0:l.uid}),P.animationWorkData.length=0}break}case S.Pencil:{if(P.useAnimation){if(P.useAnimation){if(P.isDel){(f=P.node)==null||f.clearTmpPoints(),this.workShapes.delete(R);break}const b=3,D=this.computNextAnimationIndex(P,b),B=P.isDiff?0:Math.max(0,(P.animationIndex||0)-b),E=(P.animationWorkData||[]).slice(B,D),X=(k=(w=P.node)==null?void 0:w.getWorkId())==null?void 0:k.toString();if((P.animationIndex||0)<D||P.isDiff){if((g=P.node)==null||g.consumeService({op:E,isFullWork:!1}),P.animationIndex=D,P.isDiff&&(P.isDiff=!1),E.length&&P.isEnableCursor){const _=E.slice(-3,-1);N.set(R,{workState:B===0?M.Start:D===((I=P.animationWorkData)==null?void 0:I.length)?M.Done:M.Doing,op:_})}}else P.ops&&((T=P.node)==null||T.consumeService({op:P.animationWorkData||[],isFullWork:!0,replaceId:X}),P.isDel=!0,P.isEnableCursor&&N.set(R,{workState:M.Done,op:E.slice(-3,-1)}));W=!0;break}}else{const b=!!P.ops;if((n=P.node)==null||n.consumeService({op:P.animationWorkData||[],isFullWork:b,replaceId:R}),(c=P.node)==null||c.updataOptService(P.updateNodeOpt),b){if(!P.isEnableCursor&&(h=P.updateNodeOpt)!=null&&h.useAnimation&&(p=P.animationWorkData)!=null&&p.length){const D=P.animationWorkData.slice(-3,-1);N.set(R,{workState:M.Done,op:D,uid:(d=P.updateNodeOpt)==null?void 0:d.uid})}this.selectorWorkShapes.forEach((D,B)=>{var E;(E=D.selectIds)!=null&&E.includes(R)&&(this.willRunEffectSelectorIds.add(B),this.runEffect())}),(u=P.node)==null||u.clearTmpPoints(),this.workShapes.delete(R)}}break}}if(W&&this.runAnimation(),N.size){const R=[];N.forEach((P,b)=>{R.push({type:y.Cursor,uid:P.uid||b.split(ve)[0],op:P.op,workState:P.workState,viewId:this.thread.viewId})}),this.thread.post({sp:R})}}runAnimation(){this.animationId||(this.animationId=requestAnimationFrame(this.animationDraw.bind(this)))}hasDiffData(e,t,s){const o=e.length;if(t.length<o)return!0;switch(s){case S.Pencil:{for(let r=0;r<o;r+=3)if(t[r]!==e[r]||t[r+1]!==e[r+1])return!0;break}case S.LaserPen:{for(let r=0;r<o;r+=2)if(t[r]!==e[r]||t[r+1]!==e[r+1])return!0;break}}return!1}activeWorkShape(e){var t,s,o,r;const{workId:i,opt:a,toolsType:l,type:n,updateNodeOpt:c,ops:h,op:p,useAnimation:d,imageBitmap:u,isEnableCursor:f}=e;if(!i)return;const w=i.toString(),k=(t=this.vNodes.get(w))==null?void 0:t.rect;if(!((s=this.workShapes)!=null&&s.has(w))){let I={toolsType:l,animationWorkData:p||[],animationIndex:0,type:n,updateNodeOpt:c,ops:h,useAnimation:typeof d<"u"?d:typeof c?.useAnimation<"u"?c?.useAnimation:!0,oldRect:k,isDiff:!1,imageBitmap:u,isEnableCursor:f};l&&a&&(I=this.setNodeKey(w,I,l,a)),(o=this.workShapes)==null||o.set(w,I)}const g=(r=this.workShapes)==null?void 0:r.get(w);g.isEnableCursor=f,n&&(g.type=n),h&&(g.animationWorkData=fe(h),g.ops=h),c&&(g.updateNodeOpt=c),p&&(g.isDiff=this.hasDiffData(g.animationWorkData||[],p,g.toolsType),g.animationWorkData=p),g.node&&g.node.getWorkId()!==w&&g.node.setWorkId(w),k&&(g.oldRect=k),l&&a&&(a.syncUnitTime&&(this.syncUnitTime=a.syncUnitTime),g.toolsType!==l&&l&&a&&this.setNodeKey(w,g,l,a),g.node&&g.node.setWorkOptions(a)),u&&(g.imageBitmap=u)}removeNode(e,t){e.indexOf(Y)>-1&&this.removeSelectWork(t),this.thread.fullLayer.getElementsByName(e).forEach(s=>{s.remove(),H(s,this.thread.fullLayer.parent)}),this.thread.serviceLayer.getElementsByName(e).forEach(s=>{s.remove(),H(s,this.thread.serviceLayer.parent)}),this.vNodes.delete(e)}removeSelectWork(e){const{workId:t}=e,s=t?.toString();s&&(this.activeSelectorShape(e),this.willRunEffectSelectorIds.add(s)),this.runEffect()}activeSelectorShape(e){var t,s,o;const{workId:r,opt:i,toolsType:a,type:l,selectIds:n}=e;if(!r)return;const c=r.toString();if(!((t=this.selectorWorkShapes)!=null&&t.has(c))){let p={toolsType:a,selectIds:n,type:l,opt:i};a&&i&&(p=this.setNodeKey(c,p,a,i)),(s=this.selectorWorkShapes)==null||s.set(c,p)}const h=(o=this.selectorWorkShapes)==null?void 0:o.get(c);l&&(h.type=l),h.node&&h.node.getWorkId()!==c&&h.node.setWorkId(c),h.selectIds=n||[]}setNodeKey(e,t,s,o){return t.toolsType=s,t.node=Se({toolsType:s,toolsOpt:o,vNodes:this.vNodes,fullLayer:this.thread.fullLayer,drawLayer:this.thread.serviceLayer,workId:e},this),t}}class oe{constructor(){m(this,"localWork"),m(this,"serviceWork"),m(this,"threadEngine")}registerMainThread(e){return this.threadEngine=e,this.localWork=e.localWork,this.serviceWork=e.serviceWork,this}}class lo extends oe{constructor(){super(...arguments),m(this,"emitEventType",z.CopyNode)}async consume(e){const{msgType:t,dataType:s,emitEventType:o}=e;if(t===y.FullWork&&s===x.Local&&o===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var t;const{workId:s}=e;s&&await((t=this.localWork)==null?void 0:t.consumeFull(e))}}class co extends oe{constructor(){super(...arguments),m(this,"emitEventType",z.SetColorNode)}async consume(e){const{msgType:t,dataType:s,emitEventType:o}=e;if(t===y.UpdateNode&&s===x.Local&&o===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var t;const{workId:s,updateNodeOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a,textUpdateForWoker:l}=e;s===Y&&o&&await((t=this.localWork)==null?void 0:t.updateSelector({updateSelectorOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a,textUpdateForWoker:l,callback:this.updateSelectorCallback}))}updateSelectorCallback(e){const{param:t,postData:s,newServiceStore:o}=e,{willSyncService:r,isSync:i,textUpdateForWoker:a}=t,l=s.sp||[];if(r)for(const[n,c]of o.entries())a&&c.toolsType===S.Text?l.push({...c,workId:n,type:y.TextUpdate,dataType:x.Local,willSyncService:!0}):l.push({...c,workId:n,type:y.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:i});return{sp:l}}}class ho extends oe{constructor(){super(...arguments),m(this,"emitEventType",z.ZIndexNode)}async consume(e){const{msgType:t,dataType:s,emitEventType:o}=e;if(t===y.UpdateNode&&s===x.Local&&o===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var t;const{workId:s,updateNodeOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a}=e;s===Y&&o&&await((t=this.localWork)==null?void 0:t.updateSelector({updateSelectorOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a,callback:this.updateSelectorCallback}))}updateSelectorCallback(e){const{param:t,postData:s,newServiceStore:o}=e,{willSyncService:r,isSync:i}=t,a=s.sp||[];if(r&&a)for(const[l,n]of o.entries())a.push({...n,workId:l,type:y.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:i});return{sp:a}}}class po extends oe{constructor(){super(...arguments),m(this,"emitEventType",z.TranslateNode)}async consume(e){const{msgType:t,dataType:s,emitEventType:o}=e;if(t===y.UpdateNode&&s===x.Local&&o===this.emitEventType)return await this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var t,s;const{workId:o,updateNodeOpt:r,willRefreshSelector:i,willSyncService:a,willSerializeData:l,textUpdateForWoker:n,emitEventType:c,smoothSync:h}=e;o===Y&&r&&(r.workState===M.Done&&r!=null&&r.translate&&(r.translate[0]||r.translate[1])||r.workState!==M.Done?await((t=this.localWork)==null?void 0:t.updateSelector({updateSelectorOpt:r,willRefreshSelector:i,willSyncService:a,willSerializeData:l,isSync:!0,textUpdateForWoker:n,emitEventType:c,callback:this.updateSelectorCallback,smoothSync:h})):r.workState===M.Done&&((s=this.localWork)==null||s.vNodes.deleteLastTarget()))}updateSelectorCallback(e){const{param:t,postData:s,newServiceStore:o,workShapeNode:r,res:i,smoothSync:a}=e,{willSyncService:l,isSync:n,updateSelectorOpt:c,textUpdateForWoker:h}=t,p=c.workState,d=s.sp||[];if(p===M.Start)return{sp:[],render:[]};const u=i?.selectRect;if(l){d.push({type:y.Select,selectIds:r.selectIds,selectRect:u,willSyncService:p===M.Done?!0:a,isSync:!0,points:p===M.Done&&r.getChildrenPoints()||void 0,textOpt:r.textOpt});const f={useAnimation:c.useAnimation||!1};c.uid&&(f.uid=c.uid);for(const[w,k]of o.entries())h&&k.toolsType===S.Text?d.push({...k,workId:w,type:y.TextUpdate,dataType:x.Local,willSyncService:p===M.Done?!0:a,updateNodeOpt:f}):(a||p===M.Done)&&d.push({...k,workId:w,type:y.UpdateNode,updateNodeOpt:f,isSync:n})}return{sp:d}}}class uo extends oe{constructor(){super(...arguments),m(this,"emitEventType",z.DeleteNode)}async consume(){return!1}}class mo extends oe{constructor(){super(...arguments),m(this,"emitEventType",z.ScaleNode)}async consume(e){const{msgType:t,dataType:s,emitEventType:o}=e;if(t===y.UpdateNode&&s===x.Local&&o===this.emitEventType)return await this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var t;const{workId:s,updateNodeOpt:o,willSyncService:r,willSerializeData:i,smoothSync:a}=e;s===Y&&o&&await((t=this.localWork)==null?void 0:t.updateSelector({updateSelectorOpt:o,willSyncService:r,willSerializeData:i,isSync:!0,callback:this.updateSelectorCallback.bind(this),smoothSync:a}))}updateSelectorCallback(e){const{param:t,postData:s,workShapeNode:o,res:r,newServiceStore:i,smoothSync:a}=e,{updateSelectorOpt:l,willSyncService:n}=t,c=l.workState,h=s.sp||[],p=r?.selectRect;if(c===M.Start)return{sp:[],render:[]};if(n){h.push({type:y.Select,selectIds:o.selectIds,selectRect:p,willSyncService:c===M.Done?!0:a,isSync:!0,points:c===M.Done&&o.getChildrenPoints()||void 0,textOpt:o.textOpt});const d={useAnimation:l.useAnimation||!1};l.uid&&(d.uid=l.uid);for(const[u,f]of i.entries())f.toolsType===S.Text?h.push({...f,workId:u,type:y.TextUpdate,dataType:x.Local,willSyncService:c===M.Done?!0:a,updateNodeOpt:d}):(a||c===M.Done)&&h.push({...f,workId:u,type:y.UpdateNode,updateNodeOpt:d,isSync:!0})}return{sp:h}}}class fo extends oe{constructor(){super(...arguments),m(this,"emitEventType",z.RotateNode)}async consume(e){const{msgType:t,dataType:s,emitEventType:o}=e;if(t===y.UpdateNode&&s===x.Local&&o===this.emitEventType)return await this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var t;const{workId:s,updateNodeOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a,emitEventType:l,smoothSync:n}=e;s===Y&&o&&await((t=this.localWork)==null?void 0:t.updateSelector({updateSelectorOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a,emitEventType:l,isSync:!0,callback:this.updateSelectorCallback,smoothSync:n}))}updateSelectorCallback(e){const{param:t,postData:s,workShapeNode:o,res:r,newServiceStore:i,smoothSync:a}=e,{updateSelectorOpt:l,willSyncService:n,willSerializeData:c,isSync:h}=t,p=l.workState,d=s.sp||[],u=r?.selectRect;if(n){c&&p===M.Done&&d.push({type:y.Select,selectIds:o.selectIds,selectRect:u,willSyncService:p===M.Done?!0:a,isSync:h,points:o.getChildrenPoints()});const f={useAnimation:l.useAnimation||!1};if(l.uid&&(f.uid=l.uid),a||p===M.Done)for(const[w,k]of i.entries())d.push({...k,workId:w,type:y.UpdateNode,updateNodeOpt:f,isSync:h})}return{sp:d}}}class yo extends oe{constructor(){super(...arguments),m(this,"emitEventType",z.SetFontStyle)}async consume(e){const{msgType:t,dataType:s,emitEventType:o}=e;if(t===y.UpdateNode&&s===x.Local&&o===this.emitEventType)return await this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var t;const{workId:s,updateNodeOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a,textUpdateForWoker:l}=e;s===Y&&o&&await((t=this.localWork)==null?void 0:t.updateSelector({updateSelectorOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a,textUpdateForWoker:l,callback:this.updateSelectorCallback}))}updateSelectorCallback(e){const{param:t,postData:s,newServiceStore:o,workShapeNode:r,res:i}=e,{willSyncService:a,isSync:l,updateSelectorOpt:n,textUpdateForWoker:c}=t,h=s.sp||[],p=i?.selectRect;if(a&&h){n.fontSize&&h.push({type:y.Select,selectIds:r.selectIds,selectRect:p,willSyncService:a,isSync:l,points:r.getChildrenPoints()});for(const[d,u]of o.entries())c&&u.toolsType===S.Text?h.push({...u,workId:d,type:y.TextUpdate,dataType:x.Local,willSyncService:!0}):h.push({...u,workId:d,type:y.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:l})}return{sp:h}}}class wo extends oe{constructor(){super(...arguments),m(this,"emitEventType",z.SetPoint)}async consume(e){const{msgType:t,dataType:s,emitEventType:o}=e;if(t===y.UpdateNode&&s===x.Local&&o===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var t;const{workId:s,updateNodeOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a,textUpdateForWoker:l}=e;s===Y&&o&&await((t=this.localWork)==null?void 0:t.updateSelector({updateSelectorOpt:o,willRefreshSelector:r,willSyncService:i,emitEventType:this.emitEventType,willSerializeData:a,isSync:!0,textUpdateForWoker:l,callback:this.updateSelectorCallback}))}updateSelectorCallback(e){const{param:t,postData:s,newServiceStore:o,workShapeNode:r,res:i}=e,{willSyncService:a,isSync:l}=t,n=s.sp||[],c=i?.selectRect;if(a&&n){for(const[h,p]of o.entries())n.push({...p,workId:h,type:y.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:l});n.push({type:y.Select,selectIds:r.selectIds,selectRect:c,willSyncService:a,isSync:l,points:r.getChildrenPoints()})}return{sp:n}}}class ko extends oe{constructor(){super(...arguments),m(this,"emitEventType",z.SetLock)}async consume(e){const{msgType:t,dataType:s,emitEventType:o}=e;if(t===y.UpdateNode&&s===x.Local&&o===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var t;const{workId:s,updateNodeOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a}=e;s===Y&&o&&await((t=this.localWork)==null?void 0:t.updateSelector({updateSelectorOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a,callback:this.updateSelectorCallback}))}updateSelectorCallback(e){const{param:t,postData:s,newServiceStore:o,workShapeNode:r,res:i}=e,{willSyncService:a,isSync:l,updateSelectorOpt:n}=t,c=s.sp||[],h=i?.selectRect;if(a&&c){for(const[p,d]of o.entries())c.push({...d,workId:p,type:y.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:l});c.push({isLocked:n.isLocked,selectorColor:r.selectorColor,scaleType:r.scaleType,canRotate:r.canRotate,type:y.Select,selectIds:r.selectIds,selectRect:h,willSyncService:a,isSync:l})}return{sp:c}}}class go extends oe{constructor(){super(...arguments),m(this,"emitEventType",z.SetShapeOpt)}async consume(e){const{msgType:t,dataType:s,emitEventType:o}=e;if(t===y.UpdateNode&&s===x.Local&&o===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var t;const{workId:s,updateNodeOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a}=e;s===Y&&o&&await((t=this.localWork)==null?void 0:t.updateSelector({updateSelectorOpt:o,willRefreshSelector:r,willSyncService:i,willSerializeData:a,callback:this.updateSelectorCallback}))}updateSelectorCallback(e){const{param:t,postData:s,newServiceStore:o}=e,{willSyncService:r,isSync:i}=t,a=s.sp||[];if(r&&a)for(const[l,n]of o.entries())a.push({...n,workId:l,type:y.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:i});return{sp:a}}}class vo{constructor(e){m(this,"builders",new Map),this.builders=new Map(e.map(t=>[t,this.build(t)]))}build(e){switch(e){case z.TranslateNode:return new po;case z.ZIndexNode:return new ho;case z.CopyNode:return new lo;case z.SetColorNode:return new co;case z.DeleteNode:return new uo;case z.ScaleNode:return new mo;case z.RotateNode:return new fo;case z.SetFontStyle:return new yo;case z.SetPoint:return new wo;case z.SetLock:return new ko;case z.SetShapeOpt:return new go}}registerForMainThread(e){return this.builders.forEach(t=>{t&&t.registerMainThread(e)}),this}async consumeForMainThread(e){for(const t of this.builders.values())if(await t?.consume(e))return!0;return!1}}class So{constructor(e,t){m(this,"viewId"),m(this,"fullLayer"),m(this,"topLayer"),m(this,"localLayer"),m(this,"serviceLayer"),m(this,"snapshotFullLayer"),m(this,"vNodes"),m(this,"master"),m(this,"opt"),m(this,"cameraOpt"),m(this,"scene"),m(this,"localWork"),m(this,"serviceWork"),m(this,"topWork"),m(this,"taskUpdateCameraId"),m(this,"debounceUpdateCameraId"),m(this,"debounceUpdateCache",new Set),m(this,"mainThreadPostId"),m(this,"combinePostMsg",new Set),m(this,"methodBuilder"),m(this,"cacheImages",new Map),m(this,"imageResolveMap",new Map),this.viewId=e,this.opt=t,this.scene=this.createScene({...t.canvasOpt,container:t.container}),this.master=t.master;const s=K.bufferSize.full,o=K.bufferSize.sub;this.fullLayer=this.createLayer("fullLayer",this.scene,{...t.layerOpt,bufferSize:this.viewId===ne?s:o*2}),this.topLayer=this.createLayer("topLayer",this.scene,{...t.layerOpt,bufferSize:(this.viewId,ne,o),contextType:"2d"}),this.localLayer=this.createLayer("localLayer",this.scene,{...t.layerOpt,bufferSize:(this.viewId,ne,o),contextType:"2d"}),this.serviceLayer=this.createLayer("serviceLayer",this.scene,{...t.layerOpt,bufferSize:(this.viewId,ne,o),contextType:"2d"}),this.vNodes=new $t(e,this.scene);const r={thread:this,vNodes:this.vNodes};this.localWork=new ao(r),this.serviceWork=new no(r),this.topWork=new io(r),this.vNodes.init(this.fullLayer),this.methodBuilder=new vo([z.CopyNode,z.SetColorNode,z.DeleteNode,z.RotateNode,z.ScaleNode,z.TranslateNode,z.ZIndexNode,z.SetFontStyle,z.SetPoint,z.SetLock,z.SetShapeOpt]).registerForMainThread(this)}getCachedImages(e){var t;return(t=this.cacheImages.get(e))==null?void 0:t.imageBitmap}getCachedImagesByWorkId(e){for(const[t,s]of this.cacheImages.entries())if(t===e&&s.imageBitmap)return s.imageBitmap}deleteCachedImagesByWorkId(e){for(const[t,s]of this.cacheImages.entries())s.workId===e&&(s.imageBitmap.close(),this.cacheImages.delete(t))}clearCacheImages(){this.cacheImages.forEach(e=>e.imageBitmap.close()),this.cacheImages.clear()}clearImageResolveMap(){this.imageResolveMap.forEach(({timer:e})=>{e&&clearTimeout(e)}),this.imageResolveMap.clear()}post(e){this.combinePostMsg.add(e),this.runBatchPostData()}updateDpr(e){this.scene.displayRatio=e}async on(e){if(!await this.methodBuilder.consumeForMainThread(e)){const{msgType:t,toolsType:s,opt:o,dataType:r,workId:i,workState:a,imageSrc:l,imageBitmap:n,workIds:c,isLockSentEventCursor:h}=e,p=i?.toString();switch(t){case y.UpdateDpr:ae.isNumber(e.dpr)&&this.updateDpr(e.dpr);break;case y.AuthClear:{const{clearUids:d,localUid:u}=e;this.vNodes.setCanClearUids(d),this.vNodes.setLocalUid(u);break}case y.Destroy:this.destroy();break;case y.Clear:this.clearAll();break;case y.UpdateCamera:await this.updateCamera(e);break;case y.UpdateTools:if(s&&o){const d={toolsType:s,toolsOpt:o};this.topWork.canUseTopLayer(s)?this.topWork.setToolsOpt(d):this.localWork.setToolsOpt(d)}break;case y.CreateWork:if(p&&o&&s){if(this.topWork.canUseTopLayer(s)){this.topWork.getToolsOpt()||this.topWork.setToolsOpt({toolsType:s,toolsOpt:o}),this.topWork.setWorkOptions(p,o);break}this.localWork.getToolsOpt()||this.localWork.setToolsOpt({toolsType:s,toolsOpt:o}),this.localWork.setWorkOptions(p,o)}break;case y.DrawWork:a===M.Done&&r===x.Local?(this.consumeDrawAll(r,e),s===S.LaserPen&&h&&this.post({sp:[{type:y.None,isLockSentEventCursor:h}]})):this.consumeDraw(r,e);break;case y.UpdateNode:case y.FullWork:if(s&&this.topWork.canUseTopLayer(s)){this.consumeDrawAll(r,e);break}this.consumeFull(r,e);break;case y.RemoveNode:await this.removeNode(e);return;case y.Select:r===x.Service&&(i===Y?this.localWork.updateFullSelectWork(e):this.serviceWork.runSelectWork(e));break;case y.CursorBlur:this.localWork.cursorBlur();return;case y.CursorHover:this.localWork.cursorHover(e);break;case y.GetTextActive:r===x.Local&&this.localWork.checkTextActive(e);break;case y.GetImageBitMap:if(l&&n&&i){const d=i.toString();this.deleteCachedImagesByWorkId(d),this.cacheImages.set(l,{imageBitmap:n,workId:d});const u=this.imageResolveMap.get(l);if(u){const{resolve:f,timer:w}=u;w&&clearTimeout(w),f&&f(l)}}break;case y.GetVNodeInfo:if(i&&c){const d=c.map(u=>this.vNodes.get(u));this.post({sp:[{type:y.GetVNodeInfo,dataType:x.Local,workId:i,vInfo:d}]})}break}}}getIconSize(e,t,s){const o=e*s,r=t*s;return o<=50||r<=50?[50,50]:o<=100||r<=100?[100,100]:o<=200||r<=200?[200,200]:o<=400||r<=400?[400,400]:o<=800||r<=800?[800,800]:[1600,1600]}async loadImageBitMap(e){const{toolsType:t,opt:s,workId:o}=e;if(t===S.Image&&s&&o){const r=o.toString(),{src:i,type:a,width:l,height:n,strokeColor:c}=s;if(!i||!a||!l||!n)return;let h=i;if(a===Be.Iconify){const[d,u]=this.getIconSize(l,n,this.opt.displayer.dpr);h=`${i}?width=${d}&height=${u}&color=${c}`}if(this.cacheImages.has(h)){const d=this.getCachedImages(h);if(d)return d}if(this.imageResolveMap.has(h)){const d=this.getCachedImagesByWorkId(r);if(d)return d}const p=await new Promise(d=>{const u=this.imageResolveMap.get(h)||{resolve:void 0,timer:void 0};u.timer&&clearTimeout(u.timer),u.resolve=d,u.timer=setTimeout(()=>{const f=this.imageResolveMap.get(h);f!=null&&f.resolve&&f.resolve(h)},5e3),this.imageResolveMap.set(h,u),this.opt.post({sp:[{imageSrc:h,workId:r,viewId:this.viewId,isgl:!!this.fullLayer.parent.gl,isSubWorker:!1,type:y.GetImageBitMap}]})});return this.imageResolveMap.delete(p),this.getCachedImages(h)}}async removeNode(e){const{dataType:t,workId:s,removeIds:o}=e,r=o||[];if(s&&r.push(s.toString()),r.length)for(const i of r){if(i===Y){await this.localWork.removeSelector(e);continue}t===x.Local?this.localWork.removeWork(e):t===x.Service&&this.serviceWork.removeWork(e),await this.localWork.colloctEffectSelectWork(e)}}async consumeFull(e,t){const s=await this.localWork.colloctEffectSelectWork(t);s&&e===x.Local&&await this.localWork.consumeFull(s),s&&e===x.Service&&this.serviceWork.consumeFull(s)}setCameraOpt(e){this.cameraOpt=e;const{scale:t,centerX:s,centerY:o,width:r,height:i}=e;(r!==this.scene.width||i!==this.scene.height)&&this.updateScene({width:r,height:i}),this.fullLayer.setAttribute("scale",[t,t]),this.fullLayer.setAttribute("translate",[-s,-o]),this.topLayer.setAttribute("scale",[t,t]),this.topLayer.setAttribute("translate",[-s,-o]),this.localLayer.setAttribute("scale",[t,t]),this.localLayer.setAttribute("translate",[-s,-o]),this.serviceLayer.setAttribute("scale",[t,t]),this.serviceLayer.setAttribute("translate",[-s,-o])}runBatchPostData(){this.mainThreadPostId||(this.mainThreadPostId=requestAnimationFrame(this.combinePost.bind(this)))}combinePostData(){var e;this.mainThreadPostId=void 0;const t=[];let s,o;for(const r of this.combinePostMsg.values()){if((e=r.sp)!=null&&e.length)for(const i of r.sp){let a=!1;for(const l of t)if(ae.isEqual(i,l)){a=!0;break}a||t.push(i)}ae.isNumber(r.fullWorkerDrawCount)&&(s=r.fullWorkerDrawCount),ae.isNumber(r.consumeCount)&&(o=r.consumeCount)}return this.combinePostMsg.clear(),{sp:t,fullWorkerDrawCount:s,consumeCount:o}}combinePost(){var e,t;const s=this.combinePostData(),o=(e=s.sp)==null?void 0:e.filter(r=>r.type!==y.None||r.isLockSentEventCursor);o!=null&&o.length?s.sp=o.map(r=>r.viewId?r:{...r,viewId:this.viewId}):delete s.sp,s.consumeCount===void 0&&delete s.consumeCount,s.fullWorkerDrawCount===void 0&&delete s.fullWorkerDrawCount,(s!=null&&s.consumeCount||s!=null&&s.fullWorkerDrawCount||(t=s.sp)!=null&&t.length)&&this.opt.post(s)}clearAll(){this.fullLayer.children.length&&(this.fullLayer.parent.children.forEach(e=>{e.name!=="viewport"&&e.remove()}),Z(this.fullLayer,this.fullLayer.parent)),this.clearCacheImages(),this.clearImageResolveMap(),this.localWork.clearAll(),this.topWork.clearAll(),this.serviceWork.clearAll(),this.vNodes.clear(),this.post({sp:[{type:y.Clear}]})}consumeDrawAll(e,t){const{toolsType:s,workId:o}=t;if(o){const r=o.toString();if(s&&this.topWork.canUseTopLayer(s)){e===x.Local&&(this.topWork.getLocalWorkShape(o.toString())||this.topWork.createLocalWork(t)),this.topWork.consumeDrawAll(t);return}e===x.Local&&(this.localWork.getWorkShape(r)||this.localWork.createLocalWork(t),this.localWork.consumeDrawAll(t,this.serviceWork))}}consumeDraw(e,t){const{opt:s,workId:o,toolsType:r}=t;if(o&&r&&s){const i=o.toString();if(this.topWork.canUseTopLayer(r)){e===x.Local&&(this.topWork.getLocalWorkShape(i)||this.topWork.createLocalWork(t)),this.topWork.consumeDraw(t);return}e===x.Local?(this.localWork.getWorkShape(i)||this.localWork.createLocalWork(t),this.localWork.consumeDraw(t,this.serviceWork)):e===x.Service&&this.serviceWork.consumeDraw(t);return}}async updateCamera(e){var t;const{cameraOpt:s,scenePath:o}=e;if(s&&!ae.isEqual(this.cameraOpt,s)){if(this.taskUpdateCameraId&&(clearTimeout(this.taskUpdateCameraId),this.taskUpdateCameraId=void 0),o){let n=!1;for(const[c,h]of this.localWork.getWorkShapes().entries())switch(h.toolsType){case S.Text:case S.BitMapEraser:case S.PencilEraser:case S.Eraser:case S.Selector:case S.LaserPen:break;default:c!==Ie&&c!==Y&&(n=!0);break}if(n){this.taskUpdateCameraId=setTimeout(()=>{this.taskUpdateCameraId=void 0,this.updateCamera(e)},st);return}}const r=new Map;for(const[n,c]of this.vNodes.getNodesByType(S.Text).entries()){const h=c.rect;r.set(n,ae.cloneDeep(h))}const i=new Set(r.keys());let a=!1;if(this.localWork.hasSelector()){const n=(t=this.localWork.getSelector())==null?void 0:t.selectIds;if(n){a=!0;for(const c of n)i.add(c)}}let l=!1;if(this.serviceWork.selectorWorkShapes.size)for(const n of this.serviceWork.selectorWorkShapes.values()){const c=n.selectIds;if(c){l=!0;for(const h of c)i.add(h)}}if(this.setCameraOpt(s),this.vNodes.curNodeMap.size){this.vNodes.clearTarget(),this.vNodes.updateHighLevelNodesRect(i),this.debounceUpdateCameraId&&clearTimeout(this.debounceUpdateCameraId);for(const[n,c]of r.entries()){const h=this.vNodes.get(n);if(h){const p=c,d=h.rect,u=this.getSceneRect(),f=Ce(p,u),w=Ce(d,u);let k=!1;if((f!==w||p.w!==d.w||p.h!==d.h||w===ze.intersect)&&(k=!0),k){const{toolsType:g}=h;g===S.Text&&this.debounceUpdateCache.add(n)}}}if(a&&this.localWork.reRenderSelector(),l)for(const[n,c]of this.serviceWork.selectorWorkShapes.entries())this.serviceWork.runSelectWork({workId:n,selectIds:c.selectIds,msgType:y.Select,dataType:x.Service,viewId:this.viewId});this.debounceUpdateCameraId=setTimeout(()=>{var n;this.debounceUpdateCameraId=void 0;const c=[];for(const h of this.debounceUpdateCache.values()){if((n=this.fullLayer)!=null&&n.getElementsByName(h)[0]){const p=this.vNodes.get(h);if(p){const{toolsType:d,opt:u}=p,f=this.localWork.setFullWork({toolsType:d,opt:u,workId:h});if(f){const w=this.getSceneRect();c.push(f.consumeServiceAsync({isFullWork:!0,replaceId:h,boxRect:w}))}}}this.debounceUpdateCache.delete(h)}this.vNodes.updateLowLevelNodesRect(),this.vNodes.clearHighLevelIds()},st)}}}getSceneRect(){const{width:e,height:t}=this.scene;return{x:0,y:0,w:Math.floor(e),h:Math.floor(t)}}createScene(e){return new Ge({displayRatio:this.opt.displayer.dpr,depth:!1,desynchronized:!0,...e,autoRender:!0,id:this.viewId,contextType:"2d"})}createLayer(e,t,s){const{width:o,height:r}=s,i=`canvas-${e}`,a=t.layer(i,{...s,offscreen:!1}),l=new V({anchor:[.5,.5],pos:[o*.5,r*.5],size:[o,r],name:"viewport",id:e});return a.append(l),l}updateScene(e){this.scene.attr({...e});const{width:t,height:s}=e;this.scene.width=t,this.scene.height=s,this.updateLayer({width:t,height:s})}updateLayer(e){const{width:t,height:s}=e;this.fullLayer.parent.setAttribute("width",t),this.fullLayer.parent.setAttribute("height",s),this.fullLayer.setAttribute("size",[t,s]),this.fullLayer.setAttribute("pos",[t*.5,s*.5]),this.topLayer.parent.setAttribute("width",t),this.topLayer.parent.setAttribute("height",s),this.topLayer.setAttribute("size",[t,s]),this.topLayer.setAttribute("pos",[t*.5,s*.5]),this.localLayer.parent.setAttribute("width",t),this.localLayer.parent.setAttribute("height",s),this.localLayer.setAttribute("size",[t,s]),this.localLayer.setAttribute("pos",[t*.5,s*.5]),this.serviceLayer.parent.setAttribute("width",t),this.serviceLayer.parent.setAttribute("height",s),this.serviceLayer.setAttribute("size",[t,s]),this.serviceLayer.setAttribute("pos",[t*.5,s*.5])}destroy(){this.clearCacheImages(),this.clearImageResolveMap(),this.vNodes.clear(),this.fullLayer.remove(),H(this.fullLayer,this.fullLayer.parent),this.topLayer.remove(),H(this.topLayer,this.topLayer.parent),this.localLayer.remove(),H(this.localLayer,this.localLayer.parent),this.serviceLayer.remove(),H(this.serviceLayer,this.serviceLayer.parent),this.scene.remove(),this.localWork.destroy(),this.serviceWork.destroy(),this.topWork.destroy()}}class To{constructor(e,t){m(this,"viewId"),m(this,"fullLayer"),m(this,"master"),m(this,"opt"),m(this,"scene"),m(this,"mainThreadPostId"),m(this,"combinePostMsg",new Set),m(this,"workShapes",new Map),m(this,"cacheImages",new Map),m(this,"imageResolveMap",new Map),this.viewId=e,this.opt=t,this.scene=this.createScene({...t.canvasOpt,container:t.container}),this.master=t.master,this.fullLayer=this.createLayer("fullLayer",this.scene,{...t.layerOpt,bufferSize:this.viewId===ne?6e3:3e3,contextType:"2d"})}getCachedImages(e){var t;return(t=this.cacheImages.get(e))==null?void 0:t.imageBitmap}getCachedImagesByWorkId(e){for(const[t,s]of this.cacheImages.entries())if(t===e&&s.imageBitmap)return s.imageBitmap}deleteCachedImagesByWorkId(e){for(const[t,s]of this.cacheImages.entries())s.workId===e&&(s.imageBitmap.close(),this.cacheImages.delete(t))}clearCacheImages(){this.cacheImages.forEach(e=>e.imageBitmap.close()),this.cacheImages.clear()}clearImageResolveMap(){this.imageResolveMap.forEach(({timer:e})=>{e&&clearTimeout(e)}),this.imageResolveMap.clear()}post(e){this.combinePostMsg.add(e),this.runBatchPostData()}async on(e){const{msgType:t,imageSrc:s,imageBitmap:o,workId:r}=e;switch(t){case y.Snapshot:await this.getSnapshot(e),this.destroy();return;case y.BoundingBox:await this.getBoundingRect(e),this.destroy();return;case y.GetImageBitMap:if(s&&o&&r){const i=r.toString();this.deleteCachedImagesByWorkId(i),this.cacheImages.set(s,{imageBitmap:o,workId:i});const a=this.imageResolveMap.get(s);if(a){const{resolve:l,timer:n}=a;n&&clearTimeout(n),l&&l(s)}}break}}getIconSize(e,t,s){const o=e*s,r=t*s;return o<=50||r<=50?[50,50]:o<=100||r<=100?[100,100]:o<=200||r<=200?[200,200]:o<=400||r<=400?[400,400]:o<=800||r<=800?[800,800]:[1600,1600]}async loadImageBitMap(e){const{toolsType:t,opt:s,workId:o}=e;if(t===S.Image&&s&&o){const r=o.toString(),{src:i,type:a,width:l,height:n,strokeColor:c}=s;if(!i||!a||!l||!n)return;let h=i;if(a===Be.Iconify){const[d,u]=this.getIconSize(l,n,this.opt.displayer.dpr);h=`${i}?width=${d}&height=${u}&color=${c}`}if(this.cacheImages.has(h)){const d=this.getCachedImages(h);if(d)return d}if(this.imageResolveMap.has(h)){const d=this.getCachedImagesByWorkId(r);if(d)return d}const p=await new Promise(d=>{const u=this.imageResolveMap.get(h)||{resolve:void 0,timer:void 0};u.timer&&clearTimeout(u.timer),u.resolve=d,u.timer=setTimeout(()=>{const f=this.imageResolveMap.get(h);f!=null&&f.resolve&&f.resolve(h)},5e3),this.imageResolveMap.set(h,u),this.opt.post({sp:[{imageSrc:h,workId:r,viewId:this.viewId,isgl:!!this.fullLayer.parent.gl,isSubWorker:!0,type:y.GetImageBitMap}]})});return this.imageResolveMap.delete(p),this.getCachedImages(h)}}createWorkShapeNode(e){return Se({...e,fullLayer:this.fullLayer,drawLayer:void 0})}setFullWork(e){const{workId:t,opt:s,toolsType:o}=e;if(t&&s&&o){const r=t.toString();let i;return t&&this.workShapes.has(r)?(i=this.workShapes.get(r),i?.setWorkOptions(s)):i=this.createWorkShapeNode({toolsOpt:s,toolsType:o,workId:r}),i?(this.workShapes.set(r,i),i):void 0}}async runFullWork(e){var t;const s=this.setFullWork(e),o=e.ops&&fe(e.ops);if(s){let r,i;const a=(t=s.getWorkId())==null?void 0:t.toString();return s.toolsType===S.Image?r=await s.consumeServiceAsync({isFullWork:!0,worker:this}):s.toolsType===S.Text?r=await s.consumeServiceAsync({isFullWork:!0,replaceId:a,isDrawLabel:!0}):(r=s.consumeService({op:o,isFullWork:!0,replaceId:a}),i=e?.updateNodeOpt&&s.updataOptService(e.updateNodeOpt)),A(r,i)}}async getSnapshot(e){const{scenePath:t,scenes:s,cameraOpt:o,w:r,h:i}=e;if(t&&s&&o){this.setCameraOpt(o);let a;for(const[n,c]of Object.entries(s))if(c!=null&&c.type)switch(c?.type){case y.UpdateNode:case y.FullWork:{const{opt:h}=c,p={...c,opt:h,workId:n,msgType:y.FullWork,dataType:x.Service,viewId:this.viewId},d=await this.runFullWork(p);a=A(a,d);break}}let l;r&&i&&(l={resizeWidth:r,resizeHeight:i}),await this.getSnapshotRender({scenePath:t,options:l})}}getSceneRect(){const{width:e,height:t}=this.scene;return{x:0,y:0,w:Math.floor(e),h:Math.floor(t)}}getRectImageBitmap(e,t){const s=Math.floor(e.x*this.opt.displayer.dpr),o=Math.floor(e.y*this.opt.displayer.dpr),r=e.w>0&&Math.floor(e.w*this.opt.displayer.dpr||1)||1,i=e.h>0&&Math.floor(e.h*this.opt.displayer.dpr||1)||1;return createImageBitmap(this.fullLayer.parent.canvas,s,o,r,i,t)}async getSnapshotRender(e){var t;const{scenePath:s,options:o}=e;((t=this.fullLayer)==null?void 0:t.parent).render();const r=await this.getRectImageBitmap(this.getSceneRect(),o);r&&(this.post({sp:[{type:y.Snapshot,scenePath:s,imageBitmap:r,viewId:this.viewId}]}),this.fullLayer&&Z(this.fullLayer,this.fullLayer.parent))}async getBoundingRect(e){const{scenePath:t,scenes:s,cameraOpt:o}=e;if(t&&s&&o){this.setCameraOpt(o);let r;for(const[i,a]of Object.entries(s))if(a!=null&&a.type)switch(a?.type){case y.UpdateNode:case y.FullWork:{const l=await this.runFullWork({...a,workId:i,msgType:y.FullWork,dataType:x.Service,viewId:this.viewId});r=A(r,l);break}}r&&this.post({sp:[{type:y.BoundingBox,scenePath:t,rect:r}]})}}setCameraOpt(e){const{scale:t,centerX:s,centerY:o,width:r,height:i}=e;this.updateScene({width:r,height:i}),this.fullLayer.setAttribute("scale",[t,t]),this.fullLayer.setAttribute("translate",[-s,-o])}runBatchPostData(){this.mainThreadPostId||(this.mainThreadPostId=requestAnimationFrame(this.combinePost.bind(this)))}combinePostData(){var e;this.mainThreadPostId=void 0;const t=[];for(const s of this.combinePostMsg.values())if((e=s.sp)!=null&&e.length)for(const o of s.sp){let r=!1;for(const i of t)if(le(o,i)){r=!0;break}r||t.push(o)}return this.combinePostMsg.clear(),{sp:t}}combinePost(){var e,t;const s=this.combinePostData(),o=(e=s.sp)==null?void 0:e.filter(r=>r.type!==y.None||r.isLockSentEventCursor);o!=null&&o.length?s.sp=o.map(r=>r.viewId?r:{...r,viewId:this.viewId}):delete s.sp,(t=s.sp)!=null&&t.length&&this.opt.post(s)}createScene(e){return new Ge({displayRatio:this.opt.displayer.dpr,depth:!1,desynchronized:!0,...e,autoRender:!1,contextType:"2d"})}createLayer(e,t,s){const{width:o,height:r}=s,i=`canvas-${e}`,a=t.layer(i,s),l=new V({anchor:[.5,.5],pos:[o*.5,r*.5],size:[o,r],name:"viewport",id:e});return a.append(l),l}updateScene(e){this.scene.attr({...e});const{width:t,height:s}=e;this.scene.width=t,this.scene.height=s,this.updateLayer({width:t,height:s})}updateLayer(e){const{width:t,height:s}=e;this.fullLayer.parent.setAttribute("width",t),this.fullLayer.parent.setAttribute("height",s),this.fullLayer.setAttribute("size",[t,s]),this.fullLayer.setAttribute("pos",[t*.5,s*.5])}destroy(){this.clearCacheImages(),this.clearImageResolveMap(),this.fullLayer.remove(),H(this.fullLayer,this.fullLayer.parent),this.scene.remove()}}class Lo{constructor(e){m(this,"mainThreadMap",new Map),m(this,"snapshotThread"),m(this,"master"),m(this,"post",t=>{const{fullWorkerDrawCount:s,sp:o,workerTasksqueueCount:r,consumeCount:i}=t;this.master.isBusy&&q(r)&&this.master.setWorkerTasksqueueCount(r),q(s)&&this.master.setMaxDrawCount(s),q(i)&&this.master.setConsumeCount(i),o&&this.master.collectorSyncData(o)}),this.master=e}destroy(){this.mainThreadMap.clear()}createMainThread(e,t){return new So(e,t)}createSnapshotThread(e,t){return new To(e,t)}async consume(e){var t,s,o,r;for(const i of e.values()){const{msgType:a,viewId:l,tasksqueue:n,mainTasksqueueCount:c,layerOpt:h,offscreenCanvasOpt:p,cameraOpt:d,isSubWorker:u}=i;if(a===y.Console){console.log(this);continue}if(a===y.Init){const w=(t=this.master.control.viewContainerManager.getView(l))==null?void 0:t.displayer,k=w?.canvasContainerRef.current;if(w&&k&&h&&p){const g=this.createMainThread(l,{displayer:w,container:k,layerOpt:h,master:this.master,canvasOpt:p,post:this.post});this.mainThreadMap.set(l,g),g&&d&&g.setCameraOpt(d)}continue}if((a===y.Snapshot||a===y.BoundingBox)&&l===((s=this.master.control.viewContainerManager.mainView)==null?void 0:s.id)){const w=(o=this.master.control.viewContainerManager.getView(l))==null?void 0:o.displayer,k=(r=w.snapshotContainerRef)==null?void 0:r.current;if(w&&k&&d){k.style.width=`${d.width}px`,k.style.height=`${d.height}px`;const g={...tt.defaultLayerOpt,offscreen:!1,width:d.width,height:d.height},I={...tt.defaultScreenCanvasOpt,width:d.width,height:d.height};this.snapshotThread=this.createSnapshotThread(l,{displayer:w,container:k,layerOpt:g,master:this.master,canvasOpt:I,post:this.post.bind(this)}),this.snapshotThread.on(i).then(()=>{this.snapshotThread=void 0,k.innerHTML="",k.style.width="",k.style.height=""});continue}}if(a===y.GetImageBitMap&&u&&this.snapshotThread){this.snapshotThread.on(i);continue}if(a===y.TasksQueue&&n!=null&&n.size){for(const[w,k]of this.mainThreadMap.entries()){const g=n.get(w);g&&(await k.on(g),c&&this.post({workerTasksqueueCount:c}))}continue}if(l===Ht){for(const w of this.mainThreadMap.values())w.on(i),a===y.Destroy&&this.mainThreadMap.delete(l);continue}const f=this.mainThreadMap.get(l);f&&(f.on(i),a===y.Destroy&&this.mainThreadMap.delete(l))}}}export{Lo as MainThreadManagerImpl};