fix(frontend): guard startup and loading shortcuts
This commit is contained in:
17 files changed
+420
-15
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { copyToClipboard } from "@/lib/common/clipboard";
|
||||
import { claimDataGridPaste, clearDataGridClipboardCopy, parseDataGridClipboard, planDataGridPaste, rememberDataGridClipboardCopy } from "@/lib/dataGrid/dataGridClipboard";
|
||||
import { claimDataGridPaste, claimDataGridSelectAll, clearDataGridClipboardCopy, parseDataGridClipboard, planDataGridPaste, rememberDataGridClipboardCopy } from "@/lib/dataGrid/dataGridClipboard";
|
||||
|
||||
afterEach(() => clearDataGridClipboardCopy());
|
||||
|
||||
@@ -19,6 +19,32 @@ function pasteEvent(nativeClipboard: boolean) {
|
||||
};
|
||||
}
|
||||
|
||||
describe("claimDataGridSelectAll", () => {
|
||||
it("blocks native page selection while the grid is loading", () => {
|
||||
const event = pasteEvent(false);
|
||||
|
||||
expect(claimDataGridSelectAll(event, true, false)).toBe("block");
|
||||
expect(event.preventDefault).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("selects grid data after loading", () => {
|
||||
const event = pasteEvent(false);
|
||||
|
||||
expect(claimDataGridSelectAll(event, false, true)).toBe("select");
|
||||
expect(event.preventDefault).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("keeps native selection for editors and empty idle grids", () => {
|
||||
const editorEvent = pasteEvent(true);
|
||||
const emptyGridEvent = pasteEvent(false);
|
||||
|
||||
expect(claimDataGridSelectAll(editorEvent, true, false)).toBe("native");
|
||||
expect(claimDataGridSelectAll(emptyGridEvent, false, false)).toBe("native");
|
||||
expect(editorEvent.preventDefault).not.toHaveBeenCalled();
|
||||
expect(emptyGridEvent.preventDefault).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("claimDataGridPaste", () => {
|
||||
it("keeps native paste behavior for editors inside the grid", () => {
|
||||
const event = pasteEvent(true);
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { beginDataGridNativeSelectionBlock, DATA_GRID_NATIVE_SELECTION_BLOCK_CLASS, DATA_GRID_NATIVE_SELECTION_RELEASE_DELAY_MS, finishDataGridNativeSelectionBlock } from "@/lib/dataGrid/dataGridNativeSelection";
|
||||
|
||||
function selectionEnvironment() {
|
||||
const classes = new Set<string>();
|
||||
const removeAllRanges = vi.fn();
|
||||
return {
|
||||
classes,
|
||||
removeAllRanges,
|
||||
environment: {
|
||||
document: {
|
||||
documentElement: {
|
||||
classList: {
|
||||
add: (className: string) => classes.add(className),
|
||||
remove: (className: string) => classes.delete(className),
|
||||
},
|
||||
},
|
||||
},
|
||||
getSelection: () => ({ removeAllRanges }),
|
||||
setTimeout,
|
||||
clearTimeout,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe("data grid native selection block", () => {
|
||||
it("blocks native selection immediately and clears an existing selection", () => {
|
||||
const { classes, removeAllRanges, environment } = selectionEnvironment();
|
||||
const owner = {};
|
||||
|
||||
beginDataGridNativeSelectionBlock(owner, environment);
|
||||
|
||||
expect(classes.has(DATA_GRID_NATIVE_SELECTION_BLOCK_CLASS)).toBe(true);
|
||||
expect(removeAllRanges).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("keeps the application-level block across a fast component replacement", () => {
|
||||
vi.useFakeTimers();
|
||||
const { classes, environment } = selectionEnvironment();
|
||||
const owner = {};
|
||||
|
||||
beginDataGridNativeSelectionBlock(owner, environment);
|
||||
finishDataGridNativeSelectionBlock(owner, environment);
|
||||
|
||||
vi.advanceTimersByTime(DATA_GRID_NATIVE_SELECTION_RELEASE_DELAY_MS - 1);
|
||||
expect(classes.has(DATA_GRID_NATIVE_SELECTION_BLOCK_CLASS)).toBe(true);
|
||||
|
||||
vi.advanceTimersByTime(1);
|
||||
expect(classes.has(DATA_GRID_NATIVE_SELECTION_BLOCK_CLASS)).toBe(false);
|
||||
});
|
||||
|
||||
it("cancels a pending release when another grid starts loading", () => {
|
||||
vi.useFakeTimers();
|
||||
const { classes, environment } = selectionEnvironment();
|
||||
const owner = {};
|
||||
|
||||
finishDataGridNativeSelectionBlock(owner, environment);
|
||||
vi.advanceTimersByTime(DATA_GRID_NATIVE_SELECTION_RELEASE_DELAY_MS - 1);
|
||||
beginDataGridNativeSelectionBlock(owner, environment);
|
||||
vi.advanceTimersByTime(DATA_GRID_NATIVE_SELECTION_RELEASE_DELAY_MS);
|
||||
|
||||
expect(classes.has(DATA_GRID_NATIVE_SELECTION_BLOCK_CLASS)).toBe(true);
|
||||
});
|
||||
|
||||
it("does not let another grid release the application-level block", () => {
|
||||
vi.useFakeTimers();
|
||||
const { classes, environment } = selectionEnvironment();
|
||||
const loadingGrid = {};
|
||||
const finishedGrid = {};
|
||||
|
||||
beginDataGridNativeSelectionBlock(loadingGrid, environment);
|
||||
finishDataGridNativeSelectionBlock(finishedGrid, environment);
|
||||
vi.advanceTimersByTime(DATA_GRID_NATIVE_SELECTION_RELEASE_DELAY_MS);
|
||||
|
||||
expect(classes.has(DATA_GRID_NATIVE_SELECTION_BLOCK_CLASS)).toBe(true);
|
||||
|
||||
finishDataGridNativeSelectionBlock(loadingGrid, environment);
|
||||
vi.advanceTimersByTime(DATA_GRID_NATIVE_SELECTION_RELEASE_DELAY_MS);
|
||||
expect(classes.has(DATA_GRID_NATIVE_SELECTION_BLOCK_CLASS)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { clearStartupPreloadRetry, retryStartupAfterPreloadFailure } from "@/lib/startup/startupPreloadRecovery";
|
||||
|
||||
function environment() {
|
||||
const values = new Map<string, string>();
|
||||
return {
|
||||
values,
|
||||
env: {
|
||||
sessionStorage: {
|
||||
getItem: vi.fn((key: string) => values.get(key) ?? null),
|
||||
setItem: vi.fn((key: string, value: string) => values.set(key, value)),
|
||||
removeItem: vi.fn((key: string) => values.delete(key)),
|
||||
},
|
||||
location: { reload: vi.fn() },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("startup preload recovery", () => {
|
||||
it("reloads once after a CSS preload failure", () => {
|
||||
const { env } = environment();
|
||||
const error = new Error("Unable to preload CSS for http://tauri.localhost/assets/QueryLoadingState.css");
|
||||
|
||||
expect(retryStartupAfterPreloadFailure(error, env)).toBe(true);
|
||||
expect(env.location.reload).toHaveBeenCalledOnce();
|
||||
expect(retryStartupAfterPreloadFailure(error, env)).toBe(false);
|
||||
expect(env.location.reload).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("does not reload for unrelated startup failures", () => {
|
||||
const { env } = environment();
|
||||
|
||||
expect(retryStartupAfterPreloadFailure(new Error("Database initialization failed"), env)).toBe(false);
|
||||
expect(env.location.reload).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("clears the retry marker after startup succeeds", () => {
|
||||
const { env, values } = environment();
|
||||
values.set("dbx-startup-preload-retry", "1");
|
||||
|
||||
clearStartupPreloadRetry(env);
|
||||
expect(values.has("dbx-startup-preload-retry")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -58,6 +58,7 @@ interface NativeClipboardSelectionEnvironment {
|
||||
|
||||
const EDITABLE_CLIPBOARD_TARGET_SELECTOR = "input, textarea, [contenteditable='true'], [role='textbox']";
|
||||
const NATIVE_CLIPBOARD_REGION_SELECTOR = "[data-native-clipboard]";
|
||||
const DATA_GRID_ROOT_SELECTOR = "[data-grid-root]";
|
||||
let clipboardWriteRevision = 0;
|
||||
|
||||
export function getClipboardWriteRevision(): number {
|
||||
@@ -86,6 +87,10 @@ export function isPlainClipboardShortcut(event: ClipboardShortcutEvent, key: str
|
||||
return !!(event.metaKey || event.ctrlKey) && !event.altKey && !event.shiftKey && event.key.toLowerCase() === key;
|
||||
}
|
||||
|
||||
export function shouldBlockAppNativeSelectAll(event: ClipboardShortcutEvent): boolean {
|
||||
return isPlainClipboardShortcut(event, "a") && !eventTargetUsesNativeClipboard(event) && !closestElement(event.target, DATA_GRID_ROOT_SELECTOR);
|
||||
}
|
||||
|
||||
export function hasNativeClipboardSelection(env: NativeClipboardSelectionEnvironment = globalThis as unknown as NativeClipboardSelectionEnvironment): boolean {
|
||||
const selection = env.getSelection?.();
|
||||
if (!selection || selection.isCollapsed) return false;
|
||||
|
||||
@@ -3,6 +3,7 @@ import { displayCellValue, type CellValue } from "@/lib/dataGrid/cellValue";
|
||||
import { parseClipboardTable } from "@/lib/dataGrid/gridSelection";
|
||||
|
||||
export type DataGridPasteIntent = "native" | "block" | "paste";
|
||||
export type DataGridSelectAllIntent = "native" | "block" | "select";
|
||||
|
||||
export interface DataGridPasteCell {
|
||||
rowOffset: number;
|
||||
@@ -24,6 +25,17 @@ interface DataGridPasteEvent {
|
||||
stopPropagation(): void;
|
||||
}
|
||||
|
||||
interface DataGridSelectAllEvent {
|
||||
target?: EventTarget | null;
|
||||
preventDefault(): void;
|
||||
}
|
||||
|
||||
export function claimDataGridSelectAll(event: DataGridSelectAllEvent, loading: boolean, hasData: boolean): DataGridSelectAllIntent {
|
||||
if (eventTargetUsesNativeClipboard(event) || (!loading && !hasData)) return "native";
|
||||
event.preventDefault();
|
||||
return loading ? "block" : "select";
|
||||
}
|
||||
|
||||
export function claimDataGridPaste(event: DataGridPasteEvent, editable: boolean, hasSelection: boolean): DataGridPasteIntent {
|
||||
if (eventTargetUsesNativeClipboard(event)) return "native";
|
||||
event.preventDefault();
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
export const DATA_GRID_NATIVE_SELECTION_BLOCK_CLASS = "dbx-data-grid-native-selection-blocked";
|
||||
export const DATA_GRID_NATIVE_SELECTION_RELEASE_DELAY_MS = 2000;
|
||||
|
||||
interface NativeSelectionBlockEnvironment {
|
||||
document?: {
|
||||
documentElement?: {
|
||||
classList: {
|
||||
add(className: string): void;
|
||||
remove(className: string): void;
|
||||
};
|
||||
};
|
||||
};
|
||||
getSelection?: () => { removeAllRanges(): void } | null;
|
||||
setTimeout(callback: () => void, delay: number): ReturnType<typeof setTimeout>;
|
||||
clearTimeout(timer: ReturnType<typeof setTimeout>): void;
|
||||
}
|
||||
|
||||
export type DataGridNativeSelectionBlockOwner = object;
|
||||
|
||||
interface ActiveSelectionBlock {
|
||||
environment: NativeSelectionBlockEnvironment;
|
||||
releaseTimer?: ReturnType<typeof setTimeout>;
|
||||
}
|
||||
|
||||
const activeSelectionBlocks = new Map<DataGridNativeSelectionBlockOwner, ActiveSelectionBlock>();
|
||||
|
||||
function defaultEnvironment(): NativeSelectionBlockEnvironment {
|
||||
return globalThis as unknown as NativeSelectionBlockEnvironment;
|
||||
}
|
||||
|
||||
function clearReleaseTimer(owner: DataGridNativeSelectionBlockOwner) {
|
||||
const activeBlock = activeSelectionBlocks.get(owner);
|
||||
if (activeBlock?.releaseTimer === undefined) return;
|
||||
activeBlock.environment.clearTimeout(activeBlock.releaseTimer);
|
||||
activeBlock.releaseTimer = undefined;
|
||||
}
|
||||
|
||||
function hasActiveBlockForEnvironment(environment: NativeSelectionBlockEnvironment) {
|
||||
const documentElement = environment.document?.documentElement;
|
||||
return [...activeSelectionBlocks.values()].some((activeBlock) => activeBlock.environment.document?.documentElement === documentElement);
|
||||
}
|
||||
|
||||
export function beginDataGridNativeSelectionBlock(owner: DataGridNativeSelectionBlockOwner, environment: NativeSelectionBlockEnvironment = defaultEnvironment()) {
|
||||
clearReleaseTimer(owner);
|
||||
activeSelectionBlocks.set(owner, { environment });
|
||||
environment.document?.documentElement?.classList.add(DATA_GRID_NATIVE_SELECTION_BLOCK_CLASS);
|
||||
environment.getSelection?.()?.removeAllRanges();
|
||||
}
|
||||
|
||||
export function finishDataGridNativeSelectionBlock(owner: DataGridNativeSelectionBlockOwner, environment: NativeSelectionBlockEnvironment = defaultEnvironment(), delay = DATA_GRID_NATIVE_SELECTION_RELEASE_DELAY_MS) {
|
||||
beginDataGridNativeSelectionBlock(owner, environment);
|
||||
const activeBlock = activeSelectionBlocks.get(owner);
|
||||
if (!activeBlock) return;
|
||||
activeBlock.releaseTimer = environment.setTimeout(() => {
|
||||
activeSelectionBlocks.delete(owner);
|
||||
if (!hasActiveBlockForEnvironment(environment)) {
|
||||
environment.document?.documentElement?.classList.remove(DATA_GRID_NATIVE_SELECTION_BLOCK_CLASS);
|
||||
}
|
||||
}, delay);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
const STARTUP_PRELOAD_RETRY_KEY = "dbx-startup-preload-retry";
|
||||
|
||||
interface StartupPreloadRecoveryEnvironment {
|
||||
sessionStorage: Pick<Storage, "getItem" | "setItem" | "removeItem">;
|
||||
location: Pick<Location, "reload">;
|
||||
}
|
||||
|
||||
function errorText(error: unknown): string {
|
||||
if (error instanceof Error) return [error.message, error.stack].filter(Boolean).join("\n");
|
||||
return String(error);
|
||||
}
|
||||
|
||||
export function retryStartupAfterPreloadFailure(error: unknown, env: StartupPreloadRecoveryEnvironment = window): boolean {
|
||||
if (!errorText(error).includes("Unable to preload CSS")) return false;
|
||||
try {
|
||||
if (env.sessionStorage.getItem(STARTUP_PRELOAD_RETRY_KEY) === "1") return false;
|
||||
env.sessionStorage.setItem(STARTUP_PRELOAD_RETRY_KEY, "1");
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
env.location.reload();
|
||||
return true;
|
||||
}
|
||||
|
||||
export function clearStartupPreloadRetry(env: Pick<StartupPreloadRecoveryEnvironment, "sessionStorage"> = window): void {
|
||||
try {
|
||||
env.sessionStorage.removeItem(STARTUP_PRELOAD_RETRY_KEY);
|
||||
} catch {
|
||||
// Storage can be unavailable in restricted WebView environments.
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user