tiptap/packages/extension-bullet-list/index.ts

53 lines
914 B
TypeScript
Raw Normal View History

2020-10-22 18:34:49 +08:00
import { Command, createNode } from '@tiptap/core'
2020-09-11 04:29:15 +08:00
import { wrappingInputRule } from 'prosemirror-inputrules'
2020-10-30 23:06:30 +08:00
export const inputRegex = /^\s*([-+*])\s$/
2020-10-23 04:40:40 +08:00
const BulletList = createNode({
2020-10-22 18:34:49 +08:00
name: 'bullet_list',
2020-10-30 22:46:15 +08:00
list: true,
2020-11-02 18:16:31 +08:00
group: 'list',
2020-10-22 18:34:49 +08:00
content: 'list_item+',
2020-10-22 18:34:49 +08:00
parseHTML() {
return [
2020-09-11 04:29:15 +08:00
{ tag: 'ul' },
2020-10-22 18:34:49 +08:00
]
},
renderHTML({ attributes }) {
return ['ul', attributes, 0]
},
addCommands() {
return {
2020-10-23 04:40:40 +08:00
bulletList: (): Command => ({ commands }) => {
2020-10-22 18:34:49 +08:00
return commands.toggleList('bullet_list', 'list_item')
},
}
},
addKeyboardShortcuts() {
return {
'Shift-Control-8': () => this.editor.bulletList(),
}
},
addInputRules() {
return [
2020-10-30 23:06:30 +08:00
wrappingInputRule(inputRegex, this.type),
2020-10-22 18:34:49 +08:00
]
},
})
2020-10-23 04:40:40 +08:00
export default BulletList
declare module '@tiptap/core/src/Editor' {
interface AllExtensions {
BulletList: typeof BulletList,
}
}