feat: add scored searching
This commit is contained in:
1 parent
c0b93f65a9
commit
f63db02e98
5 files changed
+116
-33
No files matched your search
@@ -17,6 +17,7 @@
|
|||||||
color="tertiary"
|
color="tertiary"
|
||||||
{...$$restProps}
|
{...$$restProps}
|
||||||
on:click
|
on:click
|
||||||
|
on:contextmenu
|
||||||
class="rounded-full w-10 h-10 flex-shrink-0 {isSelected
|
class="rounded-full w-10 h-10 flex-shrink-0 {isSelected
|
||||||
? 'bg-slate-200 dark:bg-zinc-900 text-primary-900 dark:!text-primary-100'
|
? 'bg-slate-200 dark:bg-zinc-900 text-primary-900 dark:!text-primary-100'
|
||||||
: ''} {$$props.class ?? ''}"
|
: ''} {$$props.class ?? ''}"
|
||||||
|
|||||||
@@ -41,9 +41,10 @@
|
|||||||
import Commands from './commands/Commands.svelte'
|
import Commands from './commands/Commands.svelte'
|
||||||
|
|
||||||
let searching = false
|
let searching = false
|
||||||
|
let promptOpen: boolean = false
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Commands />
|
<Commands bind:open={promptOpen} />
|
||||||
<nav
|
<nav
|
||||||
class="flex flex-row gap-2 items-center w-full mx-auto z-50 box-border p-0.5
|
class="flex flex-row gap-2 items-center w-full mx-auto z-50 box-border p-0.5
|
||||||
duration-150
|
duration-150
|
||||||
@@ -51,7 +52,16 @@
|
|||||||
"
|
"
|
||||||
style={$$props.style}
|
style={$$props.style}
|
||||||
>
|
>
|
||||||
<NavButton href="/" label={$t('nav.home')} class="ml-2 logo">
|
<NavButton
|
||||||
|
on:contextmenu={(e) => {
|
||||||
|
e.preventDefault()
|
||||||
|
promptOpen = true
|
||||||
|
return true
|
||||||
|
}}
|
||||||
|
href="/"
|
||||||
|
label={$t('nav.home')}
|
||||||
|
class="ml-2 logo"
|
||||||
|
>
|
||||||
<svelte:fragment slot="icon">
|
<svelte:fragment slot="icon">
|
||||||
{#if LINKED_INSTANCE_URL}
|
{#if LINKED_INSTANCE_URL}
|
||||||
{#if $site}
|
{#if $site}
|
||||||
|
|||||||
@@ -12,6 +12,9 @@
|
|||||||
ArrowRightOnRectangle,
|
ArrowRightOnRectangle,
|
||||||
Bookmark,
|
Bookmark,
|
||||||
Cog6Tooth,
|
Cog6Tooth,
|
||||||
|
Fire,
|
||||||
|
GlobeAlt,
|
||||||
|
Home,
|
||||||
Icon,
|
Icon,
|
||||||
Identification,
|
Identification,
|
||||||
Inbox,
|
Inbox,
|
||||||
@@ -19,6 +22,7 @@
|
|||||||
Newspaper,
|
Newspaper,
|
||||||
PencilSquare,
|
PencilSquare,
|
||||||
QuestionMarkCircle,
|
QuestionMarkCircle,
|
||||||
|
ShieldCheck,
|
||||||
UserCircle,
|
UserCircle,
|
||||||
UserGroup,
|
UserGroup,
|
||||||
type IconSource,
|
type IconSource,
|
||||||
@@ -28,6 +32,8 @@
|
|||||||
import CommandItem from './CommandItem.svelte'
|
import CommandItem from './CommandItem.svelte'
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { afterNavigate, goto } from '$app/navigation'
|
import { afterNavigate, goto } from '$app/navigation'
|
||||||
|
import { profile } from '$lib/auth'
|
||||||
|
import { fullCommunityName } from '$lib/util'
|
||||||
|
|
||||||
export let open = false
|
export let open = false
|
||||||
$: if (open) search = ''
|
$: if (open) search = ''
|
||||||
@@ -57,6 +63,13 @@
|
|||||||
href: r.url,
|
href: r.url,
|
||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: $t('nav.commands.main'),
|
||||||
|
actions: [
|
||||||
|
{ href: '/', name: $t('nav.home'), icon: Home },
|
||||||
|
{ href: '/communities', name: $t('nav.communities'), icon: GlobeAlt },
|
||||||
|
],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: $t('profile.profile'),
|
name: $t('profile.profile'),
|
||||||
actions: [
|
actions: [
|
||||||
@@ -117,6 +130,15 @@
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: $t('profile.subscribed'),
|
||||||
|
actions:
|
||||||
|
$profile?.user?.follows.map((f) => ({
|
||||||
|
icon: f.community.icon,
|
||||||
|
name: f.community.title,
|
||||||
|
href: `/c/${fullCommunityName(f.community.name, f.community.actor_id)}`,
|
||||||
|
})) ?? [],
|
||||||
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
let search = ''
|
let search = ''
|
||||||
@@ -124,22 +146,61 @@
|
|||||||
const dispatch = createEventDispatcher()
|
const dispatch = createEventDispatcher()
|
||||||
let selectedIndex = 0
|
let selectedIndex = 0
|
||||||
|
|
||||||
|
function fuzzySearch(text: string, pattern: string): number {
|
||||||
|
const textLower = text.toLowerCase()
|
||||||
|
const patternLower = pattern.toLowerCase()
|
||||||
|
let score = 0
|
||||||
|
let lastIndex = -1
|
||||||
|
let consecutiveBonus = 0
|
||||||
|
|
||||||
|
for (let i = 0; i < patternLower.length; i++) {
|
||||||
|
const index = textLower.indexOf(patternLower[i], lastIndex + 1)
|
||||||
|
if (index === -1) return 0
|
||||||
|
|
||||||
|
score += 1
|
||||||
|
if (index === lastIndex + 1) {
|
||||||
|
consecutiveBonus++
|
||||||
|
score += consecutiveBonus
|
||||||
|
} else {
|
||||||
|
consecutiveBonus = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
lastIndex = index
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bonus for matching start of words
|
||||||
|
if (textLower.startsWith(patternLower)) {
|
||||||
|
score += 2
|
||||||
|
} else if (textLower.includes(' ' + patternLower)) {
|
||||||
|
score += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return score
|
||||||
|
}
|
||||||
|
|
||||||
$: filteredGroups = groups
|
$: filteredGroups = groups
|
||||||
.map((group) => ({
|
.map((group) => {
|
||||||
...group,
|
const scoredActions = group.actions
|
||||||
actions: group.actions.filter((action) =>
|
.map((action) => ({
|
||||||
action.name.toLowerCase().includes(search.toLowerCase()),
|
...action,
|
||||||
),
|
score: Math.max(fuzzySearch(action.name, search)),
|
||||||
}))
|
}))
|
||||||
|
.filter((action) => action.score > 0)
|
||||||
|
.sort((a, b) => b.score - a.score)
|
||||||
|
|
||||||
|
return {
|
||||||
|
...group,
|
||||||
|
actions: scoredActions,
|
||||||
|
score: Math.max(
|
||||||
|
fuzzySearch(group.name, search),
|
||||||
|
...scoredActions.map((a) => a.score),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
})
|
||||||
.filter((group) => group.actions.length > 0)
|
.filter((group) => group.actions.length > 0)
|
||||||
|
.sort((a, b) => b.score - a.score)
|
||||||
|
|
||||||
$: flattenedActions = groups.flatMap((group) =>
|
$: flattenedActions = filteredGroups.flatMap((group) => group.actions)
|
||||||
group.actions.map((action) => ({ ...action, group: group.name })),
|
|
||||||
)
|
|
||||||
|
|
||||||
$: filteredActions = flattenedActions.filter((action) =>
|
|
||||||
action.name.toLowerCase().includes(search.toLowerCase()),
|
|
||||||
)
|
|
||||||
|
|
||||||
function togglePalette() {
|
function togglePalette() {
|
||||||
open = !open
|
open = !open
|
||||||
@@ -160,23 +221,26 @@
|
|||||||
switch (event.key) {
|
switch (event.key) {
|
||||||
case 'ArrowDown':
|
case 'ArrowDown':
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
selectedIndex = (selectedIndex + 1) % filteredActions.length
|
selectedIndex = (selectedIndex + 1) % flattenedActions.length
|
||||||
focusItem(selectedIndex)
|
focusItem(selectedIndex)
|
||||||
break
|
break
|
||||||
case 'ArrowUp':
|
case 'ArrowUp':
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
selectedIndex =
|
selectedIndex =
|
||||||
(selectedIndex - 1 + filteredActions.length) % filteredActions.length
|
(selectedIndex - 1 + flattenedActions.length) %
|
||||||
|
flattenedActions.length
|
||||||
focusItem(selectedIndex)
|
focusItem(selectedIndex)
|
||||||
break
|
break
|
||||||
case 'Enter':
|
case 'Enter':
|
||||||
handleSelect(filteredActions[selectedIndex])
|
handleSelect(flattenedActions[selectedIndex])
|
||||||
break
|
break
|
||||||
case 'Escape':
|
case 'Escape':
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
togglePalette()
|
togglePalette()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (selectedIndex >= flattenedActions.length) selectedIndex = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
function focusItem(index: number) {
|
function focusItem(index: number) {
|
||||||
@@ -184,10 +248,15 @@
|
|||||||
|
|
||||||
const listItems = container?.querySelectorAll('li')
|
const listItems = container?.querySelectorAll('li')
|
||||||
listItems[index % listItems.length].focus()
|
listItems[index % listItems.length].focus()
|
||||||
|
listItems[index % listItems.length].scrollIntoView({
|
||||||
|
behavior: 'smooth',
|
||||||
|
block: 'center',
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleSelect(action: Action & { group: string }) {
|
async function handleSelect(action: Action) {
|
||||||
if (action.href) await goto(action.href)
|
if (!action) return
|
||||||
|
if (action.href) goto(action.href)
|
||||||
if (action.handle) action.handle()
|
if (action.handle) action.handle()
|
||||||
togglePalette()
|
togglePalette()
|
||||||
dispatch('select', action)
|
dispatch('select', action)
|
||||||
@@ -227,17 +296,19 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
<CommandItem
|
{#if search != ''}
|
||||||
action={{
|
<CommandItem
|
||||||
name: search,
|
action={{
|
||||||
href: `/search?q=${encodeURIComponent(search)}`,
|
name: search,
|
||||||
icon: MagnifyingGlass,
|
href: `/search?q=${encodeURIComponent(search)}`,
|
||||||
}}
|
icon: MagnifyingGlass,
|
||||||
>
|
}}
|
||||||
<span class="font-normal text-slate-600 dark:text-zinc-400">
|
>
|
||||||
{$t('nav.commands.search', { default: '' })}
|
<span class="font-normal text-slate-600 dark:text-zinc-400">
|
||||||
</span>
|
{$t('nav.commands.search', { default: '' })}
|
||||||
</CommandItem>
|
</span>
|
||||||
|
</CommandItem>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Modal>
|
</Modal>
|
||||||
@@ -37,7 +37,8 @@
|
|||||||
"prompt": "Command Prompt",
|
"prompt": "Command Prompt",
|
||||||
"recents": "Recents",
|
"recents": "Recents",
|
||||||
"content": "Content",
|
"content": "Content",
|
||||||
"search": "Search for {{default}}"
|
"search": "Search for {{default}}",
|
||||||
|
"main": "Main"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"account": {
|
"account": {
|
||||||
|
|||||||
@@ -66,7 +66,6 @@
|
|||||||
|
|
||||||
if (!$resumables.find((r) => r.url == $page.url.toString()))
|
if (!$resumables.find((r) => r.url == $page.url.toString()))
|
||||||
$resumables = [
|
$resumables = [
|
||||||
...$resumables,
|
|
||||||
{
|
{
|
||||||
id: Math.floor(Math.random() * 1000000),
|
id: Math.floor(Math.random() * 1000000),
|
||||||
name: post.post_view.post.name,
|
name: post.post_view.post.name,
|
||||||
@@ -74,6 +73,7 @@
|
|||||||
url: $page.url.toString(),
|
url: $page.url.toString(),
|
||||||
avatar: post.post_view.post.thumbnail_url,
|
avatar: post.post_view.post.thumbnail_url,
|
||||||
},
|
},
|
||||||
|
...$resumables,
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user