探索 Tiptap V3 的最新功能

insertContent 命令

insertContent 命令将传递的值添加到文档中。

另请参阅: setContent, clearContent

参数

value: Content

该命令非常灵活,可以接受纯文本、HTML 甚至 JSON 作为值。

使用 insertContent 命令

// 纯文本
editor.commands.insertContent('示例文本')

// HTML
editor.commands.insertContent('<h1>示例文本</h1>')

// 去除 HTML 中的空白
editor.commands.insertContent('<h1>示例文本</h1>', {
  parseOptions: {
    preserveWhitespace: false,
  },
})

// JSON/节点
editor.commands.insertContent({
  type: 'heading',
  attrs: {
    level: 1,
  },
  content: [
    {
      type: 'text',
      text: '示例文本',
    },
  ],
})

// 一次插入多个节点
editor.commands.insertContent([
  {
    type: 'paragraph',
    content: [
      {
        type: 'text',
        text: '第一段',
      },
    ],
  },
  {
    type: 'paragraph',
    content: [
      {
        type: 'text',
        text: '第二段',
      },
    ],
  },
])