-
Notifications
You must be signed in to change notification settings - Fork 1k
/
index.js
343 lines (296 loc) · 11.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import React, { Component, PropTypes } from 'react';
import { genKey } from 'draft-js';
import { List } from 'immutable';
import Entry from './Entry';
import addMention from '../modifiers/addMention';
import decodeOffsetKey from '../utils/decodeOffsetKey';
import getSearchText from '../utils/getSearchText';
import defaultEntryComponent from './Entry/defaultEntryComponent';
export default class MentionSuggestions extends Component {
static propTypes = {
entityMutability: PropTypes.oneOf([
'SEGMENTED',
'IMMUTABLE',
'MUTABLE',
]),
entryComponent: PropTypes.func,
onAddMention: PropTypes.func,
suggestions: (props, propName, componentName) => {
if (!List.isList(props[propName])) {
return new Error(
`Invalid prop \`${propName}\` supplied to \`${componentName}\`. should be an instance of immutable list.`
);
}
return undefined;
},
};
state = {
isActive: false,
focusedOptionIndex: 0,
};
componentWillMount() {
this.key = genKey();
this.props.callbacks.onChange = this.onEditorStateChange;
}
componentWillReceiveProps(nextProps) {
if (nextProps.suggestions.size === 0 && this.state.isActive) {
this.closeDropdown();
}
}
componentDidUpdate = (prevProps, prevState) => {
if (this.popover) {
// In case the list shrinks there should be still an option focused.
// Note: this might run multiple times and deduct 1 until the condition is
// not fullfilled anymore.
const size = this.props.suggestions.size;
if (size > 0 && this.state.focusedOptionIndex >= size) {
this.setState({
focusedOptionIndex: size - 1,
});
}
// Note: this is a simple protection for the error when componentDidUpdate
// try to get new getPortalClientRect, but the key already was deleted by
// previous action. (right now, it only can happened when set the mention
// trigger to be multi-characters which not supported anyway!)
if (!this.props.store.getAllSearches().has(this.activeOffsetKey)) {
return;
}
const decoratorRect = this.props.store.getPortalClientRect(this.activeOffsetKey);
const newStyles = this.props.positionSuggestions({
decoratorRect,
prevProps,
prevState,
props: this.props,
state: this.state,
popover: this.popover,
});
Object.keys(newStyles).forEach((key) => {
this.popover.style[key] = newStyles[key];
});
}
};
componentWillUnmount = () => {
this.props.callbacks.onChange = undefined;
};
onEditorStateChange = (editorState) => {
const searches = this.props.store.getAllSearches();
// if no search portal is active there is no need to show the popover
if (searches.size === 0) {
return editorState;
}
const removeList = () => {
this.props.store.resetEscapedSearch();
this.closeDropdown();
return editorState;
};
// get the current selection
const selection = editorState.getSelection();
const anchorKey = selection.getAnchorKey();
const anchorOffset = selection.getAnchorOffset();
// the list should not be visible if a range is selected or the editor has no focus
if (!selection.isCollapsed() || !selection.getHasFocus()) return removeList();
// identify the start & end positon of each search-text
const offsetDetails = searches.map((offsetKey) => decodeOffsetKey(offsetKey));
// a leave can be empty when it is removed due e.g. using backspace
const leaves = offsetDetails
.filter(({ blockKey }) => blockKey === anchorKey)
.map(({ blockKey, decoratorKey, leafKey }) => (
editorState
.getBlockTree(blockKey)
.getIn([decoratorKey, 'leaves', leafKey])
));
// if all leaves are undefined the popover should be removed
if (leaves.every((leave) => leave === undefined)) {
return removeList();
}
// Checks that the cursor is after the @ character but still somewhere in
// the word (search term). Setting it to allow the cursor to be left of
// the @ causes troubles due selection confusion.
const selectionIsInsideWord = leaves
.filter((leave) => leave !== undefined)
.map(({ start, end }) => (
(start === 0 && anchorOffset === 1 && anchorOffset <= end) || // @ is the first character
(anchorOffset > start + 1 && anchorOffset <= end) // @ is in the text or at the end
));
if (selectionIsInsideWord.every((isInside) => isInside === false)) return removeList();
const lastActiveOffsetKey = this.activeOffsetKey;
this.activeOffsetKey = selectionIsInsideWord
.filter((value) => value === true)
.keySeq()
.first();
this.onSearchChange(editorState, selection, this.activeOffsetKey, lastActiveOffsetKey);
// make sure the escaped search is reseted in the cursor since the user
// already switched to another mention search
if (!this.props.store.isEscaped(this.activeOffsetKey)) {
this.props.store.resetEscapedSearch();
}
// If none of the above triggered to close the window, it's safe to assume
// the dropdown should be open. This is useful when a user focuses on another
// input field and then comes back: the dropdown will again.
if (!this.state.isActive && !this.props.store.isEscaped(this.activeOffsetKey)) {
this.openDropdown();
}
// makes sure the focused index is reseted every time a new selection opens
// or the selection was moved to another mention search
if (this.lastSelectionIsInsideWord === undefined ||
!selectionIsInsideWord.equals(this.lastSelectionIsInsideWord)) {
this.setState({
focusedOptionIndex: 0,
});
}
this.lastSelectionIsInsideWord = selectionIsInsideWord;
return editorState;
};
onSearchChange = (editorState, selection, activeOffsetKey, lastActiveOffsetKey) => {
const { word } = getSearchText(editorState, selection);
const searchValue = word.substring(1, word.length);
if (this.lastSearchValue !== searchValue || activeOffsetKey !== lastActiveOffsetKey) {
this.lastSearchValue = searchValue;
this.props.onSearchChange({ value: searchValue });
}
};
onDownArrow = (keyboardEvent) => {
keyboardEvent.preventDefault();
const newIndex = this.state.focusedOptionIndex + 1;
this.onMentionFocus(newIndex >= this.props.suggestions.size ? 0 : newIndex);
};
onTab = (keyboardEvent) => {
keyboardEvent.preventDefault();
this.commitSelection();
};
onUpArrow = (keyboardEvent) => {
keyboardEvent.preventDefault();
if (this.props.suggestions.size > 0) {
const newIndex = this.state.focusedOptionIndex - 1;
this.onMentionFocus(newIndex < 0 ? this.props.suggestions.size - 1 : newIndex);
}
};
onEscape = (keyboardEvent) => {
keyboardEvent.preventDefault();
const activeOffsetKey = this.lastSelectionIsInsideWord
.filter((value) => value === true)
.keySeq()
.first();
this.props.store.escapeSearch(activeOffsetKey);
this.closeDropdown();
// to force a re-render of the outer component to change the aria props
this.props.store.setEditorState(this.props.store.getEditorState());
};
onMentionSelect = (mention) => {
// Note: This can happen in case a user typed @xxx (invalid mention) and
// then hit Enter. Then the mention will be undefined.
if (!mention) {
return;
}
if (this.props.onAddMention) {
this.props.onAddMention(mention);
}
this.closeDropdown();
const newEditorState = addMention(
this.props.store.getEditorState(),
mention,
this.props.mentionPrefix,
this.props.mentionTrigger,
this.props.entityMutability,
);
this.props.store.setEditorState(newEditorState);
};
onMentionFocus = (index) => {
const descendant = `mention-option-${this.key}-${index}`;
this.props.ariaProps.ariaActiveDescendantID = descendant;
this.state.focusedOptionIndex = index;
// to force a re-render of the outer component to change the aria props
this.props.store.setEditorState(this.props.store.getEditorState());
};
commitSelection = () => {
this.onMentionSelect(this.props.suggestions.get(this.state.focusedOptionIndex));
return 'handled';
};
openDropdown = () => {
// This is a really nasty way of attaching & releasing the key related functions.
// It assumes that the keyFunctions object will not loose its reference and
// by this we can replace inner parameters spread over different modules.
// This better be some registering & unregistering logic. PRs are welcome :)
this.props.callbacks.onDownArrow = this.onDownArrow;
this.props.callbacks.onUpArrow = this.onUpArrow;
this.props.callbacks.onEscape = this.onEscape;
this.props.callbacks.handleReturn = this.commitSelection;
this.props.callbacks.onTab = this.onTab;
const descendant = `mention-option-${this.key}-${this.state.focusedOptionIndex}`;
this.props.ariaProps.ariaActiveDescendantID = descendant;
this.props.ariaProps.ariaOwneeID = `mentions-list-${this.key}`;
this.props.ariaProps.ariaHasPopup = 'true';
this.props.ariaProps.ariaExpanded = 'true';
this.setState({
isActive: true,
});
if (this.props.onOpen) {
this.props.onOpen();
}
};
closeDropdown = () => {
// make sure none of these callbacks are triggered
this.props.callbacks.onDownArrow = undefined;
this.props.callbacks.onUpArrow = undefined;
this.props.callbacks.onTab = undefined;
this.props.callbacks.onEscape = undefined;
this.props.callbacks.handleReturn = undefined;
this.props.ariaProps.ariaHasPopup = 'false';
this.props.ariaProps.ariaExpanded = 'false';
this.props.ariaProps.ariaActiveDescendantID = undefined;
this.props.ariaProps.ariaOwneeID = undefined;
this.setState({
isActive: false,
});
if (this.props.onClose) {
this.props.onClose();
}
};
render() {
if (!this.state.isActive) {
return null;
}
const {
entryComponent,
onClose, // eslint-disable-line no-unused-vars
onOpen, // eslint-disable-line no-unused-vars
onAddMention, // eslint-disable-line no-unused-vars, no-shadow
onSearchChange, // eslint-disable-line no-unused-vars, no-shadow
suggestions, // eslint-disable-line no-unused-vars
ariaProps, // eslint-disable-line no-unused-vars
callbacks, // eslint-disable-line no-unused-vars
theme = {},
store, // eslint-disable-line no-unused-vars
entityMutability, // eslint-disable-line no-unused-vars
positionSuggestions, // eslint-disable-line no-unused-vars
mentionTrigger, // eslint-disable-line no-unused-vars
mentionPrefix, // eslint-disable-line no-unused-vars
...elementProps } = this.props;
return (
<div
{...elementProps}
className={theme.mentionSuggestions}
role="listbox"
id={`mentions-list-${this.key}`}
ref={(element) => { this.popover = element; }}
>
{
this.props.suggestions.map((mention, index) => (
<Entry
key={mention.has('id') ? mention.get('id') : mention.get('name')}
onMentionSelect={this.onMentionSelect}
onMentionFocus={this.onMentionFocus}
isFocused={this.state.focusedOptionIndex === index}
mention={mention}
index={index}
id={`mention-option-${this.key}-${index}`}
theme={theme}
searchValue={this.lastSearchValue}
entryComponent={entryComponent || defaultEntryComponent}
/>
)).toJS()
}
</div>
);
}
}