tiptap Strike 扩展会渲染成删除符,内联样式 text-decoration: line-through 有同样的效果。
npm install @tiptap/extension-strike
HTMLAttributes 自定义标签对应的HTML属性。
Strike.configure({
HTMLAttributes: {
class: 'custom-class',
},
})
setStrike 标记文本为删除符。
editor.commands.setStrike()
toggleStrike 切换删除符。
editor.commands.toggleStrike()
unsetStrike 移除删除符。
editor.commands.unsetStrike()
Command | Windows/Linux | macOS |
---|---|---|
toggleBold() | Control Shift X | Cmd Shift X |
Vue 例子
React 例子
<template>
<div v-if="editor">
<button @click="editor.chain().focus().toggleStrike().run()" :class="{ 'is-active': editor.isActive('strike') }">
toggleStrike
</button>
<button @click="editor.chain().focus().setStrike().run()" :disabled="editor.isActive('strike')">
setStrike
</button>
<button @click="editor.chain().focus().unsetStrike().run()" :disabled="!editor.isActive('strike')">
unsetStrike
</button>
<editor-content :editor="editor" />
</div>
</template>
<script>
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Strike from '@tiptap/extension-strike'
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,
Strike,
],
content: `
<p>This isn’t striked through.</s></p>
<p><s>But that’s striked through.</s></p>
<p><del>And this.</del></p>
<p><strike>This too.</strike></p>
<p style="text-decoration: line-through">This as well.</p>
`,
})
},
beforeUnmount() {
this.editor.destroy()
},
}
</script>
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Strike from '@tiptap/extension-strike'
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, Strike],
content: `
<p>This isn’t striked through.</s></p>
<p><s>But that’s striked through.</s></p>
<p><del>And this.</del></p>
<p><strike>This too.</strike></p>
<p style="text-decoration: line-through">This as well.</p>
`,
})
if (!editor) {
return null
}
return (
<>
<button
onClick={() => editor.chain().focus().toggleStrike().run()}
className={editor.isActive('strike') ? 'is-active' : ''}
>
toggleStrike
</button>
<button
onClick={() => editor.chain().focus().setStrike().run()}
disabled={editor.isActive('strike')}
>
setStrike
</button>
<button
onClick={() => editor.chain().focus().unsetStrike().run()}
disabled={!editor.isActive('strike')}
>
unsetStrike
</button>
<EditorContent editor={editor} />
</>
)
}