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-30 23:06:30 +08:00
|
|
|
export const inputRegex = /^(\d+)\.\s$/
|
|
|
|
|
2020-10-23 04:40:40 +08:00
|
|
|
const OrderedList = createNode({
|
2020-10-22 18:34:49 +08:00
|
|
|
name: 'ordered_list',
|
|
|
|
|
2020-10-30 22:46:15 +08:00
|
|
|
list: true,
|
|
|
|
|
2020-10-22 18:34:49 +08:00
|
|
|
group: 'block',
|
|
|
|
|
2020-10-30 20:19:06 +08:00
|
|
|
content: 'list_item+',
|
|
|
|
|
2020-10-22 18:34:49 +08:00
|
|
|
addAttributes() {
|
|
|
|
return {
|
2020-10-27 21:53:23 +08:00
|
|
|
start: {
|
2020-09-23 02:45:30 +08:00
|
|
|
default: 1,
|
2020-10-27 21:53:23 +08:00
|
|
|
parseHTML: element => ({
|
|
|
|
start: element.hasAttribute('start')
|
|
|
|
? parseInt(element.getAttribute('start') || '', 10)
|
|
|
|
: 1,
|
|
|
|
}),
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
tag: 'ol',
|
2020-09-23 02:45:30 +08:00
|
|
|
},
|
2020-10-22 18:34:49 +08:00
|
|
|
]
|
|
|
|
},
|
|
|
|
|
2020-10-27 21:53:23 +08:00
|
|
|
renderHTML({ attributes }) {
|
|
|
|
const { start, ...attributesWithoutStart } = attributes
|
|
|
|
|
|
|
|
return start === 1
|
|
|
|
? ['ol', attributesWithoutStart, 0]
|
|
|
|
: ['ol', attributes, 0]
|
2020-10-22 18:34:49 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
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(
|
2020-10-30 23:06:30 +08:00
|
|
|
inputRegex,
|
2020-10-22 18:34:49 +08:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|