import { useState } from "react"; import type { ComponentProps, KeyboardEvent } from "react"; import { AnchoredOverlay } from "./OverlaySurface"; import { isDerivedLink, isMachineWritten, mayOverrideSourceValue } from "./types"; import type { Field } from "./types"; import "../styles/sdlc80/cell-note.css"; /** A pre-set definition lock never decides whether a cell can carry a note. */ export function cellNoteRefusal(field: Field, editable: boolean, embedded: boolean): string | undefined { if (embedded) return "This table is view only."; if (editable) return undefined; if (field.source !== "overlay" && !mayOverrideSourceValue(field)) return "This source field is read only."; if (["formula", "created_time", "rollup"].includes(field.type) || isDerivedLink(field)) return "This calculated cell is read only."; if (isMachineWritten(field)) return "This cell is filled by an automation."; return "Your access does not allow editing this field."; } export function drawCellNoteMarker(ctx: CanvasRenderingContext2D, rect: { x: number; y: number; width: number }, color: string): void { ctx.save(); ctx.fillStyle = color; ctx.beginPath(); ctx.moveTo(rect.x + rect.width - 9, rect.y + 2); ctx.lineTo(rect.x + rect.width - 2, rect.y + 2); ctx.lineTo(rect.x + rect.width - 2, rect.y + 9); ctx.closePath(); ctx.fill(); ctx.restore(); } export default function CellNoteMenu({ anchor, note, refusal, onSave, onClose }: { anchor: ComponentProps["anchor"]; note: string | null; refusal?: string; onSave: (text: string | null) => void; onClose: () => void; }) { const [editing, setEditing] = useState(false); const [draft, setDraft] = useState(note ?? ""); const label = note ? "Edit note" : "Add note"; const walk = (event: KeyboardEvent) => { if (editing || !["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) return; const buttons = [...event.currentTarget.querySelectorAll("[role=menuitem]:not(:disabled)")]; const at = buttons.indexOf(document.activeElement as HTMLButtonElement); const next = event.key === "Home" ? 0 : event.key === "End" ? buttons.length - 1 : event.key === "ArrowDown" ? at + 1 : at < 0 ? buttons.length - 1 : at - 1; event.preventDefault(); buttons[(next + buttons.length) % buttons.length]?.focus(); }; return {editing ?
{ event.preventDefault(); if (!refusal) onSave(draft.trim() || null); }}>