<!--
 * index.vue
 * Copyright (C) 2024 veypi <i@veypi.com>
 * 2024-06-07 17:46
 * Distributed under terms of the MIT license.
 -->
<template>
  <div>
    <Editor style="" v-if="app.id" :eid="app.id + '.des'" v-model="preview_mode" :content="content" @save="save">
    </Editor>
  </div>
</template>

<script lang="ts" setup>
import type { models } from '#imports';
import { fs } from '@veypi/oaer'


const app = inject('app') as Ref<models.App>
let preview_mode = ref(true)

let content = ref('')

const sync = () => {
  if (app.value.des) {
    fs.app.getFileContents(fs.app.urlunwrap(app.value.des), { format: 'text' }).then((e) => {
      content.value = e as string
    })
  }
}

watch(computed(() => app.value.id), () => {
  sync()
}, { immediate: true })

const save = (des: string) => {
  let furl = `/info/appdes/${app.value.id}.md`
  fs.app.putFileContents(furl, des).then((e) => {
    furl = fs.app.urlwrap(furl)
    if (app.value.des !== furl) {
      api.app.Patch(app.value.id, { des: furl }).then((e) => {
        preview_mode.value = true
        app.value.des = furl
      })
    } else {
      preview_mode.value = true
    }
  })
}


onMounted(() => {
})

</script>

<style scoped></style>
