forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (80 loc) · 1.82 KB
/
index.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
import { Editor } from 'slate-react'
import { Block, Value } from 'slate'
import React from 'react'
import initialValueAsJson from './value.json'
/**
* Deserialize the initial editor value.
*
* @type {Object}
*/
const initialValue = Value.fromJSON(initialValueAsJson)
/**
* A simple schema to enforce the nodes in the Slate document.
*
* @type {Object}
*/
const schema = {
document: {
nodes: [
{ match: { type: 'title' }, min: 1, max: 1 },
{ match: { type: 'paragraph' }, min: 1 },
],
normalize: (editor, { code, node, child, index }) => {
switch (code) {
case 'child_type_invalid': {
const type = index === 0 ? 'title' : 'paragraph'
return editor.setNodeByKey(child.key, type)
}
case 'child_min_invalid': {
const block = Block.create(index === 0 ? 'title' : 'paragraph')
return editor.insertNodeByKey(node.key, index, block)
}
}
},
},
}
/**
* The Forced Layout example.
*
* @type {Component}
*/
class ForcedLayout extends React.Component {
/**
* Render the editor.
*
* @return {Component} component
*/
render() {
return (
<Editor
placeholder="Enter a title..."
defaultValue={initialValue}
schema={schema}
renderBlock={this.renderBlock}
/>
)
}
/**
* Render a Slate block.
*
* @param {Object} props
* @param {Editor} editor
* @param {Function} next
* @return {Element}
*/
renderBlock = (props, editor, next) => {
const { attributes, children, node } = props
switch (node.type) {
case 'title':
return <h2 {...attributes}>{children}</h2>
case 'paragraph':
return <p {...attributes}>{children}</p>
default:
return next()
}
}
}
/**
* Export.
*/
export default ForcedLayout