feat: escape code blocks on triple enter, fix #2195

This commit is contained in:
Philipp Kühn 2021-12-05 16:47:02 +01:00
parent 8347f58167
commit d48fb24ee2

View File

@ -118,6 +118,34 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
return false return false
}, },
// escape node on triple enter
Enter: ({ editor }) => {
const { state } = editor
const { selection } = state
const { $from, empty } = selection
if (!empty || $from.parent.type !== this.type) {
return false
}
const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2
const endsWithDoubleNewline = $from.parent.textContent.endsWith('\n\n')
if (!isAtEnd || !endsWithDoubleNewline) {
return false
}
return editor
.chain()
.command(({ tr }) => {
tr.delete($from.pos - 2, $from.pos)
return true
})
.exitCode()
.run()
},
} }
}, },