diff --git a/packages/extension-code-block/src/code-block.ts b/packages/extension-code-block/src/code-block.ts index 3c4a9f758d..7b93459840 100644 --- a/packages/extension-code-block/src/code-block.ts +++ b/packages/extension-code-block/src/code-block.ts @@ -275,25 +275,21 @@ export const CodeBlock = Node.create({ return false } - const { tr } = view.state - - // create an empty code blockĀ“ - // if the cursor is at the absolute end of the document, insert the code block before the cursor instead - // of replacing the selection as the replaceSelectionWith function will cause the insertion to - // happen at the previous node - if (view.state.selection.from === view.state.doc.nodeSize - (1 + (view.state.selection.$to.depth * 2))) { - tr.insert(view.state.selection.from - 1, this.type.create({ language })) - } else { - tr.replaceSelectionWith(this.type.create({ language })) - } - - // put cursor inside the newly created code block - tr.setSelection(TextSelection.near(tr.doc.resolve(Math.max(0, tr.selection.from - 2)))) + const { tr, schema } = view.state - // add text to code block + // prepare a text node // strip carriage return chars from text pasted as code // see: https://github.com/ProseMirror/prosemirror-view/commit/a50a6bcceb4ce52ac8fcc6162488d8875613aacd - tr.insertText(text.replace(/\r\n?/g, '\n')) + const textNode = schema.text(text.replace(/\r\n?/g, '\n')) + + // create a code block with the text node + // replace selection with the code block + tr.replaceSelectionWith(this.type.create({ language }, textNode)) + + if (tr.selection.$from.parent.type !== this.type) { + // put cursor inside the newly created code block + tr.setSelection(TextSelection.near(tr.doc.resolve(Math.max(0, tr.selection.from - 2)))) + } // store meta information // this is useful for other plugins that depends on the paste event