index.vue 1.62 KB
<script setup lang="ts">
/**
 * 富文本
 * @module Tinymce
 */
import { ref, reactive, onMounted } from 'vue'
import { uploadPicSource } from '@/api/user/login'
import { toolbar, plugins } from './settings/index'

/**
 * @description 组件属性
 * @prop {string | null} tagName 标签
 * @prop {string | null} moduleName 上传接口需要传的module值
 */
interface Props {
  tagName?: string;
  moduleName?: string;
}
let content = ref('')
const props = withDefaults(defineProps<Props>(), {
  moduleName: "test",
});
const emit = defineEmits<{
  (e: "contentChange", contents: string, tagName: string): void;
}>();
onMounted (() => {
  tinymce.init({
    selector: '#mytextarea',
    height: 404,
    language_url : 'http://testweb.community.cnlive.com/languages/zh_CN.js',
    language:'zh_CN',
    plugins: plugins,
    toolbar: toolbar,
    images_upload_url: `${import.meta.env.VITE_BASE_URL}upload/uploadPicSource`,
    images_upload_handler: function (blobInfo, succFun, failFun, progress) {
      const files = new window.File([blobInfo.blob()], '图片');
      const fd = new FormData()
      fd.append('multipartFile', files)
      uploadPicSource({
        module: props.moduleName
      }, fd).then(res => {
        succFun(res.data.data)
      }).catch(error => {
        failFun(error)
      })
    },
    // setup: (ed) => { 
    //   // ed.onKeyPress.add(
    //   //   function (ed, evt) { 
    //       console.log(ed)
    //   //     console.log(evt) 
    //   //   } 
    //   // )
    // }
  });
  console.log(tinymce.editors['mytextarea'].getContent())
})
</script>

<template>
  <textarea id="mytextarea"></textarea>
</template>