@rohal12/spindle 0.52.5 → 0.52.6

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.
Files changed (41) hide show
  1. package/dist/pkg/format.js +1 -1
  2. package/dist/pkg/headless.js +651 -544
  3. package/dist/pkg/macro-registry.json +255 -0
  4. package/package.json +1 -1
  5. package/src/components/macros/Button.tsx +2 -4
  6. package/src/components/macros/Checkbox.tsx +6 -13
  7. package/src/components/macros/Computed.tsx +38 -55
  8. package/src/components/macros/Cycle.tsx +1 -0
  9. package/src/components/macros/Dialog.tsx +7 -8
  10. package/src/components/macros/Do.tsx +2 -1
  11. package/src/components/macros/For.tsx +27 -78
  12. package/src/components/macros/Goto.tsx +6 -9
  13. package/src/components/macros/Include.tsx +10 -57
  14. package/src/components/macros/Listbox.tsx +1 -0
  15. package/src/components/macros/MacroError.tsx +37 -0
  16. package/src/components/macros/MacroLink.tsx +9 -24
  17. package/src/components/macros/Meter.tsx +19 -37
  18. package/src/components/macros/Numberbox.tsx +5 -32
  19. package/src/components/macros/PassageDisplay.tsx +37 -47
  20. package/src/components/macros/Print.tsx +1 -0
  21. package/src/components/macros/Radiobutton.tsx +8 -38
  22. package/src/components/macros/Repeat.tsx +3 -3
  23. package/src/components/macros/Set.tsx +3 -29
  24. package/src/components/macros/Switch.tsx +36 -37
  25. package/src/components/macros/Textarea.tsx +2 -28
  26. package/src/components/macros/Textbox.tsx +2 -29
  27. package/src/components/macros/Type.tsx +3 -3
  28. package/src/components/macros/Unset.tsx +17 -40
  29. package/src/components/macros/Watch.tsx +23 -91
  30. package/src/components/macros/WidgetInvocation.tsx +9 -71
  31. package/src/components/macros/detached-body.tsx +2 -2
  32. package/src/components/macros/input-macro.ts +48 -0
  33. package/src/components/macros/locals-scope.tsx +60 -0
  34. package/src/components/macros/macro-args.ts +280 -0
  35. package/src/components/macros/option-utils.ts +7 -12
  36. package/src/define-macro.ts +47 -30
  37. package/src/hooks/use-render-options.ts +26 -0
  38. package/src/markup/render.tsx +2 -4
  39. package/src/registry.ts +48 -0
  40. package/types/index.d.ts +69 -6
  41. package/types/tooling.d.ts +33 -1
package/types/index.d.ts CHANGED
@@ -239,6 +239,35 @@ export interface StorageQuota {
239
239
  estimateSupported: boolean;
240
240
  }
241
241
 
242
+ /**
243
+ * How a macro argument is read into `ctx.args`. Quoted strings accept `\"`,
244
+ * `\'` and `\\` escapes.
245
+ * - `expression`: code, as written (the default).
246
+ * - `variable`: a variable reference such as `$name` or `"$name"`, as written.
247
+ * - `string`: one quoted string; anything else leaves the argument unset.
248
+ * - `text`: one quoted string, or text with any loose quotes stripped.
249
+ * - `names`: a comma-separated list of names (`@item, @i`).
250
+ * - `delay`: a duration (`2s`, `500ms`, `300`) in milliseconds.
251
+ * - `number`: a number.
252
+ * - `flag`: a keyword, the parameter's name, at the start or end; a boolean.
253
+ * - `separator`: a word (`of`) or `=` separating the parameters before it
254
+ * from those after it; a boolean.
255
+ * - `options`: keywords, the names of its `parameters`, each followed by a
256
+ * quoted string or a number unless it is a flag.
257
+ * @see {@link ../../src/registry.ts} for the implementation.
258
+ */
259
+ export type ParameterType =
260
+ | 'expression'
261
+ | 'variable'
262
+ | 'string'
263
+ | 'text'
264
+ | 'names'
265
+ | 'delay'
266
+ | 'number'
267
+ | 'flag'
268
+ | 'separator'
269
+ | 'options';
270
+
242
271
  /**
243
272
  * Parameter metadata for a macro definition.
244
273
  * @see {@link ../../src/registry.ts} for the implementation.
@@ -247,8 +276,32 @@ export interface ParameterDef {
247
276
  name: string;
248
277
  required?: boolean;
249
278
  description?: string;
279
+ /** How the argument is read (default `expression`). */
280
+ type?: ParameterType;
281
+ /** The options of an `options` parameter. */
282
+ parameters?: readonly ParameterDef[];
250
283
  }
251
284
 
