// The element: a sequence of steps, linear runs that a // branching step splits into outcomes, edited as a tree of cards. The page // declares the kinds of step and their fields; the value is JSON in a // textarea, which stays the form field of record and the no-JS fallback: // //
// // // // // // // //
// // The element hides the textarea and renders the tree after it: a "+" before // every step and at the end of every run opens a menu of the kinds; a card // shows a step's kind and a summary of its fields, with Edit (the kind's // controls, cloned from its template, Done / Cancel) and Remove. A new step // with fields opens its editor at once, and canceling that first edit takes // it back out. Removing a step puts its successor in its place (its next, or // its first non-empty branch); when that would discard other steps, the // first press asks and a second removes. One editor at a time: while it is // open the other controls wait. // // Every committed edit rewrites the textarea and emits "change" (bubbling, // detail { value }). The editor's own controls have no form owner, so they // are never posted and Enter in them does not submit; their input/change // events stay inside the element. If the form is serialized while an editor // is open (the page's Save pressed mid-edit), the open edit is included when // its controls are valid. A form reset restores the textarea and the tree. // // Words are the page's: the `label-*` attributes (add, edit, remove, // confirm, done, cancel, start, end), English by default. `readonly` renders // the tree without controls. import { computed, define, emit, For, fromSignal, keys, on, onCleanup, pipe, scan, Show, subject, subscribe, toSignal } from "@pebble/pebble"; import { DEV } from "./dev"; import { controlsOf, defaultsOf, readFields, readKinds, summarize, writeFields, type Kind } from "./kinds"; import { childAt, freshId, idsOf, isBranching, parse, ROOT, runOf, serialize, slotKey, slotOf, start, step as reduce, update, type Action, type Sequence, type Slot, type Step, } from "./model"; import styles from "./sequence.css"; export { parse, serialize, insert, remove, update, type Sequence, type Step, type Slot, type Fields, type Field } from "./model"; export { readFields, writeFields, readKinds, type Kind } from "./kinds"; /** The words the element shows, overridable with `label-` attributes. */ export const WORDS = { add: "Add a step", edit: "Edit", remove: "Remove", confirm: "Remove with the steps it leads to", done: "Done", cancel: "Cancel", start: "Start", end: "End", }; /** An id no form has: the editor's controls point their `form` at it, so they have no form owner. */ const NO_FORM = "pebble-sequence-no-form"; define("pebble-sequence", { shadow: false, scoped: false, styles }, (_props, host) => { const field = [...host.children].find((child): child is HTMLTextAreaElement => child instanceof HTMLTextAreaElement); const kinds = readKinds(host); if (!field || kinds.size === 0) { if (DEV) console.warn(": needs a