# Elements

The reference of the three custom elements, generated from `packages/element/api.ts` (do not
edit it by hand: `node packages/element/api.ts --write`). `code-playgrounds/element/define` defines
them under these names; `code-playgrounds/element` exports the classes alone. How to use them is in
the README; the look, with every custom property and how to add a stylesheet, is in
[styling.md](https://playgroundkit.github.io/docs/styling.md). A member that is not listed here is not
part of the API.

Every element answers before its code has loaded: attributes and their properties work at once,
`queued` methods (`mark`, `seed`, `stop`, …) run when the implementation arrives, and the
others return a promise that waits for it.

## `<code-playground>`

An editor for one language with a toolbar and its outputs. Its children are templates for `code`, `setup`, `prelude`, `tests` and `example`s (`<template data-code>`, …). It makes a model (`playground`) that the other two elements can show.

### Attributes

| Attribute | Meaning |
|---|---|
| `language` : `string` | The language package: a name from the package index, or an alias. |
| `base` : `string` | Where the packages are served from; the default is beside the built element. |
| `scope` : `string` | Playgrounds share one engine (one loaded runtime and language server) only when their language, base and scope are equal; with no scope, all of a language's playgrounds on the page share it. A scope shares no definitions: every run is a fresh program that sees neither another run nor another playground's code. Give playgrounds whose authors do not trust each other different scopes (docs/plan.md D63). |
| `chrome` : `string` | `none` is the editor alone, with no toolbar, tabs or outputs. |
| `tabs` : `string` | The outputs the tabs show: ids separated by spaces, `all` (default) or `none`. |
| `live` : `string` | Which outputs follow the code: `shown` (default), `all`, `none`, or ids. |
| `toolbar` : `string` | The toolbar's items, from `run stop reset examples keymap`, separated by spaces. |
| `readonly` : `boolean` | The reader cannot edit. |
| `keymap` : `string` | `vim` for Vim's key bindings; otherwise the default. |
| `start` : `string` | `edit` or `focus` starts the language server on the reader's first edit or focus, not only on Run. |
| `label` : `string` | The accessible name of the editor. |
| `disable` : `string` | Editor features to turn off, separated by spaces: `semantic-tokens`, `signature-help`, `inlay-hints`, `completion`, `hover`. |
| `share` : `string` | A name under which `#<name>=…` in the address pre-fills this playground. |
| `storage` : `string` | Where edits are kept: `none` (default), `local`, `session`, `indexeddb`. |
| `storage-key` : `string` | The key edits are kept under; by default the page and the share name. |
| `run-timeout` : `number` | Milliseconds before a run is stopped. |
| `precompute` : `boolean` | Outputs computed at build time are in a `<template data-outputs>`. |
| `outputs` : `string` | Deprecated: use `tabs`. |
| `unstyled` : `boolean` | Leaves out the default look of this element and its editor; what function needs stays (docs/styling.md). |

### Properties

| Property | Meaning |
|---|---|
| `code` : `string` | The program. |
| `setup` : `string` | Code run before the program, not shown. |
| `prelude` : `string` | Code shown before the program. |
| `tests` : `string` | Checks run with the program. |
| `examples` : `Example[]` | The page's own examples; the package's are `packageExamples()`. |
| `live` : `Live` | As the attribute. |
| `storage` : `StorageOption` | As the attribute. |
| `playground` : `Playground \| undefined` | The model; undefined until the element has made it. |
| `state` : `PlaygroundState` | `engine`, `server`, `editor`: `loading` before the chunk. |
| `outputs` : `OutputInfo[]` | The package's outputs. |
| `diagnostics` : `Diagnostic[]` | The language server's current diagnostics. |
| `selection` : `Range \| undefined` | The editor's selection. |
| `marked` : `Range[]` | Ranges marked with `mark()`. |
| `linked` : `Range[]` | Ranges linked to a hovered item. |
| `ready` : `Promise<void>` | Settles when the model is made and its stored code loaded. |
| `language` : `string` | As the attribute. |
| `base` : `string` | As the `base` attribute. |
| `scope` : `string` | As the `scope` attribute. |
| `chrome` : `string` | As the `chrome` attribute. |
| `tabs` : `string` | As the `tabs` attribute. |
| `toolbar` : `string` | As the `toolbar` attribute. |
| `keymap` : `string` | As the `keymap` attribute. |
| `start` : `string` | As the `start` attribute. |
| `label` : `string` | As the `label` attribute. |
| `disable` : `string` | As the `disable` attribute. |
| `share` : `string` | As the `share` attribute. |
| `precompute` : `boolean` | As the `precompute` attribute. |
| `readOnly` : `boolean` | As the `readonly` attribute. |
| `storageKey` : `string` | As the `storage-key` attribute. |
| `runTimeout` : `number` | As the `run-timeout` attribute. |
| `unstyled` : `boolean` | As the `unstyled` attribute. |

### Methods

| Method | Meaning |
|---|---|
| `run` : `(output?: string \| string[], options?: RunOptions) => Promise<RunInfo>` | Runs, and resolves with the run's result. |
| `act` : `(output: string, item: number, action: string) => Promise<void>` | An item's action (a proof step, for example). |
| `activate` : `() => Promise<void>` | Starts the engine and the editor now. |
| `insertText` : `(text: string) => Promise<void>` | Inserts at the cursor. |
| `hover` : `(offset: number) => Promise<Hover \| null>` | The language server's hover. |
| `inlayHints` : `(range?: Range) => Promise<InlayHint[]>` | The language server's inlay hints. |
| `reset` : `() => Promise<void>` | Restores the original code. |
| `shareLink` : `() => Promise<string>` | An address that pre-fills the playground with the code. |
| `flush` : `() => Promise<void>` | Writes the code to storage now. |
| `clearStored` : `() => Promise<void>` | Forgets the stored code. |
| `openKey` : `(key: string) => Promise<void>` | Switches to the code stored under another key. |
| `packageExamples` : `() => Promise<Example[]>` | The package's examples. |
| `mark` : `(ranges: Range[]) => void` | Marks ranges of the code. |
| `link` : `(ranges: Range[]) => void` | Links ranges as an item does. |
| `seed` : `(snapshot: RunSnapshot) => void` | Shows an output from outside: one a build computed. |
| `stop` : `() => void` | Stops a run. |
| `cancel` : `() => void` | Cancels pending work. |
| `applyChanges` : `(changes: Change[]) => void` | Applies remote changes to the code. |
| `focus` : `(options?: FocusOptions) => void` | Focuses the editor. |
| `output` : `(id: string) => RunInfo` | An output's last result: idle until it ran. |
| `isLive` : `(id: string) => boolean` | Whether the output follows the code. |
| `isStale` : `(id: string) => boolean` | Whether the output shows an earlier version of the code. |
| `tokens` : `() => Token[]` | The grammar's tokens of the code. |
| `progressOf` : `(id: string) => Progress` | An output's progress. |

### Events

| Event | Meaning |
|---|---|
| `playground-change` : `{ changes, code, by }` | The code changed; `by` is who changed it. |
| `playground-state` : `PlaygroundState` | The engine, server or editor changed state. |
| `playground-items` : `ItemsDetail` | An output's items changed. |
| `playground-diagnostics` : `Diagnostic[]` | The diagnostics changed. |
| `playground-link` : `{ ranges }` | Ranges were linked or unlinked. |

### States (`:state()`)

| State | Meaning |
|---|---|
| `loading` | The package is loading. |
| `failed` | The package or the engine failed. |
| `idle` | The package is loaded; the engine has not started. |
| `activating` | The engine is loading or starting. |
| `ready` | The engine is ready. |
| `restarting` | The engine is restarting. |
| `running` | A run is in progress. |
| `stale` | An output shows an earlier version of the code. |
| `server-starting` | The language server is starting. |
| `server-ready` | The language server is ready. |
| `fallback` | A fallback variant of the package runs. |
| `edited` | The code differs from the original. |

### Parts (`::part()`)

| Part | Is |
|---|---|
| `root` | The outer container: restyle or re-lay it out here (`display: grid` puts the editor and the output side by side). |
| `toolbar` | The toolbar. |
| `language` | The language's name in the toolbar. |
| `run` | The Run button. |
| `stop` | The Stop button, while a run is in progress. |
| `reset` | The Reset button. |
| `examples` | The examples menu. |
| `keymap` | The Vim keys checkbox and its label. |
| `note` | A note in the toolbar (`JavaScript fallback`). |
| `status` | A line of status: the engine's, a failure's, a run's progress. |
| `editor` | The box that holds the editor, or the static code before the editor loads. |
| `editor-view` | CodeMirror's own box, inside the editor: the editor's background and border. |
| `editor-scroller` | The editor's scrolling area, gutters and lines. |
| `editor-content` | The editor's lines. |
| `editor-gutters` | The editor's gutters. |
| `editor-line-numbers` | The line numbers' column. |
| `editor-tooltip` | A tooltip: hover, diagnostics, completion. |
| `prelude-toggle` | The button that folds the prelude. |
| `tabs` | The row of tabs. |
| `tab` | One tab. |
| `output` | The selected output. |
| `placeholder` | The text in an output that has not run. |
| `item` | One item's container. |
| `item-text` | A plain text item. |
| `item-ansi` | Text with terminal colours. |
| `item-code` | A highlighted code item. |
| `item-tree` | A tree. |
| `item-table` | A table. |
| `item-diagnostics` | A list of diagnostics. |
| `item-verdicts` | A list of verdicts. |
| `item-graph` | A graph, drawn as SVG. |
| `item-svg` | An SVG image. |
| `item-custom` | An item the language's renderer draws. |
| `actions` | The row of an item's action buttons. |
| `action` | An item's action button. |
| `graph-save` | A graph's download button. |

### Commands (`commandfor`)

| Command | Meaning |
|---|---|
| `--run` | Runs. |
| `--activate` | Activates. |
| `--stop` | Stops. |
| `--reset` | Resets. |

## `<code-playground-output>`

Shows one output of a `code-playground`, anywhere on the page. It finds the playground by `for` (an id in the same tree) or by its `playground` property.

### Attributes

| Attribute | Meaning |
|---|---|
| `for` : `string` | The id of the `code-playground` to show. |
| `output` : `string` | The output to show; the first by default. |
| `controls` : `boolean` | A run button and the status. |
| `chrome` : `string` | `full` or `none`. |
| `unstyled` : `boolean` | Leaves out the default look; what function needs stays. |

### Properties

| Property | Meaning |
|---|---|
| `playground` : `Playground \| Element` | The model or its element. |
| `snapshot` : `RunInfo` | What is shown. |
| `htmlFor` : `string` | As `for`. |
| `output` : `string` | As the attribute. |
| `controls` : `boolean` | As the attribute. |
| `chrome` : `string` | As the attribute. |
| `unstyled` : `boolean` | As the attribute. |

### Events

| Event | Meaning |
|---|---|
| `playground-bind` : `{ playground }` | It found, or lost, its model. |
| `playground-render` : `{ width, height }` | Its content's size changed. |

### States (`:state()`)

| State | Meaning |
|---|---|
| `bound` | It has a model. |
| `stale` | It shows an earlier version of the code. |
| `seeded` | It shows a seeded output. |

### Parts (`::part()`)

| Part | Is |
|---|---|
| `root` | The outer container. |
| `output` | The output. |
| `toolbar` | The toolbar, with `controls`. |
| `title` | The output's title in the toolbar. |
| `run` | The Run button. |
| `refresh` | The Refresh button of an output that runs on demand. |
| `stop` | The Stop button, while a run is in progress. |
| `status` | A line of status. |
| `placeholder` | The text in an output that has not run. |
| `item` | One item's container. |
| `item-text` | A plain text item. |
| `item-ansi` | Text with terminal colours. |
| `item-code` | A highlighted code item. |
| `item-tree` | A tree. |
| `item-table` | A table. |
| `item-diagnostics` | A list of diagnostics. |
| `item-verdicts` | A list of verdicts. |
| `item-graph` | A graph, drawn as SVG. |
| `item-svg` | An SVG image. |
| `item-custom` | An item the language's renderer draws. |
| `actions` | The row of an item's action buttons. |
| `action` | An item's action button. |
| `graph-save` | A graph's download button. |

## `<code-playground-items>`

Draws output items: of a `code-playground` (by `for`), or the ones it is given in `items` or `snapshot`, with no model at all.

### Attributes

| Attribute | Meaning |
|---|---|
| `for` : `string` | The id of the `code-playground` to show. |
| `output` : `string` | The output to show. |
| `chrome` : `string` | `full` or `none`. |
| `readonly` : `boolean` | Items offer no actions. |
| `language` : `string` | The language whose renderers draw the items, with no playground. |
| `base` : `string` | Where the packages are served from. |
| `unstyled` : `boolean` | Leaves out the default look; what function needs stays. |

### Properties

| Property | Meaning |
|---|---|
| `playground` : `Playground \| Element` | The model or its element. |
| `snapshot` : `RunInfo` | What to draw. |
| `items` : `Item[]` | What to draw, as bare items. |
| `htmlFor` : `string` | As `for`. |
| `output` : `string` | As the attribute. |
| `chrome` : `string` | As the attribute. |
| `readOnly` : `boolean` | As the `readonly` attribute. |
| `language` : `string` | As the attribute. |
| `base` : `string` | As the attribute. |
| `unstyled` : `boolean` | As the attribute. |

### Events

| Event | Meaning |
|---|---|
| `playground-bind` : `{ playground }` | It found, or lost, its model. |
| `playground-render` : `{ width, height }` | Its content's size changed. |

### States (`:state()`)

| State | Meaning |
|---|---|
| `empty` | There is nothing to draw. |
| `bound` | It has a model. |
| `live` | Its output follows the code. |
| `stale` | It shows an earlier version of the code. |

### Parts (`::part()`)

| Part | Is |
|---|---|
| `root` | The outer container. |
| `items` | The list of items. |
| `item` | One item's container. |
| `item-text` | A plain text item. |
| `item-ansi` | Text with terminal colours. |
| `item-code` | A highlighted code item. |
| `item-tree` | A tree. |
| `item-table` | A table. |
| `item-diagnostics` | A list of diagnostics. |
| `item-verdicts` | A list of verdicts. |
| `item-graph` | A graph, drawn as SVG. |
| `item-svg` | An SVG image. |
| `item-custom` | An item the language's renderer draws. |
| `actions` | The row of an item's action buttons. |
| `action` | An item's action button. |
| `graph-save` | A graph's download button. |
