-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeyboardShortcuts.ts
More file actions
520 lines (492 loc) · 14.7 KB
/
keyboardShortcuts.ts
File metadata and controls
520 lines (492 loc) · 14.7 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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
import type {
KeyboardShortcutSettings,
ShortcutBinding,
ShortcutCategoryDefinition,
ShortcutContextOption,
ShortcutDefinition,
ShortcutPlatform,
ShortcutScope,
} from './types';
export const SHORTCUT_CONTEXT_LIBRARY: Record<string, ShortcutContextOption> = {
global: {
id: 'global',
label: 'All contexts',
description: 'Shortcut applies anywhere inside the application interface.',
},
dashboard: {
id: 'dashboard',
label: 'Dashboard view',
description: 'Only active while the dashboard is focused.',
},
repositories: {
id: 'repositories',
label: 'Repository lists',
description: 'Active when repository lists, tables, or cards are focused.',
},
tasks: {
id: 'tasks',
label: 'Task panels',
description: 'Active for task run, log, and configuration panels.',
},
settings: {
id: 'settings',
label: 'Settings view',
description: 'Only active inside the settings experience.',
},
modals: {
id: 'modals',
label: 'Modal dialogs',
description: 'Active while a modal dialog is open and focused.',
},
system: {
id: 'system',
label: 'System wide',
description: 'Available even when the application is not focused.',
},
background: {
id: 'background',
label: 'Background agent',
description: 'Processed by background workers while the UI is closed.',
},
};
export const SHORTCUT_CATEGORIES: ShortcutCategoryDefinition[] = [
{
id: 'navigation',
title: 'Navigation',
description: 'Global movement controls for traversing the interface and quickly reaching areas of the product.',
},
{
id: 'editing',
title: 'Editing & selection',
description: 'Commands that mutate data, toggle selections, or launch editing flows.',
},
{
id: 'views',
title: 'View controls',
description: 'Presentation and layout adjustments that affect what is visible.',
},
{
id: 'tasks',
title: 'Automation & tasks',
description: 'Run, manage, and inspect automation flows and build pipelines.',
},
{
id: 'system',
title: 'System integration',
description: 'Shortcuts that can be elevated to the operating system or background agent.',
},
];
const { global, dashboard, repositories, tasks, settings, modals, system, background } = SHORTCUT_CONTEXT_LIBRARY;
export const SHORTCUT_DEFINITIONS: ShortcutDefinition[] = [
{
id: 'app.navigation.commandPalette',
label: 'Open command palette',
description: 'Launch the searchable command palette for quick navigation and task execution.',
categoryId: 'navigation',
keywords: ['search', 'palette', 'actions', 'omnibox'],
allowApp: true,
allowGlobal: false,
contexts: {
app: [global, dashboard, settings],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Mod+K', context: 'global', platform: 'all' },
],
},
{
id: 'app.navigation.focusRepositoryFilter',
label: 'Focus repository filter',
description: 'Jump to the repository search filter to quickly narrow lists.',
categoryId: 'navigation',
keywords: ['filter', 'search', 'repository', 'focus'],
allowApp: true,
allowGlobal: false,
contexts: {
app: [repositories, dashboard],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Mod+F', context: 'repositories', platform: 'all' },
],
},
{
id: 'app.navigation.switchDashboard',
label: 'Go to dashboard',
description: 'Switch to the dashboard from anywhere in the app.',
categoryId: 'navigation',
keywords: ['dashboard', 'home', 'navigate'],
allowApp: true,
allowGlobal: true,
contexts: {
app: [global],
global: [system],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Mod+1', context: 'global', platform: 'all' },
{ scope: 'global', shortcut: 'Mod+Alt+D', context: 'system', platform: 'all' },
],
},
{
id: 'app.navigation.openSettings',
label: 'Open settings',
description: 'Navigate directly to the settings view.',
categoryId: 'navigation',
keywords: ['settings', 'preferences'],
allowApp: true,
allowGlobal: true,
contexts: {
app: [global],
global: [system],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Mod+,', context: 'global', platform: 'all' },
{ scope: 'global', shortcut: 'Mod+Alt+,', context: 'system', platform: 'all' },
],
},
{
id: 'app.editing.renameRepository',
label: 'Rename repository',
description: 'Rename the currently focused repository.',
categoryId: 'editing',
keywords: ['rename', 'repository', 'edit'],
allowApp: true,
allowGlobal: false,
contexts: {
app: [repositories],
},
defaultBindings: [
{ scope: 'app', shortcut: 'F2', context: 'repositories', platform: 'all' },
],
},
{
id: 'app.editing.duplicateTask',
label: 'Duplicate task',
description: 'Clone the selected automation task for quick iteration.',
categoryId: 'editing',
keywords: ['copy', 'task', 'duplicate'],
allowApp: true,
allowGlobal: false,
contexts: {
app: [tasks],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Mod+D', context: 'tasks', platform: 'all' },
],
},
{
id: 'app.editing.toggleSelection',
label: 'Toggle repository selection',
description: 'Select or clear the currently focused repository item.',
categoryId: 'editing',
keywords: ['select', 'toggle'],
allowApp: true,
allowGlobal: false,
contexts: {
app: [repositories, dashboard],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Space', context: 'repositories', platform: 'all' },
],
},
{
id: 'app.tasks.runSelected',
label: 'Run selected task',
description: 'Execute the highlighted automation task without opening the task modal.',
categoryId: 'tasks',
keywords: ['task', 'run', 'execute'],
allowApp: true,
allowGlobal: false,
contexts: {
app: [tasks, dashboard],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Mod+Enter', context: 'tasks', platform: 'all' },
],
},
{
id: 'app.tasks.cancelActive',
label: 'Cancel active task',
description: 'Abort the active automation run and stop subsequent steps.',
categoryId: 'tasks',
keywords: ['task', 'cancel', 'stop'],
allowApp: true,
allowGlobal: true,
contexts: {
app: [tasks, dashboard],
global: [system],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Shift+Escape', context: 'tasks', platform: 'all' },
{ scope: 'global', shortcut: 'Mod+Shift+Escape', context: 'system', platform: 'all' },
],
},
{
id: 'app.view.toggleTaskLog',
label: 'Toggle task log panel',
description: 'Collapse or expand the persistent task log drawer.',
categoryId: 'views',
keywords: ['logs', 'panel', 'view'],
allowApp: true,
allowGlobal: false,
contexts: {
app: [global, dashboard],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Alt+L', context: 'global', platform: 'all' },
],
},
{
id: 'app.view.toggleTheme',
label: 'Toggle light/dark theme',
description: 'Instantly switch between the light and dark appearance.',
categoryId: 'views',
keywords: ['theme', 'appearance', 'dark'],
allowApp: true,
allowGlobal: true,
contexts: {
app: [global, settings],
global: [system],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Mod+Shift+L', context: 'global', platform: 'all' },
{ scope: 'global', shortcut: 'Mod+Alt+L', context: 'system', platform: 'all' },
],
},
{
id: 'app.view.zoomIn',
label: 'Zoom in',
description: 'Increase interface scale for improved readability.',
categoryId: 'views',
keywords: ['zoom', 'view', 'increase'],
allowApp: true,
allowGlobal: false,
contexts: {
app: [global],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Mod+=', context: 'global', platform: 'all' },
],
},
{
id: 'app.system.quickCapture',
label: 'Quick capture note',
description: 'Open the quick capture overlay to jot down an idea or todo.',
categoryId: 'system',
keywords: ['note', 'capture', 'quick'],
allowApp: true,
allowGlobal: true,
contexts: {
app: [global, dashboard, modals],
global: [system],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Mod+Shift+N', context: 'global', platform: 'all' },
{ scope: 'global', shortcut: 'Mod+Shift+Space', context: 'system', platform: 'all' },
],
},
{
id: 'app.system.toggleAutomationPause',
label: 'Toggle automation pause',
description: 'Pause or resume all background automation agents.',
categoryId: 'system',
keywords: ['pause', 'automation', 'system'],
allowApp: true,
allowGlobal: true,
contexts: {
app: [global, dashboard],
global: [system, background],
},
defaultBindings: [
{ scope: 'app', shortcut: 'Mod+Shift+P', context: 'global', platform: 'all' },
{ scope: 'global', shortcut: 'Mod+Alt+P', context: 'system', platform: 'all' },
],
},
{
id: 'app.system.bringToFront',
label: 'Bring app to front',
description: 'Focus the dashboard window and ensure it is visible.',
categoryId: 'system',
keywords: ['focus', 'activate', 'system'],
allowApp: false,
allowGlobal: true,
contexts: {
global: [system],
},
defaultBindings: [
{ scope: 'global', shortcut: 'Mod+Alt+G', context: 'system', platform: 'all' },
],
},
];
const generateBindingId = (actionId: string, scope: ShortcutScope, seed: number) =>
`${actionId.replace(/[^a-zA-Z0-9]/g, '-')}-${scope}-${seed}`;
const cloneBinding = (
actionId: string,
binding: Omit<ShortcutBinding, 'id'>,
seed: number,
markDefault: boolean,
): ShortcutBinding => ({
...binding,
id: generateBindingId(actionId, binding.scope, seed),
isDefault: markDefault,
});
const ensureContext = (action: ShortcutDefinition, scope: ShortcutScope, contextId?: string): string => {
const available = action.contexts?.[scope];
if (!available || available.length === 0) {
return 'global';
}
if (contextId && available.some(ctx => ctx.id === contextId)) {
return contextId;
}
return available[0]?.id ?? 'global';
};
export const createDefaultKeyboardShortcutSettings = (): KeyboardShortcutSettings => {
const bindings: Record<string, ShortcutBinding[]> = {};
SHORTCUT_DEFINITIONS.forEach(action => {
bindings[action.id] = action.defaultBindings.map((binding, index) =>
cloneBinding(
action.id,
{
...binding,
context: ensureContext(action, binding.scope, binding.context),
},
index,
true,
),
);
});
return {
version: 1,
lastUpdated: null,
bindings,
};
};
const ensureBindingShape = (
action: ShortcutDefinition,
binding: ShortcutBinding,
seed: number,
): ShortcutBinding => ({
...binding,
id: binding.id ?? generateBindingId(action.id, binding.scope, seed),
context: ensureContext(action, binding.scope, binding.context),
platform: binding.platform ?? 'all',
isDefault: binding.isDefault ?? false,
});
export const mergeKeyboardShortcutSettings = (
existing?: KeyboardShortcutSettings | null,
): KeyboardShortcutSettings => {
const defaults = createDefaultKeyboardShortcutSettings();
if (!existing || !existing.bindings) {
return defaults;
}
const merged: KeyboardShortcutSettings = {
version: Math.max(existing.version ?? 1, defaults.version),
lastUpdated: existing.lastUpdated ?? null,
bindings: {},
};
const definitionMap = new Map<string, ShortcutDefinition>(
SHORTCUT_DEFINITIONS.map(def => [def.id, def]),
);
// Include every known action, merging custom bindings when present.
definitionMap.forEach(action => {
const existingBindings = existing.bindings[action.id];
if (!existingBindings || existingBindings.length === 0) {
merged.bindings[action.id] = defaults.bindings[action.id].map(binding => ({ ...binding }));
return;
}
merged.bindings[action.id] = existingBindings.map((binding, index) =>
ensureBindingShape(action, binding, index),
);
});
// Preserve user-defined actions that may have been injected from experiments.
Object.keys(existing.bindings).forEach(actionId => {
if (merged.bindings[actionId]) {
return;
}
merged.bindings[actionId] = existing.bindings[actionId].map((binding, index) => ({
...binding,
id: binding.id ?? generateBindingId(actionId, binding.scope, index),
platform: binding.platform ?? 'all',
}));
});
return merged;
};
export const getDefaultBindingsForAction = (
actionId: string,
): ShortcutBinding[] => {
const definition = SHORTCUT_DEFINITIONS.find(action => action.id === actionId);
if (!definition) {
return [];
}
return definition.defaultBindings.map((binding, index) =>
cloneBinding(
definition.id,
{
...binding,
context: ensureContext(definition, binding.scope, binding.context),
},
index,
true,
),
);
};
export const findShortcutDefinition = (actionId: string): ShortcutDefinition | undefined =>
SHORTCUT_DEFINITIONS.find(action => action.id === actionId);
export const shortcutKey = (binding: ShortcutBinding): string =>
`${binding.scope}:${binding.context}:${binding.platform}:${binding.shortcut.toLowerCase()}`;
export const detectShortcutPlatform = (): ShortcutPlatform => {
if (typeof navigator !== 'undefined') {
const platform = navigator.platform ?? navigator.userAgent ?? '';
if (/Mac|iPod|iPhone|iPad/i.test(platform)) {
return 'mac';
}
if (/Win/i.test(platform)) {
return 'windows';
}
if (/Linux/i.test(platform)) {
return 'linux';
}
}
if (typeof process !== 'undefined' && typeof process.platform === 'string') {
if (process.platform === 'darwin') {
return 'mac';
}
if (process.platform === 'win32') {
return 'windows';
}
if (process.platform === 'linux') {
return 'linux';
}
}
return 'windows';
};
const displayPart = (part: string, platform: ShortcutPlatform): string => {
switch (part) {
case 'Mod':
return platform === 'mac' ? '⌘' : 'Ctrl';
case 'Cmd':
return '⌘';
case 'Ctrl':
return 'Ctrl';
case 'Win':
return platform === 'mac' ? '⌘' : 'Win';
case 'Option':
return platform === 'mac' ? '⌥' : 'Alt';
case 'Alt':
return 'Alt';
case 'Shift':
return '⇧';
default:
return part;
}
};
export const formatShortcutForDisplay = (
shortcut: string,
platform: ShortcutPlatform = detectShortcutPlatform(),
): string => {
if (!shortcut) {
return '';
}
return shortcut
.split('+')
.map(part => displayPart(part, platform))
.join(' + ');
};