tobogganSe connecter

library

Prebuilt pebble components: drop-in custom elements, no compiler needed

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

Dernier runAucun
0Étoiles
0Forks
2Issues
103 lignes · 8430 octetsBrut · Historique

Sequence (<pebble-sequence>)

A sequence of steps edited as a tree of cards: linear runs, and branching steps whose outcomes start runs of their own. 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. Self-registering, light DOM; its few words are the page's to replace.

A port of the sequence editor in future-foundation (src/components/tree), rebuilt the pebble way and reduced to its core. That editor sent every edit to the server. This one edits a value locally, and the page posts it with its own form.

<link rel="stylesheet" href="/static/pebble/sequence.css">
<script type="module" src="/static/pebble/sequence.js"></script>

<form method="post" action="/campaigns/42/sequence">
  <pebble-sequence>
    <template data-kind="invite" data-label="Send an invitation"></template>
    <template data-kind="wait" data-label="Wait" data-summary="{days} days">
      <label>Days <input name="days" type="number" min="1" value="3" required></label>
    </template>
    <template data-kind="message" data-label="Send a message">
      <label>Message <textarea name="text" required maxlength="300"></textarea></label>
    </template>
    <template data-kind="connected" data-label="Accepted?" data-branches="Yes|No">
      <label>Give up after (days) <input name="days" type="number" min="1" value="7"></label>
    </template>
    <textarea name="sequence">{"root":null}</textarea>
  </pebble-sequence>
  <button>Save</button>
</form>

The markup

wherewhatnotes
<template data-kind="…"> childrenthe kinds of stepdata-kind is the name stored in the value. data-label is the card's title and the menu entry (default: the name). data-branches="Yes|No" makes the kind branching, with one outcome per name, in order. data-summary="{days} days" is the card's summary line, with {field} holes (default: the field values joined with ·).
a kind's contentits editorOrdinary labelled form controls, named after the fields. Constraint attributes (required, min, maxlength, …) apply. Their initial values are a new step's defaults. A kind with no named controls has no editor.
the <textarea> childthe valueRequired, and so is at least one kind. Its name is what the form posts. The element hides it and puts it back on disconnect.
host label-*the element's wordslabel-add ("Add a step"), label-edit ("Edit"), label-remove ("Remove"), label-confirm ("Remove with the steps it leads to"), label-done ("Done"), label-cancel ("Cancel"), label-start ("Start"), label-end ("End")
host readonlyno controlsThe tree is drawn without "+", Edit or Remove.

A malformed value, a missing textarea or no kinds: the element declines to upgrade, leaves the textarea as it is, and warns in development.

The value

{ "root": Step | null }. A step is linear or branching:

{ "id": "s1", "kind": "invite", "fields": {}, "next": { … } | null }
{ "id": "s2", "kind": "connected", "fields": { "days": 7 }, "branches": [ { … } | null, { … } | null ] }
  • id is stable across edits, so a server can key state to a step (a journey's current step, for one). New steps get s followed by six base-36 characters. A step the server sends without an id gets one at upgrade.
  • fields holds JSON types. A number or range input gives a number (null when empty). A checkbox gives a boolean. A radio group gives the checked one's value (null when none). Any other control gives its text. A field the step lacks leaves its control at the kind's default.
  • branches has one slot per outcome the kind names, padded with null. Branches the data has beyond that are kept, never dropped.
  • root sits inside a wrapper object, which leaves room for later additions (detached fragments, a version) without breaking a decoder.
  • Key order is fixed: id, kind, fields, then next or branches. The server's initial text is left untouched until the first edit.
  • Unknown kinds: a step whose kind has no template is drawn with its kind name and a dashed border. It can be removed, not edited.

Validation stays on the server: the element checks a step's controls before Done, and nothing else.

Behavior

  • Adding: a "+" sits before every step and at the end of every run (a branching step ends its run; each outcome is a run). It opens a menu of the kinds.
    • The new step takes that place, and what was there moves after it: into a linear step's next, or a branching step's first outcome.
    • A kind with fields opens its editor at once, filled with the defaults. Canceling that first edit takes the step back out.
  • Editing: Edit opens the kind's controls, cloned from its template and filled from the step.
    • Done (or Enter in a single-line field) commits, once every control is valid; the browser shows the message otherwise.
    • Cancel or Escape leaves the step as it was.
    • While an editor is open, the other controls are disabled.
  • Removing: a step's successor takes its place. That is its next, or its first non-empty outcome; the other outcomes go with it.
    • When that would discard other steps, the first press turns the button into the confirmation (label-confirm, [data-confirming] on the card) and a second press removes.
    • Escape, or any other action, withdraws the confirmation.
  • Focus: a new step's editor takes focus, as does a kind without fields' card. Done and Cancel return focus to the card's Edit. A removal moves it to the step that took the place, or to that place's "+".

The form

  • The textarea is the field of record. Every committed edit rewrites it, then the host emits change (bubbling, composed, detail: { value }). So hx-trigger="change" on the form autosaves on commits.
  • The editor's controls are never posted. Their form attribute names no form, so they have no form owner (and Enter in them does not submit). Their own input and change events stop at the host.
  • Saving mid-edit: if the form is serialized while an editor is open (the page's Save pressed mid-edit, or htmx building its FormData), the open edit is included in the posted value when its controls are valid. The formdata event does this, so it works for any serialization.
  • Reset: a form reset restores the textarea's default (the server's value) and redraws the tree from it.

State hooks

  • On the host: [data-editing] (an editor is open) and [data-readonly].
  • On each step's <li>: [data-step] (its id) and [data-kind].
  • On a card: [data-confirming] and [data-unknown].
  • On an adder: [data-open] (its menu shows). Its "+" button carries data-slot.
  • Structure: runs are <ol class="run">; a branching step's outcomes are .branches > .branch, each labelled by .outcome.

CSS hooks

The sheet ships as sequence.css (raw: dist/sequence.css, published as @pebble/sequence/sequence.css); the element adopts it when it upgrades.

  • Layout: the tree is a vertical flow on a connector line. Outcomes sit side by side once each can have 16rem of the element's width (a container query) and stack below that.
  • Sizing: every button and every control in an editor is 44px tall.
  • Theme: dark defaults come from prefers-color-scheme.

--pebble-sequence-ink · --pebble-sequence-muted · --pebble-sequence-surface · --pebble-sequence-border · --pebble-sequence-line (the connector) · --pebble-sequence-accent (falls back to --accent) · --pebble-sequence-danger (the confirmation) · --pebble-sequence-radius (0.5rem) · --pebble-sequence-size (44px) · --pebble-sequence-gap (0.75rem) · --pebble-sequence-card-width (32rem)

Not in this version

These are from the future-foundation editor: drag-and-drop moves, the zoom/pan canvas, detached fragments and their panel, a "content only" mode for a sequence already running (edit fields, not structure), and per-step avatars of the contacts on it. Each would be its own issue.

Exports

WORDS (the default words), parse(text, branchCounts?), serialize(value), insert, remove, update (the pure tree edits), readFields(root), writeFields(root, fields), readKinds(host), and the Sequence, Step, Slot, Fields, Field, Kind types.