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