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

36 lines
874 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' {
interface Editor {
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-22 15:08:08 +08:00
.commands(({ editor, type }) => ({
hardBreak: () => ({ tr, state, dispatch, view }) => {
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()