forked from steemit/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoid.js
157 lines (135 loc) · 3.09 KB
/
void.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
import Leaf from './leaf'
import Mark from '../models/mark'
import OffsetKey from '../utils/offset-key'
import React from 'react'
import noop from '../utils/noop'
import { IS_FIREFOX } from '../constants/environment'
/**
* Void.
*
* @type {Component}
*/
class Void extends React.Component {
/**
* Property types.
*/
static propTypes = {
children: React.PropTypes.any.isRequired,
editor: React.PropTypes.object.isRequired,
node: React.PropTypes.object.isRequired,
parent: React.PropTypes.object.isRequired,
schema: React.PropTypes.object.isRequired,
state: React.PropTypes.object.isRequired,
};
/**
* When one of the wrapper elements it clicked, select the void node.
*
* @param {Event} e
*/
onClick = (e) => {
e.preventDefault()
const { node, editor } = this.props
const next = editor
.getState()
.transform()
.collapseToStartOf(node)
.focus()
.apply()
editor.onChange(next)
}
/**
* Render.
*
* @return {Element}
*/
render = () => {
const { children, node } = this.props
const Tag = node.kind == 'block' ? 'div' : 'span'
// Make the outer wrapper relative, so the spacer can overlay it.
const style = {
position: 'relative'
}
return (
<Tag style={style} onClick={this.onClick}>
{this.renderSpacer()}
<Tag contentEditable={false}>
{children}
</Tag>
</Tag>
)
}
/**
* Render a fake spacer leaf, which will catch the cursor when it the void
* node is navigated to with the arrow keys. Having this spacer there means
* the browser continues to manage the selection natively, so it keeps track
* of the right offset when moving across the block.
*
* @return {Element}
*/
renderSpacer = () => {
const { node } = this.props
let style
if (node.kind == 'block') {
style = IS_FIREFOX
? {
pointerEvents: 'none',
width: '0px',
height: '0px',
lineHeight: '0px',
visibility: 'hidden'
}
: {
position: 'absolute',
top: '0px',
left: '-9999px',
textIndent: '-9999px'
}
} else {
style = {
position: 'relative',
top: '0px',
left: '-9999px',
textIndent: '-9999px',
}
}
return (
<span style={style}>{this.renderLeaf()}</span>
)
}
/**
* Render a fake leaf.
*
* @return {Element}
*/
renderLeaf = () => {
const { node, schema, state } = this.props
const child = node.getFirstText()
const ranges = child.getRanges()
const text = ''
const marks = Mark.createSet()
const index = 0
const offsetKey = OffsetKey.stringify({
key: child.key,
index
})
return (
<Leaf
isVoid
renderMark={noop}
key={offsetKey}
schema={schema}
state={state}
node={child}
parent={node}
ranges={ranges}
index={index}
text={text}
marks={marks}
/>
)
}
}
/**
* Export.
*/
export default Void