forked from elk-zone/elk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.ts
352 lines (287 loc) · 8.33 KB
/
command.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import type { LocaleObject } from '@nuxtjs/i18n'
import type { ComputedRef } from 'vue'
import type { SearchResult } from '~/composables/masto/search'
import Fuse from 'fuse.js'
import { defineStore } from 'pinia'
// @unocss-include
const scopes = [
'',
'Actions',
'Tabs',
'Navigation',
'Preferences',
'Account',
'Languages',
'Switch account',
'Settings',
'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
}
export type ResolvedCommand = Exclude<CommandProvider, 'icon' | 'name' | 'description' | 'bindings'> & {
icon: string
name: string
description: string | undefined
bindings: string[] | undefined
}
// TODO: define a type for command arg
export type CommandHandler<T = void> = (arg: T) => void
export interface BaseQueryResultItem {
index: number
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[]>
}
function resolveFunction<T>(i: T): T extends () => infer R ? R : T {
return 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: resolveFunction(provider.icon),
name: resolveFunction(provider.name),
description: resolveFunction(provider.description),
bindings: resolveFunction(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)
},
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
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,
type: 'command',
scope,
cmd,
onActivate: cmd.onActivate,
onComplete: cmd.onComplete,
})
}
let index = 0
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 }))
const grouped = new Map<CommandScopeNames, CommandQueryResultItem[]>(
scopes.map(scope => [scope, []]),
)
for (const cmd of indexed) {
const scope = cmd.scope ?? ''
grouped.get(scope)!.push({
index: cmd.index,
type: 'command',
scope,
cmd,
onActivate: cmd.onActivate,
onComplete: cmd.onComplete,
})
}
let index = 0
const sorted: CommandQueryResultItem[] = []
for (const [scope, items] of grouped) {
if (items.length === 0) {
grouped.delete(scope)
}
else {
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,
}
}
},
}
})
export function useCommand(cmd: CommandProvider) {
const registry = useCommandRegistry()
const register = () => registry.register(cmd)
const cleanup = () => registry.remove(cmd)
register()
onActivated(register)
onDeactivated(cleanup)
tryOnScopeDispose(cleanup)
}
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 })
const cleanup = () => {
commands.value.forEach(cmd => registry.remove(cmd))
}
onDeactivated(cleanup)
tryOnScopeDispose(cleanup)
}
export function provideGlobalCommands() {
const { locale, t } = useI18n()
const { locales } = useI18n() as { locales: ComputedRef<LocaleObject[]> }
const users = useUsers()
const masto = useMasto()
const colorMode = useColorMode()
const userSettings = useUserSettings()
const { singleInstanceServer, oauth } = useSignIn()
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: () => userSettings.value.preferences.zenMode ? 'i-ri:layout-right-2-line' : 'i-ri:layout-right-line',
onActivate() {
togglePreferences('zenMode')
},
})
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() {
if (singleInstanceServer)
oauth()
else
openSigninDialog()
},
})
useCommand({
scope: 'Account',
visible: () => users.value.length > 1,
name: () => t('action.switch_account'),
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() {
loginTo(masto, 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()
},
})
}