@rohal12/spindle 0.64.16 → 0.64.18
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 +147 -38
- package/package.json +1 -1
- package/src/components/macros/PassageDisplay.tsx +16 -8
- package/src/event-emitter.ts +9 -2
- package/src/markup/render.tsx +101 -17
- package/src/store.ts +6 -5
- package/src/story-api.ts +48 -0
- package/src/twine-editor/highlight.ts +27 -13
- package/src/types-drift-check.ts +24 -1
- package/src/utils/element-visibility.ts +2 -2
- package/types/index.d.ts +43 -2
package/dist/pkg/headless.js
CHANGED
|
@@ -76,17 +76,17 @@ function* composedElements(root) {
|
|
|
76
76
|
}
|
|
77
77
|
/** Elements whose content is never text the reader reads. */
|
|
78
78
|
var NOT_TEXT = /* @__PURE__ */ new Set([
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
"
|
|
82
|
-
"
|
|
79
|
+
"script",
|
|
80
|
+
"style",
|
|
81
|
+
"template",
|
|
82
|
+
"noscript"
|
|
83
83
|
]);
|
|
84
84
|
/**
|
|
85
85
|
* Whether the element hides its whole subtree, including stylesheet rules.
|
|
86
86
|
* This needs computed styles but no layout, so it also works headlessly.
|
|
87
87
|
*/
|
|
88
88
|
function isHidden(el) {
|
|
89
|
-
return el.hasAttribute("hidden") || el.getAttribute("aria-hidden") === "true" || /(^|;)\s*display\s*:\s*none\b/i.test(el.getAttribute("style") ?? "") || el.ownerDocument.defaultView?.getComputedStyle(el).display === "none" || NOT_TEXT.has(el.
|
|
89
|
+
return el.hasAttribute("hidden") || el.getAttribute("aria-hidden") === "true" || /(^|;)\s*display\s*:\s*none\b/i.test(el.getAttribute("style") ?? "") || el.ownerDocument.defaultView?.getComputedStyle(el).display === "none" || NOT_TEXT.has(el.localName.toLowerCase()) || el.parentElement?.tagName === "DETAILS" && !el.parentElement.hasAttribute("open") && el !== Array.from(el.parentElement.children).find((c) => c.tagName === "SUMMARY");
|
|
90
90
|
}
|
|
91
91
|
/**
|
|
92
92
|
* Whether `el` is in a closed `<details>`, other than in the summary that
|
|
@@ -16529,6 +16529,31 @@ function HtmlAnnotation({ attributes, children }) {
|
|
|
16529
16529
|
}
|
|
16530
16530
|
}, isHtmlEncoding(attributes.encoding) ? void 0 : children), container && isHtmlEncoding(attributes.encoding) && createPortal(children, container)] });
|
|
16531
16531
|
}
|
|
16532
|
+
// src/components/macros/include-args.ts
|
|
16533
|
+
/**
|
|
16534
|
+
* The parameters of `{include}`. A standalone `inline` flag only counts
|
|
16535
|
+
* outside quotes and brackets, so passage names and expressions containing
|
|
16536
|
+
* the word stay intact (#201).
|
|
16537
|
+
*/
|
|
16538
|
+
var INCLUDE_PARAMETERS = [{
|
|
16539
|
+
name: "inline",
|
|
16540
|
+
type: "flag"
|
|
16541
|
+
}, {
|
|
16542
|
+
name: "passage",
|
|
16543
|
+
type: "passage",
|
|
16544
|
+
required: true
|
|
16545
|
+
}];
|
|
16546
|
+
/**
|
|
16547
|
+
* The arguments of `{include}`: whether it has the `inline` flag, as its
|
|
16548
|
+
* first or last word, and the passage expression that is left.
|
|
16549
|
+
*/
|
|
16550
|
+
function splitIncludeFlag(rawArgs) {
|
|
16551
|
+
const { inline, passage } = parseMacroArgs(rawArgs, INCLUDE_PARAMETERS);
|
|
16552
|
+
return {
|
|
16553
|
+
inline,
|
|
16554
|
+
passage
|
|
16555
|
+
};
|
|
16556
|
+
}
|
|
16532
16557
|
// src/markup/render.tsx
|
|
16533
16558
|
var defaultUpdater = {
|
|
16534
16559
|
update: (key) => {
|
|
@@ -17079,16 +17104,79 @@ var LEADING_PARTS = {
|
|
|
17079
17104
|
details: "summary",
|
|
17080
17105
|
fieldset: "legend"
|
|
17081
17106
|
};
|
|
17107
|
+
var SETUP_MACROS = /* @__PURE__ */ new Set([
|
|
17108
|
+
"set",
|
|
17109
|
+
"unset",
|
|
17110
|
+
"do",
|
|
17111
|
+
"widget"
|
|
17112
|
+
]);
|
|
17113
|
+
function isSetupMacro(node) {
|
|
17114
|
+
return SETUP_MACROS.has(node.name.toLowerCase()) && !getWidget(node.name) && getMacroParameters.source(node.name) === "builtin";
|
|
17115
|
+
}
|
|
17116
|
+
/**
|
|
17117
|
+
* Only promote bodies whose first output is a literal part (or nothing).
|
|
17118
|
+
* Nested/dynamic macros stay at the existing boundary: inspecting their
|
|
17119
|
+
* possible output recursively can be expensive and cannot predict effects.
|
|
17120
|
+
*/
|
|
17121
|
+
function hasKnownLeadingPart(node, part) {
|
|
17122
|
+
if (node.className || node.id) return false;
|
|
17123
|
+
const widget = getWidget(node.name);
|
|
17124
|
+
let bodies;
|
|
17125
|
+
if (widget) bodies = [widget.body];
|
|
17126
|
+
else if (getMacroParameters.source(node.name) === "builtin") {
|
|
17127
|
+
const name = node.name.toLowerCase();
|
|
17128
|
+
if (name === "if" || name === "switch") {
|
|
17129
|
+
const branches = node.branches ?? [];
|
|
17130
|
+
if (branches.some((branch) => branch.className || branch.id)) return false;
|
|
17131
|
+
bodies = branches.map((branch) => branch.children);
|
|
17132
|
+
} else if (name === "for" || name === "nobr") bodies = [node.children];
|
|
17133
|
+
else if (name === "include") try {
|
|
17134
|
+
const target = passageTarget(splitIncludeFlag(node.rawArgs).passage ?? "");
|
|
17135
|
+
if (target.kind !== "name") return false;
|
|
17136
|
+
const passage = useStoryStore.getState().storyData?.passages.get(target.name);
|
|
17137
|
+
if (!passage) return false;
|
|
17138
|
+
bodies = [parseMarkup(passage.content)];
|
|
17139
|
+
} catch {
|
|
17140
|
+
return false;
|
|
17141
|
+
}
|
|
17142
|
+
else return false;
|
|
17143
|
+
} else return false;
|
|
17144
|
+
let found = false;
|
|
17145
|
+
for (const body of bodies) {
|
|
17146
|
+
if (body.some((n) => n.type === "text" && (n.value.trim() !== "" || BLANK_LINE_RE.test(n.value)))) return false;
|
|
17147
|
+
const first = body.find((n) => !isBlankText(n) && !(n.type === "macro" && isSetupMacro(n)));
|
|
17148
|
+
if (!first) continue;
|
|
17149
|
+
if (first.type !== "html" || first.tag.toLowerCase() !== part) return false;
|
|
17150
|
+
found = true;
|
|
17151
|
+
}
|
|
17152
|
+
return found;
|
|
17153
|
+
}
|
|
17082
17154
|
/**
|
|
17083
|
-
* The
|
|
17084
|
-
*
|
|
17085
|
-
*
|
|
17155
|
+
* The end of the leading part of the `tag` container: preceding macros may
|
|
17156
|
+
* render nothing (setup), or produce the part themselves (#474, #739).
|
|
17157
|
+
* Keep the prefix through a literal part direct. Without one, preserve the
|
|
17158
|
+
* first macro boundary, extending past setup only for a known native part.
|
|
17086
17159
|
*/
|
|
17087
|
-
function
|
|
17160
|
+
function leadingPartEnd(tag, nodes) {
|
|
17088
17161
|
const part = LEADING_PARTS[tag];
|
|
17089
|
-
|
|
17090
|
-
|
|
17091
|
-
|
|
17162
|
+
if (!part) return 0;
|
|
17163
|
+
let end = 0;
|
|
17164
|
+
let setup = true;
|
|
17165
|
+
for (let at = 0; at < nodes.length; at++) {
|
|
17166
|
+
const node = nodes[at];
|
|
17167
|
+
if (isBlankText(node)) continue;
|
|
17168
|
+
if (node.type === "macro") {
|
|
17169
|
+
if (end === 0) end = at + 1;
|
|
17170
|
+
if (setup && !isSetupMacro(node)) {
|
|
17171
|
+
if (end < at + 1 && hasKnownLeadingPart(node, part)) end = at + 1;
|
|
17172
|
+
setup = false;
|
|
17173
|
+
}
|
|
17174
|
+
continue;
|
|
17175
|
+
}
|
|
17176
|
+
if (node.type === "html" && node.tag.toLowerCase() === part) return at + 1;
|
|
17177
|
+
break;
|
|
17178
|
+
}
|
|
17179
|
+
return end;
|
|
17092
17180
|
}
|
|
17093
17181
|
function HtmlNodeRenderer({ node }) {
|
|
17094
17182
|
const scope = useTextScope();
|
|
@@ -17119,18 +17207,14 @@ function HtmlNodeRenderer({ node }) {
|
|
|
17119
17207
|
if (RAW_TEXT_ELEMENTS.has(tag)) children = node.children.map((n) => n.type === "text" ? n.value : renderSingleNode(n));
|
|
17120
17208
|
else if (rawContent || isRawRoot) children = renderInlineNodes(node.children);
|
|
17121
17209
|
else {
|
|
17122
|
-
const
|
|
17210
|
+
const leadEnd = leadingPartEnd(tag, node.children);
|
|
17123
17211
|
const body = (nodes) => renderNodes(nodes, {
|
|
17124
17212
|
nobr,
|
|
17125
17213
|
locals,
|
|
17126
17214
|
inline: isInline
|
|
17127
17215
|
});
|
|
17128
17216
|
structural = STRUCTURAL_ELEMENTS.has(tag) && node.children.every((n) => n.type !== "text" || isBlankText(n));
|
|
17129
|
-
children = structural ? node.children.filter((n) => n.type !== "text").map((n) => renderSingleNode(n)) :
|
|
17130
|
-
body(node.children.slice(0, leadAt)),
|
|
17131
|
-
renderSingleNode(node.children[leadAt]),
|
|
17132
|
-
body(node.children.slice(leadAt + 1))
|
|
17133
|
-
] });
|
|
17217
|
+
children = structural ? node.children.filter((n) => n.type !== "text").map((n) => renderSingleNode(n)) : leadEnd === 0 ? body(node.children) : /* @__PURE__ */ jsxs(Fragment$1, { children: [renderInlineNodes(node.children.slice(0, leadEnd)), body(node.children.slice(leadEnd))] });
|
|
17134
17218
|
if (isInline !== parentInline) children = /* @__PURE__ */ jsx(InlineContext.Provider, {
|
|
17135
17219
|
value: isInline,
|
|
17136
17220
|
children
|
|
@@ -19521,7 +19605,8 @@ function moveInHistory(get, set, step) {
|
|
|
19521
19605
|
const previousPassage = get().currentPassage;
|
|
19522
19606
|
const targetPassage = history[target].passage;
|
|
19523
19607
|
const navigation = get().navigationId;
|
|
19524
|
-
|
|
19608
|
+
const kind = step < 0 ? "back" : "forward";
|
|
19609
|
+
emit("beforenavigate", targetPassage, kind);
|
|
19525
19610
|
if (get().navigationId !== navigation) return;
|
|
19526
19611
|
target = get().historyIndex + step;
|
|
19527
19612
|
if (target < 0 || target >= get().history.length) return;
|
|
@@ -19537,7 +19622,7 @@ function moveInHistory(get, set, step) {
|
|
|
19537
19622
|
lastNavigationVars = get().variables;
|
|
19538
19623
|
restorePRNGFromMoment(get().history[get().historyIndex]);
|
|
19539
19624
|
keepDrawsSoFar();
|
|
19540
|
-
persistSessionThen(get, () => emit("afternavigate", targetPassage, previousPassage));
|
|
19625
|
+
persistSessionThen(get, () => emit("afternavigate", targetPassage, previousPassage, kind));
|
|
19541
19626
|
}
|
|
19542
19627
|
/**
|
|
19543
19628
|
* Record the state StoryInit left behind as the start moment: its variable
|
|
@@ -20096,7 +20181,7 @@ var useStoryStore = create()(storyStore((set, get) => ({
|
|
|
20096
20181
|
}
|
|
20097
20182
|
const previousPassage = get().currentPassage;
|
|
20098
20183
|
const navigation = get().navigationId;
|
|
20099
|
-
if (replacesState(() => emit("beforenavigate", passageName)) || get().navigationId !== navigation) return;
|
|
20184
|
+
if (replacesState(() => emit("beforenavigate", passageName, "navigate")) || get().navigationId !== navigation) return;
|
|
20100
20185
|
const patchEntry = computeVarPatches(lastNavigationVars, get().variables);
|
|
20101
20186
|
set((state) => {
|
|
20102
20187
|
state.temporary = createNamespace();
|
|
@@ -20131,7 +20216,7 @@ var useStoryStore = create()(storyStore((set, get) => ({
|
|
|
20131
20216
|
finishEnteredMoment(get, set);
|
|
20132
20217
|
keepDrawsSoFar();
|
|
20133
20218
|
persistSessionThen(get, () => {
|
|
20134
|
-
if (replacesState(() => emit("afternavigate", passageName, previousPassage))) return;
|
|
20219
|
+
if (replacesState(() => emit("afternavigate", passageName, previousPassage, "navigate"))) return;
|
|
20135
20220
|
for (const next of deferred) get().navigate(next);
|
|
20136
20221
|
});
|
|
20137
20222
|
},
|
|
@@ -22012,6 +22097,19 @@ function setOne(draft, name, value) {
|
|
|
22012
22097
|
const { isTransient, key } = parseName(name);
|
|
22013
22098
|
setByPath(isTransient ? draft.transient : draft.variables, key.split("."), value);
|
|
22014
22099
|
}
|
|
22100
|
+
/**
|
|
22101
|
+
* The history index `index` names (the current moment's when left out), or
|
|
22102
|
+
* a RangeError naming the moments the history holds.
|
|
22103
|
+
*/
|
|
22104
|
+
function momentIndex(index) {
|
|
22105
|
+
const { history, historyIndex } = useStoryStore.getState();
|
|
22106
|
+
const at = index ?? historyIndex;
|
|
22107
|
+
if (!Number.isInteger(at) || at < 0 || at >= history.length) {
|
|
22108
|
+
const held = history.length === 0 ? "no moments yet" : `moments 0 to ${history.length - 1}`;
|
|
22109
|
+
throw new RangeError(`spindle: History index ${String(at)} is out of range: the history holds ${held}.`);
|
|
22110
|
+
}
|
|
22111
|
+
return at;
|
|
22112
|
+
}
|
|
22015
22113
|
/** A method calling the store action `name`, as the store holds it then. */
|
|
22016
22114
|
function storeAction(name) {
|
|
22017
22115
|
return ((...args) => useStoryStore.getState()[name](...args));
|
|
@@ -22062,6 +22160,22 @@ function createStoryAPI() {
|
|
|
22062
22160
|
get passage() {
|
|
22063
22161
|
return useStoryStore.getState().currentPassage;
|
|
22064
22162
|
},
|
|
22163
|
+
history: {
|
|
22164
|
+
get length() {
|
|
22165
|
+
return useStoryStore.getState().history.length;
|
|
22166
|
+
},
|
|
22167
|
+
get index() {
|
|
22168
|
+
return useStoryStore.getState().historyIndex;
|
|
22169
|
+
},
|
|
22170
|
+
passage(index) {
|
|
22171
|
+
const at = momentIndex(index);
|
|
22172
|
+
return useStoryStore.getState().history[at].passage;
|
|
22173
|
+
},
|
|
22174
|
+
variables(index) {
|
|
22175
|
+
const at = momentIndex(index);
|
|
22176
|
+
return freeze(useStoryStore.getState().getHistoryVariables(at), true);
|
|
22177
|
+
}
|
|
22178
|
+
},
|
|
22065
22179
|
settings,
|
|
22066
22180
|
registerClass(name, ctor) {
|
|
22067
22181
|
registerClass(name, ctor);
|
|
@@ -23828,7 +23942,7 @@ function installSnapshotSvgStyles(root, ids, marker, targetSheets = [...root.sty
|
|
|
23828
23942
|
}
|
|
23829
23943
|
return result;
|
|
23830
23944
|
};
|
|
23831
|
-
const inserted =
|
|
23945
|
+
const inserted = /* @__PURE__ */ new Map();
|
|
23832
23946
|
const sheets = /* @__PURE__ */ new Set();
|
|
23833
23947
|
const sheetRules = (sheet) => {
|
|
23834
23948
|
if (sheets.has(sheet) || sheet.disabled || !matchMedia(sheet.media.mediaText || "all").matches) return;
|
|
@@ -23838,14 +23952,17 @@ function installSnapshotSvgStyles(root, ids, marker, targetSheets = [...root.sty
|
|
|
23838
23952
|
} catch {}
|
|
23839
23953
|
};
|
|
23840
23954
|
const visit = (parent, base) => {
|
|
23841
|
-
for (
|
|
23955
|
+
for (let at = parent.cssRules.length - 1; at >= 0; at--) {
|
|
23956
|
+
const rule = parent.cssRules[at];
|
|
23842
23957
|
if (snapshotSvgRules.has(rule)) continue;
|
|
23843
23958
|
if (rule instanceof CSSStyleRule) {
|
|
23844
|
-
let index =
|
|
23959
|
+
let index = at + 1;
|
|
23845
23960
|
for (const css of rulesOf([rule], base)) {
|
|
23846
23961
|
parent.insertRule(css, index);
|
|
23847
23962
|
const insertedRule = parent.cssRules[index++];
|
|
23848
|
-
inserted.
|
|
23963
|
+
let rules = inserted.get(parent);
|
|
23964
|
+
if (!rules) inserted.set(parent, rules = /* @__PURE__ */ new Set());
|
|
23965
|
+
rules.add(insertedRule);
|
|
23849
23966
|
snapshotSvgRules.add(insertedRule);
|
|
23850
23967
|
}
|
|
23851
23968
|
} else if (rule instanceof CSSImportRule) {
|
|
@@ -23855,10 +23972,9 @@ function installSnapshotSvgStyles(root, ids, marker, targetSheets = [...root.sty
|
|
|
23855
23972
|
};
|
|
23856
23973
|
for (const sheet of targetSheets) sheetRules(sheet);
|
|
23857
23974
|
return () => {
|
|
23858
|
-
for (const [parent,
|
|
23859
|
-
|
|
23860
|
-
|
|
23861
|
-
snapshotSvgRules.delete(rule);
|
|
23975
|
+
for (const [parent, rules] of inserted) {
|
|
23976
|
+
for (let index = parent.cssRules.length - 1; index >= 0; index--) if (rules.has(parent.cssRules[index])) parent.deleteRule(index);
|
|
23977
|
+
for (const rule of rules) snapshotSvgRules.delete(rule);
|
|
23862
23978
|
}
|
|
23863
23979
|
};
|
|
23864
23980
|
}
|
|
@@ -25760,14 +25876,7 @@ defineMacro({
|
|
|
25760
25876
|
name: "include",
|
|
25761
25877
|
interpolate: true,
|
|
25762
25878
|
merged: true,
|
|
25763
|
-
parameters:
|
|
25764
|
-
name: "inline",
|
|
25765
|
-
type: "flag"
|
|
25766
|
-
}, {
|
|
25767
|
-
name: "passage",
|
|
25768
|
-
type: "passage",
|
|
25769
|
-
required: true
|
|
25770
|
-
}],
|
|
25879
|
+
parameters: INCLUDE_PARAMETERS,
|
|
25771
25880
|
render(_props, ctx) {
|
|
25772
25881
|
const displayOnly = ctx.hooks.useContext(DisplayOnlyContext);
|
|
25773
25882
|
const { storyData } = useStoryFields("storyData");
|
package/package.json
CHANGED
|
@@ -369,7 +369,7 @@ function installSnapshotSvgStyles(
|
|
|
369
369
|
return result;
|
|
370
370
|
};
|
|
371
371
|
type RuleContainer = CSSStyleSheet | CSSGroupingRule;
|
|
372
|
-
const inserted
|
|
372
|
+
const inserted = new Map<RuleContainer, Set<CSSRule>>();
|
|
373
373
|
const sheets = new Set<CSSStyleSheet>();
|
|
374
374
|
const sheetRules = (sheet: CSSStyleSheet) => {
|
|
375
375
|
if (
|
|
@@ -387,14 +387,19 @@ function installSnapshotSvgStyles(
|
|
|
387
387
|
}
|
|
388
388
|
};
|
|
389
389
|
const visit = (parent: RuleContainer, base: string) => {
|
|
390
|
-
|
|
390
|
+
// Work backwards so inserting adjacent aliases leaves the remaining
|
|
391
|
+
// original indexes intact, without copying the live list per rule.
|
|
392
|
+
for (let at = parent.cssRules.length - 1; at >= 0; at--) {
|
|
393
|
+
const rule = parent.cssRules[at]!;
|
|
391
394
|
if (snapshotSvgRules.has(rule)) continue;
|
|
392
395
|
if (rule instanceof CSSStyleRule) {
|
|
393
|
-
let index =
|
|
396
|
+
let index = at + 1;
|
|
394
397
|
for (const css of rulesOf([rule], base)) {
|
|
395
398
|
parent.insertRule(css, index);
|
|
396
399
|
const insertedRule = parent.cssRules[index++]!;
|
|
397
|
-
inserted.
|
|
400
|
+
let rules = inserted.get(parent);
|
|
401
|
+
if (!rules) inserted.set(parent, (rules = new Set()));
|
|
402
|
+
rules.add(insertedRule);
|
|
398
403
|
snapshotSvgRules.add(insertedRule);
|
|
399
404
|
}
|
|
400
405
|
} else if (rule instanceof CSSImportRule) {
|
|
@@ -412,10 +417,13 @@ function installSnapshotSvgStyles(
|
|
|
412
417
|
// including anonymous layers. They match snapshot elements only.
|
|
413
418
|
for (const sheet of targetSheets) sheetRules(sheet);
|
|
414
419
|
return () => {
|
|
415
|
-
for (const [parent,
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
420
|
+
for (const [parent, rules] of inserted) {
|
|
421
|
+
// Rules may have changed during the transition. Match identities in
|
|
422
|
+
// one backwards pass, preserving authored insertions and removals.
|
|
423
|
+
for (let index = parent.cssRules.length - 1; index >= 0; index--) {
|
|
424
|
+
if (rules.has(parent.cssRules[index]!)) parent.deleteRule(index);
|
|
425
|
+
}
|
|
426
|
+
for (const rule of rules) snapshotSvgRules.delete(rule);
|
|
419
427
|
}
|
|
420
428
|
};
|
|
421
429
|
}
|
package/src/event-emitter.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the story moved: `'navigate'` recorded a new history moment (a link,
|
|
3
|
+
* `{goto}`, `Story.goto()`, a watcher's goto); `'back'` and `'forward'`
|
|
4
|
+
* moved through the moments already recorded.
|
|
5
|
+
*/
|
|
6
|
+
export type NavigationKind = 'navigate' | 'back' | 'forward';
|
|
7
|
+
|
|
1
8
|
type EventMap = {
|
|
2
9
|
storyinit: () => void;
|
|
3
10
|
beforerestart: () => void;
|
|
@@ -12,8 +19,8 @@ type EventMap = {
|
|
|
12
19
|
aftersave: (slot: string | undefined) => void;
|
|
13
20
|
beforeload: (slot: string | undefined) => void;
|
|
14
21
|
afterload: (slot: string | undefined) => void;
|
|
15
|
-
beforenavigate: (passageName: string) => void;
|
|
16
|
-
afternavigate: (to: string, from: string) => void;
|
|
22
|
+
beforenavigate: (passageName: string, kind: NavigationKind) => void;
|
|
23
|
+
afternavigate: (to: string, from: string, kind: NavigationKind) => void;
|
|
17
24
|
passagerender: (passage: string, element: HTMLElement) => void;
|
|
18
25
|
dialogrender: (passage: string, element: HTMLElement) => void;
|
|
19
26
|
};
|
package/src/markup/render.tsx
CHANGED
|
@@ -4,7 +4,7 @@ import { VarDisplay } from '../components/macros/VarDisplay';
|
|
|
4
4
|
import { ExprDisplay } from '../components/macros/ExprDisplay';
|
|
5
5
|
import { WidgetInvocation } from '../components/macros/WidgetInvocation';
|
|
6
6
|
import { getWidget } from '../widgets/widget-registry';
|
|
7
|
-
import { getMacro, isSubMacro } from '../registry';
|
|
7
|
+
import { getMacro, getMacroParameters, isSubMacro } from '../registry';
|
|
8
8
|
import { markdownToHtml } from './markdown';
|
|
9
9
|
import { h } from 'preact';
|
|
10
10
|
import type { ASTNode, HtmlNode, MacroNode } from './ast';
|
|
@@ -27,6 +27,10 @@ import { isCodeAttribute } from './code-attributes';
|
|
|
27
27
|
import { errorMessage } from '../utils/error-message';
|
|
28
28
|
import { EMPTY_NAMESPACE } from '../utils/namespace';
|
|
29
29
|
import { authorElement, isHtmlEncoding } from './html-integration';
|
|
30
|
+
import { parseMarkup } from './parse';
|
|
31
|
+
import { useStoryStore } from '../store';
|
|
32
|
+
import { passageTarget } from '../components/macros/macro-args';
|
|
33
|
+
import { splitIncludeFlag } from '../components/macros/include-args';
|
|
30
34
|
|
|
31
35
|
export interface LocalsUpdater {
|
|
32
36
|
update: (key: string, value: unknown) => void;
|
|
@@ -755,21 +759,102 @@ const LEADING_PARTS: Record<string, string> = {
|
|
|
755
759
|
details: 'summary',
|
|
756
760
|
fieldset: 'legend',
|
|
757
761
|
};
|
|
762
|
+
const SETUP_MACROS = new Set(['set', 'unset', 'do', 'widget']);
|
|
763
|
+
|
|
764
|
+
function isSetupMacro(node: MacroNode): boolean {
|
|
765
|
+
return (
|
|
766
|
+
SETUP_MACROS.has(node.name.toLowerCase()) &&
|
|
767
|
+
!getWidget(node.name) &&
|
|
768
|
+
getMacroParameters.source(node.name) === 'builtin'
|
|
769
|
+
);
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
* Only promote bodies whose first output is a literal part (or nothing).
|
|
774
|
+
* Nested/dynamic macros stay at the existing boundary: inspecting their
|
|
775
|
+
* possible output recursively can be expensive and cannot predict effects.
|
|
776
|
+
*/
|
|
777
|
+
function hasKnownLeadingPart(node: MacroNode, part: string): boolean {
|
|
778
|
+
if (node.className || node.id) return false;
|
|
779
|
+
const widget = getWidget(node.name);
|
|
780
|
+
let bodies: ASTNode[][];
|
|
781
|
+
if (widget) {
|
|
782
|
+
bodies = [widget.body];
|
|
783
|
+
} else if (getMacroParameters.source(node.name) === 'builtin') {
|
|
784
|
+
const name = node.name.toLowerCase();
|
|
785
|
+
if (name === 'if' || name === 'switch') {
|
|
786
|
+
const branches = node.branches ?? [];
|
|
787
|
+
if (branches.some((branch) => branch.className || branch.id))
|
|
788
|
+
return false;
|
|
789
|
+
bodies = branches.map((branch) => branch.children);
|
|
790
|
+
} else if (name === 'for' || name === 'nobr') {
|
|
791
|
+
bodies = [node.children];
|
|
792
|
+
} else if (name === 'include') {
|
|
793
|
+
try {
|
|
794
|
+
const target = passageTarget(
|
|
795
|
+
splitIncludeFlag(node.rawArgs).passage ?? '',
|
|
796
|
+
);
|
|
797
|
+
if (target.kind !== 'name') return false;
|
|
798
|
+
const passage = useStoryStore
|
|
799
|
+
.getState()
|
|
800
|
+
.storyData?.passages.get(target.name);
|
|
801
|
+
if (!passage) return false;
|
|
802
|
+
bodies = [parseMarkup(passage.content)];
|
|
803
|
+
} catch {
|
|
804
|
+
// Let the include report its own argument or parsing error at render.
|
|
805
|
+
return false;
|
|
806
|
+
}
|
|
807
|
+
} else return false;
|
|
808
|
+
} else return false;
|
|
809
|
+
let found = false;
|
|
810
|
+
for (const body of bodies) {
|
|
811
|
+
// Prose makes renderNodes wrap even a literal part in a paragraph.
|
|
812
|
+
if (
|
|
813
|
+
body.some(
|
|
814
|
+
(n) =>
|
|
815
|
+
n.type === 'text' &&
|
|
816
|
+
(n.value.trim() !== '' || BLANK_LINE_RE.test(n.value)),
|
|
817
|
+
)
|
|
818
|
+
)
|
|
819
|
+
return false;
|
|
820
|
+
const first = body.find(
|
|
821
|
+
(n) => !isBlankText(n) && !(n.type === 'macro' && isSetupMacro(n)),
|
|
822
|
+
);
|
|
823
|
+
if (!first) continue;
|
|
824
|
+
if (first.type !== 'html' || first.tag.toLowerCase() !== part) return false;
|
|
825
|
+
found = true;
|
|
826
|
+
}
|
|
827
|
+
return found;
|
|
828
|
+
}
|
|
758
829
|
|
|
759
830
|
/**
|
|
760
|
-
* The
|
|
761
|
-
*
|
|
762
|
-
*
|
|
831
|
+
* The end of the leading part of the `tag` container: preceding macros may
|
|
832
|
+
* render nothing (setup), or produce the part themselves (#474, #739).
|
|
833
|
+
* Keep the prefix through a literal part direct. Without one, preserve the
|
|
834
|
+
* first macro boundary, extending past setup only for a known native part.
|
|
763
835
|
*/
|
|
764
|
-
function
|
|
836
|
+
function leadingPartEnd(tag: string, nodes: ASTNode[]): number {
|
|
765
837
|
const part = LEADING_PARTS[tag];
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
838
|
+
if (!part) return 0;
|
|
839
|
+
let end = 0;
|
|
840
|
+
let setup = true;
|
|
841
|
+
for (let at = 0; at < nodes.length; at++) {
|
|
842
|
+
const node = nodes[at]!;
|
|
843
|
+
if (isBlankText(node)) continue;
|
|
844
|
+
if (node.type === 'macro') {
|
|
845
|
+
if (end === 0) end = at + 1;
|
|
846
|
+
if (setup && !isSetupMacro(node)) {
|
|
847
|
+
if (end < at + 1 && hasKnownLeadingPart(node, part)) {
|
|
848
|
+
end = at + 1;
|
|
849
|
+
}
|
|
850
|
+
setup = false;
|
|
851
|
+
}
|
|
852
|
+
continue;
|
|
853
|
+
}
|
|
854
|
+
if (node.type === 'html' && node.tag.toLowerCase() === part) return at + 1;
|
|
855
|
+
break;
|
|
856
|
+
}
|
|
857
|
+
return end;
|
|
773
858
|
}
|
|
774
859
|
|
|
775
860
|
function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
@@ -833,7 +918,7 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
833
918
|
// The summary of a details element and the legend of a fieldset stay
|
|
834
919
|
// its direct children: rendered apart from the markdown of the rest of
|
|
835
920
|
// the content.
|
|
836
|
-
const
|
|
921
|
+
const leadEnd = leadingPartEnd(tag, node.children);
|
|
837
922
|
const body = (nodes: ASTNode[]) =>
|
|
838
923
|
renderNodes(nodes, { nobr, locals, inline: isInline });
|
|
839
924
|
structural =
|
|
@@ -843,13 +928,12 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
843
928
|
node.children
|
|
844
929
|
.filter((n) => n.type !== 'text')
|
|
845
930
|
.map((n) => renderSingleNode(n))
|
|
846
|
-
) :
|
|
931
|
+
) : leadEnd === 0 ? (
|
|
847
932
|
body(node.children)
|
|
848
933
|
) : (
|
|
849
934
|
<>
|
|
850
|
-
{
|
|
851
|
-
{
|
|
852
|
-
{body(node.children.slice(leadAt + 1))}
|
|
935
|
+
{renderInlineNodes(node.children.slice(0, leadEnd))}
|
|
936
|
+
{body(node.children.slice(leadEnd))}
|
|
853
937
|
</>
|
|
854
938
|
);
|
|
855
939
|
if (isInline !== parentInline) {
|
package/src/store.ts
CHANGED
|
@@ -18,7 +18,7 @@ import type {
|
|
|
18
18
|
SaveExport,
|
|
19
19
|
} from './saves/types';
|
|
20
20
|
import { executeStoryInit } from './story-init';
|
|
21
|
-
import { emit } from './event-emitter';
|
|
21
|
+
import { emit, type NavigationKind } from './event-emitter';
|
|
22
22
|
import {
|
|
23
23
|
resetTriggers,
|
|
24
24
|
checkTriggersOnNavigation,
|
|
@@ -520,7 +520,8 @@ function moveInHistory(get: StoreGet, set: StoreSet, step: -1 | 1): void {
|
|
|
520
520
|
// the game on, and this traversal is dropped rather than applied to a
|
|
521
521
|
// history that is no longer the one it was computed from (#516)
|
|
522
522
|
const navigation = get().navigationId;
|
|
523
|
-
|
|
523
|
+
const kind: NavigationKind = step < 0 ? 'back' : 'forward';
|
|
524
|
+
emit('beforenavigate', targetPassage, kind);
|
|
524
525
|
if (get().navigationId !== navigation) return;
|
|
525
526
|
|
|
526
527
|
// Lowering maxHistory in the callback can trim either end without a new
|
|
@@ -544,7 +545,7 @@ function moveInHistory(get: StoreGet, set: StoreSet, step: -1 | 1): void {
|
|
|
544
545
|
restorePRNGFromMoment(get().history[get().historyIndex]);
|
|
545
546
|
keepDrawsSoFar();
|
|
546
547
|
persistSessionThen(get, () =>
|
|
547
|
-
emit('afternavigate', targetPassage, previousPassage),
|
|
548
|
+
emit('afternavigate', targetPassage, previousPassage, kind),
|
|
548
549
|
);
|
|
549
550
|
}
|
|
550
551
|
|
|
@@ -1436,7 +1437,7 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1436
1437
|
// traversal, this navigation must then give way to the callback's.
|
|
1437
1438
|
const navigation = get().navigationId;
|
|
1438
1439
|
if (
|
|
1439
|
-
replacesState(() => emit('beforenavigate', passageName)) ||
|
|
1440
|
+
replacesState(() => emit('beforenavigate', passageName, 'navigate')) ||
|
|
1440
1441
|
get().navigationId !== navigation
|
|
1441
1442
|
)
|
|
1442
1443
|
return;
|
|
@@ -1493,7 +1494,7 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1493
1494
|
persistSessionThen(get, () => {
|
|
1494
1495
|
if (
|
|
1495
1496
|
replacesState(() =>
|
|
1496
|
-
emit('afternavigate', passageName, previousPassage),
|
|
1497
|
+
emit('afternavigate', passageName, previousPassage, 'navigate'),
|
|
1497
1498
|
)
|
|
1498
1499
|
) {
|
|
1499
1500
|
return;
|
package/src/story-api.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { useStoryStore, trackRuntimeUnsub, recordHookWrite } from './store';
|
|
2
2
|
import type { StoryState, VariableNamespaces } from './store';
|
|
3
|
+
import { freeze } from 'immer';
|
|
3
4
|
import {
|
|
4
5
|
on as emitterOn,
|
|
5
6
|
emit,
|
|
7
|
+
type NavigationKind,
|
|
6
8
|
type StoryEvent,
|
|
7
9
|
type StoryEventCallback,
|
|
8
10
|
} from './event-emitter';
|
|
@@ -70,6 +72,7 @@ import type { TransitionConfig } from './transition';
|
|
|
70
72
|
|
|
71
73
|
export type { StoryAction };
|
|
72
74
|
export type { MacroMetadata };
|
|
75
|
+
export type { NavigationKind };
|
|
73
76
|
|
|
74
77
|
// Deferred-render promise lifecycle.
|
|
75
78
|
// deferRender() creates a promise; ready() resolves it. A deferRender() while
|
|
@@ -154,6 +157,12 @@ export interface StoryAPI {
|
|
|
154
157
|
previousPassage(): Passage | undefined;
|
|
155
158
|
readonly title: string;
|
|
156
159
|
readonly passage: string;
|
|
160
|
+
readonly history: {
|
|
161
|
+
readonly length: number;
|
|
162
|
+
readonly index: number;
|
|
163
|
+
passage(index?: number): string;
|
|
164
|
+
variables(index?: number): Record<string, unknown>;
|
|
165
|
+
};
|
|
157
166
|
readonly settings: typeof settings;
|
|
158
167
|
registerClass(name: string, ctor: new (...args: any[]) => any): void;
|
|
159
168
|
defineMacro(config: MacroDefinition): void;
|
|
@@ -280,6 +289,25 @@ function setOne(draft: VariableNamespaces, name: string, value: unknown): void {
|
|
|
280
289
|
setByPath(namespace, key.split('.'), value);
|
|
281
290
|
}
|
|
282
291
|
|
|
292
|
+
/**
|
|
293
|
+
* The history index `index` names (the current moment's when left out), or
|
|
294
|
+
* a RangeError naming the moments the history holds.
|
|
295
|
+
*/
|
|
296
|
+
function momentIndex(index: number | undefined): number {
|
|
297
|
+
const { history, historyIndex } = useStoryStore.getState();
|
|
298
|
+
const at = index ?? historyIndex;
|
|
299
|
+
if (!Number.isInteger(at) || at < 0 || at >= history.length) {
|
|
300
|
+
const held =
|
|
301
|
+
history.length === 0
|
|
302
|
+
? 'no moments yet'
|
|
303
|
+
: `moments 0 to ${history.length - 1}`;
|
|
304
|
+
throw new RangeError(
|
|
305
|
+
`spindle: History index ${String(at)} is out of range: the history holds ${held}.`,
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
return at;
|
|
309
|
+
}
|
|
310
|
+
|
|
283
311
|
/** A method calling the store action `name`, as the store holds it then. */
|
|
284
312
|
function storeAction<K extends keyof StoryState>(name: K): StoryState[K] {
|
|
285
313
|
return ((...args: unknown[]) =>
|
|
@@ -364,6 +392,26 @@ function createStoryAPI(): StoryAPI {
|
|
|
364
392
|
return useStoryStore.getState().currentPassage;
|
|
365
393
|
},
|
|
366
394
|
|
|
395
|
+
history: {
|
|
396
|
+
get length(): number {
|
|
397
|
+
return useStoryStore.getState().history.length;
|
|
398
|
+
},
|
|
399
|
+
get index(): number {
|
|
400
|
+
return useStoryStore.getState().historyIndex;
|
|
401
|
+
},
|
|
402
|
+
passage(index?: number): string {
|
|
403
|
+
const at = momentIndex(index);
|
|
404
|
+
return useStoryStore.getState().history[at]!.passage;
|
|
405
|
+
},
|
|
406
|
+
variables(index?: number): Record<string, unknown> {
|
|
407
|
+
const at = momentIndex(index);
|
|
408
|
+
// The copy Back and Forward would restore, frozen like the values
|
|
409
|
+
// Story.get() hands out: it is the caller's own, and a write to it
|
|
410
|
+
// fails instead of seeming to change the recorded moment
|
|
411
|
+
return freeze(useStoryStore.getState().getHistoryVariables(at), true);
|
|
412
|
+
},
|
|
413
|
+
},
|
|
414
|
+
|
|
367
415
|
settings,
|
|
368
416
|
|
|
369
417
|
registerClass(name: string, ctor: new (...args: any[]) => any): void {
|