feat(command-palette): random tool action
This commit is contained in:
		
							parent
							
								
									63045951e1
								
							
						
					
					
						commit
						ec4c533718
					
				| @ -8,10 +8,12 @@ import { useStyleStore } from '@/stores/style.store'; | |||||||
| import SunIcon from '~icons/mdi/white-balance-sunny'; | import SunIcon from '~icons/mdi/white-balance-sunny'; | ||||||
| import GithubIcon from '~icons/mdi/github'; | import GithubIcon from '~icons/mdi/github'; | ||||||
| import BugIcon from '~icons/mdi/bug-outline'; | import BugIcon from '~icons/mdi/bug-outline'; | ||||||
|  | import DiceIcon from '~icons/mdi/dice-5'; | ||||||
| 
 | 
 | ||||||
| export const useCommandPaletteStore = defineStore('command-palette', () => { | export const useCommandPaletteStore = defineStore('command-palette', () => { | ||||||
|   const toolStore = useToolStore(); |   const toolStore = useToolStore(); | ||||||
|   const styleStore = useStyleStore(); |   const styleStore = useStyleStore(); | ||||||
|  |   const router = useRouter(); | ||||||
|   const searchPrompt = ref(''); |   const searchPrompt = ref(''); | ||||||
| 
 | 
 | ||||||
|   const toolsOptions = toolStore.tools.map(tool => ({ |   const toolsOptions = toolStore.tools.map(tool => ({ | ||||||
| @ -23,6 +25,18 @@ export const useCommandPaletteStore = defineStore('command-palette', () => { | |||||||
| 
 | 
 | ||||||
|   const searchOptions: PaletteOption[] = [ |   const searchOptions: PaletteOption[] = [ | ||||||
|     ...toolsOptions, |     ...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', |       name: 'Toggle dark mode', | ||||||
|       description: 'Toggle dark mode on or off.', |       description: 'Toggle dark mode on or off.', | ||||||
|  | |||||||
| @ -10,4 +10,5 @@ export interface PaletteOption { | |||||||
|   category: string |   category: string | ||||||
|   keywords?: string[] |   keywords?: string[] | ||||||
|   href?: string |   href?: string | ||||||
|  |   closeOnSelect?: boolean | ||||||
| } | } | ||||||
|  | |||||||
| @ -76,20 +76,36 @@ function getOptionIndex(option: PaletteOption) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function activateOption(option: PaletteOption) { | function activateOption(option: PaletteOption) { | ||||||
|  |   const { closeOnSelect } = option; | ||||||
|  | 
 | ||||||
|   if (option.action) { |   if (option.action) { | ||||||
|     option.action(); |     option.action(); | ||||||
|  | 
 | ||||||
|  |     if (closeOnSelect) { | ||||||
|  |       close(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   const closeAfterNavigation = closeOnSelect || _.isUndefined(closeOnSelect); | ||||||
|  | 
 | ||||||
|   if (option.to) { |   if (option.to) { | ||||||
|     router.push(option.to); |     router.push(option.to); | ||||||
|  | 
 | ||||||
|  |     if (closeAfterNavigation) { | ||||||
|       close(); |       close(); | ||||||
|     } |     } | ||||||
|  |     return; | ||||||
|  |   } | ||||||
| 
 | 
 | ||||||
|   if (option.href) { |   if (option.href) { | ||||||
|     window.open(option.href, '_blank'); |     window.open(option.href, '_blank'); | ||||||
|  | 
 | ||||||
|  |     if (closeAfterNavigation) { | ||||||
|       close(); |       close(); | ||||||
|     } |     } | ||||||
|  |   } | ||||||
| } | } | ||||||
| </script> | </script> | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user