Files
tiptap/packages/extension-bullet-list/src/bullet-list.ts
2021-12-11 00:13:24 +01:00

70 lines
1.3 KiB
TypeScript

import { Node, mergeAttributes, wrappingInputRule } from '@tiptap/core'
export interface BulletListOptions {
itemTypeName: string,
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',
addOptions() {
return {
itemTypeName: 'listItem',
HTMLAttributes: {},
}
},
group: 'block list',
content() {
return `${this.options.itemTypeName}+`
},
parseHTML() {
return [
{ tag: 'ul' },
]
},
renderHTML({ HTMLAttributes }) {
return ['ul', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
addCommands() {
return {
toggleBulletList: () => ({ commands }) => {
return commands.toggleList(this.name, this.options.itemTypeName)
},
}
},
addKeyboardShortcuts() {
return {
'Mod-Shift-8': () => this.editor.commands.toggleBulletList(),
}
},
addInputRules() {
return [
wrappingInputRule({
find: inputRegex,
type: this.type,
}),
]
},
})