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

72 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-10-22 18:34:49 +08:00
import { Command, createNode } from '@tiptap/core'
2020-09-23 02:45:30 +08:00
import { wrappingInputRule } from 'prosemirror-inputrules'
2020-10-23 04:40:40 +08:00
const OrderedList = createNode({
2020-10-22 18:34:49 +08:00
name: 'ordered_list',
content: 'list_item+',
group: 'block',
addAttributes() {
return {
2020-09-23 02:45:30 +08:00
order: {
default: 1,
2020-10-22 18:34:49 +08:00
rendered: false,
},
}
},
parseHTML() {
return [
{
tag: 'ol',
getAttrs: node => ({
order: (node as HTMLElement).hasAttribute('start')
? parseInt((node as HTMLElement).getAttribute('start') || '', 10)
: 1,
}),
2020-09-23 02:45:30 +08:00
},
2020-10-22 18:34:49 +08:00
]
},
renderHTML({ node, attributes }) {
return node.attrs.order === 1
? ['ol', attributes, 0]
: ['ol', { ...attributes, start: node.attrs.order }, 0]
},
addCommands() {
return {
2020-10-23 04:40:40 +08:00
orderedList: (): Command => ({ commands }) => {
2020-10-22 18:34:49 +08:00
return commands.toggleList('ordered_list', 'list_item')
},
}
},
addKeyboardShortcuts() {
return {
'Shift-Control-9': () => this.editor.orderedList(),
}
},
addInputRules() {
return [
wrappingInputRule(
/^(\d+)\.\s$/,
this.type,
match => ({ order: +match[1] }),
(match, node) => node.childCount + node.attrs.order === +match[1],
),
]
},
})
2020-10-23 04:40:40 +08:00
export default OrderedList
declare module '@tiptap/core/src/Editor' {
interface AllExtensions {
OrderedList: typeof OrderedList,
}
}