tiptap Underline扩展可以将选择的文本增加下划线,如果在tiptap的初始内容中使用 u标签或者使用text-decoration: underline的内联样式,它们都显示下划线。
npm install @tiptap/extension-underline
HTMLAttributes 自定义标签对应的HTML属性。
Underline.configure({
HTMLAttributes: {
class: 'custom-class',
},
})
setUnderline 标记选中文本为下标。
editor.commands.setUnderline()
toggleUnderline 切换下划线。
editor.commands.toggleUnderline()
unsetUnderline 移除选中文本下划线。
editor.commands.unsetUnderline()
Command | Windows/Linux | macOS |
---|---|---|
toggleBold() | Control U | Cmd U |
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} />
</>
)
}