@valbuild/core 0.153.5 → 0.154.2

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
1
  # @valbuild/core
2
2
 
3
+ ## 0.154.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#1049](https://github.com/valbuild/val/pull/1049) [`d90b9e2`](https://github.com/valbuild/val/commit/d90b9e2f856d4023821698cd22b4fbbab046c2ff) Thanks [@freekh](https://github.com/freekh)! - Experimental: link an object to the component that renders it, and let Val render and arrange it
8
+
9
+ `s.object(...).component(() => import("./Hero"))` links an object schema to a component. The link is type checked against the component's own props: when the object's fields are the props nothing else is needed, and for any other component a `props` function says how to make them.
10
+
11
+ ```ts
12
+ export const heroFields = s.object({
13
+ type: s.literal("hero"),
14
+ title: s.string(),
15
+ });
16
+ export const hero = heroFields.component(() => import("../components/Hero"));
17
+
18
+ export const note = s
19
+ .object({ type: s.literal("note"), title: s.string(), urgent: s.boolean() })
20
+ .component(() => import("../components/Callout"), {
21
+ props: (note) => ({
22
+ heading: note.title,
23
+ tone: note.urgent ? "warning" : "info",
24
+ }),
25
+ });
26
+ ```
27
+
28
+ `<ValComponentEditor value={page.sections} />`, from `@valbuild/tanstack` and `@valbuild/next`, renders a list of linked objects (or a single one) with their components, with no map from a `type` to a component to keep. It only accepts values that are linked: a list with a variant that has no component is a type error. It is optional, and a component can use it for a list of components of its own.
29
+
30
+ In Val Studio, a list rendered this way can be arranged on the page itself: drag a section to another place, add one between two others, or remove one.
31
+
32
+ This replaces the experimental `.render({ as: "component", component })` on objects. Move its `component` to the first argument of `.component()` and the rest of its options to the second.
33
+
3
34
  ## 0.153.5
4
35
 
5
36
  ### Patch Changes
