forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact.js
144 lines (124 loc) · 2.66 KB
/
react.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
import PlaceholderPlugin from 'slate-react-placeholder'
import React from 'react'
import DOMPlugin from './dom'
import Content from '../components/content'
import EVENT_HANDLERS from '../constants/event-handlers'
/**
* Props that can be defined by plugins.
*
* @type {Array}
*/
const PROPS = [
...EVENT_HANDLERS,
'commands',
'decorateNode',
'queries',
'renderEditor',
'renderMark',
'renderNode',
'schema',
]
/**
* A plugin that adds the React-specific rendering logic to the editor.
*
* @param {Object} options
* @return {Object}
*/
function ReactPlugin(options = {}) {
const { placeholder, plugins = [] } = options
/**
* Decorate node.
*
* @param {Object} node
* @param {Editor} editor
* @param {Function} next
* @return {Array}
*/
function decorateNode(node, editor, next) {
return []
}
/**
* Render editor.
*
* @param {Object} props
* @param {Editor} editor
* @param {Function} next
* @return {Element}
*/
function renderEditor(props, editor, next) {
return (
<Content
autoCorrect={props.autoCorrect}
className={props.className}
editor={editor}
id={props.id}
onEvent={(handler, event) => editor.run(handler, event)}
readOnly={props.readOnly}
role={props.role}
spellCheck={props.spellCheck}
style={props.style}
tabIndex={props.tabIndex}
tagName={props.tagName}
/>
)
}
/**
* Render node.
*
* @param {Object} props
* @param {Editor} editor
* @param {Function} next
* @return {Element}
*/
function renderNode(props, editor, next) {
const { attributes, children, node } = props
const { object } = node
if (object !== 'block' && object !== 'inline') return null
const Tag = object === 'block' ? 'div' : 'span'
const style = { position: 'relative' }
return (
<Tag {...attributes} style={style}>
{children}
</Tag>
)
}
/**
* Return the plugins.
*
* @type {Array}
*/
const ret = []
const editorPlugin = PROPS.reduce((memo, prop) => {
if (prop in options) memo[prop] = options[prop]
return memo
}, {})
ret.push(
DOMPlugin({
plugins: [editorPlugin, ...plugins],
})
)
if (placeholder) {
ret.push(
PlaceholderPlugin({
placeholder,
when: (editor, node) =>
node.object === 'document' &&
node.text === '' &&
node.nodes.size === 1 &&
node.getTexts().size === 1,
})
)
}
ret.push({
decorateNode,
renderEditor,
renderNode,
})
return ret
}
/**
* Export.
*
* @type {Function}
*/
export default ReactPlugin