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

52 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-09-23 02:45:30 +08:00
import { Command, Node } from '@tiptap/core'
import { wrappingInputRule } from 'prosemirror-inputrules'
export type OrderedListCommand = () => Command
declare module '@tiptap/core/src/Editor' {
interface Commands {
orderedList: OrderedListCommand,
}
}
export default new Node()
.name('ordered_list')
.schema(() => ({
attrs: {
order: {
default: 1,
},
},
content: 'list_item+',
group: 'block',
parseDOM: [{
tag: 'ol',
getAttrs: node => ({
order: (node as HTMLElement).hasAttribute('start')
2020-09-23 03:54:52 +08:00
? parseInt((node as HTMLElement).getAttribute('start') || '')
2020-09-23 02:45:30 +08:00
: 1,
}),
}],
toDOM: node => (node.attrs.order === 1
? ['ol', 0]
: ['ol', { start: node.attrs.order }, 0]
),
}))
.commands(({ name }) => ({
orderedList: () => ({ commands }) => {
return commands.toggleList(name, 'list_item')
},
}))
.keys(({ editor }) => ({
'Shift-Ctrl-9': () => editor.orderedList(),
}))
.inputRules(({ type }) => [
wrappingInputRule(
/^(\d+)\.\s$/,
type,
match => ({ order: +match[1] }),
(match, node) => node.childCount + node.attrs.order === +match[1],
),
])
.create()