forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
256 lines (213 loc) · 4.79 KB
/
stack.js
File metadata and controls
256 lines (213 loc) · 4.79 KB
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
import CorePlugin from '../plugins/core'
import Debug from 'debug'
import Schema from './schema'
import State from './state'
import { Record } from 'immutable'
/**
* Debug.
*
* @type {Function}
*/
const debug = Debug('slate:stack')
/**
* Methods that are triggered on events and can change the state.
*
* @type {Array}
*/
const EVENT_HANDLER_METHODS = [
'onBeforeInput',
'onBlur',
'onFocus',
'onCopy',
'onCut',
'onDrop',
'onKeyDown',
'onPaste',
'onSelect',
]
/**
* Methods that accumulate an updated state.
*
* @type {Array}
*/
const STATE_ACCUMULATOR_METHODS = [
'onBeforeChange',
'onChange',
]
/**
* Default properties.
*
* @type {Object}
*/
const DEFAULTS = {
plugins: [],
schema: new Schema(),
}
/**
* Stack.
*
* @type {Stack}
*/
class Stack extends new Record(DEFAULTS) {
/**
* Constructor.
*
* @param {Object} properties
* @property {Array} plugins
* @property {Schema|Object} schema
* @property {Function} ...handlers
*/
static create(properties) {
const plugins = resolvePlugins(properties)
const schema = resolveSchema(plugins)
return new Stack({ plugins, schema })
}
/**
* Get the kind.
*
* @return {String}
*/
get kind() {
return 'stack'
}
/**
* Invoke `render` on all of the plugins in reverse, building up a tree of
* higher-order components.
*
* @param {State} state
* @param {Editor} editor
* @param {Object} children
* @param {Object} props
* @return {Component}
*/
render = (state, editor, props) => {
debug('render')
const plugins = this.plugins.slice().reverse()
let children
for (const plugin of plugins) {
if (!plugin.render) continue
children = plugin.render(props, state, editor)
props.children = children
}
return children
}
/**
* Invoke `renderPortal` on all of the plugins, building a list of portals.
*
* @param {State} state
* @param {Editor} editor
* @return {Array}
*/
renderPortal = (state, editor) => {
debug('renderPortal')
const portals = []
for (const plugin of this.plugins) {
if (!plugin.renderPortal) continue
const portal = plugin.renderPortal(state, editor)
if (portal == null) continue
portals.push(portal)
}
return portals
}
}
/**
* Mix in the event handler methods.
*
* @param {State} state
* @param {Editor} editor
* @param {Mixed} ...args
* @return {State|Null}
*/
for (const method of EVENT_HANDLER_METHODS) {
Stack.prototype[method] = function (state, editor, ...args) {
debug(method)
if (method == 'onChange') {
state = this.onBeforeChange(state, editor)
}
for (const plugin of this.plugins) {
if (!plugin[method]) continue
const next = plugin[method](...args, state, editor)
if (next == null) continue
assertState(next)
return next
}
return state
}
}
/**
* Mix in the state accumulator methods.
*
* @param {State} state
* @param {Editor} editor
* @param {Mixed} ...args
* @return {State|Null}
*/
for (const method of STATE_ACCUMULATOR_METHODS) {
Stack.prototype[method] = function (state, editor, ...args) {
debug(method)
for (const plugin of this.plugins) {
if (!plugin[method]) continue
const next = plugin[method](...args, state, editor)
if (next == null) continue
assertState(next)
state = next
}
return state
}
}
/**
* Assert that a `value` is a state object.
*
* @param {Mixed} value
*/
function assertState(value) {
if (value instanceof State) return
throw new Error(`A plugin returned an unexpected state value: ${value}`)
}
/**
* Resolve a schema from a set of `plugins`.
*
* @param {Array} plugins
* @return {Schema}
*/
function resolveSchema(plugins) {
let rules = []
for (const plugin of plugins) {
if (plugin.schema == null) continue
const schema = Schema.create(plugin.schema)
rules = rules.concat(schema.rules)
}
const schema = Schema.create({ rules })
return schema
}
/**
* Resolve an array of plugins from `properties`.
*
* In addition to the plugins provided in `properties.plugins`, this will
* create two other plugins:
*
* - A plugin made from the top-level `properties` themselves, which are
* placed at the beginning of the stack. That way, you can add a `onKeyDown`
* handler, and it will override all of the existing plugins.
*
* - A "core" functionality plugin that handles the most basic events in Slate,
* like deleting characters, splitting blocks, etc.
*
* @param {Object} props
* @return {Array}
*/
function resolvePlugins(props) {
const { plugins = [], ...overridePlugin } = props
const corePlugin = CorePlugin(props)
return [
overridePlugin,
...plugins,
corePlugin
]
}
/**
* Export.
*
* @type {Stack}
*/
export default Stack