@serene-pub/ui-preview 0.6.0-pr-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Client-only, by contract rather than convenience: plugin surfaces mount
3
+ * after hydration (10 §9), and a harness that server-rendered them would be
4
+ * showing a first paint the product never produces.
5
+ */
6
+ export const ssr = false
7
+ export const prerender = false
@@ -0,0 +1,89 @@
1
+ <script lang="ts">
2
+ import { manifest } from 'virtual:serene-pub/surfaces'
3
+ import Problems from '$lib/Problems.svelte'
4
+ </script>
5
+
6
+ <div class="wrap">
7
+ <h1>Surface harness</h1>
8
+ <p class="muted">
9
+ Rendering what <code>{manifest.id}</code> announces. Edit a surface and it hot-reloads;
10
+ edit what the package <em>announces</em> and the list rebuilds.
11
+ </p>
12
+
13
+ <Problems problems={manifest.problems} />
14
+
15
+ {#if manifest.targets.length}
16
+ <div class="grid">
17
+ {#each manifest.targets as t (t.id)}
18
+ <a class="card" href="/t/{t.id}">
19
+ <strong>{t.label}</strong>
20
+ <span class="muted">{t.kind === 'frame' ? t.point : (t.framework ?? 'svelte')}</span>
21
+ <code>{t.entry}</code>
22
+ <span class="muted src">declared in {t.source}</span>
23
+ </a>
24
+ {/each}
25
+ </div>
26
+ {:else}
27
+ <div class="none">
28
+ <p>This package announces no UI surfaces.</p>
29
+ <p class="muted">Add one to your entry module:</p>
30
+ <pre><code>{`export default defineExtension({
31
+ slug: 'acme.dice', …
32
+ surfaces: { page: { entry: 'ui/index.html', title: 'Dice' } },
33
+ components: [component({ slug: 'tray', label: 'Dice tray', entry: 'components/tray.ts', framework: 'svelte' })],
34
+ widgets: [widget({ id: 'tray', title: 'Dice tray', component: 'tray' })],
35
+ })`}</code></pre>
36
+ <p class="muted">
37
+ A frame surface is a document mounted in an opaque-origin iframe. A component is
38
+ code the host mounts in its own document — declared with <code>component(…)</code> and
39
+ named by a widget; it places an <code>sp-frame</code> for any region that needs a real
40
+ document.
41
+ </p>
42
+ </div>
43
+ {/if}
44
+ </div>
45
+
46
+ <style>
47
+ .wrap {
48
+ padding: 2rem 2.4rem;
49
+ max-width: 60rem;
50
+ overflow-y: auto;
51
+ }
52
+ h1 {
53
+ margin: 0 0 0.3rem;
54
+ font-size: 1.4rem;
55
+ }
56
+ .grid {
57
+ display: grid;
58
+ grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
59
+ gap: 0.8rem;
60
+ margin-top: 1.4rem;
61
+ }
62
+ .card {
63
+ display: grid;
64
+ gap: 0.25rem;
65
+ border: 1px solid var(--line);
66
+ border-radius: 9px;
67
+ padding: 0.8rem 0.9rem;
68
+ background: var(--panel);
69
+ color: var(--ink);
70
+ }
71
+ .card:hover {
72
+ border-color: var(--accent);
73
+ text-decoration: none;
74
+ }
75
+ .card code {
76
+ font-size: 0.76rem;
77
+ overflow: hidden;
78
+ text-overflow: ellipsis;
79
+ }
80
+ .src {
81
+ font-size: 0.72rem;
82
+ }
83
+ .none pre {
84
+ background: var(--code);
85
+ padding: 0.9rem 1rem;
86
+ border-radius: 8px;
87
+ overflow-x: auto;
88
+ }
89
+ </style>
@@ -0,0 +1,157 @@
1
+ <script lang="ts">
2
+ /**
3
+ * The controls sandbox: every value type this build ships, with the
4
+ * control that edits it, live.
5
+ *
6
+ * Two jobs, and the second is the one that earns the page.
7
+ *
8
+ * 1. **A reference an author can touch.** `serene-pub docs` prints the
9
+ * option tables; this shows what the option actually *is* on the
10
+ * screen, on the theme the reader picked, with the declaration that
11
+ * produced it printed beside it.
12
+ *
13
+ * 2. **The four-way registry, made visible.** 24 §8 requires every value
14
+ * type to have a factory, a validator, a control and a scaffold
15
+ * printer, and a conformance canary asserts it. A canary tells you it
16
+ * broke; this tells you *what* broke, and shows the safe degradation
17
+ * (an unknown kind renders read-only with its id) rather than
18
+ * describing it.
19
+ */
20
+ import { shippedValueKinds, valueValidators, validateValue, valueKind } from '@serene-pub/sdk'
21
+ import { CONTROL_REGISTRY } from '@serene-pub/controls/registry'
22
+ import { controlFor } from '@serene-pub/controls'
23
+ import { SAMPLES, UNKNOWN_SAMPLE, type Sample } from '$lib/samples.js'
24
+ import { manifest } from 'virtual:serene-pub/surfaces'
25
+
26
+ /** Live value per row, seeded from the sample. */
27
+ let values = $state<Record<string, unknown>>(
28
+ Object.fromEntries([...SAMPLES, UNKNOWN_SAMPLE].map((s, i) => [`${s.kind}#${i}`, s.value])),
29
+ )
30
+
31
+ const rowKey = (s: Sample, i: number) => `${s.kind}#${i}`
32
+
33
+ /**
34
+ * Coverage per shipped kind. The scaffold printer is the CLI's leg and is
35
+ * Node-only, so it is named here rather than probed — claiming to have
36
+ * checked something this page cannot reach would be worse than saying
37
+ * where it is checked.
38
+ */
39
+ const coverage = $derived(
40
+ shippedValueKinds.map((kind) => ({
41
+ kind,
42
+ validator: !!valueValidators[kind],
43
+ control: !!CONTROL_REGISTRY[kind],
44
+ hostIntegrated: !!CONTROL_REGISTRY[kind]?.hostIntegrated,
45
+ component: CONTROL_REGISTRY[kind]?.component ?? '—',
46
+ })),
47
+ )
48
+ const holes = $derived(coverage.filter((c) => !c.validator || !c.control))
49
+
50
+ /** Which kinds this package's own declared surfaces actually reach. */
51
+ const usedKinds = $derived.by(() => {
52
+ const out = new Set<string>()
53
+ for (const t of manifest.targets)
54
+ for (const decl of Object.values(t.settings ?? {})) {
55
+ // Cheap structural read: the sandbox is a reference, not a
56
+ // validator, and a malformed field must not blank the page.
57
+ const type = (decl as { type?: string })?.type
58
+ if (type) out.add(type)
59
+ }
60
+ return [...out].sort()
61
+ })
62
+ </script>
63
+
64
+ <div class="harness-scroll flex-1 overflow-y-auto p-6">
65
+ <header class="mb-4">
66
+ <h1 class="text-2xl">Controls sandbox</h1>
67
+ <p class="text-surface-600-400 max-w-3xl text-sm">
68
+ Every value type this build ships, and the control that edits it. Switch the theme
69
+ in the sidebar to see them the way an instance will render them — these are the
70
+ same components Serene Pub mounts, styled by the same Skeleton layer.
71
+ </p>
72
+ </header>
73
+
74
+ <section class="mb-6">
75
+ <h2 class="mb-2 text-sm font-semibold tracking-wide uppercase">
76
+ The four-way registry
77
+ </h2>
78
+ {#if holes.length}
79
+ <div class="preset-tonal-error mb-2 rounded p-3 text-sm">
80
+ {holes.length} kind{holes.length === 1 ? '' : 's'} incomplete:
81
+ {holes.map((h) => h.kind).join(', ')}
82
+ </div>
83
+ {:else}
84
+ <p class="text-surface-600-400 mb-2 text-sm">
85
+ All {coverage.length} shipped kinds have a factory, a validator and a control.
86
+ The fourth leg — the scaffold printer — lives in the CLI and is asserted by the
87
+ conformance canary in <code>sdk-tests/values.test.ts</code>.
88
+ </p>
89
+ {/if}
90
+ <table class="w-full text-left text-xs">
91
+ <thead class="border-surface-200-800 border-b">
92
+ <tr>
93
+ <th class="py-1">Value type</th>
94
+ <th>Validator</th>
95
+ <th>Control</th>
96
+ <th>Component</th>
97
+ </tr>
98
+ </thead>
99
+ <tbody>
100
+ {#each coverage as c (c.kind)}
101
+ <tr class="border-surface-200-800 border-b">
102
+ <td class="harness-mono py-1">{c.kind}</td>
103
+ <td>{c.validator ? '✓' : '✗'}</td>
104
+ <td>{c.control ? (c.hostIntegrated ? 'host' : '✓') : '✗'}</td>
105
+ <td class="harness-mono">{c.component}</td>
106
+ </tr>
107
+ {/each}
108
+ </tbody>
109
+ </table>
110
+ {#if usedKinds.length}
111
+ <p class="text-surface-600-400 mt-2 text-xs">
112
+ Your declared surfaces use these field types: {usedKinds.join(', ')}.
113
+ </p>
114
+ {/if}
115
+ </section>
116
+
117
+ <section class="grid gap-4" style="grid-template-columns: repeat(auto-fill, minmax(22rem, 1fr))">
118
+ {#each [...SAMPLES, UNKNOWN_SAMPLE] as s, i (rowKey(s, i))}
119
+ {@const key = rowKey(s, i)}
120
+ {@const Control = controlFor(s.kind)}
121
+ {@const errors = values[key] == null ? [] : validateValue(s.decl, values[key])}
122
+ <article class="bg-surface-50-950 border-surface-200-800 rounded-lg border p-4">
123
+ <h3 class="harness-mono mb-1 text-sm">{s.kind}</h3>
124
+ <p class="text-surface-600-400 harness-mono mb-3 text-xs">{s.source}</p>
125
+
126
+ {#if Control}
127
+ <Control
128
+ decl={s.decl}
129
+ value={values[key]}
130
+ id="sandbox-{i}"
131
+ oncommit={(next: unknown) => (values[key] = next)}
132
+ />
133
+ {:else}
134
+ <p class="preset-tonal-surface rounded p-2 text-xs">
135
+ Host-integrated: the choices live in the instance's database, so the
136
+ package ships no control and the host renders a data-connected picker.
137
+ </p>
138
+ {/if}
139
+
140
+ <div class="text-surface-600-400 mt-3 text-xs">
141
+ <div class="harness-mono break-all">
142
+ value: {JSON.stringify(values[key])}
143
+ </div>
144
+ {#if errors.length}
145
+ <ul class="preset-tonal-error mt-1 rounded p-2">
146
+ {#each errors as e}
147
+ <li>{e}</li>
148
+ {/each}
149
+ </ul>
150
+ {:else}
151
+ <div class="mt-1">validates ✓</div>
152
+ {/if}
153
+ </div>
154
+ </article>
155
+ {/each}
156
+ </section>
157
+ </div>
@@ -0,0 +1,205 @@
1
+ <script lang="ts">
2
+ /**
3
+ * One surface on the stage, with its inputs beside it.
4
+ *
5
+ * Two panes of input, and the split is the design:
6
+ *
7
+ * **Settings** — *generated* from what a component declares. A
8
+ * component declares a `SettingsSchema`; core renders forms from
9
+ * schemas; so the editor is derived, never written. Values reach it as
10
+ * `ctx.settings`, the pipe an instance uses, which is what makes editing
11
+ * here evidence about there. A frame surface (page, session-view) is
12
+ * handed no declared values, so it has fixtures only.
13
+ *
14
+ * **Fixtures** — the session and messages the host posts. Free-form JSON
15
+ * because there is no declaration to derive them from: they are the
16
+ * host's data, not the surface's.
17
+ *
18
+ * The fixture editor pushes on every valid parse rather than behind an
19
+ * Apply button: the loop being optimised is "does it survive this shape of
20
+ * message", and a button is one more thing between the question and the
21
+ * answer.
22
+ */
23
+ import { page } from '$app/state'
24
+ import { manifest, frames, fixtureOverride } from 'virtual:serene-pub/surfaces'
25
+ import FrameStage from '$lib/FrameStage.svelte'
26
+ import ComponentStage from '$lib/ComponentStage.svelte'
27
+ import Problems from '$lib/Problems.svelte'
28
+ import SurfaceSettings from '$lib/SurfaceSettings.svelte'
29
+ import { DEFAULT_FIXTURES, normalizeFixtures, type Fixtures } from '$lib/fixtures.js'
30
+ import { defaultsOf } from '$lib/defaults.js'
31
+ import { onMount } from 'svelte'
32
+
33
+ // The `--fixtures` file is the modder's, so it is normalised on the way in
34
+ // too — a hand-written file that omits `messages` should not be a broken
35
+ // harness on startup.
36
+ const base = normalizeFixtures(fixtureOverride ?? DEFAULT_FIXTURES, DEFAULT_FIXTURES).fixtures
37
+
38
+ const target = $derived(manifest.targets.find((t) => t.id === page.params.id))
39
+
40
+ let draft = $state(JSON.stringify(base, null, 2))
41
+ let fixtures = $state<Fixtures>(base)
42
+ let parseError = $state<string | null>(null)
43
+ let reloadKey = $state(0)
44
+ let picked = $state<'declared' | 'fixtures'>('declared')
45
+ // A frame has no declared values, so its only pane is the fixtures.
46
+ const tab = $derived(target?.kind === 'frame' ? 'fixtures' : picked)
47
+
48
+ /**
49
+ * Declared values, per target id, seeded from each schema's defaults.
50
+ *
51
+ * Seeded for *every* target up front rather than lazily for the current
52
+ * one: SvelteKit reuses this component across `/t/[id]`, so the map
53
+ * outlives navigation and what you typed on one surface is still there
54
+ * when you come back — the same reason `ctx.state` survives a remount
55
+ * (10 §5). Eager also means the form never renders against an
56
+ * uninitialised key, which is the difference between a defaulted field
57
+ * and a crash.
58
+ */
59
+ let declared = $state<Record<string, Record<string, unknown>>>(
60
+ Object.fromEntries(
61
+ manifest.targets
62
+ .filter((t) => t.settings)
63
+ .map((t) => [t.id, defaultsOf(t.settings!)]),
64
+ ),
65
+ )
66
+ const declaredValues = $derived(target ? (declared[target.id] ?? {}) : {})
67
+
68
+ let substituted = $state<string[]>([])
69
+ $effect(() => {
70
+ try {
71
+ // Parsed *and* shaped: valid JSON is not the same promise as "has
72
+ // the fields the pushers read", and the difference used to be an
73
+ // uncaught TypeError that took the stage with it.
74
+ const next = normalizeFixtures(JSON.parse(draft), DEFAULT_FIXTURES)
75
+ fixtures = next.fixtures
76
+ substituted = next.substituted
77
+ parseError = null
78
+ } catch (e) {
79
+ parseError = (e as Error).message
80
+ }
81
+ })
82
+
83
+ // A frame document edit reloads that frame and nothing else — the harness
84
+ // asks Vite for the signal rather than polling, and a component edit never
85
+ // reaches here because Svelte HMR has already handled it (dev.ts: a
86
+ // component is always hot).
87
+ onMount(() => {
88
+ if (!import.meta.hot) return
89
+ const onFrameChange = () => reloadKey++
90
+ import.meta.hot.on('serene-pub:frame-changed', onFrameChange)
91
+ return () => import.meta.hot?.off('serene-pub:frame-changed', onFrameChange)
92
+ })
93
+ </script>
94
+
95
+ {#if !target}
96
+ <div class="p-8">
97
+ <Problems problems={[`No surface with id '${page.params.id}' is announced.`]} />
98
+ <a href="/">Back to the list</a>
99
+ </div>
100
+ {:else}
101
+ <div class="flex min-h-0 flex-1">
102
+ <div class="flex min-w-0 flex-1 flex-col">
103
+ <header class="flex items-baseline gap-3 px-5 pt-4 pb-2">
104
+ <h1 class="text-lg">{target.label}</h1>
105
+ <span class="text-surface-600-400 text-xs">declared in {target.source}</span>
106
+ {#if target.kind === 'frame'}
107
+ <button class="btn btn-sm preset-tonal ml-auto" onclick={() => reloadKey++}>
108
+ Reload frame
109
+ </button>
110
+ {/if}
111
+ </header>
112
+ <div class="px-5"><Problems problems={manifest.problems} /></div>
113
+ {#if target.kind === 'frame'}
114
+ {#if frames[target.id]}
115
+ <!-- Keyed on the target: the stage owns per-surface state (the
116
+ channel log, the scoped and suspended toggles, the mounted
117
+ component) and SvelteKit reuses this page across /t/[id],
118
+ so without the key one surface inherits the last one's. -->
119
+ {#key target.id}
120
+ <FrameStage {target} src={frames[target.id]} {fixtures} {reloadKey} />
121
+ {/key}
122
+ {:else}
123
+ <div class="px-5">
124
+ <Problems
125
+ problems={[`'${target.entry}' was not found under the package root.`]}
126
+ />
127
+ </div>
128
+ {/if}
129
+ {:else}
130
+ {#key target.id}
131
+ <ComponentStage {target} {fixtures} settings={declaredValues} />
132
+ {/key}
133
+ {/if}
134
+ </div>
135
+
136
+ <aside
137
+ class="bg-surface-50-950 border-surface-200-800 flex w-96 min-h-0 flex-none flex-col border-l p-4"
138
+ >
139
+ <div class="mb-3 flex gap-1">
140
+ {#if target.kind !== 'frame'}
141
+ <button
142
+ class="btn btn-sm {tab === 'declared' ? 'preset-filled-primary-500' : 'preset-tonal'}"
143
+ onclick={() => (picked = 'declared')}
144
+ >
145
+ Settings
146
+ </button>
147
+ {/if}
148
+ <button
149
+ class="btn btn-sm {tab === 'fixtures' ? 'preset-filled-primary-500' : 'preset-tonal'}"
150
+ onclick={() => (picked = 'fixtures')}
151
+ >
152
+ Fixtures
153
+ </button>
154
+ </div>
155
+
156
+ {#if tab === 'declared'}
157
+ <div class="harness-scroll min-h-0 flex-1 overflow-y-auto">
158
+ {#if !target.settings}
159
+ <p class="text-surface-600-400 text-xs">
160
+ This component declares no settings, so there is nothing to generate. Add a
161
+ <code>settings</code> schema to its widget and the editor appears
162
+ here — core renders forms from schemas, so you never write one.
163
+ </p>
164
+ <pre
165
+ class="bg-surface-100-900 mt-2 overflow-x-auto rounded p-2 text-xs">{`widgets: [widget({
166
+ id: 'tray',
167
+ title: 'Dice tray',
168
+ component: 'tray',
169
+ settings: {
170
+ sides: { type: 'integer', label: 'Sides', default: 20, min: 2 },
171
+ label: { type: 'string', label: 'Caption' },
172
+ shaded: { type: 'boolean', label: 'Shade the tray', default: true },
173
+ },
174
+ })]`}</pre>
175
+ {:else}
176
+ <SurfaceSettings
177
+ schema={target.settings}
178
+ bind:values={declared[target.id]}
179
+ delivery="ctx.settings"
180
+ />
181
+ {/if}
182
+ </div>
183
+ {:else}
184
+ <p class="text-surface-600-400 mb-2 text-xs">
185
+ What the host posts. A surface can only render what core chose to send it, so
186
+ this is the whole world it has.{#if target.settings}
187
+ <strong> settings</strong> comes from the declaration, not from here.{/if}
188
+ </p>
189
+ {#if parseError}
190
+ <div class="preset-tonal-error mb-2 rounded p-2 text-xs">{parseError}</div>
191
+ {:else if substituted.length}
192
+ <div class="preset-tonal-warning mb-2 rounded p-2 text-xs">
193
+ Using the defaults for <strong>{substituted.join(', ')}</strong> — the value
194
+ here is not the shape the host posts.
195
+ </div>
196
+ {/if}
197
+ <textarea
198
+ class="textarea harness-mono min-h-0 flex-1 resize-none text-xs"
199
+ bind:value={draft}
200
+ spellcheck="false"
201
+ ></textarea>
202
+ {/if}
203
+ </aside>
204
+ </div>
205
+ {/if}
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><rect width="32" height="32" rx="7" fill="#4459c9"/><path d="M9 10h14M9 16h9M9 22h12" stroke="#fff" stroke-width="2.6" stroke-linecap="round"/></svg>
@@ -0,0 +1,16 @@
1
+ import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'
2
+
3
+ /**
4
+ * No adapter on purpose. This harness only ever runs as a dev server on the
5
+ * modder's own machine — there is nothing to deploy, and an adapter would be
6
+ * a dependency bought for a `build` nobody runs.
7
+ */
8
+ export default {
9
+ preprocess: vitePreprocess(),
10
+ kit: {
11
+ // The harness renders plugin UI, and plugin UI is client-only by
12
+ // contract (10 §9) — SSRing a surface would need per-framework SSR and
13
+ // would be a fidelity lie besides.
14
+ alias: {},
15
+ },
16
+ }