@rohal12/spindle 0.64.31 → 0.64.32
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 +435 -240
- package/dist/pkg/story-variables.js +278 -226
- package/package.json +1 -1
- package/src/code-check.ts +66 -13
- package/src/components/macros/Checkbox.tsx +10 -6
- package/src/components/macros/Listbox.tsx +11 -7
- package/src/components/macros/Radiobutton.tsx +25 -6
- package/src/components/macros/input-macro.ts +190 -9
- package/src/components/macros/macro-args.ts +29 -10
- package/src/define-macro.ts +2 -0
- package/src/format/format-passage.ts +29 -17
- package/src/markup/code-end.ts +23 -5
- package/src/markup/render.tsx +1 -1
- package/src/observe/observable-text.ts +2 -1
- package/src/story-api.ts +6 -1
- package/src/utils/variable-writes.ts +24 -0
- package/types/tooling.d.ts +9 -2
package/dist/pkg/headless.js
CHANGED
|
@@ -9359,13 +9359,13 @@ var VALID_EVENTS = /* @__PURE__ */ new Set([
|
|
|
9359
9359
|
"dialogrender",
|
|
9360
9360
|
"dialogclose"
|
|
9361
9361
|
]);
|
|
9362
|
-
var listeners$
|
|
9362
|
+
var listeners$3 = /* @__PURE__ */ new Map();
|
|
9363
9363
|
function on(event, cb) {
|
|
9364
9364
|
if (!VALID_EVENTS.has(event)) throw new Error(`spindle: Unknown event "${event}".`);
|
|
9365
|
-
let set = listeners$
|
|
9365
|
+
let set = listeners$3.get(event);
|
|
9366
9366
|
if (!set) {
|
|
9367
9367
|
set = /* @__PURE__ */ new Set();
|
|
9368
|
-
listeners$
|
|
9368
|
+
listeners$3.set(event, set);
|
|
9369
9369
|
}
|
|
9370
9370
|
set.add(cb);
|
|
9371
9371
|
return () => {
|
|
@@ -9374,7 +9374,7 @@ function on(event, cb) {
|
|
|
9374
9374
|
}
|
|
9375
9375
|
/** Call `call` with each listener of `event`. */
|
|
9376
9376
|
function forEachListener(event, call) {
|
|
9377
|
-
const set = listeners$
|
|
9377
|
+
const set = listeners$3.get(event);
|
|
9378
9378
|
if (!set) return;
|
|
9379
9379
|
for (const cb of [...set]) call(cb);
|
|
9380
9380
|
}
|
|
@@ -13916,6 +13916,204 @@ function lastEnd(body) {
|
|
|
13916
13916
|
const branch = last(body.branches);
|
|
13917
13917
|
return last(branch?.children ?? body.children)?.end ?? branch?.tag.end;
|
|
13918
13918
|
}
|
|
13919
|
+
// src/components/macros/arg-utils.ts
|
|
13920
|
+
/**
|
|
13921
|
+
* Shared helpers for macros that split or read their raw argument text by
|
|
13922
|
+
* hand. Their lexical model is the expression lexer's (`js-lexer.ts`):
|
|
13923
|
+
* string, template and regex literals and comments are opaque (a backslash
|
|
13924
|
+
* inside a literal escapes the character after it, and `${…}` parts of a
|
|
13925
|
+
* template are code), `/` opens a regex only where an operand is expected,
|
|
13926
|
+
* and `(`/`[`/`{` nest.
|
|
13927
|
+
*/
|
|
13928
|
+
/** True for a whitespace character. */
|
|
13929
|
+
function isWhitespace(ch) {
|
|
13930
|
+
return /\s/.test(ch);
|
|
13931
|
+
}
|
|
13932
|
+
function isOpener(ch) {
|
|
13933
|
+
return ch === "(" || ch === "[" || ch === "{";
|
|
13934
|
+
}
|
|
13935
|
+
function isCloser(ch) {
|
|
13936
|
+
return ch === ")" || ch === "]" || ch === "}";
|
|
13937
|
+
}
|
|
13938
|
+
/**
|
|
13939
|
+
* Given the index of an opening quote (`"`, `'` or a backtick), return the
|
|
13940
|
+
* index just past its closing quote, or `src.length` if it is unterminated.
|
|
13941
|
+
* A backslash escapes the character after it, so a quote preceded by an even
|
|
13942
|
+
* run of backslashes still closes the string and one preceded by an odd run
|
|
13943
|
+
* does not. Inside a template literal, `${…}` interpolations are lexed as
|
|
13944
|
+
* code, so quotes, backticks, braces and regexes inside them do not end it.
|
|
13945
|
+
*/
|
|
13946
|
+
function skipString(src, start) {
|
|
13947
|
+
return src[start] === "`" ? lexTemplate(src, start) : scanStringLiteral(src, start).end;
|
|
13948
|
+
}
|
|
13949
|
+
/**
|
|
13950
|
+
* Undo the escapes allowed in a literal quoted macro argument: `\"`, `\'`
|
|
13951
|
+
* and `\\` become `"`, `'` and `\`. Other backslash sequences (`\n`, `\d`)
|
|
13952
|
+
* are kept as written, so code and regex text inside the string survive.
|
|
13953
|
+
*/
|
|
13954
|
+
function unescapeQuoted(body) {
|
|
13955
|
+
return body.replace(/\\(["'\\])/g, "$1");
|
|
13956
|
+
}
|
|
13957
|
+
/**
|
|
13958
|
+
* Read the `"…"` or `'…'` string whose opening quote is at `start`.
|
|
13959
|
+
* Returns its unescaped value and the index just past the closing quote, or
|
|
13960
|
+
* `null` if there is no such quote at `start` or the string is unterminated.
|
|
13961
|
+
* With `tolerant`, an unterminated string instead extends to its scanned end.
|
|
13962
|
+
*/
|
|
13963
|
+
function readQuoted(src, start, tolerant = false) {
|
|
13964
|
+
const quote = src[start];
|
|
13965
|
+
if (quote !== "\"" && quote !== "'") return null;
|
|
13966
|
+
const { end, closed } = scanStringLiteral(src, start);
|
|
13967
|
+
if (!closed && !tolerant) return null;
|
|
13968
|
+
return {
|
|
13969
|
+
value: unescapeQuoted(src.slice(start + 1, closed ? end - 1 : end)),
|
|
13970
|
+
end
|
|
13971
|
+
};
|
|
13972
|
+
}
|
|
13973
|
+
/**
|
|
13974
|
+
* The unescaped value of `src` when it is exactly one `"…"` or `'…'` string
|
|
13975
|
+
* (as with `readQuoted`), otherwise `null`.
|
|
13976
|
+
*/
|
|
13977
|
+
function readWholeQuoted(src) {
|
|
13978
|
+
const quoted = readQuoted(src, 0);
|
|
13979
|
+
return quoted && quoted.end === src.length ? quoted.value : null;
|
|
13980
|
+
}
|
|
13981
|
+
/** A dialog body's passage name, optionally enclosed in matching quotes. */
|
|
13982
|
+
function readPassageBody(src) {
|
|
13983
|
+
const name = src.trim();
|
|
13984
|
+
return readWholeQuoted(name) ?? name;
|
|
13985
|
+
}
|
|
13986
|
+
/**
|
|
13987
|
+
* Strip an optional quote from each end of an unquoted-or-loosely-quoted
|
|
13988
|
+
* argument (`"Red`, `Red'`), leaving at least one character. This is the
|
|
13989
|
+
* lenient fallback for labels that are not one well-formed quoted string.
|
|
13990
|
+
*/
|
|
13991
|
+
function stripLooseQuotes(src) {
|
|
13992
|
+
return /^["']?(.+?)["']?$/s.exec(src)?.[1] ?? src;
|
|
13993
|
+
}
|
|
13994
|
+
/** An operator character, or an operator keyword, at the end of code. */
|
|
13995
|
+
var TRAILING_OPERATOR_RE = /(?:[-+*/%&|^!~=<>?:,.]|(?:^|[^\w$@.])(?:typeof|void|new|delete|in|instanceof|await|yield))$/;
|
|
13996
|
+
/**
|
|
13997
|
+
* Whether `src` ends with an operator that still needs an operand (`$a +`,
|
|
13998
|
+
* `$a+`, `$x ==`, `typeof`), so it cannot be a complete value on its own.
|
|
13999
|
+
* Only code counts: the closing `/` of a regex literal is not division, a
|
|
14000
|
+
* variable reference (`$a`, `%name`, ...) is an operand, trailing comments are skipped,
|
|
14001
|
+
* and a postfix `++` or `--` completes its operand.
|
|
14002
|
+
*/
|
|
14003
|
+
function endsWithOperator(src) {
|
|
14004
|
+
let end = -1;
|
|
14005
|
+
lexJs(src, {
|
|
14006
|
+
code(ch, i, nesting) {
|
|
14007
|
+
if (nesting === 0 && !isWhitespace(ch)) end = i + 1;
|
|
14008
|
+
},
|
|
14009
|
+
literal(text, _i, nesting) {
|
|
14010
|
+
const comment = text.startsWith("//") || text.startsWith("/*");
|
|
14011
|
+
if (nesting === 0 && !comment) end = -1;
|
|
14012
|
+
},
|
|
14013
|
+
variable(_sigil, _name, _i, nesting) {
|
|
14014
|
+
if (nesting === 0) end = -1;
|
|
14015
|
+
}
|
|
14016
|
+
});
|
|
14017
|
+
if (end < 0) return false;
|
|
14018
|
+
const code = src.slice(0, end);
|
|
14019
|
+
if (/(?:\+\+|--)$/.test(code)) return false;
|
|
14020
|
+
return TRAILING_OPERATOR_RE.test(code);
|
|
14021
|
+
}
|
|
14022
|
+
/**
|
|
14023
|
+
* Indices of the characters in `src` that satisfy `isSeparator` and sit at
|
|
14024
|
+
* depth 0: in code outside string, template and regex literals and comments,
|
|
14025
|
+
* and outside `()`, `[]` and `{}` pairs.
|
|
14026
|
+
*/
|
|
14027
|
+
function topLevelIndices(src, isSeparator) {
|
|
14028
|
+
const indices = [];
|
|
14029
|
+
let depth = 0;
|
|
14030
|
+
lexJs(src, { code(ch, i, nesting) {
|
|
14031
|
+
if (nesting > 0) return;
|
|
14032
|
+
if (isOpener(ch)) depth++;
|
|
14033
|
+
else if (isCloser(ch)) depth--;
|
|
14034
|
+
else if (depth === 0 && isSeparator(ch)) indices.push(i);
|
|
14035
|
+
} });
|
|
14036
|
+
return indices;
|
|
14037
|
+
}
|
|
14038
|
+
/**
|
|
14039
|
+
* The segments of `src` between its depth-0 separators (see
|
|
14040
|
+
* `topLevelIndices`), as spans: untrimmed, and empty segments are kept, so
|
|
14041
|
+
* `n` separators always give `n + 1` segments.
|
|
14042
|
+
*/
|
|
14043
|
+
function topLevelSpans(src, isSeparator) {
|
|
14044
|
+
const spans = [];
|
|
14045
|
+
let from = 0;
|
|
14046
|
+
for (const i of topLevelIndices(src, isSeparator)) {
|
|
14047
|
+
spans.push([from, i]);
|
|
14048
|
+
from = i + 1;
|
|
14049
|
+
}
|
|
14050
|
+
spans.push([from, src.length]);
|
|
14051
|
+
return spans;
|
|
14052
|
+
}
|
|
14053
|
+
/**
|
|
14054
|
+
* Check whether a whitespace-delimited token looks like a standalone value
|
|
14055
|
+
* (not an operator or partial expression): it starts like a value and does
|
|
14056
|
+
* not end with an operator still waiting for its operand.
|
|
14057
|
+
*/
|
|
14058
|
+
function isStandaloneValue(token) {
|
|
14059
|
+
return startsLikeValue(token) && !endsWithOperator(token);
|
|
14060
|
+
}
|
|
14061
|
+
function startsLikeValue(token) {
|
|
14062
|
+
const first = token[0];
|
|
14063
|
+
if (first === "\"" || first === "'" || first === "`") return true;
|
|
14064
|
+
if (/^[$_@]\w|^%[A-Za-z_]/.test(token)) return true;
|
|
14065
|
+
if (/^[+-]?(?:\d|\.\d)/.test(token)) return true;
|
|
14066
|
+
if (first === "(" || first === "[" || first === "{") return true;
|
|
14067
|
+
if (/^(?:true|false|null|undefined)(?![\w$])/.test(token)) return true;
|
|
14068
|
+
if (first === "!" && token.length > 1 && token[1] !== "=") return true;
|
|
14069
|
+
return false;
|
|
14070
|
+
}
|
|
14071
|
+
/**
|
|
14072
|
+
* Try to split a raw string on whitespace at depth 0 (respecting strings,
|
|
14073
|
+
* template literals, parentheses, brackets, and braces). Each resulting token
|
|
14074
|
+
* must pass `isStandaloneValue()` or the split is rejected and `null` is
|
|
14075
|
+
* returned. The spans are those of the tokens in `raw`.
|
|
14076
|
+
*/
|
|
14077
|
+
function trySplitOnWhitespace(raw) {
|
|
14078
|
+
const args = topLevelSpans(raw, isWhitespace).filter(([a, b]) => b > a);
|
|
14079
|
+
if (args.length < 2) return null;
|
|
14080
|
+
for (const [a, b] of args) if (!isStandaloneValue(raw.slice(a, b))) return null;
|
|
14081
|
+
return args;
|
|
14082
|
+
}
|
|
14083
|
+
/** `span` less the whitespace at its ends. */
|
|
14084
|
+
function trimmed$1(src, [start, end]) {
|
|
14085
|
+
const text = src.slice(start, end);
|
|
14086
|
+
const lead = text.length - text.trimStart().length;
|
|
14087
|
+
return [start + lead, Math.max(start + lead, end - (text.length - text.trimEnd().length))];
|
|
14088
|
+
}
|
|
14089
|
+
/**
|
|
14090
|
+
* Where the arguments in `raw` are written (see `splitArgs`), as spans of
|
|
14091
|
+
* `raw`.
|
|
14092
|
+
*/
|
|
14093
|
+
function splitArgSpans(raw) {
|
|
14094
|
+
const args = topLevelSpans(raw, (ch) => ch === ",").map((span) => trimmed$1(raw, span));
|
|
14095
|
+
const hasComma = args.length > 1;
|
|
14096
|
+
const last = args[args.length - 1];
|
|
14097
|
+
if (last[0] === last[1]) args.pop();
|
|
14098
|
+
if (!hasComma && args.length === 1) {
|
|
14099
|
+
const [start, end] = args[0];
|
|
14100
|
+
const split = trySplitOnWhitespace(raw.slice(start, end));
|
|
14101
|
+
if (split) return split.map(([a, b]) => [start + a, start + b]);
|
|
14102
|
+
}
|
|
14103
|
+
return args;
|
|
14104
|
+
}
|
|
14105
|
+
/**
|
|
14106
|
+
* Split rawArgs by commas, respecting parentheses, brackets, braces, and
|
|
14107
|
+
* strings. When no top-level commas are present, also supports adjacent quoted
|
|
14108
|
+
* string literals separated by whitespace (e.g. `"Label" "target"`).
|
|
14109
|
+
*/
|
|
14110
|
+
function splitArgs(raw) {
|
|
14111
|
+
return splitArgSpans(raw).map(([a, b]) => raw.slice(a, b));
|
|
14112
|
+
}
|
|
14113
|
+
/** `text` as a regular expression source that matches it literally. */
|
|
14114
|
+
function escapeRegExp(text) {
|
|
14115
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
14116
|
+
}
|
|
13919
14117
|
// src/markup/code-end.ts
|
|
13920
14118
|
/**
|
|
13921
14119
|
* A quote directly after a letter/digit is an apostrophe (don't), not a
|
|
@@ -14020,6 +14218,8 @@ function scanLenient(input, start) {
|
|
|
14020
14218
|
* input (see `OnOpenCodeEnd`); the ends it finds are the same.
|
|
14021
14219
|
*/
|
|
14022
14220
|
function jsCodeEnd(onOpenEnd) {
|
|
14221
|
+
let indexedInput = "";
|
|
14222
|
+
const rawClosers = /* @__PURE__ */ new Map();
|
|
14023
14223
|
return {
|
|
14024
14224
|
closeBrace(input, codeStart, lenientStart) {
|
|
14025
14225
|
let ranOut = false;
|
|
@@ -14030,10 +14230,18 @@ function jsCodeEnd(onOpenEnd) {
|
|
|
14030
14230
|
return end;
|
|
14031
14231
|
},
|
|
14032
14232
|
rawBodyEnd(input, bodyStart, name) {
|
|
14033
|
-
|
|
14034
|
-
|
|
14035
|
-
|
|
14036
|
-
|
|
14233
|
+
if (indexedInput !== input) {
|
|
14234
|
+
indexedInput = input;
|
|
14235
|
+
rawClosers.clear();
|
|
14236
|
+
}
|
|
14237
|
+
name = name.toLowerCase();
|
|
14238
|
+
const closer = `\\{/${escapeRegExp(name)}\\s*\\}`;
|
|
14239
|
+
let candidates = rawClosers.get(name);
|
|
14240
|
+
if (!candidates) {
|
|
14241
|
+
candidates = Array.from(input.matchAll(new RegExp(closer, "gi")), (m) => m.index);
|
|
14242
|
+
rawClosers.set(name, candidates);
|
|
14243
|
+
}
|
|
14244
|
+
const firstAt = candidates[upperBound(candidates, bodyStart - 1)] ?? -1;
|
|
14037
14245
|
if (firstAt === -1) return -1;
|
|
14038
14246
|
const at = new RegExp(closer, "iy");
|
|
14039
14247
|
let ranOut = false;
|
|
@@ -14046,7 +14254,7 @@ function jsCodeEnd(onOpenEnd) {
|
|
|
14046
14254
|
onRunOut: () => ranOut = true
|
|
14047
14255
|
});
|
|
14048
14256
|
if (end !== -1) return end;
|
|
14049
|
-
if (onOpenEnd && (ranOut || new RegExp(`\\{/${name}\\s*$`, "i").test(input))) onOpenEnd(input, bodyStart);
|
|
14257
|
+
if (onOpenEnd && (ranOut || new RegExp(`\\{/${escapeRegExp(name)}\\s*$`, "i").test(input))) onOpenEnd(input, bodyStart);
|
|
14050
14258
|
return firstAt;
|
|
14051
14259
|
}
|
|
14052
14260
|
};
|
|
@@ -14442,10 +14650,10 @@ function createListeners() {
|
|
|
14442
14650
|
// src/runtime-errors.ts
|
|
14443
14651
|
var errors = [];
|
|
14444
14652
|
var nextId = 1;
|
|
14445
|
-
var listeners$
|
|
14653
|
+
var listeners$2 = createListeners();
|
|
14446
14654
|
function update(next) {
|
|
14447
14655
|
errors = next;
|
|
14448
|
-
listeners$
|
|
14656
|
+
listeners$2.notify();
|
|
14449
14657
|
}
|
|
14450
14658
|
/**
|
|
14451
14659
|
* Show `error` on the page, after `context` (what failed). The same error
|
|
@@ -14485,205 +14693,7 @@ function getRuntimeErrors() {
|
|
|
14485
14693
|
return errors;
|
|
14486
14694
|
}
|
|
14487
14695
|
/** Call `listener` whenever the shown errors change; returns unsubscribe. */
|
|
14488
|
-
var subscribeRuntimeErrors = listeners$
|
|
14489
|
-
// src/components/macros/arg-utils.ts
|
|
14490
|
-
/**
|
|
14491
|
-
* Shared helpers for macros that split or read their raw argument text by
|
|
14492
|
-
* hand. Their lexical model is the expression lexer's (`js-lexer.ts`):
|
|
14493
|
-
* string, template and regex literals and comments are opaque (a backslash
|
|
14494
|
-
* inside a literal escapes the character after it, and `${…}` parts of a
|
|
14495
|
-
* template are code), `/` opens a regex only where an operand is expected,
|
|
14496
|
-
* and `(`/`[`/`{` nest.
|
|
14497
|
-
*/
|
|
14498
|
-
/** True for a whitespace character. */
|
|
14499
|
-
function isWhitespace(ch) {
|
|
14500
|
-
return /\s/.test(ch);
|
|
14501
|
-
}
|
|
14502
|
-
function isOpener(ch) {
|
|
14503
|
-
return ch === "(" || ch === "[" || ch === "{";
|
|
14504
|
-
}
|
|
14505
|
-
function isCloser(ch) {
|
|
14506
|
-
return ch === ")" || ch === "]" || ch === "}";
|
|
14507
|
-
}
|
|
14508
|
-
/**
|
|
14509
|
-
* Given the index of an opening quote (`"`, `'` or a backtick), return the
|
|
14510
|
-
* index just past its closing quote, or `src.length` if it is unterminated.
|
|
14511
|
-
* A backslash escapes the character after it, so a quote preceded by an even
|
|
14512
|
-
* run of backslashes still closes the string and one preceded by an odd run
|
|
14513
|
-
* does not. Inside a template literal, `${…}` interpolations are lexed as
|
|
14514
|
-
* code, so quotes, backticks, braces and regexes inside them do not end it.
|
|
14515
|
-
*/
|
|
14516
|
-
function skipString(src, start) {
|
|
14517
|
-
return src[start] === "`" ? lexTemplate(src, start) : scanStringLiteral(src, start).end;
|
|
14518
|
-
}
|
|
14519
|
-
/**
|
|
14520
|
-
* Undo the escapes allowed in a literal quoted macro argument: `\"`, `\'`
|
|
14521
|
-
* and `\\` become `"`, `'` and `\`. Other backslash sequences (`\n`, `\d`)
|
|
14522
|
-
* are kept as written, so code and regex text inside the string survive.
|
|
14523
|
-
*/
|
|
14524
|
-
function unescapeQuoted(body) {
|
|
14525
|
-
return body.replace(/\\(["'\\])/g, "$1");
|
|
14526
|
-
}
|
|
14527
|
-
/**
|
|
14528
|
-
* Read the `"…"` or `'…'` string whose opening quote is at `start`.
|
|
14529
|
-
* Returns its unescaped value and the index just past the closing quote, or
|
|
14530
|
-
* `null` if there is no such quote at `start` or the string is unterminated.
|
|
14531
|
-
* With `tolerant`, an unterminated string instead extends to its scanned end.
|
|
14532
|
-
*/
|
|
14533
|
-
function readQuoted(src, start, tolerant = false) {
|
|
14534
|
-
const quote = src[start];
|
|
14535
|
-
if (quote !== "\"" && quote !== "'") return null;
|
|
14536
|
-
const { end, closed } = scanStringLiteral(src, start);
|
|
14537
|
-
if (!closed && !tolerant) return null;
|
|
14538
|
-
return {
|
|
14539
|
-
value: unescapeQuoted(src.slice(start + 1, closed ? end - 1 : end)),
|
|
14540
|
-
end
|
|
14541
|
-
};
|
|
14542
|
-
}
|
|
14543
|
-
/**
|
|
14544
|
-
* The unescaped value of `src` when it is exactly one `"…"` or `'…'` string
|
|
14545
|
-
* (as with `readQuoted`), otherwise `null`.
|
|
14546
|
-
*/
|
|
14547
|
-
function readWholeQuoted(src) {
|
|
14548
|
-
const quoted = readQuoted(src, 0);
|
|
14549
|
-
return quoted && quoted.end === src.length ? quoted.value : null;
|
|
14550
|
-
}
|
|
14551
|
-
/** A dialog body's passage name, optionally enclosed in matching quotes. */
|
|
14552
|
-
function readPassageBody(src) {
|
|
14553
|
-
const name = src.trim();
|
|
14554
|
-
return readWholeQuoted(name) ?? name;
|
|
14555
|
-
}
|
|
14556
|
-
/**
|
|
14557
|
-
* Strip an optional quote from each end of an unquoted-or-loosely-quoted
|
|
14558
|
-
* argument (`"Red`, `Red'`), leaving at least one character. This is the
|
|
14559
|
-
* lenient fallback for labels that are not one well-formed quoted string.
|
|
14560
|
-
*/
|
|
14561
|
-
function stripLooseQuotes(src) {
|
|
14562
|
-
return /^["']?(.+?)["']?$/s.exec(src)?.[1] ?? src;
|
|
14563
|
-
}
|
|
14564
|
-
/** An operator character, or an operator keyword, at the end of code. */
|
|
14565
|
-
var TRAILING_OPERATOR_RE = /(?:[-+*/%&|^!~=<>?:,.]|(?:^|[^\w$@.])(?:typeof|void|new|delete|in|instanceof|await|yield))$/;
|
|
14566
|
-
/**
|
|
14567
|
-
* Whether `src` ends with an operator that still needs an operand (`$a +`,
|
|
14568
|
-
* `$a+`, `$x ==`, `typeof`), so it cannot be a complete value on its own.
|
|
14569
|
-
* Only code counts: the closing `/` of a regex literal is not division, a
|
|
14570
|
-
* variable reference (`$a`, `%name`, ...) is an operand, trailing comments are skipped,
|
|
14571
|
-
* and a postfix `++` or `--` completes its operand.
|
|
14572
|
-
*/
|
|
14573
|
-
function endsWithOperator(src) {
|
|
14574
|
-
let end = -1;
|
|
14575
|
-
lexJs(src, {
|
|
14576
|
-
code(ch, i, nesting) {
|
|
14577
|
-
if (nesting === 0 && !isWhitespace(ch)) end = i + 1;
|
|
14578
|
-
},
|
|
14579
|
-
literal(text, _i, nesting) {
|
|
14580
|
-
const comment = text.startsWith("//") || text.startsWith("/*");
|
|
14581
|
-
if (nesting === 0 && !comment) end = -1;
|
|
14582
|
-
},
|
|
14583
|
-
variable(_sigil, _name, _i, nesting) {
|
|
14584
|
-
if (nesting === 0) end = -1;
|
|
14585
|
-
}
|
|
14586
|
-
});
|
|
14587
|
-
if (end < 0) return false;
|
|
14588
|
-
const code = src.slice(0, end);
|
|
14589
|
-
if (/(?:\+\+|--)$/.test(code)) return false;
|
|
14590
|
-
return TRAILING_OPERATOR_RE.test(code);
|
|
14591
|
-
}
|
|
14592
|
-
/**
|
|
14593
|
-
* Indices of the characters in `src` that satisfy `isSeparator` and sit at
|
|
14594
|
-
* depth 0: in code outside string, template and regex literals and comments,
|
|
14595
|
-
* and outside `()`, `[]` and `{}` pairs.
|
|
14596
|
-
*/
|
|
14597
|
-
function topLevelIndices(src, isSeparator) {
|
|
14598
|
-
const indices = [];
|
|
14599
|
-
let depth = 0;
|
|
14600
|
-
lexJs(src, { code(ch, i, nesting) {
|
|
14601
|
-
if (nesting > 0) return;
|
|
14602
|
-
if (isOpener(ch)) depth++;
|
|
14603
|
-
else if (isCloser(ch)) depth--;
|
|
14604
|
-
else if (depth === 0 && isSeparator(ch)) indices.push(i);
|
|
14605
|
-
} });
|
|
14606
|
-
return indices;
|
|
14607
|
-
}
|
|
14608
|
-
/**
|
|
14609
|
-
* The segments of `src` between its depth-0 separators (see
|
|
14610
|
-
* `topLevelIndices`), as spans: untrimmed, and empty segments are kept, so
|
|
14611
|
-
* `n` separators always give `n + 1` segments.
|
|
14612
|
-
*/
|
|
14613
|
-
function topLevelSpans(src, isSeparator) {
|
|
14614
|
-
const spans = [];
|
|
14615
|
-
let from = 0;
|
|
14616
|
-
for (const i of topLevelIndices(src, isSeparator)) {
|
|
14617
|
-
spans.push([from, i]);
|
|
14618
|
-
from = i + 1;
|
|
14619
|
-
}
|
|
14620
|
-
spans.push([from, src.length]);
|
|
14621
|
-
return spans;
|
|
14622
|
-
}
|
|
14623
|
-
/**
|
|
14624
|
-
* Check whether a whitespace-delimited token looks like a standalone value
|
|
14625
|
-
* (not an operator or partial expression): it starts like a value and does
|
|
14626
|
-
* not end with an operator still waiting for its operand.
|
|
14627
|
-
*/
|
|
14628
|
-
function isStandaloneValue(token) {
|
|
14629
|
-
return startsLikeValue(token) && !endsWithOperator(token);
|
|
14630
|
-
}
|
|
14631
|
-
function startsLikeValue(token) {
|
|
14632
|
-
const first = token[0];
|
|
14633
|
-
if (first === "\"" || first === "'" || first === "`") return true;
|
|
14634
|
-
if (/^[$_@]\w|^%[A-Za-z_]/.test(token)) return true;
|
|
14635
|
-
if (/^[+-]?(?:\d|\.\d)/.test(token)) return true;
|
|
14636
|
-
if (first === "(" || first === "[" || first === "{") return true;
|
|
14637
|
-
if (/^(?:true|false|null|undefined)(?![\w$])/.test(token)) return true;
|
|
14638
|
-
if (first === "!" && token.length > 1 && token[1] !== "=") return true;
|
|
14639
|
-
return false;
|
|
14640
|
-
}
|
|
14641
|
-
/**
|
|
14642
|
-
* Try to split a raw string on whitespace at depth 0 (respecting strings,
|
|
14643
|
-
* template literals, parentheses, brackets, and braces). Each resulting token
|
|
14644
|
-
* must pass `isStandaloneValue()` or the split is rejected and `null` is
|
|
14645
|
-
* returned. The spans are those of the tokens in `raw`.
|
|
14646
|
-
*/
|
|
14647
|
-
function trySplitOnWhitespace(raw) {
|
|
14648
|
-
const args = topLevelSpans(raw, isWhitespace).filter(([a, b]) => b > a);
|
|
14649
|
-
if (args.length < 2) return null;
|
|
14650
|
-
for (const [a, b] of args) if (!isStandaloneValue(raw.slice(a, b))) return null;
|
|
14651
|
-
return args;
|
|
14652
|
-
}
|
|
14653
|
-
/** `span` less the whitespace at its ends. */
|
|
14654
|
-
function trimmed$1(src, [start, end]) {
|
|
14655
|
-
const text = src.slice(start, end);
|
|
14656
|
-
const lead = text.length - text.trimStart().length;
|
|
14657
|
-
return [start + lead, Math.max(start + lead, end - (text.length - text.trimEnd().length))];
|
|
14658
|
-
}
|
|
14659
|
-
/**
|
|
14660
|
-
* Where the arguments in `raw` are written (see `splitArgs`), as spans of
|
|
14661
|
-
* `raw`.
|
|
14662
|
-
*/
|
|
14663
|
-
function splitArgSpans(raw) {
|
|
14664
|
-
const args = topLevelSpans(raw, (ch) => ch === ",").map((span) => trimmed$1(raw, span));
|
|
14665
|
-
const hasComma = args.length > 1;
|
|
14666
|
-
const last = args[args.length - 1];
|
|
14667
|
-
if (last[0] === last[1]) args.pop();
|
|
14668
|
-
if (!hasComma && args.length === 1) {
|
|
14669
|
-
const [start, end] = args[0];
|
|
14670
|
-
const split = trySplitOnWhitespace(raw.slice(start, end));
|
|
14671
|
-
if (split) return split.map(([a, b]) => [start + a, start + b]);
|
|
14672
|
-
}
|
|
14673
|
-
return args;
|
|
14674
|
-
}
|
|
14675
|
-
/**
|
|
14676
|
-
* Split rawArgs by commas, respecting parentheses, brackets, braces, and
|
|
14677
|
-
* strings. When no top-level commas are present, also supports adjacent quoted
|
|
14678
|
-
* string literals separated by whitespace (e.g. `"Label" "target"`).
|
|
14679
|
-
*/
|
|
14680
|
-
function splitArgs(raw) {
|
|
14681
|
-
return splitArgSpans(raw).map(([a, b]) => raw.slice(a, b));
|
|
14682
|
-
}
|
|
14683
|
-
/** `text` as a regular expression source that matches it literally. */
|
|
14684
|
-
function escapeRegExp(text) {
|
|
14685
|
-
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
14686
|
-
}
|
|
14696
|
+
var subscribeRuntimeErrors = listeners$2.subscribe;
|
|
14687
14697
|
// src/components/macros/macro-args.ts
|
|
14688
14698
|
/**
|
|
14689
14699
|
* The macro argument layer: reads a macro's raw argument text into the
|
|
@@ -14863,14 +14873,28 @@ function trimmed(src, at) {
|
|
|
14863
14873
|
function readGroup(params, src, args, { at: srcAt, spans, toolingSpans }) {
|
|
14864
14874
|
let [text, at] = trimmed(src ?? "", srcAt);
|
|
14865
14875
|
const alone = !params.some((p) => p.type !== "flag" && p.required);
|
|
14866
|
-
|
|
14867
|
-
|
|
14868
|
-
|
|
14869
|
-
|
|
14870
|
-
|
|
14871
|
-
|
|
14872
|
-
|
|
14876
|
+
const flags = params.filter((p) => p.type === "flag");
|
|
14877
|
+
for (const flag of flags) args[flag.name] = false;
|
|
14878
|
+
const byName = new Map(flags.map((flag) => [flag.name, flag]));
|
|
14879
|
+
while (text && byName.size) {
|
|
14880
|
+
const parts = terms(text);
|
|
14881
|
+
if (parts.length === 0) break;
|
|
14882
|
+
let changed = false;
|
|
14883
|
+
for (const name of [slice(text, parts[parts.length - 1]), slice(text, parts[0])]) {
|
|
14884
|
+
const flag = byName.get(name);
|
|
14885
|
+
if (!flag || args[flag.name]) continue;
|
|
14886
|
+
const rest = src === null ? null : takeFlag(text, flag.name, alone);
|
|
14887
|
+
if (rest) {
|
|
14888
|
+
args[flag.name] = true;
|
|
14889
|
+
byName.delete(flag.name);
|
|
14890
|
+
changed = true;
|
|
14891
|
+
const flagSpan = rest[0] > 0 ? [0, text.indexOf(flag.name) + flag.name.length] : [text.lastIndexOf(flag.name), text.length];
|
|
14892
|
+
toolingSpans?.set(flag, [at + flagSpan[0], at + flagSpan[1]]);
|
|
14893
|
+
[text, at] = trimmed(slice(text, rest), at + rest[0]);
|
|
14894
|
+
break;
|
|
14895
|
+
}
|
|
14873
14896
|
}
|
|
14897
|
+
if (!changed) break;
|
|
14874
14898
|
}
|
|
14875
14899
|
const positional = params.filter((p) => p.type !== "flag");
|
|
14876
14900
|
if (positional.length === 0) return;
|
|
@@ -15131,13 +15155,13 @@ function parseOrError(code, goal) {
|
|
|
15131
15155
|
parses?.set(key, result);
|
|
15132
15156
|
return result;
|
|
15133
15157
|
}
|
|
15134
|
-
/** Where
|
|
15158
|
+
/** Where boundary `index` of a piece's code, text or name is in the source. */
|
|
15135
15159
|
function pieceOffset(piece, index) {
|
|
15136
15160
|
return piece.sourceOffsets?.[index] ?? piece.offset + index;
|
|
15137
15161
|
}
|
|
15138
15162
|
/** How much of a piece's own text or code there is (not its source extent). */
|
|
15139
15163
|
function extentOf(piece) {
|
|
15140
|
-
return piece.kind === "code" ? piece.code.length : piece.kind === "text" ? piece.text.length : piece.length;
|
|
15164
|
+
return piece.kind === "code" ? piece.code.length : piece.kind === "text" ? piece.text.length : piece.kind === "passage" ? piece.name.length : piece.length;
|
|
15141
15165
|
}
|
|
15142
15166
|
/** Macros whose whole argument text is one expression: branch conditions. */
|
|
15143
15167
|
var CONDITION_MACROS = /* @__PURE__ */ new Set([
|
|
@@ -15334,13 +15358,17 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
15334
15358
|
if (close > t + 1) {
|
|
15335
15359
|
const text = src.slice(token.end, tokens[close].start);
|
|
15336
15360
|
const written = text.trim();
|
|
15361
|
+
const at = token.end + text.indexOf(written);
|
|
15362
|
+
const nameValue = readPassageBody(written);
|
|
15363
|
+
const sourceOffsets = readWholeQuoted(written) !== null ? escapeOffsets(written.slice(1, -1), at + 1, nameValue, "quotes", true) : void 0;
|
|
15337
15364
|
yield {
|
|
15338
15365
|
kind: "passage",
|
|
15339
|
-
name:
|
|
15340
|
-
offset:
|
|
15366
|
+
name: nameValue,
|
|
15367
|
+
offset: at,
|
|
15341
15368
|
length: written.length,
|
|
15342
15369
|
macro: name,
|
|
15343
|
-
label: src.slice(token.start, token.end)
|
|
15370
|
+
label: src.slice(token.start, token.end),
|
|
15371
|
+
...sourceOffsets && { sourceOffsets }
|
|
15344
15372
|
};
|
|
15345
15373
|
}
|
|
15346
15374
|
}
|
|
@@ -15430,7 +15458,7 @@ function nestedPiece(piece, toRoot, where) {
|
|
|
15430
15458
|
nested: true,
|
|
15431
15459
|
where
|
|
15432
15460
|
};
|
|
15433
|
-
if (piece.kind === "
|
|
15461
|
+
if (piece.kind === "argument-error") {
|
|
15434
15462
|
const length = toRoot(piece.offset + piece.length) - offset;
|
|
15435
15463
|
return {
|
|
15436
15464
|
...piece,
|
|
@@ -15440,7 +15468,7 @@ function nestedPiece(piece, toRoot, where) {
|
|
|
15440
15468
|
}
|
|
15441
15469
|
const extent = extentOf(piece);
|
|
15442
15470
|
const sourceOffsets = piece.sourceOffsets ? piece.sourceOffsets.map(toRoot) : toRoot(piece.offset + extent) - offset !== extent ? Array.from({ length: extent + 1 }, (_, i) => toRoot(piece.offset + i)) : void 0;
|
|
15443
|
-
|
|
15471
|
+
const mapped = sourceOffsets ? {
|
|
15444
15472
|
...piece,
|
|
15445
15473
|
...nested,
|
|
15446
15474
|
sourceOffsets
|
|
@@ -15448,6 +15476,10 @@ function nestedPiece(piece, toRoot, where) {
|
|
|
15448
15476
|
...piece,
|
|
15449
15477
|
...nested
|
|
15450
15478
|
};
|
|
15479
|
+
return mapped.kind === "passage" ? {
|
|
15480
|
+
...mapped,
|
|
15481
|
+
length: toRoot(piece.offset + mapped.length) - offset
|
|
15482
|
+
} : mapped;
|
|
15451
15483
|
}
|
|
15452
15484
|
/** An escape sequence of a JavaScript string literal (with its backslash). */
|
|
15453
15485
|
var JS_ESCAPE_RE = /\\(?:u\{[0-9a-fA-F]+\}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|[0-3][0-7]{0,2}|[4-7][0-7]?|\r\n|[^])/y;
|
|
@@ -15476,9 +15508,10 @@ function escapeAt(raw, i, escapes) {
|
|
|
15476
15508
|
* written as `raw` from `at` on (its escapes are those of `escapes`), is in
|
|
15477
15509
|
* the source, and where the text ends. A character that an escape stands for
|
|
15478
15510
|
* is where the escape begins. None when the text is where its length says, or
|
|
15479
|
-
* is not that string.
|
|
15511
|
+
* is not that string. With `includeLinear`, return the map even without
|
|
15512
|
+
* escapes (a passage piece's offset includes the opening quote).
|
|
15480
15513
|
*/
|
|
15481
|
-
function escapeOffsets(raw, at, value, escapes) {
|
|
15514
|
+
function escapeOffsets(raw, at, value, escapes, includeLinear = false) {
|
|
15482
15515
|
const offsets = [];
|
|
15483
15516
|
let escaped = false;
|
|
15484
15517
|
let i = 0;
|
|
@@ -15494,8 +15527,14 @@ function escapeOffsets(raw, at, value, escapes) {
|
|
|
15494
15527
|
escaped = true;
|
|
15495
15528
|
i += escape.length;
|
|
15496
15529
|
}
|
|
15530
|
+
while (raw[i] === "\\") {
|
|
15531
|
+
const escape = escapeAt(raw, i, escapes);
|
|
15532
|
+
if (!escape || escape.text.length !== 0) break;
|
|
15533
|
+
escaped = true;
|
|
15534
|
+
i += escape.length;
|
|
15535
|
+
}
|
|
15497
15536
|
offsets.push(at + i);
|
|
15498
|
-
return escaped ? offsets : void 0;
|
|
15537
|
+
return escaped || includeLinear ? offsets : void 0;
|
|
15499
15538
|
}
|
|
15500
15539
|
/** The code and text in the arguments `args` (at `offset`) of `macro`. */
|
|
15501
15540
|
function* argPieces(args, offset, params, macro, label, navigation = []) {
|
|
@@ -15563,20 +15602,21 @@ function* valuePieces(args, offset, params, values, spans, macro, label, escapes
|
|
|
15563
15602
|
const [start, end] = span;
|
|
15564
15603
|
const written = args.slice(start, end);
|
|
15565
15604
|
/** The name as written (quotes included), at the argument. */
|
|
15566
|
-
const passage = (passageName) => ({
|
|
15605
|
+
const passage = (passageName, at, mode = escapes) => ({
|
|
15567
15606
|
kind: "passage",
|
|
15568
15607
|
name: passageName,
|
|
15569
15608
|
offset: offset + start,
|
|
15570
15609
|
length: end - start,
|
|
15571
15610
|
macro: name,
|
|
15572
15611
|
label,
|
|
15612
|
+
sourceOffsets: escapeOffsets(args.slice(at - offset), at, passageName, mode, true),
|
|
15573
15613
|
...navigation.includes(param.name) && { navigation: true }
|
|
15574
15614
|
});
|
|
15575
15615
|
const goal = codeGoal(param);
|
|
15576
15616
|
if (param.type === "passage") {
|
|
15577
15617
|
const target = passageTarget(value);
|
|
15578
15618
|
if (target.kind === "name") {
|
|
15579
|
-
yield passage(target.name);
|
|
15619
|
+
yield passage(target.name, offset + start + 1, "javascript");
|
|
15580
15620
|
continue;
|
|
15581
15621
|
}
|
|
15582
15622
|
}
|
|
@@ -15609,7 +15649,7 @@ function* valuePieces(args, offset, params, values, spans, macro, label, escapes
|
|
|
15609
15649
|
};
|
|
15610
15650
|
if (sourceOffsets) piece.sourceOffsets = sourceOffsets;
|
|
15611
15651
|
yield piece;
|
|
15612
|
-
} else if (holds === "passage") yield passage(value);
|
|
15652
|
+
} else if (holds === "passage") yield passage(value, at);
|
|
15613
15653
|
else if (holds === "markup") {
|
|
15614
15654
|
const piece = textPiece(value, at, `In the ${param.name} of {${macro}}: `);
|
|
15615
15655
|
if (sourceOffsets) piece.sourceOffsets = sourceOffsets;
|
|
@@ -17625,7 +17665,7 @@ function HtmlNodeRenderer({ node }) {
|
|
|
17625
17665
|
value: childNamespace,
|
|
17626
17666
|
children
|
|
17627
17667
|
});
|
|
17628
|
-
const element = authorElement(namespace === "svg" ? svgTagName(node.tag) :
|
|
17668
|
+
const element = authorElement(namespace === "svg" ? svgTagName(node.tag) : tag, props, children, namespace === "math");
|
|
17629
17669
|
return withAttributeErrors(errors, isRawRoot ? /* @__PURE__ */ jsx(RawTextContext.Provider, {
|
|
17630
17670
|
value: true,
|
|
17631
17671
|
children: element
|
|
@@ -21934,6 +21974,24 @@ function parseStoryData() {
|
|
|
21934
21974
|
userScript
|
|
21935
21975
|
};
|
|
21936
21976
|
}
|
|
21977
|
+
// src/utils/variable-writes.ts
|
|
21978
|
+
/** Explicit writes, including assignments that leave a binding unchanged. */
|
|
21979
|
+
var listeners$1 = /* @__PURE__ */ new Set();
|
|
21980
|
+
function notifyVariableWrite(path) {
|
|
21981
|
+
for (const listener of listeners$1) listener(path);
|
|
21982
|
+
}
|
|
21983
|
+
/** Observe writes to a binding, its parent, or one of its children. */
|
|
21984
|
+
function subscribeVariableWrites(binding, onWrite) {
|
|
21985
|
+
const listener = (path) => {
|
|
21986
|
+
const length = Math.min(path.length, binding.length);
|
|
21987
|
+
for (let i = 0; i < length; i++) if (path[i] !== binding[i]) return;
|
|
21988
|
+
onWrite();
|
|
21989
|
+
};
|
|
21990
|
+
listeners$1.add(listener);
|
|
21991
|
+
return () => {
|
|
21992
|
+
listeners$1.delete(listener);
|
|
21993
|
+
};
|
|
21994
|
+
}
|
|
21937
21995
|
// src/settings.ts
|
|
21938
21996
|
var definitions = /* @__PURE__ */ new Map();
|
|
21939
21997
|
var values = createNamespace();
|
|
@@ -22146,6 +22204,7 @@ function defineMacro(config, source = "builtin") {
|
|
|
22146
22204
|
ctx.value = getByPath(useStoryFields("variables").variables, segments);
|
|
22147
22205
|
ctx.getValue = () => getByPath(readState().variables, segments);
|
|
22148
22206
|
ctx.setValue = displayOnly ? refuse : (value) => {
|
|
22207
|
+
notifyVariableWrite(segments);
|
|
22149
22208
|
runAsControlEdit(segments, () => {
|
|
22150
22209
|
mutateState((work, adopt) => {
|
|
22151
22210
|
setByPath(work.variables, segments, adopt(value), { createMissing: true });
|
|
@@ -22540,7 +22599,7 @@ function observableText(el, skip, visibilityRoot) {
|
|
|
22540
22599
|
} finally {
|
|
22541
22600
|
instances.dispose();
|
|
22542
22601
|
}
|
|
22543
|
-
return out.split("\n").map((line) => line.trimEnd()).join("\n").replace(/\n{3,}/g, "\n\n").replace(/^\n
|
|
22602
|
+
return out.split("\n").map((line) => line.trimEnd()).join("\n").replace(/\n{3,}/g, "\n\n").replace(/^\n+/, "").trimEnd();
|
|
22544
22603
|
}
|
|
22545
22604
|
/**
|
|
22546
22605
|
* The trimmed text of each error shown in the element, leaving out those
|
|
@@ -22860,7 +22919,11 @@ function createStoryAPI() {
|
|
|
22860
22919
|
for (const [name] of entries) {
|
|
22861
22920
|
const { isTransient, key } = parseName(name);
|
|
22862
22921
|
warnIfUndeclared(isTransient, key);
|
|
22863
|
-
if (!isTransient)
|
|
22922
|
+
if (!isTransient) {
|
|
22923
|
+
const path = key.split(".");
|
|
22924
|
+
recordHookWrite(path);
|
|
22925
|
+
notifyVariableWrite(path);
|
|
22926
|
+
}
|
|
22864
22927
|
}
|
|
22865
22928
|
if (entries.some(([k]) => k.includes("."))) {
|
|
22866
22929
|
mutateState((work, adopt) => {
|
|
@@ -26845,19 +26908,142 @@ var VARIABLE_PARAMETER = {
|
|
|
26845
26908
|
type: "variable",
|
|
26846
26909
|
required: true
|
|
26847
26910
|
};
|
|
26911
|
+
var formResets = /* @__PURE__ */ new WeakMap();
|
|
26912
|
+
/** Read every reset value before any binding write can trigger a rerender. */
|
|
26913
|
+
function subscribeFormReset(form, read) {
|
|
26914
|
+
let subscription = formResets.get(form);
|
|
26915
|
+
if (!subscription) {
|
|
26916
|
+
const readers = /* @__PURE__ */ new Set();
|
|
26917
|
+
const reset = (event) => {
|
|
26918
|
+
const pending = Array.from(readers, (reader) => ({
|
|
26919
|
+
reader,
|
|
26920
|
+
...reader.prepare()
|
|
26921
|
+
}));
|
|
26922
|
+
setTimeout(() => {
|
|
26923
|
+
for (const { off } of pending) off();
|
|
26924
|
+
if (event.defaultPrevented) return;
|
|
26925
|
+
const snapshot = form.ownerDocument.createElement("form");
|
|
26926
|
+
const restoreDefaults = snapshot.reset.bind(snapshot);
|
|
26927
|
+
const controls = pending.map(({ reader, read }) => {
|
|
26928
|
+
const element = reader.element.cloneNode(true);
|
|
26929
|
+
element.removeAttribute("form");
|
|
26930
|
+
snapshot.appendChild(element);
|
|
26931
|
+
return {
|
|
26932
|
+
read,
|
|
26933
|
+
element
|
|
26934
|
+
};
|
|
26935
|
+
});
|
|
26936
|
+
restoreDefaults();
|
|
26937
|
+
const commits = controls.map(({ read, element }) => read(element));
|
|
26938
|
+
for (const commit of commits) commit();
|
|
26939
|
+
}, 0);
|
|
26940
|
+
};
|
|
26941
|
+
subscription = {
|
|
26942
|
+
readers,
|
|
26943
|
+
reset
|
|
26944
|
+
};
|
|
26945
|
+
formResets.set(form, subscription);
|
|
26946
|
+
form.addEventListener("reset", subscription.reset);
|
|
26947
|
+
}
|
|
26948
|
+
const { readers, reset } = subscription;
|
|
26949
|
+
readers.add(read);
|
|
26950
|
+
return () => {
|
|
26951
|
+
readers.delete(read);
|
|
26952
|
+
if (readers.size === 0) {
|
|
26953
|
+
form.removeEventListener("reset", reset);
|
|
26954
|
+
formResets.delete(form);
|
|
26955
|
+
}
|
|
26956
|
+
};
|
|
26957
|
+
}
|
|
26958
|
+
/** Native defaults belong to the binding's initial rendered control value. */
|
|
26959
|
+
function setResetDefault(control) {
|
|
26960
|
+
if (control.tagName === "SELECT") {
|
|
26961
|
+
const options = Array.from(control.options);
|
|
26962
|
+
const selected = options.map((option) => option.selected);
|
|
26963
|
+
options.forEach((option, i) => option.defaultSelected = selected[i]);
|
|
26964
|
+
} else if (control.tagName === "INPUT" && ["checkbox", "radio"].includes(control.type)) control.defaultChecked = control.checked;
|
|
26965
|
+
else control.defaultValue = control.value;
|
|
26966
|
+
}
|
|
26848
26967
|
/**
|
|
26849
26968
|
* Register the automation action of a macro bound to a story variable: keyed
|
|
26850
26969
|
* by the variable (or `key`), with the variable's value unless `action`
|
|
26851
26970
|
* gives another.
|
|
26852
26971
|
*/
|
|
26853
|
-
function useVariableAction(ctx, action) {
|
|
26854
|
-
|
|
26972
|
+
function useVariableAction(ctx, action, onReset) {
|
|
26973
|
+
const registered = useControlAction({
|
|
26855
26974
|
key: `$${ctx.varName}`,
|
|
26856
26975
|
authorId: ctx.id,
|
|
26857
26976
|
variable: ctx.varName,
|
|
26858
26977
|
value: ctx.value,
|
|
26859
26978
|
...action
|
|
26860
26979
|
});
|
|
26980
|
+
const control = useRef(null);
|
|
26981
|
+
const [, refresh] = useState(0);
|
|
26982
|
+
const initialized = useRef(null);
|
|
26983
|
+
const subscription = useRef(null);
|
|
26984
|
+
const resetValue = useRef({
|
|
26985
|
+
read: onReset,
|
|
26986
|
+
set: ctx.setValue,
|
|
26987
|
+
get: ctx.getValue,
|
|
26988
|
+
name: ctx.varName
|
|
26989
|
+
});
|
|
26990
|
+
resetValue.current = {
|
|
26991
|
+
read: onReset,
|
|
26992
|
+
set: ctx.setValue,
|
|
26993
|
+
get: ctx.getValue,
|
|
26994
|
+
name: ctx.varName
|
|
26995
|
+
};
|
|
26996
|
+
const ref = useCallback((element) => {
|
|
26997
|
+
control.current = element;
|
|
26998
|
+
registered.ref(element);
|
|
26999
|
+
}, [registered.ref]);
|
|
27000
|
+
useLayoutEffect(() => {
|
|
27001
|
+
const element = control.current;
|
|
27002
|
+
const form = element?.form;
|
|
27003
|
+
if (subscription.current?.element === element && subscription.current?.form === form) return;
|
|
27004
|
+
subscription.current?.off();
|
|
27005
|
+
subscription.current = null;
|
|
27006
|
+
if (!element || !onReset) return;
|
|
27007
|
+
if (initialized.current !== element) {
|
|
27008
|
+
setResetDefault(element);
|
|
27009
|
+
initialized.current = element;
|
|
27010
|
+
}
|
|
27011
|
+
if (!form) return;
|
|
27012
|
+
subscription.current = {
|
|
27013
|
+
element,
|
|
27014
|
+
form,
|
|
27015
|
+
off: subscribeFormReset(form, {
|
|
27016
|
+
element,
|
|
27017
|
+
prepare: () => {
|
|
27018
|
+
const before = resetValue.current.get();
|
|
27019
|
+
let changed = false;
|
|
27020
|
+
const offWrite = subscribeVariableWrites(resetValue.current.name.split("."), () => changed = true);
|
|
27021
|
+
const offStore = useStoryStore.subscribe(() => {
|
|
27022
|
+
changed ||= !Object.is(resetValue.current.get(), before);
|
|
27023
|
+
});
|
|
27024
|
+
return {
|
|
27025
|
+
off: () => {
|
|
27026
|
+
offWrite();
|
|
27027
|
+
offStore();
|
|
27028
|
+
},
|
|
27029
|
+
read: (snapshot) => {
|
|
27030
|
+
const value = changed ? void 0 : resetValue.current.read(snapshot);
|
|
27031
|
+
return () => {
|
|
27032
|
+
if (control.current !== element || element.form !== form) return;
|
|
27033
|
+
if (changed) refresh((version) => version + 1);
|
|
27034
|
+
else resetValue.current.set(value);
|
|
27035
|
+
};
|
|
27036
|
+
}
|
|
27037
|
+
};
|
|
27038
|
+
}
|
|
27039
|
+
})
|
|
27040
|
+
};
|
|
27041
|
+
});
|
|
27042
|
+
useLayoutEffect(() => () => subscription.current?.off(), []);
|
|
27043
|
+
return {
|
|
27044
|
+
...registered,
|
|
27045
|
+
ref
|
|
27046
|
+
};
|
|
26861
27047
|
}
|
|
26862
27048
|
/**
|
|
26863
27049
|
* Define a text input macro, `{name $var "placeholder"}`: an `<input>` of
|
|
@@ -26877,14 +27063,17 @@ function defineInputMacro(name, type, toValue = (input) => input !== void 0 ? St
|
|
|
26877
27063
|
holds: "text"
|
|
26878
27064
|
}],
|
|
26879
27065
|
render(_props, ctx) {
|
|
26880
|
-
const placeholder = ctx.args
|
|
27066
|
+
const { placeholder = "" } = ctx.args;
|
|
27067
|
+
const typed = ctx.hooks.useRef(null);
|
|
27068
|
+
const shown = typed.current !== null && toValue(typed.current) === ctx.value && type === "number" ? typed.current : display(ctx.value);
|
|
26881
27069
|
const action = useVariableAction(ctx, {
|
|
26882
27070
|
type: name,
|
|
26883
27071
|
label: placeholder || ctx.varName,
|
|
26884
27072
|
perform: (v) => ctx.setValue(toValue(v))
|
|
27073
|
+
}, (control) => {
|
|
27074
|
+
typed.current = control.value;
|
|
27075
|
+
return toValue(control.value);
|
|
26885
27076
|
});
|
|
26886
|
-
const typed = ctx.hooks.useRef(null);
|
|
26887
|
-
const shown = typed.current !== null && toValue(typed.current) === ctx.value && type === "number" ? typed.current : display(ctx.value);
|
|
26888
27077
|
return h(type ? "input" : "textarea", {
|
|
26889
27078
|
type,
|
|
26890
27079
|
...actionAttributes(action, ctx),
|
|
@@ -26922,7 +27111,7 @@ defineMacro({
|
|
|
26922
27111
|
label: label || ctx.varName || "",
|
|
26923
27112
|
value: !!ctx.value,
|
|
26924
27113
|
perform: (v) => ctx.setValue(v !== void 0 ? !!v : !ctx.getValue())
|
|
26925
|
-
});
|
|
27114
|
+
}, (control) => control.checked);
|
|
26926
27115
|
return /* @__PURE__ */ jsxs("label", {
|
|
26927
27116
|
id: ctx.id,
|
|
26928
27117
|
class: ctx.cls,
|
|
@@ -26958,11 +27147,17 @@ defineMacro({
|
|
|
26958
27147
|
const radioValue = ctx.args.value ?? "";
|
|
26959
27148
|
const label = ctx.args.label ?? "";
|
|
26960
27149
|
const view = ctx.hooks.useContext(ViewScopeContext);
|
|
27150
|
+
const initialValue = ctx.hooks.useRef(ctx.value);
|
|
26961
27151
|
const action = useVariableAction(ctx, {
|
|
26962
27152
|
type: "radiobutton",
|
|
26963
27153
|
key: `$${ctx.varName}:${radioValue}`,
|
|
26964
27154
|
label: label || radioValue,
|
|
26965
27155
|
perform: () => ctx.setValue(radioValue)
|
|
27156
|
+
}, (control) => {
|
|
27157
|
+
const radio = control;
|
|
27158
|
+
const group = radio.form?.elements.namedItem(radio.name);
|
|
27159
|
+
const selected = group && ("length" in group ? Array.from(group).find((node) => node.checked) : group.checked ? group : void 0);
|
|
27160
|
+
return selected ? selected.value : initialValue.current;
|
|
26966
27161
|
});
|
|
26967
27162
|
return /* @__PURE__ */ jsxs("label", {
|
|
26968
27163
|
id: ctx.id,
|
|
@@ -26994,7 +27189,7 @@ defineMacro({
|
|
|
26994
27189
|
perform: (v) => {
|
|
26995
27190
|
if (v !== void 0) ctx.setValue(String(v));
|
|
26996
27191
|
}
|
|
26997
|
-
});
|
|
27192
|
+
}, (control) => control.value);
|
|
26998
27193
|
return /* @__PURE__ */ jsx("select", {
|
|
26999
27194
|
...actionAttributes(action, ctx),
|
|
27000
27195
|
value: display(ctx.value),
|