2020-10-22 18:34:49 +08:00
|
|
|
import { Command, createNode } from '@tiptap/core'
|
2020-09-10 17:42:55 +08:00
|
|
|
import { chainCommands, exitCode } from 'prosemirror-commands'
|
|
|
|
|
2020-10-22 18:34:49 +08:00
|
|
|
// export type HardBreakCommand = () => Command
|
|
|
|
|
|
|
|
// declare module '@tiptap/core/src/Editor' {
|
|
|
|
// interface Commands {
|
|
|
|
// hardBreak: HardBreakCommand,
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
export default createNode({
|
|
|
|
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 {
|
|
|
|
hardBreak: () => ({
|
|
|
|
tr, state, dispatch, view,
|
|
|
|
}) => {
|
|
|
|
return chainCommands(exitCode, () => {
|
|
|
|
dispatch(tr.replaceSelectionWith(this.type.create()).scrollIntoView())
|
|
|
|
return true
|
|
|
|
})(state, dispatch, view)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addKeyboardShortcuts() {
|
|
|
|
return {
|
|
|
|
'Mod-Enter': () => this.editor.hardBreak(),
|
|
|
|
'Shift-Enter': () => this.editor.hardBreak(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|