zoukankan      html  css  js  c++  java
  • vue封装tinymce富文本组件,图片上传回调方法

    这里我用了tinymce,主要解决的是上传图片回调处理

    效果预览:

    第一步:下载tinymce资源,

    把tinymce放到static目录

    在index.html中引入tinymce.min.js文件

    <script src=<%= htmlWebpackPlugin.options.path %>/tinymce/tinymce.min.js></script>

    第二步:新建tinymce组件

    template:

    <template>
      <div class="comp-tinymce-wrapper">
        <textarea :id="tinymceId"></textarea>
      </div>
    </template>

    script:

    <script>
    //上传图片方法
    import {uploadRichImg} from '@/api/common' export default { name:'tinymce', props:{ id:{ type:String }, value:{ type:String, default:'' },
    //可以自定义toolbar toolbar:{ type:Array, require:
    false, default(){ return ['removeformat undo redo | bullist numlist | outdent indent | fullscreen code', 'formatselect | bold italic strikethrough forecolor blockquote | alignleft aligncenter alignright | image link '] } }, menubar:{ default:'' }, height:{ type:Number, requier:false, default:400 } }, data(){ return { tinymceId: this.id || 'vue-tinymce'+ Date.parse(new Date()), hasInit:false, hasChange:false } }, mounted(){ this.initTinymce() }, watch:{ value(newV,oldV){ //当传入值不变化的时候更新富文本内容, if(this.hasInit&&!this.hasChange){ window.tinymce.get(this.tinymceId).setContent(newV) } } }, methods:{ initTinymce(){ const _this = this tinymce.init({ selector: `#${this.tinymceId}`, height: this.height, body_class: 'panel-body rich-text', theme: 'modern', content_style:'{p{color_red} }', //是否可拉伸 resize:false, //语言 language: 'zh_CN', //是否显示POWERED BY TINYMCE branding:false, //toolbar toolbar: this.toolbar, fixed_toolbar_container: '#mytoolbar', //menubar menubar: this.menubar, //插件 plugins: 'advlist,autolink,code,paste,textcolor, colorpicker,fullscreen,link,lists, image ', powerpaste_word_import: 'clean', //tinymce内容自定义样式 content_css:'static/tinymce/css/rewrite-mce.css', //源代码弹出层宽 code_dialog_height: 450, //源代码弹出层高 code_dialog_ 1000, // block_formats: 'Paragraph=p;Header 1=h1;Header 2=h2;Header 3=h3', imagetools_cors_hosts: ['wpimg.wallstcn.com', 'wallstreetcn.com'], //链接打开方式 default_link_target: '_blank', images_upload_url: '/oss/file/uploadRichImg?dir=image',
    //上传图片回调 images_upload_handler:(blobInfo, success, failure)
    => { let fd = new FormData() fd.append('file',blobInfo.blob()) uploadRichImg(fd).then(res => { let result = res.data[0] success(result.url) }) .catch(err => { }) }, init_instance_callback: editor => { if (_this.value) { editor.setContent(_this.value) } _this.hasInit = true editor.on('NodeChange Change input KeyUp', () => { //change触发watch去setContent,光标变化了, this.hasChange = true this.$emit('input', editor.getContent({ format: 'raw' })) }) } }) }, destroyTiny(){ if(window.tinymce.get(this.tinymceId)){ window.tinymce.get(this.tinymceId).destroy() } }, setContent(val){ window.tinymce.get(this.tinymceId).setContent(val) }, getContent(){ window.tinymce.get(this.tinymceId).getContent() }, destroyed(){ this.destroyTiny() } } } </script>

    在父组件中引入tinymce组件

    import tinymce from '@/components/Tinymce/index'
                 <tinymce :height="tiny.height" v-model="sendObj.content"></tinymce>

    script:

    export default {
        name: 'addNews',
        components:{
          tinymce
        },
        data(){
          return {
            tiny:{
              height:300
            },
            sendObj:{
              content:'nihao'
            }
        },
        methods:{
        }
      }
    </script>
  • 相关阅读:
    PHP调试的时候出现了警告:
    快报滚动
    js foreach、map函数
    箭头函数和普通函数的区别
    flex布局
    react+propTypes
    手机尺寸
    less的使用
    发现是在IE6-IE9下,下列元素table,thead,tfoot,tbody,tr,col,colgroup,html,title,style,frameset的innerHTML属性是只读的
    div+css 组织结构
  • 原文地址:https://www.cnblogs.com/lizimeme/p/8304207.html
Copyright © 2011-2022 走看看