add hardbreak extension

This commit is contained in:
Philipp Kühn
2020-09-10 11:42:55 +02:00
parent a255cb6e38
commit 3206b6e5eb
2 changed files with 56 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
import { Node } from '@tiptap/core'
import { chainCommands, exitCode } from 'prosemirror-commands'
declare module '@tiptap/core/src/Editor' {
interface Editor {
hardbreak(): Editor,
}
}
export default new Node()
.name('hardbreak')
.schema(() => ({
inline: true,
group: 'inline',
selectable: false,
parseDOM: [
{ tag: 'br' },
],
toDOM: () => ['br'],
}))
.commands(({ editor, type }) => ({
hardbreak: next => () => {
const { state, view } = editor
const { dispatch } = view
chainCommands(exitCode, () => {
dispatch(state.tr.replaceSelectionWith(type.create()).scrollIntoView())
return true
})(state, dispatch, view)
next()
},
}))
.keys(({ editor }) => ({
'Mod-Enter': () => editor.hardbreak(),
'Shift-Enter': () => editor.hardbreak(),
}))
.create()