forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode.js
331 lines (287 loc) · 7.91 KB
/
node.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
322
323
324
325
326
327
328
329
330
331
import Debug from 'debug'
import ImmutableTypes from 'react-immutable-proptypes'
import React from 'react'
import SlateTypes from 'slate-prop-types'
import warning from 'tiny-warning'
import Types from 'prop-types'
import { PathUtils } from 'slate'
import Void from './void'
import Text from './text'
import DATA_ATTRS from '../constants/data-attributes'
/**
* Debug.
*
* @type {Function}
*/
const debug = Debug('slate:node')
/**
* Node.
*
* @type {Component}
*/
class Node extends React.Component {
/**
* Property types.
*
* @type {Object}
*/
static propTypes = {
annotations: ImmutableTypes.map.isRequired,
block: SlateTypes.block,
decorations: ImmutableTypes.list.isRequired,
editor: Types.object.isRequired,
node: SlateTypes.node.isRequired,
parent: SlateTypes.node,
readOnly: Types.bool.isRequired,
selection: SlateTypes.selection,
}
/**
* Temporary values.
*
* @type {Object}
*/
tmp = {
nodeRefs: {},
}
/**
* A ref for the contenteditable DOM node.
*
* @type {Object}
*/
ref = React.createRef()
/**
* Debug.
*
* @param {String} message
* @param {Mixed} ...args
*/
debug = (message, ...args) => {
const { node } = this.props
const { key, type } = node
debug(message, `${key} (${type})`, ...args)
}
/**
* Should the node update?
*
* @param {Object} nextProps
* @param {Object} value
* @return {Boolean}
*/
shouldComponentUpdate(nextProps) {
const { props } = this
const { editor } = props
const shouldUpdate = editor.run(
'shouldNodeComponentUpdate',
props,
nextProps
)
const n = nextProps
const p = props
// If the `Component` has a custom logic to determine whether the component
// needs to be updated or not, return true if it returns true. If it returns
// false, we need to ignore it, because it shouldn't be allowed it.
if (shouldUpdate != null) {
warning(
false,
'As of [email protected] the `shouldNodeComponentUpdate` middleware is deprecated. You can pass specific values down the tree using React\'s built-in "context" construct instead.'
)
if (shouldUpdate) {
return true
}
warning(
shouldUpdate !== false,
"Returning false in `shouldNodeComponentUpdate` does not disable Slate's internal `shouldComponentUpdate` logic. If you want to prevent updates, use React's `shouldComponentUpdate` instead."
)
}
// If the `readOnly` status has changed, re-render in case there is any
// user-land logic that depends on it, like nested editable contents.
if (n.readOnly !== p.readOnly) {
return true
}
// If the node has changed, update. PERF: There are cases where it will have
// changed, but it's properties will be exactly the same (eg. copy-paste)
// which this won't catch. But that's rare and not a drag on performance, so
// for simplicity we just let them through.
if (n.node !== p.node) {
return true
}
// If the selection value of the node or of some of its children has changed,
// re-render in case there is any user-land logic depends on it to render.
// if the node is selected update it, even if it was already selected: the
// selection value of some of its children could have been changed and they
// need to be rendered again.
if (
(!n.selection && p.selection) ||
(n.selection && !p.selection) ||
(n.selection && p.selection && !n.selection.equals(p.selection))
) {
return true
}
// If the annotations have changed, update.
if (!n.annotations.equals(p.annotations)) {
return true
}
// If the decorations have changed, update.
if (!n.decorations.equals(p.decorations)) {
return true
}
// Otherwise, don't update.
return false
}
/**
* Render.
*
* @return {Element}
*/
render() {
this.debug('render', this)
const {
annotations,
block,
decorations,
editor,
node,
parent,
readOnly,
selection,
} = this.props
const newDecorations = node.getDecorations(editor)
const children = node.nodes.toArray().map((child, i) => {
const Component = child.object === 'text' ? Text : Node
const sel = selection && getRelativeRange(node, i, selection)
const decs = newDecorations
.concat(decorations)
.map(d => getRelativeRange(node, i, d))
.filter(d => d)
const anns = annotations
.map(a => getRelativeRange(node, i, a))
.filter(a => a)
return (
<Component
block={node.object === 'block' ? node : block}
editor={editor}
annotations={anns}
decorations={decs}
selection={sel}
key={child.key}
node={child}
parent={node}
readOnly={readOnly}
// COMPAT: We use this map of refs to lookup a DOM node down the
// tree of components by path.
ref={ref => {
if (ref) {
this.tmp.nodeRefs[i] = ref
} else {
delete this.tmp.nodeRefs[i]
}
}}
/>
)
})
// Attributes that the developer must mix into the element in their
// custom node renderer component.
const attributes = {
[DATA_ATTRS.OBJECT]: node.object,
[DATA_ATTRS.KEY]: node.key,
ref: this.ref,
}
// If it's a block node with inline children, add the proper `dir` attribute
// for text direction.
if (node.isLeafBlock()) {
const direction = node.getTextDirection()
if (direction === 'rtl') attributes.dir = 'rtl'
}
let render
if (node.object === 'block') {
render = 'renderBlock'
} else if (node.object === 'document') {
render = 'renderDocument'
} else if (node.object === 'inline') {
render = 'renderInline'
}
const element = editor.run(render, {
attributes,
children,
editor,
isFocused: !!selection && selection.isFocused,
isSelected: !!selection,
node,
parent,
readOnly,
})
return editor.isVoid(node) ? (
<Void
{...this.props}
textRef={ref => {
if (ref) {
this.tmp.nodeRefs[0] = ref
} else {
delete this.tmp.nodeRefs[0]
}
}}
>
{element}
</Void>
) : (
element
)
}
}
/**
* Return a `range` relative to a child at `index`.
*
* @param {Range} range
* @param {Number} index
* @return {Range}
*/
function getRelativeRange(node, index, range) {
if (range.isUnset) {
return null
}
const child = node.nodes.get(index)
let { start, end } = range
const { path: startPath } = start
const { path: endPath } = end
const startIndex = startPath.first()
const endIndex = endPath.first()
if (startIndex === index) {
start = start.setPath(startPath.rest())
} else if (startIndex < index && index <= endIndex) {
if (child.object === 'text') {
start = start.moveTo(PathUtils.create([index]), 0).setKey(child.key)
} else {
const [first] = child.texts()
const [firstNode, firstPath] = first
start = start.moveTo(firstPath, 0).setKey(firstNode.key)
}
} else {
start = null
}
if (endIndex === index) {
end = end.setPath(endPath.rest())
} else if (startIndex <= index && index < endIndex) {
if (child.object === 'text') {
const length = child.text.length
end = end.moveTo(PathUtils.create([index]), length).setKey(child.key)
} else {
const [last] = child.texts({ direction: 'backward' })
const [lastNode, lastPath] = last
end = end.moveTo(lastPath, lastNode.text.length).setKey(lastNode.key)
}
} else {
end = null
}
if (!start || !end) {
return null
}
range = range.setAnchor(start)
range = range.setFocus(end)
return range
}
/**
* Export.
*
* @type {Component}
*/
export default Node