refactor: replace lunr with fuse

This commit is contained in:
liyasthomas
2021-09-08 09:21:22 +05:30
parent 84457ddc86
commit dbae90a193
10 changed files with 192 additions and 144 deletions

38
components/app/Fuse.vue Normal file
View File

@@ -0,0 +1,38 @@
<template>
<div key="outputHash">
<AppPowerSearchEntry
v-for="(shortcut, shortcutIndex) in searchResults"
:key="`shortcut-${shortcutIndex}`"
:ref="`item-${shortcutIndex}`"
:shortcut="shortcut.item"
@action="$emit('action', shortcut.item.action)"
/>
<div
v-if="searchResults.length === 0"
class="flex flex-col text-secondaryLight p-4 items-center justify-center"
>
<i class="opacity-75 pb-2 material-icons">manage_search</i>
<span class="text-center">
{{ $t("state.nothing_found") }} "{{ search }}"
</span>
</div>
</div>
</template>
<script setup lang="ts">
import { computed } from "@nuxtjs/composition-api"
import Fuse from "fuse.js"
const props = defineProps<{
input: Record<string, any>[]
search: string
}>()
const options = {
keys: ["keys", "label", "action", "tags"],
}
const fuse = new Fuse(props.input, options)
const searchResults = computed(() => fuse.search(props.search))
</script>

View File

@@ -83,7 +83,7 @@
<AppAnnouncement v-if="!isOnLine" />
<FirebaseLogin :show="showLogin" @hide-modal="showLogin = false" />
<AppSupport :show="showSupport" @hide-modal="showSupport = false" />
<AppSearch :show="showSearch" @hide-modal="showSearch = false" />
<AppPowerSearch :show="showSearch" @hide-modal="showSearch = false" />
</div>
</template>

View File

@@ -1,59 +0,0 @@
<template>
<div key="outputHash">
<AppSearchEntry
v-for="(shortcut, shortcutIndex) in searchResults"
:key="`shortcut-${shortcutIndex}`"
:ref="`item-${shortcutIndex}`"
:shortcut="shortcut"
@action="$emit('action', shortcut.action)"
/>
<div
v-if="searchResults.length === 0"
class="flex flex-col text-secondaryLight p-4 items-center justify-center"
>
<i class="opacity-75 pb-2 material-icons">manage_search</i>
<span class="text-center">
{{ $t("state.nothing_found") }} "{{ search }}"
</span>
</div>
</div>
</template>
<script setup lang="ts">
import { computed } from "@nuxtjs/composition-api"
import lunr from "lunr"
const props = defineProps<{
input: Record<string, any>[]
search: string
}>()
const transformedInput = computed(() =>
props.input.map((val, i) => {
return {
__id: i,
...val,
}
})
)
const firstInput = computed(() =>
transformedInput.value.length > 0 ? transformedInput.value[0] : {}
)
const idx = computed(() => {
return lunr(function () {
this.ref("__id")
Object.keys(firstInput.value).forEach((key) => this.field(key), this)
transformedInput.value.forEach((doc) => {
this.add(doc)
}, this)
})
})
const searchResults = computed(() =>
idx.value.search(`${props.search}*`).map((result) => props.input[+result.ref])
)
</script>

View File

