Skip to content

Commit

Permalink
fix: when tool button is not available shortcut should not be availab…
Browse files Browse the repository at this point in the history
…le neither

close #1504
  • Loading branch information
hamed-musallam committed Jun 15, 2022
1 parent 9d79817 commit 4c08578
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
60 changes: 41 additions & 19 deletions src/component/EventsTrackers/KeysListenerTracker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useDispatch } from '../context/DispatchContext';
import { useLoader } from '../context/LoaderContext';
import { useAlert } from '../elements/popup/Alert';
import { HighlightedSource, useHighlightData } from '../highlight/index';
import { useCheckToolsVisibility } from '../hooks/useCheckToolsVisibility';
import useExport from '../hooks/useExport';
import useToolsFunctions from '../hooks/useToolsFunctions';
import { DISPLAYER_MODE } from '../reducer/core/Constants';
Expand Down Expand Up @@ -42,6 +43,7 @@ function KeysListenerTracker() {

const { saveToClipboardHandler, saveAsJSONHandler, saveAsHandler } =
useExport();
const isToolVisible = useCheckToolsVisibility();

const { highlight, remove } = useHighlightData();

Expand Down Expand Up @@ -192,52 +194,69 @@ function KeysListenerTracker() {
if (!e.shiftKey && !e.metaKey) {
switch (e.key) {
case 'f':
handleFullZoomOut();
if (isToolVisible('zoomOutTool')) {
handleFullZoomOut();
}
break;
case 'z':
case 'Escape':
handleChangeOption(options.zoom.id);
if (isToolVisible('zoomTool')) {
handleChangeOption(options.zoom.id);
}
break;
case 'r': {
if (displayerMode === DISPLAYER_MODE.DM_2D) {
check2DModeWithActiveSpectrum(options.zone2D.label, false);
handleChangeOption(options.zone2D.id);
} else {
if (isToolVisible('zonePickingTool')) {
check2DModeWithActiveSpectrum(options.zone2D.label, false);
handleChangeOption(options.zone2D.id);
}
} else if (isToolVisible('autoRangesTool')) {
check1DModeWithActiveSpectrum(
options.rangesPicking.label,
false,
);
handleChangeOption(options.rangesPicking.id);
}

break;
}
case 'a': {
check1DModeWithActiveSpectrum(options.phaseCorrection.label);
handleChangeOption(options.phaseCorrection.id);
if (isToolVisible('phaseCorrectionTool')) {
check1DModeWithActiveSpectrum(options.phaseCorrection.label);
handleChangeOption(options.phaseCorrection.id);
}

break;
}
case 'b': {
check1DModeWithActiveSpectrum(options.baseLineCorrection.label);
handleChangeOption(options.baseLineCorrection.id);
if (isToolVisible('baseLineCorrectionTool')) {
check1DModeWithActiveSpectrum(options.baseLineCorrection.label);
handleChangeOption(options.baseLineCorrection.id);
}

break;
}
case 'p': {
check1DModeWithActiveSpectrum(options.peakPicking.label);
handleChangeOption(options.peakPicking.id);
if (isToolVisible('peakTool')) {
check1DModeWithActiveSpectrum(options.peakPicking.label);
handleChangeOption(options.peakPicking.id);
}

break;
}
case 'i': {
check1DModeWithActiveSpectrum(options.integral.label);
handleChangeOption(options.integral.id);
if (isToolVisible('integralTool')) {
check1DModeWithActiveSpectrum(options.integral.label);
handleChangeOption(options.integral.id);
}

break;
}
case 'e': {
check1DModeWithActiveSpectrum(options.exclusionZones.label);
handleChangeOption(options.exclusionZones.id);
if (isToolVisible('exclusionZonesTool')) {
check1DModeWithActiveSpectrum(options.exclusionZones.label);
handleChangeOption(options.exclusionZones.id);
}

break;
}
Expand All @@ -248,13 +267,13 @@ function KeysListenerTracker() {
if (!e.shiftKey && !e.metaKey && !e.ctrlKey) {
switch (e.key) {
case 'c': {
if (allow1DTool) {
if (allow1DTool && isToolVisible('spectraCenterAlignments')) {
alignSpectrumsVerticallyHandler();
}
break;
}
case 's': {
if (allow1DTool) {
if (allow1DTool && isToolVisible('spectraStackAlignments')) {
changeDisplayViewModeHandler();
}
break;
Expand All @@ -270,8 +289,10 @@ function KeysListenerTracker() {
e.preventDefault();
break;
case 's':
void saveAsJSONHandler();
e.preventDefault();
if (isToolVisible('exportAs')) {
void saveAsJSONHandler();
e.preventDefault();
}
break;
case 'o':
openLoader();
Expand Down Expand Up @@ -303,6 +324,7 @@ function KeysListenerTracker() {
displayerMode,
handleChangeOption,
handleFullZoomOut,
isToolVisible,
openLoader,
saveAsHandler,
saveAsJSONHandler,
Expand Down
5 changes: 4 additions & 1 deletion src/component/hooks/useCheckToolsVisibility.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import lodashGet from 'lodash/get';

import { NMRiumToolBarPreferences } from '../../types/NMRiumToolBarPreferences';
import { usePreferences } from '../context/PreferencesContext';

export function useCheckToolsVisibility(): (toolKey: string) => boolean {
export function useCheckToolsVisibility(): (
toolKey: keyof NMRiumToolBarPreferences,
) => boolean {
const preferences = usePreferences();

return (toolKey) =>
Expand Down

0 comments on commit 4c08578

Please sign in to comment.