Skip to content

Commit

Permalink
fix issue with code pasting from VS Code when at the last line of code (
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbch authored Apr 30, 2024
1 parent e73073c commit 7343c51
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/extension-code-block/src/code-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,15 @@ export const CodeBlock = Node.create<CodeBlockOptions>({

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))))
Expand Down

0 comments on commit 7343c51

Please sign in to comment.