| author | Alexandre Segura <mex.zktk@gmail.com> |
| Thu, 08 Jun 2017 18:54:36 +0200 | |
| changeset 25 | e04714a1d4eb |
| parent 21 | 284e866f55c7 |
| child 26 | 930e486ad0a8 |
| permissions | -rw-r--r-- |
| 15 | 1 |
import { Editor, Plain, Raw } from 'slate' |
| 5 | 2 |
import React from 'react' |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
3 |
import Portal from 'react-portal' |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
4 |
import moment from 'moment' |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
5 |
import HtmlSerializer from '../HtmlSerializer' |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
6 |
import AnnotationPlugin from '../AnnotationPlugin' |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
7 |
import CategoriesTooltip from './CategoriesTooltip' |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
8 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
9 |
const plugins = []; |
| 5 | 10 |
|
11 |
/** |
|
12 |
* Define the default node type. |
|
13 |
*/ |
|
14 |
||
15 |
const DEFAULT_NODE = 'paragraph' |
|
16 |
||
17 |
/** |
|
18 |
* Define a schema. |
|
19 |
* |
|
20 |
* @type {Object} |
|
21 |
*/ |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
22 |
// TODO Check if we can move this to the plugin using the schema option |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
23 |
// https://docs.slatejs.org/reference/plugins/plugin.html#schema |
| 5 | 24 |
const schema = { |
25 |
nodes: { |
|
26 |
'bulleted-list': props => <ul {...props.attributes}>{props.children}</ul>, |
|
27 |
'list-item': props => <li {...props.attributes}>{props.children}</li>, |
|
28 |
'numbered-list': props => <ol {...props.attributes}>{props.children}</ol>, |
|
29 |
}, |
|
30 |
marks: { |
|
31 |
bold: { |
|
32 |
fontWeight: 'bold' |
|
33 |
}, |
|
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
34 |
category: props => { |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
35 |
const data = props.mark.data; |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
36 |
return <span style={{ backgroundColor: data.get('color') }}>{props.children}</span> |
| 5 | 37 |
}, |
38 |
italic: { |
|
39 |
fontStyle: 'italic' |
|
40 |
}, |
|
41 |
underlined: { |
|
42 |
textDecoration: 'underline' |
|
43 |
} |
|
44 |
} |
|
45 |
} |
|
46 |
||
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
47 |
const annotationCategories = [ |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
48 |
{ key: 'important', name: 'Important', color: '#F1C40F' }, |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
49 |
{ key: 'keyword', name: 'Mot-clé', color: '#2ECC71' }, |
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
50 |
{ key: 'comment', name: 'Commentaire', color: '#3498DB', hasComment: true } |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
51 |
]; |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
52 |
|
| 5 | 53 |
/** |
54 |
* The rich text example. |
|
55 |
* |
|
56 |
* @type {Component} |
|
57 |
*/ |
|
58 |
||
| 8 | 59 |
class SlateEditor extends React.Component { |
| 5 | 60 |
|
61 |
/** |
|
62 |
* Deserialize the initial editor state. |
|
63 |
* |
|
64 |
* @type {Object} |
|
65 |
*/ |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
66 |
constructor(props) { |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
67 |
super(props); |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
68 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
69 |
const annotationPlugin = AnnotationPlugin({ |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
70 |
onChange: (text) => { |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
71 |
this.setState({ currentSelectionText: text }); |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
72 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
73 |
}); |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
74 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
75 |
plugins.push(annotationPlugin); |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
76 |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
77 |
this.state = { |
|
16
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
78 |
state: Plain.deserialize(''), |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
79 |
startedAt: null, |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
80 |
finishedAt: null, |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
81 |
currentSelectionText: '', |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
82 |
hoveringMenu: null, |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
83 |
isPortalOpen: false |
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
84 |
}; |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
85 |
} |
| 5 | 86 |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
87 |
componentDidMount = () => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
88 |
this.updateMenu(); |
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
89 |
this.focus(); |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
90 |
} |
| 5 | 91 |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
92 |
componentDidUpdate = () => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
93 |
this.updateMenu(); |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
94 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
95 |
|
| 5 | 96 |
/** |
97 |
* Check if the current selection has a mark with `type` in it. |
|
98 |
* |
|
99 |
* @param {String} type |
|
100 |
* @return {Boolean} |
|
101 |
*/ |
|
102 |
||
103 |
hasMark = (type) => { |
|
104 |
const { state } = this.state |
|
105 |
return state.marks.some(mark => mark.type === type) |
|
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* Check if the any of the currently selected blocks are of `type`. |
|
110 |
* |
|
111 |
* @param {String} type |
|
112 |
* @return {Boolean} |
|
113 |
*/ |
|
114 |
||
115 |
hasBlock = (type) => { |
|
116 |
const { state } = this.state |
|
117 |
return state.blocks.some(node => node.type === type) |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* On change, save the new state. |
|
122 |
* |
|
123 |
* @param {State} state |
|
124 |
*/ |
|
125 |
||
126 |
onChange = (state) => { |
|
|
16
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
127 |
|
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
128 |
let newState = { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
129 |
state: state, |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
130 |
startedAt: this.state.startedAt |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
131 |
}; |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
132 |
|
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
133 |
const isEmpty = state.document.length === 0; |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
134 |
|
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
135 |
// Reset timers when the text is empty |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
136 |
if (isEmpty) { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
137 |
Object.assign(newState, { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
138 |
startedAt: null, |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
139 |
finishedAt: null |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
140 |
}); |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
141 |
} else { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
142 |
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
|
143 |
} |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
144 |
|
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
145 |
// 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
|
146 |
if (!isEmpty && this.state.startedAt === null) { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
147 |
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
|
148 |
} |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
149 |
|
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
150 |
this.setState(newState) |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
151 |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
152 |
if (typeof this.props.onChange === 'function') { |
|
16
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
153 |
this.props.onChange(newState); |
| 8 | 154 |
} |
| 5 | 155 |
} |
156 |
||
157 |
asPlain = () => { |
|
158 |
return Plain.serialize(this.state.state); |
|
159 |
} |
|
160 |
||
| 15 | 161 |
asRaw = () => { |
162 |
return Raw.serialize(this.state.state); |
|
163 |
} |
|
164 |
||
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
165 |
asHtml = () => { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
166 |
return HtmlSerializer.serialize(this.state.state); |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
167 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
168 |
|
| 5 | 169 |
clear = () => { |
170 |
const state = Plain.deserialize(''); |
|
| 8 | 171 |
this.onChange(state); |
| 5 | 172 |
} |
173 |
||
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
174 |
focus = () => { |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
175 |
this.refs.editor.focus(); |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
176 |
} |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
177 |
|
| 5 | 178 |
/** |
179 |
* On key down, if it's a formatting command toggle a mark. |
|
180 |
* |
|
181 |
* @param {Event} e |
|
182 |
* @param {Object} data |
|
183 |
* @param {State} state |
|
184 |
* @return {State} |
|
185 |
*/ |
|
186 |
||
187 |
onKeyDown = (e, data, state) => { |
|
188 |
if (!data.isMod) return |
|
189 |
let mark |
|
190 |
||
191 |
switch (data.key) { |
|
192 |
case 'b': |
|
193 |
mark = 'bold' |
|
194 |
break |
|
195 |
case 'i': |
|
196 |
mark = 'italic' |
|
197 |
break |
|
198 |
case 'u': |
|
199 |
mark = 'underlined' |
|
200 |
break |
|
201 |
default: |
|
202 |
return |
|
203 |
} |
|
204 |
||
205 |
state = state |
|
206 |
.transform() |
|
207 |
.toggleMark(mark) |
|
208 |
.apply() |
|
209 |
||
210 |
e.preventDefault() |
|
211 |
return state |
|
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* When a mark button is clicked, toggle the current mark. |
|
216 |
* |
|
217 |
* @param {Event} e |
|
218 |
* @param {String} type |
|
219 |
*/ |
|
220 |
||
221 |
onClickMark = (e, type) => { |
|
222 |
e.preventDefault() |
|
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
223 |
const { state } = this.state |
|
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
224 |
const transform = state.transform() |
| 5 | 225 |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
226 |
let isPortalOpen = false; |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
227 |
|
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
228 |
if (type === 'category') { |
|
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
229 |
// Can't use toggleMark here, because it expects the same object |
|
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
230 |
// @see https://github.com/ianstormtaylor/slate/issues/873 |
|
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
231 |
if (this.hasMark('category')) { |
|
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
232 |
state.marks.forEach(mark => transform.removeMark(mark)); |
|
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
233 |
} else { |
|
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
234 |
isPortalOpen = !this.state.isPortalOpen; |
|
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
235 |
} |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
236 |
} else { |
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
237 |
transform.toggleMark(type) |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
238 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
239 |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
240 |
this.setState({ |
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
241 |
state: transform.apply(), |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
242 |
isPortalOpen: isPortalOpen |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
243 |
}) |
| 5 | 244 |
} |
245 |
||
246 |
/** |
|
247 |
* When a block button is clicked, toggle the block type. |
|
248 |
* |
|
249 |
* @param {Event} e |
|
250 |
* @param {String} type |
|
251 |
*/ |
|
252 |
||
253 |
onClickBlock = (e, type) => { |
|
254 |
e.preventDefault() |
|
255 |
let { state } = this.state |
|
256 |
const transform = state.transform() |
|
257 |
const { document } = state |
|
258 |
||
259 |
// Handle everything but list buttons. |
|
260 |
if (type !== 'bulleted-list' && type !== 'numbered-list') { |
|
261 |
const isActive = this.hasBlock(type) |
|
262 |
const isList = this.hasBlock('list-item') |
|
263 |
||
264 |
if (isList) { |
|
265 |
transform |
|
266 |
.setBlock(isActive ? DEFAULT_NODE : type) |
|
267 |
.unwrapBlock('bulleted-list') |
|
268 |
.unwrapBlock('numbered-list') |
|
269 |
} |
|
270 |
||
271 |
else { |
|
272 |
transform |
|
273 |
.setBlock(isActive ? DEFAULT_NODE : type) |
|
274 |
} |
|
275 |
} |
|
276 |
||
277 |
// Handle the extra wrapping required for list buttons. |
|
278 |
else { |
|
279 |
const isList = this.hasBlock('list-item') |
|
280 |
const isType = state.blocks.some((block) => { |
|
281 |
return !!document.getClosest(block.key, parent => parent.type === type) |
|
282 |
}) |
|
283 |
||
284 |
if (isList && isType) { |
|
285 |
transform |
|
286 |
.setBlock(DEFAULT_NODE) |
|
287 |
.unwrapBlock('bulleted-list') |
|
288 |
.unwrapBlock('numbered-list') |
|
289 |
} else if (isList) { |
|
290 |
transform |
|
291 |
.unwrapBlock(type === 'bulleted-list' ? 'numbered-list' : 'bulleted-list') |
|
292 |
.wrapBlock(type) |
|
293 |
} else { |
|
294 |
transform |
|
295 |
.setBlock('list-item') |
|
296 |
.wrapBlock(type) |
|
297 |
} |
|
298 |
} |
|
299 |
||
300 |
state = transform.apply() |
|
301 |
this.setState({ state }) |
|
302 |
} |
|
303 |
||
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
304 |
onPortalOpen = (portal) => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
305 |
// When the portal opens, cache the menu element. |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
306 |
this.setState({ hoveringMenu: portal.firstChild }) |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
307 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
308 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
309 |
onPortalClose = (portal) => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
310 |
let { state } = this.state |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
311 |
const transform = state.transform(); |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
312 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
313 |
this.setState({ |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
314 |
state: transform.apply(), |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
315 |
isPortalOpen: false |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
316 |
}) |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
317 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
318 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
319 |
onCategoryClick = (category) => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
320 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
321 |
const { state } = this.state; |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
322 |
const transform = state.transform(); |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
323 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
324 |
state.marks.forEach(mark => transform.removeMark(mark)); |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
325 |
transform.addMark({ |
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
326 |
type: 'category', |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
327 |
data: { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
328 |
text: this.state.currentSelectionText, |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
329 |
color: category.color, |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
330 |
key: category.key |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
331 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
332 |
}) |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
333 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
334 |
this.setState({ |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
335 |
state: transform.apply(), |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
336 |
isPortalOpen: false |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
337 |
}); |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
338 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
339 |
|
| 8 | 340 |
/** |
| 5 | 341 |
* Render. |
342 |
* |
|
343 |
* @return {Element} |
|
344 |
*/ |
|
345 |
||
346 |
render = () => { |
|
347 |
return ( |
|
348 |
<div> |
|
349 |
{this.renderToolbar()} |
|
350 |
{this.renderEditor()} |
|
351 |
</div> |
|
352 |
) |
|
353 |
} |
|
354 |
||
355 |
/** |
|
356 |
* Render the toolbar. |
|
357 |
* |
|
358 |
* @return {Element} |
|
359 |
*/ |
|
360 |
||
361 |
renderToolbar = () => { |
|
362 |
return ( |
|
363 |
<div className="menu toolbar-menu"> |
|
364 |
{this.renderMarkButton('bold', 'format_bold')} |
|
365 |
{this.renderMarkButton('italic', 'format_italic')} |
|
366 |
{this.renderMarkButton('underlined', 'format_underlined')} |
|
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
367 |
{this.renderMarkButton('category', 'label')} |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
368 |
|
| 5 | 369 |
{this.renderBlockButton('numbered-list', 'format_list_numbered')} |
370 |
{this.renderBlockButton('bulleted-list', 'format_list_bulleted')} |
|
371 |
</div> |
|
372 |
) |
|
373 |
} |
|
374 |
||
375 |
/** |
|
376 |
* Render a mark-toggling toolbar button. |
|
377 |
* |
|
378 |
* @param {String} type |
|
379 |
* @param {String} icon |
|
380 |
* @return {Element} |
|
381 |
*/ |
|
382 |
||
383 |
renderMarkButton = (type, icon) => { |
|
384 |
const isActive = this.hasMark(type) |
|
385 |
const onMouseDown = e => this.onClickMark(e, type) |
|
386 |
||
387 |
return ( |
|
388 |
<span className="button" onMouseDown={onMouseDown} data-active={isActive}> |
|
389 |
<span className="material-icons">{icon}</span> |
|
390 |
</span> |
|
391 |
) |
|
392 |
} |
|
393 |
||
394 |
/** |
|
395 |
* Render a block-toggling toolbar button. |
|
396 |
* |
|
397 |
* @param {String} type |
|
398 |
* @param {String} icon |
|
399 |
* @return {Element} |
|
400 |
*/ |
|
401 |
||
402 |
renderBlockButton = (type, icon) => { |
|
403 |
const isActive = this.hasBlock(type) |
|
404 |
const onMouseDown = e => this.onClickBlock(e, type) |
|
405 |
||
406 |
return ( |
|
407 |
<span className="button" onMouseDown={onMouseDown} data-active={isActive}> |
|
408 |
<span className="material-icons">{icon}</span> |
|
409 |
</span> |
|
410 |
) |
|
411 |
} |
|
412 |
||
413 |
/** |
|
414 |
* Render the Slate editor. |
|
415 |
* |
|
416 |
* @return {Element} |
|
417 |
*/ |
|
418 |
||
419 |
renderEditor = () => { |
|
420 |
return ( |
|
421 |
<div className="editor"> |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
422 |
{this.renderHoveringMenu()} |
| 5 | 423 |
<Editor |
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
424 |
ref="editor" |
| 5 | 425 |
spellCheck |
426 |
placeholder={'Enter some rich text...'} |
|
427 |
schema={schema} |
|
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
428 |
plugins={plugins} |
| 5 | 429 |
state={this.state.state} |
430 |
onChange={this.onChange} |
|
431 |
onKeyDown={this.onKeyDown} |
|
432 |
/> |
|
433 |
</div> |
|
434 |
) |
|
435 |
} |
|
436 |
||
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
437 |
renderHoveringMenu = () => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
438 |
return ( |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
439 |
<Portal ref="portal" |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
440 |
isOpened={this.state.isPortalOpen} isOpen={this.state.isPortalOpen} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
441 |
onOpen={this.onPortalOpen} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
442 |
onClose={this.onPortalClose} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
443 |
closeOnOutsideClick={false} closeOnEsc={true}> |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
444 |
<div className="hovering-menu"> |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
445 |
<CategoriesTooltip categories={annotationCategories} onCategoryClick={this.onCategoryClick} /> |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
446 |
</div> |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
447 |
</Portal> |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
448 |
) |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
449 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
450 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
451 |
updateMenu = () => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
452 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
453 |
const { hoveringMenu, state } = this.state |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
454 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
455 |
if (!hoveringMenu) return |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
456 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
457 |
// if (state.isBlurred || state.isCollapsed) { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
458 |
// hoveringMenu.removeAttribute('style') |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
459 |
// return |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
460 |
// } |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
461 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
462 |
const selection = window.getSelection() |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
463 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
464 |
if (selection.isCollapsed) { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
465 |
return |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
466 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
467 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
468 |
const range = selection.getRangeAt(0) |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
469 |
const rect = range.getBoundingClientRect() |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
470 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
471 |
hoveringMenu.style.opacity = 1 |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
472 |
hoveringMenu.style.top = `${rect.top + window.scrollY + hoveringMenu.offsetHeight}px` |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
473 |
hoveringMenu.style.left = `${rect.left + window.scrollX - hoveringMenu.offsetWidth / 2 + rect.width / 2}px` |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
474 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
475 |
|
| 5 | 476 |
} |
477 |
||
478 |
/** |
|
479 |
* Export. |
|
480 |
*/ |
|
481 |
||
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
482 |
export default SlateEditor |