forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeybindings.ts
More file actions
154 lines (138 loc) · 3.9 KB
/
keybindings.ts
File metadata and controls
154 lines (138 loc) · 3.9 KB
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
import { isMac, isIOS } from './platform';
const metaKey = isMac ? (isIOS ? 'Alt' : 'Meta') : 'Alt';
const metaOrCtrlKey = isMac ? (isIOS ? 'Alt' : 'Meta') : 'Control';
const ctrlOrAltKey = isIOS ? 'Alt' : 'Control';
// String.fromCharCode receives UTF-16 code units, but the keyCode represents the actual
// "physical" key on the keyboard. For this reason it's sketchy (some do match) to
// String.fromCharCode(e.keyCode) so we have this table with the correct mapping.
// KeyCode is a weird spec (it is a key event api after all) but it's defined in a way that
// it's i18n safe: In the US keyboard "," and "<" are on the same physical key so they
// both have keyCode 188. One might expect this will break in non-US keyboards since
// these characters are in different physical keys, however, the spec is defined in a way
// that no matter which physical key the "," and the "<" are in, they'll always be keyCode 188.
// http://www.javascripter.net/faq/keycodes.htm
const keyCodeMapping = {
'188': ',',
};
export function normalizeKey(e: KeyboardEvent) {
if (e.key) {
if (e.key.split('').length === 1) {
let key;
if (Object.prototype.hasOwnProperty.call(keyCodeMapping, e.keyCode)) {
key = keyCodeMapping[e.keyCode];
} else {
key = String.fromCharCode(e.keyCode).toUpperCase();
}
if (key === ' ') {
return 'Space';
}
return key;
}
return e.key;
}
return undefined;
}
export function formatKey(key: string) {
switch (key) {
case 'Meta': {
if (isMac) {
return '⌘';
}
return 'Win';
}
case 'Control':
return 'Ctrl';
case ' ':
return 'Space';
case 'Shift':
return '⇧';
default:
if (key.split('').length === 1) {
return key.toUpperCase();
}
return key;
}
}
export const KEYBINDINGS = {
'editor.open-quick-actions': {
title: 'Open Quick Actions',
type: 'View',
bindings: [[metaOrCtrlKey, 'Shift', 'P']],
signal: 'editor.quickActionsOpened',
},
workspace: {
title: 'Toggle Sidebar',
type: 'View',
bindings: [[metaOrCtrlKey, 'B']],
signal: 'workspace.toggleCurrentWorkspaceItem',
},
'editor.close-tab': {
title: 'Close Current Tab',
type: 'View',
bindings: [[ctrlOrAltKey, 'W']],
signal: 'editor.tabClosed',
payload: state => ({
tabIndex: state.editor.tabs
.filter(x => x)
.findIndex(t => t.moduleId === state.currentModuleId),
}),
},
'editor.zen-mode': {
title: 'Toggle Zen Mode',
type: 'View',
bindings: [[metaKey, 'K', 'Z']],
signal: 'preferences.settingChanged',
payload: state => ({
name: 'zenMode',
value: !state.preferences.settings.zenMode,
}),
},
'editor.toggle-console': {
title: 'Toggle Dev Tools',
type: 'View',
bindings: [[metaKey, 'K', 'D']],
signal: 'preferences.devtoolsToggled',
},
'editor.open-preferences': {
title: 'Open Preferences',
type: 'View',
bindings: [[metaOrCtrlKey, ',']],
signal: 'modalOpened',
payload: {
modal: 'preferences',
},
},
'source.dependencies.open': {
title: 'Add Dependency',
type: 'Source',
bindings: [],
signal: 'modalOpened',
payload: {
modal: 'searchDependencies',
},
},
'source.modules.prettify': {
title: 'Prettify Current File',
type: 'Source',
bindings: [],
signal: 'editor.prettifyClicked',
payload: state => ({
moduleShortid: state.editor.currentModule.shortid,
}),
},
'source.modules.save': {
title: 'Save Current File',
type: 'Source',
bindings: [[metaOrCtrlKey, 'S']],
signal: 'editor.codeSaved',
payload: state => ({
moduleShortid: state.editor.currentModule.shortid,
}),
},
'source.modules.save-all': {
title: 'Save All Modified Files',
type: 'Source',
bindings: [[metaOrCtrlKey, 'Shift', 'S']],
signal: 'editor.saveClicked',
},
};