vue-quill编辑器vue2版

发布时间 2024-01-13 11:40:19作者: lambertlt

效果图

<template>
  <div class="app-container">
    <p class="warn-content">
      Markdown 基于
      <el-link type="primary" href="javascript:void 0;" target="_blank">vue quill</el-link>
    </p>
    <el-button @click="copyHtml()">一键复制 HTML</el-button>
    <el-button @click="copy()">一键复制 TEXT</el-button>
    <el-button @click="dialogFormVisible = true">添加图片链接</el-button>
    <div id="editor" />

    <el-dialog title="图片链接" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="图片链接" :label-width="formLabelWidth">
          <el-input v-model="form.link" auto-complete="off" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="imgAdd()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import { mapGetters } from 'vuex'
import Quill from 'quill'

export default {
  name: 'VueQuill',
  components: {
  },
  data() {
    return {
      height: document.documentElement.clientHeight - 200 + 'px',
      quill: {},
      dialogFormVisible: false,
      form: {},
      formLabelWidth: '120px'
    }
  },
  computed: {
    ...mapGetters([
      'imagesUploadApi',
      'baseApi'
    ])
  },
  mounted() {
    const that = this
    const container = document.getElementById('editor')
    this.quill = new Quill(container, {
      theme: 'snow',
      debug: 'warn',
      modules: {
        toolbar:
          [
            ['bold', 'italic', 'underline', 'strike', 'link'],
            ['blockquote', 'code-block'],
            [{ 'size': ['small', false, 'large', 'huge'] }],
            [{ 'font': [] }],
            [{ 'align': [] }],
            [{ 'list': 'ordered' }, { 'list': 'bullet' }],
            [{ 'indent': '-1' }, { 'indent': '+1' }],
            [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
            ['image'],
            [{ 'direction': 'rtl' }],
            [{ 'color': [] }],
            [{ 'background': [] }]
          ]
      },
      placeholder: '',
      readOnly: false,
      content: '',
      contentType: 'html'
    })
    window.onresize = function temp() {
      that.height = document.documentElement.clientHeight - 200 + 'px'
    }
  },
  methods: {
    copyHtml() {
      navigator.clipboard.writeText(this.quill.root.innerHTML.toString())
    },
    copy() {
      navigator.clipboard.writeText(this.quill.root.textContent.toString())
    },
    imgAdd() {
      this.dialogFormVisible = false
      this.quill.insertEmbed(10, 'image', this.form.link)
      this.form = {}
    }
  }
}
</script>

<style scoped>
.v-note-wrapper.shadow {
  z-index: 5;
}

#editor {
  height: 600px;
  width: 100%;
}

#show {
  width: 100%;
  height: 600px;
}
</style>