| author | Alexandre Segura <mex.zktk@gmail.com> |
| Thu, 01 Jun 2017 17:32:07 +0200 | |
| changeset 16 | e67cd18cc594 |
| parent 15 | 4a8bbd314a46 |
| child 17 | 877d8796b86d |
| permissions | -rw-r--r-- |
| 15 | 1 |
import { Editor, Plain, Raw } from 'slate' |
| 5 | 2 |
import React from 'react' |
|
16
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
3 |
import moment from 'moment'; |
| 5 | 4 |
|
5 |
/** |
|
6 |
* Define the default node type. |
|
7 |
*/ |
|
8 |
||
9 |
const DEFAULT_NODE = 'paragraph' |
|
10 |
||
11 |
/** |
|
12 |
* Define a schema. |
|
13 |
* |
|
14 |
* @type {Object} |
|
15 |
*/ |
|
16 |
||
17 |
const schema = { |
|
18 |
nodes: { |
|
19 |
'block-quote': props => <blockquote {...props.attributes}>{props.children}</blockquote>, |
|
20 |
'bulleted-list': props => <ul {...props.attributes}>{props.children}</ul>, |
|
21 |
'heading-one': props => <h1 {...props.attributes}>{props.children}</h1>, |
|
22 |
'heading-two': props => <h2 {...props.attributes}>{props.children}</h2>, |
|
23 |
'list-item': props => <li {...props.attributes}>{props.children}</li>, |
|
24 |
'numbered-list': props => <ol {...props.attributes}>{props.children}</ol>, |
|
25 |
}, |
|
26 |
marks: { |
|
27 |
bold: { |
|
28 |
fontWeight: 'bold' |
|
29 |
}, |
|
30 |
code: { |
|
31 |
fontFamily: 'monospace', |
|
32 |
backgroundColor: '#eee', |
|
33 |
padding: '3px', |
|
34 |
borderRadius: '4px' |
|
35 |
}, |
|
36 |
italic: { |
|
37 |
fontStyle: 'italic' |
|
38 |
}, |
|
39 |
underlined: { |
|
40 |
textDecoration: 'underline' |
|
41 |
} |
|
42 |
} |
|
43 |
} |
|
44 |
||
45 |
/** |
|
46 |
* The rich text example. |
|
47 |
* |
|
48 |
* @type {Component} |
|
49 |
*/ |
|
50 |
||
| 8 | 51 |
class SlateEditor extends React.Component { |
| 5 | 52 |
|
53 |
/** |
|
54 |
* Deserialize the initial editor state. |
|
55 |
* |
|
56 |
* @type {Object} |
|
57 |
*/ |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
58 |
constructor(props) { |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
59 |
super(props); |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
60 |
this.state = { |
|
16
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
61 |
state: Plain.deserialize(''), |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
62 |
startedAt: null, |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
63 |
finishedAt: null |
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
64 |
}; |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
65 |
} |
| 5 | 66 |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
67 |
componentDidMount() { |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
68 |
this.focus(); |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
69 |
} |
| 5 | 70 |
|
71 |
/** |
|
72 |
* Check if the current selection has a mark with `type` in it. |
|
73 |
* |
|
74 |
* @param {String} type |
|
75 |
* @return {Boolean} |
|
76 |
*/ |
|
77 |
||
78 |
hasMark = (type) => { |
|
79 |
const { state } = this.state |
|
80 |
return state.marks.some(mark => mark.type === type) |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* Check if the any of the currently selected blocks are of `type`. |
|
85 |
* |
|
86 |
* @param {String} type |
|
87 |
* @return {Boolean} |
|
88 |
*/ |
|
89 |
||
90 |
hasBlock = (type) => { |
|
91 |
const { state } = this.state |
|
92 |
return state.blocks.some(node => node.type === type) |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* On change, save the new state. |
|
97 |
* |
|
98 |
* @param {State} state |
|
99 |
*/ |
|
100 |
||
101 |
onChange = (state) => { |
|
|
16
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
102 |
|
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
103 |
let newState = { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
104 |
state: state, |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
105 |
startedAt: this.state.startedAt |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
106 |
}; |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
107 |
|
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
108 |
const isEmpty = state.document.length === 0; |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
109 |
|
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
110 |
// Reset timers when the text is empty |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
111 |
if (isEmpty) { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
112 |
Object.assign(newState, { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
113 |
startedAt: null, |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
114 |
finishedAt: null |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
115 |
}); |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
116 |
} else { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
117 |
Object.assign(newState, { finishedAt: moment().format('H:mm:ss') }); |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
118 |
} |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
119 |
|
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
120 |
// Store start time once when the first character is typed |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
121 |
if (!isEmpty && this.state.startedAt === null) { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
122 |
Object.assign(newState, { startedAt: moment().format('H:mm:ss') }); |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
123 |
} |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
124 |
|
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
125 |
this.setState(newState) |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
126 |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
127 |
if (typeof this.props.onChange === 'function') { |
|
16
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
128 |
this.props.onChange(newState); |
| 8 | 129 |
} |
| 5 | 130 |
} |
131 |
||
132 |
asPlain = () => { |
|
133 |
return Plain.serialize(this.state.state); |
|
134 |
} |
|
135 |
||
| 15 | 136 |
asRaw = () => { |
137 |
return Raw.serialize(this.state.state); |
|
138 |
} |
|
139 |
||
| 5 | 140 |
clear = () => { |
141 |
const state = Plain.deserialize(''); |
|
| 8 | 142 |
this.onChange(state); |
| 5 | 143 |
} |
144 |
||
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
145 |
focus = () => { |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
146 |
this.refs.editor.focus(); |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
147 |
} |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
148 |
|
| 5 | 149 |
/** |
150 |
* On key down, if it's a formatting command toggle a mark. |
|
151 |
* |
|
152 |
* @param {Event} e |
|
153 |
* @param {Object} data |
|
154 |
* @param {State} state |
|
155 |
* @return {State} |
|
156 |
*/ |
|
157 |
||
158 |
onKeyDown = (e, data, state) => { |
|
159 |
if (!data.isMod) return |
|
160 |
let mark |
|
161 |
||
162 |
switch (data.key) { |
|
163 |
case 'b': |
|
164 |
mark = 'bold' |
|
165 |
break |
|
166 |
case 'i': |
|
167 |
mark = 'italic' |
|
168 |
break |
|
169 |
case 'u': |
|
170 |
mark = 'underlined' |
|
171 |
break |
|
172 |
case '`': |
|
173 |
mark = 'code' |
|
174 |
break |
|
175 |
default: |
|
176 |
return |
|
177 |
} |
|
178 |
||
179 |
state = state |
|
180 |
.transform() |
|
181 |
.toggleMark(mark) |
|
182 |
.apply() |
|
183 |
||
184 |
e.preventDefault() |
|
185 |
return state |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* When a mark button is clicked, toggle the current mark. |
|
190 |
* |
|
191 |
* @param {Event} e |
|
192 |
* @param {String} type |
|
193 |
*/ |
|
194 |
||
195 |
onClickMark = (e, type) => { |
|
196 |
e.preventDefault() |
|
197 |
let { state } = this.state |
|
198 |
||
199 |
state = state |
|
200 |
.transform() |
|
201 |
.toggleMark(type) |
|
202 |
.apply() |
|
203 |
||
204 |
this.setState({ state }) |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* When a block button is clicked, toggle the block type. |
|
209 |
* |
|
210 |
* @param {Event} e |
|
211 |
* @param {String} type |
|
212 |
*/ |
|
213 |
||
214 |
onClickBlock = (e, type) => { |
|
215 |
e.preventDefault() |
|
216 |
let { state } = this.state |
|
217 |
const transform = state.transform() |
|
218 |
const { document } = state |
|
219 |
||
220 |
// Handle everything but list buttons. |
|
221 |
if (type !== 'bulleted-list' && type !== 'numbered-list') { |
|
222 |
const isActive = this.hasBlock(type) |
|
223 |
const isList = this.hasBlock('list-item') |
|
224 |
||
225 |
if (isList) { |
|
226 |
transform |
|
227 |
.setBlock(isActive ? DEFAULT_NODE : type) |
|
228 |
.unwrapBlock('bulleted-list') |
|
229 |
.unwrapBlock('numbered-list') |
|
230 |
} |
|
231 |
||
232 |
else { |
|
233 |
transform |
|
234 |
.setBlock(isActive ? DEFAULT_NODE : type) |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
// Handle the extra wrapping required for list buttons. |
|
239 |
else { |
|
240 |
const isList = this.hasBlock('list-item') |
|
241 |
const isType = state.blocks.some((block) => { |
|
242 |
return !!document.getClosest(block.key, parent => parent.type === type) |
|
243 |
}) |
|
244 |
||
245 |
if (isList && isType) { |
|
246 |
transform |
|
247 |
.setBlock(DEFAULT_NODE) |
|
248 |
.unwrapBlock('bulleted-list') |
|
249 |
.unwrapBlock('numbered-list') |
|
250 |
} else if (isList) { |
|
251 |
transform |
|
252 |
.unwrapBlock(type === 'bulleted-list' ? 'numbered-list' : 'bulleted-list') |
|
253 |
.wrapBlock(type) |
|
254 |
} else { |
|
255 |
transform |
|
256 |
.setBlock('list-item') |
|
257 |
.wrapBlock(type) |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
state = transform.apply() |
|
262 |
this.setState({ state }) |
|
263 |
} |
|
264 |
||
| 8 | 265 |
/** |
| 5 | 266 |
* Render. |
267 |
* |
|
268 |
* @return {Element} |
|
269 |
*/ |
|
270 |
||
271 |
render = () => { |
|
272 |
return ( |
|
273 |
<div> |
|
274 |
{this.renderToolbar()} |
|
275 |
{this.renderEditor()} |
|
276 |
</div> |
|
277 |
) |
|
278 |
} |
|
279 |
||
280 |
/** |
|
281 |
* Render the toolbar. |
|
282 |
* |
|
283 |
* @return {Element} |
|
284 |
*/ |
|
285 |
||
286 |
renderToolbar = () => { |
|
287 |
return ( |
|
288 |
<div className="menu toolbar-menu"> |
|
289 |
{this.renderMarkButton('bold', 'format_bold')} |
|
290 |
{this.renderMarkButton('italic', 'format_italic')} |
|
291 |
{this.renderMarkButton('underlined', 'format_underlined')} |
|
292 |
{this.renderMarkButton('code', 'code')} |
|
293 |
{this.renderBlockButton('heading-one', 'looks_one')} |
|
294 |
{this.renderBlockButton('heading-two', 'looks_two')} |
|
295 |
{this.renderBlockButton('block-quote', 'format_quote')} |
|
296 |
{this.renderBlockButton('numbered-list', 'format_list_numbered')} |
|
297 |
{this.renderBlockButton('bulleted-list', 'format_list_bulleted')} |
|
298 |
</div> |
|
299 |
) |
|
300 |
} |
|
301 |
||
302 |
/** |
|
303 |
* Render a mark-toggling toolbar button. |
|
304 |
* |
|
305 |
* @param {String} type |
|
306 |
* @param {String} icon |
|
307 |
* @return {Element} |
|
308 |
*/ |
|
309 |
||
310 |
renderMarkButton = (type, icon) => { |
|
311 |
const isActive = this.hasMark(type) |
|
312 |
const onMouseDown = e => this.onClickMark(e, type) |
|
313 |
||
314 |
return ( |
|
315 |
<span className="button" onMouseDown={onMouseDown} data-active={isActive}> |
|
316 |
<span className="material-icons">{icon}</span> |
|
317 |
</span> |
|
318 |
) |
|
319 |
} |
|
320 |
||
321 |
/** |
|
322 |
* Render a block-toggling toolbar button. |
|
323 |
* |
|
324 |
* @param {String} type |
|
325 |
* @param {String} icon |
|
326 |
* @return {Element} |
|
327 |
*/ |
|
328 |
||
329 |
renderBlockButton = (type, icon) => { |
|
330 |
const isActive = this.hasBlock(type) |
|
331 |
const onMouseDown = e => this.onClickBlock(e, type) |
|
332 |
||
333 |
return ( |
|
334 |
<span className="button" onMouseDown={onMouseDown} data-active={isActive}> |
|
335 |
<span className="material-icons">{icon}</span> |
|
336 |
</span> |
|
337 |
) |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* Render the Slate editor. |
|
342 |
* |
|
343 |
* @return {Element} |
|
344 |
*/ |
|
345 |
||
346 |
renderEditor = () => { |
|
347 |
return ( |
|
348 |
<div className="editor"> |
|
349 |
<Editor |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
350 |
ref="editor" |
| 5 | 351 |
spellCheck |
352 |
placeholder={'Enter some rich text...'} |
|
353 |
schema={schema} |
|
354 |
state={this.state.state} |
|
355 |
onChange={this.onChange} |
|
356 |
onKeyDown={this.onKeyDown} |
|
357 |
/> |
|
358 |
</div> |
|
359 |
) |
|
360 |
} |
|
361 |
||
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* Export. |
|
366 |
*/ |
|
367 |
||
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
368 |
export default SlateEditor |