File size: 5,417 Bytes
1d4419b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82cdb59
 
 
 
 
1d4419b
 
816fa8a
1d4419b
 
 
 
 
 
 
 
 
 
 
 
 
816fa8a
1d4419b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce9586e
 
1d4419b
 
 
 
82cdb59
816fa8a
82cdb59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d4419b
 
82cdb59
 
 
 
 
1d4419b
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// sheet/ShortcutsPanel.tsx β€” W51-T17, the Ctrl+/ panel (ruling R7, contract C8).
//
// ⭐⭐ THIS FILE CONTAINS NO CHORDS. Every row it renders comes from `shortcuts.ts::SHORTCUTS`,
// the same table `SheetGrid.tsx::onKeyDown` matches against, so the panel cannot advertise a key
// the handler does not answer and the handler cannot answer a key the panel does not show.
// `verify_api.py` asserts BOTH halves of that: this file maps over the table, and no chord shaped
// string literal survives in its own source. A hand written list that happens to agree today is
// exactly the defect the clause exists to stop, and it would agree right up until somebody added
// the thirty first shortcut.
//
// β›” IT IS A REACT COMPONENT AND IS NEVER NODE COMPILED, which is why it MAY import the base kit
// (`../ui/useEscapeClose`). `shortcuts.ts` may not: tsc's emitted root moves with the import
// graph and the Sheet's node proofs `require` `sheetState.js` at the top of the out directory.
//
// ⭐⭐ CONTRACT C8, AMENDMENT A2 β€” THE ESCAPE DECISION, MADE DELIBERATELY AND SAID OUT LOUD.
// A2 makes "first Escape cancels a live text edit, the second closes" OPT IN per overlay and
// names the Sheet's right side panels as the family that opts in. This is one of those panels, so
// it opts IN and passes its own `container`, which is what lets the arbiter decide whether the
// focus is inside it at all. In practice the option is inert here, because the panel holds no
// text input to cancel; opting in keeps the family's rule true rather than leaving one member of
// it behaving differently for a reason nobody would remember.
//
// β›” THERE IS NO SECOND ESCAPE LISTENER. `useEscapeClose` is the one arbiter, topmost wins, and a
// `document.addEventListener("keydown", …)` added here would race it and win or lose depending on
// registration order.
//
// ⚠ DESIGN CONTRACT: sentence case, scale tokens only and no literal font-size, tokens never
// hues, inline SVG on `currentColor` and no emoji, 150 ms ease-out on opacity and transform, and
// no em dash or en dash in any string that reaches a screen.

import { useRef } from "react";
import { useEscapeClose } from "../ui/useEscapeClose";
import { effectiveKeys, SHORTCUTS } from "./shortcuts";
// W52-T19 -- the `.sheet-shortcuts-keys--off` rule only; see that file's own header for why a
// component-owned side-effect import is this repo's existing way to add a wave-52 rule without
// editing `index.css` or another lane's `styles/wave51/sheet-engine.css`.
import "../styles/wave52/ribbon.css";

export interface ShortcutsPanelProps {
  storagePrefix?: string;
  open: boolean;
  onClose: () => void;
}

/** The close cross. An inline SVG on `currentColor`, never a glyph and never an emoji. */
function IconClose() {
  return (
    <svg viewBox="0 0 16 16" aria-hidden="true" className="sheet-shortcuts-icon">
      <path d="M4.5 4.5l7 7M11.5 4.5l-7 7" />
    </svg>
  );
}

export default function ShortcutsPanel({ open, onClose, storagePrefix = "" }: ShortcutsPanelProps) {
  const boxRef = useRef<HTMLDivElement | null>(null);
  useEscapeClose(open, onClose, {
    cancelEditFirst: true,
    container: () => boxRef.current,
  });
  if (!open) return null;

  return (
    <div
      className="sheet-shortcuts"
      role="dialog"
      aria-label="Keyboard shortcuts"
      ref={boxRef}
    >
      <div className="sheet-shortcuts-head">
        <h2 className="sheet-shortcuts-title">Keyboard shortcuts</h2>
        <button
          type="button"
          className="sheet-shortcuts-close"
          aria-label="Close the shortcuts list"
          title="Close the shortcuts list"
          onMouseDown={(e) => e.preventDefault()}
          onClick={onClose}
        >
          <IconClose />
        </button>
      </div>

      {/* S68-T28: Paste Special is included by the same table and effectiveKeys as every row.
          ⭐⭐ ONE ROW PER TABLE ENTRY, AND NOTHING ELSE. No filter, no slice and no second list:
          the panel's contents ARE the table, which is the only shape in which the two cannot
          drift. `when` is shown because a shortcut that needs a selection and one that does not
          are different promises, and a panel that hides the difference teaches the wrong thing. */}
      <ul className="sheet-shortcuts-list">
        {SHORTCUTS.map((entry) => {
          const keys = effectiveKeys(entry.id, storagePrefix);
          const disabled = keys === "Disabled";
          return (
            <li className="sheet-shortcuts-row" key={entry.id}>
              <kbd
                className={
                  "sheet-shortcuts-keys" + (disabled ? " sheet-shortcuts-keys--off" : "")
                }
              >
                {disabled ? "Disabled" : keys}
              </kbd>
              <span className="sheet-shortcuts-action">{entry.action}</span>
              <span className="sheet-shortcuts-when">{entry.when}</span>
            </li>
          );
        })}
      </ul>

      {/* Owner instruction 21 / ruling R7 β€” the deliberate deviation, said out loud. */}
      <p className="sheet-shortcuts-foot">
        Rebinding your own shortcuts is a departure from Excel, which does not allow it. Change
        them under Settings, Sheet shortcuts.
      </p>
      <p className="sheet-shortcuts-foot">
        Press Escape to close this list.
      </p>
    </div>
  );
}