Skip to content

Commit

Permalink
feat(command-palette): random tool action
Browse files Browse the repository at this point in the history
  • Loading branch information
CorentinTh committed Jun 19, 2023
1 parent 6304595 commit ec4c533
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/modules/command-palette/command-palette.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { useStyleStore } from '@/stores/style.store';
import SunIcon from '~icons/mdi/white-balance-sunny';
import GithubIcon from '~icons/mdi/github';
import BugIcon from '~icons/mdi/bug-outline';
import DiceIcon from '~icons/mdi/dice-5';

export const useCommandPaletteStore = defineStore('command-palette', () => {
const toolStore = useToolStore();
const styleStore = useStyleStore();
const router = useRouter();
const searchPrompt = ref('');

const toolsOptions = toolStore.tools.map(tool => ({
Expand All @@ -23,6 +25,18 @@ export const useCommandPaletteStore = defineStore('command-palette', () => {

const searchOptions: PaletteOption[] = [
...toolsOptions,
{
name: 'Random tool',
description: 'Get a random tool from the list.',
action: () => {
const { path } = _.sample(toolStore.tools)!;
router.push(path);
},
icon: DiceIcon,
category: 'Tools',
keywords: ['random', 'tool', 'pick', 'choose', 'select'],
closeOnSelect: true,
},
{
name: 'Toggle dark mode',
description: 'Toggle dark mode on or off.',
Expand Down
1 change: 1 addition & 0 deletions src/modules/command-palette/command-palette.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface PaletteOption {
category: string
keywords?: string[]
href?: string
closeOnSelect?: boolean
}
20 changes: 18 additions & 2 deletions src/modules/command-palette/command-palette.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,35 @@ function getOptionIndex(option: PaletteOption) {
}
function activateOption(option: PaletteOption) {
const { closeOnSelect } = option;
if (option.action) {
option.action();
if (closeOnSelect) {
close();
}
return;
}
const closeAfterNavigation = closeOnSelect || _.isUndefined(closeOnSelect);
if (option.to) {
router.push(option.to);
close();
if (closeAfterNavigation) {
close();
}
return;
}
if (option.href) {
window.open(option.href, '_blank');
close();
if (closeAfterNavigation) {
close();
}
}
}
</script>
Expand Down

0 comments on commit ec4c533

Please sign in to comment.