Line data Source code
1 : import { isCloud } from '@/platform/distribution/types'
2 : import { useSettingStore } from '@/platform/settings/settingStore'
3 : import { useCommandStore } from '@/platform/keybindings/commandStore'
4 : import { useDialogStore } from '@/stores/dialogStore'
5 : import { isModalOpen } from '@/utils/modalUtil'
6 :
7 : import { CORE_KEYBINDINGS } from './defaults'
8 : import { consultEscapeOverride } from './escapeOverride'
9 : import { KeyComboImpl } from '@comfyorg/keybindings/keyCombo'
10 : import { KeybindingImpl } from '@comfyorg/keybindings/keybinding'
11 : import { useKeybindingStore } from '@comfyorg/keybindings/keybindingStore'
12 :
13 82427 : function isExcludedFromDistribution(keybinding: { commandId: string }) {
14 82427 : return (
15 : isCloud &&
16 : keybinding.commandId === 'Workspace.ToggleBottomPanelTab.logs-terminal'
17 : )
18 : }
19 :
20 8723 : export function useKeybindingService() {
21 8723 : const keybindingStore = useKeybindingStore()
22 8723 : const commandStore = useCommandStore()
23 8723 : const settingStore = useSettingStore()
24 8723 : const dialogStore = useDialogStore()
25 :
26 840 : function getExecutableKeybinding(keyCombo: KeyComboImpl) {
27 840 : const keybinding = keybindingStore.getKeybinding(keyCombo)
28 840 : return keybinding && commandStore.isRegistered(keybinding.commandId)
29 : ? keybinding
30 : : undefined
31 : }
32 :
33 1582 : function executeCanvasKeybinding(event: KeyboardEvent): boolean {
34 1582 : if (event.type !== 'keydown' || event.repeat) return false
35 1582 : if (isModalOpen(dialogStore.dialogStack.length)) return false
36 :
37 527 : const keybinding = getExecutableKeybinding(KeyComboImpl.fromEvent(event))
38 1582 : if (keybinding?.targetElementId !== 'graph-canvas-container') return false
39 :
40 118 : void commandStore.execute(keybinding.commandId)
41 118 : event.preventDefault()
42 118 : event.stopImmediatePropagation()
43 118 : return true
44 : }
45 :
46 1310 : async function keybindHandler(event: KeyboardEvent) {
47 1310 : const keyCombo = KeyComboImpl.fromEvent(event)
48 1310 : if (keyCombo.isModifier) {
49 524 : return
50 : }
51 :
52 786 : const target = event.composedPath()[0] as HTMLElement
53 1310 : if (
54 : event.key === 'Escape' &&
55 : target instanceof Element &&
56 : target.closest('[role="menu"], [role="menubar"], [role="dialog"]')
57 : ) {
58 84 : return
59 : }
60 :
61 1310 : if (
62 : keyCombo.isReservedByTextInput &&
63 : (target.tagName === 'TEXTAREA' ||
64 : target.tagName === 'INPUT' ||
65 : target.contentEditable === 'true' ||
66 : (target.tagName === 'SPAN' &&
67 : target.classList.contains('property_value')))
68 : ) {
69 389 : return
70 : }
71 :
72 313 : const keybinding = getExecutableKeybinding(keyCombo)
73 585 : if (keybinding) {
74 111 : const targetElementId =
75 : keybinding.targetElementId === 'graph-canvas'
76 : ? 'graph-canvas-container'
77 : : keybinding.targetElementId
78 111 : if (targetElementId) {
79 : const container = document.getElementById(targetElementId)
80 26 : if (!container?.contains(target)) {
81 1 : return
82 : }
83 : }
84 110 : if (isModalOpen(dialogStore.dialogStack.length)) {
85 3 : if (keyCombo.ctrl) {
86 3 : event.preventDefault()
87 : }
88 3 : return
89 : }
90 :
91 111 : if (event.key === 'Escape' && consultEscapeOverride(event)) {
92 0 : if (!event.defaultPrevented) event.preventDefault()
93 0 : return
94 : }
95 :
96 107 : event.preventDefault()
97 111 : if (
98 : event.repeat &&
99 : keybinding.targetElementId === 'graph-canvas-container'
100 : ) {
101 0 : return
102 : }
103 : const runCommandIds = new Set([
104 : 'Comfy.QueuePrompt',
105 : 'Comfy.QueuePromptFront',
106 : 'Comfy.QueueSelectedOutputNodes'
107 : ])
108 109 : if (runCommandIds.has(keybinding.commandId)) {
109 2 : await commandStore.execute(keybinding.commandId, {
110 : metadata: {
111 : trigger_source: 'keybinding'
112 : }
113 : })
114 : } else {
115 105 : await commandStore.execute(keybinding.commandId)
116 : }
117 98 : return
118 : }
119 :
120 1310 : if (event.ctrlKey || event.altKey || event.metaKey) {
121 117 : return
122 : }
123 :
124 138 : if (event.key === 'Escape') {
125 1 : const modals = document.querySelectorAll<HTMLElement>('.comfy-modal')
126 1 : for (const modal of modals) {
127 : const modalDisplay = window
128 : .getComputedStyle(modal)
129 : .getPropertyValue('display')
130 :
131 3 : if (modalDisplay !== 'none') {
132 0 : modal.style.display = 'none'
133 0 : break
134 : }
135 : }
136 :
137 1 : for (const d of document.querySelectorAll('dialog')) d.close()
138 : }
139 : }
140 :
141 2355 : function registerCoreKeybindings() {
142 2355 : for (const keybinding of CORE_KEYBINDINGS) {
143 82425 : if (isExcludedFromDistribution(keybinding)) continue
144 82425 : keybindingStore.addDefaultKeybinding(new KeybindingImpl(keybinding))
145 : }
146 : }
147 :
148 2301 : function registerUserKeybindings() {
149 2301 : const unsetBindings = settingStore.get('Comfy.Keybinding.UnsetBindings')
150 2301 : for (const keybinding of unsetBindings) {
151 1 : if (isExcludedFromDistribution(keybinding)) continue
152 1 : keybindingStore.addUserUnsetKeybinding(new KeybindingImpl(keybinding))
153 : }
154 2301 : const newBindings = settingStore.get('Comfy.Keybinding.NewBindings')
155 2301 : for (const keybinding of newBindings) {
156 1 : if (isExcludedFromDistribution(keybinding)) continue
157 1 : keybindingStore.addUserKeybinding(new KeybindingImpl(keybinding))
158 : }
159 : }
160 :
161 19 : async function persistUserKeybindings() {
162 19 : await settingStore.setMany({
163 : 'Comfy.Keybinding.NewBindings': keybindingStore.getUserKeybindingValues(),
164 : 'Comfy.Keybinding.UnsetBindings':
165 : keybindingStore.getUserUnsetKeybindingValues()
166 : })
167 : }
168 :
169 8723 : return {
170 : executeCanvasKeybinding,
171 : keybindHandler,
172 : registerCoreKeybindings,
173 : registerUserKeybindings,
174 : persistUserKeybindings
175 : }
176 : }
|