@rohal12/spindle 0.64.15 → 0.64.17
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 +129 -40
- package/package.json +2 -2
- package/src/components/macros/PassageDisplay.tsx +16 -8
- package/src/markup/render.tsx +118 -20
- package/src/store.ts +7 -2
- package/src/twine-editor/highlight.ts +27 -13
- package/src/utils/element-visibility.ts +2 -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) => {
|
|
@@ -16913,7 +16938,7 @@ var SETTABLE_NAME = /^[A-Za-z_:][\w:.-]*$/;
|
|
|
16913
16938
|
*/
|
|
16914
16939
|
function setRawAttribute(el, name, value) {
|
|
16915
16940
|
if (SETTABLE_NAME.test(name)) {
|
|
16916
|
-
el.setAttribute(name, value);
|
|
16941
|
+
if (el.getAttribute(name) !== value) el.setAttribute(name, value);
|
|
16917
16942
|
return;
|
|
16918
16943
|
}
|
|
16919
16944
|
const parsed = parseHtmlInert(`<i ${name}=""></i>`).firstChild.attributes[0];
|
|
@@ -16941,7 +16966,10 @@ function isNativeDefault(tag, name) {
|
|
|
16941
16966
|
* loads, also drops an empty `class` and `translate="no"`.
|
|
16942
16967
|
*
|
|
16943
16968
|
* On HTML elements an upper-case prop name avoids all of these, and
|
|
16944
|
-
* setAttribute lower-cases it back.
|
|
16969
|
+
* setAttribute lower-cases it back. Native tags also pass lowercase `is`
|
|
16970
|
+
* so customized built-ins initialize when Preact creates them (#733),
|
|
16971
|
+
* and set its attribute directly in case the component owns an `is` property.
|
|
16972
|
+
* Names compat matches in any case
|
|
16945
16973
|
* (`on…`, `translate`), names setAttribute rejects, and on SVG elements,
|
|
16946
16974
|
* whose names are case-sensitive, Preact's reserved names and the `class`
|
|
16947
16975
|
* and `className` compat rewrites (dropping an empty `class`, turning
|
|
@@ -16954,7 +16982,10 @@ function splitAttributes(attributes, foreign, tag) {
|
|
|
16954
16982
|
for (const [name, value, written, bound] of attributes) {
|
|
16955
16983
|
const lower = name.toLowerCase();
|
|
16956
16984
|
if (BOOLEAN_ATTRIBUTES.has(lower) && written !== "" && value === "") continue;
|
|
16957
|
-
else if (!foreign &&
|
|
16985
|
+
else if (!foreign && lower === "is" && !tag.includes("-")) {
|
|
16986
|
+
props.is = value;
|
|
16987
|
+
direct.push([name, value]);
|
|
16988
|
+
} else if (!foreign && LIVE_PROPERTIES.has(lower)) {
|
|
16958
16989
|
if (!bound && isNativeDefault(tag, lower)) defaults.push([name.toUpperCase(), lower === "value" ? value : ""]);
|
|
16959
16990
|
else props[lower] = lower !== "value" && written === "" ? true : value;
|
|
16960
16991
|
} else if (!SETTABLE_NAME.test(name) || lower.startsWith("on") || lower === "translate" || foreign && (FOREIGN_DIRECT.has(name) || name === "__proto__")) direct.push([name, value]);
|
|
@@ -16973,13 +17004,15 @@ function splitAttributes(attributes, foreign, tag) {
|
|
|
16973
17004
|
*/
|
|
16974
17005
|
function useDirectAttributes(direct) {
|
|
16975
17006
|
const elementRef = useRef(null);
|
|
17007
|
+
const currentDirect = useRef(direct);
|
|
17008
|
+
currentDirect.current = direct;
|
|
16976
17009
|
const directKey = JSON.stringify(direct);
|
|
16977
17010
|
useLayoutEffect(() => {
|
|
16978
17011
|
const el = elementRef.current;
|
|
16979
17012
|
if (!el || direct.length === 0) return;
|
|
16980
17013
|
for (const [name, value] of direct) setRawAttribute(el, name, value);
|
|
16981
17014
|
return () => {
|
|
16982
|
-
for (const [name] of direct) el.removeAttribute(name);
|
|
17015
|
+
for (const [name] of direct) if (!currentDirect.current.some(([next]) => next === name)) el.removeAttribute(name);
|
|
16983
17016
|
};
|
|
16984
17017
|
}, [directKey]);
|
|
16985
17018
|
return direct.length > 0 ? elementRef : void 0;
|
|
@@ -17071,16 +17104,79 @@ var LEADING_PARTS = {
|
|
|
17071
17104
|
details: "summary",
|
|
17072
17105
|
fieldset: "legend"
|
|
17073
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
|
+
}
|
|
17074
17116
|
/**
|
|
17075
|
-
*
|
|
17076
|
-
*
|
|
17077
|
-
*
|
|
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.
|
|
17078
17120
|
*/
|
|
17079
|
-
function
|
|
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
|
+
}
|
|
17154
|
+
/**
|
|
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.
|
|
17159
|
+
*/
|
|
17160
|
+
function leadingPartEnd(tag, nodes) {
|
|
17080
17161
|
const part = LEADING_PARTS[tag];
|
|
17081
|
-
|
|
17082
|
-
|
|
17083
|
-
|
|
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;
|
|
17084
17180
|
}
|
|
17085
17181
|
function HtmlNodeRenderer({ node }) {
|
|
17086
17182
|
const scope = useTextScope();
|
|
@@ -17111,18 +17207,14 @@ function HtmlNodeRenderer({ node }) {
|
|
|
17111
17207
|
if (RAW_TEXT_ELEMENTS.has(tag)) children = node.children.map((n) => n.type === "text" ? n.value : renderSingleNode(n));
|
|
17112
17208
|
else if (rawContent || isRawRoot) children = renderInlineNodes(node.children);
|
|
17113
17209
|
else {
|
|
17114
|
-
const
|
|
17210
|
+
const leadEnd = leadingPartEnd(tag, node.children);
|
|
17115
17211
|
const body = (nodes) => renderNodes(nodes, {
|
|
17116
17212
|
nobr,
|
|
17117
17213
|
locals,
|
|
17118
17214
|
inline: isInline
|
|
17119
17215
|
});
|
|
17120
17216
|
structural = STRUCTURAL_ELEMENTS.has(tag) && node.children.every((n) => n.type !== "text" || isBlankText(n));
|
|
17121
|
-
children = structural ? node.children.filter((n) => n.type !== "text").map((n) => renderSingleNode(n)) :
|
|
17122
|
-
body(node.children.slice(0, leadAt)),
|
|
17123
|
-
renderSingleNode(node.children[leadAt]),
|
|
17124
|
-
body(node.children.slice(leadAt + 1))
|
|
17125
|
-
] });
|
|
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))] });
|
|
17126
17218
|
if (isInline !== parentInline) children = /* @__PURE__ */ jsx(InlineContext.Provider, {
|
|
17127
17219
|
value: isInline,
|
|
17128
17220
|
children
|
|
@@ -19507,7 +19599,7 @@ function replacesState(fn) {
|
|
|
19507
19599
|
*/
|
|
19508
19600
|
function moveInHistory(get, set, step) {
|
|
19509
19601
|
const { historyIndex, history } = get();
|
|
19510
|
-
|
|
19602
|
+
let target = historyIndex + step;
|
|
19511
19603
|
if (target < 0 || target >= history.length) return;
|
|
19512
19604
|
finishEnteredMoment(get, set);
|
|
19513
19605
|
const previousPassage = get().currentPassage;
|
|
@@ -19515,9 +19607,11 @@ function moveInHistory(get, set, step) {
|
|
|
19515
19607
|
const navigation = get().navigationId;
|
|
19516
19608
|
emit("beforenavigate", targetPassage);
|
|
19517
19609
|
if (get().navigationId !== navigation) return;
|
|
19610
|
+
target = get().historyIndex + step;
|
|
19611
|
+
if (target < 0 || target >= get().history.length) return;
|
|
19518
19612
|
const restoredVars = deepClone(reconstructVarsAt(target));
|
|
19519
19613
|
set((state) => {
|
|
19520
|
-
state.historyIndex
|
|
19614
|
+
state.historyIndex = target;
|
|
19521
19615
|
state.currentPassage = state.history[state.historyIndex].passage;
|
|
19522
19616
|
state.navigationId++;
|
|
19523
19617
|
state.variables = restoredVars;
|
|
@@ -23818,7 +23912,7 @@ function installSnapshotSvgStyles(root, ids, marker, targetSheets = [...root.sty
|
|
|
23818
23912
|
}
|
|
23819
23913
|
return result;
|
|
23820
23914
|
};
|
|
23821
|
-
const inserted =
|
|
23915
|
+
const inserted = /* @__PURE__ */ new Map();
|
|
23822
23916
|
const sheets = /* @__PURE__ */ new Set();
|
|
23823
23917
|
const sheetRules = (sheet) => {
|
|
23824
23918
|
if (sheets.has(sheet) || sheet.disabled || !matchMedia(sheet.media.mediaText || "all").matches) return;
|
|
@@ -23828,14 +23922,17 @@ function installSnapshotSvgStyles(root, ids, marker, targetSheets = [...root.sty
|
|
|
23828
23922
|
} catch {}
|
|
23829
23923
|
};
|
|
23830
23924
|
const visit = (parent, base) => {
|
|
23831
|
-
for (
|
|
23925
|
+
for (let at = parent.cssRules.length - 1; at >= 0; at--) {
|
|
23926
|
+
const rule = parent.cssRules[at];
|
|
23832
23927
|
if (snapshotSvgRules.has(rule)) continue;
|
|
23833
23928
|
if (rule instanceof CSSStyleRule) {
|
|
23834
|
-
let index =
|
|
23929
|
+
let index = at + 1;
|
|
23835
23930
|
for (const css of rulesOf([rule], base)) {
|
|
23836
23931
|
parent.insertRule(css, index);
|
|
23837
23932
|
const insertedRule = parent.cssRules[index++];
|
|
23838
|
-
inserted.
|
|
23933
|
+
let rules = inserted.get(parent);
|
|
23934
|
+
if (!rules) inserted.set(parent, rules = /* @__PURE__ */ new Set());
|
|
23935
|
+
rules.add(insertedRule);
|
|
23839
23936
|
snapshotSvgRules.add(insertedRule);
|
|
23840
23937
|
}
|
|
23841
23938
|
} else if (rule instanceof CSSImportRule) {
|
|
@@ -23845,10 +23942,9 @@ function installSnapshotSvgStyles(root, ids, marker, targetSheets = [...root.sty
|
|
|
23845
23942
|
};
|
|
23846
23943
|
for (const sheet of targetSheets) sheetRules(sheet);
|
|
23847
23944
|
return () => {
|
|
23848
|
-
for (const [parent,
|
|
23849
|
-
|
|
23850
|
-
|
|
23851
|
-
snapshotSvgRules.delete(rule);
|
|
23945
|
+
for (const [parent, rules] of inserted) {
|
|
23946
|
+
for (let index = parent.cssRules.length - 1; index >= 0; index--) if (rules.has(parent.cssRules[index])) parent.deleteRule(index);
|
|
23947
|
+
for (const rule of rules) snapshotSvgRules.delete(rule);
|
|
23852
23948
|
}
|
|
23853
23949
|
};
|
|
23854
23950
|
}
|
|
@@ -25750,14 +25846,7 @@ defineMacro({
|
|
|
25750
25846
|
name: "include",
|
|
25751
25847
|
interpolate: true,
|
|
25752
25848
|
merged: true,
|
|
25753
|
-
parameters:
|
|
25754
|
-
name: "inline",
|
|
25755
|
-
type: "flag"
|
|
25756
|
-
}, {
|
|
25757
|
-
name: "passage",
|
|
25758
|
-
type: "passage",
|
|
25759
|
-
required: true
|
|
25760
|
-
}],
|
|
25849
|
+
parameters: INCLUDE_PARAMETERS,
|
|
25761
25850
|
render(_props, ctx) {
|
|
25762
25851
|
const displayOnly = ctx.hooks.useContext(DisplayOnlyContext);
|
|
25763
25852
|
const { storyData } = useStoryFields("storyData");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rohal12/spindle",
|
|
3
|
-
"version": "0.64.
|
|
3
|
+
"version": "0.64.17",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A Preact-based story format for Twine 2.",
|
|
6
6
|
"license": "Unlicense",
|
|
@@ -108,7 +108,7 @@
|
|
|
108
108
|
"@vitest/coverage-v8": "^5.0.3",
|
|
109
109
|
"codemirror": "^5.65.21",
|
|
110
110
|
"fake-indexeddb": "^6.2.5",
|
|
111
|
-
"fallow": "3.
|
|
111
|
+
"fallow": "3.38.1",
|
|
112
112
|
"fast-check": "^4.10.2",
|
|
113
113
|
"happy-dom": "^21.0.4",
|
|
114
114
|
"husky": "^9.1.7",
|
|
@@ -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/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;
|
|
@@ -540,7 +544,8 @@ const SETTABLE_NAME = /^[A-Za-z_:][\w:.-]*$/;
|
|
|
540
544
|
*/
|
|
541
545
|
function setRawAttribute(el: Element, name: string, value: string) {
|
|
542
546
|
if (SETTABLE_NAME.test(name)) {
|
|
543
|
-
|
|
547
|
+
// Preact may already have set it; avoid firing custom-element callbacks twice.
|
|
548
|
+
if (el.getAttribute(name) !== value) el.setAttribute(name, value);
|
|
544
549
|
return;
|
|
545
550
|
}
|
|
546
551
|
const parsed = (parseHtmlInert(`<i ${name}=""></i>`).firstChild as Element)
|
|
@@ -571,7 +576,10 @@ function isNativeDefault(tag: string, name: string): boolean {
|
|
|
571
576
|
* loads, also drops an empty `class` and `translate="no"`.
|
|
572
577
|
*
|
|
573
578
|
* On HTML elements an upper-case prop name avoids all of these, and
|
|
574
|
-
* setAttribute lower-cases it back.
|
|
579
|
+
* setAttribute lower-cases it back. Native tags also pass lowercase `is`
|
|
580
|
+
* so customized built-ins initialize when Preact creates them (#733),
|
|
581
|
+
* and set its attribute directly in case the component owns an `is` property.
|
|
582
|
+
* Names compat matches in any case
|
|
575
583
|
* (`on…`, `translate`), names setAttribute rejects, and on SVG elements,
|
|
576
584
|
* whose names are case-sensitive, Preact's reserved names and the `class`
|
|
577
585
|
* and `className` compat rewrites (dropping an empty `class`, turning
|
|
@@ -591,6 +599,9 @@ function splitAttributes(
|
|
|
591
599
|
const lower = name.toLowerCase();
|
|
592
600
|
if (BOOLEAN_ATTRIBUTES.has(lower) && written !== '' && value === '') {
|
|
593
601
|
continue;
|
|
602
|
+
} else if (!foreign && lower === 'is' && !tag.includes('-')) {
|
|
603
|
+
props.is = value;
|
|
604
|
+
direct.push([name, value]);
|
|
594
605
|
} else if (!foreign && LIVE_PROPERTIES.has(lower)) {
|
|
595
606
|
if (!bound && isNativeDefault(tag, lower)) {
|
|
596
607
|
defaults.push([name.toUpperCase(), lower === 'value' ? value : '']);
|
|
@@ -621,13 +632,20 @@ function useDirectAttributes(
|
|
|
621
632
|
direct: [string, string][],
|
|
622
633
|
): RefObject<Element | null> | undefined {
|
|
623
634
|
const elementRef = useRef<Element>(null);
|
|
635
|
+
const currentDirect = useRef(direct);
|
|
636
|
+
currentDirect.current = direct;
|
|
624
637
|
const directKey = JSON.stringify(direct);
|
|
625
638
|
useLayoutEffect(() => {
|
|
626
639
|
const el = elementRef.current;
|
|
627
640
|
if (!el || direct.length === 0) return;
|
|
628
641
|
for (const [name, value] of direct) setRawAttribute(el, name, value);
|
|
629
642
|
return () => {
|
|
630
|
-
for (const [name] of direct)
|
|
643
|
+
for (const [name] of direct) {
|
|
644
|
+
// Keep retained attributes through updates without remove/add callbacks.
|
|
645
|
+
if (!currentDirect.current.some(([next]) => next === name)) {
|
|
646
|
+
el.removeAttribute(name);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
631
649
|
};
|
|
632
650
|
}, [directKey]);
|
|
633
651
|
return direct.length > 0 ? elementRef : undefined;
|
|
@@ -741,21 +759,102 @@ const LEADING_PARTS: Record<string, string> = {
|
|
|
741
759
|
details: 'summary',
|
|
742
760
|
fieldset: 'legend',
|
|
743
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
|
+
}
|
|
744
771
|
|
|
745
772
|
/**
|
|
746
|
-
*
|
|
747
|
-
*
|
|
748
|
-
*
|
|
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.
|
|
749
776
|
*/
|
|
750
|
-
function
|
|
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
|
+
}
|
|
829
|
+
|
|
830
|
+
/**
|
|
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.
|
|
835
|
+
*/
|
|
836
|
+
function leadingPartEnd(tag: string, nodes: ASTNode[]): number {
|
|
751
837
|
const part = LEADING_PARTS[tag];
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
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;
|
|
759
858
|
}
|
|
760
859
|
|
|
761
860
|
function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
@@ -819,7 +918,7 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
819
918
|
// The summary of a details element and the legend of a fieldset stay
|
|
820
919
|
// its direct children: rendered apart from the markdown of the rest of
|
|
821
920
|
// the content.
|
|
822
|
-
const
|
|
921
|
+
const leadEnd = leadingPartEnd(tag, node.children);
|
|
823
922
|
const body = (nodes: ASTNode[]) =>
|
|
824
923
|
renderNodes(nodes, { nobr, locals, inline: isInline });
|
|
825
924
|
structural =
|
|
@@ -829,13 +928,12 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
829
928
|
node.children
|
|
830
929
|
.filter((n) => n.type !== 'text')
|
|
831
930
|
.map((n) => renderSingleNode(n))
|
|
832
|
-
) :
|
|
931
|
+
) : leadEnd === 0 ? (
|
|
833
932
|
body(node.children)
|
|
834
933
|
) : (
|
|
835
934
|
<>
|
|
836
|
-
{
|
|
837
|
-
{
|
|
838
|
-
{body(node.children.slice(leadAt + 1))}
|
|
935
|
+
{renderInlineNodes(node.children.slice(0, leadEnd))}
|
|
936
|
+
{body(node.children.slice(leadEnd))}
|
|
839
937
|
</>
|
|
840
938
|
);
|
|
841
939
|
if (isInline !== parentInline) {
|
package/src/store.ts
CHANGED
|
@@ -509,7 +509,7 @@ function replacesState(fn: () => void): boolean {
|
|
|
509
509
|
*/
|
|
510
510
|
function moveInHistory(get: StoreGet, set: StoreSet, step: -1 | 1): void {
|
|
511
511
|
const { historyIndex, history } = get();
|
|
512
|
-
|
|
512
|
+
let target = historyIndex + step;
|
|
513
513
|
if (target < 0 || target >= history.length) return;
|
|
514
514
|
finishEnteredMoment(get, set);
|
|
515
515
|
|
|
@@ -523,10 +523,15 @@ function moveInHistory(get: StoreGet, set: StoreSet, step: -1 | 1): void {
|
|
|
523
523
|
emit('beforenavigate', targetPassage);
|
|
524
524
|
if (get().navigationId !== navigation) return;
|
|
525
525
|
|
|
526
|
+
// Lowering maxHistory in the callback can trim either end without a new
|
|
527
|
+
// navigation. The retained moments stay adjacent to the current one;
|
|
528
|
+
// resolve the target again, or cancel if it was removed (#732).
|
|
529
|
+
target = get().historyIndex + step;
|
|
530
|
+
if (target < 0 || target >= get().history.length) return;
|
|
526
531
|
const restoredVars = deepClone(reconstructVarsAt(target));
|
|
527
532
|
|
|
528
533
|
set((state) => {
|
|
529
|
-
state.historyIndex
|
|
534
|
+
state.historyIndex = target;
|
|
530
535
|
state.currentPassage = state.history[state.historyIndex]!.passage;
|
|
531
536
|
state.navigationId++;
|
|
532
537
|
state.variables = restoredVars;
|
|
@@ -18,6 +18,7 @@ type ScannedMarkup = ReturnType<typeof scanMarkupTolerant>;
|
|
|
18
18
|
import { jsCodeEnd } from '../markup/code-end';
|
|
19
19
|
import { builtinMacros } from './builtin-macros';
|
|
20
20
|
import { parameterLookup } from '../code-check';
|
|
21
|
+
import { upperBound } from '../utils/sorted';
|
|
21
22
|
|
|
22
23
|
const pairing = parameterLookup(builtinMacros).pairing;
|
|
23
24
|
|
|
@@ -407,26 +408,39 @@ function spansOf(text: string, reading: Reading): StyledSpan[] {
|
|
|
407
408
|
}
|
|
408
409
|
|
|
409
410
|
/**
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
*
|
|
411
|
+
* Owners of code that may end elsewhere once text follows: the damage of
|
|
412
|
+
* an error the code is in, or the tag holding the code (a raw body starts
|
|
413
|
+
* where its opener ends). Index once for all open code positions.
|
|
413
414
|
*/
|
|
414
|
-
function
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
const
|
|
420
|
-
(
|
|
421
|
-
|
|
422
|
-
|
|
415
|
+
function openCodeStarts(scan: ScannedMarkup, ends: number[]): number[] {
|
|
416
|
+
if (!ends.length) return [];
|
|
417
|
+
// Damage starts are ordered but their ranges may overlap. Prefix maxima
|
|
418
|
+
// let each query find the first owning error, just as findIndex did.
|
|
419
|
+
const damageEnds: number[] = [];
|
|
420
|
+
for (const error of scan.errors) {
|
|
421
|
+
damageEnds.push(
|
|
422
|
+
Math.max(
|
|
423
|
+
damageEnds[damageEnds.length - 1] ?? -1,
|
|
424
|
+
error.offset,
|
|
425
|
+
error.end,
|
|
426
|
+
),
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
const tokens = scan.tokens.filter((token) => token.type !== 'text');
|
|
430
|
+
const starts = tokens.map((token) => token.start);
|
|
431
|
+
return ends.map((at) => {
|
|
432
|
+
const i = upperBound(damageEnds, at - 1);
|
|
433
|
+
if (i < scan.damage.length && scan.damage[i]! < at) return scan.damage[i]!;
|
|
434
|
+
const token = tokens[upperBound(starts, at - 1) - 1];
|
|
435
|
+
return token && at <= token.end ? token.start : -1;
|
|
436
|
+
});
|
|
423
437
|
}
|
|
424
438
|
|
|
425
439
|
function openStartOf(text: string, reading: Reading): number {
|
|
426
440
|
const { tokens } = reading;
|
|
427
441
|
const starts = [
|
|
428
442
|
...unclosedStarts(text, reading),
|
|
429
|
-
...
|
|
443
|
+
...openCodeStarts(reading, reading.openEnds),
|
|
430
444
|
openComment(tokens),
|
|
431
445
|
openDo(tokens)?.start ?? -1,
|
|
432
446
|
openTag(text, tokens),
|