diff --git a/src/lib/components/CommandPalette/CommandPalette.svelte b/src/lib/components/CommandPalette/CommandPalette.svelte index a2b4ac194..94ed6d599 100644 --- a/src/lib/components/CommandPalette/CommandPalette.svelte +++ b/src/lib/components/CommandPalette/CommandPalette.svelte @@ -7,8 +7,10 @@ import { IconCircleCancel } from '$lib/components/icons'; import type { Project } from '$lib/projects'; import tinykeys from 'tinykeys'; - import type { CommandGroup } from './commands'; + import type { CommandGroup, Command } from './commands'; import { Action, previousCommand, nextCommand, firstVisibleCommand } from './commands'; + import type { ComponentType } from 'svelte'; + import { default as RewindCommand } from './RewindCommand.svelte'; $: scopeToProject = $currentProject ? true : false; @@ -26,6 +28,10 @@ ) { selection = firstVisibleCommand(commandGroups); } + $: selectedCommand = commandGroups[selection[0]].commands[selection[1]]; + + let componentOfTriggeredCommand: ComponentType | undefined; + let triggeredCommand: Command | undefined; $: commandGroups = [ { @@ -42,6 +48,21 @@ visible: project.title.toLowerCase().includes(userInput?.toLowerCase()) }; }) + }, + { + name: 'Commands', + visible: scopeToProject, + commands: [ + { + title: 'Replay', + description: 'Command', + selected: false, + action: { + component: RewindCommand + }, + visible: 'replay'.includes(userInput?.toLowerCase()) + } + ] } ] as CommandGroup[]; @@ -49,16 +70,24 @@ userInput = ''; scopeToProject = $currentProject ? true : false; selection = [0, 0]; + componentOfTriggeredCommand = undefined; + triggeredCommand = undefined; }; - const handleEnter = () => { - if (!commandGroups[0].visible || !commandGroups[0].commands[0].visible) { + const triggerCommand = () => { + if ( + !commandGroups[selection[0]].visible || + !commandGroups[selection[0]].commands[selection[1]].visible + ) { return; } - const command = commandGroups[selection[0]].commands[selection[1]]; - if (Action.isLink(command.action)) { + if (Action.isLink(selectedCommand.action)) { toggleCommandPalette(); - goto(command.action.href); + goto(selectedCommand.action.href); + } else if (Action.isActionInPalette(selectedCommand.action)) { + userInput = ''; + componentOfTriggeredCommand = selectedCommand.action.component; + triggeredCommand = selectedCommand; } }; @@ -76,18 +105,24 @@ let unsubscribeKeyboardHandler: () => void; onMount(() => { - // toggleCommandPalette(); // developmnet only + toggleCommandPalette(); // developmnet only unsubscribeKeyboardHandler = tinykeys(window, { 'Meta+k': () => { toggleCommandPalette(); }, Backspace: () => { if (!userInput) { - scopeToProject = false; + if (triggeredCommand) { + // Untrigger command + componentOfTriggeredCommand = undefined; + triggeredCommand = undefined; + } else { + scopeToProject = false; + } } }, Enter: () => { - handleEnter(); + triggerCommand(); }, ArrowDown: () => { selection = nextCommand(commandGroups, selection); @@ -129,6 +164,13 @@ / {/if} + + {#if scopeToProject && triggeredCommand} +