tiptap Bold 粗体

tiptap Bold 使文本加粗显示,如果在编辑器的初始内容中使用了strongb 标记或带有内联样式属性的文本也将以粗体展示。

Install 安装

npm install @tiptap/extension-bold

Settings 配置

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

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

Commands 命令

setBold 标记文本为粗体。

editor.commands.setBold()

toggleBold 切换粗体。

editor.commands.toggleBold()

unsetBold 移除Bold标记。

editor.commands.unsetBold()

快捷键

CommandWindows/LinuxmacOS
toggleBold()Control BCmd B

源代码

bold 源代码

在线例子

vue 在线例子

Bold 例子

  • Vue 例子

  • React 例子

<template>
  <div v-if="editor">
    <button @click="editor.chain().focus().toggleBold().run()" :class="{ 'is-active': editor.isActive('bold') }">
      toggleBold
    </button>
    <button @click="editor.chain().focus().setBold().run()" :disabled="editor.isActive('bold')">
      setBold
    </button>
    <button @click="editor.chain().focus().unsetBold().run()" :disabled="!editor.isActive('bold')">
      unsetBold
    </button>
    <editor-content :editor="editor" />
  </div>
</template>
<script>
import Bold from '@tiptap/extension-bold'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
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,
        Bold,
      ],
      content: `
        <p>This isn’t bold.</p>
        <p><strong>This is bold.</strong></p>
        <p><b>And this.</b></p>
        <p style="font-weight: bold">This as well.</p>
        <p style="font-weight: bolder">Oh, and this!</p>
        <p style="font-weight: 500">Cool, isn’t it!?</p>
        <p style="font-weight: 999">Up to font weight 999!!!</p>
      `,
    })
  },
  beforeUnmount() {
    this.editor.destroy()
  },
}
</script>
import Bold from '@tiptap/extension-bold'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
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, Bold],
    content: `
        <p>This isn’t bold.</p>
        <p><strong>This is bold.</strong></p>
        <p><b>And this.</b></p>
        <p style="font-weight: bold">This as well.</p>
        <p style="font-weight: bolder">Oh, and this!</p>
        <p style="font-weight: 500">Cool, isn’t it!?</p>
        <p style="font-weight: 999">Up to font weight 999!!!</p>
      `,
  })

  if (!editor) {
    return null
  }

  return (
    <>
      <button
        onClick={() => editor.chain().focus().toggleBold().run()}
        className={editor.isActive('bold') ? 'is-active' : ''}
      >
        toggleBold
      </button>
      <button
        onClick={() => editor.chain().focus().setBold().run()}
        disabled={editor.isActive('bold')}
      >
        setBold
      </button>
      <button
        onClick={() => editor.chain().focus().unsetBold().run()}
        disabled={!editor.isActive('bold')}
      >
        unsetBold
      </button>
      <EditorContent editor={editor} />
    </>
  )
}

下载教程 Demo

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