Files
tiptap/packages/extension-bullet-list/src/bullet-list.ts
2021-06-04 21:56:29 +02:00

62 lines
1.1 KiB
TypeScript

import { Node, mergeAttributes } from '@tiptap/core'
import { wrappingInputRule } from 'prosemirror-inputrules'
export interface BulletListOptions {
HTMLAttributes: Record<string, any>,
}
declare module '@tiptap/core' {
interface Commands<ReturnType> {
bulletList: {
/**
* Toggle a bullet list
*/
toggleBulletList: () => ReturnType,
}
}
}
export const inputRegex = /^\s*([-+*])\s$/
export const BulletList = Node.create<BulletListOptions>({
name: 'bulletList',
defaultOptions: {
HTMLAttributes: {},
},
group: 'block list',
content: 'listItem+',
parseHTML() {
return [
{ tag: 'ul' },
]
},
renderHTML({ HTMLAttributes }) {
return ['ul', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
addCommands() {
return {
toggleBulletList: () => ({ commands }) => {
return commands.toggleList('bulletList', 'listItem')
},
}
},
addKeyboardShortcuts() {
return {
'Mod-Shift-8': () => this.editor.commands.toggleBulletList(),
}
},
addInputRules() {
return [
wrappingInputRule(inputRegex, this.type),
]
},
})