Feat/better hotkeys scope (#526)

* Working version

* fix

* Fixed console log

* Fix lint

* wip

* Fix

* Fix

* consolelog

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Lucas Bordeau
2023-07-08 03:53:05 +02:00
committed by GitHub
parent 611cda1f41
commit 66dcc9b2e1
77 changed files with 1240 additions and 454 deletions

View File

@ -1,72 +1,74 @@
import { useHotkeys } from 'react-hotkeys-hook';
import { useRecoilState } from 'recoil';
import { Key } from 'ts-key-enum';
import { useRemoveFromHotkeysScopeStack } from '@/hotkeys/hooks/useRemoveFromHotkeysScopeStack';
import { useScopedHotkeys } from '@/hotkeys/hooks/useScopedHotkeys';
import { InternalHotkeysScope } from '@/hotkeys/types/internal/InternalHotkeysScope';
import { isSomeInputInEditModeState } from '../states/isSomeInputInEditModeState';
import { useDisableSoftFocus } from './useDisableSoftFocus';
import { useMoveSoftFocus } from './useMoveSoftFocus';
export function useMapKeyboardToSoftFocus() {
const { moveDown, moveLeft, moveRight, moveUp } = useMoveSoftFocus();
const removeFromHotkeysScopedStack = useRemoveFromHotkeysScopeStack();
const disableSoftFocus = useDisableSoftFocus();
const [isSomeInputInEditMode] = useRecoilState(isSomeInputInEditModeState);
useHotkeys(
'up, shift+enter',
useScopedHotkeys(
[Key.ArrowUp, `${Key.Shift}+${Key.Enter}`],
() => {
if (!isSomeInputInEditMode) {
moveUp();
}
},
InternalHotkeysScope.TableSoftFocus,
[moveUp, isSomeInputInEditMode],
{
preventDefault: true,
enableOnContentEditable: true,
enableOnFormTags: true,
},
);
useHotkeys(
'down',
useScopedHotkeys(
Key.ArrowDown,
() => {
if (!isSomeInputInEditMode) {
moveDown();
}
},
InternalHotkeysScope.TableSoftFocus,
[moveDown, isSomeInputInEditMode],
{
preventDefault: true,
enableOnContentEditable: true,
enableOnFormTags: true,
},
);
useHotkeys(
['left', 'shift+tab'],
useScopedHotkeys(
[Key.ArrowLeft, `${Key.Shift}+${Key.Tab}`],
() => {
if (!isSomeInputInEditMode) {
moveLeft();
}
},
InternalHotkeysScope.TableSoftFocus,
[moveLeft, isSomeInputInEditMode],
{
preventDefault: true,
enableOnContentEditable: true,
enableOnFormTags: true,
},
);
useHotkeys(
['right', 'tab'],
useScopedHotkeys(
[Key.ArrowRight, Key.Tab],
() => {
if (!isSomeInputInEditMode) {
moveRight();
}
},
InternalHotkeysScope.TableSoftFocus,
[moveRight, isSomeInputInEditMode],
{
preventDefault: true,
enableOnContentEditable: true,
enableOnFormTags: true,
);
useScopedHotkeys(
[Key.Escape],
() => {
removeFromHotkeysScopedStack(InternalHotkeysScope.TableSoftFocus);
disableSoftFocus();
},
InternalHotkeysScope.TableSoftFocus,
[removeFromHotkeysScopedStack, disableSoftFocus],
);
}