Skip to content

Commit e3c7a4b

Browse files
authored
refactor: the hotkeys for the grid (#554)
1 parent a8184c9 commit e3c7a4b

File tree

9 files changed

+160
-220
lines changed

9 files changed

+160
-220
lines changed

apps/nextjs-app/src/features/app/blocks/share/view/component/grid/GridViewBase.tsx

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const GridViewBase = () => {
6565
const ssrRecords = useSSRRecords();
6666
const ssrRecord = useSSRRecord();
6767
const isTouchDevice = useIsTouchDevice();
68-
const { selection, setSelection, openStatisticMenu } = useGridViewStore();
68+
const { setSelection, openStatisticMenu } = useGridViewStore();
6969
const { columns: originalColumns, cellValue2GridDisplay } = useGridColumns();
7070
const { columns, onColumnResize } = useGridColumnResize(originalColumns);
7171
const { columnStatistics } = useGridColumnStatistics(columns);
@@ -195,22 +195,6 @@ export const GridViewBase = () => {
195195
[columns, openStatisticMenu]
196196
);
197197

198-
useEffect(() => {
199-
if (!selection) {
200-
return;
201-
}
202-
const handleFocus = (event: FocusEvent) => {
203-
const target = event.target as Node;
204-
if (container.current && !container.current.contains(target)) {
205-
gridRef.current?.resetState();
206-
}
207-
};
208-
document.addEventListener('focus', handleFocus, true);
209-
return () => {
210-
document.removeEventListener('focus', handleFocus, true);
211-
};
212-
}, [selection]);
213-
214198
return (
215199
<div ref={container} className="relative size-full overflow-hidden">
216200
{prepare ? (

apps/nextjs-app/src/features/app/blocks/view/grid/GridViewBase.tsx

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -605,22 +605,6 @@ export const GridViewBase: React.FC<IGridViewProps> = (props: IGridViewProps) =>
605605
gridRef.current?.resetState();
606606
});
607607

608-
useEffect(() => {
609-
if (!selection) {
610-
return;
611-
}
612-
const handleFocus = (event: FocusEvent) => {
613-
const target = event.target as Node;
614-
if (containerRef.current && !containerRef.current.contains(target)) {
615-
gridRef.current?.resetState();
616-
}
617-
};
618-
document.addEventListener('focus', handleFocus, true);
619-
return () => {
620-
document.removeEventListener('focus', handleFocus, true);
621-
};
622-
}, [selection]);
623-
624608
useScrollFrameRate(gridRef.current?.scrollBy);
625609

626610
return (

packages/sdk/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"@teable/icons": "workspace:*",
5858
"@teable/openapi": "workspace:*",
5959
"@teable/ui-lib": "workspace:*",
60-
"@types/mousetrap": "1.6.15",
6160
"antlr4ts": "0.5.0-alpha.4",
6261
"axios": "1.6.8",
6362
"class-transformer": "0.5.1",
@@ -71,9 +70,9 @@
7170
"lodash": "4.17.21",
7271
"lru-cache": "10.2.0",
7372
"lucide-react": "0.363.0",
74-
"mousetrap": "1.6.5",
7573
"react-day-picker": "8.10.0",
7674
"react-hammerjs": "1.0.1",
75+
"react-hotkeys-hook": "4.5.0",
7776
"react-textarea-autosize": "8.5.3",
7877
"react-use": "17.5.0",
7978
"reconnecting-websocket": "4.4.0",

packages/sdk/src/components/expand-record/ExpandRecorder.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ export const ExpandRecorder = (props: IExpandRecorderProps) => {
6363
};
6464

6565
return (
66-
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
67-
<div id={`${tableId}-${recordId}`} onKeyDown={(e) => e.stopPropagation()}>
66+
<div id={`${tableId}-${recordId}`}>
6867
<Wrap tableId={tableId}>
6968
<ExpandRecord
7069
visible

packages/sdk/src/components/grid/components/editor/EditorContainer.tsx

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import { clamp } from 'lodash';
33
import type { CSSProperties, ForwardRefRenderFunction } from 'react';
44
import { useEffect, useRef, useMemo, useImperativeHandle, forwardRef } from 'react';
5+
import { HotkeysProvider } from 'react-hotkeys-hook';
56
import type { IGridTheme } from '../../configs';
6-
import { useKeyboardSelection } from '../../hooks';
7+
import { GRID_HOTKEY_SCOPE, useKeyboardSelection } from '../../hooks';
78
import type { IInteractionLayerProps } from '../../InteractionLayer';
89
import type { IActiveCellBound, ICellItem, IRectangle, IScrollState } from '../../interface';
910
import type { CombinedSelection } from '../../managers';
@@ -270,22 +271,24 @@ export const EditorContainerBase: ForwardRefRenderFunction<
270271
};
271272

272273
return (
273-
<div className="click-outside-ignore pointer-events-none absolute left-0 top-0 w-full">
274-
<div
275-
className="absolute z-10"
276-
style={{
277-
top: rect.y,
278-
left: rect.x,
279-
minWidth: width,
280-
minHeight: height,
281-
}}
282-
onKeyDown={onKeyDown}
283-
onPaste={onPasteInner}
284-
>
285-
{EditorRenderer}
286-
<input className="opacity-0" ref={defaultFocusRef} />
274+
<HotkeysProvider initiallyActiveScopes={[GRID_HOTKEY_SCOPE]}>
275+
<div className="click-outside-ignore pointer-events-none absolute left-0 top-0 w-full">
276+
<div
277+
className="absolute z-10"
278+
style={{
279+
top: rect.y,
280+
left: rect.x,
281+
minWidth: width,
282+
minHeight: height,
283+
}}
284+
onKeyDown={onKeyDown}
285+
onPaste={onPasteInner}
286+
>
287+
{EditorRenderer}
288+
<input className="opacity-0" ref={defaultFocusRef} />
289+
</div>
287290
</div>
288-
</div>
291+
</HotkeysProvider>
289292
);
290293
};
291294

packages/sdk/src/components/grid/hooks/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ export * from './useVisibleRegion';
88
export * from './useResizeObserver';
99
export * from './useScrollFrameRate';
1010
export * from './useKeyboardSelection';
11-
export * from './useKeyboardNavigation';

packages/sdk/src/components/grid/hooks/useKeyboardNavigation.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)