| author | Alexandre Segura <mex.zktk@gmail.com> |
| Wed, 31 May 2017 17:51:54 +0200 | |
| changeset 12 | 48ddaa42b810 |
| parent 11 | 6fb4de54acea |
| child 15 | 4a8bbd314a46 |
| permissions | -rw-r--r-- |
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
1 |
import { Editor, Plain } 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 |
||
109 |
clear = () => { |
|
110 |
const state = Plain.deserialize(''); |
|
| 8 | 111 |
this.onChange(state); |
| 5 | 112 |
} |
113 |
||
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
114 |
focus = () => { |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
115 |
this.refs.editor.focus(); |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
116 |
} |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
117 |
|
| 5 | 118 |
/** |
119 |
* On key down, if it's a formatting command toggle a mark. |
|
120 |
* |
|
121 |
* @param {Event} e |
|
122 |
* @param {Object} data |
|
123 |
* @param {State} state |
|
124 |
* @return {State} |
|
125 |
*/ |
|
126 |
||
127 |
onKeyDown = (e, data, state) => { |
|
128 |
if (!data.isMod) return |
|
129 |
let mark |
|
130 |
||
131 |
switch (data.key) { |
|
132 |
case 'b': |
|
133 |
mark = 'bold' |
|
134 |
break |
|
135 |
case 'i': |
|
136 |
mark = 'italic' |
|
137 |
break |
|
138 |
case 'u': |
|
139 |
mark = 'underlined' |
|
140 |
break |
|
141 |
case '`': |
|
142 |
mark = 'code' |
|
143 |
break |
|
144 |
default: |
|
145 |
return |
|
146 |
} |
|
147 |
||
148 |
state = state |
|
149 |
.transform() |
|
150 |
.toggleMark(mark) |
|
151 |
.apply() |
|
152 |
||
153 |
e.preventDefault() |
|
154 |
return state |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* When a mark button is clicked, toggle the current mark. |
|
159 |
* |
|
160 |
* @param {Event} e |
|
161 |
* @param {String} type |
|
162 |
*/ |
|
163 |
||
164 |
onClickMark = (e, type) => { |
|
165 |
e.preventDefault() |
|
166 |
let { state } = this.state |
|
167 |
||
168 |
state = state |
|
169 |
.transform() |
|
170 |
.toggleMark(type) |
|
171 |
.apply() |
|
172 |
||
173 |
this.setState({ state }) |
|
174 |
} |
|
175 |
||
176 |
/** |
|
177 |
* When a block button is clicked, toggle the block type. |
|
178 |
* |
|
179 |
* @param {Event} e |
|
180 |
* @param {String} type |
|
181 |
*/ |
|
182 |
||
183 |
onClickBlock = (e, type) => { |
|
184 |
e.preventDefault() |
|
185 |
let { state } = this.state |
|
186 |
const transform = state.transform() |
|
187 |
const { document } = state |
|
188 |
||
189 |
// Handle everything but list buttons. |
|
190 |
if (type !== 'bulleted-list' && type !== 'numbered-list') { |
|
191 |
const isActive = this.hasBlock(type) |
|
192 |
const isList = this.hasBlock('list-item') |
|
193 |
||
194 |
if (isList) { |
|
195 |
transform |
|
196 |
.setBlock(isActive ? DEFAULT_NODE : type) |
|
197 |
.unwrapBlock('bulleted-list') |
|
198 |
.unwrapBlock('numbered-list') |
|
199 |
} |
|
200 |
||
201 |
else { |
|
202 |
transform |
|
203 |
.setBlock(isActive ? DEFAULT_NODE : type) |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
// Handle the extra wrapping required for list buttons. |
|
208 |
else { |
|
209 |
const isList = this.hasBlock('list-item') |
|
210 |
const isType = state.blocks.some((block) => { |
|
211 |
return !!document.getClosest(block.key, parent => parent.type === type) |
|
212 |
}) |
|
213 |
||
214 |
if (isList && isType) { |
|
215 |
transform |
|
216 |
.setBlock(DEFAULT_NODE) |
|
217 |
.unwrapBlock('bulleted-list') |
|
218 |
.unwrapBlock('numbered-list') |
|
219 |
} else if (isList) { |
|
220 |
transform |
|
221 |
.unwrapBlock(type === 'bulleted-list' ? 'numbered-list' : 'bulleted-list') |
|
222 |
.wrapBlock(type) |
|
223 |
} else { |
|
224 |
transform |
|
225 |
.setBlock('list-item') |
|
226 |
.wrapBlock(type) |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
state = transform.apply() |
|
231 |
this.setState({ state }) |
|
232 |
} |
|
233 |
||
|
12
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
11
diff
changeset
|
234 |
// /** |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
11
diff
changeset
|
235 |
// * |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
11
diff
changeset
|
236 |
// * @param {*Cosntructor} props |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
11
diff
changeset
|
237 |
// */ |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
11
diff
changeset
|
238 |
// componentWillMount() { |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
11
diff
changeset
|
239 |
// const initialValue = Raw.deserialize(initialState, { terse: true }); |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
11
diff
changeset
|
240 |
// this.state = { state: initialValue}; |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
11
diff
changeset
|
241 |
// this.onChange(initialValue); |
|
48ddaa42b810
Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
11
diff
changeset
|
242 |
// } |
| 8 | 243 |
|
244 |
/** |
|
| 5 | 245 |
* Render. |
246 |
* |
|
247 |
* @return {Element} |
|
248 |
*/ |
|
249 |
||
250 |
render = () => { |
|
251 |
return ( |
|
252 |
<div> |
|
253 |
{this.renderToolbar()} |
|
254 |
{this.renderEditor()} |
|
255 |
</div> |
|
256 |
) |
|
257 |
} |
|
258 |
||
259 |
/** |
|
260 |
* Render the toolbar. |
|
261 |
* |
|
262 |
* @return {Element} |
|
263 |
*/ |
|
264 |
||
265 |
renderToolbar = () => { |
|
266 |
return ( |
|
267 |
<div className="menu toolbar-menu"> |
|
268 |
{this.renderMarkButton('bold', 'format_bold')} |
|
269 |
{this.renderMarkButton('italic', 'format_italic')} |
|
270 |
{this.renderMarkButton('underlined', 'format_underlined')} |
|
271 |
{this.renderMarkButton('code', 'code')} |
|
272 |
{this.renderBlockButton('heading-one', 'looks_one')} |
|
273 |
{this.renderBlockButton('heading-two', 'looks_two')} |
|
274 |
{this.renderBlockButton('block-quote', 'format_quote')} |
|
275 |
{this.renderBlockButton('numbered-list', 'format_list_numbered')} |
|
276 |
{this.renderBlockButton('bulleted-list', 'format_list_bulleted')} |
|
277 |
</div> |
|
278 |
) |
|
279 |
} |
|
280 |
||
281 |
/** |
|
282 |
* Render a mark-toggling toolbar button. |
|
283 |
* |
|
284 |
* @param {String} type |
|
285 |
* @param {String} icon |
|
286 |
* @return {Element} |
|
287 |
*/ |
|
288 |
||
289 |
renderMarkButton = (type, icon) => { |
|
290 |
const isActive = this.hasMark(type) |
|
291 |
const onMouseDown = e => this.onClickMark(e, type) |
|
292 |
||
293 |
return ( |
|
294 |
<span className="button" onMouseDown={onMouseDown} data-active={isActive}> |
|
295 |
<span className="material-icons">{icon}</span> |
|
296 |
</span> |
|
297 |
) |
|
298 |
} |
|
299 |
||
300 |
/** |
|
301 |
* Render a block-toggling toolbar button. |
|
302 |
* |
|
303 |
* @param {String} type |
|
304 |
* @param {String} icon |
|
305 |
* @return {Element} |
|
306 |
*/ |
|
307 |
||
308 |
renderBlockButton = (type, icon) => { |
|
309 |
const isActive = this.hasBlock(type) |
|
310 |
const onMouseDown = e => this.onClickBlock(e, type) |
|
311 |
||
312 |
return ( |
|
313 |
<span className="button" onMouseDown={onMouseDown} data-active={isActive}> |
|
314 |
<span className="material-icons">{icon}</span> |
|
315 |
</span> |
|
316 |
) |
|
317 |
} |
|
318 |
||
319 |
/** |
|
320 |
* Render the Slate editor. |
|
321 |
* |
|
322 |
* @return {Element} |
|
323 |
*/ |
|
324 |
||
325 |
renderEditor = () => { |
|
326 |
return ( |
|
327 |
<div className="editor"> |
|
328 |
<Editor |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
329 |
ref="editor" |
| 5 | 330 |
spellCheck |
331 |
placeholder={'Enter some rich text...'} |
|
332 |
schema={schema} |
|
333 |
state={this.state.state} |
|
334 |
onChange={this.onChange} |
|
335 |
onKeyDown={this.onKeyDown} |
|
336 |
/> |
|
337 |
</div> |
|
338 |
) |
|
339 |
} |
|
340 |
||
341 |
} |
|
342 |
||
343 |
/** |
|
344 |
* Export. |
|
345 |
*/ |
|
346 |
||
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
347 |
export default SlateEditor |