@netless/appliance-plugin 1.1.31 → 1.1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cdn/cdn.js +1 -1
- package/cdn/{fullWorker-8cQCRH.js → fullWorker-BuCr8Z.js} +1 -1
- package/cdn/{subWorker-Bj1Oy7.js → subWorker-00SXWn.js} +1 -1
- package/dist/appliance-plugin.js +1 -1
- package/dist/appliance-plugin.mjs +1 -1
- package/dist/fullWorker.js +1 -1
- package/dist/{index-MwM-TUUF.mjs → index-B9IvzEGP.mjs} +1 -1
- package/dist/{index-DiHYEAGm.mjs → index-BA9yB27Q.mjs} +351 -352
- package/dist/{index-OfIZzgAi.js → index-CcTOfya8.js} +1 -1
- package/dist/{index-CpRTvTdJ.mjs → index-DFQ4hkMe.mjs} +1 -1
- package/dist/{index-XJ7wx0mr.js → index-DfV03MPS.js} +1 -1
- package/dist/index-DpQJ_LNL.js +1 -0
- package/dist/subWorker.js +1 -1
- package/package.json +1 -1
- package/dist/index-D-zGeAx1.js +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var Je=Object.defineProperty;var et=(k,r,e)=>r in k?Je(k,r,{enumerable:!0,configurable:!0,writable:!0,value:e}):k[r]=e;var g=(k,r,e)=>et(k,typeof r!="symbol"?r+"":r,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./index-D-zGeAx1.js"),D=require("spritejs"),_=require("clipper-lib"),Se=require("lineclip");require("lz-string");require("xss");const $=require("lodash"),Me=require("./ObserverMap-DTz9zucn.js");require("white-web-sdk");require("react-dom");require("react");var tt=s._baseGetTag,st=s.isObjectLike_1,ot="[object Number]";function it(k){return typeof k=="number"||st(k)&&tt(k)==ot}var rt=it;const A=s.getDefaultExportFromCjs(rt);function Q(k,r=!0){const e=k.length;if(e<2)return"";let t=k[0],o=k[1];if(e===2)return`M${s.precise(t)}L${s.precise(o)}`;let i="";for(let n=2,a=e-1;n<a;n++)t=k[n],o=k[n+1],i+=s.average(t,o);return r?`M${s.average(k[0],k[1])}Q${s.precise(k[1])}${s.average(k[1],k[2])}T${i}${s.average(k[e-1],k[0])}${s.average(k[0],k[1])}Z`:`M${s.precise(k[0])}Q${s.precise(k[1])}${s.average(k[1],k[2])}${k.length>3?"T":""}${i}L${s.precise(k[e-1])}`}const K=class K{constructor(r){g(this,"maxImageWidth",s.DefaultAppliancePluginExtrasOptions.pencilEraser.maxImageWidth);g(this,"maxImageHeight",s.DefaultAppliancePluginExtrasOptions.pencilEraser.maxImageHeight);g(this,"syncUnitTime",s.DefaultAppliancePluginExtrasOptions.syncOpt.interval);g(this,"vNodes");g(this,"drawLayer");g(this,"fullLayer");g(this,"workId");g(this,"isDelete",!1);const{vNodes:e,fullLayer:t,drawLayer:o,workId:i,toolsOpt:n}=r;this.vNodes=e,this.fullLayer=t,this.drawLayer=o,this.workId=i,this.syncUnitTime=n.syncUnitTime||this.syncUnitTime}get baseConsumeResult(){return{workId:this.workId,toolsType:this.toolsType,opt:this.workOptions}}filterSamePoints(r,e=.01){return r.reduce((t,o)=>{const i=t[t.length-1];return(o&&!i||o&&i&&!o.isNear(i,e))&&t.push(o),t},[])}setWorkId(r){this.workId=r}getWorkId(){return this.workId}getWorkOptions(){return this.workOptions}setWorkOptions(r){var o,i,n;this.workOptions=r,this.syncUnitTime=r.syncUnitTime||this.syncUnitTime;const e=(o=this.workId)==null?void 0:o.toString(),t=e&&((i=this.vNodes)==null?void 0:i.get(e))||void 0;e&&t&&(t.opt=r,(n=this.vNodes)==null||n.setInfo(e,t))}updataOptService(r){var o,i;let e;const t=(o=this.workId)==null?void 0:o.toString();if(t&&r){const n=this.fullLayer.getElementsByName(t)||this.drawLayer&&this.drawLayer.getElementsByName(t)||[];if(n.length!==1)return;const a=n[0],{pos:c,zIndex:l,scale:h,angle:p,translate:d}=r,u={};A(l)&&(u.zIndex=l),c&&(u.pos=[c[0],c[1]]),h&&(u.scale=h),p&&(u.rotate=p),d&&(u.translate=d),a.attr(u);const f=a==null?void 0:a.getBoundingClientRect();return f&&(e=s.computRect(e,{x:Math.floor(f.x-K.SafeBorderPadding),y:Math.floor(f.y-K.SafeBorderPadding),w:Math.floor(f.width+K.SafeBorderPadding*2),h:Math.floor(f.height+K.SafeBorderPadding*2)})),(i=this.vNodes)==null||i.setInfo(t,{rect:e,centerPos:c}),e}}drawEraserlines(r,e){const{group:t,eraserlines:o,pos:i,layer:n}=r,a=n.parent;if(e)try{const l=(n.renderer.glRenderer||n.renderer.canvasRenderer).options.displayRatio,h=t.getBoundingClientRect(),p=h.width*l*n.worldScaling[0],d=h.height*l*n.worldScaling[1];let u=1;(p>this.maxImageWidth||d>this.maxImageHeight)&&(u=Math.min(this.maxImageWidth/p,this.maxImageHeight/d));let f=n.getAttribute("scale");f=[f[0]*u,f[1]*u];const y=n.parent.parent,m=n.getAttribute("translate"),w=this.createVmRenderNode(`${this.workId}_bitMapLayer`,y,{offscreen:a.offscreen,width:a.width,height:a.height,contextType:"2d",autoRender:!1,bufferSize:500},f,m),P=t.cloneNode(!0);if(!o||!o.length)return;this.addEraserlines(P,o,i,!1),w.appendChild(P);const T=w.parent;T.render();const S=w.renderer.canvasRenderer,E=S.context;if(!E)return;const I=S.options.displayRatio,R=P.getBoundingClientRect(),M={x:R.x,y:R.y,w:R.width,h:R.height},v={x:0,y:0,w:y.width,h:y.height};if(s.getRectMatrixrRelation(M,v)===s.EMatrixrRelationType.outside){U(P,T),y.removeChild(T),this.isDelete=!0;return}const C=s.cloneDeep(M);C.x=Math.floor(Math.max(M.x,0)),C.y=Math.floor(Math.max(M.y,0)),C.w=Math.min(M.x+M.w,y.width)-C.x,C.h=Math.min(M.y+M.h,y.height)-C.y;const b=this.getGroupRect(C,I),N=b.x,x=b.y,O=b.w,F=b.h;let z=E.getImageData(N,x,O,F);if(this.isTransparentRectByCanvas(z)){U(P,T),y.removeChild(T),this.isDelete=!0,z=null;return}U(t,a);const J=this.createSpriteNode({imageData:z,safariRect:C,originRect:M,worldScaling:w.worldScaling});J&&t.append(J),U(P,T),y.removeChild(T),a.deleteTexture(z),z=null}catch(c){console.error("[BaseShapeTool] drawEraserlines error:",c);return}}createVmRenderNode(r,e,t,o,i){const{width:n,height:a}=t,c=`bitMap-${r}`,l=e.layer(c,t),h=new D.Group({anchor:[.5,.5],pos:[n*.5,a*.5],size:[n,a],name:"viewport",id:r});return h.setAttribute("scale",o),h.setAttribute("translate",i),l.append(h),h}getGroupRect(r,e){const t=Math.floor(r.x*e),o=Math.floor(r.y*e),i=Math.floor(r.w*e),n=Math.floor(r.h*e);return{x:t,y:o,w:i,h:n}}addEraserlines(r,e,t,o=!0){for(const i of e){const{thickness:n,op:a}=i;for(const c of a){const l=c.map((f,y)=>y%2?f-t[1]:f-t[0]),h=this.computEraserPoints(l,n),p=Q(h,!0),d={pos:[0,0],d:p,fillColor:"rgba(0,0,0,1)"},u=new D.Path(d);u.addEventListener("beforerender",({detail:f})=>{const y=f.context;o?y.blendFuncSeparate(y.ZERO,y.ZERO,y.ZERO,y.ZERO):y.globalCompositeOperation="destination-out"}),u.addEventListener("afterrender",({detail:f})=>{const y=f.context;o?y.blendFuncSeparate(y.SRC_ALPHA,y.ONE_MINUS_SRC_ALPHA,y.ONE,y.ONE_MINUS_SRC_ALPHA):y.globalCompositeOperation="source-over"}),r.append(u)}}}isTransparentRectByCanvas(r){let e=!0;for(let t=0;t<r.data.length;t+=4)if(r.data[t+3]!==0){e=!1;break}return e}createSpriteNode(r){const{imageData:e,worldScaling:t,safariRect:o,originRect:i}=r;let n;if(typeof OffscreenCanvas=="function"){n=new OffscreenCanvas(e.width,e.height);const h=n.getContext("2d");h&&h.putImageData(e,0,0)}else if(typeof document<"u"){n=document.createElement("canvas"),n.width=e.width,n.height=e.height;const h=n.getContext("2d");h&&h.putImageData(e,0,0)}if(!n)return console.warn("Failed to create imageEraserBitmap Sprite"),null;const a=[Math.floor(i.w/t[0]),Math.floor(i.h/t[1])],c=[Math.floor((o.x-i.x)/t[0]),Math.floor((o.y-i.y)/t[1]),Math.floor(o.w/t[0]),Math.floor(o.h/t[1])],l={name:"eraserTexture",anchor:[.5,.5],pos:[0,0],size:a,texture:n,textureRect:c};return new D.Sprite(l)}computEraserPoints(r,e){const t=Math.ceil(e/2);return r.length===2?this.computDot(r,t):this.computLine(r,t)}computDot(r,e){const t=new s.Point2d(r[0],r[1]);return s.Point2d.GetDotStroke(t,e,8)}computLine(r,e){const t=[],o=[];let i,n;for(let a=0;a<r.length;a+=2){const c=new s.Point2d(r[a],r[a+1]);let l;if(a==r.length-2){const h=new s.Point2d(r[a-2],r[a-1]);l=s.Vec2d.Sub(c,h).uni(),i=c}else{a===0&&(n=c);const h=new s.Point2d(r[a+2],r[a+3]);l=s.Vec2d.Sub(h,c).uni()}if(l){const h=s.Vec2d.Per(l).mul(e);t.push(s.Point2d.Sub(c,h)),o.push(s.Point2d.Add(c,h))}}if(i&&n){const a=s.Point2d.GetSemicircleStroke(i,t[t.length-1],-1,8),c=s.Point2d.GetSemicircleStroke(n,o[0],-1,8);return t.concat(a,o.reverse(),c)}return[]}replace(r,e,t){var i;if(!e){t&&r.append(t);return}const o=r.getElementsByName(e);if(o.length)for(const n of o)t?Kt(t,n,r):(n.remove(),B(n,r.parent));else t&&r.append(t);this.fullLayer!==this.drawLayer&&(this.fullLayer===r?(i=this.drawLayer)==null||i.getElementsByName(e).forEach(a=>{var c;a.remove(),B(a,(c=this.drawLayer)==null?void 0:c.parent)}):this.fullLayer.getElementsByName(e).forEach(a=>{a.remove(),B(a,this.fullLayer.parent)}))}removeDrawCountNodes(r,e){const t=[];r.getElementsByName(this.workId).forEach(o=>{o.id&&e&&Number(o.id)<e&&t.push(o)});for(const o of t)o.remove(),B(o,r.parent)}static updateNodeOpt(r){var P;const{node:e,opt:t,vNodes:o,willSerializeData:i,targetNode:n}=r,{zIndex:a,translate:c,angle:l,originPoint:h,scenePoint:p,scale:d,pointMap:u,thickness:f}=t;let y;const m=n&&s.cloneDeep(n)||o.get(e.name);if(!m)return;A(a)&&(e.setAttribute("zIndex",a),m.opt.zIndex=a);const w=e.parent;if(w){if(h&&d&&n){const T=[m.op[0],m.op[1]];s.scalePoints(m.op,p,d,c);const S=[m.op[0],m.op[1]],E=[S[0]-T[0],S[1]-T[1]];if(m.centerPos=[m.centerPos[0]+E[0],m.centerPos[1]+E[1]],m.opt.translate=void 0,m.opt.scale=void 0,m.opt.eraserlines)for(let I=0;I<m.opt.eraserlines.length;I++){const{op:R,thickness:M}=m.opt.eraserlines[I];m.opt.eraserlines[I].thickness=Math.round(M*Math.max(d[0],d[1]));for(let v=0;v<R.length;v++)s.scaleLitePoints(R[v],p,d,c)}}else if(c)if(e.setAttribute("translate",c),m.opt.translate=c,n){const T=[c[0]*w.worldScaling[0],c[1]*w.worldScaling[1]];y=s.getRectTranslated(m.rect,T),m.rect=y}else{const T=K.getRectFromLayer(w,e.name);m.rect=T||m.rect}else if(A(l))if(e.setAttribute("rotate",l),m.opt.rotate=l,n)y=s.getRectRotated(m.rect,l),m.rect=y;else{const T=K.getRectFromLayer(w,e.name);m.rect=T||m.rect}if(u){const T=u.get(e.name);if(T)for(let S=0,E=0;S<m.op.length;S+=3,E++)m.op[S]=T[E][0],m.op[S+1]=T[E][1]}if(f&&((P=m==null?void 0:m.opt)!=null&&P.thickness)&&(m.opt.thickness=f),i&&!(h&&d&&n)){if(c){const T=m.op.map((S,E)=>{const I=E%3;return I===0?S+c[0]:I===1?S+c[1]:S});if(m.op=T,m.centerPos=[m.centerPos[0]+c[0],m.centerPos[1]+c[1]],m!=null&&m.opt&&(m.opt.translate=void 0),m.opt.eraserlines)for(let S=0;S<m.opt.eraserlines.length;S++){const{op:E}=m.opt.eraserlines[S];for(let I=0;I<E.length;I++){const R=E[I].map((M,v)=>v%2?M+c[1]:M+c[0]);m.opt.eraserlines[S].op[I]=R}}}else if(A(l)){const T=m.op;if(s.rotatePoints(T,m.centerPos,l),m.op=T,m!=null&&m.opt&&(m.opt.rotate=void 0),m.opt.eraserlines)for(let S=0;S<m.opt.eraserlines.length;S++){const{op:E}=m.opt.eraserlines[S];for(let I=0;I<E.length;I++)s.rotateLitePoints(E[I],m.centerPos,l)}}}m&&o.setInfo(e.name,m)}}static getCenterPos(r,e){const{worldPosition:t,worldScaling:o}=e;return[(r.x+r.w/2-t[0])/o[0],(r.y+r.h/2-t[1])/o[1]]}static getRectFromLayer(r,e){const t=r.getElementsByName(e)[0];if(t){const o=t.getBoundingClientRect();return{x:Math.floor(o.x-K.SafeBorderPadding),y:Math.floor(o.y-K.SafeBorderPadding),w:Math.floor(o.width+K.SafeBorderPadding*2),h:Math.floor(o.height+K.SafeBorderPadding*2)}}}static isWillRefresh(r){const{toolsType:e,opt:t,node:o,updateOpt:i,willSerializeData:n}=r;return!!(n&&(i.angle||i.translate)||i.thickness&&t.thickness&&t.thickness!==i.thickness||i.strokeType&&t.strokeType&&t.strokeType!==i.strokeType||i.originPoint&&i.scenePoint&&i.scale||i.pointMap&&i.pointMap.has(o.name)||e===s.EToolsKey.Text&&(i.fontSize||i.translate||i.textInfos&&i.textInfos.get(o.name))||e===s.EToolsKey.BackgroundSVG&&(i.translate||i.scale)||e===s.EToolsKey.Image&&(i.angle||i.translate||i.scale||i.strokeColor&&t.type===s.EImageType.Iconify)||e===i.toolsType&&i.willRefresh||t.eraserlines&&t.eraserlines.length&&(i.strokeColor||i.fillColor))}};g(K,"SafeBorderPadding",10);let W=K;const Pe=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 Be(k,r,e=.01){return Math.abs(k[0]-r[0])<e&&Math.abs(k[1]-r[1])<e}function nt(k,r=.01){if(k.length===0)return[];const e=[[k[0][0],k[0][1]]];for(let t=1;t<k.length;t++)Be([k[t][0],k[t][1]],e[e.length-1],r)||e.push([k[t][0],k[t][1]]);return e}function at(k,r=.01){if(k.length<=2)return k;const e=k[0],t=k[k.length-1];return Be(e,t,r)?k.slice(0,-1):k}function ct(k){const r=k.reduce((t,o)=>t+o[0],0)/k.length,e=k.reduce((t,o)=>t+o[1],0)/k.length;return[r,e]}function Re(k,r){return Math.atan2(r[1]-k[1],r[0]-k[0])}function lt(k,r=.01){if(k.length<=2)return k;const e=[];for(const o of k)e.some(n=>Math.abs(n[0]-o[0])<r&&Math.abs(n[1]-o[1])<r)||e.push([o[0],o[1]]);const t=ct(e);return e.sort((o,i)=>{const n=Re(t,o),a=Re(t,i);return n-a})}function ht(k,r){const e=r[0].x<r[r.length-1].x;return k.map(o=>{const n=o.map(a=>{let c=-1,l=1/0,h=0;for(let p=0;p<r.length;p++){const d=r[p],u=Math.sqrt(Math.pow(d.x-a[0],2)+Math.pow(d.y-a[1],2));u<l&&(l=u,c=p,h=d.z)}return{point:a,index:c,bestZ:h}}).sort((a,c)=>a.index-c.index).map(a=>[...a.point,a.bestZ]);return e&&n[0][0]>n[n.length-1][0]||!e&&n[0][0]<n[n.length-1][0]?n.reverse():n}).sort((o,i)=>{const n=o[0][0],a=i[0][0];return e?n-a:a-n})}function pt(k,r,e=.01){const o=k.map(p=>[p.x,p.y]),i=(p,d)=>p.map(([u,f])=>({X:Math.round(u*d),Y:Math.round(f*d)})),n=new _.ClipperOffset;n.AddPath(i(o,1e3),_.JoinType.jtRound,_.EndType.etOpenButt);const a=[];n.Execute(a,1);const c=new _.Clipper;c.AddPaths(a,_.PolyType.ptSubject,!0),r.forEach(p=>{c.AddPath(i(p,1e3),_.PolyType.ptClip,!0)});const l=new _.Paths;c.Execute(_.ClipType.ctDifference,l,_.PolyFillType.pftNonZero,_.PolyFillType.pftNonZero);const h=l.map(p=>at(lt(nt(p.map(d=>[d.X/1e3,d.Y/1e3]),e),e),e)).filter(p=>p.length>=2);return ht(h,k)}class Fe extends W{constructor(e){super(e);g(this,"canRotate",!0);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.Pencil);g(this,"syncTimestamp");g(this,"syncIndex",0);g(this,"tmpPoints",[]);g(this,"MAX_REPEAR",10);g(this,"uniThickness");g(this,"workOptions");g(this,"centerPos",[0,0]);this.workOptions=e.toolsOpt,this.uniThickness=this.MAX_REPEAR/this.workOptions.thickness/10,this.syncTimestamp=0}combineConsume(){var a;const e=(a=this.workId)==null?void 0:a.toString();if(this.tmpPoints.length<2)return{type:s.EPostMessageType.None};const t=this.transformDataAll(!0),o={name:e};let i;const n=this.drawLayer||this.fullLayer;return t.length&&(i=this.draw({attrs:o,tasks:t,replaceId:e,layer:n})),{rect:i,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local}}setWorkOptions(e){super.setWorkOptions(e),this.syncTimestamp=Date.now()}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,drawCount:a,removeDrawCount:c,isSimpleWorker:l}=e,{workId:h,syncUnitTime:p}=t;p&&(this.syncUnitTime=p);const{tasks:d,effects:u,consumeIndex:f}=this.transformData(t,!1);this.syncIndex=Math.min(this.syncIndex,f,Math.max(0,this.tmpPoints.length-2));const y={name:h,id:A(a)&&a.toString()||void 0};let m,w=!1;const P=this.syncIndex;if(this.syncTimestamp===0&&(this.syncTimestamp=Date.now()),d.length&&(d[0].taskId-this.syncTimestamp>this.syncUnitTime&&(w=!0,this.syncTimestamp=d[0].taskId,this.syncIndex=this.tmpPoints.length),i||n||l)){const S=o?this.fullLayer:this.drawLayer||this.fullLayer;m=this.draw({attrs:y,tasks:d,effects:u,layer:S,removeDrawCount:c})}if(l){const S=[];return this.tmpPoints.slice(P).forEach(E=>{S.push(E.x,E.y,this.computRadius(E.z,this.workOptions.thickness))}),{...this.baseConsumeResult,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:w?S:void 0,index:w?P*3:void 0,rect:m,updateNodeOpt:{useAnimation:!0}}}if(i)return f>10&&this.tmpPoints.splice(0,f-10),{rect:m,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local};const T=[];return this.tmpPoints.slice(P).forEach(S=>{T.push(S.x,S.y,this.computRadius(S.z,this.workOptions.thickness))}),{...this.baseConsumeResult,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,rect:m,op:w?T:void 0,index:w?P*3:void 0,updateNodeOpt:{useAnimation:!0}}}consumeAll(e){var h;const t=this.workId;if(e.data){const{op:p,workState:d}=e.data;p!=null&&p.length&&d===s.EvevtWorkState.Done&&this.workOptions.strokeType===s.EStrokeType.Stroke&&this.updateTempPointsWithPressureWhenDone(p)}const o=this.transformDataAll(!0),i={name:t};let n;const a=this.fullLayer;if(o.length&&(n=this.draw({attrs:i,tasks:o,replaceId:t,layer:a})),this.tmpPoints.length<2)return this.replace(a,t),{type:s.EPostMessageType.RemoveNode,removeIds:[t],rect:n};const c=[];this.tmpPoints.map(p=>{c.push(p.x,p.y,p.z)}),this.syncTimestamp=0,delete this.workOptions.syncUnitTime;const l=s.transformToSerializableData(c);return(h=this.vNodes)==null||h.setInfo(t,{rect:n,op:c,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:n&&W.getCenterPos(n,a)}),{...this.baseConsumeResult,rect:n,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:l,updateNodeOpt:{pos:this.centerPos,useAnimation:!0}}}clearTmpPoints(){this.tmpPoints.length=0,this.syncTimestamp=0,this.syncIndex=0}consumeService(e){var p,d;const{op:t,isFullWork:o,replaceId:i,workState:n=s.EvevtWorkState.Done}=e;this.tmpPoints.length=0;for(let u=0;u<t.length;u+=3){const f=new s.Point2d(t[u],t[u+1],t[u+2]);if(this.tmpPoints.length>0){const y=this.tmpPoints[this.tmpPoints.length-1],m=s.Vec2d.Sub(f,y).uni();f.setv(m)}this.tmpPoints.push(f)}if(this.tmpPoints.length<2)return;const a=this.transformDataAll(!0),c=(p=this.workId)==null?void 0:p.toString(),l={name:c};let h;if(c&&a.length){const u=o?this.fullLayer:this.drawLayer||this.fullLayer;h=this.draw({attrs:l,tasks:a,replaceId:i,layer:u,isDrawEraserlines:n===s.EvevtWorkState.Done}),(d=this.vNodes)==null||d.setInfo(c,{rect:h,op:t,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:h&&W.getCenterPos(h,u)})}return h}computPencilPoints(e){const t=[],{op:o,eraserPolylines:i,eraserThickness:n}=e;this.tmpPoints.length=0;for(let c=0;c<o.length;c+=3){const l=new s.Point2d(o[c],o[c+1],o[c+2]);if(this.tmpPoints.length>0){const h=this.tmpPoints[this.tmpPoints.length-1],p=s.Vec2d.Sub(l,h).uni();l.setv(p)}this.tmpPoints.push(l)}if(this.tmpPoints.length<2)return;const a=this.transformDataAll(!0);if(a.length)for(let c=0;c<a.length;c++){const{pos:l,points:h}=a[c],p=h.map(f=>f.point.addXY(l[0],l[1])),d=[];for(const f of i){const y=[];for(let w=0;w<f.length;w+=2){const P=new s.Point2d(f[w],f[w+1]);if(y.length>0){const T=y[y.length-1].point,S=s.Vec2d.Sub(P,T).uni();P.setv(S)}y.push({point:P,radius:n})}const{ps:m}=this.computStroke(y,!1);d.push(m.map(w=>w.XY))}pt(p,d,.01).forEach(f=>{t.push(f.map(y=>[y[0],y[1],y[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:o,replaceId:i,effects:n,layer:a,removeDrawCount:c,isDrawEraserlines:l=!0}=e,{strokeColor:h,strokeType:p,thickness:d,zIndex:u,scale:f,rotate:y,translate:m,eraserlines:w,lineCap:P,lineDash:T}=this.workOptions;n!=null&&n.size&&(n.forEach(v=>{var L;(L=a.getElementById(v+""))==null||L.remove()}),n.clear()),c&&this.removeDrawCountNodes(a,c);let S;const E=[],I=a.worldPosition,R=a.worldScaling;for(let v=0;v<o.length;v++){const{pos:L,points:C}=o[v],{ps:b,rect:N}=this.computDrawPoints(C);let x;const O=C.length===1;p===s.EStrokeType.Stroke||O?x=Q(b,!0):x=Q(b,!1);const F={pos:L,d:x,fillColor:p===s.EStrokeType.Stroke||O?h:void 0,lineDash:O?void 0:p===s.EStrokeType.Dotted?[T&&T[0]||1,(T&&T[1]||2)*d]:p===s.EStrokeType.LongDotted?[(T&&T[0]||1)*d,(T&&T[1]||2)*d]:void 0,strokeColor:h,lineCap:O?void 0:P,lineWidth:p===s.EStrokeType.Stroke||O?0:d};S=s.computRect(S,{x:Math.floor((N.x+L[0])*R[0]+I[0]-W.SafeBorderPadding),y:Math.floor((N.y+L[1])*R[1]+I[1]-W.SafeBorderPadding),w:Math.floor(N.w*R[0]+2*W.SafeBorderPadding),h:Math.floor(N.h*R[1]+2*W.SafeBorderPadding)}),E.push(F)}f&&(t.scale=f),y&&(t.rotate=y),m&&(t.translate=m);const M=new D.Group;if(S){this.centerPos=W.getCenterPos(S,a);const v=p===s.EStrokeType.Stroke&&!w;M.attr({...t,normalize:!0,anchor:[.5,.5],bgcolor:v?h:void 0,pos:this.centerPos,size:[(S.w-2*W.SafeBorderPadding)/R[0],(S.h-2*W.SafeBorderPadding)/R[1]],zIndex:u});const L=E.map(C=>(C.pos=[C.pos[0]-this.centerPos[0],C.pos[1]-this.centerPos[1]],new D.Path(C)));M.append(...L),v&&M.seal(),w&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:M,eraserlines:w,pos:this.centerPos,layer:a},l)),this.replace(a,i,M)}if(f||y||m){const v=M==null?void 0:M.getBoundingClientRect();v&&(S={x:Math.floor(v.x-W.SafeBorderPadding),y:Math.floor(v.y-W.SafeBorderPadding),w:Math.floor(v.width+W.SafeBorderPadding*2),h:Math.floor(v.height+W.SafeBorderPadding*2)})}return this.isDelete&&M.setAttribute("opacity",0),S}computDrawPoints(e){return this.workOptions.strokeType===s.EStrokeType.Stroke||e.length===1?this.computStroke(e):this.computNomal(e)}computNomal(e){let t=this.workOptions.thickness;const o=e.map(i=>(t=Math.max(t,i.radius),i.point));return{ps:o,rect:s.getRectFromPoints(o,t)}}computStroke(e,t=!0){return e.length===1?this.computDotStroke(e[0]):this.computLineStroke(e,t)}computLineStroke(e,t=!0){const o=[],i=[];for(let c=0;c<e.length;c++){const{point:l,radius:h}=e[c];let p=l.v;c===0&&e.length>1&&(p=e[c+1].point.v);const d=s.Vec2d.Per(p).mul(h);o.push(s.Point2d.Sub(l,d)),i.push(s.Point2d.Add(l,d))}const n=e[e.length-1];if(t){const c=s.Point2d.GetSemicircleStroke(n.point,o[o.length-1],-1,8),l=s.Point2d.GetSemicircleStroke(e[0].point,i[0],-1,8),h=o.concat(c,i.reverse(),l);return{ps:h,rect:s.getRectFromPoints(h)}}const a=o.concat(i.reverse());return{ps:a,rect:s.getRectFromPoints(a)}}computDotStroke(e){const{point:t,radius:o}=e,i={x:t.x-o,y:t.y-o,w:o*2,h:o*2};return{ps:s.Point2d.GetDotStroke(t,o,8),rect:i}}transformData(e,t){const{op:o,workState:i}=e;let n=this.tmpPoints.length-1,a=[];if(o!=null&&o.length&&i){const{strokeType:c,thickness:l}=this.workOptions,h=new Set;n=c===s.EStrokeType.Stroke?this.updateTempPointsWithPressure(o,l,h):this.updateTempPoints(o,l,h);const p=t?this.tmpPoints:this.tmpPoints.slice(n);return a=this.getTaskPoints(p,l),{tasks:a,effects:h,consumeIndex:n}}return{tasks:a,consumeIndex:n}}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 p;const o=[];if(e.length===0)return[];let i=0,n=e[0].x,a=e[0].y,c=[n,a],l=[],h=e[0].t;for(;i<e.length;){const d=e[i],u=d.x-n,f=d.y-a,y=d.z,m=t?this.computRadius(y,t):y;if(l.push({point:new s.Point2d(u,f,y,e[i].v),radius:m}),i>0&&i<e.length-1){const w=e[i].getAngleByPoints(e[i-1],e[i+1]);if(w<60||w>300){const P=(p=l.pop())==null?void 0:p.point.clone();P&&o.push({taskId:h,pos:c,points:[...l,{point:P,radius:m}]}),n=e[i].x,a=e[i].y,c=[n,a];const T=d.x-n,S=d.y-a;l=[{point:new s.Point2d(T,S,y),radius:m}],h=Date.now()}}i++}return o.push({taskId:h,pos:c,points:l}),o}updateTempPointsWithPressure(e,t,o){const i=Date.now(),n=this.tmpPoints.length;let a=n;for(let l=0;l<e.length;l+=2){a=Math.min(a,n);const h=this.tmpPoints.length,p=new s.Point2d(e[l],e[l+1]);if(h===0){this.tmpPoints.push(p);continue}const d=h-1,u=this.tmpPoints[d],f=s.Vec2d.Sub(p,u).uni();if(p.isNear(u,t)){if(u.z<this.MAX_REPEAR){if(u.setz(Math.min(u.z+1,this.MAX_REPEAR)),a=Math.min(a,d),h>1){let w=h-1;for(;w>0;){const P=this.tmpPoints[w].distance(this.tmpPoints[w-1]),T=Math.max(this.tmpPoints[w].z-this.uniThickness*P,0);if(this.tmpPoints[w-1].z>=T)break;this.tmpPoints[w-1].setz(T),a=Math.min(a,w-1),w--}}}else a=1/0;continue}p.setv(f);const y=p.distance(u),m=Math.max(u.z-this.uniThickness*y,0);h>1&&s.Vec2d.Equals(f,u.v,.02)&&(m>0||u.z<=0)&&(o&&u.t&&o.add(u.t),this.tmpPoints.pop(),a=Math.min(d,a)),p.setz(m),this.tmpPoints.push(p)}if(a===1/0)return this.tmpPoints.length;let c=n;if(a===n){c=Math.max(c-1,0);const l=this.tmpPoints[c].t;l&&(o==null||o.add(l))}else{let l=n-1;for(c=a;l>=0;){const h=this.tmpPoints[l].t;if(h&&(o==null||o.add(h),l<=a)){c=l,l=-1;break}l--}}return this.tmpPoints[c].setT(i),c}updateTempPoints(e,t,o){var l;const i=Date.now(),n=this.tmpPoints.length;let a=n;for(let h=0;h<e.length;h+=2){const p=this.tmpPoints.length,d=new s.Point2d(e[h],e[h+1]);if(p===0){this.tmpPoints.push(d);continue}const u=p-1,f=this.tmpPoints[u],y=s.Vec2d.Sub(d,f).uni();if(d.isNear(f,t/2)){a=Math.min(u,a);continue}s.Vec2d.Equals(y,f.v,.02)&&(o&&f.t&&o.add(f.t),this.tmpPoints.pop(),a=Math.min(u,a)),d.setv(y),this.tmpPoints.push(d)}let c=n;if(a===n){c=Math.max(c-1,0);const h=this.tmpPoints[c].t;h&&(o==null||o.add(h))}else{let h=Math.min(n-1,a);for(c=a;h>=0;){const p=(l=this.tmpPoints[h])==null?void 0:l.t;if(p&&(o==null||o.add(p),h<=a)){c=h,h=-1;break}h--}}return this.tmpPoints[c].setT(i),c}updateTempPointsWithPressureWhenDone(e){const{thickness:t}=this.workOptions,o=e.length,i=this.getMinZ(t);for(let n=0;n<o;n+=2){const a=this.tmpPoints.length,c=new s.Point2d(e[n],e[n+1]);if(a===0){this.tmpPoints.push(c);continue}const l=a-1,h=this.tmpPoints[l],p=s.Vec2d.Sub(c,h).uni(),d=c.distance(h);if(a>1&&h.z===i)break;if(c.isNear(h,t/2)){if(o<3&&h.z<this.MAX_REPEAR&&(h.setz(Math.min(h.z+1,this.MAX_REPEAR)),a>1)){let f=a-1;for(;f>0;){const y=this.tmpPoints[f].distance(this.tmpPoints[f-1]),m=Math.max(this.tmpPoints[f].z-this.uniThickness*y,-t/4);if(this.tmpPoints[f-1].z>=m)break;this.tmpPoints[f-1].setz(m),f--}}continue}c.setv(p);const u=Math.max(h.z-this.uniThickness*d,i);a>1&&s.Vec2d.Equals(p,h.v,.02)&&h.z<=0&&this.tmpPoints.pop(),c.setz(u),this.tmpPoints.push(c)}}static updateNodeOpt(e){var l,h;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,strokeType:a}=o,c=i.get(t.name);return n&&(t.tagName==="GROUP"?Ut(t)?t.setAttribute("bgcolor",n):t.children.forEach(p=>{p.setAttribute("strokeColor",n),p.getAttribute("fillColor")&&p.setAttribute("fillColor",n)}):(t.setAttribute("strokeColor",n),t.setAttribute("fillColor",n)),(l=c==null?void 0:c.opt)!=null&&l.strokeColor&&(c.opt.strokeColor=n)),a&&(c!=null&&c.opt)&&((h=c.opt)!=null&&h.strokeType)&&(c.opt.strokeType=a),c&&i.setInfo(t.name,c),W.updateNodeOpt(e)}}class ze extends W{constructor(e){super(e);g(this,"toolsType",s.EToolsKey.LaserPen);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.none);g(this,"syncTimestamp");g(this,"syncIndex",0);g(this,"tmpPoints",[]);g(this,"workOptions");g(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:o}=e,{workId:i,op:n,syncUnitTime:a}=t;if((n==null?void 0:n.length)===0)return{type:s.EPostMessageType.None};if(a&&(this.syncUnitTime=a),this.updateTempPoints(n||[]),this.consumeIndex>this.tmpPoints.length-4)return{type:s.EPostMessageType.None};const{strokeColor:c,thickness:l,strokeType:h,lineDash:p,lineCap:d}=this.workOptions,u=s.getRectFromPoints(this.tmpPoints,l);let f=!1;const y=this.syncIndex,m=this.tmpPoints.slice(this.consumeIndex);this.consumeIndex=this.tmpPoints.length-1,this.syncTimestamp===0&&(this.syncTimestamp=Date.now());const w={name:i==null?void 0:i.toString(),opacity:1,lineDash:h===s.EStrokeType.Dotted?[p&&p[0]||1,(p&&p[1]||2)*l]:h===s.EStrokeType.LongDotted?[(p&&p[0]||1)*l,(p&&p[1]||2)*l]:void 0,strokeColor:c,lineCap:d,lineWidth:l,anchor:[.5,.5]},P=this.getTaskPoints(m);if(P.length){const S=Date.now();S-this.syncTimestamp>this.syncUnitTime&&(f=!0,this.syncTimestamp=S,this.syncIndex=this.tmpPoints.length),o&&this.draw({attrs:w,tasks:P,isDot:!1,layer:this.drawLayer||this.fullLayer})}const T=[];return this.tmpPoints.slice(y).forEach(S=>{T.push(S.x,S.y)}),{rect:{x:u.x*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],y:u.y*this.fullLayer.worldScaling[1]+this.fullLayer.worldPosition[1],w:u.w*this.fullLayer.worldScaling[0],h:u.h*this.fullLayer.worldScaling[1]},type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:f?T:void 0,index:f?y*2:void 0,...this.baseConsumeResult}}consumeAll(){var n;const e=(n=this.workId)==null?void 0:n.toString();let t;if(this.tmpPoints.length-1>this.consumeIndex){let a=this.tmpPoints.slice(this.consumeIndex);const c=a.length===1,{strokeColor:l,thickness:h,strokeType:p}=this.workOptions;if(c){const f=this.computDotStroke({point:a[0],radius:h/2});a=f.ps,t=f.rect}else t=s.getRectFromPoints(this.tmpPoints,h);const d={name:e==null?void 0:e.toString(),fillColor:c?l:void 0,opacity:1,lineDash:p===s.EStrokeType.Dotted&&!c?[1,h*2]:p===s.EStrokeType.LongDotted&&!c?[h,h*2]:void 0,strokeColor:l,lineCap:c?void 0:"round",lineWidth:c?0:h,anchor:[.5,.5]},u=this.getTaskPoints(a);u.length&&this.draw({attrs:d,tasks:u,isDot:c,layer:this.drawLayer||this.fullLayer})}const o=[];this.tmpPoints.forEach(a=>{o.push(a.x,a.y)});const i=s.transformToSerializableData(o);return{rect:t&&{x:t.x*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],y:t.y*this.fullLayer.worldScaling[1]+this.fullLayer.worldPosition[1],w:t.w*this.fullLayer.worldScaling[0],h:t.h*this.fullLayer.worldScaling[1]},type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:i,index:this.syncIndex*2,...this.baseConsumeResult}}clearTmpPoints(){this.tmpPoints.length=0,this.syncTimestamp=0,this.syncIndex=0}consumeService(e){var y;const{op:t,replaceId:o,isFullWork:i}=e,{strokeColor:n,thickness:a,strokeType:c}=this.workOptions;if(!t.length){const m=s.getRectFromPoints(this.tmpPoints,a);return{x:m.x*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],y:m.y*this.fullLayer.worldScaling[1]+this.fullLayer.worldPosition[1],w:m.w*this.fullLayer.worldScaling[0],h:m.h*this.fullLayer.worldScaling[1]}}const l=Math.max(0,this.tmpPoints.length-1);this.updateTempPoints(t||[]);let h,p=this.tmpPoints.slice(l);const d=p.length===1;if(d){const m=this.computDotStroke({point:p[0],radius:a/2});p=m.ps,h=m.rect}else h=s.getRectFromPoints(this.tmpPoints,a);const u={name:(y=this.workId)==null?void 0:y.toString(),fillColor:d?n:void 0,opacity:1,lineDash:c===s.EStrokeType.Dotted&&!d?[1,a*2]:c===s.EStrokeType.LongDotted&&!d?[a,a*2]:void 0,strokeColor:n,lineCap:d?void 0:"round",lineWidth:d?0:a,anchor:[.5,.5]},f=this.getTaskPoints(p);if(f.length){const m=i?this.fullLayer:this.drawLayer||this.fullLayer;this.draw({attrs:u,tasks:f,isDot:d,replaceId:o,layer:m})}return{x:h.x*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],y:h.y*this.fullLayer.worldScaling[1]+this.fullLayer.worldPosition[1],w:h.w*this.fullLayer.worldScaling[0],h:h.h*this.fullLayer.worldScaling[1]}}computDotStroke(e){const{point:t,radius:o}=e,i={x:t.x-o,y:t.y-o,w:o*2,h:o*2};return{ps:s.Point2d.GetDotStroke(t,o,8),rect:i}}updateTempPoints(e){const t=this.tmpPoints.length;for(let o=0;o<e.length;o+=2){if(t){const i=this.tmpPoints.slice(-1)[0];i&&i.x===e[o]&&i.y===e[o+1]&&this.tmpPoints.pop()}this.tmpPoints.push(new s.Point2d(e[o],e[o+1]))}}async draw(e){const{attrs:t,tasks:o,isDot:i,layer:n}=e,{duration:a}=this.workOptions;for(const c of o){const l=new D.Path,{pos:h,points:p}=c;let d;i?d=Q(p,!0):d=Q(p,!1),l.attr({...t,pos:h,d});const{vertex:u,fragment:f}=this.workOptions;if(u&&f){const y=n.renderer.createProgram({vertex:u,fragment:f}),{width:m,height:w}=n.getResolution();l.setUniforms({u_time:0,u_resolution:[m,w]}),l.setProgram(y)}n.appendChild(l),l.transition(a).attr({scale:i?[.1,.1]:[1,1],lineWidth:i?0:1}).then(()=>{l.remove()})}}getTaskPoints(e){var l;const t=[];if(e.length===0)return[];let o=0,i=e[0].x,n=e[0].y,a=[i,n],c=[];for(;o<e.length;){const h=e[o],p=h.x-i,d=h.y-n;if(c.push(new s.Point2d(p,d)),o>0&&o<e.length-1){const u=e[o].getAngleByPoints(e[o-1],e[o+1]);if(u<60||u>300){const f=(l=c.pop())==null?void 0:l.clone();f&&t.push({pos:a,points:[...c,f]}),i=e[o].x,n=e[o].y,a=[i,n];const y=h.x-i,m=h.y-n;c=[new s.Point2d(y,m)]}}o++}return t.push({pos:a,points:c}),t}removeLocal(){}removeService(e){let t;const o=[];return this.fullLayer.getElementsByName(e).forEach(i=>{if(i.name===e){const n=i.getBoundingClientRect();t=s.computRect(t,{x:n.x,y:n.y,w:n.width,h:n.height}),o.push(i)}}),o.length&&o.forEach(i=>i.remove()),t}}const te=class te extends W{constructor(e,t){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.none);g(this,"toolsType",s.EToolsKey.Eraser);g(this,"serviceWork");g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"worldPosition");g(this,"worldScaling");g(this,"eraserRect");g(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],o=e[1]*this.worldScaling[1]+this.worldPosition[1],{width:i,height:n}=te.eraserSizes[this.workOptions.thickness];this.eraserRect={x:t-i*.5,y:o-n*.5,w:i,h:n},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 s.Vec2d(this.tmpPoints[0],this.tmpPoints[1]),o=new s.Vec2d(this.tmpPoints[2],this.tmpPoints[3]),i=s.Vec2d.Sub(o,t).uni(),n=s.Vec2d.Dist(t,o),{width:a,height:c}=te.eraserSizes[this.workOptions.thickness],l=Math.min(a,c),h=Math.round(n/l);if(h>1){const p=[];for(let d=0;d<h;d++){const u=s.Vec2d.Mul(i,d*l);p.push(this.tmpPoints[0]+u.x,this.tmpPoints[1]+u.y)}return p.concat(e)}}return e}isNear(e,t){const o=new s.Vec2d(e[0],e[1]),i=new s.Vec2d(t[0],t[1]),{width:n,height:a}=te.eraserSizes[this.workOptions.thickness];return s.Vec2d.Dist(o,i)<Math.hypot(n,a)*.5}remove(e){const{curNodeMap:t,removeIds:o}=e;let i;for(const n of t.values())if(n.rect&&this.eraserRect&&this.eraserPolyline&&s.isIntersect(this.eraserRect,n.rect)){const{op:a}=n,c=[],l=[];for(let p=0;p<a.length;p+=3){const d=new s.Vec2d(a[p]*this.worldScaling[0]+this.worldPosition[0],a[p+1]*this.worldScaling[1]+this.worldPosition[1],a[p+2]);l.push(d),c.push(new s.Point2d(d.x,d.y))}const h=c.length&&s.getRectFromPoints(c)||n.rect;s.isIntersect(h,this.eraserRect)&&(l.length>1?Se.polyline(l.map(d=>d.XY),this.eraserPolyline).length&&o.add(n.name):o.add(n.name),i=s.computRect(i,n.rect||h))}return o.forEach(n=>{var c;const a=this.fullLayer.getElementsByName(n);a[0]&&(a[0].remove(),B(a[0],this.fullLayer.parent),(c=this.vNodes)==null||c.delete(n))}),i&&(i.x-=W.SafeBorderPadding,i.y-=W.SafeBorderPadding,i.w+=W.SafeBorderPadding*2,i.h+=W.SafeBorderPadding*2),i}consume(e){const{op:t,disableEraseImage:o,disableEraseText:i}=e.data;if(!t||t.length===0)return{type:s.EPostMessageType.None,...this.baseConsumeResult};const n=this.tmpPoints.length;if(n>1&&this.isNear([t[0],t[1]],[this.tmpPoints[n-2],this.tmpPoints[n-1]]))return{type:s.EPostMessageType.None,...this.baseConsumeResult};n<3?this.tmpPoints.push(t[0],t[1]):this.tmpPoints.splice(2,2,t[0],t[1]);const a=this.computRectCenterPoints();let c;const l=new Set;if(!this.vNodes)return{type:s.EPostMessageType.None,...this.baseConsumeResult};const h=this.getCanEraserNodeMap(this.vNodes.getCanEraserNodes(this.vNodes.curNodeMap,{disableEraseImage:o,disableEraseText:i}));for(let p=0;p<a.length-1;p+=2){this.createEraserRect(a.slice(p,p+2));const d=this.remove({curNodeMap:h,removeIds:l});c=s.computRect(c,d)}return c&&l.size?{type:s.EPostMessageType.RemoveNode,rect:c,removeIds:[...l]}:{type:s.EPostMessageType.None,...this.baseConsumeResult}}consumeAll(e){return this.consume(e)}clearTmpPoints(){this.tmpPoints.length=0}getCanEraserNodeMap(e){var t;if(this.serviceWork){const o=new Map(e),i=this.serviceWork.selectorWorkShapes,n=this.serviceWork.workShapes;for(const a of i.values())if((t=a.selectIds)!=null&&t.length)for(const c of a.selectIds)o.delete(c);for(const a of n.keys())o.delete(a);return o}return e}};g(te,"eraserSizes",Pe);let de=te;var Ce=s._getAllKeys,dt=1,ut=Object.prototype,ft=ut.hasOwnProperty;function yt(k,r,e,t,o,i){var n=e&dt,a=Ce(k),c=a.length,l=Ce(r),h=l.length;if(c!=h&&!n)return!1;for(var p=c;p--;){var d=a[p];if(!(n?d in r:ft.call(r,d)))return!1}var u=i.get(k),f=i.get(r);if(u&&f)return u==r&&f==k;var y=!0;i.set(k,r),i.set(r,k);for(var m=n;++p<c;){d=a[p];var w=k[d],P=r[d];if(t)var T=n?t(P,w,d,r,k,i):t(w,P,d,k,r,i);if(!(T===void 0?w===P||o(w,P,e,t,i):T)){y=!1;break}m||(m=d=="constructor")}if(y&&!m){var S=k.constructor,E=r.constructor;S!=E&&"constructor"in k&&"constructor"in r&&!(typeof S=="function"&&S instanceof S&&typeof E=="function"&&E instanceof E)&&(y=!1)}return i.delete(k),i.delete(r),y}var mt=yt,Te=s._Stack,gt=s._equalArrays,wt=s._equalByTag,Tt=mt,Le=s._getTag,be=s.isArray_1,De=s.isBufferExports,kt=s.isTypedArray_1,St=1,Ne="[object Arguments]",xe="[object Array]",pe="[object Object]",Pt=Object.prototype,Oe=Pt.hasOwnProperty;function vt(k,r,e,t,o,i){var n=be(k),a=be(r),c=n?xe:Le(k),l=a?xe:Le(r);c=c==Ne?pe:c,l=l==Ne?pe:l;var h=c==pe,p=l==pe,d=c==l;if(d&&De(k)){if(!De(r))return!1;n=!0,h=!1}if(d&&!h)return i||(i=new Te),n||kt(k)?gt(k,r,e,t,o,i):wt(k,r,c,e,t,o,i);if(!(e&St)){var u=h&&Oe.call(k,"__wrapped__"),f=p&&Oe.call(r,"__wrapped__");if(u||f){var y=u?k.value():k,m=f?r.value():r;return i||(i=new Te),o(y,m,e,t,i)}}return d?(i||(i=new Te),Tt(k,r,e,t,o,i)):!1}var Et=vt,It=Et,Ae=s.isObjectLike_1;function Ue(k,r,e,t,o){return k===r?!0:k==null||r==null||!Ae(k)&&!Ae(r)?k!==k&&r!==r:It(k,r,e,t,Ue,o)}var Wt=Ue,Mt=Wt;function Rt(k,r){return Mt(k,r)}var Ct=Rt;const H=s.getDefaultExportFromCjs(Ct),ge=class ge extends W{constructor(e){super(e);g(this,"toolsType",s.EToolsKey.Selector);g(this,"tmpPoints",[]);g(this,"subTmpPoints",[]);g(this,"workOptions");g(this,"vNodes");g(this,"selectIds");g(this,"selectorColor");g(this,"strokeColor");g(this,"fillColor");g(this,"oldSelectRect");g(this,"oldSubSelectRect");g(this,"canRotate",!1);g(this,"canTextEdit",!1);g(this,"canLock",!1);g(this,"scaleType",s.EScaleType.all);g(this,"toolsTypes");g(this,"shapeOpt");g(this,"textOpt");g(this,"isLocked");g(this,"thickness");g(this,"strokeType");g(this,"useStroke");this.workOptions=e.toolsOpt,this.vNodes=e.vNodes}computSelector(e=!0){const t=s.getRectFromPoints(this.tmpPoints);if(t.w===0||t.h===0)return{selectIds:[],intersectRect:void 0,subNodeMap:new Map};const{rectRange:o,nodeRange:i}=this.vNodes.getRectIntersectRange(t,e);return{selectIds:[...i.keys()],intersectRect:o,subNodeMap:i}}updateTempPoints(e){const t=this.tmpPoints.length,o=e.length;if(o>1){const i=new s.Point2d(e[o-2]*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],e[o-1]*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[1]);t===2?this.tmpPoints.splice(1,1,i):this.tmpPoints.push(i)}}drawSelector(e){const{drawRect:t,subNodeMap:o,selectorId:i,layer:n,isService:a}=e,c=new D.Group({pos:[t.x,t.y],anchor:[0,0],size:[t.w,t.h],id:i,name:i,zIndex:9999}),l=[];if(a){const h=new D.Rect({normalize:!0,pos:[t.w/2,t.h/2],lineWidth:1,strokeColor:this.selectorColor||this.workOptions.strokeColor,width:t.w,height:t.h,name:ge.selectorBorderId});l.push(h)}o.forEach((h,p)=>{const d=[h.rect.x+h.rect.w/2-t.x,h.rect.y+h.rect.h/2-t.y],u=new D.Rect({normalize:!0,pos:d,lineWidth:1,strokeColor:o.size>1?this.selectorColor||this.workOptions.strokeColor:void 0,width:h.rect.w,height:h.rect.h,id:`selector-${p}`,name:`selector-${p}`});l.push(u)}),l&&c.append(...l),(n==null?void 0:n.parent).appendChild(c)}draw(e,t,o,i=!1){var c,l;const{intersectRect:n,subNodeMap:a}=o;(l=(c=t.parent)==null?void 0:c.getElementById(e))==null||l.remove(),n&&this.drawSelector({drawRect:n,subNodeMap:a,selectorId:e,layer:t,isService:i})}getSelecteorInfo(e){this.scaleType=s.EScaleType.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 o,i=!0;for(const n of e.values()){const{opt:a,canRotate:c,scaleType:l,toolsType:h}=n;this.selectorColor=this.workOptions.strokeColor,a.strokeColor&&(this.strokeColor=a.strokeColor),a.fillColor&&(this.fillColor=a.fillColor),a.textOpt&&(this.textOpt=a.textOpt),a.thickness&&(this.thickness=a.thickness),h!==s.EToolsKey.Pencil&&(i=!1),a.strokeType&&(this.strokeType=a.strokeType),h===s.EToolsKey.SpeechBalloon&&(t.add(h),this.shapeOpt||(this.shapeOpt={}),this.shapeOpt.placement=a.placement),h===s.EToolsKey.Polygon&&(t.add(h),this.shapeOpt||(this.shapeOpt={}),this.shapeOpt.vertices=a.vertices),h===s.EToolsKey.Star&&(t.add(h),this.shapeOpt||(this.shapeOpt={}),this.shapeOpt.vertices=a.vertices,this.shapeOpt.innerRatio=a.innerRatio,this.shapeOpt.innerVerticeStep=a.innerVerticeStep),h===s.EToolsKey.Text&&(this.textOpt=a),e.size===1&&(this.textOpt&&(this.canTextEdit=!0),this.canRotate=c,this.scaleType=l),(h===s.EToolsKey.Image||h===s.EToolsKey.BackgroundSVG)&&(o=n),(l===s.EScaleType.proportional&&this.scaleType!==s.EScaleType.none||l===s.EScaleType.none)&&(this.scaleType=l)}i&&(this.useStroke=!0),t.size&&(this.toolsTypes=[...t]),o&&(e.size===1?(this.canLock=!0,o.opt.locked&&(this.isLocked=!0,this.scaleType=s.EScaleType.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&&!o.opt.locked&&(this.canLock=!1,this.canRotate=!1))}getChildrenPoints(){var e,t;if(this.scaleType===s.EScaleType.both&&((e=this.selectIds)==null?void 0:e.length)===1){const o=this.selectIds[0],i=(t=this.vNodes.get(o))==null?void 0:t.op;if(i){const n=[];for(let a=0;a<i.length;a+=3)n.push([i[a],i[a+1]]);return n}}}consume(e){if(e.isSubWorker)return this.subWorkerConsume(e);let t={type:s.EPostMessageType.Select,dataType:s.EDataType.Local,...this.baseConsumeResult};if(e.isSimpleWorker){const h=this.subWorkerConsume(e);t.subRect=h.rect}const{op:o,workState:i}=e.data;let n=this.oldSelectRect;if(i===s.EvevtWorkState.Start&&(n=this.unSelectedAllIds()),!(o!=null&&o.length)||!this.vNodes.curNodeMap.size)return e.isSimpleWorker?t:{type:s.EPostMessageType.None};this.updateTempPoints(o);const a=this.computSelector();if(this.selectIds&&H(this.selectIds,a.selectIds))return e.isSimpleWorker?t:{type:s.EPostMessageType.None};this.selectIds=a.selectIds;const c=a.intersectRect;this.getSelecteorInfo(a.subNodeMap),this.draw(s.Storage_Selector_key,this.fullLayer,a),this.oldSelectRect=c;const l=this.getChildrenPoints();return t={...t,rect:s.computRect(c,n),selectIds:a.selectIds,selectRect:c,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 t,o;let e=this.oldSelectRect;if(!((t=this.selectIds)!=null&&t.length)&&this.tmpPoints[0]&&this.selectSingleTool(this.tmpPoints[0].XY,s.Storage_Selector_key,!1),(o=this.selectIds)!=null&&o.length&&(e=this.selectedByIds(this.selectIds)),e){const i=this.getChildrenPoints();return{type:s.EPostMessageType.Select,dataType:s.EDataType.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:i,isLocked:this.isLocked,toolsTypes:this.toolsTypes,shapeOpt:this.shapeOpt,thickness:this.thickness,useStroke:this.useStroke,strokeType:this.strokeType,...this.baseConsumeResult}}return{type:s.EPostMessageType.None}}consumeService(){}updateTempPointsForSubWorker(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]);if(this.subTmpPoints[0].isNear(o,1))return!1;if(this.subTmpPoints.length===2){if(o.isNear(this.subTmpPoints[1],1))return!1;this.subTmpPoints[1]=o}else this.subTmpPoints.push(o);return!0}computDrawPoints(e){const{thickness:t}=this.workOptions,o=[];for(const a of e)o.push(new s.Vec2d(...a));const i=s.getRectFromPoints(o,t),n=[i.x+i.w/2,i.y+i.h/2];return{rect:i,pos:n,points:o.map(a=>a.XY).flat(1)}}drawForSubWorker(e){const{workId:t,layer:o,ps:i}=e,{strokeColor:n,scale:a,rotate:c,translate:l}=this.workOptions,h=o.worldPosition,p=o.worldScaling,{points:d,rect:u,pos:f}=this.computDrawPoints(i),y=1/o.worldScaling[0],m=n&&s.colorRGBA2Array(n)||[0,0,0,0],w={close:!0,normalize:!0,points:d,lineWidth:y,fillColor:s.rgbToRgba(m[0],m[1],m[2],.1),strokeColor:s.rgbToRgba(m[0],m[1],m[2],1),lineJoin:"round",lineCap:"round"};let P={x:Math.floor(u.x*p[0]+h[0]-W.SafeBorderPadding),y:Math.floor(u.y*p[1]+h[1]-W.SafeBorderPadding),w:Math.floor(u.w*p[0]+2*W.SafeBorderPadding),h:Math.floor(u.h*p[0]+2*W.SafeBorderPadding)};const T=new D.Group({name:t,id:t,pos:f,anchor:[.5,.5],size:[u.w,u.h],scale:a,rotate:c,translate:l}),S=new D.Polyline({...w,pos:[0,0]});if(T.appendChild(S),this.replace(o,t,T),a||c||l){const E=T.getBoundingClientRect();P={x:Math.floor(E.x-W.SafeBorderPadding),y:Math.floor(E.y-W.SafeBorderPadding),w:Math.floor(E.width+2*W.SafeBorderPadding),h:Math.floor(E.height+2*W.SafeBorderPadding)}}return P}transformData(e){const t=s.getRectFromPoints(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:o}=e,{op:i,syncUnitTime:n}=t;n&&(this.syncUnitTime=n);const a=i==null?void 0:i.length;if(!a||a<2)return{type:s.EPostMessageType.None,rect:void 0};let c;if(this.subTmpPoints.length===0?(this.subTmpPoints=[new s.Point2d(i[0],i[1])],c=!1):c=this.updateTempPointsForSubWorker(i),!c)return{type:s.EPostMessageType.None,rect:void 0};const l=this.transformData(this.subTmpPoints),h=o?this.fullLayer:this.drawLayer||this.fullLayer,p=this.drawForSubWorker({ps:l,workId:this.workId,layer:h}),d=s.computRect(p,this.oldSubSelectRect);return this.oldSubSelectRect=p,{rect:d,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult}}subWorkerConsumeAll(e){const{isFullWork:t}=e,o=t?this.fullLayer:this.drawLayer||this.fullLayer;return this.replace(o,this.workId),{rect:this.oldSelectRect,type:s.EPostMessageType.FullWork,dataType:s.EDataType.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=s.Storage_Selector_key,o=!1){if(e.length===2){const i=e[0],n=e[1];let a;const{nodeRange:c}=this.vNodes.getRectIntersectRange({x:i,y:n,w:0,h:0},!1),l=[...c.values()].sort((h,p)=>(p.opt.zIndex||0)-(h.opt.zIndex||0));for(const h of l){const p=this.fullLayer.getElementsByName(h.name);if(qe(p).find(u=>u.isPointCollision(i,n))){a=h;break}}if(a){const h=a.name;if(!H(this.oldSelectRect,a.rect)){const p=new Map([[h,a]]);this.getSelecteorInfo(p),this.draw(t,this.fullLayer,{intersectRect:a.rect,subNodeMap:p,selectIds:this.selectIds||[]},o)}this.selectIds=[h],this.oldSelectRect=a.rect}}}unSelectedAllIds(){let e;for(const[t,o]of this.vNodes.curNodeMap.entries())o.isSelected&&(e=s.computRect(e,o.rect),this.vNodes.unSelected(t));return e}unSelectedByIds(e){let t;for(const o of e){const i=this.vNodes.get(o);i&&i.isSelected&&(t=s.computRect(t,i.rect),this.vNodes.unSelected(o))}return t}selectedByIds(e){let t;for(const o of e){const i=this.vNodes.get(o);i&&(t=s.computRect(t,i.rect),this.vNodes.selected(o))}return t}getSelectorRect(e,t){var a;let o;const i=(a=e.parent)==null?void 0:a.getElementById(t),n=i==null?void 0:i.getBoundingClientRect();return n&&(o=s.computRect(o,{x:Math.floor(n.x),y:Math.floor(n.y),w:Math.floor(n.width+1),h:Math.floor(n.height+1)})),o}isCanFillColor(e){return e===s.EToolsKey.Ellipse||e===s.EToolsKey.Triangle||e===s.EToolsKey.Rectangle||e===s.EToolsKey.Polygon||e===s.EToolsKey.Star||e===s.EToolsKey.SpeechBalloon}async updateSelector(e){const{updateSelectorOpt:t,selectIds:o,vNodes:i,willSerializeData:n,worker:a,offset:c}=e,l=this.fullLayer;if(!l)return;let h;const p=new Map,{originPoint:d,workState:u,angle:f,translate:y,dir:m,scale:w}=t;c&&(y?t.translate=[y[0]+c[0],y[1]+c[1]]:t.translate=c);let P;if(d||y||A(f)){if(u===s.EvevtWorkState.Start&&o)return i.setTargetAssignKeys(o),{type:s.EPostMessageType.Select,dataType:s.EDataType.Local,selectRect:this.oldSelectRect,rect:this.oldSelectRect};if(P=i.getLastTarget(),!P)return}if(o)for(const E of o){const I=i.get(E);if(I){const{toolsType:R,opt:M}=I,v=(l==null?void 0:l.getElementsByName(E))[0];if(v){const L={...t};let C;const b=W.isWillRefresh({toolsType:R,opt:M,updateOpt:L,vNodes:i,node:v,willSerializeData:n});if(R){C=P==null?void 0:P.get(E);const N=He(R);if(N==null||N.updateNodeOpt({node:v,opt:L,vNodes:i,willSerializeData:n,targetNode:C}),I&&a&&b){const x=a.createWorkShapeNode({workId:E,toolsType:R,toolsOpt:I.opt});x==null||x.setWorkId(E);let O;if(R===s.EToolsKey.BackgroundSVG)O=x.consumeService({isFullWork:!0,replaceId:E});else if(R===s.EToolsKey.Image)O=await x.consumeServiceAsync({isFullWork:!0,replaceId:E,worker:a});else if(R===s.EToolsKey.Text)O=await x.consumeServiceAsync({isFullWork:!0,replaceId:E,isDrawLabel:!0});else try{O=x==null?void 0:x.consumeService({op:I.op,isFullWork:!0,replaceId:E,workState:u})}catch(F){console.error("consumeService error",F);continue}O&&(I.rect=O)}I&&(p.set(E,I),h=s.computRect(h,I.rect))}}}}P&&u===s.EvevtWorkState.Done&&(i.deleteLastTarget(),P=void 0);const T=h;if(d&&y&&w&&m&&T&&!c){const E=[[T.x,T.y],[T.x+T.w,T.y],[T.x+T.w,T.y+T.h],[T.x,T.y+T.h]];let I;switch(m){case"top":case"topLeft":case"left":y[0]>0&&y[1]>0?I=E[0]:y[0]>0?I=E[3]:y[1]>0?I=E[1]:I=E[2];break;case"topRight":y[0]<0&&y[1]>0?I=E[1]:y[0]<0?I=E[2]:y[1]>0?I=E[0]:I=E[3];break;case"right":case"bottomRight":case"bottom":y[0]<0&&y[1]<0?I=E[2]:y[0]<0?I=E[1]:y[1]<0?I=E[3]:I=E[0];break;case"bottomLeft":y[0]>0&&y[1]<0?I=E[3]:y[0]>0?I=E[0]:y[1]<0?I=E[2]:I=E[1];break}const R=I&&[d[0]-I[0],d[1]-I[1]]||[0,0];if(!H(R,[0,0]))return await this.updateSelector({...e,updateSelectorOpt:{workState:u},offset:R})}this.getSelecteorInfo(p),this.draw(s.Storage_Selector_key,l,{selectIds:o||[],subNodeMap:p,intersectRect:T});const S=s.computRect(this.oldSelectRect,T);return this.oldSelectRect=T,{type:s.EPostMessageType.Select,dataType:s.EDataType.Local,selectRect:T,renderRect:h,rect:s.computRect(S,T),selectIds:o}}blurSelector(){const e=this.unSelectedAllIds();return{type:s.EPostMessageType.Select,dataType:s.EDataType.Local,rect:e,selectIds:[],willSyncService:!0}}getRightServiceId(e){return e.replace(s.Storage_Splitter,"-")}selectServiceNode(e,t,o){const{selectIds:i}=t,n=this.getRightServiceId(e),a=this.getSelectorRect(this.fullLayer,n);let c;const l=new Map;return i==null||i.forEach(h=>{const p=this.vNodes.get(h);p&&(c=s.computRect(c,p.rect),l.set(h,p))}),this.getSelecteorInfo(l),this.draw(n,this.fullLayer,{intersectRect:c,selectIds:i||[],subNodeMap:l},o),s.computRect(c,a)}reRenderSelector(){var o;let e;const t=new Map;return(o=this.selectIds)==null||o.forEach(i=>{const n=this.vNodes.get(i);n&&(e=s.computRect(e,n.rect),t.set(i,n))},this),this.getSelecteorInfo(t),this.draw(s.Storage_Selector_key,this.fullLayer,{intersectRect:e,subNodeMap:t,selectIds:this.selectIds||[]}),this.oldSelectRect=e,e}updateSelectIds(e){var n;let t;const o=(n=this.selectIds)==null?void 0:n.filter(a=>!e.includes(a));if(o!=null&&o.length&&(t=this.unSelectedByIds(o)),e.length){const a=this.selectedByIds(e);t=s.computRect(t,a)}this.selectIds=e;const i=this.reRenderSelector();return{bgRect:t,selectRect:i}}cursorHover(e){var n,a;const t=this.oldSelectRect;this.selectIds=[];const o=(n=this.workId)==null?void 0:n.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,o,!0),this.oldSelectRect&&!H(t,this.oldSelectRect))return{type:s.EPostMessageType.CursorHover,dataType:s.EDataType.Local,rect:s.computRect(t,this.oldSelectRect),selectorColor:this.selectorColor,willSyncService:!1};if((a=this.selectIds)!=null&&a.length||(this.oldSelectRect=void 0),t&&!this.oldSelectRect)return this.cursorBlur(),{type:s.EPostMessageType.CursorHover,dataType:s.EDataType.Local,rect:t,selectorColor:this.selectorColor,willSyncService:!1}}cursorBlur(){var t,o;this.selectIds=[];const e=(t=this.workId)==null?void 0:t.toString();((o=this.fullLayer)==null?void 0:o.parent).children.forEach(i=>{i.name===e&&i.remove()})}};g(ge,"selectorBorderId","selector-border");let ue=ge;class Ke extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.both);g(this,"toolsType",s.EToolsKey.Arrow);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"arrowTipWidth");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.arrowTipWidth=this.workOptions.thickness*4,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,l=this.workId,{op:h,syncUnitTime:p}=t;p&&(this.syncUnitTime=p);const d=h==null?void 0:h.length;if(!d||d<2)return{type:s.EPostMessageType.None};let u;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(h[0],h[1])],u=!1):u=this.updateTempPoints(h),!u)return{type:s.EPostMessageType.None};let f;if(i||n||c){const m=o?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({workId:l,layer:m})}if(c){const m=s.computRect(f,this.oldRect);this.oldRect=f;const w={rect:m,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult};if(a){const P=Date.now();P-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=P,w.op=this.tmpPoints.map(T=>[...T.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!i&&a){const m=Date.now();return m-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=m,{...this.baseConsumeResult,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0}):{type:s.EPostMessageType.None}}const y=s.computRect(f,this.oldRect);return this.oldRect=f,{rect:y,...this.baseConsumeResult,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local}}consumeAll(){var a;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.fullLayer,o=this.draw({workId:e,layer:t});this.oldRect=o;const i=this.tmpPoints.map(c=>[...c.XY,0]).flat(1),n=s.transformToSerializableData(i);return(a=this.vNodes)==null||a.setInfo(e,{rect:o,op:i,opt:this.workOptions,toolsType:this.toolsType,canRotate:this.canRotate,scaleType:this.scaleType,centerPos:W.getCenterPos(o,t)}),{rect:o,...this.baseConsumeResult,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:n,isSync:!0,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawEraserlines:i}=e,{strokeColor:n,thickness:a,zIndex:c,scale:l,rotate:h,translate:p,strokeType:d,eraserlines:u,lineDash:f,lineCap:y}=this.workOptions,m=o.worldPosition,w=o.worldScaling,{points:P,pos:T,rect:S,isTriangle:E,trianglePoints:I,trianglePos:R}=this.computDrawPoints(a),M=[S.x+S.w/2,S.y+S.h/2],v={pos:M,name:t,id:t,zIndex:c,anchor:[.5,.5],size:[S.w,S.h]};l&&(v.scale=l),h&&(v.rotate=h),p&&(v.translate=p);const L=new D.Group(v),C={points:I,pos:[R[0]-M[0],R[1]-M[1]],fillColor:n,strokeColor:n,lineWidth:0,normalize:!1},b=new D.Polyline(C);if(L.append(b),!E&&P&&T){const x={points:P,pos:[T[0]-M[0],T[1]-M[1]],fillColor:n,strokeColor:n,lineDash:d===s.EStrokeType.Dotted?[f&&f[0]||1,(f&&f[1]||2)*a]:d===s.EStrokeType.LongDotted?[(f&&f[0]||1)*a,(f&&f[1]||2)*a]:void 0,lineCap:d===s.EStrokeType.Normal?void 0:y,lineWidth:a,normalize:!1},O=new D.Polyline(x);L.append(O)}u&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:L,eraserlines:u,pos:M,layer:o},i)),this.replace(o,t,L);let N={x:Math.floor(S.x*w[0]+m[0]-W.SafeBorderPadding),y:Math.floor(S.y*w[1]+m[1]-W.SafeBorderPadding),w:Math.floor(S.w*w[0]+2*W.SafeBorderPadding),h:Math.floor(S.h*w[1]+2*W.SafeBorderPadding)};if(l||h||p){const x=L.getBoundingClientRect();N={x:Math.floor(x.x-W.SafeBorderPadding),y:Math.floor(x.y-W.SafeBorderPadding),w:Math.floor(x.width+W.SafeBorderPadding*2),h:Math.floor(x.height+W.SafeBorderPadding*2)}}return this.isDelete&&L.setAttribute("opacity",0),N}computDrawPoints(e){return this.tmpPoints[1].distance(this.tmpPoints[0])>this.arrowTipWidth?this.computFullArrowPoints(e):this.computTrianglePoints()}computFullArrowPoints(e){const t=s.Vec2d.Sub(this.tmpPoints[1],this.tmpPoints[0]).uni(),o=s.Vec2d.Per(t).mul(e/2),i=s.Point2d.Sub(this.tmpPoints[0],o),n=s.Point2d.Add(this.tmpPoints[0],o),a=s.Vec2d.Mul(t,this.arrowTipWidth),c=s.Vec2d.Sub(this.tmpPoints[1],a),l=s.Point2d.Sub(c,o),h=s.Point2d.Add(c,o),p=s.Vec2d.Per(t).mul(e*1.5),d=s.Point2d.Sub(c,p),u=s.Point2d.Add(c,p),f=[this.tmpPoints[0],c],y=[d,this.tmpPoints[1],u],m=[i,n,...y,l,h];return{trianglePoints:y.map(w=>s.Point2d.Sub(w,this.tmpPoints[1]).XY).flat(1),trianglePos:this.tmpPoints[1].XY,points:f.map(w=>s.Point2d.Sub(w,this.tmpPoints[0]).XY).flat(1),rect:s.getRectFromPoints(m),isTriangle:!1,pos:this.tmpPoints[0].XY}}computTrianglePoints(){const e=s.Vec2d.Sub(this.tmpPoints[1],this.tmpPoints[0]).uni(),t=this.tmpPoints[1].distance(this.tmpPoints[0]),o=s.Vec2d.Per(e).mul(Math.floor(t*3/8)),i=s.Point2d.Sub(this.tmpPoints[0],o),n=s.Point2d.Add(this.tmpPoints[0],o),a=[i,this.tmpPoints[1],n];return{trianglePoints:a.map(c=>s.Point2d.Sub(c,this.tmpPoints[1]).XY).flat(1),trianglePos:this.tmpPoints[1].XY,rect:s.getRectFromPoints(a),isTriangle:!0}}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var l,h;const{op:t,isFullWork:o,workState:i=s.EvevtWorkState.Done}=e,n=(l=this.workId)==null?void 0:l.toString();if(!n)return;this.tmpPoints.length=0;for(let p=0;p<t.length;p+=3)this.tmpPoints.push(new s.Point2d(t[p],t[p+1],t[p+2]));const a=o?this.fullLayer:this.drawLayer||this.fullLayer,c=this.draw({workId:n,layer:a,isDrawEraserlines:i===s.EvevtWorkState.Done});return this.oldRect=c,(h=this.vNodes)==null||h.setInfo(n,{rect:c,op:t,opt:this.workOptions,toolsType:this.toolsType,canRotate:this.canRotate,scaleType:this.scaleType,centerPos:W.getCenterPos(c,a)}),c}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var l,h;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,strokeType:a}=o,c=i.get(t.name);return n&&(t.tagName==="GROUP"?t.children.forEach(p=>{p.setAttribute("strokeColor",n),p.getAttribute("fillColor")&&p.setAttribute("fillColor",n)}):(t.setAttribute("strokeColor",n),t.setAttribute("fillColor",n)),(l=c==null?void 0:c.opt)!=null&&l.strokeColor&&(c.opt.strokeColor=n)),a&&(c!=null&&c.opt)&&((h=c.opt)!=null&&h.strokeType)&&(c.opt.strokeType=a),c&&i.setInfo(t.name,c),W.updateNodeOpt(e)}}class Ve extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.Ellipse);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,l=this.workId,{op:h,syncUnitTime:p}=t;p&&(this.syncUnitTime=p);const d=h==null?void 0:h.length;if(!d||d<2)return{type:s.EPostMessageType.None};let u;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(h[0],h[1])],u=!1):u=this.updateTempPoints(h),!u)return{type:s.EPostMessageType.None};let f;if(i||n||c){const y=o?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({workId:l,layer:y,isDrawing:!0});const m=y.parent.parent,w={x:0,y:0,w:Math.floor(m.width),h:Math.floor(m.height)},P={type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult};if(s.getRectMatrixrRelation(f,w)!==s.EMatrixrRelationType.outside){const S=s.computRect(f,this.oldRect);if(this.oldRect=f,P.rect=S,!c)return P}if(c){const S=Date.now();return S-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=S,P.op=this.tmpPoints.map(E=>[...E.XY,0]).flat(1),P.index=0,P.isSync=!0),P}return{type:s.EPostMessageType.None}}if(!i&&!n&&!c&&a){const y=Date.now();return y-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=y,{type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult,op:this.tmpPoints.map(m=>[...m.XY,0]).flat(1),isSync:!0,index:0}):{type:s.EPostMessageType.None}}return{type:s.EPostMessageType.None}}consumeAll(){var a;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.fullLayer,o=this.draw({workId:e,layer:t,isDrawing:!1});this.oldRect=o;const i=this.tmpPoints.map(c=>[...c.XY,0]).flat(1),n=s.transformToSerializableData(i);return(a=this.vNodes)==null||a.setInfo(e,{rect:o,op:i,opt:this.workOptions,toolsType:this.toolsType,canRotate:this.canRotate,scaleType:this.scaleType,centerPos:o&&W.getCenterPos(o,t)}),{rect:o,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,...this.baseConsumeResult,ops:n,isSync:!0,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawing:i,isDrawEraserlines:n}=e,{strokeColor:a,fillColor:c,thickness:l,zIndex:h,scale:p,rotate:d,translate:u,strokeType:f,eraserlines:y,lineDash:m,lineCap:w}=this.workOptions,P=o.worldScaling,{radius:T,rect:S,pos:E}=this.computDrawPoints(l),I={closeType:"normal",radius:T,lineWidth:l,fillColor:c!=="transparent"&&c||void 0,strokeColor:a,normalize:!0,lineCap:w,lineDash:f===s.EStrokeType.Dotted?[m&&m[0]||1,(m&&m[1]||2)*l]:f===s.EStrokeType.LongDotted?[(m&&m[0]||1)*l,(m&&m[1]||2)*l]:void 0},R={name:t,id:t,zIndex:h,pos:E,anchor:[.5,.5],size:[S.w,S.h]};p&&(R.scale=p),d&&(R.rotate=d),u&&(R.translate=u);const M=new D.Group(R);if(i){const C=new D.Path({d:"M-4,0H4M0,-4V4",normalize:!0,pos:[0,0],strokeColor:a,lineWidth:1,scale:[1/P[0],1/P[1]]});M.append(C)}const v=new D.Ellipse({...I,pos:[0,0]});M.append(v),y&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:M,eraserlines:y,pos:E,layer:o},n)),this.replace(o,t,M);const L=M.getBoundingClientRect();return this.isDelete&&M.setAttribute("opacity",0),{x:Math.floor(L.x-W.SafeBorderPadding),y:Math.floor(L.y-W.SafeBorderPadding),w:Math.floor(L.width+W.SafeBorderPadding*2),h:Math.floor(L.height+W.SafeBorderPadding*2)}}computDrawPoints(e){const t=s.getRectFromPoints(this.tmpPoints),o=s.getRectFromPoints(this.tmpPoints,e),i=[Math.floor(t.x+t.w/2),Math.floor(t.y+t.h/2)];return{rect:o,pos:i,radius:[Math.floor(t.w/2),Math.floor(t.h/2)]}}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var l,h;const{op:t,isFullWork:o,workState:i=s.EvevtWorkState.Done}=e,n=(l=this.workId)==null?void 0:l.toString();if(!n)return;this.tmpPoints.length=0;for(let p=0;p<t.length;p+=3)this.tmpPoints.push(new s.Point2d(t[p],t[p+1],t[p+2]));const a=o?this.fullLayer:this.drawLayer||this.fullLayer,c=this.draw({workId:n,layer:a,isDrawing:!1,isDrawEraserlines:i===s.EvevtWorkState.Done});return this.oldRect=c,(h=this.vNodes)==null||h.setInfo(n,{rect:c,op:t,opt:this.workOptions,toolsType:this.toolsType,canRotate:this.canRotate,scaleType:this.scaleType,centerPos:W.getCenterPos(c,a)}),c}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var p,d,u;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,fillColor:a,strokeType:c}=o,l=i.get(t.name);let h=t;return t.tagName==="GROUP"&&(h=t.children[0]),n&&(h.setAttribute("strokeColor",n),(p=l==null?void 0:l.opt)!=null&&p.strokeColor&&(l.opt.strokeColor=n)),a&&(a==="transparent"?h.setAttribute("fillColor","rgba(0,0,0,0)"):h.setAttribute("fillColor",a),(d=l==null?void 0:l.opt)!=null&&d.fillColor&&(l.opt.fillColor=a)),c&&(l!=null&&l.opt)&&((u=l.opt)!=null&&u.strokeType)&&(l.opt.strokeType=c),l&&i.setInfo(t.name,l),W.updateNodeOpt(e)}}class _e extends W{constructor(e){super(e);g(this,"canRotate",!0);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.Rectangle);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}transformData(){const e=s.getRectFromPoints(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,o=[];for(const a of e)o.push(new s.Vec2d(...a));const i=s.getRectFromPoints(o,t),n=[i.x+i.w/2,i.y+i.h/2];return{rect:i,pos:n,points:o.map(a=>a.XY).flat(1)}}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,l=this.workId,{op:h}=t,p=h==null?void 0:h.length;if(!p||p<2)return{type:s.EPostMessageType.None};let d;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(h[0],h[1])],d=!1):d=this.updateTempPoints(h),!d)return{type:s.EPostMessageType.None};const u=this.transformData();let f;if(i||n||c){const m=o?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({ps:u,workId:l,layer:m,isDrawing:!0})}if(c){const m=s.computRect(f,this.oldRect);this.oldRect=f;const w={rect:m,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult};if(a){const P=Date.now();P-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=P,w.op=u.flat(1),w.index=0,w.isSync=!0)}return w}if(!i&&a){const m=Date.now();return m-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=m,{type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:u.flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:s.EPostMessageType.None}}const y=s.computRect(f,this.oldRect);return this.oldRect=f,{rect:y,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult}}consumeAll(){var c;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.transformData(),o=this.fullLayer,i=this.draw({ps:t,workId:e,layer:o,isDrawing:!1});this.oldRect=i;const n=t.flat(1),a=s.transformToSerializableData(n);return(c=this.vNodes)==null||c.setInfo(e,{rect:i,op:n,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:i&&W.getCenterPos(i,o)}),{rect:i,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:a,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawing:i,ps:n,replaceId:a,isDrawEraserlines:c=!0}=e,{strokeColor:l,fillColor:h,thickness:p,zIndex:d,scale:u,rotate:f,translate:y,strokeType:m,eraserlines:w,lineCap:P,lineDash:T}=this.workOptions,S=o.worldPosition,E=o.worldScaling,{points:I,rect:R,pos:M}=this.computDrawPoints(n),v={close:!0,normalize:!0,points:I,lineWidth:p,fillColor:h!=="transparent"&&h||void 0,strokeColor:l,lineCap:P,lineDash:m===s.EStrokeType.Dotted?[T&&T[0]||1,(T&&T[1]||2)*p]:m===s.EStrokeType.LongDotted?[(T&&T[0]||1)*p,(T&&T[1]||2)*p]:void 0};let L={x:Math.floor(R.x*E[0]+S[0]-W.SafeBorderPadding),y:Math.floor(R.y*E[1]+S[1]-W.SafeBorderPadding),w:Math.floor(R.w*E[0]+2*W.SafeBorderPadding),h:Math.floor(R.h*E[0]+2*W.SafeBorderPadding)};const C=new D.Group({name:t,id:t,zIndex:d,pos:M,anchor:[.5,.5],size:[R.w,R.h],scale:u,rotate:f,translate:y}),b=new D.Polyline({...v,pos:[0,0]});if(C.appendChild(b),i){const N=new D.Path({d:"M-4,0H4M0,-4V4",normalize:!0,pos:[0,0],strokeColor:l,lineWidth:1,scale:[1/E[0],1/E[1]]});C.appendChild(N)}if(w&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:C,eraserlines:w,pos:M,layer:o},c)),this.replace(o,a||t,C),u||f||y){const N=C.getBoundingClientRect();L={x:Math.floor(N.x-W.SafeBorderPadding),y:Math.floor(N.y-W.SafeBorderPadding),w:Math.floor(N.width+2*W.SafeBorderPadding),h:Math.floor(N.height+2*W.SafeBorderPadding)}}return this.isDelete&&C.setAttribute("opacity",0),L}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var p,d;const{op:t,isFullWork:o,replaceId:i,workState:n=s.EvevtWorkState.Done}=e,a=(p=this.workId)==null?void 0:p.toString();if(!a)return;const c=[];for(let u=0;u<t.length;u+=3)c.push([t[u],t[u+1],t[u+2]]);const l=o?this.fullLayer:this.drawLayer||this.fullLayer,h=this.draw({ps:c,workId:a,layer:l,isDrawing:!1,replaceId:i,isDrawEraserlines:n===s.EvevtWorkState.Done});return this.oldRect=h,(d=this.vNodes)==null||d.setInfo(a,{rect:h,op:t,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:h&&W.getCenterPos(h,l)}),h}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var p,d,u;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,fillColor:a,strokeType:c}=o,l=i.get(t.name);let h=t;return t.tagName==="GROUP"&&(h=t.children[0]),n&&(h.setAttribute("strokeColor",n),(p=l==null?void 0:l.opt)!=null&&p.strokeColor&&(l.opt.strokeColor=n)),a&&(a==="transparent"?h.setAttribute("fillColor","rgba(0,0,0,0)"):h.setAttribute("fillColor",a),(d=l==null?void 0:l.opt)!=null&&d.fillColor&&(l.opt.fillColor=a)),c&&(l!=null&&l.opt)&&((u=l.opt)!=null&&u.strokeType)&&(l.opt.strokeType=c),l&&i.setInfo(t.name,l),W.updateNodeOpt(e)}}class Ge extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.Star);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,l=this.workId,{op:h,syncUnitTime:p}=t,d=h==null?void 0:h.length;if(!d||d<2)return{type:s.EPostMessageType.None};p&&(this.syncUnitTime=p);let u;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(h[0],h[1])],u=!1):u=this.updateTempPoints(h),!u)return{type:s.EPostMessageType.None};let f;if(i||n||c){const m=o?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({workId:l,layer:m,isDrawing:!0})}if(c){const m=s.computRect(f,this.oldRect);this.oldRect=f;const w={rect:m,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult};if(a){const P=Date.now();P-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=P,w.op=this.tmpPoints.map(T=>[...T.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!i&&a){const m=Date.now();return m-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=m,{type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:s.EPostMessageType.None}}const y=s.computRect(f,this.oldRect);return this.oldRect=f,{rect:y,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult}}consumeAll(){var a;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.fullLayer,o=this.draw({workId:e,layer:t,isDrawing:!1});this.oldRect=o;const i=this.tmpPoints.map(c=>[...c.XY,0]).flat(1),n=s.transformToSerializableData(i);return(a=this.vNodes)==null||a.setInfo(e,{rect:o,op:i,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:o&&W.getCenterPos(o,t)}),{rect:o,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:n,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawing:i,isDrawEraserlines:n=!0}=e,{strokeColor:a,fillColor:c,thickness:l,zIndex:h,vertices:p,innerVerticeStep:d,innerRatio:u,scale:f,rotate:y,translate:m,strokeType:w,eraserlines:P,lineDash:T,lineCap:S}=this.workOptions,E=o.worldScaling,{rect:I,pos:R,points:M}=this.computDrawPoints(l,p,d,u),v={close:!0,points:M,lineWidth:l,fillColor:c!=="transparent"&&c||void 0,strokeColor:a,normalize:!0,lineCap:S,lineDash:w===s.EStrokeType.Dotted?[T&&T[0]||1,(T&&T[1]||2)*l]:w===s.EStrokeType.LongDotted?[(T&&T[0]||1)*l,(T&&T[1]||2)*l]:void 0},L={name:t,id:t,zIndex:h,pos:R,anchor:[.5,.5],size:[I.w,I.h]};f&&(L.scale=f),y&&(L.rotate=y),m&&(L.translate=m);const C=new D.Group(L);if(i){const x=new D.Path({d:"M-4,0H4M0,-4V4",normalize:!0,pos:[0,0],strokeColor:a,lineWidth:1,scale:[1/E[0],1/E[1]]});C.append(x)}const b=new D.Polyline({...v,pos:[0,0]});C.append(b),P&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:C,eraserlines:P,pos:R,layer:o},n)),this.replace(o,t,C);const N=C.getBoundingClientRect();return this.isDelete&&C.setAttribute("opacity",0),{x:Math.floor(N.x-W.SafeBorderPadding),y:Math.floor(N.y-W.SafeBorderPadding),w:Math.floor(N.width+W.SafeBorderPadding*2),h:Math.floor(N.height+W.SafeBorderPadding*2)}}computDrawPoints(e,t,o,i){const n=s.getRectFromPoints(this.tmpPoints),a=[Math.floor(n.x+n.w/2),Math.floor(n.y+n.h/2)],c=s.getWHRatio(n.w,n.h),l=Math.floor(Math.min(n.w,n.h)/2),h=i*l,p=[],d=2*Math.PI/t;for(let f=0;f<t;f++){const y=f*d-.5*Math.PI;let m,w;f%o===1?(m=h*c[0]*Math.cos(y),w=h*c[1]*Math.sin(y)):(m=l*c[0]*Math.cos(y),w=l*c[1]*Math.sin(y),p.push(m,w)),p.push(m,w)}return{rect:s.getRectFromPoints(this.tmpPoints,e),pos:a,points:p}}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n)||s.Point2d.Sub(i,o).XY.includes(0))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var l,h;const{op:t,isFullWork:o,workState:i=s.EvevtWorkState.Done}=e,n=(l=this.workId)==null?void 0:l.toString();if(!n)return;this.tmpPoints.length=0;for(let p=0;p<t.length;p+=3)this.tmpPoints.push(new s.Point2d(t[p],t[p+1],t[p+2]));const a=o?this.fullLayer:this.drawLayer||this.fullLayer,c=this.draw({workId:n,layer:a,isDrawing:!1,isDrawEraserlines:i===s.EvevtWorkState.Done});return this.oldRect=c,(h=this.vNodes)==null||h.setInfo(n,{rect:c,op:t,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:c&&W.getCenterPos(c,a)}),c}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var m;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,fillColor:a,toolsType:c,vertices:l,innerVerticeStep:h,innerRatio:p,strokeType:d}=o,u=i.get(t.name),f=u==null?void 0:u.opt;let y=t;return t.tagName==="GROUP"&&(y=t.children[0]),n&&(y.setAttribute("strokeColor",n),f!=null&&f.strokeColor&&(f.strokeColor=n)),a&&(a==="transparent"?y.setAttribute("fillColor","rgba(0,0,0,0)"):y.setAttribute("fillColor",a),f!=null&&f.fillColor&&(f.fillColor=a)),c===s.EToolsKey.Star&&(l&&(f.vertices=l),h&&(f.innerVerticeStep=h),p&&(f.innerRatio=p)),d&&(u!=null&&u.opt)&&((m=u.opt)!=null&&m.strokeType)&&(u.opt.strokeType=d),u&&i.setInfo(t.name,{...u,opt:f}),W.updateNodeOpt(e)}}class Xe extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.Polygon);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,{op:l,syncUnitTime:h}=t;h&&(this.syncUnitTime=h);const p=this.workId,d=l==null?void 0:l.length;if(!d||d<2)return{type:s.EPostMessageType.None};let u;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(l[0],l[1])],u=!1):u=this.updateTempPoints(l),!u)return{type:s.EPostMessageType.None};let f;if(i||n||c){const m=o?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({workId:p,layer:m,isDrawing:!0})}if(c){const m=s.computRect(f,this.oldRect);this.oldRect=f;const w={type:s.EPostMessageType.DrawWork,rect:m,dataType:s.EDataType.Local,...this.baseConsumeResult};if(a){const P=Date.now();P-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=P,w.op=this.tmpPoints.map(T=>[...T.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!i&&a){const m=Date.now();return m-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=m,{type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:s.EPostMessageType.None}}const y=s.computRect(f,this.oldRect);return this.oldRect=f,{rect:y,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult}}consumeAll(){var a;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.fullLayer,o=this.draw({workId:e,layer:t,isDrawing:!1});this.oldRect=o;const i=this.tmpPoints.map(c=>[...c.XY,0]).flat(1),n=s.transformToSerializableData(i);return(a=this.vNodes)==null||a.setInfo(e,{rect:o,op:i,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:o&&W.getCenterPos(o,t)}),{rect:o,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:n,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawing:i,isDrawEraserlines:n=!0}=e,{strokeColor:a,fillColor:c,thickness:l,zIndex:h,vertices:p,scale:d,rotate:u,translate:f,strokeType:y,eraserlines:m,lineCap:w,lineDash:P}=this.workOptions,T=o.worldScaling,{rect:S,pos:E,points:I}=this.computDrawPoints(l,p),R={close:!0,points:I,lineWidth:l,fillColor:c!=="transparent"&&c||void 0,strokeColor:a,normalize:!0,lineCap:w,lineDash:y===s.EStrokeType.Dotted?[P&&P[0]||1,(P&&P[1]||2)*l]:y===s.EStrokeType.LongDotted?[(P&&P[0]||1)*l,(P&&P[1]||2)*l]:void 0},M={name:t,id:t,zIndex:h,pos:E,anchor:[.5,.5],size:[S.w,S.h]};d&&(M.scale=d),u&&(M.rotate=u),f&&(M.translate=f);const v=new D.Group(M);if(i){const b=new D.Path({d:"M-4,0H4M0,-4V4",normalize:!0,pos:[0,0],strokeColor:a,lineWidth:1,scale:[1/T[0],1/T[1]]});v.append(b)}const L=new D.Polyline({...R,pos:[0,0]});v.append(L),m&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:v,eraserlines:m,pos:E,layer:o},n)),this.replace(o,t,v);const C=v.getBoundingClientRect();return this.isDelete&&v.setAttribute("opacity",0),{x:Math.floor(C.x-W.SafeBorderPadding),y:Math.floor(C.y-W.SafeBorderPadding),w:Math.floor(C.width+W.SafeBorderPadding*2),h:Math.floor(C.height+W.SafeBorderPadding*2)}}computDrawPoints(e,t){const o=s.getRectFromPoints(this.tmpPoints),i=[Math.floor(o.x+o.w/2),Math.floor(o.y+o.h/2)],n=s.getWHRatio(o.w,o.h),a=Math.floor(Math.min(o.w,o.h)/2),c=[],l=2*Math.PI/t;for(let p=0;p<t;p++){const d=p*l-.5*Math.PI,u=a*n[0]*Math.cos(d),f=a*n[1]*Math.sin(d);c.push(u,f)}return{rect:s.getRectFromPoints(this.tmpPoints,e),pos:i,points:c}}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n)||s.Point2d.Sub(i,o).XY.includes(0))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var l,h;const{op:t,isFullWork:o,workState:i=s.EvevtWorkState.Done}=e,n=(l=this.workId)==null?void 0:l.toString();if(!n)return;this.tmpPoints.length=0;for(let p=0;p<t.length;p+=3)this.tmpPoints.push(new s.Point2d(t[p],t[p+1],t[p+2]));const a=o?this.fullLayer:this.drawLayer||this.fullLayer,c=this.draw({workId:n,layer:a,isDrawing:!1,isDrawEraserlines:i===s.EvevtWorkState.Done});return this.oldRect=c,(h=this.vNodes)==null||h.setInfo(n,{rect:c,op:t,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:c&&W.getCenterPos(c,a)}),c}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var f;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,fillColor:a,toolsType:c,vertices:l,strokeType:h}=o,p=i.get(t.name),d=p==null?void 0:p.opt;let u=t;return t.tagName==="GROUP"&&(u=t.children[0]),n&&(u.setAttribute("strokeColor",n),d!=null&&d.strokeColor&&(d.strokeColor=n)),a&&(a==="transparent"?u.setAttribute("fillColor","rgba(0,0,0,0)"):u.setAttribute("fillColor",a),d!=null&&d.fillColor&&(d.fillColor=a)),c===s.EToolsKey.Polygon&&l&&(d.vertices=l),h&&(p!=null&&p.opt)&&((f=p.opt)!=null&&f.strokeType)&&(p.opt.strokeType=h),p&&i.setInfo(t.name,{...p,opt:d}),W.updateNodeOpt(e)}}class V{static bezier(r,e){const t=[];for(let o=0;o<e.length;o+=4){const i=e[o],n=e[o+1],a=e[o+2],c=e[o+3];i&&n&&a&&c?t.push(...V.getBezierPoints(r,i,n,a,c)):i&&n&&a?t.push(...V.getBezierPoints(r,i,n,a)):i&&n?t.push(...V.getBezierPoints(r,i,n)):i&&t.push(i)}return t}static getBezierPoints(r=10,e,t,o,i){let n=null;const a=[];!o&&!i?n=V.oneBezier:o&&!i?n=V.twoBezier:o&&i&&(n=V.threeBezier);for(let c=0;c<r;c++)n&&a.push(n(c/r,e,t,o,i));return i?a.push(i):o&&a.push(o),a}static oneBezier(r,e,t){const o=e.x+(t.x-e.x)*r,i=e.y+(t.y-e.y)*r;return new s.Vec2d(o,i)}static twoBezier(r,e,t,o){const i=(1-r)*(1-r)*e.x+2*r*(1-r)*t.x+r*r*o.x,n=(1-r)*(1-r)*e.y+2*r*(1-r)*t.y+r*r*o.y;return new s.Vec2d(i,n)}static threeBezier(r,e,t,o,i){const n=e.x*(1-r)*(1-r)*(1-r)+3*t.x*r*(1-r)*(1-r)+3*o.x*r*r*(1-r)+i.x*r*r*r,a=e.y*(1-r)*(1-r)*(1-r)+3*t.y*r*(1-r)*(1-r)+3*o.y*r*r*(1-r)+i.y*r*r*r;return new s.Vec2d(n,a)}}class Ye extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.SpeechBalloon);g(this,"ratio",.8);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){var y;const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,l=(y=t==null?void 0:t.workId)==null?void 0:y.toString();if(!l)return{type:s.EPostMessageType.None};const{op:h}=t,p=h==null?void 0:h.length;if(!p||p<2)return{type:s.EPostMessageType.None};let d;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(h[0],h[1])],d=!1):d=this.updateTempPoints(h),!d)return{type:s.EPostMessageType.None};let u;if(i||n||c){const m=o?this.fullLayer:this.drawLayer||this.fullLayer;u=this.draw({workId:l,layer:m,isDrawing:!0})}if(c){const m=s.computRect(u,this.oldRect);this.oldRect=u;const w={rect:m,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult};if(a){const P=Date.now();P-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=P,w.op=this.tmpPoints.map(T=>[...T.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!i&&a){const m=Date.now();return m-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=m,{type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:s.EPostMessageType.None}}const f=s.computRect(u,this.oldRect);return this.oldRect=u,{rect:f,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult}}consumeAll(){var a;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.fullLayer,o=this.draw({workId:e,layer:t,isDrawing:!1});this.oldRect=o;const i=this.tmpPoints.map(c=>[...c.XY,0]).flat(1),n=s.transformToSerializableData(i);return(a=this.vNodes)==null||a.setInfo(e,{rect:o,op:i,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:o&&W.getCenterPos(o,t)}),{rect:o,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:n,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawEraserlines:i=!0}=e,{strokeColor:n,fillColor:a,thickness:c,zIndex:l,placement:h,scale:p,rotate:d,translate:u,strokeType:f,eraserlines:y,lineDash:m,lineCap:w}=this.workOptions,{rect:P,pos:T,points:S}=this.computDrawPoints(c,h),E={points:S.map(L=>L.XY),lineWidth:c,fillColor:a!=="transparent"&&a||void 0,strokeColor:n,normalize:!0,className:`${T[0]},${T[1]}`,close:!0,lineCap:w,lineDash:f===s.EStrokeType.Dotted?[m&&m[0]||1,(m&&m[1]||2)*c]:f===s.EStrokeType.LongDotted?[(m&&m[0]||1)*c,(m&&m[1]||2)*c]:void 0},I={name:t,id:t,zIndex:l,pos:T,anchor:[.5,.5],size:[P.w,P.h]};p&&(I.scale=p),d&&(I.rotate=d),u&&(I.translate=u);const R=new D.Group(I),M=new D.Polyline({...E,pos:[0,0]});R.append(M),y&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:R,eraserlines:y,pos:T,layer:o},i)),this.replace(o,t,R);const v=R.getBoundingClientRect();return this.isDelete&&R.setAttribute("opacity",0),{x:Math.floor(v.x-W.SafeBorderPadding),y:Math.floor(v.y-W.SafeBorderPadding),w:Math.floor(v.width+W.SafeBorderPadding*2),h:Math.floor(v.height+W.SafeBorderPadding*2)}}transformControlPoints(e){const t=s.getRectFromPoints(this.tmpPoints);switch(e){case"bottom":case"bottomLeft":case"bottomRight":{const o=t.y+t.h*this.ratio;return[new s.Vec2d(t.x,t.y,0),new s.Vec2d(t.x+t.w,t.y,0),new s.Vec2d(t.x+t.w,o,0),new s.Vec2d(t.x,o,0)]}case"top":case"topLeft":case"topRight":{const o=t.y+t.h*(1-this.ratio);return[new s.Vec2d(t.x,o,0),new s.Vec2d(t.x+t.w,o,0),new s.Vec2d(t.x+t.w,t.y+t.h,0),new s.Vec2d(t.x,t.y+t.h,0)]}case"left":case"leftBottom":case"leftTop":{const o=t.x+t.w*(1-this.ratio);return[new s.Vec2d(o,t.y,0),new s.Vec2d(t.x+t.w,t.y,0),new s.Vec2d(t.x+t.w,t.y+t.h,0),new s.Vec2d(o,t.y+t.h,0)]}case"right":case"rightBottom":case"rightTop":{const o=t.x+t.w*this.ratio;return[new s.Vec2d(t.x,t.y,0),new s.Vec2d(o,t.y,0),new s.Vec2d(o,t.y+t.h,0),new s.Vec2d(t.x,t.y+t.h,0)]}}}computDrawPoints(e,t){const o=s.getRectFromPoints(this.tmpPoints),i=this.transformControlPoints(t),n=Math.floor(o.w*.1),a=Math.floor(o.h*.1),c=[],l=s.Vec2d.Add(i[0],new s.Vec2d(0,a,0)),h=s.Vec2d.Add(i[0],new s.Vec2d(n,0,0)),p=V.getBezierPoints(10,l,i[0],h),d=s.Vec2d.Sub(i[1],new s.Vec2d(n,0,0)),u=s.Vec2d.Add(i[1],new s.Vec2d(0,a,0)),f=V.getBezierPoints(10,d,i[1],u),y=s.Vec2d.Sub(i[2],new s.Vec2d(0,a,0)),m=s.Vec2d.Sub(i[2],new s.Vec2d(n,0,0)),w=V.getBezierPoints(10,y,i[2],m),P=s.Vec2d.Add(i[3],new s.Vec2d(n,0,0)),T=s.Vec2d.Sub(i[3],new s.Vec2d(0,a,0)),S=V.getBezierPoints(10,P,i[3],T),E=n*(1-this.ratio)*10,I=a*(1-this.ratio)*10;switch(t){case"bottom":{const v=s.Vec2d.Sub(i[2],new s.Vec2d(n*5-E/2,0,0)),L=s.Vec2d.Sub(i[2],new s.Vec2d(n*5,-I,0)),C=s.Vec2d.Sub(i[2],new s.Vec2d(n*5+E/2,0,0));c.push(L,C,...S,...p,...f,...w,v);break}case"bottomRight":{const v=s.Vec2d.Sub(i[2],new s.Vec2d(n*1.1,0,0)),L=s.Vec2d.Sub(i[2],new s.Vec2d(n*1.1+E/2,-I,0)),C=s.Vec2d.Sub(i[2],new s.Vec2d(n*1.1+E,0,0));c.push(L,C,...S,...p,...f,...w,v);break}case"bottomLeft":{const v=s.Vec2d.Add(i[3],new s.Vec2d(n*1.1+E,0,0)),L=s.Vec2d.Add(i[3],new s.Vec2d(n*1.1+E/2,I,0)),C=s.Vec2d.Add(i[3],new s.Vec2d(n*1.1,0,0));c.push(L,C,...S,...p,...f,...w,v);break}case"top":{const v=s.Vec2d.Sub(i[1],new s.Vec2d(n*5-E/2,0,0)),L=s.Vec2d.Sub(i[1],new s.Vec2d(n*5,I,0)),C=s.Vec2d.Sub(i[1],new s.Vec2d(n*5+E/2,0,0));c.push(L,v,...f,...w,...S,...p,C);break}case"topRight":{const v=s.Vec2d.Sub(i[1],new s.Vec2d(n*1.1,0,0)),L=s.Vec2d.Sub(i[1],new s.Vec2d(n*1.1+E/2,I,0)),C=s.Vec2d.Sub(i[1],new s.Vec2d(n*1.1+E,0,0));c.push(L,v,...f,...w,...S,...p,C);break}case"topLeft":{const v=s.Vec2d.Add(i[0],new s.Vec2d(n*1.1+E,0,0)),L=s.Vec2d.Add(i[0],new s.Vec2d(n*1.1+E/2,-I,0)),C=s.Vec2d.Add(i[0],new s.Vec2d(n*1.1,0,0));c.push(L,v,...f,...w,...S,...p,C);break}case"left":{const v=s.Vec2d.Add(i[0],new s.Vec2d(0,a*5-I/2,0)),L=s.Vec2d.Add(i[0],new s.Vec2d(-E,a*5,0)),C=s.Vec2d.Add(i[0],new s.Vec2d(0,a*5+I/2,0));c.push(L,v,...p,...f,...w,...S,C);break}case"leftTop":{const v=s.Vec2d.Add(i[0],new s.Vec2d(0,a*1.1,0)),L=s.Vec2d.Add(i[0],new s.Vec2d(-E,a*1.1+I/2,0)),C=s.Vec2d.Add(i[0],new s.Vec2d(0,a*1.1+I,0));c.push(L,v,...p,...f,...w,...S,C);break}case"leftBottom":{const v=s.Vec2d.Sub(i[3],new s.Vec2d(0,a*1.1+I,0)),L=s.Vec2d.Sub(i[3],new s.Vec2d(E,a*1.1+I/2,0)),C=s.Vec2d.Sub(i[3],new s.Vec2d(0,a*1.1,0));c.push(L,v,...p,...f,...w,...S,C);break}case"right":{const v=s.Vec2d.Add(i[1],new s.Vec2d(0,a*5-I/2,0)),L=s.Vec2d.Add(i[1],new s.Vec2d(E,a*5,0)),C=s.Vec2d.Add(i[1],new s.Vec2d(0,a*5+I/2,0));c.push(L,C,...w,...S,...p,...f,v);break}case"rightTop":{const v=s.Vec2d.Add(i[1],new s.Vec2d(0,a*1.1,0)),L=s.Vec2d.Add(i[1],new s.Vec2d(E,a*1.1+I/2,0)),C=s.Vec2d.Add(i[1],new s.Vec2d(0,a*1.1+I,0));c.push(L,C,...w,...S,...p,...f,v);break}case"rightBottom":{const v=s.Vec2d.Sub(i[2],new s.Vec2d(0,a*1.1+I,0)),L=s.Vec2d.Sub(i[2],new s.Vec2d(-E,a*1.1+I/2,0)),C=s.Vec2d.Sub(i[2],new s.Vec2d(0,a*1.1,0));c.push(L,C,...w,...S,...p,...f,v);break}}const R=s.getRectFromPoints(this.tmpPoints,e),M=[Math.floor(R.x+R.w/2),Math.floor(R.y+R.h/2)];return{rect:R,pos:M,points:c}}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n)||s.Point2d.Sub(i,o).XY.includes(0))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var l,h;const{op:t,isFullWork:o,workState:i=s.EvevtWorkState.Done}=e,n=(l=this.workId)==null?void 0:l.toString();if(!n)return;this.tmpPoints.length=0;for(let p=0;p<t.length;p+=3)this.tmpPoints.push(new s.Point2d(t[p],t[p+1],t[p+2]));const a=o?this.fullLayer:this.drawLayer||this.fullLayer,c=this.draw({workId:n,layer:a,isDrawing:!1,isDrawEraserlines:i===s.EvevtWorkState.Done});return this.oldRect=c,(h=this.vNodes)==null||h.setInfo(n,{rect:c,op:t,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:c&&W.getCenterPos(c,a)}),c}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var f;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,fillColor:a,toolsType:c,placement:l,strokeType:h}=o,p=i.get(t.name),d=p==null?void 0:p.opt;let u=t;return t.tagName==="GROUP"&&(u=t.children[0]),n&&(u.setAttribute("strokeColor",n),d!=null&&d.strokeColor&&(d.strokeColor=n)),a&&(a==="transparent"?u.setAttribute("fillColor","rgba(0,0,0,0)"):u.setAttribute("fillColor",a),d!=null&&d.fillColor&&(d.fillColor=a)),c===s.EToolsKey.SpeechBalloon&&l&&(d.placement=l),h&&(p!=null&&p.opt)&&((f=p.opt)!=null&&f.strokeType)&&(p.opt.strokeType=h),p&&i.setInfo(t.name,{...p,opt:d}),W.updateNodeOpt(e)}}var Lt=s._baseGetTag,bt=s.isObjectLike_1,Dt="[object Boolean]";function Nt(k){return k===!0||k===!1||bt(k)&&Lt(k)==Dt}var xt=Nt;const ee=s.getDefaultExportFromCjs(xt);class $e extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.both);g(this,"toolsType",s.EToolsKey.Straight);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"straightTipWidth");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.straightTipWidth=this.workOptions.thickness/2,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,l=this.workId,{op:h,syncUnitTime:p}=t,d=h==null?void 0:h.length;if(!d||d<2)return{type:s.EPostMessageType.None};p&&(this.syncUnitTime=p);let u;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(h[0],h[1])],u=!1):u=this.updateTempPoints(h),!u)return{type:s.EPostMessageType.None};let f;if(i||n||c){const m=o?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({workId:l,layer:m})}if(c){const m=s.computRect(f,this.oldRect);this.oldRect=f;const w={rect:m,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult};if(a){const P=Date.now();P-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=P,w.op=this.tmpPoints.map(T=>[...T.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!i&&a){const m=Date.now();return m-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=m,{type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:s.EPostMessageType.None}}const y=s.computRect(f,this.oldRect);return this.oldRect=f,{rect:y,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult}}consumeAll(){var a;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.fullLayer,o=this.draw({workId:e,layer:t});this.oldRect=o;const i=this.tmpPoints.map(c=>[...c.XY,0]).flat(1),n=s.transformToSerializableData(i);return(a=this.vNodes)==null||a.setInfo(e,{rect:o,op:i,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:o&&W.getCenterPos(o,t)}),{rect:o,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:n,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawEraserlines:i=!0}=e,{strokeColor:n,thickness:a,zIndex:c,scale:l,rotate:h,translate:p,strokeType:d,eraserlines:u,lineCap:f,lineDash:y}=this.workOptions,m=o.worldPosition,w=o.worldScaling,{d:P,rect:T,isDot:S}=this.computDrawPoints(a,d),E=[T.x+T.w/2,T.y+T.h/2],I=new D.Group({pos:E,anchor:[.5,.5],size:[T.w,T.h],name:t,id:t,normalize:!0,zIndex:c}),R={pos:[0,0],normalize:!0,d:P,fillColor:n,strokeColor:n,lineDash:S?void 0:d===s.EStrokeType.Dotted?[y&&y[0]||1,(y&&y[1]||2)*a]:d===s.EStrokeType.LongDotted?[(y&&y[0]||1)*a,(y&&y[1]||2)*a]:void 0,lineCap:S?void 0:f,lineWidth:d===s.EStrokeType.Normal||S?0:a};l&&(R.scale=l),h&&(R.rotate=h),p&&(R.translate=p);const M=new D.Path(R);I.append(M),u&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:I,eraserlines:u,pos:E,layer:o},i)),this.replace(o,t,I);let v={x:Math.floor(T.x*w[0]+m[0]-W.SafeBorderPadding),y:Math.floor(T.y*w[1]+m[1]-W.SafeBorderPadding),w:Math.floor(T.w*w[0]+2*W.SafeBorderPadding),h:Math.floor(T.h*w[1]+2*W.SafeBorderPadding)};if(h||l||p){const L=M.getBoundingClientRect();v={x:Math.floor(L.x-W.SafeBorderPadding),y:Math.floor(L.y-W.SafeBorderPadding),w:Math.floor(L.width+W.SafeBorderPadding*2),h:Math.floor(L.height+W.SafeBorderPadding*2)}}return this.isDelete&&I.setAttribute("opacity",0),v}computDrawPoints(e,t){return this.tmpPoints[1].distance(this.tmpPoints[0])>this.straightTipWidth?this.computFullPoints(e,t):this.computDotPoints(e)}computFullPoints(e,t){const o=s.Vec2d.Sub(this.tmpPoints[1],this.tmpPoints[0]).uni(),i=s.Vec2d.Per(o).mul(e/2),n=s.Point2d.Sub(this.tmpPoints[0],i),a=s.Point2d.Add(this.tmpPoints[0],i),c=s.Point2d.Sub(this.tmpPoints[1],i),l=s.Point2d.Add(this.tmpPoints[1],i),h=s.Point2d.GetSemicircleStroke(this.tmpPoints[1],c,-1,8),p=s.Point2d.GetSemicircleStroke(this.tmpPoints[0],a,-1,8),d=[n,c,...h,l,a,...p];let u;return t!==s.EStrokeType.Normal?u=Q(this.tmpPoints,!1):u=Q(d,!0),{d:u,rect:s.getRectFromPoints(d),isDot:!1,pos:this.tmpPoints[0].XY}}computDotPoints(e){const t=s.Point2d.GetDotStroke(this.tmpPoints[0],e/2,8);return{d:Q(t,!0),rect:s.getRectFromPoints(t),isDot:!0,pos:this.tmpPoints[0].XY}}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var l,h;const{op:t,isFullWork:o,workState:i=s.EvevtWorkState.Done}=e,n=(l=this.workId)==null?void 0:l.toString();if(!n)return;this.tmpPoints.length=0;for(let p=0;p<t.length;p+=3)this.tmpPoints.push(new s.Point2d(t[p],t[p+1],t[p+2]));const a=o?this.fullLayer:this.drawLayer||this.fullLayer,c=this.draw({workId:n,layer:a,isDrawEraserlines:i===s.EvevtWorkState.Done});return this.oldRect=c,(h=this.vNodes)==null||h.setInfo(n,{rect:c,op:t,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:c&&W.getCenterPos(c,a)}),c}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var l,h;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,strokeType:a}=o,c=i.get(t.name);return n&&(t.tagName==="GROUP"?t.children.forEach(p=>{p.setAttribute("strokeColor",n),p.getAttribute("fillColor")&&p.setAttribute("fillColor",n)}):(t.setAttribute("strokeColor",n),t.setAttribute("fillColor",n)),(l=c==null?void 0:c.opt)!=null&&l.strokeColor&&(c.opt.strokeColor=n)),a&&(c!=null&&c.opt)&&((h=c.opt)!=null&&h.strokeType)&&(c.opt.strokeType=a),c&&i.setInfo(t.name,c),W.updateNodeOpt(e)}}const Z=class Z extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.proportional);g(this,"toolsType",s.EToolsKey.Text);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");this.workOptions=e.toolsOpt}consume(){return{type:s.EPostMessageType.None}}consumeAll(){return{type:s.EPostMessageType.None}}consumeService(){}async draw(e){const{workId:t,layer:o,isDrawLabel:i,boxRect:n}=e,{boxSize:a,boxPoint:c,zIndex:l}=this.workOptions,h=o.worldPosition,p=o.worldScaling;if(!c||!a)return;const d={name:t,id:t,pos:[c[0]-s.TextBoxPadding,c[1]-s.TextBoxPadding],anchor:[0,0],size:a,zIndex:l},u=new D.Group(d),f={x:c[0]-s.TextBoxPadding,y:c[1]-s.TextBoxPadding,w:a[0],h:a[1]},y={x:Math.floor(f.x*p[0]+h[0]),y:Math.floor(f.y*p[1]+h[1]),w:Math.floor(f.w*p[0])+2,h:Math.floor(f.h*p[1])+2};this.replace(o,t,u);let m;if(n&&(m=s.getRectMatrixrRelation(y,n)),(i||n&&m!==s.EMatrixrRelationType.outside)&&o&&this.workOptions.text){const w=await Z.createLabels(this.workOptions,o,y),{labels:P,maxWidth:T}=w;u.append(...P),y.w=Math.ceil(Math.max(T*o.worldScaling[0],y.w))}return y}async consumeServiceAsync(e){var h,p,d,u;const t=(h=this.workId)==null?void 0:h.toString();if(!t)return;const{isFullWork:o,replaceId:i,isDrawLabel:n,boxRect:a}=e;this.oldRect=i&&((d=(p=this.vNodes)==null?void 0:p.get(i))==null?void 0:d.rect)||void 0;const c=o?this.fullLayer:this.drawLayer||this.fullLayer,l=await this.draw({workId:t,layer:c,isDrawLabel:typeof n>"u"&&this.workOptions.workState===s.EvevtWorkState.Done||n,boxRect:a});return(u=this.vNodes)==null||u.setInfo(t,{rect:l,op:[],opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:l&&W.getCenterPos(l,c)}),l}updataOptService(){}async updataOptServiceAsync(e,t,o){var y,m;if(!this.workId)return;const i=this.workId.toString(),{fontColor:n,fontBgColor:a,bold:c,italic:l,lineThrough:h,underline:p,zIndex:d}=e,u=(y=this.vNodes)==null?void 0:y.get(i);if(!u)return;n&&(u.opt.fontColor=n),a&&(u.opt.fontBgColor=a),c&&(u.opt.bold=c),l&&(u.opt.italic=l),ee(h)&&(u.opt.lineThrough=h),ee(p)&&(u.opt.underline=p),A(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===s.EvevtWorkState.Done||t,boxRect:o});return(m=this.vNodes)==null||m.setInfo(i,{rect:f,op:[],opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:f&&W.getCenterPos(f,this.fullLayer)}),f}clearTmpPoints(){this.tmpPoints.length=0}static getSafetySnippetRatio(e){const t=(e==null?void 0:e.parent).displayRatio||1,o=Math.ceil(e.worldScaling[0]*10)/10;let i=o;return o<=2?i=o*t*2:o>2&&o<=3?i=o*t*1.6:o>3&&o<=4?i=o*t*1.2:o>4&&(i=o*t),Math.floor(i*1e3)/1e3}static getSafetySnippetFontLength(e){return Math.floor(Z.textImageSnippetSize*3/4/e)||1}static async createLabels(e,t,o){var C;const i=[],{x:n,y:a}=o,{width:c,height:l}=(C=t.parent)==null?void 0:C.parent,h=s.transformToNormalData(e.text),p=h.length,{fontSize:d,lineHeight:u,bold:f,textAlign:y,italic:m,fontFamily:w,verticalAlign:P,fontColor:T,fontBgColor:S,underline:E,lineThrough:I}=e,R=Z.getSafetySnippetRatio(t)||1,M=Math.floor(d*R),v=Z.getSafetySnippetFontLength(M);let L=0;for(let b=0;b<p;b++){const N=h[b],x=u||M*1.5;if(N){const O=s.strlen(N),F=[0,0],z=[0,d*1.2];P==="middle"&&(F[1]=Math.floor(b*d*1.2+6+d*(1.1-1)));const X=[0,Math.floor(-d*.15)];F[0]=6;const J=Math.sin(Math.PI/180*20);let q=0;const ce=[];let re=0;for(;re<O;){y==="left"&&(X[0]=q),re===0&&m==="italic"&&(X[0]=X[0]-J/2*d);const we=N.slice(re,re+v),le={anchor:[0,0],pos:X,text:we,fontFamily:w,fontSize:M,lineHeight:x,strokeColor:T,fontWeight:f,fillColor:T,textAlign:y,fontStyle:m,scale:[1/R,1/R]},j=new D.Label(le),Y=await j.textImageReady;let he=!0;if(Y){const Ee=Y.rect&&Y.rect[2],Ie=Y.rect&&Y.rect[3];if(Ee&&Ie){const We=Ee/R,Qe=Ie/R;q=We+q,m==="italic"&&(f==="bold"?q=q-J*d*1.2:q=q-J*d),((X[0]+F[0]+We)*t.worldScaling[0]+n<=0||(X[0]+F[0])*t.worldScaling[0]+n>=c||(X[1]+F[1]+Qe)*t.worldScaling[1]+a<=0||(X[1]+F[1])*t.worldScaling[1]+a>=l)&&(j.disconnect(),he=!1),he&&ce.push(j)}}re+=v}z[0]=q,m==="italic"&&(z[0]=z[0]+J*d),L=Math.max(L,z[0]);let ve=!0;if(((F[0]+z[0])*t.worldScaling[0]+n<=0||F[0]*t.worldScaling[0]+n>=c||(F[1]+z[1])*t.worldScaling[0]+a<=0||F[1]*t.worldScaling[1]+a>=l)&&(ve=!1),ve){if(E){const j=Math.floor(d/10),Y={normalize:!1,pos:[0,d*1.1+j/2],lineWidth:j,points:[0,0,Math.ceil(z[0]),0],strokeColor:T,className:"underline"},he=new D.Polyline(Y);ce.push(he)}if(I){const j={normalize:!1,pos:[0,d*1.2/2],lineWidth:Math.floor(d/10),points:[0,0,Math.ceil(z[0]),0],strokeColor:T,className:"lineThrough"},Y=new D.Polyline(j);ce.push(Y)}const we={pos:F,anchor:[0,0],size:z,bgcolor:S},le=new D.Group(we);le.append(...ce),i.push(le)}}}return{labels:i,maxWidth:L}}static updateNodeOpt(e){const{node:t,opt:o,vNodes:i,targetNode:n}=e,{fontBgColor:a,fontColor:c,translate:l,originPoint:h,scenePoint:p,scale:d,bold:u,italic:f,lineThrough:y,underline:m,fontSize:w,textInfos:P,zIndex:T}=o,S=n&&s.cloneDeep(n)||i.get(t.name);if(!S||!t.parent)return;const I=S.opt;if(A(T)&&(t.setAttribute("zIndex",T),S.opt.zIndex=T),c&&I.fontColor&&(I.fontColor=c,t.children.forEach(R=>{R.tagName==="GROUP"&&R.children.forEach(M=>{M.tagName==="LABEL"?(M.setAttribute("fillColor",c),M.setAttribute("strokeColor",c)):M.tagName==="POLYLINE"&&M.setAttribute("strokeColor",c)})})),a&&I.fontBgColor&&(I.fontBgColor=a,t.children.forEach(R=>{R.tagName==="GROUP"&&R.setAttribute("bgcolor",a)})),u&&(I.bold=u),f&&(I.italic=f),ee(y)&&(I.lineThrough=y),ee(m)&&(I.underline=m),w&&(I.fontSize=w),h&&p&&d&&n&&I.boxPoint){const R=P==null?void 0:P.get(t.name);if(R){const{fontSize:C,boxSize:b}=R;I.boxSize=b||I.boxSize,I.fontSize=C||I.fontSize}const M=[I.boxPoint[0],I.boxPoint[1]];s.scalePoints(I.boxPoint,p,d,l);const v=[S.op[0],S.op[1]],L=[v[0]-M[0],v[1]-M[1]];if(S.centerPos=[S.centerPos[0]+L[0],S.centerPos[1]+L[1]],S.opt.eraserlines)for(const C of S.opt.eraserlines){const{op:b,thickness:N}=C;C.thickness=Math.round(N*Math.max(d[0],d[1]));for(let x=0;x<b.length;x++)s.scaleLitePoints(b[x],p,d,l)}}else if(l&&I.boxPoint&&(I.boxPoint=[Math.round(I.boxPoint[0]+l[0]),Math.round(I.boxPoint[1]+l[1])],S.centerPos=[S.centerPos[0]+l[0],S.centerPos[1]+l[1]],S.opt.eraserlines))for(const R of S.opt.eraserlines){const{op:M}=R;for(let v=0;v<M.length;v++){const L=M[v].map((C,b)=>b%2?C+l[1]:C+l[0]);R.op[v]=L}}return S&&i.setInfo(t.name,S),S==null?void 0:S.rect}static getRectFromLayer(e,t){const o=e.getElementsByName(t)[0];if(o){const i=o.getBoundingClientRect();let n={x:Math.floor(i.x),y:Math.floor(i.y),w:Math.floor(i.width+2),h:Math.floor(i.height+2)};return o.children.forEach(a=>{if(a.tagName==="GROUP"){const c=o.getBoundingClientRect();n=s.computRect(n,{x:Math.floor(c.x),y:Math.floor(c.y),w:Math.floor(c.width+2),h:Math.floor(c.height+2)})}}),n}}};g(Z,"textImageSnippetSize",1024*4),g(Z,"SafeBorderPadding",30);let fe=Z;const se=class se extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.none);g(this,"toolsType",s.EToolsKey.PencilEraser);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"worldPosition");g(this,"worldScaling");g(this,"eraserRect");g(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:s.EPostMessageType.None};const{thickness:o}=this.workOptions,{width:i,height:n}=se.eraserSizes[o],a=Math.round(Math.max(i,n)/this.worldScaling[0]),{rect:c,willNewNodes:l,willDeleteNodes:h}=this.getChangeNodes(a,t);return{...this.baseConsumeResult,type:s.EPostMessageType.DrawWork,rect:c,willNewNodes:l,willDeleteNodes:h}}consumeService(){}updateTempPoints(e,t){let o=this.tmpPoints.length-1;const i=Date.now();for(let n=0;n<e.length;n+=2){const a=this.tmpPoints.length,c=new s.Point2d(e[n],e[n+1]);if(a===0){this.tmpPoints.push(c);continue}const l=a-1,h=this.tmpPoints[l],p=s.Vec2d.Sub(c,h).uni();c.isNear(h,t/2)||(s.Vec2d.Equals(p,h.v,.02)&&(this.tmpPoints.pop(),o--),c.setv(p),c.setT(i),this.tmpPoints.push(c))}return Math.max(o,0)}getChangeNodes(e,t){const o=new Map,i=new Set;let n;if(!this.vNodes)return{willDeleteNodes:i,willNewNodes:o,rect:n};const a=this.worldPosition;let c=s.getRectFromPoints(this.tmpPoints,e);c=s.getRectTranslated(c,a),c=s.getRectScaleWPoint(c,this.worldScaling,a);const{nodeRange:l}=this.vNodes.getRectIntersectRange(c);if(!l.size)return{willDeleteNodes:i,willNewNodes:o,rect:n};const h=this.computEraserPointLines(this.tmpPoints,90);for(const[p,d]of l.entries()){if(d.toolsType!==s.EToolsKey.Pencil)continue;let u=s.getRectTranslated(d.rect,[-a[0],-a[1]]);u={x:u.x/this.worldScaling[0],y:u.y/this.worldScaling[1],w:u.w/this.worldScaling[0],h:u.h/this.worldScaling[1]};const f=[u.x,u.y,u.x+u.w,u.y+u.h],y=[],m=[];for(const w of h)Se.polyline(w,f,y);if(y.length&&d.opt)for(const w of y){const P=[];for(let T=0;T<w.length;T++)T!==0&&H(w[T],w[T-1])||P.push(...w[T]);m.push(P)}if(m.length&&t){const w=t.createWorkShapeNode({workId:p,toolsType:s.EToolsKey.Pencil,toolsOpt:d.opt});w.setWorkId(p);const P=w==null?void 0:w.computPencilPoints({op:d.op,eraserPolylines:m,eraserThickness:e});if(n=s.computRect(n,d.rect),i.add(p),t.removeNode(p),P.length)for(let T=0;T<P.length;T++){const S=`${p}_${Date.now()}_${T}`,E=S,R=t.setFullWork({workId:E,opt:d.opt,toolsType:s.EToolsKey.Pencil}).consumeService({op:P[T],isFullWork:!0,replaceId:E});t.clearWorkShapeNodeCache(E),n=s.computRect(n,R),o.set(S,{...d,toolsType:s.EToolsKey.Pencil,rect:R??d.rect,name:S,op:P[T]})}}}return{willNewNodes:o,willDeleteNodes:i,rect:n}}consume(e){const{data:t}=e,{op:o}=t;if(!o||o.length===0)return{type:s.EPostMessageType.None,...this.baseConsumeResult};const{thickness:i}=this.workOptions,{width:n,height:a}=se.eraserSizes[i],c=Math.max(n,a)/this.worldScaling[0];return this.updateTempPoints(o,c),{type:s.EPostMessageType.None,...this.baseConsumeResult}}computEraserPointLines(e,t=60){const o=[];let i=0;const n=360-t;for(let a=1;a<e.length;a++){const c=e[a-1],l=e[a];if(a===1&&i===0&&(o[i]=[c.XY]),o[i].push(l.XY),a<e.length-1){const h=l.getAngleByPoints(c,e[a+1]);(h<t||h>n)&&(i++,o[i]=[l.XY])}}return e.length===1&&o.length===0&&o.push([e[0].XY,e[0].XY]),o}consumeAll(e){const{workerEngine:t}=e;if(this.replace(this.fullLayer,this.workId),!this.tmpPoints.length)return{type:s.EPostMessageType.None,...this.baseConsumeResult};const{thickness:o}=this.workOptions,{width:i,height:n}=se.eraserSizes[o],a=Math.max(i,n)/this.worldScaling[0],{willDeleteNodes:c,willNewNodes:l,rect:h}=this.getChangeNodes(a,t);if(c.size===0&&l.size===0)return{type:s.EPostMessageType.FullWork,...this.baseConsumeResult,rect:h};const p=[];this.tmpPoints.map(u=>{p.push(u.x,u.y,a/2)});const d=s.transformToSerializableData(p);return{...this.baseConsumeResult,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,rect:h,ops:d,willNewNodes:l,willDeleteNodes:c}}clearTmpPoints(){this.tmpPoints.length=0}};g(se,"eraserSizes",Pe);let ye=se;const oe=class oe extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.none);g(this,"toolsType",s.EToolsKey.BitMapEraser);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"worldPosition");g(this,"worldScaling");g(this,"eraserRect");g(this,"eraserPolyline");g(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:s.EPostMessageType.None};const{thickness:o}=this.workOptions,{width:i,height:n}=oe.eraserSizes[o],a=Math.round(Math.max(i,n)/this.worldScaling[0]),{willDeleteNodes:c,rect:l}=this.getChangeNodes(a,t);return{...this.baseConsumeResult,type:s.EPostMessageType.DrawWork,rect:l,willDeleteNodes:c}}consumeService(){}updateTempPoints(e,t){let o=this.tmpPoints.length-1;const i=Date.now();for(let n=0;n<e.length;n+=2){const a=this.tmpPoints.length,c=new s.Point2d(e[n],e[n+1]);if(a===0){this.tmpPoints.push(c);continue}const l=a-1,h=this.tmpPoints[l],p=s.Vec2d.Sub(c,h).uni();c.isNear(h,t/2)||(s.Vec2d.Equals(p,h.v,.02)&&(this.tmpPoints.pop(),o--),c.setv(p),c.setT(i),this.tmpPoints.push(c))}return Math.max(o,0)}getChangeNodes(e,t){const o=new Map,i=new Set;let n;if(!this.vNodes)return{willDeleteNodes:i,willUpdateNodes:o,rect:n};const a=this.worldPosition;let c=s.getRectFromPoints(this.tmpPoints,e);c=s.getRectTranslated(c,a),c=s.getRectScaleWPoint(c,this.worldScaling,a);const{nodeRange:l}=this.vNodes.getRectIntersectRange(c);if(!l.size)return{willDeleteNodes:i,willUpdateNodes:o,rect:n};const h=this.computEraserPointLines(this.tmpPoints,120);for(const[p,d]of l.entries()){if(d.toolsType===s.EToolsKey.Text||d.toolsType===s.EToolsKey.Image)continue;let u=s.getRectTranslated(d.rect,[-a[0],-a[1]]);u={x:u.x/this.worldScaling[0],y:u.y/this.worldScaling[1],w:u.w/this.worldScaling[0],h:u.h/this.worldScaling[1]};const f=[u.x,u.y,u.x+u.w,u.y+u.h],y=[],m=[];for(const T of h)Se.polyline(T,f,y);if(y.length&&d.opt)for(const T of y){const S=[];for(let E=0;E<T.length&&!(isNaN(T[E][0])||isNaN(T[E][1]));E++){const I=T[E].map(R=>Math.round(R*100)/100);if(E!==0){const R=T[E-1].map(M=>Math.round(M*100)/100);if(H(I,R))continue}S.push(...I)}S.length!==0&&m.push(S)}if(!m.length)continue;const w=d.opt.eraserlines?d.opt.eraserlines:[],P=w.find(T=>T.workId===this.workId);if(P?P.op=m:w.push({workId:this.workId,op:m,thickness:e}),d.opt.eraserlines=w,t){const{toolsType:T}=d,S=t.createWorkShapeNode({workId:p,toolsType:T,toolsOpt:d.opt});S==null||S.setWorkId(p);const E=S==null?void 0:S.consumeService({op:d.op,isFullWork:!0,replaceId:p});if(n=s.computRect(n,E),S!=null&&S.isDelete){t.removeNode(p),i.add(p);continue}}o.set(p,d)}return{willUpdateNodes:o,willDeleteNodes:i,rect:n}}consume(e){const{data:t}=e,{op:o}=t;if(!o||o.length===0)return{type:s.EPostMessageType.None,...this.baseConsumeResult};const{thickness:i}=this.workOptions,{width:n,height:a}=oe.eraserSizes[i],c=Math.round(Math.max(n,a)/this.worldScaling[0]);return this.updateTempPoints(o,c),{type:s.EPostMessageType.None,...this.baseConsumeResult}}computEraserPointLines(e,t=60){const o=[];let i=0;const n=360-t;for(let a=1;a<e.length;a++){const c=e[a-1],l=e[a];if(a===1&&i===0&&(o[i]=[c.XY]),o[i].push(l.XY),a<e.length-1){const h=l.getAngleByPoints(c,e[a+1]);(h<t||h>n)&&(i++,o[i]=[l.XY])}}return e.length===1&&o.length===0&&o.push([e[0].XY,e[0].XY]),o}consumeAll(e){const{workerEngine:t}=e;if(this.replace(this.fullLayer,this.workId),!this.tmpPoints.length)return{type:s.EPostMessageType.None,...this.baseConsumeResult};const{thickness:o}=this.workOptions,{width:i,height:n}=oe.eraserSizes[o],a=Math.round(Math.max(i,n)/this.worldScaling[0]),{willDeleteNodes:c,willUpdateNodes:l,rect:h}=this.getChangeNodes(a,t);if(c.size===0&&l.size===0)return{type:s.EPostMessageType.FullWork,...this.baseConsumeResult,rect:h};const p=[];this.tmpPoints.map(u=>{p.push(u.x,u.y,a/2)});const d=s.transformToSerializableData(p);return{...this.baseConsumeResult,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,rect:h,ops:d,willUpdateNodes:l,willDeleteNodes:c}}clearTmpPoints(){this.tmpPoints.length=0}};g(oe,"eraserSizes",Pe);let me=oe;class ne extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.BackgroundSVG);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");this.workOptions=e.toolsOpt,this.scaleType=ne.getScaleType(this.workOptions)}consume(){return{type:s.EPostMessageType.None}}consumeAll(){return{type:s.EPostMessageType.None}}draw(e){const{layer:t,workId:o,replaceId:i}=e,{centerX:n,centerY:a,width:c,height:l,rotate:h,zIndex:p}=this.workOptions,d=(p||0)-100,u=new D.Group({anchor:[.5,.5],pos:[n,a],name:o,size:[c,l],zIndex:d,rotate:h}),f=new D.Rect({normalize:!0,pos:[0,0],width:c,height:l});u.appendChild(f),this.replace(t,i||o,u);const y=u.getBoundingClientRect();if(y)return{x:Math.floor(y.x-W.SafeBorderPadding),y:Math.floor(y.y-W.SafeBorderPadding),w:Math.floor(y.width+W.SafeBorderPadding*2),h:Math.floor(y.height+W.SafeBorderPadding*2)}}consumeService(e){var n;const{replaceId:t}=e,o=this.workId,i=this.draw({workId:o,layer:this.fullLayer,replaceId:t});return(n=this.vNodes)==null||n.setInfo(o,{rect:i,op:[],opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:i&&W.getCenterPos(i,this.fullLayer)}),i}clearTmpPoints(){this.tmpPoints.length=0}static getScaleType(e){const{uniformScale:t,rotate:o}=e;return t!==!1||o&&Math.abs(o)%90>0?s.EScaleType.proportional:s.EScaleType.all}static updateNodeOpt(e){const{node:t,opt:o,vNodes:i,targetNode:n}=e,{translate:a,originPoint:c,scenePoint:l,scale:h,angle:p,isLocked:d,zIndexForBackgroundSVG:u}=o,f=n&&s.cloneDeep(n)||i.get(t.name);if(!f)return;const y=t.parent;if(y){if(A(u)){const m=(u||0)-100;t.setAttribute("zIndex",m),f.opt.zIndex=u}if(ee(d)&&(f.opt.locked=d),c&&l&&h&&a){const{centerX:m,centerY:w,width:P,height:T,uniformScale:S}=f.opt,E=S!==!1?[h[0],h[0]]:h,I=[m,w],R=[m,w];s.scalePoints(R,l,E,a);const M=[R[0]-I[0],R[1]-I[1]];f.centerPos=[f.centerPos[0]+M[0],f.centerPos[1]+M[1]],f.opt.width=Math.round(P*E[0]),f.opt.height=Math.round(T*E[1]),f.opt.centerX=R[0],f.opt.centerY=R[1]}else if(a)f.opt.centerX=f.opt.centerX+a[0],f.opt.centerY=f.opt.centerY+a[1],f.centerPos=[f.centerPos[0]+a[0],f.centerPos[1]+a[1]];else if(A(p))if(f.opt.rotate=p,f.scaleType=ne.getScaleType(f.opt),n){const m=s.getRectRotated(f.rect,p);f.rect=m}else{const m=W.getRectFromLayer(y,t.name);f.rect=m||f.rect}return f&&i.setInfo(t.name,f),f==null?void 0:f.rect}}}function He(k){switch(k){case s.EToolsKey.Arrow:return Ke;case s.EToolsKey.Pencil:return Fe;case s.EToolsKey.Straight:return $e;case s.EToolsKey.Ellipse:return Ve;case s.EToolsKey.Polygon:case s.EToolsKey.Triangle:return Xe;case s.EToolsKey.Star:case s.EToolsKey.Rhombus:return Ge;case s.EToolsKey.Rectangle:return _e;case s.EToolsKey.SpeechBalloon:return Ye;case s.EToolsKey.Text:return fe;case s.EToolsKey.LaserPen:return ze;case s.EToolsKey.Eraser:return de;case s.EToolsKey.PencilEraser:return ye;case s.EToolsKey.BitMapEraser:return me;case s.EToolsKey.Selector:return ue;case s.EToolsKey.Image:return ae;case s.EToolsKey.BackgroundSVG:return ne}}function ie(k,r){const{toolsType:e,...t}=k;switch(e){case s.EToolsKey.Arrow:return new Ke(t);case s.EToolsKey.Pencil:return new Fe(t);case s.EToolsKey.Straight:return new $e(t);case s.EToolsKey.Ellipse:return new Ve(t);case s.EToolsKey.Polygon:case s.EToolsKey.Triangle:return new Xe(t);case s.EToolsKey.Star:case s.EToolsKey.Rhombus:return new Ge(t);case s.EToolsKey.Rectangle:return new _e(t);case s.EToolsKey.SpeechBalloon:return new Ye(t);case s.EToolsKey.Text:return new fe(t);case s.EToolsKey.LaserPen:return new ze(t);case s.EToolsKey.Eraser:return new de(t,r);case s.EToolsKey.BitMapEraser:return new me(t);case s.EToolsKey.PencilEraser:return new ye(t);case s.EToolsKey.Selector:return t.vNodes?new ue({...t,vNodes:t.vNodes,drawLayer:t.fullLayer}):void 0;case s.EToolsKey.Image:return new ae(t);case s.EToolsKey.BackgroundSVG:return new ne({...t,toolsOpt:t.toolsOpt});default:return}}function qe(k){const r=[],e=["PATH","SPRITE","POLYLINE","RECT","ELLIPSE","LABEL","SPRITESVG"];for(const t of k){if(t.tagName==="GROUP"&&t.children.length){const o=qe(t.children);r.push(...o)}t.tagName&&e.includes(t.tagName)&&r.push(t)}return r}class je{constructor(r,e){g(this,"viewId");g(this,"scene");g(this,"fullLayer");g(this,"curNodeMap");g(this,"targetNodeMap",[]);g(this,"highLevelIds");g(this,"canClearUids");g(this,"localUid");this.viewId=r,this.scene=e,this.curNodeMap=new Me.ObserverMap}init(r){this.fullLayer=r}get(r){return this.curNodeMap.get(r)}setLocalUid(r){this.localUid=r}getLocalUid(){return this.localUid}setCanClearUids(r){this.canClearUids=r}getCanClearUids(){return this.canClearUids}getCanEraserNodes(r,e){const t=new Map;for(const[o,i]of r.entries())i.toolsType!==s.EToolsKey.BackgroundSVG&&(i.toolsType===s.EToolsKey.Image&&i.opt.type===s.EImageType.Image&&(i.opt.locked||e&&e.disableEraseImage)||i.toolsType===s.EToolsKey.Text&&(i.opt.workState===s.EvevtWorkState.Doing||i.opt.workState===s.EvevtWorkState.Start||e&&e.disableEraseText)||this.isCanClearWorkId(o)&&t.set(o,i));return t}getNodesByType(r){const e=new Map;return this.curNodeMap.forEach((t,o)=>{t.toolsType===r&&e.set(o,t)}),e}gethasEraserNodes(){const r=new Map;return this.curNodeMap.forEach((e,t)=>{var o;(o=e.opt.eraserlines)!=null&&o.length&&r.set(t,e)}),r}hasRenderNodes(){return!0}has(r){return this.curNodeMap.has(r)}setInfo(r,e){const t=this.curNodeMap.get(r)||{name:r,rect:e.rect};e.rect&&(t.rect=s.cloneDeep(e.rect)),e.op&&s.checkOp(e.op)&&(t.op=s.cloneDeep(e.op)),e.canRotate&&(t.canRotate=e.canRotate),e.scaleType&&(t.scaleType=e.scaleType),e.opt&&(t.opt=s.cloneDeep(e.opt)),e.toolsType&&(t.toolsType=e.toolsType),e.centerPos&&(t.centerPos=s.cloneDeep(e.centerPos)),ee(e.isSelected)&&(t.isSelected=e.isSelected),t.rect?this.curNodeMap.set(r,t):this.curNodeMap.delete(r)}selected(r){this.setInfo(r,{isSelected:!0})}unSelected(r){this.setInfo(r,{isSelected:!1})}delete(r){this.curNodeMap.delete(r)}clear(){this.curNodeMap.clear(),this.targetNodeMap.length=0}getRectIntersectRange(r,e=!0,t=!0){let o;const i=new Map;for(const[n,a]of this.curNodeMap.entries())if(s.isIntersect(r,a.rect)){if(e&&a.toolsType===s.EToolsKey.Image&&a.opt.locked||t&&a.toolsType===s.EToolsKey.Text&&(a.opt.workState===s.EvevtWorkState.Doing||a.opt.workState===s.EvevtWorkState.Start))continue;o=s.computRect(o,a.rect),i.set(n,a)}return{rectRange:o,nodeRange:i}}getNodeRectFormShape(r,e){const t=He(e.toolsType);return this.fullLayer&&(t==null?void 0:t.getRectFromLayer(this.fullLayer,r))}updateNodeRect(r){const e=this.curNodeMap.get(r);if(e){const t=this.getNodeRectFormShape(r,e);if(!t){this.curNodeMap.delete(r);return}e.rect=t,this.curNodeMap.set(r,e)}}updateHighLevelNodesRect(r){this.highLevelIds=r;for(const e of this.highLevelIds.keys())this.updateNodeRect(e)}updateLowLevelNodesRect(){var r;for(const e of this.curNodeMap.keys())(r=this.highLevelIds)!=null&&r.has(e)||this.updateNodeRect(e)}clearHighLevelIds(){this.highLevelIds=void 0}setTargetAssignKeys(r){const e=new Me.ObserverMap;for(const t of r){const o=this.curNodeMap.get(t);o&&e.set(t,s.cloneDeep(o))}return this.targetNodeMap.push(s.cloneDeep(e)),this.targetNodeMap.length-1}setTarget(){const r=this.curNodeMap.keys();return this.setTargetAssignKeys(Array.from(r))}getLastTarget(){return this.targetNodeMap[this.targetNodeMap.length-1]}deleteLastTarget(){this.targetNodeMap.length&&(this.targetNodeMap.length=this.targetNodeMap.length-1)}getTarget(r){return this.targetNodeMap[r]}deleteTarget(r){this.targetNodeMap.length=r}clearTarget(){this.targetNodeMap.length=0}isLocalWorkId(r){return r.split(s.Storage_Splitter).length===1}isCanClearWorkId(r){if(this.canClearUids===void 0||this.canClearUids===!0)return!0;if(s.isSet(this.canClearUids)){const e=r.split(s.Storage_Splitter);if(e.length===1)return this.canClearUids.has("localSelf")||this.localUid&&this.canClearUids.has(this.localUid);if(e.length===2)return this.canClearUids.has(e[0])}return!1}}class Ot{constructor(r,e,t){g(this,"viewId");g(this,"type");g(this,"scene");g(this,"fullLayer");g(this,"vNodes");g(this,"dpr");g(this,"contextType");g(this,"opt");g(this,"cameraOpt");g(this,"isSafari",!1);g(this,"combinePostMsg",new Set);g(this,"workerTaskId");g(this,"protectedTask");g(this,"delayPostDoneResolve");g(this,"cacheImages",new Map);g(this,"imageResolveMap",new Map);g(this,"taskUpdateCameraId");g(this,"debounceUpdateCameraId");var o,i;if(this.viewId=r,this.type=t,this.opt=e,this.dpr=e.dpr,this.contextType=this.getSupportContextType(t,(o=e==null?void 0:e.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(e),this.createRenderLayer(this.scene,e)}catch(n){if(this.contextType!=="2d")this.contextType="2d",(i=this.scene)==null||i.disconnect(),this.scene=this.createScene(e),this.createRenderLayer(this.scene,e);else throw n}this.vNodes=new je(r,this.scene)}createRenderLayer(r,e){const t=s.DefaultAppliancePluginExtrasOptions.bufferSize.full,o=s.DefaultAppliancePluginExtrasOptions.bufferSize.sub;this.fullLayer=this.createLayer("fullLayer",r,{...e.layerOpt,bufferSize:this.viewId===s.Main_View_Id?t:o*2})}async updateDpr(r){this.dpr=r,this.scene.displayRatio=r}getCachedImages(r){var e;return(e=this.cacheImages.get(r))==null?void 0:e.imageBitmap}getCachedImagesByWorkId(r){for(const[e,t]of this.cacheImages.entries())if(e===r&&t.imageBitmap)return t.imageBitmap}deleteCachedImagesByWorkId(r,e){for(const[t,o]of this.cacheImages.entries())e&&t===e||o.workId===r&&(o.imageBitmap.close(),this.cacheImages.delete(t))}clearCacheImages(){this.cacheImages.forEach(r=>r.imageBitmap.close()),this.cacheImages.clear()}clearImageResolveMap(){this.imageResolveMap.forEach(({timer:r})=>{r&&clearTimeout(r)}),this.imageResolveMap.clear()}setIsSafari(r){this.isSafari=r}async on(r){const{msgType:e,toolsType:t,opt:o,imageSrc:i,imageBitmap:n,workId:a,dpr:c}=r;switch(e){case s.EPostMessageType.UpdateDpr:{A(c)&&await this.updateDpr(c);break}case s.EPostMessageType.UpdateCamera:{await this.updateCamera(r);break}case s.EPostMessageType.Destroy:{this.destroy();break}case s.EPostMessageType.Clear:{await this.clearAll();break}case s.EPostMessageType.UpdateTools:{if(t&&o){const l={toolsType:t,toolsOpt:o,combineUnitTime:r.combineUnitTime,maxCombineEraserTime:r.maxCombineEraserTime};this.localWork.setToolsOpt(l)}break}case s.EPostMessageType.GetImageBitMap:{if(i&&n&&a){const l=a.toString();this.cacheImages.set(i,{imageBitmap:n,workId:l});const h=this.imageResolveMap.get(i);if(h){const{resolve:p,timer:d}=h;d&&clearTimeout(d),p&&p(i)}this.deleteCachedImagesByWorkId(l,i)}break}}}getIconSize(r,e,t){const o=r*t,i=e*t;return o<=50||i<=50?[50,50]:o<=100||i<=100?[100,100]:o<=200||i<=200?[200,200]:o<=400||i<=400?[400,400]:o<=800||i<=800?[800,800]:[1600,1600]}async loadImageBitMap(r){const{toolsType:e,opt:t,workId:o,isSubWorker:i}=r;if(e===s.EToolsKey.Image&&t&&o){const n=o.toString(),{src:a,type:c,width:l,height:h,strokeColor:p}=t;if(!a||!c||!l||!h)return;let d=a;if(c===s.EImageType.Iconify){const[m,w]=this.getIconSize(l,h,this.dpr);d=`${a}?width=${m}&height=${w}&color=${p}`}if(this.cacheImages.has(d)){const m=this.getCachedImages(d);if(m)return m}if(this.imageResolveMap.has(d)){const m=this.getCachedImagesByWorkId(n);if(m)return m}const y=await new Promise(m=>{const w=this.imageResolveMap.get(d)||{resolve:void 0,timer:void 0};w.timer&&clearTimeout(w.timer),w.resolve=m,w.timer=setTimeout(()=>{const P=this.imageResolveMap.get(d);P!=null&&P.resolve&&P.resolve(d)},5e3),this.imageResolveMap.set(d,w),this._post({sp:[{imageSrc:d,workId:n,viewId:this.viewId,isgl:!!this.fullLayer.parent.gl,isSubWorker:i,type:s.EPostMessageType.GetImageBitMap}]})});return this.imageResolveMap.delete(y),this.getCachedImages(d)}}createLocalWork(r){const{workId:e,opt:t,toolsType:o}=r;e&&t&&(!this.localWork.getToolsOpt()&&o&&this.setToolsOpt({toolsType:o,toolsOpt:t}),this.setWorkOpt({workId:e,toolsOpt:t}))}updateScene(r,e,t){r.attr({...e});const{width:o,height:i}=e;r.container.width=o,r.container.height=i,r.width=o,r.height=i,this.updateLayer({width:o,height:i},t)}updateLayer(r,e){const{width:t,height:o}=r,i=e||this.fullLayer;i&&(i.parent.setAttribute("width",t),i.parent.setAttribute("height",o),i.setAttribute("size",[t,o]),i.setAttribute("pos",[t*.5,o*.5]))}getSupportContextType(r,e){const t=new OffscreenCanvas(100,100);let o=["2d"];r===ke.Full&&this.viewId===s.Main_View_Id&&(o=["webgl2","webgl","2d"],e&&o.unshift(e));for(const i of o)try{if(t.getContext(i))return i}catch(n){throw n}}createScene(r){const{offscreenCanvasOpt:e}=r,{width:t,height:o}=e,i=new OffscreenCanvas(t,o);e.contextType&&delete e.contextType;const n=new D.Scene({container:i,displayRatio:this.dpr,depth:!1,desynchronized:!0,failIfMajorPerformanceCaveat:!0,...e,contextType:this.contextType,id:this.viewId});return n.setAttribute("id",this.viewId),n}createLayer(r,e,t,o){const i=`offscreen-${r}`,{width:n,height:a}=t;o&&(t.offscreen=!0,t.canvas=o);const c=e.layer(i,t),l=new D.Group({anchor:[.5,.5],pos:[n*.5,a*.5],size:[n,a],name:"viewport",id:r});return c.append(l),l}async clearAll(){var r;this.fullLayer&&(this.fullLayer.parent.children.forEach(e=>{e.name!=="viewport"&&e.remove()}),U(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(),(r=this.serviceWork)==null||r.destroy()}setToolsOpt(r){this.localWork.setToolsOpt(r)}setWorkOpt(r){const{workId:e,toolsOpt:t}=r;e&&t&&this.localWork.setWorkOptions(e.toString(),t)}destroy(){var r;this.vNodes.clear(),this.fullLayer.remove(),B(this.fullLayer,this.fullLayer.parent),this.clearCacheImages(),this.clearImageResolveMap(),this.scene.remove(),this.localWork.destroy(),(r=this.serviceWork)==null||r.destroy()}async post(r){this.combinePostMsg.add(r),await this.runBatchPostData()}async runBatchPostData(){this.workerTaskId||(this.workerTaskId=setTimeout(()=>{this.workerTaskId=void 0,this.combinePost()},16)),this.type===ke.Full&&!this.delayPostDoneResolve&&await new Promise(e=>{this.delayPostDoneResolve=e})&&(this.delayPostDoneResolve=void 0)}combinePostData(){var a,c;this.workerTaskId=void 0;const r=[],e=[];let t,o,i;const n=new Set;for(const l of this.combinePostMsg.values()){if((a=l.render)!=null&&a.length)for(const h of l.render){let p=!1;if(h.workId&&n.add(h.workId),h.isClearAll&&(h.rect=this.getSceneRect(),h.isClear=!0,delete h.isClearAll),h.drawCanvas){const d=this.getLayer(h.drawCanvas);if(!d||!(d.parent instanceof D.Layer))continue;if(d.parent.render(),h.isDrawAll){const u=this.getSceneRect();h.rect=u,delete h.isDrawAll}}for(const d of r)if(h.viewId===d.viewId){h.isClear&&d.clearCanvas&&d.isClear&&d.clearCanvas===h.clearCanvas&&(d.rect=s.computRect(d.rect,h.rect),p=!0),d.drawCanvas&&d.drawCanvas===h.drawCanvas&&(d.rect=s.computRect(d.rect,h.rect),p=!0);continue}p||(h.isClear&&!h.drawCanvas?r.unshift(h):r.push(h))}if((c=l.sp)!=null&&c.length)for(const h of l.sp){let p=!1;for(const d of e)if(H(h,d)){p=!0;break}p||e.push(h)}A(l.fullWorkerDrawCount)&&(t=l.fullWorkerDrawCount),A(l.subWorkerDrawCount)&&(o=l.subWorkerDrawCount),A(l.consumeCount)&&(i=l.consumeCount)}return this.combinePostMsg.clear(),{render:r,sp:e,fullWorkerDrawCount:t,subWorkerDrawCount:o,consumeCount:i,workIds:n,viewId:this.viewId}}getSceneRect(){const{width:r,height:e}=this.scene;return{x:0,y:0,w:Math.floor(r),h:Math.floor(e)}}}class Ze{constructor(r){g(this,"viewId");g(this,"vNodes");g(this,"thread");g(this,"fullLayer");g(this,"drawLayer");g(this,"_post");g(this,"tmpOpt");g(this,"workShapes",new Map);g(this,"drawCount",0);g(this,"consumeCount",0);g(this,"syncUnitTime",s.DefaultAppliancePluginExtrasOptions.syncOpt.interval);g(this,"combineUnitTime",s.DefaultAppliancePluginExtrasOptions.bezier.combineUnitTime);g(this,"maxCombineEraserTime",s.DefaultAppliancePluginExtrasOptions.pencilEraser.maxCombineTime);this.thread=r.thread,this.viewId=r.viewId,this.vNodes=r.vNodes,this.fullLayer=r.fullLayer,this.drawLayer=r.drawLayer,this._post=this.thread.post.bind(r.thread)}setmaxCombineEraserTime(r){this.maxCombineEraserTime=r}setCombineUnitTime(r){this.combineUnitTime=r}setSyncUnitTime(r){this.syncUnitTime=r}destroy(){this.workShapes.clear()}getWorkShapes(){return this.workShapes}getWorkShape(r){return this.workShapes.get(r)}createWorkShape(r,e){if(r&&this.tmpOpt){const t={toolsType:this.tmpOpt.toolsType,toolsOpt:e||this.tmpOpt.toolsOpt},o=this.createWorkShapeNode({...t,workId:r});o&&this.workShapes.set(r,o)}}setWorkOptions(r,e){const t=this.getWorkShape(r);t||this.createWorkShape(r,e),t==null||t.setWorkOptions(e)}createWorkShapeNode(r){var e;return ie({...r,vNodes:this.vNodes,fullLayer:this.fullLayer,drawLayer:this.drawLayer},(e=this.thread)==null?void 0:e.serviceWork)}setToolsOpt(r){var e,t,o;((e=this.tmpOpt)==null?void 0:e.toolsType)!==r.toolsType&&(t=this.tmpOpt)!=null&&t.toolsType&&this.clearAllWorkShapesCache(),this.tmpOpt=r,(o=r.toolsOpt)!=null&&o.syncUnitTime&&(this.syncUnitTime=r.toolsOpt.syncUnitTime),r.combineUnitTime&&(this.combineUnitTime=r.combineUnitTime),r.maxCombineEraserTime&&(this.maxCombineEraserTime=r.maxCombineEraserTime)}getToolsOpt(){return this.tmpOpt}clearWorkShapeNodeCache(r){var e;(e=this.getWorkShape(r))==null||e.clearTmpPoints(),this.workShapes.delete(r)}clearAllWorkShapesCache(){this.workShapes.forEach(r=>r.clearTmpPoints()),this.workShapes.clear()}setFullWork(r){const{workId:e,opt:t,toolsType:o}=r;if(e&&t&&o){const i=e.toString();let n;return e&&this.workShapes.has(i)?(n=this.workShapes.get(i),n==null||n.setWorkOptions(t)):n=this.createWorkShapeNode({toolsOpt:t,toolsType:o,workId:i}),n?(this.workShapes.set(i,n),n):void 0}}}class At extends Ze{constructor(e){super(e);g(this,"drawWorkActiveId")}runSelectWork(e){var o;const t=this.setFullWork(e);t&&((o=e.selectIds)!=null&&o.length)&&e.workId&&t.selectServiceNode(e.workId.toString(),{selectIds:e.selectIds},!1)}workShapesDone(){for(const e of this.workShapes.keys())this.clearWorkShapeNodeCache(e);U(this.fullLayer,this.fullLayer.parent)}async consumeDraw(e){const{workId:t,fullWorkerDrawCount:o,postCount:i}=e,n=t==null?void 0:t.toString(),a=n&&this.workShapes.get(n);if(!a)return;this.drawWorkActiveId&&this.drawWorkActiveId!==n&&(await this.consumeDrawAll({workId:this.drawWorkActiveId,viewId:this.viewId,msgType:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local}),this.drawWorkActiveId=void 0),!this.drawWorkActiveId&&n!==s.Storage_Selector_key&&(this.drawWorkActiveId=n);const c=a.toolsType;A(i)&&(this.consumeCount=i);const l=a.consume({data:e,drawCount:this.drawCount,isFullWork:!0,isSubWorker:!0,removeDrawCount:o});switch(c){case s.EToolsKey.Selector:l&&(this.drawCount++,await this.drawSelector(l));break;case s.EToolsKey.Ellipse:case s.EToolsKey.Arrow:case s.EToolsKey.Straight:case s.EToolsKey.Rectangle:case s.EToolsKey.Star:case s.EToolsKey.Polygon:case s.EToolsKey.SpeechBalloon:{l&&(this.drawCount++,await this.drawShape(l));break}case s.EToolsKey.Pencil:{l&&(this.drawCount++,await this.drawPencil(l,t==null?void 0:t.toString()));break}case s.EToolsKey.BitMapEraser:case s.EToolsKey.PencilEraser:{l&&(this.drawCount++,await this._post({subWorkerDrawCount:this.drawCount,consumeCount:this.consumeCount}));break}}}async consumeDrawAll(e){const{workId:t}=e;if(t){const o=t.toString();this.drawWorkActiveId===o&&(this.drawWorkActiveId=void 0);const i=this.workShapes.get(o);if(!i)return;switch(i.toolsType){case s.EToolsKey.Selector:this.drawCount=0,U(this.fullLayer,this.fullLayer.parent),this.clearWorkShapeNodeCache(o),this._post({render:[{isClearAll:!0,clearCanvas:s.ECanvasShowType.Float,viewId:this.viewId}]});break;case s.EToolsKey.Arrow:case s.EToolsKey.Straight:case s.EToolsKey.Ellipse:case s.EToolsKey.Pencil:case s.EToolsKey.Rectangle:case s.EToolsKey.Star:case s.EToolsKey.Polygon:case s.EToolsKey.SpeechBalloon:case s.EToolsKey.BitMapEraser:case s.EToolsKey.PencilEraser:this.drawCount=0,U(this.fullLayer,this.fullLayer.parent),this.clearWorkShapeNodeCache(o);break}}}async removeWork(e){const{workId:t}=e,o=t==null?void 0:t.toString();if(o){const i=this.removeNode(o);if(i){const n=[];n.push({rect:s.getSafetyRect(i),clearCanvas:s.ECanvasShowType.Float,isClear:!0,viewId:this.viewId},{rect:s.getSafetyRect(i),drawCanvas:s.ECanvasShowType.Float,viewId:this.viewId}),await this._post({render:n})}}}removeNode(e){const t=this.workShapes.has(e);let o;return t&&(this.fullLayer.getElementsByName(e).forEach(i=>{const n=i.getBoundingClientRect();o=s.computRect(o,{x:n.x-W.SafeBorderPadding,y:n.y-W.SafeBorderPadding,w:n.width+W.SafeBorderPadding*2,h:n.height+W.SafeBorderPadding*2}),i.remove(),B(i,this.fullLayer.parent)}),o&&this.clearWorkShapeNodeCache(e)),o}async drawPencil(e,t){await this._post({subWorkerDrawCount:this.drawCount,consumeCount:this.consumeCount,render:[{rect:e==null?void 0:e.rect,workId:t,drawCanvas:s.ECanvasShowType.Float,viewId:this.viewId}],sp:(e==null?void 0:e.op)&&[e]})}async drawShape(e){await this._post({subWorkerDrawCount:this.drawCount,consumeCount:this.consumeCount,render:[{rect:(e==null?void 0:e.rect)&&s.getSafetyRect(e.rect),isClear:!0,clearCanvas:s.ECanvasShowType.Float,viewId:this.viewId},{rect:(e==null?void 0:e.rect)&&s.getSafetyRect(e.rect),drawCanvas:s.ECanvasShowType.Float,viewId:this.viewId}]})}async drawSelector(e){await this._post({subWorkerDrawCount:this.drawCount,consumeCount:this.consumeCount,render:[{rect:(e==null?void 0:e.rect)&&s.getSafetyRect(e.rect),isClear:!0,clearCanvas:s.ECanvasShowType.Float,viewId:this.viewId},{rect:(e==null?void 0:e.rect)&&s.getSafetyRect(e.rect),drawCanvas:s.ECanvasShowType.Float,viewId:this.viewId}]})}}class Bt{constructor(r){g(this,"viewId");g(this,"vNodes");g(this,"topLayer");g(this,"thread");g(this,"post");g(this,"serviceWorkShapes",new Map);g(this,"localWorkShapes",new Map);g(this,"tmpOpt");g(this,"syncUnitTime",s.DefaultAppliancePluginExtrasOptions.syncOpt.interval);g(this,"animationId");this.viewId=r.viewId,this.vNodes=r.vNodes,this.topLayer=r.topLayer,this.thread=r.thread,this.post=r.thread.post.bind(r.thread)}canUseTopLayer(r){return r===s.EToolsKey.LaserPen}getWorkShape(r){return this.localWorkShapes.get(r)}createWorkShape(r,e){if(r&&this.tmpOpt){const t={toolsType:this.tmpOpt.toolsType,toolsOpt:e||this.tmpOpt.toolsOpt},o=this.createWorkShapeNode({...t,workId:r});return o&&this.localWorkShapes.set(r,{node:o,toolsType:o.toolsType,workState:s.EvevtWorkState.Start}),o}}setWorkOptions(r,e){var o;const t=(o=this.localWorkShapes.get(r))==null?void 0:o.node;t||this.createWorkShape(r,e),t==null||t.setWorkOptions(e)}createWorkShapeNode(r){const{toolsType:e}=r;if(e===s.EToolsKey.LaserPen)return ie({...r,vNodes:this.vNodes,fullLayer:this.topLayer,drawLayer:this.topLayer})}clearAllWorkShapesCache(){this.localWorkShapes.forEach(r=>{var e;return(e=r.node)==null?void 0:e.clearTmpPoints()}),this.localWorkShapes.clear()}setToolsOpt(r){var e;this.tmpOpt=r,(e=r.toolsOpt)!=null&&e.syncUnitTime&&(this.syncUnitTime=r.toolsOpt.syncUnitTime)}getToolsOpt(){return this.tmpOpt}consumeDraw(r){const{workId:e,dataType:t}=r;if(t===s.EDataType.Service)this.activeServiceWorkShape(r);else{const o=e==null?void 0:e.toString(),i=o&&this.localWorkShapes.get(o);if(!i)return;const n=i.node.consume({data:r,isFullWork:!1,isSubWorker:!0});n.rect&&(i.totalRect=s.computRect(n.rect,i.totalRect),i.result=n,i.workState=s.EvevtWorkState.Doing,o&&this.localWorkShapes.set(o,i))}this.runAnimation()}consumeDrawAll(r){const{workId:e,dataType:t}=r;if(t===s.EDataType.Service)this.activeServiceWorkShape(r);else{const o=e==null?void 0:e.toString(),i=o&&this.localWorkShapes.get(o);if(!i)return;const n=i.node.consumeAll({data:r});i.totalRect=s.computRect(n.rect,i.totalRect),i.result=n,i.workState=s.EvevtWorkState.Done,o&&this.localWorkShapes.set(o,i)}this.runAnimation()}destroy(){this.serviceWorkShapes.clear(),this.localWorkShapes.clear()}setNodeKey(r,e,t,o){return e.toolsType=t,e.node=this.createWorkShapeNode({workId:r,toolsType:t,toolsOpt:o}),e}activeServiceWorkShape(r){var d,u;const{workId:e,opt:t,toolsType:o,type:i,updateNodeOpt:n,ops:a,op:c}=r;if(!e)return;const l=e.toString(),h=(d=this.vNodes.get(l))==null?void 0:d.rect;if(!((u=this.serviceWorkShapes)!=null&&u.has(l))){let f={toolsType:o,animationWorkData:c||[],animationIndex:0,type:i,updateNodeOpt:n,ops:a,oldRect:h};o&&t&&(f=this.setNodeKey(l,f,o,t)),this.serviceWorkShapes.set(l,f)}const p=this.serviceWorkShapes.get(l);i&&(p.type=i),a&&(p.animationWorkData=s.transformToNormalData(a),p.ops=a),n&&(p.updateNodeOpt=n),c&&(p.animationWorkData=c),p.node&&p.node.getWorkId()!==l&&p.node.setWorkId(l),h&&(p.oldRect=h),o&&t&&(p.toolsType!==o&&o&&t&&this.setNodeKey(l,p,o,t),p.node&&p.node.setWorkOptions(t))}computNextAnimationIndex(r,e){const t=Math.floor((r.animationWorkData||[]).slice(r.animationIndex).length*32/e/this.syncUnitTime)*e;return Math.min((r.animationIndex||0)+(t||e),(r.animationWorkData||[]).length)}async animationDraw(){var n,a,c,l;this.animationId=void 0;let r=!1;const e=new Map,t=[],o=[],i=[];for(const[h,p]of this.serviceWorkShapes.entries())switch(p.toolsType){case s.EToolsKey.LaserPen:{const u=this.computNextAnimationIndex(p,8),f=Math.max(0,p.animationIndex||0),y=(p.animationWorkData||[]).slice(f,u);if((p.animationIndex||0)<u){const m=(n=p.node)==null?void 0:n.consumeService({op:y,isFullWork:!1});p.totalRect=s.computRect(p.totalRect,m),p.animationIndex=u,y.length&&e.set(h,{workState:f===0?s.EvevtWorkState.Start:u===((a=p.animationWorkData)==null?void 0:a.length)?s.EvevtWorkState.Done:s.EvevtWorkState.Doing,op:y.slice(-2)})}if(o.push({isClear:!0,rect:p.totalRect,clearCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId}),t.push({rect:p.totalRect,drawCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId}),p.isDel){(c=p.node)==null||c.clearTmpPoints(),this.serviceWorkShapes.delete(h);break}p.ops&&p.animationIndex===((l=p.animationWorkData)==null?void 0:l.length)&&!p.isDel&&(this.topLayer.getElementsByName(h.toString())[0]||(p.isDel=!0,this.serviceWorkShapes.set(h,p))),r=!0;break}}for(const[h,p]of this.localWorkShapes.entries()){const{result:d,toolsType:u,totalRect:f,isDel:y,workState:m}=p;switch(u){case s.EToolsKey.LaserPen:{if(f&&(o.push({isClear:!0,rect:f,clearCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId}),t.push({rect:f,drawCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId})),y){p.node.clearTmpPoints(),this.localWorkShapes.delete(h),i.push({removeIds:[h.toString()],viewId:this.viewId,type:s.EPostMessageType.RemoveNode});break}d&&((d.op||d.ops)&&i.push(d),p.result=void 0),!this.topLayer.getElementsByName(h.toString())[0]&&m===s.EvevtWorkState.Done&&(p.isDel=!0,this.localWorkShapes.set(h,p)),r=!0;break}}}r&&this.runAnimation(),e.size&&e.forEach((h,p)=>{i.push({type:s.EPostMessageType.Cursor,uid:p.split(s.Storage_Splitter)[0],op:h.op,workState:h.workState,viewId:this.viewId})}),(t.length||o.length||i.length)&&this.post({render:[...o,...t],sp:i})}runAnimation(){this.animationId||(this.animationId=setTimeout(()=>{this.animationId=void 0,this.animationDraw()},16))}}class Ft extends Ze{constructor(e){super(e);g(this,"opt");g(this,"scene");g(this,"cameraOpt");g(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,o;U(this.fullLayer,this.fullLayer.parent),(e=this.fullLayer)==null||e.disconnect(),(t=this.fullLayer)==null||t.remove(),(o=this.scene)==null||o.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:o}=e;this.scene.width=t,this.scene.height=o,this.updateLayer({width:t,height:o})}updateLayer(e){if(!this.fullLayer)throw new Error("SnapshotWork snapshotFullLayer is not initialized");const{width:t,height:o}=e;this.fullLayer.parent.setAttribute("width",t),this.fullLayer.parent.setAttribute("height",o),this.fullLayer.setAttribute("size",[t,o]),this.fullLayer.setAttribute("pos",[t*.5,o*.5])}setCameraOpt(e){this.cameraOpt=e;const{scale:t,centerX:o,centerY:i,width:n,height:a}=e;if(!this.scene)throw new Error("SnapshotWork scene is not initialized");if(!this.fullLayer)throw new Error("SnapshotWork snapshotFullLayer is not initialized");(n!==this.scene.width||a!==this.scene.height)&&this.updateScene({width:n,height:a}),this.fullLayer.setAttribute("scale",[t,t]),this.fullLayer.setAttribute("translate",[-o,-i])}getOffscreen(){var t;return((t=this.fullLayer)==null?void 0:t.parent).canvas}getRectImageBitmap(e,t){const{rect:o}=e,i=this.thread.dpr,n=Math.floor(o.x*i),a=Math.floor(o.y*i),c=Math.floor(o.w*i),l=Math.floor(o.h*i);return createImageBitmap(this.getOffscreen(),n,a,c,l,t)}async getSnapshot(e){const{scenePath:t,scenes:o,cameraOpt:i,w:n,h:a}=e;if(t&&o&&i&&this.fullLayer){this.setCameraOpt(i);for(const[l,h]of Object.entries(o))if(h!=null&&h.type)switch(h==null?void 0:h.type){case s.EPostMessageType.UpdateNode:case s.EPostMessageType.FullWork:{const{opt:p}=h,d={...h,opt:p,workId:l,msgType:s.EPostMessageType.FullWork,dataType:s.EDataType.Service,viewId:this.viewId};await this.runFullWork(d);break}}let c;n&&a&&(c={resizeWidth:n,resizeHeight:a});try{await this.getSnapshotRender({scenePath:t,options:c})}catch(l){const h=l&&l instanceof Error?l.message:l==null?void 0:l.toString();console.error("[SnapshotWork] getSnapshotRender error",h),this.thread._post({sp:[{type:s.EPostMessageType.ReportError,reportString:`[SnapshotWork] getSnapshotRender error: ${h}`}]})}}}async runFullWork(e){var i;const t=this.setFullWork(e),o=e.ops&&s.transformToNormalData(e.ops);if(t){let n,a;const c=(i=t.getWorkId())==null?void 0:i.toString();return t.toolsType===s.EToolsKey.BackgroundSVG?n=t.consumeService({isFullWork:!0,replaceId:c}):t.toolsType===s.EToolsKey.Image?n=await t.consumeServiceAsync({isFullWork:!0,worker:this.thread}):t.toolsType===s.EToolsKey.Text?n=await t.consumeServiceAsync({isFullWork:!0,replaceId:c,isDrawLabel:!0}):(n=t.consumeService({op:o,isFullWork:!0,replaceId:c}),a=(e==null?void 0:e.updateNodeOpt)&&t.updataOptService(e.updateNodeOpt)),e.workId&&this.workShapes.delete(e.workId.toString()),s.computRect(n,a)}}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 n;const{scenePath:t,options:o}=e;((n=this.fullLayer)==null?void 0:n.parent).render();const i=await this.getRectImageBitmap({rect:this.getSceneRect(),drawCanvas:s.ECanvasShowType.None},o);i&&(this.thread._post({sp:[{type:s.EPostMessageType.Snapshot,scenePath:t,imageBitmap:i,viewId:this.viewId,index:0}]},[i]),i.close())}async getBoundingRect(e){const{scenePath:t,scenes:o,cameraOpt:i}=e;if(t&&o&&i&&this.fullLayer){this.setCameraOpt(i);let n;for(const[a,c]of Object.entries(o))if(c!=null&&c.type)switch(c==null?void 0:c.type){case s.EPostMessageType.UpdateNode:case s.EPostMessageType.FullWork:{const l=await this.runFullWork({...c,workId:a,msgType:s.EPostMessageType.FullWork,dataType:s.EDataType.Service,viewId:this.viewId});n=s.computRect(n,l);break}}n&&this.thread._post({sp:[{type:s.EPostMessageType.BoundingBox,scenePath:t,rect:n,viewId:this.viewId}]})}}}var ke=(k=>(k.Full="full",k.Sub="sub",k))(ke||{});class zt extends Ot{constructor(e,t,o){super(e,t,"sub");g(this,"type","sub");g(this,"_post");g(this,"topLayer");g(this,"serviceWork");g(this,"localWork");g(this,"topLayerWork");g(this,"snapshotWork");this._post=o;const i=s.DefaultAppliancePluginExtrasOptions.bufferSize.sub;this.topLayer=t.isUseSimple?void 0:this.createLayer("topLayer",this.scene,{...t.layerOpt,bufferSize:i,contextType:"2d"});const n={thread:this,viewId:this.viewId,vNodes:this.vNodes,fullLayer:this.fullLayer,topLayer:this.topLayer,isUseSimple:t.isUseSimple};this.localWork=new At(n),t.isUseSimple||(this.topLayerWork=new Bt(n)),this.snapshotWork=new Ft({...n,createScene:this.createScene.bind(this),createLayer:this.createLayer.bind(this)}),this.vNodes.init(this.fullLayer)}async combinePost(){var n,a;const{render:e,...t}=this.combinePostData();let o;if(e!=null&&e.length){const c=[];for(const l of e)if(l.rect){if(l.rect=Vt(s.cloneDeep(l.rect),this.scene,this.dpr),!l.rect)continue;if(l.drawCanvas&&l.rect&&l.rect.w>0&&l.rect.h>0){const h=await this.getRectImageBitmap(l);l.imageBitmap=h,o||(o=[]),o.push(h)}c.push(l)}t.render=c}const i=(n=t.sp)==null?void 0:n.filter(c=>c.type!==s.EPostMessageType.None||c.isLockSentEventCursor||c.needUndoTicker);i!=null&&i.length?t.sp=i.map(c=>({...c,viewId:this.viewId})):delete t.sp,t.consumeCount===void 0&&delete t.consumeCount,t.subWorkerDrawCount===void 0&&delete t.subWorkerDrawCount,(i!=null&&i.length||t.consumeCount||t.subWorkerDrawCount||(a=t==null?void 0:t.render)!=null&&a.length)&&this._post(t,o),this.delayPostDoneResolve&&this.delayPostDoneResolve(!0)}async on(e){var l,h;const{msgType:t,toolsType:o,opt:i,dataType:n,workState:a,isLockSentEventCursor:c}=e;switch(t){case s.EPostMessageType.UpdateTools:{if(o&&((l=this.topLayerWork)!=null&&l.canUseTopLayer(o))&&i){const p={toolsType:o,toolsOpt:i};this.topLayerWork.setToolsOpt(p);return}break}case s.EPostMessageType.CreateWork:{this.createLocalWork(e);return}case s.EPostMessageType.DrawWork:{a===s.EvevtWorkState.Done&&n===s.EDataType.Local?(await this.consumeDrawAll(n,e),o===s.EToolsKey.LaserPen&&c&&this.post({sp:[{type:s.EPostMessageType.None,isLockSentEventCursor:c}]})):this.consumeDraw(n,e);return}case s.EPostMessageType.RemoveNode:{await this.removeNode(e);return}case s.EPostMessageType.FullWork:{o&&((h=this.topLayerWork)!=null&&h.canUseTopLayer(o))&&await this.consumeDrawAll(n,e);return}case s.EPostMessageType.Snapshot:{this.snapshotWork.createSnapshotFullLayer(this.opt,{...this.opt.layerOpt,bufferSize:this.viewId===s.Main_View_Id?6e3:3e3,contextType:"2d"}),await this.snapshotWork.getSnapshot(e),this.snapshotWork.destroySnapshotFullLayer();return}case s.EPostMessageType.BoundingBox:{this.snapshotWork.createSnapshotFullLayer(this.opt,{...this.opt.layerOpt,bufferSize:this.viewId===s.Main_View_Id?6e3:3e3,contextType:"2d"}),await this.snapshotWork.getBoundingRect(e),this.snapshotWork.destroySnapshotFullLayer();return}}await super.on(e)}createLocalWork(e){var n;const{workId:t,toolsType:o,opt:i}=e;if(o&&((n=this.topLayerWork)!=null&&n.canUseTopLayer(o))&&t&&i){this.topLayerWork.getToolsOpt()||this.topLayerWork.setToolsOpt({toolsType:o,toolsOpt:i}),this.topLayerWork.setWorkOptions(t.toString(),i);return}o&&super.createLocalWork(e)}async removeNode(e){const{dataType:t}=e;t===s.EDataType.Local&&await this.localWork.removeWork(e)}getLayer(e){switch(e){case s.ECanvasShowType.TopFloat:return this.topLayer||this.fullLayer;default:return this.fullLayer}}getOffscreen(e){var o;return((o=this.getLayer(e))==null?void 0:o.parent).canvas}async consumeDraw(e,t){var n;const{workId:o,toolsType:i}=t;if(o){if(i&&((n=this.topLayerWork)!=null&&n.canUseTopLayer(i))){e===s.EDataType.Local&&(this.topLayerWork.getWorkShape(o.toString())||this.createLocalWork(t)),this.topLayerWork.consumeDraw(t);return}i&&(this.localWork.getWorkShape(o.toString())||this.createLocalWork(t),await this.localWork.consumeDraw(t));return}}async consumeDrawAll(e,t){var a;const{workId:o,toolsType:i,dataType:n}=t;if(o){const c=o.toString();if(i&&((a=this.topLayerWork)!=null&&a.canUseTopLayer(i))){n===s.EDataType.Local&&(this.topLayerWork.getWorkShape(c)||this.createLocalWork(t)),this.topLayerWork.consumeDrawAll(t);return}i&&(this.localWork.getWorkShape(c)||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()}),U(this.topLayer,this.topLayer.parent)),await this.post({render:[{isClearAll:!0,clearCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId}],sp:[{type:s.EPostMessageType.Clear}]})}getRectImageBitmap(e,t){const{rect:o,drawCanvas:i}=e,n=Math.floor(o.x*this.dpr),a=Math.floor(o.y*this.dpr),c=Math.floor(o.w*this.dpr||1),l=Math.floor(o.h*this.dpr||1);return createImageBitmap(this.getOffscreen(i),n,a,c,l,t)}updateLayer(e){const{width:t,height:o}=e;super.updateLayer(e),this.topLayer&&(this.topLayer.parent.setAttribute("width",t),this.topLayer.parent.setAttribute("height",o),this.topLayer.setAttribute("size",[t,o]),this.topLayer.setAttribute("pos",[t*.5,o*.5]))}async updateDpr(e){super.updateDpr(e);const t=this.topLayerWork&&!!this.topLayerWork.localWorkShapes.size||!1,o=!!this.localWork.getWorkShapes().size,i=[];o&&i.push({isClearAll:!0,clearCanvas:s.ECanvasShowType.Float,viewId:this.viewId}),t&&i.push({isClearAll:!0,clearCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId}),i.length&&await this.post({render:i})}async updateCamera(e){var i;const t=[],{cameraOpt:o}=e;if(o&&!H(this.cameraOpt,o)){const n=this.topLayerWork&&!!this.topLayerWork.localWorkShapes.size||!1,a=!!this.localWork.getWorkShapes().size;if(a&&this.localWork.workShapesDone(),this.setCameraOpt(o),a&&t.push({isClearAll:!0,clearCanvas:s.ECanvasShowType.Float,viewId:this.viewId}),n&&this.topLayerWork){t.push({isClearAll:!0,clearCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId});for(const[c,l]of this.topLayerWork.localWorkShapes.entries()||[])if(l.totalRect){let h;(i=this.topLayer)==null||i.getElementsByName(c.toString()).forEach(p=>{const d=p.getBoundingClientRect(),u=s.getSafetyRect({x:d.x,y:d.y,w:d.width,h:d.height});h=s.computRect(h,u)}),l.totalRect=h,this.topLayerWork.localWorkShapes.set(c,l)}}t.length&&await this.post({render:t})}}setCameraOpt(e,t){var l,h;this.cameraOpt=e;const{scale:o,centerX:i,centerY:n,width:a,height:c}=e;(a!==this.scene.width||c!==this.scene.height)&&this.updateScene(this.scene,{width:a,height:c}),t?(t.setAttribute("scale",[o,o]),t.setAttribute("translate",[-i,-n])):(this.fullLayer.setAttribute("scale",[o,o]),this.fullLayer.setAttribute("translate",[-i,-n]),(l=this.topLayer)==null||l.setAttribute("scale",[o,o]),(h=this.topLayer)==null||h.setAttribute("translate",[-i,-n]))}}class ae extends W{constructor(e){super(e);g(this,"canRotate",!0);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.Image);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");this.workOptions=e.toolsOpt,this.scaleType=ae.getScaleType(this.workOptions)}consume(){return{type:s.EPostMessageType.None}}consumeAll(){return{type:s.EPostMessageType.None}}draw(e){const{layer:t,workId:o,replaceId:i,imageBitmap:n}=e,{centerX:a,centerY:c,width:l,height:h,rotate:p,zIndex:d,eraserlines:u}=this.workOptions,f=new D.Group({anchor:[.5,.5],pos:[a,c],name:o,size:[l,h],zIndex:d,rotate:p}),y={anchor:[.5,.5],pos:[0,0],size:[l,h],texture:n};n||(y.bgcolor="rgba(0,0,0,0.3)");const m=new D.Sprite(y);f.append(m),u&&this.drawEraserlines({group:f,eraserlines:u,pos:[a,c],layer:t}),this.replace(t,i||o,f);const w=f.getBoundingClientRect();if(w)return{x:Math.floor(w.x-W.SafeBorderPadding),y:Math.floor(w.y-W.SafeBorderPadding),w:Math.floor(w.width+W.SafeBorderPadding*2),h:Math.floor(w.height+W.SafeBorderPadding*2)}}consumeService(){}async consumeServiceAsync(e){var h,p,d,u;const{isFullWork:t,replaceId:o,worker:i}=e,{src:n,uuid:a}=this.workOptions,c=((h=this.workId)==null?void 0:h.toString())||a,l=t?this.fullLayer:this.drawLayer||this.fullLayer;if(n){const f=await i.loadImageBitMap({toolsType:this.toolsType,opt:this.workOptions,workId:c,isSubWorker:i instanceof zt});if(f){const y=this.draw({workId:c,layer:l,replaceId:o,imageBitmap:f});return this.oldRect=c&&((d=(p=this.vNodes)==null?void 0:p.get(c))==null?void 0:d.rect)||void 0,(u=this.vNodes)==null||u.setInfo(c,{rect:y,op:[],opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:y&&W.getCenterPos(y,l)}),y}}}clearTmpPoints(){this.tmpPoints.length=0}static getScaleType(e){const{uniformScale:t,rotate:o}=e;return t!==!1||o&&Math.abs(o)%90>0?s.EScaleType.proportional:s.EScaleType.all}static updateNodeOpt(e){const{node:t,opt:o,vNodes:i,targetNode:n}=e,{translate:a,originPoint:c,scenePoint:l,scale:h,angle:p,isLocked:d,zIndex:u,strokeColor:f}=o,y=n&&s.cloneDeep(n)||i.get(t.name);if(!y)return;const m=t.parent;if(m){if(f&&(y.opt.strokeColor=f),A(u)&&(t.setAttribute("zIndex",u),y.opt.zIndex=u),ee(d)&&(y.opt.locked=d),c&&l&&h&&a){const{centerX:w,centerY:P,width:T,height:S,uniformScale:E}=y.opt,I=E!==!1?[h[0],h[0]]:h,R=[w,P],M=[w,P];s.scalePoints(M,l,I,a);const v=[M[0]-R[0],M[1]-R[1]];if(y.centerPos=[y.centerPos[0]+v[0],y.centerPos[1]+v[1]],y.opt.width=Math.round(T*I[0]),y.opt.height=Math.round(S*I[1]),y.opt.centerX=M[0],y.opt.centerY=M[1],y.opt.eraserlines)for(const L of y.opt.eraserlines){const{op:C,thickness:b}=L;L.thickness=Math.round(b*Math.max(h[0],h[1]));for(let N=0;N<C.length;N++)s.scaleLitePoints(C[N],l,h,a)}}else if(a){if(y.opt.centerX=y.opt.centerX+a[0],y.opt.centerY=y.opt.centerY+a[1],y.centerPos=[y.centerPos[0]+a[0],y.centerPos[1]+a[1]],y.opt.eraserlines)for(const w of y.opt.eraserlines){const{op:P}=w;for(let T=0;T<P.length;T++){const S=P[T].map((E,I)=>I%2?E+a[1]:E+a[0]);w.op[T]=S}}}else if(A(p))if(y.opt.rotate=p,y.scaleType=ae.getScaleType(y.opt),n){const w=s.getRectRotated(y.rect,p);y.rect=w}else{const w=W.getRectFromLayer(m,t.name);y.rect=w||y.rect}return y&&i.setInfo(t.name,y),y==null?void 0:y.rect}}}const Ut=k=>{if(k.tagName==="GROUP"){const r=Object.getOwnPropertySymbols(k).find(e=>e.toString()==="Symbol(sealed)");if(r&&k[r])return!0}return!1},B=(k,r)=>{if(k.mesh&&k.mesh.texture&&k.name==="eraserTexture"){const e=k.mesh.texture.image;r.deleteTexture(e)}else if(k.tagName==="GROUP"&&k&&k.children)for(const e of k.children)B(e,r)},U=(k,r)=>{B(k,r),k.removeAllChildren()},Kt=(k,r,e)=>{const t=e.parent;if(r){const o=r.children;if(o){for(const i of o)if(B(i,t),i.tagName==="GROUP")for(const n of i.children)B(n,t)}}r.parent.replaceChild(k,r)},Vt=(k,r,e)=>{if(k.w+k.x<=0||k.h+k.y<=0||k.w<=0||k.h<=0)return;const t=r.width,o=r.height,i={x:Math.floor(Math.max(0,k.x)),y:Math.floor(Math.max(0,k.y)),w:Math.floor(Math.min(t,k.w)),h:Math.floor(Math.min(o,k.h))};if(i.x+i.w>t&&(i.w=Math.floor(t-i.x)),i.y+i.h>o&&(i.h=Math.floor(o-i.y)),s.isDecimal(e)){const n=s.decimalPlaces(e),a=Math.pow(10,n),c=s.gcd(a,e*a),l=i.x%c,h=i.x-l;h>=0?(i.x=h,i.w=i.w+l):(i.x=0,i.w=i.w+l-h);const p=i.y%c,d=i.y-p;d>=0?(i.y=d,i.h=i.h+p):(i.y=0,i.h=i.h+p-d)}return i};class _t{constructor(r){g(this,"vNodes");g(this,"thread");g(this,"serviceWorkShapes",new Map);g(this,"localWorkShapes",new Map);g(this,"tmpOpt");g(this,"animationId");g(this,"syncUnitTime",s.DefaultAppliancePluginExtrasOptions.syncOpt.interval);this.vNodes=r.vNodes,this.thread=r.thread}createLocalWork(r){const{workId:e,opt:t,toolsType:o}=r;if(e&&t){const i=e.toString();!this.getToolsOpt()&&o&&this.setToolsOpt({toolsType:o,toolsOpt:t}),this.setWorkOptions(i,t)}}getLocalWorkShape(r){return this.localWorkShapes.get(r)}createLocalWorkShape(r,e){if(r&&this.tmpOpt){const t={toolsType:this.tmpOpt.toolsType,toolsOpt:e||this.tmpOpt.toolsOpt},o=this.createWorkShapeNode({...t,workId:r});return o&&this.localWorkShapes.set(r,{node:o,toolsType:o.toolsType,workState:s.EvevtWorkState.Start}),o}}canUseTopLayer(r){return r===s.EToolsKey.LaserPen}destroy(){this.clearAll()}clearAll(){this.thread.topLayer.children.length&&(this.thread.topLayer.parent.children.forEach(r=>{r.name!=="viewport"&&r.remove()}),U(this.thread.serviceLayer,this.thread.serviceLayer.parent)),this.serviceWorkShapes.clear(),this.localWorkShapes.clear()}consumeDraw(r){const{workId:e,dataType:t}=r;if(t===s.EDataType.Service)this.activeServiceWorkShape(r);else{const o=e==null?void 0:e.toString(),i=o&&this.localWorkShapes.get(o);if(!i)return;const n=i.node.consume({data:r,isFullWork:!1,isSubWorker:!0});n.rect&&(i.result=n,i.workState=s.EvevtWorkState.Doing,o&&this.localWorkShapes.set(o,i))}this.runAnimation()}setToolsOpt(r){var e;this.tmpOpt=r,(e=r.toolsOpt)!=null&&e.syncUnitTime&&(this.syncUnitTime=r.toolsOpt.syncUnitTime)}getToolsOpt(){return this.tmpOpt}createWorkShapeNode(r){const{toolsType:e}=r;if(e===s.EToolsKey.LaserPen)return ie({...r,vNodes:this.vNodes,fullLayer:this.thread.topLayer,drawLayer:this.thread.topLayer})}setNodeKey(r,e,t,o){return e.toolsType=t,e.node=this.createWorkShapeNode({workId:r,toolsType:t,toolsOpt:o}),e}activeServiceWorkShape(r){var d,u;const{workId:e,opt:t,toolsType:o,type:i,updateNodeOpt:n,ops:a,op:c}=r;if(!e)return;const l=e.toString(),h=(d=this.vNodes.get(l))==null?void 0:d.rect;if(!((u=this.serviceWorkShapes)!=null&&u.has(l))){let f={toolsType:o,animationWorkData:c||[],animationIndex:0,type:i,updateNodeOpt:n,ops:a,oldRect:h};o&&t&&(f=this.setNodeKey(l,f,o,t)),this.serviceWorkShapes.set(l,f)}const p=this.serviceWorkShapes.get(l);i&&(p.type=i),a&&(p.animationWorkData=s.transformToNormalData(a),p.ops=a),n&&(p.updateNodeOpt=n),c&&(p.animationWorkData=c),p.node&&p.node.getWorkId()!==l&&p.node.setWorkId(l),h&&(p.oldRect=h),o&&t&&(p.toolsType!==o&&o&&t&&this.setNodeKey(l,p,o,t),p.node&&p.node.setWorkOptions(t))}computNextAnimationIndex(r,e){var i;const t=((i=r.node)==null?void 0:i.syncUnitTime)||this.syncUnitTime,o=Math.floor((r.animationWorkData||[]).slice(r.animationIndex).length*32/e/t)*e;return Math.min((r.animationIndex||0)+(o||e),(r.animationWorkData||[]).length)}animationDraw(){var o,i,n,a;this.animationId=void 0;let r=!1;const e=new Map,t=[];for(const[c,l]of this.serviceWorkShapes.entries())switch(l.toolsType){case s.EToolsKey.LaserPen:{const p=this.computNextAnimationIndex(l,8),d=Math.max(0,l.animationIndex||0),u=(l.animationWorkData||[]).slice(d,p);if((l.animationIndex||0)<p&&((o=l.node)==null||o.consumeService({op:u,isFullWork:!1}),l.animationIndex=p,u.length&&e.set(c,{workState:d===0?s.EvevtWorkState.Start:p===((i=l.animationWorkData)==null?void 0:i.length)?s.EvevtWorkState.Done:s.EvevtWorkState.Doing,op:u.slice(-2)})),l.isDel){(n=l.node)==null||n.clearTmpPoints(),this.serviceWorkShapes.delete(c);break}l.ops&&l.animationIndex===((a=l.animationWorkData)==null?void 0:a.length)&&!l.isDel&&(this.thread.topLayer.getElementsByName(c.toString())[0]||(l.isDel=!0,this.serviceWorkShapes.set(c,l))),r=!0;break}}for(const[c,l]of this.localWorkShapes.entries()){const{result:h,toolsType:p,isDel:d,workState:u}=l;switch(p){case s.EToolsKey.LaserPen:{if(d){l.node.clearTmpPoints(),this.localWorkShapes.delete(c),t.push({removeIds:[c.toString()],type:s.EPostMessageType.RemoveNode});break}h&&((h.op||h.ops)&&t.push(h),l.result=void 0),!this.thread.topLayer.getElementsByName(c.toString())[0]&&u===s.EvevtWorkState.Done&&(l.isDel=!0,this.localWorkShapes.set(c,l)),r=!0;break}}}r&&this.runAnimation(),e.size&&e.forEach((c,l)=>{t.push({type:s.EPostMessageType.Cursor,uid:l.split(s.Storage_Splitter)[0],op:c.op,workState:c.workState,viewId:this.thread.viewId})}),t.length&&this.thread.post({sp:t})}runAnimation(){this.animationId||(this.animationId=requestAnimationFrame(this.animationDraw.bind(this)))}setWorkOptions(r,e){var o;let t=(o=this.localWorkShapes.get(r))==null?void 0:o.node;if(!t&&this.tmpOpt){const{toolsType:i}=this.tmpOpt;this.tmpOpt.toolsOpt=e,t=this.createWorkShapeNode({workId:r,toolsType:i,toolsOpt:e}),t&&this.localWorkShapes.set(r,{node:t,toolsType:i,workState:s.EvevtWorkState.Start}),this.setToolsOpt(this.tmpOpt)}e!=null&&e.syncUnitTime||(e.syncUnitTime=this.syncUnitTime),t&&t.setWorkOptions(e)}consumeDrawAll(r){const{workId:e,dataType:t}=r;if(t===s.EDataType.Service)this.activeServiceWorkShape(r);else{const o=e==null?void 0:e.toString(),i=o&&this.localWorkShapes.get(o);if(!i)return;const n=i.node.consumeAll({data:r});i.result=n,i.workState=s.EvevtWorkState.Done,o&&this.localWorkShapes.set(o,i)}this.runAnimation()}}class Gt{constructor(r){g(this,"vNodes");g(this,"thread");g(this,"workShapes",new Map);g(this,"effectSelectNodeData",new Set);g(this,"batchEraserRemoveNodes",new Set);g(this,"batchEraserWorks",new Set);g(this,"tmpOpt");g(this,"syncUnitTime",s.DefaultAppliancePluginExtrasOptions.syncOpt.interval);g(this,"fullWorkerDrawCount",0);g(this,"drawWorkActiveId");g(this,"consumeCount",0);g(this,"combineTimerId");g(this,"combineDrawResolve");g(this,"combineDrawActiveId");this.vNodes=r.vNodes,this.thread=r.thread}async loadImageBitMap(r){return await this.thread.loadImageBitMap(r)}createLocalWork(r){const{workId:e,opt:t,toolsType:o}=r;if(e&&t){const i=e.toString();!this.getToolsOpt()&&o&&this.setToolsOpt({toolsType:o,toolsOpt:t}),this.setWorkOptions(i,t)}}async updateSelector(r){var u;const e=this.workShapes.get(s.Storage_Selector_key);if(!((u=e==null?void 0:e.selectIds)!=null&&u.length))return;const{callback:t,...o}=r,{updateSelectorOpt:i,willSerializeData:n,smoothSync:a}=o,c=await(e==null?void 0:e.updateSelector({updateSelectorOpt:i,selectIds:$.cloneDeep(e.selectIds),vNodes:this.vNodes,willSerializeData:n,worker:this})),l=new Map;let h;c!=null&&c.selectIds&&(h=$.xor(e.selectIds,c.selectIds),c.selectIds.forEach(f=>{const y=this.vNodes.get(f);if(y){const{toolsType:m,op:w,opt:P}=y;l.set(f,{opt:P,toolsType:m,ops:(w==null?void 0:w.length)&&s.transformToSerializableData(w)||void 0})}}),e.selectIds=c.selectIds);const p=[],d=t&&t({res:c,workShapeNode:e,param:o,postData:{sp:p},newServiceStore:l,smoothSync:a})||{sp:p};h&&d.sp.push({type:s.EPostMessageType.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()}),U(this.thread.localLayer,this.thread.localLayer.parent)),this.workShapes.get(s.Storage_Selector_key)){const e=[];e.push({type:s.EPostMessageType.Select,dataType:s.EDataType.Local,selectIds:[],willSyncService:!1}),this.thread.post({sp:e})}this.workShapes.clear(),this.effectSelectNodeData.clear(),this.batchEraserWorks.clear(),this.batchEraserRemoveNodes.clear()}async checkTextActive(r){const{op:e,viewId:t,dataType:o}=r;if(e!=null&&e.length){let i;for(const n of this.vNodes.curNodeMap.values()){const{rect:a,name:c,toolsType:l,opt:h}=n,p=e[0]*this.thread.fullLayer.worldScaling[0]+this.thread.fullLayer.worldPosition[0],d=e[1]*this.thread.fullLayer.worldScaling[1]+this.thread.fullLayer.worldPosition[1];if(l===s.EToolsKey.Text&&s.isIntersectForPoint([p,d],a)&&h.workState===s.EvevtWorkState.Done){i=c;break}}i&&(await this.blurSelector({viewId:t,msgType:s.EPostMessageType.Select,dataType:o,isSync:!0}),this.thread.post({sp:[{type:s.EPostMessageType.GetTextActive,toolsType:s.EToolsKey.Text,workId:i}]}))}}cursorHover(r){const{opt:e,toolsType:t,point:o}=r,i=this.setFullWork({workId:s.Cursor_Hover_Id,toolsType:t,opt:e});i&&o&&i.cursorHover(o)}cursorBlur(){var e;const r=this.getWorkShape(s.Cursor_Hover_Id);r&&((e=r.selectIds)!=null&&e.length)&&(r.cursorBlur(),this.clearWorkShapeNodeCache(s.Cursor_Hover_Id)),this.thread.fullLayer.parent.children.forEach(t=>{t.name==="Cursor_Hover_Id"&&t.remove()})}updateFullSelectWork(r){var o,i,n,a,c;const e=this.workShapes.get(s.Storage_Selector_key),{selectIds:t}=r;if(!(t!=null&&t.length)){this.blurSelector(r);return}if(!e){const l=this.setFullWork(r);!l&&r.workId&&this.tmpOpt&&((o=this.tmpOpt)==null?void 0:o.toolsType)===s.EToolsKey.Selector&&this.setWorkOptions(r.workId.toString(),r.opt||this.tmpOpt.toolsOpt),l&&this.updateFullSelectWork(r);return}if(e&&(t!=null&&t.length)){const{selectRect:l}=e.updateSelectIds(t),h=[{...r,selectorColor:((i=r.opt)==null?void 0:i.strokeColor)||e.selectorColor,strokeColor:((n=r.opt)==null?void 0:n.strokeColor)||e.strokeColor,fillColor:((a=r.opt)==null?void 0:a.fillColor)||e.fillColor,textOpt:((c=r.opt)==null?void 0:c.textOpt)||e.textOpt,canTextEdit:e.canTextEdit,canRotate:e.canRotate,scaleType:e.scaleType,type:s.EPostMessageType.Select,selectRect:l,points:e.getChildrenPoints(),willSyncService:(r==null?void 0:r.willSyncService)||!1,opt:(r==null?void 0:r.willSyncService)&&e.getWorkOptions()||void 0,canLock:e.canLock,isLocked:e.isLocked,toolsTypes:e.toolsTypes,shapeOpt:e.shapeOpt,thickness:e.thickness,useStroke:e.useStroke,strokeType:e.strokeType}];this.thread.post({sp:h})}}async removeSelector(r){const{willSyncService:e,needUndoTicker:t}=r,o=[],i=[],n=this.workShapes.get(s.Storage_Selector_key);if(!n)return;const a=n.selectIds&&[...n.selectIds]||[];for(const c of a){const l=this.vNodes.get(c);if(l)switch(l.toolsType){case s.EToolsKey.Text:{o.push({type:s.EPostMessageType.TextUpdate,toolsType:s.EToolsKey.Text,workId:c,dataType:s.EDataType.Local});break}case s.EToolsKey.BackgroundSVG:o.push({type:s.EPostMessageType.BackgroundSVGDelete,toolsType:s.EToolsKey.BackgroundSVG,workId:c,dataType:s.EDataType.Local,viewId:this.thread.viewId});break}this.removeNode(c),i.push(c)}i.length&&o.push({type:s.EPostMessageType.RemoveNode,removeIds:i}),o.push({type:s.EPostMessageType.Select,selectIds:[],willSyncService:e}),await this.blurSelector(),t&&o.push({type:s.EPostMessageType.None,needUndoTicker:t}),o.length&&this.thread.post({sp:o})}removeWork(r){const{workId:e}=r,t=e==null?void 0:e.toString();t&&this.removeNode(t)}removeNode(r){var o;const e=this.vNodes.get(r);e&&(e.toolsType===s.EToolsKey.BackgroundSVG&&this.thread.post({sp:[{type:s.EPostMessageType.BackgroundSVGDelete,toolsType:s.EToolsKey.BackgroundSVG,workId:r,dataType:s.EDataType.Local,viewId:this.thread.viewId}]}),(o=this.thread.fullLayer)==null||o.getElementsByName(r).forEach(i=>{i.remove(),B(i,this.thread.fullLayer.parent)}),this.vNodes.delete(r)),this.workShapes.has(r)&&(this.thread.localLayer.getElementsByName(r).forEach(i=>{i.remove(),B(i,this.thread.localLayer.parent)}),this.clearWorkShapeNodeCache(r))}setFullWork(r){const{workId:e,opt:t,toolsType:o}=r;if(e&&t&&o){const i=e.toString();let n;return e&&this.workShapes.has(i)?(n=this.workShapes.get(i),n==null||n.setWorkOptions(t)):n=this.createWorkShapeNode({toolsOpt:t,toolsType:o,workId:i}),n?(this.workShapes.set(i,n),n):void 0}}async consumeFull(r){var o;const e=this.setFullWork(r),t=r.ops&&s.transformToNormalData(r.ops);if(e){const i=(o=r.workId)==null?void 0:o.toString();e.toolsType===s.EToolsKey.BackgroundSVG?e.consumeService({isFullWork:!0,replaceId:i}):e.toolsType===s.EToolsKey.Image?await e.consumeServiceAsync({isFullWork:!0,replaceId:i,worker:this}):e.toolsType===s.EToolsKey.Text?await e.consumeServiceAsync({isFullWork:!0,replaceId:i,boxRect:this.thread.getSceneRect()}):e.consumeService({op:t,isFullWork:!0,replaceId:i}),r!=null&&r.updateNodeOpt&&e.updataOptService(r.updateNodeOpt);const n=[];r.workId&&this.workShapes.delete(r.workId.toString()),r.willSyncService&&n.push({opt:r.opt,toolsType:r.toolsType,type:s.EPostMessageType.FullWork,workId:r.workId,ops:r.ops,updateNodeOpt:r.updateNodeOpt,viewId:this.thread.viewId}),r.needUndoTicker&&n.push({type:s.EPostMessageType.None,needUndoTicker:r.needUndoTicker}),n.length&&this.thread.post({sp:n})}}async colloctEffectSelectWork(r){const e=this.workShapes.get(s.Storage_Selector_key),{workId:t,msgType:o}=r;if(e&&t&&e.selectIds&&e.selectIds.includes(t.toString())){o===s.EPostMessageType.RemoveNode?e.selectIds=e.selectIds.filter(i=>i!==t.toString()):this.effectSelectNodeData.add(r),await new Promise(i=>{setTimeout(()=>{i(!0)},0)}),await this.runEffectSelectWork(!0).then(()=>{var i;(i=this.effectSelectNodeData)==null||i.clear()});return}return r}async runEffectSelectWork(r){var e;for(const t of this.effectSelectNodeData.values()){const o=this.setFullWork(t);if(o){const i=(e=t.workId)==null?void 0:e.toString();if(o.toolsType===s.EToolsKey.BackgroundSVG)o.consumeService({isFullWork:!0,replaceId:i});else if(o.toolsType===s.EToolsKey.Image)await o.consumeServiceAsync({isFullWork:!0,replaceId:i,worker:this});else if(o.toolsType===s.EToolsKey.Text)await o.consumeServiceAsync({isFullWork:!0,replaceId:i,boxRect:this.thread.getSceneRect()});else{const n=t.ops&&s.transformToNormalData(t.ops);o.consumeService({op:n,isFullWork:!0,replaceId:i}),t!=null&&t.updateNodeOpt&&o.updataOptService(t.updateNodeOpt)}t.workId&&this.workShapes.delete(t.workId.toString())}}this.reRenderSelector(r)}hasSelector(){return this.workShapes.has(s.Storage_Selector_key)}getSelector(){return this.workShapes.get(s.Storage_Selector_key)}reRenderSelector(r=!1){var o;const e=this.workShapes.get(s.Storage_Selector_key);if(!e)return;if(e&&!((o=e.selectIds)!=null&&o.length))return this.blurSelector();const t=e.reRenderSelector();t&&this.thread.post({sp:[{type:s.EPostMessageType.Select,selectIds:e.selectIds,selectRect:t,willSyncService:r,viewId:this.thread.viewId,points:e.getChildrenPoints(),textOpt:e.textOpt,selectorColor:e.selectorColor,strokeColor:e.strokeColor,fillColor:e.fillColor,canTextEdit:e.canTextEdit,canRotate:e.canRotate,scaleType:e.scaleType,opt:e.getWorkOptions()||void 0,canLock:e.canLock,isLocked:e.isLocked,toolsTypes:e.toolsTypes,shapeOpt:e.shapeOpt,thickness:e.thickness,useStroke:e.useStroke,strokeType:e.strokeType}]})}async blurSelector(r){var o;const e=this.workShapes.get(s.Storage_Selector_key),t=e==null?void 0:e.blurSelector();if(this.clearWorkShapeNodeCache(s.Storage_Selector_key),((o=this.thread.fullLayer)==null?void 0:o.parent).children.forEach(i=>{i.name===s.Storage_Selector_key&&i.remove()}),t){const i=[];i.push({...t,isSync:r==null?void 0:r.isSync}),this.thread.post({sp:i})}}clearWorkShapeNodeCache(r){var e;(e=this.getWorkShape(r))==null||e.clearTmpPoints(),this.workShapes.delete(r)}async drawBitMapEraserFull(r,e,t,o){const{willUpdateNodes:i,willDeleteNodes:n}=e,a=r.getWorkId(),c=[{type:s.EPostMessageType.RemoveNode,removeIds:[a],viewId:this.thread.viewId}];if(t&&c.push({type:s.EPostMessageType.None,isLockSentEventCursor:t}),i!=null&&i.size||n!=null&&n.size){if(i!=null&&i.size)for(const[l,h]of i)c.push({type:s.EPostMessageType.UpdateNode,dataType:s.EDataType.Local,opt:h.opt,workId:l,updateNodeOpt:{useAnimation:!1}});n!=null&&n.size&&c.push({type:s.EPostMessageType.RemoveNode,removeIds:[...n],viewId:this.thread.viewId})}o&&c.push({type:s.EPostMessageType.None,needUndoTicker:o}),c.length&&this.thread.post({sp:c})}drawPencilEraserFull(r,e,t,o){const{willNewNodes:i,willDeleteNodes:n}=e,a=r.getWorkId(),c=[{type:s.EPostMessageType.RemoveNode,removeIds:[a],viewId:this.thread.viewId}];if(t&&c.push({type:s.EPostMessageType.None,isLockSentEventCursor:t}),i!=null&&i.size||n!=null&&n.size){if(i!=null&&i.size)for(const[l,h]of i)c.push({type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,toolsType:h.toolsType,ops:s.transformToSerializableData(h.op),opt:h.opt,workId:l,updateNodeOpt:{useAnimation:!1}});n!=null&&n.size&&c.push({type:s.EPostMessageType.RemoveNode,removeIds:[...n],viewId:this.thread.viewId})}o&&c.push({type:s.EPostMessageType.None,needUndoTicker:o}),c.length&&this.thread.post({sp:c})}drawEraser(r,e,t){const o=[];r.removeIds&&o.push(r),e&&o.push({type:s.EPostMessageType.None,isLockSentEventCursor:e}),t&&o.push({type:s.EPostMessageType.None,needUndoTicker:t}),this.thread.post({sp:o,consumeCount:this.consumeCount})}getWorkShape(r){return this.workShapes.get(r)}getWorkShapes(){return this.workShapes}consumeDraw(r,e){const{op:t,workId:o,scenePath:i,postCount:n,smoothSync:a}=r;if(t!=null&&t.length&&o){const c=o.toString(),l=this.workShapes.get(c);if(!l)return;const h=l.toolsType;if(h===s.EToolsKey.LaserPen)return;switch(this.combineDrawActiveId&&this.combineDrawActiveId!==c&&(this.combineTimerId&&(clearTimeout(this.combineTimerId),this.combineTimerId=void 0,this.combineDrawResolve&&this.combineDrawResolve(!1),this.combineDrawActiveId=void 0),this.consumeDrawAll({workId:this.combineDrawActiveId,scenePath:i,viewId:this.thread.viewId,msgType:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local},e)),this.drawWorkActiveId&&this.drawWorkActiveId!==c&&(this.consumeDrawAll({workId:this.drawWorkActiveId,scenePath:i,viewId:this.thread.viewId,msgType:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local},e),this.drawWorkActiveId=void 0),!this.drawWorkActiveId&&c!==s.Storage_Selector_key&&(this.drawWorkActiveId=c),$.isNumber(n)&&(this.consumeCount=n),h){case s.EToolsKey.Selector:{const p=l.consume({data:r,isFullWork:!0,isMainThread:!0});this.fullWorkerDrawCount++;const d=[];p.type===s.EPostMessageType.Select&&(p.selectIds&&e.runReverseSelectWork(p.selectIds),d.push(p)),this.thread.post({consumeCount:this.consumeCount,fullWorkerDrawCount:this.fullWorkerDrawCount,sp:d});break}case s.EToolsKey.PencilEraser:case s.EToolsKey.BitMapEraser:{l.consume({data:r,isFullWork:!1,isMainThread:!0}),this.fullWorkerDrawCount++,this.thread.post({sp:void 0,consumeCount:this.consumeCount,fullWorkerDrawCount:this.fullWorkerDrawCount}),this.combineTimerId||new Promise(p=>{this.combineDrawActiveId=c,this.combineDrawResolve=p,this.combineTimerId=s.requestAsyncCallBack(()=>{this.combineTimerId=void 0,this.combineDrawResolve&&this.combineDrawResolve(!0)},this.thread.master.maxCombineEraserTime,this.thread.master.control.hasPolyfillMethod("requestIdleCallback"))}).then(p=>{p&&this.drawEraserCombine(c),this.combineDrawResolve=void 0});break}case s.EToolsKey.Eraser:{const p=l.consume({data:r,isFullWork:!0});this.drawEraser(p)}break;case s.EToolsKey.Arrow:case s.EToolsKey.Straight:case s.EToolsKey.Ellipse:case s.EToolsKey.Rectangle:case s.EToolsKey.Star:case s.EToolsKey.Polygon:case s.EToolsKey.SpeechBalloon:case s.EToolsKey.Pencil:{const p=l.consume({data:r,isFullWork:!1,isMainThread:!0,smoothSync:a});p&&(this.fullWorkerDrawCount++,this.thread.post({consumeCount:this.consumeCount,fullWorkerDrawCount:this.fullWorkerDrawCount,sp:p.op&&[{...p,scenePath:i}]||void 0}))}break}}}drawEraserCombine(r){var t,o,i,n;const e=(t=this.workShapes.get(r))==null?void 0:t.combineConsume({workerEngine:this});if(e){const{willDeleteNodes:a,willNewNodes:c}=e,l={render:[],sp:[]};if(e!=null&&e.rect){const h=s.getSafetyRect(e.rect);(o=l.render)==null||o.push({rect:h,isClear:!0,clearCanvas:s.ECanvasShowType.Bg,viewId:this.thread.viewId},{rect:h,drawCanvas:s.ECanvasShowType.Bg,viewId:this.thread.viewId})}if(a!=null&&a.size&&((i=l.sp)==null||i.push({type:s.EPostMessageType.RemoveNode,removeIds:[...a],viewId:this.thread.viewId})),c!=null&&c.size)for(const[h,p]of c)(n=l.sp)==null||n.push({type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,toolsType:p.toolsType,ops:s.transformToSerializableData(p.op),opt:p.opt,workId:h,updateNodeOpt:{useAnimation:!1}});this.thread.post(l)}}consumeDrawAll(r,e){var n,a,c;const{workId:t,scenePath:o,isLockSentEventCursor:i}=r;if(t){this.combineTimerId&&(clearTimeout(this.combineTimerId),this.combineTimerId=void 0,this.combineDrawResolve&&this.combineDrawResolve(!1),this.combineDrawActiveId=void 0);const l=t.toString();this.drawWorkActiveId===l&&(this.drawWorkActiveId=void 0);const h=this.workShapes.get(l);if(!h)return;const p=h.toolsType;if(p===s.EToolsKey.LaserPen)return;const d=this.workShapes.get(s.Cursor_Hover_Id),u=(n=d==null?void 0:d.selectIds)==null?void 0:n[0],f=h.consumeAll({data:r,workerEngine:this});switch(p){case s.EToolsKey.Selector:{f.selectIds&&u&&((a=f.selectIds)!=null&&a.includes(u))&&d.cursorBlur();const y=[];i&&y.push({type:s.EPostMessageType.None,isLockSentEventCursor:i}),f.type===s.EPostMessageType.Select&&(f.selectIds&&e.runReverseSelectWork(f.selectIds),y.push({...f,scenePath:o})),y.length&&this.thread.post({sp:y}),(c=h.selectIds)!=null&&c.length?h.clearTmpPoints():this.clearWorkShapeNodeCache(l)}break;case s.EToolsKey.PencilEraser:this.drawPencilEraserFull(h,f,i,r.needUndoTicker),this.fullWorkerDrawCount=0,this.clearWorkShapeNodeCache(l);break;case s.EToolsKey.BitMapEraser:this.drawBitMapEraserFull(h,f,i,r.needUndoTicker),this.fullWorkerDrawCount=0,this.clearWorkShapeNodeCache(l);break;case s.EToolsKey.Eraser:this.drawEraser({...f,scenePath:o},i,r.needUndoTicker),h.clearTmpPoints();break;case s.EToolsKey.Arrow:case s.EToolsKey.Straight:case s.EToolsKey.Ellipse:case s.EToolsKey.Rectangle:case s.EToolsKey.Star:case s.EToolsKey.Polygon:case s.EToolsKey.SpeechBalloon:case s.EToolsKey.Pencil:{const y=[];i&&y.push({type:s.EPostMessageType.None,isLockSentEventCursor:i}),f&&(y.push(f),this.fullWorkerDrawCount=0,r.needUndoTicker&&y.push({type:s.EPostMessageType.None,needUndoTicker:r.needUndoTicker}),this.thread.post({fullWorkerDrawCount:this.fullWorkerDrawCount,sp:y})),this.clearWorkShapeNodeCache(l);break}}}}getToolsOpt(){return this.tmpOpt}setToolsOpt(r){var e;this.tmpOpt=r,(e=r.toolsOpt)!=null&&e.syncUnitTime&&(this.syncUnitTime=r.toolsOpt.syncUnitTime)}setWorkOptions(r,e){let t=this.workShapes.get(r);if(!t&&this.tmpOpt){const{toolsType:o}=this.tmpOpt;this.tmpOpt.toolsOpt=e,t=this.createWorkShapeNode({workId:r,toolsType:o,toolsOpt:e}),t&&this.workShapes.set(r,t),this.setToolsOpt(this.tmpOpt)}e.syncUnitTime||(e.syncUnitTime=this.syncUnitTime),t==null||t.setWorkOptions(e)}createWorkShapeNode(r){return ie({...r,vNodes:this.vNodes,fullLayer:this.thread.fullLayer,drawLayer:this.thread.localLayer},this.thread.serviceWork)}}class Xt{constructor(r){g(this,"vNodes");g(this,"thread");g(this,"workShapes",new Map);g(this,"selectorWorkShapes",new Map);g(this,"willRunEffectSelectorIds",new Set);g(this,"runEffectId");g(this,"animationId");g(this,"syncUnitTime",s.DefaultAppliancePluginExtrasOptions.syncOpt.interval);this.vNodes=r.vNodes,this.thread=r.thread}async loadImageBitMap(r){return await this.thread.loadImageBitMap(r)}destroy(){this.clearAll()}clearAll(){this.thread.serviceLayer.children.length&&(this.thread.serviceLayer.parent.children.forEach(r=>{r.name!=="viewport"&&r.remove()}),U(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(r=>{var t,o;const e=this.selectorWorkShapes.get(r);e&&e.selectIds&&((t=e.node)==null||t.selectServiceNode(r,e,!0)),(o=e==null?void 0:e.selectIds)!=null&&o.length||this.selectorWorkShapes.delete(r)}),this.willRunEffectSelectorIds.clear()}runSelectWork(r){this.activeSelectorShape(r);const{workId:e}=r,t=e==null?void 0:e.toString();t&&this.willRunEffectSelectorIds.add(t),this.runEffect()}removeWork(r){const{workId:e}=r,t=e==null?void 0:e.toString();if(t){if(this.workShapes.get(t)){this.workShapes.delete(t),this.removeNode(t,r);return}this.removeNode(t,r)}}consumeFull(r){this.activeWorkShape(r),this.runAnimation()}runReverseSelectWork(r){r.forEach(e=>{this.selectorWorkShapes.forEach((t,o)=>{var i;if((i=t.selectIds)!=null&&i.length){const n=t.selectIds.indexOf(e);n>-1&&(t.selectIds.splice(n,1),this.willRunEffectSelectorIds.add(o))}})}),this.willRunEffectSelectorIds.size&&this.runEffect()}consumeDraw(r){this.activeWorkShape(r),this.runAnimation()}computNextAnimationIndex(r,e){const t=Math.floor((r.animationWorkData||[]).slice(r.animationIndex).length*32/e/this.syncUnitTime)*e;return Math.min((r.animationIndex||0)+(t||e),(r.animationWorkData||[]).length)}async animationDraw(){var t,o,i,n,a,c,l,h,p,d,u,f,y,m,w,P,T,S,E,I,R;this.animationId=void 0;let r=!1;const e=new Map;for(const[M,v]of this.workShapes.entries())switch(v.toolsType){case s.EToolsKey.BackgroundSVG:{(t=v.node)==null||t.consumeService({isFullWork:!0,replaceId:M}),this.selectorWorkShapes.forEach((L,C)=>{var b;(b=L.selectIds)!=null&&b.includes(M)&&(this.willRunEffectSelectorIds.add(C),this.runEffect())}),this.workShapes.delete(M);break}case s.EToolsKey.Image:{await((o=v.node)==null?void 0:o.consumeServiceAsync({isFullWork:!0,worker:this})),this.selectorWorkShapes.forEach((L,C)=>{var b;(b=L.selectIds)!=null&&b.includes(M)&&(this.willRunEffectSelectorIds.add(C),this.runEffect())}),this.workShapes.delete(M);break}case s.EToolsKey.Text:{v.node&&(await((i=v.node)==null?void 0:i.consumeServiceAsync({isFullWork:!0,replaceId:M,boxRect:this.thread.getSceneRect()})),this.selectorWorkShapes.forEach((L,C)=>{var b;(b=L.selectIds)!=null&&b.includes(M)&&(this.willRunEffectSelectorIds.add(C),this.runEffect())}),(n=v.node)==null||n.clearTmpPoints(),this.workShapes.delete(M));break}case s.EToolsKey.Arrow:case s.EToolsKey.Straight:case s.EToolsKey.Rectangle:case s.EToolsKey.Ellipse:case s.EToolsKey.Star:case s.EToolsKey.Polygon:case s.EToolsKey.SpeechBalloon:{const L=!!v.ops;if((a=v.animationWorkData)!=null&&a.length){const C=v.oldRect;(c=v.node)==null||c.consumeService({op:v.animationWorkData,isFullWork:L}),L&&(this.selectorWorkShapes.forEach((b,N)=>{var x;(x=b.selectIds)!=null&&x.includes(M)&&(this.willRunEffectSelectorIds.add(N),this.runEffect())}),(l=v.node)==null||l.clearTmpPoints(),this.workShapes.delete(M)),v.isEnableCursor?e.set(M,{workState:C?v.ops?s.EvevtWorkState.Done:s.EvevtWorkState.Doing:s.EvevtWorkState.Start,op:v.animationWorkData.slice(-3,-1)}):L&&!v.useAnimation&&((h=v.updateNodeOpt)!=null&&h.useAnimation)&&e.set(M,{workState:s.EvevtWorkState.Done,op:v.animationWorkData.slice(-3,-1),uid:(p=v.updateNodeOpt)==null?void 0:p.uid}),v.animationWorkData.length=0}break}case s.EToolsKey.Pencil:{if(v.useAnimation){if(v.useAnimation){if(v.isDel){(P=v.node)==null||P.clearTmpPoints(),this.workShapes.delete(M);break}const L=3,C=this.computNextAnimationIndex(v,L),b=v.isDiff?0:Math.max(0,(v.animationIndex||0)-L),N=(v.animationWorkData||[]).slice(b,C),x=(S=(T=v.node)==null?void 0:T.getWorkId())==null?void 0:S.toString();if((v.animationIndex||0)<C||v.isDiff){if((E=v.node)==null||E.consumeService({op:N,isFullWork:!1}),v.animationIndex=C,v.isDiff&&(v.isDiff=!1),N.length&&v.isEnableCursor){const O=N.slice(-3,-1);e.set(M,{workState:b===0?s.EvevtWorkState.Start:C===((I=v.animationWorkData)==null?void 0:I.length)?s.EvevtWorkState.Done:s.EvevtWorkState.Doing,op:O})}}else v.ops&&((R=v.node)==null||R.consumeService({op:v.animationWorkData||[],isFullWork:!0,replaceId:x}),v.isDel=!0,v.isEnableCursor&&e.set(M,{workState:s.EvevtWorkState.Done,op:N.slice(-3,-1)}));r=!0;break}}else{const L=!!v.ops;if((d=v.node)==null||d.consumeService({op:v.animationWorkData||[],isFullWork:L,replaceId:M}),(u=v.node)==null||u.updataOptService(v.updateNodeOpt),L){if(!v.isEnableCursor&&((f=v.updateNodeOpt)!=null&&f.useAnimation)&&((y=v.animationWorkData)!=null&&y.length)){const C=v.animationWorkData.slice(-3,-1);e.set(M,{workState:s.EvevtWorkState.Done,op:C,uid:(m=v.updateNodeOpt)==null?void 0:m.uid})}this.selectorWorkShapes.forEach((C,b)=>{var N;(N=C.selectIds)!=null&&N.includes(M)&&(this.willRunEffectSelectorIds.add(b),this.runEffect())}),(w=v.node)==null||w.clearTmpPoints(),this.workShapes.delete(M)}}break}}if(r&&this.runAnimation(),e.size){const M=[];e.forEach((v,L)=>{M.push({type:s.EPostMessageType.Cursor,uid:v.uid||L.split(s.Storage_Splitter)[0],op:v.op,workState:v.workState,viewId:this.thread.viewId})}),this.thread.post({sp:M})}}runAnimation(){this.animationId||(this.animationId=requestAnimationFrame(this.animationDraw.bind(this)))}hasDiffData(r,e,t){const o=r.length;if(e.length<o)return!0;switch(t){case s.EToolsKey.Pencil:{for(let i=0;i<o;i+=3)if(e[i]!==r[i]||e[i+1]!==r[i+1])return!0;break}case s.EToolsKey.LaserPen:{for(let i=0;i<o;i+=2)if(e[i]!==r[i]||e[i+1]!==r[i+1])return!0;break}}return!1}activeWorkShape(r){var y,m,w,P;const{workId:e,opt:t,toolsType:o,type:i,updateNodeOpt:n,ops:a,op:c,useAnimation:l,imageBitmap:h,isEnableCursor:p}=r;if(!e)return;const d=e.toString(),u=(y=this.vNodes.get(d))==null?void 0:y.rect;if(!((m=this.workShapes)!=null&&m.has(d))){let T={toolsType:o,animationWorkData:c||[],animationIndex:0,type:i,updateNodeOpt:n,ops:a,useAnimation:typeof l<"u"?l:typeof(n==null?void 0:n.useAnimation)<"u"?n==null?void 0:n.useAnimation:!0,oldRect:u,isDiff:!1,imageBitmap:h,isEnableCursor:p};o&&t&&(T=this.setNodeKey(d,T,o,t)),(w=this.workShapes)==null||w.set(d,T)}const f=(P=this.workShapes)==null?void 0:P.get(d);f.isEnableCursor=p,i&&(f.type=i),a&&(f.animationWorkData=s.transformToNormalData(a),f.ops=a),n&&(f.updateNodeOpt=n),c&&(f.isDiff=this.hasDiffData(f.animationWorkData||[],c,f.toolsType),f.animationWorkData=c),f.node&&f.node.getWorkId()!==d&&f.node.setWorkId(d),u&&(f.oldRect=u),o&&t&&(t.syncUnitTime&&(this.syncUnitTime=t.syncUnitTime),f.toolsType!==o&&o&&t&&this.setNodeKey(d,f,o,t),f.node&&f.node.setWorkOptions(t)),h&&(f.imageBitmap=h)}removeNode(r,e){r.indexOf(s.Storage_Selector_key)>-1&&this.removeSelectWork(e),this.thread.fullLayer.getElementsByName(r).forEach(o=>{o.remove(),B(o,this.thread.fullLayer.parent)}),this.thread.serviceLayer.getElementsByName(r).forEach(o=>{o.remove(),B(o,this.thread.serviceLayer.parent)});const t=this.vNodes.get(r);t&&t.toolsType===s.EToolsKey.BackgroundSVG&&this.thread.post({sp:[{type:s.EPostMessageType.BackgroundSVGDelete,toolsType:s.EToolsKey.BackgroundSVG,workId:r,dataType:s.EDataType.Service,viewId:this.thread.viewId}]}),this.vNodes.delete(r)}removeSelectWork(r){const{workId:e}=r,t=e==null?void 0:e.toString();t&&(this.activeSelectorShape(r),this.willRunEffectSelectorIds.add(t)),this.runEffect()}activeSelectorShape(r){var l,h,p;const{workId:e,opt:t,toolsType:o,type:i,selectIds:n}=r;if(!e)return;const a=e.toString();if(!((l=this.selectorWorkShapes)!=null&&l.has(a))){let d={toolsType:o,selectIds:n,type:i,opt:t};o&&t&&(d=this.setNodeKey(a,d,o,t)),(h=this.selectorWorkShapes)==null||h.set(a,d)}const c=(p=this.selectorWorkShapes)==null?void 0:p.get(a);i&&(c.type=i),c.node&&c.node.getWorkId()!==a&&c.node.setWorkId(a),c.selectIds=n||[]}setNodeKey(r,e,t,o){return e.toolsType=t,e.node=ie({toolsType:t,toolsOpt:o,vNodes:this.vNodes,fullLayer:this.thread.fullLayer,drawLayer:this.thread.serviceLayer,workId:r},this),e}}class G{constructor(){g(this,"localWork");g(this,"serviceWork");g(this,"threadEngine")}registerMainThread(r){return this.threadEngine=r,this.localWork=r.localWork,this.serviceWork=r.serviceWork,this}}class Yt extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.CopyNode)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.FullWork&&o===s.EDataType.Local&&i===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var o;const{workId:t}=e;t&&await((o=this.localWork)==null?void 0:o.consumeFull(e))}}class $t extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.SetColorNode)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var h;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,textUpdateForWoker:c,needUndoTicker:l}=e;t===s.Storage_Selector_key&&o&&await((h=this.localWork)==null?void 0:h.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,textUpdateForWoker:c,callback:this.updateSelectorCallback,needUndoTicker:l}))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i}=e,{willSyncService:n,isSync:a,textUpdateForWoker:c,needUndoTicker:l}=t,h=o.sp||[];if(n)for(const[p,d]of i.entries())c&&d.toolsType===s.EToolsKey.Text?h.push({...d,workId:p,type:s.EPostMessageType.TextUpdate,dataType:s.EDataType.Local,willSyncService:!0}):h.push({...d,workId:p,type:s.EPostMessageType.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:a});return l&&h.push({type:s.EPostMessageType.None,needUndoTicker:l}),{sp:h}}}class Ht extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.ZIndexNode)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var c;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a}=e;t===s.Storage_Selector_key&&o&&await((c=this.localWork)==null?void 0:c.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,callback:this.updateSelectorCallback}))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i}=e,{willSyncService:n,isSync:a}=t,c=o.sp||[];if(n&&c)for(const[l,h]of i.entries())h.toolsType===s.EToolsKey.BackgroundSVG&&c.push({...h,workId:l,type:s.EPostMessageType.BackgroundSVGUpdate,dataType:s.EDataType.Local,updateNodeOpt:{useAnimation:!1}}),c.push({...h,workId:l,type:s.EPostMessageType.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:a});return{sp:c}}}class qt extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.TranslateNode)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return await this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var d,u;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,textUpdateForWoker:c,emitEventType:l,smoothSync:h,needUndoTicker:p}=e;t===s.Storage_Selector_key&&o&&(o.workState===s.EvevtWorkState.Done&&(o!=null&&o.translate)&&(o.translate[0]||o.translate[1])||o.workState!==s.EvevtWorkState.Done?await((d=this.localWork)==null?void 0:d.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,isSync:!0,textUpdateForWoker:c,emitEventType:l,callback:this.updateSelectorCallback,smoothSync:h,needUndoTicker:p})):o.workState===s.EvevtWorkState.Done&&((u=this.localWork)==null||u.vNodes.deleteLastTarget()))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i,workShapeNode:n,res:a,smoothSync:c}=e,{willSyncService:l,isSync:h,updateSelectorOpt:p,textUpdateForWoker:d,needUndoTicker:u}=t,f=p.workState,y=o.sp||[];if(f===s.EvevtWorkState.Start)return{sp:[],render:[]};const m=a==null?void 0:a.selectRect;if(l){y.push({type:s.EPostMessageType.Select,selectIds:n.selectIds,selectRect:m,willSyncService:f===s.EvevtWorkState.Done?!0:c,isSync:!0,points:f===s.EvevtWorkState.Done&&n.getChildrenPoints()||void 0,textOpt:n.textOpt});const w={useAnimation:p.useAnimation||!1};p.uid&&(w.uid=p.uid);for(const[P,T]of i.entries())T.toolsType===s.EToolsKey.BackgroundSVG&&y.push({...T,workId:P,type:s.EPostMessageType.BackgroundSVGUpdate,dataType:s.EDataType.Local,willSyncService:f===s.EvevtWorkState.Done?!0:c,updateNodeOpt:w}),d&&T.toolsType===s.EToolsKey.Text?y.push({...T,workId:P,type:s.EPostMessageType.TextUpdate,dataType:s.EDataType.Local,willSyncService:f===s.EvevtWorkState.Done?!0:c,updateNodeOpt:w}):(c||f===s.EvevtWorkState.Done)&&y.push({...T,workId:P,type:s.EPostMessageType.UpdateNode,updateNodeOpt:w,isSync:h})}return u&&y.push({type:s.EPostMessageType.None,needUndoTicker:u}),{sp:y}}}class jt extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.ScaleNode)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return await this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var l;const{workId:t,updateNodeOpt:o,willSyncService:i,willSerializeData:n,smoothSync:a,needUndoTicker:c}=e;t===s.Storage_Selector_key&&o&&await((l=this.localWork)==null?void 0:l.updateSelector({updateSelectorOpt:o,willSyncService:i,willSerializeData:n,isSync:!0,callback:this.updateSelectorCallback.bind(this),smoothSync:a,needUndoTicker:c}))}updateSelectorCallback(e){const{param:t,postData:o,workShapeNode:i,res:n,newServiceStore:a,smoothSync:c}=e,{updateSelectorOpt:l,willSyncService:h,needUndoTicker:p}=t,d=l.workState,u=o.sp||[],f=n==null?void 0:n.selectRect;if(d===s.EvevtWorkState.Start)return{sp:[],render:[]};if(h){u.push({type:s.EPostMessageType.Select,selectIds:i.selectIds,selectRect:f,willSyncService:d===s.EvevtWorkState.Done?!0:c,isSync:!0,points:d===s.EvevtWorkState.Done&&i.getChildrenPoints()||void 0,textOpt:i.textOpt});const y={useAnimation:l.useAnimation||!1};l.uid&&(y.uid=l.uid);for(const[m,w]of a.entries())w.toolsType===s.EToolsKey.BackgroundSVG&&u.push({...w,workId:m,type:s.EPostMessageType.BackgroundSVGUpdate,dataType:s.EDataType.Local,willSyncService:d===s.EvevtWorkState.Done?!0:c,updateNodeOpt:y}),w.toolsType===s.EToolsKey.Text?u.push({...w,workId:m,type:s.EPostMessageType.TextUpdate,dataType:s.EDataType.Local,willSyncService:d===s.EvevtWorkState.Done?!0:c,updateNodeOpt:y}):(c||d===s.EvevtWorkState.Done)&&u.push({...w,workId:m,type:s.EPostMessageType.UpdateNode,updateNodeOpt:y,isSync:!0})}return p&&u.push({type:s.EPostMessageType.None,needUndoTicker:p}),{sp:u}}}class Zt extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.RotateNode)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return await this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var p;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,emitEventType:c,smoothSync:l,needUndoTicker:h}=e;t===s.Storage_Selector_key&&o&&await((p=this.localWork)==null?void 0:p.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,emitEventType:c,isSync:!0,callback:this.updateSelectorCallback,smoothSync:l,needUndoTicker:h}))}updateSelectorCallback(e){const{param:t,postData:o,workShapeNode:i,res:n,newServiceStore:a,smoothSync:c}=e,{updateSelectorOpt:l,willSyncService:h,willSerializeData:p,isSync:d,needUndoTicker:u}=t,f=l.workState,y=o.sp||[],m=n==null?void 0:n.selectRect;if(h){p&&f===s.EvevtWorkState.Done&&y.push({type:s.EPostMessageType.Select,selectIds:i.selectIds,selectRect:m,willSyncService:f===s.EvevtWorkState.Done?!0:c,isSync:d,points:i.getChildrenPoints()});const w={useAnimation:l.useAnimation||!1};if(l.uid&&(w.uid=l.uid),c||f===s.EvevtWorkState.Done)for(const[P,T]of a.entries())y.push({...T,workId:P,type:s.EPostMessageType.UpdateNode,updateNodeOpt:w,isSync:d})}return u&&y.push({type:s.EPostMessageType.None,needUndoTicker:u}),{sp:y}}}class Qt extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.SetFontStyle)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return await this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var l;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,textUpdateForWoker:c}=e;t===s.Storage_Selector_key&&o&&await((l=this.localWork)==null?void 0:l.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,textUpdateForWoker:c,callback:this.updateSelectorCallback}))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i,workShapeNode:n,res:a}=e,{willSyncService:c,isSync:l,updateSelectorOpt:h,textUpdateForWoker:p}=t,d=o.sp||[],u=a==null?void 0:a.selectRect;if(c&&d){h.fontSize&&d.push({type:s.EPostMessageType.Select,selectIds:n.selectIds,selectRect:u,willSyncService:c,isSync:l,points:n.getChildrenPoints()});for(const[f,y]of i.entries())p&&y.toolsType===s.EToolsKey.Text?d.push({...y,workId:f,type:s.EPostMessageType.TextUpdate,dataType:s.EDataType.Local,willSyncService:!0}):d.push({...y,workId:f,type:s.EPostMessageType.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:l})}return{sp:d}}}class Jt extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.SetPoint)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var h;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,textUpdateForWoker:c,needUndoTicker:l}=e;t===s.Storage_Selector_key&&o&&await((h=this.localWork)==null?void 0:h.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,emitEventType:this.emitEventType,willSerializeData:a,isSync:!0,textUpdateForWoker:c,callback:this.updateSelectorCallback,needUndoTicker:l}))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i,workShapeNode:n,res:a}=e,{willSyncService:c,isSync:l,needUndoTicker:h}=t,p=o.sp||[],d=a==null?void 0:a.selectRect;if(c&&p){for(const[u,f]of i.entries())p.push({...f,workId:u,type:s.EPostMessageType.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:l});p.push({type:s.EPostMessageType.Select,selectIds:n.selectIds,selectRect:d,willSyncService:c,isSync:l,points:n.getChildrenPoints()})}return h&&p.push({type:s.EPostMessageType.None,needUndoTicker:h}),{sp:p}}}class es extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.SetLock)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var l;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,needUndoTicker:c}=e;t===s.Storage_Selector_key&&o&&await((l=this.localWork)==null?void 0:l.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,callback:this.updateSelectorCallback,needUndoTicker:c}))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i,workShapeNode:n,res:a}=e,{willSyncService:c,isSync:l,updateSelectorOpt:h,needUndoTicker:p}=t,d=o.sp||[],u=a==null?void 0:a.selectRect;if(c&&d){for(const[f,y]of i.entries())d.push({...y,workId:f,type:s.EPostMessageType.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:l});d.push({isLocked:h.isLocked,selectorColor:n.selectorColor,scaleType:n.scaleType,canRotate:n.canRotate,type:s.EPostMessageType.Select,selectIds:n.selectIds,selectRect:u,willSyncService:c,isSync:l})}return p&&d.push({type:s.EPostMessageType.None,needUndoTicker:p}),{sp:d}}}class ts extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.SetShapeOpt)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var l;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,needUndoTicker:c}=e;t===s.Storage_Selector_key&&o&&await((l=this.localWork)==null?void 0:l.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,callback:this.updateSelectorCallback,needUndoTicker:c}))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i}=e,{willSyncService:n,isSync:a,needUndoTicker:c}=t,l=o.sp||[];if(n&&l)for(const[h,p]of i.entries())l.push({...p,workId:h,type:s.EPostMessageType.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:a});return c&&l.push({type:s.EPostMessageType.None,needUndoTicker:c}),{sp:l}}}class ss{constructor(r){g(this,"builders",new Map);this.builders=new Map(r.map(e=>[e,this.build(e)]))}build(r){switch(r){case s.EmitEventType.TranslateNode:return new qt;case s.EmitEventType.ZIndexNode:return new Ht;case s.EmitEventType.CopyNode:return new Yt;case s.EmitEventType.SetColorNode:return new $t;case s.EmitEventType.ScaleNode:return new jt;case s.EmitEventType.RotateNode:return new Zt;case s.EmitEventType.SetFontStyle:return new Qt;case s.EmitEventType.SetPoint:return new Jt;case s.EmitEventType.SetLock:return new es;case s.EmitEventType.SetShapeOpt:return new ts}}registerForMainThread(r){return this.builders.forEach(e=>{e&&e.registerMainThread(r)}),this}async consumeForMainThread(r){for(const e of this.builders.values())if(await(e==null?void 0:e.consume(r)))return!0;return!1}}class os{constructor(r,e){g(this,"viewId");g(this,"fullLayer");g(this,"topLayer");g(this,"localLayer");g(this,"serviceLayer");g(this,"snapshotFullLayer");g(this,"vNodes");g(this,"master");g(this,"opt");g(this,"cameraOpt");g(this,"scene");g(this,"localWork");g(this,"serviceWork");g(this,"topWork");g(this,"taskUpdateCameraId");g(this,"debounceUpdateCameraId");g(this,"debounceUpdateCache",new Set);g(this,"mainThreadPostId");g(this,"combinePostMsg",new Set);g(this,"methodBuilder");g(this,"cacheImages",new Map);g(this,"imageResolveMap",new Map);this.viewId=r,this.opt=e,this.scene=this.createScene({...e.canvasOpt,container:e.container}),this.master=e.master;const t=s.DefaultAppliancePluginExtrasOptions.bufferSize.full,o=s.DefaultAppliancePluginExtrasOptions.bufferSize.sub;this.fullLayer=this.createLayer("fullLayer",this.scene,{...e.layerOpt,bufferSize:this.viewId===s.Main_View_Id?t:o*2}),this.topLayer=this.createLayer("topLayer",this.scene,{...e.layerOpt,bufferSize:(this.viewId===s.Main_View_Id,o),contextType:"2d"}),this.localLayer=this.createLayer("localLayer",this.scene,{...e.layerOpt,bufferSize:(this.viewId===s.Main_View_Id,o),contextType:"2d"}),this.serviceLayer=this.createLayer("serviceLayer",this.scene,{...e.layerOpt,bufferSize:(this.viewId===s.Main_View_Id,o),contextType:"2d"}),this.vNodes=new je(r,this.scene);const i={thread:this,vNodes:this.vNodes};this.localWork=new Gt(i),this.serviceWork=new Xt(i),this.topWork=new _t(i),this.vNodes.init(this.fullLayer),this.methodBuilder=new ss([s.EmitEventType.CopyNode,s.EmitEventType.SetColorNode,s.EmitEventType.DeleteNode,s.EmitEventType.RotateNode,s.EmitEventType.ScaleNode,s.EmitEventType.TranslateNode,s.EmitEventType.ZIndexNode,s.EmitEventType.SetFontStyle,s.EmitEventType.SetPoint,s.EmitEventType.SetLock,s.EmitEventType.SetShapeOpt]).registerForMainThread(this)}getCachedImages(r){var e;return(e=this.cacheImages.get(r))==null?void 0:e.imageBitmap}getCachedImagesByWorkId(r){for(const[e,t]of this.cacheImages.entries())if(e===r&&t.imageBitmap)return t.imageBitmap}deleteCachedImagesByWorkId(r){for(const[e,t]of this.cacheImages.entries())t.workId===r&&(t.imageBitmap.close(),this.cacheImages.delete(e))}clearCacheImages(){this.cacheImages.forEach(r=>r.imageBitmap.close()),this.cacheImages.clear()}clearImageResolveMap(){this.imageResolveMap.forEach(({timer:r})=>{r&&clearTimeout(r)}),this.imageResolveMap.clear()}post(r){this.combinePostMsg.add(r),this.runBatchPostData()}updateDpr(r){this.scene.displayRatio=r}async on(r){if(!await this.methodBuilder.consumeForMainThread(r)){const{msgType:e,toolsType:t,opt:o,dataType:i,workId:n,workState:a,imageSrc:c,imageBitmap:l,workIds:h,isLockSentEventCursor:p}=r,d=n==null?void 0:n.toString();switch(e){case s.EPostMessageType.UpdateDpr:$.isNumber(r.dpr)&&this.updateDpr(r.dpr);break;case s.EPostMessageType.AuthClear:{const{clearUids:u,localUid:f}=r;this.vNodes.setCanClearUids(u),this.vNodes.setLocalUid(f);break}case s.EPostMessageType.Destroy:this.destroy();break;case s.EPostMessageType.Clear:this.clearAll();break;case s.EPostMessageType.UpdateCamera:await this.updateCamera(r);break;case s.EPostMessageType.UpdateTools:if(t&&o){const u={toolsType:t,toolsOpt:o};this.topWork.canUseTopLayer(t)?this.topWork.setToolsOpt(u):this.localWork.setToolsOpt(u)}break;case s.EPostMessageType.CreateWork:if(d&&o&&t){if(this.topWork.canUseTopLayer(t)){this.topWork.getToolsOpt()||this.topWork.setToolsOpt({toolsType:t,toolsOpt:o}),this.topWork.setWorkOptions(d,o);break}this.localWork.getToolsOpt()||this.localWork.setToolsOpt({toolsType:t,toolsOpt:o}),this.localWork.setWorkOptions(d,o)}break;case s.EPostMessageType.DrawWork:a===s.EvevtWorkState.Done&&i===s.EDataType.Local?(this.consumeDrawAll(i,r),t===s.EToolsKey.LaserPen&&p&&this.post({sp:[{type:s.EPostMessageType.None,isLockSentEventCursor:p}]})):this.consumeDraw(i,r);break;case s.EPostMessageType.UpdateNode:case s.EPostMessageType.FullWork:if(t&&this.topWork.canUseTopLayer(t)){this.consumeDrawAll(i,r);break}this.consumeFull(i,r);break;case s.EPostMessageType.RemoveNode:await this.removeNode(r);return;case s.EPostMessageType.Select:i===s.EDataType.Service&&(n===s.Storage_Selector_key?this.localWork.updateFullSelectWork(r):this.serviceWork.runSelectWork(r));break;case s.EPostMessageType.CursorBlur:this.localWork.cursorBlur();return;case s.EPostMessageType.CursorHover:this.localWork.cursorHover(r);break;case s.EPostMessageType.GetTextActive:i===s.EDataType.Local&&this.localWork.checkTextActive(r);break;case s.EPostMessageType.GetImageBitMap:if(c&&l&&n){const u=n.toString();this.deleteCachedImagesByWorkId(u),this.cacheImages.set(c,{imageBitmap:l,workId:u});const f=this.imageResolveMap.get(c);if(f){const{resolve:y,timer:m}=f;m&&clearTimeout(m),y&&y(c)}}break;case s.EPostMessageType.GetVNodeInfo:if(n&&h){const u=h.map(f=>this.vNodes.get(f));this.post({sp:[{type:s.EPostMessageType.GetVNodeInfo,dataType:s.EDataType.Local,workId:n,vInfo:u}]})}break}}}getIconSize(r,e,t){const o=r*t,i=e*t;return o<=50||i<=50?[50,50]:o<=100||i<=100?[100,100]:o<=200||i<=200?[200,200]:o<=400||i<=400?[400,400]:o<=800||i<=800?[800,800]:[1600,1600]}async loadImageBitMap(r){const{toolsType:e,opt:t,workId:o}=r;if(e===s.EToolsKey.Image&&t&&o){const i=o.toString(),{src:n,type:a,width:c,height:l,strokeColor:h}=t;if(!n||!a||!c||!l)return;let p=n;if(a===s.EImageType.Iconify){const[y,m]=this.getIconSize(c,l,this.opt.displayer.dpr);p=`${n}?width=${y}&height=${m}&color=${h}`}if(this.cacheImages.has(p)){const y=this.getCachedImages(p);if(y)return y}if(this.imageResolveMap.has(p)){const y=this.getCachedImagesByWorkId(i);if(y)return y}const f=await new Promise(y=>{const m=this.imageResolveMap.get(p)||{resolve:void 0,timer:void 0};m.timer&&clearTimeout(m.timer),m.resolve=y,m.timer=setTimeout(()=>{const w=this.imageResolveMap.get(p);w!=null&&w.resolve&&w.resolve(p)},5e3),this.imageResolveMap.set(p,m),this.opt.post({sp:[{imageSrc:p,workId:i,viewId:this.viewId,isgl:!!this.fullLayer.parent.gl,isSubWorker:!1,type:s.EPostMessageType.GetImageBitMap}]})});return this.imageResolveMap.delete(f),this.getCachedImages(p)}}async removeNode(r){const{dataType:e,workId:t,removeIds:o}=r,i=o||[];if(t&&i.push(t.toString()),i.length)for(const n of i){if(n===s.Storage_Selector_key){await this.localWork.removeSelector(r);continue}e===s.EDataType.Local?this.localWork.removeWork(r):e===s.EDataType.Service&&this.serviceWork.removeWork(r),await this.localWork.colloctEffectSelectWork(r)}}async consumeFull(r,e){const t=await this.localWork.colloctEffectSelectWork(e);t&&r===s.EDataType.Local&&await this.localWork.consumeFull(t),t&&r===s.EDataType.Service&&this.serviceWork.consumeFull(t)}setCameraOpt(r){this.cameraOpt=r;const{scale:e,centerX:t,centerY:o,width:i,height:n}=r;(i!==this.scene.width||n!==this.scene.height)&&this.updateScene({width:i,height:n}),this.fullLayer.setAttribute("scale",[e,e]),this.fullLayer.setAttribute("translate",[-t,-o]),this.topLayer.setAttribute("scale",[e,e]),this.topLayer.setAttribute("translate",[-t,-o]),this.localLayer.setAttribute("scale",[e,e]),this.localLayer.setAttribute("translate",[-t,-o]),this.serviceLayer.setAttribute("scale",[e,e]),this.serviceLayer.setAttribute("translate",[-t,-o])}runBatchPostData(){this.mainThreadPostId||(this.mainThreadPostId=requestAnimationFrame(this.combinePost.bind(this)))}combinePostData(){var o;this.mainThreadPostId=void 0;const r=[];let e,t;for(const i of this.combinePostMsg.values()){if((o=i.sp)!=null&&o.length)for(const n of i.sp){let a=!1;for(const c of r)if($.isEqual(n,c)){a=!0;break}a||r.push(n)}$.isNumber(i.fullWorkerDrawCount)&&(e=i.fullWorkerDrawCount),$.isNumber(i.consumeCount)&&(t=i.consumeCount)}return this.combinePostMsg.clear(),{sp:r,fullWorkerDrawCount:e,consumeCount:t}}combinePost(){var t,o;const r=this.combinePostData(),e=(t=r.sp)==null?void 0:t.filter(i=>i.type!==s.EPostMessageType.None||i.isLockSentEventCursor||i.needUndoTicker);e!=null&&e.length?r.sp=e.map(i=>i.viewId?i:{...i,viewId:this.viewId}):delete r.sp,r.consumeCount===void 0&&delete r.consumeCount,r.fullWorkerDrawCount===void 0&&delete r.fullWorkerDrawCount,(r!=null&&r.consumeCount||r!=null&&r.fullWorkerDrawCount||(o=r.sp)!=null&&o.length)&&this.opt.post(r)}clearAll(){this.fullLayer.children.length&&(this.fullLayer.parent.children.forEach(r=>{r.name!=="viewport"&&r.remove()}),U(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:s.EPostMessageType.Clear}]})}consumeDrawAll(r,e){const{toolsType:t,workId:o}=e;if(o){const i=o.toString();if(t&&this.topWork.canUseTopLayer(t)){r===s.EDataType.Local&&(this.topWork.getLocalWorkShape(o.toString())||this.topWork.createLocalWork(e)),this.topWork.consumeDrawAll(e);return}r===s.EDataType.Local&&(this.localWork.getWorkShape(i)||this.localWork.createLocalWork(e),this.localWork.consumeDrawAll(e,this.serviceWork))}}consumeDraw(r,e){const{opt:t,workId:o,toolsType:i}=e;if(o&&i&&t){const n=o.toString();if(this.topWork.canUseTopLayer(i)){r===s.EDataType.Local&&(this.topWork.getLocalWorkShape(n)||this.topWork.createLocalWork(e)),this.topWork.consumeDraw(e);return}r===s.EDataType.Local?(this.localWork.getWorkShape(n)||this.localWork.createLocalWork(e),this.localWork.consumeDraw(e,this.serviceWork)):r===s.EDataType.Service&&this.serviceWork.consumeDraw(e);return}}async updateCamera(r){var o;const{cameraOpt:e,scenePath:t}=r;if(e&&!$.isEqual(this.cameraOpt,e)){if(this.taskUpdateCameraId&&(clearTimeout(this.taskUpdateCameraId),this.taskUpdateCameraId=void 0),t){let l=!1;for(const[h,p]of this.localWork.getWorkShapes().entries())switch(p.toolsType){case s.EToolsKey.Text:case s.EToolsKey.BitMapEraser:case s.EToolsKey.PencilEraser:case s.EToolsKey.Eraser:case s.EToolsKey.Selector:case s.EToolsKey.LaserPen:break;default:h!==s.Cursor_Hover_Id&&h!==s.Storage_Selector_key&&(l=!0);break}if(l){this.taskUpdateCameraId=setTimeout(()=>{this.taskUpdateCameraId=void 0,this.updateCamera(r)},s.Task_Time_Interval);return}}const i=new Map;for(const[l,h]of this.vNodes.getNodesByType(s.EToolsKey.Text).entries()){const p=h.rect;i.set(l,$.cloneDeep(p))}const n=new Set(i.keys());let a=!1;if(this.localWork.hasSelector()){const l=(o=this.localWork.getSelector())==null?void 0:o.selectIds;if(l){a=!0;for(const h of l)n.add(h)}}let c=!1;if(this.serviceWork.selectorWorkShapes.size)for(const l of this.serviceWork.selectorWorkShapes.values()){const h=l.selectIds;if(h){c=!0;for(const p of h)n.add(p)}}if(this.setCameraOpt(e),this.vNodes.curNodeMap.size){this.vNodes.clearTarget(),this.vNodes.updateHighLevelNodesRect(n),this.debounceUpdateCameraId&&clearTimeout(this.debounceUpdateCameraId);for(const[l,h]of i.entries()){const p=this.vNodes.get(l);if(p){const d=h,u=p.rect,f=this.getSceneRect(),y=s.getRectMatrixrRelation(d,f),m=s.getRectMatrixrRelation(u,f);let w=!1;if((y!==m||d.w!==u.w||d.h!==u.h||m===s.EMatrixrRelationType.intersect)&&(w=!0),w){const{toolsType:P}=p;P===s.EToolsKey.Text&&this.debounceUpdateCache.add(l)}}}if(a&&this.localWork.reRenderSelector(),c)for(const[l,h]of this.serviceWork.selectorWorkShapes.entries())this.serviceWork.runSelectWork({workId:l,selectIds:h.selectIds,msgType:s.EPostMessageType.Select,dataType:s.EDataType.Service,viewId:this.viewId});this.debounceUpdateCameraId=setTimeout(()=>{var h;this.debounceUpdateCameraId=void 0;const l=[];for(const p of this.debounceUpdateCache.values()){if((h=this.fullLayer)==null?void 0:h.getElementsByName(p)[0]){const u=this.vNodes.get(p);if(u){const{toolsType:f,opt:y}=u,m=this.localWork.setFullWork({toolsType:f,opt:y,workId:p});if(m){const w=this.getSceneRect();l.push(m.consumeServiceAsync({isFullWork:!0,replaceId:p,boxRect:w}))}}}this.debounceUpdateCache.delete(p)}this.vNodes.updateLowLevelNodesRect(),this.vNodes.clearHighLevelIds()},s.Task_Time_Interval)}}}getSceneRect(){const{width:r,height:e}=this.scene;return{x:0,y:0,w:Math.floor(r),h:Math.floor(e)}}createScene(r){return new D.Scene({displayRatio:this.opt.displayer.dpr,depth:!1,desynchronized:!0,...r,autoRender:!0,id:this.viewId,contextType:"2d"})}createLayer(r,e,t){const{width:o,height:i}=t,n=`canvas-${r}`,a=e.layer(n,{...t,offscreen:!1}),c=new D.Group({anchor:[.5,.5],pos:[o*.5,i*.5],size:[o,i],name:"viewport",id:r});return a.append(c),c}updateScene(r){this.scene.attr({...r});const{width:e,height:t}=r;this.scene.width=e,this.scene.height=t,this.updateLayer({width:e,height:t})}updateLayer(r){const{width:e,height:t}=r;this.fullLayer.parent.setAttribute("width",e),this.fullLayer.parent.setAttribute("height",t),this.fullLayer.setAttribute("size",[e,t]),this.fullLayer.setAttribute("pos",[e*.5,t*.5]),this.topLayer.parent.setAttribute("width",e),this.topLayer.parent.setAttribute("height",t),this.topLayer.setAttribute("size",[e,t]),this.topLayer.setAttribute("pos",[e*.5,t*.5]),this.localLayer.parent.setAttribute("width",e),this.localLayer.parent.setAttribute("height",t),this.localLayer.setAttribute("size",[e,t]),this.localLayer.setAttribute("pos",[e*.5,t*.5]),this.serviceLayer.parent.setAttribute("width",e),this.serviceLayer.parent.setAttribute("height",t),this.serviceLayer.setAttribute("size",[e,t]),this.serviceLayer.setAttribute("pos",[e*.5,t*.5])}destroy(){this.clearCacheImages(),this.clearImageResolveMap(),this.vNodes.clear(),this.fullLayer.remove(),B(this.fullLayer,this.fullLayer.parent),this.topLayer.remove(),B(this.topLayer,this.topLayer.parent),this.localLayer.remove(),B(this.localLayer,this.localLayer.parent),this.serviceLayer.remove(),B(this.serviceLayer,this.serviceLayer.parent),this.scene.remove(),this.localWork.destroy(),this.serviceWork.destroy(),this.topWork.destroy()}}class is{constructor(r,e){g(this,"viewId");g(this,"fullLayer");g(this,"master");g(this,"opt");g(this,"scene");g(this,"mainThreadPostId");g(this,"combinePostMsg",new Set);g(this,"workShapes",new Map);g(this,"cacheImages",new Map);g(this,"imageResolveMap",new Map);this.viewId=r,this.opt=e,this.scene=this.createScene({...e.canvasOpt,container:e.container}),this.master=e.master,this.fullLayer=this.createLayer("fullLayer",this.scene,{...e.layerOpt,bufferSize:this.viewId===s.Main_View_Id?6e3:3e3,contextType:"2d"})}getCachedImages(r){var e;return(e=this.cacheImages.get(r))==null?void 0:e.imageBitmap}getCachedImagesByWorkId(r){for(const[e,t]of this.cacheImages.entries())if(e===r&&t.imageBitmap)return t.imageBitmap}deleteCachedImagesByWorkId(r){for(const[e,t]of this.cacheImages.entries())t.workId===r&&(t.imageBitmap.close(),this.cacheImages.delete(e))}clearCacheImages(){this.cacheImages.forEach(r=>r.imageBitmap.close()),this.cacheImages.clear()}clearImageResolveMap(){this.imageResolveMap.forEach(({timer:r})=>{r&&clearTimeout(r)}),this.imageResolveMap.clear()}post(r){this.combinePostMsg.add(r),this.runBatchPostData()}async on(r){const{msgType:e,imageSrc:t,imageBitmap:o,workId:i}=r;switch(e){case s.EPostMessageType.Snapshot:{await this.getSnapshot(r),this.destroy();return}case s.EPostMessageType.BoundingBox:{await this.getBoundingRect(r),this.destroy();return}case s.EPostMessageType.GetImageBitMap:{if(t&&o&&i){const n=i.toString();this.deleteCachedImagesByWorkId(n),this.cacheImages.set(t,{imageBitmap:o,workId:n});const a=this.imageResolveMap.get(t);if(a){const{resolve:c,timer:l}=a;l&&clearTimeout(l),c&&c(t)}}break}}}getIconSize(r,e,t){const o=r*t,i=e*t;return o<=50||i<=50?[50,50]:o<=100||i<=100?[100,100]:o<=200||i<=200?[200,200]:o<=400||i<=400?[400,400]:o<=800||i<=800?[800,800]:[1600,1600]}async loadImageBitMap(r){const{toolsType:e,opt:t,workId:o}=r;if(e===s.EToolsKey.Image&&t&&o){const i=o.toString(),{src:n,type:a,width:c,height:l,strokeColor:h}=t;if(!n||!a||!c||!l)return;let p=n;if(a===s.EImageType.Iconify){const[y,m]=this.getIconSize(c,l,this.opt.displayer.dpr);p=`${n}?width=${y}&height=${m}&color=${h}`}if(this.cacheImages.has(p)){const y=this.getCachedImages(p);if(y)return y}if(this.imageResolveMap.has(p)){const y=this.getCachedImagesByWorkId(i);if(y)return y}const f=await new Promise(y=>{const m=this.imageResolveMap.get(p)||{resolve:void 0,timer:void 0};m.timer&&clearTimeout(m.timer),m.resolve=y,m.timer=setTimeout(()=>{const w=this.imageResolveMap.get(p);w!=null&&w.resolve&&w.resolve(p)},5e3),this.imageResolveMap.set(p,m),this.opt.post({sp:[{imageSrc:p,workId:i,viewId:this.viewId,isgl:!!this.fullLayer.parent.gl,isSubWorker:!0,type:s.EPostMessageType.GetImageBitMap}]})});return this.imageResolveMap.delete(f),this.getCachedImages(p)}}createWorkShapeNode(r){return ie({...r,fullLayer:this.fullLayer,drawLayer:void 0})}setFullWork(r){const{workId:e,opt:t,toolsType:o}=r;if(e&&t&&o){const i=e.toString();let n;return e&&this.workShapes.has(i)?(n=this.workShapes.get(i),n==null||n.setWorkOptions(t)):n=this.createWorkShapeNode({toolsOpt:t,toolsType:o,workId:i}),n?(this.workShapes.set(i,n),n):void 0}}async runFullWork(r){var o;const e=this.setFullWork(r),t=r.ops&&s.transformToNormalData(r.ops);if(e){let i,n;const a=(o=e.getWorkId())==null?void 0:o.toString();return e.toolsType===s.EToolsKey.BackgroundSVG?i=e.consumeService({isFullWork:!0,replaceId:a}):e.toolsType===s.EToolsKey.Image?i=await e.consumeServiceAsync({isFullWork:!0,worker:this}):e.toolsType===s.EToolsKey.Text?i=await e.consumeServiceAsync({isFullWork:!0,replaceId:a,isDrawLabel:!0}):(i=e.consumeService({op:t,isFullWork:!0,replaceId:a}),n=(r==null?void 0:r.updateNodeOpt)&&e.updataOptService(r.updateNodeOpt)),s.computRect(i,n)}}async getSnapshot(r){const{scenePath:e,scenes:t,cameraOpt:o,w:i,h:n}=r;if(e&&t&&o){this.setCameraOpt(o);for(const[c,l]of Object.entries(t))if(l!=null&&l.type)switch(l==null?void 0:l.type){case s.EPostMessageType.UpdateNode:case s.EPostMessageType.FullWork:{const{opt:h}=l,p={...l,opt:h,workId:c,msgType:s.EPostMessageType.FullWork,dataType:s.EDataType.Service,viewId:this.viewId};await this.runFullWork(p);break}}let a;i&&n&&(a={resizeWidth:i,resizeHeight:n});try{await this.getSnapshotRender({scenePath:e,options:a})}catch(c){const l=c&&c instanceof Error?c.message:c==null?void 0:c.toString();console.error("[SnapshotThreadImpl] getSnapshotRender error",l),this.post({sp:[{type:s.EPostMessageType.ReportError,reportString:`[SnapshotThreadImpl] getSnapshotRender error: ${l}`}]})}}}getSceneRect(){const{width:r,height:e}=this.scene;return{x:0,y:0,w:Math.floor(r),h:Math.floor(e)}}getRectImageBitmap(r,e){const t=Math.floor(r.x*this.opt.displayer.dpr),o=Math.floor(r.y*this.opt.displayer.dpr),i=r.w>0&&Math.floor(r.w*this.opt.displayer.dpr||1)||1,n=r.h>0&&Math.floor(r.h*this.opt.displayer.dpr||1)||1;return createImageBitmap(this.fullLayer.parent.canvas,t,o,i,n,e)}async getSnapshotRender(r){var i;const{scenePath:e,options:t}=r;((i=this.fullLayer)==null?void 0:i.parent).render();const o=await this.getRectImageBitmap(this.getSceneRect(),t);o&&(this.post({sp:[{type:s.EPostMessageType.Snapshot,scenePath:e,imageBitmap:o,viewId:this.viewId,index:0}]}),this.fullLayer&&U(this.fullLayer,this.fullLayer.parent))}async getBoundingRect(r){const{scenePath:e,scenes:t,cameraOpt:o}=r;if(e&&t&&o){this.setCameraOpt(o);let i;for(const[n,a]of Object.entries(t))if(a!=null&&a.type)switch(a==null?void 0:a.type){case s.EPostMessageType.UpdateNode:case s.EPostMessageType.FullWork:{const c=await this.runFullWork({...a,workId:n,msgType:s.EPostMessageType.FullWork,dataType:s.EDataType.Service,viewId:this.viewId});i=s.computRect(i,c);break}}i&&this.post({sp:[{type:s.EPostMessageType.BoundingBox,scenePath:e,rect:i}]})}}setCameraOpt(r){const{scale:e,centerX:t,centerY:o,width:i,height:n}=r;this.updateScene({width:i,height:n}),this.fullLayer.setAttribute("scale",[e,e]),this.fullLayer.setAttribute("translate",[-t,-o])}runBatchPostData(){this.mainThreadPostId||(this.mainThreadPostId=requestAnimationFrame(this.combinePost.bind(this)))}combinePostData(){var e;this.mainThreadPostId=void 0;const r=[];for(const t of this.combinePostMsg.values())if((e=t.sp)!=null&&e.length)for(const o of t.sp){let i=!1;for(const n of r)if(H(o,n)){i=!0;break}i||r.push(o)}return this.combinePostMsg.clear(),{sp:r}}combinePost(){var t,o;const r=this.combinePostData(),e=(t=r.sp)==null?void 0:t.filter(i=>i.type!==s.EPostMessageType.None||i.isLockSentEventCursor||i.needUndoTicker);e!=null&&e.length?r.sp=e.map(i=>i.viewId?i:{...i,viewId:this.viewId}):delete r.sp,(o=r.sp)!=null&&o.length&&this.opt.post(r)}createScene(r){return new D.Scene({displayRatio:this.opt.displayer.dpr,depth:!1,desynchronized:!0,...r,autoRender:!1,contextType:"2d"})}createLayer(r,e,t){const{width:o,height:i}=t,n=`canvas-${r}`,a=e.layer(n,t),c=new D.Group({anchor:[.5,.5],pos:[o*.5,i*.5],size:[o,i],name:"viewport",id:r});return a.append(c),c}updateScene(r){this.scene.attr({...r});const{width:e,height:t}=r;this.scene.width=e,this.scene.height=t,this.updateLayer({width:e,height:t})}updateLayer(r){const{width:e,height:t}=r;this.fullLayer.parent.setAttribute("width",e),this.fullLayer.parent.setAttribute("height",t),this.fullLayer.setAttribute("size",[e,t]),this.fullLayer.setAttribute("pos",[e*.5,t*.5])}destroy(){this.clearCacheImages(),this.clearImageResolveMap(),this.fullLayer.remove(),B(this.fullLayer,this.fullLayer.parent),this.scene.remove()}}class rs{constructor(r){g(this,"mainThreadMap",new Map);g(this,"snapshotThread");g(this,"master");g(this,"post",r=>{const{fullWorkerDrawCount:e,sp:t,workerTasksqueueCount:o,consumeCount:i}=r;this.master.isBusy&&A(o)&&this.master.setWorkerTasksqueueCount(o),A(e)&&this.master.setMaxDrawCount(e),A(i)&&this.master.setConsumeCount(i),t&&this.master.collectorSyncData(t)});this.master=r}destroy(){this.mainThreadMap.clear()}createMainThread(r,e){return new os(r,e)}createSnapshotThread(r,e){return new is(r,e)}async consume(r){var e,t,o,i;for(const n of r.values()){const{msgType:a,viewId:c,tasksqueue:l,mainTasksqueueCount:h,layerOpt:p,offscreenCanvasOpt:d,cameraOpt:u,isSubWorker:f}=n;if(a===s.EPostMessageType.Console){console.log(this);continue}if(a===s.EPostMessageType.Init){const m=(e=this.master.control.viewContainerManager.getView(c))==null?void 0:e.displayer,w=m==null?void 0:m.canvasContainerRef.current;if(m&&w&&p&&d){const P=this.createMainThread(c,{displayer:m,container:w,layerOpt:p,master:this.master,canvasOpt:d,post:this.post});this.mainThreadMap.set(c,P),P&&u&&P.setCameraOpt(u)}continue}if((a===s.EPostMessageType.Snapshot||a===s.EPostMessageType.BoundingBox)&&c===((t=this.master.control.viewContainerManager.mainView)==null?void 0:t.id)){const m=(o=this.master.control.viewContainerManager.getView(c))==null?void 0:o.displayer,w=(i=m.snapshotContainerRef)==null?void 0:i.current;if(m&&w&&u){w.style.width=`${u.width}px`,w.style.height=`${u.height}px`;const P={...s.ViewContainerManager.defaultLayerOpt,offscreen:!1,width:u.width,height:u.height},T={...s.ViewContainerManager.defaultScreenCanvasOpt,width:u.width,height:u.height};this.snapshotThread=this.createSnapshotThread(c,{displayer:m,container:w,layerOpt:P,master:this.master,canvasOpt:T,post:this.post}),this.snapshotThread.on(n).then(()=>{this.snapshotThread=void 0,w.innerHTML="",w.style.width="",w.style.height=""});continue}}if(a===s.EPostMessageType.GetImageBitMap&&f&&this.snapshotThread){this.snapshotThread.on(n);continue}if(a===s.EPostMessageType.TasksQueue&&(l!=null&&l.size)){for(const[m,w]of this.mainThreadMap.entries()){const P=l.get(m);P&&(await w.on(P),h&&this.post({workerTasksqueueCount:h}))}continue}if(c===s.Storage_ViewId_ALL){for(const m of this.mainThreadMap.values())m.on(n),a===s.EPostMessageType.Destroy&&this.mainThreadMap.delete(c);continue}const y=this.mainThreadMap.get(c);y&&(y.on(n),a===s.EPostMessageType.Destroy&&this.mainThreadMap.delete(c))}}}exports.MainThreadManagerImpl=rs;
|
|
1
|
+
"use strict";var Je=Object.defineProperty;var et=(k,r,e)=>r in k?Je(k,r,{enumerable:!0,configurable:!0,writable:!0,value:e}):k[r]=e;var g=(k,r,e)=>et(k,typeof r!="symbol"?r+"":r,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./index-DpQJ_LNL.js"),D=require("spritejs"),_=require("clipper-lib"),Se=require("lineclip");require("lz-string");require("xss");const $=require("lodash"),Me=require("./ObserverMap-DTz9zucn.js");require("white-web-sdk");require("react-dom");require("react");var tt=s._baseGetTag,st=s.isObjectLike_1,ot="[object Number]";function it(k){return typeof k=="number"||st(k)&&tt(k)==ot}var rt=it;const A=s.getDefaultExportFromCjs(rt);function Q(k,r=!0){const e=k.length;if(e<2)return"";let t=k[0],o=k[1];if(e===2)return`M${s.precise(t)}L${s.precise(o)}`;let i="";for(let n=2,a=e-1;n<a;n++)t=k[n],o=k[n+1],i+=s.average(t,o);return r?`M${s.average(k[0],k[1])}Q${s.precise(k[1])}${s.average(k[1],k[2])}T${i}${s.average(k[e-1],k[0])}${s.average(k[0],k[1])}Z`:`M${s.precise(k[0])}Q${s.precise(k[1])}${s.average(k[1],k[2])}${k.length>3?"T":""}${i}L${s.precise(k[e-1])}`}const K=class K{constructor(r){g(this,"maxImageWidth",s.DefaultAppliancePluginExtrasOptions.pencilEraser.maxImageWidth);g(this,"maxImageHeight",s.DefaultAppliancePluginExtrasOptions.pencilEraser.maxImageHeight);g(this,"syncUnitTime",s.DefaultAppliancePluginExtrasOptions.syncOpt.interval);g(this,"vNodes");g(this,"drawLayer");g(this,"fullLayer");g(this,"workId");g(this,"isDelete",!1);const{vNodes:e,fullLayer:t,drawLayer:o,workId:i,toolsOpt:n}=r;this.vNodes=e,this.fullLayer=t,this.drawLayer=o,this.workId=i,this.syncUnitTime=n.syncUnitTime||this.syncUnitTime}get baseConsumeResult(){return{workId:this.workId,toolsType:this.toolsType,opt:this.workOptions}}filterSamePoints(r,e=.01){return r.reduce((t,o)=>{const i=t[t.length-1];return(o&&!i||o&&i&&!o.isNear(i,e))&&t.push(o),t},[])}setWorkId(r){this.workId=r}getWorkId(){return this.workId}getWorkOptions(){return this.workOptions}setWorkOptions(r){var o,i,n;this.workOptions=r,this.syncUnitTime=r.syncUnitTime||this.syncUnitTime;const e=(o=this.workId)==null?void 0:o.toString(),t=e&&((i=this.vNodes)==null?void 0:i.get(e))||void 0;e&&t&&(t.opt=r,(n=this.vNodes)==null||n.setInfo(e,t))}updataOptService(r){var o,i;let e;const t=(o=this.workId)==null?void 0:o.toString();if(t&&r){const n=this.fullLayer.getElementsByName(t)||this.drawLayer&&this.drawLayer.getElementsByName(t)||[];if(n.length!==1)return;const a=n[0],{pos:c,zIndex:l,scale:h,angle:p,translate:d}=r,u={};A(l)&&(u.zIndex=l),c&&(u.pos=[c[0],c[1]]),h&&(u.scale=h),p&&(u.rotate=p),d&&(u.translate=d),a.attr(u);const f=a==null?void 0:a.getBoundingClientRect();return f&&(e=s.computRect(e,{x:Math.floor(f.x-K.SafeBorderPadding),y:Math.floor(f.y-K.SafeBorderPadding),w:Math.floor(f.width+K.SafeBorderPadding*2),h:Math.floor(f.height+K.SafeBorderPadding*2)})),(i=this.vNodes)==null||i.setInfo(t,{rect:e,centerPos:c}),e}}drawEraserlines(r,e){const{group:t,eraserlines:o,pos:i,layer:n}=r,a=n.parent;if(e)try{const l=(n.renderer.glRenderer||n.renderer.canvasRenderer).options.displayRatio,h=t.getBoundingClientRect(),p=h.width*l*n.worldScaling[0],d=h.height*l*n.worldScaling[1];let u=1;(p>this.maxImageWidth||d>this.maxImageHeight)&&(u=Math.min(this.maxImageWidth/p,this.maxImageHeight/d));let f=n.getAttribute("scale");f=[f[0]*u,f[1]*u];const y=n.parent.parent,m=n.getAttribute("translate"),w=this.createVmRenderNode(`${this.workId}_bitMapLayer`,y,{offscreen:a.offscreen,width:a.width,height:a.height,contextType:"2d",autoRender:!1,bufferSize:500},f,m),P=t.cloneNode(!0);if(!o||!o.length)return;this.addEraserlines(P,o,i,!1),w.appendChild(P);const T=w.parent;T.render();const S=w.renderer.canvasRenderer,E=S.context;if(!E)return;const I=S.options.displayRatio,R=P.getBoundingClientRect(),M={x:R.x,y:R.y,w:R.width,h:R.height},v={x:0,y:0,w:y.width,h:y.height};if(s.getRectMatrixrRelation(M,v)===s.EMatrixrRelationType.outside){U(P,T),y.removeChild(T),this.isDelete=!0;return}const C=s.cloneDeep(M);C.x=Math.floor(Math.max(M.x,0)),C.y=Math.floor(Math.max(M.y,0)),C.w=Math.min(M.x+M.w,y.width)-C.x,C.h=Math.min(M.y+M.h,y.height)-C.y;const b=this.getGroupRect(C,I),N=b.x,x=b.y,O=b.w,F=b.h;let z=E.getImageData(N,x,O,F);if(this.isTransparentRectByCanvas(z)){U(P,T),y.removeChild(T),this.isDelete=!0,z=null;return}U(t,a);const J=this.createSpriteNode({imageData:z,safariRect:C,originRect:M,worldScaling:w.worldScaling});J&&t.append(J),U(P,T),y.removeChild(T),a.deleteTexture(z),z=null}catch(c){console.error("[BaseShapeTool] drawEraserlines error:",c);return}}createVmRenderNode(r,e,t,o,i){const{width:n,height:a}=t,c=`bitMap-${r}`,l=e.layer(c,t),h=new D.Group({anchor:[.5,.5],pos:[n*.5,a*.5],size:[n,a],name:"viewport",id:r});return h.setAttribute("scale",o),h.setAttribute("translate",i),l.append(h),h}getGroupRect(r,e){const t=Math.floor(r.x*e),o=Math.floor(r.y*e),i=Math.floor(r.w*e),n=Math.floor(r.h*e);return{x:t,y:o,w:i,h:n}}addEraserlines(r,e,t,o=!0){for(const i of e){const{thickness:n,op:a}=i;for(const c of a){const l=c.map((f,y)=>y%2?f-t[1]:f-t[0]),h=this.computEraserPoints(l,n),p=Q(h,!0),d={pos:[0,0],d:p,fillColor:"rgba(0,0,0,1)"},u=new D.Path(d);u.addEventListener("beforerender",({detail:f})=>{const y=f.context;o?y.blendFuncSeparate(y.ZERO,y.ZERO,y.ZERO,y.ZERO):y.globalCompositeOperation="destination-out"}),u.addEventListener("afterrender",({detail:f})=>{const y=f.context;o?y.blendFuncSeparate(y.SRC_ALPHA,y.ONE_MINUS_SRC_ALPHA,y.ONE,y.ONE_MINUS_SRC_ALPHA):y.globalCompositeOperation="source-over"}),r.append(u)}}}isTransparentRectByCanvas(r){let e=!0;for(let t=0;t<r.data.length;t+=4)if(r.data[t+3]!==0){e=!1;break}return e}createSpriteNode(r){const{imageData:e,worldScaling:t,safariRect:o,originRect:i}=r;let n;if(typeof OffscreenCanvas=="function"){n=new OffscreenCanvas(e.width,e.height);const h=n.getContext("2d");h&&h.putImageData(e,0,0)}else if(typeof document<"u"){n=document.createElement("canvas"),n.width=e.width,n.height=e.height;const h=n.getContext("2d");h&&h.putImageData(e,0,0)}if(!n)return console.warn("Failed to create imageEraserBitmap Sprite"),null;const a=[Math.floor(i.w/t[0]),Math.floor(i.h/t[1])],c=[Math.floor((o.x-i.x)/t[0]),Math.floor((o.y-i.y)/t[1]),Math.floor(o.w/t[0]),Math.floor(o.h/t[1])],l={name:"eraserTexture",anchor:[.5,.5],pos:[0,0],size:a,texture:n,textureRect:c};return new D.Sprite(l)}computEraserPoints(r,e){const t=Math.ceil(e/2);return r.length===2?this.computDot(r,t):this.computLine(r,t)}computDot(r,e){const t=new s.Point2d(r[0],r[1]);return s.Point2d.GetDotStroke(t,e,8)}computLine(r,e){const t=[],o=[];let i,n;for(let a=0;a<r.length;a+=2){const c=new s.Point2d(r[a],r[a+1]);let l;if(a==r.length-2){const h=new s.Point2d(r[a-2],r[a-1]);l=s.Vec2d.Sub(c,h).uni(),i=c}else{a===0&&(n=c);const h=new s.Point2d(r[a+2],r[a+3]);l=s.Vec2d.Sub(h,c).uni()}if(l){const h=s.Vec2d.Per(l).mul(e);t.push(s.Point2d.Sub(c,h)),o.push(s.Point2d.Add(c,h))}}if(i&&n){const a=s.Point2d.GetSemicircleStroke(i,t[t.length-1],-1,8),c=s.Point2d.GetSemicircleStroke(n,o[0],-1,8);return t.concat(a,o.reverse(),c)}return[]}replace(r,e,t){var i;if(!e){t&&r.append(t);return}const o=r.getElementsByName(e);if(o.length)for(const n of o)t?Kt(t,n,r):(n.remove(),B(n,r.parent));else t&&r.append(t);this.fullLayer!==this.drawLayer&&(this.fullLayer===r?(i=this.drawLayer)==null||i.getElementsByName(e).forEach(a=>{var c;a.remove(),B(a,(c=this.drawLayer)==null?void 0:c.parent)}):this.fullLayer.getElementsByName(e).forEach(a=>{a.remove(),B(a,this.fullLayer.parent)}))}removeDrawCountNodes(r,e){const t=[];r.getElementsByName(this.workId).forEach(o=>{o.id&&e&&Number(o.id)<e&&t.push(o)});for(const o of t)o.remove(),B(o,r.parent)}static updateNodeOpt(r){var P;const{node:e,opt:t,vNodes:o,willSerializeData:i,targetNode:n}=r,{zIndex:a,translate:c,angle:l,originPoint:h,scenePoint:p,scale:d,pointMap:u,thickness:f}=t;let y;const m=n&&s.cloneDeep(n)||o.get(e.name);if(!m)return;A(a)&&(e.setAttribute("zIndex",a),m.opt.zIndex=a);const w=e.parent;if(w){if(h&&d&&n){const T=[m.op[0],m.op[1]];s.scalePoints(m.op,p,d,c);const S=[m.op[0],m.op[1]],E=[S[0]-T[0],S[1]-T[1]];if(m.centerPos=[m.centerPos[0]+E[0],m.centerPos[1]+E[1]],m.opt.translate=void 0,m.opt.scale=void 0,m.opt.eraserlines)for(let I=0;I<m.opt.eraserlines.length;I++){const{op:R,thickness:M}=m.opt.eraserlines[I];m.opt.eraserlines[I].thickness=Math.round(M*Math.max(d[0],d[1]));for(let v=0;v<R.length;v++)s.scaleLitePoints(R[v],p,d,c)}}else if(c)if(e.setAttribute("translate",c),m.opt.translate=c,n){const T=[c[0]*w.worldScaling[0],c[1]*w.worldScaling[1]];y=s.getRectTranslated(m.rect,T),m.rect=y}else{const T=K.getRectFromLayer(w,e.name);m.rect=T||m.rect}else if(A(l))if(e.setAttribute("rotate",l),m.opt.rotate=l,n)y=s.getRectRotated(m.rect,l),m.rect=y;else{const T=K.getRectFromLayer(w,e.name);m.rect=T||m.rect}if(u){const T=u.get(e.name);if(T)for(let S=0,E=0;S<m.op.length;S+=3,E++)m.op[S]=T[E][0],m.op[S+1]=T[E][1]}if(f&&((P=m==null?void 0:m.opt)!=null&&P.thickness)&&(m.opt.thickness=f),i&&!(h&&d&&n)){if(c){const T=m.op.map((S,E)=>{const I=E%3;return I===0?S+c[0]:I===1?S+c[1]:S});if(m.op=T,m.centerPos=[m.centerPos[0]+c[0],m.centerPos[1]+c[1]],m!=null&&m.opt&&(m.opt.translate=void 0),m.opt.eraserlines)for(let S=0;S<m.opt.eraserlines.length;S++){const{op:E}=m.opt.eraserlines[S];for(let I=0;I<E.length;I++){const R=E[I].map((M,v)=>v%2?M+c[1]:M+c[0]);m.opt.eraserlines[S].op[I]=R}}}else if(A(l)){const T=m.op;if(s.rotatePoints(T,m.centerPos,l),m.op=T,m!=null&&m.opt&&(m.opt.rotate=void 0),m.opt.eraserlines)for(let S=0;S<m.opt.eraserlines.length;S++){const{op:E}=m.opt.eraserlines[S];for(let I=0;I<E.length;I++)s.rotateLitePoints(E[I],m.centerPos,l)}}}m&&o.setInfo(e.name,m)}}static getCenterPos(r,e){const{worldPosition:t,worldScaling:o}=e;return[(r.x+r.w/2-t[0])/o[0],(r.y+r.h/2-t[1])/o[1]]}static getRectFromLayer(r,e){const t=r.getElementsByName(e)[0];if(t){const o=t.getBoundingClientRect();return{x:Math.floor(o.x-K.SafeBorderPadding),y:Math.floor(o.y-K.SafeBorderPadding),w:Math.floor(o.width+K.SafeBorderPadding*2),h:Math.floor(o.height+K.SafeBorderPadding*2)}}}static isWillRefresh(r){const{toolsType:e,opt:t,node:o,updateOpt:i,willSerializeData:n}=r;return!!(n&&(i.angle||i.translate)||i.thickness&&t.thickness&&t.thickness!==i.thickness||i.strokeType&&t.strokeType&&t.strokeType!==i.strokeType||i.originPoint&&i.scenePoint&&i.scale||i.pointMap&&i.pointMap.has(o.name)||e===s.EToolsKey.Text&&(i.fontSize||i.translate||i.textInfos&&i.textInfos.get(o.name))||e===s.EToolsKey.BackgroundSVG&&(i.translate||i.scale)||e===s.EToolsKey.Image&&(i.angle||i.translate||i.scale||i.strokeColor&&t.type===s.EImageType.Iconify)||e===i.toolsType&&i.willRefresh||t.eraserlines&&t.eraserlines.length&&(i.strokeColor||i.fillColor))}};g(K,"SafeBorderPadding",10);let W=K;const Pe=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 Be(k,r,e=.01){return Math.abs(k[0]-r[0])<e&&Math.abs(k[1]-r[1])<e}function nt(k,r=.01){if(k.length===0)return[];const e=[[k[0][0],k[0][1]]];for(let t=1;t<k.length;t++)Be([k[t][0],k[t][1]],e[e.length-1],r)||e.push([k[t][0],k[t][1]]);return e}function at(k,r=.01){if(k.length<=2)return k;const e=k[0],t=k[k.length-1];return Be(e,t,r)?k.slice(0,-1):k}function ct(k){const r=k.reduce((t,o)=>t+o[0],0)/k.length,e=k.reduce((t,o)=>t+o[1],0)/k.length;return[r,e]}function Re(k,r){return Math.atan2(r[1]-k[1],r[0]-k[0])}function lt(k,r=.01){if(k.length<=2)return k;const e=[];for(const o of k)e.some(n=>Math.abs(n[0]-o[0])<r&&Math.abs(n[1]-o[1])<r)||e.push([o[0],o[1]]);const t=ct(e);return e.sort((o,i)=>{const n=Re(t,o),a=Re(t,i);return n-a})}function ht(k,r){const e=r[0].x<r[r.length-1].x;return k.map(o=>{const n=o.map(a=>{let c=-1,l=1/0,h=0;for(let p=0;p<r.length;p++){const d=r[p],u=Math.sqrt(Math.pow(d.x-a[0],2)+Math.pow(d.y-a[1],2));u<l&&(l=u,c=p,h=d.z)}return{point:a,index:c,bestZ:h}}).sort((a,c)=>a.index-c.index).map(a=>[...a.point,a.bestZ]);return e&&n[0][0]>n[n.length-1][0]||!e&&n[0][0]<n[n.length-1][0]?n.reverse():n}).sort((o,i)=>{const n=o[0][0],a=i[0][0];return e?n-a:a-n})}function pt(k,r,e=.01){const o=k.map(p=>[p.x,p.y]),i=(p,d)=>p.map(([u,f])=>({X:Math.round(u*d),Y:Math.round(f*d)})),n=new _.ClipperOffset;n.AddPath(i(o,1e3),_.JoinType.jtRound,_.EndType.etOpenButt);const a=[];n.Execute(a,1);const c=new _.Clipper;c.AddPaths(a,_.PolyType.ptSubject,!0),r.forEach(p=>{c.AddPath(i(p,1e3),_.PolyType.ptClip,!0)});const l=new _.Paths;c.Execute(_.ClipType.ctDifference,l,_.PolyFillType.pftNonZero,_.PolyFillType.pftNonZero);const h=l.map(p=>at(lt(nt(p.map(d=>[d.X/1e3,d.Y/1e3]),e),e),e)).filter(p=>p.length>=2);return ht(h,k)}class Fe extends W{constructor(e){super(e);g(this,"canRotate",!0);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.Pencil);g(this,"syncTimestamp");g(this,"syncIndex",0);g(this,"tmpPoints",[]);g(this,"MAX_REPEAR",10);g(this,"uniThickness");g(this,"workOptions");g(this,"centerPos",[0,0]);this.workOptions=e.toolsOpt,this.uniThickness=this.MAX_REPEAR/this.workOptions.thickness/10,this.syncTimestamp=0}combineConsume(){var a;const e=(a=this.workId)==null?void 0:a.toString();if(this.tmpPoints.length<2)return{type:s.EPostMessageType.None};const t=this.transformDataAll(!0),o={name:e};let i;const n=this.drawLayer||this.fullLayer;return t.length&&(i=this.draw({attrs:o,tasks:t,replaceId:e,layer:n})),{rect:i,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local}}setWorkOptions(e){super.setWorkOptions(e),this.syncTimestamp=Date.now()}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,drawCount:a,removeDrawCount:c,isSimpleWorker:l}=e,{workId:h,syncUnitTime:p}=t;p&&(this.syncUnitTime=p);const{tasks:d,effects:u,consumeIndex:f}=this.transformData(t,!1);this.syncIndex=Math.min(this.syncIndex,f,Math.max(0,this.tmpPoints.length-2));const y={name:h,id:A(a)&&a.toString()||void 0};let m,w=!1;const P=this.syncIndex;if(this.syncTimestamp===0&&(this.syncTimestamp=Date.now()),d.length&&(d[0].taskId-this.syncTimestamp>this.syncUnitTime&&(w=!0,this.syncTimestamp=d[0].taskId,this.syncIndex=this.tmpPoints.length),i||n||l)){const S=o?this.fullLayer:this.drawLayer||this.fullLayer;m=this.draw({attrs:y,tasks:d,effects:u,layer:S,removeDrawCount:c})}if(l){const S=[];return this.tmpPoints.slice(P).forEach(E=>{S.push(E.x,E.y,this.computRadius(E.z,this.workOptions.thickness))}),{...this.baseConsumeResult,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:w?S:void 0,index:w?P*3:void 0,rect:m,updateNodeOpt:{useAnimation:!0}}}if(i)return f>10&&this.tmpPoints.splice(0,f-10),{rect:m,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local};const T=[];return this.tmpPoints.slice(P).forEach(S=>{T.push(S.x,S.y,this.computRadius(S.z,this.workOptions.thickness))}),{...this.baseConsumeResult,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,rect:m,op:w?T:void 0,index:w?P*3:void 0,updateNodeOpt:{useAnimation:!0}}}consumeAll(e){var h;const t=this.workId;if(e.data){const{op:p,workState:d}=e.data;p!=null&&p.length&&d===s.EvevtWorkState.Done&&this.workOptions.strokeType===s.EStrokeType.Stroke&&this.updateTempPointsWithPressureWhenDone(p)}const o=this.transformDataAll(!0),i={name:t};let n;const a=this.fullLayer;if(o.length&&(n=this.draw({attrs:i,tasks:o,replaceId:t,layer:a})),this.tmpPoints.length<2)return this.replace(a,t),{type:s.EPostMessageType.RemoveNode,removeIds:[t],rect:n};const c=[];this.tmpPoints.map(p=>{c.push(p.x,p.y,p.z)}),this.syncTimestamp=0,delete this.workOptions.syncUnitTime;const l=s.transformToSerializableData(c);return(h=this.vNodes)==null||h.setInfo(t,{rect:n,op:c,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:n&&W.getCenterPos(n,a)}),{...this.baseConsumeResult,rect:n,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:l,updateNodeOpt:{pos:this.centerPos,useAnimation:!0}}}clearTmpPoints(){this.tmpPoints.length=0,this.syncTimestamp=0,this.syncIndex=0}consumeService(e){var p,d;const{op:t,isFullWork:o,replaceId:i,workState:n=s.EvevtWorkState.Done}=e;this.tmpPoints.length=0;for(let u=0;u<t.length;u+=3){const f=new s.Point2d(t[u],t[u+1],t[u+2]);if(this.tmpPoints.length>0){const y=this.tmpPoints[this.tmpPoints.length-1],m=s.Vec2d.Sub(f,y).uni();f.setv(m)}this.tmpPoints.push(f)}if(this.tmpPoints.length<2)return;const a=this.transformDataAll(!0),c=(p=this.workId)==null?void 0:p.toString(),l={name:c};let h;if(c&&a.length){const u=o?this.fullLayer:this.drawLayer||this.fullLayer;h=this.draw({attrs:l,tasks:a,replaceId:i,layer:u,isDrawEraserlines:n===s.EvevtWorkState.Done}),(d=this.vNodes)==null||d.setInfo(c,{rect:h,op:t,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:h&&W.getCenterPos(h,u)})}return h}computPencilPoints(e){const t=[],{op:o,eraserPolylines:i,eraserThickness:n}=e;this.tmpPoints.length=0;for(let c=0;c<o.length;c+=3){const l=new s.Point2d(o[c],o[c+1],o[c+2]);if(this.tmpPoints.length>0){const h=this.tmpPoints[this.tmpPoints.length-1],p=s.Vec2d.Sub(l,h).uni();l.setv(p)}this.tmpPoints.push(l)}if(this.tmpPoints.length<2)return;const a=this.transformDataAll(!0);if(a.length)for(let c=0;c<a.length;c++){const{pos:l,points:h}=a[c],p=h.map(f=>f.point.addXY(l[0],l[1])),d=[];for(const f of i){const y=[];for(let w=0;w<f.length;w+=2){const P=new s.Point2d(f[w],f[w+1]);if(y.length>0){const T=y[y.length-1].point,S=s.Vec2d.Sub(P,T).uni();P.setv(S)}y.push({point:P,radius:n})}const{ps:m}=this.computStroke(y,!1);d.push(m.map(w=>w.XY))}pt(p,d,.01).forEach(f=>{t.push(f.map(y=>[y[0],y[1],y[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:o,replaceId:i,effects:n,layer:a,removeDrawCount:c,isDrawEraserlines:l=!0}=e,{strokeColor:h,strokeType:p,thickness:d,zIndex:u,scale:f,rotate:y,translate:m,eraserlines:w,lineCap:P,lineDash:T}=this.workOptions;n!=null&&n.size&&(n.forEach(v=>{var L;(L=a.getElementById(v+""))==null||L.remove()}),n.clear()),c&&this.removeDrawCountNodes(a,c);let S;const E=[],I=a.worldPosition,R=a.worldScaling;for(let v=0;v<o.length;v++){const{pos:L,points:C}=o[v],{ps:b,rect:N}=this.computDrawPoints(C);let x;const O=C.length===1;p===s.EStrokeType.Stroke||O?x=Q(b,!0):x=Q(b,!1);const F={pos:L,d:x,fillColor:p===s.EStrokeType.Stroke||O?h:void 0,lineDash:O?void 0:p===s.EStrokeType.Dotted?[T&&T[0]||1,(T&&T[1]||2)*d]:p===s.EStrokeType.LongDotted?[(T&&T[0]||1)*d,(T&&T[1]||2)*d]:void 0,strokeColor:h,lineCap:O?void 0:P,lineWidth:p===s.EStrokeType.Stroke||O?0:d};S=s.computRect(S,{x:Math.floor((N.x+L[0])*R[0]+I[0]-W.SafeBorderPadding),y:Math.floor((N.y+L[1])*R[1]+I[1]-W.SafeBorderPadding),w:Math.floor(N.w*R[0]+2*W.SafeBorderPadding),h:Math.floor(N.h*R[1]+2*W.SafeBorderPadding)}),E.push(F)}f&&(t.scale=f),y&&(t.rotate=y),m&&(t.translate=m);const M=new D.Group;if(S){this.centerPos=W.getCenterPos(S,a);const v=p===s.EStrokeType.Stroke&&!w;M.attr({...t,normalize:!0,anchor:[.5,.5],bgcolor:v?h:void 0,pos:this.centerPos,size:[(S.w-2*W.SafeBorderPadding)/R[0],(S.h-2*W.SafeBorderPadding)/R[1]],zIndex:u});const L=E.map(C=>(C.pos=[C.pos[0]-this.centerPos[0],C.pos[1]-this.centerPos[1]],new D.Path(C)));M.append(...L),v&&M.seal(),w&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:M,eraserlines:w,pos:this.centerPos,layer:a},l)),this.replace(a,i,M)}if(f||y||m){const v=M==null?void 0:M.getBoundingClientRect();v&&(S={x:Math.floor(v.x-W.SafeBorderPadding),y:Math.floor(v.y-W.SafeBorderPadding),w:Math.floor(v.width+W.SafeBorderPadding*2),h:Math.floor(v.height+W.SafeBorderPadding*2)})}return this.isDelete&&M.setAttribute("opacity",0),S}computDrawPoints(e){return this.workOptions.strokeType===s.EStrokeType.Stroke||e.length===1?this.computStroke(e):this.computNomal(e)}computNomal(e){let t=this.workOptions.thickness;const o=e.map(i=>(t=Math.max(t,i.radius),i.point));return{ps:o,rect:s.getRectFromPoints(o,t)}}computStroke(e,t=!0){return e.length===1?this.computDotStroke(e[0]):this.computLineStroke(e,t)}computLineStroke(e,t=!0){const o=[],i=[];for(let c=0;c<e.length;c++){const{point:l,radius:h}=e[c];let p=l.v;c===0&&e.length>1&&(p=e[c+1].point.v);const d=s.Vec2d.Per(p).mul(h);o.push(s.Point2d.Sub(l,d)),i.push(s.Point2d.Add(l,d))}const n=e[e.length-1];if(t){const c=s.Point2d.GetSemicircleStroke(n.point,o[o.length-1],-1,8),l=s.Point2d.GetSemicircleStroke(e[0].point,i[0],-1,8),h=o.concat(c,i.reverse(),l);return{ps:h,rect:s.getRectFromPoints(h)}}const a=o.concat(i.reverse());return{ps:a,rect:s.getRectFromPoints(a)}}computDotStroke(e){const{point:t,radius:o}=e,i={x:t.x-o,y:t.y-o,w:o*2,h:o*2};return{ps:s.Point2d.GetDotStroke(t,o,8),rect:i}}transformData(e,t){const{op:o,workState:i}=e;let n=this.tmpPoints.length-1,a=[];if(o!=null&&o.length&&i){const{strokeType:c,thickness:l}=this.workOptions,h=new Set;n=c===s.EStrokeType.Stroke?this.updateTempPointsWithPressure(o,l,h):this.updateTempPoints(o,l,h);const p=t?this.tmpPoints:this.tmpPoints.slice(n);return a=this.getTaskPoints(p,l),{tasks:a,effects:h,consumeIndex:n}}return{tasks:a,consumeIndex:n}}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 p;const o=[];if(e.length===0)return[];let i=0,n=e[0].x,a=e[0].y,c=[n,a],l=[],h=e[0].t;for(;i<e.length;){const d=e[i],u=d.x-n,f=d.y-a,y=d.z,m=t?this.computRadius(y,t):y;if(l.push({point:new s.Point2d(u,f,y,e[i].v),radius:m}),i>0&&i<e.length-1){const w=e[i].getAngleByPoints(e[i-1],e[i+1]);if(w<60||w>300){const P=(p=l.pop())==null?void 0:p.point.clone();P&&o.push({taskId:h,pos:c,points:[...l,{point:P,radius:m}]}),n=e[i].x,a=e[i].y,c=[n,a];const T=d.x-n,S=d.y-a;l=[{point:new s.Point2d(T,S,y),radius:m}],h=Date.now()}}i++}return o.push({taskId:h,pos:c,points:l}),o}updateTempPointsWithPressure(e,t,o){const i=Date.now(),n=this.tmpPoints.length;let a=n;for(let l=0;l<e.length;l+=2){a=Math.min(a,n);const h=this.tmpPoints.length,p=new s.Point2d(e[l],e[l+1]);if(h===0){this.tmpPoints.push(p);continue}const d=h-1,u=this.tmpPoints[d],f=s.Vec2d.Sub(p,u).uni();if(p.isNear(u,t)){if(u.z<this.MAX_REPEAR){if(u.setz(Math.min(u.z+1,this.MAX_REPEAR)),a=Math.min(a,d),h>1){let w=h-1;for(;w>0;){const P=this.tmpPoints[w].distance(this.tmpPoints[w-1]),T=Math.max(this.tmpPoints[w].z-this.uniThickness*P,0);if(this.tmpPoints[w-1].z>=T)break;this.tmpPoints[w-1].setz(T),a=Math.min(a,w-1),w--}}}else a=1/0;continue}p.setv(f);const y=p.distance(u),m=Math.max(u.z-this.uniThickness*y,0);h>1&&s.Vec2d.Equals(f,u.v,.02)&&(m>0||u.z<=0)&&(o&&u.t&&o.add(u.t),this.tmpPoints.pop(),a=Math.min(d,a)),p.setz(m),this.tmpPoints.push(p)}if(a===1/0)return this.tmpPoints.length;let c=n;if(a===n){c=Math.max(c-1,0);const l=this.tmpPoints[c].t;l&&(o==null||o.add(l))}else{let l=n-1;for(c=a;l>=0;){const h=this.tmpPoints[l].t;if(h&&(o==null||o.add(h),l<=a)){c=l,l=-1;break}l--}}return this.tmpPoints[c].setT(i),c}updateTempPoints(e,t,o){var l;const i=Date.now(),n=this.tmpPoints.length;let a=n;for(let h=0;h<e.length;h+=2){const p=this.tmpPoints.length,d=new s.Point2d(e[h],e[h+1]);if(p===0){this.tmpPoints.push(d);continue}const u=p-1,f=this.tmpPoints[u],y=s.Vec2d.Sub(d,f).uni();if(d.isNear(f,t/2)){a=Math.min(u,a);continue}s.Vec2d.Equals(y,f.v,.02)&&(o&&f.t&&o.add(f.t),this.tmpPoints.pop(),a=Math.min(u,a)),d.setv(y),this.tmpPoints.push(d)}let c=n;if(a===n){c=Math.max(c-1,0);const h=this.tmpPoints[c].t;h&&(o==null||o.add(h))}else{let h=Math.min(n-1,a);for(c=a;h>=0;){const p=(l=this.tmpPoints[h])==null?void 0:l.t;if(p&&(o==null||o.add(p),h<=a)){c=h,h=-1;break}h--}}return this.tmpPoints[c].setT(i),c}updateTempPointsWithPressureWhenDone(e){const{thickness:t}=this.workOptions,o=e.length,i=this.getMinZ(t);for(let n=0;n<o;n+=2){const a=this.tmpPoints.length,c=new s.Point2d(e[n],e[n+1]);if(a===0){this.tmpPoints.push(c);continue}const l=a-1,h=this.tmpPoints[l],p=s.Vec2d.Sub(c,h).uni(),d=c.distance(h);if(a>1&&h.z===i)break;if(c.isNear(h,t/2)){if(o<3&&h.z<this.MAX_REPEAR&&(h.setz(Math.min(h.z+1,this.MAX_REPEAR)),a>1)){let f=a-1;for(;f>0;){const y=this.tmpPoints[f].distance(this.tmpPoints[f-1]),m=Math.max(this.tmpPoints[f].z-this.uniThickness*y,-t/4);if(this.tmpPoints[f-1].z>=m)break;this.tmpPoints[f-1].setz(m),f--}}continue}c.setv(p);const u=Math.max(h.z-this.uniThickness*d,i);a>1&&s.Vec2d.Equals(p,h.v,.02)&&h.z<=0&&this.tmpPoints.pop(),c.setz(u),this.tmpPoints.push(c)}}static updateNodeOpt(e){var l,h;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,strokeType:a}=o,c=i.get(t.name);return n&&(t.tagName==="GROUP"?Ut(t)?t.setAttribute("bgcolor",n):t.children.forEach(p=>{p.setAttribute("strokeColor",n),p.getAttribute("fillColor")&&p.setAttribute("fillColor",n)}):(t.setAttribute("strokeColor",n),t.setAttribute("fillColor",n)),(l=c==null?void 0:c.opt)!=null&&l.strokeColor&&(c.opt.strokeColor=n)),a&&(c!=null&&c.opt)&&((h=c.opt)!=null&&h.strokeType)&&(c.opt.strokeType=a),c&&i.setInfo(t.name,c),W.updateNodeOpt(e)}}class ze extends W{constructor(e){super(e);g(this,"toolsType",s.EToolsKey.LaserPen);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.none);g(this,"syncTimestamp");g(this,"syncIndex",0);g(this,"tmpPoints",[]);g(this,"workOptions");g(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:o}=e,{workId:i,op:n,syncUnitTime:a}=t;if((n==null?void 0:n.length)===0)return{type:s.EPostMessageType.None};if(a&&(this.syncUnitTime=a),this.updateTempPoints(n||[]),this.consumeIndex>this.tmpPoints.length-4)return{type:s.EPostMessageType.None};const{strokeColor:c,thickness:l,strokeType:h,lineDash:p,lineCap:d}=this.workOptions,u=s.getRectFromPoints(this.tmpPoints,l);let f=!1;const y=this.syncIndex,m=this.tmpPoints.slice(this.consumeIndex);this.consumeIndex=this.tmpPoints.length-1,this.syncTimestamp===0&&(this.syncTimestamp=Date.now());const w={name:i==null?void 0:i.toString(),opacity:1,lineDash:h===s.EStrokeType.Dotted?[p&&p[0]||1,(p&&p[1]||2)*l]:h===s.EStrokeType.LongDotted?[(p&&p[0]||1)*l,(p&&p[1]||2)*l]:void 0,strokeColor:c,lineCap:d,lineWidth:l,anchor:[.5,.5]},P=this.getTaskPoints(m);if(P.length){const S=Date.now();S-this.syncTimestamp>this.syncUnitTime&&(f=!0,this.syncTimestamp=S,this.syncIndex=this.tmpPoints.length),o&&this.draw({attrs:w,tasks:P,isDot:!1,layer:this.drawLayer||this.fullLayer})}const T=[];return this.tmpPoints.slice(y).forEach(S=>{T.push(S.x,S.y)}),{rect:{x:u.x*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],y:u.y*this.fullLayer.worldScaling[1]+this.fullLayer.worldPosition[1],w:u.w*this.fullLayer.worldScaling[0],h:u.h*this.fullLayer.worldScaling[1]},type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:f?T:void 0,index:f?y*2:void 0,...this.baseConsumeResult}}consumeAll(){var n;const e=(n=this.workId)==null?void 0:n.toString();let t;if(this.tmpPoints.length-1>this.consumeIndex){let a=this.tmpPoints.slice(this.consumeIndex);const c=a.length===1,{strokeColor:l,thickness:h,strokeType:p}=this.workOptions;if(c){const f=this.computDotStroke({point:a[0],radius:h/2});a=f.ps,t=f.rect}else t=s.getRectFromPoints(this.tmpPoints,h);const d={name:e==null?void 0:e.toString(),fillColor:c?l:void 0,opacity:1,lineDash:p===s.EStrokeType.Dotted&&!c?[1,h*2]:p===s.EStrokeType.LongDotted&&!c?[h,h*2]:void 0,strokeColor:l,lineCap:c?void 0:"round",lineWidth:c?0:h,anchor:[.5,.5]},u=this.getTaskPoints(a);u.length&&this.draw({attrs:d,tasks:u,isDot:c,layer:this.drawLayer||this.fullLayer})}const o=[];this.tmpPoints.forEach(a=>{o.push(a.x,a.y)});const i=s.transformToSerializableData(o);return{rect:t&&{x:t.x*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],y:t.y*this.fullLayer.worldScaling[1]+this.fullLayer.worldPosition[1],w:t.w*this.fullLayer.worldScaling[0],h:t.h*this.fullLayer.worldScaling[1]},type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:i,index:this.syncIndex*2,...this.baseConsumeResult}}clearTmpPoints(){this.tmpPoints.length=0,this.syncTimestamp=0,this.syncIndex=0}consumeService(e){var y;const{op:t,replaceId:o,isFullWork:i}=e,{strokeColor:n,thickness:a,strokeType:c}=this.workOptions;if(!t.length){const m=s.getRectFromPoints(this.tmpPoints,a);return{x:m.x*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],y:m.y*this.fullLayer.worldScaling[1]+this.fullLayer.worldPosition[1],w:m.w*this.fullLayer.worldScaling[0],h:m.h*this.fullLayer.worldScaling[1]}}const l=Math.max(0,this.tmpPoints.length-1);this.updateTempPoints(t||[]);let h,p=this.tmpPoints.slice(l);const d=p.length===1;if(d){const m=this.computDotStroke({point:p[0],radius:a/2});p=m.ps,h=m.rect}else h=s.getRectFromPoints(this.tmpPoints,a);const u={name:(y=this.workId)==null?void 0:y.toString(),fillColor:d?n:void 0,opacity:1,lineDash:c===s.EStrokeType.Dotted&&!d?[1,a*2]:c===s.EStrokeType.LongDotted&&!d?[a,a*2]:void 0,strokeColor:n,lineCap:d?void 0:"round",lineWidth:d?0:a,anchor:[.5,.5]},f=this.getTaskPoints(p);if(f.length){const m=i?this.fullLayer:this.drawLayer||this.fullLayer;this.draw({attrs:u,tasks:f,isDot:d,replaceId:o,layer:m})}return{x:h.x*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],y:h.y*this.fullLayer.worldScaling[1]+this.fullLayer.worldPosition[1],w:h.w*this.fullLayer.worldScaling[0],h:h.h*this.fullLayer.worldScaling[1]}}computDotStroke(e){const{point:t,radius:o}=e,i={x:t.x-o,y:t.y-o,w:o*2,h:o*2};return{ps:s.Point2d.GetDotStroke(t,o,8),rect:i}}updateTempPoints(e){const t=this.tmpPoints.length;for(let o=0;o<e.length;o+=2){if(t){const i=this.tmpPoints.slice(-1)[0];i&&i.x===e[o]&&i.y===e[o+1]&&this.tmpPoints.pop()}this.tmpPoints.push(new s.Point2d(e[o],e[o+1]))}}async draw(e){const{attrs:t,tasks:o,isDot:i,layer:n}=e,{duration:a}=this.workOptions;for(const c of o){const l=new D.Path,{pos:h,points:p}=c;let d;i?d=Q(p,!0):d=Q(p,!1),l.attr({...t,pos:h,d});const{vertex:u,fragment:f}=this.workOptions;if(u&&f){const y=n.renderer.createProgram({vertex:u,fragment:f}),{width:m,height:w}=n.getResolution();l.setUniforms({u_time:0,u_resolution:[m,w]}),l.setProgram(y)}n.appendChild(l),l.transition(a).attr({scale:i?[.1,.1]:[1,1],lineWidth:i?0:1}).then(()=>{l.remove()})}}getTaskPoints(e){var l;const t=[];if(e.length===0)return[];let o=0,i=e[0].x,n=e[0].y,a=[i,n],c=[];for(;o<e.length;){const h=e[o],p=h.x-i,d=h.y-n;if(c.push(new s.Point2d(p,d)),o>0&&o<e.length-1){const u=e[o].getAngleByPoints(e[o-1],e[o+1]);if(u<60||u>300){const f=(l=c.pop())==null?void 0:l.clone();f&&t.push({pos:a,points:[...c,f]}),i=e[o].x,n=e[o].y,a=[i,n];const y=h.x-i,m=h.y-n;c=[new s.Point2d(y,m)]}}o++}return t.push({pos:a,points:c}),t}removeLocal(){}removeService(e){let t;const o=[];return this.fullLayer.getElementsByName(e).forEach(i=>{if(i.name===e){const n=i.getBoundingClientRect();t=s.computRect(t,{x:n.x,y:n.y,w:n.width,h:n.height}),o.push(i)}}),o.length&&o.forEach(i=>i.remove()),t}}const te=class te extends W{constructor(e,t){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.none);g(this,"toolsType",s.EToolsKey.Eraser);g(this,"serviceWork");g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"worldPosition");g(this,"worldScaling");g(this,"eraserRect");g(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],o=e[1]*this.worldScaling[1]+this.worldPosition[1],{width:i,height:n}=te.eraserSizes[this.workOptions.thickness];this.eraserRect={x:t-i*.5,y:o-n*.5,w:i,h:n},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 s.Vec2d(this.tmpPoints[0],this.tmpPoints[1]),o=new s.Vec2d(this.tmpPoints[2],this.tmpPoints[3]),i=s.Vec2d.Sub(o,t).uni(),n=s.Vec2d.Dist(t,o),{width:a,height:c}=te.eraserSizes[this.workOptions.thickness],l=Math.min(a,c),h=Math.round(n/l);if(h>1){const p=[];for(let d=0;d<h;d++){const u=s.Vec2d.Mul(i,d*l);p.push(this.tmpPoints[0]+u.x,this.tmpPoints[1]+u.y)}return p.concat(e)}}return e}isNear(e,t){const o=new s.Vec2d(e[0],e[1]),i=new s.Vec2d(t[0],t[1]),{width:n,height:a}=te.eraserSizes[this.workOptions.thickness];return s.Vec2d.Dist(o,i)<Math.hypot(n,a)*.5}remove(e){const{curNodeMap:t,removeIds:o}=e;let i;for(const n of t.values())if(n.rect&&this.eraserRect&&this.eraserPolyline&&s.isIntersect(this.eraserRect,n.rect)){const{op:a}=n,c=[],l=[];for(let p=0;p<a.length;p+=3){const d=new s.Vec2d(a[p]*this.worldScaling[0]+this.worldPosition[0],a[p+1]*this.worldScaling[1]+this.worldPosition[1],a[p+2]);l.push(d),c.push(new s.Point2d(d.x,d.y))}const h=c.length&&s.getRectFromPoints(c)||n.rect;s.isIntersect(h,this.eraserRect)&&(l.length>1?Se.polyline(l.map(d=>d.XY),this.eraserPolyline).length&&o.add(n.name):o.add(n.name),i=s.computRect(i,n.rect||h))}return o.forEach(n=>{var c;const a=this.fullLayer.getElementsByName(n);a[0]&&(a[0].remove(),B(a[0],this.fullLayer.parent),(c=this.vNodes)==null||c.delete(n))}),i&&(i.x-=W.SafeBorderPadding,i.y-=W.SafeBorderPadding,i.w+=W.SafeBorderPadding*2,i.h+=W.SafeBorderPadding*2),i}consume(e){const{op:t,disableEraseImage:o,disableEraseText:i}=e.data;if(!t||t.length===0)return{type:s.EPostMessageType.None,...this.baseConsumeResult};const n=this.tmpPoints.length;if(n>1&&this.isNear([t[0],t[1]],[this.tmpPoints[n-2],this.tmpPoints[n-1]]))return{type:s.EPostMessageType.None,...this.baseConsumeResult};n<3?this.tmpPoints.push(t[0],t[1]):this.tmpPoints.splice(2,2,t[0],t[1]);const a=this.computRectCenterPoints();let c;const l=new Set;if(!this.vNodes)return{type:s.EPostMessageType.None,...this.baseConsumeResult};const h=this.getCanEraserNodeMap(this.vNodes.getCanEraserNodes(this.vNodes.curNodeMap,{disableEraseImage:o,disableEraseText:i}));for(let p=0;p<a.length-1;p+=2){this.createEraserRect(a.slice(p,p+2));const d=this.remove({curNodeMap:h,removeIds:l});c=s.computRect(c,d)}return c&&l.size?{type:s.EPostMessageType.RemoveNode,rect:c,removeIds:[...l]}:{type:s.EPostMessageType.None,...this.baseConsumeResult}}consumeAll(e){return this.consume(e)}clearTmpPoints(){this.tmpPoints.length=0}getCanEraserNodeMap(e){var t;if(this.serviceWork){const o=new Map(e),i=this.serviceWork.selectorWorkShapes,n=this.serviceWork.workShapes;for(const a of i.values())if((t=a.selectIds)!=null&&t.length)for(const c of a.selectIds)o.delete(c);for(const a of n.keys())o.delete(a);return o}return e}};g(te,"eraserSizes",Pe);let de=te;var Ce=s._getAllKeys,dt=1,ut=Object.prototype,ft=ut.hasOwnProperty;function yt(k,r,e,t,o,i){var n=e&dt,a=Ce(k),c=a.length,l=Ce(r),h=l.length;if(c!=h&&!n)return!1;for(var p=c;p--;){var d=a[p];if(!(n?d in r:ft.call(r,d)))return!1}var u=i.get(k),f=i.get(r);if(u&&f)return u==r&&f==k;var y=!0;i.set(k,r),i.set(r,k);for(var m=n;++p<c;){d=a[p];var w=k[d],P=r[d];if(t)var T=n?t(P,w,d,r,k,i):t(w,P,d,k,r,i);if(!(T===void 0?w===P||o(w,P,e,t,i):T)){y=!1;break}m||(m=d=="constructor")}if(y&&!m){var S=k.constructor,E=r.constructor;S!=E&&"constructor"in k&&"constructor"in r&&!(typeof S=="function"&&S instanceof S&&typeof E=="function"&&E instanceof E)&&(y=!1)}return i.delete(k),i.delete(r),y}var mt=yt,Te=s._Stack,gt=s._equalArrays,wt=s._equalByTag,Tt=mt,Le=s._getTag,be=s.isArray_1,De=s.isBufferExports,kt=s.isTypedArray_1,St=1,Ne="[object Arguments]",xe="[object Array]",pe="[object Object]",Pt=Object.prototype,Oe=Pt.hasOwnProperty;function vt(k,r,e,t,o,i){var n=be(k),a=be(r),c=n?xe:Le(k),l=a?xe:Le(r);c=c==Ne?pe:c,l=l==Ne?pe:l;var h=c==pe,p=l==pe,d=c==l;if(d&&De(k)){if(!De(r))return!1;n=!0,h=!1}if(d&&!h)return i||(i=new Te),n||kt(k)?gt(k,r,e,t,o,i):wt(k,r,c,e,t,o,i);if(!(e&St)){var u=h&&Oe.call(k,"__wrapped__"),f=p&&Oe.call(r,"__wrapped__");if(u||f){var y=u?k.value():k,m=f?r.value():r;return i||(i=new Te),o(y,m,e,t,i)}}return d?(i||(i=new Te),Tt(k,r,e,t,o,i)):!1}var Et=vt,It=Et,Ae=s.isObjectLike_1;function Ue(k,r,e,t,o){return k===r?!0:k==null||r==null||!Ae(k)&&!Ae(r)?k!==k&&r!==r:It(k,r,e,t,Ue,o)}var Wt=Ue,Mt=Wt;function Rt(k,r){return Mt(k,r)}var Ct=Rt;const H=s.getDefaultExportFromCjs(Ct),ge=class ge extends W{constructor(e){super(e);g(this,"toolsType",s.EToolsKey.Selector);g(this,"tmpPoints",[]);g(this,"subTmpPoints",[]);g(this,"workOptions");g(this,"vNodes");g(this,"selectIds");g(this,"selectorColor");g(this,"strokeColor");g(this,"fillColor");g(this,"oldSelectRect");g(this,"oldSubSelectRect");g(this,"canRotate",!1);g(this,"canTextEdit",!1);g(this,"canLock",!1);g(this,"scaleType",s.EScaleType.all);g(this,"toolsTypes");g(this,"shapeOpt");g(this,"textOpt");g(this,"isLocked");g(this,"thickness");g(this,"strokeType");g(this,"useStroke");this.workOptions=e.toolsOpt,this.vNodes=e.vNodes}computSelector(e=!0){const t=s.getRectFromPoints(this.tmpPoints);if(t.w===0||t.h===0)return{selectIds:[],intersectRect:void 0,subNodeMap:new Map};const{rectRange:o,nodeRange:i}=this.vNodes.getRectIntersectRange(t,e);return{selectIds:[...i.keys()],intersectRect:o,subNodeMap:i}}updateTempPoints(e){const t=this.tmpPoints.length,o=e.length;if(o>1){const i=new s.Point2d(e[o-2]*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[0],e[o-1]*this.fullLayer.worldScaling[0]+this.fullLayer.worldPosition[1]);t===2?this.tmpPoints.splice(1,1,i):this.tmpPoints.push(i)}}drawSelector(e){const{drawRect:t,subNodeMap:o,selectorId:i,layer:n,isService:a}=e,c=new D.Group({pos:[t.x,t.y],anchor:[0,0],size:[t.w,t.h],id:i,name:i,zIndex:9999}),l=[];if(a){const h=new D.Rect({normalize:!0,pos:[t.w/2,t.h/2],lineWidth:1,strokeColor:this.selectorColor||this.workOptions.strokeColor,width:t.w,height:t.h,name:ge.selectorBorderId});l.push(h)}o.forEach((h,p)=>{const d=[h.rect.x+h.rect.w/2-t.x,h.rect.y+h.rect.h/2-t.y],u=new D.Rect({normalize:!0,pos:d,lineWidth:1,strokeColor:o.size>1?this.selectorColor||this.workOptions.strokeColor:void 0,width:h.rect.w,height:h.rect.h,id:`selector-${p}`,name:`selector-${p}`});l.push(u)}),l&&c.append(...l),(n==null?void 0:n.parent).appendChild(c)}draw(e,t,o,i=!1){var c,l;const{intersectRect:n,subNodeMap:a}=o;(l=(c=t.parent)==null?void 0:c.getElementById(e))==null||l.remove(),n&&this.drawSelector({drawRect:n,subNodeMap:a,selectorId:e,layer:t,isService:i})}getSelecteorInfo(e){this.scaleType=s.EScaleType.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 o,i=!0;for(const n of e.values()){const{opt:a,canRotate:c,scaleType:l,toolsType:h}=n;this.selectorColor=this.workOptions.strokeColor,a.strokeColor&&(this.strokeColor=a.strokeColor),a.fillColor&&(this.fillColor=a.fillColor),a.textOpt&&(this.textOpt=a.textOpt),a.thickness&&(this.thickness=a.thickness),h!==s.EToolsKey.Pencil&&(i=!1),a.strokeType&&(this.strokeType=a.strokeType),h===s.EToolsKey.SpeechBalloon&&(t.add(h),this.shapeOpt||(this.shapeOpt={}),this.shapeOpt.placement=a.placement),h===s.EToolsKey.Polygon&&(t.add(h),this.shapeOpt||(this.shapeOpt={}),this.shapeOpt.vertices=a.vertices),h===s.EToolsKey.Star&&(t.add(h),this.shapeOpt||(this.shapeOpt={}),this.shapeOpt.vertices=a.vertices,this.shapeOpt.innerRatio=a.innerRatio,this.shapeOpt.innerVerticeStep=a.innerVerticeStep),h===s.EToolsKey.Text&&(this.textOpt=a),e.size===1&&(this.textOpt&&(this.canTextEdit=!0),this.canRotate=c,this.scaleType=l),(h===s.EToolsKey.Image||h===s.EToolsKey.BackgroundSVG)&&(o=n),(l===s.EScaleType.proportional&&this.scaleType!==s.EScaleType.none||l===s.EScaleType.none)&&(this.scaleType=l)}i&&(this.useStroke=!0),t.size&&(this.toolsTypes=[...t]),o&&(e.size===1?(this.canLock=!0,o.opt.locked&&(this.isLocked=!0,this.scaleType=s.EScaleType.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&&!o.opt.locked&&(this.canLock=!1,this.canRotate=!1))}getChildrenPoints(){var e,t;if(this.scaleType===s.EScaleType.both&&((e=this.selectIds)==null?void 0:e.length)===1){const o=this.selectIds[0],i=(t=this.vNodes.get(o))==null?void 0:t.op;if(i){const n=[];for(let a=0;a<i.length;a+=3)n.push([i[a],i[a+1]]);return n}}}consume(e){if(e.isSubWorker)return this.subWorkerConsume(e);let t={type:s.EPostMessageType.Select,dataType:s.EDataType.Local,...this.baseConsumeResult};if(e.isSimpleWorker){const h=this.subWorkerConsume(e);t.subRect=h.rect}const{op:o,workState:i}=e.data;let n=this.oldSelectRect;if(i===s.EvevtWorkState.Start&&(n=this.unSelectedAllIds()),!(o!=null&&o.length)||!this.vNodes.curNodeMap.size)return e.isSimpleWorker?t:{type:s.EPostMessageType.None};this.updateTempPoints(o);const a=this.computSelector();if(this.selectIds&&H(this.selectIds,a.selectIds))return e.isSimpleWorker?t:{type:s.EPostMessageType.None};this.selectIds=a.selectIds;const c=a.intersectRect;this.getSelecteorInfo(a.subNodeMap),this.draw(s.Storage_Selector_key,this.fullLayer,a),this.oldSelectRect=c;const l=this.getChildrenPoints();return t={...t,rect:s.computRect(c,n),selectIds:a.selectIds,selectRect:c,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 t,o;let e=this.oldSelectRect;if(!((t=this.selectIds)!=null&&t.length)&&this.tmpPoints[0]&&this.selectSingleTool(this.tmpPoints[0].XY,s.Storage_Selector_key,!1),(o=this.selectIds)!=null&&o.length&&(e=this.selectedByIds(this.selectIds)),e){const i=this.getChildrenPoints();return{type:s.EPostMessageType.Select,dataType:s.EDataType.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:i,isLocked:this.isLocked,toolsTypes:this.toolsTypes,shapeOpt:this.shapeOpt,thickness:this.thickness,useStroke:this.useStroke,strokeType:this.strokeType,...this.baseConsumeResult}}return{type:s.EPostMessageType.None}}consumeService(){}updateTempPointsForSubWorker(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]);if(this.subTmpPoints[0].isNear(o,1))return!1;if(this.subTmpPoints.length===2){if(o.isNear(this.subTmpPoints[1],1))return!1;this.subTmpPoints[1]=o}else this.subTmpPoints.push(o);return!0}computDrawPoints(e){const{thickness:t}=this.workOptions,o=[];for(const a of e)o.push(new s.Vec2d(...a));const i=s.getRectFromPoints(o,t),n=[i.x+i.w/2,i.y+i.h/2];return{rect:i,pos:n,points:o.map(a=>a.XY).flat(1)}}drawForSubWorker(e){const{workId:t,layer:o,ps:i}=e,{strokeColor:n,scale:a,rotate:c,translate:l}=this.workOptions,h=o.worldPosition,p=o.worldScaling,{points:d,rect:u,pos:f}=this.computDrawPoints(i),y=1/o.worldScaling[0],m=n&&s.colorRGBA2Array(n)||[0,0,0,0],w={close:!0,normalize:!0,points:d,lineWidth:y,fillColor:s.rgbToRgba(m[0],m[1],m[2],.1),strokeColor:s.rgbToRgba(m[0],m[1],m[2],1),lineJoin:"round",lineCap:"round"};let P={x:Math.floor(u.x*p[0]+h[0]-W.SafeBorderPadding),y:Math.floor(u.y*p[1]+h[1]-W.SafeBorderPadding),w:Math.floor(u.w*p[0]+2*W.SafeBorderPadding),h:Math.floor(u.h*p[0]+2*W.SafeBorderPadding)};const T=new D.Group({name:t,id:t,pos:f,anchor:[.5,.5],size:[u.w,u.h],scale:a,rotate:c,translate:l}),S=new D.Polyline({...w,pos:[0,0]});if(T.appendChild(S),this.replace(o,t,T),a||c||l){const E=T.getBoundingClientRect();P={x:Math.floor(E.x-W.SafeBorderPadding),y:Math.floor(E.y-W.SafeBorderPadding),w:Math.floor(E.width+2*W.SafeBorderPadding),h:Math.floor(E.height+2*W.SafeBorderPadding)}}return P}transformData(e){const t=s.getRectFromPoints(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:o}=e,{op:i,syncUnitTime:n}=t;n&&(this.syncUnitTime=n);const a=i==null?void 0:i.length;if(!a||a<2)return{type:s.EPostMessageType.None,rect:void 0};let c;if(this.subTmpPoints.length===0?(this.subTmpPoints=[new s.Point2d(i[0],i[1])],c=!1):c=this.updateTempPointsForSubWorker(i),!c)return{type:s.EPostMessageType.None,rect:void 0};const l=this.transformData(this.subTmpPoints),h=o?this.fullLayer:this.drawLayer||this.fullLayer,p=this.drawForSubWorker({ps:l,workId:this.workId,layer:h}),d=s.computRect(p,this.oldSubSelectRect);return this.oldSubSelectRect=p,{rect:d,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult}}subWorkerConsumeAll(e){const{isFullWork:t}=e,o=t?this.fullLayer:this.drawLayer||this.fullLayer;return this.replace(o,this.workId),{rect:this.oldSelectRect,type:s.EPostMessageType.FullWork,dataType:s.EDataType.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=s.Storage_Selector_key,o=!1){if(e.length===2){const i=e[0],n=e[1];let a;const{nodeRange:c}=this.vNodes.getRectIntersectRange({x:i,y:n,w:0,h:0},!1),l=[...c.values()].sort((h,p)=>(p.opt.zIndex||0)-(h.opt.zIndex||0));for(const h of l){const p=this.fullLayer.getElementsByName(h.name);if(qe(p).find(u=>u.isPointCollision(i,n))){a=h;break}}if(a){const h=a.name;if(!H(this.oldSelectRect,a.rect)){const p=new Map([[h,a]]);this.getSelecteorInfo(p),this.draw(t,this.fullLayer,{intersectRect:a.rect,subNodeMap:p,selectIds:this.selectIds||[]},o)}this.selectIds=[h],this.oldSelectRect=a.rect}}}unSelectedAllIds(){let e;for(const[t,o]of this.vNodes.curNodeMap.entries())o.isSelected&&(e=s.computRect(e,o.rect),this.vNodes.unSelected(t));return e}unSelectedByIds(e){let t;for(const o of e){const i=this.vNodes.get(o);i&&i.isSelected&&(t=s.computRect(t,i.rect),this.vNodes.unSelected(o))}return t}selectedByIds(e){let t;for(const o of e){const i=this.vNodes.get(o);i&&(t=s.computRect(t,i.rect),this.vNodes.selected(o))}return t}getSelectorRect(e,t){var a;let o;const i=(a=e.parent)==null?void 0:a.getElementById(t),n=i==null?void 0:i.getBoundingClientRect();return n&&(o=s.computRect(o,{x:Math.floor(n.x),y:Math.floor(n.y),w:Math.floor(n.width+1),h:Math.floor(n.height+1)})),o}isCanFillColor(e){return e===s.EToolsKey.Ellipse||e===s.EToolsKey.Triangle||e===s.EToolsKey.Rectangle||e===s.EToolsKey.Polygon||e===s.EToolsKey.Star||e===s.EToolsKey.SpeechBalloon}async updateSelector(e){const{updateSelectorOpt:t,selectIds:o,vNodes:i,willSerializeData:n,worker:a,offset:c}=e,l=this.fullLayer;if(!l)return;let h;const p=new Map,{originPoint:d,workState:u,angle:f,translate:y,dir:m,scale:w}=t;c&&(y?t.translate=[y[0]+c[0],y[1]+c[1]]:t.translate=c);let P;if(d||y||A(f)){if(u===s.EvevtWorkState.Start&&o)return i.setTargetAssignKeys(o),{type:s.EPostMessageType.Select,dataType:s.EDataType.Local,selectRect:this.oldSelectRect,rect:this.oldSelectRect};if(P=i.getLastTarget(),!P)return}if(o)for(const E of o){const I=i.get(E);if(I){const{toolsType:R,opt:M}=I,v=(l==null?void 0:l.getElementsByName(E))[0];if(v){const L={...t};let C;const b=W.isWillRefresh({toolsType:R,opt:M,updateOpt:L,vNodes:i,node:v,willSerializeData:n});if(R){C=P==null?void 0:P.get(E);const N=He(R);if(N==null||N.updateNodeOpt({node:v,opt:L,vNodes:i,willSerializeData:n,targetNode:C}),I&&a&&b){const x=a.createWorkShapeNode({workId:E,toolsType:R,toolsOpt:I.opt});x==null||x.setWorkId(E);let O;if(R===s.EToolsKey.BackgroundSVG)O=x.consumeService({isFullWork:!0,replaceId:E});else if(R===s.EToolsKey.Image)O=await x.consumeServiceAsync({isFullWork:!0,replaceId:E,worker:a});else if(R===s.EToolsKey.Text)O=await x.consumeServiceAsync({isFullWork:!0,replaceId:E,isDrawLabel:!0});else try{O=x==null?void 0:x.consumeService({op:I.op,isFullWork:!0,replaceId:E,workState:u})}catch(F){console.error("consumeService error",F);continue}O&&(I.rect=O)}I&&(p.set(E,I),h=s.computRect(h,I.rect))}}}}P&&u===s.EvevtWorkState.Done&&(i.deleteLastTarget(),P=void 0);const T=h;if(d&&y&&w&&m&&T&&!c){const E=[[T.x,T.y],[T.x+T.w,T.y],[T.x+T.w,T.y+T.h],[T.x,T.y+T.h]];let I;switch(m){case"top":case"topLeft":case"left":y[0]>0&&y[1]>0?I=E[0]:y[0]>0?I=E[3]:y[1]>0?I=E[1]:I=E[2];break;case"topRight":y[0]<0&&y[1]>0?I=E[1]:y[0]<0?I=E[2]:y[1]>0?I=E[0]:I=E[3];break;case"right":case"bottomRight":case"bottom":y[0]<0&&y[1]<0?I=E[2]:y[0]<0?I=E[1]:y[1]<0?I=E[3]:I=E[0];break;case"bottomLeft":y[0]>0&&y[1]<0?I=E[3]:y[0]>0?I=E[0]:y[1]<0?I=E[2]:I=E[1];break}const R=I&&[d[0]-I[0],d[1]-I[1]]||[0,0];if(!H(R,[0,0]))return await this.updateSelector({...e,updateSelectorOpt:{workState:u},offset:R})}this.getSelecteorInfo(p),this.draw(s.Storage_Selector_key,l,{selectIds:o||[],subNodeMap:p,intersectRect:T});const S=s.computRect(this.oldSelectRect,T);return this.oldSelectRect=T,{type:s.EPostMessageType.Select,dataType:s.EDataType.Local,selectRect:T,renderRect:h,rect:s.computRect(S,T),selectIds:o}}blurSelector(){const e=this.unSelectedAllIds();return{type:s.EPostMessageType.Select,dataType:s.EDataType.Local,rect:e,selectIds:[],willSyncService:!0}}getRightServiceId(e){return e.replace(s.Storage_Splitter,"-")}selectServiceNode(e,t,o){const{selectIds:i}=t,n=this.getRightServiceId(e),a=this.getSelectorRect(this.fullLayer,n);let c;const l=new Map;return i==null||i.forEach(h=>{const p=this.vNodes.get(h);p&&(c=s.computRect(c,p.rect),l.set(h,p))}),this.getSelecteorInfo(l),this.draw(n,this.fullLayer,{intersectRect:c,selectIds:i||[],subNodeMap:l},o),s.computRect(c,a)}reRenderSelector(){var o;let e;const t=new Map;return(o=this.selectIds)==null||o.forEach(i=>{const n=this.vNodes.get(i);n&&(e=s.computRect(e,n.rect),t.set(i,n))},this),this.getSelecteorInfo(t),this.draw(s.Storage_Selector_key,this.fullLayer,{intersectRect:e,subNodeMap:t,selectIds:this.selectIds||[]}),this.oldSelectRect=e,e}updateSelectIds(e){var n;let t;const o=(n=this.selectIds)==null?void 0:n.filter(a=>!e.includes(a));if(o!=null&&o.length&&(t=this.unSelectedByIds(o)),e.length){const a=this.selectedByIds(e);t=s.computRect(t,a)}this.selectIds=e;const i=this.reRenderSelector();return{bgRect:t,selectRect:i}}cursorHover(e){var n,a;const t=this.oldSelectRect;this.selectIds=[];const o=(n=this.workId)==null?void 0:n.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,o,!0),this.oldSelectRect&&!H(t,this.oldSelectRect))return{type:s.EPostMessageType.CursorHover,dataType:s.EDataType.Local,rect:s.computRect(t,this.oldSelectRect),selectorColor:this.selectorColor,willSyncService:!1};if((a=this.selectIds)!=null&&a.length||(this.oldSelectRect=void 0),t&&!this.oldSelectRect)return this.cursorBlur(),{type:s.EPostMessageType.CursorHover,dataType:s.EDataType.Local,rect:t,selectorColor:this.selectorColor,willSyncService:!1}}cursorBlur(){var t,o;this.selectIds=[];const e=(t=this.workId)==null?void 0:t.toString();((o=this.fullLayer)==null?void 0:o.parent).children.forEach(i=>{i.name===e&&i.remove()})}};g(ge,"selectorBorderId","selector-border");let ue=ge;class Ke extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.both);g(this,"toolsType",s.EToolsKey.Arrow);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"arrowTipWidth");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.arrowTipWidth=this.workOptions.thickness*4,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,l=this.workId,{op:h,syncUnitTime:p}=t;p&&(this.syncUnitTime=p);const d=h==null?void 0:h.length;if(!d||d<2)return{type:s.EPostMessageType.None};let u;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(h[0],h[1])],u=!1):u=this.updateTempPoints(h),!u)return{type:s.EPostMessageType.None};let f;if(i||n||c){const m=o?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({workId:l,layer:m})}if(c){const m=s.computRect(f,this.oldRect);this.oldRect=f;const w={rect:m,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult};if(a){const P=Date.now();P-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=P,w.op=this.tmpPoints.map(T=>[...T.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!i&&a){const m=Date.now();return m-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=m,{...this.baseConsumeResult,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0}):{type:s.EPostMessageType.None}}const y=s.computRect(f,this.oldRect);return this.oldRect=f,{rect:y,...this.baseConsumeResult,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local}}consumeAll(){var a;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.fullLayer,o=this.draw({workId:e,layer:t});this.oldRect=o;const i=this.tmpPoints.map(c=>[...c.XY,0]).flat(1),n=s.transformToSerializableData(i);return(a=this.vNodes)==null||a.setInfo(e,{rect:o,op:i,opt:this.workOptions,toolsType:this.toolsType,canRotate:this.canRotate,scaleType:this.scaleType,centerPos:W.getCenterPos(o,t)}),{rect:o,...this.baseConsumeResult,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:n,isSync:!0,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawEraserlines:i}=e,{strokeColor:n,thickness:a,zIndex:c,scale:l,rotate:h,translate:p,strokeType:d,eraserlines:u,lineDash:f,lineCap:y}=this.workOptions,m=o.worldPosition,w=o.worldScaling,{points:P,pos:T,rect:S,isTriangle:E,trianglePoints:I,trianglePos:R}=this.computDrawPoints(a),M=[S.x+S.w/2,S.y+S.h/2],v={pos:M,name:t,id:t,zIndex:c,anchor:[.5,.5],size:[S.w,S.h]};l&&(v.scale=l),h&&(v.rotate=h),p&&(v.translate=p);const L=new D.Group(v),C={points:I,pos:[R[0]-M[0],R[1]-M[1]],fillColor:n,strokeColor:n,lineWidth:0,normalize:!1},b=new D.Polyline(C);if(L.append(b),!E&&P&&T){const x={points:P,pos:[T[0]-M[0],T[1]-M[1]],fillColor:n,strokeColor:n,lineDash:d===s.EStrokeType.Dotted?[f&&f[0]||1,(f&&f[1]||2)*a]:d===s.EStrokeType.LongDotted?[(f&&f[0]||1)*a,(f&&f[1]||2)*a]:void 0,lineCap:d===s.EStrokeType.Normal?void 0:y,lineWidth:a,normalize:!1},O=new D.Polyline(x);L.append(O)}u&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:L,eraserlines:u,pos:M,layer:o},i)),this.replace(o,t,L);let N={x:Math.floor(S.x*w[0]+m[0]-W.SafeBorderPadding),y:Math.floor(S.y*w[1]+m[1]-W.SafeBorderPadding),w:Math.floor(S.w*w[0]+2*W.SafeBorderPadding),h:Math.floor(S.h*w[1]+2*W.SafeBorderPadding)};if(l||h||p){const x=L.getBoundingClientRect();N={x:Math.floor(x.x-W.SafeBorderPadding),y:Math.floor(x.y-W.SafeBorderPadding),w:Math.floor(x.width+W.SafeBorderPadding*2),h:Math.floor(x.height+W.SafeBorderPadding*2)}}return this.isDelete&&L.setAttribute("opacity",0),N}computDrawPoints(e){return this.tmpPoints[1].distance(this.tmpPoints[0])>this.arrowTipWidth?this.computFullArrowPoints(e):this.computTrianglePoints()}computFullArrowPoints(e){const t=s.Vec2d.Sub(this.tmpPoints[1],this.tmpPoints[0]).uni(),o=s.Vec2d.Per(t).mul(e/2),i=s.Point2d.Sub(this.tmpPoints[0],o),n=s.Point2d.Add(this.tmpPoints[0],o),a=s.Vec2d.Mul(t,this.arrowTipWidth),c=s.Vec2d.Sub(this.tmpPoints[1],a),l=s.Point2d.Sub(c,o),h=s.Point2d.Add(c,o),p=s.Vec2d.Per(t).mul(e*1.5),d=s.Point2d.Sub(c,p),u=s.Point2d.Add(c,p),f=[this.tmpPoints[0],c],y=[d,this.tmpPoints[1],u],m=[i,n,...y,l,h];return{trianglePoints:y.map(w=>s.Point2d.Sub(w,this.tmpPoints[1]).XY).flat(1),trianglePos:this.tmpPoints[1].XY,points:f.map(w=>s.Point2d.Sub(w,this.tmpPoints[0]).XY).flat(1),rect:s.getRectFromPoints(m),isTriangle:!1,pos:this.tmpPoints[0].XY}}computTrianglePoints(){const e=s.Vec2d.Sub(this.tmpPoints[1],this.tmpPoints[0]).uni(),t=this.tmpPoints[1].distance(this.tmpPoints[0]),o=s.Vec2d.Per(e).mul(Math.floor(t*3/8)),i=s.Point2d.Sub(this.tmpPoints[0],o),n=s.Point2d.Add(this.tmpPoints[0],o),a=[i,this.tmpPoints[1],n];return{trianglePoints:a.map(c=>s.Point2d.Sub(c,this.tmpPoints[1]).XY).flat(1),trianglePos:this.tmpPoints[1].XY,rect:s.getRectFromPoints(a),isTriangle:!0}}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var l,h;const{op:t,isFullWork:o,workState:i=s.EvevtWorkState.Done}=e,n=(l=this.workId)==null?void 0:l.toString();if(!n)return;this.tmpPoints.length=0;for(let p=0;p<t.length;p+=3)this.tmpPoints.push(new s.Point2d(t[p],t[p+1],t[p+2]));const a=o?this.fullLayer:this.drawLayer||this.fullLayer,c=this.draw({workId:n,layer:a,isDrawEraserlines:i===s.EvevtWorkState.Done});return this.oldRect=c,(h=this.vNodes)==null||h.setInfo(n,{rect:c,op:t,opt:this.workOptions,toolsType:this.toolsType,canRotate:this.canRotate,scaleType:this.scaleType,centerPos:W.getCenterPos(c,a)}),c}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var l,h;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,strokeType:a}=o,c=i.get(t.name);return n&&(t.tagName==="GROUP"?t.children.forEach(p=>{p.setAttribute("strokeColor",n),p.getAttribute("fillColor")&&p.setAttribute("fillColor",n)}):(t.setAttribute("strokeColor",n),t.setAttribute("fillColor",n)),(l=c==null?void 0:c.opt)!=null&&l.strokeColor&&(c.opt.strokeColor=n)),a&&(c!=null&&c.opt)&&((h=c.opt)!=null&&h.strokeType)&&(c.opt.strokeType=a),c&&i.setInfo(t.name,c),W.updateNodeOpt(e)}}class Ve extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.Ellipse);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,l=this.workId,{op:h,syncUnitTime:p}=t;p&&(this.syncUnitTime=p);const d=h==null?void 0:h.length;if(!d||d<2)return{type:s.EPostMessageType.None};let u;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(h[0],h[1])],u=!1):u=this.updateTempPoints(h),!u)return{type:s.EPostMessageType.None};let f;if(i||n||c){const y=o?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({workId:l,layer:y,isDrawing:!0});const m=y.parent.parent,w={x:0,y:0,w:Math.floor(m.width),h:Math.floor(m.height)},P={type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult};if(s.getRectMatrixrRelation(f,w)!==s.EMatrixrRelationType.outside){const S=s.computRect(f,this.oldRect);if(this.oldRect=f,P.rect=S,!c)return P}if(c){const S=Date.now();return S-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=S,P.op=this.tmpPoints.map(E=>[...E.XY,0]).flat(1),P.index=0,P.isSync=!0),P}return{type:s.EPostMessageType.None}}if(!i&&!n&&!c&&a){const y=Date.now();return y-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=y,{type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult,op:this.tmpPoints.map(m=>[...m.XY,0]).flat(1),isSync:!0,index:0}):{type:s.EPostMessageType.None}}return{type:s.EPostMessageType.None}}consumeAll(){var a;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.fullLayer,o=this.draw({workId:e,layer:t,isDrawing:!1});this.oldRect=o;const i=this.tmpPoints.map(c=>[...c.XY,0]).flat(1),n=s.transformToSerializableData(i);return(a=this.vNodes)==null||a.setInfo(e,{rect:o,op:i,opt:this.workOptions,toolsType:this.toolsType,canRotate:this.canRotate,scaleType:this.scaleType,centerPos:o&&W.getCenterPos(o,t)}),{rect:o,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,...this.baseConsumeResult,ops:n,isSync:!0,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawing:i,isDrawEraserlines:n}=e,{strokeColor:a,fillColor:c,thickness:l,zIndex:h,scale:p,rotate:d,translate:u,strokeType:f,eraserlines:y,lineDash:m,lineCap:w}=this.workOptions,P=o.worldScaling,{radius:T,rect:S,pos:E}=this.computDrawPoints(l),I={closeType:"normal",radius:T,lineWidth:l,fillColor:c!=="transparent"&&c||void 0,strokeColor:a,normalize:!0,lineCap:w,lineDash:f===s.EStrokeType.Dotted?[m&&m[0]||1,(m&&m[1]||2)*l]:f===s.EStrokeType.LongDotted?[(m&&m[0]||1)*l,(m&&m[1]||2)*l]:void 0},R={name:t,id:t,zIndex:h,pos:E,anchor:[.5,.5],size:[S.w,S.h]};p&&(R.scale=p),d&&(R.rotate=d),u&&(R.translate=u);const M=new D.Group(R);if(i){const C=new D.Path({d:"M-4,0H4M0,-4V4",normalize:!0,pos:[0,0],strokeColor:a,lineWidth:1,scale:[1/P[0],1/P[1]]});M.append(C)}const v=new D.Ellipse({...I,pos:[0,0]});M.append(v),y&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:M,eraserlines:y,pos:E,layer:o},n)),this.replace(o,t,M);const L=M.getBoundingClientRect();return this.isDelete&&M.setAttribute("opacity",0),{x:Math.floor(L.x-W.SafeBorderPadding),y:Math.floor(L.y-W.SafeBorderPadding),w:Math.floor(L.width+W.SafeBorderPadding*2),h:Math.floor(L.height+W.SafeBorderPadding*2)}}computDrawPoints(e){const t=s.getRectFromPoints(this.tmpPoints),o=s.getRectFromPoints(this.tmpPoints,e),i=[Math.floor(t.x+t.w/2),Math.floor(t.y+t.h/2)];return{rect:o,pos:i,radius:[Math.floor(t.w/2),Math.floor(t.h/2)]}}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var l,h;const{op:t,isFullWork:o,workState:i=s.EvevtWorkState.Done}=e,n=(l=this.workId)==null?void 0:l.toString();if(!n)return;this.tmpPoints.length=0;for(let p=0;p<t.length;p+=3)this.tmpPoints.push(new s.Point2d(t[p],t[p+1],t[p+2]));const a=o?this.fullLayer:this.drawLayer||this.fullLayer,c=this.draw({workId:n,layer:a,isDrawing:!1,isDrawEraserlines:i===s.EvevtWorkState.Done});return this.oldRect=c,(h=this.vNodes)==null||h.setInfo(n,{rect:c,op:t,opt:this.workOptions,toolsType:this.toolsType,canRotate:this.canRotate,scaleType:this.scaleType,centerPos:W.getCenterPos(c,a)}),c}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var p,d,u;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,fillColor:a,strokeType:c}=o,l=i.get(t.name);let h=t;return t.tagName==="GROUP"&&(h=t.children[0]),n&&(h.setAttribute("strokeColor",n),(p=l==null?void 0:l.opt)!=null&&p.strokeColor&&(l.opt.strokeColor=n)),a&&(a==="transparent"?h.setAttribute("fillColor","rgba(0,0,0,0)"):h.setAttribute("fillColor",a),(d=l==null?void 0:l.opt)!=null&&d.fillColor&&(l.opt.fillColor=a)),c&&(l!=null&&l.opt)&&((u=l.opt)!=null&&u.strokeType)&&(l.opt.strokeType=c),l&&i.setInfo(t.name,l),W.updateNodeOpt(e)}}class _e extends W{constructor(e){super(e);g(this,"canRotate",!0);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.Rectangle);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}transformData(){const e=s.getRectFromPoints(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,o=[];for(const a of e)o.push(new s.Vec2d(...a));const i=s.getRectFromPoints(o,t),n=[i.x+i.w/2,i.y+i.h/2];return{rect:i,pos:n,points:o.map(a=>a.XY).flat(1)}}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,l=this.workId,{op:h}=t,p=h==null?void 0:h.length;if(!p||p<2)return{type:s.EPostMessageType.None};let d;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(h[0],h[1])],d=!1):d=this.updateTempPoints(h),!d)return{type:s.EPostMessageType.None};const u=this.transformData();let f;if(i||n||c){const m=o?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({ps:u,workId:l,layer:m,isDrawing:!0})}if(c){const m=s.computRect(f,this.oldRect);this.oldRect=f;const w={rect:m,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult};if(a){const P=Date.now();P-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=P,w.op=u.flat(1),w.index=0,w.isSync=!0)}return w}if(!i&&a){const m=Date.now();return m-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=m,{type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:u.flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:s.EPostMessageType.None}}const y=s.computRect(f,this.oldRect);return this.oldRect=f,{rect:y,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult}}consumeAll(){var c;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.transformData(),o=this.fullLayer,i=this.draw({ps:t,workId:e,layer:o,isDrawing:!1});this.oldRect=i;const n=t.flat(1),a=s.transformToSerializableData(n);return(c=this.vNodes)==null||c.setInfo(e,{rect:i,op:n,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:i&&W.getCenterPos(i,o)}),{rect:i,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:a,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawing:i,ps:n,replaceId:a,isDrawEraserlines:c=!0}=e,{strokeColor:l,fillColor:h,thickness:p,zIndex:d,scale:u,rotate:f,translate:y,strokeType:m,eraserlines:w,lineCap:P,lineDash:T}=this.workOptions,S=o.worldPosition,E=o.worldScaling,{points:I,rect:R,pos:M}=this.computDrawPoints(n),v={close:!0,normalize:!0,points:I,lineWidth:p,fillColor:h!=="transparent"&&h||void 0,strokeColor:l,lineCap:P,lineDash:m===s.EStrokeType.Dotted?[T&&T[0]||1,(T&&T[1]||2)*p]:m===s.EStrokeType.LongDotted?[(T&&T[0]||1)*p,(T&&T[1]||2)*p]:void 0};let L={x:Math.floor(R.x*E[0]+S[0]-W.SafeBorderPadding),y:Math.floor(R.y*E[1]+S[1]-W.SafeBorderPadding),w:Math.floor(R.w*E[0]+2*W.SafeBorderPadding),h:Math.floor(R.h*E[0]+2*W.SafeBorderPadding)};const C=new D.Group({name:t,id:t,zIndex:d,pos:M,anchor:[.5,.5],size:[R.w,R.h],scale:u,rotate:f,translate:y}),b=new D.Polyline({...v,pos:[0,0]});if(C.appendChild(b),i){const N=new D.Path({d:"M-4,0H4M0,-4V4",normalize:!0,pos:[0,0],strokeColor:l,lineWidth:1,scale:[1/E[0],1/E[1]]});C.appendChild(N)}if(w&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:C,eraserlines:w,pos:M,layer:o},c)),this.replace(o,a||t,C),u||f||y){const N=C.getBoundingClientRect();L={x:Math.floor(N.x-W.SafeBorderPadding),y:Math.floor(N.y-W.SafeBorderPadding),w:Math.floor(N.width+2*W.SafeBorderPadding),h:Math.floor(N.height+2*W.SafeBorderPadding)}}return this.isDelete&&C.setAttribute("opacity",0),L}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var p,d;const{op:t,isFullWork:o,replaceId:i,workState:n=s.EvevtWorkState.Done}=e,a=(p=this.workId)==null?void 0:p.toString();if(!a)return;const c=[];for(let u=0;u<t.length;u+=3)c.push([t[u],t[u+1],t[u+2]]);const l=o?this.fullLayer:this.drawLayer||this.fullLayer,h=this.draw({ps:c,workId:a,layer:l,isDrawing:!1,replaceId:i,isDrawEraserlines:n===s.EvevtWorkState.Done});return this.oldRect=h,(d=this.vNodes)==null||d.setInfo(a,{rect:h,op:t,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:h&&W.getCenterPos(h,l)}),h}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var p,d,u;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,fillColor:a,strokeType:c}=o,l=i.get(t.name);let h=t;return t.tagName==="GROUP"&&(h=t.children[0]),n&&(h.setAttribute("strokeColor",n),(p=l==null?void 0:l.opt)!=null&&p.strokeColor&&(l.opt.strokeColor=n)),a&&(a==="transparent"?h.setAttribute("fillColor","rgba(0,0,0,0)"):h.setAttribute("fillColor",a),(d=l==null?void 0:l.opt)!=null&&d.fillColor&&(l.opt.fillColor=a)),c&&(l!=null&&l.opt)&&((u=l.opt)!=null&&u.strokeType)&&(l.opt.strokeType=c),l&&i.setInfo(t.name,l),W.updateNodeOpt(e)}}class Ge extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.Star);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,l=this.workId,{op:h,syncUnitTime:p}=t,d=h==null?void 0:h.length;if(!d||d<2)return{type:s.EPostMessageType.None};p&&(this.syncUnitTime=p);let u;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(h[0],h[1])],u=!1):u=this.updateTempPoints(h),!u)return{type:s.EPostMessageType.None};let f;if(i||n||c){const m=o?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({workId:l,layer:m,isDrawing:!0})}if(c){const m=s.computRect(f,this.oldRect);this.oldRect=f;const w={rect:m,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult};if(a){const P=Date.now();P-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=P,w.op=this.tmpPoints.map(T=>[...T.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!i&&a){const m=Date.now();return m-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=m,{type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:s.EPostMessageType.None}}const y=s.computRect(f,this.oldRect);return this.oldRect=f,{rect:y,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult}}consumeAll(){var a;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.fullLayer,o=this.draw({workId:e,layer:t,isDrawing:!1});this.oldRect=o;const i=this.tmpPoints.map(c=>[...c.XY,0]).flat(1),n=s.transformToSerializableData(i);return(a=this.vNodes)==null||a.setInfo(e,{rect:o,op:i,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:o&&W.getCenterPos(o,t)}),{rect:o,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:n,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawing:i,isDrawEraserlines:n=!0}=e,{strokeColor:a,fillColor:c,thickness:l,zIndex:h,vertices:p,innerVerticeStep:d,innerRatio:u,scale:f,rotate:y,translate:m,strokeType:w,eraserlines:P,lineDash:T,lineCap:S}=this.workOptions,E=o.worldScaling,{rect:I,pos:R,points:M}=this.computDrawPoints(l,p,d,u),v={close:!0,points:M,lineWidth:l,fillColor:c!=="transparent"&&c||void 0,strokeColor:a,normalize:!0,lineCap:S,lineDash:w===s.EStrokeType.Dotted?[T&&T[0]||1,(T&&T[1]||2)*l]:w===s.EStrokeType.LongDotted?[(T&&T[0]||1)*l,(T&&T[1]||2)*l]:void 0},L={name:t,id:t,zIndex:h,pos:R,anchor:[.5,.5],size:[I.w,I.h]};f&&(L.scale=f),y&&(L.rotate=y),m&&(L.translate=m);const C=new D.Group(L);if(i){const x=new D.Path({d:"M-4,0H4M0,-4V4",normalize:!0,pos:[0,0],strokeColor:a,lineWidth:1,scale:[1/E[0],1/E[1]]});C.append(x)}const b=new D.Polyline({...v,pos:[0,0]});C.append(b),P&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:C,eraserlines:P,pos:R,layer:o},n)),this.replace(o,t,C);const N=C.getBoundingClientRect();return this.isDelete&&C.setAttribute("opacity",0),{x:Math.floor(N.x-W.SafeBorderPadding),y:Math.floor(N.y-W.SafeBorderPadding),w:Math.floor(N.width+W.SafeBorderPadding*2),h:Math.floor(N.height+W.SafeBorderPadding*2)}}computDrawPoints(e,t,o,i){const n=s.getRectFromPoints(this.tmpPoints),a=[Math.floor(n.x+n.w/2),Math.floor(n.y+n.h/2)],c=s.getWHRatio(n.w,n.h),l=Math.floor(Math.min(n.w,n.h)/2),h=i*l,p=[],d=2*Math.PI/t;for(let f=0;f<t;f++){const y=f*d-.5*Math.PI;let m,w;f%o===1?(m=h*c[0]*Math.cos(y),w=h*c[1]*Math.sin(y)):(m=l*c[0]*Math.cos(y),w=l*c[1]*Math.sin(y),p.push(m,w)),p.push(m,w)}return{rect:s.getRectFromPoints(this.tmpPoints,e),pos:a,points:p}}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n)||s.Point2d.Sub(i,o).XY.includes(0))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var l,h;const{op:t,isFullWork:o,workState:i=s.EvevtWorkState.Done}=e,n=(l=this.workId)==null?void 0:l.toString();if(!n)return;this.tmpPoints.length=0;for(let p=0;p<t.length;p+=3)this.tmpPoints.push(new s.Point2d(t[p],t[p+1],t[p+2]));const a=o?this.fullLayer:this.drawLayer||this.fullLayer,c=this.draw({workId:n,layer:a,isDrawing:!1,isDrawEraserlines:i===s.EvevtWorkState.Done});return this.oldRect=c,(h=this.vNodes)==null||h.setInfo(n,{rect:c,op:t,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:c&&W.getCenterPos(c,a)}),c}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var m;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,fillColor:a,toolsType:c,vertices:l,innerVerticeStep:h,innerRatio:p,strokeType:d}=o,u=i.get(t.name),f=u==null?void 0:u.opt;let y=t;return t.tagName==="GROUP"&&(y=t.children[0]),n&&(y.setAttribute("strokeColor",n),f!=null&&f.strokeColor&&(f.strokeColor=n)),a&&(a==="transparent"?y.setAttribute("fillColor","rgba(0,0,0,0)"):y.setAttribute("fillColor",a),f!=null&&f.fillColor&&(f.fillColor=a)),c===s.EToolsKey.Star&&(l&&(f.vertices=l),h&&(f.innerVerticeStep=h),p&&(f.innerRatio=p)),d&&(u!=null&&u.opt)&&((m=u.opt)!=null&&m.strokeType)&&(u.opt.strokeType=d),u&&i.setInfo(t.name,{...u,opt:f}),W.updateNodeOpt(e)}}class Xe extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.Polygon);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,{op:l,syncUnitTime:h}=t;h&&(this.syncUnitTime=h);const p=this.workId,d=l==null?void 0:l.length;if(!d||d<2)return{type:s.EPostMessageType.None};let u;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(l[0],l[1])],u=!1):u=this.updateTempPoints(l),!u)return{type:s.EPostMessageType.None};let f;if(i||n||c){const m=o?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({workId:p,layer:m,isDrawing:!0})}if(c){const m=s.computRect(f,this.oldRect);this.oldRect=f;const w={type:s.EPostMessageType.DrawWork,rect:m,dataType:s.EDataType.Local,...this.baseConsumeResult};if(a){const P=Date.now();P-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=P,w.op=this.tmpPoints.map(T=>[...T.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!i&&a){const m=Date.now();return m-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=m,{type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:s.EPostMessageType.None}}const y=s.computRect(f,this.oldRect);return this.oldRect=f,{rect:y,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult}}consumeAll(){var a;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.fullLayer,o=this.draw({workId:e,layer:t,isDrawing:!1});this.oldRect=o;const i=this.tmpPoints.map(c=>[...c.XY,0]).flat(1),n=s.transformToSerializableData(i);return(a=this.vNodes)==null||a.setInfo(e,{rect:o,op:i,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:o&&W.getCenterPos(o,t)}),{rect:o,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:n,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawing:i,isDrawEraserlines:n=!0}=e,{strokeColor:a,fillColor:c,thickness:l,zIndex:h,vertices:p,scale:d,rotate:u,translate:f,strokeType:y,eraserlines:m,lineCap:w,lineDash:P}=this.workOptions,T=o.worldScaling,{rect:S,pos:E,points:I}=this.computDrawPoints(l,p),R={close:!0,points:I,lineWidth:l,fillColor:c!=="transparent"&&c||void 0,strokeColor:a,normalize:!0,lineCap:w,lineDash:y===s.EStrokeType.Dotted?[P&&P[0]||1,(P&&P[1]||2)*l]:y===s.EStrokeType.LongDotted?[(P&&P[0]||1)*l,(P&&P[1]||2)*l]:void 0},M={name:t,id:t,zIndex:h,pos:E,anchor:[.5,.5],size:[S.w,S.h]};d&&(M.scale=d),u&&(M.rotate=u),f&&(M.translate=f);const v=new D.Group(M);if(i){const b=new D.Path({d:"M-4,0H4M0,-4V4",normalize:!0,pos:[0,0],strokeColor:a,lineWidth:1,scale:[1/T[0],1/T[1]]});v.append(b)}const L=new D.Polyline({...R,pos:[0,0]});v.append(L),m&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:v,eraserlines:m,pos:E,layer:o},n)),this.replace(o,t,v);const C=v.getBoundingClientRect();return this.isDelete&&v.setAttribute("opacity",0),{x:Math.floor(C.x-W.SafeBorderPadding),y:Math.floor(C.y-W.SafeBorderPadding),w:Math.floor(C.width+W.SafeBorderPadding*2),h:Math.floor(C.height+W.SafeBorderPadding*2)}}computDrawPoints(e,t){const o=s.getRectFromPoints(this.tmpPoints),i=[Math.floor(o.x+o.w/2),Math.floor(o.y+o.h/2)],n=s.getWHRatio(o.w,o.h),a=Math.floor(Math.min(o.w,o.h)/2),c=[],l=2*Math.PI/t;for(let p=0;p<t;p++){const d=p*l-.5*Math.PI,u=a*n[0]*Math.cos(d),f=a*n[1]*Math.sin(d);c.push(u,f)}return{rect:s.getRectFromPoints(this.tmpPoints,e),pos:i,points:c}}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n)||s.Point2d.Sub(i,o).XY.includes(0))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var l,h;const{op:t,isFullWork:o,workState:i=s.EvevtWorkState.Done}=e,n=(l=this.workId)==null?void 0:l.toString();if(!n)return;this.tmpPoints.length=0;for(let p=0;p<t.length;p+=3)this.tmpPoints.push(new s.Point2d(t[p],t[p+1],t[p+2]));const a=o?this.fullLayer:this.drawLayer||this.fullLayer,c=this.draw({workId:n,layer:a,isDrawing:!1,isDrawEraserlines:i===s.EvevtWorkState.Done});return this.oldRect=c,(h=this.vNodes)==null||h.setInfo(n,{rect:c,op:t,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:c&&W.getCenterPos(c,a)}),c}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var f;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,fillColor:a,toolsType:c,vertices:l,strokeType:h}=o,p=i.get(t.name),d=p==null?void 0:p.opt;let u=t;return t.tagName==="GROUP"&&(u=t.children[0]),n&&(u.setAttribute("strokeColor",n),d!=null&&d.strokeColor&&(d.strokeColor=n)),a&&(a==="transparent"?u.setAttribute("fillColor","rgba(0,0,0,0)"):u.setAttribute("fillColor",a),d!=null&&d.fillColor&&(d.fillColor=a)),c===s.EToolsKey.Polygon&&l&&(d.vertices=l),h&&(p!=null&&p.opt)&&((f=p.opt)!=null&&f.strokeType)&&(p.opt.strokeType=h),p&&i.setInfo(t.name,{...p,opt:d}),W.updateNodeOpt(e)}}class V{static bezier(r,e){const t=[];for(let o=0;o<e.length;o+=4){const i=e[o],n=e[o+1],a=e[o+2],c=e[o+3];i&&n&&a&&c?t.push(...V.getBezierPoints(r,i,n,a,c)):i&&n&&a?t.push(...V.getBezierPoints(r,i,n,a)):i&&n?t.push(...V.getBezierPoints(r,i,n)):i&&t.push(i)}return t}static getBezierPoints(r=10,e,t,o,i){let n=null;const a=[];!o&&!i?n=V.oneBezier:o&&!i?n=V.twoBezier:o&&i&&(n=V.threeBezier);for(let c=0;c<r;c++)n&&a.push(n(c/r,e,t,o,i));return i?a.push(i):o&&a.push(o),a}static oneBezier(r,e,t){const o=e.x+(t.x-e.x)*r,i=e.y+(t.y-e.y)*r;return new s.Vec2d(o,i)}static twoBezier(r,e,t,o){const i=(1-r)*(1-r)*e.x+2*r*(1-r)*t.x+r*r*o.x,n=(1-r)*(1-r)*e.y+2*r*(1-r)*t.y+r*r*o.y;return new s.Vec2d(i,n)}static threeBezier(r,e,t,o,i){const n=e.x*(1-r)*(1-r)*(1-r)+3*t.x*r*(1-r)*(1-r)+3*o.x*r*r*(1-r)+i.x*r*r*r,a=e.y*(1-r)*(1-r)*(1-r)+3*t.y*r*(1-r)*(1-r)+3*o.y*r*r*(1-r)+i.y*r*r*r;return new s.Vec2d(n,a)}}class Ye extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.SpeechBalloon);g(this,"ratio",.8);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){var y;const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,l=(y=t==null?void 0:t.workId)==null?void 0:y.toString();if(!l)return{type:s.EPostMessageType.None};const{op:h}=t,p=h==null?void 0:h.length;if(!p||p<2)return{type:s.EPostMessageType.None};let d;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(h[0],h[1])],d=!1):d=this.updateTempPoints(h),!d)return{type:s.EPostMessageType.None};let u;if(i||n||c){const m=o?this.fullLayer:this.drawLayer||this.fullLayer;u=this.draw({workId:l,layer:m,isDrawing:!0})}if(c){const m=s.computRect(u,this.oldRect);this.oldRect=u;const w={rect:m,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult};if(a){const P=Date.now();P-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=P,w.op=this.tmpPoints.map(T=>[...T.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!i&&a){const m=Date.now();return m-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=m,{type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:s.EPostMessageType.None}}const f=s.computRect(u,this.oldRect);return this.oldRect=u,{rect:f,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult}}consumeAll(){var a;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.fullLayer,o=this.draw({workId:e,layer:t,isDrawing:!1});this.oldRect=o;const i=this.tmpPoints.map(c=>[...c.XY,0]).flat(1),n=s.transformToSerializableData(i);return(a=this.vNodes)==null||a.setInfo(e,{rect:o,op:i,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:o&&W.getCenterPos(o,t)}),{rect:o,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:n,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawEraserlines:i=!0}=e,{strokeColor:n,fillColor:a,thickness:c,zIndex:l,placement:h,scale:p,rotate:d,translate:u,strokeType:f,eraserlines:y,lineDash:m,lineCap:w}=this.workOptions,{rect:P,pos:T,points:S}=this.computDrawPoints(c,h),E={points:S.map(L=>L.XY),lineWidth:c,fillColor:a!=="transparent"&&a||void 0,strokeColor:n,normalize:!0,className:`${T[0]},${T[1]}`,close:!0,lineCap:w,lineDash:f===s.EStrokeType.Dotted?[m&&m[0]||1,(m&&m[1]||2)*c]:f===s.EStrokeType.LongDotted?[(m&&m[0]||1)*c,(m&&m[1]||2)*c]:void 0},I={name:t,id:t,zIndex:l,pos:T,anchor:[.5,.5],size:[P.w,P.h]};p&&(I.scale=p),d&&(I.rotate=d),u&&(I.translate=u);const R=new D.Group(I),M=new D.Polyline({...E,pos:[0,0]});R.append(M),y&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:R,eraserlines:y,pos:T,layer:o},i)),this.replace(o,t,R);const v=R.getBoundingClientRect();return this.isDelete&&R.setAttribute("opacity",0),{x:Math.floor(v.x-W.SafeBorderPadding),y:Math.floor(v.y-W.SafeBorderPadding),w:Math.floor(v.width+W.SafeBorderPadding*2),h:Math.floor(v.height+W.SafeBorderPadding*2)}}transformControlPoints(e){const t=s.getRectFromPoints(this.tmpPoints);switch(e){case"bottom":case"bottomLeft":case"bottomRight":{const o=t.y+t.h*this.ratio;return[new s.Vec2d(t.x,t.y,0),new s.Vec2d(t.x+t.w,t.y,0),new s.Vec2d(t.x+t.w,o,0),new s.Vec2d(t.x,o,0)]}case"top":case"topLeft":case"topRight":{const o=t.y+t.h*(1-this.ratio);return[new s.Vec2d(t.x,o,0),new s.Vec2d(t.x+t.w,o,0),new s.Vec2d(t.x+t.w,t.y+t.h,0),new s.Vec2d(t.x,t.y+t.h,0)]}case"left":case"leftBottom":case"leftTop":{const o=t.x+t.w*(1-this.ratio);return[new s.Vec2d(o,t.y,0),new s.Vec2d(t.x+t.w,t.y,0),new s.Vec2d(t.x+t.w,t.y+t.h,0),new s.Vec2d(o,t.y+t.h,0)]}case"right":case"rightBottom":case"rightTop":{const o=t.x+t.w*this.ratio;return[new s.Vec2d(t.x,t.y,0),new s.Vec2d(o,t.y,0),new s.Vec2d(o,t.y+t.h,0),new s.Vec2d(t.x,t.y+t.h,0)]}}}computDrawPoints(e,t){const o=s.getRectFromPoints(this.tmpPoints),i=this.transformControlPoints(t),n=Math.floor(o.w*.1),a=Math.floor(o.h*.1),c=[],l=s.Vec2d.Add(i[0],new s.Vec2d(0,a,0)),h=s.Vec2d.Add(i[0],new s.Vec2d(n,0,0)),p=V.getBezierPoints(10,l,i[0],h),d=s.Vec2d.Sub(i[1],new s.Vec2d(n,0,0)),u=s.Vec2d.Add(i[1],new s.Vec2d(0,a,0)),f=V.getBezierPoints(10,d,i[1],u),y=s.Vec2d.Sub(i[2],new s.Vec2d(0,a,0)),m=s.Vec2d.Sub(i[2],new s.Vec2d(n,0,0)),w=V.getBezierPoints(10,y,i[2],m),P=s.Vec2d.Add(i[3],new s.Vec2d(n,0,0)),T=s.Vec2d.Sub(i[3],new s.Vec2d(0,a,0)),S=V.getBezierPoints(10,P,i[3],T),E=n*(1-this.ratio)*10,I=a*(1-this.ratio)*10;switch(t){case"bottom":{const v=s.Vec2d.Sub(i[2],new s.Vec2d(n*5-E/2,0,0)),L=s.Vec2d.Sub(i[2],new s.Vec2d(n*5,-I,0)),C=s.Vec2d.Sub(i[2],new s.Vec2d(n*5+E/2,0,0));c.push(L,C,...S,...p,...f,...w,v);break}case"bottomRight":{const v=s.Vec2d.Sub(i[2],new s.Vec2d(n*1.1,0,0)),L=s.Vec2d.Sub(i[2],new s.Vec2d(n*1.1+E/2,-I,0)),C=s.Vec2d.Sub(i[2],new s.Vec2d(n*1.1+E,0,0));c.push(L,C,...S,...p,...f,...w,v);break}case"bottomLeft":{const v=s.Vec2d.Add(i[3],new s.Vec2d(n*1.1+E,0,0)),L=s.Vec2d.Add(i[3],new s.Vec2d(n*1.1+E/2,I,0)),C=s.Vec2d.Add(i[3],new s.Vec2d(n*1.1,0,0));c.push(L,C,...S,...p,...f,...w,v);break}case"top":{const v=s.Vec2d.Sub(i[1],new s.Vec2d(n*5-E/2,0,0)),L=s.Vec2d.Sub(i[1],new s.Vec2d(n*5,I,0)),C=s.Vec2d.Sub(i[1],new s.Vec2d(n*5+E/2,0,0));c.push(L,v,...f,...w,...S,...p,C);break}case"topRight":{const v=s.Vec2d.Sub(i[1],new s.Vec2d(n*1.1,0,0)),L=s.Vec2d.Sub(i[1],new s.Vec2d(n*1.1+E/2,I,0)),C=s.Vec2d.Sub(i[1],new s.Vec2d(n*1.1+E,0,0));c.push(L,v,...f,...w,...S,...p,C);break}case"topLeft":{const v=s.Vec2d.Add(i[0],new s.Vec2d(n*1.1+E,0,0)),L=s.Vec2d.Add(i[0],new s.Vec2d(n*1.1+E/2,-I,0)),C=s.Vec2d.Add(i[0],new s.Vec2d(n*1.1,0,0));c.push(L,v,...f,...w,...S,...p,C);break}case"left":{const v=s.Vec2d.Add(i[0],new s.Vec2d(0,a*5-I/2,0)),L=s.Vec2d.Add(i[0],new s.Vec2d(-E,a*5,0)),C=s.Vec2d.Add(i[0],new s.Vec2d(0,a*5+I/2,0));c.push(L,v,...p,...f,...w,...S,C);break}case"leftTop":{const v=s.Vec2d.Add(i[0],new s.Vec2d(0,a*1.1,0)),L=s.Vec2d.Add(i[0],new s.Vec2d(-E,a*1.1+I/2,0)),C=s.Vec2d.Add(i[0],new s.Vec2d(0,a*1.1+I,0));c.push(L,v,...p,...f,...w,...S,C);break}case"leftBottom":{const v=s.Vec2d.Sub(i[3],new s.Vec2d(0,a*1.1+I,0)),L=s.Vec2d.Sub(i[3],new s.Vec2d(E,a*1.1+I/2,0)),C=s.Vec2d.Sub(i[3],new s.Vec2d(0,a*1.1,0));c.push(L,v,...p,...f,...w,...S,C);break}case"right":{const v=s.Vec2d.Add(i[1],new s.Vec2d(0,a*5-I/2,0)),L=s.Vec2d.Add(i[1],new s.Vec2d(E,a*5,0)),C=s.Vec2d.Add(i[1],new s.Vec2d(0,a*5+I/2,0));c.push(L,C,...w,...S,...p,...f,v);break}case"rightTop":{const v=s.Vec2d.Add(i[1],new s.Vec2d(0,a*1.1,0)),L=s.Vec2d.Add(i[1],new s.Vec2d(E,a*1.1+I/2,0)),C=s.Vec2d.Add(i[1],new s.Vec2d(0,a*1.1+I,0));c.push(L,C,...w,...S,...p,...f,v);break}case"rightBottom":{const v=s.Vec2d.Sub(i[2],new s.Vec2d(0,a*1.1+I,0)),L=s.Vec2d.Sub(i[2],new s.Vec2d(-E,a*1.1+I/2,0)),C=s.Vec2d.Sub(i[2],new s.Vec2d(0,a*1.1,0));c.push(L,C,...w,...S,...p,...f,v);break}}const R=s.getRectFromPoints(this.tmpPoints,e),M=[Math.floor(R.x+R.w/2),Math.floor(R.y+R.h/2)];return{rect:R,pos:M,points:c}}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n)||s.Point2d.Sub(i,o).XY.includes(0))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var l,h;const{op:t,isFullWork:o,workState:i=s.EvevtWorkState.Done}=e,n=(l=this.workId)==null?void 0:l.toString();if(!n)return;this.tmpPoints.length=0;for(let p=0;p<t.length;p+=3)this.tmpPoints.push(new s.Point2d(t[p],t[p+1],t[p+2]));const a=o?this.fullLayer:this.drawLayer||this.fullLayer,c=this.draw({workId:n,layer:a,isDrawing:!1,isDrawEraserlines:i===s.EvevtWorkState.Done});return this.oldRect=c,(h=this.vNodes)==null||h.setInfo(n,{rect:c,op:t,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:c&&W.getCenterPos(c,a)}),c}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var f;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,fillColor:a,toolsType:c,placement:l,strokeType:h}=o,p=i.get(t.name),d=p==null?void 0:p.opt;let u=t;return t.tagName==="GROUP"&&(u=t.children[0]),n&&(u.setAttribute("strokeColor",n),d!=null&&d.strokeColor&&(d.strokeColor=n)),a&&(a==="transparent"?u.setAttribute("fillColor","rgba(0,0,0,0)"):u.setAttribute("fillColor",a),d!=null&&d.fillColor&&(d.fillColor=a)),c===s.EToolsKey.SpeechBalloon&&l&&(d.placement=l),h&&(p!=null&&p.opt)&&((f=p.opt)!=null&&f.strokeType)&&(p.opt.strokeType=h),p&&i.setInfo(t.name,{...p,opt:d}),W.updateNodeOpt(e)}}var Lt=s._baseGetTag,bt=s.isObjectLike_1,Dt="[object Boolean]";function Nt(k){return k===!0||k===!1||bt(k)&&Lt(k)==Dt}var xt=Nt;const ee=s.getDefaultExportFromCjs(xt);class $e extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.both);g(this,"toolsType",s.EToolsKey.Straight);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");g(this,"straightTipWidth");g(this,"syncTimestamp");this.workOptions=e.toolsOpt,this.straightTipWidth=this.workOptions.thickness/2,this.syncTimestamp=0,this.syncUnitTime=50}consume(e){const{data:t,isFullWork:o,isSubWorker:i,isMainThread:n,smoothSync:a,isSimpleWorker:c}=e,l=this.workId,{op:h,syncUnitTime:p}=t,d=h==null?void 0:h.length;if(!d||d<2)return{type:s.EPostMessageType.None};p&&(this.syncUnitTime=p);let u;if(this.tmpPoints.length===0?(this.tmpPoints=[new s.Point2d(h[0],h[1])],u=!1):u=this.updateTempPoints(h),!u)return{type:s.EPostMessageType.None};let f;if(i||n||c){const m=o?this.fullLayer:this.drawLayer||this.fullLayer;f=this.draw({workId:l,layer:m})}if(c){const m=s.computRect(f,this.oldRect);this.oldRect=f;const w={rect:m,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult};if(a){const P=Date.now();P-this.syncTimestamp>this.syncUnitTime&&(this.syncTimestamp=P,w.op=this.tmpPoints.map(T=>[...T.XY,0]).flat(1),w.index=0,w.isSync=!0)}return w}if(!i&&a){const m=Date.now();return m-this.syncTimestamp>this.syncUnitTime?(this.syncTimestamp=m,{type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,op:this.tmpPoints.map(w=>[...w.XY,0]).flat(1),isSync:!0,index:0,...this.baseConsumeResult}):{type:s.EPostMessageType.None}}const y=s.computRect(f,this.oldRect);return this.oldRect=f,{rect:y,type:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local,...this.baseConsumeResult}}consumeAll(){var a;const e=this.workId;if(this.tmpPoints.length<2)return{type:s.EPostMessageType.RemoveNode,removeIds:[e]};const t=this.fullLayer,o=this.draw({workId:e,layer:t});this.oldRect=o;const i=this.tmpPoints.map(c=>[...c.XY,0]).flat(1),n=s.transformToSerializableData(i);return(a=this.vNodes)==null||a.setInfo(e,{rect:o,op:i,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:o&&W.getCenterPos(o,t)}),{rect:o,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,ops:n,isSync:!0,...this.baseConsumeResult,updateNodeOpt:{useAnimation:!0}}}draw(e){const{workId:t,layer:o,isDrawEraserlines:i=!0}=e,{strokeColor:n,thickness:a,zIndex:c,scale:l,rotate:h,translate:p,strokeType:d,eraserlines:u,lineCap:f,lineDash:y}=this.workOptions,m=o.worldPosition,w=o.worldScaling,{d:P,rect:T,isDot:S}=this.computDrawPoints(a,d),E=[T.x+T.w/2,T.y+T.h/2],I=new D.Group({pos:E,anchor:[.5,.5],size:[T.w,T.h],name:t,id:t,normalize:!0,zIndex:c}),R={pos:[0,0],normalize:!0,d:P,fillColor:n,strokeColor:n,lineDash:S?void 0:d===s.EStrokeType.Dotted?[y&&y[0]||1,(y&&y[1]||2)*a]:d===s.EStrokeType.LongDotted?[(y&&y[0]||1)*a,(y&&y[1]||2)*a]:void 0,lineCap:S?void 0:f,lineWidth:d===s.EStrokeType.Normal||S?0:a};l&&(R.scale=l),h&&(R.rotate=h),p&&(R.translate=p);const M=new D.Path(R);I.append(M),u&&(this.scaleType=s.EScaleType.proportional,this.drawEraserlines({group:I,eraserlines:u,pos:E,layer:o},i)),this.replace(o,t,I);let v={x:Math.floor(T.x*w[0]+m[0]-W.SafeBorderPadding),y:Math.floor(T.y*w[1]+m[1]-W.SafeBorderPadding),w:Math.floor(T.w*w[0]+2*W.SafeBorderPadding),h:Math.floor(T.h*w[1]+2*W.SafeBorderPadding)};if(h||l||p){const L=M.getBoundingClientRect();v={x:Math.floor(L.x-W.SafeBorderPadding),y:Math.floor(L.y-W.SafeBorderPadding),w:Math.floor(L.width+W.SafeBorderPadding*2),h:Math.floor(L.height+W.SafeBorderPadding*2)}}return this.isDelete&&I.setAttribute("opacity",0),v}computDrawPoints(e,t){return this.tmpPoints[1].distance(this.tmpPoints[0])>this.straightTipWidth?this.computFullPoints(e,t):this.computDotPoints(e)}computFullPoints(e,t){const o=s.Vec2d.Sub(this.tmpPoints[1],this.tmpPoints[0]).uni(),i=s.Vec2d.Per(o).mul(e/2),n=s.Point2d.Sub(this.tmpPoints[0],i),a=s.Point2d.Add(this.tmpPoints[0],i),c=s.Point2d.Sub(this.tmpPoints[1],i),l=s.Point2d.Add(this.tmpPoints[1],i),h=s.Point2d.GetSemicircleStroke(this.tmpPoints[1],c,-1,8),p=s.Point2d.GetSemicircleStroke(this.tmpPoints[0],a,-1,8),d=[n,c,...h,l,a,...p];let u;return t!==s.EStrokeType.Normal?u=Q(this.tmpPoints,!1):u=Q(d,!0),{d:u,rect:s.getRectFromPoints(d),isDot:!1,pos:this.tmpPoints[0].XY}}computDotPoints(e){const t=s.Point2d.GetDotStroke(this.tmpPoints[0],e/2,8);return{d:Q(t,!0),rect:s.getRectFromPoints(t),isDot:!0,pos:this.tmpPoints[0].XY}}updateTempPoints(e){const t=e.slice(-2),o=new s.Point2d(t[0],t[1]),i=this.tmpPoints[0],{thickness:n}=this.workOptions;if(i.isNear(o,n))return!1;if(this.tmpPoints.length===2){if(o.isNear(this.tmpPoints[1],1))return!1;this.tmpPoints[1]=o}else this.tmpPoints.push(o);return!0}consumeService(e){var l,h;const{op:t,isFullWork:o,workState:i=s.EvevtWorkState.Done}=e,n=(l=this.workId)==null?void 0:l.toString();if(!n)return;this.tmpPoints.length=0;for(let p=0;p<t.length;p+=3)this.tmpPoints.push(new s.Point2d(t[p],t[p+1],t[p+2]));const a=o?this.fullLayer:this.drawLayer||this.fullLayer,c=this.draw({workId:n,layer:a,isDrawEraserlines:i===s.EvevtWorkState.Done});return this.oldRect=c,(h=this.vNodes)==null||h.setInfo(n,{rect:c,op:t,opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:c&&W.getCenterPos(c,a)}),c}clearTmpPoints(){this.tmpPoints.length=0}static updateNodeOpt(e){var l,h;const{node:t,opt:o,vNodes:i}=e,{strokeColor:n,strokeType:a}=o,c=i.get(t.name);return n&&(t.tagName==="GROUP"?t.children.forEach(p=>{p.setAttribute("strokeColor",n),p.getAttribute("fillColor")&&p.setAttribute("fillColor",n)}):(t.setAttribute("strokeColor",n),t.setAttribute("fillColor",n)),(l=c==null?void 0:c.opt)!=null&&l.strokeColor&&(c.opt.strokeColor=n)),a&&(c!=null&&c.opt)&&((h=c.opt)!=null&&h.strokeType)&&(c.opt.strokeType=a),c&&i.setInfo(t.name,c),W.updateNodeOpt(e)}}const Z=class Z extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.proportional);g(this,"toolsType",s.EToolsKey.Text);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");this.workOptions=e.toolsOpt}consume(){return{type:s.EPostMessageType.None}}consumeAll(){return{type:s.EPostMessageType.None}}consumeService(){}async draw(e){const{workId:t,layer:o,isDrawLabel:i,boxRect:n}=e,{boxSize:a,boxPoint:c,zIndex:l}=this.workOptions,h=o.worldPosition,p=o.worldScaling;if(!c||!a)return;const d={name:t,id:t,pos:[c[0]-s.TextBoxPadding,c[1]-s.TextBoxPadding],anchor:[0,0],size:a,zIndex:l},u=new D.Group(d),f={x:c[0]-s.TextBoxPadding,y:c[1]-s.TextBoxPadding,w:a[0],h:a[1]},y={x:Math.floor(f.x*p[0]+h[0]),y:Math.floor(f.y*p[1]+h[1]),w:Math.floor(f.w*p[0])+2,h:Math.floor(f.h*p[1])+2};this.replace(o,t,u);let m;if(n&&(m=s.getRectMatrixrRelation(y,n)),(i||n&&m!==s.EMatrixrRelationType.outside)&&o&&this.workOptions.text){const w=await Z.createLabels(this.workOptions,o,y),{labels:P,maxWidth:T}=w;u.append(...P),y.w=Math.ceil(Math.max(T*o.worldScaling[0],y.w))}return y}async consumeServiceAsync(e){var h,p,d,u;const t=(h=this.workId)==null?void 0:h.toString();if(!t)return;const{isFullWork:o,replaceId:i,isDrawLabel:n,boxRect:a}=e;this.oldRect=i&&((d=(p=this.vNodes)==null?void 0:p.get(i))==null?void 0:d.rect)||void 0;const c=o?this.fullLayer:this.drawLayer||this.fullLayer,l=await this.draw({workId:t,layer:c,isDrawLabel:typeof n>"u"&&this.workOptions.workState===s.EvevtWorkState.Done||n,boxRect:a});return(u=this.vNodes)==null||u.setInfo(t,{rect:l,op:[],opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:l&&W.getCenterPos(l,c)}),l}updataOptService(){}async updataOptServiceAsync(e,t,o){var y,m;if(!this.workId)return;const i=this.workId.toString(),{fontColor:n,fontBgColor:a,bold:c,italic:l,lineThrough:h,underline:p,zIndex:d}=e,u=(y=this.vNodes)==null?void 0:y.get(i);if(!u)return;n&&(u.opt.fontColor=n),a&&(u.opt.fontBgColor=a),c&&(u.opt.bold=c),l&&(u.opt.italic=l),ee(h)&&(u.opt.lineThrough=h),ee(p)&&(u.opt.underline=p),A(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===s.EvevtWorkState.Done||t,boxRect:o});return(m=this.vNodes)==null||m.setInfo(i,{rect:f,op:[],opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:f&&W.getCenterPos(f,this.fullLayer)}),f}clearTmpPoints(){this.tmpPoints.length=0}static getSafetySnippetRatio(e){const t=(e==null?void 0:e.parent).displayRatio||1,o=Math.ceil(e.worldScaling[0]*10)/10;let i=o;return o<=2?i=o*t*2:o>2&&o<=3?i=o*t*1.6:o>3&&o<=4?i=o*t*1.2:o>4&&(i=o*t),Math.floor(i*1e3)/1e3}static getSafetySnippetFontLength(e){return Math.floor(Z.textImageSnippetSize*3/4/e)||1}static async createLabels(e,t,o){var C;const i=[],{x:n,y:a}=o,{width:c,height:l}=(C=t.parent)==null?void 0:C.parent,h=s.transformToNormalData(e.text),p=h.length,{fontSize:d,lineHeight:u,bold:f,textAlign:y,italic:m,fontFamily:w,verticalAlign:P,fontColor:T,fontBgColor:S,underline:E,lineThrough:I}=e,R=Z.getSafetySnippetRatio(t)||1,M=Math.floor(d*R),v=Z.getSafetySnippetFontLength(M);let L=0;for(let b=0;b<p;b++){const N=h[b],x=u||M*1.5;if(N){const O=s.strlen(N),F=[0,0],z=[0,d*1.2];P==="middle"&&(F[1]=Math.floor(b*d*1.2+6+d*(1.1-1)));const X=[0,Math.floor(-d*.15)];F[0]=6;const J=Math.sin(Math.PI/180*20);let q=0;const ce=[];let re=0;for(;re<O;){y==="left"&&(X[0]=q),re===0&&m==="italic"&&(X[0]=X[0]-J/2*d);const we=N.slice(re,re+v),le={anchor:[0,0],pos:X,text:we,fontFamily:w,fontSize:M,lineHeight:x,strokeColor:T,fontWeight:f,fillColor:T,textAlign:y,fontStyle:m,scale:[1/R,1/R]},j=new D.Label(le),Y=await j.textImageReady;let he=!0;if(Y){const Ee=Y.rect&&Y.rect[2],Ie=Y.rect&&Y.rect[3];if(Ee&&Ie){const We=Ee/R,Qe=Ie/R;q=We+q,m==="italic"&&(f==="bold"?q=q-J*d*1.2:q=q-J*d),((X[0]+F[0]+We)*t.worldScaling[0]+n<=0||(X[0]+F[0])*t.worldScaling[0]+n>=c||(X[1]+F[1]+Qe)*t.worldScaling[1]+a<=0||(X[1]+F[1])*t.worldScaling[1]+a>=l)&&(j.disconnect(),he=!1),he&&ce.push(j)}}re+=v}z[0]=q,m==="italic"&&(z[0]=z[0]+J*d),L=Math.max(L,z[0]);let ve=!0;if(((F[0]+z[0])*t.worldScaling[0]+n<=0||F[0]*t.worldScaling[0]+n>=c||(F[1]+z[1])*t.worldScaling[0]+a<=0||F[1]*t.worldScaling[1]+a>=l)&&(ve=!1),ve){if(E){const j=Math.floor(d/10),Y={normalize:!1,pos:[0,d*1.1+j/2],lineWidth:j,points:[0,0,Math.ceil(z[0]),0],strokeColor:T,className:"underline"},he=new D.Polyline(Y);ce.push(he)}if(I){const j={normalize:!1,pos:[0,d*1.2/2],lineWidth:Math.floor(d/10),points:[0,0,Math.ceil(z[0]),0],strokeColor:T,className:"lineThrough"},Y=new D.Polyline(j);ce.push(Y)}const we={pos:F,anchor:[0,0],size:z,bgcolor:S},le=new D.Group(we);le.append(...ce),i.push(le)}}}return{labels:i,maxWidth:L}}static updateNodeOpt(e){const{node:t,opt:o,vNodes:i,targetNode:n}=e,{fontBgColor:a,fontColor:c,translate:l,originPoint:h,scenePoint:p,scale:d,bold:u,italic:f,lineThrough:y,underline:m,fontSize:w,textInfos:P,zIndex:T}=o,S=n&&s.cloneDeep(n)||i.get(t.name);if(!S||!t.parent)return;const I=S.opt;if(A(T)&&(t.setAttribute("zIndex",T),S.opt.zIndex=T),c&&I.fontColor&&(I.fontColor=c,t.children.forEach(R=>{R.tagName==="GROUP"&&R.children.forEach(M=>{M.tagName==="LABEL"?(M.setAttribute("fillColor",c),M.setAttribute("strokeColor",c)):M.tagName==="POLYLINE"&&M.setAttribute("strokeColor",c)})})),a&&I.fontBgColor&&(I.fontBgColor=a,t.children.forEach(R=>{R.tagName==="GROUP"&&R.setAttribute("bgcolor",a)})),u&&(I.bold=u),f&&(I.italic=f),ee(y)&&(I.lineThrough=y),ee(m)&&(I.underline=m),w&&(I.fontSize=w),h&&p&&d&&n&&I.boxPoint){const R=P==null?void 0:P.get(t.name);if(R){const{fontSize:C,boxSize:b}=R;I.boxSize=b||I.boxSize,I.fontSize=C||I.fontSize}const M=[I.boxPoint[0],I.boxPoint[1]];s.scalePoints(I.boxPoint,p,d,l);const v=[S.op[0],S.op[1]],L=[v[0]-M[0],v[1]-M[1]];if(S.centerPos=[S.centerPos[0]+L[0],S.centerPos[1]+L[1]],S.opt.eraserlines)for(const C of S.opt.eraserlines){const{op:b,thickness:N}=C;C.thickness=Math.round(N*Math.max(d[0],d[1]));for(let x=0;x<b.length;x++)s.scaleLitePoints(b[x],p,d,l)}}else if(l&&I.boxPoint&&(I.boxPoint=[Math.round(I.boxPoint[0]+l[0]),Math.round(I.boxPoint[1]+l[1])],S.centerPos=[S.centerPos[0]+l[0],S.centerPos[1]+l[1]],S.opt.eraserlines))for(const R of S.opt.eraserlines){const{op:M}=R;for(let v=0;v<M.length;v++){const L=M[v].map((C,b)=>b%2?C+l[1]:C+l[0]);R.op[v]=L}}return S&&i.setInfo(t.name,S),S==null?void 0:S.rect}static getRectFromLayer(e,t){const o=e.getElementsByName(t)[0];if(o){const i=o.getBoundingClientRect();let n={x:Math.floor(i.x),y:Math.floor(i.y),w:Math.floor(i.width+2),h:Math.floor(i.height+2)};return o.children.forEach(a=>{if(a.tagName==="GROUP"){const c=o.getBoundingClientRect();n=s.computRect(n,{x:Math.floor(c.x),y:Math.floor(c.y),w:Math.floor(c.width+2),h:Math.floor(c.height+2)})}}),n}}};g(Z,"textImageSnippetSize",1024*4),g(Z,"SafeBorderPadding",30);let fe=Z;const se=class se extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.none);g(this,"toolsType",s.EToolsKey.PencilEraser);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"worldPosition");g(this,"worldScaling");g(this,"eraserRect");g(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:s.EPostMessageType.None};const{thickness:o}=this.workOptions,{width:i,height:n}=se.eraserSizes[o],a=Math.round(Math.max(i,n)/this.worldScaling[0]),{rect:c,willNewNodes:l,willDeleteNodes:h}=this.getChangeNodes(a,t);return{...this.baseConsumeResult,type:s.EPostMessageType.DrawWork,rect:c,willNewNodes:l,willDeleteNodes:h}}consumeService(){}updateTempPoints(e,t){let o=this.tmpPoints.length-1;const i=Date.now();for(let n=0;n<e.length;n+=2){const a=this.tmpPoints.length,c=new s.Point2d(e[n],e[n+1]);if(a===0){this.tmpPoints.push(c);continue}const l=a-1,h=this.tmpPoints[l],p=s.Vec2d.Sub(c,h).uni();c.isNear(h,t/2)||(s.Vec2d.Equals(p,h.v,.02)&&(this.tmpPoints.pop(),o--),c.setv(p),c.setT(i),this.tmpPoints.push(c))}return Math.max(o,0)}getChangeNodes(e,t){const o=new Map,i=new Set;let n;if(!this.vNodes)return{willDeleteNodes:i,willNewNodes:o,rect:n};const a=this.worldPosition;let c=s.getRectFromPoints(this.tmpPoints,e);c=s.getRectTranslated(c,a),c=s.getRectScaleWPoint(c,this.worldScaling,a);const{nodeRange:l}=this.vNodes.getRectIntersectRange(c);if(!l.size)return{willDeleteNodes:i,willNewNodes:o,rect:n};const h=this.computEraserPointLines(this.tmpPoints,90);for(const[p,d]of l.entries()){if(d.toolsType!==s.EToolsKey.Pencil)continue;let u=s.getRectTranslated(d.rect,[-a[0],-a[1]]);u={x:u.x/this.worldScaling[0],y:u.y/this.worldScaling[1],w:u.w/this.worldScaling[0],h:u.h/this.worldScaling[1]};const f=[u.x,u.y,u.x+u.w,u.y+u.h],y=[],m=[];for(const w of h)Se.polyline(w,f,y);if(y.length&&d.opt)for(const w of y){const P=[];for(let T=0;T<w.length;T++)T!==0&&H(w[T],w[T-1])||P.push(...w[T]);m.push(P)}if(m.length&&t){const w=t.createWorkShapeNode({workId:p,toolsType:s.EToolsKey.Pencil,toolsOpt:d.opt});w.setWorkId(p);const P=w==null?void 0:w.computPencilPoints({op:d.op,eraserPolylines:m,eraserThickness:e});if(n=s.computRect(n,d.rect),i.add(p),t.removeNode(p),P.length)for(let T=0;T<P.length;T++){const S=`${p}_${Date.now()}_${T}`,E=S,R=t.setFullWork({workId:E,opt:d.opt,toolsType:s.EToolsKey.Pencil}).consumeService({op:P[T],isFullWork:!0,replaceId:E});t.clearWorkShapeNodeCache(E),n=s.computRect(n,R),o.set(S,{...d,toolsType:s.EToolsKey.Pencil,rect:R??d.rect,name:S,op:P[T]})}}}return{willNewNodes:o,willDeleteNodes:i,rect:n}}consume(e){const{data:t}=e,{op:o}=t;if(!o||o.length===0)return{type:s.EPostMessageType.None,...this.baseConsumeResult};const{thickness:i}=this.workOptions,{width:n,height:a}=se.eraserSizes[i],c=Math.max(n,a)/this.worldScaling[0];return this.updateTempPoints(o,c),{type:s.EPostMessageType.None,...this.baseConsumeResult}}computEraserPointLines(e,t=60){const o=[];let i=0;const n=360-t;for(let a=1;a<e.length;a++){const c=e[a-1],l=e[a];if(a===1&&i===0&&(o[i]=[c.XY]),o[i].push(l.XY),a<e.length-1){const h=l.getAngleByPoints(c,e[a+1]);(h<t||h>n)&&(i++,o[i]=[l.XY])}}return e.length===1&&o.length===0&&o.push([e[0].XY,e[0].XY]),o}consumeAll(e){const{workerEngine:t}=e;if(this.replace(this.fullLayer,this.workId),!this.tmpPoints.length)return{type:s.EPostMessageType.None,...this.baseConsumeResult};const{thickness:o}=this.workOptions,{width:i,height:n}=se.eraserSizes[o],a=Math.max(i,n)/this.worldScaling[0],{willDeleteNodes:c,willNewNodes:l,rect:h}=this.getChangeNodes(a,t);if(c.size===0&&l.size===0)return{type:s.EPostMessageType.FullWork,...this.baseConsumeResult,rect:h};const p=[];this.tmpPoints.map(u=>{p.push(u.x,u.y,a/2)});const d=s.transformToSerializableData(p);return{...this.baseConsumeResult,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,rect:h,ops:d,willNewNodes:l,willDeleteNodes:c}}clearTmpPoints(){this.tmpPoints.length=0}};g(se,"eraserSizes",Pe);let ye=se;const oe=class oe extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.none);g(this,"toolsType",s.EToolsKey.BitMapEraser);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"worldPosition");g(this,"worldScaling");g(this,"eraserRect");g(this,"eraserPolyline");g(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:s.EPostMessageType.None};const{thickness:o}=this.workOptions,{width:i,height:n}=oe.eraserSizes[o],a=Math.round(Math.max(i,n)/this.worldScaling[0]),{willDeleteNodes:c,rect:l}=this.getChangeNodes(a,t);return{...this.baseConsumeResult,type:s.EPostMessageType.DrawWork,rect:l,willDeleteNodes:c}}consumeService(){}updateTempPoints(e,t){let o=this.tmpPoints.length-1;const i=Date.now();for(let n=0;n<e.length;n+=2){const a=this.tmpPoints.length,c=new s.Point2d(e[n],e[n+1]);if(a===0){this.tmpPoints.push(c);continue}const l=a-1,h=this.tmpPoints[l],p=s.Vec2d.Sub(c,h).uni();c.isNear(h,t/2)||(s.Vec2d.Equals(p,h.v,.02)&&(this.tmpPoints.pop(),o--),c.setv(p),c.setT(i),this.tmpPoints.push(c))}return Math.max(o,0)}getChangeNodes(e,t){const o=new Map,i=new Set;let n;if(!this.vNodes)return{willDeleteNodes:i,willUpdateNodes:o,rect:n};const a=this.worldPosition;let c=s.getRectFromPoints(this.tmpPoints,e);c=s.getRectTranslated(c,a),c=s.getRectScaleWPoint(c,this.worldScaling,a);const{nodeRange:l}=this.vNodes.getRectIntersectRange(c);if(!l.size)return{willDeleteNodes:i,willUpdateNodes:o,rect:n};const h=this.computEraserPointLines(this.tmpPoints,120);for(const[p,d]of l.entries()){if(d.toolsType===s.EToolsKey.Text||d.toolsType===s.EToolsKey.Image)continue;let u=s.getRectTranslated(d.rect,[-a[0],-a[1]]);u={x:u.x/this.worldScaling[0],y:u.y/this.worldScaling[1],w:u.w/this.worldScaling[0],h:u.h/this.worldScaling[1]};const f=[u.x,u.y,u.x+u.w,u.y+u.h],y=[],m=[];for(const T of h)Se.polyline(T,f,y);if(y.length&&d.opt)for(const T of y){const S=[];for(let E=0;E<T.length&&!(isNaN(T[E][0])||isNaN(T[E][1]));E++){const I=T[E].map(R=>Math.round(R*100)/100);if(E!==0){const R=T[E-1].map(M=>Math.round(M*100)/100);if(H(I,R))continue}S.push(...I)}S.length!==0&&m.push(S)}if(!m.length)continue;const w=d.opt.eraserlines?d.opt.eraserlines:[],P=w.find(T=>T.workId===this.workId);if(P?P.op=m:w.push({workId:this.workId,op:m,thickness:e}),d.opt.eraserlines=w,t){const{toolsType:T}=d,S=t.createWorkShapeNode({workId:p,toolsType:T,toolsOpt:d.opt});S==null||S.setWorkId(p);const E=S==null?void 0:S.consumeService({op:d.op,isFullWork:!0,replaceId:p});if(n=s.computRect(n,E),S!=null&&S.isDelete){t.removeNode(p),i.add(p);continue}}o.set(p,d)}return{willUpdateNodes:o,willDeleteNodes:i,rect:n}}consume(e){const{data:t}=e,{op:o}=t;if(!o||o.length===0)return{type:s.EPostMessageType.None,...this.baseConsumeResult};const{thickness:i}=this.workOptions,{width:n,height:a}=oe.eraserSizes[i],c=Math.round(Math.max(n,a)/this.worldScaling[0]);return this.updateTempPoints(o,c),{type:s.EPostMessageType.None,...this.baseConsumeResult}}computEraserPointLines(e,t=60){const o=[];let i=0;const n=360-t;for(let a=1;a<e.length;a++){const c=e[a-1],l=e[a];if(a===1&&i===0&&(o[i]=[c.XY]),o[i].push(l.XY),a<e.length-1){const h=l.getAngleByPoints(c,e[a+1]);(h<t||h>n)&&(i++,o[i]=[l.XY])}}return e.length===1&&o.length===0&&o.push([e[0].XY,e[0].XY]),o}consumeAll(e){const{workerEngine:t}=e;if(this.replace(this.fullLayer,this.workId),!this.tmpPoints.length)return{type:s.EPostMessageType.None,...this.baseConsumeResult};const{thickness:o}=this.workOptions,{width:i,height:n}=oe.eraserSizes[o],a=Math.round(Math.max(i,n)/this.worldScaling[0]),{willDeleteNodes:c,willUpdateNodes:l,rect:h}=this.getChangeNodes(a,t);if(c.size===0&&l.size===0)return{type:s.EPostMessageType.FullWork,...this.baseConsumeResult,rect:h};const p=[];this.tmpPoints.map(u=>{p.push(u.x,u.y,a/2)});const d=s.transformToSerializableData(p);return{...this.baseConsumeResult,type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,rect:h,ops:d,willUpdateNodes:l,willDeleteNodes:c}}clearTmpPoints(){this.tmpPoints.length=0}};g(oe,"eraserSizes",Pe);let me=oe;class ne extends W{constructor(e){super(e);g(this,"canRotate",!1);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.BackgroundSVG);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");this.workOptions=e.toolsOpt,this.scaleType=ne.getScaleType(this.workOptions)}consume(){return{type:s.EPostMessageType.None}}consumeAll(){return{type:s.EPostMessageType.None}}draw(e){const{layer:t,workId:o,replaceId:i}=e,{centerX:n,centerY:a,width:c,height:l,rotate:h,zIndex:p}=this.workOptions,d=(p||0)-100,u=new D.Group({anchor:[.5,.5],pos:[n,a],name:o,size:[c,l],zIndex:d,rotate:h}),f=new D.Rect({normalize:!0,pos:[0,0],width:c,height:l});u.appendChild(f),this.replace(t,i||o,u);const y=u.getBoundingClientRect();if(y)return{x:Math.floor(y.x-W.SafeBorderPadding),y:Math.floor(y.y-W.SafeBorderPadding),w:Math.floor(y.width+W.SafeBorderPadding*2),h:Math.floor(y.height+W.SafeBorderPadding*2)}}consumeService(e){var n;const{replaceId:t}=e,o=this.workId,i=this.draw({workId:o,layer:this.fullLayer,replaceId:t});return(n=this.vNodes)==null||n.setInfo(o,{rect:i,op:[],opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:i&&W.getCenterPos(i,this.fullLayer)}),i}clearTmpPoints(){this.tmpPoints.length=0}static getScaleType(e){const{uniformScale:t,rotate:o}=e;return t!==!1||o&&Math.abs(o)%90>0?s.EScaleType.proportional:s.EScaleType.all}static updateNodeOpt(e){const{node:t,opt:o,vNodes:i,targetNode:n}=e,{translate:a,originPoint:c,scenePoint:l,scale:h,angle:p,isLocked:d,zIndexForBackgroundSVG:u}=o,f=n&&s.cloneDeep(n)||i.get(t.name);if(!f)return;const y=t.parent;if(y){if(A(u)){const m=(u||0)-100;t.setAttribute("zIndex",m),f.opt.zIndex=u}if(ee(d)&&(f.opt.locked=d),c&&l&&h&&a){const{centerX:m,centerY:w,width:P,height:T,uniformScale:S}=f.opt,E=S!==!1?[h[0],h[0]]:h,I=[m,w],R=[m,w];s.scalePoints(R,l,E,a);const M=[R[0]-I[0],R[1]-I[1]];f.centerPos=[f.centerPos[0]+M[0],f.centerPos[1]+M[1]],f.opt.width=Math.round(P*E[0]),f.opt.height=Math.round(T*E[1]),f.opt.centerX=R[0],f.opt.centerY=R[1]}else if(a)f.opt.centerX=f.opt.centerX+a[0],f.opt.centerY=f.opt.centerY+a[1],f.centerPos=[f.centerPos[0]+a[0],f.centerPos[1]+a[1]];else if(A(p))if(f.opt.rotate=p,f.scaleType=ne.getScaleType(f.opt),n){const m=s.getRectRotated(f.rect,p);f.rect=m}else{const m=W.getRectFromLayer(y,t.name);f.rect=m||f.rect}return f&&i.setInfo(t.name,f),f==null?void 0:f.rect}}}function He(k){switch(k){case s.EToolsKey.Arrow:return Ke;case s.EToolsKey.Pencil:return Fe;case s.EToolsKey.Straight:return $e;case s.EToolsKey.Ellipse:return Ve;case s.EToolsKey.Polygon:case s.EToolsKey.Triangle:return Xe;case s.EToolsKey.Star:case s.EToolsKey.Rhombus:return Ge;case s.EToolsKey.Rectangle:return _e;case s.EToolsKey.SpeechBalloon:return Ye;case s.EToolsKey.Text:return fe;case s.EToolsKey.LaserPen:return ze;case s.EToolsKey.Eraser:return de;case s.EToolsKey.PencilEraser:return ye;case s.EToolsKey.BitMapEraser:return me;case s.EToolsKey.Selector:return ue;case s.EToolsKey.Image:return ae;case s.EToolsKey.BackgroundSVG:return ne}}function ie(k,r){const{toolsType:e,...t}=k;switch(e){case s.EToolsKey.Arrow:return new Ke(t);case s.EToolsKey.Pencil:return new Fe(t);case s.EToolsKey.Straight:return new $e(t);case s.EToolsKey.Ellipse:return new Ve(t);case s.EToolsKey.Polygon:case s.EToolsKey.Triangle:return new Xe(t);case s.EToolsKey.Star:case s.EToolsKey.Rhombus:return new Ge(t);case s.EToolsKey.Rectangle:return new _e(t);case s.EToolsKey.SpeechBalloon:return new Ye(t);case s.EToolsKey.Text:return new fe(t);case s.EToolsKey.LaserPen:return new ze(t);case s.EToolsKey.Eraser:return new de(t,r);case s.EToolsKey.BitMapEraser:return new me(t);case s.EToolsKey.PencilEraser:return new ye(t);case s.EToolsKey.Selector:return t.vNodes?new ue({...t,vNodes:t.vNodes,drawLayer:t.fullLayer}):void 0;case s.EToolsKey.Image:return new ae(t);case s.EToolsKey.BackgroundSVG:return new ne({...t,toolsOpt:t.toolsOpt});default:return}}function qe(k){const r=[],e=["PATH","SPRITE","POLYLINE","RECT","ELLIPSE","LABEL","SPRITESVG"];for(const t of k){if(t.tagName==="GROUP"&&t.children.length){const o=qe(t.children);r.push(...o)}t.tagName&&e.includes(t.tagName)&&r.push(t)}return r}class je{constructor(r,e){g(this,"viewId");g(this,"scene");g(this,"fullLayer");g(this,"curNodeMap");g(this,"targetNodeMap",[]);g(this,"highLevelIds");g(this,"canClearUids");g(this,"localUid");this.viewId=r,this.scene=e,this.curNodeMap=new Me.ObserverMap}init(r){this.fullLayer=r}get(r){return this.curNodeMap.get(r)}setLocalUid(r){this.localUid=r}getLocalUid(){return this.localUid}setCanClearUids(r){this.canClearUids=r}getCanClearUids(){return this.canClearUids}getCanEraserNodes(r,e){const t=new Map;for(const[o,i]of r.entries())i.toolsType!==s.EToolsKey.BackgroundSVG&&(i.toolsType===s.EToolsKey.Image&&i.opt.type===s.EImageType.Image&&(i.opt.locked||e&&e.disableEraseImage)||i.toolsType===s.EToolsKey.Text&&(i.opt.workState===s.EvevtWorkState.Doing||i.opt.workState===s.EvevtWorkState.Start||e&&e.disableEraseText)||this.isCanClearWorkId(o)&&t.set(o,i));return t}getNodesByType(r){const e=new Map;return this.curNodeMap.forEach((t,o)=>{t.toolsType===r&&e.set(o,t)}),e}gethasEraserNodes(){const r=new Map;return this.curNodeMap.forEach((e,t)=>{var o;(o=e.opt.eraserlines)!=null&&o.length&&r.set(t,e)}),r}hasRenderNodes(){return!0}has(r){return this.curNodeMap.has(r)}setInfo(r,e){const t=this.curNodeMap.get(r)||{name:r,rect:e.rect};e.rect&&(t.rect=s.cloneDeep(e.rect)),e.op&&s.checkOp(e.op)&&(t.op=s.cloneDeep(e.op)),e.canRotate&&(t.canRotate=e.canRotate),e.scaleType&&(t.scaleType=e.scaleType),e.opt&&(t.opt=s.cloneDeep(e.opt)),e.toolsType&&(t.toolsType=e.toolsType),e.centerPos&&(t.centerPos=s.cloneDeep(e.centerPos)),ee(e.isSelected)&&(t.isSelected=e.isSelected),t.rect?this.curNodeMap.set(r,t):this.curNodeMap.delete(r)}selected(r){this.setInfo(r,{isSelected:!0})}unSelected(r){this.setInfo(r,{isSelected:!1})}delete(r){this.curNodeMap.delete(r)}clear(){this.curNodeMap.clear(),this.targetNodeMap.length=0}getRectIntersectRange(r,e=!0,t=!0){let o;const i=new Map;for(const[n,a]of this.curNodeMap.entries())if(s.isIntersect(r,a.rect)){if(e&&a.toolsType===s.EToolsKey.Image&&a.opt.locked||t&&a.toolsType===s.EToolsKey.Text&&(a.opt.workState===s.EvevtWorkState.Doing||a.opt.workState===s.EvevtWorkState.Start))continue;o=s.computRect(o,a.rect),i.set(n,a)}return{rectRange:o,nodeRange:i}}getNodeRectFormShape(r,e){const t=He(e.toolsType);return this.fullLayer&&(t==null?void 0:t.getRectFromLayer(this.fullLayer,r))}updateNodeRect(r){const e=this.curNodeMap.get(r);if(e){const t=this.getNodeRectFormShape(r,e);if(!t){this.curNodeMap.delete(r);return}e.rect=t,this.curNodeMap.set(r,e)}}updateHighLevelNodesRect(r){this.highLevelIds=r;for(const e of this.highLevelIds.keys())this.updateNodeRect(e)}updateLowLevelNodesRect(){var r;for(const e of this.curNodeMap.keys())(r=this.highLevelIds)!=null&&r.has(e)||this.updateNodeRect(e)}clearHighLevelIds(){this.highLevelIds=void 0}setTargetAssignKeys(r){const e=new Me.ObserverMap;for(const t of r){const o=this.curNodeMap.get(t);o&&e.set(t,s.cloneDeep(o))}return this.targetNodeMap.push(s.cloneDeep(e)),this.targetNodeMap.length-1}setTarget(){const r=this.curNodeMap.keys();return this.setTargetAssignKeys(Array.from(r))}getLastTarget(){return this.targetNodeMap[this.targetNodeMap.length-1]}deleteLastTarget(){this.targetNodeMap.length&&(this.targetNodeMap.length=this.targetNodeMap.length-1)}getTarget(r){return this.targetNodeMap[r]}deleteTarget(r){this.targetNodeMap.length=r}clearTarget(){this.targetNodeMap.length=0}isLocalWorkId(r){return r.split(s.Storage_Splitter).length===1}isCanClearWorkId(r){if(this.canClearUids===void 0||this.canClearUids===!0)return!0;if(s.isSet(this.canClearUids)){const e=r.split(s.Storage_Splitter);if(e.length===1)return this.canClearUids.has("localSelf")||this.localUid&&this.canClearUids.has(this.localUid);if(e.length===2)return this.canClearUids.has(e[0])}return!1}}class Ot{constructor(r,e,t){g(this,"viewId");g(this,"type");g(this,"scene");g(this,"fullLayer");g(this,"vNodes");g(this,"dpr");g(this,"contextType");g(this,"opt");g(this,"cameraOpt");g(this,"isSafari",!1);g(this,"combinePostMsg",new Set);g(this,"workerTaskId");g(this,"protectedTask");g(this,"delayPostDoneResolve");g(this,"cacheImages",new Map);g(this,"imageResolveMap",new Map);g(this,"taskUpdateCameraId");g(this,"debounceUpdateCameraId");var o,i;if(this.viewId=r,this.type=t,this.opt=e,this.dpr=e.dpr,this.contextType=this.getSupportContextType(t,(o=e==null?void 0:e.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(e),this.createRenderLayer(this.scene,e)}catch(n){if(this.contextType!=="2d")this.contextType="2d",(i=this.scene)==null||i.disconnect(),this.scene=this.createScene(e),this.createRenderLayer(this.scene,e);else throw n}this.vNodes=new je(r,this.scene)}createRenderLayer(r,e){const t=s.DefaultAppliancePluginExtrasOptions.bufferSize.full,o=s.DefaultAppliancePluginExtrasOptions.bufferSize.sub;this.fullLayer=this.createLayer("fullLayer",r,{...e.layerOpt,bufferSize:this.viewId===s.Main_View_Id?t:o*2})}async updateDpr(r){this.dpr=r,this.scene.displayRatio=r}getCachedImages(r){var e;return(e=this.cacheImages.get(r))==null?void 0:e.imageBitmap}getCachedImagesByWorkId(r){for(const[e,t]of this.cacheImages.entries())if(e===r&&t.imageBitmap)return t.imageBitmap}deleteCachedImagesByWorkId(r,e){for(const[t,o]of this.cacheImages.entries())e&&t===e||o.workId===r&&(o.imageBitmap.close(),this.cacheImages.delete(t))}clearCacheImages(){this.cacheImages.forEach(r=>r.imageBitmap.close()),this.cacheImages.clear()}clearImageResolveMap(){this.imageResolveMap.forEach(({timer:r})=>{r&&clearTimeout(r)}),this.imageResolveMap.clear()}setIsSafari(r){this.isSafari=r}async on(r){const{msgType:e,toolsType:t,opt:o,imageSrc:i,imageBitmap:n,workId:a,dpr:c}=r;switch(e){case s.EPostMessageType.UpdateDpr:{A(c)&&await this.updateDpr(c);break}case s.EPostMessageType.UpdateCamera:{await this.updateCamera(r);break}case s.EPostMessageType.Destroy:{this.destroy();break}case s.EPostMessageType.Clear:{await this.clearAll();break}case s.EPostMessageType.UpdateTools:{if(t&&o){const l={toolsType:t,toolsOpt:o,combineUnitTime:r.combineUnitTime,maxCombineEraserTime:r.maxCombineEraserTime};this.localWork.setToolsOpt(l)}break}case s.EPostMessageType.GetImageBitMap:{if(i&&n&&a){const l=a.toString();this.cacheImages.set(i,{imageBitmap:n,workId:l});const h=this.imageResolveMap.get(i);if(h){const{resolve:p,timer:d}=h;d&&clearTimeout(d),p&&p(i)}this.deleteCachedImagesByWorkId(l,i)}break}}}getIconSize(r,e,t){const o=r*t,i=e*t;return o<=50||i<=50?[50,50]:o<=100||i<=100?[100,100]:o<=200||i<=200?[200,200]:o<=400||i<=400?[400,400]:o<=800||i<=800?[800,800]:[1600,1600]}async loadImageBitMap(r){const{toolsType:e,opt:t,workId:o,isSubWorker:i}=r;if(e===s.EToolsKey.Image&&t&&o){const n=o.toString(),{src:a,type:c,width:l,height:h,strokeColor:p}=t;if(!a||!c||!l||!h)return;let d=a;if(c===s.EImageType.Iconify){const[m,w]=this.getIconSize(l,h,this.dpr);d=`${a}?width=${m}&height=${w}&color=${p}`}if(this.cacheImages.has(d)){const m=this.getCachedImages(d);if(m)return m}if(this.imageResolveMap.has(d)){const m=this.getCachedImagesByWorkId(n);if(m)return m}const y=await new Promise(m=>{const w=this.imageResolveMap.get(d)||{resolve:void 0,timer:void 0};w.timer&&clearTimeout(w.timer),w.resolve=m,w.timer=setTimeout(()=>{const P=this.imageResolveMap.get(d);P!=null&&P.resolve&&P.resolve(d)},5e3),this.imageResolveMap.set(d,w),this._post({sp:[{imageSrc:d,workId:n,viewId:this.viewId,isgl:!!this.fullLayer.parent.gl,isSubWorker:i,type:s.EPostMessageType.GetImageBitMap}]})});return this.imageResolveMap.delete(y),this.getCachedImages(d)}}createLocalWork(r){const{workId:e,opt:t,toolsType:o}=r;e&&t&&(!this.localWork.getToolsOpt()&&o&&this.setToolsOpt({toolsType:o,toolsOpt:t}),this.setWorkOpt({workId:e,toolsOpt:t}))}updateScene(r,e,t){r.attr({...e});const{width:o,height:i}=e;r.container.width=o,r.container.height=i,r.width=o,r.height=i,this.updateLayer({width:o,height:i},t)}updateLayer(r,e){const{width:t,height:o}=r,i=e||this.fullLayer;i&&(i.parent.setAttribute("width",t),i.parent.setAttribute("height",o),i.setAttribute("size",[t,o]),i.setAttribute("pos",[t*.5,o*.5]))}getSupportContextType(r,e){const t=new OffscreenCanvas(100,100);let o=["2d"];r===ke.Full&&this.viewId===s.Main_View_Id&&(o=["webgl2","webgl","2d"],e&&o.unshift(e));for(const i of o)try{if(t.getContext(i))return i}catch(n){throw n}}createScene(r){const{offscreenCanvasOpt:e}=r,{width:t,height:o}=e,i=new OffscreenCanvas(t,o);e.contextType&&delete e.contextType;const n=new D.Scene({container:i,displayRatio:this.dpr,depth:!1,desynchronized:!0,failIfMajorPerformanceCaveat:!0,...e,contextType:this.contextType,id:this.viewId});return n.setAttribute("id",this.viewId),n}createLayer(r,e,t,o){const i=`offscreen-${r}`,{width:n,height:a}=t;o&&(t.offscreen=!0,t.canvas=o);const c=e.layer(i,t),l=new D.Group({anchor:[.5,.5],pos:[n*.5,a*.5],size:[n,a],name:"viewport",id:r});return c.append(l),l}async clearAll(){var r;this.fullLayer&&(this.fullLayer.parent.children.forEach(e=>{e.name!=="viewport"&&e.remove()}),U(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(),(r=this.serviceWork)==null||r.destroy()}setToolsOpt(r){this.localWork.setToolsOpt(r)}setWorkOpt(r){const{workId:e,toolsOpt:t}=r;e&&t&&this.localWork.setWorkOptions(e.toString(),t)}destroy(){var r;this.vNodes.clear(),this.fullLayer.remove(),B(this.fullLayer,this.fullLayer.parent),this.clearCacheImages(),this.clearImageResolveMap(),this.scene.remove(),this.localWork.destroy(),(r=this.serviceWork)==null||r.destroy()}async post(r){this.combinePostMsg.add(r),await this.runBatchPostData()}async runBatchPostData(){this.workerTaskId||(this.workerTaskId=setTimeout(()=>{this.workerTaskId=void 0,this.combinePost()},16)),this.type===ke.Full&&!this.delayPostDoneResolve&&await new Promise(e=>{this.delayPostDoneResolve=e})&&(this.delayPostDoneResolve=void 0)}combinePostData(){var a,c;this.workerTaskId=void 0;const r=[],e=[];let t,o,i;const n=new Set;for(const l of this.combinePostMsg.values()){if((a=l.render)!=null&&a.length)for(const h of l.render){let p=!1;if(h.workId&&n.add(h.workId),h.isClearAll&&(h.rect=this.getSceneRect(),h.isClear=!0,delete h.isClearAll),h.drawCanvas){const d=this.getLayer(h.drawCanvas);if(!d||!(d.parent instanceof D.Layer))continue;if(d.parent.render(),h.isDrawAll){const u=this.getSceneRect();h.rect=u,delete h.isDrawAll}}for(const d of r)if(h.viewId===d.viewId){h.isClear&&d.clearCanvas&&d.isClear&&d.clearCanvas===h.clearCanvas&&(d.rect=s.computRect(d.rect,h.rect),p=!0),d.drawCanvas&&d.drawCanvas===h.drawCanvas&&(d.rect=s.computRect(d.rect,h.rect),p=!0);continue}p||(h.isClear&&!h.drawCanvas?r.unshift(h):r.push(h))}if((c=l.sp)!=null&&c.length)for(const h of l.sp){let p=!1;for(const d of e)if(H(h,d)){p=!0;break}p||e.push(h)}A(l.fullWorkerDrawCount)&&(t=l.fullWorkerDrawCount),A(l.subWorkerDrawCount)&&(o=l.subWorkerDrawCount),A(l.consumeCount)&&(i=l.consumeCount)}return this.combinePostMsg.clear(),{render:r,sp:e,fullWorkerDrawCount:t,subWorkerDrawCount:o,consumeCount:i,workIds:n,viewId:this.viewId}}getSceneRect(){const{width:r,height:e}=this.scene;return{x:0,y:0,w:Math.floor(r),h:Math.floor(e)}}}class Ze{constructor(r){g(this,"viewId");g(this,"vNodes");g(this,"thread");g(this,"fullLayer");g(this,"drawLayer");g(this,"_post");g(this,"tmpOpt");g(this,"workShapes",new Map);g(this,"drawCount",0);g(this,"consumeCount",0);g(this,"syncUnitTime",s.DefaultAppliancePluginExtrasOptions.syncOpt.interval);g(this,"combineUnitTime",s.DefaultAppliancePluginExtrasOptions.bezier.combineUnitTime);g(this,"maxCombineEraserTime",s.DefaultAppliancePluginExtrasOptions.pencilEraser.maxCombineTime);this.thread=r.thread,this.viewId=r.viewId,this.vNodes=r.vNodes,this.fullLayer=r.fullLayer,this.drawLayer=r.drawLayer,this._post=this.thread.post.bind(r.thread)}setmaxCombineEraserTime(r){this.maxCombineEraserTime=r}setCombineUnitTime(r){this.combineUnitTime=r}setSyncUnitTime(r){this.syncUnitTime=r}destroy(){this.workShapes.clear()}getWorkShapes(){return this.workShapes}getWorkShape(r){return this.workShapes.get(r)}createWorkShape(r,e){if(r&&this.tmpOpt){const t={toolsType:this.tmpOpt.toolsType,toolsOpt:e||this.tmpOpt.toolsOpt},o=this.createWorkShapeNode({...t,workId:r});o&&this.workShapes.set(r,o)}}setWorkOptions(r,e){const t=this.getWorkShape(r);t||this.createWorkShape(r,e),t==null||t.setWorkOptions(e)}createWorkShapeNode(r){var e;return ie({...r,vNodes:this.vNodes,fullLayer:this.fullLayer,drawLayer:this.drawLayer},(e=this.thread)==null?void 0:e.serviceWork)}setToolsOpt(r){var e,t,o;((e=this.tmpOpt)==null?void 0:e.toolsType)!==r.toolsType&&(t=this.tmpOpt)!=null&&t.toolsType&&this.clearAllWorkShapesCache(),this.tmpOpt=r,(o=r.toolsOpt)!=null&&o.syncUnitTime&&(this.syncUnitTime=r.toolsOpt.syncUnitTime),r.combineUnitTime&&(this.combineUnitTime=r.combineUnitTime),r.maxCombineEraserTime&&(this.maxCombineEraserTime=r.maxCombineEraserTime)}getToolsOpt(){return this.tmpOpt}clearWorkShapeNodeCache(r){var e;(e=this.getWorkShape(r))==null||e.clearTmpPoints(),this.workShapes.delete(r)}clearAllWorkShapesCache(){this.workShapes.forEach(r=>r.clearTmpPoints()),this.workShapes.clear()}setFullWork(r){const{workId:e,opt:t,toolsType:o}=r;if(e&&t&&o){const i=e.toString();let n;return e&&this.workShapes.has(i)?(n=this.workShapes.get(i),n==null||n.setWorkOptions(t)):n=this.createWorkShapeNode({toolsOpt:t,toolsType:o,workId:i}),n?(this.workShapes.set(i,n),n):void 0}}}class At extends Ze{constructor(e){super(e);g(this,"drawWorkActiveId")}runSelectWork(e){var o;const t=this.setFullWork(e);t&&((o=e.selectIds)!=null&&o.length)&&e.workId&&t.selectServiceNode(e.workId.toString(),{selectIds:e.selectIds},!1)}workShapesDone(){for(const e of this.workShapes.keys())this.clearWorkShapeNodeCache(e);U(this.fullLayer,this.fullLayer.parent)}async consumeDraw(e){const{workId:t,fullWorkerDrawCount:o,postCount:i}=e,n=t==null?void 0:t.toString(),a=n&&this.workShapes.get(n);if(!a)return;this.drawWorkActiveId&&this.drawWorkActiveId!==n&&(await this.consumeDrawAll({workId:this.drawWorkActiveId,viewId:this.viewId,msgType:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local}),this.drawWorkActiveId=void 0),!this.drawWorkActiveId&&n!==s.Storage_Selector_key&&(this.drawWorkActiveId=n);const c=a.toolsType;A(i)&&(this.consumeCount=i);const l=a.consume({data:e,drawCount:this.drawCount,isFullWork:!0,isSubWorker:!0,removeDrawCount:o});switch(c){case s.EToolsKey.Selector:l&&(this.drawCount++,await this.drawSelector(l));break;case s.EToolsKey.Ellipse:case s.EToolsKey.Arrow:case s.EToolsKey.Straight:case s.EToolsKey.Rectangle:case s.EToolsKey.Star:case s.EToolsKey.Polygon:case s.EToolsKey.SpeechBalloon:{l&&(this.drawCount++,await this.drawShape(l));break}case s.EToolsKey.Pencil:{l&&(this.drawCount++,await this.drawPencil(l,t==null?void 0:t.toString()));break}case s.EToolsKey.BitMapEraser:case s.EToolsKey.PencilEraser:{l&&(this.drawCount++,await this._post({subWorkerDrawCount:this.drawCount,consumeCount:this.consumeCount}));break}}}async consumeDrawAll(e){const{workId:t}=e;if(t){const o=t.toString();this.drawWorkActiveId===o&&(this.drawWorkActiveId=void 0);const i=this.workShapes.get(o);if(!i)return;switch(i.toolsType){case s.EToolsKey.Selector:this.drawCount=0,U(this.fullLayer,this.fullLayer.parent),this.clearWorkShapeNodeCache(o),this._post({render:[{isClearAll:!0,clearCanvas:s.ECanvasShowType.Float,viewId:this.viewId}]});break;case s.EToolsKey.Arrow:case s.EToolsKey.Straight:case s.EToolsKey.Ellipse:case s.EToolsKey.Pencil:case s.EToolsKey.Rectangle:case s.EToolsKey.Star:case s.EToolsKey.Polygon:case s.EToolsKey.SpeechBalloon:case s.EToolsKey.BitMapEraser:case s.EToolsKey.PencilEraser:this.drawCount=0,U(this.fullLayer,this.fullLayer.parent),this.clearWorkShapeNodeCache(o);break}}}async removeWork(e){const{workId:t}=e,o=t==null?void 0:t.toString();if(o){const i=this.removeNode(o);if(i){const n=[];n.push({rect:s.getSafetyRect(i),clearCanvas:s.ECanvasShowType.Float,isClear:!0,viewId:this.viewId},{rect:s.getSafetyRect(i),drawCanvas:s.ECanvasShowType.Float,viewId:this.viewId}),await this._post({render:n})}}}removeNode(e){const t=this.workShapes.has(e);let o;return t&&(this.fullLayer.getElementsByName(e).forEach(i=>{const n=i.getBoundingClientRect();o=s.computRect(o,{x:n.x-W.SafeBorderPadding,y:n.y-W.SafeBorderPadding,w:n.width+W.SafeBorderPadding*2,h:n.height+W.SafeBorderPadding*2}),i.remove(),B(i,this.fullLayer.parent)}),o&&this.clearWorkShapeNodeCache(e)),o}async drawPencil(e,t){await this._post({subWorkerDrawCount:this.drawCount,consumeCount:this.consumeCount,render:[{rect:e==null?void 0:e.rect,workId:t,drawCanvas:s.ECanvasShowType.Float,viewId:this.viewId}],sp:(e==null?void 0:e.op)&&[e]})}async drawShape(e){await this._post({subWorkerDrawCount:this.drawCount,consumeCount:this.consumeCount,render:[{rect:(e==null?void 0:e.rect)&&s.getSafetyRect(e.rect),isClear:!0,clearCanvas:s.ECanvasShowType.Float,viewId:this.viewId},{rect:(e==null?void 0:e.rect)&&s.getSafetyRect(e.rect),drawCanvas:s.ECanvasShowType.Float,viewId:this.viewId}]})}async drawSelector(e){await this._post({subWorkerDrawCount:this.drawCount,consumeCount:this.consumeCount,render:[{rect:(e==null?void 0:e.rect)&&s.getSafetyRect(e.rect),isClear:!0,clearCanvas:s.ECanvasShowType.Float,viewId:this.viewId},{rect:(e==null?void 0:e.rect)&&s.getSafetyRect(e.rect),drawCanvas:s.ECanvasShowType.Float,viewId:this.viewId}]})}}class Bt{constructor(r){g(this,"viewId");g(this,"vNodes");g(this,"topLayer");g(this,"thread");g(this,"post");g(this,"serviceWorkShapes",new Map);g(this,"localWorkShapes",new Map);g(this,"tmpOpt");g(this,"syncUnitTime",s.DefaultAppliancePluginExtrasOptions.syncOpt.interval);g(this,"animationId");this.viewId=r.viewId,this.vNodes=r.vNodes,this.topLayer=r.topLayer,this.thread=r.thread,this.post=r.thread.post.bind(r.thread)}canUseTopLayer(r){return r===s.EToolsKey.LaserPen}getWorkShape(r){return this.localWorkShapes.get(r)}createWorkShape(r,e){if(r&&this.tmpOpt){const t={toolsType:this.tmpOpt.toolsType,toolsOpt:e||this.tmpOpt.toolsOpt},o=this.createWorkShapeNode({...t,workId:r});return o&&this.localWorkShapes.set(r,{node:o,toolsType:o.toolsType,workState:s.EvevtWorkState.Start}),o}}setWorkOptions(r,e){var o;const t=(o=this.localWorkShapes.get(r))==null?void 0:o.node;t||this.createWorkShape(r,e),t==null||t.setWorkOptions(e)}createWorkShapeNode(r){const{toolsType:e}=r;if(e===s.EToolsKey.LaserPen)return ie({...r,vNodes:this.vNodes,fullLayer:this.topLayer,drawLayer:this.topLayer})}clearAllWorkShapesCache(){this.localWorkShapes.forEach(r=>{var e;return(e=r.node)==null?void 0:e.clearTmpPoints()}),this.localWorkShapes.clear()}setToolsOpt(r){var e;this.tmpOpt=r,(e=r.toolsOpt)!=null&&e.syncUnitTime&&(this.syncUnitTime=r.toolsOpt.syncUnitTime)}getToolsOpt(){return this.tmpOpt}consumeDraw(r){const{workId:e,dataType:t}=r;if(t===s.EDataType.Service)this.activeServiceWorkShape(r);else{const o=e==null?void 0:e.toString(),i=o&&this.localWorkShapes.get(o);if(!i)return;const n=i.node.consume({data:r,isFullWork:!1,isSubWorker:!0});n.rect&&(i.totalRect=s.computRect(n.rect,i.totalRect),i.result=n,i.workState=s.EvevtWorkState.Doing,o&&this.localWorkShapes.set(o,i))}this.runAnimation()}consumeDrawAll(r){const{workId:e,dataType:t}=r;if(t===s.EDataType.Service)this.activeServiceWorkShape(r);else{const o=e==null?void 0:e.toString(),i=o&&this.localWorkShapes.get(o);if(!i)return;const n=i.node.consumeAll({data:r});i.totalRect=s.computRect(n.rect,i.totalRect),i.result=n,i.workState=s.EvevtWorkState.Done,o&&this.localWorkShapes.set(o,i)}this.runAnimation()}destroy(){this.serviceWorkShapes.clear(),this.localWorkShapes.clear()}setNodeKey(r,e,t,o){return e.toolsType=t,e.node=this.createWorkShapeNode({workId:r,toolsType:t,toolsOpt:o}),e}activeServiceWorkShape(r){var d,u;const{workId:e,opt:t,toolsType:o,type:i,updateNodeOpt:n,ops:a,op:c}=r;if(!e)return;const l=e.toString(),h=(d=this.vNodes.get(l))==null?void 0:d.rect;if(!((u=this.serviceWorkShapes)!=null&&u.has(l))){let f={toolsType:o,animationWorkData:c||[],animationIndex:0,type:i,updateNodeOpt:n,ops:a,oldRect:h};o&&t&&(f=this.setNodeKey(l,f,o,t)),this.serviceWorkShapes.set(l,f)}const p=this.serviceWorkShapes.get(l);i&&(p.type=i),a&&(p.animationWorkData=s.transformToNormalData(a),p.ops=a),n&&(p.updateNodeOpt=n),c&&(p.animationWorkData=c),p.node&&p.node.getWorkId()!==l&&p.node.setWorkId(l),h&&(p.oldRect=h),o&&t&&(p.toolsType!==o&&o&&t&&this.setNodeKey(l,p,o,t),p.node&&p.node.setWorkOptions(t))}computNextAnimationIndex(r,e){const t=Math.floor((r.animationWorkData||[]).slice(r.animationIndex).length*32/e/this.syncUnitTime)*e;return Math.min((r.animationIndex||0)+(t||e),(r.animationWorkData||[]).length)}async animationDraw(){var n,a,c,l;this.animationId=void 0;let r=!1;const e=new Map,t=[],o=[],i=[];for(const[h,p]of this.serviceWorkShapes.entries())switch(p.toolsType){case s.EToolsKey.LaserPen:{const u=this.computNextAnimationIndex(p,8),f=Math.max(0,p.animationIndex||0),y=(p.animationWorkData||[]).slice(f,u);if((p.animationIndex||0)<u){const m=(n=p.node)==null?void 0:n.consumeService({op:y,isFullWork:!1});p.totalRect=s.computRect(p.totalRect,m),p.animationIndex=u,y.length&&e.set(h,{workState:f===0?s.EvevtWorkState.Start:u===((a=p.animationWorkData)==null?void 0:a.length)?s.EvevtWorkState.Done:s.EvevtWorkState.Doing,op:y.slice(-2)})}if(o.push({isClear:!0,rect:p.totalRect,clearCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId}),t.push({rect:p.totalRect,drawCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId}),p.isDel){(c=p.node)==null||c.clearTmpPoints(),this.serviceWorkShapes.delete(h);break}p.ops&&p.animationIndex===((l=p.animationWorkData)==null?void 0:l.length)&&!p.isDel&&(this.topLayer.getElementsByName(h.toString())[0]||(p.isDel=!0,this.serviceWorkShapes.set(h,p))),r=!0;break}}for(const[h,p]of this.localWorkShapes.entries()){const{result:d,toolsType:u,totalRect:f,isDel:y,workState:m}=p;switch(u){case s.EToolsKey.LaserPen:{if(f&&(o.push({isClear:!0,rect:f,clearCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId}),t.push({rect:f,drawCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId})),y){p.node.clearTmpPoints(),this.localWorkShapes.delete(h),i.push({removeIds:[h.toString()],viewId:this.viewId,type:s.EPostMessageType.RemoveNode});break}d&&((d.op||d.ops)&&i.push(d),p.result=void 0),!this.topLayer.getElementsByName(h.toString())[0]&&m===s.EvevtWorkState.Done&&(p.isDel=!0,this.localWorkShapes.set(h,p)),r=!0;break}}}r&&this.runAnimation(),e.size&&e.forEach((h,p)=>{i.push({type:s.EPostMessageType.Cursor,uid:p.split(s.Storage_Splitter)[0],op:h.op,workState:h.workState,viewId:this.viewId})}),(t.length||o.length||i.length)&&this.post({render:[...o,...t],sp:i})}runAnimation(){this.animationId||(this.animationId=setTimeout(()=>{this.animationId=void 0,this.animationDraw()},16))}}class Ft extends Ze{constructor(e){super(e);g(this,"opt");g(this,"scene");g(this,"cameraOpt");g(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,o;U(this.fullLayer,this.fullLayer.parent),(e=this.fullLayer)==null||e.disconnect(),(t=this.fullLayer)==null||t.remove(),(o=this.scene)==null||o.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:o}=e;this.scene.width=t,this.scene.height=o,this.updateLayer({width:t,height:o})}updateLayer(e){if(!this.fullLayer)throw new Error("SnapshotWork snapshotFullLayer is not initialized");const{width:t,height:o}=e;this.fullLayer.parent.setAttribute("width",t),this.fullLayer.parent.setAttribute("height",o),this.fullLayer.setAttribute("size",[t,o]),this.fullLayer.setAttribute("pos",[t*.5,o*.5])}setCameraOpt(e){this.cameraOpt=e;const{scale:t,centerX:o,centerY:i,width:n,height:a}=e;if(!this.scene)throw new Error("SnapshotWork scene is not initialized");if(!this.fullLayer)throw new Error("SnapshotWork snapshotFullLayer is not initialized");(n!==this.scene.width||a!==this.scene.height)&&this.updateScene({width:n,height:a}),this.fullLayer.setAttribute("scale",[t,t]),this.fullLayer.setAttribute("translate",[-o,-i])}getOffscreen(){var t;return((t=this.fullLayer)==null?void 0:t.parent).canvas}getRectImageBitmap(e,t){const{rect:o}=e,i=this.thread.dpr,n=Math.floor(o.x*i),a=Math.floor(o.y*i),c=Math.floor(o.w*i),l=Math.floor(o.h*i);return createImageBitmap(this.getOffscreen(),n,a,c,l,t)}async getSnapshot(e){const{scenePath:t,scenes:o,cameraOpt:i,w:n,h:a}=e;if(t&&o&&i&&this.fullLayer){this.setCameraOpt(i);for(const[l,h]of Object.entries(o))if(h!=null&&h.type)switch(h==null?void 0:h.type){case s.EPostMessageType.UpdateNode:case s.EPostMessageType.FullWork:{const{opt:p}=h,d={...h,opt:p,workId:l,msgType:s.EPostMessageType.FullWork,dataType:s.EDataType.Service,viewId:this.viewId};await this.runFullWork(d);break}}let c;n&&a&&(c={resizeWidth:n,resizeHeight:a});try{await this.getSnapshotRender({scenePath:t,options:c})}catch(l){const h=l&&l instanceof Error?l.message:l==null?void 0:l.toString();console.error("[SnapshotWork] getSnapshotRender error",h),this.thread._post({sp:[{type:s.EPostMessageType.ReportError,reportString:`[SnapshotWork] getSnapshotRender error: ${h}`}]})}}}async runFullWork(e){var i;const t=this.setFullWork(e),o=e.ops&&s.transformToNormalData(e.ops);if(t){let n,a;const c=(i=t.getWorkId())==null?void 0:i.toString();return t.toolsType===s.EToolsKey.BackgroundSVG?n=t.consumeService({isFullWork:!0,replaceId:c}):t.toolsType===s.EToolsKey.Image?n=await t.consumeServiceAsync({isFullWork:!0,worker:this.thread}):t.toolsType===s.EToolsKey.Text?n=await t.consumeServiceAsync({isFullWork:!0,replaceId:c,isDrawLabel:!0}):(n=t.consumeService({op:o,isFullWork:!0,replaceId:c}),a=(e==null?void 0:e.updateNodeOpt)&&t.updataOptService(e.updateNodeOpt)),e.workId&&this.workShapes.delete(e.workId.toString()),s.computRect(n,a)}}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 n;const{scenePath:t,options:o}=e;((n=this.fullLayer)==null?void 0:n.parent).render();const i=await this.getRectImageBitmap({rect:this.getSceneRect(),drawCanvas:s.ECanvasShowType.None},o);i&&(this.thread._post({sp:[{type:s.EPostMessageType.Snapshot,scenePath:t,imageBitmap:i,viewId:this.viewId,index:0}]},[i]),i.close())}async getBoundingRect(e){const{scenePath:t,scenes:o,cameraOpt:i}=e;if(t&&o&&i&&this.fullLayer){this.setCameraOpt(i);let n;for(const[a,c]of Object.entries(o))if(c!=null&&c.type)switch(c==null?void 0:c.type){case s.EPostMessageType.UpdateNode:case s.EPostMessageType.FullWork:{const l=await this.runFullWork({...c,workId:a,msgType:s.EPostMessageType.FullWork,dataType:s.EDataType.Service,viewId:this.viewId});n=s.computRect(n,l);break}}n&&this.thread._post({sp:[{type:s.EPostMessageType.BoundingBox,scenePath:t,rect:n,viewId:this.viewId}]})}}}var ke=(k=>(k.Full="full",k.Sub="sub",k))(ke||{});class zt extends Ot{constructor(e,t,o){super(e,t,"sub");g(this,"type","sub");g(this,"_post");g(this,"topLayer");g(this,"serviceWork");g(this,"localWork");g(this,"topLayerWork");g(this,"snapshotWork");this._post=o;const i=s.DefaultAppliancePluginExtrasOptions.bufferSize.sub;this.topLayer=t.isUseSimple?void 0:this.createLayer("topLayer",this.scene,{...t.layerOpt,bufferSize:i,contextType:"2d"});const n={thread:this,viewId:this.viewId,vNodes:this.vNodes,fullLayer:this.fullLayer,topLayer:this.topLayer,isUseSimple:t.isUseSimple};this.localWork=new At(n),t.isUseSimple||(this.topLayerWork=new Bt(n)),this.snapshotWork=new Ft({...n,createScene:this.createScene.bind(this),createLayer:this.createLayer.bind(this)}),this.vNodes.init(this.fullLayer)}async combinePost(){var n,a;const{render:e,...t}=this.combinePostData();let o;if(e!=null&&e.length){const c=[];for(const l of e)if(l.rect){if(l.rect=Vt(s.cloneDeep(l.rect),this.scene,this.dpr),!l.rect)continue;if(l.drawCanvas&&l.rect&&l.rect.w>0&&l.rect.h>0){const h=await this.getRectImageBitmap(l);l.imageBitmap=h,o||(o=[]),o.push(h)}c.push(l)}t.render=c}const i=(n=t.sp)==null?void 0:n.filter(c=>c.type!==s.EPostMessageType.None||c.isLockSentEventCursor||c.needUndoTicker);i!=null&&i.length?t.sp=i.map(c=>({...c,viewId:this.viewId})):delete t.sp,t.consumeCount===void 0&&delete t.consumeCount,t.subWorkerDrawCount===void 0&&delete t.subWorkerDrawCount,(i!=null&&i.length||t.consumeCount||t.subWorkerDrawCount||(a=t==null?void 0:t.render)!=null&&a.length)&&this._post(t,o),this.delayPostDoneResolve&&this.delayPostDoneResolve(!0)}async on(e){var l,h;const{msgType:t,toolsType:o,opt:i,dataType:n,workState:a,isLockSentEventCursor:c}=e;switch(t){case s.EPostMessageType.UpdateTools:{if(o&&((l=this.topLayerWork)!=null&&l.canUseTopLayer(o))&&i){const p={toolsType:o,toolsOpt:i};this.topLayerWork.setToolsOpt(p);return}break}case s.EPostMessageType.CreateWork:{this.createLocalWork(e);return}case s.EPostMessageType.DrawWork:{a===s.EvevtWorkState.Done&&n===s.EDataType.Local?(await this.consumeDrawAll(n,e),o===s.EToolsKey.LaserPen&&c&&this.post({sp:[{type:s.EPostMessageType.None,isLockSentEventCursor:c}]})):this.consumeDraw(n,e);return}case s.EPostMessageType.RemoveNode:{await this.removeNode(e);return}case s.EPostMessageType.FullWork:{o&&((h=this.topLayerWork)!=null&&h.canUseTopLayer(o))&&await this.consumeDrawAll(n,e);return}case s.EPostMessageType.Snapshot:{this.snapshotWork.createSnapshotFullLayer(this.opt,{...this.opt.layerOpt,bufferSize:this.viewId===s.Main_View_Id?6e3:3e3,contextType:"2d"}),await this.snapshotWork.getSnapshot(e),this.snapshotWork.destroySnapshotFullLayer();return}case s.EPostMessageType.BoundingBox:{this.snapshotWork.createSnapshotFullLayer(this.opt,{...this.opt.layerOpt,bufferSize:this.viewId===s.Main_View_Id?6e3:3e3,contextType:"2d"}),await this.snapshotWork.getBoundingRect(e),this.snapshotWork.destroySnapshotFullLayer();return}}await super.on(e)}createLocalWork(e){var n;const{workId:t,toolsType:o,opt:i}=e;if(o&&((n=this.topLayerWork)!=null&&n.canUseTopLayer(o))&&t&&i){this.topLayerWork.getToolsOpt()||this.topLayerWork.setToolsOpt({toolsType:o,toolsOpt:i}),this.topLayerWork.setWorkOptions(t.toString(),i);return}o&&super.createLocalWork(e)}async removeNode(e){const{dataType:t}=e;t===s.EDataType.Local&&await this.localWork.removeWork(e)}getLayer(e){switch(e){case s.ECanvasShowType.TopFloat:return this.topLayer||this.fullLayer;default:return this.fullLayer}}getOffscreen(e){var o;return((o=this.getLayer(e))==null?void 0:o.parent).canvas}async consumeDraw(e,t){var n;const{workId:o,toolsType:i}=t;if(o){if(i&&((n=this.topLayerWork)!=null&&n.canUseTopLayer(i))){e===s.EDataType.Local&&(this.topLayerWork.getWorkShape(o.toString())||this.createLocalWork(t)),this.topLayerWork.consumeDraw(t);return}i&&(this.localWork.getWorkShape(o.toString())||this.createLocalWork(t),await this.localWork.consumeDraw(t));return}}async consumeDrawAll(e,t){var a;const{workId:o,toolsType:i,dataType:n}=t;if(o){const c=o.toString();if(i&&((a=this.topLayerWork)!=null&&a.canUseTopLayer(i))){n===s.EDataType.Local&&(this.topLayerWork.getWorkShape(c)||this.createLocalWork(t)),this.topLayerWork.consumeDrawAll(t);return}i&&(this.localWork.getWorkShape(c)||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()}),U(this.topLayer,this.topLayer.parent)),await this.post({render:[{isClearAll:!0,clearCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId}],sp:[{type:s.EPostMessageType.Clear}]})}getRectImageBitmap(e,t){const{rect:o,drawCanvas:i}=e,n=Math.floor(o.x*this.dpr),a=Math.floor(o.y*this.dpr),c=Math.floor(o.w*this.dpr||1),l=Math.floor(o.h*this.dpr||1);return createImageBitmap(this.getOffscreen(i),n,a,c,l,t)}updateLayer(e){const{width:t,height:o}=e;super.updateLayer(e),this.topLayer&&(this.topLayer.parent.setAttribute("width",t),this.topLayer.parent.setAttribute("height",o),this.topLayer.setAttribute("size",[t,o]),this.topLayer.setAttribute("pos",[t*.5,o*.5]))}async updateDpr(e){super.updateDpr(e);const t=this.topLayerWork&&!!this.topLayerWork.localWorkShapes.size||!1,o=!!this.localWork.getWorkShapes().size,i=[];o&&i.push({isClearAll:!0,clearCanvas:s.ECanvasShowType.Float,viewId:this.viewId}),t&&i.push({isClearAll:!0,clearCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId}),i.length&&await this.post({render:i})}async updateCamera(e){var i;const t=[],{cameraOpt:o}=e;if(o&&!H(this.cameraOpt,o)){const n=this.topLayerWork&&!!this.topLayerWork.localWorkShapes.size||!1,a=!!this.localWork.getWorkShapes().size;if(a&&this.localWork.workShapesDone(),this.setCameraOpt(o),a&&t.push({isClearAll:!0,clearCanvas:s.ECanvasShowType.Float,viewId:this.viewId}),n&&this.topLayerWork){t.push({isClearAll:!0,clearCanvas:s.ECanvasShowType.TopFloat,viewId:this.viewId});for(const[c,l]of this.topLayerWork.localWorkShapes.entries()||[])if(l.totalRect){let h;(i=this.topLayer)==null||i.getElementsByName(c.toString()).forEach(p=>{const d=p.getBoundingClientRect(),u=s.getSafetyRect({x:d.x,y:d.y,w:d.width,h:d.height});h=s.computRect(h,u)}),l.totalRect=h,this.topLayerWork.localWorkShapes.set(c,l)}}t.length&&await this.post({render:t})}}setCameraOpt(e,t){var l,h;this.cameraOpt=e;const{scale:o,centerX:i,centerY:n,width:a,height:c}=e;(a!==this.scene.width||c!==this.scene.height)&&this.updateScene(this.scene,{width:a,height:c}),t?(t.setAttribute("scale",[o,o]),t.setAttribute("translate",[-i,-n])):(this.fullLayer.setAttribute("scale",[o,o]),this.fullLayer.setAttribute("translate",[-i,-n]),(l=this.topLayer)==null||l.setAttribute("scale",[o,o]),(h=this.topLayer)==null||h.setAttribute("translate",[-i,-n]))}}class ae extends W{constructor(e){super(e);g(this,"canRotate",!0);g(this,"scaleType",s.EScaleType.all);g(this,"toolsType",s.EToolsKey.Image);g(this,"tmpPoints",[]);g(this,"workOptions");g(this,"oldRect");this.workOptions=e.toolsOpt,this.scaleType=ae.getScaleType(this.workOptions)}consume(){return{type:s.EPostMessageType.None}}consumeAll(){return{type:s.EPostMessageType.None}}draw(e){const{layer:t,workId:o,replaceId:i,imageBitmap:n}=e,{centerX:a,centerY:c,width:l,height:h,rotate:p,zIndex:d,eraserlines:u}=this.workOptions,f=new D.Group({anchor:[.5,.5],pos:[a,c],name:o,size:[l,h],zIndex:d,rotate:p}),y={anchor:[.5,.5],pos:[0,0],size:[l,h],texture:n};n||(y.bgcolor="rgba(0,0,0,0.3)");const m=new D.Sprite(y);f.append(m),u&&this.drawEraserlines({group:f,eraserlines:u,pos:[a,c],layer:t}),this.replace(t,i||o,f);const w=f.getBoundingClientRect();if(w)return{x:Math.floor(w.x-W.SafeBorderPadding),y:Math.floor(w.y-W.SafeBorderPadding),w:Math.floor(w.width+W.SafeBorderPadding*2),h:Math.floor(w.height+W.SafeBorderPadding*2)}}consumeService(){}async consumeServiceAsync(e){var h,p,d,u;const{isFullWork:t,replaceId:o,worker:i}=e,{src:n,uuid:a}=this.workOptions,c=((h=this.workId)==null?void 0:h.toString())||a,l=t?this.fullLayer:this.drawLayer||this.fullLayer;if(n){const f=await i.loadImageBitMap({toolsType:this.toolsType,opt:this.workOptions,workId:c,isSubWorker:i instanceof zt});if(f){const y=this.draw({workId:c,layer:l,replaceId:o,imageBitmap:f});return this.oldRect=c&&((d=(p=this.vNodes)==null?void 0:p.get(c))==null?void 0:d.rect)||void 0,(u=this.vNodes)==null||u.setInfo(c,{rect:y,op:[],opt:this.workOptions,toolsType:this.toolsType,scaleType:this.scaleType,canRotate:this.canRotate,centerPos:y&&W.getCenterPos(y,l)}),y}}}clearTmpPoints(){this.tmpPoints.length=0}static getScaleType(e){const{uniformScale:t,rotate:o}=e;return t!==!1||o&&Math.abs(o)%90>0?s.EScaleType.proportional:s.EScaleType.all}static updateNodeOpt(e){const{node:t,opt:o,vNodes:i,targetNode:n}=e,{translate:a,originPoint:c,scenePoint:l,scale:h,angle:p,isLocked:d,zIndex:u,strokeColor:f}=o,y=n&&s.cloneDeep(n)||i.get(t.name);if(!y)return;const m=t.parent;if(m){if(f&&(y.opt.strokeColor=f),A(u)&&(t.setAttribute("zIndex",u),y.opt.zIndex=u),ee(d)&&(y.opt.locked=d),c&&l&&h&&a){const{centerX:w,centerY:P,width:T,height:S,uniformScale:E}=y.opt,I=E!==!1?[h[0],h[0]]:h,R=[w,P],M=[w,P];s.scalePoints(M,l,I,a);const v=[M[0]-R[0],M[1]-R[1]];if(y.centerPos=[y.centerPos[0]+v[0],y.centerPos[1]+v[1]],y.opt.width=Math.round(T*I[0]),y.opt.height=Math.round(S*I[1]),y.opt.centerX=M[0],y.opt.centerY=M[1],y.opt.eraserlines)for(const L of y.opt.eraserlines){const{op:C,thickness:b}=L;L.thickness=Math.round(b*Math.max(h[0],h[1]));for(let N=0;N<C.length;N++)s.scaleLitePoints(C[N],l,h,a)}}else if(a){if(y.opt.centerX=y.opt.centerX+a[0],y.opt.centerY=y.opt.centerY+a[1],y.centerPos=[y.centerPos[0]+a[0],y.centerPos[1]+a[1]],y.opt.eraserlines)for(const w of y.opt.eraserlines){const{op:P}=w;for(let T=0;T<P.length;T++){const S=P[T].map((E,I)=>I%2?E+a[1]:E+a[0]);w.op[T]=S}}}else if(A(p))if(y.opt.rotate=p,y.scaleType=ae.getScaleType(y.opt),n){const w=s.getRectRotated(y.rect,p);y.rect=w}else{const w=W.getRectFromLayer(m,t.name);y.rect=w||y.rect}return y&&i.setInfo(t.name,y),y==null?void 0:y.rect}}}const Ut=k=>{if(k.tagName==="GROUP"){const r=Object.getOwnPropertySymbols(k).find(e=>e.toString()==="Symbol(sealed)");if(r&&k[r])return!0}return!1},B=(k,r)=>{if(k.mesh&&k.mesh.texture&&k.name==="eraserTexture"){const e=k.mesh.texture.image;r.deleteTexture(e)}else if(k.tagName==="GROUP"&&k&&k.children)for(const e of k.children)B(e,r)},U=(k,r)=>{B(k,r),k.removeAllChildren()},Kt=(k,r,e)=>{const t=e.parent;if(r){const o=r.children;if(o){for(const i of o)if(B(i,t),i.tagName==="GROUP")for(const n of i.children)B(n,t)}}r.parent.replaceChild(k,r)},Vt=(k,r,e)=>{if(k.w+k.x<=0||k.h+k.y<=0||k.w<=0||k.h<=0)return;const t=r.width,o=r.height,i={x:Math.floor(Math.max(0,k.x)),y:Math.floor(Math.max(0,k.y)),w:Math.floor(Math.min(t,k.w)),h:Math.floor(Math.min(o,k.h))};if(i.x+i.w>t&&(i.w=Math.floor(t-i.x)),i.y+i.h>o&&(i.h=Math.floor(o-i.y)),s.isDecimal(e)){const n=s.decimalPlaces(e),a=Math.pow(10,n),c=s.gcd(a,e*a),l=i.x%c,h=i.x-l;h>=0?(i.x=h,i.w=i.w+l):(i.x=0,i.w=i.w+l-h);const p=i.y%c,d=i.y-p;d>=0?(i.y=d,i.h=i.h+p):(i.y=0,i.h=i.h+p-d)}return i};class _t{constructor(r){g(this,"vNodes");g(this,"thread");g(this,"serviceWorkShapes",new Map);g(this,"localWorkShapes",new Map);g(this,"tmpOpt");g(this,"animationId");g(this,"syncUnitTime",s.DefaultAppliancePluginExtrasOptions.syncOpt.interval);this.vNodes=r.vNodes,this.thread=r.thread}createLocalWork(r){const{workId:e,opt:t,toolsType:o}=r;if(e&&t){const i=e.toString();!this.getToolsOpt()&&o&&this.setToolsOpt({toolsType:o,toolsOpt:t}),this.setWorkOptions(i,t)}}getLocalWorkShape(r){return this.localWorkShapes.get(r)}createLocalWorkShape(r,e){if(r&&this.tmpOpt){const t={toolsType:this.tmpOpt.toolsType,toolsOpt:e||this.tmpOpt.toolsOpt},o=this.createWorkShapeNode({...t,workId:r});return o&&this.localWorkShapes.set(r,{node:o,toolsType:o.toolsType,workState:s.EvevtWorkState.Start}),o}}canUseTopLayer(r){return r===s.EToolsKey.LaserPen}destroy(){this.clearAll()}clearAll(){this.thread.topLayer.children.length&&(this.thread.topLayer.parent.children.forEach(r=>{r.name!=="viewport"&&r.remove()}),U(this.thread.serviceLayer,this.thread.serviceLayer.parent)),this.serviceWorkShapes.clear(),this.localWorkShapes.clear()}consumeDraw(r){const{workId:e,dataType:t}=r;if(t===s.EDataType.Service)this.activeServiceWorkShape(r);else{const o=e==null?void 0:e.toString(),i=o&&this.localWorkShapes.get(o);if(!i)return;const n=i.node.consume({data:r,isFullWork:!1,isSubWorker:!0});n.rect&&(i.result=n,i.workState=s.EvevtWorkState.Doing,o&&this.localWorkShapes.set(o,i))}this.runAnimation()}setToolsOpt(r){var e;this.tmpOpt=r,(e=r.toolsOpt)!=null&&e.syncUnitTime&&(this.syncUnitTime=r.toolsOpt.syncUnitTime)}getToolsOpt(){return this.tmpOpt}createWorkShapeNode(r){const{toolsType:e}=r;if(e===s.EToolsKey.LaserPen)return ie({...r,vNodes:this.vNodes,fullLayer:this.thread.topLayer,drawLayer:this.thread.topLayer})}setNodeKey(r,e,t,o){return e.toolsType=t,e.node=this.createWorkShapeNode({workId:r,toolsType:t,toolsOpt:o}),e}activeServiceWorkShape(r){var d,u;const{workId:e,opt:t,toolsType:o,type:i,updateNodeOpt:n,ops:a,op:c}=r;if(!e)return;const l=e.toString(),h=(d=this.vNodes.get(l))==null?void 0:d.rect;if(!((u=this.serviceWorkShapes)!=null&&u.has(l))){let f={toolsType:o,animationWorkData:c||[],animationIndex:0,type:i,updateNodeOpt:n,ops:a,oldRect:h};o&&t&&(f=this.setNodeKey(l,f,o,t)),this.serviceWorkShapes.set(l,f)}const p=this.serviceWorkShapes.get(l);i&&(p.type=i),a&&(p.animationWorkData=s.transformToNormalData(a),p.ops=a),n&&(p.updateNodeOpt=n),c&&(p.animationWorkData=c),p.node&&p.node.getWorkId()!==l&&p.node.setWorkId(l),h&&(p.oldRect=h),o&&t&&(p.toolsType!==o&&o&&t&&this.setNodeKey(l,p,o,t),p.node&&p.node.setWorkOptions(t))}computNextAnimationIndex(r,e){var i;const t=((i=r.node)==null?void 0:i.syncUnitTime)||this.syncUnitTime,o=Math.floor((r.animationWorkData||[]).slice(r.animationIndex).length*32/e/t)*e;return Math.min((r.animationIndex||0)+(o||e),(r.animationWorkData||[]).length)}animationDraw(){var o,i,n,a;this.animationId=void 0;let r=!1;const e=new Map,t=[];for(const[c,l]of this.serviceWorkShapes.entries())switch(l.toolsType){case s.EToolsKey.LaserPen:{const p=this.computNextAnimationIndex(l,8),d=Math.max(0,l.animationIndex||0),u=(l.animationWorkData||[]).slice(d,p);if((l.animationIndex||0)<p&&((o=l.node)==null||o.consumeService({op:u,isFullWork:!1}),l.animationIndex=p,u.length&&e.set(c,{workState:d===0?s.EvevtWorkState.Start:p===((i=l.animationWorkData)==null?void 0:i.length)?s.EvevtWorkState.Done:s.EvevtWorkState.Doing,op:u.slice(-2)})),l.isDel){(n=l.node)==null||n.clearTmpPoints(),this.serviceWorkShapes.delete(c);break}l.ops&&l.animationIndex===((a=l.animationWorkData)==null?void 0:a.length)&&!l.isDel&&(this.thread.topLayer.getElementsByName(c.toString())[0]||(l.isDel=!0,this.serviceWorkShapes.set(c,l))),r=!0;break}}for(const[c,l]of this.localWorkShapes.entries()){const{result:h,toolsType:p,isDel:d,workState:u}=l;switch(p){case s.EToolsKey.LaserPen:{if(d){l.node.clearTmpPoints(),this.localWorkShapes.delete(c),t.push({removeIds:[c.toString()],type:s.EPostMessageType.RemoveNode});break}h&&((h.op||h.ops)&&t.push(h),l.result=void 0),!this.thread.topLayer.getElementsByName(c.toString())[0]&&u===s.EvevtWorkState.Done&&(l.isDel=!0,this.localWorkShapes.set(c,l)),r=!0;break}}}r&&this.runAnimation(),e.size&&e.forEach((c,l)=>{t.push({type:s.EPostMessageType.Cursor,uid:l.split(s.Storage_Splitter)[0],op:c.op,workState:c.workState,viewId:this.thread.viewId})}),t.length&&this.thread.post({sp:t})}runAnimation(){this.animationId||(this.animationId=requestAnimationFrame(this.animationDraw.bind(this)))}setWorkOptions(r,e){var o;let t=(o=this.localWorkShapes.get(r))==null?void 0:o.node;if(!t&&this.tmpOpt){const{toolsType:i}=this.tmpOpt;this.tmpOpt.toolsOpt=e,t=this.createWorkShapeNode({workId:r,toolsType:i,toolsOpt:e}),t&&this.localWorkShapes.set(r,{node:t,toolsType:i,workState:s.EvevtWorkState.Start}),this.setToolsOpt(this.tmpOpt)}e!=null&&e.syncUnitTime||(e.syncUnitTime=this.syncUnitTime),t&&t.setWorkOptions(e)}consumeDrawAll(r){const{workId:e,dataType:t}=r;if(t===s.EDataType.Service)this.activeServiceWorkShape(r);else{const o=e==null?void 0:e.toString(),i=o&&this.localWorkShapes.get(o);if(!i)return;const n=i.node.consumeAll({data:r});i.result=n,i.workState=s.EvevtWorkState.Done,o&&this.localWorkShapes.set(o,i)}this.runAnimation()}}class Gt{constructor(r){g(this,"vNodes");g(this,"thread");g(this,"workShapes",new Map);g(this,"effectSelectNodeData",new Set);g(this,"batchEraserRemoveNodes",new Set);g(this,"batchEraserWorks",new Set);g(this,"tmpOpt");g(this,"syncUnitTime",s.DefaultAppliancePluginExtrasOptions.syncOpt.interval);g(this,"fullWorkerDrawCount",0);g(this,"drawWorkActiveId");g(this,"consumeCount",0);g(this,"combineTimerId");g(this,"combineDrawResolve");g(this,"combineDrawActiveId");this.vNodes=r.vNodes,this.thread=r.thread}async loadImageBitMap(r){return await this.thread.loadImageBitMap(r)}createLocalWork(r){const{workId:e,opt:t,toolsType:o}=r;if(e&&t){const i=e.toString();!this.getToolsOpt()&&o&&this.setToolsOpt({toolsType:o,toolsOpt:t}),this.setWorkOptions(i,t)}}async updateSelector(r){var u;const e=this.workShapes.get(s.Storage_Selector_key);if(!((u=e==null?void 0:e.selectIds)!=null&&u.length))return;const{callback:t,...o}=r,{updateSelectorOpt:i,willSerializeData:n,smoothSync:a}=o,c=await(e==null?void 0:e.updateSelector({updateSelectorOpt:i,selectIds:$.cloneDeep(e.selectIds),vNodes:this.vNodes,willSerializeData:n,worker:this})),l=new Map;let h;c!=null&&c.selectIds&&(h=$.xor(e.selectIds,c.selectIds),c.selectIds.forEach(f=>{const y=this.vNodes.get(f);if(y){const{toolsType:m,op:w,opt:P}=y;l.set(f,{opt:P,toolsType:m,ops:(w==null?void 0:w.length)&&s.transformToSerializableData(w)||void 0})}}),e.selectIds=c.selectIds);const p=[],d=t&&t({res:c,workShapeNode:e,param:o,postData:{sp:p},newServiceStore:l,smoothSync:a})||{sp:p};h&&d.sp.push({type:s.EPostMessageType.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()}),U(this.thread.localLayer,this.thread.localLayer.parent)),this.workShapes.get(s.Storage_Selector_key)){const e=[];e.push({type:s.EPostMessageType.Select,dataType:s.EDataType.Local,selectIds:[],willSyncService:!1}),this.thread.post({sp:e})}this.workShapes.clear(),this.effectSelectNodeData.clear(),this.batchEraserWorks.clear(),this.batchEraserRemoveNodes.clear()}async checkTextActive(r){const{op:e,viewId:t,dataType:o}=r;if(e!=null&&e.length){let i;for(const n of this.vNodes.curNodeMap.values()){const{rect:a,name:c,toolsType:l,opt:h}=n,p=e[0]*this.thread.fullLayer.worldScaling[0]+this.thread.fullLayer.worldPosition[0],d=e[1]*this.thread.fullLayer.worldScaling[1]+this.thread.fullLayer.worldPosition[1];if(l===s.EToolsKey.Text&&s.isIntersectForPoint([p,d],a)&&h.workState===s.EvevtWorkState.Done){i=c;break}}i&&(await this.blurSelector({viewId:t,msgType:s.EPostMessageType.Select,dataType:o,isSync:!0}),this.thread.post({sp:[{type:s.EPostMessageType.GetTextActive,toolsType:s.EToolsKey.Text,workId:i}]}))}}cursorHover(r){const{opt:e,toolsType:t,point:o}=r,i=this.setFullWork({workId:s.Cursor_Hover_Id,toolsType:t,opt:e});i&&o&&i.cursorHover(o)}cursorBlur(){var e;const r=this.getWorkShape(s.Cursor_Hover_Id);r&&((e=r.selectIds)!=null&&e.length)&&(r.cursorBlur(),this.clearWorkShapeNodeCache(s.Cursor_Hover_Id)),this.thread.fullLayer.parent.children.forEach(t=>{t.name==="Cursor_Hover_Id"&&t.remove()})}updateFullSelectWork(r){var o,i,n,a,c;const e=this.workShapes.get(s.Storage_Selector_key),{selectIds:t}=r;if(!(t!=null&&t.length)){this.blurSelector(r);return}if(!e){const l=this.setFullWork(r);!l&&r.workId&&this.tmpOpt&&((o=this.tmpOpt)==null?void 0:o.toolsType)===s.EToolsKey.Selector&&this.setWorkOptions(r.workId.toString(),r.opt||this.tmpOpt.toolsOpt),l&&this.updateFullSelectWork(r);return}if(e&&(t!=null&&t.length)){const{selectRect:l}=e.updateSelectIds(t),h=[{...r,selectorColor:((i=r.opt)==null?void 0:i.strokeColor)||e.selectorColor,strokeColor:((n=r.opt)==null?void 0:n.strokeColor)||e.strokeColor,fillColor:((a=r.opt)==null?void 0:a.fillColor)||e.fillColor,textOpt:((c=r.opt)==null?void 0:c.textOpt)||e.textOpt,canTextEdit:e.canTextEdit,canRotate:e.canRotate,scaleType:e.scaleType,type:s.EPostMessageType.Select,selectRect:l,points:e.getChildrenPoints(),willSyncService:(r==null?void 0:r.willSyncService)||!1,opt:(r==null?void 0:r.willSyncService)&&e.getWorkOptions()||void 0,canLock:e.canLock,isLocked:e.isLocked,toolsTypes:e.toolsTypes,shapeOpt:e.shapeOpt,thickness:e.thickness,useStroke:e.useStroke,strokeType:e.strokeType}];this.thread.post({sp:h})}}async removeSelector(r){const{willSyncService:e,needUndoTicker:t}=r,o=[],i=[],n=this.workShapes.get(s.Storage_Selector_key);if(!n)return;const a=n.selectIds&&[...n.selectIds]||[];for(const c of a){const l=this.vNodes.get(c);if(l)switch(l.toolsType){case s.EToolsKey.Text:{o.push({type:s.EPostMessageType.TextUpdate,toolsType:s.EToolsKey.Text,workId:c,dataType:s.EDataType.Local});break}case s.EToolsKey.BackgroundSVG:o.push({type:s.EPostMessageType.BackgroundSVGDelete,toolsType:s.EToolsKey.BackgroundSVG,workId:c,dataType:s.EDataType.Local,viewId:this.thread.viewId});break}this.removeNode(c),i.push(c)}i.length&&o.push({type:s.EPostMessageType.RemoveNode,removeIds:i}),o.push({type:s.EPostMessageType.Select,selectIds:[],willSyncService:e}),await this.blurSelector(),t&&o.push({type:s.EPostMessageType.None,needUndoTicker:t}),o.length&&this.thread.post({sp:o})}removeWork(r){const{workId:e}=r,t=e==null?void 0:e.toString();t&&this.removeNode(t)}removeNode(r){var o;const e=this.vNodes.get(r);e&&(e.toolsType===s.EToolsKey.BackgroundSVG&&this.thread.post({sp:[{type:s.EPostMessageType.BackgroundSVGDelete,toolsType:s.EToolsKey.BackgroundSVG,workId:r,dataType:s.EDataType.Local,viewId:this.thread.viewId}]}),(o=this.thread.fullLayer)==null||o.getElementsByName(r).forEach(i=>{i.remove(),B(i,this.thread.fullLayer.parent)}),this.vNodes.delete(r)),this.workShapes.has(r)&&(this.thread.localLayer.getElementsByName(r).forEach(i=>{i.remove(),B(i,this.thread.localLayer.parent)}),this.clearWorkShapeNodeCache(r))}setFullWork(r){const{workId:e,opt:t,toolsType:o}=r;if(e&&t&&o){const i=e.toString();let n;return e&&this.workShapes.has(i)?(n=this.workShapes.get(i),n==null||n.setWorkOptions(t)):n=this.createWorkShapeNode({toolsOpt:t,toolsType:o,workId:i}),n?(this.workShapes.set(i,n),n):void 0}}async consumeFull(r){var o;const e=this.setFullWork(r),t=r.ops&&s.transformToNormalData(r.ops);if(e){const i=(o=r.workId)==null?void 0:o.toString();e.toolsType===s.EToolsKey.BackgroundSVG?e.consumeService({isFullWork:!0,replaceId:i}):e.toolsType===s.EToolsKey.Image?await e.consumeServiceAsync({isFullWork:!0,replaceId:i,worker:this}):e.toolsType===s.EToolsKey.Text?await e.consumeServiceAsync({isFullWork:!0,replaceId:i,boxRect:this.thread.getSceneRect()}):e.consumeService({op:t,isFullWork:!0,replaceId:i}),r!=null&&r.updateNodeOpt&&e.updataOptService(r.updateNodeOpt);const n=[];r.workId&&this.workShapes.delete(r.workId.toString()),r.willSyncService&&n.push({opt:r.opt,toolsType:r.toolsType,type:s.EPostMessageType.FullWork,workId:r.workId,ops:r.ops,updateNodeOpt:r.updateNodeOpt,viewId:this.thread.viewId}),r.needUndoTicker&&n.push({type:s.EPostMessageType.None,needUndoTicker:r.needUndoTicker}),n.length&&this.thread.post({sp:n})}}async colloctEffectSelectWork(r){const e=this.workShapes.get(s.Storage_Selector_key),{workId:t,msgType:o}=r;if(e&&t&&e.selectIds&&e.selectIds.includes(t.toString())){o===s.EPostMessageType.RemoveNode?e.selectIds=e.selectIds.filter(i=>i!==t.toString()):this.effectSelectNodeData.add(r),await new Promise(i=>{setTimeout(()=>{i(!0)},0)}),await this.runEffectSelectWork(!0).then(()=>{var i;(i=this.effectSelectNodeData)==null||i.clear()});return}return r}async runEffectSelectWork(r){var e;for(const t of this.effectSelectNodeData.values()){const o=this.setFullWork(t);if(o){const i=(e=t.workId)==null?void 0:e.toString();if(o.toolsType===s.EToolsKey.BackgroundSVG)o.consumeService({isFullWork:!0,replaceId:i});else if(o.toolsType===s.EToolsKey.Image)await o.consumeServiceAsync({isFullWork:!0,replaceId:i,worker:this});else if(o.toolsType===s.EToolsKey.Text)await o.consumeServiceAsync({isFullWork:!0,replaceId:i,boxRect:this.thread.getSceneRect()});else{const n=t.ops&&s.transformToNormalData(t.ops);o.consumeService({op:n,isFullWork:!0,replaceId:i}),t!=null&&t.updateNodeOpt&&o.updataOptService(t.updateNodeOpt)}t.workId&&this.workShapes.delete(t.workId.toString())}}this.reRenderSelector(r)}hasSelector(){return this.workShapes.has(s.Storage_Selector_key)}getSelector(){return this.workShapes.get(s.Storage_Selector_key)}reRenderSelector(r=!1){var o;const e=this.workShapes.get(s.Storage_Selector_key);if(!e)return;if(e&&!((o=e.selectIds)!=null&&o.length))return this.blurSelector();const t=e.reRenderSelector();t&&this.thread.post({sp:[{type:s.EPostMessageType.Select,selectIds:e.selectIds,selectRect:t,willSyncService:r,viewId:this.thread.viewId,points:e.getChildrenPoints(),textOpt:e.textOpt,selectorColor:e.selectorColor,strokeColor:e.strokeColor,fillColor:e.fillColor,canTextEdit:e.canTextEdit,canRotate:e.canRotate,scaleType:e.scaleType,opt:e.getWorkOptions()||void 0,canLock:e.canLock,isLocked:e.isLocked,toolsTypes:e.toolsTypes,shapeOpt:e.shapeOpt,thickness:e.thickness,useStroke:e.useStroke,strokeType:e.strokeType}]})}async blurSelector(r){var o;const e=this.workShapes.get(s.Storage_Selector_key),t=e==null?void 0:e.blurSelector();if(this.clearWorkShapeNodeCache(s.Storage_Selector_key),((o=this.thread.fullLayer)==null?void 0:o.parent).children.forEach(i=>{i.name===s.Storage_Selector_key&&i.remove()}),t){const i=[];i.push({...t,isSync:r==null?void 0:r.isSync}),this.thread.post({sp:i})}}clearWorkShapeNodeCache(r){var e;(e=this.getWorkShape(r))==null||e.clearTmpPoints(),this.workShapes.delete(r)}async drawBitMapEraserFull(r,e,t,o){const{willUpdateNodes:i,willDeleteNodes:n}=e,a=r.getWorkId(),c=[{type:s.EPostMessageType.RemoveNode,removeIds:[a],viewId:this.thread.viewId}];if(t&&c.push({type:s.EPostMessageType.None,isLockSentEventCursor:t}),i!=null&&i.size||n!=null&&n.size){if(i!=null&&i.size)for(const[l,h]of i)c.push({type:s.EPostMessageType.UpdateNode,dataType:s.EDataType.Local,opt:h.opt,workId:l,updateNodeOpt:{useAnimation:!1}});n!=null&&n.size&&c.push({type:s.EPostMessageType.RemoveNode,removeIds:[...n],viewId:this.thread.viewId})}o&&c.push({type:s.EPostMessageType.None,needUndoTicker:o}),c.length&&this.thread.post({sp:c})}drawPencilEraserFull(r,e,t,o){const{willNewNodes:i,willDeleteNodes:n}=e,a=r.getWorkId(),c=[{type:s.EPostMessageType.RemoveNode,removeIds:[a],viewId:this.thread.viewId}];if(t&&c.push({type:s.EPostMessageType.None,isLockSentEventCursor:t}),i!=null&&i.size||n!=null&&n.size){if(i!=null&&i.size)for(const[l,h]of i)c.push({type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,toolsType:h.toolsType,ops:s.transformToSerializableData(h.op),opt:h.opt,workId:l,updateNodeOpt:{useAnimation:!1}});n!=null&&n.size&&c.push({type:s.EPostMessageType.RemoveNode,removeIds:[...n],viewId:this.thread.viewId})}o&&c.push({type:s.EPostMessageType.None,needUndoTicker:o}),c.length&&this.thread.post({sp:c})}drawEraser(r,e,t){const o=[];r.removeIds&&o.push(r),e&&o.push({type:s.EPostMessageType.None,isLockSentEventCursor:e}),t&&o.push({type:s.EPostMessageType.None,needUndoTicker:t}),this.thread.post({sp:o,consumeCount:this.consumeCount})}getWorkShape(r){return this.workShapes.get(r)}getWorkShapes(){return this.workShapes}consumeDraw(r,e){const{op:t,workId:o,scenePath:i,postCount:n,smoothSync:a}=r;if(t!=null&&t.length&&o){const c=o.toString(),l=this.workShapes.get(c);if(!l)return;const h=l.toolsType;if(h===s.EToolsKey.LaserPen)return;switch(this.combineDrawActiveId&&this.combineDrawActiveId!==c&&(this.combineTimerId&&(clearTimeout(this.combineTimerId),this.combineTimerId=void 0,this.combineDrawResolve&&this.combineDrawResolve(!1),this.combineDrawActiveId=void 0),this.consumeDrawAll({workId:this.combineDrawActiveId,scenePath:i,viewId:this.thread.viewId,msgType:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local},e)),this.drawWorkActiveId&&this.drawWorkActiveId!==c&&(this.consumeDrawAll({workId:this.drawWorkActiveId,scenePath:i,viewId:this.thread.viewId,msgType:s.EPostMessageType.DrawWork,dataType:s.EDataType.Local},e),this.drawWorkActiveId=void 0),!this.drawWorkActiveId&&c!==s.Storage_Selector_key&&(this.drawWorkActiveId=c),$.isNumber(n)&&(this.consumeCount=n),h){case s.EToolsKey.Selector:{const p=l.consume({data:r,isFullWork:!0,isMainThread:!0});this.fullWorkerDrawCount++;const d=[];p.type===s.EPostMessageType.Select&&(p.selectIds&&e.runReverseSelectWork(p.selectIds),d.push(p)),this.thread.post({consumeCount:this.consumeCount,fullWorkerDrawCount:this.fullWorkerDrawCount,sp:d});break}case s.EToolsKey.PencilEraser:case s.EToolsKey.BitMapEraser:{l.consume({data:r,isFullWork:!1,isMainThread:!0}),this.fullWorkerDrawCount++,this.thread.post({sp:void 0,consumeCount:this.consumeCount,fullWorkerDrawCount:this.fullWorkerDrawCount}),this.combineTimerId||new Promise(p=>{this.combineDrawActiveId=c,this.combineDrawResolve=p,this.combineTimerId=s.requestAsyncCallBack(()=>{this.combineTimerId=void 0,this.combineDrawResolve&&this.combineDrawResolve(!0)},this.thread.master.maxCombineEraserTime,this.thread.master.control.hasPolyfillMethod("requestIdleCallback"))}).then(p=>{p&&this.drawEraserCombine(c),this.combineDrawResolve=void 0});break}case s.EToolsKey.Eraser:{const p=l.consume({data:r,isFullWork:!0});this.drawEraser(p)}break;case s.EToolsKey.Arrow:case s.EToolsKey.Straight:case s.EToolsKey.Ellipse:case s.EToolsKey.Rectangle:case s.EToolsKey.Star:case s.EToolsKey.Polygon:case s.EToolsKey.SpeechBalloon:case s.EToolsKey.Pencil:{const p=l.consume({data:r,isFullWork:!1,isMainThread:!0,smoothSync:a});p&&(this.fullWorkerDrawCount++,this.thread.post({consumeCount:this.consumeCount,fullWorkerDrawCount:this.fullWorkerDrawCount,sp:p.op&&[{...p,scenePath:i}]||void 0}))}break}}}drawEraserCombine(r){var t,o,i,n;const e=(t=this.workShapes.get(r))==null?void 0:t.combineConsume({workerEngine:this});if(e){const{willDeleteNodes:a,willNewNodes:c}=e,l={render:[],sp:[]};if(e!=null&&e.rect){const h=s.getSafetyRect(e.rect);(o=l.render)==null||o.push({rect:h,isClear:!0,clearCanvas:s.ECanvasShowType.Bg,viewId:this.thread.viewId},{rect:h,drawCanvas:s.ECanvasShowType.Bg,viewId:this.thread.viewId})}if(a!=null&&a.size&&((i=l.sp)==null||i.push({type:s.EPostMessageType.RemoveNode,removeIds:[...a],viewId:this.thread.viewId})),c!=null&&c.size)for(const[h,p]of c)(n=l.sp)==null||n.push({type:s.EPostMessageType.FullWork,dataType:s.EDataType.Local,toolsType:p.toolsType,ops:s.transformToSerializableData(p.op),opt:p.opt,workId:h,updateNodeOpt:{useAnimation:!1}});this.thread.post(l)}}consumeDrawAll(r,e){var n,a,c;const{workId:t,scenePath:o,isLockSentEventCursor:i}=r;if(t){this.combineTimerId&&(clearTimeout(this.combineTimerId),this.combineTimerId=void 0,this.combineDrawResolve&&this.combineDrawResolve(!1),this.combineDrawActiveId=void 0);const l=t.toString();this.drawWorkActiveId===l&&(this.drawWorkActiveId=void 0);const h=this.workShapes.get(l);if(!h)return;const p=h.toolsType;if(p===s.EToolsKey.LaserPen)return;const d=this.workShapes.get(s.Cursor_Hover_Id),u=(n=d==null?void 0:d.selectIds)==null?void 0:n[0],f=h.consumeAll({data:r,workerEngine:this});switch(p){case s.EToolsKey.Selector:{f.selectIds&&u&&((a=f.selectIds)!=null&&a.includes(u))&&d.cursorBlur();const y=[];i&&y.push({type:s.EPostMessageType.None,isLockSentEventCursor:i}),f.type===s.EPostMessageType.Select&&(f.selectIds&&e.runReverseSelectWork(f.selectIds),y.push({...f,scenePath:o})),y.length&&this.thread.post({sp:y}),(c=h.selectIds)!=null&&c.length?h.clearTmpPoints():this.clearWorkShapeNodeCache(l)}break;case s.EToolsKey.PencilEraser:this.drawPencilEraserFull(h,f,i,r.needUndoTicker),this.fullWorkerDrawCount=0,this.clearWorkShapeNodeCache(l);break;case s.EToolsKey.BitMapEraser:this.drawBitMapEraserFull(h,f,i,r.needUndoTicker),this.fullWorkerDrawCount=0,this.clearWorkShapeNodeCache(l);break;case s.EToolsKey.Eraser:this.drawEraser({...f,scenePath:o},i,r.needUndoTicker),h.clearTmpPoints();break;case s.EToolsKey.Arrow:case s.EToolsKey.Straight:case s.EToolsKey.Ellipse:case s.EToolsKey.Rectangle:case s.EToolsKey.Star:case s.EToolsKey.Polygon:case s.EToolsKey.SpeechBalloon:case s.EToolsKey.Pencil:{const y=[];i&&y.push({type:s.EPostMessageType.None,isLockSentEventCursor:i}),f&&(y.push(f),this.fullWorkerDrawCount=0,r.needUndoTicker&&y.push({type:s.EPostMessageType.None,needUndoTicker:r.needUndoTicker}),this.thread.post({fullWorkerDrawCount:this.fullWorkerDrawCount,sp:y})),this.clearWorkShapeNodeCache(l);break}}}}getToolsOpt(){return this.tmpOpt}setToolsOpt(r){var e;this.tmpOpt=r,(e=r.toolsOpt)!=null&&e.syncUnitTime&&(this.syncUnitTime=r.toolsOpt.syncUnitTime)}setWorkOptions(r,e){let t=this.workShapes.get(r);if(!t&&this.tmpOpt){const{toolsType:o}=this.tmpOpt;this.tmpOpt.toolsOpt=e,t=this.createWorkShapeNode({workId:r,toolsType:o,toolsOpt:e}),t&&this.workShapes.set(r,t),this.setToolsOpt(this.tmpOpt)}e.syncUnitTime||(e.syncUnitTime=this.syncUnitTime),t==null||t.setWorkOptions(e)}createWorkShapeNode(r){return ie({...r,vNodes:this.vNodes,fullLayer:this.thread.fullLayer,drawLayer:this.thread.localLayer},this.thread.serviceWork)}}class Xt{constructor(r){g(this,"vNodes");g(this,"thread");g(this,"workShapes",new Map);g(this,"selectorWorkShapes",new Map);g(this,"willRunEffectSelectorIds",new Set);g(this,"runEffectId");g(this,"animationId");g(this,"syncUnitTime",s.DefaultAppliancePluginExtrasOptions.syncOpt.interval);this.vNodes=r.vNodes,this.thread=r.thread}async loadImageBitMap(r){return await this.thread.loadImageBitMap(r)}destroy(){this.clearAll()}clearAll(){this.thread.serviceLayer.children.length&&(this.thread.serviceLayer.parent.children.forEach(r=>{r.name!=="viewport"&&r.remove()}),U(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(r=>{var t,o;const e=this.selectorWorkShapes.get(r);e&&e.selectIds&&((t=e.node)==null||t.selectServiceNode(r,e,!0)),(o=e==null?void 0:e.selectIds)!=null&&o.length||this.selectorWorkShapes.delete(r)}),this.willRunEffectSelectorIds.clear()}runSelectWork(r){this.activeSelectorShape(r);const{workId:e}=r,t=e==null?void 0:e.toString();t&&this.willRunEffectSelectorIds.add(t),this.runEffect()}removeWork(r){const{workId:e}=r,t=e==null?void 0:e.toString();if(t){if(this.workShapes.get(t)){this.workShapes.delete(t),this.removeNode(t,r);return}this.removeNode(t,r)}}consumeFull(r){this.activeWorkShape(r),this.runAnimation()}runReverseSelectWork(r){r.forEach(e=>{this.selectorWorkShapes.forEach((t,o)=>{var i;if((i=t.selectIds)!=null&&i.length){const n=t.selectIds.indexOf(e);n>-1&&(t.selectIds.splice(n,1),this.willRunEffectSelectorIds.add(o))}})}),this.willRunEffectSelectorIds.size&&this.runEffect()}consumeDraw(r){this.activeWorkShape(r),this.runAnimation()}computNextAnimationIndex(r,e){const t=Math.floor((r.animationWorkData||[]).slice(r.animationIndex).length*32/e/this.syncUnitTime)*e;return Math.min((r.animationIndex||0)+(t||e),(r.animationWorkData||[]).length)}async animationDraw(){var t,o,i,n,a,c,l,h,p,d,u,f,y,m,w,P,T,S,E,I,R;this.animationId=void 0;let r=!1;const e=new Map;for(const[M,v]of this.workShapes.entries())switch(v.toolsType){case s.EToolsKey.BackgroundSVG:{(t=v.node)==null||t.consumeService({isFullWork:!0,replaceId:M}),this.selectorWorkShapes.forEach((L,C)=>{var b;(b=L.selectIds)!=null&&b.includes(M)&&(this.willRunEffectSelectorIds.add(C),this.runEffect())}),this.workShapes.delete(M);break}case s.EToolsKey.Image:{await((o=v.node)==null?void 0:o.consumeServiceAsync({isFullWork:!0,worker:this})),this.selectorWorkShapes.forEach((L,C)=>{var b;(b=L.selectIds)!=null&&b.includes(M)&&(this.willRunEffectSelectorIds.add(C),this.runEffect())}),this.workShapes.delete(M);break}case s.EToolsKey.Text:{v.node&&(await((i=v.node)==null?void 0:i.consumeServiceAsync({isFullWork:!0,replaceId:M,boxRect:this.thread.getSceneRect()})),this.selectorWorkShapes.forEach((L,C)=>{var b;(b=L.selectIds)!=null&&b.includes(M)&&(this.willRunEffectSelectorIds.add(C),this.runEffect())}),(n=v.node)==null||n.clearTmpPoints(),this.workShapes.delete(M));break}case s.EToolsKey.Arrow:case s.EToolsKey.Straight:case s.EToolsKey.Rectangle:case s.EToolsKey.Ellipse:case s.EToolsKey.Star:case s.EToolsKey.Polygon:case s.EToolsKey.SpeechBalloon:{const L=!!v.ops;if((a=v.animationWorkData)!=null&&a.length){const C=v.oldRect;(c=v.node)==null||c.consumeService({op:v.animationWorkData,isFullWork:L}),L&&(this.selectorWorkShapes.forEach((b,N)=>{var x;(x=b.selectIds)!=null&&x.includes(M)&&(this.willRunEffectSelectorIds.add(N),this.runEffect())}),(l=v.node)==null||l.clearTmpPoints(),this.workShapes.delete(M)),v.isEnableCursor?e.set(M,{workState:C?v.ops?s.EvevtWorkState.Done:s.EvevtWorkState.Doing:s.EvevtWorkState.Start,op:v.animationWorkData.slice(-3,-1)}):L&&!v.useAnimation&&((h=v.updateNodeOpt)!=null&&h.useAnimation)&&e.set(M,{workState:s.EvevtWorkState.Done,op:v.animationWorkData.slice(-3,-1),uid:(p=v.updateNodeOpt)==null?void 0:p.uid}),v.animationWorkData.length=0}break}case s.EToolsKey.Pencil:{if(v.useAnimation){if(v.useAnimation){if(v.isDel){(P=v.node)==null||P.clearTmpPoints(),this.workShapes.delete(M);break}const L=3,C=this.computNextAnimationIndex(v,L),b=v.isDiff?0:Math.max(0,(v.animationIndex||0)-L),N=(v.animationWorkData||[]).slice(b,C),x=(S=(T=v.node)==null?void 0:T.getWorkId())==null?void 0:S.toString();if((v.animationIndex||0)<C||v.isDiff){if((E=v.node)==null||E.consumeService({op:N,isFullWork:!1}),v.animationIndex=C,v.isDiff&&(v.isDiff=!1),N.length&&v.isEnableCursor){const O=N.slice(-3,-1);e.set(M,{workState:b===0?s.EvevtWorkState.Start:C===((I=v.animationWorkData)==null?void 0:I.length)?s.EvevtWorkState.Done:s.EvevtWorkState.Doing,op:O})}}else v.ops&&((R=v.node)==null||R.consumeService({op:v.animationWorkData||[],isFullWork:!0,replaceId:x}),v.isDel=!0,v.isEnableCursor&&e.set(M,{workState:s.EvevtWorkState.Done,op:N.slice(-3,-1)}));r=!0;break}}else{const L=!!v.ops;if((d=v.node)==null||d.consumeService({op:v.animationWorkData||[],isFullWork:L,replaceId:M}),(u=v.node)==null||u.updataOptService(v.updateNodeOpt),L){if(!v.isEnableCursor&&((f=v.updateNodeOpt)!=null&&f.useAnimation)&&((y=v.animationWorkData)!=null&&y.length)){const C=v.animationWorkData.slice(-3,-1);e.set(M,{workState:s.EvevtWorkState.Done,op:C,uid:(m=v.updateNodeOpt)==null?void 0:m.uid})}this.selectorWorkShapes.forEach((C,b)=>{var N;(N=C.selectIds)!=null&&N.includes(M)&&(this.willRunEffectSelectorIds.add(b),this.runEffect())}),(w=v.node)==null||w.clearTmpPoints(),this.workShapes.delete(M)}}break}}if(r&&this.runAnimation(),e.size){const M=[];e.forEach((v,L)=>{M.push({type:s.EPostMessageType.Cursor,uid:v.uid||L.split(s.Storage_Splitter)[0],op:v.op,workState:v.workState,viewId:this.thread.viewId})}),this.thread.post({sp:M})}}runAnimation(){this.animationId||(this.animationId=requestAnimationFrame(this.animationDraw.bind(this)))}hasDiffData(r,e,t){const o=r.length;if(e.length<o)return!0;switch(t){case s.EToolsKey.Pencil:{for(let i=0;i<o;i+=3)if(e[i]!==r[i]||e[i+1]!==r[i+1])return!0;break}case s.EToolsKey.LaserPen:{for(let i=0;i<o;i+=2)if(e[i]!==r[i]||e[i+1]!==r[i+1])return!0;break}}return!1}activeWorkShape(r){var y,m,w,P;const{workId:e,opt:t,toolsType:o,type:i,updateNodeOpt:n,ops:a,op:c,useAnimation:l,imageBitmap:h,isEnableCursor:p}=r;if(!e)return;const d=e.toString(),u=(y=this.vNodes.get(d))==null?void 0:y.rect;if(!((m=this.workShapes)!=null&&m.has(d))){let T={toolsType:o,animationWorkData:c||[],animationIndex:0,type:i,updateNodeOpt:n,ops:a,useAnimation:typeof l<"u"?l:typeof(n==null?void 0:n.useAnimation)<"u"?n==null?void 0:n.useAnimation:!0,oldRect:u,isDiff:!1,imageBitmap:h,isEnableCursor:p};o&&t&&(T=this.setNodeKey(d,T,o,t)),(w=this.workShapes)==null||w.set(d,T)}const f=(P=this.workShapes)==null?void 0:P.get(d);f.isEnableCursor=p,i&&(f.type=i),a&&(f.animationWorkData=s.transformToNormalData(a),f.ops=a),n&&(f.updateNodeOpt=n),c&&(f.isDiff=this.hasDiffData(f.animationWorkData||[],c,f.toolsType),f.animationWorkData=c),f.node&&f.node.getWorkId()!==d&&f.node.setWorkId(d),u&&(f.oldRect=u),o&&t&&(t.syncUnitTime&&(this.syncUnitTime=t.syncUnitTime),f.toolsType!==o&&o&&t&&this.setNodeKey(d,f,o,t),f.node&&f.node.setWorkOptions(t)),h&&(f.imageBitmap=h)}removeNode(r,e){r.indexOf(s.Storage_Selector_key)>-1&&this.removeSelectWork(e),this.thread.fullLayer.getElementsByName(r).forEach(o=>{o.remove(),B(o,this.thread.fullLayer.parent)}),this.thread.serviceLayer.getElementsByName(r).forEach(o=>{o.remove(),B(o,this.thread.serviceLayer.parent)});const t=this.vNodes.get(r);t&&t.toolsType===s.EToolsKey.BackgroundSVG&&this.thread.post({sp:[{type:s.EPostMessageType.BackgroundSVGDelete,toolsType:s.EToolsKey.BackgroundSVG,workId:r,dataType:s.EDataType.Service,viewId:this.thread.viewId}]}),this.vNodes.delete(r)}removeSelectWork(r){const{workId:e}=r,t=e==null?void 0:e.toString();t&&(this.activeSelectorShape(r),this.willRunEffectSelectorIds.add(t)),this.runEffect()}activeSelectorShape(r){var l,h,p;const{workId:e,opt:t,toolsType:o,type:i,selectIds:n}=r;if(!e)return;const a=e.toString();if(!((l=this.selectorWorkShapes)!=null&&l.has(a))){let d={toolsType:o,selectIds:n,type:i,opt:t};o&&t&&(d=this.setNodeKey(a,d,o,t)),(h=this.selectorWorkShapes)==null||h.set(a,d)}const c=(p=this.selectorWorkShapes)==null?void 0:p.get(a);i&&(c.type=i),c.node&&c.node.getWorkId()!==a&&c.node.setWorkId(a),c.selectIds=n||[]}setNodeKey(r,e,t,o){return e.toolsType=t,e.node=ie({toolsType:t,toolsOpt:o,vNodes:this.vNodes,fullLayer:this.thread.fullLayer,drawLayer:this.thread.serviceLayer,workId:r},this),e}}class G{constructor(){g(this,"localWork");g(this,"serviceWork");g(this,"threadEngine")}registerMainThread(r){return this.threadEngine=r,this.localWork=r.localWork,this.serviceWork=r.serviceWork,this}}class Yt extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.CopyNode)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.FullWork&&o===s.EDataType.Local&&i===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var o;const{workId:t}=e;t&&await((o=this.localWork)==null?void 0:o.consumeFull(e))}}class $t extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.SetColorNode)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var h;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,textUpdateForWoker:c,needUndoTicker:l}=e;t===s.Storage_Selector_key&&o&&await((h=this.localWork)==null?void 0:h.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,textUpdateForWoker:c,callback:this.updateSelectorCallback,needUndoTicker:l}))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i}=e,{willSyncService:n,isSync:a,textUpdateForWoker:c,needUndoTicker:l}=t,h=o.sp||[];if(n)for(const[p,d]of i.entries())c&&d.toolsType===s.EToolsKey.Text?h.push({...d,workId:p,type:s.EPostMessageType.TextUpdate,dataType:s.EDataType.Local,willSyncService:!0}):h.push({...d,workId:p,type:s.EPostMessageType.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:a});return l&&h.push({type:s.EPostMessageType.None,needUndoTicker:l}),{sp:h}}}class Ht extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.ZIndexNode)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var c;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a}=e;t===s.Storage_Selector_key&&o&&await((c=this.localWork)==null?void 0:c.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,callback:this.updateSelectorCallback}))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i}=e,{willSyncService:n,isSync:a}=t,c=o.sp||[];if(n&&c)for(const[l,h]of i.entries())h.toolsType===s.EToolsKey.BackgroundSVG&&c.push({...h,workId:l,type:s.EPostMessageType.BackgroundSVGUpdate,dataType:s.EDataType.Local,updateNodeOpt:{useAnimation:!1}}),c.push({...h,workId:l,type:s.EPostMessageType.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:a});return{sp:c}}}class qt extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.TranslateNode)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return await this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var d,u;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,textUpdateForWoker:c,emitEventType:l,smoothSync:h,needUndoTicker:p}=e;t===s.Storage_Selector_key&&o&&(o.workState===s.EvevtWorkState.Done&&(o!=null&&o.translate)&&(o.translate[0]||o.translate[1])||o.workState!==s.EvevtWorkState.Done?await((d=this.localWork)==null?void 0:d.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,isSync:!0,textUpdateForWoker:c,emitEventType:l,callback:this.updateSelectorCallback,smoothSync:h,needUndoTicker:p})):o.workState===s.EvevtWorkState.Done&&((u=this.localWork)==null||u.vNodes.deleteLastTarget()))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i,workShapeNode:n,res:a,smoothSync:c}=e,{willSyncService:l,isSync:h,updateSelectorOpt:p,textUpdateForWoker:d,needUndoTicker:u}=t,f=p.workState,y=o.sp||[];if(f===s.EvevtWorkState.Start)return{sp:[],render:[]};const m=a==null?void 0:a.selectRect;if(l){y.push({type:s.EPostMessageType.Select,selectIds:n.selectIds,selectRect:m,willSyncService:f===s.EvevtWorkState.Done?!0:c,isSync:!0,points:f===s.EvevtWorkState.Done&&n.getChildrenPoints()||void 0,textOpt:n.textOpt});const w={useAnimation:p.useAnimation||!1};p.uid&&(w.uid=p.uid);for(const[P,T]of i.entries())T.toolsType===s.EToolsKey.BackgroundSVG&&y.push({...T,workId:P,type:s.EPostMessageType.BackgroundSVGUpdate,dataType:s.EDataType.Local,willSyncService:f===s.EvevtWorkState.Done?!0:c,updateNodeOpt:w}),d&&T.toolsType===s.EToolsKey.Text?y.push({...T,workId:P,type:s.EPostMessageType.TextUpdate,dataType:s.EDataType.Local,willSyncService:f===s.EvevtWorkState.Done?!0:c,updateNodeOpt:w}):(c||f===s.EvevtWorkState.Done)&&y.push({...T,workId:P,type:s.EPostMessageType.UpdateNode,updateNodeOpt:w,isSync:h})}return u&&y.push({type:s.EPostMessageType.None,needUndoTicker:u}),{sp:y}}}class jt extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.ScaleNode)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return await this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var l;const{workId:t,updateNodeOpt:o,willSyncService:i,willSerializeData:n,smoothSync:a,needUndoTicker:c}=e;t===s.Storage_Selector_key&&o&&await((l=this.localWork)==null?void 0:l.updateSelector({updateSelectorOpt:o,willSyncService:i,willSerializeData:n,isSync:!0,callback:this.updateSelectorCallback.bind(this),smoothSync:a,needUndoTicker:c}))}updateSelectorCallback(e){const{param:t,postData:o,workShapeNode:i,res:n,newServiceStore:a,smoothSync:c}=e,{updateSelectorOpt:l,willSyncService:h,needUndoTicker:p}=t,d=l.workState,u=o.sp||[],f=n==null?void 0:n.selectRect;if(d===s.EvevtWorkState.Start)return{sp:[],render:[]};if(h){u.push({type:s.EPostMessageType.Select,selectIds:i.selectIds,selectRect:f,willSyncService:d===s.EvevtWorkState.Done?!0:c,isSync:!0,points:d===s.EvevtWorkState.Done&&i.getChildrenPoints()||void 0,textOpt:i.textOpt});const y={useAnimation:l.useAnimation||!1};l.uid&&(y.uid=l.uid);for(const[m,w]of a.entries())w.toolsType===s.EToolsKey.BackgroundSVG&&u.push({...w,workId:m,type:s.EPostMessageType.BackgroundSVGUpdate,dataType:s.EDataType.Local,willSyncService:d===s.EvevtWorkState.Done?!0:c,updateNodeOpt:y}),w.toolsType===s.EToolsKey.Text?u.push({...w,workId:m,type:s.EPostMessageType.TextUpdate,dataType:s.EDataType.Local,willSyncService:d===s.EvevtWorkState.Done?!0:c,updateNodeOpt:y}):(c||d===s.EvevtWorkState.Done)&&u.push({...w,workId:m,type:s.EPostMessageType.UpdateNode,updateNodeOpt:y,isSync:!0})}return p&&u.push({type:s.EPostMessageType.None,needUndoTicker:p}),{sp:u}}}class Zt extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.RotateNode)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return await this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var p;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,emitEventType:c,smoothSync:l,needUndoTicker:h}=e;t===s.Storage_Selector_key&&o&&await((p=this.localWork)==null?void 0:p.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,emitEventType:c,isSync:!0,callback:this.updateSelectorCallback,smoothSync:l,needUndoTicker:h}))}updateSelectorCallback(e){const{param:t,postData:o,workShapeNode:i,res:n,newServiceStore:a,smoothSync:c}=e,{updateSelectorOpt:l,willSyncService:h,willSerializeData:p,isSync:d,needUndoTicker:u}=t,f=l.workState,y=o.sp||[],m=n==null?void 0:n.selectRect;if(h){p&&f===s.EvevtWorkState.Done&&y.push({type:s.EPostMessageType.Select,selectIds:i.selectIds,selectRect:m,willSyncService:f===s.EvevtWorkState.Done?!0:c,isSync:d,points:i.getChildrenPoints()});const w={useAnimation:l.useAnimation||!1};if(l.uid&&(w.uid=l.uid),c||f===s.EvevtWorkState.Done)for(const[P,T]of a.entries())y.push({...T,workId:P,type:s.EPostMessageType.UpdateNode,updateNodeOpt:w,isSync:d})}return u&&y.push({type:s.EPostMessageType.None,needUndoTicker:u}),{sp:y}}}class Qt extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.SetFontStyle)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return await this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var l;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,textUpdateForWoker:c}=e;t===s.Storage_Selector_key&&o&&await((l=this.localWork)==null?void 0:l.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,textUpdateForWoker:c,callback:this.updateSelectorCallback}))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i,workShapeNode:n,res:a}=e,{willSyncService:c,isSync:l,updateSelectorOpt:h,textUpdateForWoker:p}=t,d=o.sp||[],u=a==null?void 0:a.selectRect;if(c&&d){h.fontSize&&d.push({type:s.EPostMessageType.Select,selectIds:n.selectIds,selectRect:u,willSyncService:c,isSync:l,points:n.getChildrenPoints()});for(const[f,y]of i.entries())p&&y.toolsType===s.EToolsKey.Text?d.push({...y,workId:f,type:s.EPostMessageType.TextUpdate,dataType:s.EDataType.Local,willSyncService:!0}):d.push({...y,workId:f,type:s.EPostMessageType.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:l})}return{sp:d}}}class Jt extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.SetPoint)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var h;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,textUpdateForWoker:c,needUndoTicker:l}=e;t===s.Storage_Selector_key&&o&&await((h=this.localWork)==null?void 0:h.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,emitEventType:this.emitEventType,willSerializeData:a,isSync:!0,textUpdateForWoker:c,callback:this.updateSelectorCallback,needUndoTicker:l}))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i,workShapeNode:n,res:a}=e,{willSyncService:c,isSync:l,needUndoTicker:h}=t,p=o.sp||[],d=a==null?void 0:a.selectRect;if(c&&p){for(const[u,f]of i.entries())p.push({...f,workId:u,type:s.EPostMessageType.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:l});p.push({type:s.EPostMessageType.Select,selectIds:n.selectIds,selectRect:d,willSyncService:c,isSync:l,points:n.getChildrenPoints()})}return h&&p.push({type:s.EPostMessageType.None,needUndoTicker:h}),{sp:p}}}class es extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.SetLock)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var l;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,needUndoTicker:c}=e;t===s.Storage_Selector_key&&o&&await((l=this.localWork)==null?void 0:l.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,callback:this.updateSelectorCallback,needUndoTicker:c}))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i,workShapeNode:n,res:a}=e,{willSyncService:c,isSync:l,updateSelectorOpt:h,needUndoTicker:p}=t,d=o.sp||[],u=a==null?void 0:a.selectRect;if(c&&d){for(const[f,y]of i.entries())d.push({...y,workId:f,type:s.EPostMessageType.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:l});d.push({isLocked:h.isLocked,selectorColor:n.selectorColor,scaleType:n.scaleType,canRotate:n.canRotate,type:s.EPostMessageType.Select,selectIds:n.selectIds,selectRect:u,willSyncService:c,isSync:l})}return p&&d.push({type:s.EPostMessageType.None,needUndoTicker:p}),{sp:d}}}class ts extends G{constructor(){super(...arguments);g(this,"emitEventType",s.EmitEventType.SetShapeOpt)}async consume(e){const{msgType:t,dataType:o,emitEventType:i}=e;if(t===s.EPostMessageType.UpdateNode&&o===s.EDataType.Local&&i===this.emitEventType)return this.consumeForLocalWorker(e),!0}async consumeForLocalWorker(e){var l;const{workId:t,updateNodeOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,needUndoTicker:c}=e;t===s.Storage_Selector_key&&o&&await((l=this.localWork)==null?void 0:l.updateSelector({updateSelectorOpt:o,willRefreshSelector:i,willSyncService:n,willSerializeData:a,callback:this.updateSelectorCallback,needUndoTicker:c}))}updateSelectorCallback(e){const{param:t,postData:o,newServiceStore:i}=e,{willSyncService:n,isSync:a,needUndoTicker:c}=t,l=o.sp||[];if(n&&l)for(const[h,p]of i.entries())l.push({...p,workId:h,type:s.EPostMessageType.UpdateNode,updateNodeOpt:{useAnimation:!1},isSync:a});return c&&l.push({type:s.EPostMessageType.None,needUndoTicker:c}),{sp:l}}}class ss{constructor(r){g(this,"builders",new Map);this.builders=new Map(r.map(e=>[e,this.build(e)]))}build(r){switch(r){case s.EmitEventType.TranslateNode:return new qt;case s.EmitEventType.ZIndexNode:return new Ht;case s.EmitEventType.CopyNode:return new Yt;case s.EmitEventType.SetColorNode:return new $t;case s.EmitEventType.ScaleNode:return new jt;case s.EmitEventType.RotateNode:return new Zt;case s.EmitEventType.SetFontStyle:return new Qt;case s.EmitEventType.SetPoint:return new Jt;case s.EmitEventType.SetLock:return new es;case s.EmitEventType.SetShapeOpt:return new ts}}registerForMainThread(r){return this.builders.forEach(e=>{e&&e.registerMainThread(r)}),this}async consumeForMainThread(r){for(const e of this.builders.values())if(await(e==null?void 0:e.consume(r)))return!0;return!1}}class os{constructor(r,e){g(this,"viewId");g(this,"fullLayer");g(this,"topLayer");g(this,"localLayer");g(this,"serviceLayer");g(this,"snapshotFullLayer");g(this,"vNodes");g(this,"master");g(this,"opt");g(this,"cameraOpt");g(this,"scene");g(this,"localWork");g(this,"serviceWork");g(this,"topWork");g(this,"taskUpdateCameraId");g(this,"debounceUpdateCameraId");g(this,"debounceUpdateCache",new Set);g(this,"mainThreadPostId");g(this,"combinePostMsg",new Set);g(this,"methodBuilder");g(this,"cacheImages",new Map);g(this,"imageResolveMap",new Map);this.viewId=r,this.opt=e,this.scene=this.createScene({...e.canvasOpt,container:e.container}),this.master=e.master;const t=s.DefaultAppliancePluginExtrasOptions.bufferSize.full,o=s.DefaultAppliancePluginExtrasOptions.bufferSize.sub;this.fullLayer=this.createLayer("fullLayer",this.scene,{...e.layerOpt,bufferSize:this.viewId===s.Main_View_Id?t:o*2}),this.topLayer=this.createLayer("topLayer",this.scene,{...e.layerOpt,bufferSize:(this.viewId===s.Main_View_Id,o),contextType:"2d"}),this.localLayer=this.createLayer("localLayer",this.scene,{...e.layerOpt,bufferSize:(this.viewId===s.Main_View_Id,o),contextType:"2d"}),this.serviceLayer=this.createLayer("serviceLayer",this.scene,{...e.layerOpt,bufferSize:(this.viewId===s.Main_View_Id,o),contextType:"2d"}),this.vNodes=new je(r,this.scene);const i={thread:this,vNodes:this.vNodes};this.localWork=new Gt(i),this.serviceWork=new Xt(i),this.topWork=new _t(i),this.vNodes.init(this.fullLayer),this.methodBuilder=new ss([s.EmitEventType.CopyNode,s.EmitEventType.SetColorNode,s.EmitEventType.DeleteNode,s.EmitEventType.RotateNode,s.EmitEventType.ScaleNode,s.EmitEventType.TranslateNode,s.EmitEventType.ZIndexNode,s.EmitEventType.SetFontStyle,s.EmitEventType.SetPoint,s.EmitEventType.SetLock,s.EmitEventType.SetShapeOpt]).registerForMainThread(this)}getCachedImages(r){var e;return(e=this.cacheImages.get(r))==null?void 0:e.imageBitmap}getCachedImagesByWorkId(r){for(const[e,t]of this.cacheImages.entries())if(e===r&&t.imageBitmap)return t.imageBitmap}deleteCachedImagesByWorkId(r){for(const[e,t]of this.cacheImages.entries())t.workId===r&&(t.imageBitmap.close(),this.cacheImages.delete(e))}clearCacheImages(){this.cacheImages.forEach(r=>r.imageBitmap.close()),this.cacheImages.clear()}clearImageResolveMap(){this.imageResolveMap.forEach(({timer:r})=>{r&&clearTimeout(r)}),this.imageResolveMap.clear()}post(r){this.combinePostMsg.add(r),this.runBatchPostData()}updateDpr(r){this.scene.displayRatio=r}async on(r){if(!await this.methodBuilder.consumeForMainThread(r)){const{msgType:e,toolsType:t,opt:o,dataType:i,workId:n,workState:a,imageSrc:c,imageBitmap:l,workIds:h,isLockSentEventCursor:p}=r,d=n==null?void 0:n.toString();switch(e){case s.EPostMessageType.UpdateDpr:$.isNumber(r.dpr)&&this.updateDpr(r.dpr);break;case s.EPostMessageType.AuthClear:{const{clearUids:u,localUid:f}=r;this.vNodes.setCanClearUids(u),this.vNodes.setLocalUid(f);break}case s.EPostMessageType.Destroy:this.destroy();break;case s.EPostMessageType.Clear:this.clearAll();break;case s.EPostMessageType.UpdateCamera:await this.updateCamera(r);break;case s.EPostMessageType.UpdateTools:if(t&&o){const u={toolsType:t,toolsOpt:o};this.topWork.canUseTopLayer(t)?this.topWork.setToolsOpt(u):this.localWork.setToolsOpt(u)}break;case s.EPostMessageType.CreateWork:if(d&&o&&t){if(this.topWork.canUseTopLayer(t)){this.topWork.getToolsOpt()||this.topWork.setToolsOpt({toolsType:t,toolsOpt:o}),this.topWork.setWorkOptions(d,o);break}this.localWork.getToolsOpt()||this.localWork.setToolsOpt({toolsType:t,toolsOpt:o}),this.localWork.setWorkOptions(d,o)}break;case s.EPostMessageType.DrawWork:a===s.EvevtWorkState.Done&&i===s.EDataType.Local?(this.consumeDrawAll(i,r),t===s.EToolsKey.LaserPen&&p&&this.post({sp:[{type:s.EPostMessageType.None,isLockSentEventCursor:p}]})):this.consumeDraw(i,r);break;case s.EPostMessageType.UpdateNode:case s.EPostMessageType.FullWork:if(t&&this.topWork.canUseTopLayer(t)){this.consumeDrawAll(i,r);break}this.consumeFull(i,r);break;case s.EPostMessageType.RemoveNode:await this.removeNode(r);return;case s.EPostMessageType.Select:i===s.EDataType.Service&&(n===s.Storage_Selector_key?this.localWork.updateFullSelectWork(r):this.serviceWork.runSelectWork(r));break;case s.EPostMessageType.CursorBlur:this.localWork.cursorBlur();return;case s.EPostMessageType.CursorHover:this.localWork.cursorHover(r);break;case s.EPostMessageType.GetTextActive:i===s.EDataType.Local&&this.localWork.checkTextActive(r);break;case s.EPostMessageType.GetImageBitMap:if(c&&l&&n){const u=n.toString();this.deleteCachedImagesByWorkId(u),this.cacheImages.set(c,{imageBitmap:l,workId:u});const f=this.imageResolveMap.get(c);if(f){const{resolve:y,timer:m}=f;m&&clearTimeout(m),y&&y(c)}}break;case s.EPostMessageType.GetVNodeInfo:if(n&&h){const u=h.map(f=>this.vNodes.get(f));this.post({sp:[{type:s.EPostMessageType.GetVNodeInfo,dataType:s.EDataType.Local,workId:n,vInfo:u}]})}break}}}getIconSize(r,e,t){const o=r*t,i=e*t;return o<=50||i<=50?[50,50]:o<=100||i<=100?[100,100]:o<=200||i<=200?[200,200]:o<=400||i<=400?[400,400]:o<=800||i<=800?[800,800]:[1600,1600]}async loadImageBitMap(r){const{toolsType:e,opt:t,workId:o}=r;if(e===s.EToolsKey.Image&&t&&o){const i=o.toString(),{src:n,type:a,width:c,height:l,strokeColor:h}=t;if(!n||!a||!c||!l)return;let p=n;if(a===s.EImageType.Iconify){const[y,m]=this.getIconSize(c,l,this.opt.displayer.dpr);p=`${n}?width=${y}&height=${m}&color=${h}`}if(this.cacheImages.has(p)){const y=this.getCachedImages(p);if(y)return y}if(this.imageResolveMap.has(p)){const y=this.getCachedImagesByWorkId(i);if(y)return y}const f=await new Promise(y=>{const m=this.imageResolveMap.get(p)||{resolve:void 0,timer:void 0};m.timer&&clearTimeout(m.timer),m.resolve=y,m.timer=setTimeout(()=>{const w=this.imageResolveMap.get(p);w!=null&&w.resolve&&w.resolve(p)},5e3),this.imageResolveMap.set(p,m),this.opt.post({sp:[{imageSrc:p,workId:i,viewId:this.viewId,isgl:!!this.fullLayer.parent.gl,isSubWorker:!1,type:s.EPostMessageType.GetImageBitMap}]})});return this.imageResolveMap.delete(f),this.getCachedImages(p)}}async removeNode(r){const{dataType:e,workId:t,removeIds:o}=r,i=o||[];if(t&&i.push(t.toString()),i.length)for(const n of i){if(n===s.Storage_Selector_key){await this.localWork.removeSelector(r);continue}e===s.EDataType.Local?this.localWork.removeWork(r):e===s.EDataType.Service&&this.serviceWork.removeWork(r),await this.localWork.colloctEffectSelectWork(r)}}async consumeFull(r,e){const t=await this.localWork.colloctEffectSelectWork(e);t&&r===s.EDataType.Local&&await this.localWork.consumeFull(t),t&&r===s.EDataType.Service&&this.serviceWork.consumeFull(t)}setCameraOpt(r){this.cameraOpt=r;const{scale:e,centerX:t,centerY:o,width:i,height:n}=r;(i!==this.scene.width||n!==this.scene.height)&&this.updateScene({width:i,height:n}),this.fullLayer.setAttribute("scale",[e,e]),this.fullLayer.setAttribute("translate",[-t,-o]),this.topLayer.setAttribute("scale",[e,e]),this.topLayer.setAttribute("translate",[-t,-o]),this.localLayer.setAttribute("scale",[e,e]),this.localLayer.setAttribute("translate",[-t,-o]),this.serviceLayer.setAttribute("scale",[e,e]),this.serviceLayer.setAttribute("translate",[-t,-o])}runBatchPostData(){this.mainThreadPostId||(this.mainThreadPostId=requestAnimationFrame(this.combinePost.bind(this)))}combinePostData(){var o;this.mainThreadPostId=void 0;const r=[];let e,t;for(const i of this.combinePostMsg.values()){if((o=i.sp)!=null&&o.length)for(const n of i.sp){let a=!1;for(const c of r)if($.isEqual(n,c)){a=!0;break}a||r.push(n)}$.isNumber(i.fullWorkerDrawCount)&&(e=i.fullWorkerDrawCount),$.isNumber(i.consumeCount)&&(t=i.consumeCount)}return this.combinePostMsg.clear(),{sp:r,fullWorkerDrawCount:e,consumeCount:t}}combinePost(){var t,o;const r=this.combinePostData(),e=(t=r.sp)==null?void 0:t.filter(i=>i.type!==s.EPostMessageType.None||i.isLockSentEventCursor||i.needUndoTicker);e!=null&&e.length?r.sp=e.map(i=>i.viewId?i:{...i,viewId:this.viewId}):delete r.sp,r.consumeCount===void 0&&delete r.consumeCount,r.fullWorkerDrawCount===void 0&&delete r.fullWorkerDrawCount,(r!=null&&r.consumeCount||r!=null&&r.fullWorkerDrawCount||(o=r.sp)!=null&&o.length)&&this.opt.post(r)}clearAll(){this.fullLayer.children.length&&(this.fullLayer.parent.children.forEach(r=>{r.name!=="viewport"&&r.remove()}),U(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:s.EPostMessageType.Clear}]})}consumeDrawAll(r,e){const{toolsType:t,workId:o}=e;if(o){const i=o.toString();if(t&&this.topWork.canUseTopLayer(t)){r===s.EDataType.Local&&(this.topWork.getLocalWorkShape(o.toString())||this.topWork.createLocalWork(e)),this.topWork.consumeDrawAll(e);return}r===s.EDataType.Local&&(this.localWork.getWorkShape(i)||this.localWork.createLocalWork(e),this.localWork.consumeDrawAll(e,this.serviceWork))}}consumeDraw(r,e){const{opt:t,workId:o,toolsType:i}=e;if(o&&i&&t){const n=o.toString();if(this.topWork.canUseTopLayer(i)){r===s.EDataType.Local&&(this.topWork.getLocalWorkShape(n)||this.topWork.createLocalWork(e)),this.topWork.consumeDraw(e);return}r===s.EDataType.Local?(this.localWork.getWorkShape(n)||this.localWork.createLocalWork(e),this.localWork.consumeDraw(e,this.serviceWork)):r===s.EDataType.Service&&this.serviceWork.consumeDraw(e);return}}async updateCamera(r){var o;const{cameraOpt:e,scenePath:t}=r;if(e&&!$.isEqual(this.cameraOpt,e)){if(this.taskUpdateCameraId&&(clearTimeout(this.taskUpdateCameraId),this.taskUpdateCameraId=void 0),t){let l=!1;for(const[h,p]of this.localWork.getWorkShapes().entries())switch(p.toolsType){case s.EToolsKey.Text:case s.EToolsKey.BitMapEraser:case s.EToolsKey.PencilEraser:case s.EToolsKey.Eraser:case s.EToolsKey.Selector:case s.EToolsKey.LaserPen:break;default:h!==s.Cursor_Hover_Id&&h!==s.Storage_Selector_key&&(l=!0);break}if(l){this.taskUpdateCameraId=setTimeout(()=>{this.taskUpdateCameraId=void 0,this.updateCamera(r)},s.Task_Time_Interval);return}}const i=new Map;for(const[l,h]of this.vNodes.getNodesByType(s.EToolsKey.Text).entries()){const p=h.rect;i.set(l,$.cloneDeep(p))}const n=new Set(i.keys());let a=!1;if(this.localWork.hasSelector()){const l=(o=this.localWork.getSelector())==null?void 0:o.selectIds;if(l){a=!0;for(const h of l)n.add(h)}}let c=!1;if(this.serviceWork.selectorWorkShapes.size)for(const l of this.serviceWork.selectorWorkShapes.values()){const h=l.selectIds;if(h){c=!0;for(const p of h)n.add(p)}}if(this.setCameraOpt(e),this.vNodes.curNodeMap.size){this.vNodes.clearTarget(),this.vNodes.updateHighLevelNodesRect(n),this.debounceUpdateCameraId&&clearTimeout(this.debounceUpdateCameraId);for(const[l,h]of i.entries()){const p=this.vNodes.get(l);if(p){const d=h,u=p.rect,f=this.getSceneRect(),y=s.getRectMatrixrRelation(d,f),m=s.getRectMatrixrRelation(u,f);let w=!1;if((y!==m||d.w!==u.w||d.h!==u.h||m===s.EMatrixrRelationType.intersect)&&(w=!0),w){const{toolsType:P}=p;P===s.EToolsKey.Text&&this.debounceUpdateCache.add(l)}}}if(a&&this.localWork.reRenderSelector(),c)for(const[l,h]of this.serviceWork.selectorWorkShapes.entries())this.serviceWork.runSelectWork({workId:l,selectIds:h.selectIds,msgType:s.EPostMessageType.Select,dataType:s.EDataType.Service,viewId:this.viewId});this.debounceUpdateCameraId=setTimeout(()=>{var h;this.debounceUpdateCameraId=void 0;const l=[];for(const p of this.debounceUpdateCache.values()){if((h=this.fullLayer)==null?void 0:h.getElementsByName(p)[0]){const u=this.vNodes.get(p);if(u){const{toolsType:f,opt:y}=u,m=this.localWork.setFullWork({toolsType:f,opt:y,workId:p});if(m){const w=this.getSceneRect();l.push(m.consumeServiceAsync({isFullWork:!0,replaceId:p,boxRect:w}))}}}this.debounceUpdateCache.delete(p)}this.vNodes.updateLowLevelNodesRect(),this.vNodes.clearHighLevelIds()},s.Task_Time_Interval)}}}getSceneRect(){const{width:r,height:e}=this.scene;return{x:0,y:0,w:Math.floor(r),h:Math.floor(e)}}createScene(r){return new D.Scene({displayRatio:this.opt.displayer.dpr,depth:!1,desynchronized:!0,...r,autoRender:!0,id:this.viewId,contextType:"2d"})}createLayer(r,e,t){const{width:o,height:i}=t,n=`canvas-${r}`,a=e.layer(n,{...t,offscreen:!1}),c=new D.Group({anchor:[.5,.5],pos:[o*.5,i*.5],size:[o,i],name:"viewport",id:r});return a.append(c),c}updateScene(r){this.scene.attr({...r});const{width:e,height:t}=r;this.scene.width=e,this.scene.height=t,this.updateLayer({width:e,height:t})}updateLayer(r){const{width:e,height:t}=r;this.fullLayer.parent.setAttribute("width",e),this.fullLayer.parent.setAttribute("height",t),this.fullLayer.setAttribute("size",[e,t]),this.fullLayer.setAttribute("pos",[e*.5,t*.5]),this.topLayer.parent.setAttribute("width",e),this.topLayer.parent.setAttribute("height",t),this.topLayer.setAttribute("size",[e,t]),this.topLayer.setAttribute("pos",[e*.5,t*.5]),this.localLayer.parent.setAttribute("width",e),this.localLayer.parent.setAttribute("height",t),this.localLayer.setAttribute("size",[e,t]),this.localLayer.setAttribute("pos",[e*.5,t*.5]),this.serviceLayer.parent.setAttribute("width",e),this.serviceLayer.parent.setAttribute("height",t),this.serviceLayer.setAttribute("size",[e,t]),this.serviceLayer.setAttribute("pos",[e*.5,t*.5])}destroy(){this.clearCacheImages(),this.clearImageResolveMap(),this.vNodes.clear(),this.fullLayer.remove(),B(this.fullLayer,this.fullLayer.parent),this.topLayer.remove(),B(this.topLayer,this.topLayer.parent),this.localLayer.remove(),B(this.localLayer,this.localLayer.parent),this.serviceLayer.remove(),B(this.serviceLayer,this.serviceLayer.parent),this.scene.remove(),this.localWork.destroy(),this.serviceWork.destroy(),this.topWork.destroy()}}class is{constructor(r,e){g(this,"viewId");g(this,"fullLayer");g(this,"master");g(this,"opt");g(this,"scene");g(this,"mainThreadPostId");g(this,"combinePostMsg",new Set);g(this,"workShapes",new Map);g(this,"cacheImages",new Map);g(this,"imageResolveMap",new Map);this.viewId=r,this.opt=e,this.scene=this.createScene({...e.canvasOpt,container:e.container}),this.master=e.master,this.fullLayer=this.createLayer("fullLayer",this.scene,{...e.layerOpt,bufferSize:this.viewId===s.Main_View_Id?6e3:3e3,contextType:"2d"})}getCachedImages(r){var e;return(e=this.cacheImages.get(r))==null?void 0:e.imageBitmap}getCachedImagesByWorkId(r){for(const[e,t]of this.cacheImages.entries())if(e===r&&t.imageBitmap)return t.imageBitmap}deleteCachedImagesByWorkId(r){for(const[e,t]of this.cacheImages.entries())t.workId===r&&(t.imageBitmap.close(),this.cacheImages.delete(e))}clearCacheImages(){this.cacheImages.forEach(r=>r.imageBitmap.close()),this.cacheImages.clear()}clearImageResolveMap(){this.imageResolveMap.forEach(({timer:r})=>{r&&clearTimeout(r)}),this.imageResolveMap.clear()}post(r){this.combinePostMsg.add(r),this.runBatchPostData()}async on(r){const{msgType:e,imageSrc:t,imageBitmap:o,workId:i}=r;switch(e){case s.EPostMessageType.Snapshot:{await this.getSnapshot(r),this.destroy();return}case s.EPostMessageType.BoundingBox:{await this.getBoundingRect(r),this.destroy();return}case s.EPostMessageType.GetImageBitMap:{if(t&&o&&i){const n=i.toString();this.deleteCachedImagesByWorkId(n),this.cacheImages.set(t,{imageBitmap:o,workId:n});const a=this.imageResolveMap.get(t);if(a){const{resolve:c,timer:l}=a;l&&clearTimeout(l),c&&c(t)}}break}}}getIconSize(r,e,t){const o=r*t,i=e*t;return o<=50||i<=50?[50,50]:o<=100||i<=100?[100,100]:o<=200||i<=200?[200,200]:o<=400||i<=400?[400,400]:o<=800||i<=800?[800,800]:[1600,1600]}async loadImageBitMap(r){const{toolsType:e,opt:t,workId:o}=r;if(e===s.EToolsKey.Image&&t&&o){const i=o.toString(),{src:n,type:a,width:c,height:l,strokeColor:h}=t;if(!n||!a||!c||!l)return;let p=n;if(a===s.EImageType.Iconify){const[y,m]=this.getIconSize(c,l,this.opt.displayer.dpr);p=`${n}?width=${y}&height=${m}&color=${h}`}if(this.cacheImages.has(p)){const y=this.getCachedImages(p);if(y)return y}if(this.imageResolveMap.has(p)){const y=this.getCachedImagesByWorkId(i);if(y)return y}const f=await new Promise(y=>{const m=this.imageResolveMap.get(p)||{resolve:void 0,timer:void 0};m.timer&&clearTimeout(m.timer),m.resolve=y,m.timer=setTimeout(()=>{const w=this.imageResolveMap.get(p);w!=null&&w.resolve&&w.resolve(p)},5e3),this.imageResolveMap.set(p,m),this.opt.post({sp:[{imageSrc:p,workId:i,viewId:this.viewId,isgl:!!this.fullLayer.parent.gl,isSubWorker:!0,type:s.EPostMessageType.GetImageBitMap}]})});return this.imageResolveMap.delete(f),this.getCachedImages(p)}}createWorkShapeNode(r){return ie({...r,fullLayer:this.fullLayer,drawLayer:void 0})}setFullWork(r){const{workId:e,opt:t,toolsType:o}=r;if(e&&t&&o){const i=e.toString();let n;return e&&this.workShapes.has(i)?(n=this.workShapes.get(i),n==null||n.setWorkOptions(t)):n=this.createWorkShapeNode({toolsOpt:t,toolsType:o,workId:i}),n?(this.workShapes.set(i,n),n):void 0}}async runFullWork(r){var o;const e=this.setFullWork(r),t=r.ops&&s.transformToNormalData(r.ops);if(e){let i,n;const a=(o=e.getWorkId())==null?void 0:o.toString();return e.toolsType===s.EToolsKey.BackgroundSVG?i=e.consumeService({isFullWork:!0,replaceId:a}):e.toolsType===s.EToolsKey.Image?i=await e.consumeServiceAsync({isFullWork:!0,worker:this}):e.toolsType===s.EToolsKey.Text?i=await e.consumeServiceAsync({isFullWork:!0,replaceId:a,isDrawLabel:!0}):(i=e.consumeService({op:t,isFullWork:!0,replaceId:a}),n=(r==null?void 0:r.updateNodeOpt)&&e.updataOptService(r.updateNodeOpt)),s.computRect(i,n)}}async getSnapshot(r){const{scenePath:e,scenes:t,cameraOpt:o,w:i,h:n}=r;if(e&&t&&o){this.setCameraOpt(o);for(const[c,l]of Object.entries(t))if(l!=null&&l.type)switch(l==null?void 0:l.type){case s.EPostMessageType.UpdateNode:case s.EPostMessageType.FullWork:{const{opt:h}=l,p={...l,opt:h,workId:c,msgType:s.EPostMessageType.FullWork,dataType:s.EDataType.Service,viewId:this.viewId};await this.runFullWork(p);break}}let a;i&&n&&(a={resizeWidth:i,resizeHeight:n});try{await this.getSnapshotRender({scenePath:e,options:a})}catch(c){const l=c&&c instanceof Error?c.message:c==null?void 0:c.toString();console.error("[SnapshotThreadImpl] getSnapshotRender error",l),this.post({sp:[{type:s.EPostMessageType.ReportError,reportString:`[SnapshotThreadImpl] getSnapshotRender error: ${l}`}]})}}}getSceneRect(){const{width:r,height:e}=this.scene;return{x:0,y:0,w:Math.floor(r),h:Math.floor(e)}}getRectImageBitmap(r,e){const t=Math.floor(r.x*this.opt.displayer.dpr),o=Math.floor(r.y*this.opt.displayer.dpr),i=r.w>0&&Math.floor(r.w*this.opt.displayer.dpr||1)||1,n=r.h>0&&Math.floor(r.h*this.opt.displayer.dpr||1)||1;return createImageBitmap(this.fullLayer.parent.canvas,t,o,i,n,e)}async getSnapshotRender(r){var i;const{scenePath:e,options:t}=r;((i=this.fullLayer)==null?void 0:i.parent).render();const o=await this.getRectImageBitmap(this.getSceneRect(),t);o&&(this.post({sp:[{type:s.EPostMessageType.Snapshot,scenePath:e,imageBitmap:o,viewId:this.viewId,index:0}]}),this.fullLayer&&U(this.fullLayer,this.fullLayer.parent))}async getBoundingRect(r){const{scenePath:e,scenes:t,cameraOpt:o}=r;if(e&&t&&o){this.setCameraOpt(o);let i;for(const[n,a]of Object.entries(t))if(a!=null&&a.type)switch(a==null?void 0:a.type){case s.EPostMessageType.UpdateNode:case s.EPostMessageType.FullWork:{const c=await this.runFullWork({...a,workId:n,msgType:s.EPostMessageType.FullWork,dataType:s.EDataType.Service,viewId:this.viewId});i=s.computRect(i,c);break}}i&&this.post({sp:[{type:s.EPostMessageType.BoundingBox,scenePath:e,rect:i}]})}}setCameraOpt(r){const{scale:e,centerX:t,centerY:o,width:i,height:n}=r;this.updateScene({width:i,height:n}),this.fullLayer.setAttribute("scale",[e,e]),this.fullLayer.setAttribute("translate",[-t,-o])}runBatchPostData(){this.mainThreadPostId||(this.mainThreadPostId=requestAnimationFrame(this.combinePost.bind(this)))}combinePostData(){var e;this.mainThreadPostId=void 0;const r=[];for(const t of this.combinePostMsg.values())if((e=t.sp)!=null&&e.length)for(const o of t.sp){let i=!1;for(const n of r)if(H(o,n)){i=!0;break}i||r.push(o)}return this.combinePostMsg.clear(),{sp:r}}combinePost(){var t,o;const r=this.combinePostData(),e=(t=r.sp)==null?void 0:t.filter(i=>i.type!==s.EPostMessageType.None||i.isLockSentEventCursor||i.needUndoTicker);e!=null&&e.length?r.sp=e.map(i=>i.viewId?i:{...i,viewId:this.viewId}):delete r.sp,(o=r.sp)!=null&&o.length&&this.opt.post(r)}createScene(r){return new D.Scene({displayRatio:this.opt.displayer.dpr,depth:!1,desynchronized:!0,...r,autoRender:!1,contextType:"2d"})}createLayer(r,e,t){const{width:o,height:i}=t,n=`canvas-${r}`,a=e.layer(n,t),c=new D.Group({anchor:[.5,.5],pos:[o*.5,i*.5],size:[o,i],name:"viewport",id:r});return a.append(c),c}updateScene(r){this.scene.attr({...r});const{width:e,height:t}=r;this.scene.width=e,this.scene.height=t,this.updateLayer({width:e,height:t})}updateLayer(r){const{width:e,height:t}=r;this.fullLayer.parent.setAttribute("width",e),this.fullLayer.parent.setAttribute("height",t),this.fullLayer.setAttribute("size",[e,t]),this.fullLayer.setAttribute("pos",[e*.5,t*.5])}destroy(){this.clearCacheImages(),this.clearImageResolveMap(),this.fullLayer.remove(),B(this.fullLayer,this.fullLayer.parent),this.scene.remove()}}class rs{constructor(r){g(this,"mainThreadMap",new Map);g(this,"snapshotThread");g(this,"master");g(this,"post",r=>{const{fullWorkerDrawCount:e,sp:t,workerTasksqueueCount:o,consumeCount:i}=r;this.master.isBusy&&A(o)&&this.master.setWorkerTasksqueueCount(o),A(e)&&this.master.setMaxDrawCount(e),A(i)&&this.master.setConsumeCount(i),t&&this.master.collectorSyncData(t)});this.master=r}destroy(){this.mainThreadMap.clear()}createMainThread(r,e){return new os(r,e)}createSnapshotThread(r,e){return new is(r,e)}async consume(r){var e,t,o,i;for(const n of r.values()){const{msgType:a,viewId:c,tasksqueue:l,mainTasksqueueCount:h,layerOpt:p,offscreenCanvasOpt:d,cameraOpt:u,isSubWorker:f}=n;if(a===s.EPostMessageType.Console){console.log(this);continue}if(a===s.EPostMessageType.Init){const m=(e=this.master.control.viewContainerManager.getView(c))==null?void 0:e.displayer,w=m==null?void 0:m.canvasContainerRef.current;if(m&&w&&p&&d){const P=this.createMainThread(c,{displayer:m,container:w,layerOpt:p,master:this.master,canvasOpt:d,post:this.post});this.mainThreadMap.set(c,P),P&&u&&P.setCameraOpt(u)}continue}if((a===s.EPostMessageType.Snapshot||a===s.EPostMessageType.BoundingBox)&&c===((t=this.master.control.viewContainerManager.mainView)==null?void 0:t.id)){const m=(o=this.master.control.viewContainerManager.getView(c))==null?void 0:o.displayer,w=(i=m.snapshotContainerRef)==null?void 0:i.current;if(m&&w&&u){w.style.width=`${u.width}px`,w.style.height=`${u.height}px`;const P={...s.ViewContainerManager.defaultLayerOpt,offscreen:!1,width:u.width,height:u.height},T={...s.ViewContainerManager.defaultScreenCanvasOpt,width:u.width,height:u.height};this.snapshotThread=this.createSnapshotThread(c,{displayer:m,container:w,layerOpt:P,master:this.master,canvasOpt:T,post:this.post}),this.snapshotThread.on(n).then(()=>{this.snapshotThread=void 0,w.innerHTML="",w.style.width="",w.style.height=""});continue}}if(a===s.EPostMessageType.GetImageBitMap&&f&&this.snapshotThread){this.snapshotThread.on(n);continue}if(a===s.EPostMessageType.TasksQueue&&(l!=null&&l.size)){for(const[m,w]of this.mainThreadMap.entries()){const P=l.get(m);P&&(await w.on(P),h&&this.post({workerTasksqueueCount:h}))}continue}if(c===s.Storage_ViewId_ALL){for(const m of this.mainThreadMap.values())m.on(n),a===s.EPostMessageType.Destroy&&this.mainThreadMap.delete(c);continue}const y=this.mainThreadMap.get(c);y&&(y.on(n),a===s.EPostMessageType.Destroy&&this.mainThreadMap.delete(c))}}}exports.MainThreadManagerImpl=rs;
|