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

58 lines
1.1 KiB
TypeScript
Raw Normal View History

import { Command, Node } from '@tiptap/core'
2020-11-04 22:40:32 +08:00
import { exitCode } from 'prosemirror-commands'
2020-09-10 17:42:55 +08:00
const HardBreak = Node.create({
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
]
},
2020-11-13 23:07:20 +08:00
renderHTML({ HTMLAttributes }) {
return ['br', HTMLAttributes]
2020-10-22 18:34:49 +08:00
},
addCommands() {
return {
2020-11-13 22:08:30 +08:00
/**
* Add a hard break
*/
2020-11-18 19:11:45 +08:00
setHardBreak: (): Command => ({ commands, state, dispatch }) => {
2020-11-18 23:43:27 +08:00
return commands.first([
2020-11-04 22:40:32 +08:00
() => 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 {
2020-11-18 19:11:45 +08:00
'Mod-Enter': () => this.editor.commands.setHardBreak(),
'Shift-Enter': () => this.editor.commands.setHardBreak(),
2020-10-22 18:34:49 +08:00
}
},
})
2020-10-23 04:40:40 +08:00
export default HardBreak
declare module '@tiptap/core' {
interface AllExtensions {
HardBreak: typeof HardBreak,
2020-10-23 04:40:40 +08:00
}
}