// S68-T26: draft C8 options only; SheetGrid owns the clipboard and the one paste engine. import { useId, useRef, useState } from "react"; import type { KeyboardEvent as ReactKeyboardEvent } from "react"; import { ConfirmDialog } from "../ui/ConfirmDialog"; import { useEscapeClose } from "../ui/useEscapeClose"; import type { PasteOperation, PasteOptions, PasteWhat } from "./clipboardOps"; import "../styles/wave68/paste-special.css"; // Row order produces Excel's two columns. Accelerators are Microsoft's documented letters; O and G // are read off Excel's own dialog (bettersolutions.com paste-special-dialog*.png, fetched 2026-09-30). const CHOICES: { what: PasteWhat; label: string; key?: string }[] = [ { what: "all", label: "All", key: "A" }, { what: "all-source-theme", label: "All using Source theme", key: "H" }, { what: "formulas", label: "Formulas", key: "F" }, { what: "all-except-borders", label: "All except borders", key: "X" }, { what: "values", label: "Values", key: "V" }, { what: "column-widths", label: "Column widths", key: "W" }, { what: "formats", label: "Formats", key: "T" }, { what: "formulas-number-formats", label: "Formulas and number formats", key: "R" }, { what: "comments", label: "Comments and Notes", key: "C" }, { what: "values-number-formats", label: "Values and number formats", key: "U" }, { what: "validation", label: "Validation", key: "N" }, { what: "all-merging-cf", label: "All merging conditional formats", key: "G" }, ]; const OPERATIONS: { value: PasteOperation; label: string; key?: string }[] = [ { value: "none", label: "None", key: "O" }, { value: "add", label: "Add", key: "D" }, { value: "subtract", label: "Subtract", key: "S" }, { value: "multiply", label: "Multiply", key: "M" }, { value: "divide", label: "Divide", key: "I" }, ]; function Accelerator({ label, letter }: { label: string; letter?: string }) { const at = letter ? label.toUpperCase().indexOf(letter) : -1; return at < 0 ? <>{label} : <>{label.slice(0, at)}{label[at]}{label.slice(at + 1)}; } export function PasteSpecialDialog({ reason, onApply, onClose }: { /** Empty while a clipboard payload is available; otherwise the visible reason OK is disabled. */ reason: string; onApply: (options: PasteOptions) => void; onClose: () => void; }) { const id = useId(); const root = useRef(null); // One Escape closes, as in Excel. The kit shell's own claim cancels a focused INPUT first and // counts a radio as a text edit, so this dialog claims above it through the same arbiter. useEscapeClose(true, onClose, { container: () => root.current?.closest(".lp-dialog") ?? root.current, }); const [what, setWhat] = useState("all"); const [operation, setOperation] = useState("none"); const [skipBlanks, setSkipBlanks] = useState(false); const [transpose, setTranspose] = useState(false); const apply = (link = false) => { // Excel's Link button always applies All without arithmetic or the draft checkboxes. if (!reason && link) { onApply({ what: "all", link: true }); return; } if (!reason) onApply({ what, operation, skipBlanks, transpose, link }); }; const keys = (event: ReactKeyboardEvent) => { if (event.ctrlKey || event.metaKey || event.repeat) return; if (event.key === "Enter") { // Cancel and Paste Link keep their own native button activation. if ((event.target as HTMLElement).closest("button")) return; event.preventDefault(); event.stopPropagation(); apply(); return; } const letter = event.key.toUpperCase(); const choice = CHOICES.find(item => item.key === letter); const op = OPERATIONS.find(item => item.key === letter); if (!choice && !op && !["B", "E", "L"].includes(letter)) return; event.preventDefault(); event.stopPropagation(); if (choice) setWhat(choice.what); else if (op) setOperation(op.value); else if (letter === "B") setSkipBlanks(value => !value); else if (letter === "E") setTranspose(value => !value); else { apply(true); return; } event.currentTarget.querySelector(`[data-accelerator="${letter}"]`)?.focus(); }; const body = (
Paste
{CHOICES.map((choice, index) => ( ))}
Operation
{OPERATIONS.map(op => ( ))}

{reason}

); // The shell still owns Escape, close, motion, focus trap/return and non-dismissing scrim. // Our footer supplies independent OK disabling and Excel's three-button arrangement. return apply()} onCancel={onClose} />; }