@rohal12/spindle 0.52.2 → 0.52.4
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 +430 -786
- package/dist/pkg/story-variables.js +179 -476
- package/package.json +1 -1
- package/src/components/App.tsx +6 -3
- package/src/components/macros/ExprDisplay.tsx +2 -7
- package/src/components/macros/MenubarButtons.tsx +13 -12
- package/src/components/macros/PassageDisplay.tsx +8 -4
- package/src/components/macros/SaveManager.tsx +8 -4
- package/src/components/macros/VarDisplay.tsx +11 -12
- package/src/components/macros/Widget.tsx +18 -7
- package/src/event-emitter.ts +15 -11
- package/src/expression.ts +44 -42
- package/src/hooks/use-merged-locals.ts +6 -4
- package/src/hooks/use-story-fields.ts +19 -0
- package/src/index.tsx +5 -8
- package/src/interpolation.ts +16 -22
- package/src/js-lexer.ts +20 -9
- package/src/markup/ast.ts +108 -147
- package/src/markup/code-attributes.ts +4 -6
- package/src/markup/tokenizer.ts +218 -574
- package/src/settings.ts +20 -22
- package/src/store.ts +2 -1
- package/src/story-api.ts +29 -77
- package/src/story-variables.ts +5 -9
- package/src/utils/live-locals.ts +2 -4
- package/src/utils/namespace.ts +89 -25
- package/src/utils/object-path.ts +1 -3
- package/src/utils/counts.ts +0 -45
package/src/markup/ast.ts
CHANGED
|
@@ -1,40 +1,39 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
withSelectors,
|
|
3
|
+
type HtmlToken,
|
|
4
|
+
type MacroToken,
|
|
5
|
+
type Selectors,
|
|
6
|
+
type Token,
|
|
7
|
+
type VariableScope,
|
|
8
|
+
} from './tokenizer';
|
|
2
9
|
|
|
3
10
|
export interface TextNode {
|
|
4
11
|
type: 'text';
|
|
5
12
|
value: string;
|
|
6
13
|
}
|
|
7
14
|
|
|
8
|
-
export interface VariableNode {
|
|
15
|
+
export interface VariableNode extends Selectors {
|
|
9
16
|
type: 'variable';
|
|
10
17
|
name: string;
|
|
11
|
-
scope:
|
|
12
|
-
className?: string;
|
|
13
|
-
id?: string;
|
|
18
|
+
scope: VariableScope;
|
|
14
19
|
}
|
|
15
20
|
|
|
16
|
-
export interface ExpressionNode {
|
|
21
|
+
export interface ExpressionNode extends Selectors {
|
|
17
22
|
type: 'expression';
|
|
18
23
|
expression: string;
|
|
19
|
-
className?: string;
|
|
20
|
-
id?: string;
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
export interface Branch {
|
|
26
|
+
export interface Branch extends Selectors {
|
|
24
27
|
rawArgs: string;
|
|
25
|
-
className?: string;
|
|
26
|
-
id?: string;
|
|
27
28
|
children: ASTNode[];
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
export interface MacroNode {
|
|
31
|
+
export interface MacroNode extends Selectors {
|
|
31
32
|
type: 'macro';
|
|
32
33
|
name: string;
|
|
33
34
|
rawArgs: string;
|
|
34
35
|
children: ASTNode[];
|
|
35
36
|
branches?: Branch[];
|
|
36
|
-
className?: string;
|
|
37
|
-
id?: string;
|
|
38
37
|
}
|
|
39
38
|
|
|
40
39
|
export interface HtmlNode {
|
|
@@ -99,6 +98,24 @@ function quoteArg(value: string): string {
|
|
|
99
98
|
return `"${value.replace(/[\\"]/g, '\\$&')}"`;
|
|
100
99
|
}
|
|
101
100
|
|
|
101
|
+
/** A new branch for the macro token opening it, with its selectors. */
|
|
102
|
+
function newBranch(token: MacroToken): Branch {
|
|
103
|
+
return withSelectors<Branch>({ rawArgs: token.rawArgs, children: [] }, token);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** A macro or HTML element, or a token opening or closing one. */
|
|
107
|
+
type Tagged = MacroNode | HtmlNode | MacroToken | HtmlToken;
|
|
108
|
+
|
|
109
|
+
/** The name of a macro or HTML element. */
|
|
110
|
+
function tagName(tagged: Tagged): string {
|
|
111
|
+
return tagged.type === 'html' ? tagged.tag : tagged.name;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The tag closing a macro or HTML element, as written in markup. */
|
|
115
|
+
function closerOf(tagged: Tagged): string {
|
|
116
|
+
return tagged.type === 'html' ? `</${tagged.tag}>` : `{/${tagged.name}}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
102
119
|
/**
|
|
103
120
|
* Build an AST from a token array. Block macros are nested into trees
|
|
104
121
|
* using a stack. Throws on unclosed or mismatched macros.
|
|
@@ -119,6 +136,32 @@ export function buildAST(tokens: Token[]): ASTNode[] {
|
|
|
119
136
|
return top.children;
|
|
120
137
|
}
|
|
121
138
|
|
|
139
|
+
/**
|
|
140
|
+
* Pop the node on top of the stack, which the closing `token` (`</b>`,
|
|
141
|
+
* `{/if}`) must close. Names match ignoring case.
|
|
142
|
+
*/
|
|
143
|
+
function close(token: MacroToken | HtmlToken) {
|
|
144
|
+
const found = closerOf(token);
|
|
145
|
+
if (stack.length === 0) {
|
|
146
|
+
throw new Error(
|
|
147
|
+
`Unexpected closing ${found} (at character ${token.start})`,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const top = stack[stack.length - 1]!;
|
|
152
|
+
if (
|
|
153
|
+
top.node.type !== token.type ||
|
|
154
|
+
tagName(top.node).toLowerCase() !== tagName(token).toLowerCase()
|
|
155
|
+
) {
|
|
156
|
+
throw new Error(
|
|
157
|
+
`Expected ${closerOf(top.node)} but found ${found} (at character ${token.start})`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
stack.pop();
|
|
162
|
+
current().push(top.node);
|
|
163
|
+
}
|
|
164
|
+
|
|
122
165
|
for (const token of tokens) {
|
|
123
166
|
switch (token.type) {
|
|
124
167
|
case 'text':
|
|
@@ -127,120 +170,64 @@ export function buildAST(tokens: Token[]): ASTNode[] {
|
|
|
127
170
|
|
|
128
171
|
case 'link': {
|
|
129
172
|
const rawArgs = `${quoteArg(token.display)} ${quoteArg(token.target)}`;
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (token.className) macroNode.className = token.className;
|
|
137
|
-
if (token.id) macroNode.id = token.id;
|
|
138
|
-
current().push(macroNode);
|
|
173
|
+
current().push(
|
|
174
|
+
withSelectors<MacroNode>(
|
|
175
|
+
{ type: 'macro', name: 'link', rawArgs, children: [] },
|
|
176
|
+
token,
|
|
177
|
+
),
|
|
178
|
+
);
|
|
139
179
|
break;
|
|
140
180
|
}
|
|
141
181
|
|
|
142
|
-
case 'variable':
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (token.id) varNode.id = token.id;
|
|
150
|
-
current().push(varNode);
|
|
182
|
+
case 'variable':
|
|
183
|
+
current().push(
|
|
184
|
+
withSelectors<VariableNode>(
|
|
185
|
+
{ type: 'variable', name: token.name, scope: token.scope },
|
|
186
|
+
token,
|
|
187
|
+
),
|
|
188
|
+
);
|
|
151
189
|
break;
|
|
152
|
-
}
|
|
153
190
|
|
|
154
|
-
case 'expression':
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
current().push(exprNode);
|
|
191
|
+
case 'expression':
|
|
192
|
+
current().push(
|
|
193
|
+
withSelectors<ExpressionNode>(
|
|
194
|
+
{ type: 'expression', expression: token.expression },
|
|
195
|
+
token,
|
|
196
|
+
),
|
|
197
|
+
);
|
|
162
198
|
break;
|
|
163
|
-
}
|
|
164
199
|
|
|
165
200
|
case 'html': {
|
|
166
|
-
if (token.isSelfClose) {
|
|
167
|
-
// Self-closing HTML tag (br, hr, img, etc.)
|
|
168
|
-
current().push({
|
|
169
|
-
type: 'html',
|
|
170
|
-
tag: token.tag,
|
|
171
|
-
attributes: token.attributes,
|
|
172
|
-
children: [],
|
|
173
|
-
});
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
if (token.isClose) {
|
|
178
|
-
// Closing HTML tag — pop from stack
|
|
179
|
-
if (stack.length === 0) {
|
|
180
|
-
throw new Error(
|
|
181
|
-
`Unexpected closing </${token.tag}> (at character ${token.start})`,
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const top = stack[stack.length - 1]!;
|
|
186
|
-
if (
|
|
187
|
-
top.node.type !== 'html' ||
|
|
188
|
-
top.node.tag.toLowerCase() !== token.tag.toLowerCase()
|
|
189
|
-
) {
|
|
190
|
-
const expected =
|
|
191
|
-
top.node.type === 'html'
|
|
192
|
-
? `</${top.node.tag}>`
|
|
193
|
-
: `{/${top.node.name}}`;
|
|
194
|
-
throw new Error(
|
|
195
|
-
`Expected ${expected} but found </${token.tag}> (at character ${token.start})`,
|
|
196
|
-
);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
stack.pop();
|
|
200
|
-
current().push(top.node);
|
|
201
|
-
break;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// Opening HTML tag — push onto stack
|
|
205
201
|
const htmlNode: HtmlNode = {
|
|
206
202
|
type: 'html',
|
|
207
203
|
tag: token.tag,
|
|
208
204
|
attributes: token.attributes,
|
|
209
205
|
children: [],
|
|
210
206
|
};
|
|
211
|
-
|
|
207
|
+
if (token.isSelfClose) {
|
|
208
|
+
// Self-closing HTML tag (br, hr, img, etc.)
|
|
209
|
+
current().push(htmlNode);
|
|
210
|
+
} else if (token.isClose) {
|
|
211
|
+
// Closing HTML tag — pop from stack
|
|
212
|
+
close(token);
|
|
213
|
+
} else {
|
|
214
|
+
// Opening HTML tag — push onto stack
|
|
215
|
+
stack.push({ node: htmlNode, start: token.start });
|
|
216
|
+
}
|
|
212
217
|
break;
|
|
213
218
|
}
|
|
214
219
|
|
|
215
220
|
case 'macro': {
|
|
216
|
-
// Normalize macro name to lowercase — registration is lowercase,
|
|
217
|
-
// but the tokenizer preserves original casing from passage markup.
|
|
218
|
-
const name = token.name.toLowerCase();
|
|
219
|
-
|
|
220
221
|
if (token.isClose) {
|
|
221
222
|
// Closing tag — pop from stack
|
|
222
|
-
|
|
223
|
-
throw new Error(
|
|
224
|
-
`Unexpected closing {/${token.name}} (at character ${token.start})`,
|
|
225
|
-
);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const top = stack[stack.length - 1]!;
|
|
229
|
-
if (top.node.type !== 'macro' || top.node.name !== name) {
|
|
230
|
-
const expected =
|
|
231
|
-
top.node.type === 'macro'
|
|
232
|
-
? `{/${top.node.name}}`
|
|
233
|
-
: `</${top.node.tag}>`;
|
|
234
|
-
throw new Error(
|
|
235
|
-
`Expected ${expected} but found {/${token.name}} (at character ${token.start})`,
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
stack.pop();
|
|
240
|
-
current().push(top.node);
|
|
223
|
+
close(token);
|
|
241
224
|
break;
|
|
242
225
|
}
|
|
243
226
|
|
|
227
|
+
// Normalize macro name to lowercase — registration is lowercase,
|
|
228
|
+
// but the tokenizer preserves original casing from passage markup.
|
|
229
|
+
const name = token.name.toLowerCase();
|
|
230
|
+
|
|
244
231
|
// Handle branch macros (elseif/else, case/default, next)
|
|
245
232
|
const expectedParent = Object.prototype.hasOwnProperty.call(
|
|
246
233
|
BRANCH_PARENT,
|
|
@@ -261,55 +248,29 @@ export function buildAST(tokens: Token[]): ASTNode[] {
|
|
|
261
248
|
);
|
|
262
249
|
}
|
|
263
250
|
|
|
264
|
-
|
|
265
|
-
rawArgs: token.rawArgs,
|
|
266
|
-
children: [],
|
|
267
|
-
};
|
|
268
|
-
if (token.className) branch.className = token.className;
|
|
269
|
-
if (token.id) branch.id = token.id;
|
|
270
|
-
topNode.branches!.push(branch);
|
|
251
|
+
topNode.branches!.push(newBranch(token));
|
|
271
252
|
break;
|
|
272
253
|
}
|
|
273
254
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
children: [],
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
// Branching blocks: className/id goes on the first branch, not the node
|
|
284
|
-
if (BRANCHING_BLOCK_MACROS.has(name)) {
|
|
285
|
-
const firstBranch: Branch = {
|
|
286
|
-
rawArgs: token.rawArgs,
|
|
287
|
-
children: [],
|
|
288
|
-
};
|
|
289
|
-
if (token.className) firstBranch.className = token.className;
|
|
290
|
-
if (token.id) firstBranch.id = token.id;
|
|
291
|
-
node.branches = [firstBranch];
|
|
292
|
-
} else {
|
|
293
|
-
if (token.className) node.className = token.className;
|
|
294
|
-
if (token.id) node.id = token.id;
|
|
295
|
-
}
|
|
255
|
+
const node: MacroNode = {
|
|
256
|
+
type: 'macro',
|
|
257
|
+
name,
|
|
258
|
+
rawArgs: token.rawArgs,
|
|
259
|
+
children: [],
|
|
260
|
+
};
|
|
296
261
|
|
|
297
|
-
|
|
262
|
+
if (!BLOCK_MACROS.has(name)) {
|
|
263
|
+
// Self-closing macro (set, print, etc.)
|
|
264
|
+
current().push(withSelectors(node, token));
|
|
298
265
|
break;
|
|
299
266
|
}
|
|
300
267
|
|
|
301
|
-
//
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
children: [],
|
|
308
|
-
};
|
|
309
|
-
if (token.className) macroNode.className = token.className;
|
|
310
|
-
if (token.id) macroNode.id = token.id;
|
|
311
|
-
current().push(macroNode);
|
|
312
|
-
}
|
|
268
|
+
// Block macro — push onto stack. Branching blocks: className/id
|
|
269
|
+
// goes on the first branch, not the node
|
|
270
|
+
if (BRANCHING_BLOCK_MACROS.has(name))
|
|
271
|
+
node.branches = [newBranch(token)];
|
|
272
|
+
else withSelectors(node, token);
|
|
273
|
+
stack.push({ node, start: token.start });
|
|
313
274
|
break;
|
|
314
275
|
}
|
|
315
276
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createScanMemo, scanBalancedBrace } from './tokenizer';
|
|
1
|
+
import { createScanMemo, isSigil, scanBalancedBrace } from './tokenizer';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Attributes whose value is code with braces of its own, not text: event
|
|
@@ -41,11 +41,9 @@ export function splitSigilTemplate(template: string): SigilPart[] {
|
|
|
41
41
|
}
|
|
42
42
|
text += template.slice(i, brace);
|
|
43
43
|
i = brace + 1;
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
? scanBalancedBrace(template, i, memo)
|
|
48
|
-
: -1;
|
|
44
|
+
const end = isSigil(template[i])
|
|
45
|
+
? scanBalancedBrace(template, i, memo)
|
|
46
|
+
: -1;
|
|
49
47
|
if (end === -1) {
|
|
50
48
|
text += '{';
|
|
51
49
|
continue;
|