|
22 | 22 | let newTaskContent = $state('') |
23 | 23 | let addingTask = $state(false) |
24 | 24 | let togglingTasks = $state(new Set()) |
| 25 | + let addTaskComponent = $state() |
25 | 26 |
|
26 | 27 | // Derived project match |
27 | 28 | let parsedProject = $derived( |
|
83 | 84 | } |
84 | 85 | } |
85 | 86 |
|
| 87 | + function handleGlobalKeydown(event) { |
| 88 | + // Only auto-focus if: |
| 89 | + // 1. No input, textarea, or contenteditable element is currently focused |
| 90 | + // 2. The key is a printable character (not a modifier or special key) |
| 91 | + const activeElement = document.activeElement |
| 92 | + const isInputFocused = |
| 93 | + activeElement?.tagName === 'INPUT' || |
| 94 | + activeElement?.tagName === 'TEXTAREA' || |
| 95 | + (activeElement instanceof HTMLElement && activeElement.isContentEditable) |
| 96 | +
|
| 97 | + if ( |
| 98 | + addTaskComponent && |
| 99 | + !isInputFocused && |
| 100 | + event.key.length === 1 && // Printable character |
| 101 | + !event.ctrlKey && |
| 102 | + !event.metaKey && |
| 103 | + !event.altKey |
| 104 | + ) { |
| 105 | + addTaskComponent.focus() |
| 106 | + } |
| 107 | + } |
| 108 | +
|
86 | 109 | $effect(() => { |
87 | 110 | const backend = settings.taskBackend |
88 | 111 | const token = settings.todoistApiToken |
|
334 | 357 | onMount(() => { |
335 | 358 | initializeAPI(settings.taskBackend, settings.todoistApiToken) |
336 | 359 | document.addEventListener('visibilitychange', handleVisibilityChange) |
| 360 | + document.addEventListener('keydown', handleGlobalKeydown) |
337 | 361 | }) |
338 | 362 |
|
339 | 363 | onDestroy(() => { |
340 | 364 | document.removeEventListener('visibilitychange', handleVisibilityChange) |
| 365 | + document.removeEventListener('keydown', handleGlobalKeydown) |
341 | 366 | }) |
342 | 367 | </script> |
343 | 368 |
|
|
379 | 404 | </span> |
380 | 405 | {/if} |
381 | 406 | <AddTask |
| 407 | + bind:this={addTaskComponent} |
382 | 408 | bind:value={newTaskContent} |
383 | 409 | bind:parsed={parsedDate} |
384 | 410 | {parsedProject} |
|
0 commit comments