1
0
elk/composables/command.ts

369 lines
8.4 KiB
TypeScript
Raw Normal View History

import type { ComputedRef } from 'vue'
import { defineStore } from 'pinia'
import Fuse from 'fuse.js'
import type { LocaleObject } from '#i18n'
2022-12-29 01:29:07 +09:00
import type { SearchResult } from '@/components/search/types'
2022-11-29 21:08:56 +09:00
// @unocss-include
const scopes = [
'',
'Actions',
'Tabs',
'Navigation',
'Preferences',
'Account',
'Languages',
'Switch account',
'Settings',
2022-12-29 01:29:07 +09:00
'Hashtags',
'Users',
] as const
export type CommandScopeNames = typeof scopes[number]
export interface CommandScope {
id: string
display: string
}
export interface CommandProvider {
parent?: string
scope?: CommandScopeNames
// smaller is higher priority
order?: number
visible?: () => unknown
icon: string | (() => string)
name: string | (() => string)
description?: string | (() => string | undefined)
bindings?: string[] | (() => string[])
onActivate?: () => void
onComplete?: () => CommandScope
}
2022-12-29 01:29:07 +09:00
export type ResolvedCommand = Exclude<CommandProvider, 'icon' | 'name' | 'description' | 'bindings'> & {
icon: string
name: string
description: string | undefined
bindings: string[] | undefined
}
2022-12-29 01:29:07 +09:00
export interface BaseQueryResultItem {
index: number
2022-12-29 01:29:07 +09:00
type: string
scope?: CommandScopeNames
onActivate?: () => void
onComplete?: () => CommandScope
}
export interface SearchQueryResultItem extends BaseQueryResultItem {
type: 'search'
search: SearchResult
}
export interface CommandQueryResultItem extends BaseQueryResultItem {
type: 'command'
cmd: ResolvedCommand
}
export type QueryResultItem = SearchQueryResultItem | CommandQueryResultItem
export interface QueryResult {
length: number
items: QueryResultItem[]
grouped: Map<CommandScopeNames, QueryResultItem[]>
}
const r = <T extends Object | undefined>(i: T | (() => T)): T =>
typeof i === 'function' ? i() : i
export const useCommandRegistry = defineStore('command', () => {
const providers = reactive(new Set<CommandProvider>())
const commands = computed<ResolvedCommand[]>(() =>
[...providers]
.filter(command => command.visible ? command.visible() : true)
.map(provider => ({
...provider,
icon: r(provider.icon),
name: r(provider.name),
description: r(provider.description),
bindings: r(provider.bindings),
})))
let lastScope = ''
let lastFuse: Fuse<ResolvedCommand> | undefined
watch(commands, () => {
lastFuse = undefined
})
return {
register: (provider: CommandProvider) => {
providers.add(provider)
},
remove: (provider: CommandProvider) => {
providers.delete(provider)
},
2022-12-29 01:29:07 +09:00
query: (scope: string, query: string): QueryResult => {
const cmds = commands.value
.filter(cmd => (cmd.parent ?? '') === scope)
if (query) {
const fuse = lastScope === scope && lastFuse
? lastFuse
: new Fuse(cmds, {
keys: ['scope', 'name', 'description'],
includeScore: true,
})
lastScope = scope
lastFuse = fuse
const res = fuse.search(query)
.map(({ item }) => ({ ...item }))
// group by scope
2022-12-29 01:29:07 +09:00
const grouped = new Map<CommandScopeNames, CommandQueryResultItem[]>()
for (const cmd of res) {
const scope = cmd.scope ?? ''
if (!grouped.has(scope))
grouped.set(scope, [])
grouped
.get(scope)!
.push({
index: 0,
2022-12-29 01:29:07 +09:00
type: 'command',
scope,
cmd,
onActivate: cmd.onActivate,
onComplete: cmd.onComplete,
})
}
let index = 0
2022-12-29 01:29:07 +09:00
const indexed: CommandQueryResultItem[] = []
for (const items of grouped.values()) {
for (const cmd of items) {
cmd.index = index++
indexed.push(cmd)
}
}
return {
length: res.length,
items: indexed,
grouped,
}
}
else {
const indexed = cmds.map((cmd, index) => ({ ...cmd, index }))
2022-12-29 01:29:07 +09:00
const grouped = new Map<CommandScopeNames, CommandQueryResultItem[]>(
scopes.map(scope => [scope, []]))
for (const cmd of indexed) {
const scope = cmd.scope ?? ''
2022-12-29 01:29:07 +09:00
grouped.get(scope)!.push({
index: cmd.index,
type: 'command',
scope,
cmd,
onActivate: cmd.onActivate,
onComplete: cmd.onComplete,
})
}
let index = 0
2022-12-29 01:29:07 +09:00
const sorted: CommandQueryResultItem[] = []
for (const [scope, items] of grouped) {
if (items.length === 0) {
grouped.delete(scope)
}
else {
2022-12-29 01:29:07 +09:00
const o = (item: CommandQueryResultItem) => (item.cmd.order ?? 0) * 100 + item.index
items.sort((a, b) => o(a) - o(b))
for (const cmd of items) {
cmd.index = index++
sorted.push(cmd)
}
}
}
return {
length: indexed.length,
items: sorted,
grouped,
}
}
},
}
})
2023-01-04 02:16:04 +09:00
export function useCommand(cmd: CommandProvider) {
const registry = useCommandRegistry()
const register = () => registry.register(cmd)
2022-12-01 22:52:40 +09:00
const cleanup = () => registry.remove(cmd)
register()
onActivated(register)
2022-12-01 22:52:40 +09:00
onDeactivated(cleanup)
tryOnScopeDispose(cleanup)
}
2023-01-04 02:16:04 +09:00
export function useCommands(cmds: () => CommandProvider[]) {
const registry = useCommandRegistry()
const commands = computed(cmds)
watch(commands, (n, o = []) => {
for (const cmd of o)
registry.remove(cmd)
for (const cmd of n)
registry.register(cmd)
}, { deep: true, immediate: true })
2022-12-01 22:52:40 +09:00
const cleanup = () => {
commands.value.forEach(cmd => registry.remove(cmd))
2022-12-01 22:52:40 +09:00
}
onDeactivated(cleanup)
tryOnScopeDispose(cleanup)
}
export const provideGlobalCommands = () => {
const { locale, t } = useI18n()
const { locales } = useI18n() as { locales: ComputedRef<LocaleObject[]> }
2022-12-30 04:58:00 +09:00
const router = useRouter()
const users = useUsers()
const masto = useMasto()
const colorMode = useColorMode()
useCommand({
scope: 'Actions',
visible: () => currentUser.value,
name: () => t('action.compose'),
icon: 'i-ri:quill-pen-line',
description: () => t('command.compose_desc'),
onActivate() {
openPublishDialog()
},
})
2022-12-30 04:58:00 +09:00
useCommand({
scope: 'Navigation',
name: () => t('nav.settings'),
2023-01-04 02:16:04 +09:00
icon: 'i-ri:settings-3-line',
2022-12-30 04:58:00 +09:00
onActivate() {
router.push('/settings')
},
})
useCommand({
scope: 'Preferences',
name: () => t('command.toggle_dark_mode'),
icon: () => colorMode.value === 'light' ? 'i-ri:sun-line' : 'i-ri:moon-line',
onActivate() {
colorMode.preference = colorMode.value === 'light' ? 'dark' : 'light'
},
})
useCommand({
scope: 'Preferences',
name: () => t('command.toggle_zen_mode'),
icon: () => isZenMode.value ? 'i-ri:layout-right-2-line' : 'i-ri:layout-right-line',
onActivate() {
toggleZenMode()
},
})
useCommand({
scope: 'Preferences',
name: () => t('command.select_lang'),
icon: 'i-ri:earth-line',
onComplete: () => ({
id: 'language',
display: 'Languages',
}),
})
useCommands(() => locales.value.map(l => ({
parent: 'language',
scope: 'Languages',
name: l.name!,
icon: 'i-ri:earth-line',
onActivate() {
locale.value = l.code
},
})))
useCommand({
scope: 'Account',
name: () => t('action.sign_in'),
description: () => t('command.sign_in_desc'),
icon: 'i-ri:user-add-line',
onActivate() {
openSigninDialog()
},
})
useCommand({
scope: 'Account',
visible: () => users.value.length > 1,
name: () => t('action.switch_account'),
2022-12-10 06:18:21 +09:00
description: () => t('command.switch_account_desc'),
icon: 'i-ri:user-shared-line',
onComplete: () => ({
id: 'account-switch',
display: 'Accounts',
}),
})
useCommands(() => users.value.map(user => ({
parent: 'account-switch',
scope: 'Switch account',
visible: () => user.account.id !== currentUser.value?.account.id,
name: () => t('command.switch_account', [getFullHandle(user.account)]),
icon: 'i-ri:user-shared-line',
onActivate() {
masto.loginTo(user)
},
})))
useCommand({
scope: 'Account',
visible: () => currentUser.value,
name: () => t('user.sign_out_account', [getFullHandle(currentUser.value!.account)]),
icon: 'i-ri:logout-box-line',
onActivate() {
signout()
},
})
}