@@ -27,6 +27,8 @@ export type { MediaHotspot, MediaSource, GalleryImageSource, GalleryFileSource,
27
27
  export type { AllRichTextOptions, Bold, Styles, HeadingNode, ImageNode, Italic, LineThrough, ListItemNode, LinkNode, OrderedListNode, ParagraphNode, BrNode, RichTextNode, RichTextOptions, SerializedRichTextOptions, RichTextSource, BlockNode, SpanNode, UnorderedListNode, } from "./source/richtext.js";
28
28
  export { type Val, type SerializedVal, type ModuleFilePath, type PatchId, type ModulePath, type SourcePath, type JsonOfSource, type ParentPatchId, } from "./val/index.js";
29
29
  export type { Json, JsonPrimitive, JsonArray, JsonObject } from "./Json.js";
30
+ export type { ValEncodedString, Image, File, Video, StegaOfRichTextSource, RichText, StegaOfSource, ReadOf, } from "./read.js";
31
+ export type { ValComponent, ValComponentSource, IsValComponentSource, } from "./source/component.js";
30
32
  export type { ValidationError, ValidationErrors, } from "./schema/validation/ValidationError.js";
31
33
  export type { ValidationFix } from "./schema/validation/ValidationFix.js";
32
34
  export { VAL_EXTENSION, type SourceArray } from "./source/index.js";
@@ -36,7 +38,7 @@ import { getSource, isValModule, splitModulePath, splitModuleFilePath, resolvePa
36
38
  declare const ModuleFilePathSep = "?p=";
37
39
  export { ModuleFilePathSep };
38
40
  import { SelectorSource, getSchema } from "./selector/index.js";
39
- import { ModulePath, SourcePath, getValPath, isVal, setValPath } from "./val/index.js";
41
+ import { ModulePath, SourcePath, getValPath, isVal, setValPath, valPathOf } from "./val/index.js";
40
42
  import { createValPathOfItem } from "./selector/SelectorProxy.js";
41
43
  import { Operation } from "./patch/index.js";
42
44
  import { initSchema } from "./initSchema.js";
@@ -92,7 +94,7 @@ export { type SerializedUnionSchema, type UnionSchema, type SerializedStringUnio
92
94
  export { type SerializedLiteralSchema, LiteralSchema } from "./schema/literal.js";
93
95
  export { deserializeSchema } from "./schema/deserialize.js";
94
96
  export { type PreviewItem, type ItemPreviewInput, type RecordPreview, type ArrayPreview, type ReifiedPreview, type PreviewScope, previewScope, } from "./preview.js";
95
- export { type InlineRender, type FieldRender, type SliderRender, type NumberRender, isInlineRender, } from "./render.js";
97
+ export { type InlineRender, type FieldRender, type SliderRender, type NumberRender, isInlineRender, type ComponentRender, type SerializedComponentRender, type ValComponentType, type ValComponentImport, type ComponentPropsOf, type ComponentOptions, type ComponentArgs, getComponentRender, loadComponent, componentPropsOf, setComponentOfValue, componentOfValue, VAL_PREVIEW_MESSAGES, } from "./render.js";
96
98
  export type { ValRouter, ExternalPageRouter, RouteValidationError, } from "./router.js";
97
99
  export { parseNextJsRoutePattern, parseTanStackRoutePattern, tanStackSegmentsOfRoutePath, validateUrlAgainstPattern, matchRoutePattern, } from "./router.js";
98
100
  export { getSourcePathFromRoute, isPageRouter, routePatternOf, } from "./getSourcePathFromRoute.js";
@@ -176,6 +178,7 @@ declare const Internal: {
176
178
  getSchema: typeof getSchema;
177
179
  getValPath: typeof getValPath;
178
180
  setValPath: typeof setValPath;
181
+ valPathOf: typeof valPathOf;
179
182
  getSource: typeof getSource;
180
183
  isValModule: typeof isValModule;
181
184
  resolvePath: typeof resolvePath;
@@ -0,0 +1,239 @@
1
+ /**
2
+ * What content READS as: the types a reader (`useVal`, `fetchVal`,
3
+ * `useValRoute`) gives back for a Source.
4
+ *
5
+ * They are here, and not beside `stegaEncode` in `@valbuild/react` where they
6
+ * were, because a schema has to be able to say them: `.component()` checks
7
+ * the component it links against what the app will actually hand it, which is
8
+ * the READ value (an image with its `url`, a string with its edit tag) and
9
+ * not the Source. `@valbuild/react/stega` re-exports every one of them, so
10
+ * nothing an app imports has moved.
11
+ */
12
+ import type { Json, JsonPrimitive } from "./Json.js";
13
+ import type { RawString } from "./schema/string.js";
14
+ import type { GenericSelector, SelectorOf, SelectorSource } from "./selector/index.js";
15
+ import type { Source, SourceArray, SourceObject } from "./source/index.js";
16
+ import type { ValView } from "./selector/view.js";
17
+ import type { FileSource, ImageSource, IsVideoSource, MediaHotspot, VideoCaptionSource } from "./source/media.js";
18
+ import type { RichTextOptions, RichTextSource } from "./source/richtext.js";
19
+ import type { IsValComponentSource, ValComponent } from "./source/component.js";
20
+ import type { ValViewSource } from "./source/view.js";
21
+ declare const brand: unique symbol;
22
+ /**
23
+ * ValEncodedString is a string that is encoded using steganography.
24
+ *
25
+ * This means that there is a hidden / non-visible object embedded in the string.
26
+ * This object includes a path, which is used to automatically tag
27
+ * where the content comes from for contextual editing.
28
+ *
29
+ */
30
+ export type ValEncodedString = `${string}__VAL_ENCODED_STRING_INVISIBLE_CHARS` & {
31
+ /**
32
+ *
33
+ * NB: This is a Val ENCODED string, which means that you should not use this method.
34
+ *
35
+ * Instead, get the raw string using `val.raw`
36
+ *
37
+ * @deprecated NB: This is a Val ENCODED string, which means that you should not use this method.
38
+ *
39
+ * @example
40
+ * val.raw(myEncodedString) // returns a normal string
41
+ */
42
+ substring: never;
43
+ /**
44
+ *
45
+ * NB: This is a Val ENCODED string, which means that you should not use this method.
46
+ *
47
+ * Instead, get the raw string using `val.raw`
48
+ *
49
+ * @deprecated NB: This is a Val ENCODED string, which means that you should not use this method.
50
+ *
51
+ * @example
52
+ * val.raw(myEncodedString) // returns a normal string
53
+ */
54
+ match: never;
55
+ /**
56
+ *
57
+ * NB: This is a Val ENCODED string, which means that you should not use this method.
58
+ *
59
+ * Instead, get the raw string using `val.raw`
60
+ *
61
+ * @deprecated NB: This is a Val ENCODED string, which means that you should not use this method.
62
+ *
63
+ * @example
64
+ * val.raw(myEncodedString) // returns a normal string
65
+ */
66
+ charAt: never;
67
+ /**
68
+ *
69
+ * NB: This is a Val ENCODED string, which means that you should not use this method.
70
+ *
71
+ * Instead, get the raw string using `val.raw`
72
+ *
73
+ * @deprecated NB: This is a Val ENCODED string, which means that you should not use this method.
74
+ *
75
+ * @example
76
+ * val.raw(myEncodedString) // returns a normal string
77
+ */
78
+ charCodeAt: never;
79
+ /**
80
+ *
81
+ * NB: This is a Val ENCODED string, which means that you should not use this method.
82
+ *
83
+ * Instead, get the raw string using `val.raw`
84
+ *
85
+ * @deprecated NB: This is a Val ENCODED string, which means that you should not use this method.
86
+ *
87
+ * @example
88
+ * val.raw(myEncodedString) // returns a normal string
89
+ */
90
+ concat: never;
91
+ /**
92
+ *
93
+ * NB: This is a Val ENCODED string, which means that you should not use this method.
94
+ *
95
+ * Instead, get the raw string using `val.raw`
96
+ *
97
+ * @deprecated NB: This is a Val ENCODED string, which means that you should not use this method.
98
+ *
99
+ * @example
100
+ * val.raw(myEncodedString) // returns a normal string
101
+ */
102
+ indexOf: never;
103
+ /**
104
+ *
105
+ * NB: This is a Val ENCODED string, which means that you should not use this method.
106
+ *
107
+ * Instead, get the raw string using `val.raw`
108
+ *
109
+ * @deprecated NB: This is a Val ENCODED string, which means that you should not use this method.
110
+ *
111
+ * @example
112
+ * val.raw(myEncodedString) // returns a normal string
113
+ */
114
+ lastIndexOf: never;
115
+ /**
116
+ *
117
+ * NB: This is a Val ENCODED string, which means that you should not use this method.
118
+ *
119
+ * Instead, get the raw string using `val.raw`
120
+ *
121
+ * @deprecated NB: This is a Val ENCODED string, which means that you should not use this method.
122
+ *
123
+ * @example
124
+ * val.raw(myEncodedString) // returns a normal string
125
+ */
126
+ slice: never;
127
+ /**
128
+ *
129
+ * NB: This is a Val ENCODED string, which means that you should not use this method.
130
+ *
131
+ * Instead, get the raw string using `val.raw`
132
+ *
133
+ * @deprecated NB: This is a Val ENCODED string, which means that you should not use this method.
134
+ *
135
+ * @example
136
+ * val.raw(myEncodedString) // returns a normal string
137
+ */
138
+ toString: never;
139
+ /**
140
+ *
141
+ * NB: This is a Val ENCODED string, which means that you should not use this method.
142
+ *
143
+ * Instead, get the raw string using `val.raw`
144
+ *
145
+ * @deprecated NB: This is a Val ENCODED string, which means that you should not use this method.
146
+ *
147
+ * @example
148
+ * val.raw(myEncodedString) // returns a normal string
149
+ */
150
+ /**
151
+ *
152
+ * NB: This is a Val ENCODED string, which means that you should not use length on it.
153
+ *
154
+ * Instead, get the raw string using `val.raw`
155
+ *
156
+ * @deprecated NB: This is a Val ENCODED string, which means that you should not use this method.
157
+ *
158
+ * @example
159
+ * val.raw(myEncodedString) // returns a normal string
160
+ */
161
+ length: never;
162
+ [brand]: "ValEncodedString";
163
+ };
164
+ /**
165
+ * An image as a consumer sees it: what was authored, plus the generated `url`.
166
+ *
167
+ * `path` stays a plain string — it is what `url` is derived from, so encoding it
168
+ * would corrupt every URL built from it.
169
+ */
170
+ export type Image = {
171
+ readonly path: string;
172
+ readonly url: ValEncodedString;
173
+ readonly width?: number;
174
+ readonly height?: number;
175
+ readonly mimeType?: string;
176
+ readonly alt?: string;
177
+ readonly hotspot?: MediaHotspot;
178
+ };
179
+ export type File = {
180
+ readonly path: string;
181
+ readonly url: ValEncodedString;
182
+ readonly mimeType?: string;
183
+ };
184
+ /**
185
+ * A video as a consumer sees it: what was authored, plus a generated `url` on
186
+ * the video, on its poster and on each caption track.
187
+ *
188
+ * Only the video's own `url` carries the edit tag — a click on the player
189
+ * reaches the field. The poster and the captions are parts of the same field,
190
+ * not fields of their own, so their URLs are plain.
191
+ *
192
+ * An HLS stream (`mimeType` `application/vnd.apple.mpegurl`) plays natively in
193
+ * Safari and needs a library such as hls.js everywhere else; `url` is then the
194
+ * master playlist.
195
+ */
196
+ export type Video = {
197
+ readonly path: string;
198
+ readonly url: ValEncodedString;
199
+ readonly mimeType: string;
200
+ readonly width?: number;
201
+ readonly height?: number;
202
+ readonly duration?: number;
203
+ readonly alt?: string;
204
+ readonly hotspot?: MediaHotspot;
205
+ readonly posterTime?: number;
206
+ readonly poster?: {
207
+ readonly path: string;
208
+ readonly url: string;
209
+ readonly width?: number;
210
+ readonly height?: number;
211
+ readonly mimeType?: string;
212
+ };
213
+ readonly startTime?: number;
214
+ readonly endTime?: number;
215
+ readonly captions?: readonly (Omit<VideoCaptionSource, "patch_id"> & {
216
+ readonly url: string;
217
+ })[];
218
+ };
219
+ export type StegaOfRichTextSource<T extends Source> = Json extends T ? Json : T extends ImageSource ? Image : T extends SourceObject ? {
220
+ [key in keyof T]: StegaOfRichTextSource<T[key]>;
221
+ } : T extends SourceArray ? StegaOfRichTextSource<T[number]>[] : T extends JsonPrimitive ? T : never;
222
+ /**
223
+ * RichText is accessible by users (after conversion via useVal / fetchVal)
224
+ **/
225
+ export type RichText<O extends RichTextOptions> = StegaOfRichTextSource<RichTextSource<O>> & {
226
+ readonly __brand?: "RichText";
227
+ };
228
+ export type StegaOfSource<T extends Source> = Json extends T ? Json : T extends RichTextSource<infer O> ? RichText<O> : IsVideoSource<T> extends true ? Video : T extends ImageSource ? Image : T extends FileSource ? File : T extends ValViewSource<string, infer Target> ? ValView<Target> : T extends SourceObject ? IsValComponentSource<T> extends true ? {
229
+ [key in keyof T]: StegaOfSource<T[key]>;
230
+ } & ValComponent : {
231
+ [key in keyof T]: StegaOfSource<T[key]>;
232
+ } : T extends SourceArray ? StegaOfSource<T[number]>[] : T extends RawString ? string : string extends T ? ValEncodedString : T extends JsonPrimitive ? T : never;
233
+ /**
234
+ * What a reader gives back for a schema's source type `T`: `T` as a selector,
235
+ * resolved. The same answer as `ResolvedVal` in `@valbuild/react/stega` for a
236
+ * value that is not a view, which a schema's own source never is.
237
+ */
238
+ export type ReadOf<T extends SelectorSource> = SelectorOf<T> extends GenericSelector<infer S> ? StegaOfSource<S> : never;
239
+ export {};
@@ -1,4 +1,5 @@
1
- import type { SerializedSchema } from "./schema/index.js";
1
+ import type { Schema, SerializedSchema } from "./schema/index.js";
2
+ import type { SelectorSource } from "./selector/index.js";
2
3
  /**
3
4
  * THE RULE, and the only sentence that needs to be remembered:
4
5
  *
@@ -29,11 +30,16 @@ import type { SerializedSchema } from "./schema/index.js";
29
30
  * A RENDER is how a field is laid out in the editor, and it applies only when
30
31
  * you are looking at the field. On a list it is declared by the CONTAINER about
31
32
  * its children, never by a child about itself: `s.array(item).render({ as: "inline" })`
32
- * and `s.record(item).render({ as: "inline" })`. Two other schemas take a
33
+ * and `s.record(item).render({ as: "inline" })`. Three other schemas take a
33
34
  * render, each meaning something of its own: `s.keyOf(...)` (see
34
- * `KeyOfSchema.render`) and `s.number()` / `s.int()`, whose `{ as: "slider" }`
35
+ * `KeyOfSchema.render`), `s.number()` / `s.int()`, whose `{ as: "slider" }`
35
36
  * is a different input for the same value (see {@link SliderRender}).
36
37
  *
38
+ * EXPERIMENTAL: an object's component is declared with `.component(...)` and
39
+ * not with `.render(...)`, because it is more than layout in the editor: the
40
+ * site renders the object with it (see {@link ComponentRender}). Its marker
41
+ * still travels as the serialized `render`, `{ as: "component" }`.
42
+ *
37
43
  * Why the container: inline is a property of the LIST — "my items are edited in
38
44
  * their rows" — and the item schema is the wrong place to say it. An item can be
39
45
  * reused in a list that wants preview rows and one that wants forms; a tagged
@@ -138,3 +144,169 @@ export type NumberRender = SliderRender;
138
144
  * serialized schema alone, never of the value the row happens to hold.
139
145
  */
140
146
  export declare function isInlineRender(container: SerializedSchema): boolean;
147
+ /**
148
+ * EXPERIMENTAL. A React (or any) component, typed loosely because core knows
149
+ * nothing about React. `never` in the parameter position accepts a component
150
+ * of any props, since parameters are contravariant.
151
+ */
152
+ export type ValComponentType = ((props: never) => unknown) | (new (props: never) => unknown);
153
+ /**
154
+ * What the `component` thunk of a {@link ComponentRender} resolves to: the
155
+ * component itself, or a module whose default export is the component.
156
+ */
157
+ export type ValComponentImport = ValComponentType | {
158
+ default: ValComponentType;
159
+ };
160
+ /**
161
+ * The props a component takes, read off its own type: what `.component()`
162
+ * checks a schema's fields (or its `props` function) against. Structural, so
163
+ * it needs nothing of React: a function component is a function of its props,
164
+ * and a class component is constructed with them.
165
+ */
166
+ export type ComponentPropsOf<C> = C extends (props: infer P) => unknown ? P : C extends new (props: infer P) => unknown ? P : never;
167
+ /**
168
+ * EXPERIMENTAL. What `.component(load, options)` on `s.object(...)` holds:
169
+ * the component that renders this object on the site.
170
+ *
171
+ * The site renders the object through it (`ValComponentEditor`), and the
172
+ * app's preview route (`ValComponentPreview`) renders it alone. Both go
173
+ * through the same thunk and the same `props`, so what the editor sees cannot
174
+ * drift from what a visitor gets. In the Studio's field panel the object is
175
+ * edited as any other object: its fields, with no component beside them.
176
+ *
177
+ * It holds CLOSURES - the `component` thunk and `props` - so, exactly like a
178
+ * preview, only a marker travels in the serialized schema
179
+ * ({@link SerializedComponentRender}) and the component is rendered by the
180
+ * host app, which has the schema instance.
181
+ *
182
+ * `component` is a lazy `import()` rather than the component itself. A
183
+ * `.val.ts` may not import app code (`@valbuild/eslint-plugin`'s
184
+ * `no-illegal-imports`), because Val evaluates val files outside of the app -
185
+ * `val validate`, the language server and the Studio all do - and a component
186
+ * pulls in CSS, images and framework code none of those can load. A thunk is
187
+ * never called there, so the component's file is never loaded either. On the
188
+ * site it also means a page downloads the components it shows and no others.
189
+ *
190
+ * `props` turns the value into the component's props, for a component whose
191
+ * props are not the object's fields: one that was written before the schema,
192
+ * or belongs to a design system. It is called with the READ value (an image
193
+ * has its `url`, a string carries its edit tag), so a string it passes on
194
+ * unchanged is still a click away from its field, and one it transforms is
195
+ * not. Without it the value itself is the props.
196
+ *
197
+ * `example` is content to preview the component with that is not on the site:
198
+ * a fixture, and a good place for deliberately unrepresentative values (very
199
+ * long titles, empty lists) that make layout problems easy to spot.
200
+ */
201
+ export type ComponentRender<Src> = {
202
+ as: "component";
203
+ component: () => Promise<ValComponentImport>;
204
+ /**
205
+ * `never`, because this is the mapper with its types erased: the schema
206
+ * that holds it knows what it takes, and nothing that finds it by path
207
+ * does. Call it through {@link componentPropsOf}.
208
+ */
209
+ props?: (value: never) => unknown;
210
+ example?: Src;
211
+ };
212
+ /**
213
+ * The options of `.component(load, options)` that are not the `props`
214
+ * function. See {@link ComponentRender} for each.
215
+ */
216
+ export type ComponentOptions<Src> = {
217
+ example?: Src;
218
+ };
219
+ /** {@link ComponentOptions} for a component whose props ARE the read value. */
220
+ export interface ComponentOptionsWithOptionalProps<Src, Read, Props> extends ComponentOptions<Src> {
221
+ props?: (value: Read) => Props;
222
+ }
223
+ /**
224
+ * {@link ComponentOptions} for a component whose props are NOT the read
225
+ * value: `props` is what makes them, so it is required.
226
+ */
227
+ export interface ComponentOptionsWithProps<Src, Read, Props> extends ComponentOptions<Src> {
228
+ props: (value: Read) => Props;
229
+ }
230
+ /**
231
+ * What follows the thunk in `.component(load, ...)`: nothing or options when
232
+ * the value a reader gives back (`Read`) is assignable to the component's
233
+ * props, and options with a `props` function when it is not. That is the
234
+ * whole type check: a component that cannot be handed this object does not
235
+ * compile, on the line that links them.
236
+ *
237
+ * In tuples so the test does not distribute over a union of props.
238
+ */
239
+ export type ComponentArgs<Src, Read, C> = [Read] extends [ComponentPropsOf<C>] ? [
240
+ options?: ComponentOptionsWithOptionalProps<Src, Read, ComponentPropsOf<C>>
241
+ ] : [options: ComponentOptionsWithProps<Src, Read, ComponentPropsOf<C>>];
242
+ /**
243
+ * What a {@link ComponentRender} serializes to. The component is a closure and
244
+ * cannot travel; `example` says whether there is one to preview with.
245
+ */
246
+ export type SerializedComponentRender = {
247
+ as: "component";
248
+ example?: true;
249
+ };
250
+ /**
251
+ * The messages between the Studio and the host app's preview route, which run
252
+ * in different frames. Both sides read them from here so they cannot drift.
253
+ *
254
+ * - `ready`: preview to Studio. The preview is listening; tell it whether
255
+ * clicking picks.
256
+ * - `refresh`: Studio to preview. The content changed; render it again.
257
+ * - `size`: preview to Studio, with `{ height }`. What the component rendered
258
+ * is this tall, so the Studio can size the frame to fit it.
259
+ * - `picking`: Studio to preview, with `{ enabled }`. Whether a click on
260
+ * content picks the field it came from, rather than doing what a click on
261
+ * the site does.
262
+ * - `pick`: preview to Studio, with `{ paths }`. The editor clicked content
263
+ * with these source paths behind it (from its `data-val-path`).
264
+ */
265
+ export declare const VAL_PREVIEW_MESSAGES: {
266
+ ready: "val-preview-ready";
267
+ refresh: "val-preview-refresh";
268
+ size: "val-preview-size";
269
+ picking: "val-preview-picking";
270
+ pick: "val-preview-pick";
271
+ };
272
+ /**
273
+ * EXPERIMENTAL: the component that renders a value of `schema` whose source is
274
+ * `src`, or `null` when there is none. A discriminated union answers for the
275
+ * variant `src` takes, which is what a list of page sections needs.
276
+ */
277
+ export declare function getComponentRender(schema: Schema<SelectorSource>, src: unknown): ComponentRender<unknown> | null;
278
+ /**
279
+ * EXPERIMENTAL: call a {@link ComponentRender}'s thunk and unwrap a module's
280
+ * default export.
281
+ */
282
+ export declare function loadComponent(render: ComponentRender<unknown>): Promise<ValComponentType>;
283
+ /**
284
+ * EXPERIMENTAL: the props to render a {@link ComponentRender}'s component
285
+ * with, for `value` as a reader gave it back: what its `props` function makes
286
+ * of it, or the value itself when it has none.
287
+ *
288
+ * The one place the mapper is called, so the site (`ValComponentEditor`) and
289
+ * the Studio's preview cannot disagree about what the component is handed.
290
+ */
291
+ export declare function componentPropsOf(render: ComponentRender<unknown>, value: unknown): unknown;
292
+ /**
293
+ * EXPERIMENTAL: attach, to a value a reader is about to give back, the
294
+ * component its schema links it to (`.component()`), so `ValComponentEditor`
295
+ * can render the value without a registry: the value says what renders it.
296
+ *
297
+ * The same idea as a view's handle, for the same reason. The app cannot get
298
+ * from a value back to its schema, and the reader (`stegaEncode`) is the one
299
+ * place that has both.
300
+ *
301
+ * Not enumerable, and a symbol: `Object.keys`, `JSON.stringify` and every
302
+ * walk that reads content as data still see only the content. So, like a
303
+ * view's handle, it does not survive serialization: a value passed from a
304
+ * server component to a client one arrives without it.
305
+ */
306
+ export declare function setComponentOfValue(target: object, render: ComponentRender<unknown>): void;
307
+ /**
308
+ * EXPERIMENTAL: the component a read value is linked to, or `null` when it
309
+ * carries none: its schema has no `.component()`, or the value crossed a
310
+ * server/client boundary and lost it.
311
+ */
312
+ export declare function componentOfValue(value: unknown): ComponentRender<unknown> | null;
@@ -4,6 +4,7 @@ import { SelectorSource } from "../selector/index.js";
4
4
  import { SourceObject } from "../source/index.js";
5
5
  import { ModuleFilePath, SourcePath } from "../val/index.js";
6
6
  import { SerializedObjectSchema } from "./object.js";
7
+ import { ComponentRender } from "../render.js";
7
8
  import { ValidationError, ValidationErrors } from "./validation/ValidationError.js";
8
9
  export type SerializedDiscriminatedUnionSchema = {
9
10
  type: "discriminated-union";
@@ -144,6 +145,7 @@ export declare class DiscriminatedUnionSchema<Key extends string, T extends Disc
144
145
  */
145
146
  preview(select: ItemPreviewInput<Src>): DiscriminatedUnionSchema<Key, T, Src>;
146
147
  protected executePreviewItem(src: NonNullable<Src>): PreviewItem | null;
148
+ protected executeComponentRender(src: unknown): ComponentRender<unknown> | null;
147
149
  protected declaresItemPreview(): boolean;
148
150
  protected executeSerialize(): SerializedDiscriminatedUnionSchema;
149
151
  protected executePreview(sourcePath: SourcePath | ModuleFilePath, src: Src, scope?: PreviewScope, selfIsReifiedByParent?: boolean): ReifiedPreview;
@@ -26,6 +26,7 @@ import { ValidationError, ValidationErrors } from "./validation/ValidationError.
26
26
  import { FileSource } from "../source/media.js";
27
27
  import { GenericRichTextSourceNode, RichTextSource } from "../source/richtext.js";
28
28
  import { ReifiedPreview, PreviewScope, PreviewItem } from "../preview.js";
29
+ import { ComponentRender } from "../render.js";
29
30
  export type SerializedSchema = SerializedStringSchema | SerializedLiteralSchema | SerializedBooleanSchema | SerializedNumberSchema | SerializedObjectSchema | SerializedArraySchema | SerializedDiscriminatedUnionSchema | SerializedEnumSchema | SerializedRichTextSchema | SerializedRecordSchema | SerializedKeyOfSchema | SerializedFileSchema | SerializedDateSchema | SerializedDateTimeSchema | SerializedColorSchema | SerializedCodeSchema | SerializedRouteSchema | SerializedLocaleSchema | SerializedSettingsSchema | SerializedImageSchema | SerializedVideoSchema | SerializedValViewSchema;
30
31
  type Primitives = number | string | boolean | null | FileSource;
31
32
  export type AssertError = {
@@ -323,6 +324,12 @@ export declare abstract class Schema<Src extends SelectorSource> {
323
324
  * from item-level declarations.
324
325
  */
325
326
  protected executePreviewItem(src: NonNullable<Src>): PreviewItem | null;
327
+ /**
328
+ * EXPERIMENTAL: the component that renders this value, for a value whose
329
+ * source is `src`. Only `s.object(...).component(...)` declares
330
+ * one; a discriminated union answers for the variant `src` takes.
331
+ */
332
+ protected executeComponentRender(src: unknown): ComponentRender<unknown> | null;
326
333
  /**
327
334
  * Could {@link executePreviewItem} ever answer? A container reifies a rows
328
335
  * preview only when its item schema says yes — asked here rather than by
@@ -1,5 +1,8 @@
1
1
  import { CustomValidateFunction, Schema, SchemaAssertResult, SelectorOfSchema, SerializedSchema } from "./index.js";
2
2
  import { ItemPreviewInput, PreviewItem, ReifiedPreview, PreviewScope } from "../preview.js";
3
+ import type { ReadOf } from "../read.js";
4
+ import { ComponentArgs, ComponentRender, SerializedComponentRender, ValComponentType } from "../render.js";
5
+ import type { LinkedToComponent } from "../source/component.js";
3
6
  import { SelectorSource } from "../selector/index.js";
4
7
  import { ModuleFilePath, SourcePath } from "../val/index.js";
5
8
  import { ValidationError, ValidationErrors } from "./validation/ValidationError.js";
@@ -13,6 +16,8 @@ export type SerializedObjectSchema = {
13
16
  readonly?: boolean;
14
17
  hidden?: boolean;
15
18
  description?: string;
19
+ /** EXPERIMENTAL. See {@link ComponentRender}. */
20
+ render?: SerializedComponentRender;
16
21
  };
17
22
  type ObjectSchemaProps = {
18
23
  [key: string]: Schema<SelectorSource>;
@@ -55,7 +60,8 @@ export declare class ObjectSchema<Props extends ObjectSchemaProps, Src extends O
55
60
  private readonly isHidden;
56
61
  private readonly description?;
57
62
  private readonly previewInput;
58
- constructor(items: Props, opt?: boolean, customValidateFunctions?: CustomValidateFunction<Src>[], isReadonly?: boolean, isHidden?: boolean, description?: string | undefined, previewInput?: ItemPreviewInput<Src> | null);
63
+ private readonly renderInput;
64
+ constructor(items: Props, opt?: boolean, customValidateFunctions?: CustomValidateFunction<Src>[], isReadonly?: boolean, isHidden?: boolean, description?: string | undefined, previewInput?: ItemPreviewInput<Src> | null, renderInput?: ComponentRender<unknown> | null);
59
65
  /**
60
66
  * Describe this field.
61
67
  *
@@ -133,6 +139,54 @@ export declare class ObjectSchema<Props extends ObjectSchemaProps, Src extends O
133
139
  * ]);
134
140
  */
135
141
  preview(select: ItemPreviewInput<Src>): ObjectSchema<Props, Src>;
142
+ /**
143
+ * EXPERIMENTAL: the component that renders this object on the site.
144
+ *
145
+ * The site renders the object with it through `ValComponentEditor`, and the
146
+ * Studio shows it next to the fields while they are edited, so the two
147
+ * cannot drift.
148
+ *
149
+ * `load` is a lazy `import()`, never the component itself: a `.val.ts` is
150
+ * evaluated outside of the app, where a component's CSS, images and
151
+ * framework code cannot be loaded. See {@link ComponentRender}.
152
+ *
153
+ * It is type checked against the component's own props. An object whose
154
+ * fields ARE the props needs nothing else. One whose fields are not - any
155
+ * component that was not written for this schema - takes a `props`
156
+ * function, which is then required and whose result is checked instead.
157
+ * `props` is given the value as a reader returns it (an image has its
158
+ * `url`), so a string it passes on unchanged can still be clicked to edit.
159
+ *
160
+ * Do not type the component's props from the schema it is linked from
161
+ * (`t.inferSchema<typeof hero>` where `hero` is the result of this call):
162
+ * the schema's type then depends on the component's and the component's on
163
+ * the schema's, and TypeScript gives up on both. Declare the fields first
164
+ * and link them in a second `const`, or let the component own its props.
165
+ *
166
+ * `example` is content that is not on the site, to preview with as well:
167
+ * long titles and empty lists that make layout problems easy to spot.
168
+ *
169
+ * @example
170
+ * const hero = s
171
+ * .object({ title: s.string() })
172
+ * .component(() => import("./HeroSection"), {
173
+ * example: { title: "A very long title that should wrap nicely" },
174
+ * });
175
+ * export default c.define("/example.val.ts", s.array(hero), []);
176
+ *
177
+ * @example
178
+ * // A component with props of its own: `props` says how to make them.
179
+ * const banner = s
180
+ * .object({ heading: s.string() })
181
+ * .component(() => import("./HeroSection"), {
182
+ * props: (value) => ({ title: value.heading }),
183
+ * });
184
+ * export default c.define("/example.val.ts", banner, { heading: "Hello" });
185
+ */
186
+ component<C extends ValComponentType>(load: () => Promise<{
187
+ default: C;
188
+ }>, ...rest: ComponentArgs<NonNullable<Src>, ReadOf<NonNullable<Src>>, C>): ObjectSchema<Props, LinkedToComponent<Src>>;
189
+ protected executeComponentRender(src: unknown): ComponentRender<unknown> | null;
136
190
  protected executePreviewItem(src: NonNullable<Src>): PreviewItem | null;
137
191
  protected declaresItemPreview(): boolean;
138
192
  protected executeSerialize(): SerializedSchema;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Type-level-only slot saying "this object is linked to a component"
3
+ * (`s.object(...).component(...)`). Nothing is ever stored under it: it is how
4
+ * the link travels from the schema, through the module, to what a reader gives
5
+ * back, so `ValComponentEditor` can refuse a value it has no component for
6
+ * where the code is written rather than where the page is opened.
7
+ */
8
+ declare const ComponentLink: unique symbol;
9
+ /**
10
+ * What `.component()` adds to an object's SOURCE type.
11
+ *
12
+ * Optional, and that is what keeps the source writable: the object in the
13
+ * `.val.ts` is still a plain literal, and an optional symbol-keyed property
14
+ * costs its author nothing to satisfy. The same shape, and the same reason, as
15
+ * the slot on `ValViewSource`.
16
+ */
17
+ export type ValComponentSource = {
18
+ readonly [ComponentLink]?: true;
19
+ };
20
+ /**
21
+ * EXPERIMENTAL. An object that is linked to a component, as consuming code
22
+ * sees it: what `useVal` / `useValRoute` / `fetchVal` give back for an
23
+ * `s.object(...).component(...)`. Its fields are all there; this is the part
24
+ * that says it can be handed to `ValComponentEditor`.
25
+ *
26
+ * Required here where it is optional on the source, so that nothing is a
27
+ * `ValComponent` by accident: an object of a schema with no component does
28
+ * not have it, and neither does a union with one variant that was not linked.
29
+ */
30
+ export type ValComponent = {
31
+ readonly [ComponentLink]: true;
32
+ };
33
+ /** Does this source type carry the slot `.component()` adds? */
34
+ export type IsValComponentSource<T> = typeof ComponentLink extends keyof T ? true : false;
35
+ /**
36
+ * A schema's source type, linked: every object in it gains the slot, and a
37
+ * `null` (a nullable schema) stays one.
38
+ */
39
+ export type LinkedToComponent<Src> = Src extends null ? Src : Src & ValComponentSource;
40
+ export {};