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