|
| 1 | +<template> |
| 2 | + <Dialog v-model="show" :options="{ size: '2xl' }"> |
| 3 | + <template #body> |
| 4 | + <div class="text-base"> |
| 5 | + <div class="flex items-center space-x-2 pl-4.5 border-b"> |
| 6 | + <Search class="size-4 text-ink-gray-4" /> |
| 7 | + <input |
| 8 | + ref="inputRef" |
| 9 | + type="text" |
| 10 | + placeholder="Search" |
| 11 | + class="w-full border-none bg-transparent py-3 !pl-2 pr-4.5 text-base text-ink-gray-7 placeholder-ink-gray-4 focus:ring-0" |
| 12 | + @input="onInput" |
| 13 | + v-model="query" |
| 14 | + autocomplete="off" |
| 15 | + /> |
| 16 | + </div> |
| 17 | + |
| 18 | + <div class="max-h-96 overflow-auto mb-2"> |
| 19 | + <div v-if="query.length" class="mt-5 space-y-5"> |
| 20 | + <CommandPaletteGroup |
| 21 | + :list="searchResults" |
| 22 | + @navigateTo="navigateTo" |
| 23 | + /> |
| 24 | + </div> |
| 25 | + |
| 26 | + <div v-else class="mt-5 space-y-5"> |
| 27 | + <CommandPaletteGroup |
| 28 | + :list="jumpToOptions" |
| 29 | + @navigateTo="navigateTo" |
| 30 | + /> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | + |
| 34 | + <div |
| 35 | + class="flex items-center space-x-5 w-full border-t py-2 text-sm text-ink-gray-7 px-4.5" |
| 36 | + > |
| 37 | + <div class="flex items-center space-x-2"> |
| 38 | + <MoveUp |
| 39 | + class="size-5 stroke-1.5 bg-surface-gray-2 p-1 rounded-sm" |
| 40 | + /> |
| 41 | + <MoveDown |
| 42 | + class="size-5 stroke-1.5 bg-surface-gray-2 p-1 rounded-sm" |
| 43 | + /> |
| 44 | + <span> |
| 45 | + {{ __('to navigate') }} |
| 46 | + </span> |
| 47 | + </div> |
| 48 | + <div class="flex items-center space-x-2"> |
| 49 | + <CornerDownLeft |
| 50 | + class="size-5 stroke-1.5 bg-surface-gray-2 p-1 rounded-sm" |
| 51 | + /> |
| 52 | + <span> |
| 53 | + {{ __('to select') }} |
| 54 | + </span> |
| 55 | + </div> |
| 56 | + <div class="flex items-center space-x-2"> |
| 57 | + <span class="bg-surface-gray-2 p-1 rounded-sm"> esc </span> |
| 58 | + <span> |
| 59 | + {{ __('to close') }} |
| 60 | + </span> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + </template> |
| 65 | + </Dialog> |
| 66 | +</template> |
| 67 | +<script setup lang="ts"> |
| 68 | +import { createResource, debounce, Dialog } from 'frappe-ui' |
| 69 | +import { nextTick, onMounted, ref, watch } from 'vue' |
| 70 | +import { useRouter } from 'vue-router' |
| 71 | +import { |
| 72 | + BookOpen, |
| 73 | + Briefcase, |
| 74 | + CornerDownLeft, |
| 75 | + FileSearch, |
| 76 | + MoveUp, |
| 77 | + MoveDown, |
| 78 | + Search, |
| 79 | + Users, |
| 80 | +} from 'lucide-vue-next' |
| 81 | +import CommandPaletteGroup from './CommandPaletteGroup.vue' |
| 82 | +
|
| 83 | +const show = defineModel<boolean>({ required: true, default: false }) |
| 84 | +const router = useRouter() |
| 85 | +const query = ref<string>('') |
| 86 | +const searchResults = ref<Array<any>>([]) |
| 87 | +
|
| 88 | +const search = createResource({ |
| 89 | + url: 'lms.command_palette.search_sqlite', |
| 90 | + makeParams: () => ({ |
| 91 | + query: query.value, |
| 92 | + }), |
| 93 | + onSuccess() { |
| 94 | + generateSearchResults() |
| 95 | + }, |
| 96 | +}) |
| 97 | +
|
| 98 | +const debouncedSearch = debounce(() => { |
| 99 | + if (query.value.length > 2) { |
| 100 | + search.reload() |
| 101 | + } |
| 102 | +}, 500) |
| 103 | +
|
| 104 | +const onInput = () => { |
| 105 | + debouncedSearch() |
| 106 | +} |
| 107 | +
|
| 108 | +const generateSearchResults = () => { |
| 109 | + search.data?.forEach((type: any) => { |
| 110 | + let result: { title: string; items: any[] } = { title: '', items: [] } |
| 111 | + result.title = type.title |
| 112 | + type.items.forEach((item: any) => { |
| 113 | + let paramName = item.doctype === 'LMS Course' ? 'courseName' : 'batchName' |
| 114 | + item.route = { |
| 115 | + name: item.doctype === 'LMS Course' ? 'CourseDetail' : 'BatchDetail', |
| 116 | + params: { |
| 117 | + [paramName]: item.name, |
| 118 | + }, |
| 119 | + } |
| 120 | + item.isActive = false |
| 121 | + }) |
| 122 | + result.items = type.items |
| 123 | + searchResults.value.push(result) |
| 124 | + }) |
| 125 | +} |
| 126 | +
|
| 127 | +const appendSearchPage = () => { |
| 128 | + let searchPage: { title: string; items: Array<any> } = { |
| 129 | + title: '', |
| 130 | + items: [], |
| 131 | + } |
| 132 | + searchPage.title = __('Jump to') |
| 133 | + searchPage.items = [ |
| 134 | + { |
| 135 | + title: __('Search for ') + `"${query.value}"`, |
| 136 | + route: { |
| 137 | + name: 'Search', |
| 138 | + query: { |
| 139 | + q: query.value, |
| 140 | + }, |
| 141 | + }, |
| 142 | + icon: FileSearch, |
| 143 | + isActive: true, |
| 144 | + }, |
| 145 | + ] |
| 146 | + searchResults.value = [searchPage] |
| 147 | +} |
| 148 | +
|
| 149 | +watch( |
| 150 | + query, |
| 151 | + () => { |
| 152 | + appendSearchPage() |
| 153 | + }, |
| 154 | + { immediate: true } |
| 155 | +) |
| 156 | +
|
| 157 | +watch(show, () => { |
| 158 | + if (!show.value) { |
| 159 | + query.value = '' |
| 160 | + searchResults.value = [] |
| 161 | + } |
| 162 | +}) |
| 163 | +
|
| 164 | +onMounted(() => { |
| 165 | + addKeyboardShortcuts() |
| 166 | +}) |
| 167 | +
|
| 168 | +const addKeyboardShortcuts = () => { |
| 169 | + window.addEventListener('keydown', (e: KeyboardEvent) => { |
| 170 | + if (e.key === 'ArrowUp' && show.value) { |
| 171 | + e.preventDefault() |
| 172 | + shortcutForArrowKey(-1) |
| 173 | + } else if (e.key === 'ArrowDown' && show.value) { |
| 174 | + shortcutForArrowKey(1) |
| 175 | + } else if (e.key === 'Enter' && show.value) { |
| 176 | + shortcutForEnter() |
| 177 | + } else if (e.key === 'Escape' && show.value) { |
| 178 | + show.value = false |
| 179 | + } |
| 180 | + }) |
| 181 | +} |
| 182 | +
|
| 183 | +const shortcutForArrowKey = (direction: number) => { |
| 184 | + let currentList = query.value.length |
| 185 | + ? searchResults.value |
| 186 | + : jumpToOptions.value |
| 187 | + let allItems = currentList.flatMap((result: any) => result.items) |
| 188 | + let indexOfActive = allItems.findIndex((option: any) => option.isActive) |
| 189 | + let newIndex = indexOfActive + direction |
| 190 | + if (newIndex < 0) newIndex = allItems.length - 1 |
| 191 | + if (newIndex >= allItems.length) newIndex = 0 |
| 192 | + allItems[indexOfActive].isActive = false |
| 193 | + allItems[newIndex].isActive = true |
| 194 | + nextTick(scrollActiveItemIntoView) |
| 195 | +} |
| 196 | +
|
| 197 | +const scrollActiveItemIntoView = () => { |
| 198 | + const activeItem = document.querySelector( |
| 199 | + '.hover\\:bg-surface-gray-2.bg-surface-gray-2' |
| 200 | + ) as HTMLElement |
| 201 | + if (activeItem) { |
| 202 | + activeItem.scrollIntoView({ block: 'nearest' }) |
| 203 | + } |
| 204 | +} |
| 205 | +
|
| 206 | +const shortcutForEnter = () => { |
| 207 | + let currentList = query.value.length |
| 208 | + ? searchResults.value |
| 209 | + : jumpToOptions.value |
| 210 | + let allItems = currentList.flatMap((result: any) => result.items) |
| 211 | + let activeOption = allItems.find((option) => option.isActive) |
| 212 | + if (activeOption) { |
| 213 | + navigateTo(activeOption.route) |
| 214 | + } |
| 215 | +} |
| 216 | +
|
| 217 | +const navigateTo = (route: { |
| 218 | + name: string |
| 219 | + params?: Record<string, any> |
| 220 | + query?: Record<string, any> |
| 221 | +}) => { |
| 222 | + show.value = false |
| 223 | + query.value = '' |
| 224 | + router.replace({ name: route.name, params: route.params, query: route.query }) |
| 225 | +} |
| 226 | +
|
| 227 | +const jumpToOptions = ref([ |
| 228 | + { |
| 229 | + title: __('Jump to'), |
| 230 | + items: [ |
| 231 | + { |
| 232 | + title: 'Advanced Search', |
| 233 | + icon: Search, |
| 234 | + route: { |
| 235 | + name: 'Search', |
| 236 | + }, |
| 237 | + isActive: true, |
| 238 | + }, |
| 239 | + { |
| 240 | + title: 'Courses', |
| 241 | + icon: BookOpen, |
| 242 | + route: { |
| 243 | + name: 'Courses', |
| 244 | + }, |
| 245 | + isActive: false, |
| 246 | + }, |
| 247 | + { |
| 248 | + title: 'Batches', |
| 249 | + icon: Users, |
| 250 | + route: { |
| 251 | + name: 'Batches', |
| 252 | + }, |
| 253 | + isActive: false, |
| 254 | + }, |
| 255 | + { |
| 256 | + title: 'Jobs', |
| 257 | + icon: Briefcase, |
| 258 | + route: { |
| 259 | + name: 'Jobs', |
| 260 | + }, |
| 261 | + isActive: false, |
| 262 | + }, |
| 263 | + ], |
| 264 | + }, |
| 265 | +]) |
| 266 | +</script> |
| 267 | +<style> |
| 268 | +mark { |
| 269 | + background-color: theme('colors.amber.100'); |
| 270 | + font-weight: 500; |
| 271 | +} |
| 272 | +</style> |
0 commit comments