Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when pasting into cell in edit mode #4345

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions mathesar_ui/src/AppContext.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@
// Data Explorer.

function handleCopy(e: ClipboardEvent) {
if (clipboardHandler) {
clipboardHandler.handleCopy(e);
e.preventDefault();
}
if (!clipboardHandler) return;
if (!clipboardHandler.shouldHandleCopy(e)) return;
clipboardHandler.handleCopy(e);
e.preventDefault();
}

function handlePaste(e: ClipboardEvent) {
if (clipboardHandler) {
void clipboardHandler.handlePaste(e);
e.preventDefault();
}
if (!clipboardHandler) return;
if (!clipboardHandler.shouldHandlePaste(e)) return;
void clipboardHandler.handlePaste(e);
e.preventDefault();
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,27 @@ interface Dependencies {
showToastError: (msg: string) => void;
}

function shouldHandleEvent(): boolean {
// Only handle clipboard events when a cell is focused. This prevents pasting
// into inputs while editing cells, and other unexpected behavior.
return document.activeElement?.hasAttribute('data-active-cell') ?? false;
}

export class SheetClipboardHandler implements ClipboardHandler {
private readonly deps: Dependencies;

constructor(deps: Dependencies) {
this.deps = deps;
}

shouldHandleCopy() {
return shouldHandleEvent();
}

shouldHandlePaste() {
return shouldHandleEvent();
}

handleCopy(event: ClipboardEvent): void {
if (event.clipboardData == null) return;
try {
Expand Down
2 changes: 2 additions & 0 deletions mathesar_ui/src/stores/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { getContext, setContext } from 'svelte';
import { type Writable, writable } from 'svelte/store';

export interface ClipboardHandler {
shouldHandleCopy: (event: ClipboardEvent) => boolean;
shouldHandlePaste: (event: ClipboardEvent) => boolean;
handleCopy: (event: ClipboardEvent) => void;
handlePaste: (event: ClipboardEvent) => Promise<void>;
}
Expand Down
Loading