Wraith.Widgets (Wraith v0.1.0)

View Source

The widget library and the live state of every embedded instance.

A widget definition is an item: slug, name, description, kind ("lua" or "elixir"), source (Lua) or module (Elixir), props (documentation of the props it takes), enabled, version. Every save of a Lua widget writes a numbered version (VER#n) so the studio can roll back. The built-in Lua widgets ship in priv/widgets/*.lua and are seeded on boot when missing (seed_builtins/0); the Elixir ones are the modules in builtin_modules/0.

An instance is {slug, instance_id}; its state lives at WSTATE#slug#instance and changes are broadcast on "widget:" <> instance_id as {:widget, slug, instance_id, state}.

Summary

Functions

A reader's event: run handle, save and broadcast the state. {:ok, state} | {:error, message}. The save is a read-modify-write, so two readers clicking at once both count.

This visitor's private slice of an instance (a map; empty when none).

Render an instance: {:ok, html} | {:error, message}.

Make an earlier version the current source (writes a new version).

Allowlist-sanitise widget HTML: MDEx.safe_html/2 (ammonia) for tags, attributes and URLs, then a second pass that pins every phx-click / phx-submit to the value "widget". The sanitiser can allow an attribute but not reliably constrain its value; a widget that could emit phx-click="comment-delete" would be clicking the reader's own buttons.

Create or replace a widget. Lua source is checked first; a change writes a version.

Seed the built-ins that are missing. Idempotent; run at boot.

The state of an instance (initialised from props when it has none).

The refresh interval a Lua widget asks for (tick = N in its chunk), in seconds, or nil.

Functions

builtin_modules()

delete(slug)

dispatch(widget, instance, props, event, payload, meta)

A reader's event: run handle, save and broadcast the state. {:ok, state} | {:error, message}. The save is a read-modify-write, so two readers clicking at once both count.

get(slug)

list()

list_enabled()

private(slug, instance, visitor)

This visitor's private slice of an instance (a map; empty when none).

render(widget, instance, props, state, meta)

Render an instance: {:ok, html} | {:error, message}.

reset_state(slug, instance)

rollback(slug, n)

Make an earlier version the current source (writes a new version).

sanitize(html)

Allowlist-sanitise widget HTML: MDEx.safe_html/2 (ammonia) for tags, attributes and URLs, then a second pass that pins every phx-click / phx-submit to the value "widget". The sanitiser can allow an attribute but not reliably constrain its value; a widget that could emit phx-click="comment-delete" would be clicking the reader's own buttons.

save(attrs)

@spec save(map()) :: {:ok, map()} | {:error, map()}

Create or replace a widget. Lua source is checked first; a change writes a version.

seed_builtins()

Seed the built-ins that are missing. Idempotent; run at boot.

set_enabled(slug, enabled?)

state(widget, instance, props)

The state of an instance (initialised from props when it has none).

subscribe(instance)

tick(arg1)

The refresh interval a Lua widget asks for (tick = N in its chunk), in seconds, or nil.

topic(instance)

unsubscribe(instance)

versions(slug)