tiptap/packages/extension-hard-break/index.ts

39 lines
867 B
TypeScript
Raw Normal View History

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