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-23 04:40:40 +08:00
|
|
|
const Heading = createNode({
|
2020-10-22 18:34:49 +08:00
|
|
|
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 {
|
2020-10-23 04:40:40 +08:00
|
|
|
/**
|
|
|
|
* heading command
|
|
|
|
*/
|
|
|
|
heading: (options: { level: Level }): Command => ({ commands }) => {
|
|
|
|
return commands.toggleBlockType('heading', 'paragraph', options)
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
},
|
|
|
|
})
|
2020-10-23 04:40:40 +08:00
|
|
|
|
|
|
|
export default Heading
|
|
|
|
|
|
|
|
declare module '@tiptap/core/src/Editor' {
|
|
|
|
interface AllExtensions {
|
|
|
|
Heading: typeof Heading,
|
|
|
|
}
|
|
|
|
}
|