@@ -8,7 +8,7 @@
type="text"
autocomplete="off"
name="command"
:placeholder="$t('app.type_a_command_search')"
:placeholder="$t('app.type_a_command_search').toString()"
class="
bg-transparent
border-b border-dividerLight
@@ -17,10 +17,9 @@
p-6
"
/>
<AppLunr
<AppFuse
v-if="search"
log
:input="lunr"
:input="fuse"
:search="search"
@action="runAction"
/>
@@ -39,7 +38,7 @@
<h5 class="my-2 text-secondaryLight py-2 px-6">
{{ $t(map.section) }}
</h5>
<AppSearchEntry
<AppPowerSearchEntry
v-for="(shortcut, shortcutIndex) in map.shortcuts"
:key="`map-${mapIndex}-shortcut-${shortcutIndex}`"
:shortcut="shortcut"
@@ -54,7 +53,7 @@
<script setup lang="ts">
import { ref } from "@nuxtjs/composition-api"
import { HoppAction, invokeAction } from "~/helpers/actions"
import { spotlight as mappings, lunr } from "~/helpers/shortcuts"
import { spotlight as mappings, fuse } from "~/helpers/shortcuts"
defineProps<{
show: boolean

View File

@@ -19,11 +19,12 @@
<ButtonSecondary svg="x" class="rounded" @click.native="close()" />
</div>
</div>
<!-- <div class="bg-primary border-b border-dividerLight">
<div class="bg-primary border-b border-dividerLight">
<div class="flex flex-col my-4 mx-6">
<input
v-model="filterText"
type="search" autocomplete="off"
type="search"
autocomplete="off"
class="
bg-primaryLight
border border-dividerLight
@@ -35,10 +36,43 @@
focus-visible:border-divider
"
:placeholder="$t('action.search')"
v-focus
/>
</div>
</div> -->
</div>
<div v-if="filterText">
<div
v-for="(map, mapIndex) in searchResults"
:key="`map-${mapIndex}`"
class="space-y-4 py-4 px-6"
>
<h1 class="font-semibold text-secondaryDark">
{{ $t(map.item.section) }}
</h1>
<AppShortcutsEntry
v-for="(shortcut, index) in map.item.shortcuts"
:key="`shortcut-${index}`"
:shortcut="shortcut"
/>
</div>
<div
v-if="searchResults.length === 0"
class="
flex flex-col
text-secondaryLight
p-4
items-center
justify-center
"
>
<i class="opacity-75 pb-2 material-icons">manage_search</i>
<span class="text-center">
{{ $t("state.nothing_found") }} "{{ filterText }}"
</span>
</div>
</div>
<div
v-else
class="
divide-y divide-dividerLight
flex flex-col flex-1
@@ -54,53 +88,44 @@
<h1 class="font-semibold text-secondaryDark">
{{ $t(map.section) }}
</h1>
<div
<AppShortcutsEntry
v-for="(shortcut, shortcutIndex) in map.shortcuts"
:key="`map-${mapIndex}-shortcut-${shortcutIndex}`"
class="flex items-center"
>
<span class="flex flex-1 mr-4">
{{ $t(shortcut.label) }}
</span>
<span
v-for="(key, keyIndex) in shortcut.keys"
:key="`map-${mapIndex}-shortcut-${shortcutIndex}-key-${keyIndex}`"
class="shortcut-key"
>
{{ key }}
</span>
</div>
:shortcut="shortcut"
/>
</div>
</div>
</template>
</AppSlideOver>
</template>
<script>
import { defineComponent } from "@nuxtjs/composition-api"
import shortcuts from "~/helpers/shortcuts"
<script setup lang="ts">
import { computed, ref } from "@nuxtjs/composition-api"
import Fuse from "fuse.js"
import mappings from "~/helpers/shortcuts"
export default defineComponent({
props: {
show: Boolean,
},
data() {
return {
// filterText: "",
mappings: shortcuts,
}
},
watch: {
$route() {
this.$emit("close")
},
},
methods: {
close() {
this.$emit("close")
},
},
})
defineProps<{
show: boolean
}>()
const options = {
keys: ["shortcuts.label"],
}
const fuse = new Fuse(mappings, options)
const filterText = ref("")
const searchResults = computed(() => fuse.search(filterText.value))
const emit = defineEmits<{
(e: "close"): void
}>()
const close = () => {
filterText.value = ""
emit("close")
}
</script>
<style lang="scss" scoped>

View File

@@ -0,0 +1,31 @@
<template>
<div class="flex items-center">
<span class="flex flex-1 mr-4">
{{ $t(shortcut.label) }}
</span>
<span
v-for="(key, index) in shortcut.keys"
:key="`key-${index}`"
class="shortcut-key"
>
{{ key }}
</span>
</div>
</template>
<script setup lang="ts">
defineProps<{
shortcut: Object
}>()
</script>
<style lang="scss" scoped>
.shortcut-key {
@apply bg-dividerLight;
@apply rounded;
@apply ml-2;
@apply py-1;
@apply px-2;
@apply inline-flex;
}
</style>