@rohal12/spindle 0.64.31 → 0.64.33
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 +477 -253
- package/dist/pkg/story-variables.js +357 -255
- package/package.json +1 -1
- package/src/code-check.ts +67 -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 +38 -17
- package/src/hooks/use-modal-focus.ts +23 -10
- package/src/js-bindings.ts +41 -23
- package/src/markup/ast.ts +6 -1
- package/src/markup/code-end.ts +23 -5
- package/src/markup/pair.ts +10 -2
- package/src/markup/parse.ts +40 -1
- package/src/markup/render.tsx +1 -1
- package/src/markup/spindle.d.peggy.ts +2 -0
- package/src/markup/spindle.peggy +5 -6
- package/src/markup/validate.ts +3 -0
- package/src/observe/observable-text.ts +2 -1
- package/src/story-api.ts +6 -1
- package/src/tooling.ts +1 -0
- package/src/utils/variable-writes.ts +24 -0
- package/types/tooling.d.ts +11 -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
|
}
|
|
@@ -10353,7 +10353,7 @@ function peg$parse(input, options) {
|
|
|
10353
10353
|
return lexeme("text", location(), { value });
|
|
10354
10354
|
}
|
|
10355
10355
|
function peg$f24() {
|
|
10356
|
-
return completionMode || offset()
|
|
10356
|
+
return completionMode || findDelimiter("-->", offset()) >= 0;
|
|
10357
10357
|
}
|
|
10358
10358
|
function peg$f25(value) {
|
|
10359
10359
|
const node = lexeme("text", location(), { value });
|
|
@@ -10410,8 +10410,8 @@ function peg$parse(input, options) {
|
|
|
10410
10410
|
fail("Unclosed link: [[ without ]]", location(), "unclosed-link");
|
|
10411
10411
|
}
|
|
10412
10412
|
function peg$f37(start) {
|
|
10413
|
-
const close =
|
|
10414
|
-
const nested =
|
|
10413
|
+
const close = findDelimiter("]]", start);
|
|
10414
|
+
const nested = findDelimiter("[[", start);
|
|
10415
10415
|
linkTextEnd = Math.min(close < 0 ? input.length : close, nested < 0 ? input.length : nested);
|
|
10416
10416
|
return true;
|
|
10417
10417
|
}
|
|
@@ -13271,7 +13271,7 @@ function peg$parse(input, options) {
|
|
|
13271
13271
|
const textMode = options.text === true;
|
|
13272
13272
|
const completionMode = options.completion === true;
|
|
13273
13273
|
const hooks = options.hooks;
|
|
13274
|
-
const
|
|
13274
|
+
const findDelimiter = options.findDelimiter;
|
|
13275
13275
|
/** End of the stretch a `.|n|` repetition consumes (see skipTo). */
|
|
13276
13276
|
let skipEnd = -1;
|
|
13277
13277
|
/** Whether the attribute whose value is being read holds code. */
|
|
@@ -13605,6 +13605,10 @@ function isBlockMacro(name) {
|
|
|
13605
13605
|
if (DEFINED_MACROS.has(name)) return DEFINED_BLOCK_MACROS.has(name);
|
|
13606
13606
|
return isBuiltinBlockMacro(name);
|
|
13607
13607
|
}
|
|
13608
|
+
/** Explicit implementations can also be invoked outside a companion owner. */
|
|
13609
|
+
function isStandaloneMacro(name) {
|
|
13610
|
+
return WIDGETS.has(name) || getMacro(name) !== void 0;
|
|
13611
|
+
}
|
|
13608
13612
|
/** Built-in defaults without any process-global registrations or widgets. */
|
|
13609
13613
|
function isBuiltinBlockMacro(name) {
|
|
13610
13614
|
return BLOCK_MACROS.has(name);
|
|
@@ -13769,6 +13773,7 @@ var closes = (open, close) => open.type === close.type && lowerName(open) === lo
|
|
|
13769
13773
|
function pairMarkup(tokens, options = {}) {
|
|
13770
13774
|
const isBlock = options.isBlock ?? isBlockMacro;
|
|
13771
13775
|
const isRaw = options.isRaw ?? isRawMacro;
|
|
13776
|
+
const isStandalone = options.isStandalone ?? isStandaloneMacro;
|
|
13772
13777
|
const parentsOf = (name) => {
|
|
13773
13778
|
if (options.branchParents) return options.branchParents(name);
|
|
13774
13779
|
const registered = branchParents(name);
|
|
@@ -13848,7 +13853,7 @@ function pairMarkup(tokens, options = {}) {
|
|
|
13848
13853
|
}
|
|
13849
13854
|
continue;
|
|
13850
13855
|
}
|
|
13851
|
-
if (token.type === "macro" && !token.isClose && parentsOf(token.name).length > 0) {
|
|
13856
|
+
if (token.type === "macro" && !token.isClose && parentsOf(token.name).length > 0 && (!isStandalone(token.name) || last(stack)?.node.token.type === "macro" && parentsOf(token.name).includes(lowerName(last(stack).node.token)))) {
|
|
13852
13857
|
const tag = token;
|
|
13853
13858
|
const frame = last(stack);
|
|
13854
13859
|
if (!frame) {
|
|
@@ -13916,6 +13921,204 @@ function lastEnd(body) {
|
|
|
13916
13921
|
const branch = last(body.branches);
|
|
13917
13922
|
return last(branch?.children ?? body.children)?.end ?? branch?.tag.end;
|
|
13918
13923
|
}
|
|
13924
|
+
// src/components/macros/arg-utils.ts
|
|
13925
|
+
/**
|
|
13926
|
+
* Shared helpers for macros that split or read their raw argument text by
|
|
13927
|
+
* hand. Their lexical model is the expression lexer's (`js-lexer.ts`):
|
|
13928
|
+
* string, template and regex literals and comments are opaque (a backslash
|
|
13929
|
+
* inside a literal escapes the character after it, and `${…}` parts of a
|
|
13930
|
+
* template are code), `/` opens a regex only where an operand is expected,
|
|
13931
|
+
* and `(`/`[`/`{` nest.
|
|
13932
|
+
*/
|
|
13933
|
+
/** True for a whitespace character. */
|
|
13934
|
+
function isWhitespace(ch) {
|
|
13935
|
+
return /\s/.test(ch);
|
|
13936
|
+
}
|
|
13937
|
+
function isOpener(ch) {
|
|
13938
|
+
return ch === "(" || ch === "[" || ch === "{";
|
|
13939
|
+
}
|
|
13940
|
+
function isCloser(ch) {
|
|
13941
|
+
return ch === ")" || ch === "]" || ch === "}";
|
|
13942
|
+
}
|
|
13943
|
+
/**
|
|
13944
|
+
* Given the index of an opening quote (`"`, `'` or a backtick), return the
|
|
13945
|
+
* index just past its closing quote, or `src.length` if it is unterminated.
|
|
13946
|
+
* A backslash escapes the character after it, so a quote preceded by an even
|
|
13947
|
+
* run of backslashes still closes the string and one preceded by an odd run
|
|
13948
|
+
* does not. Inside a template literal, `${…}` interpolations are lexed as
|
|
13949
|
+
* code, so quotes, backticks, braces and regexes inside them do not end it.
|
|
13950
|
+
*/
|
|
13951
|
+
function skipString(src, start) {
|
|
13952
|
+
return src[start] === "`" ? lexTemplate(src, start) : scanStringLiteral(src, start).end;
|
|
13953
|
+
}
|
|
13954
|
+
/**
|
|
13955
|
+
* Undo the escapes allowed in a literal quoted macro argument: `\"`, `\'`
|
|
13956
|
+
* and `\\` become `"`, `'` and `\`. Other backslash sequences (`\n`, `\d`)
|
|
13957
|
+
* are kept as written, so code and regex text inside the string survive.
|
|
13958
|
+
*/
|
|
13959
|
+
function unescapeQuoted(body) {
|
|
13960
|
+
return body.replace(/\\(["'\\])/g, "$1");
|
|
13961
|
+
}
|
|
13962
|
+
/**
|
|
13963
|
+
* Read the `"…"` or `'…'` string whose opening quote is at `start`.
|
|
13964
|
+
* Returns its unescaped value and the index just past the closing quote, or
|
|
13965
|
+
* `null` if there is no such quote at `start` or the string is unterminated.
|
|
13966
|
+
* With `tolerant`, an unterminated string instead extends to its scanned end.
|
|
13967
|
+
*/
|
|
13968
|
+
function readQuoted(src, start, tolerant = false) {
|
|
13969
|
+
const quote = src[start];
|
|
13970
|
+
if (quote !== "\"" && quote !== "'") return null;
|
|
13971
|
+
const { end, closed } = scanStringLiteral(src, start);
|
|
13972
|
+
if (!closed && !tolerant) return null;
|
|
13973
|
+
return {
|
|
13974
|
+
value: unescapeQuoted(src.slice(start + 1, closed ? end - 1 : end)),
|
|
13975
|
+
end
|
|
13976
|
+
};
|
|
13977
|
+
}
|
|
13978
|
+
/**
|
|
13979
|
+
* The unescaped value of `src` when it is exactly one `"…"` or `'…'` string
|
|
13980
|
+
* (as with `readQuoted`), otherwise `null`.
|
|
13981
|
+
*/
|
|
13982
|
+
function readWholeQuoted(src) {
|
|
13983
|
+
const quoted = readQuoted(src, 0);
|
|
13984
|
+
return quoted && quoted.end === src.length ? quoted.value : null;
|
|
13985
|
+
}
|
|
13986
|
+
/** A dialog body's passage name, optionally enclosed in matching quotes. */
|
|
13987
|
+
function readPassageBody(src) {
|
|
13988
|
+
const name = src.trim();
|
|
13989
|
+
return readWholeQuoted(name) ?? name;
|
|
13990
|
+
}
|
|
13991
|
+
/**
|
|
13992
|
+
* Strip an optional quote from each end of an unquoted-or-loosely-quoted
|
|
13993
|
+
* argument (`"Red`, `Red'`), leaving at least one character. This is the
|
|
13994
|
+
* lenient fallback for labels that are not one well-formed quoted string.
|
|
13995
|
+
*/
|
|
13996
|
+
function stripLooseQuotes(src) {
|
|
13997
|
+
return /^["']?(.+?)["']?$/s.exec(src)?.[1] ?? src;
|
|
13998
|
+
}
|
|
13999
|
+
/** An operator character, or an operator keyword, at the end of code. */
|
|
14000
|
+
var TRAILING_OPERATOR_RE = /(?:[-+*/%&|^!~=<>?:,.]|(?:^|[^\w$@.])(?:typeof|void|new|delete|in|instanceof|await|yield))$/;
|
|
14001
|
+
/**
|
|
14002
|
+
* Whether `src` ends with an operator that still needs an operand (`$a +`,
|
|
14003
|
+
* `$a+`, `$x ==`, `typeof`), so it cannot be a complete value on its own.
|
|
14004
|
+
* Only code counts: the closing `/` of a regex literal is not division, a
|
|
14005
|
+
* variable reference (`$a`, `%name`, ...) is an operand, trailing comments are skipped,
|
|
14006
|
+
* and a postfix `++` or `--` completes its operand.
|
|
14007
|
+
*/
|
|
14008
|
+
function endsWithOperator(src) {
|
|
14009
|
+
let end = -1;
|
|
14010
|
+
lexJs(src, {
|
|
14011
|
+
code(ch, i, nesting) {
|
|
14012
|
+
if (nesting === 0 && !isWhitespace(ch)) end = i + 1;
|
|
14013
|
+
},
|
|
14014
|
+
literal(text, _i, nesting) {
|
|
14015
|
+
const comment = text.startsWith("//") || text.startsWith("/*");
|
|
14016
|
+
if (nesting === 0 && !comment) end = -1;
|
|
14017
|
+
},
|
|
14018
|
+
variable(_sigil, _name, _i, nesting) {
|
|
14019
|
+
if (nesting === 0) end = -1;
|
|
14020
|
+
}
|
|
14021
|
+
});
|
|
14022
|
+
if (end < 0) return false;
|
|
14023
|
+
const code = src.slice(0, end);
|
|
14024
|
+
if (/(?:\+\+|--)$/.test(code)) return false;
|
|
14025
|
+
return TRAILING_OPERATOR_RE.test(code);
|
|
14026
|
+
}
|
|
14027
|
+
/**
|
|
14028
|
+
* Indices of the characters in `src` that satisfy `isSeparator` and sit at
|
|
14029
|
+
* depth 0: in code outside string, template and regex literals and comments,
|
|
14030
|
+
* and outside `()`, `[]` and `{}` pairs.
|
|
14031
|
+
*/
|
|
14032
|
+
function topLevelIndices(src, isSeparator) {
|
|
14033
|
+
const indices = [];
|
|
14034
|
+
let depth = 0;
|
|
14035
|
+
lexJs(src, { code(ch, i, nesting) {
|
|
14036
|
+
if (nesting > 0) return;
|
|
14037
|
+
if (isOpener(ch)) depth++;
|
|
14038
|
+
else if (isCloser(ch)) depth--;
|
|
14039
|
+
else if (depth === 0 && isSeparator(ch)) indices.push(i);
|
|
14040
|
+
} });
|
|
14041
|
+
return indices;
|
|
14042
|
+
}
|
|
14043
|
+
/**
|
|
14044
|
+
* The segments of `src` between its depth-0 separators (see
|
|
14045
|
+
* `topLevelIndices`), as spans: untrimmed, and empty segments are kept, so
|
|
14046
|
+
* `n` separators always give `n + 1` segments.
|
|
14047
|
+
*/
|
|
14048
|
+
function topLevelSpans(src, isSeparator) {
|
|
14049
|
+
const spans = [];
|
|
14050
|
+
let from = 0;
|
|
14051
|
+
for (const i of topLevelIndices(src, isSeparator)) {
|
|
14052
|
+
spans.push([from, i]);
|
|
14053
|
+
from = i + 1;
|
|
14054
|
+
}
|
|
14055
|
+
spans.push([from, src.length]);
|
|
14056
|
+
return spans;
|
|
14057
|
+
}
|
|
14058
|
+
/**
|
|
14059
|
+
* Check whether a whitespace-delimited token looks like a standalone value
|
|
14060
|
+
* (not an operator or partial expression): it starts like a value and does
|
|
14061
|
+
* not end with an operator still waiting for its operand.
|
|
14062
|
+
*/
|
|
14063
|
+
function isStandaloneValue(token) {
|
|
14064
|
+
return startsLikeValue(token) && !endsWithOperator(token);
|
|
14065
|
+
}
|
|
14066
|
+
function startsLikeValue(token) {
|
|
14067
|
+
const first = token[0];
|
|
14068
|
+
if (first === "\"" || first === "'" || first === "`") return true;
|
|
14069
|
+
if (/^[$_@]\w|^%[A-Za-z_]/.test(token)) return true;
|
|
14070
|
+
if (/^[+-]?(?:\d|\.\d)/.test(token)) return true;
|
|
14071
|
+
if (first === "(" || first === "[" || first === "{") return true;
|
|
14072
|
+
if (/^(?:true|false|null|undefined)(?![\w$])/.test(token)) return true;
|
|
14073
|
+
if (first === "!" && token.length > 1 && token[1] !== "=") return true;
|
|
14074
|
+
return false;
|
|
14075
|
+
}
|
|
14076
|
+
/**
|
|
14077
|
+
* Try to split a raw string on whitespace at depth 0 (respecting strings,
|
|
14078
|
+
* template literals, parentheses, brackets, and braces). Each resulting token
|
|
14079
|
+
* must pass `isStandaloneValue()` or the split is rejected and `null` is
|
|
14080
|
+
* returned. The spans are those of the tokens in `raw`.
|
|
14081
|
+
*/
|
|
14082
|
+
function trySplitOnWhitespace(raw) {
|
|
14083
|
+
const args = topLevelSpans(raw, isWhitespace).filter(([a, b]) => b > a);
|
|
14084
|
+
if (args.length < 2) return null;
|
|
14085
|
+
for (const [a, b] of args) if (!isStandaloneValue(raw.slice(a, b))) return null;
|
|
14086
|
+
return args;
|
|
14087
|
+
}
|
|
14088
|
+
/** `span` less the whitespace at its ends. */
|
|
14089
|
+
function trimmed$1(src, [start, end]) {
|
|
14090
|
+
const text = src.slice(start, end);
|
|
14091
|
+
const lead = text.length - text.trimStart().length;
|
|
14092
|
+
return [start + lead, Math.max(start + lead, end - (text.length - text.trimEnd().length))];
|
|
14093
|
+
}
|
|
14094
|
+
/**
|
|
14095
|
+
* Where the arguments in `raw` are written (see `splitArgs`), as spans of
|
|
14096
|
+
* `raw`.
|
|
14097
|
+
*/
|
|
14098
|
+
function splitArgSpans(raw) {
|
|
14099
|
+
const args = topLevelSpans(raw, (ch) => ch === ",").map((span) => trimmed$1(raw, span));
|
|
14100
|
+
const hasComma = args.length > 1;
|
|
14101
|
+
const last = args[args.length - 1];
|
|
14102
|
+
if (last[0] === last[1]) args.pop();
|
|
14103
|
+
if (!hasComma && args.length === 1) {
|
|
14104
|
+
const [start, end] = args[0];
|
|
14105
|
+
const split = trySplitOnWhitespace(raw.slice(start, end));
|
|
14106
|
+
if (split) return split.map(([a, b]) => [start + a, start + b]);
|
|
14107
|
+
}
|
|
14108
|
+
return args;
|
|
14109
|
+
}
|
|
14110
|
+
/**
|
|
14111
|
+
* Split rawArgs by commas, respecting parentheses, brackets, braces, and
|
|
14112
|
+
* strings. When no top-level commas are present, also supports adjacent quoted
|
|
14113
|
+
* string literals separated by whitespace (e.g. `"Label" "target"`).
|
|
14114
|
+
*/
|
|
14115
|
+
function splitArgs(raw) {
|
|
14116
|
+
return splitArgSpans(raw).map(([a, b]) => raw.slice(a, b));
|
|
14117
|
+
}
|
|
14118
|
+
/** `text` as a regular expression source that matches it literally. */
|
|
14119
|
+
function escapeRegExp(text) {
|
|
14120
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
14121
|
+
}
|
|
13919
14122
|
// src/markup/code-end.ts
|
|
13920
14123
|
/**
|
|
13921
14124
|
* A quote directly after a letter/digit is an apostrophe (don't), not a
|
|
@@ -14020,6 +14223,8 @@ function scanLenient(input, start) {
|
|
|
14020
14223
|
* input (see `OnOpenCodeEnd`); the ends it finds are the same.
|
|
14021
14224
|
*/
|
|
14022
14225
|
function jsCodeEnd(onOpenEnd) {
|
|
14226
|
+
let indexedInput = "";
|
|
14227
|
+
const rawClosers = /* @__PURE__ */ new Map();
|
|
14023
14228
|
return {
|
|
14024
14229
|
closeBrace(input, codeStart, lenientStart) {
|
|
14025
14230
|
let ranOut = false;
|
|
@@ -14030,10 +14235,18 @@ function jsCodeEnd(onOpenEnd) {
|
|
|
14030
14235
|
return end;
|
|
14031
14236
|
},
|
|
14032
14237
|
rawBodyEnd(input, bodyStart, name) {
|
|
14033
|
-
|
|
14034
|
-
|
|
14035
|
-
|
|
14036
|
-
|
|
14238
|
+
if (indexedInput !== input) {
|
|
14239
|
+
indexedInput = input;
|
|
14240
|
+
rawClosers.clear();
|
|
14241
|
+
}
|
|
14242
|
+
name = name.toLowerCase();
|
|
14243
|
+
const closer = `\\{/${escapeRegExp(name)}\\s*\\}`;
|
|
14244
|
+
let candidates = rawClosers.get(name);
|
|
14245
|
+
if (!candidates) {
|
|
14246
|
+
candidates = Array.from(input.matchAll(new RegExp(closer, "gi")), (m) => m.index);
|
|
14247
|
+
rawClosers.set(name, candidates);
|
|
14248
|
+
}
|
|
14249
|
+
const firstAt = candidates[upperBound(candidates, bodyStart - 1)] ?? -1;
|
|
14037
14250
|
if (firstAt === -1) return -1;
|
|
14038
14251
|
const at = new RegExp(closer, "iy");
|
|
14039
14252
|
let ranOut = false;
|
|
@@ -14046,7 +14259,7 @@ function jsCodeEnd(onOpenEnd) {
|
|
|
14046
14259
|
onRunOut: () => ranOut = true
|
|
14047
14260
|
});
|
|
14048
14261
|
if (end !== -1) return end;
|
|
14049
|
-
if (onOpenEnd && (ranOut || new RegExp(`\\{/${name}\\s*$`, "i").test(input))) onOpenEnd(input, bodyStart);
|
|
14262
|
+
if (onOpenEnd && (ranOut || new RegExp(`\\{/${escapeRegExp(name)}\\s*$`, "i").test(input))) onOpenEnd(input, bodyStart);
|
|
14050
14263
|
return firstAt;
|
|
14051
14264
|
}
|
|
14052
14265
|
};
|
|
@@ -14170,7 +14383,7 @@ var MarkupError = class extends Error {
|
|
|
14170
14383
|
this.name = "MarkupError";
|
|
14171
14384
|
}
|
|
14172
14385
|
};
|
|
14173
|
-
function run(source, startRule, options, completion = false) {
|
|
14386
|
+
function run(source, startRule, options, completion = false, findDelimiter = indexDelimiters(source)) {
|
|
14174
14387
|
const hooks = {
|
|
14175
14388
|
...defaultHooks,
|
|
14176
14389
|
...options.hooks
|
|
@@ -14184,7 +14397,8 @@ function run(source, startRule, options, completion = false) {
|
|
|
14184
14397
|
startRule,
|
|
14185
14398
|
text: options.text === true,
|
|
14186
14399
|
hooks,
|
|
14187
|
-
completion
|
|
14400
|
+
completion,
|
|
14401
|
+
findDelimiter
|
|
14188
14402
|
});
|
|
14189
14403
|
} catch (err) {
|
|
14190
14404
|
const { location, code } = err;
|
|
@@ -14196,6 +14410,20 @@ function run(source, startRule, options, completion = false) {
|
|
|
14196
14410
|
throw err;
|
|
14197
14411
|
}
|
|
14198
14412
|
}
|
|
14413
|
+
/** Literal delimiter searches shared by suffix and prefix recovery attempts. */
|
|
14414
|
+
function indexDelimiters(source) {
|
|
14415
|
+
const positions = /* @__PURE__ */ new Map();
|
|
14416
|
+
return (delimiter, from, end = source.length) => {
|
|
14417
|
+
let offsets = positions.get(delimiter);
|
|
14418
|
+
if (!offsets) {
|
|
14419
|
+
offsets = [];
|
|
14420
|
+
for (let at = source.indexOf(delimiter); at >= 0; at = source.indexOf(delimiter, at + 1)) offsets.push(at);
|
|
14421
|
+
positions.set(delimiter, offsets);
|
|
14422
|
+
}
|
|
14423
|
+
const at = offsets[upperBound(offsets, from - 1)];
|
|
14424
|
+
return at !== void 0 && at + delimiter.length <= end ? at : -1;
|
|
14425
|
+
};
|
|
14426
|
+
}
|
|
14199
14427
|
/**
|
|
14200
14428
|
* Read markup that may be malformed: its tokens, their pairing and every
|
|
14201
14429
|
* error (malformed tags, and closers and branches that pair with nothing),
|
|
@@ -14212,6 +14440,7 @@ function analyzeMarkup(source, options = {}) {
|
|
|
14212
14440
|
isBlock: hooks.isBlock,
|
|
14213
14441
|
isRaw: hooks.isRaw,
|
|
14214
14442
|
branchParents: hooks.branchParents,
|
|
14443
|
+
isStandalone: hooks.isStandalone,
|
|
14215
14444
|
source
|
|
14216
14445
|
});
|
|
14217
14446
|
if (errors.length === 0 && paired.errors.length === 0) return {
|
|
@@ -14284,6 +14513,7 @@ function scanMarkupTolerant(source, options = {}, completion = false) {
|
|
|
14284
14513
|
const damage = [];
|
|
14285
14514
|
let position;
|
|
14286
14515
|
const codeEnd = indexCodeEnds(source);
|
|
14516
|
+
const findDelimiter = indexDelimiters(source);
|
|
14287
14517
|
const customCloseBrace = options.hooks?.closeBrace;
|
|
14288
14518
|
const lastBrace = source.lastIndexOf("}");
|
|
14289
14519
|
/** `text` shifted by `base`, tokenized: the tokens or the error. */
|
|
@@ -14301,7 +14531,10 @@ function scanMarkupTolerant(source, options = {}, completion = false) {
|
|
|
14301
14531
|
return run(text, "Tokens", {
|
|
14302
14532
|
...options,
|
|
14303
14533
|
hooks
|
|
14304
|
-
}, completion
|
|
14534
|
+
}, completion, (delimiter, from) => {
|
|
14535
|
+
const at = findDelimiter(delimiter, base + from, base + text.length);
|
|
14536
|
+
return at < 0 ? -1 : at - base;
|
|
14537
|
+
}).map((token) => shiftOffsets(token, base));
|
|
14305
14538
|
} catch (error) {
|
|
14306
14539
|
if (!(error instanceof MarkupError)) throw error;
|
|
14307
14540
|
return error;
|
|
@@ -14442,10 +14675,10 @@ function createListeners() {
|
|
|
14442
14675
|
// src/runtime-errors.ts
|
|
14443
14676
|
var errors = [];
|
|
14444
14677
|
var nextId = 1;
|
|
14445
|
-
var listeners$
|
|
14678
|
+
var listeners$2 = createListeners();
|
|
14446
14679
|
function update(next) {
|
|
14447
14680
|
errors = next;
|
|
14448
|
-
listeners$
|
|
14681
|
+
listeners$2.notify();
|
|
14449
14682
|
}
|
|
14450
14683
|
/**
|
|
14451
14684
|
* Show `error` on the page, after `context` (what failed). The same error
|
|
@@ -14485,205 +14718,7 @@ function getRuntimeErrors() {
|
|
|
14485
14718
|
return errors;
|
|
14486
14719
|
}
|
|
14487
14720
|
/** 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
|
-
}
|
|
14721
|
+
var subscribeRuntimeErrors = listeners$2.subscribe;
|
|
14687
14722
|
// src/components/macros/macro-args.ts
|
|
14688
14723
|
/**
|
|
14689
14724
|
* The macro argument layer: reads a macro's raw argument text into the
|
|
@@ -14863,14 +14898,28 @@ function trimmed(src, at) {
|
|
|
14863
14898
|
function readGroup(params, src, args, { at: srcAt, spans, toolingSpans }) {
|
|
14864
14899
|
let [text, at] = trimmed(src ?? "", srcAt);
|
|
14865
14900
|
const alone = !params.some((p) => p.type !== "flag" && p.required);
|
|
14866
|
-
|
|
14867
|
-
|
|
14868
|
-
|
|
14869
|
-
|
|
14870
|
-
|
|
14871
|
-
|
|
14872
|
-
|
|
14901
|
+
const flags = params.filter((p) => p.type === "flag");
|
|
14902
|
+
for (const flag of flags) args[flag.name] = false;
|
|
14903
|
+
const byName = new Map(flags.map((flag) => [flag.name, flag]));
|
|
14904
|
+
while (text && byName.size) {
|
|
14905
|
+
const parts = terms(text);
|
|
14906
|
+
if (parts.length === 0) break;
|
|
14907
|
+
let changed = false;
|
|
14908
|
+
for (const name of [slice(text, parts[parts.length - 1]), slice(text, parts[0])]) {
|
|
14909
|
+
const flag = byName.get(name);
|
|
14910
|
+
if (!flag || args[flag.name]) continue;
|
|
14911
|
+
const rest = src === null ? null : takeFlag(text, flag.name, alone);
|
|
14912
|
+
if (rest) {
|
|
14913
|
+
args[flag.name] = true;
|
|
14914
|
+
byName.delete(flag.name);
|
|
14915
|
+
changed = true;
|
|
14916
|
+
const flagSpan = rest[0] > 0 ? [0, text.indexOf(flag.name) + flag.name.length] : [text.lastIndexOf(flag.name), text.length];
|
|
14917
|
+
toolingSpans?.set(flag, [at + flagSpan[0], at + flagSpan[1]]);
|
|
14918
|
+
[text, at] = trimmed(slice(text, rest), at + rest[0]);
|
|
14919
|
+
break;
|
|
14920
|
+
}
|
|
14873
14921
|
}
|
|
14922
|
+
if (!changed) break;
|
|
14874
14923
|
}
|
|
14875
14924
|
const positional = params.filter((p) => p.type !== "flag");
|
|
14876
14925
|
if (positional.length === 0) return;
|
|
@@ -15131,13 +15180,13 @@ function parseOrError(code, goal) {
|
|
|
15131
15180
|
parses?.set(key, result);
|
|
15132
15181
|
return result;
|
|
15133
15182
|
}
|
|
15134
|
-
/** Where
|
|
15183
|
+
/** Where boundary `index` of a piece's code, text or name is in the source. */
|
|
15135
15184
|
function pieceOffset(piece, index) {
|
|
15136
15185
|
return piece.sourceOffsets?.[index] ?? piece.offset + index;
|
|
15137
15186
|
}
|
|
15138
15187
|
/** How much of a piece's own text or code there is (not its source extent). */
|
|
15139
15188
|
function extentOf(piece) {
|
|
15140
|
-
return piece.kind === "code" ? piece.code.length : piece.kind === "text" ? piece.text.length : piece.length;
|
|
15189
|
+
return piece.kind === "code" ? piece.code.length : piece.kind === "text" ? piece.text.length : piece.kind === "passage" ? piece.name.length : piece.length;
|
|
15141
15190
|
}
|
|
15142
15191
|
/** Macros whose whole argument text is one expression: branch conditions. */
|
|
15143
15192
|
var CONDITION_MACROS = /* @__PURE__ */ new Set([
|
|
@@ -15239,6 +15288,7 @@ function parameterLookup(macros) {
|
|
|
15239
15288
|
lookup.source = (name) => sources.get(name);
|
|
15240
15289
|
const definitions = new NameMap(effectiveNamedDefinitions([...list.filter((macro) => !macro.widget), ...list.filter((macro) => macro.widget)]).map((macro) => [macro.name, macro]));
|
|
15241
15290
|
lookup.pairing = {
|
|
15291
|
+
isStandalone: (name) => widgets.has(name) || sources.get(name) === "user",
|
|
15242
15292
|
isBlock: (name) => definitions.get(name)?.block ?? isBuiltinBlockMacro(name),
|
|
15243
15293
|
isRaw: (name) => isRawMacro(name, widgets.has(name)),
|
|
15244
15294
|
branchParents: branchParentLookup([...definitions.values()].map((macro) => ({
|
|
@@ -15334,13 +15384,17 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
15334
15384
|
if (close > t + 1) {
|
|
15335
15385
|
const text = src.slice(token.end, tokens[close].start);
|
|
15336
15386
|
const written = text.trim();
|
|
15387
|
+
const at = token.end + text.indexOf(written);
|
|
15388
|
+
const nameValue = readPassageBody(written);
|
|
15389
|
+
const sourceOffsets = readWholeQuoted(written) !== null ? escapeOffsets(written.slice(1, -1), at + 1, nameValue, "quotes", true) : void 0;
|
|
15337
15390
|
yield {
|
|
15338
15391
|
kind: "passage",
|
|
15339
|
-
name:
|
|
15340
|
-
offset:
|
|
15392
|
+
name: nameValue,
|
|
15393
|
+
offset: at,
|
|
15341
15394
|
length: written.length,
|
|
15342
15395
|
macro: name,
|
|
15343
|
-
label: src.slice(token.start, token.end)
|
|
15396
|
+
label: src.slice(token.start, token.end),
|
|
15397
|
+
...sourceOffsets && { sourceOffsets }
|
|
15344
15398
|
};
|
|
15345
15399
|
}
|
|
15346
15400
|
}
|
|
@@ -15430,7 +15484,7 @@ function nestedPiece(piece, toRoot, where) {
|
|
|
15430
15484
|
nested: true,
|
|
15431
15485
|
where
|
|
15432
15486
|
};
|
|
15433
|
-
if (piece.kind === "
|
|
15487
|
+
if (piece.kind === "argument-error") {
|
|
15434
15488
|
const length = toRoot(piece.offset + piece.length) - offset;
|
|
15435
15489
|
return {
|
|
15436
15490
|
...piece,
|
|
@@ -15440,7 +15494,7 @@ function nestedPiece(piece, toRoot, where) {
|
|
|
15440
15494
|
}
|
|
15441
15495
|
const extent = extentOf(piece);
|
|
15442
15496
|
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
|
-
|
|
15497
|
+
const mapped = sourceOffsets ? {
|
|
15444
15498
|
...piece,
|
|
15445
15499
|
...nested,
|
|
15446
15500
|
sourceOffsets
|
|
@@ -15448,6 +15502,10 @@ function nestedPiece(piece, toRoot, where) {
|
|
|
15448
15502
|
...piece,
|
|
15449
15503
|
...nested
|
|
15450
15504
|
};
|
|
15505
|
+
return mapped.kind === "passage" ? {
|
|
15506
|
+
...mapped,
|
|
15507
|
+
length: toRoot(piece.offset + mapped.length) - offset
|
|
15508
|
+
} : mapped;
|
|
15451
15509
|
}
|
|
15452
15510
|
/** An escape sequence of a JavaScript string literal (with its backslash). */
|
|
15453
15511
|
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 +15534,10 @@ function escapeAt(raw, i, escapes) {
|
|
|
15476
15534
|
* written as `raw` from `at` on (its escapes are those of `escapes`), is in
|
|
15477
15535
|
* the source, and where the text ends. A character that an escape stands for
|
|
15478
15536
|
* is where the escape begins. None when the text is where its length says, or
|
|
15479
|
-
* is not that string.
|
|
15537
|
+
* is not that string. With `includeLinear`, return the map even without
|
|
15538
|
+
* escapes (a passage piece's offset includes the opening quote).
|
|
15480
15539
|
*/
|
|
15481
|
-
function escapeOffsets(raw, at, value, escapes) {
|
|
15540
|
+
function escapeOffsets(raw, at, value, escapes, includeLinear = false) {
|
|
15482
15541
|
const offsets = [];
|
|
15483
15542
|
let escaped = false;
|
|
15484
15543
|
let i = 0;
|
|
@@ -15494,8 +15553,14 @@ function escapeOffsets(raw, at, value, escapes) {
|
|
|
15494
15553
|
escaped = true;
|
|
15495
15554
|
i += escape.length;
|
|
15496
15555
|
}
|
|
15556
|
+
while (raw[i] === "\\") {
|
|
15557
|
+
const escape = escapeAt(raw, i, escapes);
|
|
15558
|
+
if (!escape || escape.text.length !== 0) break;
|
|
15559
|
+
escaped = true;
|
|
15560
|
+
i += escape.length;
|
|
15561
|
+
}
|
|
15497
15562
|
offsets.push(at + i);
|
|
15498
|
-
return escaped ? offsets : void 0;
|
|
15563
|
+
return escaped || includeLinear ? offsets : void 0;
|
|
15499
15564
|
}
|
|
15500
15565
|
/** The code and text in the arguments `args` (at `offset`) of `macro`. */
|
|
15501
15566
|
function* argPieces(args, offset, params, macro, label, navigation = []) {
|
|
@@ -15563,20 +15628,21 @@ function* valuePieces(args, offset, params, values, spans, macro, label, escapes
|
|
|
15563
15628
|
const [start, end] = span;
|
|
15564
15629
|
const written = args.slice(start, end);
|
|
15565
15630
|
/** The name as written (quotes included), at the argument. */
|
|
15566
|
-
const passage = (passageName) => ({
|
|
15631
|
+
const passage = (passageName, at, mode = escapes) => ({
|
|
15567
15632
|
kind: "passage",
|
|
15568
15633
|
name: passageName,
|
|
15569
15634
|
offset: offset + start,
|
|
15570
15635
|
length: end - start,
|
|
15571
15636
|
macro: name,
|
|
15572
15637
|
label,
|
|
15638
|
+
sourceOffsets: escapeOffsets(args.slice(at - offset), at, passageName, mode, true),
|
|
15573
15639
|
...navigation.includes(param.name) && { navigation: true }
|
|
15574
15640
|
});
|
|
15575
15641
|
const goal = codeGoal(param);
|
|
15576
15642
|
if (param.type === "passage") {
|
|
15577
15643
|
const target = passageTarget(value);
|
|
15578
15644
|
if (target.kind === "name") {
|
|
15579
|
-
yield passage(target.name);
|
|
15645
|
+
yield passage(target.name, offset + start + 1, "javascript");
|
|
15580
15646
|
continue;
|
|
15581
15647
|
}
|
|
15582
15648
|
}
|
|
@@ -15609,7 +15675,7 @@ function* valuePieces(args, offset, params, values, spans, macro, label, escapes
|
|
|
15609
15675
|
};
|
|
15610
15676
|
if (sourceOffsets) piece.sourceOffsets = sourceOffsets;
|
|
15611
15677
|
yield piece;
|
|
15612
|
-
} else if (holds === "passage") yield passage(value);
|
|
15678
|
+
} else if (holds === "passage") yield passage(value, at);
|
|
15613
15679
|
else if (holds === "markup") {
|
|
15614
15680
|
const piece = textPiece(value, at, `In the ${param.name} of {${macro}}: `);
|
|
15615
15681
|
if (sourceOffsets) piece.sourceOffsets = sourceOffsets;
|
|
@@ -17625,7 +17691,7 @@ function HtmlNodeRenderer({ node }) {
|
|
|
17625
17691
|
value: childNamespace,
|
|
17626
17692
|
children
|
|
17627
17693
|
});
|
|
17628
|
-
const element = authorElement(namespace === "svg" ? svgTagName(node.tag) :
|
|
17694
|
+
const element = authorElement(namespace === "svg" ? svgTagName(node.tag) : tag, props, children, namespace === "math");
|
|
17629
17695
|
return withAttributeErrors(errors, isRawRoot ? /* @__PURE__ */ jsx(RawTextContext.Provider, {
|
|
17630
17696
|
value: true,
|
|
17631
17697
|
children: element
|
|
@@ -21150,6 +21216,10 @@ var FOCUSABLE = [
|
|
|
21150
21216
|
"[contenteditable]",
|
|
21151
21217
|
"[tabindex]"
|
|
21152
21218
|
].join(",");
|
|
21219
|
+
/** Only the first summary child of a details element is its native toggle. */
|
|
21220
|
+
function isDetailsToggle(el) {
|
|
21221
|
+
return el.localName === "summary" && el.parentElement?.localName === "details" && Array.from(el.parentElement.children).find((c) => c.localName === "summary") === el;
|
|
21222
|
+
}
|
|
21153
21223
|
/**
|
|
21154
21224
|
* Whether `el` can be focused at all: shown, rendered, enabled (also by its
|
|
21155
21225
|
* fieldset) and not inert.
|
|
@@ -21158,7 +21228,8 @@ function isAvailable(el) {
|
|
|
21158
21228
|
if (!isRendered(el)) return false;
|
|
21159
21229
|
if ((el.ownerDocument.defaultView ?? window).getComputedStyle(el).display === "contents") return false;
|
|
21160
21230
|
for (let at = el; at; at = composedParent(at)) if (at.hasAttribute("inert")) return false;
|
|
21161
|
-
|
|
21231
|
+
if (el.matches(":disabled")) return false;
|
|
21232
|
+
return tabIndexAttr(el) !== null || el.tabIndex >= 0 || el.isContentEditable && !el.parentElement?.isContentEditable || isDetailsToggle(el);
|
|
21162
21233
|
}
|
|
21163
21234
|
/** The `tabindex` of `el` as an integer, or null when absent or invalid. */
|
|
21164
21235
|
function tabIndexAttr(el) {
|
|
@@ -21188,10 +21259,7 @@ function isTabStop(el, radioStops) {
|
|
|
21188
21259
|
const tabIndex = tabIndexAttr(el);
|
|
21189
21260
|
if (tabIndex !== null && tabIndex < 0) return false;
|
|
21190
21261
|
if (tabIndex === null && el.hasAttribute("contenteditable") && !el.matches("a[href], button, input, select, textarea, iframe, summary") && (!el.isContentEditable || el.parentElement?.isContentEditable)) return false;
|
|
21191
|
-
if (el.localName === "summary")
|
|
21192
|
-
const details = el.parentElement;
|
|
21193
|
-
if (details?.localName !== "details" || Array.from(details.children).find((c) => c.localName === "summary") !== el) return tabIndex !== null;
|
|
21194
|
-
}
|
|
21262
|
+
if (el.localName === "summary" && !isDetailsToggle(el)) return tabIndex !== null;
|
|
21195
21263
|
const input = el;
|
|
21196
21264
|
if (el.localName === "input" && input.type === "radio" && input.name) {
|
|
21197
21265
|
const tree = el.getRootNode();
|
|
@@ -21934,6 +22002,24 @@ function parseStoryData() {
|
|
|
21934
22002
|
userScript
|
|
21935
22003
|
};
|
|
21936
22004
|
}
|
|
22005
|
+
// src/utils/variable-writes.ts
|
|
22006
|
+
/** Explicit writes, including assignments that leave a binding unchanged. */
|
|
22007
|
+
var listeners$1 = /* @__PURE__ */ new Set();
|
|
22008
|
+
function notifyVariableWrite(path) {
|
|
22009
|
+
for (const listener of listeners$1) listener(path);
|
|
22010
|
+
}
|
|
22011
|
+
/** Observe writes to a binding, its parent, or one of its children. */
|
|
22012
|
+
function subscribeVariableWrites(binding, onWrite) {
|
|
22013
|
+
const listener = (path) => {
|
|
22014
|
+
const length = Math.min(path.length, binding.length);
|
|
22015
|
+
for (let i = 0; i < length; i++) if (path[i] !== binding[i]) return;
|
|
22016
|
+
onWrite();
|
|
22017
|
+
};
|
|
22018
|
+
listeners$1.add(listener);
|
|
22019
|
+
return () => {
|
|
22020
|
+
listeners$1.delete(listener);
|
|
22021
|
+
};
|
|
22022
|
+
}
|
|
21937
22023
|
// src/settings.ts
|
|
21938
22024
|
var definitions = /* @__PURE__ */ new Map();
|
|
21939
22025
|
var values = createNamespace();
|
|
@@ -22146,6 +22232,7 @@ function defineMacro(config, source = "builtin") {
|
|
|
22146
22232
|
ctx.value = getByPath(useStoryFields("variables").variables, segments);
|
|
22147
22233
|
ctx.getValue = () => getByPath(readState().variables, segments);
|
|
22148
22234
|
ctx.setValue = displayOnly ? refuse : (value) => {
|
|
22235
|
+
notifyVariableWrite(segments);
|
|
22149
22236
|
runAsControlEdit(segments, () => {
|
|
22150
22237
|
mutateState((work, adopt) => {
|
|
22151
22238
|
setByPath(work.variables, segments, adopt(value), { createMissing: true });
|
|
@@ -22540,7 +22627,7 @@ function observableText(el, skip, visibilityRoot) {
|
|
|
22540
22627
|
} finally {
|
|
22541
22628
|
instances.dispose();
|
|
22542
22629
|
}
|
|
22543
|
-
return out.split("\n").map((line) => line.trimEnd()).join("\n").replace(/\n{3,}/g, "\n\n").replace(/^\n
|
|
22630
|
+
return out.split("\n").map((line) => line.trimEnd()).join("\n").replace(/\n{3,}/g, "\n\n").replace(/^\n+/, "").trimEnd();
|
|
22544
22631
|
}
|
|
22545
22632
|
/**
|
|
22546
22633
|
* The trimmed text of each error shown in the element, leaving out those
|
|
@@ -22860,7 +22947,11 @@ function createStoryAPI() {
|
|
|
22860
22947
|
for (const [name] of entries) {
|
|
22861
22948
|
const { isTransient, key } = parseName(name);
|
|
22862
22949
|
warnIfUndeclared(isTransient, key);
|
|
22863
|
-
if (!isTransient)
|
|
22950
|
+
if (!isTransient) {
|
|
22951
|
+
const path = key.split(".");
|
|
22952
|
+
recordHookWrite(path);
|
|
22953
|
+
notifyVariableWrite(path);
|
|
22954
|
+
}
|
|
22864
22955
|
}
|
|
22865
22956
|
if (entries.some(([k]) => k.includes("."))) {
|
|
22866
22957
|
mutateState((work, adopt) => {
|
|
@@ -23178,6 +23269,7 @@ function validateMarkup(passages, options) {
|
|
|
23178
23269
|
const diagnostics = [];
|
|
23179
23270
|
const positions = /* @__PURE__ */ new Map();
|
|
23180
23271
|
const hooks = {
|
|
23272
|
+
...options.parametersOf?.pairing?.isStandalone && { isStandalone: options.parametersOf.pairing.isStandalone },
|
|
23181
23273
|
...options.isBlockMacro && { isBlock: options.isBlockMacro },
|
|
23182
23274
|
...options.branchParents && { branchParents: options.branchParents },
|
|
23183
23275
|
isRaw: (name) => isRawMacro(name, options.parametersOf ? options.parametersOf.widget?.(name) !== void 0 : void 0)
|
|
@@ -26845,19 +26937,142 @@ var VARIABLE_PARAMETER = {
|
|
|
26845
26937
|
type: "variable",
|
|
26846
26938
|
required: true
|
|
26847
26939
|
};
|
|
26940
|
+
var formResets = /* @__PURE__ */ new WeakMap();
|
|
26941
|
+
/** Read every reset value before any binding write can trigger a rerender. */
|
|
26942
|
+
function subscribeFormReset(form, read) {
|
|
26943
|
+
let subscription = formResets.get(form);
|
|
26944
|
+
if (!subscription) {
|
|
26945
|
+
const readers = /* @__PURE__ */ new Set();
|
|
26946
|
+
const reset = (event) => {
|
|
26947
|
+
const pending = Array.from(readers, (reader) => ({
|
|
26948
|
+
reader,
|
|
26949
|
+
...reader.prepare()
|
|
26950
|
+
}));
|
|
26951
|
+
setTimeout(() => {
|
|
26952
|
+
for (const { off } of pending) off();
|
|
26953
|
+
if (event.defaultPrevented) return;
|
|
26954
|
+
const snapshot = form.ownerDocument.createElement("form");
|
|
26955
|
+
const restoreDefaults = snapshot.reset.bind(snapshot);
|
|
26956
|
+
const controls = pending.map(({ reader, read }) => {
|
|
26957
|
+
const element = reader.element.cloneNode(true);
|
|
26958
|
+
element.removeAttribute("form");
|
|
26959
|
+
snapshot.appendChild(element);
|
|
26960
|
+
return {
|
|
26961
|
+
read,
|
|
26962
|
+
element
|
|
26963
|
+
};
|
|
26964
|
+
});
|
|
26965
|
+
restoreDefaults();
|
|
26966
|
+
const commits = controls.map(({ read, element }) => read(element));
|
|
26967
|
+
for (const commit of commits) commit();
|
|
26968
|
+
}, 0);
|
|
26969
|
+
};
|
|
26970
|
+
subscription = {
|
|
26971
|
+
readers,
|
|
26972
|
+
reset
|
|
26973
|
+
};
|
|
26974
|
+
formResets.set(form, subscription);
|
|
26975
|
+
form.addEventListener("reset", subscription.reset);
|
|
26976
|
+
}
|
|
26977
|
+
const { readers, reset } = subscription;
|
|
26978
|
+
readers.add(read);
|
|
26979
|
+
return () => {
|
|
26980
|
+
readers.delete(read);
|
|
26981
|
+
if (readers.size === 0) {
|
|
26982
|
+
form.removeEventListener("reset", reset);
|
|
26983
|
+
formResets.delete(form);
|
|
26984
|
+
}
|
|
26985
|
+
};
|
|
26986
|
+
}
|
|
26987
|
+
/** Native defaults belong to the binding's initial rendered control value. */
|
|
26988
|
+
function setResetDefault(control) {
|
|
26989
|
+
if (control.tagName === "SELECT") {
|
|
26990
|
+
const options = Array.from(control.options);
|
|
26991
|
+
const selected = options.map((option) => option.selected);
|
|
26992
|
+
options.forEach((option, i) => option.defaultSelected = selected[i]);
|
|
26993
|
+
} else if (control.tagName === "INPUT" && ["checkbox", "radio"].includes(control.type)) control.defaultChecked = control.checked;
|
|
26994
|
+
else control.defaultValue = control.value;
|
|
26995
|
+
}
|
|
26848
26996
|
/**
|
|
26849
26997
|
* Register the automation action of a macro bound to a story variable: keyed
|
|
26850
26998
|
* by the variable (or `key`), with the variable's value unless `action`
|
|
26851
26999
|
* gives another.
|
|
26852
27000
|
*/
|
|
26853
|
-
function useVariableAction(ctx, action) {
|
|
26854
|
-
|
|
27001
|
+
function useVariableAction(ctx, action, onReset) {
|
|
27002
|
+
const registered = useControlAction({
|
|
26855
27003
|
key: `$${ctx.varName}`,
|
|
26856
27004
|
authorId: ctx.id,
|
|
26857
27005
|
variable: ctx.varName,
|
|
26858
27006
|
value: ctx.value,
|
|
26859
27007
|
...action
|
|
26860
27008
|
});
|
|
27009
|
+
const control = useRef(null);
|
|
27010
|
+
const [, refresh] = useState(0);
|
|
27011
|
+
const initialized = useRef(null);
|
|
27012
|
+
const subscription = useRef(null);
|
|
27013
|
+
const resetValue = useRef({
|
|
27014
|
+
read: onReset,
|
|
27015
|
+
set: ctx.setValue,
|
|
27016
|
+
get: ctx.getValue,
|
|
27017
|
+
name: ctx.varName
|
|
27018
|
+
});
|
|
27019
|
+
resetValue.current = {
|
|
27020
|
+
read: onReset,
|
|
27021
|
+
set: ctx.setValue,
|
|
27022
|
+
get: ctx.getValue,
|
|
27023
|
+
name: ctx.varName
|
|
27024
|
+
};
|
|
27025
|
+
const ref = useCallback((element) => {
|
|
27026
|
+
control.current = element;
|
|
27027
|
+
registered.ref(element);
|
|
27028
|
+
}, [registered.ref]);
|
|
27029
|
+
useLayoutEffect(() => {
|
|
27030
|
+
const element = control.current;
|
|
27031
|
+
const form = element?.form;
|
|
27032
|
+
if (subscription.current?.element === element && subscription.current?.form === form) return;
|
|
27033
|
+
subscription.current?.off();
|
|
27034
|
+
subscription.current = null;
|
|
27035
|
+
if (!element || !onReset) return;
|
|
27036
|
+
if (initialized.current !== element) {
|
|
27037
|
+
setResetDefault(element);
|
|
27038
|
+
initialized.current = element;
|
|
27039
|
+
}
|
|
27040
|
+
if (!form) return;
|
|
27041
|
+
subscription.current = {
|
|
27042
|
+
element,
|
|
27043
|
+
form,
|
|
27044
|
+
off: subscribeFormReset(form, {
|
|
27045
|
+
element,
|
|
27046
|
+
prepare: () => {
|
|
27047
|
+
const before = resetValue.current.get();
|
|
27048
|
+
let changed = false;
|
|
27049
|
+
const offWrite = subscribeVariableWrites(resetValue.current.name.split("."), () => changed = true);
|
|
27050
|
+
const offStore = useStoryStore.subscribe(() => {
|
|
27051
|
+
changed ||= !Object.is(resetValue.current.get(), before);
|
|
27052
|
+
});
|
|
27053
|
+
return {
|
|
27054
|
+
off: () => {
|
|
27055
|
+
offWrite();
|
|
27056
|
+
offStore();
|
|
27057
|
+
},
|
|
27058
|
+
read: (snapshot) => {
|
|
27059
|
+
const value = changed ? void 0 : resetValue.current.read(snapshot);
|
|
27060
|
+
return () => {
|
|
27061
|
+
if (control.current !== element || element.form !== form) return;
|
|
27062
|
+
if (changed) refresh((version) => version + 1);
|
|
27063
|
+
else resetValue.current.set(value);
|
|
27064
|
+
};
|
|
27065
|
+
}
|
|
27066
|
+
};
|
|
27067
|
+
}
|
|
27068
|
+
})
|
|
27069
|
+
};
|
|
27070
|
+
});
|
|
27071
|
+
useLayoutEffect(() => () => subscription.current?.off(), []);
|
|
27072
|
+
return {
|
|
27073
|
+
...registered,
|
|
27074
|
+
ref
|
|
27075
|
+
};
|
|
26861
27076
|
}
|
|
26862
27077
|
/**
|
|
26863
27078
|
* Define a text input macro, `{name $var "placeholder"}`: an `<input>` of
|
|
@@ -26877,14 +27092,17 @@ function defineInputMacro(name, type, toValue = (input) => input !== void 0 ? St
|
|
|
26877
27092
|
holds: "text"
|
|
26878
27093
|
}],
|
|
26879
27094
|
render(_props, ctx) {
|
|
26880
|
-
const placeholder = ctx.args
|
|
27095
|
+
const { placeholder = "" } = ctx.args;
|
|
27096
|
+
const typed = ctx.hooks.useRef(null);
|
|
27097
|
+
const shown = typed.current !== null && toValue(typed.current) === ctx.value && type === "number" ? typed.current : display(ctx.value);
|
|
26881
27098
|
const action = useVariableAction(ctx, {
|
|
26882
27099
|
type: name,
|
|
26883
27100
|
label: placeholder || ctx.varName,
|
|
26884
27101
|
perform: (v) => ctx.setValue(toValue(v))
|
|
27102
|
+
}, (control) => {
|
|
27103
|
+
typed.current = control.value;
|
|
27104
|
+
return toValue(control.value);
|
|
26885
27105
|
});
|
|
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
27106
|
return h(type ? "input" : "textarea", {
|
|
26889
27107
|
type,
|
|
26890
27108
|
...actionAttributes(action, ctx),
|
|
@@ -26922,7 +27140,7 @@ defineMacro({
|
|
|
26922
27140
|
label: label || ctx.varName || "",
|
|
26923
27141
|
value: !!ctx.value,
|
|
26924
27142
|
perform: (v) => ctx.setValue(v !== void 0 ? !!v : !ctx.getValue())
|
|
26925
|
-
});
|
|
27143
|
+
}, (control) => control.checked);
|
|
26926
27144
|
return /* @__PURE__ */ jsxs("label", {
|
|
26927
27145
|
id: ctx.id,
|
|
26928
27146
|
class: ctx.cls,
|
|
@@ -26958,11 +27176,17 @@ defineMacro({
|
|
|
26958
27176
|
const radioValue = ctx.args.value ?? "";
|
|
26959
27177
|
const label = ctx.args.label ?? "";
|
|
26960
27178
|
const view = ctx.hooks.useContext(ViewScopeContext);
|
|
27179
|
+
const initialValue = ctx.hooks.useRef(ctx.value);
|
|
26961
27180
|
const action = useVariableAction(ctx, {
|
|
26962
27181
|
type: "radiobutton",
|
|
26963
27182
|
key: `$${ctx.varName}:${radioValue}`,
|
|
26964
27183
|
label: label || radioValue,
|
|
26965
27184
|
perform: () => ctx.setValue(radioValue)
|
|
27185
|
+
}, (control) => {
|
|
27186
|
+
const radio = control;
|
|
27187
|
+
const group = radio.form?.elements.namedItem(radio.name);
|
|
27188
|
+
const selected = group && ("length" in group ? Array.from(group).find((node) => node.checked) : group.checked ? group : void 0);
|
|
27189
|
+
return selected ? selected.value : initialValue.current;
|
|
26966
27190
|
});
|
|
26967
27191
|
return /* @__PURE__ */ jsxs("label", {
|
|
26968
27192
|
id: ctx.id,
|
|
@@ -26994,7 +27218,7 @@ defineMacro({
|
|
|
26994
27218
|
perform: (v) => {
|
|
26995
27219
|
if (v !== void 0) ctx.setValue(String(v));
|
|
26996
27220
|
}
|
|
26997
|
-
});
|
|
27221
|
+
}, (control) => control.value);
|
|
26998
27222
|
return /* @__PURE__ */ jsx("select", {
|
|
26999
27223
|
...actionAttributes(action, ctx),
|
|
27000
27224
|
value: display(ctx.value),
|