"use strict";(self["webpackChunkaida"]=self["webpackChunkaida"]||[]).push([[8538],{58538:function(e,t,a){a.r(t),a.d(t,{ThumbnailManager:function(){return i}});class i{constructor(e,t={}){this.canvas=e,this.layers=t.layers||[],this.layerThumbSize=t.layerThumbSize||{width:48,height:48},this.elementThumbSize=t.elementThumbSize||{width:36,height:36},this.layerThumbnails=new Map,this.elementThumbnails=new Map,this.defaultThumbnail="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="}generateLayerThumbnail(e){}_generateLayerThumbnailNow(e){if(e&&this.canvas&&e.fabricObjects&&e.fabricObjects?.length)try{let t=null;if(e.children?.length){if("group"===e.type||e.children?.length){const a=e.children.reduce(((e,t)=>(t.fabricObjects.length&&e.push(...t.fabricObjects),e)),[]);t=this._generateThumbnailFromObjects(a,this.layerThumbSize.width,this.layerThumbSize.height)}}else t=this._generateThumbnailFromObjects(e.fabricObjects,this.layerThumbSize.width,this.layerThumbSize.height);t?this.layerThumbnails.set(e.id,t):this.layerThumbnails.set(e.id,this.defaultThumbnail)}catch(t){console.error("生成图层缩略图出错:",t)}}generateElementThumbnail(e,t){e&&e.id&&t&&requestAnimationFrame((()=>{try{const a=this._generateThumbnailFromObject(t,this.elementThumbSize.width,this.elementThumbSize.height);a&&this.elementThumbnails.set(e.id,a)}catch(a){console.error("生成元素缩略图出错:",a)}}))}_generateThumbnailFromObject(e,t,a){if(!e||!this.canvas)return null;const i={active:e.active,visible:e.visible,left:e.left,top:e.top,scaleX:e.scaleX,scaleY:e.scaleY,opacity:e.opacity};e.set({active:!1,visible:!0,opacity:1});const n=document.createElement("canvas");n.width=t,n.height=a;const l=n.getContext("2d"),h=e.getBoundingRect();try{l.clearRect(0,0,t,a);const s=t/h.width,r=a/h.height,c=.8*Math.min(s,r),u=t/2,m=a/2;l.save(),l.translate(u,m),l.scale(c,c),l.translate(-h.left-h.width/2,-h.top-h.height/2),e.render(l),l.restore();const o=n.toDataURL("image/png");return e.set(i),o}catch(s){return console.error("绘制对象缩略图出错:",s),e.set(i),null}}_generateThumbnailFromObjects(e,t,a){if(!e||!e.length||!this.canvas)return null;const i=document.createElement("canvas");i.width=t,i.height=a;const n=i.getContext("2d");let l=1/0,h=1/0,s=-1/0,r=-1/0;e.forEach((e=>{if(!e.visible)return;const t=e.getBoundingRect();l=Math.min(l,t.left),h=Math.min(h,t.top),s=Math.max(s,t.left+t.width),r=Math.max(r,t.top+t.height)}));const c=s-l,u=r-h;if(c<=0||u<=0)return null;const m=e.map((e=>({obj:e,state:{active:e.active,visible:e.visible,opacity:e.opacity}})));m.forEach((e=>{e.obj.set({active:!1,visible:!0,opacity:1})}));try{n.clearRect(0,0,t,a);const s=t/c,r=a/u,o=.8*Math.min(s,r),b=t/2,g=a/2;n.save(),n.translate(b,g),n.scale(o,o),n.translate(-(l+c/2),-(h+u/2)),e.forEach((e=>{e.visible&&e.render(n)})),n.restore();const T=i.toDataURL("image/png");return m.forEach((e=>{e.obj.set(e.state)})),T}catch(o){return console.error("绘制组合缩略图出错:",o),m.forEach((e=>{e.obj.set(e.state)})),null}}generateAllLayerThumbnails(e){e&&Array.isArray(e)&&requestAnimationFrame((()=>{e.forEach((e=>{e&&e.id&&this._generateLayerThumbnailNow(e)}))}))}getLayerThumbnail(e){return e&&this.layerThumbnails.get(e)||null}getElementThumbnail(e){return e&&this.elementThumbnails.get(e)||null}clearLayerThumbnail(e){e&&this.layerThumbnails.has(e)&&this.layerThumbnails.delete(e)}clearElementThumbnail(e){e&&this.elementThumbnails.has(e)&&this.elementThumbnails.delete(e)}clearAllThumbnails(){this.layerThumbnails.clear(),this.elementThumbnails.clear()}dispose(){this.clearAllThumbnails(),this.canvas=null}}}}]);