# `Alternera.PluginWasmFramework.StepEditor`
[🔗](https://github.com/10e-ab/alternera-wasm/blob/v0.9.1/alternera_plugin_wasm_framework/lib/alternera/plugin_wasm_framework/step_editor.ex#L1)

The step editor every language step shares, laid out as Script's
editor is, so what a user knows of it carries over. A language plugin's
editor is a thin LiveComponent that hands its `%Language{}` and its
callbacks here, as Camel's hands its `%PluginHosts.StepEditor{}` to the
plugin hosts' editor:

    defmodule Alternera.Plugins.Javascript.Editor do
      use Phoenix.LiveComponent

      alias Alternera.Plugins.Javascript
      alias Alternera.PluginWasmFramework.StepEditor

      @impl true
      def update(assigns, socket),
        do: StepEditor.update(Javascript.language(), assigns, socket)

      @impl true
      def handle_event(event, params, socket),
        do: StepEditor.handle_event(Javascript.language(), event, params, socket)

      @impl true
      def handle_async(name, result, socket),
        do: StepEditor.handle_async(Javascript.language(), name, result, socket)

      @impl true
      def render(assigns), do: StepEditor.render(Javascript.language(), assigns)
    end

## What it shows

Top to bottom, every DOM id and input name starting with `prefix`:

  * **Help** (`<prefix>-help-section`, `Plugin.Editor.disclosure/1`,
    its state remembered per plugin), the language's own `help`
    component, given `direction` and `prefix`; none when `help` is nil.
  * **Module** (`<prefix>-module`), only for a language with
    `step_module?: true`: see "A module uploaded into the step" below.
  * **Configuration** (`Plugin.Editor.section_toggle/1`, `<prefix>-code`),
    holding the rest.
  * **Body** (`<prefix>-reads-body`): Full body or Head only, the
    `reads_body` setting (`set_reads_body`), with a note under it on
    what each holds. Absent means Full body, so only `false` is stored.
  * **Memory limit** (`<prefix>-memory-mode`): Flat or Body ×
    (`set_memory_mode`), and its number beside it, MiB under Flat
    (`<prefix>-memory-mib`), a multiple of the body under Body ×
    (`<prefix>-memory-factor`), reported as `memory_value`. Body × is
    disabled while Head only; switching to Head only from Body × also
    goes back to Flat. The `memory_limit` setting is dropped while it is
    the default, Flat 4 MiB (`MemoryLimit.default/0`); a typed value
    that is no whole number is stored as typed, so the save refuses it
    by name. The node's ceiling and Body ×'s floor are in the note
    under it (`<prefix>-memory-note`), and a refusal about the setting
    under that (`<prefix>-memory-error`).
  * `<prefix>-language`: the language's name.
  * **To** (`to_section/1`), as Script's: **Returned value**, one place
    for what the script returns or none (**Don't write it**,
    **Write it**), and **Named values**, each copied to a variable by a
    row of its own (**Copy a value to a variable**), through the To
    list's events (`targets`, `add_target`, `add_result`, `add_named`,
    `remove_target`, `TargetList`). The list is stored in the shape a
    step had before when it fits: nothing for the default row, a single
    `target` for one Result row elsewhere, `targets` otherwise, an
    empty `targets` when no row is left.
  * **Source** (`<prefix>-source`): the textarea under the proxy's code
    editor (the `LuaEditor` hook, `data-language` the language's
    `editor_mode`), reported as typed (`change`, debounced 500 ms).
    **Format** is offered when the language names a `format_worker`.
    The check's messages that are neither the memory limit's nor the
    To list's stand under it (`<prefix>-check`) while the save has
    refused nothing.

## A module uploaded into the step

For a step-module language (`Language`'s "A module uploaded into the
step") the editor's `mount/2` allows one upload, `:module` (`.wasm`, at
most the plugin's `upload_settings().max_bytes`, uploaded as soon as it
is picked). The step editor renders inside the pipeline editor's form,
and forms cannot nest, so the upload's own empty form
(`<prefix>-module-upload-form`, `module_upload_validate`) is teleported
to the end of the page (`Phoenix.Component.portal/1`) and the file
input names it with its `form` attribute.

  * Without a module: a drop zone (`<prefix>-module-drop`) with a
    **Pick a module** button.
  * With one: its card (`<prefix>-module-card`): the file's name, its
    size, who uploaded it and when (`Files.info/3`, read once per digest
    and kept), the short digest with a copy button for the whole
    `sha256:…`, and **Replace**, which opens the picker again; a module
    the tenant does not store says so.
  * A finished upload is read and handed to `Upload.accept/5` off the
    LiveView (`start_async(:accept_module, …)`), the card saying
    **Checking…** meanwhile. An accepted module is written as `module`
    (`%{"digest" => "sha256:<hex>", "name" => <file name>}`), keeping
    the `bindings` of the parameters it still declares; a refusal is
    shown in its own words and changes nothing.
  * **Use a stored module** (`<prefix>-stored-modules-toggle`, beside
    **Pick a module** and beside **Replace**, `stored_modules_toggle`)
    opens a list of the modules the tenant stores for this plugin
    (`<prefix>-stored-modules`, read with `Files.list/2` as it opens,
    newest first), one row each (`<prefix>-stored-module-<12 hex>`): the
    name the file was first stored under, its size, the short digest
    with a copy button, what it declares, who uploaded it and when, and
    the pipelines that pin it now. **Use** (`…-pick`,
    `stored_module_pick`) pins that module as a finished upload does,
    keeping the bindings it still declares, with no upload, check or
    compile: the tenant stores it already. The module the step pins
    now is marked **Current** and cannot be picked.
  * **What it uses** (`<prefix>-bindings`): one row per parameter the
    module declares (`Uses.params/1`, from the compiled module on this
    node, else from the declaration stored with the file), each with a
    picker of the tenant's stores, pipelines, variables or secrets
    (`<prefix>-bind-<kind>-<name>`, reported as `bind`), written to
    `bindings` (a cleared picker drops its name, an empty kind is
    dropped). A save's refusal naming a parameter stands under its row.
  * The source is the module's **Configuration text**, compiled-checked
    with the module itself when this node has it compiled
    (`Language.check_with/3`).

