Skip to content

Commit c3a8a0c

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

File tree

4 files changed

+135
-74
lines changed

4 files changed

+135
-74
lines changed

src/Spreadsheet.tsx

Lines changed: 26 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,51 @@ 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 (
293+
shouldHandleClipboardEvent(
294+
document.activeElement,
295+
rootRef.current,
296+
mode
297+
)
298+
) {
301299
event.preventDefault();
302300
event.stopPropagation();
303301
clip(event);
304302
cut();
305303
}
306304
},
307-
[isFocused, clip, cut]
305+
[mode, clip, cut]
308306
);
309307

310308
const handleCopy = React.useCallback(
311309
(event: ClipboardEvent) => {
312-
if (isFocused()) {
310+
if (
311+
shouldHandleClipboardEvent(
312+
document.activeElement,
313+
rootRef.current,
314+
mode
315+
)
316+
) {
313317
event.preventDefault();
314318
event.stopPropagation();
315319
clip(event);
316320
copy();
317321
}
318322
},
319-
[isFocused, clip, copy]
323+
[mode, clip, copy]
320324
);
321325

322326
const handlePaste = React.useCallback(
323-
async (event: ClipboardEvent) => {
324-
if (mode === "view" && isFocused()) {
327+
(event: ClipboardEvent) => {
328+
if (
329+
shouldHandleClipboardEvent(
330+
document.activeElement,
331+
rootRef.current,
332+
mode
333+
)
334+
) {
325335
event.preventDefault();
326336
event.stopPropagation();
327337
if (event.clipboardData) {
@@ -330,7 +340,7 @@ const Spreadsheet = <CellType extends Types.CellBase>(
330340
}
331341
}
332342
},
333-
[mode, isFocused, paste]
343+
[mode, paste]
334344
);
335345

336346
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)