-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
list-item.ts
230 lines (207 loc) · 5.94 KB
/
list-item.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Copyright 2021, Milkdown by Mirone. */
import { commandsCtx } from '@milkdown/core'
import { expectDomTypeError } from '@milkdown/exception'
import { liftListItem, sinkListItem, splitListItem } from '@milkdown/prose/schema-list'
import { $command, $nodeAttr, $nodeSchema, $useKeymap } from '@milkdown/utils'
import { type Command, TextSelection } from '@milkdown/prose/state'
import type { Ctx } from '@milkdown/ctx'
import { withMeta } from '../__internal__'
/// HTML attributes for list item node.
export const listItemAttr = $nodeAttr('listItem')
withMeta(listItemAttr, {
displayName: 'Attr<listItem>',
group: 'ListItem',
})
/// Schema for list item node.
export const listItemSchema = $nodeSchema('list_item', ctx => ({
group: 'listItem',
content: 'paragraph block*',
attrs: {
label: {
default: '•',
},
listType: {
default: 'bullet',
},
spread: {
default: 'true',
},
},
defining: true,
parseDOM: [
{
tag: 'li',
getAttrs: (dom) => {
if (!(dom instanceof HTMLElement))
throw expectDomTypeError(dom)
return {
label: dom.dataset.label,
listType: dom.dataset['list-type'],
spread: dom.dataset.spread,
}
},
},
],
toDOM: node => [
'li',
{
...ctx.get(listItemAttr.key)(node),
'data-label': node.attrs.label,
'data-list-type': node.attrs.listType,
'data-spread': node.attrs.spread,
},
0,
],
parseMarkdown: {
match: ({ type }) => type === 'listItem',
runner: (state, node, type) => {
const label = node.label != null ? `${node.label}.` : '•'
const listType = node.label != null ? 'ordered' : 'bullet'
const spread = node.spread != null ? `${node.spread}` : 'true'
state.openNode(type, { label, listType, spread })
state.next(node.children)
state.closeNode()
},
},
toMarkdown: {
match: node => node.type.name === 'list_item',
runner: (state, node) => {
state.openNode('listItem', undefined, { spread: node.attrs.spread === 'true' })
state.next(node.content)
state.closeNode()
},
},
}))
withMeta(listItemSchema.node, {
displayName: 'NodeSchema<listItem>',
group: 'ListItem',
})
withMeta(listItemSchema.ctx, {
displayName: 'NodeSchemaCtx<listItem>',
group: 'ListItem',
})
/// The command to sink list item.
///
/// For example:
/// ```md
/// * List item 1
/// * List item 2 <- cursor here
/// ```
/// Will get:
/// ```md
/// * List item 1
/// * List item 2
/// ```
export const sinkListItemCommand = $command('SinkListItem', ctx => () => sinkListItem(listItemSchema.type(ctx)))
withMeta(sinkListItemCommand, {
displayName: 'Command<sinkListItemCommand>',
group: 'ListItem',
})
/// The command to lift list item.
///
/// For example:
/// ```md
/// * List item 1
/// * List item 2 <- cursor here
/// ```
/// Will get:
/// ```md
/// * List item 1
/// * List item 2
/// ```
export const liftListItemCommand = $command('SplitListItem', ctx => () => liftListItem(listItemSchema.type(ctx)))
withMeta(liftListItemCommand, {
displayName: 'Command<liftListItemCommand>',
group: 'ListItem',
})
/// The command to split a list item.
///
/// For example:
/// ```md
/// * List item 1
/// * List item 2 <- cursor here
/// ```
/// Will get:
/// ```md
/// * List item 1
/// * List item 2
/// * <- cursor here
/// ```
export const splitListItemCommand = $command('SplitListItem', ctx => () => splitListItem(listItemSchema.type(ctx)))
withMeta(splitListItemCommand, {
displayName: 'Command<splitListItemCommand>',
group: 'ListItem',
})
function liftFirstListItem(ctx: Ctx): Command {
return (state, dispatch, view) => {
const { selection } = state
if (!(selection instanceof TextSelection))
return false
const { empty, $from } = selection
// selection should be empty and at the start of the node
if (!empty || $from.parentOffset !== 0)
return false
const parentItem = $from.node(-1)
// selection should be in list item and list item should be the first child of the list
if (parentItem.type !== listItemSchema.type(ctx) || parentItem.firstChild !== $from.node())
return false
const list = $from.node(-2)
// list should have only one list item
if (list.childCount > 1)
return false
return liftListItem(listItemSchema.type(ctx))(state, dispatch, view)
}
}
/// The command to remove list item **only if**:
///
/// - Selection is at the start of the list item.
/// - List item is the only child of the list.
///
/// Most of the time, you shouldn't use this command directly.
export const liftFirstListItemCommand = $command('LiftFirstListItem', ctx => () => liftFirstListItem(ctx))
withMeta(liftFirstListItemCommand, {
displayName: 'Command<liftFirstListItemCommand>',
group: 'ListItem',
})
/// Keymap for list item node.
/// - `<Enter>`: Split the current list item.
/// - `<Tab>/<Mod-]>`: Sink the current list item.
/// - `<Shift-Tab>/<Mod-[>`: Lift the current list item.
export const listItemKeymap = $useKeymap('listItemKeymap', {
NextListItem: {
shortcuts: 'Enter',
command: (ctx) => {
const commands = ctx.get(commandsCtx)
return () => commands.call(splitListItemCommand.key)
},
},
SinkListItem: {
shortcuts: ['Tab', 'Mod-]'],
command: (ctx) => {
const commands = ctx.get(commandsCtx)
return () => commands.call(sinkListItemCommand.key)
},
},
LiftListItem: {
shortcuts: ['Shift-Tab', 'Mod-['],
command: (ctx) => {
const commands = ctx.get(commandsCtx)
return () => commands.call(liftListItemCommand.key)
},
},
LiftFirstListItem: {
shortcuts: ['Backspace', 'Delete'],
command: (ctx) => {
const commands = ctx.get(commandsCtx)
return () => commands.call(liftFirstListItemCommand.key)
},
},
})
withMeta(listItemKeymap.ctx, {
displayName: 'KeymapCtx<listItem>',
group: 'ListItem',
})
withMeta(listItemKeymap.shortcuts, {
displayName: 'Keymap<listItem>',
group: 'ListItem',
})