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