tiptap 插入内容
insertContent
命令将传递的值添加到文档中。
参数
value: 内容
该命令非常灵活,可以接受纯文本、HTML 或甚至 JSON 作为值。
使用示例
// 纯文本
editor.commands.insertContent("示例文本");
// HTML
editor.commands.insertContent("<h1>示例文本</h1>");
// 去除空白的 HTML
editor.commands.insertContent("<h1>示例文本</h1>", {
parseOptions: {
preserveWhitespace: false,
},
});
// JSON/节点
editor.commands.insertContent({
type: "标题",
attrs: {
level: 1,
},
content: [
{
type: "文本",
text: "示例文本",
},
],
});
// 一次插入多个节点
editor.commands.insertContent([
{
type: "段落",
content: [
{
type: "文本",
text: "第一段落",
},
],
},
{
type: "段落",
content: [
{
type: "文本",
text: "第二段落",
},
],
},
]);