index.vue 4.5 KB
Newer Older
P
init  
Pan 已提交
1
<template>
P
Pan 已提交
2 3
  <div class="tinymce-container editor-container">
    <textarea class="tinymce-textarea" :id="tinymceId"></textarea>
P
Pan 已提交
4
    <div class="editor-custom-btn-container">
P
Pan 已提交
5 6
      <editorImage color="#1890ff" class="editor-upload-btn" @successCBK="imageSuccessCBK"></editorImage>
    </div>
P
init  
Pan 已提交
7 8 9 10
  </div>
</template>

<script>
P
Pan 已提交
11
import editorImage from './components/editorImage'
P
Pan 已提交
12 13
import plugins from './plugins'
import toolbar from './toolbar'
P
Pan 已提交
14

P
Pan 已提交
15 16
export default {
  name: 'tinymce',
P
Pan 已提交
17
  components: { editorImage },
P
Pan 已提交
18 19
  props: {
    id: {
P
Pan 已提交
20
      type: String
P
Pan 已提交
21 22 23 24 25 26 27 28 29
    },
    value: {
      type: String,
      default: ''
    },
    toolbar: {
      type: Array,
      required: false,
      default() {
P
Pan 已提交
30
        return []
P
Pan 已提交
31 32 33
      }
    },
    menubar: {
P
Pan 已提交
34
      default: 'file edit insert view format table'
P
Pan 已提交
35 36 37 38 39 40 41
    },
    height: {
      type: Number,
      required: false,
      default: 360
    }
  },
P
Pan 已提交
42 43 44 45 46 47 48
  data() {
    return {
      hasChange: false,
      hasInit: false,
      tinymceId: this.id || 'vue-tinymce-' + +new Date()
    }
  },
P
Pan 已提交
49 50 51
  watch: {
    value(val) {
      if (!this.hasChange && this.hasInit) {
P
Pan 已提交
52
        this.$nextTick(() => window.tinymce.get(this.tinymceId).setContent(val))
P
Pan 已提交
53 54 55 56
      }
    }
  },
  mounted() {
P
Pan 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    this.initTinymce()
  },
  activated() {
    this.initTinymce()
  },
  deactivated() {
    this.destroyTinymce()
  },
  methods: {
    initTinymce() {
      const _this = this
      window.tinymce.init({
        selector: `#${this.tinymceId}`,
        height: this.height,
        body_class: 'panel-body ',
        object_resizing: false,
P
Pan 已提交
73
        toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar,
P
Pan 已提交
74
        menubar: this.menubar,
P
Pan 已提交
75
        plugins: plugins,
P
Pan 已提交
76 77 78 79 80 81
        end_container_on_empty_block: true,
        powerpaste_word_import: 'clean',
        code_dialog_height: 450,
        code_dialog_width: 1000,
        advlist_bullet_styles: 'square',
        advlist_number_styles: 'default',
P
Pan 已提交
82
        imagetools_cors_hosts: ['www.tinymce.com', 'codepen.io'],
P
Pan 已提交
83 84 85 86 87 88 89 90 91 92 93
        default_link_target: '_blank',
        link_title: false,
        init_instance_callback: editor => {
          if (_this.value) {
            editor.setContent(_this.value)
          }
          _this.hasInit = true
          editor.on('NodeChange Change KeyUp', () => {
            this.hasChange = true
            this.$emit('input', editor.getContent({ format: 'raw' }))
          })
P
Pan 已提交
94
        }
P
Pan 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        // 整合七牛上传
        // images_dataimg_filter(img) {
        //   setTimeout(() => {
        //     const $image = $(img);
        //     $image.removeAttr('width');
        //     $image.removeAttr('height');
        //     if ($image[0].height && $image[0].width) {
        //       $image.attr('data-wscntype', 'image');
        //       $image.attr('data-wscnh', $image[0].height);
        //       $image.attr('data-wscnw', $image[0].width);
        //       $image.addClass('wscnph');
        //     }
        //   }, 0);
        //   return img
        // },
        // images_upload_handler(blobInfo, success, failure, progress) {
        //   progress(0);
        //   const token = _this.$store.getters.token;
        //   getToken(token).then(response => {
        //     const url = response.data.qiniu_url;
        //     const formData = new FormData();
        //     formData.append('token', response.data.qiniu_token);
        //     formData.append('key', response.data.qiniu_key);
        //     formData.append('file', blobInfo.blob(), url);
        //     upload(formData).then(() => {
        //       success(url);
        //       progress(100);
        //     })
        //   }).catch(err => {
        //     failure('出现未知问题,刷新页面,或者联系程序员')
        //     console.log(err);
        //   });
        // },
P
Pan 已提交
128 129 130
      })
    },
    destroyTinymce() {
P
Pan 已提交
131 132 133
      if (window.tinymce.get(this.tinymceId)) {
        window.tinymce.get(this.tinymceId).destroy()
      }
P
Pan 已提交
134
    },
135 136 137 138 139 140
    setContent(value) {
      window.tinymce.get(this.tinymceId).setContent(value)
    },
    getContent() {
      window.tinymce.get(this.tinymceId).getContent()
    },
P
Pan 已提交
141 142 143
    imageSuccessCBK(arr) {
      const _this = this
      arr.forEach(v => {
P
Pan 已提交
144
        window.tinymce.get(_this.tinymceId).insertContent(`<img class="wscnph" src="${v.url}" >`)
P
Pan 已提交
145 146 147
      })
    }
  },
P
Pan 已提交
148
  destroyed() {
P
Pan 已提交
149
    this.destroyTinymce()
P
Pan 已提交
150
  }
P
Pan 已提交
151
}
P
init  
Pan 已提交
152 153 154 155 156 157 158 159 160 161
</script>

<style scoped>
.tinymce-container {
  position: relative
}
.tinymce-textarea {
  visibility: hidden;
  z-index: -1;
}
P
Pan 已提交
162 163
.editor-custom-btn-container {
  position: absolute;
P
Pan 已提交
164 165
  right: 4px;
  top: 4px;
P
Pan 已提交
166 167 168 169 170
  /*z-index: 2005;*/
}
.editor-upload-btn {
  display: inline-block;
}
P
init  
Pan 已提交
171
</style>