<template>
  <div class="QdmEditor" :class="inline ? 'inline' : ''" :style="'height:' + (Number(height) + 42) +'px;'">
    <span v-show="titleShow" :style="'width:' + colSpan + 'px;text-align:' + align + ';'"><i v-if="must">*</i>{{ name }}</span>
    <input v-show="false" id="quill-img" class="quill-img" type="file" ref="editorfileRef" @change="fileChange" />
    <!-- <QuillEditor
    :style="'width:' + width + 'px;height:' + height +'px;'"
    v-model="content"
    ref="myQuillEditor"
    :options="editorOption"
    @change="onEditorChange($event)">
    </QuillEditor> -->
    <QuillEditor
    theme="snow"
    @textChange="onEditorChange"
    />
  </div>
</template>
<script>
import axios from 'axios'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css';
const toolbarOptions = [
   ['bold', 'italic', 'underline', 'strike'],    //加粗，斜体，下划线，删除线
  //  ['blockquote', 'code-block'],     //引用，代码块
   [{ 'header': 1 }, { 'header': 2 }],        // 标题，键值对的形式；1、2表示字体大小
   [{ 'list': 'ordered'}, { 'list': 'bullet' }],     //列表
  //  [{ 'script': 'sub'}, { 'script': 'super' }],   // 上下标
  //  [{ 'indent': '-1'}, { 'indent': '+1' }],     // 缩进
   [{ 'direction': 'rtl' }],             // 文本方向
   [{ 'size': ['small', false, 'large', 'huge'] }], // 字体大小
  //  [{ 'header': [1, 2, 3, 4, 5, 6, false] }],     //几级标题
   [{ 'color': [] }, { 'background': [] }],     // 字体颜色，字体背景颜色
   [{ 'font': [] }],     //字体
   [{ 'align': [] }],    //对齐方式
   ['clean'],    //清除字体样式
   ['image','video']    //上传图片、上传视频
]
export default {
  name: "QdmEditor",
  components: { QuillEditor },
  props: {
    titleShow: {
      type: Boolean,
      default: false
    },
    name: {
      type: String,
      default: ''
    },
    colSpan: {
      type: Number,
      default: 60
    },
    align: {
    	type: String,
      default: 'right'
    },
    must: {
      type: Boolean,
      default: false
    },
    inline: {
      type: Boolean,
      default: false
    },
    width: {
      type: String,
      default: '800'
    },
    height: {
      type: String,
      default: '300'
    },
    contentHtml: {
      type: String,
      default: ''
    },
    uploadUrl: {
    	type: String,
      default: ''
    }
  },
  data() {
    return {
      editor: null,
      content: '',
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions,
            handlers: {
            	image: function(value) {
            		if(value) {
            			document.getElementById('quill-img').click()
            		} else {
            			this.quill.format('image', false)
            		}
            	}
            }
          }
        },
        theme: 'snow',
        placeholder: '请输入正文'
      }
    }
  },
  mounted() {
    this.editor = this.$refs.myQuillEditor.quill
  },
  beforeUnmount() {
    this.editor = null
    delete this.editor
  },
  methods: {
  	fileChange() {
  		let that = this
  		let file = that.$refs.editorfileRef.files
  		let formData = new FormData()
      formData.append('file', file[0])
  		axios.post(this.uploadUrl, formData).then(function(res){
  			that.quillImgSuccess(res.data.data.fullFilePath)
	      that.$refs.editorfileRef.value = ''
	    })
  	},
    onEditorChange (delta, oldContents, source) {
      console.log(delta)
      console.log(oldContents)
      console.log(source)
      // this.$emit('editorReturn', editor.html)
    },
    quillImgSuccess(url) {
      console.log(url)
      console.log(quill)
    	let quill = this.$refs.myQuillEditor.quill
    	let length = quill.getSelection().index
    	quill.insertEmbed(length, 'image', url)
    	quill.setSelection(length + 1)
    }
  },
  watch: {
    contentHtml(val) {
      this.content = val
    }
  }
}
</script>
<style lang="less">
.inline {
  float: left;
}
.QdmEditor {
  margin-right: 10px;
  margin-bottom: 8px;
  >span {
    display: block;
    height: 30px;
    float: left;
    line-height: 30px;
    padding-right: 8px;
    font-size: 12px;
    >i {
      color: #F5222D;
      margin-right: 5px;
      padding-left: 3px;
    }
  }
  .quill-editor {
    height: 100%;
    float: left;
    .ql-container {
    	height: calc(100% - 42px);
    }
  }
}
</style>