vue中使用element-tiptap富文本编辑器

element-tiptap富文本编辑器
vue中使用element-tiptap富文本编辑器_第1张图片
vue中使用element-tiptap富文本编辑器_第2张图片

npm安装

npm install element-tiptap --save

yarn安装

yarn add element-tiptap

全局引入

import Vue from 'vue';
import ElementUI from 'element-ui';
import { ElementTiptapPlugin } from 'element-tiptap';
// 引入 ElementUI 样式
import 'element-ui/lib/theme-chalk/index.css';
// 引入 import element-tiptap 样式
import 'element-tiptap/lib/index.css';

// 安装 ElementUI 插件
Vue.use(ElementUI);
// 安装 element-tiptap 插件
Vue.use(ElementTiptapPlugin, {
  lang: 'zh', // 设置语言为中文
});

局部引入

<script>
import {
  ElementTiptap,
  // 需要的 extensions
  Doc,
  Text,
  Paragraph,
  Heading,
  Bold,
  Underline,
  Italic,
  Strike,
  ListItem,
  BulletList,
  OrderedList,
  Link,
  Image,
  CodeBlock,
  Blockquote,
  TodoItem,
  TodoList,
  TextAlign,
  FontSize,
  FontType,
  Fullscreen,
  TextHighlight,
  TextColor,
  FormatClear,
  Table,
  TableHeader,
  TableCell,
  TableRow,
  History,
  TrailingNode,
  HardBreak,
  HorizontalRule,
  LineHeight,
  Indent
} from 'element-tiptap';

export default {
  data () {
    // 编辑器的 extensions
    // 它们将会按照你声明的顺序被添加到菜单栏和气泡菜单中
    return {
      extensions: [
        new Doc(), // 必须项
        new Text(), // 必须项
        new Paragraph(), // 必须项
        new Heading({ level: 6 }), // 标题
        new Bold({ bubble: true }), // 加粗 bubble: true 在气泡菜单中渲染菜单按钮
        new Underline({ bubble: true, menubar: false }), // 下划线 bubble: true, menubar: false 在气泡菜单而不在菜单栏中渲染菜单按钮
        new Italic({ bubble: true }), // 斜体
        new Strike({ bubble: true }), // 删除线
        new ListItem(), // 使用列表必须项
        new BulletList({ bubble: true }), // 无序列表
        new OrderedList({ bubble: true }), // 有序列表
        new Link(), // 链接
        new Image(), // 图片
        new CodeBlock({ bubble: true }), // 代码块
        new Blockquote(), // 引用
        new TodoItem(), // 任务列表必须项
        new TodoList(), // 任务列表
        new TextAlign({ bubble: true }), // 文本对齐方式
        new FontSize({ bubble: true }), // 字号
        new FontType({ bubble: true }), // 字体
        new Fullscreen(), // 全屏
        new TextHighlight({ bubble: true }), // 文本高亮
        new TextColor({ bubble: true }), // 文本颜色
        new FormatClear({ bubble: true }), // 清除格式
        new Table({ resizable: true }), // 表格
        new TableHeader(), // 表格必须项
        new TableCell(), // 表格必须项
        new TableRow(), // 表格必须项
        new History(), // 撤销
        new TrailingNode(), // 重做
        new HardBreak(), // 分割线
        new HorizontalRule(), // 行距
        new LineHeight(), // 增加缩进
        new Indent() // 减少缩进
      ],
      // 编辑器的内容
      content: `
        

Heading

This Editor is awesome!

`
, }; }, }, </script>

注意:局部引入同样需要在main.js中引入element-tiptap样式文件

// 引入 import element-tiptap 样式
import 'element-tiptap/lib/index.css';

使用

<template>
  <div>
    <el-tiptap
      v-model="content"
      :extensions="extensions"
      lang="zh"
    />
  div>
template>

注意:如果使用的是局部引入,需要在使用的时候指定 lang="zh",否则会报错

自定义图片上传

element-tiptap的本地图片上传默认将图片转换成base64格式,然后与内容保存在一起,可以在extensions中自定义图片的上传方式

new Image({
 // 自定义图片上传(默认会把图片生成base64字符串和内容存储到一起)
 // 返回一个 Promise
 uploadRequest(file) {
   // 自定义图片上传方式
 }
}), 

报错(Duplicate use of selection JSON ID cell)

找到根目录下的 node_modules/tiptap-extensions/node-modules,将最后的 node-modules 改为node-modules–,即可解决

element-tiptap中文文档

你可能感兴趣的:(Vue,前端,vue.js)