"use strict";(self["webpackChunkaida"]=self["webpackChunkaida"]||[]).push([[9224],{89224:function(e,a,t){t.r(a),t.d(a,{ExportManager:function(){return r}});t(21703);class r{constructor(e,a){this.canvasManager=e,this.layerManager=a,this.canvas=e.canvas}exportImage(e={}){const{isContainBg:a=!1,isContainFixed:t=!1,layerId:r="",layerIdArray:s=[],expPicType:n="png"}=e;try{return r?this._exportSpecificLayer(r,n):s&&s.length>0?this._exportMultipleLayers(s,n,a,t):this._exportAllLayers(n,a,t)}catch(i){throw console.error("导出图片失败:",i),new Error(`图片导出失败: ${i.message}`)}}_exportSpecificLayer(e,a){if(!this.layerManager)throw new Error("图层管理器未初始化");const t=this._getLayerById(e);if(!t)throw new Error(`未找到ID为 ${e} 的图层`);t.visible||console.warn(`图层 ${t.name} 不可见，将导出空白图片`);const r=this._createExportCanvas(),s=this._createTempFabricCanvas(r);try{return this._addLayerObjectsToCanvas(s,t),s.renderAll(),this._generateDataURL(r,a)}finally{this._cleanupTempCanvas(s)}}_exportMultipleLayers(e,a,t,r){if(!this.layerManager)throw new Error("图层管理器未初始化");const s=this._createExportCanvas(),n=this._createTempFabricCanvas(s);try{const i=this._getAllLayers();return i.forEach((a=>{e.includes(a.id)&&this._shouldIncludeLayer(a,t,r)&&a.visible&&this._addLayerObjectsToCanvas(n,a)})),n.renderAll(),this._generateDataURL(s,a)}finally{this._cleanupTempCanvas(n)}}_exportAllLayers(e,a,t){const r=this._createExportCanvas(),s=this._createTempFabricCanvas(r);try{const n=this._getAllLayers();return n.forEach((e=>{this._shouldIncludeLayer(e,a,t)&&e.visible&&this._addLayerObjectsToCanvas(s,e)})),s.renderAll(),this._generateDataURL(r,e)}finally{this._cleanupTempCanvas(s)}}_shouldIncludeLayer(e,a,t){return"background"===e.type||e.isBackground?a:"fixed"!==e.type&&!e.isFixed&&!e.locked||t}_createExportCanvas(){const e=document.createElement("canvas");return e.width=this.canvas.width||800,e.height=this.canvas.height||600,e}_createTempFabricCanvas(e){const{fabric:a}=window,t=new a.StaticCanvas(e,{width:this.canvas.width||800,height:this.canvas.height||600,backgroundColor:this.canvas.backgroundColor||"transparent"});return t.enableRetinaScaling=!0,t.imageSmoothingEnabled=!0,t}_addLayerObjectsToCanvas(e,a){a&&("background"===a.type&&a.fabricObject?this._cloneAndAddObject(e,a.fabricObject):(a.fabricObjects&&Array.isArray(a.fabricObjects)&&a.fabricObjects.forEach((a=>{a&&!1!==a.visible&&this._cloneAndAddObject(e,a)})),a.fabricObject&&!a.fabricObjects&&this._cloneAndAddObject(e,a.fabricObject),a.children&&Array.isArray(a.children)&&a.children.forEach((a=>{const t=this._getLayerById(a);t&&t.visible&&this._addLayerObjectsToCanvas(e,t)}))))}_cloneAndAddObject(e,a){if(a)try{a.clone((a=>{a&&(a.set({selectable:!1,evented:!1,visible:!0}),e.add(a))}),["id","layerId","name"])}catch(t){console.warn("克隆对象失败:",t)}}_generateDataURL(e,a){const t=a.toLowerCase();switch(t){case"jpg":case"jpeg":return e.toDataURL("image/jpeg",.9);case"svg":return console.warn("SVG导出暂未实现，返回PNG格式"),e.toDataURL("image/png",1);case"png":default:return e.toDataURL("image/png",1)}}_cleanupTempCanvas(e){if(e)try{e.dispose()}catch(a){console.warn("清理临时画布失败:",a)}}_getAllLayers(){return this.layerManager&&this.layerManager.layers&&this.layerManager.layers.value||[]}_getLayerById(e){if(this.layerManager&&this.layerManager.getLayerById)return this.layerManager.getLayerById(e);const a=this._getAllLayers();return a.find((a=>a.id===e))||null}}}}]);