This repository has been archived by the owner on Feb 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
makeSchema.js
303 lines (254 loc) · 8.3 KB
/
makeSchema.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const Slate = require('slate');
const { Range, List } = require('immutable');
const createAlign = require('./createAlign');
/**
* Create a schema for tables
* @param {Options} opts The plugin options
* @return {Object} A schema definition with rules to normalize tables
*/
function makeSchema(opts) {
return {
rules: [
noBlocksWithinCell(opts),
cellsWithinTable(opts),
rowsWithinTable(opts),
tablesContainOnlyRows(opts),
rowsContainRequiredColumns(opts),
tableContainAlignData(opts)
]
};
}
/**
* Rule to enforce cells only contain inlines or text.
* It unwrap blocks in cell blocks
*
* @param {Options} opts The plugin options
* @return {Object} A rule to enforce cells only contain inlines or text.
*/
function noBlocksWithinCell(opts) {
return {
match(node) {
return node.kind == 'block' && node.type == opts.typeCell;
},
// Find nested blocks
validate(node) {
const nestedBlocks = node.nodes.filter(
child => child.kind === 'block'
);
return nestedBlocks.size > 0 ? nestedBlocks : null;
},
// If any, unwrap all nested blocks
normalize(change, node, nestedBlocks) {
nestedBlocks.forEach(
(block, blockIndex) => block.nodes.forEach((grandChild, nodeIndex) => {
const isLast = nodeIndex === block.nodes.size - 1 && blockIndex === nestedBlocks.size - 1;
change.unwrapNodeByKey(grandChild.key, { normalize: isLast });
})
);
return change;
}
};
}
/**
* Rule to enforce cells are always surrounded by a row.
*
* @param {Options} opts The plugin options
* @return {Object} A rule to ensure cells are always surrounded by a row.
*/
function cellsWithinTable(opts) {
return {
match(node) {
return (node.kind === 'document' || node.kind === 'block')
&& node.type !== opts.typeRow;
},
// Find child cells nodes not in a row
validate(node) {
const cells = node.nodes.filter((n) => {
return n.type === opts.typeCell;
});
if (cells.isEmpty()) return;
return {
cells
};
},
// If any, wrap all cells in a row block
normalize(change, node, { cells }) {
cells.forEach((cell) => {
return change.wrapBlockByKey(cell.key, opts.typeRow, { normalize: false });
});
return change;
}
};
}
/**
* Rule to enforce rows are always surrounded by a table.
*
* @param {Options} opts The plugin options
* @return {Object} A rule to ensure rows are always surrounded by a table.
*/
function rowsWithinTable(opts) {
return {
match(node) {
return (node.kind === 'document' || node.kind === 'block')
&& node.type !== opts.typeTable;
},
// Find child cells nodes not in a row
validate(node) {
const rows = node.nodes.filter((n) => {
return n.type === opts.typeRow;
});
if (rows.isEmpty()) return;
return {
rows
};
},
// If any, wrap all cells in a row block
normalize(change, node, { rows }) {
rows.forEach((row) => {
return change.wrapBlockByKey(row.key, {
type: opts.typeTable,
data: {
align: createAlign(row.nodes.size)
}
}, { normalize: false });
});
return change;
}
};
}
/**
* @param {Options} opts The plugin options
* @return {Object} A rule that ensures tables only contain rows and
* at least one.
*/
function tablesContainOnlyRows(opts) {
const isRow = (node) => node.type === opts.typeRow;
return {
match(node) {
return node.type === opts.typeTable;
},
validate(table) {
// Figure out invalid rows
const invalids = table.nodes.filterNot(isRow);
// Figure out valid rows
const add = invalids.size === table.nodes.size ? [makeEmptyRow(opts)] : [];
if (invalids.isEmpty() && add.length === 0) {
return null;
}
return {
invalids,
add
};
},
/**
* Replaces the node's children
* @param {List<Nodes>} value.nodes
*/
normalize(change, node, {invalids = [], add = []}) {
// Remove invalids
invalids.forEach((child) => {
return change.removeNodeByKey(child.key, { normalize: false });
});
// Add valids
add.forEach((child) => {
return change.insertNodeByKey(node.key, 0, child);
});
return change;
}
};
}
/**
* @param {Options} opts The plugin options
* @return {Object} A rule that ensures rows contains only cells, and
* as much cells as there is columns in the table.
*/
function rowsContainRequiredColumns(opts) {
const isRow = (node) => node.type === opts.typeRow;
const isCell = (node) => node.type === opts.typeCell;
const countCells = (row) => row.nodes.count(isCell);
return {
match(node) {
return node.type === opts.typeTable;
},
validate(table) {
const rows = table.nodes.filter(isRow);
// The number of column this table has
const columns = rows.reduce((count, row) => {
return Math.max(count, countCells(row));
}, 1); // Min 1 column
// else normalize, by padding with empty cells
const invalidRows = rows
.map(row => {
const cells = countCells(row);
const invalids = row.nodes.filterNot(isCell);
// Row is valid: right count of cells and no extra node
if (invalids.isEmpty() && cells === columns) {
return null;
}
// Otherwise, remove the invalids and append the missing cells
return {
row,
invalids,
add: (columns - cells)
};
})
.filter(Boolean);
return invalidRows.size > 0 ? invalidRows : null;
},
/**
* Updates by key every given nodes
* @param {List<Nodes>} value.toUpdate
*/
normalize(change, node, rows) {
rows.forEach(({ row, invalids, add }) => {
invalids.forEach((child) => {
return change.removeNodeByKey(child.key, { normalize: false });
});
Range(0, add).forEach(() => {
const cell = makeEmptyCell(opts);
return change.insertNodeByKey(row.key, 0, cell, { normalize: false });
});
});
return change;
}
};
}
/**
* @param {Options} opts The plugin options
* @return {Object} A rule that ensures table node has all align data
*/
function tableContainAlignData(opts) {
return {
match(node) {
return node.type === opts.typeTable;
},
validate(table) {
const align = table.data.get('align', List());
const row = table.nodes.first();
const columns = row.nodes.size;
return align.length == columns ? null : { align, columns };
},
/**
* Updates by key the table to add the data
* @param {Map} align
* @param {Number} columns
*/
normalize(change, node, { align, columns }) {
return change.setNodeByKey(node.key, {
data: { align: createAlign(columns, align) }
}, { normalize: false });
}
};
}
function makeEmptyCell(opts) {
return Slate.Block.create({
type: opts.typeCell
});
}
function makeEmptyRow(opts) {
return Slate.Block.create({
type: opts.typeRow,
nodes: List([makeEmptyCell(opts)])
});
}
module.exports = makeSchema;