File size: 3,674 Bytes
7f72224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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<typeof AnchoredOverlay>["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<HTMLElement>) => {
    if (editing || !["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) return;
    const buttons = [...event.currentTarget.querySelectorAll<HTMLButtonElement>("[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 <AnchoredOverlay anchor={anchor} className="cg-pop cg-cell-note-pop" placement="bottom-start"
    role={editing ? "dialog" : "menu"} ariaLabel={editing ? label : "Cell note"}
    dismissOnOutside={!editing} onDismiss={onClose} onKeyDown={walk}>
    {editing ? <form className="cg-cell-note-editor" onSubmit={(event) => {
      event.preventDefault();
      if (!refusal) onSave(draft.trim() || null);
    }}>
      <label>{label}<textarea rows={4} aria-label="Cell note" autoFocus value={draft} disabled={!!refusal}
        onChange={(event) => setDraft(event.currentTarget.value)} /></label>
      <div className="cg-cell-note-actions">
        <button type="submit" disabled={!!refusal || (!note && !draft.trim())}>Save</button>
        <button type="button" onClick={onClose}>Cancel</button>
      </div>
    </form> : <>
      <button type="button" role="menuitem" className="cg-cell-note-item" disabled={!!refusal}
        title={refusal} onClick={() => setEditing(true)}>{label}</button>
      {note && <button type="button" role="menuitem" className="cg-cell-note-item" disabled={!!refusal}
        title={refusal} onClick={() => onSave(null)}>Delete note</button>}
    </>}
    {refusal && <p className="cg-cell-note-refusal" role="status">{refusal}</p>}
  </AnchoredOverlay>;
}