Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(extension-code-block): #3604 paste code from vscode #3606

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions packages/extension-code-block/src/code-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,21 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
return false
}

const { tr } = view.state
const { tr, schema } = view.state

// create an empty code block
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))))

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