-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathcore-schema-rules.js
267 lines (217 loc) · 6.43 KB
/
core-schema-rules.js
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { List } from 'immutable'
import Text from '../models/text'
/**
* Define the core schema rules, order-sensitive.
*
* @type {Array}
*/
const CORE_SCHEMA_RULES = [
/**
* Only allow block nodes in documents.
*
* @type {Object}
*/
{
validateNode(node) {
if (node.object != 'document') return
const invalids = node.nodes.filter(n => n.object != 'block')
if (!invalids.size) return
return change => {
invalids.forEach(child => {
change.removeNodeByKey(child.key, { normalize: false })
})
}
},
},
/**
* Only allow block nodes or inline and text nodes in blocks.
*
* @type {Object}
*/
{
validateNode(node) {
if (node.object != 'block') return
const first = node.nodes.first()
if (!first) return
const objects = first.object == 'block' ? ['block'] : ['inline', 'text']
const invalids = node.nodes.filter(n => !objects.includes(n.object))
if (!invalids.size) return
return change => {
invalids.forEach(child => {
change.removeNodeByKey(child.key, { normalize: false })
})
}
},
},
/**
* Only allow inline and text nodes in inlines.
*
* @type {Object}
*/
{
validateNode(node) {
if (node.object != 'inline') return
const invalids = node.nodes.filter(
n => n.object != 'inline' && n.object != 'text'
)
if (!invalids.size) return
return change => {
invalids.forEach(child => {
change.removeNodeByKey(child.key, { normalize: false })
})
}
},
},
/**
* Ensure that block and inline nodes have at least one text child.
*
* @type {Object}
*/
{
validateNode(node) {
if (node.object != 'block' && node.object != 'inline') return
if (node.nodes.size > 0) return
return change => {
const text = Text.create()
change.insertNodeByKey(node.key, 0, text, { normalize: false })
}
},
},
/**
* Ensure that inline non-void nodes are never empty.
*
* This rule is applied to all blocks and inlines, because when they contain an empty
* inline, we need to remove the empty inline from that parent node. If `validate`
* was to be memoized, it should be against the parent node, not the empty inline itself.
*
* @type {Object}
*/
{
validateNode(node) {
if (node.object != 'inline' && node.object != 'block') return
const invalids = node.nodes.filter(
child => child.object === 'inline' && child.isEmpty
)
if (!invalids.size) return
return change => {
// If all of the block's nodes are invalid, insert an empty text node so
// that the selection will be preserved when they are all removed.
if (node.nodes.size == invalids.size) {
const text = Text.create()
change.insertNodeByKey(node.key, 1, text, { normalize: false })
}
invalids.forEach(child => {
change.removeNodeByKey(child.key, { normalize: false })
})
}
},
},
/**
* Ensure that inline void nodes are surrounded by text nodes, by adding extra
* blank text nodes if necessary.
*
* @type {Object}
*/
{
validateNode(node) {
if (node.object != 'block' && node.object != 'inline') return
const invalids = node.nodes.reduce((list, child, index) => {
if (child.object !== 'inline') return list
const prev = index > 0 ? node.nodes.get(index - 1) : null
const next = node.nodes.get(index + 1)
// We don't test if "prev" is inline, since it has already been
// processed in the loop
const insertBefore = !prev
const insertAfter = !next || next.object == 'inline'
if (insertAfter || insertBefore) {
list = list.push({ insertAfter, insertBefore, index })
}
return list
}, new List())
if (!invalids.size) return
return change => {
// Shift for every text node inserted previously.
let shift = 0
invalids.forEach(({ index, insertAfter, insertBefore }) => {
if (insertBefore) {
change.insertNodeByKey(node.key, shift + index, Text.create(), {
normalize: false,
})
shift++
}
if (insertAfter) {
change.insertNodeByKey(node.key, shift + index + 1, Text.create(), {
normalize: false,
})
shift++
}
})
}
},
},
/**
* Merge adjacent text nodes.
*
* @type {Object}
*/
{
validateNode(node) {
if (node.object != 'block' && node.object != 'inline') return
const invalids = node.nodes
.map((child, i) => {
const next = node.nodes.get(i + 1)
if (child.object != 'text') return
if (!next || next.object != 'text') return
return next
})
.filter(Boolean)
if (!invalids.size) return
return change => {
// Reverse the list to handle consecutive merges, since the earlier nodes
// will always exist after each merge.
invalids.reverse().forEach(n => {
change.mergeNodeByKey(n.key, { normalize: false })
})
}
},
},
/**
* Prevent extra empty text nodes, except when adjacent to inline void nodes.
*
* @type {Object}
*/
{
validateNode(node) {
if (node.object != 'block' && node.object != 'inline') return
const { nodes } = node
if (nodes.size <= 1) return
const invalids = nodes.filter((desc, i) => {
if (desc.object != 'text') return
if (desc.text.length > 0) return
const prev = i > 0 ? nodes.get(i - 1) : null
const next = nodes.get(i + 1)
// If it's the first node, and the next is a void, preserve it.
if (!prev && next.object == 'inline') return
// It it's the last node, and the previous is an inline, preserve it.
if (!next && prev.object == 'inline') return
// If it's surrounded by inlines, preserve it.
if (next && prev && next.object == 'inline' && prev.object == 'inline')
return
// Otherwise, remove it.
return true
})
if (!invalids.size) return
return change => {
invalids.forEach(text => {
change.removeNodeByKey(text.key, { normalize: false })
})
}
},
},
]
/**
* Export.
*
* @type {Array}
*/
export default CORE_SCHEMA_RULES