Skip to content

Commit d876fc4

Browse files
committed
feat: added preselected commands to the command palette
1 parent 81599a2 commit d876fc4

File tree

8 files changed

+101
-14
lines changed

8 files changed

+101
-14
lines changed

client/src/lib/CommandPalette.svelte

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<script lang="ts">
22
import type { HighlightWords } from "highlight-words";
33
import highlightWords from "highlight-words";
4-
import { onMount } from "svelte";
4+
import { onMount, tick } from "svelte";
55
import {
66
baseCommandsStore,
77
Close,
88
getBaseCommands,
9+
preselectedCommandStore,
910
tempCommandsStore,
1011
type Command
1112
} from "./command";
@@ -47,6 +48,19 @@
4748
showingTempCommands = true;
4849
});
4950
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+
5064
onMount(() => {
5165
cmdPalOpen.subscribe((val) => {
5266
if (val) {
@@ -127,13 +141,13 @@
127141
}
128142
};
129143
130-
const scrollIntoView = () => {
144+
const scrollIntoView = (smooth: boolean = true) => {
131145
const index = filteredCommands.findIndex((cmd) => cmd === selectedCommand);
132146
133147
if (index === -1) return;
134148
135149
commandElements[index]?.scrollIntoView({
136-
behavior: "smooth",
150+
behavior: smooth ? "smooth" : "instant",
137151
block: "nearest"
138152
});
139153
};
@@ -165,6 +179,8 @@
165179
166180
selectedCommand = filteredCommands[0];
167181
selectedCommand.onHover?.();
182+
183+
scrollIntoView(false);
168184
} else {
169185
// deselect commands
170186
isCommandSelected = false;
@@ -207,7 +223,7 @@
207223
}
208224
};
209225
210-
const open = () => {
226+
const open = async () => {
211227
if (isOpen) return;
212228
213229
// save the focused element
@@ -217,12 +233,23 @@
217233
218234
searchElement?.focus();
219235
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?.();
222247
223248
cmdPalOpen.set(true);
224249
225-
scrollIntoView();
250+
await tick();
251+
252+
scrollIntoView(false);
226253
};
227254
228255
const close = () => {

client/src/lib/Editor.svelte

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { Compartment, EditorState } from "@codemirror/state";
88
import { getLangs, getPopularLangNames, autodetectLanguage, type Language } from "./api/lang";
99
import { tooltip } from "$lib/tooltips";
10-
import { Close, setTempCommands, type Command } from "./command";
10+
import { Close, setPreselectedCommand, setTempCommands, type Command } from "./command";
1111
import { cmdPalOpen, cmdPalTitle, themeStore } from "./stores.svelte";
1212
import { languages as cmLangs } from "@codemirror/language-data";
1313
import { isLanguageMarkdown } from "./utils/markdown";
@@ -249,6 +249,8 @@
249249
cmdPalTitle.set("select indentation width (spaces)");
250250
setTempCommands(getIndentWidthCommands(prevUnit, convertIndent));
251251
252+
setPreselectedCommand(selectedIndentWidth.toString());
253+
252254
return Close.no;
253255
}
254256
},
@@ -261,6 +263,8 @@
261263
cmdPalTitle.set("select indentation width (tabs)");
262264
setTempCommands(getIndentWidthCommands(prevUnit, convertIndent));
263265
266+
setPreselectedCommand(selectedIndentWidth.toString());
267+
264268
return Close.no;
265269
}
266270
}
@@ -295,6 +299,7 @@
295299
296300
const onLanguageClick = async () => {
297301
setTempCommands(await getLanguageCommands());
302+
if (selectedLanguage) setPreselectedCommand(selectedLanguage.name);
298303
299304
cmdPalTitle.set("select language");
300305
cmdPalOpen.set(true);
@@ -303,6 +308,8 @@
303308
const onIndentClick = () => {
304309
setTempCommands(getIndentUnitCommands());
305310
311+
setPreselectedCommand(selectedIndentUnit);
312+
306313
cmdPalTitle.set("select indentation unit");
307314
cmdPalOpen.set(true);
308315
};

client/src/lib/TabbedEditor.svelte

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@
251251
252252
return editor.getIndentUnitCommands(convertIndent);
253253
};
254+
255+
export const getSelectedLang = () => {
256+
if (!editor) return undefined;
257+
258+
return editor.getSelectedLang();
259+
};
260+
261+
export const getSelectedIndentationUnit = () => {
262+
if (!editor) return undefined;
263+
264+
return editor.getIndentation()[0];
265+
};
254266
</script>
255267

