add heading keyboard shortcuts

This commit is contained in:
Hans Pagel
2020-09-25 17:17:54 +02:00
parent f71e4df72e
commit 6a78cc602f
2 changed files with 11 additions and 3 deletions

View File

@@ -33,3 +33,4 @@ editor.chain().focus().bold().run()
| .deleteSelection() | Delete the selection, if there is one. | | .deleteSelection() | Delete the selection, if there is one. |
| .focus() | Focus the editor at the given position. | | .focus() | Focus the editor at the given position. |
| .selectAll() | Select the whole document. | | .selectAll() | Select the whole document. |
| .scrollIntoView() | … |

View File

@@ -7,7 +7,7 @@ export interface HeadingOptions {
levels: Level[], levels: Level[],
} }
export type HeadingCommand = (level: Level) => Command export type HeadingCommand = (options: { level: Level }) => Command
declare module '@tiptap/core/src/Editor' { declare module '@tiptap/core/src/Editor' {
interface Commands { interface Commands {
@@ -38,11 +38,18 @@ export default new Node<HeadingOptions>()
toDOM: node => [`h${node.attrs.level}`, 0], toDOM: node => [`h${node.attrs.level}`, 0],
})) }))
.commands(({ name }) => ({ .commands(({ name }) => ({
[name]: attrs => ({ commands }) => { heading: attrs => ({ commands }) => {
return commands.toggleBlockType(name, 'paragraph', attrs) return commands.toggleBlockType(name, 'paragraph', attrs)
}, },
})) }))
// TODO: Keyboard Shortcuts .keys(({ name, options, editor }) => {
return options.levels.reduce((items, level) => ({
...items,
...{
[`Mod-Alt-${level}`]: () => editor.setBlockType(name, { level }),
},
}), {})
})
.inputRules(({ options, type }) => { .inputRules(({ options, type }) => {
return options.levels.map((level: Level) => { return options.levels.map((level: Level) => {
return textblockTypeInputRule(new RegExp(`^(#{1,${level}})\\s$`), type, { level }) return textblockTypeInputRule(new RegExp(`^(#{1,${level}})\\s$`), type, { level })