-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaf.js
executable file
·163 lines (136 loc) · 3.74 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
import Debug from 'debug'
import React from 'react'
import Types from 'prop-types'
import SlateTypes from 'slate-prop-types'
import OffsetKey from '../utils/offset-key'
/**
* Debugger.
*
* @type {Function}
*/
const debug = Debug('slate:leaves')
/**
* Leaf.
*
* @type {Component}
*/
class Leaf extends React.Component {
/**
* Property types.
*
* @type {Object}
*/
static propTypes = {
block: SlateTypes.block.isRequired,
editor: Types.object.isRequired,
index: Types.number.isRequired,
leaves: SlateTypes.leaves.isRequired,
marks: SlateTypes.marks.isRequired,
node: SlateTypes.node.isRequired,
offset: Types.number.isRequired,
parent: SlateTypes.node.isRequired,
text: Types.string.isRequired,
}
/**
* 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.text != this.props.text ||
props.parent != this.props.parent
) {
return true
}
// Otherwise, don't update.
return false
}
/**
* Render the leaf.
*
* @return {Element}
*/
render() {
this.debug('render', this)
const { node, index } = this.props
const offsetKey = OffsetKey.stringify({
key: node.key,
index,
})
return <span data-offset-key={offsetKey}>{this.renderMarks()}</span>
}
/**
* Render all of the leaf's mark components.
*
* @return {Element}
*/
renderMarks() {
const { marks, node, offset, text, editor } = this.props
const { stack } = editor
const leaf = this.renderText()
return marks.reduce((children, mark) => {
const props = { editor, mark, marks, node, offset, text, children }
const element = stack.find('renderMark', props)
return element || children
}, leaf)
}
/**
* Render the text content of the leaf, accounting for browsers.
*
* @return {Element}
*/
renderText() {
const { block, node, parent, text, index, leaves } = this.props
// COMPAT: Render text inside void nodes with a zero-width space.
// So the node can contain selection but the text is not visible.
if (parent.isVoid) {
return <span data-slate-zero-width="z">{'\u200B'}</span>
}
// COMPAT: If this is the last text node in an empty block, render a zero-
// width space that will convert into a line break when copying and pasting
// to support expected plain text.
if (
text === '' &&
parent.object === 'block' &&
parent.text === '' &&
parent.nodes.size === 1
) {
return <span data-slate-zero-width="n">{'\u200B'}</span>
}
// COMPAT: If the text is empty, 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 data-slate-zero-width="z">{'\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 lastText = block.getLastText()
const lastChar = text.charAt(text.length - 1)
const isLastText = node === lastText
const isLastLeaf = index === leaves.size - 1
if (isLastText && isLastLeaf && lastChar === '\n') return `${text}\n`
// Otherwise, just return the text.
return text
}
}
/**
* Export.
*
* @type {Component}
*/
export default Leaf