fix(grid): prevent duplicate paste handling
This commit is contained in:
1 parent
b11f38d2df
commit
977f47a65f
5 files changed
+84
-16
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { claimDataGridPaste } from "@/lib/dataGrid/dataGridClipboard";
|
||||
|
||||
function target(nativeClipboard: boolean): EventTarget {
|
||||
return {
|
||||
closest: () => (nativeClipboard ? {} : null),
|
||||
} as unknown as EventTarget;
|
||||
}
|
||||
|
||||
function pasteEvent(nativeClipboard: boolean) {
|
||||
return {
|
||||
target: target(nativeClipboard),
|
||||
preventDefault: vi.fn(),
|
||||
stopPropagation: vi.fn(),
|
||||
};
|
||||
}
|
||||
|
||||
describe("claimDataGridPaste", () => {
|
||||
it("keeps native paste behavior for editors inside the grid", () => {
|
||||
const event = pasteEvent(true);
|
||||
|
||||
expect(claimDataGridPaste(event, true, true)).toBe("native");
|
||||
expect(event.preventDefault).not.toHaveBeenCalled();
|
||||
expect(event.stopPropagation).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("owns and applies paste for editable grid selections", () => {
|
||||
const event = pasteEvent(false);
|
||||
|
||||
expect(claimDataGridPaste(event, true, true)).toBe("paste");
|
||||
expect(event.preventDefault).toHaveBeenCalledOnce();
|
||||
expect(event.stopPropagation).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("blocks paste for read-only results", () => {
|
||||
const event = pasteEvent(false);
|
||||
|
||||
expect(claimDataGridPaste(event, false, true)).toBe("block");
|
||||
expect(event.preventDefault).toHaveBeenCalledOnce();
|
||||
expect(event.stopPropagation).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("blocks paste when the grid has no selection target", () => {
|
||||
const event = pasteEvent(false);
|
||||
|
||||
expect(claimDataGridPaste(event, true, false)).toBe("block");
|
||||
expect(event.preventDefault).toHaveBeenCalledOnce();
|
||||
expect(event.stopPropagation).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
@@ -63,6 +63,10 @@ function closestElement(target: unknown, selector: string): unknown {
|
||||
return (target as { closest?: (selector: string) => unknown } | null)?.closest?.(selector) ?? null;
|
||||
}
|
||||
|
||||
export function eventTargetUsesNativeClipboard(event: Pick<ClipboardShortcutEvent, "target">): boolean {
|
||||
return !!closestElement(event.target, EDITABLE_CLIPBOARD_TARGET_SELECTOR);
|
||||
}
|
||||
|
||||
function selectionNodeElement(node: Node | null): Element | null {
|
||||
if (!node) return null;
|
||||
if (typeof Element !== "undefined" && node instanceof Element) return node;
|
||||
@@ -82,13 +86,13 @@ export function hasNativeClipboardSelection(env: NativeClipboardSelectionEnviron
|
||||
}
|
||||
|
||||
export function eventTargetAllowsNativeClipboard(event: ClipboardShortcutEvent, env: NativeClipboardSelectionEnvironment = globalThis as unknown as NativeClipboardSelectionEnvironment): boolean {
|
||||
if (closestElement(event.target, EDITABLE_CLIPBOARD_TARGET_SELECTOR)) return true;
|
||||
if (eventTargetUsesNativeClipboard(event)) return true;
|
||||
return isPlainClipboardShortcut(event, "c") && hasNativeClipboardSelection(env);
|
||||
}
|
||||
|
||||
export function eventTargetAllowsAppClipboardShortcut(event: ClipboardShortcutEvent, key = "v"): boolean {
|
||||
if (!isPlainClipboardShortcut(event, key)) return false;
|
||||
return !closestElement(event.target, EDITABLE_CLIPBOARD_TARGET_SELECTOR);
|
||||
return !eventTargetUsesNativeClipboard(event);
|
||||
}
|
||||
|
||||
export async function readTextFromClipboard(env: ClipboardEnvironment = globalThis as unknown as ClipboardEnvironment): Promise<string> {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { eventTargetUsesNativeClipboard } from "@/lib/common/clipboard";
|
||||
|
||||
export type DataGridPasteIntent = "native" | "block" | "paste";
|
||||
|
||||
interface DataGridPasteEvent {
|
||||
target?: EventTarget | null;
|
||||
preventDefault(): void;
|
||||
stopPropagation(): void;
|
||||
}
|
||||
|
||||
export function claimDataGridPaste(event: DataGridPasteEvent, editable: boolean, hasSelection: boolean): DataGridPasteIntent {
|
||||
if (eventTargetUsesNativeClipboard(event)) return "native";
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
return editable && hasSelection ? "paste" : "block";
|
||||
}
|
||||
Reference in new issue
Block a user