-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
executable file
·204 lines (173 loc) · 5.24 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
import Debug from 'debug'
import ImmutableTypes from 'react-immutable-proptypes'
import React from 'react'
import SlateTypes from 'slate-prop-types'
import logger from 'slate-dev-logger'
import Types from 'prop-types'
import Void from './void'
import Text from './text'
/**
* Debug.
*
* @type {Function}
*/
const debug = Debug('slate:node')
/**
* Node.
*
* @type {Component}
*/
class Node extends React.Component {
/**
* Property types.
*
* @type {Object}
*/
static propTypes = {
block: SlateTypes.block,
decorations: ImmutableTypes.list.isRequired,
editor: Types.object.isRequired,
isSelected: Types.bool.isRequired,
node: SlateTypes.node.isRequired,
parent: SlateTypes.node.isRequired,
readOnly: Types.bool.isRequired,
}
/**
* 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 { stack } = props.editor
const shouldUpdate = stack.find(
'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) {
if (shouldUpdate) {
return true
}
if (shouldUpdate === false) {
logger.warn(
"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.isSelected || p.isSelected) 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 { editor, isSelected, node, parent, readOnly } = this.props
const { value } = editor
const { selection } = value
const { stack } = editor
const indexes = node.getSelectionIndexes(selection, isSelected)
let children = node.nodes.toArray().map((child, i) => {
const isChildSelected = !!indexes && indexes.start <= i && i < indexes.end
return this.renderNode(child, isChildSelected)
})
// Attributes that the developer must to mix into the element in their
// custom node renderer component.
const attributes = { 'data-key': node.key }
// If it's a block node with inline children, add the proper `dir` attribute
// for text direction.
if (node.object == 'block' && node.nodes.first().object != 'block') {
const direction = node.getTextDirection()
if (direction == 'rtl') attributes.dir = 'rtl'
}
const props = {
key: node.key,
editor,
isSelected,
node,
parent,
readOnly,
}
let placeholder = stack.find('renderPlaceholder', props)
if (placeholder) {
placeholder = React.cloneElement(placeholder, {
key: `${node.key}-placeholder`,
})
children = [placeholder, ...children]
}
const element = stack.find('renderNode', {
...props,
attributes,
children,
})
return node.isVoid ? <Void {...this.props}>{element}</Void> : element
}
/**
* Render a `child` node.
*
* @param {Node} child
* @param {Boolean} isSelected
* @return {Element}
*/
renderNode = (child, isSelected) => {
const { block, decorations, editor, node, readOnly } = this.props
const { stack } = editor
const Component = child.object == 'text' ? Text : Node
const decs = decorations.concat(node.getDecorations(stack))
return (
<Component
block={node.object == 'block' ? node : block}
decorations={decs}
editor={editor}
isSelected={isSelected}
key={child.key}
node={child}
parent={node}
readOnly={readOnly}
/>
)
}
}
/**
* Export.
*
* @type {Component}
*/
export default Node