Skip to content

Commit 5cf4295

Browse files
authored
Merge pull request #4345 from mathesar-foundation/4330_past_in_edit_mode
Fix bug when pasting into cell in edit mode
2 parents caf3eb1 + 542f478 commit 5cf4295

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

mathesar_ui/src/AppContext.svelte

+8-8
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@
6666
// Data Explorer.
6767
6868
function handleCopy(e: ClipboardEvent) {
69-
if (clipboardHandler) {
70-
clipboardHandler.handleCopy(e);
71-
e.preventDefault();
72-
}
69+
if (!clipboardHandler) return;
70+
if (!clipboardHandler.shouldHandleCopy(e)) return;
71+
clipboardHandler.handleCopy(e);
72+
e.preventDefault();
7373
}
7474
7575
function handlePaste(e: ClipboardEvent) {
76-
if (clipboardHandler) {
77-
void clipboardHandler.handlePaste(e);
78-
e.preventDefault();
79-
}
76+
if (!clipboardHandler) return;
77+
if (!clipboardHandler.shouldHandlePaste(e)) return;
78+
void clipboardHandler.handlePaste(e);
79+
e.preventDefault();
8080
}
8181
</script>
8282

mathesar_ui/src/components/sheet/clipboard/SheetClipboardHandler.ts

+14
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,27 @@ interface Dependencies {
1818
showToastError: (msg: string) => void;
1919
}
2020

21+
function shouldHandleEvent(): boolean {
22+
// Only handle clipboard events when a cell is focused. This prevents pasting
23+
// into inputs while editing cells, and other unexpected behavior.
24+
return document.activeElement?.hasAttribute('data-active-cell') ?? false;
25+
}
26+
2127
export class SheetClipboardHandler implements ClipboardHandler {
2228
private readonly deps: Dependencies;
2329

2430
constructor(deps: Dependencies) {
2531
this.deps = deps;
2632
}
2733

34+
shouldHandleCopy() {
35+
return shouldHandleEvent();
36+
}
37+
38+
shouldHandlePaste() {
39+
return shouldHandleEvent();
40+
}
41+
2842
handleCopy(event: ClipboardEvent): void {
2943
if (event.clipboardData == null) return;
3044
try {

mathesar_ui/src/stores/clipboard.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { getContext, setContext } from 'svelte';
22
import { type Writable, writable } from 'svelte/store';
33

44
export interface ClipboardHandler {
5+
shouldHandleCopy: (event: ClipboardEvent) => boolean;
6+
shouldHandlePaste: (event: ClipboardEvent) => boolean;
57
handleCopy: (event: ClipboardEvent) => void;
68
handlePaste: (event: ClipboardEvent) => Promise<void>;
79
}

0 commit comments

Comments
 (0)