@rohal12/spindle 0.59.28 → 0.59.29
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 +163 -33
- package/dist/pkg/story-variables.js +245 -152
- package/package.json +1 -1
- package/src/code-check.ts +104 -8
- package/src/components/macros/Widget.tsx +2 -0
- package/src/components/macros/arg-utils.ts +58 -21
- package/src/index.tsx +2 -2
- package/src/store.ts +27 -1
- package/src/story-api.ts +2 -1
- package/src/tooling.ts +16 -3
- package/src/widgets/register-widget-def.ts +7 -8
- package/src/widgets/widget-def.ts +41 -4
- package/src/widgets/widget-registry.ts +14 -0
package/dist/pkg/headless.js
CHANGED
|
@@ -10128,18 +10128,29 @@ var widgets = new NameMap();
|
|
|
10128
10128
|
* that no namespace can hold (`@__proto__`) throws a TypeError and the
|
|
10129
10129
|
* widget is not registered.
|
|
10130
10130
|
*/
|
|
10131
|
-
function registerWidget(name, bodyAST, params, isBlock = false) {
|
|
10131
|
+
function registerWidget(name, bodyAST, params, isBlock = false, holds = {}) {
|
|
10132
10132
|
const filteredParams = params.filter((p) => p !== "@children");
|
|
10133
10133
|
for (const param of filteredParams) if (param.startsWith("@")) checkVariableName(param.slice(1), param);
|
|
10134
10134
|
widgets.set(name, {
|
|
10135
10135
|
body: bodyAST,
|
|
10136
10136
|
params: filteredParams,
|
|
10137
|
+
holds,
|
|
10137
10138
|
isBlock
|
|
10138
10139
|
});
|
|
10139
10140
|
}
|
|
10140
10141
|
function getWidget(name) {
|
|
10141
10142
|
return widgets.get(name);
|
|
10142
10143
|
}
|
|
10144
|
+
/** The widgets as `parameterLookup` reads them (see MacroParameters). */
|
|
10145
|
+
function widgetMacros() {
|
|
10146
|
+
return [...widgets].map(([name, { params, holds }]) => ({
|
|
10147
|
+
name,
|
|
10148
|
+
widget: {
|
|
10149
|
+
params,
|
|
10150
|
+
holds
|
|
10151
|
+
}
|
|
10152
|
+
}));
|
|
10153
|
+
}
|
|
10143
10154
|
//#endregion
|
|
10144
10155
|
//#region src/utils/parse-delay.ts
|
|
10145
10156
|
/**
|
|
@@ -10336,19 +10347,19 @@ function topLevelIndices(src, isSeparator) {
|
|
|
10336
10347
|
return indices;
|
|
10337
10348
|
}
|
|
10338
10349
|
/**
|
|
10339
|
-
*
|
|
10340
|
-
*
|
|
10350
|
+
* The segments of `src` between its depth-0 separators (see
|
|
10351
|
+
* `topLevelIndices`), as spans: untrimmed, and empty segments are kept, so
|
|
10341
10352
|
* `n` separators always give `n + 1` segments.
|
|
10342
10353
|
*/
|
|
10343
|
-
function
|
|
10344
|
-
const
|
|
10354
|
+
function topLevelSpans(src, isSeparator) {
|
|
10355
|
+
const spans = [];
|
|
10345
10356
|
let from = 0;
|
|
10346
10357
|
for (const i of topLevelIndices(src, isSeparator)) {
|
|
10347
|
-
|
|
10358
|
+
spans.push([from, i]);
|
|
10348
10359
|
from = i + 1;
|
|
10349
10360
|
}
|
|
10350
|
-
|
|
10351
|
-
return
|
|
10361
|
+
spans.push([from, src.length]);
|
|
10362
|
+
return spans;
|
|
10352
10363
|
}
|
|
10353
10364
|
/**
|
|
10354
10365
|
* Check whether a whitespace-delimited token looks like a standalone value
|
|
@@ -10373,29 +10384,44 @@ function startsLikeValue(token) {
|
|
|
10373
10384
|
* Try to split a raw string on whitespace at depth 0 (respecting strings,
|
|
10374
10385
|
* template literals, parentheses, brackets, and braces). Each resulting token
|
|
10375
10386
|
* must pass `isStandaloneValue()` or the split is rejected and `null` is
|
|
10376
|
-
* returned.
|
|
10387
|
+
* returned. The spans are those of the tokens in `raw`.
|
|
10377
10388
|
*/
|
|
10378
10389
|
function trySplitOnWhitespace(raw) {
|
|
10379
|
-
const args =
|
|
10390
|
+
const args = topLevelSpans(raw, isWhitespace).filter(([a, b]) => b > a);
|
|
10380
10391
|
if (args.length < 2) return null;
|
|
10381
|
-
for (const
|
|
10392
|
+
for (const [a, b] of args) if (!isStandaloneValue(raw.slice(a, b))) return null;
|
|
10382
10393
|
return args;
|
|
10383
10394
|
}
|
|
10395
|
+
/** `span` less the whitespace at its ends. */
|
|
10396
|
+
function trimmed$1(src, [start, end]) {
|
|
10397
|
+
const text = src.slice(start, end);
|
|
10398
|
+
const lead = text.length - text.trimStart().length;
|
|
10399
|
+
return [start + lead, Math.max(start + lead, end - (text.length - text.trimEnd().length))];
|
|
10400
|
+
}
|
|
10384
10401
|
/**
|
|
10385
|
-
*
|
|
10386
|
-
*
|
|
10387
|
-
* string literals separated by whitespace (e.g. `"Label" "target"`).
|
|
10402
|
+
* Where the arguments in `raw` are written (see `splitArgs`), as spans of
|
|
10403
|
+
* `raw`.
|
|
10388
10404
|
*/
|
|
10389
|
-
function
|
|
10390
|
-
const args =
|
|
10405
|
+
function splitArgSpans(raw) {
|
|
10406
|
+
const args = topLevelSpans(raw, (ch) => ch === ",").map((span) => trimmed$1(raw, span));
|
|
10391
10407
|
const hasComma = args.length > 1;
|
|
10392
|
-
|
|
10408
|
+
const last = args[args.length - 1];
|
|
10409
|
+
if (last[0] === last[1]) args.pop();
|
|
10393
10410
|
if (!hasComma && args.length === 1) {
|
|
10394
|
-
const
|
|
10395
|
-
|
|
10411
|
+
const [start, end] = args[0];
|
|
10412
|
+
const split = trySplitOnWhitespace(raw.slice(start, end));
|
|
10413
|
+
if (split) return split.map(([a, b]) => [start + a, start + b]);
|
|
10396
10414
|
}
|
|
10397
10415
|
return args;
|
|
10398
10416
|
}
|
|
10417
|
+
/**
|
|
10418
|
+
* Split rawArgs by commas, respecting parentheses, brackets, braces, and
|
|
10419
|
+
* strings. When no top-level commas are present, also supports adjacent quoted
|
|
10420
|
+
* string literals separated by whitespace (e.g. `"Label" "target"`).
|
|
10421
|
+
*/
|
|
10422
|
+
function splitArgs(raw) {
|
|
10423
|
+
return splitArgSpans(raw).map(([a, b]) => raw.slice(a, b));
|
|
10424
|
+
}
|
|
10399
10425
|
//#endregion
|
|
10400
10426
|
//#region src/components/macros/macro-args.ts
|
|
10401
10427
|
/**
|
|
@@ -10764,6 +10790,18 @@ var CONDITION_MACROS = /* @__PURE__ */ new Set([
|
|
|
10764
10790
|
/** Block macros whose body is the name of a passage. */
|
|
10765
10791
|
var PASSAGE_BODIES = /* @__PURE__ */ new Set(["dialog"]);
|
|
10766
10792
|
/**
|
|
10793
|
+
* The parameters of a widget given its `@` parameters and what they declare
|
|
10794
|
+
* to hold, or none when it declares nothing (a call is then not read by
|
|
10795
|
+
* parameter).
|
|
10796
|
+
*/
|
|
10797
|
+
function widgetParameters(params, holds = {}) {
|
|
10798
|
+
if (!params.some((param) => hasOwn(holds, param))) return void 0;
|
|
10799
|
+
return params.map((name) => ({
|
|
10800
|
+
name,
|
|
10801
|
+
...hasOwn(holds, name) ? { holds: holds[name] } : {}
|
|
10802
|
+
}));
|
|
10803
|
+
}
|
|
10804
|
+
/**
|
|
10767
10805
|
* `params` with each `string` and `text` parameter (options too) saying
|
|
10768
10806
|
* what it holds: what it declares, else `holds`.
|
|
10769
10807
|
*/
|
|
@@ -10785,11 +10823,17 @@ function withHolds(params, holds) {
|
|
|
10785
10823
|
* collection and tooling read arguments by. Each `string` and `text`
|
|
10786
10824
|
* parameter says what it holds: what it declares, else `markup` for a macro
|
|
10787
10825
|
* with `interpolate` (which can resolve markup) and `text` for any other.
|
|
10826
|
+
* A widget among `macros` says what its parameters hold (see ParametersOf).
|
|
10788
10827
|
*/
|
|
10789
10828
|
function parameterLookup(macros) {
|
|
10790
10829
|
const parameters = new NameMap();
|
|
10791
|
-
|
|
10792
|
-
|
|
10830
|
+
const widgets = new NameMap();
|
|
10831
|
+
for (const { name, parameters: params, interpolate, widget } of macros) {
|
|
10832
|
+
if (params) parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
|
|
10833
|
+
const declared = widget && widgetParameters(widget.params, widget.holds);
|
|
10834
|
+
if (declared) widgets.set(name, declared);
|
|
10835
|
+
}
|
|
10836
|
+
const lookup = (name) => {
|
|
10793
10837
|
let params = parameters.get(name);
|
|
10794
10838
|
if (!params) {
|
|
10795
10839
|
const sub = subMacroParameters(name);
|
|
@@ -10797,6 +10841,8 @@ function parameterLookup(macros) {
|
|
|
10797
10841
|
}
|
|
10798
10842
|
return params;
|
|
10799
10843
|
};
|
|
10844
|
+
if (widgets.size > 0) lookup.widget = (name) => widgets.get(name);
|
|
10845
|
+
return lookup;
|
|
10800
10846
|
}
|
|
10801
10847
|
/** What a `string` or `text` argument holds when its parameter doesn't say. */
|
|
10802
10848
|
var holdsByDefault = (interpolate) => interpolate ? "markup" : "text";
|
|
@@ -10899,6 +10945,8 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
10899
10945
|
}
|
|
10900
10946
|
const params = parametersOf(name);
|
|
10901
10947
|
if (params) yield* argPieces(args, argsAt, params, token.name, label);
|
|
10948
|
+
const widget = parametersOf.widget?.(name);
|
|
10949
|
+
if (widget) yield* widgetPieces(args, argsAt, widget, token.name, label);
|
|
10902
10950
|
}
|
|
10903
10951
|
}
|
|
10904
10952
|
}
|
|
@@ -10990,7 +11038,6 @@ function escapeOffsets(raw, at, value) {
|
|
|
10990
11038
|
}
|
|
10991
11039
|
/** The code and text in the arguments `args` (at `offset`) of `macro`. */
|
|
10992
11040
|
function* argPieces(args, offset, params, macro, label) {
|
|
10993
|
-
const name = macro.toLowerCase();
|
|
10994
11041
|
let values;
|
|
10995
11042
|
const spans = /* @__PURE__ */ new Map();
|
|
10996
11043
|
try {
|
|
@@ -11003,10 +11050,44 @@ function* argPieces(args, offset, params, macro, label) {
|
|
|
11003
11050
|
offset,
|
|
11004
11051
|
length: args.length,
|
|
11005
11052
|
label,
|
|
11006
|
-
macro:
|
|
11053
|
+
macro: macro.toLowerCase()
|
|
11007
11054
|
};
|
|
11008
11055
|
return;
|
|
11009
11056
|
}
|
|
11057
|
+
yield* valuePieces(args, offset, params, values, spans, macro, label);
|
|
11058
|
+
}
|
|
11059
|
+
/**
|
|
11060
|
+
* The code and text in the arguments `args` (at `offset`) of the widget
|
|
11061
|
+
* `macro`, the ones whose parameter declares what it holds. Such an argument
|
|
11062
|
+
* is checked when it is one quoted string, as a `string` parameter with that
|
|
11063
|
+
* `holds` of a macro is; any other expression is its value at run time.
|
|
11064
|
+
*/
|
|
11065
|
+
function* widgetPieces(args, offset, params, macro, label) {
|
|
11066
|
+
const written = splitArgSpans(args);
|
|
11067
|
+
const declared = [];
|
|
11068
|
+
const values = {};
|
|
11069
|
+
const spans = /* @__PURE__ */ new Map();
|
|
11070
|
+
params.forEach(({ name, holds }, i) => {
|
|
11071
|
+
const span = written[i];
|
|
11072
|
+
const value = span && stringLiteralValue(args.slice(...span));
|
|
11073
|
+
if (!holds || value == null) return;
|
|
11074
|
+
const param = {
|
|
11075
|
+
name,
|
|
11076
|
+
type: "string",
|
|
11077
|
+
holds
|
|
11078
|
+
};
|
|
11079
|
+
declared.push(param);
|
|
11080
|
+
values[name] = value;
|
|
11081
|
+
spans.set(param, span);
|
|
11082
|
+
});
|
|
11083
|
+
yield* valuePieces(args, offset, declared, values, spans, macro, label);
|
|
11084
|
+
}
|
|
11085
|
+
/**
|
|
11086
|
+
* The pieces of the `params` of `macro` with the `values` read from `args`
|
|
11087
|
+
* (at `offset`), whose `spans` tell where each was written.
|
|
11088
|
+
*/
|
|
11089
|
+
function* valuePieces(args, offset, params, values, spans, macro, label) {
|
|
11090
|
+
const name = macro.toLowerCase();
|
|
11010
11091
|
/** The pieces of the parameters `list`, with their values in `from`. */
|
|
11011
11092
|
function* visit(list, from) {
|
|
11012
11093
|
for (const param of list) {
|
|
@@ -11341,15 +11422,39 @@ var WIDGET_PARAMETERS = [{
|
|
|
11341
11422
|
/**
|
|
11342
11423
|
* The widget a {widget} definition's arguments declare: its name and its
|
|
11343
11424
|
* parameters, the words after it that start with `@` (docs/widgets.md),
|
|
11344
|
-
* separated by spaces or commas (`@a, @b`).
|
|
11345
|
-
*
|
|
11425
|
+
* separated by spaces or commas (`@a, @b`). A parameter may say what its
|
|
11426
|
+
* argument holds after a colon (`@target:passage`, one of the `holds` of a
|
|
11427
|
+
* macro parameter; another word is a `badHolds`). Other words, such as
|
|
11428
|
+
* `$name`, are not parameters.
|
|
11346
11429
|
*/
|
|
11347
11430
|
function widgetDef({ name = "", parameters = "" }) {
|
|
11431
|
+
const params = [];
|
|
11432
|
+
const holds = {};
|
|
11433
|
+
const badHolds = [];
|
|
11434
|
+
for (const word of parameters.split(/[\s,]+/)) {
|
|
11435
|
+
if (!word.startsWith("@")) continue;
|
|
11436
|
+
const [param = word, written] = word.split(/:(.*)/s);
|
|
11437
|
+
params.push(param);
|
|
11438
|
+
if (written === void 0) continue;
|
|
11439
|
+
if (STRING_HOLDS.includes(written)) holds[param] = written;
|
|
11440
|
+
else badHolds.push({
|
|
11441
|
+
param,
|
|
11442
|
+
written
|
|
11443
|
+
});
|
|
11444
|
+
}
|
|
11348
11445
|
return {
|
|
11349
11446
|
name,
|
|
11350
|
-
params
|
|
11447
|
+
params,
|
|
11448
|
+
...Object.keys(holds).length > 0 ? { holds } : {},
|
|
11449
|
+
...badHolds.length > 0 ? { badHolds } : {}
|
|
11351
11450
|
};
|
|
11352
11451
|
}
|
|
11452
|
+
/** Throw if a parameter of `def` declares what it holds wrongly. */
|
|
11453
|
+
function checkWidgetHolds({ name, badHolds = [] }) {
|
|
11454
|
+
const [bad] = badHolds;
|
|
11455
|
+
if (!bad) return;
|
|
11456
|
+
throw new Error(`spindle: The widget parameter ${bad.param} of {widget "${name}"} holds the unknown ${JSON.stringify(bad.written)}. Write one of ${STRING_HOLDS.join(", ")} after the colon (see docs/widgets.md#what-an-argument-holds).`);
|
|
11457
|
+
}
|
|
11353
11458
|
/** Read the arguments of a {widget} definition (see widgetDef). */
|
|
11354
11459
|
function parseWidgetDef(rawArgs) {
|
|
11355
11460
|
return widgetDef(parseMacroArgs(rawArgs, WIDGET_PARAMETERS));
|
|
@@ -11426,12 +11531,14 @@ function blockWidgetNames(passages, parametersOf = registeredParameters) {
|
|
|
11426
11531
|
* Register the widget a definition declares, with its body. Widgets whose
|
|
11427
11532
|
* body renders {@children} take a closing tag, so they are registered as
|
|
11428
11533
|
* block macros too: passages parsed later nest their content. A parameter
|
|
11429
|
-
* that no namespace can hold throws (see
|
|
11430
|
-
* nothing.
|
|
11534
|
+
* that no namespace can hold or declares what it holds wrongly throws (see
|
|
11535
|
+
* registerWidget and checkWidgetHolds), registering nothing.
|
|
11431
11536
|
*/
|
|
11432
|
-
function registerWidgetDef(
|
|
11537
|
+
function registerWidgetDef(def, body) {
|
|
11538
|
+
checkWidgetHolds(def);
|
|
11539
|
+
const { name, params, holds } = def;
|
|
11433
11540
|
const isBlock = astContainsChildren(body);
|
|
11434
|
-
registerWidget(name, body, params, isBlock);
|
|
11541
|
+
registerWidget(name, body, params, isBlock, holds);
|
|
11435
11542
|
if (isBlock) registerBlockMacro(name);
|
|
11436
11543
|
}
|
|
11437
11544
|
/**
|
|
@@ -17960,6 +18067,12 @@ function loadedEntryMoment(payload) {
|
|
|
17960
18067
|
if (typeof moment.variables !== "object" || moment.variables === null) return;
|
|
17961
18068
|
return moment;
|
|
17962
18069
|
}
|
|
18070
|
+
/** The variable paths `Story.set` writes while a save's hooks run. */
|
|
18071
|
+
var hookWrites = null;
|
|
18072
|
+
/** Note that `path` of the variables was written (see keepHookWrites). */
|
|
18073
|
+
function recordHookWrite(path) {
|
|
18074
|
+
hookWrites?.set(pathKey(path), path);
|
|
18075
|
+
}
|
|
17963
18076
|
/**
|
|
17964
18077
|
* Write the changes between `before` and `after` (the live variables around
|
|
17965
18078
|
* the `beforesave` hooks) into the payload's snapshot of the saved moment. A
|
|
@@ -17973,10 +18086,22 @@ function loadedEntryMoment(payload) {
|
|
|
17973
18086
|
* snapshot's own object of it (#302). Only the payload's copy changes: the
|
|
17974
18087
|
* live history keeps the recorded snapshot (#159).
|
|
17975
18088
|
*/
|
|
17976
|
-
function keepHookWrites(payload, before, after) {
|
|
18089
|
+
function keepHookWrites(payload, before, after, written) {
|
|
17977
18090
|
const moment = payload.history[payload.historyIndex];
|
|
17978
18091
|
if (!moment) return;
|
|
17979
18092
|
const changes = changesBetween(before, after, true);
|
|
18093
|
+
const keys = new Set(changes.map((c) => pathKey(c.path)));
|
|
18094
|
+
for (const path of written) {
|
|
18095
|
+
if (underChange(path, keys)) continue;
|
|
18096
|
+
keys.add(pathKey(path));
|
|
18097
|
+
const value = getByPath(after, path);
|
|
18098
|
+
if (deepEqual(getByPath(moment.variables, path), value)) continue;
|
|
18099
|
+
changes.push({
|
|
18100
|
+
path,
|
|
18101
|
+
deleted: false,
|
|
18102
|
+
value
|
|
18103
|
+
});
|
|
18104
|
+
}
|
|
17980
18105
|
if (changes.length === 0) return;
|
|
17981
18106
|
const work = deepClone(moment.variables);
|
|
17982
18107
|
const seen = existingObjects(locateObjects(after, changes), work);
|
|
@@ -18493,9 +18618,12 @@ var useStoryStore = create()(storyStore((set, get) => ({
|
|
|
18493
18618
|
},
|
|
18494
18619
|
beginSave: () => {
|
|
18495
18620
|
const before = get().variables;
|
|
18621
|
+
const writes = /* @__PURE__ */ new Map();
|
|
18622
|
+
hookWrites = writes;
|
|
18496
18623
|
return () => {
|
|
18624
|
+
if (hookWrites === writes) hookWrites = null;
|
|
18497
18625
|
const payload = get().getSavePayload();
|
|
18498
|
-
keepHookWrites(payload, before, get().variables);
|
|
18626
|
+
keepHookWrites(payload, before, get().variables, [...writes.values()]);
|
|
18499
18627
|
return payload;
|
|
18500
18628
|
};
|
|
18501
18629
|
},
|
|
@@ -19772,6 +19900,7 @@ function createStoryAPI() {
|
|
|
19772
19900
|
for (const [name] of entries) {
|
|
19773
19901
|
const { isTransient, key } = parseName(name);
|
|
19774
19902
|
warnIfUndeclared(isTransient, key);
|
|
19903
|
+
if (!isTransient) recordHookWrite(key.split("."));
|
|
19775
19904
|
}
|
|
19776
19905
|
if (entries.some(([k]) => k.includes("."))) {
|
|
19777
19906
|
mutateState((work, adopt) => {
|
|
@@ -22442,6 +22571,7 @@ defineMacro({
|
|
|
22442
22571
|
try {
|
|
22443
22572
|
const def = widgetDef(ctx.args);
|
|
22444
22573
|
for (const param of def.params) checkVariableName(param.slice(1), param);
|
|
22574
|
+
checkWidgetHolds(def);
|
|
22445
22575
|
parsed = def;
|
|
22446
22576
|
} catch (err) {
|
|
22447
22577
|
error = err;
|
|
@@ -23513,7 +23643,7 @@ function markupErrors(storyData, only) {
|
|
|
23513
23643
|
return validateMarkup(storyData.passages.values(), {
|
|
23514
23644
|
isKnownMacro: (name) => !!getMacro(name) || isSubMacro(name) || !!getWidget(name),
|
|
23515
23645
|
macroNames: macros.map((m) => m.name),
|
|
23516
|
-
parametersOf: parameterLookup(macros),
|
|
23646
|
+
parametersOf: parameterLookup([...macros, ...widgetMacros()]),
|
|
23517
23647
|
only
|
|
23518
23648
|
}).map(formatDiagnostic);
|
|
23519
23649
|
}
|