强悍!基于Vue的无渲染的富文本编辑器——tiptap!

介绍

tiptap编辑器基于Prosemirror,完全可扩展且无渲染。可以轻松地将自定义节点添加为Vue组件。

 


 

强悍!基于Vue的无渲染的富文本编辑器——tiptap!

 

 


Github

https://github.com/scrumpy/tiptap

为什么使用tiptap?

市面上有不少富文本编辑器,但大多数可能并不能满足你的需求。编辑器应该易于扩展,并且不应基于旧的依赖项(例如jQuery)。对于React,已经有一个名为Slate.js的出色编辑器,其模块化给人留下深刻的印象。tiptap是基于Prosemirror进行扩展开发的没有很多公司在Prosemirror(富文本工具包)基础上进行开发。

无渲染要如何理解?

使用无渲染组件(函数式组件),你将(几乎)完全控制标记和样式。菜单的外观或在DOM中的显示位置。这完全取决于使用者。

安装和使用

  • 安装
npm install tiptap
#或者
yarn add tiptap
  • 使用
<template>
  <editor-content :editor="editor" />
</template>

<script>
// Import the editor
import { Editor, EditorContent } from 'tiptap'

export default {
  components: {
    EditorContent,
  },
  data() {
    return {
      editor: null,
    }
  },
  mounted() {
    this.editor = new Editor({
      content: '<p>This is just a boring paragraph</p>',
    })
  },
  beforeDestroy() {
    this.editor.destroy()
  },
}
</script>

Demo截图

下面是来自官方网站的一组Demo截图:

  • 基本使用

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
好的,我可以给你提供一个基于 vue2.0 的富文本编辑器的实现思路。 首先,我们需要引入一个富文本编辑器的库,比较常用的有Quill、WangEditor、TinyMCE等。这里以Quill为例进行实现。 1. 安装Quill ``` npm install quill --save ``` 2. 在Vue组件中引入Quill ```vue <template> <div id="editor"></div> </template> <script> import Quill from 'quill' export default { mounted() { this.quillEditor = new Quill('#editor', { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], // 加粗、斜体、下划线、删除线 ['blockquote', 'code-block'], // 引用、代码块 [{ 'header': 1 }, { 'header': 2 }], // 标题 [{ 'list': 'ordered' }, { 'list': 'bullet' }], // 列表 [{ 'script': 'sub' }, { 'script': 'super' }], // 上下标 [{ 'indent': '-1' }, { 'indent': '+1' }], // 缩进 [{ 'direction': 'rtl' }, { 'align': [] }], // 对齐方式 [{ 'color': [] }, { 'background': [] }], // 颜色、背景色 ['image', 'video'], // 图片、视频 ['clean'] // 清除格式 ] }, placeholder: '请输入内容', theme: 'snow' // 主题,有两个值可选:snow和bubble }) } } </script> <style> /* Quill需要的CSS */ @import "~quill/dist/quill.snow.css"; </style> ``` 这样就可以得到一个基本的富文本编辑器了。但是这个富文本编辑器不能与Vue组件的数据进行双向绑定,我们需要对Quill进行一些扩展。 3. 扩展Quill 我们需要在Quill的change事件中,把编辑器中的内容同步到Vue组件的data中。同时,当Vue组件的data中的值发生变化时,要把新的值同步到Quill中。 ```vue <template> <div id="editor" ref="editor"></div> </template> <script> import Quill from 'quill' export default { data() { return { content: '' } }, mounted() { this.quillEditor = new Quill(this.$refs.editor, { modules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], // 加粗、斜体、下划线、删除线 ['blockquote', 'code-block'], // 引用、代码块 [{ 'header': 1 }, { 'header': 2 }], // 标题 [{ 'list': 'ordered' }, { 'list': 'bullet' }], // 列表 [{ 'script': 'sub' }, { 'script': 'super' }], // 上下标 [{ 'indent': '-1' }, { 'indent': '+1' }], // 缩进 [{ 'direction': 'rtl' }, { 'align': [] }], // 对齐方式 [{ 'color': [] }, { 'background': [] }], // 颜色、背景色 ['image', 'video'], // 图片、视频 ['clean'] // 清除格式 ] }, placeholder: '请输入内容', theme: 'snow' // 主题,有两个值可选:snow和bubble }) // 把编辑器中的内容同步到Vue组件的data中 this.quillEditor.on('text-change', () => { this.content = this.quillEditor.root.innerHTML }) // 把Vue组件的data中的值同步到Quill中 this.$watch('content', (newValue) => { this.quillEditor.root.innerHTML = newValue }) } } </script> <style> /* Quill需要的CSS */ @import "~quill/dist/quill.snow.css"; </style> ``` 现在,我们就可以在Vue组件中使用这个富文本编辑器,并且能够与Vue组件的data进行双向绑定了。 ```vue <template> <div> <editor v-model="content"></editor> <div v-html="content"></div> </div> </template> <script> import Editor from './Editor.vue' export default { components: { Editor }, data() { return { content: '' } } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hotice888

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值