diff --git a/public/app/features/dashboard-scene/scene/keyboardShortcuts.ts b/public/app/features/dashboard-scene/scene/keyboardShortcuts.ts index 99123eaf9ca..dfa49e6625a 100644 --- a/public/app/features/dashboard-scene/scene/keyboardShortcuts.ts +++ b/public/app/features/dashboard-scene/scene/keyboardShortcuts.ts @@ -1,5 +1,7 @@ +import { SetPanelAttentionEvent } from '@grafana/data'; import { locationService } from '@grafana/runtime'; import { sceneGraph, VizPanel } from '@grafana/scenes'; +import appEvents from 'app/core/app_events'; import { KeybindingSet } from 'app/core/services/KeybindingSet'; import { ShareModal } from '../sharing/ShareModal'; @@ -12,6 +14,23 @@ import { onRemovePanel, toggleVizPanelLegend } from './PanelMenuBehavior'; export function setupKeyboardShortcuts(scene: DashboardScene) { const keybindings = new KeybindingSet(); + let vizPanelKey: string | null = null; + + const panelAttentionSubscription = appEvents.subscribe(SetPanelAttentionEvent, (event) => { + if (typeof event.payload.panelId === 'string') { + vizPanelKey = event.payload.panelId; + } + }); + + function withFocusedPanel(scene: DashboardScene, fn: (vizPanel: VizPanel) => void) { + return () => { + const vizPanel = sceneGraph.findObject(scene, (o) => o.state.key === vizPanelKey); + if (vizPanel && vizPanel instanceof VizPanel) { + fn(vizPanel); + return; + } + }; + } // View panel keybindings.addBinding({ @@ -140,26 +159,9 @@ export function setupKeyboardShortcuts(scene: DashboardScene) { // collapse all rows (TODO) // expand all rows (TODO) - return () => keybindings.removeAll; -} - -export function withFocusedPanel(scene: DashboardScene, fn: (vizPanel: VizPanel) => void) { return () => { - const elements = document.querySelectorAll(':hover'); - - for (let i = elements.length - 1; i > 0; i--) { - const element = elements[i]; - - if (element instanceof HTMLElement && element.dataset?.vizPanelKey) { - const panelKey = element.dataset?.vizPanelKey; - const vizPanel = sceneGraph.findObject(scene, (o) => o.state.key === panelKey); - - if (vizPanel && vizPanel instanceof VizPanel) { - fn(vizPanel); - return; - } - } - } + keybindings.removeAll(); + panelAttentionSubscription.unsubscribe(); }; }