@rohal12/spindle 0.59.23 → 0.59.25
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 +373 -140
- package/dist/pkg/story-variables.js +383 -37
- package/dist/pkg/tooling.js +4 -0
- package/package.json +1 -1
- package/src/code-check.ts +23 -9
- package/src/components/PassageDialog.tsx +28 -1
- package/src/components/macros/MenubarButtons.tsx +3 -0
- 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/hooks/use-modal-focus.ts +62 -19
- package/src/markup/render.tsx +15 -4
- package/src/settings.ts +9 -0
- package/src/story-variables.ts +290 -115
- package/src/tooling.ts +89 -1
- package/src/widgets/widget-def.ts +93 -22
- package/types/tooling.d.ts +122 -0
|
@@ -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/tooling.d.ts
CHANGED
|
@@ -652,6 +652,8 @@ export interface ToolingMacro {
|
|
|
652
652
|
parameters?: readonly ParameterDef[];
|
|
653
653
|
/** Whether it resolves markup: its strings hold markup by default. */
|
|
654
654
|
interpolate?: boolean;
|
|
655
|
+
/** Whether its first argument names the story variable it binds. */
|
|
656
|
+
storeVar?: boolean;
|
|
655
657
|
}
|
|
656
658
|
|
|
657
659
|
/**
|
|
@@ -680,6 +682,126 @@ export declare function collectStoryPassageReferences(
|
|
|
680
682
|
macros: Iterable<ToolingMacro>,
|
|
681
683
|
): PassageReference[];
|
|
682
684
|
|
|
685
|
+
// ---------------------------------------------------------------------------
|
|
686
|
+
// Variable references
|
|
687
|
+
// ---------------------------------------------------------------------------
|
|
688
|
+
|
|
689
|
+
/** A reference to a story (`$`) or transient (`%`) variable in markup. */
|
|
690
|
+
export interface VariableReference {
|
|
691
|
+
sigil: '$' | '%';
|
|
692
|
+
/** The variable, without the sigil. */
|
|
693
|
+
name: string;
|
|
694
|
+
/** The fields accessed with dots (`$a.b.c`: `['b', 'c']`). */
|
|
695
|
+
path: string[];
|
|
696
|
+
/** Where `$a.b.c` is written: `[start, end)` UTF-16 offsets into the markup. */
|
|
697
|
+
start: number;
|
|
698
|
+
end: number;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* What is declared. A schema is absent for a variable whose initializer is not
|
|
703
|
+
* static (`parseDeclarations`): it is declared, and its fields are not checked.
|
|
704
|
+
*/
|
|
705
|
+
export interface VariableDeclarations {
|
|
706
|
+
variables: ReadonlyMap<string, FieldSchema | undefined>;
|
|
707
|
+
/** The transients (`%`); not checked when absent, as at story start. */
|
|
708
|
+
transients?: ReadonlyMap<string, FieldSchema | undefined>;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
export type VariableDiagnosticCode =
|
|
712
|
+
| 'undeclared-variable'
|
|
713
|
+
| 'undeclared-transient'
|
|
714
|
+
| 'reserved-name'
|
|
715
|
+
| 'primitive-field';
|
|
716
|
+
|
|
717
|
+
/** A variable reference that is not valid against the declarations. */
|
|
718
|
+
export interface VariableDiagnostic {
|
|
719
|
+
passage: string;
|
|
720
|
+
code: VariableDiagnosticCode;
|
|
721
|
+
/** The variable, without the sigil. */
|
|
722
|
+
name: string;
|
|
723
|
+
/** The fields accessed with dots, if any. */
|
|
724
|
+
path?: string[];
|
|
725
|
+
/** The reference, as offsets into the passage content. */
|
|
726
|
+
start: number;
|
|
727
|
+
end: number;
|
|
728
|
+
/** The message the story start shows (after `Passage "name": `). */
|
|
729
|
+
message: string;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/**
|
|
733
|
+
* The variable references the markup of a passage evaluates, in source order,
|
|
734
|
+
* with their positions. Malformed tags are skipped. The macros are those the
|
|
735
|
+
* markup may use (`builtinMacros` and the user-defined ones).
|
|
736
|
+
*/
|
|
737
|
+
export declare function variableReferences(
|
|
738
|
+
source: string,
|
|
739
|
+
macros?: Iterable<ToolingMacro>,
|
|
740
|
+
): VariableReference[];
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* The variable references of the passages that are not valid against the
|
|
744
|
+
* declarations, as the story start checks them: undeclared variables (and
|
|
745
|
+
* transients, when `transients` is given), and fields accessed on primitives
|
|
746
|
+
* that have no such member. Unknown fields of objects are valid (classes may
|
|
747
|
+
* add members), as are any fields of arrays and `null` defaults. Tolerant of
|
|
748
|
+
* half-typed markup; evaluates no project code.
|
|
749
|
+
*/
|
|
750
|
+
export declare function validateVariableReferences(
|
|
751
|
+
passages: Iterable<MarkupPassage>,
|
|
752
|
+
declarations: VariableDeclarations,
|
|
753
|
+
macros?: Iterable<ToolingMacro>,
|
|
754
|
+
): VariableDiagnostic[];
|
|
755
|
+
|
|
756
|
+
// ---------------------------------------------------------------------------
|
|
757
|
+
// Widget definitions
|
|
758
|
+
// ---------------------------------------------------------------------------
|
|
759
|
+
|
|
760
|
+
/** What a `{widget}` definition's arguments declare. */
|
|
761
|
+
export interface WidgetDef {
|
|
762
|
+
name: string;
|
|
763
|
+
/** The `@` parameters. */
|
|
764
|
+
params: string[];
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/**
|
|
768
|
+
* A widget defined by a `{widget}` macro. The offsets are UTF-16 offsets into
|
|
769
|
+
* the content of `passage`.
|
|
770
|
+
*/
|
|
771
|
+
export interface WidgetDefinition extends WidgetDef {
|
|
772
|
+
/** Whether its body renders `{@children}`: it takes a closing tag. */
|
|
773
|
+
block: boolean;
|
|
774
|
+
passage: string;
|
|
775
|
+
/** The opening `{widget …}` tag. */
|
|
776
|
+
start: number;
|
|
777
|
+
end: number;
|
|
778
|
+
/** The name as written, without quotes. */
|
|
779
|
+
nameStart: number;
|
|
780
|
+
nameEnd: number;
|
|
781
|
+
/** The `{/widget}` closer; absent while the definition is not closed. */
|
|
782
|
+
closeStart?: number;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* The name and `@` parameters a `{widget}` macro reads from its arguments
|
|
787
|
+
* (`"Wrap" @a @b`), as the runtime reads them. Throws where the macro
|
|
788
|
+
* reports the arguments.
|
|
789
|
+
*/
|
|
790
|
+
export declare function parseWidgetDef(rawArgs: string): WidgetDef;
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* The widgets the story defines, as the runtime registers them: those of
|
|
794
|
+
* `StoryInit` and of the passages tagged `widget`, in source order. Block-ness
|
|
795
|
+
* is decided on tokens, so a `{@children}` in an HTML comment or a `{do}` body
|
|
796
|
+
* does not count; one in an attribute value or label of the `macros` does.
|
|
797
|
+
* Tolerant: a half-typed definition is reported without `closeStart`, and one
|
|
798
|
+
* whose arguments cannot be read is left out.
|
|
799
|
+
*/
|
|
800
|
+
export declare function widgetDefinitions(
|
|
801
|
+
passages: Iterable<MarkupPassage>,
|
|
802
|
+
macros?: Iterable<ToolingMacro>,
|
|
803
|
+
): WidgetDefinition[];
|
|
804
|
+
|
|
683
805
|
// ---------------------------------------------------------------------------
|
|
684
806
|
// Pieces of a passage
|
|
685
807
|
// ---------------------------------------------------------------------------
|