javascript - tiptap - 在当前节点下方/末尾插入节点

标签 javascript reactjs tiptap prose-mirror

我正在尝试使用 tiptap 创建一个文本编辑器使用 reactjs。我想在编辑器的每个“ block ”旁边创建一个按钮(paragraph block ,blockquote block ,codeblock block ,...),允许用户在选定之前添加一个新的空 block 堵塞。它看起来像这样(概念编辑器):
Add block below current demo
所以我尝试这样做的方法是将光标的位置设置在当前节点的末尾:

src/components/blocks/Paragraph.js

const Paragraph = (props) => {
    // ...

    

    return {
        // ...
        <button onMouseDown={() => {
            // ...

            // props.getPos() gives the position at the beginning of the block
            // props.node.nodeSize gives the "length" of the node
            const endPos = props.getPos() + props.node.nodeSize;
            props.editor.commands.focus(endPos);

            // ...
        }}>
             Add block below
        </button>
    }
}
所以在这一点上,它起作用了。但是,当我尝试插入一个新的 node在这个位置...
// It will insert a paragraph node containing text "New block added"
props.editor.commands.insertContent({
    "type":"paragraph",
    "content":[
        {
            "type":"text",
            "text":"New block added"
        }
    ]
})
...我收到一个错误:TypeError: Cannot read property 'nodeType' of null因此,为了让您详细了解此错误,我在 codesandbox.io 上制作了一个沙箱。要重现错误,您只需将注意力集中在编辑器上,随意写一些东西,然后单击 +按钮。您应该看到错误。
Error when adding a new block
Edit react-passing-local-state-to-parent-component
在此先感谢您的帮助 !

最佳答案

当前解决方案

目前我找到的最佳解决方案:

const endPos = props.getPos() + props.node.nodeSize

// I focus the start of the editor because
// when the cursor is at the end of the node below which 
// we want to add a block, it doesn't focus the next block
props.editor.commands.focus('start')

props.editor
    .chain()
    .insertContentAt(endPos, {type: "paragraph"})
    .focus(endPos)
    .run()

关于javascript - tiptap - 在当前节点下方/末尾插入节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68146588/

相关文章:

javascript - mysql2 中的 promise

javascript - SVG 顶部的 React 单选按钮组的高级定位和大小调整

reactjs - 在react js中通过getDerivedStateFromProps在状态改变后调用方法

javascript - Tiptap (Prosemirror) 提及标签的协作模式错误

javascript - 粘贴纯文本时如何更改 tiptap 的行为?

javascript - 连接到 neo4j 时,aws lambda 给出 "Unable to deserialize request: Unexpected end-of-input: expected close marker for ARRAY"

javascript - Bootstrap selectpicker "data-subtext"不更新实时更改

javascript - Next.js 中奇怪的命令式 onScroll 路由行为(仅在服务器上呈现)

javascript - React组件中的绑定(bind)方法

javascript - 输入期间匹配单个单词的正则表达式规则 (TipTap InputRule)