@rohal12/spindle 0.61.4 → 0.61.5
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 +64 -33
- package/dist/pkg/story-variables.js +9 -4
- package/package.json +1 -1
- package/src/class-registry.ts +16 -8
- package/src/components/macros/VarDisplay.tsx +6 -4
- package/src/components/macros/arg-utils.ts +2 -9
- package/src/components/macros/macro-args.ts +1 -1
- package/src/define-macro.ts +2 -2
- package/src/format/format-passage.ts +12 -1
- package/src/saves/format.ts +6 -0
- package/src/structural.ts +57 -32
- package/src/utils/stable-key.ts +8 -5
- package/src/utils/value-kinds.ts +4 -0
package/dist/pkg/headless.js
CHANGED
|
@@ -11259,8 +11259,7 @@ function startsLikeValue(token) {
|
|
|
11259
11259
|
const first = token[0];
|
|
11260
11260
|
if (first === "\"" || first === "'" || first === "`") return true;
|
|
11261
11261
|
if (/^[$_@]\w|^%[A-Za-z_]/.test(token)) return true;
|
|
11262
|
-
if (
|
|
11263
|
-
if ((first === "-" || first === "+") && token.length > 1 && /\d/.test(token[1])) return true;
|
|
11262
|
+
if (/^[+-]?(?:\d|\.\d)/.test(token)) return true;
|
|
11264
11263
|
if (first === "(" || first === "[" || first === "{") return true;
|
|
11265
11264
|
if (/^(?:true|false|null|undefined)(?![\w$])/.test(token)) return true;
|
|
11266
11265
|
if (first === "!" && token.length > 1 && token[1] !== "=") return true;
|
|
@@ -11423,7 +11422,7 @@ function readOptions(params, src, { at, spans }) {
|
|
|
11423
11422
|
return options;
|
|
11424
11423
|
}
|
|
11425
11424
|
/** `inline %name` reads a transient variable; `inline % 2` is modulo. */
|
|
11426
|
-
var LEADING_OPERATOR_RE = /^(?:[
|
|
11425
|
+
var LEADING_OPERATOR_RE = /^(?:[-+*/&|^=<>?:,]|\.(?!\d)|%(?![A-Za-z_]))/;
|
|
11427
11426
|
/**
|
|
11428
11427
|
* Where the rest of `src` is without the flag `word` as its last or first
|
|
11429
11428
|
* term, or `null` if it has no such flag. Next to a binary operator the
|
|
@@ -13254,7 +13253,8 @@ function VarDisplay({ node }) {
|
|
|
13254
13253
|
const localsValues = useLiveLocals();
|
|
13255
13254
|
const parts = name.split(".");
|
|
13256
13255
|
const root = parts[0];
|
|
13257
|
-
const
|
|
13256
|
+
const state = useStoryFields("variables", "temporary", "transient");
|
|
13257
|
+
const storeValue = scope === "local" ? void 0 : ownValue(state[STORE_NAMESPACES[scope]], root);
|
|
13258
13258
|
const sigil = SCOPE_SIGILS[scope];
|
|
13259
13259
|
const nameError = variableNameError(root, sigil + root);
|
|
13260
13260
|
if (nameError) return /* @__PURE__ */ jsx("span", {
|
|
@@ -15182,6 +15182,8 @@ var ERROR_HIDDEN_KEYS$1 = [
|
|
|
15182
15182
|
];
|
|
15183
15183
|
/** Whether `key` is an index of an array element. */
|
|
15184
15184
|
var isIndexKey = (key) => key !== "4294967295" && String(Number(key) >>> 0) === key;
|
|
15185
|
+
/** Populated own array indices, in order, without visiting holes. */
|
|
15186
|
+
var arrayIndices = (value) => Object.getOwnPropertyNames(value).filter(isIndexKey);
|
|
15185
15187
|
/**
|
|
15186
15188
|
* The own enumerable string keys of a collection (an Array, Map or Set) that
|
|
15187
15189
|
* are not its elements: what an instance of a subclass may hold besides
|
|
@@ -15355,7 +15357,7 @@ function classData(value) {
|
|
|
15355
15357
|
const extra = () => ownData(value, extraKeys(value));
|
|
15356
15358
|
if (Array.isArray(value)) {
|
|
15357
15359
|
const items = new Array(value.length);
|
|
15358
|
-
for (
|
|
15360
|
+
for (const key of arrayIndices(value)) items[Number(key)] = value[Number(key)];
|
|
15359
15361
|
return [
|
|
15360
15362
|
"Array",
|
|
15361
15363
|
items,
|
|
@@ -15480,9 +15482,9 @@ function reviveBuiltin(name, ctor, list) {
|
|
|
15480
15482
|
/** Set the elements of a revived collection, with the built-in methods. */
|
|
15481
15483
|
function fillCollection(made, contents) {
|
|
15482
15484
|
if (Array.isArray(made)) {
|
|
15485
|
+
for (const key of arrayIndices(made)) delete made[Number(key)];
|
|
15483
15486
|
made.length = contents.length;
|
|
15484
|
-
for (
|
|
15485
|
-
else delete made[i];
|
|
15487
|
+
for (const key of arrayIndices(contents)) made[Number(key)] = contents[Number(key)];
|
|
15486
15488
|
} else if (made instanceof Map) {
|
|
15487
15489
|
Map.prototype.clear.call(made);
|
|
15488
15490
|
for (const entry of contents) {
|
|
@@ -15491,7 +15493,15 @@ function fillCollection(made, contents) {
|
|
|
15491
15493
|
}
|
|
15492
15494
|
} else {
|
|
15493
15495
|
Set.prototype.clear.call(made);
|
|
15494
|
-
|
|
15496
|
+
const indices = arrayIndices(contents);
|
|
15497
|
+
let next = 0;
|
|
15498
|
+
for (const key of indices) {
|
|
15499
|
+
const i = Number(key);
|
|
15500
|
+
if (i > next) Set.prototype.add.call(made, void 0);
|
|
15501
|
+
Set.prototype.add.call(made, contents[i]);
|
|
15502
|
+
next = i + 1;
|
|
15503
|
+
}
|
|
15504
|
+
if (next < contents.length) Set.prototype.add.call(made, void 0);
|
|
15495
15505
|
}
|
|
15496
15506
|
}
|
|
15497
15507
|
/** The object of a `[KEYS_TAG, ...entries]` list (see savedData); other data as is. */
|
|
@@ -15831,7 +15841,7 @@ function deepClone(value, options = {}) {
|
|
|
15831
15841
|
if (seen.has(obj)) return seen.get(obj);
|
|
15832
15842
|
if (Array.isArray(val)) {
|
|
15833
15843
|
const arr = builtin(obj, new Array(val.length));
|
|
15834
|
-
for (
|
|
15844
|
+
for (const key of arrayIndices(val)) arr[Number(key)] = clone(val[Number(key)]);
|
|
15835
15845
|
return arr;
|
|
15836
15846
|
}
|
|
15837
15847
|
if (val instanceof Map) {
|
|
@@ -15957,7 +15967,7 @@ function shareEqual(prev, curr) {
|
|
|
15957
15967
|
}
|
|
15958
15968
|
if (Array.isArray(c) && Array.isArray(p)) {
|
|
15959
15969
|
const out = collection(new Array(c.length));
|
|
15960
|
-
for (
|
|
15970
|
+
for (const key of arrayIndices(c)) out[Number(key)] = shareKey(key);
|
|
15961
15971
|
return out;
|
|
15962
15972
|
}
|
|
15963
15973
|
if (mergesWith(p, c, false)) {
|
|
@@ -16042,8 +16052,7 @@ function equal(a, b, assumed) {
|
|
|
16042
16052
|
if (Array.isArray(a)) {
|
|
16043
16053
|
const bc = b;
|
|
16044
16054
|
if (a.length !== bc.length) return false;
|
|
16045
|
-
|
|
16046
|
-
return equalExtraKeys(a, b, assumed);
|
|
16055
|
+
return equalKeys(a, b, assumed, (v) => arrayIndices(v)) && equalExtraKeys(a, b, assumed);
|
|
16047
16056
|
}
|
|
16048
16057
|
return equalKeys(a, b, assumed);
|
|
16049
16058
|
}
|
|
@@ -16096,8 +16105,10 @@ function keyChanges(before, after, arrays) {
|
|
|
16096
16105
|
if (Array.isArray(after)) {
|
|
16097
16106
|
const b = before;
|
|
16098
16107
|
const a = after;
|
|
16099
|
-
|
|
16100
|
-
|
|
16108
|
+
const indices = [.../* @__PURE__ */ new Set([...arrayIndices(b), ...arrayIndices(a)])].sort((x, y) => Number(x) - Number(y));
|
|
16109
|
+
for (const key of indices) {
|
|
16110
|
+
const i = Number(key);
|
|
16111
|
+
if (i >= Math.min(b.length, a.length)) continue;
|
|
16101
16112
|
if (i in b && i in a) compare(key, b[i], a[i]);
|
|
16102
16113
|
else if (i in b) changes.push({
|
|
16103
16114
|
key,
|
|
@@ -16111,8 +16122,9 @@ function keyChanges(before, after, arrays) {
|
|
|
16111
16122
|
});
|
|
16112
16123
|
}
|
|
16113
16124
|
let next = b.length;
|
|
16114
|
-
for (
|
|
16115
|
-
|
|
16125
|
+
for (const key of arrayIndices(a)) {
|
|
16126
|
+
const i = Number(key);
|
|
16127
|
+
if (i < b.length) continue;
|
|
16116
16128
|
const gap = i - next;
|
|
16117
16129
|
changes.push({
|
|
16118
16130
|
key: String(i),
|
|
@@ -16122,8 +16134,8 @@ function keyChanges(before, after, arrays) {
|
|
|
16122
16134
|
});
|
|
16123
16135
|
next = i + 1;
|
|
16124
16136
|
}
|
|
16125
|
-
|
|
16126
|
-
key: String(
|
|
16137
|
+
if (a.length < b.length) changes.push({
|
|
16138
|
+
key: String(a.length),
|
|
16127
16139
|
kind: "deleted"
|
|
16128
16140
|
});
|
|
16129
16141
|
if (a.length > next) changes.push({
|
|
@@ -16234,8 +16246,9 @@ var entryKey = (kind, index) => ({
|
|
|
16234
16246
|
var isEntryKey = (segment) => typeof segment !== "string";
|
|
16235
16247
|
/**
|
|
16236
16248
|
* What a Map, Set, Error or view holds in place of properties (keys and
|
|
16237
|
-
* values, members, `cause` and `errors`, the buffer), each with
|
|
16238
|
-
* a path (see entryKey); undefined for any
|
|
16249
|
+
* values, members, `message`, `cause` and `errors`, the buffer), each with
|
|
16250
|
+
* the segment standing for it in a path (see entryKey); undefined for any
|
|
16251
|
+
* other value.
|
|
16239
16252
|
*/
|
|
16240
16253
|
function entryChildren(value) {
|
|
16241
16254
|
const children = [];
|
|
@@ -16249,7 +16262,7 @@ function entryChildren(value) {
|
|
|
16249
16262
|
let index = 0;
|
|
16250
16263
|
for (const member of setMembers(value)) children.push([entryKey("s", index++), member]);
|
|
16251
16264
|
} else if (value instanceof Error) {
|
|
16252
|
-
for (const key of
|
|
16265
|
+
for (const key of ERROR_HIDDEN_KEYS$1) if (hasOwn(value, key)) children.push([entryKey(key === "message" ? "m" : key === "cause" ? "c" : "e", 0), value[key]]);
|
|
16253
16266
|
} else if (ArrayBuffer.isView(value)) children.push([entryKey("b", 0), value.buffer]);
|
|
16254
16267
|
else return;
|
|
16255
16268
|
return children;
|
|
@@ -16281,11 +16294,13 @@ function setEntryValue(root, path, value) {
|
|
|
16281
16294
|
return true;
|
|
16282
16295
|
}
|
|
16283
16296
|
/**
|
|
16284
|
-
* The own enumerable fields of
|
|
16285
|
-
* registered subclass may hold some besides its built-in
|
|
16297
|
+
* The own enumerable fields of an Error, Map, Set, Date or RegExp (an
|
|
16298
|
+
* instance of a registered subclass may hold some besides its built-in
|
|
16299
|
+
* content, #412). Hidden Error data is visited through entryChildren;
|
|
16286
16300
|
* none for any other value.
|
|
16287
16301
|
*/
|
|
16288
16302
|
function builtinFields(value) {
|
|
16303
|
+
if (value instanceof Error) return Object.keys(value).filter((key) => !ERROR_HIDDEN_KEYS$1.some((hidden) => hidden === key));
|
|
16289
16304
|
return value instanceof Map || value instanceof Set || value instanceof Date || value instanceof RegExp ? extraKeys(value) : [];
|
|
16290
16305
|
}
|
|
16291
16306
|
/** Whether `x` and `y` are built-ins of one class that may hold fields. */
|
|
@@ -16294,16 +16309,16 @@ function sameBuiltin(x, y) {
|
|
|
16294
16309
|
}
|
|
16295
16310
|
/**
|
|
16296
16311
|
* Every path each object (or array, or other value) of `root` is at, the
|
|
16297
|
-
* first appearance first, in depth-first order. Plain objects and arrays
|
|
16298
|
-
* entered, where first met only (an
|
|
16299
|
-
*
|
|
16300
|
-
* of `skip` is left out.
|
|
16312
|
+
* first appearance first, in depth-first order. Plain objects and arrays
|
|
16313
|
+
* (including non-enumerable indices) are entered, where first met only (an
|
|
16314
|
+
* object held in an array is one object, however it was reached); other
|
|
16315
|
+
* values are leaves. A path at or below one of `skip` is left out.
|
|
16301
16316
|
*
|
|
16302
16317
|
* With `entries`, the objects held in a Map or Set (as key, value or member)
|
|
16303
16318
|
* are placed too, after everything else: one met nowhere else is at the path
|
|
16304
16319
|
* of its Map or Set and the entry's segment (see entryKey), which is no path
|
|
16305
16320
|
* a value can be read at (see getByEntryPath), so only to tell where an
|
|
16306
|
-
* object is from; so are the `cause` and `errors` of an Error.
|
|
16321
|
+
* object is from; so are the `message`, `cause` and `errors` of an Error.
|
|
16307
16322
|
*/
|
|
16308
16323
|
function objectPaths(root, skip = /* @__PURE__ */ new Set(), entries = false) {
|
|
16309
16324
|
const all = /* @__PURE__ */ new Map();
|
|
@@ -16321,7 +16336,7 @@ function objectPaths(root, skip = /* @__PURE__ */ new Set(), entries = false) {
|
|
|
16321
16336
|
if (entries && entryChildren(value)) collections.push([value, at]);
|
|
16322
16337
|
}
|
|
16323
16338
|
}
|
|
16324
|
-
function walk(node, path, keys = Object.keys(node)) {
|
|
16339
|
+
function walk(node, path, keys = Array.isArray(node) ? [...arrayIndices(node), ...extraKeys(node)] : Object.keys(node)) {
|
|
16325
16340
|
for (const key of keys) {
|
|
16326
16341
|
const value = node[key];
|
|
16327
16342
|
if (!isObjectValue(value)) continue;
|
|
@@ -16339,6 +16354,14 @@ function firstPaths(root, skip, entries) {
|
|
|
16339
16354
|
return new Map([...objectPaths(root, skip, entries)].map(([object, paths]) => [object, paths[0]]));
|
|
16340
16355
|
}
|
|
16341
16356
|
/**
|
|
16357
|
+
* Refuse arrays whose logical length would make downstream slot scans
|
|
16358
|
+
* (including Immer's array freezing) excessive. Inspect populated keys and
|
|
16359
|
+
* collection entries only, sharing the graph walk used for structural diffs.
|
|
16360
|
+
*/
|
|
16361
|
+
function assertArrayLengths(root, maxLength) {
|
|
16362
|
+
for (const value of objectPaths({ root }, void 0, true).keys()) if (Array.isArray(value) && value.length > maxLength) throw new RangeError(`Save arrays cannot exceed ${maxLength} elements`);
|
|
16363
|
+
}
|
|
16364
|
+
/**
|
|
16342
16365
|
* The objects `root` holds at more than one path (an object two variables
|
|
16343
16366
|
* refer to, or one that refers to itself), with every path they are at,
|
|
16344
16367
|
* parents before children.
|
|
@@ -17297,6 +17320,7 @@ function closeAllOpenDialogs() {
|
|
|
17297
17320
|
function isDialogShowing() {
|
|
17298
17321
|
return openDialogs.length > 0 || (dialogHostCallbacks?.isOpen() ?? false);
|
|
17299
17322
|
}
|
|
17323
|
+
var MAX_SAVE_ARRAY_LENGTH = 1e6;
|
|
17300
17324
|
/** A save, export or session from a version of Spindle this one cannot read. */
|
|
17301
17325
|
var IncompatibleSaveError = class extends Error {
|
|
17302
17326
|
constructor() {
|
|
@@ -17346,6 +17370,7 @@ function variablePathError(err) {
|
|
|
17346
17370
|
*/
|
|
17347
17371
|
function encodePayload(payload) {
|
|
17348
17372
|
try {
|
|
17373
|
+
assertArrayLengths(payload, MAX_SAVE_ARRAY_LENGTH);
|
|
17349
17374
|
return {
|
|
17350
17375
|
formatVersion: 1,
|
|
17351
17376
|
data: serialize({
|
|
@@ -17407,6 +17432,7 @@ function decodePayload(encoded) {
|
|
|
17407
17432
|
if (typeof data !== "string") throw invalid();
|
|
17408
17433
|
const body = deserialize(migrate(formatVersion, data));
|
|
17409
17434
|
if (!isPayload(body)) throw invalid();
|
|
17435
|
+
assertArrayLengths(body, MAX_SAVE_ARRAY_LENGTH);
|
|
17410
17436
|
assertTrustedWatchers(body.watchers ?? []);
|
|
17411
17437
|
const payload = {
|
|
17412
17438
|
...body,
|
|
@@ -20933,7 +20959,7 @@ function defineMacro(config, source = "builtin") {
|
|
|
20933
20959
|
const varExpr = segments.join(".");
|
|
20934
20960
|
if (segments.includes("__proto__")) return h("span", { class: "error" }, `{${config.name}}: "$${varExpr}" cannot be bound: ${RESERVED_NAME} is reserved`);
|
|
20935
20961
|
ctx.varName = varExpr;
|
|
20936
|
-
ctx.value =
|
|
20962
|
+
ctx.value = getByPath(useStoryFields("variables").variables, segments);
|
|
20937
20963
|
ctx.getValue = () => getByPath(readState().variables, segments);
|
|
20938
20964
|
ctx.setValue = displayOnly ? refuse : (value) => {
|
|
20939
20965
|
noteControlEdit(segments);
|
|
@@ -21215,8 +21241,6 @@ function stableKey(value) {
|
|
|
21215
21241
|
return "<unkeyable>";
|
|
21216
21242
|
}
|
|
21217
21243
|
}
|
|
21218
|
-
/** The key of an array slot that holds no element. */
|
|
21219
|
-
var HOLE = "<hole>";
|
|
21220
21244
|
/**
|
|
21221
21245
|
* The key of `val`. `seen` numbers the objects keyed so far, in the order
|
|
21222
21246
|
* they were first met.
|
|
@@ -21250,7 +21274,14 @@ function keyOf(val, seen) {
|
|
|
21250
21274
|
seen.set(obj, seen.size);
|
|
21251
21275
|
if (Array.isArray(val)) {
|
|
21252
21276
|
const items = [];
|
|
21253
|
-
|
|
21277
|
+
let next = 0;
|
|
21278
|
+
for (const index of arrayIndices(val)) {
|
|
21279
|
+
const i = Number(index);
|
|
21280
|
+
if (i > next) items.push(`<holes ${i - next}>`);
|
|
21281
|
+
items.push(keyOf(val[i], seen));
|
|
21282
|
+
next = i + 1;
|
|
21283
|
+
}
|
|
21284
|
+
if (val.length > next) items.push(`<holes ${val.length - next}>`);
|
|
21254
21285
|
return withFields(`[${items.join(",")}]`);
|
|
21255
21286
|
}
|
|
21256
21287
|
const atomic = atomicKey(obj, seen);
|
|
@@ -10591,8 +10591,7 @@ function startsLikeValue(token) {
|
|
|
10591
10591
|
const first = token[0];
|
|
10592
10592
|
if (first === "\"" || first === "'" || first === "`") return true;
|
|
10593
10593
|
if (/^[$_@]\w|^%[A-Za-z_]/.test(token)) return true;
|
|
10594
|
-
if (
|
|
10595
|
-
if ((first === "-" || first === "+") && token.length > 1 && /\d/.test(token[1])) return true;
|
|
10594
|
+
if (/^[+-]?(?:\d|\.\d)/.test(token)) return true;
|
|
10596
10595
|
if (first === "(" || first === "[" || first === "{") return true;
|
|
10597
10596
|
if (/^(?:true|false|null|undefined)(?![\w$])/.test(token)) return true;
|
|
10598
10597
|
if (first === "!" && token.length > 1 && token[1] !== "=") return true;
|
|
@@ -10755,7 +10754,7 @@ function readOptions(params, src, { at, spans }) {
|
|
|
10755
10754
|
return options;
|
|
10756
10755
|
}
|
|
10757
10756
|
/** `inline %name` reads a transient variable; `inline % 2` is modulo. */
|
|
10758
|
-
var LEADING_OPERATOR_RE = /^(?:[
|
|
10757
|
+
var LEADING_OPERATOR_RE = /^(?:[-+*/&|^=<>?:,]|\.(?!\d)|%(?![A-Za-z_]))/;
|
|
10759
10758
|
/**
|
|
10760
10759
|
* Where the rest of `src` is without the flag `word` as its last or first
|
|
10761
10760
|
* term, or `null` if it has no such flag. Next to a binary operator the
|
|
@@ -25051,12 +25050,14 @@ var NO_WRAP = /* @__PURE__ */ new Set([
|
|
|
25051
25050
|
"atxHeading",
|
|
25052
25051
|
"setextHeading",
|
|
25053
25052
|
"table",
|
|
25054
|
-
"
|
|
25053
|
+
"listOrdered",
|
|
25054
|
+
"listUnordered",
|
|
25055
25055
|
"blockQuote",
|
|
25056
25056
|
"definition",
|
|
25057
25057
|
"thematicBreak",
|
|
25058
25058
|
"htmlFlow",
|
|
25059
25059
|
"codeText",
|
|
25060
|
+
"codeFenced",
|
|
25060
25061
|
"link",
|
|
25061
25062
|
"image"
|
|
25062
25063
|
]);
|
|
@@ -25080,6 +25081,10 @@ function wrapLines(lines, maxLen, literalLines, isBlock) {
|
|
|
25080
25081
|
continue;
|
|
25081
25082
|
}
|
|
25082
25083
|
const wrapped = wordWrap(trimmed, maxLen, line.match(/^(\s*)/)?.[1] ?? "");
|
|
25084
|
+
if (wrapped.length > 1 && markdownLines(wrapped.join("\n"), isBlock, NO_WRAP).size) {
|
|
25085
|
+
result.push(line);
|
|
25086
|
+
continue;
|
|
25087
|
+
}
|
|
25083
25088
|
if (line.endsWith(" ")) wrapped[wrapped.length - 1] += " ";
|
|
25084
25089
|
result.push(...wrapped);
|
|
25085
25090
|
}
|
package/package.json
CHANGED
package/src/class-registry.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import { parse, stringify } from 'devalue';
|
|
4
4
|
import { hasOwn } from './utils/namespace';
|
|
5
5
|
import {
|
|
6
|
+
arrayIndices,
|
|
6
7
|
extraKeys,
|
|
7
8
|
mapEntries,
|
|
8
9
|
regExpParts,
|
|
@@ -219,9 +220,8 @@ function classData(value: object): unknown {
|
|
|
219
220
|
if (Array.isArray(value)) {
|
|
220
221
|
// Holes stay holes
|
|
221
222
|
const items = new Array(value.length) as unknown[];
|
|
222
|
-
for (
|
|
223
|
-
|
|
224
|
-
}
|
|
223
|
+
for (const key of arrayIndices(value))
|
|
224
|
+
items[Number(key)] = value[Number(key)] as unknown;
|
|
225
225
|
return ['Array', items, extra()];
|
|
226
226
|
}
|
|
227
227
|
if (value instanceof Map) {
|
|
@@ -391,11 +391,10 @@ function reviveBuiltin(name: string, ctor: Constructor, list: unknown[]) {
|
|
|
391
391
|
/** Set the elements of a revived collection, with the built-in methods. */
|
|
392
392
|
function fillCollection(made: object, contents: unknown[]): void {
|
|
393
393
|
if (Array.isArray(made)) {
|
|
394
|
+
for (const key of arrayIndices(made)) delete made[Number(key)];
|
|
394
395
|
made.length = contents.length;
|
|
395
|
-
for (
|
|
396
|
-
|
|
397
|
-
else delete made[i];
|
|
398
|
-
}
|
|
396
|
+
for (const key of arrayIndices(contents))
|
|
397
|
+
made[Number(key)] = contents[Number(key)];
|
|
399
398
|
} else if (made instanceof Map) {
|
|
400
399
|
Map.prototype.clear.call(made);
|
|
401
400
|
for (const entry of contents) {
|
|
@@ -406,7 +405,16 @@ function fillCollection(made: object, contents: unknown[]): void {
|
|
|
406
405
|
}
|
|
407
406
|
} else {
|
|
408
407
|
Set.prototype.clear.call(made);
|
|
409
|
-
|
|
408
|
+
// Holes contribute only undefined to a Set; do not expand a sparse list.
|
|
409
|
+
const indices = arrayIndices(contents);
|
|
410
|
+
let next = 0;
|
|
411
|
+
for (const key of indices) {
|
|
412
|
+
const i = Number(key);
|
|
413
|
+
if (i > next) Set.prototype.add.call(made, undefined);
|
|
414
|
+
Set.prototype.add.call(made, contents[i]);
|
|
415
|
+
next = i + 1;
|
|
416
|
+
}
|
|
417
|
+
if (next < contents.length) Set.prototype.add.call(made, undefined);
|
|
410
418
|
}
|
|
411
419
|
}
|
|
412
420
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useStoryFields } from '../../hooks/use-story-fields';
|
|
2
2
|
import { useLiveLocals } from '../../hooks/use-merged-locals';
|
|
3
3
|
import { useInterpolate } from '../../hooks/use-interpolate';
|
|
4
4
|
import { ownValue, variableNameError } from '../../utils/namespace';
|
|
@@ -23,9 +23,11 @@ export function VarDisplay({ node }: { node: VariableNode }) {
|
|
|
23
23
|
const parts = name.split('.');
|
|
24
24
|
const root = parts[0]!;
|
|
25
25
|
// Own entries only, as the expression engine reads them
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
const state = useStoryFields('variables', 'temporary', 'transient');
|
|
27
|
+
const storeValue =
|
|
28
|
+
scope === 'local'
|
|
29
|
+
? undefined
|
|
30
|
+
: ownValue(state[STORE_NAMESPACES[scope]], root);
|
|
29
31
|
|
|
30
32
|
const sigil = SCOPE_SIGILS[scope];
|
|
31
33
|
const nameError = variableNameError(root, sigil + root);
|
|
@@ -182,15 +182,8 @@ function startsLikeValue(token: string): boolean {
|
|
|
182
182
|
if (first === '"' || first === "'" || first === '`') return true;
|
|
183
183
|
// Variable ($var, _var, @var, %var), but not the modulo operator in `$a % 2`
|
|
184
184
|
if (/^[$_@]\w|^%[A-Za-z_]/.test(token)) return true;
|
|
185
|
-
// Number literal
|
|
186
|
-
if (
|
|
187
|
-
// Signed number (-1, +2)
|
|
188
|
-
if (
|
|
189
|
-
(first === '-' || first === '+') &&
|
|
190
|
-
token.length > 1 &&
|
|
191
|
-
/\d/.test(token[1]!)
|
|
192
|
-
)
|
|
193
|
-
return true;
|
|
185
|
+
// Number literal, optionally signed, including leading-dot decimals.
|
|
186
|
+
if (/^[+-]?(?:\d|\.\d)/.test(token)) return true;
|
|
194
187
|
// Grouped expression or collection literal
|
|
195
188
|
if (first === '(' || first === '[' || first === '{') return true;
|
|
196
189
|
// Boolean / null / undefined, alone or leading an expression (true||$x)
|
|
@@ -176,7 +176,7 @@ function readOptions(
|
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
/** `inline %name` reads a transient variable; `inline % 2` is modulo. */
|
|
179
|
-
const LEADING_OPERATOR_RE = /^(?:[
|
|
179
|
+
const LEADING_OPERATOR_RE = /^(?:[-+*/&|^=<>?:,]|\.(?!\d)|%(?![A-Za-z_]))/;
|
|
180
180
|
|
|
181
181
|
/**
|
|
182
182
|
* Where the rest of `src` is without the flag `word` as its last or first
|
package/src/define-macro.ts
CHANGED
|
@@ -25,7 +25,7 @@ import {
|
|
|
25
25
|
import type { ASTNode } from './markup/ast';
|
|
26
26
|
import { executeMutation, mutateState, readState } from './execute-mutation';
|
|
27
27
|
import { evaluateDisplay } from './expression';
|
|
28
|
-
import {
|
|
28
|
+
import { useStoryFields } from './hooks/use-story-fields';
|
|
29
29
|
import { getByPath, setByPath } from './utils/object-path';
|
|
30
30
|
import { RESERVED_NAME } from './utils/namespace';
|
|
31
31
|
import { parseMemberIdentifier, parseReferencePath } from './js-lexer';
|
|
@@ -299,7 +299,7 @@ export function defineMacro<const P extends readonly ParameterDef[] = []>(
|
|
|
299
299
|
);
|
|
300
300
|
}
|
|
301
301
|
ctx.varName = varExpr;
|
|
302
|
-
ctx.value =
|
|
302
|
+
ctx.value = getByPath(useStoryFields('variables').variables, segments);
|
|
303
303
|
// In program order, also when mutation code performs the input
|
|
304
304
|
ctx.getValue = () => getByPath(readState().variables, segments);
|
|
305
305
|
ctx.setValue = displayOnly
|
|
@@ -611,12 +611,14 @@ const NO_WRAP = new Set([
|
|
|
611
611
|
'atxHeading',
|
|
612
612
|
'setextHeading',
|
|
613
613
|
'table',
|
|
614
|
-
'
|
|
614
|
+
'listOrdered',
|
|
615
|
+
'listUnordered',
|
|
615
616
|
'blockQuote',
|
|
616
617
|
'definition',
|
|
617
618
|
'thematicBreak',
|
|
618
619
|
'htmlFlow',
|
|
619
620
|
'codeText',
|
|
621
|
+
'codeFenced',
|
|
620
622
|
'link',
|
|
621
623
|
'image',
|
|
622
624
|
]);
|
|
@@ -668,6 +670,15 @@ function wrapLines(
|
|
|
668
670
|
const indent = indentMatch?.[1] ?? '';
|
|
669
671
|
|
|
670
672
|
const wrapped = wordWrap(trimmed, maxLen, indent);
|
|
673
|
+
// A new line before inline punctuation can turn prose into a heading,
|
|
674
|
+
// list, quote, fence, etc. Keep the source line when wrapping adds syntax.
|
|
675
|
+
if (
|
|
676
|
+
wrapped.length > 1 &&
|
|
677
|
+
markdownLines(wrapped.join('\n'), isBlock, NO_WRAP).size
|
|
678
|
+
) {
|
|
679
|
+
result.push(line);
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
671
682
|
// Keep a Markdown hard break on the last wrapped line
|
|
672
683
|
if (line.endsWith(' ')) wrapped[wrapped.length - 1] += ' ';
|
|
673
684
|
result.push(...wrapped);
|
package/src/saves/format.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
import { assertTrustedWatchers } from '../triggers';
|
|
5
5
|
import { deserialize, serialize } from '../class-registry';
|
|
6
|
+
import { assertArrayLengths } from '../structural';
|
|
6
7
|
import { createCounts, hasOwn } from '../utils/namespace';
|
|
7
8
|
import type { SavePayload } from './types';
|
|
8
9
|
|
|
@@ -13,6 +14,9 @@ import type { SavePayload } from './types';
|
|
|
13
14
|
*/
|
|
14
15
|
export const SAVE_FORMAT_VERSION = 1;
|
|
15
16
|
|
|
17
|
+
// Immer and author-facing array methods may scan logical slots, even holes.
|
|
18
|
+
const MAX_SAVE_ARRAY_LENGTH = 1_000_000;
|
|
19
|
+
|
|
16
20
|
/**
|
|
17
21
|
* A payload as stored: `data` is the payload serialized in one piece (see
|
|
18
22
|
* serialize() in class-registry.ts), so values the history moments share
|
|
@@ -94,6 +98,7 @@ function variablePathError(err: unknown): unknown {
|
|
|
94
98
|
*/
|
|
95
99
|
export function encodePayload(payload: SavePayload): EncodedPayload {
|
|
96
100
|
try {
|
|
101
|
+
assertArrayLengths(payload, MAX_SAVE_ARRAY_LENGTH);
|
|
97
102
|
return {
|
|
98
103
|
formatVersion: SAVE_FORMAT_VERSION,
|
|
99
104
|
data: serialize({
|
|
@@ -211,6 +216,7 @@ export function decodePayload(encoded: unknown): SavePayload {
|
|
|
211
216
|
if (typeof data !== 'string') throw invalid();
|
|
212
217
|
const body = deserialize(migrate(formatVersion, data));
|
|
213
218
|
if (!isPayload(body)) throw invalid();
|
|
219
|
+
assertArrayLengths(body, MAX_SAVE_ARRAY_LENGTH);
|
|
214
220
|
assertTrustedWatchers(
|
|
215
221
|
(body.watchers ?? []) as NonNullable<SavePayload['watchers']>,
|
|
216
222
|
);
|