From 01a5c572dcbc6c68f1c93be1bf2582e7fdbcdb0a Mon Sep 17 00:00:00 2001 From: Scott Chacon Date: Mon, 13 Mar 2023 13:44:56 +0100 Subject: [PATCH] more general palette approach --- src/lib/components/CommandPalette.svelte | 131 ++++++++--------------- 1 file changed, 46 insertions(+), 85 deletions(-) diff --git a/src/lib/components/CommandPalette.svelte b/src/lib/components/CommandPalette.svelte index 502ed83f9..a479ca6fa 100644 --- a/src/lib/components/CommandPalette.svelte +++ b/src/lib/components/CommandPalette.svelte @@ -12,16 +12,9 @@ import { currentProject } from '$lib/current_project'; import type { Project } from '$lib/projects'; - let showCommand = false; - let showCommit = false; - - let is_command_down = false; - let is_k_down = false; - let is_c_down = false; - let is_e_down = false; - + let showPalette = false; + let keysDown = []; let palette: HTMLElement; - let commitPalette: HTMLElement; let changedFiles = {}; let commitMessage = ''; @@ -44,86 +37,58 @@ function onKeyDown(event: KeyboardEvent) { if (event.repeat) return; + keysDown.push(event.key); switch (event.key) { case 'Meta': - is_command_down = true; event.preventDefault(); break; - case 'k': - is_k_down = true; - break; - case 'c': - is_c_down = true; - break; - case 'e': - is_e_down = true; - break; case 'Escape': - showCommand = false; - showCommit = false; + showPalette = false; break; case 'ArrowDown': - if (showCommand) { + if (showPalette == 'command') { event.preventDefault(); downMenu(); } break; case 'ArrowUp': - if (showCommand) { + if (showPalette == 'command') { event.preventDefault(); upMenu(); } break; case 'Enter': - if (showCommand) { + if (showPalette == 'command') { event.preventDefault(); selectItem(); } break; } - if (is_command_down && is_k_down) { - showCommand = true; - setTimeout(function () { - document.getElementById('command')?.focus(); - }, 100); - } - if (is_command_down && is_c_down) { - showCommit = true; - executeCommand('commit'); - } - if (is_command_down && is_e_down) { - executeCommand('contact'); + if (keysDown.includes('Meta')) { + if (keysDown.includes('k')) { + showPalette = 'command'; + setTimeout(function () { + document.getElementById('command')?.focus(); + }, 100); + } + if (keysDown.includes('c')) { + showPalette = 'commit'; + executeCommand('commit'); + } + if (keysDown.includes('e')) { + executeCommand('contact'); + } } } function onKeyUp(event: KeyboardEvent) { - switch (event.key) { - case 'Meta': - is_command_down = false; - event.preventDefault(); - break; - case 'k': - is_k_down = false; - event.preventDefault(); - break; - case 'c': - is_c_down = false; - event.preventDefault(); - break; - case 'e': - is_e_down = false; - event.preventDefault(); - break; - } + keysDown = keysDown.filter((key) => key !== event.key); } - function checkCommandModal(event: Event) { + function checkPaletteModal(event: Event) { const target = event.target as HTMLElement; - if (showCommand && !palette.contains(target)) { - showCommand = false; - } - if (showCommit && !commitPalette.contains(target)) { - showCommit = false; + if (showPalette !== false && !palette.contains(target)) { + showPalette = false; } } @@ -164,8 +129,7 @@ } function selectItem() { - showCommand = false; - showCommit = false; + showPalette = false; const menu = document.getElementById('commandMenu'); if (menu) { const active = menu.querySelector('li.active'); @@ -191,23 +155,22 @@ console.log('files', files); changedFiles = files; }); - showCommit = true; + showPalette = 'commit'; setTimeout(function () { commitMessageInput.focus(); }, 100); } break; case 'contact': - console.log('contact us'); goto('/contact'); break; case 'switch': - console.log('switch', command, context); goto('/projects/' + context); break; case 'bookmark': break; case 'branch': + showBranch = true; break; } } @@ -215,7 +178,7 @@ let search = ''; $: { - searchChanged(search, showCommand); + searchChanged(search, showPalette == 'command'); } let projectCommands = [ @@ -225,7 +188,7 @@ ]; let switchCommands = []; - $: if ($currentProject) { + $: { listProjects().then((projects) => { switchCommands = []; projects.forEach((p) => { @@ -303,16 +266,16 @@ }).then((result) => { console.log('commit result', result); commitMessage = ''; - showCommit = false; + showPalette = false; }); } } - +
- {#if showCommand || showCommit} + {#if showPalette}