Files
tiptap/packages/extension-bullet-list/src/index.ts
2020-11-18 11:33:33 +01:00

64 lines
1.1 KiB
TypeScript

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