-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ordered-list.ts
90 lines (76 loc) · 1.85 KB
/
ordered-list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { mergeAttributes, Node, wrappingInputRule } from '@tiptap/core'
export interface OrderedListOptions {
itemTypeName: string,
HTMLAttributes: Record<string, any>,
}
declare module '@tiptap/core' {
interface Commands<ReturnType> {
orderedList: {
/**
* Toggle an ordered list
*/
toggleOrderedList: () => ReturnType,
}
}
}
export const inputRegex = /^(\d+)\.\s$/
export const OrderedList = Node.create<OrderedListOptions>({
name: 'orderedList',
addOptions() {
return {
itemTypeName: 'listItem',
HTMLAttributes: {},
}
},
group: 'block list',
content() {
return `${this.options.itemTypeName}+`
},
addAttributes() {
return {
start: {
default: 1,
parseHTML: element => {
return element.hasAttribute('start')
? parseInt(element.getAttribute('start') || '', 10)
: 1
},
},
}
},
parseHTML() {
return [
{
tag: 'ol',
},
]
},
renderHTML({ HTMLAttributes }) {
const { start, ...attributesWithoutStart } = HTMLAttributes
return start === 1
? ['ol', mergeAttributes(this.options.HTMLAttributes, attributesWithoutStart), 0]
: ['ol', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
},
addCommands() {
return {
toggleOrderedList: () => ({ commands }) => {
return commands.toggleList(this.name, this.options.itemTypeName)
},
}
},
addKeyboardShortcuts() {
return {
'Mod-Shift-7': () => this.editor.commands.toggleOrderedList(),
}
},
addInputRules() {
return [
wrappingInputRule({
find: inputRegex,
type: this.type,
getAttributes: match => ({ start: +match[1] }),
joinPredicate: (match, node) => node.childCount + node.attrs.start === +match[1],
}),
]
},
})