-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathKeyboardShortcutItem.jsx
138 lines (124 loc) · 3.18 KB
/
KeyboardShortcutItem.jsx
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
import React, { useRef, useState } from 'react';
import PropTypes from 'prop-types';
import { useEditorKeyMap } from './Editor/contexts';
function KeyboardShortcutItem({ desc, keyName }) {
const [edit, setEdit] = useState(false);
const pressedKeyCombination = useRef({});
const inputRef = useRef(null);
const { updateKeyMap, keyMaps } = useEditorKeyMap();
if (!Object.keys(keyMaps).includes(keyName)) {
return null;
}
const cancelEdit = () => {
setEdit(false);
pressedKeyCombination.current = {};
inputRef.current.innerText = keyMaps[keyName];
};
const handleEdit = (state, key) => {
setEdit(state);
if (!state) {
updateKeyMap(key, inputRef.current.innerText);
cancelEdit();
}
};
const handleKeyDown = (event) => {
if (!edit) return;
event.preventDefault();
event.stopPropagation();
let { key } = event;
if (key === 'Control') {
key = 'Ctrl';
}
if (key === ' ') {
key = 'Space';
}
if (key.length === 1 && key.match(/[a-z]/i)) {
key = key.toUpperCase();
}
pressedKeyCombination.current[key] = true;
const allKeys = Object.keys(pressedKeyCombination.current).filter(
(k) => !['Shift', 'Ctrl', 'Alt'].includes(k)
);
if (event.altKey) {
allKeys.unshift('Alt');
}
if (event.ctrlKey) {
allKeys.unshift('Ctrl');
}
if (event.shiftKey) {
allKeys.unshift('Shift');
}
event.currentTarget.innerText = allKeys.join('-');
};
const handleKeyUp = (event) => {
if (!edit) return;
event.preventDefault();
event.stopPropagation();
let { key } = event;
if (key === 'Control') {
key = 'Ctrl';
}
if (key === ' ') {
key = 'Space';
}
if (key.length === 1 && key.match(/[a-z]/i)) {
key = key.toUpperCase();
}
delete pressedKeyCombination.current[key];
};
return (
<li className="keyboard-shortcut-item">
<button
type="button"
title="edit shortcut"
className="keyboard-shortcut__edit"
style={{
display: edit ? 'none' : 'block'
}}
onClick={() => handleEdit(true, keyName)}
>
✎
</button>
<button
type="button"
title="cancel shortcut edit"
className="keyboard-shortcut__edit"
style={{
display: !edit ? 'none' : 'block'
}}
onClick={cancelEdit}
>
⨯
</button>
<button
type="button"
title="save shortcut"
className="keyboard-shortcut__edit"
style={{
display: !edit ? 'none' : 'block'
}}
onClick={() => handleEdit(false, keyName)}
>
✓
</button>
<span
className="keyboard-shortcut__command"
role="textbox"
ref={inputRef}
tabIndex={0}
contentEditable={edit}
suppressContentEditableWarning
onKeyDown={handleKeyDown}
onKeyUp={handleKeyUp}
>
{keyMaps[keyName]}
</span>
<span>{desc}</span>
</li>
);
}
KeyboardShortcutItem.propTypes = {
desc: PropTypes.string.isRequired,
keyName: PropTypes.string.isRequired
};
export default KeyboardShortcutItem;