quill 定制富文本编辑器

更新日期: 2019-12-21阅读: 2.9k标签: 富文本

quill 定制富文本编辑器

很多时候 <textarea> 并不能满足我们对文本输入的需求,当我们需要为输入的文本添加格式时,我们需要使用像 quill 这样的富文本编辑器来完成富文本的输入。

本文将会详细的讲解如何使用 quill 定制一个自己的富文本编辑器。

这里面定制了两个特殊的功能(添加卡片、添加图片墙),感兴趣的同学可以先看一下实现后的效果:富文本编辑器

接下来将会讲解如何在 quill 的基础上实现定制化的富文本编辑器。


quill 简介

一个好的富文本编辑器的标志就是它能够支持多少种格式,而 quill 支持无限种类的格式。你可以在 quill 上定制任意种类的文本。并且,如果你不想自定义个任何功能,那么 quill 也是极易上手的

var quill = new Quill('#editor', {
  modules: { toolbar: true },
  theme: 'snow'
})

如何在 Quill 上定制功能

quill 提供了非常细粒度、定义良好的 api,我们可以在此基础之上定制化开发自己的富文本编辑器。(环境为 vue + iview ,使用 iview 进行辅助样式开发)

首先我们从最简单的 demo 入手

<template>
  <div id="editor" class="editor"></div>
</template>
<script>
import Quill from "quill";
export default {
  mounted() {
    const quill = new Quill("#editor", {
      theme: "snow",
      placeholder: "请在此开始编辑..."
    });
  }
};
</script>

这是一个默认参数条件下的一个富文本编辑器,我们首先对 Toolbar 进行替换,Toolbar 是 modules 的一部分。


我们需要在新建 quill 实例的时候覆盖原来的 toolbar,并使用的自己的 toolbar 元素。

<template>
  <div class="container">
    <div id="toolbar">
      <Tooltip content="加粗" placement="bottom">
        <button class="ql-bold"></button>
      </Tooltip>
      <Tooltip content="倾斜" placement="bottom">
        <button class="ql-italic"></button>
      </Tooltip>
      <Tooltip content="下划线" placement="bottom">
        <button class="ql-underline"></button>
      </Tooltip>
      <Tooltip content="删除线" placement="bottom">
        <button class="ql-strike"></button>
      </Tooltip>
    </div>
    <div id="editor" class="editor"></div>
  </div>
</template>
<script>
import Quill from "quill";
export default {
  mounted() {
    const quill = new Quill("#editor", {
      theme: "snow",
      modules: {
        toolbar: "#toolbar"
      },
      placeholder: "请在此开始编辑..."
    });
  }
};
</script>
<style lang="less" scoped></style>

会将我们的 toolbar 的样式变成如下的样子。


quill 在初始化时会读取 #toolbar 元素,并获取其子节点的 classNames,对于 ql-bold ,quill 会截取 ql-之后的部分,并且和已经注册的 handlers 做匹配,上面的 bold italic underline strike 存在注册好的 handler。当我们点击 toolbar 中的元素时便会调用 handler 对富文本内容进行格式化。

当我们需要添加一个本不存在的格式化效果时我们还需要在 modules 中补充它的 handler,下面添加一个可以添加卡片的按钮,并添加其对应的 handler。

<template>
  <div class="”container“">
    <div id="toolbar">
      <Tooltip content="添加卡片" placement="bottom">
        <button class="ql-card">
          <Icon type="md-card" />
        </button>
      </Tooltip>
    </div>
    <div id="editor" class="editor"></div>
  </div>
</template>
<script>
import Quill from "quill";
export default {
  mounted() {
    const quill = new Quill("#editor", {
      theme: "snow",
      modules: {
        toolbar: {
          container: "#toolbar",
          handlers: {
            card: () => { // 属性名称要与 ql-xxx 对应
              console.log(`点击了 card`);
            }
          }
        }
      },
      placeholder: "请在此开始编辑..."
    });
  }
};
</script>
<style lang="less" scoped></style>

接下来我们为这个按钮添加实际的效果。

在 Quill 中,使用 Blots 表示节点,我们可以认为 Blots 对应 dom 中的 Node。当我们需要在 quill 中添加一自定义的 Blots 节点时,我们需要让其继承自 Blots 节点。

