Macaw.Live.Shortcuts (macaw_live v0.1.0)

Copy Markdown View Source

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.

Summary

Types

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

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

t()

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

Functions

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.

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.

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.

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.

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

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.

Types

entry()

@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()

@type preference() :: %{required(String.t()) => String.t() | nil}

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

t()

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

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

Functions

active(shortcuts, preference)

@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(entries)

@spec build([{String.t(), [map()]}] | %{required(String.t()) => [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(registry, preference, id, key)

@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(assigns)

@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

help()

@spec help() :: entry()

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

off(shortcuts, preference)

@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.