2022-11-29 08:15:05 +00:00
|
|
|
import type { ComputedRef } from 'vue'
|
2022-11-29 12:01:05 +00:00
|
|
|
import { defineStore } from 'pinia'
|
2022-11-29 08:15:05 +00:00
|
|
|
import Fuse from 'fuse.js'
|
2024-02-24 16:46:14 +00:00
|
|
|
import type { LocaleObject } from '@nuxtjs/i18n'
|
2023-01-07 14:52:58 +00:00
|
|
|
import type { SearchResult } from '~/composables/masto/search'
|
2022-11-29 08:15:05 +00:00
|
|
|
|
2022-11-29 12:08:56 +00:00
|
|
|
// @unocss-include
|
|
|
|
|
2022-11-29 08:15:05 +00:00
|
|
|
const scopes = [
|
|
|
|
'',
|
|
|
|
'Actions',
|
|
|
|
'Tabs',
|
|
|
|
'Navigation',
|
|
|
|
'Preferences',
|
|
|
|
'Account',
|
|
|
|
'Languages',
|
|
|
|
'Switch account',
|
2022-12-26 08:50:11 +00:00
|
|
|
'Settings',
|
2022-12-28 16:29:07 +00:00
|
|
|
'Hashtags',
|
|
|
|
'Users',
|
2022-11-29 08:15:05 +00:00
|
|
|
] 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-28 16:29:07 +00:00
|
|
|
export type ResolvedCommand = Exclude<CommandProvider, 'icon' | 'name' | 'description' | 'bindings'> & {
|
|
|
|
icon: string
|
|
|
|
name: string
|
|
|
|
description: string | undefined
|
|
|
|
bindings: string[] | undefined
|
|
|
|
}
|
2022-11-29 08:15:05 +00:00
|
|
|
|
2023-08-02 11:28:18 +01:00
|
|
|
// TODO: define a type for command arg
|
|
|
|
export type CommandHandler<T = void> = (arg: T) => void
|
|
|
|
|
2022-12-28 16:29:07 +00:00
|
|
|
export interface BaseQueryResultItem {
|
2022-11-29 08:15:05 +00:00
|
|
|
index: number
|
2022-12-28 16:29:07 +00: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[]>
|
2022-11-29 08:15:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 11:28:18 +01:00
|
|
|
function resolveFunction<T>(i: T): T extends () => infer R ? R : T {
|
2023-03-30 20:01:24 +01:00
|
|
|
return typeof i === 'function' ? i() : i
|
|
|
|
}
|
2022-11-29 08:15:05 +00:00
|
|
|
|
2022-11-29 12:01:05 +00:00
|
|
|
export const useCommandRegistry = defineStore('command', () => {
|
2022-11-29 08:15:05 +00:00
|
|
|
const providers = reactive(new Set<CommandProvider>())
|
|
|
|
|
|
|
|
const commands = computed<ResolvedCommand[]>(() =>
|
|
|
|
[...providers]
|
|
|
|
.filter(command => command.visible ? command.visible() : true)
|
|
|
|
.map(provider => ({
|
|
|
|
...provider,
|
2023-08-02 11:28:18 +01:00
|
|
|
icon: resolveFunction(provider.icon),
|
|
|
|
name: resolveFunction(provider.name),
|
|
|
|
description: resolveFunction(provider.description),
|
|
|
|
bindings: resolveFunction(provider.bindings),
|
2022-11-29 08:15:05 +00:00
|
|
|
})))
|
|
|
|
|
|
|
|
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-28 16:29:07 +00:00
|
|
|
query: (scope: string, query: string): QueryResult => {
|
2022-11-29 08:15:05 +00:00
|
|
|
const cmds = commands.value
|
|
|
|
.filter(cmd => (cmd.parent ?? '') === scope)
|
|
|
|
|
|
|
|
if (query) {
|
2023-03-19 12:12:20 +00:00
|
|
|
const fuse = (lastScope === scope && lastFuse)
|
2022-11-29 08:15:05 +00:00
|
|
|
? 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-28 16:29:07 +00:00
|
|
|
const grouped = new Map<CommandScopeNames, CommandQueryResultItem[]>()
|
2022-11-29 08:15:05 +00:00
|
|
|
for (const cmd of res) {
|
|
|
|
const scope = cmd.scope ?? ''
|
|
|
|
if (!grouped.has(scope))
|
|
|
|
grouped.set(scope, [])
|
|
|
|
grouped
|
|
|
|
.get(scope)!
|
|
|
|
.push({
|
|
|
|
index: 0,
|
2022-12-28 16:29:07 +00:00
|
|
|
type: 'command',
|
|
|
|
scope,
|
|
|
|
cmd,
|
|
|
|
onActivate: cmd.onActivate,
|
|
|
|
onComplete: cmd.onComplete,
|
2022-11-29 08:15:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = 0
|
2022-12-28 16:29:07 +00:00
|
|
|
const indexed: CommandQueryResultItem[] = []
|
2022-11-29 08:15:05 +00:00
|
|
|
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-28 16:29:07 +00:00
|
|
|
const grouped = new Map<CommandScopeNames, CommandQueryResultItem[]>(
|
2024-03-05 14:48:58 +00:00
|
|
|
scopes.map(scope => [scope, []]),
|
|
|
|
)
|
2022-11-29 08:15:05 +00:00
|
|
|
for (const cmd of indexed) {
|
|
|
|
const scope = cmd.scope ?? ''
|
2022-12-28 16:29:07 +00:00
|
|
|
grouped.get(scope)!.push({
|
|
|
|
index: cmd.index,
|
|
|
|
type: 'command',
|
|
|
|
scope,
|
|
|
|
cmd,
|
|
|
|
onActivate: cmd.onActivate,
|
|
|
|
onComplete: cmd.onComplete,
|
|
|
|
})
|
2022-11-29 08:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let index = 0
|
2022-12-28 16:29:07 +00:00
|
|
|
const sorted: CommandQueryResultItem[] = []
|
2022-11-29 08:15:05 +00:00
|
|
|
for (const [scope, items] of grouped) {
|
|
|
|
if (items.length === 0) {
|
|
|
|
grouped.delete(scope)
|
|
|
|
}
|
|
|
|
else {
|
2022-12-28 16:29:07 +00:00
|
|
|
const o = (item: CommandQueryResultItem) => (item.cmd.order ?? 0) * 100 + item.index
|
2022-11-29 08:15:05 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2022-11-29 12:01:05 +00:00
|
|
|
})
|
2022-11-29 08:15:05 +00:00
|
|
|
|
2023-01-03 17:16:04 +00:00
|
|
|
export function useCommand(cmd: CommandProvider) {
|
2022-11-29 08:15:05 +00:00
|
|
|
const registry = useCommandRegistry()
|
|
|
|
|
2022-12-29 19:57:39 +00:00
|
|
|
const register = () => registry.register(cmd)
|
2022-12-01 13:52:40 +00:00
|
|
|
const cleanup = () => registry.remove(cmd)
|
|
|
|
|
2022-12-29 19:57:39 +00:00
|
|
|
register()
|
|
|
|
onActivated(register)
|
2022-12-01 13:52:40 +00:00
|
|
|
onDeactivated(cleanup)
|
|
|
|
tryOnScopeDispose(cleanup)
|
2022-11-29 08:15:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 17:16:04 +00:00
|
|
|
export function useCommands(cmds: () => CommandProvider[]) {
|
2022-11-29 08:15:05 +00:00
|
|
|
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 13:52:40 +00:00
|
|
|
const cleanup = () => {
|
2022-11-29 08:15:05 +00:00
|
|
|
commands.value.forEach(cmd => registry.remove(cmd))
|
2022-12-01 13:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onDeactivated(cleanup)
|
|
|
|
tryOnScopeDispose(cleanup)
|
2022-11-29 08:15:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 20:01:24 +01:00
|
|
|
export function provideGlobalCommands() {
|
2022-12-05 05:48:23 +00:00
|
|
|
const { locale, t } = useI18n()
|
2022-11-29 08:15:05 +00:00
|
|
|
const { locales } = useI18n() as { locales: ComputedRef<LocaleObject[]> }
|
|
|
|
const users = useUsers()
|
2022-12-20 15:56:54 +00:00
|
|
|
const masto = useMasto()
|
2022-12-29 12:26:08 +00:00
|
|
|
const colorMode = useColorMode()
|
2023-01-12 17:52:52 +00:00
|
|
|
const userSettings = useUserSettings()
|
2023-02-05 12:10:19 +00:00
|
|
|
const { singleInstanceServer, oauth } = useSignIn()
|
2022-11-29 08:15:05 +00:00
|
|
|
|
|
|
|
useCommand({
|
|
|
|
scope: 'Preferences',
|
|
|
|
|
2022-12-05 05:48:23 +00:00
|
|
|
name: () => t('command.toggle_dark_mode'),
|
2022-12-28 01:06:54 +00:00
|
|
|
icon: () => colorMode.value === 'light' ? 'i-ri:sun-line' : 'i-ri:moon-line',
|
2022-11-29 08:15:05 +00:00
|
|
|
|
|
|
|
onActivate() {
|
2022-12-29 12:26:08 +00:00
|
|
|
colorMode.preference = colorMode.value === 'light' ? 'dark' : 'light'
|
2022-11-29 08:15:05 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
useCommand({
|
|
|
|
scope: 'Preferences',
|
|
|
|
|
2022-12-05 05:48:23 +00:00
|
|
|
name: () => t('command.toggle_zen_mode'),
|
2023-04-23 11:21:33 +01:00
|
|
|
icon: () => userSettings.value.preferences.zenMode ? 'i-ri:layout-right-2-line' : 'i-ri:layout-right-line',
|
2022-11-29 08:15:05 +00:00
|
|
|
|
|
|
|
onActivate() {
|
2023-04-23 11:21:33 +01:00
|
|
|
togglePreferences('zenMode')
|
2022-11-29 08:15:05 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
useCommand({
|
|
|
|
scope: 'Preferences',
|
|
|
|
|
2022-12-05 05:48:23 +00:00
|
|
|
name: () => t('command.select_lang'),
|
2022-11-29 08:15:05 +00:00
|
|
|
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',
|
|
|
|
|
2022-12-05 05:48:23 +00:00
|
|
|
name: () => t('action.sign_in'),
|
|
|
|
description: () => t('command.sign_in_desc'),
|
2022-11-29 08:15:05 +00:00
|
|
|
icon: 'i-ri:user-add-line',
|
|
|
|
|
|
|
|
onActivate() {
|
2023-02-05 12:10:19 +00:00
|
|
|
if (singleInstanceServer)
|
|
|
|
oauth()
|
|
|
|
else
|
|
|
|
openSigninDialog()
|
2022-11-29 08:15:05 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
useCommand({
|
|
|
|
scope: 'Account',
|
|
|
|
|
|
|
|
visible: () => users.value.length > 1,
|
|
|
|
|
2022-12-05 05:48:23 +00:00
|
|
|
name: () => t('action.switch_account'),
|
2022-12-09 21:18:21 +00:00
|
|
|
description: () => t('command.switch_account_desc'),
|
2022-11-29 08:15:05 +00:00
|
|
|
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,
|
|
|
|
|
2022-12-05 05:48:23 +00:00
|
|
|
name: () => t('command.switch_account', [getFullHandle(user.account)]),
|
2022-11-29 08:15:05 +00:00
|
|
|
icon: 'i-ri:user-shared-line',
|
|
|
|
|
|
|
|
onActivate() {
|
2023-01-15 08:38:02 +00:00
|
|
|
loginTo(masto, user)
|
2022-11-29 08:15:05 +00:00
|
|
|
},
|
|
|
|
})))
|
|
|
|
useCommand({
|
|
|
|
scope: 'Account',
|
|
|
|
|
|
|
|
visible: () => currentUser.value,
|
|
|
|
|
2022-12-05 05:48:23 +00:00
|
|
|
name: () => t('user.sign_out_account', [getFullHandle(currentUser.value!.account)]),
|
2022-11-29 08:15:05 +00:00
|
|
|
icon: 'i-ri:logout-box-line',
|
|
|
|
|
|
|
|
onActivate() {
|
2023-01-30 10:58:18 +00:00
|
|
|
signOut()
|
2022-11-29 08:15:05 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|