@rohal12/spindle 0.64.22 → 0.64.23

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.
@@ -1,6 +1,6 @@
1
1
  import { Fragment, createContext, createPortal, h, render } from "preact";
2
2
  import { useCallback, useContext, useEffect, useErrorBoundary, useId, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";
3
- import { useSyncExternalStore } from "preact/compat";
3
+ import { memo, useSyncExternalStore } from "preact/compat";
4
4
  import { createStore } from "zustand/vanilla";
5
5
  import { immer } from "zustand/middleware/immer";
6
6
  import { Immer, current, enableMapSet, enablePatches, freeze, isDraft, original } from "immer";
@@ -13633,17 +13633,17 @@ function lenientClose(input, i, onRunOut) {
13633
13633
  function lenientRead(input, i) {
13634
13634
  if (memoInput !== input) {
13635
13635
  memoInput = input;
13636
- memo.clear();
13636
+ memo$1.clear();
13637
13637
  }
13638
- let read = memo.get(i);
13638
+ let read = memo$1.get(i);
13639
13639
  if (read === void 0) {
13640
13640
  read = scanLenient(input, i);
13641
- memo.set(i, read);
13641
+ memo$1.set(i, read);
13642
13642
  }
13643
13643
  return read;
13644
13644
  }
13645
13645
  var memoInput = "";
13646
- var memo = /* @__PURE__ */ new Map();
13646
+ var memo$1 = /* @__PURE__ */ new Map();
13647
13647
  function scanLenient(input, start) {
13648
13648
  const codeFrame = (start) => ({
13649
13649
  kind: "code",
@@ -13663,7 +13663,7 @@ function scanLenient(input, start) {
13663
13663
  end,
13664
13664
  ranOut
13665
13665
  };
13666
- if (frame.kind === "code") memo.set(frame.start, result);
13666
+ if (frame.kind === "code") memo$1.set(frame.start, result);
13667
13667
  const parent = frames[frames.length - 1];
13668
13668
  if (!parent) return;
13669
13669
  parent.ranOut ||= ranOut;
@@ -13684,7 +13684,7 @@ function scanLenient(input, start) {
13684
13684
  else if (c === frame.quote) finish(i + 1, ranOut);
13685
13685
  else if (frame.quote !== "`" && c === "\n") finish(-1, ranOut);
13686
13686
  else if (frame.quote === "`" && c === "$" && input[i + 1] === "{") {
13687
- const cached = memo.get(i + 2);
13687
+ const cached = memo$1.get(i + 2);
13688
13688
  if (cached) {
13689
13689
  frame.ranOut ||= cached.ranOut;
13690
13690
  frame.i = cached.end === -1 ? input.length : cached.end + 1;
@@ -23669,19 +23669,58 @@ function renderPassageContent(passage) {
23669
23669
  return renderNodes(parsePassage(passage), passage.tags.includes("nobr") ? { nobr: true } : void 0);
23670
23670
  }
23671
23671
  /**
23672
- * The content of a special passage shown with every passage (PassageHeader,
23673
- * PassageFooter, PassageDone), or null. One that fails to render is logged
23674
- * and left out, so it can't take the passage down with it.
23672
+ * A passage's rendered content, drawn again only when the passage or its
23673
+ * text changes (the props are compared shallowly, so a parent re-rendering
23674
+ * with the same passage skips it).
23675
+ *
23676
+ * It is a component and not a vnode kept by `useMemo`. A kept vnode that is
23677
+ * placed again carries Preact's links (its parent, its rendered children) to
23678
+ * the tree it was first placed in. A navigation's tree reaches the kept vnode
23679
+ * of the passage on its way out, so every tree held the one before it and
23680
+ * none was ever freed. The `content` prop is only here so that an edit of
23681
+ * the passage text in place draws it again.
23682
+ */
23683
+ var PassageBody = memo(function PassageBody({ passage }) {
23684
+ try {
23685
+ return /* @__PURE__ */ jsx(Fragment$1, { children: renderPassageContent(passage) });
23686
+ } catch (err) {
23687
+ return /* @__PURE__ */ jsxs("div", {
23688
+ class: "error",
23689
+ children: [
23690
+ "Error parsing passage “",
23691
+ passage.name,
23692
+ "”",
23693
+ sourceLocationOf(passage),
23694
+ ": ",
23695
+ errorMessage(err)
23696
+ ]
23697
+ });
23698
+ }
23699
+ });
23700
+ /**
23701
+ * A special passage shown with every passage (PassageHeader, PassageFooter,
23702
+ * PassageDone), in its wrapper; nothing if it has no content. One that fails
23703
+ * to render is logged and left out, so it can't take the passage down with
23704
+ * it. Memoized like PassageBody.
23675
23705
  */
23676
- function renderSpecialPassage(passage) {
23677
- if (!passage) return null;
23706
+ var SpecialPassage = memo(function SpecialPassage({ passage, kind }) {
23707
+ let content;
23678
23708
  try {
23679
- return renderPassageContent(passage);
23709
+ content = renderPassageContent(passage);
23680
23710
  } catch (err) {
23681
23711
  console.error(`spindle: Error in ${passage.name}:`, err);
23682
23712
  return null;
23683
23713
  }
23684
- }
23714
+ if (!content) return null;
23715
+ if (kind === "done") return /* @__PURE__ */ jsx(EntryRenderBoundary, { children: /* @__PURE__ */ jsx("div", {
23716
+ hidden: true,
23717
+ children: content
23718
+ }) });
23719
+ return /* @__PURE__ */ jsx("div", {
23720
+ class: `passage-${kind}`,
23721
+ children: content
23722
+ });
23723
+ });
23685
23724
  var CODE_PASSAGES = /* @__PURE__ */ new Set([
23686
23725
  "PassageReady",
23687
23726
  "PassageHeader",
@@ -23703,34 +23742,20 @@ function Passage({ passage, dataTransition, navigationId }) {
23703
23742
  const { storyData } = useStoryFields("storyData");
23704
23743
  const isCodePassage = CODE_PASSAGES.has(passage.name);
23705
23744
  const [doneReady, setDoneReady] = useState(false);
23706
- const content = useMemo(() => {
23707
- try {
23708
- return renderPassageContent(passage);
23709
- } catch (err) {
23710
- return /* @__PURE__ */ jsxs("div", {
23711
- class: "error",
23712
- children: [
23713
- "Error parsing passage “",
23714
- passage.name,
23715
- "”",
23716
- sourceLocationOf(passage),
23717
- ": ",
23718
- errorMessage(err)
23719
- ]
23720
- });
23721
- }
23722
- }, [passage.content, passage.name]);
23723
23745
  const special = (name) => isCodePassage ? void 0 : storyData?.passages.get(name);
23724
- const headerPassage = special("PassageHeader");
23725
- const footerPassage = special("PassageFooter");
23746
+ const specialPassage = (name, kind) => {
23747
+ const found = special(name);
23748
+ return found ? /* @__PURE__ */ jsx(SpecialPassage, {
23749
+ passage: found,
23750
+ content: found.content,
23751
+ kind
23752
+ }) : null;
23753
+ };
23726
23754
  const donePassage = special("PassageDone");
23727
- const headerContent = useMemo(() => renderSpecialPassage(headerPassage), [headerPassage?.content]);
23728
- const footerContent = useMemo(() => renderSpecialPassage(footerPassage), [footerPassage?.content]);
23729
23755
  useEffect(() => {
23730
23756
  if (donePassage) setDoneReady(true);
23731
23757
  return () => setDoneReady(false);
23732
23758
  }, [passage.name]);
23733
- const doneContent = useMemo(() => doneReady ? renderSpecialPassage(donePassage) : null, [doneReady, donePassage?.content]);
23734
23759
  const elRef = useRef(null);
23735
23760
  useLayoutEffect(() => {
23736
23761
  if (navigationId !== void 0) markPassageRendered(navigationId);
@@ -23754,19 +23779,13 @@ function Passage({ passage, dataTransition, navigationId }) {
23754
23779
  "data-tags": passage.tags.join(" "),
23755
23780
  "data-transition": dataTransition,
23756
23781
  children: [
23757
- headerContent && /* @__PURE__ */ jsx("div", {
23758
- class: "passage-header",
23759
- children: headerContent
23760
- }),
23761
- content,
23762
- footerContent && /* @__PURE__ */ jsx("div", {
23763
- class: "passage-footer",
23764
- children: footerContent
23782
+ specialPassage("PassageHeader", "header"),
23783
+ /* @__PURE__ */ jsx(PassageBody, {
23784
+ passage,
23785
+ content: passage.content
23765
23786
  }),
23766
- doneContent && /* @__PURE__ */ jsx(EntryRenderBoundary, { children: /* @__PURE__ */ jsx("div", {
23767
- hidden: true,
23768
- children: doneContent
23769
- }) })
23787
+ specialPassage("PassageFooter", "footer"),
23788
+ doneReady && specialPassage("PassageDone", "done")
23770
23789
  ]
23771
23790
  })
23772
23791
  })
@@ -24325,7 +24344,7 @@ defineMacro({
24325
24344
  const displayedId = displayed.id;
24326
24345
  const isOutgoing = useCallback((state) => state.navigationId !== displayedId, [displayedId]);
24327
24346
  const hasLeft = useCallback(() => isOutgoing(useStoryStore.getState()), [isOutgoing]);
24328
- const lastElement = useRef(null);
24347
+ const lastShown = useRef(null);
24329
24348
  const containerRef = useRef(null);
24330
24349
  const timeoutsRef = useRef([]);
24331
24350
  const scrollAfterCleanup = useRef(false);
@@ -24433,12 +24452,16 @@ defineMacro({
24433
24452
  ]
24434
24453
  });
24435
24454
  const readyPassage = storyData?.passages.get("PassageReady");
24436
- const passageElement = !renderDeferred && displayed.id !== navigationId && lastElement.current ? lastElement.current : effectivePassage && /* @__PURE__ */ jsx(Passage, {
24437
- passage: effectivePassage,
24438
- dataTransition: renderDeferred ? "none" : resolvedTypeRef.current,
24439
- navigationId: renderDeferred ? void 0 : displayed.id
24440
- }, renderDeferred ? "loading" : `nav-${displayed.id}`);
24441
- lastElement.current = passageElement || null;
24455
+ const shown = !renderDeferred && displayed.id !== navigationId && lastShown.current ? lastShown.current : effectivePassage ? {
24456
+ key: renderDeferred ? "loading" : `nav-${displayed.id}`,
24457
+ props: {
24458
+ passage: effectivePassage,
24459
+ dataTransition: renderDeferred ? "none" : resolvedTypeRef.current,
24460
+ navigationId: renderDeferred ? void 0 : displayed.id
24461
+ }
24462
+ } : null;
24463
+ const passageElement = shown && /* @__PURE__ */ jsx(Passage, { ...shown.props }, shown.key);
24464
+ lastShown.current = shown;
24442
24465
  return /* @__PURE__ */ jsxs("div", {
24443
24466
  id: ctx.id ?? "story",
24444
24467
  class: ctx.className ?? "story",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.64.22",
3
+ "version": "0.64.23",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
@@ -1,10 +1,4 @@
1
- import {
2
- useMemo,
3
- useEffect,
4
- useLayoutEffect,
5
- useRef,
6
- useState,
7
- } from 'preact/hooks';
1
+ import { useEffect, useLayoutEffect, useRef, useState } from 'preact/hooks';
8
2
  import { parseMarkup } from '../markup/parse';
9
3
  import type { ASTNode } from '../markup/ast';
10
4
  import {
@@ -20,6 +14,7 @@ import { sourceLocationOf } from '../utils/source-location';
20
14
  import { emitFromRender } from '../event-emitter';
21
15
  import { duringEntryRender } from '../triggers';
22
16
  import type { ComponentChildren } from 'preact';
17
+ import { memo } from 'preact/compat';
23
18
  import {
24
19
  EntryRenderContext,
25
20
  markPassageRendered,
@@ -52,21 +47,68 @@ export function renderPassageContent(passage: PassageData) {
52
47
  }
53
48
 
54
49
  /**
55
- * The content of a special passage shown with every passage (PassageHeader,
56
- * PassageFooter, PassageDone), or null. One that fails to render is logged
57
- * and left out, so it can't take the passage down with it.
50
+ * A passage's rendered content, drawn again only when the passage or its
51
+ * text changes (the props are compared shallowly, so a parent re-rendering
52
+ * with the same passage skips it).
53
+ *
54
+ * It is a component and not a vnode kept by `useMemo`. A kept vnode that is
55
+ * placed again carries Preact's links (its parent, its rendered children) to
56
+ * the tree it was first placed in. A navigation's tree reaches the kept vnode
57
+ * of the passage on its way out, so every tree held the one before it and
58
+ * none was ever freed. The `content` prop is only here so that an edit of
59
+ * the passage text in place draws it again.
58
60
  */
59
- function renderSpecialPassage(passage: PassageData | undefined) {
60
- if (!passage) return null;
61
+ const PassageBody = memo(function PassageBody({
62
+ passage,
63
+ }: {
64
+ passage: PassageData;
65
+ content: string;
66
+ }) {
67
+ try {
68
+ return <>{renderPassageContent(passage)}</>;
69
+ } catch (err) {
70
+ return (
71
+ <div class="error">
72
+ Error parsing passage &ldquo;{passage.name}&rdquo;
73
+ {sourceLocationOf(passage)}: {errorMessage(err)}
74
+ </div>
75
+ );
76
+ }
77
+ });
78
+
79
+ /**
80
+ * A special passage shown with every passage (PassageHeader, PassageFooter,
81
+ * PassageDone), in its wrapper; nothing if it has no content. One that fails
82
+ * to render is logged and left out, so it can't take the passage down with
83
+ * it. Memoized like PassageBody.
84
+ */
85
+ const SpecialPassage = memo(function SpecialPassage({
86
+ passage,
87
+ kind,
88
+ }: {
89
+ passage: PassageData;
90
+ content: string;
91
+ kind: 'header' | 'footer' | 'done';
92
+ }) {
93
+ let content: ComponentChildren;
61
94
  try {
62
- return renderPassageContent(passage);
95
+ content = renderPassageContent(passage);
63
96
  } catch (err) {
64
97
  console.error(`spindle: Error in ${passage.name}:`, err);
65
98
  return null;
66
99
  }
67
- }
100
+ if (!content) return null;
101
+ if (kind === 'done') {
102
+ return (
103
+ <EntryRenderBoundary>
104
+ <div hidden>{content}</div>
105
+ </EntryRenderBoundary>
106
+ );
107
+ }
108
+ return <div class={`passage-${kind}`}>{content}</div>;
109
+ });
68
110
 
69
- interface PassageProps {
111
+ export interface PassageProps {
70
112
  passage: PassageData;
71
113
  dataTransition?: string;
72
114
  /** The navigation this passage displays; omitted for placeholders. */
@@ -102,46 +144,27 @@ export function Passage({
102
144
  const isCodePassage = CODE_PASSAGES.has(passage.name);
103
145
  const [doneReady, setDoneReady] = useState(false);
104
146
 
105
- const content = useMemo(() => {
106
- try {
107
- return renderPassageContent(passage);
108
- } catch (err) {
109
- return (
110
- <div class="error">
111
- Error parsing passage &ldquo;{passage.name}&rdquo;
112
- {sourceLocationOf(passage)}: {errorMessage(err)}
113
- </div>
114
- );
115
- }
116
- }, [passage.content, passage.name]);
117
-
118
147
  // Code passages are shown without the special passages
119
148
  const special = (name: string) =>
120
149
  isCodePassage ? undefined : storyData?.passages.get(name);
121
- const headerPassage = special('PassageHeader');
122
- const footerPassage = special('PassageFooter');
150
+ const specialPassage = (name: string, kind: 'header' | 'footer' | 'done') => {
151
+ const found = special(name);
152
+ return found ? (
153
+ <SpecialPassage
154
+ passage={found}
155
+ content={found.content}
156
+ kind={kind}
157
+ />
158
+ ) : null;
159
+ };
123
160
  const donePassage = special('PassageDone');
124
161
 
125
- const headerContent = useMemo(
126
- () => renderSpecialPassage(headerPassage),
127
- [headerPassage?.content],
128
- );
129
- const footerContent = useMemo(
130
- () => renderSpecialPassage(footerPassage),
131
- [footerPassage?.content],
132
- );
133
-
134
162
  // Defer PassageDone to after DOM commit
135
163
  useEffect(() => {
136
164
  if (donePassage) setDoneReady(true);
137
165
  return () => setDoneReady(false);
138
166
  }, [passage.name]);
139
167
 
140
- const doneContent = useMemo(
141
- () => (doneReady ? renderSpecialPassage(donePassage) : null),
142
- [doneReady, donePassage?.content],
143
- );
144
-
145
168
  // Signal that this passage's DOM is committed (after descendants' layout
146
169
  // effects, before paint). Runs once per mount: PassageDisplay keys the
147
170
  // Passage so every navigation that shows a passage mounts a new one.
@@ -169,14 +192,13 @@ export function Passage({
169
192
  data-tags={passage.tags.join(' ')}
170
193
  data-transition={dataTransition}
171
194
  >
172
- {headerContent && <div class="passage-header">{headerContent}</div>}
173
- {content}
174
- {footerContent && <div class="passage-footer">{footerContent}</div>}
175
- {doneContent && (
176
- <EntryRenderBoundary>
177
- <div hidden>{doneContent}</div>
178
- </EntryRenderBoundary>
179
- )}
195
+ {specialPassage('PassageHeader', 'header')}
196
+ <PassageBody
197
+ passage={passage}
198
+ content={passage.content}
199
+ />
200
+ {specialPassage('PassageFooter', 'footer')}
201
+ {doneReady && specialPassage('PassageDone', 'done')}
180
202
  </div>
181
203
  </ViewScopeContext.Provider>
182
204
  </InterfaceContext.Provider>
@@ -11,7 +11,7 @@ import {
11
11
  FrozenStateContext,
12
12
  OutgoingContext,
13
13
  } from '../../hooks/use-story-fields';
14
- import { Passage, renderPassageContent } from '../Passage';
14
+ import { Passage, renderPassageContent, type PassageProps } from '../Passage';
15
15
  import { defineMacro } from '../../define-macro';
16
16
  import { resolveTransition, type ResolvedTransition } from '../../transition';
17
17
  import { skipString } from './arg-utils';
@@ -807,7 +807,13 @@ defineMacro({
807
807
  () => isOutgoing(useStoryStore.getState()),
808
808
  [isOutgoing],
809
809
  );
810
- const lastElement = useRef<preact.VNode | null>(null);
810
+ // What the passage on display was drawn with (see the render below):
811
+ // props, not the vnode, which a vnode of the next render could not
812
+ // be released from (the parent a vnode is placed under stays with it)
813
+ const lastShown = useRef<{
814
+ key: string;
815
+ props: PassageProps;
816
+ } | null>(null);
811
817
 
812
818
  // Container ref for snapshot insertion
813
819
  const containerRef = useRef<HTMLDivElement>(null);
@@ -1001,20 +1007,30 @@ defineMacro({
1001
1007
 
1002
1008
  // Between a navigation and the commit of its passage, the old passage is
1003
1009
  // still mounted. It must not execute against the destination's state, so
1004
- // it keeps the element and the state it last rendered with (#352).
1010
+ // it is drawn with what it was last drawn with (#352): the same props.
1005
1011
  const outgoing = !renderDeferred && displayed.id !== navigationId;
1006
- const passageElement =
1007
- outgoing && lastElement.current
1008
- ? lastElement.current
1009
- : effectivePassage && (
1010
- <Passage
1011
- passage={effectivePassage}
1012
- key={renderDeferred ? 'loading' : `nav-${displayed.id}`}
1013
- dataTransition={renderDeferred ? 'none' : resolvedTypeRef.current}
1014
- navigationId={renderDeferred ? undefined : displayed.id}
1015
- />
1016
- );
1017
- lastElement.current = passageElement || null;
1012
+ const shown =
1013
+ outgoing && lastShown.current
1014
+ ? lastShown.current
1015
+ : effectivePassage
1016
+ ? {
1017
+ key: renderDeferred ? 'loading' : `nav-${displayed.id}`,
1018
+ props: {
1019
+ passage: effectivePassage,
1020
+ dataTransition: renderDeferred
1021
+ ? 'none'
1022
+ : resolvedTypeRef.current,
1023
+ navigationId: renderDeferred ? undefined : displayed.id,
1024
+ },
1025
+ }
1026
+ : null;
1027
+ const passageElement = shown && (
1028
+ <Passage
1029
+ key={shown.key}
1030
+ {...shown.props}
1031
+ />
1032
+ );
1033
+ lastShown.current = shown;
1018
1034
 
1019
1035
  return (
1020
1036
  <div