@rohal12/spindle 0.59.23 → 0.59.26
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/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +694 -407
- package/dist/pkg/story-variables.js +510 -123
- package/dist/pkg/tooling.js +4 -0
- package/package.json +23 -24
- package/src/automation/load-yaml.ts +1 -1
- package/src/code-check.ts +23 -9
- package/src/components/PassageDialog.tsx +28 -1
- package/src/components/macros/MacroError.tsx +1 -1
- package/src/components/macros/MenubarButtons.tsx +3 -0
- package/src/components/macros/SaveManager.tsx +2 -2
- package/src/components/macros/SettingsControls.tsx +10 -6
- package/src/components/macros/Type.tsx +1 -1
- package/src/components/macros/detached-body.tsx +62 -3
- package/src/components/macros/macro-args.ts +1 -2
- package/src/execute-mutation.ts +4 -3
- package/src/hooks/use-modal-focus.ts +63 -20
- package/src/hooks/use-story-fields.ts +1 -1
- package/src/markup/ast.ts +1 -5
- package/src/markup/code-attributes.ts +1 -3
- package/src/markup/code-end.ts +1 -1
- package/src/markup/line-column.ts +1 -1
- package/src/markup/pair.ts +1 -4
- package/src/markup/render.tsx +25 -13
- package/src/settings.ts +9 -0
- package/src/static-literal.ts +1 -6
- package/src/store.ts +1 -1
- package/src/story-variables.ts +291 -121
- package/src/tooling.ts +89 -1
- package/src/widgets/widget-def.ts +93 -22
- package/types/index.d.ts +2 -10
- package/types/tooling.d.ts +126 -17
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
* Reading widget definitions without rendering anything: the `{widget}`
|
|
3
3
|
* macro, startup and markup validation (also in tooling) share these.
|
|
4
4
|
*/
|
|
5
|
-
import { parseMacroArgs } from '../components/macros/macro-args';
|
|
5
|
+
import { parseMacroArgs, type ArgSpans } from '../components/macros/macro-args';
|
|
6
6
|
import type { MacroArgs } from '../registry';
|
|
7
7
|
import { tokenizeMarkupTolerant } from '../markup/parse';
|
|
8
|
+
import type { MacroToken } from '../markup/tokens';
|
|
8
9
|
import type { ParametersOf } from '../code-check';
|
|
9
10
|
import { registeredParameters, tokenTextContainsChildren } from './ast-scanner';
|
|
10
11
|
|
|
@@ -39,39 +40,74 @@ export function parseWidgetDef(rawArgs: string): WidgetDef {
|
|
|
39
40
|
return widgetDef(parseMacroArgs(rawArgs, WIDGET_PARAMETERS));
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
/** A passage that may define widgets, as the tooling reads it. */
|
|
44
|
+
export interface WidgetPassage {
|
|
45
|
+
name: string;
|
|
46
|
+
tags?: string[];
|
|
47
|
+
content: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
42
50
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
* A widget defined by a `{widget}` macro, with where it is written (UTF-16
|
|
52
|
+
* offsets into the passage content).
|
|
53
|
+
*/
|
|
54
|
+
export interface WidgetDefinition extends WidgetDef {
|
|
55
|
+
/** Whether its body renders `{@children}`. */
|
|
56
|
+
block: boolean;
|
|
57
|
+
/** The passage defining it. */
|
|
58
|
+
passage: string;
|
|
59
|
+
/** The opening `{widget …}` tag. */
|
|
60
|
+
start: number;
|
|
61
|
+
end: number;
|
|
62
|
+
/** The name as written, without quotes. */
|
|
63
|
+
nameStart: number;
|
|
64
|
+
nameEnd: number;
|
|
65
|
+
/** The `{/widget}` closer, absent while the definition is not closed. */
|
|
66
|
+
closeStart?: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Where the name is written in the arguments, without its quotes. */
|
|
70
|
+
function nameSpan(rawArgs: string): [number, number] {
|
|
71
|
+
const spans: ArgSpans = new Map();
|
|
72
|
+
parseMacroArgs(rawArgs, WIDGET_PARAMETERS, spans);
|
|
73
|
+
const [start, end] = spans.get(WIDGET_PARAMETERS[0]) ?? [0, 0];
|
|
74
|
+
const quote = rawArgs[start];
|
|
75
|
+
return (quote === '"' || quote === "'") && end - start >= 2
|
|
76
|
+
? [start + 1, rawArgs[end - 1] === quote ? end - 1 : end]
|
|
77
|
+
: [start, end];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The widgets that StoryInit and the passages tagged `widget` define, in
|
|
82
|
+
* source order: the ones the runtime registers, read tolerantly (a half-typed
|
|
83
|
+
* definition is still reported, without a `closeStart`). Definitions whose
|
|
84
|
+
* arguments cannot be read are left out.
|
|
47
85
|
*
|
|
48
86
|
* The passages are read as flat tokens, which need no block macros to be
|
|
49
87
|
* known, so a `{@children}` in an HTML comment or in a `{do}` body is text
|
|
50
|
-
* here as it is to the parser, and does not make a block widget (#387).
|
|
88
|
+
* here as it is to the parser, and does not make a block widget (#387). In
|
|
89
|
+
* nested definitions, it belongs to the innermost.
|
|
51
90
|
*/
|
|
52
|
-
export function
|
|
53
|
-
passages: Iterable<
|
|
91
|
+
export function widgetDefinitions(
|
|
92
|
+
passages: Iterable<WidgetPassage>,
|
|
54
93
|
parametersOf: ParametersOf = registeredParameters,
|
|
55
|
-
):
|
|
56
|
-
const
|
|
94
|
+
): WidgetDefinition[] {
|
|
95
|
+
const found: WidgetDefinition[] = [];
|
|
57
96
|
for (const passage of passages) {
|
|
58
97
|
if (passage.name !== 'StoryInit' && !passage.tags?.includes('widget')) {
|
|
59
98
|
continue;
|
|
60
99
|
}
|
|
61
|
-
// The open definitions: their
|
|
62
|
-
const open: {
|
|
100
|
+
// The open definitions: their tags, and whether a slot was seen
|
|
101
|
+
const open: { token: MacroToken; isBlock: boolean; closeStart?: number }[] =
|
|
102
|
+
[];
|
|
103
|
+
const done: typeof open = [];
|
|
63
104
|
for (const token of tokenizeMarkupTolerant(passage.content).tokens) {
|
|
64
105
|
if (token.type === 'macro' && token.name.toLowerCase() === 'widget') {
|
|
65
106
|
if (!token.isClose) {
|
|
66
|
-
open.push({
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (!def?.isBlock) continue;
|
|
71
|
-
try {
|
|
72
|
-
names.push(parseWidgetDef(def.rawArgs).name);
|
|
73
|
-
} catch {
|
|
74
|
-
// The {widget} macro reports a definition it cannot read
|
|
107
|
+
open.push({ token, isBlock: false });
|
|
108
|
+
} else {
|
|
109
|
+
const def = open.pop();
|
|
110
|
+
if (def) done.push({ ...def, closeStart: token.start });
|
|
75
111
|
}
|
|
76
112
|
} else if (
|
|
77
113
|
open.length > 0 &&
|
|
@@ -83,6 +119,41 @@ export function blockWidgetNames(
|
|
|
83
119
|
open[open.length - 1]!.isBlock = true;
|
|
84
120
|
}
|
|
85
121
|
}
|
|
122
|
+
done.push(...open);
|
|
123
|
+
const defs: WidgetDefinition[] = [];
|
|
124
|
+
for (const { token, isBlock, closeStart } of done) {
|
|
125
|
+
try {
|
|
126
|
+
const [from, to] = nameSpan(token.rawArgs);
|
|
127
|
+
defs.push({
|
|
128
|
+
...parseWidgetDef(token.rawArgs),
|
|
129
|
+
block: isBlock,
|
|
130
|
+
passage: passage.name,
|
|
131
|
+
start: token.start,
|
|
132
|
+
end: token.end,
|
|
133
|
+
nameStart: token.argsStart + from,
|
|
134
|
+
nameEnd: token.argsStart + to,
|
|
135
|
+
...(closeStart === undefined ? {} : { closeStart }),
|
|
136
|
+
});
|
|
137
|
+
} catch {
|
|
138
|
+
// The {widget} macro reports a definition it cannot read
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
found.push(...defs.sort((a, b) => a.start - b.start));
|
|
86
142
|
}
|
|
87
|
-
return
|
|
143
|
+
return found;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The names of the block widgets (those whose body renders `{@children}`)
|
|
148
|
+
* that StoryInit and the passages tagged `widget` define, closed. They must
|
|
149
|
+
* be known as block macros before any passage is parsed, so that passages
|
|
150
|
+
* invoking them nest their content whatever the passage order.
|
|
151
|
+
*/
|
|
152
|
+
export function blockWidgetNames(
|
|
153
|
+
passages: Iterable<WidgetPassage>,
|
|
154
|
+
parametersOf: ParametersOf = registeredParameters,
|
|
155
|
+
): string[] {
|
|
156
|
+
return widgetDefinitions(passages, parametersOf)
|
|
157
|
+
.filter((def) => def.block && def.closeStart !== undefined)
|
|
158
|
+
.map((def) => def.name);
|
|
88
159
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -287,11 +287,7 @@ export type ParameterType =
|
|
|
287
287
|
* @see {@link ../../src/registry.ts} for the implementation.
|
|
288
288
|
*/
|
|
289
289
|
export type StringHolds =
|
|
290
|
-
| '
|
|
291
|
-
| 'text'
|
|
292
|
-
| 'passage'
|
|
293
|
-
| 'expression'
|
|
294
|
-
| 'statements';
|
|
290
|
+
'markup' | 'text' | 'passage' | 'expression' | 'statements';
|
|
295
291
|
|
|
296
292
|
/**
|
|
297
293
|
* Parameter metadata for a macro definition.
|
|
@@ -401,11 +397,7 @@ export interface HtmlNode {
|
|
|
401
397
|
* @see {@link ../../src/markup/ast.ts} for the implementation.
|
|
402
398
|
*/
|
|
403
399
|
export type ASTNode =
|
|
404
|
-
|
|
|
405
|
-
| VariableNode
|
|
406
|
-
| ExpressionNode
|
|
407
|
-
| MacroNode
|
|
408
|
-
| HtmlNode;
|
|
400
|
+
TextNode | VariableNode | ExpressionNode | MacroNode | HtmlNode;
|
|
409
401
|
|
|
410
402
|
/**
|
|
411
403
|
* Props passed to a macro's render function.
|
package/types/tooling.d.ts
CHANGED
|
@@ -44,11 +44,7 @@ export type ParameterType =
|
|
|
44
44
|
* any other `text`.
|
|
45
45
|
*/
|
|
46
46
|
export type StringHolds =
|
|
47
|
-
| '
|
|
48
|
-
| 'text'
|
|
49
|
-
| 'passage'
|
|
50
|
-
| 'expression'
|
|
51
|
-
| 'statements';
|
|
47
|
+
'markup' | 'text' | 'passage' | 'expression' | 'statements';
|
|
52
48
|
|
|
53
49
|
export interface ParameterDef {
|
|
54
50
|
name: string;
|
|
@@ -101,12 +97,7 @@ export declare function getMacroRegistry(): MacroMetadata[];
|
|
|
101
97
|
|
|
102
98
|
/** Variable type inferred from a StoryVariables/StoryTransients default value. */
|
|
103
99
|
export type VarType =
|
|
104
|
-
| '
|
|
105
|
-
| 'string'
|
|
106
|
-
| 'boolean'
|
|
107
|
-
| 'array'
|
|
108
|
-
| 'object'
|
|
109
|
-
| 'null';
|
|
100
|
+
'number' | 'string' | 'boolean' | 'array' | 'object' | 'null';
|
|
110
101
|
|
|
111
102
|
/** Inferred shape of a declared variable (or one of its object fields). */
|
|
112
103
|
export interface FieldSchema {
|
|
@@ -580,8 +571,7 @@ export declare function transform(expr: string, goal?: JsGoal): string;
|
|
|
580
571
|
|
|
581
572
|
/** What a `passage` argument is: a quoted name, or an expression. */
|
|
582
573
|
export type PassageTarget =
|
|
583
|
-
|
|
584
|
-
| { kind: 'expression'; expression: string };
|
|
574
|
+
{ kind: 'name'; name: string } | { kind: 'expression'; expression: string };
|
|
585
575
|
|
|
586
576
|
/**
|
|
587
577
|
* Read a `passage` argument (`{goto "Hall"}`, `{include $room}`) as written:
|
|
@@ -652,6 +642,8 @@ export interface ToolingMacro {
|
|
|
652
642
|
parameters?: readonly ParameterDef[];
|
|
653
643
|
/** Whether it resolves markup: its strings hold markup by default. */
|
|
654
644
|
interpolate?: boolean;
|
|
645
|
+
/** Whether its first argument names the story variable it binds. */
|
|
646
|
+
storeVar?: boolean;
|
|
655
647
|
}
|
|
656
648
|
|
|
657
649
|
/**
|
|
@@ -680,6 +672,126 @@ export declare function collectStoryPassageReferences(
|
|
|
680
672
|
macros: Iterable<ToolingMacro>,
|
|
681
673
|
): PassageReference[];
|
|
682
674
|
|
|
675
|
+
// ---------------------------------------------------------------------------
|
|
676
|
+
// Variable references
|
|
677
|
+
// ---------------------------------------------------------------------------
|
|
678
|
+
|
|
679
|
+
/** A reference to a story (`$`) or transient (`%`) variable in markup. */
|
|
680
|
+
export interface VariableReference {
|
|
681
|
+
sigil: '$' | '%';
|
|
682
|
+
/** The variable, without the sigil. */
|
|
683
|
+
name: string;
|
|
684
|
+
/** The fields accessed with dots (`$a.b.c`: `['b', 'c']`). */
|
|
685
|
+
path: string[];
|
|
686
|
+
/** Where `$a.b.c` is written: `[start, end)` UTF-16 offsets into the markup. */
|
|
687
|
+
start: number;
|
|
688
|
+
end: number;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/**
|
|
692
|
+
* What is declared. A schema is absent for a variable whose initializer is not
|
|
693
|
+
* static (`parseDeclarations`): it is declared, and its fields are not checked.
|
|
694
|
+
*/
|
|
695
|
+
export interface VariableDeclarations {
|
|
696
|
+
variables: ReadonlyMap<string, FieldSchema | undefined>;
|
|
697
|
+
/** The transients (`%`); not checked when absent, as at story start. */
|
|
698
|
+
transients?: ReadonlyMap<string, FieldSchema | undefined>;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
export type VariableDiagnosticCode =
|
|
702
|
+
| 'undeclared-variable'
|
|
703
|
+
| 'undeclared-transient'
|
|
704
|
+
| 'reserved-name'
|
|
705
|
+
| 'primitive-field';
|
|
706
|
+
|
|
707
|
+
/** A variable reference that is not valid against the declarations. */
|
|
708
|
+
export interface VariableDiagnostic {
|
|
709
|
+
passage: string;
|
|
710
|
+
code: VariableDiagnosticCode;
|
|
711
|
+
/** The variable, without the sigil. */
|
|
712
|
+
name: string;
|
|
713
|
+
/** The fields accessed with dots, if any. */
|
|
714
|
+
path?: string[];
|
|
715
|
+
/** The reference, as offsets into the passage content. */
|
|
716
|
+
start: number;
|
|
717
|
+
end: number;
|
|
718
|
+
/** The message the story start shows (after `Passage "name": `). */
|
|
719
|
+
message: string;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* The variable references the markup of a passage evaluates, in source order,
|
|
724
|
+
* with their positions. Malformed tags are skipped. The macros are those the
|
|
725
|
+
* markup may use (`builtinMacros` and the user-defined ones).
|
|
726
|
+
*/
|
|
727
|
+
export declare function variableReferences(
|
|
728
|
+
source: string,
|
|
729
|
+
macros?: Iterable<ToolingMacro>,
|
|
730
|
+
): VariableReference[];
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* The variable references of the passages that are not valid against the
|
|
734
|
+
* declarations, as the story start checks them: undeclared variables (and
|
|
735
|
+
* transients, when `transients` is given), and fields accessed on primitives
|
|
736
|
+
* that have no such member. Unknown fields of objects are valid (classes may
|
|
737
|
+
* add members), as are any fields of arrays and `null` defaults. Tolerant of
|
|
738
|
+
* half-typed markup; evaluates no project code.
|
|
739
|
+
*/
|
|
740
|
+
export declare function validateVariableReferences(
|
|
741
|
+
passages: Iterable<MarkupPassage>,
|
|
742
|
+
declarations: VariableDeclarations,
|
|
743
|
+
macros?: Iterable<ToolingMacro>,
|
|
744
|
+
): VariableDiagnostic[];
|
|
745
|
+
|
|
746
|
+
// ---------------------------------------------------------------------------
|
|
747
|
+
// Widget definitions
|
|
748
|
+
// ---------------------------------------------------------------------------
|
|
749
|
+
|
|
750
|
+
/** What a `{widget}` definition's arguments declare. */
|
|
751
|
+
export interface WidgetDef {
|
|
752
|
+
name: string;
|
|
753
|
+
/** The `@` parameters. */
|
|
754
|
+
params: string[];
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/**
|
|
758
|
+
* A widget defined by a `{widget}` macro. The offsets are UTF-16 offsets into
|
|
759
|
+
* the content of `passage`.
|
|
760
|
+
*/
|
|
761
|
+
export interface WidgetDefinition extends WidgetDef {
|
|
762
|
+
/** Whether its body renders `{@children}`: it takes a closing tag. */
|
|
763
|
+
block: boolean;
|
|
764
|
+
passage: string;
|
|
765
|
+
/** The opening `{widget …}` tag. */
|
|
766
|
+
start: number;
|
|
767
|
+
end: number;
|
|
768
|
+
/** The name as written, without quotes. */
|
|
769
|
+
nameStart: number;
|
|
770
|
+
nameEnd: number;
|
|
771
|
+
/** The `{/widget}` closer; absent while the definition is not closed. */
|
|
772
|
+
closeStart?: number;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* The name and `@` parameters a `{widget}` macro reads from its arguments
|
|
777
|
+
* (`"Wrap" @a @b`), as the runtime reads them. Throws where the macro
|
|
778
|
+
* reports the arguments.
|
|
779
|
+
*/
|
|
780
|
+
export declare function parseWidgetDef(rawArgs: string): WidgetDef;
|
|
781
|
+
|
|
782
|
+
/**
|
|
783
|
+
* The widgets the story defines, as the runtime registers them: those of
|
|
784
|
+
* `StoryInit` and of the passages tagged `widget`, in source order. Block-ness
|
|
785
|
+
* is decided on tokens, so a `{@children}` in an HTML comment or a `{do}` body
|
|
786
|
+
* does not count; one in an attribute value or label of the `macros` does.
|
|
787
|
+
* Tolerant: a half-typed definition is reported without `closeStart`, and one
|
|
788
|
+
* whose arguments cannot be read is left out.
|
|
789
|
+
*/
|
|
790
|
+
export declare function widgetDefinitions(
|
|
791
|
+
passages: Iterable<MarkupPassage>,
|
|
792
|
+
macros?: Iterable<ToolingMacro>,
|
|
793
|
+
): WidgetDefinition[];
|
|
794
|
+
|
|
683
795
|
// ---------------------------------------------------------------------------
|
|
684
796
|
// Pieces of a passage
|
|
685
797
|
// ---------------------------------------------------------------------------
|
|
@@ -800,10 +912,7 @@ export declare function passagePieces(
|
|
|
800
912
|
|
|
801
913
|
/** What is wrong with the pairing of tags (a subset of {@link MarkupErrorCode}). */
|
|
802
914
|
export type PairingErrorCode =
|
|
803
|
-
|
|
804
|
-
| 'mismatched-closer'
|
|
805
|
-
| 'stray-closer'
|
|
806
|
-
| 'misplaced-branch';
|
|
915
|
+
'unclosed-block' | 'mismatched-closer' | 'stray-closer' | 'misplaced-branch';
|
|
807
916
|
|
|
808
917
|
/** A problem in the pairing of the tokens. */
|
|
809
918
|
export interface PairingError {
|