-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
ShortcutInput.tsx
501 lines (462 loc) · 14.4 KB
/
ShortcutInput.tsx
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
* Copyright (c) Jupyter Development Team.
* Distributed under the terms of the Modified BSD License.
*/
import * as React from 'react';
import { ITranslator } from '@jupyterlab/translation';
import { EN_US } from '@lumino/keyboard';
import { checkIcon, errorIcon } from '@jupyterlab/ui-components';
export interface IShortcutInputProps {
handleUpdate: Function;
deleteShortcut: Function;
toggleInput: Function;
shortcut: ShortcutObject;
shortcutId: string;
toSymbols: Function;
keyBindingsUsed: { [index: string]: TakenByObject };
sortConflict: Function;
clearConflicts: Function;
displayInput: boolean;
newOrReplace: string;
placeholder: string;
translator: ITranslator;
}
export interface IShortcutInputState {
value: string;
userInput: string;
isAvailable: boolean;
isFunctional: boolean;
takenByObject: TakenByObject;
keys: Array<string>;
currentChain: string;
selected: boolean;
}
/** Object for shortcut items */
export class ShortcutObject {
commandName: string;
label: string;
keys: { [index: string]: Array<string> };
source: string;
selector: string;
category: string;
id: string;
hasConflict: boolean;
numberOfShortcuts: number;
constructor() {
this.commandName = '';
this.label = '';
this.keys = {};
this.source = '';
this.selector = '';
this.category = '';
this.id = '';
this.numberOfShortcuts = 0;
this.hasConflict = false;
}
get(sortCriteria: string): string {
if (sortCriteria === 'label') {
return this.label;
} else if (sortCriteria === 'selector') {
return this.selector;
} else if (sortCriteria === 'category') {
return this.category;
} else if (sortCriteria === 'source') {
return this.source;
} else {
return '';
}
}
}
/** Object for conflicting shortcut error messages */
export class ErrorObject extends ShortcutObject {
takenBy: TakenByObject;
constructor() {
super();
this.takenBy = new TakenByObject();
}
}
/** Object for showing which shortcut conflicts with the new one */
export class TakenByObject {
takenBy: ShortcutObject;
takenByKey: string;
takenByLabel: string;
id: string;
constructor(shortcut?: ShortcutObject) {
if (shortcut) {
this.takenBy = shortcut;
this.takenByKey = '';
this.takenByLabel = shortcut.category + ': ' + shortcut.label;
this.id = shortcut.commandName + '_' + shortcut.selector;
} else {
this.takenBy = new ShortcutObject();
this.takenByKey = '';
this.takenByLabel = '';
this.id = '';
}
}
}
export class ShortcutInput extends React.Component<
IShortcutInputProps,
IShortcutInputState
> {
constructor(props: IShortcutInputProps) {
super(props);
this.state = {
value: this.props.placeholder,
userInput: '',
isAvailable: true,
isFunctional: this.props.newOrReplace === 'replace',
takenByObject: new TakenByObject(),
keys: new Array<string>(),
currentChain: '',
selected: true
};
}
handleUpdate = () => {
let keys = this.state.keys;
keys.push(this.state.currentChain);
this.setState({ keys: keys });
this.props.handleUpdate(this.props.shortcut, this.state.keys);
};
handleOverwrite = async () => {
this.props
.deleteShortcut(
this.state.takenByObject.takenBy,
this.state.takenByObject.takenByKey
)
.then(this.handleUpdate());
};
handleReplace = async () => {
let keys = this.state.keys;
keys.push(this.state.currentChain);
this.props.toggleInput();
await this.props.deleteShortcut(this.props.shortcut, this.props.shortcutId);
this.props.handleUpdate(this.props.shortcut, keys);
};
/** Parse user input for chained shortcuts */
parseChaining = (
event: React.KeyboardEvent,
value: string,
userInput: string,
keys: Array<string>,
currentChain: string
): Array<any> => {
let key = EN_US.keyForKeydownEvent(event.nativeEvent);
const modKeys = ['Shift', 'Control', 'Alt', 'Meta', 'Ctrl', 'Accel'];
if (event.key === 'Backspace') {
userInput = '';
value = '';
keys = [];
currentChain = '';
this.setState({
value: value,
userInput: userInput,
keys: keys,
currentChain: currentChain
});
} else if (event.key !== 'CapsLock') {
const lastKey = userInput
.substr(userInput.lastIndexOf(' ') + 1, userInput.length)
.trim();
/** if last key was not a modefier then there is a chain */
if (modKeys.lastIndexOf(lastKey) === -1 && lastKey != '') {
userInput = userInput + ',';
keys.push(currentChain);
currentChain = '';
/** check if a modefier key was held down through chain */
if (event.ctrlKey && event.key != 'Control') {
userInput = (userInput + ' Ctrl').trim();
currentChain = (currentChain + ' Ctrl').trim();
}
if (event.metaKey && event.key != 'Meta') {
userInput = (userInput + ' Accel').trim();
currentChain = (currentChain + ' Accel').trim();
}
if (event.altKey && event.key != 'Alt') {
userInput = (userInput + ' Alt').trim();
currentChain = (currentChain + ' Alt').trim();
}
if (event.shiftKey && event.key != 'Shift') {
userInput = (userInput + ' Shift').trim();
currentChain = (currentChain + ' Shift').trim();
}
/** if not a modefier key, add to user input and current chain */
if (modKeys.lastIndexOf(event.key) === -1) {
userInput = (userInput + ' ' + key).trim();
currentChain = (currentChain + ' ' + key).trim();
/** if a modefier key, add to user input and current chain */
} else {
if (event.key === 'Meta') {
userInput = (userInput + ' Accel').trim();
currentChain = (currentChain + ' Accel').trim();
} else if (event.key === 'Control') {
userInput = (userInput + ' Ctrl').trim();
currentChain = (currentChain + ' Ctrl').trim();
} else if (event.key === 'Shift') {
userInput = (userInput + ' Shift').trim();
currentChain = (currentChain + ' Shift').trim();
} else if (event.key === 'Alt') {
userInput = (userInput + ' Alt').trim();
currentChain = (currentChain + ' Alt').trim();
} else {
userInput = (userInput + ' ' + event.key).trim();
currentChain = (currentChain + ' ' + event.key).trim();
}
}
/** if not a chain, add the key to user input and current chain */
} else {
/** if modefier key, rename */
if (event.key === 'Control') {
userInput = (userInput + ' Ctrl').trim();
currentChain = (currentChain + ' Ctrl').trim();
} else if (event.key === 'Meta') {
userInput = (userInput + ' Accel').trim();
currentChain = (currentChain + ' Accel').trim();
} else if (event.key === 'Shift') {
userInput = (userInput + ' Shift').trim();
currentChain = (currentChain + ' Shift').trim();
} else if (event.key === 'Alt') {
userInput = (userInput + ' Alt').trim();
currentChain = (currentChain + ' Alt').trim();
/** if not a modefier key, add it regularly */
} else {
userInput = (userInput + ' ' + key).trim();
currentChain = (currentChain + ' ' + key).trim();
}
}
}
/** update state of keys and currentChain */
this.setState({
keys: keys,
currentChain: currentChain
});
return [userInput, keys, currentChain];
};
/**
* Check if shorcut being typed will work
* (does not end with ctrl, alt, command, or shift)
* */
checkNonFunctional = (shortcut: string): boolean => {
const dontEnd = ['Ctrl', 'Alt', 'Accel', 'Shift'];
const shortcutKeys = this.state.currentChain.split(' ');
const last = shortcutKeys[shortcutKeys.length - 1];
this.setState({
isFunctional: !(dontEnd.indexOf(last) !== -1)
});
return dontEnd.indexOf(last) !== -1;
};
/** Check if shortcut being typed is already taken */
checkShortcutAvailability = (
userInput: string,
keys: string[],
currentChain: string
): TakenByObject => {
/** First, check whole shortcut */
let isAvailable =
Object.keys(this.props.keyBindingsUsed).indexOf(
keys.join(' ') + currentChain + '_' + this.props.shortcut.selector
) === -1 || userInput === '';
let takenByObject: TakenByObject = new TakenByObject();
if (isAvailable) {
/** Next, check each piece of a chain */
for (let binding of keys) {
if (
Object.keys(this.props.keyBindingsUsed).indexOf(
binding + '_' + this.props.shortcut.selector
) !== -1 &&
binding !== ''
) {
isAvailable = false;
takenByObject =
this.props.keyBindingsUsed[
binding + '_' + this.props.shortcut.selector
];
break;
}
}
/** Check current chain */
if (
isAvailable &&
Object.keys(this.props.keyBindingsUsed).indexOf(
currentChain + '_' + this.props.shortcut.selector
) !== -1 &&
currentChain !== ''
) {
isAvailable = false;
takenByObject =
this.props.keyBindingsUsed[
currentChain + '_' + this.props.shortcut.selector
];
}
/** If unavailable set takenByObject */
} else {
takenByObject =
this.props.keyBindingsUsed[
keys.join(' ') + currentChain + '_' + this.props.shortcut.selector
];
}
/** allow to set shortcut to what it initially was if replacing */
if (!isAvailable) {
if (
takenByObject.takenBy.id === this.props.shortcut.id &&
this.props.newOrReplace === 'replace'
) {
isAvailable = true;
takenByObject = new TakenByObject();
}
}
this.setState({ isAvailable: isAvailable });
return takenByObject;
};
checkConflict(takenByObject: TakenByObject, keys: string): void {
if (
takenByObject.id !== '' &&
takenByObject.takenBy.id !== this.props.shortcut.id
) {
this.props.sortConflict(
this.props.shortcut,
takenByObject,
takenByObject.takenByLabel,
''
);
} else {
this.props.clearConflicts();
}
}
/** Parse and normalize user input */
handleInput = (event: React.KeyboardEvent): void => {
event.preventDefault();
this.setState({ selected: false });
const parsed = this.parseChaining(
event,
this.state.value,
this.state.userInput,
this.state.keys,
this.state.currentChain
);
const userInput = parsed[0];
const keys = parsed[1];
const currentChain = parsed[2];
const value = this.props.toSymbols(userInput);
let takenByObject = this.checkShortcutAvailability(
userInput,
keys,
currentChain
);
this.checkConflict(takenByObject, keys);
this.setState(
{
value: value,
userInput: userInput,
takenByObject: takenByObject,
keys: keys,
currentChain: currentChain
},
() => this.checkNonFunctional(this.state.userInput)
);
};
handleBlur = (event: React.FocusEvent<HTMLDivElement>) => {
if (
event.relatedTarget === null ||
((event.relatedTarget as HTMLElement).id !== 'no-blur' &&
(event.relatedTarget as HTMLElement).id !== 'overwrite')
) {
this.props.toggleInput();
this.setState({
value: '',
userInput: ''
});
this.props.clearConflicts();
}
};
render() {
const trans = this.props.translator.load('jupyterlab');
let inputClassName = 'jp-Shortcuts-Input';
if (!this.state.isAvailable) {
inputClassName += ' jp-mod-unavailable-Input';
}
return (
<div
className={
this.props.displayInput
? this.props.newOrReplace === 'new'
? 'jp-Shortcuts-InputBox jp-Shortcuts-InputBoxNew'
: 'jp-Shortcuts-InputBox'
: 'jp-mod-hidden'
}
onBlur={event => this.handleBlur(event)}
>
<div
tabIndex={0}
id="no-blur"
className={inputClassName}
onKeyDown={this.handleInput}
ref={input => input && input.focus()}
>
<p
className={
this.state.selected && this.props.newOrReplace === 'replace'
? 'jp-Shortcuts-InputText jp-mod-selected-InputText'
: this.state.value === ''
? 'jp-Shortcuts-InputText jp-mod-waiting-InputText'
: 'jp-Shortcuts-InputText'
}
>
{this.state.value === ''
? trans.__('press keys')
: this.state.value}
</p>
</div>
<button
className={
!this.state.isFunctional
? 'jp-Shortcuts-Submit jp-mod-defunc-Submit'
: !this.state.isAvailable
? 'jp-Shortcuts-Submit jp-mod-conflict-Submit'
: 'jp-Shortcuts-Submit'
}
id={'no-blur'}
disabled={!this.state.isAvailable || !this.state.isFunctional}
onClick={() => {
if (this.props.newOrReplace === 'new') {
this.handleUpdate();
this.setState({
value: '',
keys: [],
currentChain: ''
});
this.props.toggleInput();
} else {
/** don't replace if field has not been edited */
if (this.state.selected) {
this.props.toggleInput();
this.setState({
value: '',
userInput: ''
});
this.props.clearConflicts();
} else {
void this.handleReplace();
}
}
}}
>
{this.state.isAvailable ? <checkIcon.react /> : <errorIcon.react />}
</button>
{!this.state.isAvailable && (
<button
hidden
id="overwrite"
onClick={() => {
void this.handleOverwrite();
this.props.clearConflicts();
this.props.toggleInput();
}}
>
{trans.__('Overwrite')}
</button>
)}
</div>
);
}
}