Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download web/src/sheet/CellContextMenu.tsx from fsanyoto/runloopable: direct link, hf CLI and curl.
- Browser
- Download file 21.2 kB
-
https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/web/src/sheet/CellContextMenu.tsx
- Command line
-
hf download hf://spaces/fsanyoto/runloopable/web/src/sheet/CellContextMenu.tsx
-
curl -L -o CellContextMenu.tsx https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/web/src/sheet/CellContextMenu.tsx
21.2 kB
| // --------------------------------------------------------------------------- | |
| // sheet/CellContextMenu.tsx — S67-T07 (QI19, R31, 2026-09-28): right-clicking a CELL shows Excel | |
| // desktop's cell menu, with its mini toolbar above it. | |
| // | |
| // ⭐⭐ R31: the labels are Excel's own, Title Case with Excel's ellipses ("Clear Contents", | |
| // "Format Cells..."), an exception to DESIGN.md §2 for these menus only. Order and separators | |
| // follow Excel desktop: Cut, Copy, Paste Options, Paste Special... | Insert..., Delete..., Clear | |
| // Contents | Filter >, Sort > | New Comment, New Note | Format Cells..., Pick From Drop-down | |
| // List..., Link. Smart Lookup, Translate, Get Data from Table/Range..., Quick Analysis and Define | |
| // Name... have no Loopable backend and are left out (R31). | |
| // | |
| // ⛔ EVERY ITEM RUNS A DOOR THAT ALREADY EXISTS. This file decides nothing about the document: it | |
| // reports `onAct(<act>)` and `SheetGrid.tsx` routes each act to the clipboard, structure, style, | |
| // sort, filter, note, link or validation door the ribbon already uses. A second write path here | |
| // would be two answers to one act. | |
| // | |
| // ⚠ A read-only sheet DISABLES the edit items rather than hiding them (the done-when's NC), and an | |
| // empty clipboard disables Paste; both say why in the item's title. | |
| // --------------------------------------------------------------------------- | |
| import { useEffect, useLayoutEffect, useRef, useState } from "react"; | |
| import type { ComponentProps, KeyboardEvent as ReactKeyboardEvent, ReactNode } from "react"; | |
| import { AnchoredOverlay } from "../customer-grid/OverlaySurface"; | |
| import { | |
| IconAccounting, IconAlignCenter, IconBorderBottom, IconCellFormat, IconClear, IconCommaStyle, | |
| IconCopy, IconCut, IconDecimalLess, IconDecimalMore, IconDelete, IconFillColor, IconFontColor, | |
| IconFormatPainter, IconGrowFont, IconInsert, IconMenuFilter, IconMenuLink, IconMenuSort, | |
| IconMergeCenter, IconNewComment, IconNewNote, IconPaste, IconPasteFormatting, IconPasteFormulas, | |
| IconPasteLink, IconPasteSpecial, IconPasteTranspose, IconPasteValues, IconPercentStyle, | |
| IconPickList, IconRowGroup, IconShrinkFont, IconRowHeight, IconColumnWidth, IconHideBand, IconUnhideBand, | |
| } from "./ribbonIcons"; | |
| import "../styles/wave67/cell-menu.css"; | |
| /** Every act the menu can report. `SheetGrid.tsx::onCellMenuAct` owns what each one does. */ | |
| export type CellMenuAct = | |
| | "cut" | "copy" | "paste" | "paste-values" | "paste-formulas" | "paste-formatting" | |
| | "paste-special" | "paste-transpose" | "paste-link" | |
| | "insert-right" | "insert-down" | "insert-row" | "insert-col" | |
| | "delete-left" | "delete-up" | "delete-row" | "delete-col" | |
| | "clear-contents" | |
| | "filter-reapply" | "filter-clear" | "filter-value" | |
| | "sort-asc" | "sort-desc" | |
| | "new-comment" | "new-note" | "format-cells" | "pick-list" | "link" | |
| | "bold" | "italic" | "center" | "accounting" | "percent" | "comma" | |
| | "decimal-more" | "decimal-less" | "grow-font" | "shrink-font" | "format-painter" | |
| | "fill" | "font-color" | "borders" | "merge-center" | |
| // S67-T08: the row and column header menus' own acts. | |
| | "row-height" | "col-width" | "hide-rows" | "hide-cols" | "unhide" | "group-rows" | "ungroup-rows"; | |
| /** Excel's font size ladder, which Increase and Decrease Font Size step along. */ | |
| export const FONT_SIZE_LADDER = [8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72] as const; | |
| export interface CellContextMenuProps { | |
| anchor: ComponentProps<typeof AnchoredOverlay>["anchor"]; | |
| /** | |
| * S67-T08: which surface was right clicked. A cell gets Excel's cell menu; a row or column | |
| * header gets Excel's row or column menu (Insert, Delete, Row Height... or Column Width..., | |
| * Hide, Unhide), and the frozen band's headers the same. | |
| */ | |
| kind?: "cell" | "row" | "column"; | |
| /** Row menu only: the sheet can group rows here, and some rows are grouped already. */ | |
| canGroup?: boolean; | |
| canUngroup?: boolean; | |
| /** The workbook is read only for this reader: edit items are shown, disabled. */ | |
| readOnly: boolean; | |
| /** The browser clipboard is known empty. Unknown or denied reads keep Paste available. */ | |
| clipboardEmpty: boolean; | |
| /** A sheet filter is on, so Reapply and Clear Filter have something to act on. */ | |
| filterActive: boolean; | |
| /** The selection's anchor font and size, shown in the mini toolbar. */ | |
| fontName: string; | |
| fontSize: number; | |
| fonts: readonly string[]; | |
| onFont: (name: string) => void; | |
| onSize: (size: number) => void; | |
| onAct: (act: CellMenuAct) => void; | |
| onClose: () => void; | |
| } | |
| type Sub = "filter" | "sort" | null; | |
| type Ask = "insert" | "delete" | null; | |
| const READ_ONLY_WHY = "You can view this workbook but not change it."; | |
| /** The accessible name of each menu, which is also how a person's screen reader tells them apart. */ | |
| const MENU_LABEL = { cell: "Cell", row: "Row", column: "Column" } as const; | |
| /** The existing in-menu Insert and Delete choices. Paste Special is owned by the grid. */ | |
| const ASK_TITLE: Record<Exclude<Ask, null>, string> = { | |
| insert: "Insert", delete: "Delete", | |
| }; | |
| const SIDEWAYS_WHY = "Cells shift down or up in Loopable today; a sideways shift is not there yet."; | |
| const ASK_OPTIONS: Record<Exclude<Ask, null>, [string, string, string?][]> = { | |
| insert: [["right", "Shift cells right", SIDEWAYS_WHY], ["down", "Shift cells down"], | |
| ["row", "Entire row"], ["col", "Entire column"]], | |
| delete: [["left", "Shift cells left", SIDEWAYS_WHY], ["up", "Shift cells up"], | |
| ["row", "Entire row"], ["col", "Entire column"]], | |
| }; | |
| export default function CellContextMenu({ | |
| anchor, kind = "cell", canGroup = false, canUngroup = false, readOnly, clipboardEmpty, filterActive, | |
| fontName, fontSize, fonts, onFont, onSize, onAct, onClose, | |
| }: CellContextMenuProps) { | |
| const [sub, setSub] = useState<Sub>(null); | |
| const [ask, setAsk] = useState<Ask>(null); | |
| const [shift, setShift] = useState<string>(""); | |
| const listRef = useRef<HTMLDivElement | null>(null); | |
| const subRef = useRef<HTMLDivElement | null>(null); | |
| const hoverTimer = useRef(0); | |
| useEffect(() => () => window.clearTimeout(hoverTimer.current), []); | |
| const act = (a: CellMenuAct) => { onClose(); onAct(a); }; | |
| // Submenu hover intent: open after a short dwell, so a pointer crossing the list does not | |
| // flicker every submenu it passes (Interaction layer). | |
| const hoverOpen = (next: Sub) => { | |
| window.clearTimeout(hoverTimer.current); | |
| hoverTimer.current = window.setTimeout(() => setSub(next), next ? 160 : 220); | |
| }; | |
| // Keyboard: Up and Down walk the enabled items, Right opens a submenu, Left or Escape closes | |
| // it, Home and End jump. Escape on the main list is the overlay's own dismiss. | |
| const items = (root: HTMLElement | null) => { | |
| if (!root) return []; | |
| const home = root.classList.contains("sheet-cell-submenu") ? root : null; | |
| return [...root.querySelectorAll<HTMLButtonElement>("[role=menuitem]:not([disabled])")] | |
| .filter((el) => el.closest(".sheet-cell-submenu") === home); | |
| }; | |
| const walk = (root: HTMLElement | null, e: ReactKeyboardEvent<HTMLElement>) => { | |
| const list = items(root); | |
| const at = list.indexOf(document.activeElement as HTMLButtonElement); | |
| const go = (i: number) => { e.preventDefault(); list[(i + list.length) % list.length]?.focus(); }; | |
| if (e.key === "ArrowDown") return go(at + 1); | |
| if (e.key === "ArrowUp") return go(at < 0 ? list.length - 1 : at - 1); | |
| if (e.key === "Home") return go(0); | |
| if (e.key === "End") return go(list.length - 1); | |
| }; | |
| const onListKey = (e: ReactKeyboardEvent<HTMLElement>) => { | |
| if (ask) return; | |
| if (sub && subRef.current?.contains(document.activeElement)) { | |
| if (e.key === "ArrowLeft" || e.key === "Escape") { | |
| e.preventDefault(); e.stopPropagation(); | |
| const owner = listRef.current?.querySelector<HTMLButtonElement>(`[data-sub="${sub}"]`); | |
| setSub(null); | |
| owner?.focus(); | |
| return; | |
| } | |
| walk(subRef.current, e); | |
| return; | |
| } | |
| const owner = (document.activeElement as HTMLElement | null)?.dataset?.sub as Sub | undefined; | |
| if (owner && (e.key === "ArrowRight" || e.key === "Enter" || e.key === " ")) { | |
| e.preventDefault(); | |
| setSub(owner); | |
| return; | |
| } | |
| walk(listRef.current, e); | |
| }; | |
| useLayoutEffect(() => { | |
| if (sub) items(subRef.current)[0]?.focus(); | |
| }, [sub]); | |
| useLayoutEffect(() => { | |
| if (!ask) return; | |
| setShift(ask === "insert" ? "down" : "up"); | |
| }, [ask]); | |
| const item = ( | |
| label: string, icon: ReactNode, a: CellMenuAct | null, | |
| opts: { disabled?: boolean; why?: string; sub?: Sub; shortcut?: string } = {}, | |
| ) => ( | |
| <button | |
| type="button" role="menuitem" className="sheet-cell-menu-item" | |
| disabled={opts.disabled} | |
| title={opts.disabled ? opts.why : undefined} | |
| aria-haspopup={opts.sub ? "menu" : undefined} | |
| aria-expanded={opts.sub ? sub === opts.sub : undefined} | |
| data-sub={opts.sub ?? undefined} | |
| onMouseEnter={() => hoverOpen(opts.sub ?? null)} | |
| onClick={() => { | |
| if (opts.sub) { setSub(sub === opts.sub ? null : opts.sub); return; } | |
| if (a) act(a); | |
| }} | |
| > | |
| <span className="sheet-cell-menu-icon-slot">{icon}</span> | |
| <span className="sheet-cell-menu-label">{label}</span> | |
| {opts.shortcut && <span className="sheet-cell-menu-shortcut">{opts.shortcut}</span>} | |
| {opts.sub && ( | |
| <svg className="sheet-cell-menu-chevron" viewBox="0 0 16 16" aria-hidden="true"> | |
| <path d="M6 3.5l4.5 4.5L6 12.5" /> | |
| </svg> | |
| )} | |
| </button> | |
| ); | |
| const sep = (key: string) => <div key={key} className="sheet-cell-menu-sep" role="separator" />; | |
| const edit = { disabled: readOnly, why: READ_ONLY_WHY }; | |
| const pasteGate = readOnly | |
| ? { disabled: true, why: READ_ONLY_WHY } | |
| : clipboardEmpty | |
| ? { disabled: true, why: "There is nothing on the clipboard to paste." } | |
| : { disabled: false }; | |
| const pasteOption = (label: string, icon: ReactNode, a: CellMenuAct | null, why?: string) => ( | |
| <button | |
| type="button" role="menuitem" className="sheet-cell-menu-paste" | |
| aria-label={label} title={why ?? label} | |
| disabled={pasteGate.disabled || a === null} | |
| onMouseEnter={() => hoverOpen(null)} | |
| onClick={() => a && act(a)} | |
| > | |
| {icon} | |
| </button> | |
| ); | |
| const mini = (label: string, icon: ReactNode, a: CellMenuAct) => ( | |
| <button | |
| type="button" className="sheet-cell-mini-btn" aria-label={label} title={label} | |
| disabled={readOnly} onClick={() => act(a)} | |
| > | |
| {icon} | |
| </button> | |
| ); | |
| return ( | |
| <AnchoredOverlay | |
| anchor={anchor} | |
| className="cg-pop sheet-cell-menu-pop" | |
| placement="bottom-start" | |
| role="menu" | |
| ariaLabel={MENU_LABEL[kind]} | |
| onDismiss={onClose} | |
| onKeyDown={onListKey} | |
| > | |
| {/* ⭐ EXCEL'S MINI TOOLBAR, ABOVE THE MENU: font, size, grow and shrink, Accounting, | |
| Percent, Comma, Format Painter on the first row; Bold, Italic, Center, Borders, Fill, | |
| Font Color, the two decimals and Merge and Center on the second. */} | |
| <div className="sheet-cell-mini" role="toolbar" aria-label="Formatting"> | |
| <div className="sheet-cell-mini-row"> | |
| <select className="sheet-cell-mini-font" aria-label="Font" value={fontName} | |
| disabled={readOnly} onChange={(e) => { onClose(); onFont(e.currentTarget.value); }}> | |
| {fonts.map((f) => <option key={f} value={f}>{f}</option>)} | |
| </select> | |
| <select className="sheet-cell-mini-size" aria-label="Font Size" value={String(fontSize)} | |
| disabled={readOnly} onChange={(e) => { onClose(); onSize(Number(e.currentTarget.value)); }}> | |
| {[...new Set([...FONT_SIZE_LADDER, fontSize])].sort((a, b) => a - b) | |
| .map((s) => <option key={s} value={String(s)}>{s}</option>)} | |
| </select> | |
| {mini("Increase Font Size", <IconGrowFont />, "grow-font")} | |
| {mini("Decrease Font Size", <IconShrinkFont />, "shrink-font")} | |
| {mini("Accounting Number Format", <IconAccounting />, "accounting")} | |
| {mini("Percent Style", <IconPercentStyle />, "percent")} | |
| {mini("Comma Style", <IconCommaStyle />, "comma")} | |
| {mini("Format Painter", <IconFormatPainter />, "format-painter")} | |
| </div> | |
| <div className="sheet-cell-mini-row"> | |
| <button type="button" className="sheet-cell-mini-btn sheet-cell-mini-b" aria-label="Bold" | |
| title="Bold" disabled={readOnly} onClick={() => act("bold")}>B</button> | |
| <button type="button" className="sheet-cell-mini-btn sheet-cell-mini-i" aria-label="Italic" | |
| title="Italic" disabled={readOnly} onClick={() => act("italic")}>I</button> | |
| {mini("Center", <IconAlignCenter />, "center")} | |
| {mini("Borders", <IconBorderBottom />, "borders")} | |
| {mini("Fill Color", <IconFillColor />, "fill")} | |
| {mini("Font Color", <IconFontColor />, "font-color")} | |
| {mini("Increase Decimal", <IconDecimalMore />, "decimal-more")} | |
| {mini("Decrease Decimal", <IconDecimalLess />, "decimal-less")} | |
| {mini("Merge and Center", <IconMergeCenter />, "merge-center")} | |
| </div> | |
| </div> | |
| {ask ? ( | |
| <div className="sheet-cell-menu-ask" role="dialog" aria-label={ASK_TITLE[ask]}> | |
| <div className="sheet-cell-menu-ask-title">{ASK_TITLE[ask]}</div> | |
| {ASK_OPTIONS[ask].map(([value, label, why]) => ( | |
| <label key={value} className="sheet-cell-menu-ask-option" title={why}> | |
| <input type="radio" name="sheet-cell-shift" value={value} checked={shift === value} | |
| disabled={Boolean(why)} onChange={() => setShift(value)} /> | |
| <span>{label}</span> | |
| </label> | |
| ))} | |
| <div className="sheet-cell-menu-ask-actions"> | |
| <button type="button" className="sheet-cell-menu-ask-ok" onClick={() => { | |
| act(`${ask}-${shift}` as CellMenuAct); | |
| }}>OK</button> | |
| <button type="button" className="sheet-cell-menu-ask-cancel" onClick={onClose}>Cancel</button> | |
| </div> | |
| </div> | |
| ) : kind !== "cell" ? ( | |
| <div className="sheet-cell-menu" ref={listRef}> | |
| {item("Cut", <IconCut />, "cut", edit)} | |
| {item("Copy", <IconCopy />, "copy")} | |
| <div className="sheet-cell-menu-heading"> | |
| <span className="sheet-cell-menu-icon-slot"><IconPaste /></span> | |
| <span className="sheet-cell-menu-label">Paste Options:</span> | |
| </div> | |
| <div className="sheet-cell-menu-paste-row" role="group" aria-label="Paste Options"> | |
| {pasteOption("Paste", <IconPaste />, "paste")} | |
| {pasteOption("Formulas", <IconPasteFormulas />, "paste-formulas")} | |
| {pasteOption("Values", <IconPasteValues />, "paste-values")} | |
| {pasteOption("Formatting", <IconPasteFormatting />, "paste-formatting")} | |
| {pasteOption("Transpose", <IconPasteTranspose />, "paste-transpose")} | |
| {pasteOption("Paste Link", <IconPasteLink />, "paste-link")} | |
| </div> | |
| <button | |
| type="button" role="menuitem" className="sheet-cell-menu-item" | |
| disabled={pasteGate.disabled} title={pasteGate.disabled ? pasteGate.why : undefined} | |
| onClick={() => act("paste-special")} | |
| > | |
| <span className="sheet-cell-menu-icon-slot"><IconPasteSpecial /></span> | |
| <span className="sheet-cell-menu-label">Paste Special...</span> | |
| </button> | |
| {sep("h1")} | |
| {item("Insert", <IconInsert />, kind === "row" ? "insert-row" : "insert-col", edit)} | |
| {item("Delete", <IconDelete />, kind === "row" ? "delete-row" : "delete-col", edit)} | |
| {item("Clear Contents", <IconClear />, "clear-contents", edit)} | |
| {sep("h2")} | |
| {item("Format Cells...", <IconCellFormat />, "format-cells", { ...edit, shortcut: "Ctrl+1" })} | |
| {kind === "row" | |
| ? item("Row Height...", <IconRowHeight />, "row-height", edit) | |
| : item("Column Width...", <IconColumnWidth />, "col-width", edit)} | |
| {item("Hide", <IconHideBand column={kind === "column"} />, kind === "row" ? "hide-rows" : "hide-cols", edit)} | |
| {item("Unhide", <IconUnhideBand column={kind === "column"} />, "unhide", edit)} | |
| {kind === "row" && (canGroup || canUngroup) && sep("h3")} | |
| {/* ⚠ NOT IN EXCEL'S ROW MENU. Kept because this tree's Data ribbon has no Group | |
| control (its Outline group holds Hide and Unhide only), so this is the only mouse | |
| door to row grouping; S67-T08's record names it. */} | |
| {kind === "row" && canGroup && item("Group rows", <IconRowGroup collapsed={false} />, "group-rows", edit)} | |
| {kind === "row" && canUngroup && item("Ungroup rows", <IconRowGroup collapsed />, "ungroup-rows", edit)} | |
| </div> | |
| ) : ( | |
| <div className="sheet-cell-menu" ref={listRef} onMouseLeave={() => window.clearTimeout(hoverTimer.current)}> | |
| {item("Cut", <IconCut />, "cut", edit)} | |
| {item("Copy", <IconCopy />, "copy")} | |
| <div className="sheet-cell-menu-heading" onMouseEnter={() => hoverOpen(null)}> | |
| <span className="sheet-cell-menu-icon-slot"> | |
| <IconPaste /> | |
| </span> | |
| <span className="sheet-cell-menu-label">Paste Options:</span> | |
| </div> | |
| <div className="sheet-cell-menu-paste-row" role="group" aria-label="Paste Options"> | |
| {pasteOption("Paste", <IconPaste />, "paste")} | |
| {pasteOption("Formulas", <IconPasteFormulas />, "paste-formulas")} | |
| {pasteOption("Values", <IconPasteValues />, "paste-values")} | |
| {pasteOption("Formatting", <IconPasteFormatting />, "paste-formatting")} | |
| {pasteOption("Transpose", <IconPasteTranspose />, "paste-transpose")} | |
| {pasteOption("Paste Link", <IconPasteLink />, "paste-link")} | |
| </div> | |
| <button | |
| type="button" role="menuitem" className="sheet-cell-menu-item" | |
| disabled={pasteGate.disabled} title={pasteGate.disabled ? pasteGate.why : undefined} | |
| onMouseEnter={() => hoverOpen(null)} onClick={() => act("paste-special")} | |
| > | |
| <span className="sheet-cell-menu-icon-slot"><IconPasteSpecial /></span> | |
| <span className="sheet-cell-menu-label">Paste Special...</span> | |
| </button> | |
| {sep("s1")} | |
| <button | |
| type="button" role="menuitem" className="sheet-cell-menu-item" disabled={readOnly} | |
| title={readOnly ? READ_ONLY_WHY : undefined} | |
| onMouseEnter={() => hoverOpen(null)} onClick={() => setAsk("insert")} | |
| > | |
| <span className="sheet-cell-menu-icon-slot"><IconInsert /></span> | |
| <span className="sheet-cell-menu-label">Insert...</span> | |
| </button> | |
| <button | |
| type="button" role="menuitem" className="sheet-cell-menu-item" disabled={readOnly} | |
| title={readOnly ? READ_ONLY_WHY : undefined} | |
| onMouseEnter={() => hoverOpen(null)} onClick={() => setAsk("delete")} | |
| > | |
| <span className="sheet-cell-menu-icon-slot"><IconDelete /></span> | |
| <span className="sheet-cell-menu-label">Delete...</span> | |
| </button> | |
| {item("Clear Contents", <IconClear />, "clear-contents", edit)} | |
| {sep("s2")} | |
| <div className="sheet-cell-menu-subwrap"> | |
| {item("Filter", <IconMenuFilter />, null, { sub: "filter" })} | |
| {sub === "filter" && ( | |
| <div className="sheet-cell-submenu" role="menu" aria-label="Filter" ref={subRef} | |
| onMouseEnter={() => window.clearTimeout(hoverTimer.current)}> | |
| {item("Reapply", <IconMenuFilter />, "filter-reapply", | |
| { disabled: !filterActive, why: "No filter is on in this sheet." })} | |
| {item("Clear Filter", <IconClear />, "filter-clear", | |
| { disabled: !filterActive, why: "No filter is on in this sheet." })} | |
| {item("Filter by Selected Cell's Value", <IconMenuFilter />, "filter-value")} | |
| </div> | |
| )} | |
| </div> | |
| <div className="sheet-cell-menu-subwrap"> | |
| {item("Sort", <IconMenuSort />, null, { sub: "sort" })} | |
| {sub === "sort" && ( | |
| <div className="sheet-cell-submenu" role="menu" aria-label="Sort" ref={subRef} | |
| onMouseEnter={() => window.clearTimeout(hoverTimer.current)}> | |
| {item("Sort A to Z", <IconMenuSort />, "sort-asc", edit)} | |
| {item("Sort Z to A", <IconMenuSort />, "sort-desc", edit)} | |
| </div> | |
| )} | |
| </div> | |
| {sep("s3")} | |
| {item("New Comment", <IconNewComment />, "new-comment", edit)} | |
| {item("Add note", <IconNewNote />, "new-note", edit)} | |
| {sep("s4")} | |
| {item("Format Cells...", <IconCellFormat />, "format-cells", { ...edit, shortcut: "Ctrl+1" })} | |
| {item("Pick From Drop-down List...", <IconPickList />, "pick-list", edit)} | |
| {item("Link", <IconMenuLink />, "link", edit)} | |
| </div> | |
| )} | |
| </AnchoredOverlay> | |
| ); | |
| } | |