256268
<svelte:window

client/src/lib/command.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ let baseCommands: Command[] = [];
2121

2222
export const baseCommandsStore: Writable<Command[]> = writable(baseCommands);
2323
export const tempCommandsStore: Writable<Command[]> = writable([]);
24+
export const preselectedCommandStore: Writable<string | undefined> = writable();
2425

2526
export const getBaseCommands = (): Command[] => {
2627
return baseCommands;
@@ -42,6 +43,10 @@ export const setTempCommands = (commands: Command[]) => {
4243
tempCommandsStore.set(commands);
4344
};
4445

46+
export const setPreselectedCommand = (command: string) => {
47+
preselectedCommandStore.set(command);
48+
};
49+
4550
export const getConfirmActionCommands = (confirmAction: CommandAction): Command[] => [
4651
{
4752
name: "yes",

client/src/routes/+layout.svelte

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
import Footer from "$lib/Footer.svelte";
44
import type { LayoutServerData } from "./$types";
55
import CommandPalette from "$lib/CommandPalette.svelte";
6-
import { Close, setBaseCommands, setTempCommands, type Command } from "$lib/command";
6+
import {
7+
Close,
8+
setBaseCommands,
9+
setPreselectedCommand,
10+
setTempCommands,
11+
type Command
12+
} from "$lib/command";
713
import { beforeNavigate, goto } from "$app/navigation";
814
import ThemeContext from "$lib/ThemeContext.svelte";
915
import { onMount, type Snippet } from "svelte";
@@ -98,6 +104,12 @@
98104
cmdPalTitle.set("select a theme");
99105
setTempCommands(themeCommands());
100106
107+
if (data.settings.followSystemTheme && $systemThemeStore === "dark") {
108+
setPreselectedCommand(data.settings.darkTheme);
109+
} else {
110+
setPreselectedCommand(data.settings.theme);
111+
}
112+
101113
return Close.no;
102114
}
103115
}

client/src/routes/+page.svelte

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
type PasteCreateInfo,
88
type PastyCreateInfo
99
} from "$lib/api/paste";
10-
import { addBaseCommands, Close, setTempCommands, type Command } from "$lib/command";
10+
import {
11+
addBaseCommands,
12+
Close,
13+
setPreselectedCommand,
14+
setTempCommands,
15+
type Command
16+
} from "$lib/command";
1117
import PasteOptions from "$lib/PasteOptions.svelte";
1218
import {
1319
cmdPalOpen,
@@ -54,6 +60,7 @@
5460
name: "set expires in",
5561
action: () => {
5662
setTempCommands(getExpiresInCommands());
63+
setPreselectedCommand(expiresInToLongString(selectedExpiresIn));
5764
return Close.no;
5865
}
5966
},
@@ -62,7 +69,11 @@
6269
action: () => {
6370
const cmds = editor.getLanguageCommands();
6471
(async () => {
65-
if (cmds) setTempCommands(await cmds);
72+
if (cmds) {
73+
setTempCommands(await cmds);
74+
const selectedLang = editor.getSelectedLang();
75+
if (selectedLang) setPreselectedCommand(selectedLang.name);
76+
}
6677
})();
6778
return Close.no;
6879
}
@@ -71,7 +82,11 @@
7182
name: "set editor indentation",
7283
action: () => {
7384
const cmds = editor.getIndentUnitCommands();
74-
if (cmds) setTempCommands(cmds);
85+
if (cmds) {
86+
setTempCommands(cmds);
87+
const selectedIndentationUnit = editor.getSelectedIndentationUnit();
88+
if (selectedIndentationUnit) setPreselectedCommand(selectedIndentationUnit);
89+
}
7590
return Close.no;
7691
}
7792
},
@@ -150,6 +165,7 @@
150165
151166
const openExpiresSelect = () => {
152167
setTempCommands(getExpiresInCommands());
168+
setPreselectedCommand(expiresInToLongString(selectedExpiresIn));
153169
154170
cmdPalTitle.set("select when the paste will expire");
155171
cmdPalOpen.set(true);

client/src/routes/settings/appearance/+page.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { updateSettings } from "$lib/api/settings";
33
import type { PageData } from "./$types";
44
import { cmdPalOpen, cmdPalTitle, systemThemeStore, themeStore } from "$lib/stores.svelte";
5-
import { Close, setTempCommands, type Command } from "$lib/command";
5+
import { Close, setPreselectedCommand, setTempCommands, type Command } from "$lib/command";
66
import { themes } from "$lib/themes";
77
import Checkbox from "$lib/Checkbox.svelte";
88
@@ -70,18 +70,21 @@
7070
7171
const onPasteViewClicked = () => {
7272
setTempCommands(getPasteViewCommands());
73+
setPreselectedCommand(settings.pasteView);
7374
cmdPalTitle.set("select default paste view");
7475
cmdPalOpen.set(true);
7576
};
7677
7778
const onThemeClicked = () => {
7879
setTempCommands(getThemeCommands(false));
80+
setPreselectedCommand(settings.theme);
7981
cmdPalTitle.set("select the theme");
8082
cmdPalOpen.set(true);
8183
};
8284
8385
const onDarkThemeClicked = () => {
8486
setTempCommands(getThemeCommands(true));
87+
setPreselectedCommand(settings.darkTheme);
8588
cmdPalTitle.set("select the theme");
8689
cmdPalOpen.set(true);
8790
};

client/src/routes/settings/behaviour/+page.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import Checkbox from "$lib/Checkbox.svelte";
33
import { getLangs, getPopularLangNames } from "$lib/api/lang";
44
import { updateSettings } from "$lib/api/settings";
5-
import { Close, setTempCommands, type Command } from "$lib/command";
5+
import { Close, setPreselectedCommand, setTempCommands, type Command } from "$lib/command";
66
import { cmdPalOpen, cmdPalTitle } from "$lib/stores.svelte";
77
import type { PageData } from "./$types";
88
@@ -23,6 +23,7 @@
2323
cmdPalTitle.set("select indentation width (spaces)");
2424
2525
setTempCommands(getIndentWidthCommands());
26+
setPreselectedCommand(settings.defaultIndentationWidth.toString());
2627
2728
updateSettings(fetch, settings);
2829
@@ -34,7 +35,9 @@
3435
action: () => {
3536
settings.defaultIndentationUnit = "tabs";
3637
cmdPalTitle.set("select indentation width (tabs)");
38+
3739
setTempCommands(getIndentWidthCommands());
40+
setPreselectedCommand(settings.defaultIndentationWidth.toString());
3841
3942
updateSettings(fetch, settings);
4043
@@ -111,13 +114,15 @@
111114
112115
const onDefaultLanguageClicked = async () => {
113116
setTempCommands(await getLanguageCommands());
117+
setPreselectedCommand(settings.defaultLanguage);
114118
115119
cmdPalTitle.set("select language");
116120
cmdPalOpen.set(true);
117121
};
118122
119123
const onDefaultIndentationClicked = () => {
120124
setTempCommands(getIndentUnitCommands());
125+
setPreselectedCommand(settings.defaultIndentationUnit);
121126
122127
cmdPalTitle.set("select indentation unit");
123128
cmdPalOpen.set(true);

0 commit comments

Comments
 (0)