Download web/src/sheet/refHighlight.ts from fsanyoto/loopable: direct link, hf CLI and curl.
- Browser
- Download file 3.21 kB
-
https://huggingface.co/spaces/fsanyoto/loopable/resolve/9291c0100f4e7b8da2efcbbf1ebdea72ee1663e7/web/src/sheet/refHighlight.ts
- Command line
-
hf download hf://spaces/fsanyoto/loopable@9291c0100f4e7b8da2efcbbf1ebdea72ee1663e7/web/src/sheet/refHighlight.ts
-
curl -L -o refHighlight.ts https://huggingface.co/spaces/fsanyoto/loopable/resolve/9291c0100f4e7b8da2efcbbf1ebdea72ee1663e7/web/src/sheet/refHighlight.ts
3.21 kB
| // Hotfix 2026-09-24, owner: "when a user ANYWHERE in the app must reference a cell, the cell must | |
| // be highlighted automatically ... This is the immutable rule everywhere a cell must be referenced." | |
| // | |
| // ONE store, so the rule is a property of the door rather than of each panel. Any surface that | |
| // asks a person for a cell reference calls `useCellRefHighlight(texts)` while it is mounted; the | |
| // grid reads `useHighlightedCellRefs()` and paints those cells through the SAME outline path a | |
| // formula's references use (`SheetGrid.tsx::formulaRefCells`), so there is no second border | |
| // mechanism to keep in step. Unmounting a panel takes its outlines off with it. | |
| // | |
| // ⛔ OUTLINE ON OPEN, SCROLL ONLY ON A CHANGE. A panel opened over a saved binding names every cell | |
| // it reads, including a far-away helper (Daily Sales F10000). Scrolling to the last one registered | |
| // would move the sheet the moment a cell is opened, so the grid scrolls only to a reference that | |
| // APPEARED after the surface mounted: the one the person just typed. | |
| import { useEffect, useRef, useSyncExternalStore } from "react"; | |
| const owners = new Map<number, readonly string[]>(); | |
| const listeners = new Set<() => void>(); | |
| const focusListeners = new Set<(text: string) => void>(); | |
| let snapshot: readonly string[] = []; | |
| let nextOwner = 1; | |
| const publish = () => { | |
| const seen = new Set<string>(); | |
| for (const texts of owners.values()) for (const text of texts) seen.add(text); | |
| snapshot = [...seen]; | |
| for (const listener of listeners) listener(); | |
| }; | |
| /** Every cell reference some mounted surface is asking about, as typed (bare or sheet-qualified). */ | |
| export const useHighlightedCellRefs = (): readonly string[] => useSyncExternalStore( | |
| (listener) => { listeners.add(listener); return () => { listeners.delete(listener); }; }, | |
| () => snapshot, | |
| () => snapshot, | |
| ); | |
| /** The grid's subscription to "a reference was just typed": it scrolls there. */ | |
| export const useCellRefFocus = (onFocus: (text: string) => void): void => { | |
| const latest = useRef(onFocus); | |
| latest.current = onFocus; | |
| useEffect(() => { | |
| const listener = (text: string) => latest.current(text); | |
| focusListeners.add(listener); | |
| return () => { focusListeners.delete(listener); }; | |
| }, []); | |
| }; | |
| const clean = (texts: readonly string[]) => | |
| texts.map((text) => String(text ?? "").trim()).filter(Boolean); | |
| /** Register the cell references a surface is showing. Blank or partial text is simply ignored | |
| * by the grid, which only outlines what parses as a reference. */ | |
| export const useCellRefHighlight = (texts: readonly string[]): void => { | |
| const key = clean(texts).join("\u0000"); | |
| const opened = useRef<Set<string> | null>(null); | |
| useEffect(() => { | |
| const now = key ? key.split("\u0000") : []; | |
| if (opened.current === null) opened.current = new Set(now); | |
| else { | |
| const typed = now.filter((text) => !opened.current!.has(text)); | |
| opened.current = new Set(now); | |
| if (typed.length) for (const listener of focusListeners) listener(typed[typed.length - 1]); | |
| } | |
| if (!key) return; | |
| const id = nextOwner++; | |
| owners.set(id, now); | |
| publish(); | |
| return () => { owners.delete(id); publish(); }; | |
| }, [key]); | |
| }; | |