| author | ymh <ymh.work@gmail.com> |
| Fri, 04 Aug 2017 09:48:09 +0200 | |
| changeset 138 | a1fb2ced3049 |
| parent 103 | dc906a578616 |
| child 143 | cfcbf4bc66f1 |
| 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' |
|
72
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
4 |
import { Button } from 'react-bootstrap' |
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
5 |
import Immutable from 'immutable' |
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
6 |
import HtmlSerializer from '../HtmlSerializer' |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
7 |
import AnnotationPlugin from '../AnnotationPlugin' |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
8 |
import CategoriesTooltip from './CategoriesTooltip' |
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
72
diff
changeset
|
9 |
import { now } from '../utils'; |
|
138
a1fb2ced3049
propagate annotations categories from session protocol definition
ymh <ymh.work@gmail.com>
parents:
103
diff
changeset
|
10 |
import { defaultAnnotationsCategories } from '../constants'; |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
11 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
12 |
const plugins = []; |
| 5 | 13 |
|
14 |
/** |
|
15 |
* Define the default node type. |
|
16 |
*/ |
|
17 |
||
18 |
const DEFAULT_NODE = 'paragraph' |
|
19 |
||
20 |
/** |
|
21 |
* Define a schema. |
|
22 |
* |
|
23 |
* @type {Object} |
|
24 |
*/ |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
25 |
// 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
|
26 |
// https://docs.slatejs.org/reference/plugins/plugin.html#schema |
| 5 | 27 |
const schema = { |
28 |
nodes: { |
|
29 |
'bulleted-list': props => <ul {...props.attributes}>{props.children}</ul>, |
|
30 |
'list-item': props => <li {...props.attributes}>{props.children}</li>, |
|
31 |
'numbered-list': props => <ol {...props.attributes}>{props.children}</ol>, |
|
32 |
}, |
|
33 |
marks: { |
|
34 |
bold: { |
|
35 |
fontWeight: 'bold' |
|
36 |
}, |
|
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
37 |
category: props => { |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
38 |
const data = props.mark.data; |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
39 |
return <span style={{ backgroundColor: data.get('color') }}>{props.children}</span> |
| 5 | 40 |
}, |
41 |
italic: { |
|
42 |
fontStyle: 'italic' |
|
43 |
}, |
|
44 |
underlined: { |
|
45 |
textDecoration: 'underline' |
|
46 |
} |
|
47 |
} |
|
48 |
} |
|
49 |
||
50 |
/** |
|
51 |
* The rich text example. |
|
52 |
* |
|
53 |
* @type {Component} |
|
54 |
*/ |
|
55 |
||
| 8 | 56 |
class SlateEditor extends React.Component { |
| 5 | 57 |
|
58 |
/** |
|
59 |
* Deserialize the initial editor state. |
|
60 |
* |
|
61 |
* @type {Object} |
|
62 |
*/ |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
63 |
constructor(props) { |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
64 |
super(props); |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
65 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
66 |
const annotationPlugin = AnnotationPlugin({ |
|
102
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
67 |
onChange: (text, start, end) => { |
|
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
68 |
this.setState({ |
|
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
69 |
currentSelectionText: text, |
|
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
70 |
currentSelectionStart: start, |
|
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
71 |
currentSelectionEnd: end |
|
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
72 |
}); |
|
19
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 |
|
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
76 |
plugins.push(annotationPlugin); |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
77 |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
78 |
this.state = { |
|
79
772b73e31069
Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
74
diff
changeset
|
79 |
state: props.note ? Raw.deserialize(props.note.raw) : Plain.deserialize(''), |
|
16
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
80 |
startedAt: null, |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
81 |
finishedAt: null, |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
82 |
currentSelectionText: '', |
|
102
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
83 |
currentSelectionStart: 0, |
|
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
84 |
currentSelectionEnd: 0, |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
85 |
hoveringMenu: null, |
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
86 |
isPortalOpen: false, |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
87 |
categories: Immutable.List([]), |
|
72
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
88 |
isCheckboxChecked: false, |
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
89 |
}; |
|
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 |
componentDidMount = () => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
93 |
this.updateMenu(); |
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
94 |
this.focus(); |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
95 |
} |
| 5 | 96 |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
97 |
componentDidUpdate = () => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
98 |
this.updateMenu(); |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
99 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
100 |
|
| 5 | 101 |
/** |
102 |
* Check if the current selection has a mark with `type` in it. |
|
103 |
* |
|
104 |
* @param {String} type |
|
105 |
* @return {Boolean} |
|
106 |
*/ |
|
107 |
||
108 |
hasMark = (type) => { |
|
109 |
const { state } = this.state |
|
110 |
return state.marks.some(mark => mark.type === type) |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Check if the any of the currently selected blocks are of `type`. |
|
115 |
* |
|
116 |
* @param {String} type |
|
117 |
* @return {Boolean} |
|
118 |
*/ |
|
119 |
||
120 |
hasBlock = (type) => { |
|
121 |
const { state } = this.state |
|
122 |
return state.blocks.some(node => node.type === type) |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* On change, save the new state. |
|
127 |
* |
|
128 |
* @param {State} state |
|
129 |
*/ |
|
130 |
||
131 |
onChange = (state) => { |
|
|
16
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 |
let newState = { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
134 |
state: state, |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
135 |
startedAt: this.state.startedAt |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
136 |
}; |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
137 |
|
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
138 |
const isEmpty = state.document.length === 0; |
|
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 |
// Reset timers when the text is empty |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
141 |
if (isEmpty) { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
142 |
Object.assign(newState, { |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
143 |
startedAt: null, |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
144 |
finishedAt: null |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
145 |
}); |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
146 |
} else { |
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
72
diff
changeset
|
147 |
Object.assign(newState, { finishedAt: now() }); |
|
16
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 |
// 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
|
151 |
if (!isEmpty && this.state.startedAt === null) { |
|
74
043477fd5c5c
add api call to save notes. internally use ts for time data for notes and session
ymh <ymh.work@gmail.com>
parents:
72
diff
changeset
|
152 |
Object.assign(newState, { startedAt: now() }); |
|
16
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
153 |
} |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
154 |
|
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
155 |
this.setState(newState) |
|
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
156 |
|
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
157 |
if (typeof this.props.onChange === 'function') { |
|
16
e67cd18cc594
Draft implementation of note timing.
Alexandre Segura <mex.zktk@gmail.com>
parents:
15
diff
changeset
|
158 |
this.props.onChange(newState); |
| 8 | 159 |
} |
| 5 | 160 |
} |
161 |
||
162 |
asPlain = () => { |
|
163 |
return Plain.serialize(this.state.state); |
|
164 |
} |
|
165 |
||
| 15 | 166 |
asRaw = () => { |
167 |
return Raw.serialize(this.state.state); |
|
168 |
} |
|
169 |
||
|
17
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
170 |
asHtml = () => { |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
171 |
return HtmlSerializer.serialize(this.state.state); |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
172 |
} |
|
877d8796b86d
Store serialized HTML in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
16
diff
changeset
|
173 |
|
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
174 |
asCategories = () => { |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
175 |
return this.state.categories |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
176 |
} |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
177 |
|
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
178 |
removeCategory = (categories, key, text) => { |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
179 |
const categoryIndex = categories.findIndex(category => category.key === key && category.text === text) |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
180 |
return categories.delete(categoryIndex) |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
181 |
} |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
182 |
|
| 5 | 183 |
clear = () => { |
184 |
const state = Plain.deserialize(''); |
|
| 8 | 185 |
this.onChange(state); |
| 5 | 186 |
} |
187 |
||
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
188 |
focus = () => { |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
189 |
this.refs.editor.focus(); |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
190 |
} |
|
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
191 |
|
| 5 | 192 |
/** |
193 |
* On key down, if it's a formatting command toggle a mark. |
|
194 |
* |
|
195 |
* @param {Event} e |
|
196 |
* @param {Object} data |
|
197 |
* @param {State} state |
|
198 |
* @return {State} |
|
199 |
*/ |
|
200 |
||
201 |
onKeyDown = (e, data, state) => { |
|
|
64
aecde527900a
Introduce adding note on "enter" key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
62
diff
changeset
|
202 |
|
|
72
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
203 |
if (data.key === 'enter' && this.props.isChecked && typeof this.props.onEnterKeyDown === 'function') { |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
204 |
e.preventDefault(); |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
205 |
this.props.onEnterKeyDown(); |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
206 |
|
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
207 |
return state; |
|
64
aecde527900a
Introduce adding note on "enter" key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
62
diff
changeset
|
208 |
} |
|
aecde527900a
Introduce adding note on "enter" key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
62
diff
changeset
|
209 |
|
| 5 | 210 |
if (!data.isMod) return |
211 |
let mark |
|
212 |
||
213 |
switch (data.key) { |
|
214 |
case 'b': |
|
215 |
mark = 'bold' |
|
216 |
break |
|
217 |
case 'i': |
|
218 |
mark = 'italic' |
|
219 |
break |
|
220 |
case 'u': |
|
221 |
mark = 'underlined' |
|
222 |
break |
|
223 |
default: |
|
224 |
return |
|
225 |
} |
|
226 |
||
227 |
state = state |
|
228 |
.transform() |
|
229 |
.toggleMark(mark) |
|
230 |
.apply() |
|
231 |
||
232 |
e.preventDefault() |
|
233 |
return state |
|
234 |
} |
|
235 |
||
236 |
/** |
|
237 |
* When a mark button is clicked, toggle the current mark. |
|
238 |
* |
|
239 |
* @param {Event} e |
|
240 |
* @param {String} type |
|
241 |
*/ |
|
242 |
||
243 |
onClickMark = (e, type) => { |
|
244 |
e.preventDefault() |
|
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
245 |
const { state } = this.state |
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
246 |
let { categories } = this.state |
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
247 |
const transform = state.transform() |
| 5 | 248 |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
249 |
let isPortalOpen = false; |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
250 |
|
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
251 |
if (type === 'category') { |
|
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
252 |
// 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
|
253 |
// @see https://github.com/ianstormtaylor/slate/issues/873 |
|
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
254 |
if (this.hasMark('category')) { |
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
255 |
const categoryMarks = state.marks.filter(mark => mark.type === 'category') |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
256 |
categoryMarks.forEach(mark => { |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
257 |
const key = mark.data.get('key'); |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
258 |
const text = mark.data.get('text'); |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
259 |
|
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
260 |
categories = this.removeCategory(categories, key, text) |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
261 |
transform.removeMark(mark) |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
262 |
}) |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
263 |
|
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
264 |
} else { |
|
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
265 |
isPortalOpen = !this.state.isPortalOpen; |
|
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
266 |
} |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
267 |
} else { |
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
268 |
transform.toggleMark(type) |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
269 |
} |
|
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
270 |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
271 |
this.setState({ |
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
272 |
state: transform.apply(), |
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
273 |
isPortalOpen: isPortalOpen, |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
274 |
categories: categories |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
275 |
}) |
| 5 | 276 |
} |
277 |
||
278 |
/** |
|
279 |
* When a block button is clicked, toggle the block type. |
|
280 |
* |
|
281 |
* @param {Event} e |
|
282 |
* @param {String} type |
|
283 |
*/ |
|
284 |
||
285 |
onClickBlock = (e, type) => { |
|
286 |
e.preventDefault() |
|
287 |
let { state } = this.state |
|
288 |
const transform = state.transform() |
|
289 |
const { document } = state |
|
290 |
||
291 |
// Handle everything but list buttons. |
|
292 |
if (type !== 'bulleted-list' && type !== 'numbered-list') { |
|
293 |
const isActive = this.hasBlock(type) |
|
294 |
const isList = this.hasBlock('list-item') |
|
295 |
||
296 |
if (isList) { |
|
297 |
transform |
|
298 |
.setBlock(isActive ? DEFAULT_NODE : type) |
|
299 |
.unwrapBlock('bulleted-list') |
|
300 |
.unwrapBlock('numbered-list') |
|
301 |
} |
|
302 |
||
303 |
else { |
|
304 |
transform |
|
305 |
.setBlock(isActive ? DEFAULT_NODE : type) |
|
306 |
} |
|
307 |
} |
|
308 |
||
309 |
// Handle the extra wrapping required for list buttons. |
|
310 |
else { |
|
311 |
const isList = this.hasBlock('list-item') |
|
312 |
const isType = state.blocks.some((block) => { |
|
313 |
return !!document.getClosest(block.key, parent => parent.type === type) |
|
314 |
}) |
|
315 |
||
316 |
if (isList && isType) { |
|
317 |
transform |
|
318 |
.setBlock(DEFAULT_NODE) |
|
319 |
.unwrapBlock('bulleted-list') |
|
320 |
.unwrapBlock('numbered-list') |
|
321 |
} else if (isList) { |
|
322 |
transform |
|
323 |
.unwrapBlock(type === 'bulleted-list' ? 'numbered-list' : 'bulleted-list') |
|
324 |
.wrapBlock(type) |
|
325 |
} else { |
|
326 |
transform |
|
327 |
.setBlock('list-item') |
|
328 |
.wrapBlock(type) |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
state = transform.apply() |
|
333 |
this.setState({ state }) |
|
334 |
} |
|
335 |
||
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
336 |
onPortalOpen = (portal) => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
337 |
// When the portal opens, cache the menu element. |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
338 |
this.setState({ hoveringMenu: portal.firstChild }) |
|
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 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
341 |
onPortalClose = (portal) => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
342 |
let { state } = this.state |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
343 |
const transform = state.transform(); |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
344 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
345 |
this.setState({ |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
346 |
state: transform.apply(), |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
347 |
isPortalOpen: false |
|
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 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
350 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
351 |
onCategoryClick = (category) => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
352 |
|
|
102
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
353 |
const { state, currentSelectionText, currentSelectionStart, currentSelectionEnd } = this.state; |
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
354 |
let { categories } = this.state; |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
355 |
const transform = state.transform(); |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
356 |
|
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
357 |
const categoryMarks = state.marks.filter(mark => mark.type === 'category') |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
358 |
categoryMarks.forEach(mark => transform.removeMark(mark)); |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
359 |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
360 |
transform.addMark({ |
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
361 |
type: 'category', |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
362 |
data: { |
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
363 |
text: currentSelectionText, |
|
102
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
364 |
selection: { |
|
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
365 |
start: currentSelectionStart, |
|
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
366 |
end: currentSelectionEnd, |
|
b0e36664f1f2
Add TextPositionSelector.
Alexandre Segura <mex.zktk@gmail.com>
parents:
80
diff
changeset
|
367 |
}, |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
368 |
color: category.color, |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
369 |
key: category.key |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
370 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
371 |
}) |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
372 |
|
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
373 |
Object.assign(category, { |
| 103 | 374 |
text: currentSelectionText, |
375 |
selection: { |
|
376 |
start: currentSelectionStart, |
|
377 |
end: currentSelectionEnd, |
|
378 |
}, |
|
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
379 |
}); |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
380 |
categories = categories.push(category); |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
381 |
|
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
382 |
this.setState({ |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
383 |
state: transform.apply(), |
|
26
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
384 |
isPortalOpen: false, |
|
930e486ad0a8
Store categories in note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
25
diff
changeset
|
385 |
categories: categories |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
386 |
}); |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
387 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
388 |
|
|
72
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
389 |
onButtonClick = () => { |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
390 |
if (typeof this.props.onButtonClick === 'function') { |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
391 |
this.props.onButtonClick(); |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
392 |
} |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
393 |
} |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
394 |
|
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
395 |
onCheckboxChange = (e) => { |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
396 |
if (typeof this.props.onCheckboxChange === 'function') { |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
397 |
this.props.onCheckboxChange(e); |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
398 |
} |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
399 |
} |
|
7634b424f426
Add checkbox to add not on enter key.
Alexandre Segura <mex.zktk@gmail.com>
parents:
64
diff
changeset
|
400 |
|
| 8 | 401 |
/** |
| 5 | 402 |
* Render. |
403 |
* |
|
404 |
* @return {Element} |
|
405 |
*/ |
|
406 |
||
407 |
render = () => { |
|
408 |
return ( |
|
409 |
<div> |
|
410 |
{this.renderToolbar()} |
|
411 |
{this.renderEditor()} |
|
412 |
</div> |
|
413 |
) |
|
414 |
} |
|
415 |
||
416 |
/** |
|
417 |
* Render the toolbar. |
|
418 |
* |
|
419 |
* @return {Element} |
|
420 |
*/ |
|
421 |
||
422 |
renderToolbar = () => { |
|
423 |
return ( |
|
424 |
<div className="menu toolbar-menu"> |
|
425 |
{this.renderMarkButton('bold', 'format_bold')} |
|
426 |
{this.renderMarkButton('italic', 'format_italic')} |
|
427 |
{this.renderMarkButton('underlined', 'format_underlined')} |
|
|
25
e04714a1d4eb
Improve categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
21
diff
changeset
|
428 |
{this.renderMarkButton('category', 'label')} |
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
429 |
|
| 5 | 430 |
{this.renderBlockButton('numbered-list', 'format_list_numbered')} |
431 |
{this.renderBlockButton('bulleted-list', 'format_list_bulleted')} |
|
|
79
772b73e31069
Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
74
diff
changeset
|
432 |
|
|
772b73e31069
Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
74
diff
changeset
|
433 |
{this.renderToolbarButtons()} |
| 5 | 434 |
</div> |
435 |
) |
|
436 |
} |
|
437 |
||
|
80
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
438 |
renderToolbarCheckbox = () => { |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
439 |
return ( |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
440 |
<div className="checkbox"> |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
441 |
<label> |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
442 |
<input type="checkbox" checked={this.props.isChecked} onChange={this.onCheckboxChange} /> <kbd>Enter</kbd> = add note |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
443 |
</label> |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
444 |
</div> |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
445 |
) |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
446 |
} |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
447 |
|
|
79
772b73e31069
Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
74
diff
changeset
|
448 |
renderToolbarButtons = () => { |
|
772b73e31069
Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
74
diff
changeset
|
449 |
return ( |
|
772b73e31069
Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
74
diff
changeset
|
450 |
<div> |
|
80
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
451 |
{ !this.props.note && this.renderToolbarCheckbox() } |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
452 |
<Button bsStyle="primary" disabled={this.props.isButtonDisabled} onClick={this.onButtonClick}> |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
453 |
{ this.props.note ? 'Save note' : 'Add note' } |
|
b3a02ea6d097
Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents:
79
diff
changeset
|
454 |
</Button> |
|
79
772b73e31069
Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
74
diff
changeset
|
455 |
</div> |
|
772b73e31069
Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
74
diff
changeset
|
456 |
); |
|
772b73e31069
Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
74
diff
changeset
|
457 |
} |
|
772b73e31069
Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents:
74
diff
changeset
|
458 |
|
| 5 | 459 |
/** |
460 |
* Render a mark-toggling toolbar button. |
|
461 |
* |
|
462 |
* @param {String} type |
|
463 |
* @param {String} icon |
|
464 |
* @return {Element} |
|
465 |
*/ |
|
466 |
||
467 |
renderMarkButton = (type, icon) => { |
|
468 |
const isActive = this.hasMark(type) |
|
469 |
const onMouseDown = e => this.onClickMark(e, type) |
|
470 |
||
471 |
return ( |
|
472 |
<span className="button" onMouseDown={onMouseDown} data-active={isActive}> |
|
473 |
<span className="material-icons">{icon}</span> |
|
474 |
</span> |
|
475 |
) |
|
476 |
} |
|
477 |
||
478 |
/** |
|
479 |
* Render a block-toggling toolbar button. |
|
480 |
* |
|
481 |
* @param {String} type |
|
482 |
* @param {String} icon |
|
483 |
* @return {Element} |
|
484 |
*/ |
|
485 |
||
486 |
renderBlockButton = (type, icon) => { |
|
487 |
const isActive = this.hasBlock(type) |
|
488 |
const onMouseDown = e => this.onClickBlock(e, type) |
|
489 |
||
490 |
return ( |
|
491 |
<span className="button" onMouseDown={onMouseDown} data-active={isActive}> |
|
492 |
<span className="material-icons">{icon}</span> |
|
493 |
</span> |
|
494 |
) |
|
495 |
} |
|
496 |
||
497 |
/** |
|
498 |
* Render the Slate editor. |
|
499 |
* |
|
500 |
* @return {Element} |
|
501 |
*/ |
|
502 |
||
503 |
renderEditor = () => { |
|
504 |
return ( |
|
|
35
97106bacb24e
Improve session page design.
Alexandre Segura <mex.zktk@gmail.com>
parents:
26
diff
changeset
|
505 |
<div className="editor-slatejs"> |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
506 |
{this.renderHoveringMenu()} |
| 5 | 507 |
<Editor |
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
508 |
ref="editor" |
| 5 | 509 |
spellCheck |
510 |
placeholder={'Enter some rich text...'} |
|
511 |
schema={schema} |
|
|
19
f1b125b95fe9
Introduce "annotation" plugin.
Alexandre Segura <mex.zktk@gmail.com>
parents:
17
diff
changeset
|
512 |
plugins={plugins} |
| 5 | 513 |
state={this.state.state} |
514 |
onChange={this.onChange} |
|
515 |
onKeyDown={this.onKeyDown} |
|
516 |
/> |
|
517 |
</div> |
|
518 |
) |
|
519 |
} |
|
520 |
||
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
521 |
renderHoveringMenu = () => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
522 |
return ( |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
523 |
<Portal ref="portal" |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
524 |
isOpened={this.state.isPortalOpen} isOpen={this.state.isPortalOpen} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
525 |
onOpen={this.onPortalOpen} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
526 |
onClose={this.onPortalClose} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
527 |
closeOnOutsideClick={false} closeOnEsc={true}> |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
528 |
<div className="hovering-menu"> |
|
138
a1fb2ced3049
propagate annotations categories from session protocol definition
ymh <ymh.work@gmail.com>
parents:
103
diff
changeset
|
529 |
<CategoriesTooltip categories={this.props.annotationCategories || defaultAnnotationsCategories} onCategoryClick={this.onCategoryClick} /> |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
530 |
</div> |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
531 |
</Portal> |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
532 |
) |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
533 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
534 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
535 |
updateMenu = () => { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
536 |
|
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
35
diff
changeset
|
537 |
const { hoveringMenu } = this.state |
|
21
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
538 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
539 |
if (!hoveringMenu) return |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
540 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
541 |
// if (state.isBlurred || state.isCollapsed) { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
542 |
// hoveringMenu.removeAttribute('style') |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
543 |
// return |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
544 |
// } |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
545 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
546 |
const selection = window.getSelection() |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
547 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
548 |
if (selection.isCollapsed) { |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
549 |
return |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
550 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
551 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
552 |
const range = selection.getRangeAt(0) |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
553 |
const rect = range.getBoundingClientRect() |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
554 |
|
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
555 |
hoveringMenu.style.opacity = 1 |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
556 |
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
|
557 |
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
|
558 |
} |
|
284e866f55c7
First version of categories tooltip.
Alexandre Segura <mex.zktk@gmail.com>
parents:
19
diff
changeset
|
559 |
|
| 5 | 560 |
} |
561 |
||
562 |
/** |
|
563 |
* Export. |
|
564 |
*/ |
|
565 |
||
|
11
6fb4de54acea
Delete default state, focus on textarea.
Alexandre Segura <mex.zktk@gmail.com>
parents:
8
diff
changeset
|
566 |
export default SlateEditor |