refac
This commit is contained in:
1 parent
1760b073c7
commit
343eb1d659
22 files changed
+572
-308
No files matched your search
+187
-25
@@ -1,16 +1,21 @@
|
||||
type ShortcutRegistry = {
|
||||
[key in Shortcut]?: {
|
||||
name: string;
|
||||
keys: string[];
|
||||
category: string;
|
||||
tooltip?: string;
|
||||
setting?: {
|
||||
id: string;
|
||||
value: any;
|
||||
};
|
||||
import { get, writable } from 'svelte/store';
|
||||
|
||||
export type ShortcutDefinition = {
|
||||
name: string;
|
||||
keys: string[];
|
||||
category: string;
|
||||
tooltip?: string;
|
||||
configurable?: boolean;
|
||||
setting?: {
|
||||
id: string;
|
||||
value: unknown;
|
||||
};
|
||||
};
|
||||
|
||||
type ShortcutRegistry = {
|
||||
[key in Shortcut]?: ShortcutDefinition;
|
||||
};
|
||||
|
||||
export enum Shortcut {
|
||||
//Chat
|
||||
NEW_CHAT = 'newChat',
|
||||
@@ -46,66 +51,219 @@ export enum Shortcut {
|
||||
TOGGLE_MUTE = 'toggleMute'
|
||||
}
|
||||
|
||||
export const CONFIGURABLE_SHORTCUTS = [
|
||||
Shortcut.NEW_CHAT,
|
||||
Shortcut.NEW_TEMPORARY_CHAT,
|
||||
Shortcut.DELETE_CHAT,
|
||||
Shortcut.OPEN_MODEL_SELECTOR,
|
||||
Shortcut.TOGGLE_DICTATION,
|
||||
Shortcut.SEARCH,
|
||||
Shortcut.OPEN_SETTINGS,
|
||||
Shortcut.SHOW_SHORTCUTS,
|
||||
Shortcut.TOGGLE_SIDEBAR,
|
||||
Shortcut.CLOSE_MODAL,
|
||||
Shortcut.FOCUS_INPUT,
|
||||
Shortcut.GENERATE_MESSAGE_PAIR,
|
||||
Shortcut.REGENERATE_RESPONSE,
|
||||
Shortcut.COPY_LAST_CODE_BLOCK,
|
||||
Shortcut.COPY_LAST_RESPONSE
|
||||
] as const;
|
||||
|
||||
export type ConfigurableShortcut = (typeof CONFIGURABLE_SHORTCUTS)[number];
|
||||
export type KeybindingsMap = Record<ConfigurableShortcut, string>;
|
||||
|
||||
export const DEFAULT_KEYBINDINGS: KeybindingsMap = {
|
||||
[Shortcut.NEW_CHAT]: 'Cmd+Shift+O',
|
||||
[Shortcut.NEW_TEMPORARY_CHAT]: "Cmd+Shift+'",
|
||||
[Shortcut.DELETE_CHAT]: 'Cmd+Shift+Backspace',
|
||||
[Shortcut.OPEN_MODEL_SELECTOR]: 'Cmd+Shift+M',
|
||||
[Shortcut.TOGGLE_DICTATION]: 'Cmd+Shift+L',
|
||||
[Shortcut.SEARCH]: 'Cmd+K',
|
||||
[Shortcut.OPEN_SETTINGS]: 'Cmd+.',
|
||||
[Shortcut.SHOW_SHORTCUTS]: 'Cmd+/',
|
||||
[Shortcut.TOGGLE_SIDEBAR]: 'Cmd+Shift+S',
|
||||
[Shortcut.CLOSE_MODAL]: 'Escape',
|
||||
[Shortcut.FOCUS_INPUT]: 'Shift+Escape',
|
||||
[Shortcut.GENERATE_MESSAGE_PAIR]: 'Cmd+Shift+Enter',
|
||||
[Shortcut.REGENERATE_RESPONSE]: 'Cmd+R',
|
||||
[Shortcut.COPY_LAST_CODE_BLOCK]: 'Cmd+Shift+;',
|
||||
[Shortcut.COPY_LAST_RESPONSE]: 'Cmd+Shift+C'
|
||||
};
|
||||
|
||||
export const keybindings = writable<KeybindingsMap>({ ...DEFAULT_KEYBINDINGS });
|
||||
|
||||
export function isConfigurableShortcut(id: Shortcut): id is ConfigurableShortcut {
|
||||
return (CONFIGURABLE_SHORTCUTS as readonly Shortcut[]).includes(id);
|
||||
}
|
||||
|
||||
export function loadKeybindings(saved: Partial<Record<string, string>> | undefined): void {
|
||||
if (!saved || typeof saved !== 'object') return;
|
||||
|
||||
keybindings.update((current) => {
|
||||
const updated = { ...current };
|
||||
for (const id of CONFIGURABLE_SHORTCUTS) {
|
||||
if (typeof saved[id] === 'string') {
|
||||
updated[id] = saved[id]!;
|
||||
}
|
||||
}
|
||||
return updated;
|
||||
});
|
||||
}
|
||||
|
||||
export function resetKeybindings(): void {
|
||||
keybindings.set({ ...DEFAULT_KEYBINDINGS });
|
||||
}
|
||||
|
||||
const IS_MAC = typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.userAgent);
|
||||
|
||||
export function eventToChord(event: KeyboardEvent): string {
|
||||
const parts: string[] = [];
|
||||
|
||||
if (IS_MAC) {
|
||||
if (event.metaKey) parts.push('Cmd');
|
||||
if (event.ctrlKey) parts.push('Ctrl');
|
||||
} else if (event.ctrlKey) {
|
||||
parts.push('Cmd');
|
||||
}
|
||||
|
||||
if (event.altKey) parts.push('Alt');
|
||||
if (event.shiftKey) parts.push('Shift');
|
||||
|
||||
let key = event.key;
|
||||
if (['Meta', 'Control', 'Alt', 'Shift'].includes(key)) return '';
|
||||
|
||||
if (key === ' ') key = 'Space';
|
||||
if (key.length === 1) key = key.toUpperCase();
|
||||
|
||||
if (key === '"') key = "'";
|
||||
if (key === ':') key = ';';
|
||||
if (key === '?') key = '/';
|
||||
if (key === '{') key = '[';
|
||||
if (key === '}') key = ']';
|
||||
|
||||
parts.push(key);
|
||||
return parts.join('+');
|
||||
}
|
||||
|
||||
function formatChordPart(part: string): string {
|
||||
if (IS_MAC) {
|
||||
switch (part) {
|
||||
case 'Cmd':
|
||||
return '⌘';
|
||||
case 'Ctrl':
|
||||
return '⌃';
|
||||
case 'Alt':
|
||||
return '⌥';
|
||||
case 'Shift':
|
||||
return '⇧';
|
||||
case 'Backspace':
|
||||
return '⌫';
|
||||
case 'Escape':
|
||||
return 'Esc';
|
||||
case 'Enter':
|
||||
return '↩\uFE0E';
|
||||
case 'Tab':
|
||||
return '⇥';
|
||||
}
|
||||
}
|
||||
|
||||
if (part === 'Cmd') return 'Ctrl';
|
||||
if (part === 'Escape') return 'Esc';
|
||||
return part;
|
||||
}
|
||||
|
||||
export function formatChord(chord: string): string {
|
||||
if (!chord) return '';
|
||||
const parts = chord.split('+').map(formatChordPart);
|
||||
return IS_MAC ? parts.join('') : parts.join('+');
|
||||
}
|
||||
|
||||
function buildReverseLookup(bindings: KeybindingsMap): Map<string, ConfigurableShortcut> {
|
||||
const lookup = new Map<string, ConfigurableShortcut>();
|
||||
for (const id of CONFIGURABLE_SHORTCUTS) {
|
||||
const chord = bindings[id];
|
||||
if (chord) lookup.set(chord, id);
|
||||
}
|
||||
return lookup;
|
||||
}
|
||||
|
||||
export function matchKeybinding(event: KeyboardEvent): ConfigurableShortcut | null {
|
||||
const chord = eventToChord(event);
|
||||
if (!chord) return null;
|
||||
return buildReverseLookup(get(keybindings)).get(chord) ?? null;
|
||||
}
|
||||
|
||||
export const shortcuts: ShortcutRegistry = {
|
||||
//Chat
|
||||
[Shortcut.NEW_CHAT]: {
|
||||
name: 'New Chat',
|
||||
keys: ['mod', 'shift', 'O'],
|
||||
category: 'Chat'
|
||||
category: 'Chat',
|
||||
configurable: true
|
||||
},
|
||||
[Shortcut.NEW_TEMPORARY_CHAT]: {
|
||||
name: 'New Temporary Chat',
|
||||
keys: ['mod', 'shift', `'`],
|
||||
category: 'Chat'
|
||||
category: 'Chat',
|
||||
configurable: true
|
||||
},
|
||||
[Shortcut.DELETE_CHAT]: {
|
||||
name: 'Delete Chat',
|
||||
keys: ['mod', 'shift', 'Backspace', 'Delete'],
|
||||
category: 'Chat'
|
||||
keys: ['mod', 'shift', 'Backspace'],
|
||||
category: 'Chat',
|
||||
configurable: true
|
||||
},
|
||||
[Shortcut.OPEN_MODEL_SELECTOR]: {
|
||||
name: 'Open Model Selector',
|
||||
keys: ['mod', 'shift', 'M'],
|
||||
category: 'Chat'
|
||||
category: 'Chat',
|
||||
configurable: true
|
||||
},
|
||||
[Shortcut.TOGGLE_DICTATION]: {
|
||||
name: 'Toggle Dictation',
|
||||
keys: ['mod', 'shift', 'L'],
|
||||
category: 'Chat'
|
||||
category: 'Chat',
|
||||
configurable: true
|
||||
},
|
||||
|
||||
//Global
|
||||
[Shortcut.SEARCH]: {
|
||||
name: 'Search',
|
||||
keys: ['mod', 'K'],
|
||||
category: 'Global'
|
||||
category: 'Global',
|
||||
configurable: true
|
||||
},
|
||||
[Shortcut.OPEN_SETTINGS]: {
|
||||
name: 'Open Settings',
|
||||
keys: ['mod', '.'],
|
||||
category: 'Global'
|
||||
category: 'Global',
|
||||
configurable: true
|
||||
},
|
||||
[Shortcut.SHOW_SHORTCUTS]: {
|
||||
name: 'Show Shortcuts',
|
||||
keys: ['mod', '/'],
|
||||
category: 'Global'
|
||||
category: 'Global',
|
||||
configurable: true
|
||||
},
|
||||
[Shortcut.TOGGLE_SIDEBAR]: {
|
||||
name: 'Toggle Sidebar',
|
||||
keys: ['mod', 'shift', 'S'],
|
||||
category: 'Global'
|
||||
category: 'Global',
|
||||
configurable: true
|
||||
},
|
||||
[Shortcut.CLOSE_MODAL]: {
|
||||
name: 'Close Modal',
|
||||
keys: ['Escape'],
|
||||
category: 'Global'
|
||||
category: 'Global',
|
||||
configurable: true
|
||||
},
|
||||
|
||||
//Input
|
||||
[Shortcut.FOCUS_INPUT]: {
|
||||
name: 'Focus Chat Input',
|
||||
keys: ['shift', 'Escape'],
|
||||
category: 'Input'
|
||||
category: 'Input',
|
||||
configurable: true
|
||||
},
|
||||
[Shortcut.ACCEPT_AUTOCOMPLETE]: {
|
||||
name: 'Accept Autocomplete Generation\nJump to Prompt Variable',
|
||||
@@ -139,12 +297,14 @@ export const shortcuts: ShortcutRegistry = {
|
||||
name: 'Generate Message Pair',
|
||||
keys: ['mod', 'shift', 'Enter'],
|
||||
category: 'Message',
|
||||
configurable: true,
|
||||
tooltip: 'Only active when the chat input is in focus.'
|
||||
},
|
||||
[Shortcut.REGENERATE_RESPONSE]: {
|
||||
name: 'Regenerate Response',
|
||||
keys: ['mod', 'R'],
|
||||
category: 'Message'
|
||||
category: 'Message',
|
||||
configurable: true
|
||||
},
|
||||
[Shortcut.STOP_GENERATING]: {
|
||||
name: 'Stop Generating',
|
||||
@@ -161,12 +321,14 @@ export const shortcuts: ShortcutRegistry = {
|
||||
[Shortcut.COPY_LAST_RESPONSE]: {
|
||||
name: 'Copy Last Response',
|
||||
keys: ['mod', 'shift', 'C'],
|
||||
category: 'Message'
|
||||
category: 'Message',
|
||||
configurable: true
|
||||
},
|
||||
[Shortcut.COPY_LAST_CODE_BLOCK]: {
|
||||
name: 'Copy Last Code Block',
|
||||
keys: ['mod', 'shift', ';'],
|
||||
category: 'Message'
|
||||
category: 'Message',
|
||||
configurable: true
|
||||
},
|
||||
|
||||
//Voice
|
||||
|
||||
Reference in new issue
Block a user