2021-06-05 03:56:29 +08:00
|
|
|
import { Node, mergeAttributes } from '@tiptap/core'
|
2020-09-10 17:42:55 +08:00
|
|
|
|
2020-11-25 16:50:54 +08:00
|
|
|
export interface HardBreakOptions {
|
2021-09-23 15:34:47 +08:00
|
|
|
keepMarks: boolean,
|
2021-04-21 15:43:31 +08:00
|
|
|
HTMLAttributes: Record<string, any>,
|
2020-11-25 16:50:54 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:59:35 +08:00
|
|
|
declare module '@tiptap/core' {
|
2021-06-05 03:56:29 +08:00
|
|
|
interface Commands<ReturnType> {
|
2021-02-16 18:27:58 +08:00
|
|
|
hardBreak: {
|
|
|
|
/**
|
|
|
|
* Add a hard break
|
|
|
|
*/
|
2021-06-05 03:56:29 +08:00
|
|
|
setHardBreak: () => ReturnType,
|
2021-02-16 18:27:58 +08:00
|
|
|
}
|
2021-02-10 16:59:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 01:25:08 +08:00
|
|
|
export const HardBreak = Node.create<HardBreakOptions>({
|
2020-10-22 18:34:49 +08:00
|
|
|
name: 'hardBreak',
|
|
|
|
|
2021-10-27 00:31:13 +08:00
|
|
|
addOptions() {
|
|
|
|
return {
|
|
|
|
keepMarks: true,
|
|
|
|
HTMLAttributes: {},
|
|
|
|
}
|
2020-11-25 16:50:54 +08:00
|
|
|
},
|
|
|
|
|
2020-10-22 18:34:49 +08:00
|
|
|
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 }) {
|
2020-11-25 16:50:54 +08:00
|
|
|
return ['br', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
|
2021-09-07 01:05:01 +08:00
|
|
|
renderText() {
|
|
|
|
return '\n'
|
|
|
|
},
|
|
|
|
|
2020-10-22 18:34:49 +08:00
|
|
|
addCommands() {
|
|
|
|
return {
|
2021-09-23 15:34:47 +08:00
|
|
|
setHardBreak: () => ({
|
|
|
|
commands,
|
|
|
|
chain,
|
|
|
|
state,
|
|
|
|
editor,
|
|
|
|
}) => {
|
2020-11-18 23:43:27 +08:00
|
|
|
return commands.first([
|
2021-05-06 00:51:42 +08:00
|
|
|
() => commands.exitCode(),
|
2021-09-23 15:34:47 +08:00
|
|
|
() => commands.command(() => {
|
|
|
|
const { keepMarks } = this.options
|
|
|
|
const { splittableMarks } = editor.extensionManager
|
|
|
|
const marks = state.storedMarks
|
|
|
|
|| (state.selection.$to.parentOffset && state.selection.$from.marks())
|
|
|
|
|
|
|
|
return chain()
|
|
|
|
.insertContent({ type: this.name })
|
|
|
|
.command(({ tr, dispatch }) => {
|
|
|
|
if (dispatch && marks && keepMarks) {
|
|
|
|
const filteredMarks = marks
|
|
|
|
.filter(mark => splittableMarks.includes(mark.type.name))
|
|
|
|
|
|
|
|
tr.ensureMarks(filteredMarks)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
.run()
|
|
|
|
}),
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|