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