From 7343c518aeac75dbeeeb1db5a7be9e6e983a4bab Mon Sep 17 00:00:00 2001 From: bdbch <6538827+bdbch@users.noreply.github.com> Date: Tue, 30 Apr 2024 17:22:37 +0200 Subject: [PATCH] fix issue with code pasting from VS Code when at the last line of code (#5106) --- packages/extension-code-block/src/code-block.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/extension-code-block/src/code-block.ts b/packages/extension-code-block/src/code-block.ts index 9f8619487e4..ecb5a42e561 100644 --- a/packages/extension-code-block/src/code-block.ts +++ b/packages/extension-code-block/src/code-block.ts @@ -260,8 +260,15 @@ export const CodeBlock = Node.create({ const { tr } = view.state - // create an empty code block - tr.replaceSelectionWith(this.type.create({ language })) + // 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))))