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

38 lines
879 B
TypeScript
Raw Normal View History

2020-09-22 15:08:08 +08:00
import { Command, Node } from '@tiptap/core'
2020-09-10 17:42:55 +08:00
import { chainCommands, exitCode } from 'prosemirror-commands'
2020-09-22 15:08:08 +08:00
export type HardBreakCommand = () => Command
2020-09-10 17:42:55 +08:00
declare module '@tiptap/core/src/Editor' {
2020-09-22 16:49:38 +08:00
interface Commands {
2020-09-22 15:08:08 +08:00
hardBreak: HardBreakCommand,
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'],
}))
2020-09-24 06:29:05 +08:00
.commands(({ type }) => ({
hardBreak: () => ({
tr, state, dispatch, view,
}) => {
2020-09-22 15:08:08 +08:00
return chainCommands(exitCode, () => {
dispatch(tr.replaceSelectionWith(type.create()).scrollIntoView())
return true
})(state, dispatch, view)
},
}))
2020-09-10 17:42:55 +08:00
.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()