forked from steemit/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaf.js
316 lines (265 loc) · 8.52 KB
/
leaf.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
import Debug from 'debug'
import OffsetKey from '../utils/offset-key'
import React from 'react'
import ReactDOM from 'react-dom'
import getWindow from 'get-window'
/**
* Debugger.
*
* @type {Function}
*/
const debug = Debug('slate:leaf')
/**
* Leaf.
*
* @type {Component}
*/
class Leaf extends React.Component {
/**
* Property types.
*
* @type {Object}
*/
static propTypes = {
index: React.PropTypes.number.isRequired,
isVoid: React.PropTypes.bool,
marks: React.PropTypes.object.isRequired,
node: React.PropTypes.object.isRequired,
parent: React.PropTypes.object.isRequired,
ranges: React.PropTypes.object.isRequired,
schema: React.PropTypes.object.isRequired,
state: React.PropTypes.object.isRequired,
text: React.PropTypes.string.isRequired
};
/**
* Default properties.
*
* @type {Object}
*/
static defaultProps = {
isVoid: false
};
/**
* Constructor.
*
* @param {Object} props
*/
constructor(props) {
super(props)
this.tmp = {}
this.tmp.renders = 0
}
/**
* Debug.
*
* @param {String} message
* @param {Mixed} ...args
*/
debug = (message, ...args) => {
debug(message, `${this.props.node.key}-${this.props.index}`, ...args)
}
/**
* Should component update?
*
* @param {Object} props
* @return {Boolean}
*/
shouldComponentUpdate(props) {
// If any of the regular properties have changed, re-render.
if (
props.index != this.props.index ||
props.marks != this.props.marks ||
props.schema != this.props.schema ||
props.text != this.props.text
) {
return true
}
// If the DOM text does not equal the `text` property, re-render, this can
// happen because React gets out of sync when previously natively rendered.
const el = findDeepestNode(ReactDOM.findDOMNode(this))
const text = this.renderText(props)
if (el.textContent != text) return true
// If the selection was previously focused, and now it isn't, re-render so
// that the selection will be properly removed.
if (this.props.state.isFocused && props.state.isBlurred) {
const { index, node, ranges, state } = this.props
const { start, end } = OffsetKey.findBounds(index, ranges)
if (state.selection.hasEdgeBetween(node, start, end)) return true
}
// If the selection will be focused, only re-render if this leaf contains
// one or both of the selection's edges.
if (props.state.isFocused) {
const { index, node, ranges, state } = props
const { start, end } = OffsetKey.findBounds(index, ranges)
if (state.selection.hasEdgeBetween(node, start, end)) return true
}
// Otherwise, don't update.
return false
}
/**
* When the DOM updates, try updating the selection.
*/
componentDidMount() {
this.updateSelection()
}
componentDidUpdate() {
this.updateSelection()
}
/**
* Update the DOM selection if it's inside the leaf.
*/
updateSelection() {
const { state, ranges, isVoid } = this.props
const { selection } = state
// If the selection is blurred we have nothing to do.
if (selection.isBlurred) return
let { anchorOffset, focusOffset } = selection
const { node, index } = this.props
const { start, end } = OffsetKey.findBounds(index, ranges)
// If neither matches, the selection doesn't start or end here, so exit.
const hasAnchor = selection.hasAnchorBetween(node, start, end)
const hasFocus = selection.hasFocusBetween(node, start, end)
if (!hasAnchor && !hasFocus) return
// If the leaf is a void leaf, ensure that it has no width. This is due to
// void nodes always rendering an empty leaf, for browser compatibility.
if (isVoid) {
anchorOffset = 0
focusOffset = 0
}
// We have a selection to render, so prepare a few things...
const ref = ReactDOM.findDOMNode(this)
const el = findDeepestNode(ref)
const window = getWindow(el)
const native = window.getSelection()
const parent = ref.closest('[contenteditable]')
// COMPAT: In Firefox, it's not enough to create a range, you also need to
// focus the contenteditable element. (2016/11/16)
function focus() {
if (parent) setTimeout(() => parent.focus())
}
// If both the start and end are here, set the selection all at once.
if (hasAnchor && hasFocus) {
native.removeAllRanges()
const range = window.document.createRange()
range.setStart(el, anchorOffset - start)
native.addRange(range)
native.extend(el, focusOffset - start)
focus()
}
// Otherwise we need to set the selection across two different leaves.
else {
// If the selection is forward, we can set things in sequence. In the
// first leaf to render, reset the selection and set the new start. And
// then in the second leaf to render, extend to the new end.
if (selection.isForward) {
if (hasAnchor) {
native.removeAllRanges()
const range = window.document.createRange()
range.setStart(el, anchorOffset - start)
native.addRange(range)
} else if (hasFocus) {
native.extend(el, focusOffset - start)
focus()
}
}
// Otherwise, if the selection is backward, we need to hack the order a bit.
// In the first leaf to render, set a phony start anchor to store the true
// end position. And then in the second leaf to render, set the start and
// extend the end to the stored value.
else {
if (hasFocus) {
native.removeAllRanges()
const range = window.document.createRange()
range.setStart(el, focusOffset - start)
native.addRange(range)
} else if (hasAnchor) {
const endNode = native.focusNode
const endOffset = native.focusOffset
native.removeAllRanges()
const range = window.document.createRange()
range.setStart(el, anchorOffset - start)
native.addRange(range)
native.extend(endNode, endOffset)
focus()
}
}
}
this.debug('updateSelection')
}
/**
* Render the leaf.
*
* @return {Element}
*/
render() {
this.debug('render')
const { props } = this
const { node, index } = props
const offsetKey = OffsetKey.stringify({
key: node.key,
index
})
// Increment the renders key, which forces a re-render whenever this
// component is told it should update. This is required because "native"
// renders where we don't update the leaves cause React's internal state to
// get out of sync, causing it to not realize the DOM needs updating.
this.tmp.renders++
return (
<span key={this.tmp.renders} data-offset-key={offsetKey}>
{this.renderMarks(props)}
</span>
)
}
/**
* Render the text content of the leaf, accounting for browsers.
*
* @param {Object} props
* @return {Element}
*/
renderText({ parent, text, index, ranges }) {
// COMPAT: If the text is empty and it's the only child, we need to render a
// <br/> to get the block to have the proper height.
if (text == '' && parent.kind == 'block' && parent.text == '') return <br />
// COMPAT: If the text is empty otherwise, it's because it's on the edge of
// an inline void node, so we render a zero-width space so that the
// selection can be inserted next to it still.
if (text == '') return <span className="slate-zero-width-space">{'\u200B'}</span>
// COMPAT: Browsers will collapse trailing new lines at the end of blocks,
// so we need to add an extra trailing new lines to prevent that.
const lastChar = text.charAt(text.length - 1)
const isLast = index == ranges.size - 1
if (isLast && lastChar == '\n') return `${text}\n`
// Otherwise, just return the text.
return text
}
/**
* Render all of the leaf's mark components.
*
* @param {Object} props
* @return {Element}
*/
renderMarks(props) {
const { marks, schema } = props
const text = this.renderText(props)
return marks.reduce((children, mark) => {
const Component = mark.getComponent(schema)
if (!Component) return children
return <Component mark={mark} marks={marks}>{children}</Component>
}, text)
}
}
/**
* Find the deepest descendant of a DOM `element`.
*
* @param {Element} node
* @return {Element}
*/
function findDeepestNode(element) {
return element.firstChild
? findDeepestNode(element.firstChild)
: element
}
/**
* Export.
*/
export default Leaf