index.vue 5.28 KB
<template>
  <div class="editor-ctnr">
    <!-- <div class="extend-bar" v-if="operateList && operateList.length">
      <el-button v-for="item in operateList" :key="item.key" class="el-icon-circle-plus extend-icon" @click="insertNodeFn(item)">{{ item.name }}</el-button>
    </div> -->

    <label v-if="label" for="" :class="['content-label', { 'required-field': required }]">{{ label }}</label>
    <div>
      <div class="editor" style="border: 1px solid #ccc;">
        <Toolbar v-if="hasTool" style="border-bottom: 1px solid #ccc" :editor="editor" :defaultConfig="toolbarConfig" :mode="mode" />
        <Editor style="min-height: 52px; overflow-y: hidden;" @onChange="onChange" @customPaste="customPaste" v-model="value" :defaultConfig="editorConfig" :mode="mode" @onCreated="onCreated" />
      </div>
      <div class="extend-bar" v-if="operateList && operateList.length">
        <el-button v-for="item in operateList" :key="item.key" class="el-icon-circle-plus extend-icon" @click="insertNodeFn(item)">{{ item.name }}</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { Boot, DomEditor } from '@wangeditor/editor'

import { withBreakAndDelete } from './withimage'
Boot.registerPlugin(withBreakAndDelete)

import { attachmentToHtml } from './attachmentToHtml'
const elemToHtmlConf = {
  type: 'text', // 新元素的 type ,重要!!!
  elemToHtml: attachmentToHtml
}
Boot.registerElemToHtml(elemToHtmlConf)

import { renderAttachment } from './renderAttachment'
const renderElemConf = {
  type: 'text', // 新元素 type ,重要!!!
  renderElem: renderAttachment
}
Boot.registerRenderElem(renderElemConf)

import { parseAttachmentHtml } from './parseAttachmentHtml'
const parseHtmlConf = {
  selector: 'span[data-w-e-type="text"]', // CSS 选择器,匹配特定的 HTML 标签
  parseElemHtml: parseAttachmentHtml
}
Boot.registerParseElemHtml(parseHtmlConf)

export default Vue.extend({
  components: { Editor, Toolbar },
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    required: {
      type: Boolean,
      default: false
    },
    hasTool: {
      type: Boolean,
      default: true
    },
    value: {
      type: Object
    },
    label: {
      type: String,
      default: ''
    },
    operateList: {
      type: Array,
      default: () => {
        return []
      }
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      editor: null,
      html: '',
      toolbarConfig: {
        toolbarKeys: ['fontSize', 'lineHeight', 'bold', 'justifyLeft', 'justifyRight', 'justifyCenter', 'underline', 'through', 'color', 'bgColor', 'insertTable', 'undo', 'redo', 'clearStyle']
        // toolbarKeys: ['fontSize', 'lineHeight', "justifyLeft","justifyRight", "justifyCenter","justifyJustify", 'underline', 'through', 'color', 'bgColor', 'insertTable', 'undo', 'redo', 'clearStyle']
      },
      editorConfig: {
        placeholder: '请输入内容...'
      },
      mode: 'simple' // 设置simple,选中文字不会出现hoverBar
    }
  },
  methods: {
    onChange(editer) {
      this.$emit('change', this.editor.getHtml())
    },
    // 复制粘贴内容时,自动清除格式
    customPaste(editor, event) {
      editor.insertText(event.clipboardData.getData('text/plain'))
      event.preventDefault()
      return false
    },
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
    },
    insertNodeFn(item) {
      // event.preventDefault()
      const resume = {
        // JS 语法
        type: 'text',
        name: item.name,
        key: item.key,
        children: [{ text: '' }]
      }
      this.editor.insertNode(resume)
      setTimeout(() => {
        this.editor.focus()
        this.editor.move(1)
      }, 300)
    }
  },
  mounted() {},
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时,及时销毁编辑器
  }
})
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style scoped lang="scss">
.editor-ctnr {
  & > div {
    width: 100%;
  }
  width: 100%;
  height: 100%;
  display: flex;
  .editor {
    background: #fff;
    // /deep/ .w-e-text-container {
    //   .w-e-hover-bar {
    //     display: none;
    //   }
    // }
  }
  .content-label {
    flex-shrink: 0;
    width: 154px;
    display: inline-block;
    vertical-align: top;
    height: 20px;
    font-weight: 400;
    font-size: 14px;
    color: #333333;
    text-align: right;
    line-height: 20px;
  }
  /deep/ .w-e-text-container {
    padding: 10px 5px;
    p {
      margin: 0;
    }
    .w-e-text-placeholder {
      top: 5px;
      left: 14px;
      height: 32px;
      line-height: 32px;
    }
  }
}
.extend-bar {
  width: 937px;
  padding: 0 20px;
  line-height: 40px;
  background: #fafbfe;
  border: 1px solid #ccc;
  box-sizing: border-box;
  border-radius: 0 0 4px 4px;
  border-top: none;

  /deep/ .el-button {
    border: none;
    padding: 0;
    margin: 0 16px;
    background-color: transparent;
    &:hover {
      background-color: unset;
    }
  }
  .extend-icon {
    cursor: pointer;
    &::before {
      // font-size: 16px;
      margin-right: 4px;
    }
    font-size: 14px;
    color: #3f3b3b;
  }
}
</style>