forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
230 lines (196 loc) · 4.43 KB
/
index.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
import { Editor } from 'slate-react'
import { Value } from 'slate'
import React from 'react'
import ReactDOM from 'react-dom'
import initialValue from './value.json'
import styled from 'react-emotion'
import { Button, Icon, Menu } from '../components'
/**
* Give the menu some styles.
*
* @type {Component}
*/
const StyledMenu = styled(Menu)`
padding: 8px 7px 6px;
position: absolute;
z-index: 1;
top: -10000px;
left: -10000px;
margin-top: -6px;
opacity: 0;
background-color: #222;
border-radius: 4px;
transition: opacity 0.75s;
`
/**
* The hovering menu.
*
* @type {Component}
*/
class HoverMenu extends React.Component {
/**
* Render.
*
* @return {Element}
*/
render() {
const { className, innerRef } = this.props
const root = window.document.getElementById('root')
return ReactDOM.createPortal(
<StyledMenu className={className} innerRef={innerRef}>
{this.renderMarkButton('bold', 'format_bold')}
{this.renderMarkButton('italic', 'format_italic')}
{this.renderMarkButton('underlined', 'format_underlined')}
{this.renderMarkButton('code', 'code')}
</StyledMenu>,
root
)
}
/**
* Render a mark-toggling toolbar button.
*
* @param {String} type
* @param {String} icon
* @return {Element}
*/
renderMarkButton(type, icon) {
const { editor } = this.props
const { value } = editor
const isActive = value.activeMarks.some(mark => mark.type == type)
return (
<Button
reversed
active={isActive}
onMouseDown={event => this.onClickMark(event, type)}
>
<Icon>{icon}</Icon>
</Button>
)
}
/**
* When a mark button is clicked, toggle the current mark.
*
* @param {Event} event
* @param {String} type
*/
onClickMark(event, type) {
const { editor } = this.props
event.preventDefault()
editor.toggleMark(type)
}
}
/**
* The hovering menu example.
*
* @type {Component}
*/
class HoveringMenu extends React.Component {
/**
* Deserialize the raw initial value.
*
* @type {Object}
*/
state = {
value: Value.fromJSON(initialValue),
}
/**
* On update, update the menu.
*/
componentDidMount = () => {
this.updateMenu()
}
componentDidUpdate = () => {
this.updateMenu()
}
/**
* Update the menu's absolute position.
*/
updateMenu = () => {
const menu = this.menu
if (!menu) return
const { value } = this.state
const { fragment, selection } = value
if (selection.isBlurred || selection.isCollapsed || fragment.text === '') {
menu.removeAttribute('style')
return
}
const native = window.getSelection()
const range = native.getRangeAt(0)
const rect = range.getBoundingClientRect()
menu.style.opacity = 1
menu.style.top = `${rect.top + window.pageYOffset - menu.offsetHeight}px`
menu.style.left = `${rect.left +
window.pageXOffset -
menu.offsetWidth / 2 +
rect.width / 2}px`
}
/**
* Render.
*
* @return {Element}
*/
render() {
return (
<div>
<Editor
placeholder="Enter some text..."
value={this.state.value}
onChange={this.onChange}
renderEditor={this.renderEditor}
renderMark={this.renderMark}
/>
</div>
)
}
/**
* Render the editor.
*
* @param {Object} props
* @param {Function} next
* @return {Element}
*/
renderEditor = (props, editor, next) => {
const children = next()
return (
<React.Fragment>
{children}
<HoverMenu innerRef={menu => (this.menu = menu)} editor={editor} />
</React.Fragment>
)
}
/**
* Render a Slate mark.
*
* @param {Object} props
* @param {Editor} editor
* @param {Function} next
* @return {Element}
*/
renderMark = (props, editor, next) => {
const { children, mark, attributes } = props
switch (mark.type) {
case 'bold':
return <strong {...attributes}>{children}</strong>
case 'code':
return <code {...attributes}>{children}</code>
case 'italic':
return <em {...attributes}>{children}</em>
case 'underlined':
return <u {...attributes}>{children}</u>
default:
return next()
}
}
/**
* On change.
*
* @param {Editor} editor
*/
onChange = ({ value }) => {
this.setState({ value })
}
}
/**
* Export.
*/
export default HoveringMenu