## Checks

Every change of the settings is checked at once with
`Language.load_step/2`, which checks them all without compiling the
source. A changed source is also compiled, off the LiveView
(`start_async(:check_source, …)` running `Language.check/2`), and so is
the source a step opens with and one the pipeline editor replaces
(which reaches the code editor as the `"lua_editor:set"` event); a
result for a source that is no longer the current one is dropped. The
messages that name a line (`line N: …`), from the save's `errors` when
there are any, else from both checks, go to the code editor as
`data-diagnostics`, a JSON list of `%{line, message}`, each line marked.

## Assigns

From the pipeline editor: `id`, `prefix`, `node_key`, `settings` (with
the section's side in `direction`), `errors`, `read_only`,
`json_paths`, `name_hints`, `step_id` and `notify`; it ignores the
others; a step-module language also reads `tenant_id`,
`current_user_id`, `store_names` and `pipelines`. New settings go back
with `Plugin.Editor.notify_step_settings_changed/2`, only when they
changed, holding the framework's keys alone (`direction`, `source`,
`target` or `targets`, `reads_body`, `memory_limit`, `module`,
`bindings`). A read-only editor refuses every event and logs it, and
offers no upload.

# `check`

```elixir
@spec check(Alternera.PluginWasmFramework.Language.t(), map()) :: [String.t()] | nil
```

What `Language.load_step/2` refuses in `settings`, nil for nothing:
the editor's check of the settings as they are edited.

# `diagnostics`

```elixir
@spec diagnostics([String.t()]) :: String.t()
```

The code editor's marks: every message naming a line (`line 3:
Unexpected token`), as JSON `[{"line": 3, "message": "Unexpected
token"}]`, `"[]"` without any.

# `handle_async`

The LiveComponent's `handle_async/3`: a compile check's result.

# `handle_event`

The LiveComponent's `handle_event/3`.

# `kept_bindings`

```elixir
@spec kept_bindings(term(), Alternera.PluginWasmFramework.Uses.t()) :: map()
```

The bindings in `bindings` of the parameters `uses` declares, the
others dropped, and any kind left empty.

# `mount`

The LiveComponent's `mount/1`: allows the module upload for a
step-module language, and leaves the socket as it is for any other.

# `put_binding`

```elixir
@spec put_binding(map(), String.t(), String.t(), String.t()) :: map()
```

`settings` with `param` of `kind` bound to `value`, trimmed; a blank
value unbinds it, and a kind left empty is dropped.

# `put_memory_mode`

```elixir
@spec put_memory_mode(map(), String.t(), pos_integer()) ::
  {:ok, map()} | {:error, String.t()}
```

`settings` with the memory mode `"flat"` (back to Flat 4 MiB, the key
dropped) or `"body"` (Body × at `factor`, the language's
`body_factor`). The mode already set stays as it is; Body × is refused
while Head only.

# `put_memory_value`

```elixir
@spec put_memory_value(map(), String.t(), String.t()) :: map()
```

`settings` with the memory limit's number (`"mib"` under Flat,
`"factor"` under Body ×) as typed (`Plugin.Editor.put_number/3`): a
whole number, else the text as typed, so the save refuses it by name.
Flat at its default, 4 MiB or blank, drops the setting.

# `put_reads_body`

```elixir
@spec put_reads_body(map(), boolean()) :: map()
```

`settings` with Full body (`true`: the key dropped, its default) or
Head only (`false`); Head only from Body × goes back to Flat 4 MiB too.

# `render`

The LiveComponent's `render/1`.

# `settings`

```elixir
@spec settings(term()) :: map()
```

The settings the editor keeps and sends: string keys, the framework's
keys only (`direction`, `source`, `target` or `targets`, `reads_body`,
`memory_limit`, and a step module's `module` and `bindings`), the
direction as given.

# `update`

The LiveComponent's `update/2`.

---

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