<template>
  <view class="d-flex flex-col">
    <Parse v-if="modelValue" :content="modelValue" />
    <text v-else>未编写内容，点击编写</text>
    <view class="d-flex flex-row align-center gap-s mt-3">
      <Button @click="preview">预览内容</Button>
      <Button @click="edit" type="primary">编辑内容</Button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { onPageShow } from '@dcloudio/uni-app';
import { navTo } from '@/components/utils/PageAction';
import Parse from '@/components/display/parse/Parse.vue';
import Button from '@/components/basic/Button.vue';

const props = defineProps({	
  modelValue: { 
    type: String,
    default: null 
  },
})
const emit = defineEmits(['update:modelValue'])
let editorOpened = false;

function preview() {
  uni.setStorage({
    key: 'editorContent',
    data: props.modelValue,
    success: () => navTo('/pages/editor/preview'),
  })
}
function edit() {
  editorOpened = true;
  uni.setStorage({
    key: 'editorContent',
    data: props.modelValue,
    success: () => navTo('/pages/editor/editor'),
  })
}

onPageShow(() => {
  if (editorOpened) {
    editorOpened = false;
    uni.getStorage({
      key: 'editorContent',
      success: (success) => emit('update:modelValue', success.data),
    })
  }
})
</script>