tiptap Subscript 下标

tiptap Subscript下标扩展,和html的sub标签和内联样式的vertical-align: sub有相同的效果。

Install 安装

npm install @tiptap/extension-subscript

Settings 配置

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

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

Commands 命令

setSubscript 标记选中文本为下标。

editor.commands.setSubscript()

toggleSubscript 切换下标。

editor.commands.toggleSubscript()

unsetSubscript 移除选中文本下标。

editor.commands.unsetSubscript()

快捷键

CommandWindows/LinuxmacOS
toggleSubscript()Control ,Cmd ,

源代码

subscript 源代码

在线例子

vue 在线例子

Subscript 例子

  • Vue 例子

  • React 例子

<template>
  <div v-if="editor">
    <button @click="editor.chain().focus().toggleSubscript().run()" :class="{ 'is-active': editor.isActive('subscript') }">
      toggleSubscript
    </button>
    <button @click="editor.chain().focus().setSubscript().run()" :disabled="editor.isActive('subscript')">
      setSubscript
    </button>
    <button @click="editor.chain().focus().unsetSubscript().run()" :disabled="!editor.isActive('subscript')">
      unsetSubscript
    </button>
    <editor-content :editor="editor" />
  </div>
</template>
<script>
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Subscript from '@tiptap/extension-subscript'
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,
        Subscript,
      ],
      content: `
        <p>正常文本</p>
        <p>这个是<sub>下标</sub></p>
        <p>这个也是<span style="vertical-align: sub"下标</span></p>
      `,
    })
  },
  beforeUnmount() {
    this.editor.destroy()
  },
}
</script>
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Subscript from '@tiptap/extension-subscript'
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, Subscript],
    content: `
        <p>这个是正常文本</p>
        <p>这个是<sub>下标</sub></p>
        <p>这个也是<span style="vertical-align: sub"下标</span></p>
      `,
  })

  if (!editor) {
    return null
  }

  return (
    <>
      <button
        onClick={() => editor.chain().focus().toggleSubscript().run()}
        className={editor.isActive('subscript') ? 'is-active' : ''}
      >
        toggleSubscript
      </button>
      <button
        onClick={() => editor.chain().focus().setSubscript().run()}
        disabled={editor.isActive('subscript')}
      >
        setSubscript
      </button>
      <button
        onClick={() => editor.chain().focus().unsetSubscript().run()}
        disabled={!editor.isActive('subscript')}
      >
        unsetSubscript
      </button>
      <EditorContent editor={editor} />
    </>
  )
}

下载教程 Demo

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