tiptap HorizontalRule 横线节点

tiptap 的 HorizontalRule 节点扩展功能相当于 html 里面的 hr 标签,将光标所在的内容用横线分隔。

Install 安装

npm install @tiptap/extension-horizontal-rule

Settings 配置

HTMLAttributes 自定义标签对应的HTML属性。

HorizontalRule.configure({
  HTMLAttributes: {
    class: 'custom-class',
  },
})

levels 设置支持的标题级别。

Heading.configure({
  levels: [1, 2],
})

Commands 命令

HorizontalRule 将在光标位置的内容用横线分隔。

editor.commands.setHorizontalRule()

源代码

HorizontalRule 源代码

在线例子

vue 在线例子

HorizontalRule 例子

  • Vue 例子

  • React 例子

<template>
  <div v-if="editor">
    <button @click="editor.chain().focus().setHorizontalRule().run()">
      setHorizontalRule
    </button>
    <editor-content :editor="editor" />
  </div>
</template>
<script>
import Document from '@tiptap/extension-document'
import HorizontalRule from '@tiptap/extension-horizontal-rule'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { Editor, EditorContent } from '@tiptap/vue-3'
export default {
  components: {
    EditorContent,
  },
  data() {
    return {
      editor: null,
    }
  },
  mounted() {
    this.editor = new Editor({
      extensions: [
        Document,
        Paragraph,
        Text,
        HorizontalRule,
      ],
      content: `
        <p>This is a paragraph.</p>
        <hr>
        <p>And this is another paragraph.</p>
        <hr>
        <p>But between those paragraphs are horizontal rules.</p>
      `,
    })
  },
  beforeUnmount() {
    this.editor.destroy()
  },
}
</script>
<style>
hr.ProseMirror-selectednode {
  border-top: 1px solid #68CEF8;
}
</style>
import './styles.scss'
import Document from '@tiptap/extension-document'
import HorizontalRule from '@tiptap/extension-horizontal-rule'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { EditorContent, useEditor } from '@tiptap/react'
import React from 'react'

export default () => {
  const editor = useEditor({
    extensions: [Document, Paragraph, Text, HorizontalRule],
    content: `
        <p>This is a paragraph.</p>
        <hr>
        <p>And this is another paragraph.</p>
        <hr>
        <p>But between those paragraphs are horizontal rules.</p>
      `,
  })

  if (!editor) {
    return null
  }

  return (
    <>
      <button onClick={() => editor.chain().focus().setHorizontalRule().run()}>
        setHorizontalRule
      </button>
      <EditorContent editor={editor} />
    </>
  )
}

/*
import './styles.scss'
hr.ProseMirror-selectednode {
  border-top: 1px solid #68cef8;
}
*/

下载教程 Demo

本教程Demo的源码点击这里下载,Tiptap Demo,下载完成后运行npm i初始化依赖包。