quill

富文本编辑器-2-组件封装

2018-06-23  本文已影响0人  minxuan

目前github上已经有将Quill封装一层的vue-quill-editor,对于使用Vue且想直接用Quill基本功能的同学,可以直接安装使用。由于公司业务需要定制一些功能,用vue-quill-editor或直接用quill没有太大区别,都需要自行封装组件,所以没有引入vue-quill-editor,而是参考其封装。

主要的props

  1. value: v-model绑定内容
  2. toolbarOptions:指定工具栏功能项,可以传入数组,也可以传入类或id,如'.toolbar'。一开始考虑用html的方式实现工具栏,以更灵活地进行改写,绑定事件等。但这样做,如果使用者想用组件已有几种功能,也必须在外部重写toolbar的slot。最后选择默认使用options数组。
defaultToolbarOptions: [
  ['undo', 'redo'],
  [{ 'size': this.sizeList }],
  [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
  ['blockquote', 'divider'],
  ['clean', 'formatBrush'],
  ['link', 'emoji', 'image'],
  ['bold', 'italic', 'underline', 'strike'],
  [{ 'color': [] }, {'background': []}],
  [{ 'script': 'sub' }, { 'script': 'super' }],
  [{ 'indent': '-1' }, { 'indent': '+1' }, { 'align': [] }, { 'direction': 'rtl' }],
  [{ 'list': 'ordered' }, { 'list': 'bullet' }]
]
...
// 初始化
let toolbarOptions = this.toolbarOptions || this.defaultToolbarOptions
  1. options: 指定整个Quill初始化的options。用toolbarOptions可以满足大部分业务,但不排除部分业务处理方法也需要定制,所以也应该支持由外部来指定初始化的options。
  2. disabled: 是否可写

基础文本模块的封装

一开始考虑直接在组件中添加/重写模块,分为以下4步:

  1. 组件引入格式类
  2. 组件引入handler.js
  3. 统一增加对应的工具栏图标
  4. 注册引入的格式
  5. 绑定点击工具栏图标的handler
···
  // 1.引入对应的格式类
  import Blockquote, {BlockquoteItem} from './editor-blot/blockquote.js'
  import Divider from './editor-blot/divider.js'
// 2.引入handler
  import {undo, redo, insertDivider, copyFormat} from './editor-blot/handler.js'
···
  init () {
        Quill.register('modules/imageResize', ImageResize)
        // 3.图标增加
        this.addIcons()
        // 4.重写/添加格式功能
        this.registerFormats()
        this.options = {
          modules: {
            toolbar: {
              container: this.$refs.toolbar,
              // 5.改写handler
              handlers: {
                'undo': () => { undo(this.quill) },
                'redo': () => { redo(this.quill) },
                'divider': () => { insertDivider(this.quill) },
                'formatBrush': () => { copyFormat(this.quill, this.copyFormatting) },
                'emoji': function () {},
                'image': () => {
                  this.img.show = true
                }
              }
            },
            imageResize: {},
            toolbar_emoji: true,
            short_name_emoji: true,
            textarea_emoji: false,
            tooltip: true
          },
          readOnly: this.readOnly,
          placeholder: '请输入内容',
          theme: 'snow'
        }
        this.quill = new Quill(this.$refs.editor, this.options)
      },
      /* 添加icons */
      addIcons () {
        let icons = Quill.import('ui/icons')
        for (let key in ToolIcons) {
          icons[key] = ToolIcons[key]
        }
      },
      /* 注册格式 */
      registerFormats () {
        // 字号重写
        let Size = Quill.import('attributors/style/size')
        Size.whitelist = ['12px', '14px', '16px', '18px', '20px', '24px']
        Quill.register({
          // 字号
          'formats/size': Size,
          // 段落
          'formats/blockquote': Blockquote,
          'formats/blockquote-item': BlockquoteItem,
          // 分割线
          'formats/divider': Divider
        })
      },

但这样做,存在问题:

  1. 如果使用者使用options属性定制整个Quill,且又需要段落、分割线等模块,则需要将步骤1,2, 4 , 5重复写一遍。
  2. handler中需要传入组件的this.quill,意味着在使用文件需要用this.refs.editor.refs.quill来传参,显然这种方式不太友好。

为了解决这些问题,最终决定参考github的其他Quill功能模块(如quill-emoji),多一步封装过程,合并格式类和handler,将功能模块化:
1.编写格式类
2.编写模块类,编写对应的handler,并注册到Quill的modules上(模块对应的index.js)
3.使用时,(组件/外部)直接引入模块,并在options设置模块为true
这样一来,即便以后需要实现其他个性化业务,使用者也能用步骤3,简单地引用已有模块。如果需要新的模块,可以编写后在组件引入。

...
  // 引入模块
  import './quill-tooltip/tooltip-body.js'
  import './quill-blockquote/index.js'
  import './quill-divider/index.js'
  import './quill-undo-redo/index.js'
  import './quill-format-brush/index.js'
...
  init () {
        Quill.register('modules/imageResize', ImageResize)
        this.addIcons()
        this.registerFont()
        let toolbarOptions = this.toolbarOptions || this.defaultToolbarOptions
        this.defaultOptions = {
          modules: {
            toolbar: {
              container: toolbarOptions,
              handlers: {
                'emoji': () => {},
                'image': () => { this.img.show = true }
              }
            },
            imageResize: {},
            toolbar_emoji: true,
            short_name_emoji: true,
            textarea_emoji: false,
            //  设置需要的模块为true,从外部传入模块时,直接设置即可
            tooltip: true,
            divider: true,
            undo_redo: true,
            format_brush: true
          },
          placeholder: '请输入内容',
          theme: 'snow'
        }
        let options = this.options || this.defaultOptions
        this.quill = new Quill(this.$refs.editor, options)
        this.quill.enable(false)
        this.initContent()
        if (!this.disabled) {
          this.quill.enable(true)
        }
        this.addEvents()
      }
上一篇下一篇

猜你喜欢

热点阅读