From 2f33e4200400bbd8daa5c598c8e91db960aca691 Mon Sep 17 00:00:00 2001 From: Bill Wallace Date: Tue, 28 Mar 2023 12:15:55 -0400 Subject: [PATCH] fix: Store hotkeys to specified name (#3280) * fix: Store hotkeys to specified name * fix: Move hotkey name into a new hotkey object --- modes/basic-test-mode/src/index.js | 8 ++++++-- platform/core/src/classes/HotkeysManager.ts | 10 ++++------ platform/docs/docs/platform/modes/index.md | 12 ++++++++++-- platform/viewer/src/routes/Mode/Mode.tsx | 18 ++++++++++++------ 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/modes/basic-test-mode/src/index.js b/modes/basic-test-mode/src/index.js index ca6dc25fd..11e06ec24 100644 --- a/modes/basic-test-mode/src/index.js +++ b/modes/basic-test-mode/src/index.js @@ -81,7 +81,6 @@ function modeFactory() { initToolGroups(extensionManager, toolGroupService, commandsManager); // init customizations - console.log('* Adding mode customizations'); customizationService.addModeCustomizations([ '@ohif/extension-test.customizationModule.custom-context-menu', ]); @@ -211,7 +210,12 @@ function modeFactory() { dicompdf.sopClassHandler, dicomsr.sopClassHandler, ], - hotkeys: [...hotkeys.defaults.hotkeyBindings], + hotkeys: { + // Don't store the hotkeys for basic-test-mode under the same key + // because they get customized by tests + name: 'basic-test-hotkeys', + hotkeys: [...hotkeys.defaults.hotkeyBindings], + }, }; } diff --git a/platform/core/src/classes/HotkeysManager.ts b/platform/core/src/classes/HotkeysManager.ts index ddb2757ec..50ccc0fe5 100644 --- a/platform/core/src/classes/HotkeysManager.ts +++ b/platform/core/src/classes/HotkeysManager.ts @@ -64,19 +64,17 @@ export class HotkeysManager { * * @param {HotkeyDefinition[] | Object} [hotkeyDefinitions=[]] Contains hotkeys definitions */ - setHotkeys(hotkeyDefinitions = [], key = 'hotkey-definitions') { + setHotkeys(hotkeyDefinitions = [], name = 'hotkey-definitions') { try { const definitions = this.getValidDefinitions(hotkeyDefinitions); if (isequal(definitions, this.hotkeyDefaults)) { - console.log('hotkeys REMOVING unused definition', key); - localStorage.removeItem(key); + localStorage.removeItem(name); } else { - console.log('hotkeys setting local storage', key); - localStorage.setItem(key, JSON.stringify(definitions)); + localStorage.setItem(name, JSON.stringify(definitions)); } definitions.forEach(definition => this.registerHotkeys(definition)); } catch (error) { - const { uiNotificationService, } = this._servicesManager.services; + const { uiNotificationService } = this._servicesManager.services; uiNotificationService.show({ title: 'Hotkeys Manager', message: 'Error while setting hotkeys', diff --git a/platform/docs/docs/platform/modes/index.md b/platform/docs/docs/platform/modes/index.md index 44a58fde1..aa3f0cb0d 100644 --- a/platform/docs/docs/platform/modes/index.md +++ b/platform/docs/docs/platform/modes/index.md @@ -315,7 +315,9 @@ handles creation of the displaySets. ### Hotkeys `hotkeys` is another property in the configuration of a mode that can be defined -to add the specific hotkeys to the viewer at all routes. +to add the specific hotkeys to the viewer on the mode route. Additionally, the +name under which the hotkeys are stored can be configured as `hotkeyName`. +This allows user customization of the mode specific hotkeys. ```js // default hotkeys @@ -347,7 +349,13 @@ function modeFactory() { /* ... */ - hotkeys: [..hotkeys.defaults.hotkeyBindings, ...myHotkeys], + hotkeys: { + // The name in preferences to use for this set of hotkeys + // Allows defining different sets for different modes + name: 'custom-hotkey-name', + // And the actual custom values here. + hotkeys:[..hotkeys.defaults.hotkeyBindings, ...myHotkeys] + }, } } diff --git a/platform/viewer/src/routes/Mode/Mode.tsx b/platform/viewer/src/routes/Mode/Mode.tsx index 0eb3f5a0a..a48f05500 100644 --- a/platform/viewer/src/routes/Mode/Mode.tsx +++ b/platform/viewer/src/routes/Mode/Mode.tsx @@ -114,7 +114,15 @@ export default function ModeRoute({ hangingProtocolService, } = (servicesManager as ServicesManager).services; - const { extensions, sopClassHandlers, hotkeys, hangingProtocol } = mode; + const { + extensions, + sopClassHandlers, + hotkeys: hotkeyObj, + hangingProtocol, + } = mode; + // Preserve the old array interface for hotkeys + const hotkeys = Array.isArray(hotkeyObj) ? hotkeyObj : hotkeyObj?.hotkeys; + const hotkeyName = hotkeyObj?.name || 'hotkey-definitions-v2'; if (dataSourceName === undefined) { dataSourceName = extensionManager.defaultDataSourceName; @@ -204,14 +212,12 @@ export default function ModeRoute({ hotkeysManager.setDefaultHotKeys(hotkeys); - const userPreferredHotkeys = JSON.parse( - localStorage.getItem('hotkey-definitions') - ); + const userPreferredHotkeys = JSON.parse(localStorage.getItem(hotkeyName)); if (userPreferredHotkeys?.length) { - hotkeysManager.setHotkeys(userPreferredHotkeys); + hotkeysManager.setHotkeys(userPreferredHotkeys, hotkeyName); } else { - hotkeysManager.setHotkeys(hotkeys); + hotkeysManager.setHotkeys(hotkeys, hotkeyName); } return () => {