2020-10-22 18:34:49 +08:00
|
|
|
import { Command, createNode } from '@tiptap/core'
|
2020-04-22 04:48:27 +08:00
|
|
|
import { textblockTypeInputRule } from 'prosemirror-inputrules'
|
2020-04-22 04:16:26 +08:00
|
|
|
|
|
|
|
type Level = 1 | 2 | 3 | 4 | 5 | 6
|
|
|
|
|
2020-09-10 00:55:19 +08:00
|
|
|
export interface HeadingOptions {
|
2020-08-22 03:01:41 +08:00
|
|
|
levels: Level[],
|
2020-04-22 04:16:26 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 18:34:49 +08:00
|
|
|
// export type HeadingCommand = (options: { level: Level }) => Command
|
2020-09-22 05:17:30 +08:00
|
|
|
|
2020-10-22 18:34:49 +08:00
|
|
|
// declare module '@tiptap/core/src/Editor' {
|
|
|
|
// interface Commands {
|
|
|
|
// heading: HeadingCommand,
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
export default createNode({
|
|
|
|
name: 'heading',
|
2020-04-22 04:16:26 +08:00
|
|
|
|
2020-10-22 18:34:49 +08:00
|
|
|
defaultOptions: <HeadingOptions>{
|
2020-09-09 05:44:45 +08:00
|
|
|
levels: [1, 2, 3, 4, 5, 6],
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
content: 'inline*',
|
|
|
|
|
|
|
|
group: 'block',
|
|
|
|
|
|
|
|
defining: true,
|
|
|
|
|
|
|
|
addAttributes() {
|
|
|
|
return {
|
2020-09-09 05:44:45 +08:00
|
|
|
level: {
|
|
|
|
default: 1,
|
2020-10-22 18:34:49 +08:00
|
|
|
rendered: false,
|
2020-04-22 04:36:31 +08:00
|
|
|
},
|
2020-10-22 18:34:49 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
parseHTML() {
|
|
|
|
return this.options.levels
|
2020-09-09 05:44:45 +08:00
|
|
|
.map((level: Level) => ({
|
|
|
|
tag: `h${level}`,
|
|
|
|
attrs: { level },
|
2020-10-22 18:34:49 +08:00
|
|
|
}))
|
|
|
|
},
|
|
|
|
|
|
|
|
renderHTML({ node, attributes }) {
|
|
|
|
return [`h${node.attrs.level}`, attributes, 0]
|
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
|
|
|
heading: attrs => ({ commands }) => {
|
|
|
|
return commands.toggleBlockType('heading', 'paragraph', attrs)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addKeyboardShortcuts() {
|
|
|
|
return this.options.levels.reduce((items, level) => ({
|
2020-09-25 23:17:54 +08:00
|
|
|
...items,
|
|
|
|
...{
|
2020-10-22 18:34:49 +08:00
|
|
|
[`Mod-Alt-${level}`]: () => this.editor.setBlockType('heading', { level }),
|
2020-09-25 23:17:54 +08:00
|
|
|
},
|
|
|
|
}), {})
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
addInputRules() {
|
|
|
|
return this.options.levels.map(level => {
|
|
|
|
return textblockTypeInputRule(new RegExp(`^(#{1,${level}})\\s$`), this.type, { level })
|
2020-09-10 00:07:17 +08:00
|
|
|
})
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
})
|