|
1 | 1 | <script lang="ts"> |
2 | 2 | import type { HighlightWords } from "highlight-words"; |
3 | 3 | import highlightWords from "highlight-words"; |
4 | | - import { onMount } from "svelte"; |
| 4 | + import { onMount, tick } from "svelte"; |
5 | 5 | import { |
6 | 6 | baseCommandsStore, |
7 | 7 | Close, |
8 | 8 | getBaseCommands, |
| 9 | + preselectedCommandStore, |
9 | 10 | tempCommandsStore, |
10 | 11 | type Command |
11 | 12 | } from "./command"; |
|
47 | 48 | showingTempCommands = true; |
48 | 49 | }); |
49 | 50 |
|
| 51 | + preselectedCommandStore.subscribe(async (cmd) => { |
| 52 | + const command = commands.find((c) => c.name.toLowerCase() === cmd?.toLowerCase()); |
| 53 | +
|
| 54 | + if (command) { |
| 55 | + selectedCommand = command; |
| 56 | + selectedCommand?.onHover?.(); |
| 57 | +
|
| 58 | + await tick(); |
| 59 | +
|
| 60 | + scrollIntoView(false); |
| 61 | + } |
| 62 | + }); |
| 63 | +
|
50 | 64 | onMount(() => { |
51 | 65 | cmdPalOpen.subscribe((val) => { |
52 | 66 | if (val) { |
|
127 | 141 | } |
128 | 142 | }; |
129 | 143 |
|
130 | | - const scrollIntoView = () => { |
| 144 | + const scrollIntoView = (smooth: boolean = true) => { |
131 | 145 | const index = filteredCommands.findIndex((cmd) => cmd === selectedCommand); |
132 | 146 |
|
133 | 147 | if (index === -1) return; |
134 | 148 |
|
135 | 149 | commandElements[index]?.scrollIntoView({ |
136 | | - behavior: "smooth", |
| 150 | + behavior: smooth ? "smooth" : "instant", |
137 | 151 | block: "nearest" |
138 | 152 | }); |
139 | 153 | }; |
|
165 | 179 |
|
166 | 180 | selectedCommand = filteredCommands[0]; |
167 | 181 | selectedCommand.onHover?.(); |
| 182 | +
|
| 183 | + scrollIntoView(false); |
168 | 184 | } else { |
169 | 185 | // deselect commands |
170 | 186 | isCommandSelected = false; |
|
207 | 223 | } |
208 | 224 | }; |
209 | 225 |
|
210 | | - const open = () => { |
| 226 | + const open = async () => { |
211 | 227 | if (isOpen) return; |
212 | 228 |
|
213 | 229 | // save the focused element |
|
217 | 233 |
|
218 | 234 | searchElement?.focus(); |
219 | 235 |
|
220 | | - selectedCommand = commands[0]; |
221 | | - selectedCommand.onHover?.(); |
| 236 | + const preselectedCommand = commands.find( |
| 237 | + (c) => c.name.toLowerCase() === $preselectedCommandStore?.toLowerCase() |
| 238 | + ); |
| 239 | +
|
| 240 | + if (preselectedCommand) { |
| 241 | + selectedCommand = preselectedCommand; |
| 242 | + } else { |
| 243 | + selectedCommand = commands[0]; |
| 244 | + } |
| 245 | +
|
| 246 | + selectedCommand?.onHover?.(); |
222 | 247 |
|
223 | 248 | cmdPalOpen.set(true); |
224 | 249 |
|
225 | | - scrollIntoView(); |
| 250 | + await tick(); |
| 251 | +
|
| 252 | + scrollIntoView(false); |
226 | 253 | }; |
227 | 254 |
|
228 | 255 | const close = () => { |
|
0 commit comments