runloopable / web /src /sheet /PasteSpecialDialog.tsx
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
ce9586e verified
Raw History Blame Contribute Delete
7.43 kB
// 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)}<u>{label[at]}</u>{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<HTMLDivElement>(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<HTMLElement>(".lp-dialog") ?? root.current,
});
const [what, setWhat] = useState<PasteWhat>("all");
const [operation, setOperation] = useState<PasteOperation>("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<HTMLDivElement>) => {
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<HTMLElement>(`[data-accelerator="${letter}"]`)?.focus();
};
const body = (
<div className="sheet-paste-special" ref={root} onKeyDown={keys}>
<fieldset className="sheet-paste-special-group">
<legend>Paste</legend>
<div className="sheet-paste-special-grid">
{CHOICES.map((choice, index) => (
<label key={choice.what} className="sheet-paste-special-option">
<input type="radio" name={`${id}-paste`} value={choice.what}
checked={what === choice.what} onChange={() => setWhat(choice.what)}
data-overlay-autofocus={index === 0 ? "" : undefined}
data-accelerator={choice.key} />
<span><Accelerator label={choice.label} letter={choice.key} /></span>
</label>
))}
</div>
</fieldset>
<fieldset className="sheet-paste-special-group">
<legend>Operation</legend>
<div className="sheet-paste-special-grid is-operation">
{OPERATIONS.map(op => (
<label key={op.value} className="sheet-paste-special-option">
<input type="radio" name={`${id}-operation`} value={op.value}
checked={operation === op.value} onChange={() => setOperation(op.value)}
data-accelerator={op.key} />
<span><Accelerator label={op.label} letter={op.key} /></span>
</label>
))}
</div>
</fieldset>
<div className="sheet-paste-special-grid">
<label className="sheet-paste-special-option">
<input type="checkbox" checked={skipBlanks} data-accelerator="B"
onChange={event => setSkipBlanks(event.currentTarget.checked)} />
<span><Accelerator label="Skip blanks" letter="B" /></span>
</label>
<label className="sheet-paste-special-option">
<input type="checkbox" checked={transpose} data-accelerator="E"
onChange={event => setTranspose(event.currentTarget.checked)} />
<span><Accelerator label="Transpose" letter="E" /></span>
</label>
</div>
<p id={`${id}-reason`} className="sheet-paste-special-reason" role="status">{reason}</p>
<div className="sheet-paste-special-actions">
<button type="button" className="lp-dialog-btn lp-dialog-btn-quiet"
disabled={Boolean(reason)} data-accelerator="L" onClick={() => apply(true)}>
<span><Accelerator label="Paste Link" letter="L" /></span>
</button>
<div className="sheet-paste-special-confirm">
<button type="button" className="lp-dialog-btn lp-dialog-btn-primary"
disabled={Boolean(reason)} aria-describedby={reason ? `${id}-reason` : undefined}
onClick={() => apply()}>OK</button>
<button type="button" className="lp-dialog-btn lp-dialog-btn-quiet" onClick={onClose}>Cancel</button>
</div>
</div>
</div>
);
// 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 <ConfirmDialog open title="Paste Special" body={body} confirmLabel="OK" tone="primary"
onConfirm={() => apply()} onCancel={onClose} />;
}