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

55 lines
1.0 KiB
TypeScript
Raw Normal View History

2020-10-22 18:34:49 +08:00
import { Command, createNode } from '@tiptap/core'
2020-11-04 22:40:32 +08:00
import { exitCode } from 'prosemirror-commands'
2020-09-10 17:42:55 +08:00
2020-10-23 04:40:40 +08:00
const HardBreak = createNode({
2020-10-22 18:34:49 +08:00
name: 'hardBreak',
inline: true,
group: 'inline',
selectable: false,
parseHTML() {
return [
2020-09-10 17:42:55 +08:00
{ tag: 'br' },
2020-10-22 18:34:49 +08:00
]
},
renderHTML({ attributes }) {
return ['br', attributes]
},
addCommands() {
return {
2020-11-04 22:40:32 +08:00
hardBreak: (): Command => ({ commands, state, dispatch }) => {
return commands.try([
() => exitCode(state, dispatch),
() => {
if (dispatch) {
state.tr.replaceSelectionWith(this.type.create()).scrollIntoView()
2020-11-02 21:46:18 +08:00
}
return true
},
2020-11-04 22:40:32 +08:00
])
2020-10-22 18:34:49 +08:00
},
}
},
addKeyboardShortcuts() {
return {
'Mod-Enter': () => this.editor.commands.hardBreak(),
'Shift-Enter': () => this.editor.commands.hardBreak(),
2020-10-22 18:34:49 +08:00
}
},
})
2020-10-23 04:40:40 +08:00
export default HardBreak
2020-11-11 04:18:22 +08:00
declare module '@tiptap/core' {
2020-10-23 04:40:40 +08:00
interface AllExtensions {
HardBreak: typeof HardBreak,
}
}