@pwfabric/authoring 0.0.0-stage → 1.0.0-rc.0
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/LICENSE +202 -0
- package/README.md +13 -2
- package/dist/define-block.d.ts +112 -0
- package/dist/define-block.d.ts.map +1 -0
- package/dist/define-block.js +297 -0
- package/dist/define-block.js.map +1 -0
- package/dist/define-factory.d.ts +114 -0
- package/dist/define-factory.d.ts.map +1 -0
- package/dist/define-factory.js +247 -0
- package/dist/define-factory.js.map +1 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/dist/registry.d.ts +165 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +277 -0
- package/dist/registry.js.map +1 -0
- package/dist/types.d.ts +272 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/validation.d.ts +81 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +217 -0
- package/dist/validation.js.map +1 -0
- package/package.json +57 -3
package/dist/registry.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CustomBlockRegistry
|
|
3
|
+
*
|
|
4
|
+
* Registry for managing custom block definitions.
|
|
5
|
+
*
|
|
6
|
+
* @module @pwfabric/authoring/registry
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { createCustomBlockRegistry, defineBlock } from '@pwfabric/authoring'
|
|
11
|
+
* import { z } from 'zod'
|
|
12
|
+
*
|
|
13
|
+
* const registry = createCustomBlockRegistry()
|
|
14
|
+
*
|
|
15
|
+
* const MyBlock = defineBlock({
|
|
16
|
+
* type: 'my-block',
|
|
17
|
+
* name: 'My Block',
|
|
18
|
+
* schema: z.object({ title: z.string() }),
|
|
19
|
+
* defaultProps: { title: 'Default' },
|
|
20
|
+
* component: ({ props }) => <div>{props.title}</div>
|
|
21
|
+
* })
|
|
22
|
+
*
|
|
23
|
+
* registry.register(MyBlock)
|
|
24
|
+
*
|
|
25
|
+
* // Use with SurfaceRenderer
|
|
26
|
+
* <SurfaceRenderer components={registry.getComponents()} />
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
import { createElement } from 'react';
|
|
30
|
+
// ============================================================
|
|
31
|
+
// CUSTOM BLOCK REGISTRY
|
|
32
|
+
// ============================================================
|
|
33
|
+
/**
|
|
34
|
+
* CustomBlockRegistry
|
|
35
|
+
*
|
|
36
|
+
* Manages registration and retrieval of custom block definitions.
|
|
37
|
+
* Provides a bridge between custom block definitions and the
|
|
38
|
+
* SurfaceRenderer component registry.
|
|
39
|
+
*/
|
|
40
|
+
export class CustomBlockRegistry {
|
|
41
|
+
definitions = new Map();
|
|
42
|
+
/**
|
|
43
|
+
* Register a custom block definition
|
|
44
|
+
*
|
|
45
|
+
* @param definition - The custom block definition to register
|
|
46
|
+
* @throws Error if block type already registered
|
|
47
|
+
*/
|
|
48
|
+
register(definition) {
|
|
49
|
+
if (this.definitions.has(definition.type)) {
|
|
50
|
+
throw new Error(`Block type "${definition.type}" is already registered`);
|
|
51
|
+
}
|
|
52
|
+
this.definitions.set(definition.type, definition);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Register multiple custom block definitions
|
|
56
|
+
*
|
|
57
|
+
* @param definitions - Array of custom block definitions
|
|
58
|
+
*/
|
|
59
|
+
registerMany(definitions) {
|
|
60
|
+
for (const definition of definitions) {
|
|
61
|
+
this.register(definition);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Get a custom block definition by type
|
|
66
|
+
*
|
|
67
|
+
* @param type - Block type identifier
|
|
68
|
+
* @returns The block definition or undefined
|
|
69
|
+
*/
|
|
70
|
+
get(type) {
|
|
71
|
+
return this.definitions.get(type);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Check if a block type is registered
|
|
75
|
+
*
|
|
76
|
+
* @param type - Block type identifier
|
|
77
|
+
* @returns True if registered
|
|
78
|
+
*/
|
|
79
|
+
has(type) {
|
|
80
|
+
return this.definitions.has(type);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Get all registered custom block definitions
|
|
84
|
+
*
|
|
85
|
+
* @returns Array of all block definitions
|
|
86
|
+
*/
|
|
87
|
+
list() {
|
|
88
|
+
return Array.from(this.definitions.values());
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get block definitions by category
|
|
92
|
+
*
|
|
93
|
+
* @param category - Block category
|
|
94
|
+
* @returns Array of block definitions in category
|
|
95
|
+
*/
|
|
96
|
+
getByCategory(category) {
|
|
97
|
+
return this.list().filter((def) => def.category === category);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Get all registered block types
|
|
101
|
+
*
|
|
102
|
+
* @returns Array of block type identifiers
|
|
103
|
+
*/
|
|
104
|
+
types() {
|
|
105
|
+
return Array.from(this.definitions.keys());
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Unregister a custom block
|
|
109
|
+
*
|
|
110
|
+
* @param type - Block type identifier
|
|
111
|
+
* @returns True if block was unregistered
|
|
112
|
+
*/
|
|
113
|
+
unregister(type) {
|
|
114
|
+
return this.definitions.delete(type);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Clear all registrations
|
|
118
|
+
*/
|
|
119
|
+
clear() {
|
|
120
|
+
this.definitions.clear();
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Get number of registered blocks
|
|
124
|
+
*/
|
|
125
|
+
get size() {
|
|
126
|
+
return this.definitions.size;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get a component registry for use with SurfaceRenderer
|
|
130
|
+
*
|
|
131
|
+
* This method creates a BlockComponentRegistry that can be passed
|
|
132
|
+
* to the SurfaceRenderer's `components` prop to enable rendering
|
|
133
|
+
* of custom blocks.
|
|
134
|
+
*
|
|
135
|
+
* @returns BlockComponentRegistry with all registered custom blocks
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```typescript
|
|
139
|
+
* import { SurfaceRenderer } from '@pwfabric/runtime'
|
|
140
|
+
*
|
|
141
|
+
* const registry = createCustomBlockRegistry()
|
|
142
|
+
* registry.register(MyCustomBlock)
|
|
143
|
+
*
|
|
144
|
+
* // Pass to SurfaceRenderer
|
|
145
|
+
* <SurfaceRenderer
|
|
146
|
+
* surface={mySurface}
|
|
147
|
+
* components={registry.getComponents()}
|
|
148
|
+
* />
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
getComponents() {
|
|
152
|
+
const components = {};
|
|
153
|
+
for (const definition of this.definitions.values()) {
|
|
154
|
+
components[definition.type] = createBlockComponentWrapper(definition);
|
|
155
|
+
}
|
|
156
|
+
return components;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Create a wrapper component that adapts CustomBlockComponent to BlockComponent
|
|
161
|
+
*
|
|
162
|
+
* This bridges the gap between the SDK's CustomBlockComponent interface
|
|
163
|
+
* and the runtime's BlockComponent interface.
|
|
164
|
+
*/
|
|
165
|
+
/**
|
|
166
|
+
* Map runtime BlockContext to SDK CustomBlockContext
|
|
167
|
+
*
|
|
168
|
+
* Narrows the full runtime context to only what custom block authors need.
|
|
169
|
+
* Excludes: capabilities, imageAdapter, basePath (platform internals).
|
|
170
|
+
*/
|
|
171
|
+
function mapToCustomBlockContext(runtimeContext) {
|
|
172
|
+
if (!runtimeContext || typeof runtimeContext !== 'object')
|
|
173
|
+
return undefined;
|
|
174
|
+
const ctx = runtimeContext;
|
|
175
|
+
return {
|
|
176
|
+
theme: ctx.theme ?? 'light',
|
|
177
|
+
viewport: ctx.viewport ?? 'lg',
|
|
178
|
+
onEvent: typeof ctx.onEvent === 'function'
|
|
179
|
+
? (event) => {
|
|
180
|
+
;
|
|
181
|
+
ctx.onEvent(event);
|
|
182
|
+
}
|
|
183
|
+
: undefined,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function createBlockComponentWrapper(definition) {
|
|
187
|
+
return function WrappedComponent({ block, props: rawProps, children, context, }) {
|
|
188
|
+
// Validate and apply defaults using the schema
|
|
189
|
+
const result = definition.schema.safeParse(rawProps);
|
|
190
|
+
const validatedProps = result.success ? result.data : definition.defaultProps;
|
|
191
|
+
// Map runtime context to custom block context
|
|
192
|
+
const customContext = mapToCustomBlockContext(context);
|
|
193
|
+
// Create the custom block component props
|
|
194
|
+
const customProps = {
|
|
195
|
+
props: validatedProps,
|
|
196
|
+
blockId: block.id,
|
|
197
|
+
children,
|
|
198
|
+
context: customContext,
|
|
199
|
+
};
|
|
200
|
+
// Render using the custom component
|
|
201
|
+
return createElement(definition.component, customProps);
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
// ============================================================
|
|
205
|
+
// FACTORY FUNCTIONS
|
|
206
|
+
// ============================================================
|
|
207
|
+
/**
|
|
208
|
+
* Create a new CustomBlockRegistry
|
|
209
|
+
*
|
|
210
|
+
* @returns A new CustomBlockRegistry instance
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```typescript
|
|
214
|
+
* const registry = createCustomBlockRegistry()
|
|
215
|
+
* registry.register(MyBlock)
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
export function createCustomBlockRegistry() {
|
|
219
|
+
return new CustomBlockRegistry();
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get a singleton instance of CustomBlockRegistry
|
|
223
|
+
*
|
|
224
|
+
* Use this when you want a shared registry across your application.
|
|
225
|
+
* For isolated registries (e.g., in tests), use `createCustomBlockRegistry()`.
|
|
226
|
+
*/
|
|
227
|
+
let globalRegistry = null;
|
|
228
|
+
export function getCustomBlockRegistry() {
|
|
229
|
+
if (!globalRegistry) {
|
|
230
|
+
globalRegistry = new CustomBlockRegistry();
|
|
231
|
+
}
|
|
232
|
+
return globalRegistry;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Reset the global registry
|
|
236
|
+
*
|
|
237
|
+
* Useful for testing or when you need to clear all registrations.
|
|
238
|
+
*/
|
|
239
|
+
export function resetGlobalRegistry() {
|
|
240
|
+
globalRegistry = null;
|
|
241
|
+
}
|
|
242
|
+
// ============================================================
|
|
243
|
+
// MERGE UTILITIES
|
|
244
|
+
// ============================================================
|
|
245
|
+
/**
|
|
246
|
+
* Merge multiple custom block registries into one
|
|
247
|
+
*
|
|
248
|
+
* @param registries - Registries to merge
|
|
249
|
+
* @returns A new registry containing all blocks from input registries
|
|
250
|
+
* @throws Error if there are duplicate block types
|
|
251
|
+
*/
|
|
252
|
+
export function mergeRegistries(...registries) {
|
|
253
|
+
const merged = new CustomBlockRegistry();
|
|
254
|
+
for (const registry of registries) {
|
|
255
|
+
for (const definition of registry.list()) {
|
|
256
|
+
merged.register(definition);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return merged;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Merge a custom block registry with a base BlockComponentRegistry
|
|
263
|
+
*
|
|
264
|
+
* This is useful when you want to extend the default block components
|
|
265
|
+
* with custom blocks.
|
|
266
|
+
*
|
|
267
|
+
* @param baseComponents - The base component registry (e.g., defaultBlockComponents)
|
|
268
|
+
* @param customRegistry - The custom block registry
|
|
269
|
+
* @returns A merged BlockComponentRegistry
|
|
270
|
+
*/
|
|
271
|
+
export function mergeWithBaseComponents(baseComponents, customRegistry) {
|
|
272
|
+
return {
|
|
273
|
+
...baseComponents,
|
|
274
|
+
...customRegistry.getComponents(),
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;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ExC,KAAK,MAAM,QAAQ,IAAI,UAAU,EAAE,CAAC;QAClC,KAAK,MAAM,UAAU,IAAI,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC;YACzC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAA;QAC7B,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,uBAAuB,CACrC,cAAsC,EACtC,cAAmC;IAEnC,OAAO;QACL,GAAG,cAAc;QACjB,GAAG,cAAc,CAAC,aAAa,EAAE;KAClC,CAAA;AACH,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @pwfabric/authoring Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* Core type definitions for the Custom Block SDK.
|
|
5
|
+
*
|
|
6
|
+
* @module @pwfabric/authoring/types
|
|
7
|
+
*/
|
|
8
|
+
import type { ZodSchema, ZodTypeDef } from 'zod';
|
|
9
|
+
import type { Block, BlockCategory } from '@pwfabric/core';
|
|
10
|
+
import type { BlockComponentRegistry } from '@pwfabric/runtime';
|
|
11
|
+
import type { ReactNode } from 'react';
|
|
12
|
+
/**
|
|
13
|
+
* Custom block definition options
|
|
14
|
+
*
|
|
15
|
+
* Used with `defineBlock()` to create a type-safe custom block.
|
|
16
|
+
*/
|
|
17
|
+
export interface CustomBlockOptions<TProps extends Record<string, unknown> = Record<string, unknown>, TSchema extends ZodSchema<TProps, ZodTypeDef, unknown> = ZodSchema<TProps>> {
|
|
18
|
+
/** Unique block type identifier (kebab-case) */
|
|
19
|
+
readonly type: string;
|
|
20
|
+
/** Human-readable display name */
|
|
21
|
+
readonly name: string;
|
|
22
|
+
/** Block category for grouping */
|
|
23
|
+
readonly category?: BlockCategory;
|
|
24
|
+
/** Icon character or identifier */
|
|
25
|
+
readonly icon?: string;
|
|
26
|
+
/** Zod schema for props validation */
|
|
27
|
+
readonly schema: TSchema;
|
|
28
|
+
/** Default props for new instances */
|
|
29
|
+
readonly defaultProps: TProps;
|
|
30
|
+
/** Whether this block can contain children */
|
|
31
|
+
readonly canHaveChildren?: boolean;
|
|
32
|
+
/** Restrict which block types can be children */
|
|
33
|
+
readonly allowedChildren?: readonly string[];
|
|
34
|
+
/** Restrict which block types can be parents */
|
|
35
|
+
readonly allowedParents?: readonly string[];
|
|
36
|
+
/** open trait vocabulary this block carries (kebab-case). */
|
|
37
|
+
readonly traits?: readonly string[];
|
|
38
|
+
/** children must carry ANY of these traits (containers only). */
|
|
39
|
+
readonly allowedChildTraits?: readonly string[];
|
|
40
|
+
/** Maximum instances allowed in a surface */
|
|
41
|
+
readonly maxInstances?: number;
|
|
42
|
+
/** React component to render this block */
|
|
43
|
+
readonly component: CustomBlockComponent<TProps>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Custom block context
|
|
47
|
+
*
|
|
48
|
+
* Narrowed view of the runtime BlockContext for custom block authors.
|
|
49
|
+
* Excludes platform internals (capabilities, imageAdapter, basePath).
|
|
50
|
+
*/
|
|
51
|
+
export interface CustomBlockContext {
|
|
52
|
+
/** Current theme */
|
|
53
|
+
readonly theme: 'light' | 'dark';
|
|
54
|
+
/** Current viewport breakpoint */
|
|
55
|
+
readonly viewport: string;
|
|
56
|
+
/** Event handler for block-level events */
|
|
57
|
+
readonly onEvent?: ((event: {
|
|
58
|
+
type: string;
|
|
59
|
+
blockId?: string;
|
|
60
|
+
payload?: unknown;
|
|
61
|
+
}) => void) | undefined;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Custom block component props
|
|
65
|
+
*/
|
|
66
|
+
export interface CustomBlockComponentProps<TProps = Record<string, unknown>> {
|
|
67
|
+
/** The resolved, type-safe props */
|
|
68
|
+
readonly props: TProps;
|
|
69
|
+
/** Block ID */
|
|
70
|
+
readonly blockId: string;
|
|
71
|
+
/** Rendered children (for container blocks) */
|
|
72
|
+
readonly children?: ReactNode;
|
|
73
|
+
/** Runtime context (theme, viewport, events) */
|
|
74
|
+
readonly context?: CustomBlockContext | undefined;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Custom block component type
|
|
78
|
+
*/
|
|
79
|
+
export type CustomBlockComponent<TProps = Record<string, unknown>> = (props: CustomBlockComponentProps<TProps>) => ReactNode;
|
|
80
|
+
/**
|
|
81
|
+
* Custom block definition (result of defineBlock)
|
|
82
|
+
*
|
|
83
|
+
* Contains both the block metadata and the component for rendering.
|
|
84
|
+
*/
|
|
85
|
+
export interface CustomBlockDefinition<TProps extends Record<string, unknown> = Record<string, unknown>> {
|
|
86
|
+
/** Unique block type identifier */
|
|
87
|
+
readonly type: string;
|
|
88
|
+
/** Human-readable display name */
|
|
89
|
+
readonly name: string;
|
|
90
|
+
/** Block category */
|
|
91
|
+
readonly category: BlockCategory;
|
|
92
|
+
/** Icon character or identifier */
|
|
93
|
+
readonly icon: string;
|
|
94
|
+
/** Zod schema for validation */
|
|
95
|
+
readonly schema: ZodSchema<TProps>;
|
|
96
|
+
/** Default props */
|
|
97
|
+
readonly defaultProps: TProps;
|
|
98
|
+
/** Whether this block can contain children */
|
|
99
|
+
readonly canHaveChildren: boolean;
|
|
100
|
+
/** Allowed child block types */
|
|
101
|
+
readonly allowedChildren?: readonly string[];
|
|
102
|
+
/** Allowed parent block types */
|
|
103
|
+
readonly allowedParents?: readonly string[];
|
|
104
|
+
/** open trait vocabulary this block carries (kebab-case). */
|
|
105
|
+
readonly traits?: readonly string[];
|
|
106
|
+
/** children must carry ANY of these traits (containers only). */
|
|
107
|
+
readonly allowedChildTraits?: readonly string[];
|
|
108
|
+
/** Maximum instances */
|
|
109
|
+
readonly maxInstances?: number;
|
|
110
|
+
/** React component */
|
|
111
|
+
readonly component: CustomBlockComponent<TProps>;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Factory configuration input
|
|
115
|
+
*
|
|
116
|
+
* Passed to the build function to customize the generated blocks.
|
|
117
|
+
*/
|
|
118
|
+
export type FactoryConfig = Record<string, unknown>;
|
|
119
|
+
/**
|
|
120
|
+
* Block specification for factory output
|
|
121
|
+
*
|
|
122
|
+
* Simplified block definition without requiring an id (auto-generated).
|
|
123
|
+
*/
|
|
124
|
+
export interface BlockSpec {
|
|
125
|
+
/** Block type */
|
|
126
|
+
readonly type: string;
|
|
127
|
+
/** Block props */
|
|
128
|
+
readonly props: Record<string, unknown>;
|
|
129
|
+
/** Nested children */
|
|
130
|
+
readonly children?: readonly BlockSpec[];
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Factory definition options
|
|
134
|
+
*
|
|
135
|
+
* Used with `defineFactory()` to create a factory function.
|
|
136
|
+
*/
|
|
137
|
+
export interface FactoryOptions<TConfig extends FactoryConfig = FactoryConfig> {
|
|
138
|
+
/** Unique factory identifier (kebab-case) */
|
|
139
|
+
readonly id: string;
|
|
140
|
+
/** Human-readable factory name */
|
|
141
|
+
readonly name?: string;
|
|
142
|
+
/** Factory description */
|
|
143
|
+
readonly description?: string;
|
|
144
|
+
/** Optional Zod schema for config validation */
|
|
145
|
+
readonly configSchema?: ZodSchema<TConfig>;
|
|
146
|
+
/** Build function that returns block specifications */
|
|
147
|
+
readonly build: (config: TConfig) => readonly BlockSpec[];
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Factory definition (result of defineFactory)
|
|
151
|
+
*/
|
|
152
|
+
export interface FactoryDefinition<TConfig extends FactoryConfig = FactoryConfig> {
|
|
153
|
+
/** Factory identifier */
|
|
154
|
+
readonly id: string;
|
|
155
|
+
/** Factory name */
|
|
156
|
+
readonly name: string;
|
|
157
|
+
/** Factory description */
|
|
158
|
+
readonly description: string;
|
|
159
|
+
/** Config schema (if provided) */
|
|
160
|
+
readonly configSchema?: ZodSchema<TConfig>;
|
|
161
|
+
/** Build function */
|
|
162
|
+
readonly build: (config: TConfig) => readonly Block[];
|
|
163
|
+
/** Validate config against schema (if provided) */
|
|
164
|
+
readonly validateConfig: (config: unknown) => FactoryValidationResult<TConfig>;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Factory validation result
|
|
168
|
+
*/
|
|
169
|
+
export interface FactoryValidationResult<TConfig = FactoryConfig> {
|
|
170
|
+
/** Whether validation passed */
|
|
171
|
+
readonly valid: boolean;
|
|
172
|
+
/** Validated config (if valid) */
|
|
173
|
+
readonly config?: TConfig;
|
|
174
|
+
/** Validation errors (if invalid) */
|
|
175
|
+
readonly errors?: readonly FactoryValidationError[];
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Factory validation error
|
|
179
|
+
*/
|
|
180
|
+
export interface FactoryValidationError {
|
|
181
|
+
/** Error path in the config */
|
|
182
|
+
readonly path: string;
|
|
183
|
+
/** Error message */
|
|
184
|
+
readonly message: string;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Custom block registry interface
|
|
188
|
+
*/
|
|
189
|
+
export interface ICustomBlockRegistry {
|
|
190
|
+
/**
|
|
191
|
+
* Register a custom block definition
|
|
192
|
+
*/
|
|
193
|
+
register<TProps extends Record<string, unknown>>(definition: CustomBlockDefinition<TProps>): void;
|
|
194
|
+
/**
|
|
195
|
+
* Get a custom block definition by type
|
|
196
|
+
*/
|
|
197
|
+
get(type: string): CustomBlockDefinition | undefined;
|
|
198
|
+
/**
|
|
199
|
+
* Check if a block type is registered
|
|
200
|
+
*/
|
|
201
|
+
has(type: string): boolean;
|
|
202
|
+
/**
|
|
203
|
+
* Get all registered custom block definitions
|
|
204
|
+
*/
|
|
205
|
+
list(): readonly CustomBlockDefinition[];
|
|
206
|
+
/**
|
|
207
|
+
* Get block definitions by category
|
|
208
|
+
*/
|
|
209
|
+
getByCategory(category: BlockCategory): readonly CustomBlockDefinition[];
|
|
210
|
+
/**
|
|
211
|
+
* Get all registered block types
|
|
212
|
+
*/
|
|
213
|
+
types(): readonly string[];
|
|
214
|
+
/**
|
|
215
|
+
* Unregister a custom block
|
|
216
|
+
*/
|
|
217
|
+
unregister(type: string): boolean;
|
|
218
|
+
/**
|
|
219
|
+
* Clear all registrations
|
|
220
|
+
*/
|
|
221
|
+
clear(): void;
|
|
222
|
+
/**
|
|
223
|
+
* Get a component registry for use with SurfaceRenderer
|
|
224
|
+
*/
|
|
225
|
+
getComponents(): BlockComponentRegistry;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Block validation result
|
|
229
|
+
*/
|
|
230
|
+
export interface BlockValidationResult {
|
|
231
|
+
/** Whether the block is valid */
|
|
232
|
+
readonly valid: boolean;
|
|
233
|
+
/** The validated block (with defaults applied) */
|
|
234
|
+
readonly block?: Block;
|
|
235
|
+
/** Validation errors */
|
|
236
|
+
readonly errors: readonly BlockValidationError[];
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Block validation error
|
|
240
|
+
*/
|
|
241
|
+
export interface BlockValidationError {
|
|
242
|
+
/** Error code */
|
|
243
|
+
readonly code: string;
|
|
244
|
+
/** Error message */
|
|
245
|
+
readonly message: string;
|
|
246
|
+
/** Path to the invalid property */
|
|
247
|
+
readonly path?: string;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Props validation result
|
|
251
|
+
*/
|
|
252
|
+
export interface PropsValidationResult<TProps = Record<string, unknown>> {
|
|
253
|
+
/** Whether the props are valid */
|
|
254
|
+
readonly valid: boolean;
|
|
255
|
+
/** The validated props (with defaults applied) */
|
|
256
|
+
readonly props?: TProps;
|
|
257
|
+
/** Validation errors */
|
|
258
|
+
readonly errors: readonly BlockValidationError[];
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Infer props type from a custom block definition
|
|
262
|
+
*/
|
|
263
|
+
export type InferBlockProps<T> = T extends CustomBlockDefinition<infer TProps> ? TProps : never;
|
|
264
|
+
/**
|
|
265
|
+
* Infer config type from a factory definition
|
|
266
|
+
*/
|
|
267
|
+
export type InferFactoryConfig<T> = T extends FactoryDefinition<infer TConfig> ? TConfig : never;
|
|
268
|
+
/**
|
|
269
|
+
* Infer props type from a Zod schema
|
|
270
|
+
*/
|
|
271
|
+
export type InferSchemaProps<T> = T extends ZodSchema<infer TOutput> ? TOutput : never;
|
|
272
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,KAAK,CAAA;AAChD,OAAO,KAAK,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAC1D,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AAC/D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAMtC;;;;GAIG;AACH,MAAM,WAAW,kBAAkB,CACjC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChE,OAAO,SAAS,SAAS,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC,MAAM,CAAC;IAE1E,gDAAgD;IAChD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IAErB,kCAAkC;IAClC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IAErB,kCAAkC;IAClC,QAAQ,CAAC,QAAQ,CAAC,EAAE,aAAa,CAAA;IAEjC,mCAAmC;IACnC,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;IAEtB,sCAAsC;IACtC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;IAExB,sCAAsC;IACtC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;IAE7B,8CAA8C;IAC9C,QAAQ,CAAC,eAAe,CAAC,EAAE,OAAO,CAAA;IAElC,iDAAiD;IACjD,QAAQ,CAAC,eAAe,CAAC,EAAE,SAAS,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
3
|
+
*
|
|
4
|
+
* Schema validation utilities for custom blocks.
|
|
5
|
+
*
|
|
6
|
+
* @module @pwfabric/authoring/validation
|
|
7
|
+
*/
|
|
8
|
+
import type { ZodSchema } from 'zod';
|
|
9
|
+
import type { Block } from '@pwfabric/core';
|
|
10
|
+
import type { CustomBlockDefinition, BlockValidationResult, BlockValidationError, PropsValidationResult, ICustomBlockRegistry } from './types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Validate props against a Zod schema
|
|
13
|
+
*
|
|
14
|
+
* @param props - The props to validate
|
|
15
|
+
* @param schema - The Zod schema to validate against
|
|
16
|
+
* @returns Validation result with validated props or errors
|
|
17
|
+
*/
|
|
18
|
+
export declare function validateProps<TProps extends Record<string, unknown>>(props: unknown, schema: ZodSchema<TProps>): PropsValidationResult<TProps>;
|
|
19
|
+
/**
|
|
20
|
+
* Validate props with defaults applied
|
|
21
|
+
*
|
|
22
|
+
* If validation fails, returns the default props.
|
|
23
|
+
*
|
|
24
|
+
* @param props - The props to validate
|
|
25
|
+
* @param schema - The Zod schema
|
|
26
|
+
* @param defaultProps - Default props to use if validation fails
|
|
27
|
+
* @returns Validated props or defaults
|
|
28
|
+
*/
|
|
29
|
+
export declare function validatePropsWithDefaults<TProps extends Record<string, unknown>>(props: unknown, schema: ZodSchema<TProps>, defaultProps: TProps): TProps;
|
|
30
|
+
/**
|
|
31
|
+
* Validate a block against its definition
|
|
32
|
+
*
|
|
33
|
+
* @param block - The block to validate
|
|
34
|
+
* @param definition - The block definition
|
|
35
|
+
* @returns Validation result
|
|
36
|
+
*/
|
|
37
|
+
export declare function validateBlock<TProps extends Record<string, unknown>>(block: Block, definition: CustomBlockDefinition<TProps>): BlockValidationResult;
|
|
38
|
+
/**
|
|
39
|
+
* Validate a block tree against a registry of definitions
|
|
40
|
+
*
|
|
41
|
+
* Recursively validates all blocks in the tree.
|
|
42
|
+
*
|
|
43
|
+
* @param blocks - The blocks to validate
|
|
44
|
+
* @param registry - The block registry
|
|
45
|
+
* @returns Array of validation errors (empty if all valid)
|
|
46
|
+
*/
|
|
47
|
+
export declare function validateBlockTree(blocks: readonly Block[], registry: ICustomBlockRegistry): readonly BlockValidationError[];
|
|
48
|
+
/**
|
|
49
|
+
* Create a validation function for a specific block type
|
|
50
|
+
*
|
|
51
|
+
* @param definition - The block definition
|
|
52
|
+
* @returns A validation function
|
|
53
|
+
*/
|
|
54
|
+
export declare function createBlockValidator<TProps extends Record<string, unknown>>(definition: CustomBlockDefinition<TProps>): (block: Block) => BlockValidationResult;
|
|
55
|
+
/**
|
|
56
|
+
* Create a props validation function for a specific schema
|
|
57
|
+
*
|
|
58
|
+
* @param schema - The Zod schema
|
|
59
|
+
* @returns A validation function
|
|
60
|
+
*/
|
|
61
|
+
export declare function createPropsValidator<TProps extends Record<string, unknown>>(schema: ZodSchema<TProps>): (props: unknown) => PropsValidationResult<TProps>;
|
|
62
|
+
/**
|
|
63
|
+
* Format validation errors as a human-readable string
|
|
64
|
+
*
|
|
65
|
+
* @param errors - The validation errors
|
|
66
|
+
* @returns Formatted error string
|
|
67
|
+
*/
|
|
68
|
+
export declare function formatValidationErrors(errors: readonly BlockValidationError[]): string;
|
|
69
|
+
/**
|
|
70
|
+
* Check if a validation result has errors
|
|
71
|
+
*/
|
|
72
|
+
export declare function hasErrors(result: BlockValidationResult | PropsValidationResult): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Check if a value is a valid Block
|
|
75
|
+
*/
|
|
76
|
+
export declare function isBlock(value: unknown): value is Block;
|
|
77
|
+
/**
|
|
78
|
+
* Check if a value is a valid Block array
|
|
79
|
+
*/
|
|
80
|
+
export declare function isBlockArray(value: unknown): value is Block[];
|
|
81
|
+
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,KAA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