tiptap Underline 下划线

tiptap Underline扩展可以将选择的文本增加下划线,如果在tiptap的初始内容中使用 u标签或者使用text-decoration: underline的内联样式,它们都显示下划线。

Install 安装

npm install @tiptap/extension-underline

Settings 配置

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

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

Commands 命令

setUnderline 标记选中文本为下标。

editor.commands.setUnderline()

toggleUnderline 切换下划线。

editor.commands.toggleUnderline()

unsetUnderline 移除选中文本下划线。

editor.commands.unsetUnderline()

快捷键

CommandWindows/LinuxmacOS
toggleBold()Control UCmd U

源代码

underline 源代码

在线例子

vue 在线例子

Underline 例子

  • Vue 例子

  • React 例子

<template>
  <div v-if="editor">
    <button @click="editor.chain().focus().toggleUnderline().run()" :class="{ 'is-active': editor.isActive('underline') }">
      toggleUnderline
    </button>
    <button @click="editor.chain().focus().setUnderline().run()" :disabled="editor.isActive('underline')">
      setUnderline
    </button>
    <button @click="editor.chain().focus().unsetUnderline().run()" :disabled="!editor.isActive('underline')">
      unsetUnderline
    </button>
    <editor-content :editor="editor" />
  </div>
</template>
<script>
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import Underline from '@tiptap/extension-underline'
import { Editor, EditorContent } from '@tiptap/vue-3'

export default {
  components: {
    EditorContent,
  },
  data() {
    return {
      editor: null,
    }
  },
  mounted() {
    this.editor = new Editor({
      extensions: [
        Document,
        Paragraph,
        Text,
        Underline,
      ],
      content: `
        <p>这些文字没有下划线</p>
        <p><u>U标签下划线</u></p>
        <p style="text-decoration: underline">内联样式下划线</p>
      `,
    })
  },
  beforeUnmount() {
    this.editor.destroy()
  },
}
</script>
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import Underline from '@tiptap/extension-underline'
import { EditorContent, useEditor } from '@tiptap/react'
import React from 'react'

export default () => {
  const editor = useEditor({
    extensions: [Document, Paragraph, Text, Underline],
    content: `
        <p>这些文字没有下划线</p>
        <p><u>U标签下划线</u></p>
        <p style="text-decoration: underline">内联样式下划线</p>
      `,
  })
  if (!editor) {
    return null
  }
  return (
    <>
      <button
        onClick={() => editor.chain().focus().toggleUnderline().run()}
        className={editor.isActive('underline') ? 'is-active' : ''}
      >
        toggleUnderline
      </button>
      <button
        onClick={() => editor.chain().focus().setUnderline().run()}
        disabled={editor.isActive('underline')}
      >
        setUnderline
      </button>
      <button
        onClick={() => editor.chain().focus().unsetUnderline().run()}
        disabled={!editor.isActive('underline')}
      >
        unsetUnderline
      </button>
      <EditorContent editor={editor} />
    </>
  )
}

下载教程 Demo

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