api-client/packages/hoppscotch-app/components/app/PowerSearch.vue

123 lines
3.3 KiB
Vue
Raw Normal View History

2021-08-10 08:16:42 +00:00
<template>
2021-09-20 16:03:36 +00:00
<SmartModal
v-if="show"
max-width="sm:max-w-md"
full-width
@close="$emit('hide-modal')"
>
2021-08-10 08:16:42 +00:00
<template #body>
2022-01-01 14:02:34 +00:00
<div class="flex transition flex-col border-b border-dividerLight">
<input
id="command"
v-model="search"
v-focus
type="text"
autocomplete="off"
name="command"
:placeholder="`${t('app.type_a_command_search')}`"
2021-12-31 13:09:31 +00:00
class="flex flex-shrink-0 p-6 text-base bg-transparent text-secondaryDark"
/>
<div
class="flex flex-shrink-0 text-tiny text-secondaryLight px-4 pb-4 justify-between whitespace-nowrap overflow-auto hide-scrollbar <sm:hidden"
>
<div class="flex items-center">
<kbd class="shortcut-key"></kbd>
<kbd class="shortcut-key"></kbd>
<span class="ml-2 truncate">
{{ t("action.to_navigate") }}
</span>
<kbd class="shortcut-key"></kbd>
<span class="ml-2 truncate">
{{ t("action.to_select") }}
</span>
</div>
<div class="flex items-center">
<kbd class="shortcut-key">ESC</kbd>
<span class="ml-2 truncate">
{{ t("action.to_close") }}
</span>
</div>
2021-12-14 17:11:07 +00:00
</div>
</div>
2021-09-08 03:51:22 +00:00
<AppFuse
v-if="search && show"
2021-09-08 03:51:22 +00:00
:input="fuse"
2021-08-28 17:53:16 +00:00
:search="search"
@action="runAction"
/>
2021-08-10 08:16:42 +00:00
<div
2021-08-28 17:53:16 +00:00
v-else
2021-12-31 13:09:31 +00:00
class="flex flex-col flex-1 space-y-4 overflow-auto divide-y divide-dividerLight hide-scrollbar"
2021-08-10 08:16:42 +00:00
>
<div
v-for="(map, mapIndex) in mappings"
:key="`map-${mapIndex}`"
class="flex flex-col"
>
2021-12-31 13:09:31 +00:00
<h5 class="px-6 py-2 my-2 text-secondaryLight">
{{ t(map.section) }}
2021-08-10 08:16:42 +00:00
</h5>
2021-09-08 03:51:22 +00:00
<AppPowerSearchEntry
2021-08-10 08:16:42 +00:00
v-for="(shortcut, shortcutIndex) in map.shortcuts"
:key="`map-${mapIndex}-shortcut-${shortcutIndex}`"
2021-08-29 06:30:19 +00:00
:shortcut="shortcut"
:active="shortcutsItems.indexOf(shortcut) === selectedEntry"
2021-08-29 06:30:19 +00:00
@action="runAction"
@mouseover.native="selectedEntry = shortcutsItems.indexOf(shortcut)"
2021-08-29 06:30:19 +00:00
/>
2021-08-10 08:16:42 +00:00
</div>
</div>
</template>
</SmartModal>
</template>
<script setup lang="ts">
import { ref, computed, watch } from "@nuxtjs/composition-api"
2021-08-29 06:30:19 +00:00
import { HoppAction, invokeAction } from "~/helpers/actions"
2021-09-08 03:51:22 +00:00
import { spotlight as mappings, fuse } from "~/helpers/shortcuts"
import { useArrowKeysNavigation } from "~/helpers/powerSearchNavigation"
import { useI18n } from "~/helpers/utils/composables"
const t = useI18n()
2021-08-10 08:16:42 +00:00
const props = defineProps<{
show: boolean
}>()
const emit = defineEmits<{
(e: "hide-modal"): void
}>()
const search = ref("")
const hideModal = () => {
search.value = ""
emit("hide-modal")
}
const runAction = (command: HoppAction) => {
invokeAction(command)
hideModal()
}
const shortcutsItems = computed(() =>
mappings.reduce(
(shortcuts, section) => [...shortcuts, ...section.shortcuts],
[]
)
)
const { bindArrowKeysListerners, unbindArrowKeysListerners, selectedEntry } =
useArrowKeysNavigation(shortcutsItems, {
onEnter: runAction,
})
watch(
() => props.show,
(show) => {
if (show) bindArrowKeysListerners()
else unbindArrowKeysListerners()
}
)
2021-08-10 08:16:42 +00:00
</script>