@rohal12/spindle 0.61.6 → 0.61.7
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 +120 -30
- package/dist/pkg/story-variables.js +33 -16
- package/package.json +1 -1
- package/src/components/macros/PassageDisplay.tsx +172 -35
- package/src/format/format-passage.ts +14 -6
- package/src/format/placeholders.ts +20 -18
- package/src/index.tsx +8 -3
- package/src/tooling.ts +7 -5
- package/src/utils/object-path.ts +12 -1
package/dist/pkg/headless.js
CHANGED
|
@@ -15698,7 +15698,9 @@ function shallowCopy(value) {
|
|
|
15698
15698
|
*/
|
|
15699
15699
|
function setByPath(root, segments, value, options = {}) {
|
|
15700
15700
|
const parent = walkToParent(root, segments, options.createMissing ?? false);
|
|
15701
|
-
|
|
15701
|
+
const key = segments[segments.length - 1];
|
|
15702
|
+
if (value === 0 && isDraft(parent) && parent[key] === 0 && !Object.is(parent[key], value) && !(Array.isArray(parent) && key === "length")) parent[key] = void 0;
|
|
15703
|
+
parent[key] = value;
|
|
15702
15704
|
}
|
|
15703
15705
|
/**
|
|
15704
15706
|
* Delete the property at dot-path `segments` below `root`, copying
|
|
@@ -23088,6 +23090,7 @@ function copyLiveState(source, clone) {
|
|
|
23088
23090
|
const input = to;
|
|
23089
23091
|
input.checked = from.checked;
|
|
23090
23092
|
input.indeterminate = from.indeterminate;
|
|
23093
|
+
if (from.type === "file") input.files = from.files;
|
|
23091
23094
|
if (!VALUELESS_INPUTS.has(from.type)) input.value = from.value;
|
|
23092
23095
|
} else to.value = from.value;
|
|
23093
23096
|
});
|
|
@@ -23115,8 +23118,8 @@ function copyLiveState(source, clone) {
|
|
|
23115
23118
|
* is in the page: a detached element has nothing to scroll.
|
|
23116
23119
|
*/
|
|
23117
23120
|
function scrollOffsets(source, clone) {
|
|
23118
|
-
const from = [source, ...source.querySelectorAll("*")];
|
|
23119
|
-
const to = [clone, ...clone.querySelectorAll("*")];
|
|
23121
|
+
const from = [...source instanceof Element ? [source] : [], ...source.querySelectorAll("*")];
|
|
23122
|
+
const to = [...clone instanceof Element ? [clone] : [], ...clone.querySelectorAll("*")];
|
|
23120
23123
|
const offsets = [];
|
|
23121
23124
|
from.forEach((el, i) => {
|
|
23122
23125
|
if (to[i] && (el.scrollTop || el.scrollLeft)) offsets.push([
|
|
@@ -23124,10 +23127,12 @@ function scrollOffsets(source, clone) {
|
|
|
23124
23127
|
el.scrollTop,
|
|
23125
23128
|
el.scrollLeft
|
|
23126
23129
|
]);
|
|
23130
|
+
if (el.shadowRoot && to[i]?.shadowRoot) offsets.push(...scrollOffsets(el.shadowRoot, to[i].shadowRoot));
|
|
23127
23131
|
});
|
|
23128
23132
|
return offsets;
|
|
23129
23133
|
}
|
|
23130
23134
|
var pendingScrolls = /* @__PURE__ */ new WeakMap();
|
|
23135
|
+
var snapshotStyleCleanup = /* @__PURE__ */ new WeakMap();
|
|
23131
23136
|
/** Add `snapshot` to `container`, scrolled as what it shows was. */
|
|
23132
23137
|
function appendSnapshot(container, snapshot) {
|
|
23133
23138
|
container.appendChild(snapshot);
|
|
@@ -23137,6 +23142,97 @@ function appendSnapshot(container, snapshot) {
|
|
|
23137
23142
|
}
|
|
23138
23143
|
pendingScrolls.delete(snapshot);
|
|
23139
23144
|
}
|
|
23145
|
+
function copyComputedStyle(style, target) {
|
|
23146
|
+
for (let i = 0; i < style.length; i++) {
|
|
23147
|
+
const name = style[i];
|
|
23148
|
+
target.setProperty(name, style.getPropertyValue(name));
|
|
23149
|
+
}
|
|
23150
|
+
}
|
|
23151
|
+
function installSnapshotStyles(root, rules) {
|
|
23152
|
+
if (!rules.length) return;
|
|
23153
|
+
const sheet = new CSSStyleSheet();
|
|
23154
|
+
sheet.replaceSync(rules.join("\n"));
|
|
23155
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
23156
|
+
return () => {
|
|
23157
|
+
root.adoptedStyleSheets = root.adoptedStyleSheets.filter((current) => current !== sheet);
|
|
23158
|
+
};
|
|
23159
|
+
}
|
|
23160
|
+
var snapshotPseudoId = 0;
|
|
23161
|
+
/**
|
|
23162
|
+
* Build a visual tree without cloning upgraded custom elements: cloneNode
|
|
23163
|
+
* runs their constructors, and insertion runs their connection callbacks.
|
|
23164
|
+
* Autonomous elements become styled spans; customized built-ins are created
|
|
23165
|
+
* as ordinary native elements, without an `is` option (even if the original
|
|
23166
|
+
* has since removed its `is` attribute).
|
|
23167
|
+
*/
|
|
23168
|
+
function cloneSnapshotTree(source, freezeStyles = false, pseudoRules = []) {
|
|
23169
|
+
const custom = source.namespaceURI === "http://www.w3.org/1999/xhtml" && source.localName.includes("-");
|
|
23170
|
+
freezeStyles ||= custom || source.hasAttribute("is");
|
|
23171
|
+
const clone = document.createElementNS(source.namespaceURI, custom ? "span" : source.localName);
|
|
23172
|
+
for (const attr of [...source.attributes]) {
|
|
23173
|
+
if (attr.name === "id" || attr.name === "data-action" || attr.name === "is" || attr.name.toLowerCase().startsWith("on")) continue;
|
|
23174
|
+
if (attr.namespaceURI) clone.setAttributeNS(attr.namespaceURI, attr.name, attr.value);
|
|
23175
|
+
else clone.setAttribute(attr.name, attr.value);
|
|
23176
|
+
}
|
|
23177
|
+
if (freezeStyles) {
|
|
23178
|
+
copyComputedStyle(getComputedStyle(source), clone.style);
|
|
23179
|
+
let id;
|
|
23180
|
+
for (const pseudo of [
|
|
23181
|
+
"::before",
|
|
23182
|
+
"::after",
|
|
23183
|
+
"::marker"
|
|
23184
|
+
]) {
|
|
23185
|
+
const style = getComputedStyle(source, pseudo);
|
|
23186
|
+
if (!style.content || ["none", "normal"].includes(style.content)) continue;
|
|
23187
|
+
id ??= String(++snapshotPseudoId);
|
|
23188
|
+
clone.setAttribute("data-spindle-snapshot-pseudo", id);
|
|
23189
|
+
const target = document.createElement("span").style;
|
|
23190
|
+
copyComputedStyle(style, target);
|
|
23191
|
+
pseudoRules.push(`[data-spindle-snapshot-pseudo="${id}"]${pseudo}{${target.cssText}}`);
|
|
23192
|
+
}
|
|
23193
|
+
}
|
|
23194
|
+
if (source.localName === "script") clone.setAttribute("type", "application/x-spindle-snapshot");
|
|
23195
|
+
const from = source instanceof HTMLTemplateElement ? source.content : source;
|
|
23196
|
+
const to = clone instanceof HTMLTemplateElement ? clone.content : clone;
|
|
23197
|
+
const copyChildren = (from, to, rules = pseudoRules) => {
|
|
23198
|
+
for (const node of from.childNodes) to.appendChild(node instanceof Element ? cloneSnapshotTree(node, freezeStyles, rules) : node.cloneNode(false));
|
|
23199
|
+
};
|
|
23200
|
+
copyChildren(from, to);
|
|
23201
|
+
if (source.shadowRoot) {
|
|
23202
|
+
const shadow = clone.attachShadow({ mode: "open" });
|
|
23203
|
+
const rules = [];
|
|
23204
|
+
copyChildren(source.shadowRoot, shadow, rules);
|
|
23205
|
+
copyLiveState(source.shadowRoot, shadow);
|
|
23206
|
+
installSnapshotStyles(shadow, rules);
|
|
23207
|
+
}
|
|
23208
|
+
return clone;
|
|
23209
|
+
}
|
|
23210
|
+
/** Replace browsing contexts throughout the snapshot, including shadow trees. */
|
|
23211
|
+
function replaceEmbeddedContexts(source, clone) {
|
|
23212
|
+
const originals = source.querySelectorAll("*");
|
|
23213
|
+
clone.querySelectorAll("*").forEach((embedded, i) => {
|
|
23214
|
+
const original = originals[i];
|
|
23215
|
+
if (!original) return;
|
|
23216
|
+
if (original.shadowRoot && embedded.shadowRoot) replaceEmbeddedContexts(original.shadowRoot, embedded.shadowRoot);
|
|
23217
|
+
if (!embedded.matches("iframe, object, embed")) return;
|
|
23218
|
+
const placeholder = document.createElement("div");
|
|
23219
|
+
for (const name of [
|
|
23220
|
+
"class",
|
|
23221
|
+
"style",
|
|
23222
|
+
"width",
|
|
23223
|
+
"height"
|
|
23224
|
+
]) {
|
|
23225
|
+
const value = embedded.getAttribute(name);
|
|
23226
|
+
if (value !== null) placeholder.setAttribute(name, value);
|
|
23227
|
+
}
|
|
23228
|
+
const rect = original.getBoundingClientRect();
|
|
23229
|
+
if (getComputedStyle(original).display === "inline") placeholder.style.display = "inline-block";
|
|
23230
|
+
placeholder.style.boxSizing = "border-box";
|
|
23231
|
+
placeholder.style.width = `${rect.width}px`;
|
|
23232
|
+
placeholder.style.height = `${rect.height}px`;
|
|
23233
|
+
embedded.replaceWith(placeholder);
|
|
23234
|
+
});
|
|
23235
|
+
}
|
|
23140
23236
|
/**
|
|
23141
23237
|
* Create a snapshot clone of a .passage element for outgoing transitions.
|
|
23142
23238
|
* - Strips IDs, action associations and event handlers from the clone tree
|
|
@@ -23148,30 +23244,13 @@ function appendSnapshot(container, snapshot) {
|
|
|
23148
23244
|
* - Adds `.passage-snapshot` class and `data-transition` attribute
|
|
23149
23245
|
*/
|
|
23150
23246
|
function createSnapshot(passageEl, transitionType) {
|
|
23151
|
-
const
|
|
23152
|
-
|
|
23247
|
+
const pseudoRules = [];
|
|
23248
|
+
const clone = cloneSnapshotTree(passageEl, false, pseudoRules);
|
|
23153
23249
|
copyLiveState(passageEl, clone);
|
|
23154
23250
|
pendingScrolls.set(clone, scrollOffsets(passageEl, clone));
|
|
23155
|
-
|
|
23156
|
-
|
|
23157
|
-
|
|
23158
|
-
for (const name of [
|
|
23159
|
-
"class",
|
|
23160
|
-
"style",
|
|
23161
|
-
"width",
|
|
23162
|
-
"height"
|
|
23163
|
-
]) {
|
|
23164
|
-
const value = embedded.getAttribute(name);
|
|
23165
|
-
if (value !== null) placeholder.setAttribute(name, value);
|
|
23166
|
-
}
|
|
23167
|
-
const original = originals[i];
|
|
23168
|
-
if (original) {
|
|
23169
|
-
const rect = original.getBoundingClientRect();
|
|
23170
|
-
placeholder.style.width = `${rect.width}px`;
|
|
23171
|
-
placeholder.style.height = `${rect.height}px`;
|
|
23172
|
-
}
|
|
23173
|
-
embedded.replaceWith(placeholder);
|
|
23174
|
-
});
|
|
23251
|
+
replaceEmbeddedContexts(passageEl, clone);
|
|
23252
|
+
const cleanup = installSnapshotStyles(passageEl.getRootNode(), pseudoRules);
|
|
23253
|
+
if (cleanup) snapshotStyleCleanup.set(clone, cleanup);
|
|
23175
23254
|
clone.style.pointerEvents = "none";
|
|
23176
23255
|
clone.style.userSelect = "none";
|
|
23177
23256
|
clone.setAttribute("inert", "");
|
|
@@ -23206,7 +23285,12 @@ function cleanupSnapshots(containerEl) {
|
|
|
23206
23285
|
if (!containerEl) return;
|
|
23207
23286
|
containerEl.classList.remove("passage-container--crossfading");
|
|
23208
23287
|
const snapshots = containerEl.querySelectorAll(".passage-snapshot");
|
|
23209
|
-
for (let i = 0; i < snapshots.length; i++)
|
|
23288
|
+
for (let i = 0; i < snapshots.length; i++) {
|
|
23289
|
+
const snapshot = snapshots[i];
|
|
23290
|
+
snapshotStyleCleanup.get(snapshot)?.();
|
|
23291
|
+
snapshotStyleCleanup.delete(snapshot);
|
|
23292
|
+
snapshot.remove();
|
|
23293
|
+
}
|
|
23210
23294
|
}
|
|
23211
23295
|
/**
|
|
23212
23296
|
* The navigation each mounted passage display last committed to the page,
|
|
@@ -23283,7 +23367,13 @@ defineMacro({
|
|
|
23283
23367
|
scrollAfterCleanup.current = false;
|
|
23284
23368
|
cleanupSnapshots(containerRef.current);
|
|
23285
23369
|
}, []);
|
|
23286
|
-
useEffect(() =>
|
|
23370
|
+
useEffect(() => {
|
|
23371
|
+
const container = containerRef.current;
|
|
23372
|
+
return () => {
|
|
23373
|
+
cancelTransition();
|
|
23374
|
+
cleanupSnapshots(container);
|
|
23375
|
+
};
|
|
23376
|
+
}, [cancelTransition]);
|
|
23287
23377
|
useEffect(() => {
|
|
23288
23378
|
const store = useStoryStore.getState();
|
|
23289
23379
|
const { history, historyIndex, transitionConfig } = store;
|
|
@@ -25375,8 +25465,8 @@ function storeVarMacroNames() {
|
|
|
25375
25465
|
* the input macros that bind a variable (registered by author JS or
|
|
25376
25466
|
* StoryInit) and the roles of their parameters decide what is a reference.
|
|
25377
25467
|
*/
|
|
25378
|
-
function variableErrors(storyData, schema) {
|
|
25379
|
-
return validatePassages(storyData.passages, schema, storeVarMacroNames(), knownParameters());
|
|
25468
|
+
function variableErrors(storyData, schema, only = () => true) {
|
|
25469
|
+
return validatePassages(new Map([...storyData.passages].filter(([, passage]) => only(passage))), schema, storeVarMacroNames(), knownParameters());
|
|
25380
25470
|
}
|
|
25381
25471
|
/**
|
|
25382
25472
|
* The markup errors (malformed markup, unknown macros, syntax errors in
|
|
@@ -25427,7 +25517,7 @@ function boot() {
|
|
|
25427
25517
|
}
|
|
25428
25518
|
const schema = orStop(() => parseStoryVariables(storyVarsPassage.content));
|
|
25429
25519
|
for (const { name, block } of orStop(() => effectiveWidgets(storyData.passages.values()))) setBlockWidget(name, block);
|
|
25430
|
-
const errors = variableErrors(storyData, schema);
|
|
25520
|
+
const errors = variableErrors(storyData, schema, (p) => p.name === "StoryInit");
|
|
25431
25521
|
errors.push(...markupErrors(storyData, (p) => p.name === "StoryInit"));
|
|
25432
25522
|
let transientDefaults = {};
|
|
25433
25523
|
const storyTransientsPassage = storyData.passages.get("StoryTransients");
|
|
@@ -24565,10 +24565,6 @@ function multilineJsLiterals(code) {
|
|
|
24565
24565
|
} }, "statements");
|
|
24566
24566
|
return spans;
|
|
24567
24567
|
}
|
|
24568
|
-
/** Whether an HTML tag (opening, closing or self-closing) overlaps the range. */
|
|
24569
|
-
function lineHasTag(tags, start, end) {
|
|
24570
|
-
return tags.some((tag) => tag.start < end && tag.end > start);
|
|
24571
|
-
}
|
|
24572
24568
|
/**
|
|
24573
24569
|
* Replace `<svg>…</svg>` blocks with HTML comment placeholders so Prettier
|
|
24574
24570
|
* does not reformat them (CommonMark does not recognise multi-line SVG tags
|
|
@@ -24587,6 +24583,7 @@ function replaceSvgBlocks(html) {
|
|
|
24587
24583
|
}
|
|
24588
24584
|
var SVG_PLACEHOLDERS = (i) => [`<!--SVG:${i}-->`];
|
|
24589
24585
|
var SPINDLE_PLACEHOLDERS = (i) => [`<!--SP:${i}-->`, `__SP${i}__`];
|
|
24586
|
+
var PLACEHOLDER_PATTERN = /<!--(?:SP|SVG):\d+-->|__SP\d+__/g;
|
|
24590
24587
|
/**
|
|
24591
24588
|
* Whether `text` (what a formatter made of the placeholdered text) holds
|
|
24592
24589
|
* the placeholder of every one of `tokens` exactly once. If not, the
|
|
@@ -24594,7 +24591,17 @@ var SPINDLE_PLACEHOLDERS = (i) => [`<!--SP:${i}-->`, `__SP${i}__`];
|
|
|
24594
24591
|
* lose or duplicate story text.
|
|
24595
24592
|
*/
|
|
24596
24593
|
function placeholdersIntact(text, tokens, placeholdersOf) {
|
|
24597
|
-
|
|
24594
|
+
if (tokens.length === 0) return true;
|
|
24595
|
+
const indices = /* @__PURE__ */ new Map();
|
|
24596
|
+
const counts = new Array(tokens.length).fill(0);
|
|
24597
|
+
tokens.forEach((_, index) => {
|
|
24598
|
+
for (const marker of placeholdersOf(index)) indices.set(marker, index);
|
|
24599
|
+
});
|
|
24600
|
+
for (const [marker] of text.matchAll(PLACEHOLDER_PATTERN)) {
|
|
24601
|
+
const index = indices.get(marker);
|
|
24602
|
+
if (index !== void 0 && ++counts[index] > 1) return false;
|
|
24603
|
+
}
|
|
24604
|
+
return counts.every((count) => count === 1);
|
|
24598
24605
|
}
|
|
24599
24606
|
/** Whether `before` and `after` hold the same Spindle tokens, in the same order. */
|
|
24600
24607
|
function sameSpindleTokens(before, after) {
|
|
@@ -24612,7 +24619,7 @@ function restorePlaceholders(text, groups) {
|
|
|
24612
24619
|
for (const [tokens, placeholdersOf] of groups) tokens.forEach((token, index) => {
|
|
24613
24620
|
for (const marker of placeholdersOf(index)) replacements.set(marker, token);
|
|
24614
24621
|
});
|
|
24615
|
-
return text.replace(
|
|
24622
|
+
return text.replace(PLACEHOLDER_PATTERN, (marker) => {
|
|
24616
24623
|
const token = replacements.get(marker);
|
|
24617
24624
|
replacements.delete(marker);
|
|
24618
24625
|
return token ?? marker;
|
|
@@ -24656,6 +24663,7 @@ function replaceSpindleTokens(html) {
|
|
|
24656
24663
|
const tokens = [];
|
|
24657
24664
|
const resultLines = [];
|
|
24658
24665
|
let unit = 0;
|
|
24666
|
+
let tag = 0;
|
|
24659
24667
|
let lineStart = 0;
|
|
24660
24668
|
while (lineStart <= html.length) {
|
|
24661
24669
|
let lineEnd = lineStart;
|
|
@@ -24675,8 +24683,10 @@ function replaceSpindleTokens(html) {
|
|
|
24675
24683
|
}
|
|
24676
24684
|
const line = html.slice(lineStart, lineEnd);
|
|
24677
24685
|
const indent = line.match(/^(\s*)/)?.[1] ?? "";
|
|
24686
|
+
while (tag < tags.length && tags[tag].end <= lineStart) tag++;
|
|
24687
|
+
const hasTag = tag < tags.length && tags[tag].start < lineEnd;
|
|
24678
24688
|
if (lineUnits.length === 0) resultLines.push(line);
|
|
24679
|
-
else if (!
|
|
24689
|
+
else if (!hasTag) {
|
|
24680
24690
|
const idx = tokens.length;
|
|
24681
24691
|
tokens.push(line.trim());
|
|
24682
24692
|
resultLines.push(`${indent}<!--SP:${idx}-->`);
|
|
@@ -25073,10 +25083,10 @@ var NO_WRAP = /* @__PURE__ */ new Set([
|
|
|
25073
25083
|
*/
|
|
25074
25084
|
function wrapLines(lines, maxLen, literalLines, isBlock) {
|
|
25075
25085
|
const result = [];
|
|
25076
|
-
const
|
|
25086
|
+
const verbatim = verbatimLines(lines);
|
|
25077
25087
|
const structured = markdownLines(lines.join("\n"), isBlock, NO_WRAP);
|
|
25078
25088
|
for (const [index, line] of lines.entries()) {
|
|
25079
|
-
if (line.length <= maxLen ||
|
|
25089
|
+
if (line.length <= maxLen || verbatim.has(index) || structured.has(index) || literalLines.has(line.trim())) {
|
|
25080
25090
|
result.push(line);
|
|
25081
25091
|
continue;
|
|
25082
25092
|
}
|
|
@@ -25095,16 +25105,23 @@ function wrapLines(lines, maxLen, literalLines, isBlock) {
|
|
|
25095
25105
|
}
|
|
25096
25106
|
return result;
|
|
25097
25107
|
}
|
|
25098
|
-
/**
|
|
25099
|
-
function
|
|
25108
|
+
/** Lines holding JavaScript or a multiline HTML tag must not be wrapped. */
|
|
25109
|
+
function verbatimLines(lines) {
|
|
25100
25110
|
const found = /* @__PURE__ */ new Set();
|
|
25101
25111
|
const text = lines.join("\n");
|
|
25102
25112
|
const lineAt = sourceLines(text);
|
|
25103
|
-
|
|
25113
|
+
const { doBlocks, tags } = scanSpindleMarkup(text);
|
|
25114
|
+
for (const block of doBlocks) {
|
|
25104
25115
|
const first = lineAt(block.bodyStart);
|
|
25105
25116
|
const last = lineAt(block.bodyEnd);
|
|
25106
25117
|
for (let i = first; i <= last; i++) found.add(i);
|
|
25107
25118
|
}
|
|
25119
|
+
for (const tag of tags) {
|
|
25120
|
+
const first = lineAt(tag.start);
|
|
25121
|
+
const last = lineAt(tag.end - 1);
|
|
25122
|
+
if (first === last) continue;
|
|
25123
|
+
for (let i = first; i <= last; i++) found.add(i);
|
|
25124
|
+
}
|
|
25108
25125
|
return found;
|
|
25109
25126
|
}
|
|
25110
25127
|
/**
|
|
@@ -25117,7 +25134,7 @@ function wordWrap(text, maxLen, indent) {
|
|
|
25117
25134
|
/** Whether the character is inside a token whose text must stay as written. */
|
|
25118
25135
|
const inToken = new Array(text.length).fill(false);
|
|
25119
25136
|
for (const token of tokenizeMarkupTolerant(text).tokens) {
|
|
25120
|
-
if (token.type === "text"
|
|
25137
|
+
if (token.type === "text") continue;
|
|
25121
25138
|
inToken.fill(true, token.start, token.end);
|
|
25122
25139
|
}
|
|
25123
25140
|
const room = maxLen - indentColumn(indent);
|
|
@@ -25253,8 +25270,8 @@ function validateStoryMarkup(passages, macros, options = {}) {
|
|
|
25253
25270
|
const list = [...passages];
|
|
25254
25271
|
const all = [...macros];
|
|
25255
25272
|
const known = /* @__PURE__ */ new Set();
|
|
25256
|
-
const widgets = widgetDefinitions$1(list, parameterLookup(all));
|
|
25257
|
-
const blocks = new Set(
|
|
25273
|
+
const widgets = effectiveWidgetDefinitions(widgetDefinitions$1(list, parameterLookup(all)));
|
|
25274
|
+
const blocks = new Set(widgets.filter((def) => def.block).map((def) => def.name.toLowerCase()));
|
|
25258
25275
|
const parametersOf = parameterLookup([...all, ...definedWidgetMacros(widgets)]);
|
|
25259
25276
|
for (const macro of all) {
|
|
25260
25277
|
const name = macro.name.toLowerCase();
|
|
@@ -25336,7 +25353,7 @@ var NOT_MARKUP_TAGS = ["script", "stylesheet"];
|
|
|
25336
25353
|
*/
|
|
25337
25354
|
function referenceOptions(macros, all = false, passages = []) {
|
|
25338
25355
|
const list = [...macros];
|
|
25339
|
-
const widgets = widgetDefinitions$1(passages, parameterLookup(list));
|
|
25356
|
+
const widgets = effectiveWidgetDefinitions(widgetDefinitions$1(passages, parameterLookup(list)));
|
|
25340
25357
|
return {
|
|
25341
25358
|
all,
|
|
25342
25359
|
storeVarMacros: new Set(list.filter((m) => m.storeVar).map((m) => m.name.toLowerCase())),
|
package/package.json
CHANGED
|
@@ -35,7 +35,7 @@ const VALUELESS_INPUTS = new Set([
|
|
|
35
35
|
* elements pair up by position. Best effort: a canvas or video that can not
|
|
36
36
|
* be drawn from stays as cloned.
|
|
37
37
|
*/
|
|
38
|
-
function copyLiveState(source:
|
|
38
|
+
function copyLiveState(source: ParentNode, clone: ParentNode): void {
|
|
39
39
|
const pairs = <T extends Element>(
|
|
40
40
|
selector: string,
|
|
41
41
|
each: (a: T, b: T) => void,
|
|
@@ -76,6 +76,7 @@ function copyLiveState(source: Element, clone: Element): void {
|
|
|
76
76
|
const input = to as HTMLInputElement;
|
|
77
77
|
input.checked = from.checked;
|
|
78
78
|
input.indeterminate = from.indeterminate;
|
|
79
|
+
if (from.type === 'file') input.files = from.files;
|
|
79
80
|
if (!VALUELESS_INPUTS.has(from.type)) input.value = from.value;
|
|
80
81
|
} else {
|
|
81
82
|
to.value = from.value;
|
|
@@ -120,19 +121,29 @@ type ScrollOffset = [element: Element, top: number, left: number];
|
|
|
120
121
|
* scrolled, keyed by the matching element of `clone`. Applied once the clone
|
|
121
122
|
* is in the page: a detached element has nothing to scroll.
|
|
122
123
|
*/
|
|
123
|
-
function scrollOffsets(source:
|
|
124
|
-
const from = [
|
|
125
|
-
|
|
124
|
+
function scrollOffsets(source: ParentNode, clone: ParentNode): ScrollOffset[] {
|
|
125
|
+
const from = [
|
|
126
|
+
...(source instanceof Element ? [source] : []),
|
|
127
|
+
...source.querySelectorAll('*'),
|
|
128
|
+
];
|
|
129
|
+
const to = [
|
|
130
|
+
...(clone instanceof Element ? [clone] : []),
|
|
131
|
+
...clone.querySelectorAll('*'),
|
|
132
|
+
];
|
|
126
133
|
const offsets: ScrollOffset[] = [];
|
|
127
134
|
from.forEach((el, i) => {
|
|
128
135
|
if (to[i] && (el.scrollTop || el.scrollLeft)) {
|
|
129
136
|
offsets.push([to[i]!, el.scrollTop, el.scrollLeft]);
|
|
130
137
|
}
|
|
138
|
+
if (el.shadowRoot && to[i]?.shadowRoot) {
|
|
139
|
+
offsets.push(...scrollOffsets(el.shadowRoot, to[i]!.shadowRoot!));
|
|
140
|
+
}
|
|
131
141
|
});
|
|
132
142
|
return offsets;
|
|
133
143
|
}
|
|
134
144
|
|
|
135
145
|
const pendingScrolls = new WeakMap<Element, ScrollOffset[]>();
|
|
146
|
+
const snapshotStyleCleanup = new WeakMap<Element, () => void>();
|
|
136
147
|
|
|
137
148
|
/** Add `snapshot` to `container`, scrolled as what it shows was. */
|
|
138
149
|
function appendSnapshot(container: Element, snapshot: HTMLElement): void {
|
|
@@ -144,6 +155,143 @@ function appendSnapshot(container: Element, snapshot: HTMLElement): void {
|
|
|
144
155
|
pendingScrolls.delete(snapshot);
|
|
145
156
|
}
|
|
146
157
|
|
|
158
|
+
function copyComputedStyle(
|
|
159
|
+
style: CSSStyleDeclaration,
|
|
160
|
+
target: CSSStyleDeclaration,
|
|
161
|
+
): void {
|
|
162
|
+
for (let i = 0; i < style.length; i++) {
|
|
163
|
+
const name = style[i]!;
|
|
164
|
+
target.setProperty(name, style.getPropertyValue(name));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function installSnapshotStyles(
|
|
169
|
+
root: Document | ShadowRoot,
|
|
170
|
+
rules: string[],
|
|
171
|
+
): (() => void) | undefined {
|
|
172
|
+
if (!rules.length) return;
|
|
173
|
+
// A style element would change :last-child and other structural selectors.
|
|
174
|
+
const sheet = new CSSStyleSheet();
|
|
175
|
+
sheet.replaceSync(rules.join('\n'));
|
|
176
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
177
|
+
return () => {
|
|
178
|
+
root.adoptedStyleSheets = root.adoptedStyleSheets.filter(
|
|
179
|
+
(current) => current !== sheet,
|
|
180
|
+
);
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let snapshotPseudoId = 0;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Build a visual tree without cloning upgraded custom elements: cloneNode
|
|
188
|
+
* runs their constructors, and insertion runs their connection callbacks.
|
|
189
|
+
* Autonomous elements become styled spans; customized built-ins are created
|
|
190
|
+
* as ordinary native elements, without an `is` option (even if the original
|
|
191
|
+
* has since removed its `is` attribute).
|
|
192
|
+
*/
|
|
193
|
+
function cloneSnapshotTree(
|
|
194
|
+
source: Element,
|
|
195
|
+
freezeStyles = false,
|
|
196
|
+
pseudoRules: string[] = [],
|
|
197
|
+
): Element {
|
|
198
|
+
const html = source.namespaceURI === 'http://www.w3.org/1999/xhtml';
|
|
199
|
+
const custom = html && source.localName.includes('-');
|
|
200
|
+
freezeStyles ||= custom || source.hasAttribute('is');
|
|
201
|
+
const clone = document.createElementNS(
|
|
202
|
+
source.namespaceURI,
|
|
203
|
+
custom ? 'span' : source.localName,
|
|
204
|
+
);
|
|
205
|
+
for (const attr of [...source.attributes]) {
|
|
206
|
+
if (
|
|
207
|
+
attr.name === 'id' ||
|
|
208
|
+
attr.name === 'data-action' ||
|
|
209
|
+
attr.name === 'is' ||
|
|
210
|
+
attr.name.toLowerCase().startsWith('on')
|
|
211
|
+
)
|
|
212
|
+
continue;
|
|
213
|
+
if (attr.namespaceURI)
|
|
214
|
+
clone.setAttributeNS(attr.namespaceURI, attr.name, attr.value);
|
|
215
|
+
else clone.setAttribute(attr.name, attr.value);
|
|
216
|
+
}
|
|
217
|
+
if (freezeStyles) {
|
|
218
|
+
// Tag-name selectors no longer match the stand-in or its descendants.
|
|
219
|
+
// Keep the component subtree's current appearance through computed CSS.
|
|
220
|
+
copyComputedStyle(getComputedStyle(source), (clone as HTMLElement).style);
|
|
221
|
+
// Generated content needs CSS rules in the same document or shadow root.
|
|
222
|
+
// A unique selector keeps simultaneous snapshots from styling each other.
|
|
223
|
+
let id: string | undefined;
|
|
224
|
+
for (const pseudo of ['::before', '::after', '::marker']) {
|
|
225
|
+
const style = getComputedStyle(source, pseudo);
|
|
226
|
+
if (!style.content || ['none', 'normal'].includes(style.content))
|
|
227
|
+
continue;
|
|
228
|
+
id ??= String(++snapshotPseudoId);
|
|
229
|
+
clone.setAttribute('data-spindle-snapshot-pseudo', id);
|
|
230
|
+
const target = document.createElement('span').style;
|
|
231
|
+
copyComputedStyle(style, target);
|
|
232
|
+
pseudoRules.push(
|
|
233
|
+
`[data-spindle-snapshot-pseudo="${id}"]${pseudo}{${target.cssText}}`,
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
// Recreated scripts must remain inert when inserted, just like the
|
|
238
|
+
// already-executed script nodes that cloneNode used to produce.
|
|
239
|
+
if (source.localName === 'script')
|
|
240
|
+
clone.setAttribute('type', 'application/x-spindle-snapshot');
|
|
241
|
+
const from = source instanceof HTMLTemplateElement ? source.content : source;
|
|
242
|
+
const to = clone instanceof HTMLTemplateElement ? clone.content : clone;
|
|
243
|
+
const copyChildren = (
|
|
244
|
+
from: ParentNode,
|
|
245
|
+
to: ParentNode,
|
|
246
|
+
rules = pseudoRules,
|
|
247
|
+
) => {
|
|
248
|
+
for (const node of from.childNodes) {
|
|
249
|
+
to.appendChild(
|
|
250
|
+
node instanceof Element
|
|
251
|
+
? cloneSnapshotTree(node, freezeStyles, rules)
|
|
252
|
+
: node.cloneNode(false),
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
copyChildren(from, to);
|
|
257
|
+
if (source.shadowRoot) {
|
|
258
|
+
const shadow = clone.attachShadow({ mode: 'open' });
|
|
259
|
+
const rules: string[] = [];
|
|
260
|
+
copyChildren(source.shadowRoot, shadow, rules);
|
|
261
|
+
copyLiveState(source.shadowRoot, shadow);
|
|
262
|
+
installSnapshotStyles(shadow, rules);
|
|
263
|
+
}
|
|
264
|
+
return clone;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Replace browsing contexts throughout the snapshot, including shadow trees. */
|
|
268
|
+
function replaceEmbeddedContexts(source: ParentNode, clone: ParentNode): void {
|
|
269
|
+
const originals = source.querySelectorAll('*');
|
|
270
|
+
const copies = clone.querySelectorAll('*');
|
|
271
|
+
copies.forEach((embedded, i) => {
|
|
272
|
+
const original = originals[i];
|
|
273
|
+
if (!original) return;
|
|
274
|
+
if (original.shadowRoot && embedded.shadowRoot) {
|
|
275
|
+
replaceEmbeddedContexts(original.shadowRoot, embedded.shadowRoot);
|
|
276
|
+
}
|
|
277
|
+
if (!embedded.matches('iframe, object, embed')) return;
|
|
278
|
+
const placeholder = document.createElement('div');
|
|
279
|
+
for (const name of ['class', 'style', 'width', 'height']) {
|
|
280
|
+
const value = embedded.getAttribute(name);
|
|
281
|
+
if (value !== null) placeholder.setAttribute(name, value);
|
|
282
|
+
}
|
|
283
|
+
const rect = original.getBoundingClientRect();
|
|
284
|
+
// Unlike an iframe, an empty inline div does not honor width and height.
|
|
285
|
+
if (getComputedStyle(original).display === 'inline')
|
|
286
|
+
placeholder.style.display = 'inline-block';
|
|
287
|
+
// The measured dimensions already include any copied border and padding.
|
|
288
|
+
placeholder.style.boxSizing = 'border-box';
|
|
289
|
+
placeholder.style.width = `${rect.width}px`;
|
|
290
|
+
placeholder.style.height = `${rect.height}px`;
|
|
291
|
+
embedded.replaceWith(placeholder);
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
|
|
147
295
|
/**
|
|
148
296
|
* Create a snapshot clone of a .passage element for outgoing transitions.
|
|
149
297
|
* - Strips IDs, action associations and event handlers from the clone tree
|
|
@@ -158,40 +306,19 @@ function createSnapshot(
|
|
|
158
306
|
passageEl: Element,
|
|
159
307
|
transitionType: string,
|
|
160
308
|
): HTMLElement {
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
// media copying or insertion can reload a resource in the destination state.
|
|
164
|
-
for (const el of [clone, ...clone.querySelectorAll('*')]) {
|
|
165
|
-
for (const attr of [...el.attributes]) {
|
|
166
|
-
if (
|
|
167
|
-
attr.name === 'id' ||
|
|
168
|
-
attr.name === 'data-action' ||
|
|
169
|
-
attr.name.toLowerCase().startsWith('on')
|
|
170
|
-
)
|
|
171
|
-
el.removeAttribute(attr.name);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
309
|
+
const pseudoRules: string[] = [];
|
|
310
|
+
const clone = cloneSnapshotTree(passageEl, false, pseudoRules) as HTMLElement;
|
|
174
311
|
copyLiveState(passageEl, clone);
|
|
175
312
|
pendingScrolls.set(clone, scrollOffsets(passageEl, clone));
|
|
176
313
|
|
|
177
314
|
// A cloned browsing context would reload and execute scripts on insertion.
|
|
178
315
|
// Keep its authored size/style as a passive placeholder instead.
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if (value !== null) placeholder.setAttribute(name, value);
|
|
186
|
-
}
|
|
187
|
-
const original = originals[i];
|
|
188
|
-
if (original) {
|
|
189
|
-
const rect = original.getBoundingClientRect();
|
|
190
|
-
placeholder.style.width = `${rect.width}px`;
|
|
191
|
-
placeholder.style.height = `${rect.height}px`;
|
|
192
|
-
}
|
|
193
|
-
embedded.replaceWith(placeholder);
|
|
194
|
-
});
|
|
316
|
+
replaceEmbeddedContexts(passageEl, clone);
|
|
317
|
+
const cleanup = installSnapshotStyles(
|
|
318
|
+
passageEl.getRootNode() as Document | ShadowRoot,
|
|
319
|
+
pseudoRules,
|
|
320
|
+
);
|
|
321
|
+
if (cleanup) snapshotStyleCleanup.set(clone, cleanup);
|
|
195
322
|
|
|
196
323
|
// Disable interaction
|
|
197
324
|
clone.style.pointerEvents = 'none';
|
|
@@ -240,7 +367,10 @@ function cleanupSnapshots(containerEl: Element | null): void {
|
|
|
240
367
|
containerEl.classList.remove('passage-container--crossfading');
|
|
241
368
|
const snapshots = containerEl.querySelectorAll('.passage-snapshot');
|
|
242
369
|
for (let i = 0; i < snapshots.length; i++) {
|
|
243
|
-
snapshots[i]
|
|
370
|
+
const snapshot = snapshots[i]!;
|
|
371
|
+
snapshotStyleCleanup.get(snapshot)?.();
|
|
372
|
+
snapshotStyleCleanup.delete(snapshot);
|
|
373
|
+
snapshot.remove();
|
|
244
374
|
}
|
|
245
375
|
}
|
|
246
376
|
|
|
@@ -356,7 +486,14 @@ defineMacro({
|
|
|
356
486
|
}, []);
|
|
357
487
|
|
|
358
488
|
// A transition still running on unmount must not fire into it.
|
|
359
|
-
useEffect(() =>
|
|
489
|
+
useEffect(() => {
|
|
490
|
+
const container = containerRef.current;
|
|
491
|
+
return () => {
|
|
492
|
+
cancelTransition();
|
|
493
|
+
// The ref may already be cleared when the display unmounts.
|
|
494
|
+
cleanupSnapshots(container);
|
|
495
|
+
};
|
|
496
|
+
}, [cancelTransition]);
|
|
360
497
|
|
|
361
498
|
// State machine effect — triggers on every navigation
|
|
362
499
|
useEffect(() => {
|