-
Notifications
You must be signed in to change notification settings - Fork 1
/
paragrapher.es6
56 lines (51 loc) · 1.57 KB
/
paragrapher.es6
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
const Slate = require('slate');
/**
* Slate plugin to ensure a trailing block.
* @param {Object} [opts] Options for the plugin
* @param {String|Function} [opts.match='paragraph'] Match last block
* @param {String} [opts.type] The type of the trailing block to insert
* @return {Object}
*/
function Paragrapher(opts) {
opts = opts || {};
opts.type = opts.type || 'paragraph';
opts.match = opts.match || (node => node.type === opts.type);
return {
schema: {
rules: [
{
match(node) {
return (
Slate.Block.isBlock(node) &&
node.type !== 'paragraph' &&
node.type !== 'block-quote' &&
node.type !== 'link' &&
node.type !== 'bulleted-list' &&
node.type !== 'bulleted-list-item' &&
node.type.indexOf('heading') !== 0 &&
node.type !== 'numbered-list' &&
node.type !== 'numbered-list-item' &&
node.isVoid === false
);
},
validate(node) {
const invalidNodes = node.nodes.filter(x => Slate.Text.isText(x) && x.text.length > 0);
return invalidNodes.size === 0 ? null : invalidNodes;
},
normalize(change, node, invalidNodes) {
invalidNodes.forEach(x =>
change.wrapBlockByKey(
x.key,
Slate.Block.create({
type: opts.type,
}),
),
);
return change;
},
},
],
},
};
}
export default Paragrapher;