-
Notifications
You must be signed in to change notification settings - Fork 17
/
event-callbacks.js
219 lines (210 loc) · 6.31 KB
/
event-callbacks.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
/* Main DOM event-handlers */
import keyboard, { TAB_NEXT, TAB_PREV } from 'core/keyboard';
import {
injectTabsInList,
addHeadTabListNodeSelectedStyle,
} from './utils/dom';
import { navigateResults } from './utils/keyboard';
import {
switchActiveTab,
restoreClosedTab,
deleteTab,
openBookmark,
openHistoryItem,
} from './utils/browser';
import {
deleteButton,
searchInput,
tabList,
SESSION_TYPE,
HISTORY_TYPE,
BOOKMARK_TYPE,
TAB_TYPE,
OTHER_WINDOW_TAB_TYPE,
TAB_DELETE_BTN_CLASSNAME,
} from './constants';
import { updateLastQuery } from './actions';
import filterResults from './search';
export function configureSearch(store) {
const { getState, tabQueryPromise, currentWindowId } = store;
const { fuzzy, general } = getState();
return function updateSearchResults(event = { currentTarget: { value: '' } }) {
const isSearchEmpty = event.currentTarget.value.length === 0;
// If input is empty hide the button
if (isSearchEmpty) {
deleteButton.classList.add('hidden');
} else {
deleteButton.classList.remove('hidden');
}
// isSearchEmpty isn't based on this var so we can show delete button with
// just spaces
const query = event.currentTarget.value.trim().toLowerCase();
return Promise.resolve(tabQueryPromise())
.then(filterResults(query, fuzzy, general, currentWindowId))
.then(injectTabsInList(getState))
.then((results) => {
// Apply the selected style to the head of the tabList to suggest
// pressing <Enter> from the search input activates this tab
if (results.length > 0 && !isSearchEmpty) {
addHeadTabListNodeSelectedStyle();
// Scroll to the top
tabList.firstElementChild.scrollIntoView(true);
}
return results;
});
};
}
export function clearInput(event) {
event.target.value = '';
tabList.childNodes[0].focus();
}
// Merges maps into the one specified in target
function mapAssign(target, ...sources) {
return sources.reduce(
(acc, map) => {
// eslint-disable-next-line no-restricted-syntax
for (const [key, value] of map) {
acc.set(key, value);
}
return acc;
},
target,
);
}
// Given an object returns a Map with the keys and values swapped
function swapKeyValueMap(obj, f = x => x) {
return Object.keys(obj)
.reduce(
(acc, key) => (f(obj[key]) ? acc.set(f(obj[key]), key) : acc),
new Map(),
);
}
function isModifierSingle(event) {
const modifiers = [
'Control',
'Ctrl',
'Alt',
'Shift',
'Meta',
'Shift',
];
return modifiers.some(m => event.key === m);
}
export function keydownHandler(store) {
const navigate = cmdKey => navigateResults(cmdKey, store.getState);
const { keyboard: keyboardControls } = store.getState();
// The keyboard object is an object with the mapping { [ACTION]: kbdcommand }
// Mirror the keys and values so we have a Map:
// {[kbdCommand]: ACTION}
const kbdControlMap = mapAssign(
swapKeyValueMap(keyboardControls, x => x.command),
swapKeyValueMap(keyboardControls, x => x.secondaryCommand),
);
return function handleKeyDown(event) {
if (isModifierSingle(event)) {
event.preventDefault();
}
// Handle preventing default key actions
switch (event.key) {
case 'Tab':
// // Change it so tab no longer focuses the entire popup window
// // and behaves like a TAB_NEXT command
// // This could break the tab-fix if the popup focus bug occurs
// // so remove this if someone complains
//
case 'ArrowUp':
case 'ArrowDown':
case 'Enter':
case 'PageDown':
case 'PageUp':
event.preventDefault();
break;
default: break;
}
if (keyboard.isValid(event)) {
const cmd = keyboard.command(event);
const key = [...kbdControlMap.keys()].find(x => keyboard.isEqual(x, cmd));
return navigate(kbdControlMap.get(key));
}
// Keys that require special behavior
// In the case of 'End' or 'Home' this breaks the behavior of skipping to
// the beginning of the end of the line in the searchInput.
// Maybe we should only focus if the searchInput isn't active?
switch (event.key) {
case 'End':
if (document.activeElement !== searchInput) {
event.preventDefault();
tabList.lastChild.focus();
}
break;
case 'Home':
if (document.activeElement !== searchInput) {
event.preventDefault();
tabList.firstChild.focus();
}
break;
case 'PageDown':
return navigate(TAB_NEXT);
case 'PageUp':
return navigate(TAB_PREV);
case 'Tab':
return navigate(event.shiftKey ? TAB_PREV : TAB_NEXT);
default: break;
}
const shouldJustFocusSearchBar = (event.key === 'Backspace' && !isModifierSingle(event))
|| (/^([A-Za-z]|\d)$/.test(event.key) && !isModifierSingle(event));
if (shouldJustFocusSearchBar) {
searchInput.focus();
}
return noop();
};
}
function noop() {}
export function handleTabClick(getState) {
return function doHandleTabClick(event) {
const generalSettings = getState().general;
const { currentTarget, ctrlKey, target } = event;
if (target.nodeName === 'IMG'
&& target.classList.contains(TAB_DELETE_BTN_CLASSNAME)) {
return deleteTab(currentTarget, generalSettings, true);
}
const { dataset } = currentTarget;
const { type } = dataset;
// Decide what to do depending if ctrl is held
switch (type) {
case OTHER_WINDOW_TAB_TYPE:
case TAB_TYPE: {
if (ctrlKey) {
return deleteTab(currentTarget, generalSettings, true);
}
return switchActiveTab(dataset);
}
case SESSION_TYPE: {
if (ctrlKey) {
return noop();
}
return restoreClosedTab(dataset);
}
case BOOKMARK_TYPE: {
if (ctrlKey) {
return noop();
}
return openBookmark(dataset);
}
case HISTORY_TYPE: {
if (ctrlKey) {
return noop();
}
return openHistoryItem(dataset);
}
default: return noop();
}
};
}
export function updateLastQueryOnKeydown(store) {
const { dispatch } = store;
return (event) => {
const { value } = event.currentTarget;
dispatch(updateLastQuery(value.trim()));
};
}