feat(keybindings): add shortcut configuration UI (Phase 4)

- Create ShortcutInput.svelte with recording mode and conflict detection
- Create KeybindingsSettings.svelte modal for customizing shortcuts
- Integrate settings modal accessible from shortcuts modal
- Add i18n translations for configuration UI (en/es)
- Visual indicators for modified shortcuts
- Reset individual or all shortcuts to defaults
This commit is contained in:
vicrodh committed 2026-02-02 23:24:08 -06:00
1 parent fdab331c5c
commit d2913f0c0c
5 files changed
+672 -1

No files matched your search

@@ -0,0 +1,388 @@
<script lang="ts">
import { X, Settings, RotateCcw } from 'lucide-svelte';
import { t } from '$lib/i18n';
import ShortcutInput from './ShortcutInput.svelte';
import {
ACTIONS,
CATEGORIES,
DEFAULT_BINDINGS,
getActiveBindings,
setBinding,
resetBinding,
resetAllBindings,
type KeybindingCategory,
} from '$lib/stores/keybindingsStore';
interface Props {
isOpen: boolean;
onClose: () => void;
}
let { isOpen, onClose }: Props = $props();
let showResetConfirm = $state(false);
let bindingsVersion = $state(0); // Force reactivity
function handleShortcutChange(actionId: string, newShortcut: string) {
setBinding(actionId, newShortcut);
bindingsVersion++; // Trigger update
}
function handleReset(actionId: string) {
resetBinding(actionId);
bindingsVersion++;
}
function handleResetAll() {
showResetConfirm = true;
}
function confirmResetAll() {
resetAllBindings();
bindingsVersion++;
showResetConfirm = false;
}
function handleKeydown(e: KeyboardEvent) {
if (!isOpen) return;
// Only close with Escape if not recording a shortcut
if (e.key === 'Escape' && !document.querySelector('.shortcut-input.recording')) {
e.preventDefault();
e.stopPropagation();
onClose();
}
}
function handleBackdropClick(e: MouseEvent) {
if (e.target === e.currentTarget) {
onClose();
}
}
// Count modifications
function getModifiedCount(): number {
void bindingsVersion; // Depend on version for reactivity
const bindings = getActiveBindings();
return ACTIONS.filter(a => bindings[a.id] !== DEFAULT_BINDINGS[a.id]).length;
}
// Get category label with i18n
function getCategoryLabel(categoryId: KeybindingCategory): string {
const translated = $t(`keybindings.categories.${categoryId}`);
if (translated && !translated.startsWith('keybindings.categories.')) {
return translated;
}
return CATEGORIES.find(c => c.id === categoryId)?.label || categoryId;
}
</script>
<svelte:document onkeydown={handleKeydown} />
{#if isOpen}
<div class="modal-backdrop" onclick={handleBackdropClick} role="presentation">
<div
class="modal"
role="dialog"
aria-modal="true"
aria-labelledby="keybindings-settings-title"
onclick={(e) => e.stopPropagation()}
>
<header class="modal-header">
<div class="header-title">
<Settings size={20} />
<h2 id="keybindings-settings-title">{$t('keybindings.customize') || 'Customize Shortcuts'}</h2>
{#if getModifiedCount() > 0}
<span class="modified-badge">
{getModifiedCount()} {$t('keybindings.modified') || 'modified'}
</span>
{/if}
</div>
<button class="close-btn" onclick={onClose} title={$t('actions.close')}>
<X size={20} />
</button>
</header>
<div class="modal-content">
{#each CATEGORIES as category}
{@const actions = ACTIONS.filter(a => a.category === category.id)}
{#if actions.length > 0}
<section class="category-section">
<h3 class="category-title">{getCategoryLabel(category.id)}</h3>
{#each actions as action}
{@const bindings = getActiveBindings()}
<ShortcutInput
{action}
currentShortcut={bindings[action.id]}
defaultShortcut={DEFAULT_BINDINGS[action.id]}
onShortcutChange={(shortcut) => handleShortcutChange(action.id, shortcut)}
onReset={() => handleReset(action.id)}
/>
{/each}
</section>
{/if}
{/each}
</div>
<footer class="modal-footer">
<button
class="reset-all-btn"
onclick={handleResetAll}
disabled={getModifiedCount() === 0}
>
<RotateCcw size={14} />
{$t('keybindings.resetAll') || 'Reset All to Defaults'}
</button>
</footer>
</div>
</div>
<!-- Reset Confirmation Dialog -->
{#if showResetConfirm}
<div class="confirm-backdrop" role="presentation">
<div class="confirm-dialog" role="alertdialog" aria-modal="true">
<h3>{$t('keybindings.resetAllConfirmTitle') || 'Reset All Shortcuts?'}</h3>
<p>
{$t('keybindings.resetAllConfirmText', { values: { count: getModifiedCount() } }) ||
`This will reset all ${getModifiedCount()} modified shortcuts to their default values.`}
</p>
<div class="confirm-actions">
<button class="cancel-btn" onclick={() => showResetConfirm = false}>
{$t('actions.cancel')}
</button>
<button class="confirm-btn" onclick={confirmResetAll}>
{$t('keybindings.resetAll') || 'Reset All'}
</button>
</div>
</div>
</div>
{/if}
{/if}
<style>
.modal-backdrop {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.7);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
backdrop-filter: blur(4px);
animation: fadeIn 150ms ease-out;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.modal {
background: var(--bg-secondary);
border-radius: 12px;
width: 90%;
max-width: 550px;
max-height: 85vh;
display: flex;
flex-direction: column;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
border: 1px solid var(--border-subtle);
animation: slideUp 200ms ease-out;
}
@keyframes slideUp {
from {
opacity: 0;
transform: translateY(16px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.modal-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 16px 20px;
border-bottom: 1px solid var(--border-subtle);
}
.header-title {
display: flex;
align-items: center;
gap: 10px;
color: var(--text-primary);
}
.header-title h2 {
margin: 0;
font-size: 18px;
font-weight: 600;
}
.modified-badge {
font-size: 11px;
padding: 2px 8px;
background: var(--accent-primary);
color: white;
border-radius: 10px;
font-weight: 500;
}
.close-btn {
background: none;
border: none;
color: var(--text-muted);
cursor: pointer;
padding: 4px;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
transition: all 150ms ease;
}
.close-btn:hover {
color: var(--text-primary);
background: var(--bg-tertiary);
}
.modal-content {
flex: 1;
overflow-y: auto;
padding: 20px;
}
.category-section {
margin-bottom: 28px;
}
.category-section:last-child {
margin-bottom: 0;
}
.category-title {
font-size: 11px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.05em;
color: var(--text-muted);
margin: 0 0 16px 0;
padding-bottom: 8px;
border-bottom: 1px solid var(--border-subtle);
}
.modal-footer {
padding: 16px 20px;
border-top: 1px solid var(--border-subtle);
display: flex;
justify-content: center;
}
.reset-all-btn {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 20px;
background: transparent;
border: 1px solid var(--color-error, #ef4444);
border-radius: 8px;
color: var(--color-error, #ef4444);
font-size: 14px;
cursor: pointer;
transition: all 150ms ease;
}
.reset-all-btn:hover:not(:disabled) {
background: rgba(239, 68, 68, 0.1);
}
.reset-all-btn:disabled {
opacity: 0.3;
cursor: not-allowed;
}
/* Confirmation Dialog */
.confirm-backdrop {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 1001;
}
.confirm-dialog {
background: var(--bg-secondary);
border-radius: 12px;
padding: 24px;
max-width: 400px;
border: 1px solid var(--border-subtle);
}
.confirm-dialog h3 {
margin: 0 0 12px 0;
font-size: 16px;
color: var(--text-primary);
}
.confirm-dialog p {
margin: 0 0 20px 0;
font-size: 14px;
color: var(--text-secondary);
}
.confirm-actions {
display: flex;
gap: 12px;
justify-content: flex-end;
}
.cancel-btn {
padding: 8px 16px;
background: var(--bg-tertiary);
border: 1px solid var(--border-subtle);
border-radius: 6px;
color: var(--text-secondary);
cursor: pointer;
transition: all 150ms ease;
}
.cancel-btn:hover {
background: var(--bg-hover);
color: var(--text-primary);
}
.confirm-btn {
padding: 8px 16px;
background: var(--color-error, #ef4444);
border: none;
border-radius: 6px;
color: white;
cursor: pointer;
transition: all 150ms ease;
}
.confirm-btn:hover {
background: #dc2626;
}
/* Responsive */
@media (max-width: 600px) {
.modal {
width: 95%;
max-height: 90vh;
}
.modal-header {
padding: 14px 16px;
}
.modal-content {
padding: 16px;
}
}
</style>
+259
View File
@@ -0,0 +1,259 @@
<script lang="ts">
import { RotateCcw } from 'lucide-svelte';
import { t } from '$lib/i18n';
import {
eventToShortcut,
formatShortcutDisplay,
getConflictingAction,
type KeybindingAction,
} from '$lib/stores/keybindingsStore';
interface Props {
action: KeybindingAction;
currentShortcut: string;
defaultShortcut: string;
onShortcutChange: (newShortcut: string) => void;
onReset: () => void;
}
let {
action,
currentShortcut,
defaultShortcut,
onShortcutChange,
onReset,
}: Props = $props();
let isRecording = $state(false);
let recordedShortcut = $state('');
let conflict = $state<KeybindingAction | null>(null);
let inputElement: HTMLButtonElement | null = $state(null);
const isModified = $derived(currentShortcut !== defaultShortcut);
const displayShortcut = $derived(
isRecording
? (recordedShortcut || $t('keybindings.pressKeys') || 'Press keys...')
: formatShortcutDisplay(currentShortcut)
);
function startRecording() {
isRecording = true;
recordedShortcut = '';
conflict = null;
inputElement?.focus();
}
function stopRecording() {
isRecording = false;
recordedShortcut = '';
conflict = null;
}
function handleKeydown(e: KeyboardEvent) {
if (!isRecording) return;
e.preventDefault();
e.stopPropagation();
// Ignore modifier keys alone
if (['Control', 'Alt', 'Shift', 'Meta'].includes(e.key)) {
return;
}
const shortcut = eventToShortcut(e);
if (!shortcut) return;
recordedShortcut = formatShortcutDisplay(shortcut);
// Check for conflict
const conflicting = getConflictingAction(shortcut, action.id);
if (conflicting) {
conflict = conflicting;
} else {
conflict = null;
// Apply the change
onShortcutChange(shortcut);
stopRecording();
}
}
function handleBlur() {
// Small delay to allow button clicks
setTimeout(() => {
if (isRecording) {
stopRecording();
}
}, 150);
}
function handleReset() {
onReset();
conflict = null;
}
// Get i18n action label
function getActionLabel(): string {
const parts = action.id.split('.');
if (parts.length === 2) {
const key = parts[0] + parts[1].charAt(0).toUpperCase() + parts[1].slice(1);
const translated = $t(`keybindings.actions.${key}`);
if (translated && !translated.startsWith('keybindings.actions.')) {
return translated;
}
}
return action.label;
}
</script>
<div class="shortcut-input-wrapper">
<div class="action-info">
<span class="action-label">{getActionLabel()}</span>
</div>
<div class="input-row">
<button
bind:this={inputElement}
class="shortcut-input"
class:recording={isRecording}
class:conflict={conflict !== null}
class:modified={isModified && !isRecording}
onclick={startRecording}
onkeydown={handleKeydown}
onblur={handleBlur}
>
<span class="shortcut-text">{displayShortcut}</span>
{#if isModified && !isRecording}
<span class="modified-indicator" title={$t('keybindings.modified') || 'Modified'}>*</span>
{/if}
</button>
<button
class="reset-btn"
onclick={handleReset}
disabled={!isModified}
title={$t('keybindings.resetToDefault') || `Reset to default (${formatShortcutDisplay(defaultShortcut)})`}
>
<RotateCcw size={14} />
</button>
</div>
{#if conflict}
<div class="conflict-message">
{$t('keybindings.conflict', { values: { shortcut: recordedShortcut, action: conflict.label } }) ||
`"${recordedShortcut}" is already used by "${conflict.label}"`}
</div>
{/if}
</div>
<style>
.shortcut-input-wrapper {
margin-bottom: 16px;
}
.action-info {
margin-bottom: 6px;
}
.action-label {
font-size: 14px;
font-weight: 500;
color: var(--text-primary);
}
.input-row {
display: flex;
gap: 8px;
align-items: center;
}
.shortcut-input {
flex: 1;
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 14px;
background: var(--bg-tertiary);
border: 2px solid transparent;
border-radius: 8px;
font-family: var(--font-mono, monospace);
font-size: 13px;
color: var(--text-primary);
cursor: pointer;
transition: all 150ms ease;
min-height: 42px;
}
.shortcut-input:hover {
background: var(--bg-hover);
border-color: var(--border-subtle);
}
.shortcut-input:focus {
outline: none;
border-color: var(--accent-primary);
}
.shortcut-input.recording {
border-color: var(--accent-primary);
background: var(--bg-hover);
animation: pulse 1.5s ease-in-out infinite;
}
.shortcut-input.conflict {
border-color: var(--color-error, #ef4444);
}
.shortcut-input.modified {
border-color: var(--accent-primary);
border-style: dashed;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.7; }
}
.shortcut-text {
color: inherit;
}
.modified-indicator {
color: var(--accent-primary);
font-size: 14px;
font-weight: bold;
}
.reset-btn {
width: 36px;
height: 36px;
display: flex;
align-items: center;
justify-content: center;
background: var(--bg-tertiary);
border: 1px solid var(--border-subtle);
border-radius: 6px;
color: var(--text-muted);
cursor: pointer;
transition: all 150ms ease;
}
.reset-btn:hover:not(:disabled) {
background: var(--bg-hover);
color: var(--text-primary);
}
.reset-btn:disabled {
opacity: 0.3;
cursor: not-allowed;
}
.conflict-message {
margin-top: 6px;
padding: 8px 12px;
background: rgba(239, 68, 68, 0.1);
border: 1px solid rgba(239, 68, 68, 0.3);
border-radius: 6px;
font-size: 12px;
color: var(--color-error, #ef4444);
}
</style>