forked from mjrussell/draft-js-autocomplete-plugin-creator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
125 lines (112 loc) · 3.89 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
import completionSuggestionsCreator from './CompletionSuggestions';
import CompletionSuggestionsPortal from './CompletionSuggestionsPortal';
import decorateComponentWithProps from 'decorate-component-with-props';
import { Map } from 'immutable';
import suggestionsFilter from './utils/defaultSuggestionsFilter';
import defaultPositionSuggestions from './utils/positionSuggestions';
import defaultSelectionPredicate from './utils/defaultSelectionPredicate';
const createCompletionPlugin = (
completionSuggestionsStrategy,
addModifier,
SuggestionEntry,
suggestionsThemeKey = 'completionSuggestions',
additionalDecorators = [],
selectionPredicate = defaultSelectionPredicate,
) => (config = {}) => {
const callbacks = {
keyBindingFn: undefined,
handleKeyCommand: undefined,
onDownArrow: undefined,
onUpArrow: undefined,
onTab: undefined,
onEscape: undefined,
handleReturn: undefined,
onChange: undefined,
};
const ariaProps = {
ariaHasPopup: 'false',
ariaExpanded: 'false',
ariaOwneeID: undefined,
ariaActiveDescendantID: undefined,
};
let searches = Map();
let escapedSearch = undefined;
let clientRectFunctions = Map();
const store = {
getEditorState: undefined,
setEditorState: undefined,
getPortalClientRect: (offsetKey) => clientRectFunctions.get(offsetKey)(),
getAllSearches: () => searches,
isEscaped: (offsetKey) => escapedSearch === offsetKey,
escapeSearch: (offsetKey) => {
escapedSearch = offsetKey;
},
resetEscapedSearch: () => {
escapedSearch = undefined;
},
register: (offsetKey) => {
searches = searches.set(offsetKey, offsetKey);
},
updatePortalClientRect: (offsetKey, func) => {
clientRectFunctions = clientRectFunctions.set(offsetKey, func);
},
unregister: (offsetKey) => {
searches = searches.delete(offsetKey);
clientRectFunctions = clientRectFunctions.delete(offsetKey);
},
};
const {
theme = {},
positionSuggestions = defaultPositionSuggestions,
} = config;
const completionSearchProps = {
ariaProps,
callbacks,
theme,
store,
entityMutability: config.entityMutability ? config.entityMutability : 'SEGMENTED',
positionSuggestions,
};
const CompletionSuggestions = completionSuggestionsCreator(
addModifier,
SuggestionEntry,
suggestionsThemeKey,
selectionPredicate,
);
return {
CompletionSuggestions: decorateComponentWithProps(CompletionSuggestions, completionSearchProps),
decorators: [
{
strategy: completionSuggestionsStrategy,
component: decorateComponentWithProps(CompletionSuggestionsPortal, { store }),
},
...additionalDecorators,
],
getAccessibilityProps: () => (
{
role: 'combobox',
ariaAutoComplete: 'list',
ariaHasPopup: ariaProps.ariaHasPopup,
ariaExpanded: ariaProps.ariaExpanded,
ariaActiveDescendantID: ariaProps.ariaActiveDescendantID,
ariaOwneeID: ariaProps.ariaOwneeID,
}
),
initialize: ({ getEditorState, setEditorState }) => {
store.getEditorState = getEditorState;
store.setEditorState = setEditorState;
},
onDownArrow: (keyboardEvent) => callbacks.onDownArrow && callbacks.onDownArrow(keyboardEvent),
onTab: (keyboardEvent) => callbacks.onTab && callbacks.onTab(keyboardEvent),
onUpArrow: (keyboardEvent) => callbacks.onUpArrow && callbacks.onUpArrow(keyboardEvent),
onEscape: (keyboardEvent) => callbacks.onEscape && callbacks.onEscape(keyboardEvent),
handleReturn: (keyboardEvent) => callbacks.handleReturn && callbacks.handleReturn(keyboardEvent),
onChange: (editorState) => {
if (callbacks.onChange) return callbacks.onChange(editorState);
return editorState;
},
};
};
export default createCompletionPlugin;
export const defaultSuggestionsFilter = suggestionsFilter;
export getSearchText from './utils/getSearchText';