vue3 tinymce4实现复制word文档内容保留原格式

封装组件
<template>
    <div class="tinymce-editor">
      <textarea :id="id" v-model="Value"></textarea>
   </div>
</template>

<script>
import '../../../public/tinymce/tinymce.min.js'
window.tinymce.baseURL = '/tinymce'
window.tinymce.suffix = '.min'

export default {
  props: {
    value: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      id: 'vue-tinymce-' + Date.now(),
      Value: this.value,
      flag: true
    }
  },
  mounted () {
    tinymce.init({
      selector: `#${this.id}`,
      language: 'zh_CN',
      height: 500,
      theme: 'modern',
      plugins: 'powerpaste print preview fullpage searchreplace autolink directionality code visualblocks visualchars fullscreen image link media template codesample table charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists textcolor wordcount spellchecker imagetools media contextmenu colorpicker textpattern help',
      toolbar: 'undo redo | formatselect | bold italic strikethrough forecolor backcolor fontsizeselect | alignleft aligncenter alignright alignjustify  | numlist bullist outdent indent  | removeformat fullscreen  preview save print | link code',
      fontsize_formats: '8px 10px 12px 13px 14px 16px 18px 24px 36px 48px',
      //黏贴功能
	    powerpaste_word_import: 'propmt',// 参数可以是propmt, merge, clear,效果自行切换对比
      powerpaste_html_import: 'propmt',// propmt, merge, clear
      powerpaste_allow_local_images: true,
      paste_data_images: true,
      //上传图片功能
      //automatic_uploads: true,//自动上传	
	    image_advtab: true,//开启图片上传的高级选项功能
      image_title: true, // 是否开启图片标题设置的选择,这里设置否
      //images_upload_url: '',//上传图片地址
      images_upload_handler: function (blobInfo, success, failure) {
   	    const base64Img = "data:image/jpeg;base64," + blobInfo.base64();
   	    success(base64Img);
      },
      init_instance_callback: editor => {
        editor.on('input change undo redo execCommand', () => {
          this.flag = false;
          this.$emit('input', editor.getContent())
        })
      },
      images_dataimg_filter: function(img) {
    	  return img.hasAttribute('internal-blob');
      },
      paste_preprocess: function(plugin, args) {}  
	 });
  },
  beforeDestroy: function(){
    if (tinymce.get(this.id)) {
      tinymce.get(this.id).destroy();
    }
  },
  watch: {
    value(val){
      if(this.flag){
        this.$nextTick(() => tinymce.get(this.id).setContent(val))
      }
      this.flag = true;
    }
  }
}
</script>

引用组件
<template>
    <div class="container__r-content">
        <tinymce-editor ref="editor" v-model="msg"></tinymce-editor>
    </div>
</template>
import TinymceEditor from 'components/Tinymce.vue'
export default {
    components: {
        TinymceEditor
    },
    data() {
        return {
            msg:'',
        }
    }
 }
</script>

其中会用到tinymce powerpaste插件:下载地址

你可能感兴趣的:(vue3 tinymce4实现复制word文档内容保留原格式)