-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathcore.js
209 lines (184 loc) · 5 KB
/
core.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
import AtRange from '../commands/at-range'
import ByPath from '../commands/by-path'
import Commands from './commands'
import OnHistory from '../commands/on-history'
import OnSelection from '../commands/on-selection'
import OnValue from '../commands/on-value'
import Queries from './queries'
import Schema from './schema'
import Text from '../models/text'
import WithIntent from '../commands/with-intent'
/**
* A plugin that defines the core Slate logic.
*
* @param {Object} options
* @return {Object}
*/
function CorePlugin(options = {}) {
const { plugins = [] } = options
/**
* The core Slate commands.
*
* @type {Object}
*/
const commands = Commands({
...AtRange,
...ByPath,
...OnHistory,
...OnSelection,
...OnValue,
...WithIntent,
})
/**
* The core Slate queries.
*
* @type {Object}
*/
const queries = Queries({
isAtomic: () => false,
isVoid: () => false,
})
/**
* The core Slate schema.
*
* @type {Object}
*/
const schema = Schema({
rules: [
// Only allow block nodes in documents.
{
match: { object: 'document' },
nodes: [
{
match: { object: 'block' },
},
],
},
// Only allow block nodes or inline and text nodes in blocks.
{
match: {
object: 'block',
first: { object: 'block' },
},
nodes: [
{
match: { object: 'block' },
},
],
},
{
match: {
object: 'block',
first: [{ object: 'inline' }, { object: 'text' }],
},
nodes: [
{
match: [{ object: 'inline' }, { object: 'text' }],
},
],
},
// Only allow inline and text nodes in inlines.
{
match: { object: 'inline' },
nodes: [{ match: [{ object: 'inline' }, { object: 'text' }] }],
},
// Ensure that block and inline nodes have at least one text child.
{
match: [{ object: 'block' }, { object: 'inline' }],
nodes: [{ min: 1 }],
normalize: (editor, error) => {
const { code, node } = error
if (code === 'child_min_invalid' && node.nodes.isEmpty()) {
editor.insertNodeByKey(node.key, 0, Text.create())
}
},
},
// Ensure that inline nodes are surrounded by text nodes.
{
match: { object: 'block' },
first: [{ object: 'block' }, { object: 'text' }],
last: [{ object: 'block' }, { object: 'text' }],
normalize: (editor, error) => {
const { code, node } = error
const text = Text.create()
let i
if (code === 'first_child_object_invalid') {
i = 0
} else if (code === 'last_child_object_invalid') {
i = node.nodes.size
} else {
return
}
editor.insertNodeByKey(node.key, i, text)
},
},
{
match: { object: 'inline' },
first: [{ object: 'block' }, { object: 'text' }],
last: [{ object: 'block' }, { object: 'text' }],
previous: [{ object: 'block' }, { object: 'text' }],
next: [{ object: 'block' }, { object: 'text' }],
normalize: (editor, error) => {
const { code, node, index } = error
const text = Text.create()
let i
if (code === 'first_child_object_invalid') {
i = 0
} else if (code === 'last_child_object_invalid') {
i = node.nodes.size
} else if (code === 'previous_sibling_object_invalid') {
i = index
} else if (code === 'next_sibling_object_invalid') {
i = index + 1
} else {
return
}
editor.insertNodeByKey(node.key, i, text)
},
},
// Merge adjacent text nodes with the same marks.
{
match: { object: 'text' },
next: (next, match) => {
return next.object !== 'text' || !match.marks.equals(next.marks)
},
normalize: (editor, error) => {
const { code, next } = error
if (code === 'next_sibling_invalid') {
editor.mergeNodeByKey(next.key)
}
},
},
// Remove extra adjacent empty text nodes.
{
match: { object: 'text' },
previous: prev => {
return prev.object !== 'text' || prev.text !== ''
},
next: next => {
return next.object !== 'text' || next.text !== ''
},
normalize: (editor, error) => {
const { code, next, previous } = error
if (code === 'next_sibling_invalid') {
editor.removeNodeByKey(next.key)
} else if (code === 'previous_sibling_invalid') {
editor.removeNodeByKey(previous.key)
}
},
},
],
})
/**
* Return the plugins.
*
* @type {Array}
*/
return [schema, ...plugins, commands, queries]
}
/**
* Export.
*
* @type {Object}
*/
export default CorePlugin