<template>
  <div style="border: 1px solid #ccc" class="h-full">
    <Toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editorRef"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <Editor
      style="height: 500px; overflow-y: hidden"
      v-model="valueHtml"
      :defaultConfig="editorConfig"
      :mode="mode"
      @onCreated="handleCreated"
    />
  </div>
</template>
<script setup lang="ts">
import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { uploadFile } from '@/api'
const mode = 'default'

// 编辑器实例，必须用 shallowRef
const editorRef = shallowRef()

// 内容 HTML
const valueHtml = defineModel<string>()

// 去掉上传视频的功能
const toolbarConfig = {
  excludeKeys: ['group-video', 'group-more-video', 'group-more-video'],
}
// 去掉上传视频
const editorConfig = {
  placeholder: '请输入内容...',
  MENU_CONF: {
    uploadImage: {
      customUpload: async (file: File, insertFn: (url: string) => void) => {
        const { promise } = uploadFile(file)
        const data = await promise
        insertFn(data.filePath)
      },
    },
  },
}

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value
  if (editor == null) return
  editor.destroy()
})

const handleCreated = (editor: typeof Editor) => {
  editorRef.value = editor // 记录 editor 实例，重要！
}
</script>
<style scoped>
/* 使用 :deep() 来穿透 scoped 样式的限制, 直接为编辑器内容区域设置样式。这里的 p 标签是内容的最小单位，为其设置样式最精确。 */
:deep(.w-e-text-container p) {
  font-size: 18px !important;
}
</style>
