@rohal12/spindle 0.61.6 → 0.62.0
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/README.md +3 -1
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +200 -90
- package/dist/pkg/play.js +3 -49
- package/dist/pkg/story-variables.js +142 -77
- package/dist/pkg/tooling.js +3 -0
- package/package.json +5 -5
- package/src/components/macros/PassageDisplay.tsx +172 -35
- package/src/components/macros/arg-utils.ts +7 -2
- package/src/components/macros/macro-args.ts +113 -19
- package/src/format/format-passage.ts +14 -6
- package/src/format/placeholders.ts +20 -18
- package/src/index.tsx +8 -3
- package/src/js-lexer.ts +47 -12
- package/src/play/resolve-story.ts +5 -63
- package/src/tooling.ts +26 -9
- package/src/types-drift-check.ts +17 -23
- package/src/utils/object-path.ts +12 -1
- package/types/tooling.d.ts +56 -1
package/dist/pkg/headless.js
CHANGED
|
@@ -9656,34 +9656,6 @@ function parse$1(input, options) {
|
|
|
9656
9656
|
}
|
|
9657
9657
|
//#endregion
|
|
9658
9658
|
//#region src/js-lexer.ts
|
|
9659
|
-
/**
|
|
9660
|
-
* The JavaScript in story markup, read with acorn.
|
|
9661
|
-
*
|
|
9662
|
-
* acorn reads the code: where string, template and regex literals and
|
|
9663
|
-
* comments begin and end, whether `/` opens a regex, and (`parseCode`) the
|
|
9664
|
-
* syntax tree. A plugin (`SigilParser`) teaches its tokenizer the two sigils
|
|
9665
|
-
* that are no JavaScript: `@name` (locals) anywhere, and `%name`
|
|
9666
|
-
* (transients) where an operand is expected, the same place acorn reads `/`
|
|
9667
|
-
* as a regex; `%` elsewhere is the modulo operator. `$name` and `_name` are
|
|
9668
|
-
* identifiers already; the syntax tree tells references from property names
|
|
9669
|
-
* and declarations.
|
|
9670
|
-
*
|
|
9671
|
-
* - `parseCode`: the references and string text of well-formed code, or a
|
|
9672
|
-
* `CodeSyntaxError` that says what is wrong and where. The expression
|
|
9673
|
-
* engine (`expression.ts`) rewrites the references it finds, and the
|
|
9674
|
-
* story-start check (`markup/validate.ts`) reports the errors.
|
|
9675
|
-
* - `findCodeEnd`: where the code in a `{…}` ends, for the markup grammar
|
|
9676
|
-
* (`markup/code-end.ts`). -1 when acorn can't read it: the grammar then
|
|
9677
|
-
* scans it leniently, as prose-like macro arguments (`{link Don't go}`)
|
|
9678
|
-
* need.
|
|
9679
|
-
* - `lexJs`: a lenient token walk for the macro argument splitters
|
|
9680
|
-
* (`components/macros/arg-utils.ts`), which also read text that is no
|
|
9681
|
-
* JavaScript.
|
|
9682
|
-
*
|
|
9683
|
-
* Restrictions (docs/variables.md "Code in passages"): sigil variables can't
|
|
9684
|
-
* be declared (`let _x`, `(_a) => …`) or be property names (`a.@x`), and
|
|
9685
|
-
* code must be valid modern JavaScript (a non-strict script).
|
|
9686
|
-
*/
|
|
9687
9659
|
/** Code runs through `new Function`: a non-strict script body. */
|
|
9688
9660
|
var OPTIONS = {
|
|
9689
9661
|
ecmaVersion: "latest",
|
|
@@ -10077,13 +10049,13 @@ function propertyName(node, src) {
|
|
|
10077
10049
|
* identifiers here are declared (a `let`, a parameter), where a sigil
|
|
10078
10050
|
* variable can't be. `shorthand`: the node is a shorthand property's value.
|
|
10079
10051
|
*/
|
|
10080
|
-
function walk(node, src, out, binding, shorthand) {
|
|
10052
|
+
function walk(node, src, out, binding, shorthand, access = "read") {
|
|
10081
10053
|
if (Array.isArray(node)) {
|
|
10082
|
-
for (const n of node) walk(n, src, out, binding, false);
|
|
10054
|
+
for (const n of node) walk(n, src, out, binding, false, access);
|
|
10083
10055
|
return;
|
|
10084
10056
|
}
|
|
10085
10057
|
if (!isNode(node)) return;
|
|
10086
|
-
const sub = (child, bind = false, short = false) => walk(child, src, out, bind, short);
|
|
10058
|
+
const sub = (child, bind = false, short = false, role = "read") => walk(child, src, out, bind, short, role);
|
|
10087
10059
|
switch (node.type) {
|
|
10088
10060
|
case "Identifier": {
|
|
10089
10061
|
const sigil = sigilOf(node, src);
|
|
@@ -10094,7 +10066,9 @@ function walk(node, src, out, binding, shorthand) {
|
|
|
10094
10066
|
name: sigil[1],
|
|
10095
10067
|
start: node.start,
|
|
10096
10068
|
end: node.end,
|
|
10097
|
-
shorthand
|
|
10069
|
+
shorthand,
|
|
10070
|
+
read: access !== "write",
|
|
10071
|
+
write: access !== "read"
|
|
10098
10072
|
});
|
|
10099
10073
|
return;
|
|
10100
10074
|
}
|
|
@@ -10115,10 +10089,10 @@ function walk(node, src, out, binding, shorthand) {
|
|
|
10115
10089
|
if (node.shorthand) {
|
|
10116
10090
|
const value = node.value;
|
|
10117
10091
|
if (value.type === "AssignmentPattern") {
|
|
10118
|
-
sub(value.left, binding, true);
|
|
10092
|
+
sub(value.left, binding, true, access);
|
|
10119
10093
|
sub(value.right);
|
|
10120
|
-
} else sub(value, binding, true);
|
|
10121
|
-
} else sub(node.value, binding);
|
|
10094
|
+
} else sub(value, binding, true, access);
|
|
10095
|
+
} else sub(node.value, binding, false, access);
|
|
10122
10096
|
return;
|
|
10123
10097
|
case "MethodDefinition":
|
|
10124
10098
|
case "PropertyDefinition":
|
|
@@ -10154,8 +10128,24 @@ function walk(node, src, out, binding, shorthand) {
|
|
|
10154
10128
|
sub(node.param, true);
|
|
10155
10129
|
sub(node.body);
|
|
10156
10130
|
return;
|
|
10131
|
+
case "AssignmentExpression":
|
|
10132
|
+
sub(node.left, false, false, node.operator === "=" ? "write" : "readwrite");
|
|
10133
|
+
sub(node.right);
|
|
10134
|
+
return;
|
|
10135
|
+
case "UnaryExpression":
|
|
10136
|
+
sub(node.argument, false, false, node.operator === "delete" ? "write" : "read");
|
|
10137
|
+
return;
|
|
10138
|
+
case "UpdateExpression":
|
|
10139
|
+
sub(node.argument, false, false, "readwrite");
|
|
10140
|
+
return;
|
|
10141
|
+
case "ForInStatement":
|
|
10142
|
+
case "ForOfStatement":
|
|
10143
|
+
sub(node.left, false, false, "write");
|
|
10144
|
+
sub(node.right);
|
|
10145
|
+
sub(node.body);
|
|
10146
|
+
return;
|
|
10157
10147
|
case "AssignmentPattern":
|
|
10158
|
-
sub(node.left, binding);
|
|
10148
|
+
sub(node.left, binding, false, access);
|
|
10159
10149
|
sub(node.right);
|
|
10160
10150
|
return;
|
|
10161
10151
|
case "ObjectPattern":
|
|
@@ -10165,7 +10155,7 @@ function walk(node, src, out, binding, shorthand) {
|
|
|
10165
10155
|
"properties",
|
|
10166
10156
|
"elements",
|
|
10167
10157
|
"argument"
|
|
10168
|
-
]) sub(node[key], binding);
|
|
10158
|
+
]) sub(node[key], binding, false, access);
|
|
10169
10159
|
return;
|
|
10170
10160
|
default: for (const key in node) {
|
|
10171
10161
|
if (key === "type" || key === "start" || key === "end") continue;
|
|
@@ -11161,14 +11151,15 @@ function unescapeQuoted(body) {
|
|
|
11161
11151
|
* Read the `"…"` or `'…'` string whose opening quote is at `start`.
|
|
11162
11152
|
* Returns its unescaped value and the index just past the closing quote, or
|
|
11163
11153
|
* `null` if there is no such quote at `start` or the string is unterminated.
|
|
11154
|
+
* With `tolerant`, an unterminated string instead extends to its scanned end.
|
|
11164
11155
|
*/
|
|
11165
|
-
function readQuoted(src, start) {
|
|
11156
|
+
function readQuoted(src, start, tolerant = false) {
|
|
11166
11157
|
const quote = src[start];
|
|
11167
11158
|
if (quote !== "\"" && quote !== "'") return null;
|
|
11168
11159
|
const { end, closed } = scanStringLiteral(src, start);
|
|
11169
|
-
if (!closed) return null;
|
|
11160
|
+
if (!closed && !tolerant) return null;
|
|
11170
11161
|
return {
|
|
11171
|
-
value: unescapeQuoted(src.slice(start + 1, end - 1)),
|
|
11162
|
+
value: unescapeQuoted(src.slice(start + 1, closed ? end - 1 : end)),
|
|
11172
11163
|
end
|
|
11173
11164
|
};
|
|
11174
11165
|
}
|
|
@@ -11355,11 +11346,16 @@ var MacroArgumentError = class extends Error {
|
|
|
11355
11346
|
};
|
|
11356
11347
|
/** Read `src`, at `place`, as `param` says. */
|
|
11357
11348
|
function readValue(param, src, place) {
|
|
11358
|
-
|
|
11349
|
+
const span = [place.at, place.at + src.length];
|
|
11350
|
+
place.spans?.set(param, span);
|
|
11351
|
+
place.toolingSpans?.set(param, span);
|
|
11359
11352
|
switch (param.type) {
|
|
11360
11353
|
case "string": {
|
|
11361
11354
|
const value = readWholeQuoted(src);
|
|
11362
|
-
if (value === null)
|
|
11355
|
+
if (value === null) {
|
|
11356
|
+
if (place.toolingSpans && (src[0] === "\"" || src[0] === "'")) return readText(src);
|
|
11357
|
+
throw new MacroArgumentError(param, src);
|
|
11358
|
+
}
|
|
11363
11359
|
return value;
|
|
11364
11360
|
}
|
|
11365
11361
|
case "text": return readText(src);
|
|
@@ -11390,20 +11386,21 @@ function matchAt(re, src, pos) {
|
|
|
11390
11386
|
* skipped. A keyword of a `string` parameter must be followed by a quoted
|
|
11391
11387
|
* string.
|
|
11392
11388
|
*/
|
|
11393
|
-
function readOptions(params, src, { at, spans }) {
|
|
11389
|
+
function readOptions(params, src, { at, spans, toolingSpans }) {
|
|
11394
11390
|
const options = {};
|
|
11395
11391
|
let i = 0;
|
|
11396
11392
|
while (i < src.length) {
|
|
11397
11393
|
const key = matchAt(WORD_RE, src, i);
|
|
11398
11394
|
if (!key) {
|
|
11399
|
-
i = readQuoted(src, i)?.end ?? i + 1;
|
|
11395
|
+
i = readQuoted(src, i, !!toolingSpans)?.end ?? i + 1;
|
|
11400
11396
|
continue;
|
|
11401
11397
|
}
|
|
11398
|
+
const keyAt = i;
|
|
11402
11399
|
i += key.length;
|
|
11403
11400
|
let val;
|
|
11404
11401
|
let j = i;
|
|
11405
11402
|
while (j < src.length && isWhitespace(src[j])) j++;
|
|
11406
|
-
const quoted = readQuoted(src, j);
|
|
11403
|
+
const quoted = readQuoted(src, j, !!toolingSpans);
|
|
11407
11404
|
const digits = quoted || j === i ? null : matchAt(NUMBER_RE$1, src, j);
|
|
11408
11405
|
if (quoted) {
|
|
11409
11406
|
val = quoted.value;
|
|
@@ -11414,7 +11411,8 @@ function readOptions(params, src, { at, spans }) {
|
|
|
11414
11411
|
}
|
|
11415
11412
|
const param = params.find((p) => p.name === key);
|
|
11416
11413
|
if (param && param.type !== "flag" && val !== void 0) spans?.set(param, [at + j, at + i]);
|
|
11417
|
-
if (param
|
|
11414
|
+
if (param) toolingSpans?.set(param, [at + keyAt, at + i]);
|
|
11415
|
+
if (param?.type === "string" && !quoted && !toolingSpans) throw new MacroArgumentError(param, matchAt(WORD_OR_PUNCT_RE, src, j) ?? "");
|
|
11418
11416
|
if (param?.type === "flag") options[key] = true;
|
|
11419
11417
|
else if (param?.type === "number") options[key] = Number(val ?? 0);
|
|
11420
11418
|
else if (param) options[key] = val;
|
|
@@ -11450,7 +11448,7 @@ function takeFlag(src, word, alone) {
|
|
|
11450
11448
|
* term with terms on both sides; `=` is an assignment at depth 0, not part
|
|
11451
11449
|
* of `==` or `!=`.
|
|
11452
11450
|
*/
|
|
11453
|
-
function findSeparator(src, word) {
|
|
11451
|
+
function findSeparator(src, word, tolerant = false) {
|
|
11454
11452
|
if (word === "=") {
|
|
11455
11453
|
const indices = topLevelIndices(src, (ch) => ch === "=");
|
|
11456
11454
|
for (let k = 0; k < indices.length; k++) {
|
|
@@ -11464,7 +11462,7 @@ function findSeparator(src, word) {
|
|
|
11464
11462
|
return null;
|
|
11465
11463
|
}
|
|
11466
11464
|
const spans = terms(src);
|
|
11467
|
-
for (let k = 1; k < spans.length - 1; k++) if (slice(src, spans[k]) === word) return [spans[k - 1][1], spans[k + 1][0]];
|
|
11465
|
+
for (let k = 1; k < spans.length - (tolerant ? 0 : 1); k++) if (slice(src, spans[k]) === word) return [spans[k - 1][1], spans[k + 1]?.[0] ?? src.length];
|
|
11468
11466
|
return null;
|
|
11469
11467
|
}
|
|
11470
11468
|
var takesRest = (param) => param.type === "expression" || param.type === "statements" || param.type === "passage" || param.type === "text";
|
|
@@ -11477,23 +11475,36 @@ function trimmed(src, at) {
|
|
|
11477
11475
|
* Read the parameters of one group from `src` (`null`: no text), which is
|
|
11478
11476
|
* at `at` in the raw arguments, into `args`.
|
|
11479
11477
|
*/
|
|
11480
|
-
function readGroup(params, src, args, { at: srcAt, spans }) {
|
|
11478
|
+
function readGroup(params, src, args, { at: srcAt, spans, toolingSpans }) {
|
|
11481
11479
|
let [text, at] = trimmed(src ?? "", srcAt);
|
|
11482
11480
|
const alone = !params.some((p) => p.type !== "flag" && p.required);
|
|
11483
11481
|
for (const flag of params.filter((p) => p.type === "flag")) {
|
|
11484
11482
|
const rest = src === null ? null : takeFlag(text, flag.name, alone);
|
|
11485
11483
|
args[flag.name] = rest !== null;
|
|
11486
|
-
if (rest)
|
|
11484
|
+
if (rest) {
|
|
11485
|
+
const flagSpan = rest[0] > 0 ? [0, text.indexOf(flag.name) + flag.name.length] : [text.lastIndexOf(flag.name), text.length];
|
|
11486
|
+
toolingSpans?.set(flag, [at + flagSpan[0], at + flagSpan[1]]);
|
|
11487
|
+
[text, at] = trimmed(slice(text, rest), at + rest[0]);
|
|
11488
|
+
}
|
|
11487
11489
|
}
|
|
11488
11490
|
const positional = params.filter((p) => p.type !== "flag");
|
|
11489
11491
|
if (positional.length === 0) return;
|
|
11490
11492
|
const lastText = positional.findIndex((p, i) => takesRest(p) && !positional.slice(i + 1).some(takesRest));
|
|
11491
11493
|
const restIndex = lastText < 0 ? positional.length - 1 : lastText;
|
|
11492
11494
|
/** Read `param` from the text from `start` to `end`. */
|
|
11493
|
-
const read = (param, [start, end]
|
|
11494
|
-
|
|
11495
|
-
|
|
11496
|
-
|
|
11495
|
+
const read = (param, [start, end], tolerant = true) => {
|
|
11496
|
+
if (start === end) return unset(param);
|
|
11497
|
+
try {
|
|
11498
|
+
return readValue(param, text.slice(start, end), {
|
|
11499
|
+
at: at + start,
|
|
11500
|
+
spans,
|
|
11501
|
+
toolingSpans
|
|
11502
|
+
});
|
|
11503
|
+
} catch (error) {
|
|
11504
|
+
if (toolingSpans && tolerant && error instanceof MacroArgumentError) return;
|
|
11505
|
+
throw error;
|
|
11506
|
+
}
|
|
11507
|
+
};
|
|
11497
11508
|
if (positional.length === 1) {
|
|
11498
11509
|
args[positional[0].name] = read(positional[0], [0, text.length]);
|
|
11499
11510
|
return;
|
|
@@ -11504,7 +11515,7 @@ function readGroup(params, src, args, { at: srcAt, spans }) {
|
|
|
11504
11515
|
for (const param of positional.slice(0, restIndex)) args[param.name] = from < to ? read(param, parts[from++]) : unset(param);
|
|
11505
11516
|
const readTrailing = (param, span) => {
|
|
11506
11517
|
try {
|
|
11507
|
-
return read(param, span);
|
|
11518
|
+
return read(param, span, false);
|
|
11508
11519
|
} catch (error) {
|
|
11509
11520
|
if (error instanceof MacroArgumentError) return void 0;
|
|
11510
11521
|
throw error;
|
|
@@ -11514,7 +11525,10 @@ function readGroup(params, src, args, { at: srcAt, spans }) {
|
|
|
11514
11525
|
const value = from < to && !endsWithOperator(text.slice(0, parts[to - 1][0])) ? readTrailing(param, parts[to - 1]) : void 0;
|
|
11515
11526
|
args[param.name] = value ?? unset(param);
|
|
11516
11527
|
if (value !== void 0) to--;
|
|
11517
|
-
else
|
|
11528
|
+
else {
|
|
11529
|
+
spans?.delete(param);
|
|
11530
|
+
toolingSpans?.delete(param);
|
|
11531
|
+
}
|
|
11518
11532
|
}
|
|
11519
11533
|
const rest = positional[restIndex];
|
|
11520
11534
|
args[rest.name] = from < to ? read(rest, [parts[from][0], parts[to - 1][1]]) : unset(rest);
|
|
@@ -11529,18 +11543,23 @@ var DELAY_PARAMETER = {
|
|
|
11529
11543
|
* Read `rawArgs` into the declared `parameters` (see above). With `spans`,
|
|
11530
11544
|
* note there where each argument set was read.
|
|
11531
11545
|
*/
|
|
11532
|
-
function parseMacroArgs(rawArgs, parameters, spans) {
|
|
11546
|
+
function parseMacroArgs(rawArgs, parameters, spans, toolingSpans) {
|
|
11533
11547
|
const args = {};
|
|
11534
11548
|
let [rest, restAt] = trimmed(rawArgs, 0);
|
|
11535
11549
|
let groupStart = 0;
|
|
11536
11550
|
parameters.forEach((separator, i) => {
|
|
11537
11551
|
if (separator.type !== "separator") return;
|
|
11538
|
-
const at = rest === null ? null : findSeparator(rest, separator.name);
|
|
11552
|
+
const at = rest === null ? null : findSeparator(rest, separator.name, !!toolingSpans);
|
|
11539
11553
|
args[separator.name] = at !== null;
|
|
11554
|
+
if (at && toolingSpans) {
|
|
11555
|
+
const start = restAt + at[0] + rest.slice(at[0], at[1]).indexOf(separator.name);
|
|
11556
|
+
toolingSpans.set(separator, [start, start + separator.name.length]);
|
|
11557
|
+
}
|
|
11540
11558
|
const before = at ? rest.slice(0, at[0]) : rest;
|
|
11541
11559
|
readGroup(parameters.slice(groupStart, i), before, args, {
|
|
11542
11560
|
at: restAt,
|
|
11543
|
-
spans
|
|
11561
|
+
spans,
|
|
11562
|
+
toolingSpans
|
|
11544
11563
|
});
|
|
11545
11564
|
rest = at ? rest.slice(at[1]) : null;
|
|
11546
11565
|
if (at) restAt += at[1];
|
|
@@ -11548,7 +11567,8 @@ function parseMacroArgs(rawArgs, parameters, spans) {
|
|
|
11548
11567
|
});
|
|
11549
11568
|
readGroup(parameters.slice(groupStart), rest, args, {
|
|
11550
11569
|
at: restAt,
|
|
11551
|
-
spans
|
|
11570
|
+
spans,
|
|
11571
|
+
toolingSpans
|
|
11552
11572
|
});
|
|
11553
11573
|
return args;
|
|
11554
11574
|
}
|
|
@@ -15698,7 +15718,9 @@ function shallowCopy(value) {
|
|
|
15698
15718
|
*/
|
|
15699
15719
|
function setByPath(root, segments, value, options = {}) {
|
|
15700
15720
|
const parent = walkToParent(root, segments, options.createMissing ?? false);
|
|
15701
|
-
|
|
15721
|
+
const key = segments[segments.length - 1];
|
|
15722
|
+
if (value === 0 && isDraft(parent) && parent[key] === 0 && !Object.is(parent[key], value) && !(Array.isArray(parent) && key === "length")) parent[key] = void 0;
|
|
15723
|
+
parent[key] = value;
|
|
15702
15724
|
}
|
|
15703
15725
|
/**
|
|
15704
15726
|
* Delete the property at dot-path `segments` below `root`, copying
|
|
@@ -23088,6 +23110,7 @@ function copyLiveState(source, clone) {
|
|
|
23088
23110
|
const input = to;
|
|
23089
23111
|
input.checked = from.checked;
|
|
23090
23112
|
input.indeterminate = from.indeterminate;
|
|
23113
|
+
if (from.type === "file") input.files = from.files;
|
|
23091
23114
|
if (!VALUELESS_INPUTS.has(from.type)) input.value = from.value;
|
|
23092
23115
|
} else to.value = from.value;
|
|
23093
23116
|
});
|
|
@@ -23115,8 +23138,8 @@ function copyLiveState(source, clone) {
|
|
|
23115
23138
|
* is in the page: a detached element has nothing to scroll.
|
|
23116
23139
|
*/
|
|
23117
23140
|
function scrollOffsets(source, clone) {
|
|
23118
|
-
const from = [source, ...source.querySelectorAll("*")];
|
|
23119
|
-
const to = [clone, ...clone.querySelectorAll("*")];
|
|
23141
|
+
const from = [...source instanceof Element ? [source] : [], ...source.querySelectorAll("*")];
|
|
23142
|
+
const to = [...clone instanceof Element ? [clone] : [], ...clone.querySelectorAll("*")];
|
|
23120
23143
|
const offsets = [];
|
|
23121
23144
|
from.forEach((el, i) => {
|
|
23122
23145
|
if (to[i] && (el.scrollTop || el.scrollLeft)) offsets.push([
|
|
@@ -23124,10 +23147,12 @@ function scrollOffsets(source, clone) {
|
|
|
23124
23147
|
el.scrollTop,
|
|
23125
23148
|
el.scrollLeft
|
|
23126
23149
|
]);
|
|
23150
|
+
if (el.shadowRoot && to[i]?.shadowRoot) offsets.push(...scrollOffsets(el.shadowRoot, to[i].shadowRoot));
|
|
23127
23151
|
});
|
|
23128
23152
|
return offsets;
|
|
23129
23153
|
}
|
|
23130
23154
|
var pendingScrolls = /* @__PURE__ */ new WeakMap();
|
|
23155
|
+
var snapshotStyleCleanup = /* @__PURE__ */ new WeakMap();
|
|
23131
23156
|
/** Add `snapshot` to `container`, scrolled as what it shows was. */
|
|
23132
23157
|
function appendSnapshot(container, snapshot) {
|
|
23133
23158
|
container.appendChild(snapshot);
|
|
@@ -23137,6 +23162,97 @@ function appendSnapshot(container, snapshot) {
|
|
|
23137
23162
|
}
|
|
23138
23163
|
pendingScrolls.delete(snapshot);
|
|
23139
23164
|
}
|
|
23165
|
+
function copyComputedStyle(style, target) {
|
|
23166
|
+
for (let i = 0; i < style.length; i++) {
|
|
23167
|
+
const name = style[i];
|
|
23168
|
+
target.setProperty(name, style.getPropertyValue(name));
|
|
23169
|
+
}
|
|
23170
|
+
}
|
|
23171
|
+
function installSnapshotStyles(root, rules) {
|
|
23172
|
+
if (!rules.length) return;
|
|
23173
|
+
const sheet = new CSSStyleSheet();
|
|
23174
|
+
sheet.replaceSync(rules.join("\n"));
|
|
23175
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
23176
|
+
return () => {
|
|
23177
|
+
root.adoptedStyleSheets = root.adoptedStyleSheets.filter((current) => current !== sheet);
|
|
23178
|
+
};
|
|
23179
|
+
}
|
|
23180
|
+
var snapshotPseudoId = 0;
|
|
23181
|
+
/**
|
|
23182
|
+
* Build a visual tree without cloning upgraded custom elements: cloneNode
|
|
23183
|
+
* runs their constructors, and insertion runs their connection callbacks.
|
|
23184
|
+
* Autonomous elements become styled spans; customized built-ins are created
|
|
23185
|
+
* as ordinary native elements, without an `is` option (even if the original
|
|
23186
|
+
* has since removed its `is` attribute).
|
|
23187
|
+
*/
|
|
23188
|
+
function cloneSnapshotTree(source, freezeStyles = false, pseudoRules = []) {
|
|
23189
|
+
const custom = source.namespaceURI === "http://www.w3.org/1999/xhtml" && source.localName.includes("-");
|
|
23190
|
+
freezeStyles ||= custom || source.hasAttribute("is");
|
|
23191
|
+
const clone = document.createElementNS(source.namespaceURI, custom ? "span" : source.localName);
|
|
23192
|
+
for (const attr of [...source.attributes]) {
|
|
23193
|
+
if (attr.name === "id" || attr.name === "data-action" || attr.name === "is" || attr.name.toLowerCase().startsWith("on")) continue;
|
|
23194
|
+
if (attr.namespaceURI) clone.setAttributeNS(attr.namespaceURI, attr.name, attr.value);
|
|
23195
|
+
else clone.setAttribute(attr.name, attr.value);
|
|
23196
|
+
}
|
|
23197
|
+
if (freezeStyles) {
|
|
23198
|
+
copyComputedStyle(getComputedStyle(source), clone.style);
|
|
23199
|
+
let id;
|
|
23200
|
+
for (const pseudo of [
|
|
23201
|
+
"::before",
|
|
23202
|
+
"::after",
|
|
23203
|
+
"::marker"
|
|
23204
|
+
]) {
|
|
23205
|
+
const style = getComputedStyle(source, pseudo);
|
|
23206
|
+
if (!style.content || ["none", "normal"].includes(style.content)) continue;
|
|
23207
|
+
id ??= String(++snapshotPseudoId);
|
|
23208
|
+
clone.setAttribute("data-spindle-snapshot-pseudo", id);
|
|
23209
|
+
const target = document.createElement("span").style;
|
|
23210
|
+
copyComputedStyle(style, target);
|
|
23211
|
+
pseudoRules.push(`[data-spindle-snapshot-pseudo="${id}"]${pseudo}{${target.cssText}}`);
|
|
23212
|
+
}
|
|
23213
|
+
}
|
|
23214
|
+
if (source.localName === "script") clone.setAttribute("type", "application/x-spindle-snapshot");
|
|
23215
|
+
const from = source instanceof HTMLTemplateElement ? source.content : source;
|
|
23216
|
+
const to = clone instanceof HTMLTemplateElement ? clone.content : clone;
|
|
23217
|
+
const copyChildren = (from, to, rules = pseudoRules) => {
|
|
23218
|
+
for (const node of from.childNodes) to.appendChild(node instanceof Element ? cloneSnapshotTree(node, freezeStyles, rules) : node.cloneNode(false));
|
|
23219
|
+
};
|
|
23220
|
+
copyChildren(from, to);
|
|
23221
|
+
if (source.shadowRoot) {
|
|
23222
|
+
const shadow = clone.attachShadow({ mode: "open" });
|
|
23223
|
+
const rules = [];
|
|
23224
|
+
copyChildren(source.shadowRoot, shadow, rules);
|
|
23225
|
+
copyLiveState(source.shadowRoot, shadow);
|
|
23226
|
+
installSnapshotStyles(shadow, rules);
|
|
23227
|
+
}
|
|
23228
|
+
return clone;
|
|
23229
|
+
}
|
|
23230
|
+
/** Replace browsing contexts throughout the snapshot, including shadow trees. */
|
|
23231
|
+
function replaceEmbeddedContexts(source, clone) {
|
|
23232
|
+
const originals = source.querySelectorAll("*");
|
|
23233
|
+
clone.querySelectorAll("*").forEach((embedded, i) => {
|
|
23234
|
+
const original = originals[i];
|
|
23235
|
+
if (!original) return;
|
|
23236
|
+
if (original.shadowRoot && embedded.shadowRoot) replaceEmbeddedContexts(original.shadowRoot, embedded.shadowRoot);
|
|
23237
|
+
if (!embedded.matches("iframe, object, embed")) return;
|
|
23238
|
+
const placeholder = document.createElement("div");
|
|
23239
|
+
for (const name of [
|
|
23240
|
+
"class",
|
|
23241
|
+
"style",
|
|
23242
|
+
"width",
|
|
23243
|
+
"height"
|
|
23244
|
+
]) {
|
|
23245
|
+
const value = embedded.getAttribute(name);
|
|
23246
|
+
if (value !== null) placeholder.setAttribute(name, value);
|
|
23247
|
+
}
|
|
23248
|
+
const rect = original.getBoundingClientRect();
|
|
23249
|
+
if (getComputedStyle(original).display === "inline") placeholder.style.display = "inline-block";
|
|
23250
|
+
placeholder.style.boxSizing = "border-box";
|
|
23251
|
+
placeholder.style.width = `${rect.width}px`;
|
|
23252
|
+
placeholder.style.height = `${rect.height}px`;
|
|
23253
|
+
embedded.replaceWith(placeholder);
|
|
23254
|
+
});
|
|
23255
|
+
}
|
|
23140
23256
|
/**
|
|
23141
23257
|
* Create a snapshot clone of a .passage element for outgoing transitions.
|
|
23142
23258
|
* - Strips IDs, action associations and event handlers from the clone tree
|
|
@@ -23148,30 +23264,13 @@ function appendSnapshot(container, snapshot) {
|
|
|
23148
23264
|
* - Adds `.passage-snapshot` class and `data-transition` attribute
|
|
23149
23265
|
*/
|
|
23150
23266
|
function createSnapshot(passageEl, transitionType) {
|
|
23151
|
-
const
|
|
23152
|
-
|
|
23267
|
+
const pseudoRules = [];
|
|
23268
|
+
const clone = cloneSnapshotTree(passageEl, false, pseudoRules);
|
|
23153
23269
|
copyLiveState(passageEl, clone);
|
|
23154
23270
|
pendingScrolls.set(clone, scrollOffsets(passageEl, clone));
|
|
23155
|
-
|
|
23156
|
-
|
|
23157
|
-
|
|
23158
|
-
for (const name of [
|
|
23159
|
-
"class",
|
|
23160
|
-
"style",
|
|
23161
|
-
"width",
|
|
23162
|
-
"height"
|
|
23163
|
-
]) {
|
|
23164
|
-
const value = embedded.getAttribute(name);
|
|
23165
|
-
if (value !== null) placeholder.setAttribute(name, value);
|
|
23166
|
-
}
|
|
23167
|
-
const original = originals[i];
|
|
23168
|
-
if (original) {
|
|
23169
|
-
const rect = original.getBoundingClientRect();
|
|
23170
|
-
placeholder.style.width = `${rect.width}px`;
|
|
23171
|
-
placeholder.style.height = `${rect.height}px`;
|
|
23172
|
-
}
|
|
23173
|
-
embedded.replaceWith(placeholder);
|
|
23174
|
-
});
|
|
23271
|
+
replaceEmbeddedContexts(passageEl, clone);
|
|
23272
|
+
const cleanup = installSnapshotStyles(passageEl.getRootNode(), pseudoRules);
|
|
23273
|
+
if (cleanup) snapshotStyleCleanup.set(clone, cleanup);
|
|
23175
23274
|
clone.style.pointerEvents = "none";
|
|
23176
23275
|
clone.style.userSelect = "none";
|
|
23177
23276
|
clone.setAttribute("inert", "");
|
|
@@ -23206,7 +23305,12 @@ function cleanupSnapshots(containerEl) {
|
|
|
23206
23305
|
if (!containerEl) return;
|
|
23207
23306
|
containerEl.classList.remove("passage-container--crossfading");
|
|
23208
23307
|
const snapshots = containerEl.querySelectorAll(".passage-snapshot");
|
|
23209
|
-
for (let i = 0; i < snapshots.length; i++)
|
|
23308
|
+
for (let i = 0; i < snapshots.length; i++) {
|
|
23309
|
+
const snapshot = snapshots[i];
|
|
23310
|
+
snapshotStyleCleanup.get(snapshot)?.();
|
|
23311
|
+
snapshotStyleCleanup.delete(snapshot);
|
|
23312
|
+
snapshot.remove();
|
|
23313
|
+
}
|
|
23210
23314
|
}
|
|
23211
23315
|
/**
|
|
23212
23316
|
* The navigation each mounted passage display last committed to the page,
|
|
@@ -23283,7 +23387,13 @@ defineMacro({
|
|
|
23283
23387
|
scrollAfterCleanup.current = false;
|
|
23284
23388
|
cleanupSnapshots(containerRef.current);
|
|
23285
23389
|
}, []);
|
|
23286
|
-
useEffect(() =>
|
|
23390
|
+
useEffect(() => {
|
|
23391
|
+
const container = containerRef.current;
|
|
23392
|
+
return () => {
|
|
23393
|
+
cancelTransition();
|
|
23394
|
+
cleanupSnapshots(container);
|
|
23395
|
+
};
|
|
23396
|
+
}, [cancelTransition]);
|
|
23287
23397
|
useEffect(() => {
|
|
23288
23398
|
const store = useStoryStore.getState();
|
|
23289
23399
|
const { history, historyIndex, transitionConfig } = store;
|
|
@@ -25375,8 +25485,8 @@ function storeVarMacroNames() {
|
|
|
25375
25485
|
* the input macros that bind a variable (registered by author JS or
|
|
25376
25486
|
* StoryInit) and the roles of their parameters decide what is a reference.
|
|
25377
25487
|
*/
|
|
25378
|
-
function variableErrors(storyData, schema) {
|
|
25379
|
-
return validatePassages(storyData.passages, schema, storeVarMacroNames(), knownParameters());
|
|
25488
|
+
function variableErrors(storyData, schema, only = () => true) {
|
|
25489
|
+
return validatePassages(new Map([...storyData.passages].filter(([, passage]) => only(passage))), schema, storeVarMacroNames(), knownParameters());
|
|
25380
25490
|
}
|
|
25381
25491
|
/**
|
|
25382
25492
|
* The markup errors (malformed markup, unknown macros, syntax errors in
|
|
@@ -25427,7 +25537,7 @@ function boot() {
|
|
|
25427
25537
|
}
|
|
25428
25538
|
const schema = orStop(() => parseStoryVariables(storyVarsPassage.content));
|
|
25429
25539
|
for (const { name, block } of orStop(() => effectiveWidgets(storyData.passages.values()))) setBlockWidget(name, block);
|
|
25430
|
-
const errors = variableErrors(storyData, schema);
|
|
25540
|
+
const errors = variableErrors(storyData, schema, (p) => p.name === "StoryInit");
|
|
25431
25541
|
errors.push(...markupErrors(storyData, (p) => p.name === "StoryInit"));
|
|
25432
25542
|
let transientDefaults = {};
|
|
25433
25543
|
const storyTransientsPassage = storyData.passages.get("StoryTransients");
|
package/dist/pkg/play.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { glob, readFile, stat } from "node:fs/promises";
|
|
2
|
-
import {
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import { Worker } from "node:worker_threads";
|
|
5
5
|
//#region src/utils/error-message.ts
|
|
@@ -62,44 +62,6 @@ async function expandGlob(pattern) {
|
|
|
62
62
|
for await (const file of glob(pattern)) matches.push(file);
|
|
63
63
|
return matches.sort();
|
|
64
64
|
}
|
|
65
|
-
/** A path segment with glob syntax in it (as `path.matchesGlob` reads it). */
|
|
66
|
-
var GLOB_SEGMENT = /[*?[\]{}()!+@]/;
|
|
67
|
-
/**
|
|
68
|
-
* An `exclude` glob of the project config as twee-ts matches it: against
|
|
69
|
-
* paths relative to the working directory. A relative glob is relative to the
|
|
70
|
-
* project directory; an absolute one has its literal leading folders made
|
|
71
|
-
* relative. Either way the folder part added or rewritten is escaped.
|
|
72
|
-
*/
|
|
73
|
-
function rebaseExclude(dir, pattern) {
|
|
74
|
-
let base = dir;
|
|
75
|
-
let rest = pattern.replace(/^\.\//, "");
|
|
76
|
-
if (isAbsolute(pattern)) {
|
|
77
|
-
const parts = pattern.split("/");
|
|
78
|
-
let literal = parts.findIndex((part) => GLOB_SEGMENT.test(part));
|
|
79
|
-
if (literal === -1) literal = parts.length;
|
|
80
|
-
base = parts.slice(0, literal).join("/") || "/";
|
|
81
|
-
rest = parts.slice(literal).join("/");
|
|
82
|
-
}
|
|
83
|
-
const fromCwd = globLiteral(relative(process.cwd(), base).split(sep).join("/"));
|
|
84
|
-
if (fromCwd === "") return rest;
|
|
85
|
-
return rest === "" ? fromCwd : `${fromCwd}/${rest}`;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* The project config with its paths made independent of the working
|
|
89
|
-
* directory. twee-ts reads `sources`, `modules` and `headFile` relative to
|
|
90
|
-
* the working directory, and matches `exclude` globs against paths relative
|
|
91
|
-
* to it, so each is rebased from the project directory.
|
|
92
|
-
*/
|
|
93
|
-
function rebaseConfig(dir, config) {
|
|
94
|
-
const rebased = {
|
|
95
|
-
...config,
|
|
96
|
-
sources: (config.sources ?? ["."]).map((s) => resolve(dir, s))
|
|
97
|
-
};
|
|
98
|
-
if (config.modules) rebased.modules = config.modules.map((m) => resolve(dir, m));
|
|
99
|
-
if (config.headFile !== void 0) rebased.headFile = resolve(dir, config.headFile);
|
|
100
|
-
if (config.exclude) rebased.exclude = config.exclude.map((e) => rebaseExclude(dir, e));
|
|
101
|
-
return rebased;
|
|
102
|
-
}
|
|
103
65
|
/**
|
|
104
66
|
* The project config less its output and format options. Those are ours: the
|
|
105
67
|
* project's own would write a file, compile for another format, or fetch one.
|
|
@@ -107,14 +69,6 @@ function rebaseConfig(dir, config) {
|
|
|
107
69
|
function withoutFormatOptions(config) {
|
|
108
70
|
return Object.fromEntries(Object.entries(config).filter(([key]) => !key.startsWith("format") && !key.startsWith("output") && key !== "useTweegoPath" && key !== "noRemote"));
|
|
109
71
|
}
|
|
110
|
-
/**
|
|
111
|
-
* `text` as a glob matching only itself, for Node's `path.matchesGlob` (which
|
|
112
|
-
* twee-ts uses): a metacharacter goes in brackets. Braces and `!` cannot be
|
|
113
|
-
* bracketed there, so each becomes `?` (any one character).
|
|
114
|
-
*/
|
|
115
|
-
function globLiteral(text) {
|
|
116
|
-
return text.replace(/[*?[\]()+@]/g, (c) => `[${c}]`).replace(/[{}!]/g, "?");
|
|
117
|
-
}
|
|
118
72
|
/** The compile sources and project config for a path (or glob). */
|
|
119
73
|
async function sourcesFor(tweeTs, path) {
|
|
120
74
|
let isDirectory = false;
|
|
@@ -138,10 +92,10 @@ async function sourcesFor(tweeTs, path) {
|
|
|
138
92
|
sources: [path],
|
|
139
93
|
config: {}
|
|
140
94
|
};
|
|
141
|
-
const config =
|
|
95
|
+
const config = found;
|
|
142
96
|
if (config.sources?.length === 0) throw new Error(`spindle: ${path} has a twee-ts config with no sources`);
|
|
143
97
|
return {
|
|
144
|
-
sources: config.sources ?? [],
|
|
98
|
+
sources: [...config.sources ?? [resolve(path)]],
|
|
145
99
|
config
|
|
146
100
|
};
|
|
147
101
|
}
|