Skip to content

Commit 2c246e2

Browse files
committed
Extract shouldHandleClipboardEvent from Spreadsheet, improve and test
Fix util Fix spreadsheet
1 parent f4a6799 commit 2c246e2

File tree

4 files changed

+117
-74
lines changed

4 files changed

+117
-74
lines changed

src/Spreadsheet.tsx

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
transformCoordToPoint,
4040
getCellRangeValue,
4141
getCellValue,
42+
shouldHandleClipboardEvent,
4243
} from "./util";
4344
import "./Spreadsheet.css";
4445

@@ -286,42 +287,33 @@ const Spreadsheet = <CellType extends Types.CellBase>(
286287
[store]
287288
);
288289

289-
const isFocused = React.useCallback(() => {
290-
const root = rootRef.current;
291-
const { activeElement } = document;
292-
293-
return mode === "view" && root
294-
? root === activeElement || root.contains(activeElement)
295-
: false;
296-
}, [rootRef, mode]);
297-
298290
const handleCut = React.useCallback(
299291
(event: ClipboardEvent) => {
300-
if (isFocused()) {
292+
if (shouldHandleClipboardEvent(rootRef.current, mode)) {
301293
event.preventDefault();
302294
event.stopPropagation();
303295
clip(event);
304296
cut();
305297
}
306298
},
307-
[isFocused, clip, cut]
299+
[mode, clip, cut]
308300
);
309301

310302
const handleCopy = React.useCallback(
311303
(event: ClipboardEvent) => {
312-
if (isFocused()) {
304+
if (shouldHandleClipboardEvent(rootRef.current, mode)) {
313305
event.preventDefault();
314306
event.stopPropagation();
315307
clip(event);
316308
copy();
317309
}
318310
},
319-
[isFocused, clip, copy]
311+
[mode, clip, copy]
320312
);
321313

322314
const handlePaste = React.useCallback(
323-
async (event: ClipboardEvent) => {
324-
if (mode === "view" && isFocused()) {
315+
(event: ClipboardEvent) => {
316+
if (shouldHandleClipboardEvent(rootRef.current, mode)) {
325317
event.preventDefault();
326318
event.stopPropagation();
327319
if (event.clipboardData) {
@@ -330,7 +322,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
330322
}
331323
}
332324
},
333-
[mode, isFocused, paste]
325+
[mode, paste]
334326
);
335327

336328
const handleKeyDown = React.useCallback(

src/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ export function cut(state: Types.StoreState): Partial<Types.StoreState> {
123123
};
124124
}
125125

126-
export async function paste<Cell extends Types.CellBase>(
126+
export function paste<Cell extends Types.CellBase>(
127127
state: Types.StoreState<Cell>,
128128
text: string
129-
): Promise<Partial<Types.StoreState<Cell>> | null> {
129+
): Partial<Types.StoreState<Cell>> | null {
130130
const { active } = state;
131131
if (!active) {
132132
return null;

0 commit comments

Comments
 (0)