From 44ca89fe77d8b943518329896325c080b6c83132 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed <1438972+tskarhed@users.noreply.github.com> Date: Mon, 3 Jun 2024 12:49:13 +0200 Subject: [PATCH] KeyboardShortcuts: Support panel attention for scenes (#88476) * Support panel attention for scenes * Remove console.log * Bump scenes version for good measure * Mock appEvents * Remove unused import --- .../scene/keyboardShortcuts.ts | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) 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(); }; }