Line data Source code
1 : /**
2 : * Consulted by `keybindHandler` before it dispatches the default bare-Escape
3 : * keybinding (normally `Comfy.Graph.ExitSubgraph`). A handler returns true to
4 : * mark the event handled, which suppresses that dispatch; it is responsible
5 : * for calling `event.preventDefault()` itself if it wants to.
6 : *
7 : * This lives in `platform/` so a workbench feature (e.g. the agent composer)
8 : * can make its own runtime state win over the default Escape keybinding
9 : * without `keybindingService` importing from `workbench/`, which the
10 : * layer-architecture lint rule forbids.
11 : */
12 : export type EscapeOverride = (event: KeyboardEvent) => boolean
13 :
14 2356 : const overrides: EscapeOverride[] = []
15 :
16 : /**
17 : * Registers a handler consulted on every bare Escape keydown that would
18 : * otherwise dispatch the default keybinding. Returns an unregister function.
19 : *
20 : * Handlers are consulted most-recently-registered first, and the first one
21 : * to return true wins. In practice there is at most one registrant at a
22 : * time, so this ordering rarely matters.
23 : */
24 1 : export function registerEscapeOverride(handler: EscapeOverride): () => void {
25 1 : overrides.push(handler)
26 1 : return () => {
27 0 : const index = overrides.indexOf(handler)
28 0 : if (index !== -1) overrides.splice(index, 1)
29 : }
30 : }
31 :
32 : /** True if a registered handler claimed the event. */
33 19 : export function consultEscapeOverride(event: KeyboardEvent): boolean {
34 19 : for (let i = overrides.length - 1; i >= 0; i--) {
35 0 : if (overrides[i](event)) return true
36 : }
37 19 : return false
38 : }
39 :
40 : /** Resets shared state between tests. */
41 : export function clearEscapeOverrides(): void {
42 : overrides.length = 0
43 : }
|