@cadit-app/script-params 0.5.2 → 0.5.3

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,132 @@
1
+ /**
2
+ * Script module types and core functions (defineParams, createMain).
3
+ *
4
+ * This module provides the main API for defining parametric scripts.
5
+ */
6
+ import type { ParamSchema, Params } from './params';
7
+ import type { Exporters } from './exporters';
8
+ /**
9
+ * Configuration object for defineParams when using named properties.
10
+ * All options are named for clarity and extensibility.
11
+ */
12
+ export interface ScriptConfig<S extends ParamSchema, R = unknown> {
13
+ /** The parameter schema defining all inputs. */
14
+ params: S;
15
+ /** Optional main function that generates geometry. */
16
+ main?: (params: Params<S>) => R;
17
+ /** Optional exporters for custom download formats (SVG, PNG, etc.). */
18
+ exporters?: Exporters<Params<S>>;
19
+ }
20
+ /**
21
+ * The result returned by defineParams.
22
+ * Contains the schema (for UI), optional exporters, and is optionally callable.
23
+ */
24
+ export interface ScriptModule<S extends ParamSchema, R = unknown> {
25
+ /** The parameter schema for UI generation. */
26
+ params: S;
27
+ /** Optional exporters for custom download formats. */
28
+ exporters?: Exporters<Params<S>>;
29
+ /**
30
+ * Execute the script with optional parameter overrides.
31
+ * Missing parameters use their default values.
32
+ * Only available if a main function was provided.
33
+ */
34
+ (inputParams?: Partial<Params<S>>): R;
35
+ }
36
+ /**
37
+ * Infer the params type from a ScriptModule.
38
+ * Use this to get the typed params for use in exporters defined in separate files.
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * // In main.ts
43
+ * const script = defineParams({
44
+ * params: {
45
+ * size: { type: 'number', default: 10 },
46
+ * label: { type: 'text', default: 'Hello' },
47
+ * },
48
+ * main: (p) => { ... },
49
+ * });
50
+ * export default script;
51
+ *
52
+ * // In pngExport.ts
53
+ * import type { Exporter, InferParams } from '@cadit-app/script-params';
54
+ * import type script from './main';
55
+ *
56
+ * type QrCodeParams = InferParams<typeof script>;
57
+ * // { size: number; label: string }
58
+ *
59
+ * export const pngExporter: Exporter<QrCodeParams> = {
60
+ * name: 'PNG',
61
+ * export: (params) => { ... }, // params.size is number, params.label is string
62
+ * };
63
+ * ```
64
+ */
65
+ export type InferParams<T> = T extends ScriptModule<infer S, unknown> ? Params<S> : T extends {
66
+ params: infer S extends ParamSchema;
67
+ } ? Params<S> : never;
68
+ /**
69
+ * Build default values object from a schema.
70
+ * @internal
71
+ */
72
+ declare function buildDefaults<S extends ParamSchema>(schema: S): Params<S>;
73
+ /**
74
+ * Define parameters for a parametric script.
75
+ *
76
+ * Pass a config object with `params` (required), and optionally `main` and `exporters`.
77
+ *
78
+ * @example Basic usage
79
+ * ```typescript
80
+ * export default defineParams({
81
+ * params: {
82
+ * size: { type: 'number', default: 10 },
83
+ * },
84
+ * main: (params) => Manifold.cube([params.size, params.size, params.size]),
85
+ * });
86
+ * ```
87
+ *
88
+ * @example With exporters
89
+ * ```typescript
90
+ * export default defineParams({
91
+ * params: {
92
+ * size: { type: 'number', default: 10 },
93
+ * },
94
+ * exporters: {
95
+ * svg: { name: 'SVG', export: (p) => svgExport(p) },
96
+ * png: { name: 'PNG', export: (p) => pngExport(p) },
97
+ * },
98
+ * main: (params) => Manifold.cube([params.size, params.size, params.size]),
99
+ * });
100
+ * ```
101
+ *
102
+ * @example Without main (add main separately with createMain)
103
+ * ```typescript
104
+ * const script = defineParams({
105
+ * params: { size: { type: 'number', default: 10 } },
106
+ * exporters: { svg: svgExporter },
107
+ * });
108
+ * export default createMain(script, myMainFunction);
109
+ * ```
110
+ */
111
+ export declare function defineParams<const S extends ParamSchema, R = unknown>(config: ScriptConfig<S, R>): ScriptModule<S, R>;
112
+ /**
113
+ * Create an executable script module by adding a main function.
114
+ *
115
+ * Use this when you want to define your main function separately from the params,
116
+ * for example when the main is complex or needs to be tested independently.
117
+ *
118
+ * Preserves any exporters defined on the original ScriptModule.
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * const script = defineParams({
123
+ * params: { size: { type: 'number', default: 10 } },
124
+ * exporters: { svg: svgExporter },
125
+ * });
126
+ *
127
+ * export default createMain(script, (p) => Manifold.cube([p.size, ...]));
128
+ * ```
129
+ */
130
+ export declare function createMain<S extends ParamSchema, R>(scriptModule: ScriptModule<S, unknown>, main: (params: Params<S>) => R): ScriptModule<S, R>;
131
+ export { buildDefaults as _buildDefaults };
132
+ //# sourceMappingURL=script.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script.d.ts","sourceRoot":"","sources":["../src/script.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAoB,MAAM,UAAU,CAAC;AACtE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAM7C;;;GAGG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,GAAG,OAAO;IAC9D,gDAAgD;IAChD,MAAM,EAAE,CAAC,CAAC;IACV,sDAAsD;IACtD,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;IAChC,uEAAuE;IACvE,SAAS,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;CAClC;AAED;;;GAGG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,GAAG,OAAO;IAC9D,8CAA8C;IAC9C,MAAM,EAAE,CAAC,CAAC;IACV,sDAAsD;IACtD,SAAS,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IACjC;;;;OAIG;IACH,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;CACvC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,SAAS,YAAY,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,GACjE,MAAM,CAAC,CAAC,CAAC,GACT,CAAC,SAAS;IAAE,MAAM,EAAE,MAAM,CAAC,SAAS,WAAW,CAAA;CAAE,GAC/C,MAAM,CAAC,CAAC,CAAC,GACT,KAAK,CAAC;AAMZ;;;GAGG;AACH,iBAAS,aAAa,CAAC,CAAC,SAAS,WAAW,EAAE,MAAM,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAgBlE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,YAAY,CAAC,KAAK,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,GAAG,OAAO,EACnE,MAAM,EAAE,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,GACzB,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,CAmBpB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,EACjD,YAAY,EAAE,YAAY,CAAC,CAAC,EAAE,OAAO,CAAC,EACtC,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,GAC7B,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,CAcpB;AAGD,OAAO,EAAE,aAAa,IAAI,cAAc,EAAE,CAAC"}
package/dist/script.js ADDED
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Script module types and core functions (defineParams, createMain).
3
+ *
4
+ * This module provides the main API for defining parametric scripts.
5
+ */
6
+ // =============================================================================
7
+ // Core Functions
8
+ // =============================================================================
9
+ /**
10
+ * Build default values object from a schema.
11
+ * @internal
12
+ */
13
+ function buildDefaults(schema) {
14
+ const defaults = {};
15
+ for (const [key, def] of Object.entries(schema)) {
16
+ if (def.type === 'embedded') {
17
+ // For embedded params, build a nested default object
18
+ const embeddedDef = def;
19
+ defaults[key] = {
20
+ enabled: embeddedDef.enabled ?? false,
21
+ showSettings: embeddedDef.showSettings ?? false,
22
+ params: buildDefaults(embeddedDef.params),
23
+ };
24
+ }
25
+ else {
26
+ defaults[key] = def.default;
27
+ }
28
+ }
29
+ return defaults;
30
+ }
31
+ /**
32
+ * Define parameters for a parametric script.
33
+ *
34
+ * Pass a config object with `params` (required), and optionally `main` and `exporters`.
35
+ *
36
+ * @example Basic usage
37
+ * ```typescript
38
+ * export default defineParams({
39
+ * params: {
40
+ * size: { type: 'number', default: 10 },
41
+ * },
42
+ * main: (params) => Manifold.cube([params.size, params.size, params.size]),
43
+ * });
44
+ * ```
45
+ *
46
+ * @example With exporters
47
+ * ```typescript
48
+ * export default defineParams({
49
+ * params: {
50
+ * size: { type: 'number', default: 10 },
51
+ * },
52
+ * exporters: {
53
+ * svg: { name: 'SVG', export: (p) => svgExport(p) },
54
+ * png: { name: 'PNG', export: (p) => pngExport(p) },
55
+ * },
56
+ * main: (params) => Manifold.cube([params.size, params.size, params.size]),
57
+ * });
58
+ * ```
59
+ *
60
+ * @example Without main (add main separately with createMain)
61
+ * ```typescript
62
+ * const script = defineParams({
63
+ * params: { size: { type: 'number', default: 10 } },
64
+ * exporters: { svg: svgExporter },
65
+ * });
66
+ * export default createMain(script, myMainFunction);
67
+ * ```
68
+ */
69
+ export function defineParams(config) {
70
+ const { params: schema, main, exporters } = config;
71
+ const defaults = buildDefaults(schema);
72
+ const scriptModule = ((inputParams) => {
73
+ if (!main) {
74
+ throw new Error('This script does not have a main function. Use createMain() to add one.');
75
+ }
76
+ const merged = { ...defaults, ...inputParams };
77
+ return main(merged);
78
+ });
79
+ scriptModule.params = schema;
80
+ if (exporters) {
81
+ scriptModule.exporters = exporters;
82
+ }
83
+ return scriptModule;
84
+ }
85
+ /**
86
+ * Create an executable script module by adding a main function.
87
+ *
88
+ * Use this when you want to define your main function separately from the params,
89
+ * for example when the main is complex or needs to be tested independently.
90
+ *
91
+ * Preserves any exporters defined on the original ScriptModule.
92
+ *
93
+ * @example
94
+ * ```typescript
95
+ * const script = defineParams({
96
+ * params: { size: { type: 'number', default: 10 } },
97
+ * exporters: { svg: svgExporter },
98
+ * });
99
+ *
100
+ * export default createMain(script, (p) => Manifold.cube([p.size, ...]));
101
+ * ```
102
+ */
103
+ export function createMain(scriptModule, main) {
104
+ const defaults = buildDefaults(scriptModule.params);
105
+ const newModule = ((inputParams) => {
106
+ const merged = { ...defaults, ...inputParams };
107
+ return main(merged);
108
+ });
109
+ newModule.params = scriptModule.params;
110
+ if (scriptModule.exporters) {
111
+ newModule.exporters = scriptModule.exporters;
112
+ }
113
+ return newModule;
114
+ }
115
+ // Re-export buildDefaults for internal use
116
+ export { buildDefaults as _buildDefaults };
117
+ //# sourceMappingURL=script.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script.js","sourceRoot":"","sources":["../src/script.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA0EH,gFAAgF;AAChF,iBAAiB;AACjB,gFAAgF;AAEhF;;;GAGG;AACH,SAAS,aAAa,CAAwB,MAAS;IACrD,MAAM,QAAQ,GAAG,EAAe,CAAC;IACjC,KAAK,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QAChD,IAAI,GAAG,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;YAC5B,qDAAqD;YACrD,MAAM,WAAW,GAAG,GAAuB,CAAC;YAC3C,QAAoC,CAAC,GAAG,CAAC,GAAG;gBAC3C,OAAO,EAAE,WAAW,CAAC,OAAO,IAAI,KAAK;gBACrC,YAAY,EAAE,WAAW,CAAC,YAAY,IAAI,KAAK;gBAC/C,MAAM,EAAE,aAAa,CAAC,WAAW,CAAC,MAAM,CAAC;aAC1C,CAAC;QACJ,CAAC;aAAM,CAAC;YACL,QAAoC,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,OAAO,CAAC;QAC3D,CAAC;IACH,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;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AAO,EAAE,aAAa,IAAI,cAAc,EAAE,CAAC"}
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Utility functions for working with script-params.
3
+ *
4
+ * Includes conversion utilities, validation functions, and helpers for
5
+ * embedding parameters from other scripts.
6
+ */
7
+ import type { ParamSchema, Params, EmbeddedParamDef } from './params';
8
+ import type { ScriptModule } from './script';
9
+ import type { Exporters } from './exporters';
10
+ /**
11
+ * Convert an object-based schema to array format for legacy UI compatibility.
12
+ *
13
+ * This function transforms the script-params schema format into the
14
+ * ObjectMakerParam[] format expected by the Core UI.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const schema = { size: { type: 'number', default: 10 } };
19
+ * const array = schemaToArray(schema);
20
+ * // [{ name: 'size', type: 'number', initial: 10, caption: 'size' }]
21
+ * ```
22
+ */
23
+ export declare function schemaToArray<S extends ParamSchema>(schema: S): Array<Record<string, unknown>>;
24
+ /**
25
+ * Extract defaults from a schema as a plain object.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * const schema = {
30
+ * size: { type: 'number', default: 10 },
31
+ * label: { type: 'text', default: 'Hi' },
32
+ * };
33
+ * const defaults = getDefaults(schema);
34
+ * // { size: 10, label: 'Hi' }
35
+ * ```
36
+ */
37
+ export declare function getDefaults<S extends ParamSchema>(schema: S): Params<S>;
38
+ /**
39
+ * Check if a module export uses the defineParams format.
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const mod = await import('./script.ts');
44
+ * if (isScriptModule(mod.default)) {
45
+ * const result = mod.default({ size: 20 });
46
+ * }
47
+ * ```
48
+ */
49
+ export declare function isScriptModule(value: unknown): value is ScriptModule<ParamSchema, unknown>;
50
+ /**
51
+ * Extract the parameter schema from a ScriptModule.
52
+ * Useful for embedding another script's parameters.
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * import qrCodeScript from './qr-code';
57
+ *
58
+ * const qrParams = getParams(qrCodeScript);
59
+ *
60
+ * // Use in embedded param definition
61
+ * export default defineParams({
62
+ * params: {
63
+ * qrCode: {
64
+ * type: 'embedded',
65
+ * params: getParams(qrCodeScript),
66
+ * enabled: false,
67
+ * },
68
+ * },
69
+ * });
70
+ * ```
71
+ */
72
+ export declare function getParams<S extends ParamSchema>(source: ScriptModule<S, unknown>): S;
73
+ /**
74
+ * Extract exporters from a ScriptModule.
75
+ * Returns an empty object if the module has no exporters.
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * import qrCodeScript from './qr-code';
80
+ *
81
+ * const exporters = getExporters(qrCodeScript);
82
+ * // { svg: { name: 'SVG', ... }, png: { name: 'PNG', ... } }
83
+ * ```
84
+ */
85
+ export declare function getExporters<S extends ParamSchema>(source: ScriptModule<S, unknown>): Exporters<Params<S>>;
86
+ /**
87
+ * Helper to create an embedded param definition with npm package metadata.
88
+ * This enables relative asset paths (like buttonGrid images) to be resolved
89
+ * correctly when embedding another npm package's parameters.
90
+ *
91
+ * @param npmPackage - The npm package name (e.g., '@cadit-app/qr-code')
92
+ * @param embeddedDef - The embedded param definition
93
+ * @returns The embedded definition with npmPackage attached
94
+ *
95
+ * @example
96
+ * ```typescript
97
+ * import qrCodeModule from '@cadit-app/qr-code';
98
+ * import { defineParams, embedParams } from '@cadit-app/script-params';
99
+ *
100
+ * export default defineParams({
101
+ * params: {
102
+ * qrCodeSettings: embedParams('@cadit-app/qr-code', {
103
+ * type: 'embedded',
104
+ * label: 'QR Code (Optional)',
105
+ * params: qrCodeModule.params,
106
+ * enabled: false,
107
+ * showSettings: false,
108
+ * }),
109
+ * },
110
+ * main: (params) => { ... },
111
+ * });
112
+ * ```
113
+ */
114
+ export declare function embedParams(npmPackage: string, embeddedDef: Omit<EmbeddedParamDef, 'npmPackage'>): EmbeddedParamDef;
115
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAY,MAAM,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAChF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AA2F7C;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,WAAW,EACjD,MAAM,EAAE,CAAC,GACR,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAEhC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,WAAW,EAAE,MAAM,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAEvE;AAMD;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,YAAY,CAAC,WAAW,EAAE,OAAO,CAAC,CAO7C;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,WAAW,EAC7C,MAAM,EAAE,YAAY,CAAC,CAAC,EAAE,OAAO,CAAC,GAC/B,CAAC,CAEH;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,CAAC,SAAS,WAAW,EAChD,MAAM,EAAE,YAAY,CAAC,CAAC,EAAE,OAAO,CAAC,GAC/B,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAEtB;AAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,WAAW,CACzB,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,IAAI,CAAC,gBAAgB,EAAE,YAAY,CAAC,GAChD,gBAAgB,CAKlB"}
package/dist/utils.js ADDED
@@ -0,0 +1,224 @@
1
+ /**
2
+ * Utility functions for working with script-params.
3
+ *
4
+ * Includes conversion utilities, validation functions, and helpers for
5
+ * embedding parameters from other scripts.
6
+ */
7
+ import { _buildDefaults } from './script';
8
+ // =============================================================================
9
+ // Schema Conversion Utilities
10
+ // =============================================================================
11
+ /**
12
+ * Convert a single param def to ObjectMakerParam format.
13
+ * Handles special cases like embedded params that need recursive conversion.
14
+ * @internal
15
+ */
16
+ function convertParamDef(name, def) {
17
+ const base = {
18
+ name,
19
+ type: def.type,
20
+ caption: def.label ?? name,
21
+ };
22
+ // Copy common properties
23
+ if ('default' in def) {
24
+ base.initial = def.default;
25
+ }
26
+ if ('min' in def)
27
+ base.min = def.min;
28
+ if ('max' in def)
29
+ base.max = def.max;
30
+ if ('step' in def)
31
+ base.step = def.step;
32
+ if ('maxLength' in def)
33
+ base.maxLength = def.maxLength;
34
+ // Handle type-specific conversions
35
+ switch (def.type) {
36
+ case 'choice':
37
+ case 'radio': {
38
+ // Handle both simple string options and { value, label } objects
39
+ const options = def.options;
40
+ const values = [];
41
+ const captions = [];
42
+ let hasCaptions = false;
43
+ for (const opt of options) {
44
+ if (typeof opt === 'string') {
45
+ values.push(opt);
46
+ captions.push(opt); // Use value as caption if no label
47
+ }
48
+ else {
49
+ values.push(opt.value);
50
+ captions.push(opt.label ?? opt.value);
51
+ if (opt.label)
52
+ hasCaptions = true;
53
+ }
54
+ }
55
+ base.values = values;
56
+ // Only set captions if at least one option has a label
57
+ if (hasCaptions) {
58
+ base.captions = captions;
59
+ }
60
+ break;
61
+ }
62
+ case 'buttonGrid': {
63
+ // Convert buttonGrid options to ObjectMakerButtonGridParam format
64
+ base.options = def.options.map((opt) => ({
65
+ value: opt.value,
66
+ image: opt.image,
67
+ caption: opt.caption,
68
+ }));
69
+ break;
70
+ }
71
+ case 'preset': {
72
+ // Convert preset options to ObjectMakerPresetParam format
73
+ base.presets = def.presets.map((preset) => ({
74
+ value: preset.value,
75
+ label: preset.label,
76
+ presetValues: preset.presetValues,
77
+ }));
78
+ break;
79
+ }
80
+ case 'embedded': {
81
+ // Convert embedded params recursively
82
+ const embeddedDef = def;
83
+ base.parameters = schemaToArray(embeddedDef.params);
84
+ base.enabled = embeddedDef.enabled ?? false;
85
+ base.showSettings = embeddedDef.showSettings ?? false;
86
+ // Propagate npm package for resolving relative asset URLs
87
+ if (embeddedDef.npmPackage) {
88
+ base.npmPackage = embeddedDef.npmPackage;
89
+ }
90
+ break;
91
+ }
92
+ }
93
+ return base;
94
+ }
95
+ /**
96
+ * Convert an object-based schema to array format for legacy UI compatibility.
97
+ *
98
+ * This function transforms the script-params schema format into the
99
+ * ObjectMakerParam[] format expected by the Core UI.
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * const schema = { size: { type: 'number', default: 10 } };
104
+ * const array = schemaToArray(schema);
105
+ * // [{ name: 'size', type: 'number', initial: 10, caption: 'size' }]
106
+ * ```
107
+ */
108
+ export function schemaToArray(schema) {
109
+ return Object.entries(schema).map(([name, def]) => convertParamDef(name, def));
110
+ }
111
+ /**
112
+ * Extract defaults from a schema as a plain object.
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * const schema = {
117
+ * size: { type: 'number', default: 10 },
118
+ * label: { type: 'text', default: 'Hi' },
119
+ * };
120
+ * const defaults = getDefaults(schema);
121
+ * // { size: 10, label: 'Hi' }
122
+ * ```
123
+ */
124
+ export function getDefaults(schema) {
125
+ return _buildDefaults(schema);
126
+ }
127
+ // =============================================================================
128
+ // ScriptModule Utilities
129
+ // =============================================================================
130
+ /**
131
+ * Check if a module export uses the defineParams format.
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * const mod = await import('./script.ts');
136
+ * if (isScriptModule(mod.default)) {
137
+ * const result = mod.default({ size: 20 });
138
+ * }
139
+ * ```
140
+ */
141
+ export function isScriptModule(value) {
142
+ return (typeof value === 'function' &&
143
+ 'params' in value &&
144
+ typeof value.params === 'object' &&
145
+ value.params !== null);
146
+ }
147
+ /**
148
+ * Extract the parameter schema from a ScriptModule.
149
+ * Useful for embedding another script's parameters.
150
+ *
151
+ * @example
152
+ * ```typescript
153
+ * import qrCodeScript from './qr-code';
154
+ *
155
+ * const qrParams = getParams(qrCodeScript);
156
+ *
157
+ * // Use in embedded param definition
158
+ * export default defineParams({
159
+ * params: {
160
+ * qrCode: {
161
+ * type: 'embedded',
162
+ * params: getParams(qrCodeScript),
163
+ * enabled: false,
164
+ * },
165
+ * },
166
+ * });
167
+ * ```
168
+ */
169
+ export function getParams(source) {
170
+ return source.params;
171
+ }
172
+ /**
173
+ * Extract exporters from a ScriptModule.
174
+ * Returns an empty object if the module has no exporters.
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * import qrCodeScript from './qr-code';
179
+ *
180
+ * const exporters = getExporters(qrCodeScript);
181
+ * // { svg: { name: 'SVG', ... }, png: { name: 'PNG', ... } }
182
+ * ```
183
+ */
184
+ export function getExporters(source) {
185
+ return source.exporters ?? {};
186
+ }
187
+ // =============================================================================
188
+ // Embedding Utilities
189
+ // =============================================================================
190
+ /**
191
+ * Helper to create an embedded param definition with npm package metadata.
192
+ * This enables relative asset paths (like buttonGrid images) to be resolved
193
+ * correctly when embedding another npm package's parameters.
194
+ *
195
+ * @param npmPackage - The npm package name (e.g., '@cadit-app/qr-code')
196
+ * @param embeddedDef - The embedded param definition
197
+ * @returns The embedded definition with npmPackage attached
198
+ *
199
+ * @example
200
+ * ```typescript
201
+ * import qrCodeModule from '@cadit-app/qr-code';
202
+ * import { defineParams, embedParams } from '@cadit-app/script-params';
203
+ *
204
+ * export default defineParams({
205
+ * params: {
206
+ * qrCodeSettings: embedParams('@cadit-app/qr-code', {
207
+ * type: 'embedded',
208
+ * label: 'QR Code (Optional)',
209
+ * params: qrCodeModule.params,
210
+ * enabled: false,
211
+ * showSettings: false,
212
+ * }),
213
+ * },
214
+ * main: (params) => { ... },
215
+ * });
216
+ * ```
217
+ */
218
+ export function embedParams(npmPackage, embeddedDef) {
219
+ return {
220
+ ...embeddedDef,
221
+ npmPackage,
222
+ };
223
+ }
224
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;G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tB,gFAAgF;AAEhF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,WAAW,CACzB,UAAkB,EAClB,WAAiD;IAEjD,OAAO;QACL,GAAG,WAAW;QACd,UAAU;KACX,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cadit-app/script-params",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "Type-safe parameter definitions for CADit scripts",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",