forked from steemit/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.js
321 lines (265 loc) · 6.48 KB
/
editor.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import Content from './content'
import CorePlugin from '../plugins/core'
import Debug from 'debug'
import React from 'react'
import Schema from '../models/schema'
import noop from '../utils/noop'
/**
* Debug.
*/
const debug = Debug('slate:editor')
/**
* Event handlers to mix in to the editor.
*
* @type {Array}
*/
const EVENT_HANDLERS = [
'onBeforeInput',
'onBlur',
'onCopy',
'onCut',
'onDrop',
'onKeyDown',
'onPaste',
'onSelect'
]
/**
* Editor.
*
* @type {Component}
*/
class Editor extends React.Component {
/**
* Properties.
*/
static propTypes = {
className: React.PropTypes.string,
onBeforeChange: React.PropTypes.func,
onChange: React.PropTypes.func,
onDocumentChange: React.PropTypes.func,
onSelectionChange: React.PropTypes.func,
placeholder: React.PropTypes.any,
placeholderClassName: React.PropTypes.string,
placeholderStyle: React.PropTypes.object,
plugins: React.PropTypes.array,
readOnly: React.PropTypes.bool,
schema: React.PropTypes.object,
spellCheck: React.PropTypes.bool,
state: React.PropTypes.object.isRequired,
style: React.PropTypes.object
};
/**
* Default properties.
*/
static defaultProps = {
onChange: noop,
onDocumentChange: noop,
onSelectionChange: noop,
plugins: [],
readOnly: false,
schema: {},
spellCheck: true
};
/**
* When created, compute the plugins from `props`.
*
* @param {Object} props
*/
constructor(props) {
super(props)
this.tmp = {}
this.state = {}
this.state.plugins = this.resolvePlugins(props)
this.state.schema = this.resolveSchema(this.state.plugins)
this.state.state = this.onBeforeChange(props.state)
// Mix in the event handlers.
for (const method of EVENT_HANDLERS) {
this[method] = (...args) => {
this.onEvent(method, ...args)
}
}
}
/**
* When the `props` are updated, recompute the state and plugins.
*
* @param {Object} props
*/
componentWillReceiveProps = (props) => {
if (props.plugins != this.props.plugins) {
const plugins = this.resolvePlugins(props)
const schema = this.resolveSchema(plugins)
this.setState({ plugins, schema })
}
else if (props.schema != this.props.schema) {
const schema = this.resolveSchema(this.state.plugins)
this.setState({ schema })
}
this.setState({
state: this.onBeforeChange(props.state)
})
}
/**
* Programmatically blur the editor.
*/
blur = () => {
const state = this.state.state
.transform()
.blur()
.apply()
this.onChange(state)
}
/**
* Programmatically focus the editor.
*/
focus = () => {
const state = this.state.state
.transform()
.focus()
.apply()
this.onChange(state)
}
/**
* Get the editor's current `schema`.
*
* @return {Schema}
*/
getSchema = () => {
return this.state.schema
}
/**
* Get the editor's current `state`.
*
* @return {State}
*/
getState = () => {
return this.state.state
}
/**
* When the editor receives a new 'state'
*
* @param {State} state
* @return {State} newState
*/
onBeforeChange = (state) => {
if (state == this.state.state) return state
for (const plugin of this.state.plugins) {
if (!plugin.onBeforeChange) continue
const newState = plugin.onBeforeChange(state, this)
if (newState == null) continue
state = newState
}
return state
}
/**
* When the `state` changes, pass through plugins, then bubble up.
*
* @param {State} state
*/
onChange = (state) => {
if (state == this.state.state) return
for (const plugin of this.state.plugins) {
if (!plugin.onChange) continue
const newState = plugin.onChange(state, this)
if (newState == null) continue
state = newState
}
this.props.onChange(state)
if (state.document != this.tmp.document) {
this.props.onDocumentChange(state.document, state)
this.tmp.document = state.document
}
if (state.selection != this.tmp.selection) {
this.props.onSelectionChange(state.selection, state)
this.tmp.selection = state.selection
}
}
/**
* When an event by `name` fires, pass it through the plugins, and update the
* state if one of them chooses to.
*
* @param {String} name
* @param {Mixed} ...args
*/
onEvent = (name, ...args) => {
for (const plugin of this.state.plugins) {
if (!plugin[name]) continue
const newState = plugin[name](...args, this.state.state, this)
if (!newState) continue
this.onChange(newState)
break
}
}
/**
* Render the editor.
*
* @return {Element}
*/
render = () => {
debug('render')
const handlers = {}
for (const property of EVENT_HANDLERS) {
handlers[property] = this[property]
}
return (
<Content
{...handlers}
className={this.props.className}
editor={this}
onChange={this.onChange}
readOnly={this.props.readOnly}
schema={this.state.schema}
spellCheck={this.props.spellCheck}
state={this.state.state}
style={this.props.style}
/>
)
}
/**
* Resolve the editor's current plugins from `props` when they change.
*
* Add a plugin made from the editor's own `props` at the beginning of the
* stack. That way, you can add a `onKeyDown` handler to the editor itself,
* and it will override all of the existing plugins.
*
* Also add the "core" functionality plugin that handles the most basic events
* for the editor, like delete characters and such.
*
* @param {Object} props
* @return {Array}
*/
resolvePlugins = (props) => {
const { onChange, plugins, ...editorPlugin } = props
const corePlugin = CorePlugin(props)
return [
editorPlugin,
...plugins,
corePlugin
]
}
/**
* Resolve the editor's schema from a set of `plugins`.
*
* @param {Array} plugins
* @return {Schema}
*/
resolveSchema = (plugins) => {
let rules = []
for (const plugin of plugins) {
if (!plugin.schema) continue
const schema = Schema.create(plugin.schema)
rules = rules.concat(schema.rules)
}
const schema = Schema.create({ rules })
return schema
}
}
/**
* Mix in the property types for the event handlers.
*/
for (const property of EVENT_HANDLERS) {
Editor.propTypes[property] = React.PropTypes.func
}
/**
* Export.
*/
export default Editor