2022-04-04 01:03:06 +02:00
|
|
|
<script lang="ts" setup>
|
2022-12-16 18:10:50 +01:00
|
|
|
import { useFuzzySearch } from '@/composable/fuzzySearch';
|
2022-07-23 18:49:10 +02:00
|
|
|
import { tools } from '@/tools';
|
2022-12-17 01:30:02 +01:00
|
|
|
import type { Tool } from '@/tools/tools.types';
|
2022-04-22 23:31:40 +02:00
|
|
|
import { SearchRound } from '@vicons/material';
|
2022-07-23 18:49:10 +02:00
|
|
|
import { useMagicKeys, whenever } from '@vueuse/core';
|
2022-12-16 21:44:54 +01:00
|
|
|
import { computed, h, ref } from 'vue';
|
2022-04-04 01:03:06 +02:00
|
|
|
import { useRouter } from 'vue-router';
|
2022-12-16 21:44:54 +01:00
|
|
|
import SearchBarItem from './SearchBarItem.vue';
|
2022-04-04 01:03:06 +02:00
|
|
|
|
2022-04-22 23:31:40 +02:00
|
|
|
const router = useRouter();
|
|
|
|
const queryString = ref('');
|
2022-04-04 01:03:06 +02:00
|
|
|
|
2022-12-16 18:10:50 +01:00
|
|
|
const { searchResult } = useFuzzySearch({
|
|
|
|
search: queryString,
|
|
|
|
data: tools,
|
|
|
|
options: { keys: [{ name: 'name', weight: 2 }, 'description', 'keywords'] },
|
2022-04-22 23:31:40 +02:00
|
|
|
});
|
2022-04-04 01:03:06 +02:00
|
|
|
|
2022-12-17 01:30:02 +01:00
|
|
|
const toolToOption = (tool: Tool) => ({ label: tool.name, value: tool.path, tool });
|
2022-12-16 21:44:54 +01:00
|
|
|
|
|
|
|
const options = computed(() => {
|
|
|
|
if (queryString.value === '') {
|
|
|
|
return tools.map(toolToOption);
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchResult.value.map(toolToOption);
|
|
|
|
});
|
2022-12-16 18:10:50 +01:00
|
|
|
|
2022-04-04 01:03:06 +02:00
|
|
|
function onSelect(path: string) {
|
2022-04-22 23:31:40 +02:00
|
|
|
router.push(path);
|
|
|
|
queryString.value = '';
|
2022-04-04 01:03:06 +02:00
|
|
|
}
|
2022-07-23 18:49:10 +02:00
|
|
|
|
|
|
|
const focusTarget = ref();
|
|
|
|
|
|
|
|
const keys = useMagicKeys({
|
|
|
|
passive: false,
|
|
|
|
onEventFired(e) {
|
|
|
|
if (e.ctrlKey && e.key === 'k' && e.type === 'keydown') {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
whenever(keys.ctrl_k, () => {
|
|
|
|
focusTarget.value.focus();
|
|
|
|
});
|
2022-12-16 21:44:54 +01:00
|
|
|
|
2022-12-17 01:30:02 +01:00
|
|
|
function renderOption({ tool }: { tool: Tool }) {
|
2022-12-16 21:44:54 +01:00
|
|
|
return h(SearchBarItem, { tool });
|
|
|
|
}
|
2022-04-04 01:03:06 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2022-04-15 23:10:47 +02:00
|
|
|
<div class="search-bar">
|
|
|
|
<n-auto-complete
|
|
|
|
v-model:value="queryString"
|
|
|
|
:options="options"
|
2022-12-16 18:10:50 +01:00
|
|
|
:on-select="(value) => onSelect(String(value))"
|
2022-12-16 21:44:54 +01:00
|
|
|
:render-label="renderOption"
|
|
|
|
:default-value="'aa'"
|
|
|
|
:get-show="() => true"
|
2022-12-21 00:03:31 +01:00
|
|
|
:on-focus="() => $tracker.trackEvent({ eventName: 'Search-bar focused' })"
|
2022-04-15 23:10:47 +02:00
|
|
|
>
|
|
|
|
<template #default="{ handleInput, handleBlur, handleFocus, value: slotValue }">
|
|
|
|
<n-input
|
2022-07-23 18:49:10 +02:00
|
|
|
ref="focusTarget"
|
2022-04-15 23:10:47 +02:00
|
|
|
round
|
|
|
|
clearable
|
2022-07-23 18:49:10 +02:00
|
|
|
placeholder="Search a tool... [Ctrl + K]"
|
2022-04-15 23:10:47 +02:00
|
|
|
:value="slotValue"
|
2022-12-16 21:44:54 +01:00
|
|
|
:input-props="{ autocomplete: 'disabled' }"
|
2022-04-15 23:10:47 +02:00
|
|
|
@input="handleInput"
|
|
|
|
@focus="handleFocus"
|
|
|
|
@blur="handleBlur"
|
|
|
|
>
|
|
|
|
<template #prefix>
|
|
|
|
<n-icon :component="SearchRound" />
|
|
|
|
</template>
|
|
|
|
</n-input>
|
|
|
|
</template>
|
|
|
|
</n-auto-complete>
|
|
|
|
</div>
|
2022-04-04 01:03:06 +02:00
|
|
|
</template>
|