# `Macaw.Live.Shortcuts`
[🔗](https://github.com/dcoai/macaw_live/blob/v0.1.0/lib/macaw/live/shortcuts.ex#L1)

The page-shortcut registry and its dialog (spec/05 §6). Page-wide shortcuts go only
through it: each mounted cell's declared shortcuts (`%{key, action, description}`,
macaw_component spec/01 §1.6), each entry identified as `"<instance>.<action>"`, and the
runtime's own help entry, `"mw-live.help"` on `?`.

- **Built at mount** by `build/1`: two shortcuts on one key across the page refuse the
  mount. Until the core adds a placement's acceptance list (macaw#165), every declared
  shortcut is accepted.
- **The viewer controls every single-character shortcut** through the `shortcuts`
  preference, a map from an entry's id to its key, or `nil` for off: `active/2` is the
  registry with it applied, and `change/4` refuses a remap onto a key already in use,
  the preference unchanged.
- **The dialog**, `dialog/1` (`<Macaw.Live.shortcuts macaw={@macaw} />`), lists every
  active shortcut for assistive technology and for every viewer, as data attributes the
  live module reads; it holds no JSON.

# `entry`

```elixir
@type entry() :: %{
  id: String.t(),
  instance: String.t(),
  action: String.t(),
  key: String.t(),
  description: String.t()
}
```

One shortcut of the page: its id, the instance it is for, its action, key and description.

# `preference`

```elixir
@type preference() :: %{required(String.t()) =&gt; String.t() | nil}
```

The viewer's `shortcuts` preference: an entry's id to its key, or `nil` for off.

# `t`

```elixir
@type t() :: %Macaw.Live.Shortcuts{entries: [entry()]}
```

The page's registry: every entry as declared, by id.

# `active`

```elixir
@spec active(t(), preference()) :: [entry()]
```

The registry's entries with the viewer's preference applied (spec/05 §6), in id order:
a single-character entry the preference maps to `nil` is off and absent, one it maps to
another key takes it; every other entry keeps its declared key. A preference naming an
entry the registry doesn't hold changes nothing.

# `build`

```elixir
@spec build([{String.t(), [map()]}] | %{required(String.t()) =&gt; [map()]}) ::
  {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The registry of a page's mounted cells (spec/05 §6): `entries` is each instance with the
shortcuts its cell declares, `[{instance, [%{key, action, description}]}]` or a map of
the same. `{:ok, registry}`, its entries the help entry's and every cell's, in id order;
or `{:error, faults}`: `{[:shortcuts, key], {:duplicate_shortcut, key, [id, id]}}` for each
key two or more entries share, their ids in order, and `{[:shortcuts], {:type, :list}}`
for entries of another shape.

# `change`

```elixir
@spec change(t(), preference(), String.t(), String.t() | nil) ::
  {:ok, preference()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The viewer's change to one entry (spec/05 §6): `key` a single character to remap it, or
`nil` to turn it off. `{:ok, preference}`, or `{:error, faults}` with the preference
unchanged: `{[:shortcuts, id], :unknown}` for an id the registry doesn't hold,
`{[:shortcuts, id], :not_single_character}` for an entry whose declared key is a named
key, which the viewer doesn't control, `{[:shortcuts, id], {:type, :key}}` for a key that
is no single character, and `{[:shortcuts, id], {:key_in_use, key, other}}` for a remap
onto a key another active entry has.

# `dialog`

```elixir
@spec dialog(map()) :: Phoenix.LiveView.Rendered.t()
```

The shortcuts dialog (spec/05 §6), placed once by the host as
`<Macaw.Live.shortcuts macaw={@macaw} />`: a `<dialog id="mw-shortcuts">` listing each
active shortcut as a `<dt><kbd>` key and a `<dd>` description, each entry carrying
`data-mw-shortcut` (its id), `data-mw-key`, `data-mw-for` (the instance) and
`data-mw-action`. A single-character entry has a control to turn it off and a form to
remap it, each a form sending `"mw-shortcut"` (with no `key` to turn it off); an entry
turned off is listed with a control to turn it on again and no key. The dialog opens on `?` and holds no JSON.

## Attributes

* `macaw` (`Macaw.Live.Mounted`) (required) - the `@macaw` assign mount/3 set.

# `help`

```elixir
@spec help() :: entry()
```

The runtime's own entry, the help dialog's, on `?` (spec/05 §6).

# `off`

```elixir
@spec off(t(), preference()) :: [entry()]
```

The registry's off entries under the preference: each single-character entry it turns
off, in id order, as declared, so the dialog can offer to turn it on again.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