285
+ type ArgValue<T, D> = T extends 'flag' | 'separator'
286
+ ? boolean
287
+ : T extends 'names'
288
+ ? string[] | undefined
289
+ : T extends 'delay' | 'number'
290
+ ? number | undefined
291
+ : T extends 'options'
292
+ ? D extends { parameters: infer Q extends readonly ParameterDef[] }
293
+ ? Partial<MacroArgs<Q>>
294
+ : Partial<MacroArgs>
295
+ : string | undefined;
296
+
297
+ /**
298
+ * A macro's arguments (`ctx.args`), by parameter name.
299
+ * @see {@link ../../src/registry.ts} for the implementation.
300
+ */
301
+ export type MacroArgs<P extends readonly ParameterDef[] = ParameterDef[]> = {
302
+ [D in P[number] as D['name']]: ArgValue<D['type'], D>;
303
+ };
304
+
252
305
  /**
253
306
  * Metadata about a registered macro, returned by `Story.getMacroRegistry()`.
254
307
  * @see {@link ../../src/registry.ts} for the implementation.
@@ -363,7 +416,9 @@ export interface UseActionOptions {
363
416
  * this package), so `ctx.hooks.useState<T>()`, `ctx.h()` etc. are fully typed.
364
417
  * @see {@link ../../src/define-macro.ts} for the implementation.
365
418
  */
366
- export interface MacroContext {
419
+ export interface MacroContext<A = MacroArgs> {
420
+ /** The arguments, read into the macro's declared `parameters`. */
421
+ args: A;
367
422
  className?: string;
368
423
  id?: string;
369
424
  resolve?: (s: string | undefined) => string | undefined;
@@ -437,7 +492,9 @@ export interface MacroTextContext {
437
492
  * Configuration object for `Story.defineMacro()`.
438
493
  * @see {@link ../../src/define-macro.ts} for the implementation.
439
494
  */
440
- export interface MacroDefinition {
495
+ export interface MacroDefinition<
496
+ P extends readonly ParameterDef[] = ParameterDef[],
497
+ > {
441
498
  name: string;
442
499
  /** Sub-macro names (e.g. `['option']`); a non-empty list makes the macro a block macro. */
443
500
  subMacros?: string[];
@@ -451,15 +508,21 @@ export interface MacroDefinition {
451
508
  storeVar?: boolean;
452
509
  /** Tooling hint: one-line description shown by editors. */
453
510
  description?: string;
454
- /** Tooling hint: positional parameters. */
455
- parameters?: ParameterDef[];
456
- render: (props: MacroProps, ctx: MacroContext) => ComponentChildren;
511
+ /** The parameters: read into `ctx.args`, and shown by tooling. */
512
+ parameters?: P;
513
+ render: (
514
+ props: MacroProps,
515
+ ctx: MacroContext<MacroArgs<P>>,
516
+ ) => ComponentChildren;
457
517
  /**
458
518
  * The macro's text form, used where markup becomes a string: HTML
459
519
  * attribute values, image alt text and link titles, macro labels. Without
460
520
  * one, the macro can't be used there and is reported as an error.
461
521
  */
462
- text?: (props: MacroProps, ctx: MacroTextContext) => string;
522
+ text?: (
523
+ props: MacroProps,
524
+ ctx: MacroTextContext & { args: MacroArgs<P> },
525
+ ) => string;
463
526
  }
464
527
 
465
528
  /**
@@ -1,7 +1,39 @@
1
+ /**
2
+ * How a macro argument is read into `ctx.args`. Quoted strings accept `\"`,
3
+ * `\'` and `\\` escapes.
4
+ * - `expression`: code, as written (the default).
5
+ * - `variable`: a variable reference such as `$name` or `"$name"`, as written.
6
+ * - `string`: one quoted string; anything else leaves the argument unset.
7
+ * - `text`: one quoted string, or text with any loose quotes stripped.
8
+ * - `names`: a comma-separated list of names (`@item, @i`).
9
+ * - `delay`: a duration (`2s`, `500ms`, `300`) in milliseconds.
10
+ * - `number`: a number.
11
+ * - `flag`: a keyword, the parameter's name, at the start or end; a boolean.
12
+ * - `separator`: a word (`of`) or `=` separating the parameters before it
13
+ * from those after it; a boolean.
14
+ * - `options`: keywords, the names of its `parameters`, each followed by a
15
+ * quoted string or a number unless it is a flag.
16
+ */
17
+ export type ParameterType =
18
+ | 'expression'
19
+ | 'variable'
20
+ | 'string'
21
+ | 'text'
22
+ | 'names'
23
+ | 'delay'
24
+ | 'number'
25
+ | 'flag'
26
+ | 'separator'
27
+ | 'options';
28
+
1
29
  export interface ParameterDef {
2
30
  name: string;
3
31
  required?: boolean;
4
32
  description?: string;
33
+ /** How the argument is read (default `expression`). */
34
+ type?: ParameterType;
35
+ /** The options of an `options` parameter. */
36
+ parameters?: readonly ParameterDef[];
5
37
  }
6
38
 
7
39
  export interface MacroMetadata {
@@ -24,7 +56,7 @@ export interface MacroDefinition {
24
56
  merged?: boolean;
25
57
  storeVar?: boolean;
26
58
  description?: string;
27
- parameters?: ParameterDef[];
59
+ parameters?: readonly ParameterDef[];
28
60
  render: (...args: any[]) => any;
29
61
  text?: (...args: any[]) => string;
30
62
  }