import"./ace.js?v=1761009159";import{E as e,bH as a,bI as t,b as l}from"./utils-lib.js?v=1761009159";import{c as u,t as o,r as s,V as i,w as r,q as n,n as v,e as c,m as d,a7 as f}from"./base-lib.js?v=1761009159";const{load:m,request:h}=l(),g=u({name:"BtAceEditor",props:{modelValue:{type:String,default:""},isZoom:{type:Boolean,default:!1},isScrollBottom:{type:Boolean,default:!1},filePath:{type:String,default:""},request:{type:[Object,Boolean],default:!1},encoding:{type:String,default:"utf-8"},autoSave:{type:Boolean,default:!1},editorOption:{type:Object,default:()=>({})}},emits:["update:modelValue","save"],setup(u,{attrs:g,emit:p,expose:q}){const{modelValue:w,editorOption:y}=o(u),V=s("file"),b=s(),P=i(),S=s(!1),B={theme:e.value?"ace/theme/clouds_midnight":"ace/theme/chrome",mode:"ace/mode/nginx",wrap:!0,showInvisibles:!1,showFoldWidgets:!1,useSoftTabs:!0,tabSize:2,showPrintMargin:!1,readOnly:!1,fontSize:"12px"},C=s(!1),O=l();r(()=>w.value,e=>{var a,t;if(P.value){if(S.value)return void(S.value=!1);try{P.value.getValue()!==w.value&&(P.value.setValue(e||""),P.value.moveCursorTo(0,0)),u.isScrollBottom&&(null==(t=null==(a=P.value.renderer)?void 0:a.scrollBar)?void 0:t.scrollHeight)&&setTimeout(()=>{P.value&&P.value.getSession().setScrollTop(P.value.renderer.scrollBar.scrollHeight)},50)}catch(l){}}}),r(()=>u.filePath,(e,a)=>{e!==a&&P.value&&(T(),"model"!==V.value&&j({filePath:e,request:u.request}))}),r(()=>u.request,(e,a)=>{e!==a&&P.value&&(T(),"request"===V.value&&j({filePath:u.filePath,request:e}))},{deep:!0});const T=()=>{var e,a;""!==u.filePath?V.value="file":(null==(e=null==u?void 0:u.request)?void 0:e.get)&&(null==(a=null==u?void 0:u.request)?void 0:a.set)?V.value="request":V.value="model"},z=(e=!1)=>{try{if(!b.value)return!1;if(P.value&&P.value.container&&P.value.container.parentNode)return!0;if(!e){const e=b.value.getBoundingClientRect();if(0===e.width||0===e.height)return!1}b.value.innerHTML="",window.ace.config.set("basePath","/static/editor/");const a=window.ace.edit(b.value,{...f(B),...y.value});return a.commands.addCommand({name:"保存文件",bindKey:{win:"Ctrl-S",mac:"Command-S"},exec:()=>{p("save",a.getValue()),u.autoSave&&x({filePath:u.filePath,request:u.request,encoding:u.encoding})},readOnly:!1}),a.getSession().on("change",()=>{p("update:modelValue",a.getValue())}),P.value=a,!0}catch(a){return P.value=null,!1}},j=async({filePath:e,request:t})=>{const l=m("正在加载文件内容，请稍后...");try{if(!P.value)return;if("file"===V.value){const t=await a({path:e});if(t.status){const{data:e}=t,a=e&&e.data||"";P.value.setValue(a)}else h(t)}else if("request"===V.value){const{data:e}=await t.get();P.value.setValue(e||"")}else S.value=!0,p("update:modelValue",P.value.getValue());v(()=>{P.value&&(P.value.focus(),P.value.moveCursorTo(0,0))})}catch(u){}finally{l.close()}},x=async({filePath:e,request:a,encoding:l})=>{const u=O.load("正在保存文件，请稍后...");try{if("file"===V.value){const a=await t({path:e,encoding:l,data:P.value.getValue()});O.request(a)}else if("request"===V.value){const e=await a.set({data:P.value.getValue(),encoding:l});O.request(e)}else p("save",P.value.getValue())}catch(o){O.error(o)}finally{u.close()}},E=()=>{C.value=!C.value};let H=null;return n(async()=>{await v(),T(),z(),await v(),"model"!==V.value?j({filePath:u.filePath,request:u.request}):P.value&&(P.value.setValue(w.value||""),v(()=>{P.value&&(P.value.focus(),P.value.moveCursorTo(0,0))})),b.value&&window.ResizeObserver&&(H=new ResizeObserver(e=>{e.forEach(e=>{const{width:a,height:t}=e.contentRect;!(a>0&&t>0)||P.value&&P.value.container?P.value&&a>0&&t>0&&P.value.resize():(async()=>{await v(),!b.value||P.value&&P.value.container||z(!0)&&("model"!==V.value?j({filePath:u.filePath,request:u.request}):P.value&&(P.value.setValue(w.value||""),v(()=>{P.value&&(P.value.focus(),P.value.moveCursorTo(0,0))})))})()})}),H.observe(b.value))}),q({getEditor:()=>P.value}),()=>c("div",{class:"ace-editor-content h-full relative"},[c("div",d({class:"ace-editor ".concat((u.isZoom,"")),style:"height:100%",ref:b},g),null),u.isZoom?c("div",{class:"absolute right-0 top-0 icon-zoom",title:u.isZoom?"全屏显示":"退出全屏",onClick:E},null):null])}});export{g as B};
