tiptap Strike 删除符

tiptap Strike 扩展会渲染成删除符,内联样式 text-decoration: line-through 有同样的效果。

Install 安装

npm install @tiptap/extension-strike

Settings 配置

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

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

Commands 命令

setStrike 标记文本为删除符。

editor.commands.setStrike()

toggleStrike 切换删除符。

editor.commands.toggleStrike()

unsetStrike 移除删除符。

editor.commands.unsetStrike()

快捷键

CommandWindows/LinuxmacOS
toggleBold()Control Shift XCmd Shift X

源代码

strike 源代码

在线例子

vue 在线例子

Strike 例子

  • 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} />
    </>
  )
}

下载教程 Demo

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