const BlockEmbed = Quill.import('blots/block/embed')
function customCard(node) {
  // 在一个节点中插入自定义的 dom
  const leftDiv = document.createElement('div')
  leftDiv.className = 'media-container'
  const mediaDiv = document.createElement('div')
  mediaDiv.style['background-image'] = `url(${value.image})`
  mediaDiv.className = 'media'
  leftDiv.appendChild(mediaDiv)

  const rightDiv = document.createElement('div')
  rightDiv.className = 'content-container'

  const titleP = document.createElement('p')
  titleP.className = 'title'
  titleP.innerText = value.title

  const authorP = document.createElement('p')
  authorP.className = 'author'
  authorP.innerText = value.author

  const contentP = document.createElement('p')
  contentP.className = 'content'
  contentP.innerText = value.content

  rightDiv.appendChild(titleP)
  rightDiv.appendChild(authorP)
  rightDiv.appendChild(contentP)
  node.appendChild(leftDiv)
  node.appendChild(rightDiv)
}
// 自定义卡片节点
class CardBlot extends BlockEmbed {
  static create(value) {
    const node = super.create()
    // 新建节点时传入的数据
    node.dataset.title = value.title
    node.dataset.image = value.image
    node.dataset.content = value.content
    node.dataset.author = value.author

    customizeCard(node)

    node.setAttribute('contenteditable', false) // 设置该节点不可编辑

    return node
  }
  static value(node) {
    // 这里需要返回 create 函数中传入的数据
    return node.dataset
  }
}
CardBlot.blotName = 'card' // quill 中的标记名称
CardBlot.tagName = 'div' // dom 节点名称
CardBlot.className = 'card' // dom 中真实的 class 名称
Quill.register(CardBlot)

同时我们还需要添加对应的 handler

const CARD_INFO = {
  title: 'Quill 编辑器',
  author: 'jhchen',
  content:
    'Quill是一种现代的富文本编辑器,旨在实现兼容性和可扩展性。它由Jason Chen和Byron Milligan创建,并由Slab积极维护。 ',
  image:
    'http://neau-lib-static.test.upcdn.net/quill/resource/1576812308405-0.0544z7sqq9au-quill.png'
}

new Quill('#editor', {
  theme: 'snow',
  modules: {
    toolbar: {
      container: '#toolbar',
      handlers: {
        card: () => {
          const addRange = this.quill.getSelection() || 0 // 获取当前光标选中的位置
          this.quill.insertEmbed(
            addRange,
            'card',
            CARD_INFO,
            Quill.sources.USER
          ) // 插入 card blots
          this.$nextTick(() => {
            this.quill.setSelection(addRange + 1, Quill.sources.SILENT)
          }) // 插入完成后将光标向后移动一位
        }
      }
    }
  },
  placeholder: '请在此开始编辑...'
})

至此我们就可以通过点击 card 图标来添加一个 card 了,但是这个 card 还没有添加样式,我们手动在显示 card 页面上添加样式就大功告成了。

通过这种方式,我们可以便可以在 quill 添加任意类型的内容。

链接: https://www.fly63.com/article/detial/7018

pell.js_不到200行Js代码如何实现富文本编辑器

前段时间在寻找一些关于富文本编辑器的资料,然后发现了这个名为 Pell 的项目,它是一个所见即所得(WYSIWYG)的文本编辑器,虽然它的功能很简单,但是令人吃惊的是它只有 1kb 大小。

html5中contenteditable属性如果过滤标签,过滤富文本样式

​​在div中使用contenteditable=”true”可以达到模拟输入框的效果,但是当我们复制其他网页内容进去的时候,会发现连带的样式也一起复制进去了。很明显我们不需要复制富文本样式,那么如何过滤这些标签呢?

在Vue2.0中集成UEditor 富文本编辑器

在vue的’项目中遇到了需要使用富文本编辑器的需求,在github上看了很多vue封装的editor插件,很多对图片上传和视频上传的支持并不是很好,最终还是决定使用UEditor。

angular富文本编辑器tinymce-angular

快速使用富文本,上手很迅速.tinymce-angular把tinymce分装成angular的一个组件,安装引入后,添加一个editor标签就能使用,如下安装与引入

Monaco Editor编辑器的使用

vscode是我们经常在用的编辑器,它的前身是微软的一个叫Monaco Workbench的项目,而Monaco Editor就是从这个项目中成长出来的一个web编辑器,他们很大一部分的代码都是共用的

编写一个非常简单的 JavaScript 编辑器

当然,我们已经有可以使用的很好的Web编辑器:你只需下载,并插入页面即可。我以前习惯于使用CodeMirror和ACE。例如,我为CodeMirror写了一个插件来支持PlantUML。然而,这些编辑器有一个问题:它们难以扩展和难以理解。

web前端常用的富文本编辑器插件

富文本编辑器是一种可内嵌于浏览器,所见即所得的文本编辑器。它提供类似于Office Word 的编辑功能,方便那些不太懂HTML用户使用,富文本编辑器的应用非常广泛,它的历史与图文网页诞生的历史几乎一样长。

内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!