-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand-availability.ts
More file actions
63 lines (55 loc) · 1.49 KB
/
command-availability.ts
File metadata and controls
63 lines (55 loc) · 1.49 KB
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
import { createShortcuts, type RunnableInput, type ShortcutRuntime } from 'powerkeys'
export type EditorCommand = RunnableInput & {
id: string
title: string
run(): void
}
export type CommandState = {
modalOpen: boolean
canRename: boolean
readOnly: boolean
}
export function mountCommandAvailability(
target: HTMLElement,
state: CommandState,
commands: {
renameSymbol(): void
},
): {
shortcuts: ShortcutRuntime
getPaletteCommands(): EditorCommand[]
syncState(nextState: Partial<CommandState>): void
} {
const shortcuts = createShortcuts({
target,
getActiveScopes: () => (state.modalOpen ? ['modal', 'editor'] : ['editor']),
})
const paletteCommands: readonly EditorCommand[] = [
{
id: 'editor.renameSymbol',
title: 'Rename Symbol',
scope: 'editor',
when: 'editor.canRename && !editor.readOnly',
run: () => commands.renameSymbol(),
},
]
for (const command of paletteCommands) {
shortcuts.bind({
combo: 'F2',
scope: command.scope,
when: command.when,
handler: command.run,
})
}
const getPaletteCommands = (): EditorCommand[] =>
paletteCommands.filter((command) => shortcuts.isAvailable(command))
const syncState = (nextState: Partial<CommandState>): void => {
Object.assign(state, nextState)
shortcuts.batchContext({
'editor.canRename': state.canRename,
'editor.readOnly': state.readOnly,
})
}
syncState(state)
return { shortcuts, getPaletteCommands, syncState }
}