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,8 +1,9 @@
import React from 'react';
import { useHotkeys } from 'react-hotkeys-hook';
import { useRecoilState } from 'recoil';
import { captureHotkeyTypeInFocusState } from '@/hotkeys/states/captureHotkeyTypeInFocusState';
import { useAddToHotkeysScopeStack } from '@/hotkeys/hooks/useAddToHotkeysScopeStack';
import { useScopedHotkeys } from '@/hotkeys/hooks/useScopedHotkeys';
import { HotkeysScopeStackItem } from '@/hotkeys/types/internal/HotkeysScopeStackItems';
import { InternalHotkeysScope } from '@/hotkeys/types/internal/InternalHotkeysScope';
import { isNonTextWritingKey } from '@/utils/hotkeys/isNonTextWritingKey';
import { useEditableCell } from './hooks/useCloseEditableCell';
@ -10,26 +11,26 @@ import { EditableCellDisplayMode } from './EditableCellDisplayMode';
export function EditableCellSoftFocusMode({
children,
}: React.PropsWithChildren<unknown>) {
editHotkeysScope,
}: React.PropsWithChildren<{ editHotkeysScope?: HotkeysScopeStackItem }>) {
const { closeEditableCell, openEditableCell } = useEditableCell();
const [captureHotkeyTypeInFocus] = useRecoilState(
captureHotkeyTypeInFocusState,
);
const addToHotkeysScopeStack = useAddToHotkeysScopeStack();
useHotkeys(
useScopedHotkeys(
'enter',
() => {
openEditableCell();
addToHotkeysScopeStack(
editHotkeysScope ?? {
scope: InternalHotkeysScope.CellEditMode,
},
);
},
{
enableOnContentEditable: true,
enableOnFormTags: true,
preventDefault: true,
},
InternalHotkeysScope.TableSoftFocus,
[closeEditableCell],
);
useHotkeys(
useScopedHotkeys(
'*',
(keyboardEvent) => {
const isWritingText =
@ -41,14 +42,16 @@ export function EditableCellSoftFocusMode({
return;
}
if (captureHotkeyTypeInFocus) {
return;
}
openEditableCell();
addToHotkeysScopeStack(
editHotkeysScope ?? {
scope: InternalHotkeysScope.CellEditMode,
},
);
},
InternalHotkeysScope.TableSoftFocus,
[openEditableCell, addToHotkeysScopeStack, editHotkeysScope],
{
enableOnContentEditable: true,
enableOnFormTags: true,
preventDefault: false,
},
);