| author | ymh <ymh.work@gmail.com> |
| Wed, 05 Dec 2018 23:52:25 +0100 | |
| changeset 194 | d19ba6045e82 |
| parent 191 | 3f71ad81a5a9 |
| child 195 | 669b563563f5 |
| permissions | -rw-r--r-- |
|
171
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
170
diff
changeset
|
1 |
import { Value } from 'slate'; |
|
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
170
diff
changeset
|
2 |
import Plain from 'slate-plain-serializer'; |
|
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
170
diff
changeset
|
3 |
import { Editor } from 'slate-react'; |
|
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
170
diff
changeset
|
4 |
import React from 'react'; |
| 173 | 5 |
import { withNamespaces } from 'react-i18next'; |
6 |
import { connect } from 'react-redux'; |
|
|
171
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
170
diff
changeset
|
7 |
import HtmlSerializer from './HtmlSerializer'; |
|
168
ea92f4fe783d
- move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents:
161
diff
changeset
|
8 |
import { now } from '../../utils'; |
| 173 | 9 |
import Toolbar from './Toolbar'; |
10 |
import { getAutoSubmit } from '../../selectors/authSelectors'; |
|
| 157 | 11 |
|
|
168
ea92f4fe783d
- move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents:
161
diff
changeset
|
12 |
|
| 5 | 13 |
/** |
14 |
* |
|
15 |
* @type {Component} |
|
16 |
*/ |
|
17 |
||
| 8 | 18 |
class SlateEditor extends React.Component { |
| 5 | 19 |
|
20 |
/** |
|
21 |
* Deserialize the initial editor state. |
|
22 |
* |
|
23 |
* @type {Object} |
|
24 |
*/ |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
25 |
constructor(props) { |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
26 |
super(props); |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
27 |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
28 |
this.state = { |
| 173 | 29 |
value: props.note ? Value.fromJSON(JSON.parse(props.note.raw)) : Plain.deserialize(''), |
|
16
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
30 |
startedAt: null, |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
31 |
finishedAt: null, |
|
159
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
32 |
enterKeyValue: 0, |
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
33 |
}; |
|
171
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
170
diff
changeset
|
34 |
|
| 172 | 35 |
this.editorRef = React.createRef(); |
36 |
} |
|
37 |
||
38 |
get editor() { |
|
39 |
if(this.editorRef) { |
|
40 |
return this.editorRef.current; |
|
41 |
} |
|
42 |
return null; |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
43 |
} |
| 5 | 44 |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
45 |
componentDidMount = () => { |
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
46 |
this.focus(); |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
47 |
} |
| 5 | 48 |
|
| 157 | 49 |
/** |
50 |
* On change, save the new state. |
|
| 5 | 51 |
* |
| 157 | 52 |
* @param {Change} change |
| 5 | 53 |
*/ |
54 |
||
| 173 | 55 |
onChange = ({value, operations}) => { |
|
16
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
56 |
|
| 173 | 57 |
const newState = { |
58 |
value |
|
59 |
}; |
|
60 |
||
61 |
(operations || []).some((op) => { |
|
62 |
if(['insert_text', 'remove_text', 'add_mark', 'remove_mark', 'set_mark', 'insert_node', 'merge_node', 'move_node', 'remove_node', 'set_node', 'split_node'].indexOf(op.type)>=0) { |
|
63 |
const tsnow = now(); |
|
64 |
if(this.state.startedAt == null) { |
|
65 |
newState.startedAt = tsnow; |
|
66 |
} |
|
67 |
newState.finishedAt = tsnow; |
|
68 |
return true; |
|
| 172 | 69 |
} |
| 173 | 70 |
return false; |
71 |
}); |
|
| 172 | 72 |
|
73 |
this.setState(newState, () => { |
|
74 |
if (typeof this.props.onChange === 'function') { |
|
|
194
d19ba6045e82
Optimize code a little bit. Get rid of very expensive and useless clone operation
ymh <ymh.work@gmail.com>
parents:
191
diff
changeset
|
75 |
this.props.onChange(this.state, {value, operations}); |
| 172 | 76 |
} |
77 |
}) |
|
| 5 | 78 |
} |
79 |
||
| 157 | 80 |
|
| 5 | 81 |
asPlain = () => { |
| 157 | 82 |
return Plain.serialize(this.state.value); |
| 5 | 83 |
} |
84 |
||
| 15 | 85 |
asRaw = () => { |
| 157 | 86 |
return JSON.stringify(this.state.value.toJSON()); |
| 15 | 87 |
} |
88 |
||
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
89 |
asHtml = () => { |
| 157 | 90 |
return HtmlSerializer.serialize(this.state.value); |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
91 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
92 |
|
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
93 |
asCategories = () => { |
| 173 | 94 |
return this.state.value.document.getMarksByType('category').map((mark) => mark.data.toJS()).toArray(); |
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
95 |
} |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
96 |
|
| 5 | 97 |
clear = () => { |
| 157 | 98 |
const value = Plain.deserialize(''); |
| 173 | 99 |
this.setState({ |
| 172 | 100 |
value, |
| 173 | 101 |
enterKeyValue: 0 |
102 |
}) |
|
| 5 | 103 |
} |
104 |
||
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
105 |
focus = () => { |
| 172 | 106 |
if(this.editor) { |
107 |
this.editor.focus(); |
|
|
171
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
170
diff
changeset
|
108 |
} |
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
109 |
} |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
110 |
|
| 173 | 111 |
submitNote = () => { |
112 |
this.setState({ enterKeyValue: 0 }, () => { |
|
113 |
if (typeof this.props.submitNote === 'function') { |
|
114 |
this.props.submitNote(); |
|
115 |
} |
|
116 |
}); |
|
| 5 | 117 |
} |
118 |
||
119 |
/** |
|
| 173 | 120 |
* On key down, if it's a formatting command toggle a mark. |
| 5 | 121 |
* |
122 |
* @param {Event} e |
|
| 173 | 123 |
* @param {Change} change |
124 |
* @return {Change} |
|
| 5 | 125 |
*/ |
126 |
||
| 173 | 127 |
onKeyUp = (e, editor, next) => { |
| 157 | 128 |
|
| 173 | 129 |
const { value } = this.state; |
130 |
const noteText = value.document.text.trim(); |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
131 |
|
| 173 | 132 |
if(e.key === "Enter" && noteText.length !== 0) { |
133 |
this.setState({ enterKeyValue: this.state.enterKeyValue + 1 }); |
|
134 |
} else if ( e.getModifierState() || (e.key !== "Control" && e.key !== "Shift" && e.key !== "Meta" && e.key !== "Alt") ) { |
|
135 |
this.setState({ enterKeyValue: 0 }); |
|
| 172 | 136 |
} |
| 173 | 137 |
return next(); |
|
72
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
138 |
} |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
139 |
|
| 8 | 140 |
/** |
|
159
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
141 |
* On key down, if it's a formatting command toggle a mark. |
|
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
142 |
* |
|
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
143 |
* @param {Event} e |
|
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
144 |
* @param {Change} change |
|
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
145 |
* @return {Change} |
|
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
146 |
*/ |
|
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
147 |
|
| 173 | 148 |
onKeyDown = (e, editor, next) => { |
|
159
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
149 |
|
| 173 | 150 |
const { value, enterKeyValue } = this.state; |
151 |
const { autoSubmit } = this.props; |
|
152 |
const noteText = value.document.text.trim(); |
|
|
159
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
153 |
|
| 173 | 154 |
// we prevent empty first lines |
155 |
if(e.key === "Enter" && noteText.length === 0) { |
|
156 |
e.preventDefault(); |
|
157 |
return next(); |
|
|
159
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
158 |
} |
|
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
159 |
|
| 173 | 160 |
// Enter submit the note |
161 |
if(e.key === "Enter" && ( enterKeyValue === 2 || e.ctrlKey || autoSubmit ) && noteText.length !== 0) { |
|
|
159
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
162 |
e.preventDefault(); |
| 173 | 163 |
this.submitNote(); |
164 |
return next(); |
|
|
159
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
165 |
} |
|
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
166 |
|
| 173 | 167 |
if (!e.ctrlKey) { |
168 |
return next(); |
|
|
159
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
169 |
} |
|
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
170 |
|
| 173 | 171 |
e.preventDefault(); |
| 5 | 172 |
|
| 173 | 173 |
// Decide what to do based on the key code... |
174 |
switch (e.key) { |
|
175 |
default: { |
|
176 |
break; |
|
177 |
} |
|
178 |
// When "B" is pressed, add a "bold" mark to the text. |
|
179 |
case 'b': { |
|
180 |
editor.toggleMark('bold'); |
|
181 |
break; |
|
182 |
} |
|
183 |
case 'i': { |
|
184 |
// When "U" is pressed, add an "italic" mark to the text. |
|
185 |
editor.toggleMark('italic'); |
|
186 |
break; |
|
187 |
} |
|
188 |
case 'u': { |
|
189 |
// When "U" is pressed, add an "underline" mark to the text. |
|
190 |
editor.toggleMark('underlined'); |
|
191 |
break; |
|
192 |
} |
|
193 |
} |
|
|
159
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
194 |
|
| 173 | 195 |
return next(); |
| 157 | 196 |
|
| 172 | 197 |
} |
198 |
||
199 |
// Add a `renderMark` method to render marks. |
|
200 |
||
201 |
renderMark = (props, editor, next) => { |
|
202 |
const { children, mark, attributes } = props |
|
203 |
||
204 |
switch (mark.type) { |
|
205 |
case 'bold': |
|
206 |
return <strong {...attributes}>{children}</strong> |
|
207 |
case 'code': |
|
208 |
return <code {...attributes}>{children}</code> |
|
209 |
case 'italic': |
|
210 |
return <em {...attributes}>{children}</em> |
|
211 |
case 'underlined': |
|
212 |
return <ins {...attributes}>{children}</ins> |
|
213 |
case 'category': |
|
214 |
let spanStyle = { |
|
215 |
backgroundColor: mark.data.get('color') |
|
216 |
}; |
|
217 |
return <span {...attributes} style={ spanStyle } >{children}</span> |
|
218 |
default: |
|
219 |
return next(); |
|
220 |
} |
|
| 157 | 221 |
} |
| 5 | 222 |
|
| 172 | 223 |
renderNode = (props, editor, next) => { |
| 157 | 224 |
const { attributes, children, node } = props |
225 |
||
226 |
switch (node.type) { |
|
227 |
case 'block-quote': |
|
228 |
return <blockquote {...attributes}>{children}</blockquote> |
|
229 |
case 'bulleted-list': |
|
230 |
return <ul {...attributes}>{children}</ul> |
|
231 |
case 'heading-one': |
|
232 |
return <h1 {...attributes}>{children}</h1> |
|
233 |
case 'heading-two': |
|
234 |
return <h2 {...attributes}>{children}</h2> |
|
235 |
case 'list-item': |
|
236 |
return <li {...attributes}>{children}</li> |
|
237 |
case 'numbered-list': |
|
238 |
return <ol {...attributes}>{children}</ol> |
|
|
168
ea92f4fe783d
- move SlateEditor and dependencies to its own folder
ymh <ymh.work@gmail.com>
parents:
161
diff
changeset
|
239 |
default: |
| 172 | 240 |
return next(); |
| 157 | 241 |
} |
| 173 | 242 |
} |
| 157 | 243 |
|
| 173 | 244 |
/** |
245 |
* Render. |
|
| 5 | 246 |
* |
247 |
* @return {Element} |
|
248 |
*/ |
|
249 |
||
| 173 | 250 |
render = () => ( |
251 |
<div className="bg-secondary mb-5"> |
|
252 |
<div className="sticky-top"> |
|
253 |
<Toolbar |
|
254 |
value={this.state.value} |
|
255 |
editor={this.editor} |
|
256 |
note={this.props.note} |
|
257 |
annotationCategories={this.props.annotationCategories} |
|
258 |
isButtonDisabled={this.props.isButtonDisabled} |
|
259 |
submitNote={this.submitNote} |
|
260 |
/> |
|
261 |
</div> |
|
|
159
a4705c2b4544
Add send note options to the editor
salimr <riwad.salim@yahoo.fr>
parents:
157
diff
changeset
|
262 |
<div className="editor-slatejs p-2"> |
| 5 | 263 |
<Editor |
| 172 | 264 |
ref={this.editorRef} |
| 5 | 265 |
spellCheck |
| 173 | 266 |
placeholder={this.props.t('slate_editor.placeholder')} |
| 157 | 267 |
value={this.state.value} |
| 5 | 268 |
onChange={this.onChange} |
| 173 | 269 |
onKeyDown={this.onKeyDown} |
270 |
onKeyUp={this.onKeyUp} |
|
| 157 | 271 |
renderMark={this.renderMark} |
272 |
renderNode = {this.renderNode} |
|
| 5 | 273 |
/> |
274 |
</div> |
|
| 173 | 275 |
</div> |
276 |
); |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
277 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
278 |
|
| 5 | 279 |
} |
280 |
||
281 |
/** |
|
282 |
* Export. |
|
283 |
*/ |
|
| 173 | 284 |
function mapStateToProps(state, props) { |
285 |
||
286 |
const autoSubmit = getAutoSubmit(state); |
|
287 |
||
288 |
return { |
|
289 |
autoSubmit, |
|
290 |
}; |
|
291 |
} |
|
| 5 | 292 |
|
|
171
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
170
diff
changeset
|
293 |
export default withNamespaces("", { |
|
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
170
diff
changeset
|
294 |
innerRef: (ref) => { |
| 173 | 295 |
if(!ref) { |
296 |
return; |
|
297 |
} |
|
298 |
const wrappedRef = ref.getWrappedInstance(); |
|
299 |
const editorRef = (wrappedRef && wrappedRef.props) ? wrappedRef.props.editorRef : null; |
|
|
171
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
170
diff
changeset
|
300 |
if(editorRef && editorRef.hasOwnProperty('current')) { |
| 173 | 301 |
editorRef.current = wrappedRef; |
|
171
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
170
diff
changeset
|
302 |
} |
|
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
170
diff
changeset
|
303 |
} |
| 173 | 304 |
})(connect(mapStateToProps, null, null, { withRef: true })(SlateEditor)); |