@rohal12/spindle 0.64.31 → 0.64.32

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,4 +1,6 @@
1
1
  import { findCodeEnd, parsedCodeEnd } from '../js-lexer';
2
+ import { escapeRegExp } from '../components/macros/arg-utils';
3
+ import { upperBound } from '../utils/sorted';
2
4
 
3
5
  /**
4
6
  * Where the JavaScript inside markup ends. The grammar (spindle.peggy) leaves
@@ -161,6 +163,10 @@ export type OnOpenCodeEnd = (input: string, codeStart: number) => void;
161
163
  * input (see `OnOpenCodeEnd`); the ends it finds are the same.
162
164
  */
163
165
  export function jsCodeEnd(onOpenEnd?: OnOpenCodeEnd): CodeEnd {
166
+ // Keep only the latest immutable source. Missing closers are indexed too,
167
+ // so every unclosed raw opener does not search the same suffix again.
168
+ let indexedInput = '';
169
+ const rawClosers = new Map<string, number[]>();
164
170
  return {
165
171
  closeBrace(input, codeStart, lenientStart) {
166
172
  let ranOut = false;
@@ -172,10 +178,21 @@ export function jsCodeEnd(onOpenEnd?: OnOpenCodeEnd): CodeEnd {
172
178
  return end;
173
179
  },
174
180
  rawBodyEnd(input, bodyStart, name) {
175
- const closer = `\\{/${name}\\s*\\}`;
176
- const first = new RegExp(closer, 'gi');
177
- first.lastIndex = bodyStart;
178
- const firstAt = first.exec(input)?.index ?? -1;
181
+ if (indexedInput !== input) {
182
+ indexedInput = input;
183
+ rawClosers.clear();
184
+ }
185
+ name = name.toLowerCase();
186
+ const closer = `\\{/${escapeRegExp(name)}\\s*\\}`;
187
+ let candidates = rawClosers.get(name);
188
+ if (!candidates) {
189
+ candidates = Array.from(
190
+ input.matchAll(new RegExp(closer, 'gi')),
191
+ (m) => m.index,
192
+ );
193
+ rawClosers.set(name, candidates);
194
+ }
195
+ const firstAt = candidates[upperBound(candidates, bodyStart - 1)] ?? -1;
179
196
  if (firstAt === -1) return -1;
180
197
  const at = new RegExp(closer, 'iy');
181
198
  let ranOut = false;
@@ -192,7 +209,8 @@ export function jsCodeEnd(onOpenEnd?: OnOpenCodeEnd): CodeEnd {
192
209
  // with its `}` follows
193
210
  if (
194
211
  onOpenEnd &&
195
- (ranOut || new RegExp(`\\{/${name}\\s*$`, 'i').test(input))
212
+ (ranOut ||
213
+ new RegExp(`\\{/${escapeRegExp(name)}\\s*$`, 'i').test(input))
196
214
  ) {
197
215
  onOpenEnd(input, bodyStart);
198
216
  }
@@ -972,7 +972,7 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
972
972
  );
973
973
  }
974
974
  const element = authorElement(
975
- namespace === 'svg' ? svgTagName(node.tag) : node.tag,
975
+ namespace === 'svg' ? svgTagName(node.tag) : tag,
976
976
  props,
977
977
  children,
978
978
  namespace === 'math',
@@ -232,7 +232,8 @@ export function observableText(
232
232
  .map((line) => line.trimEnd())
233
233
  .join('\n')
234
234
  .replace(/\n{3,}/g, '\n\n')
235
- .replace(/^\n+|\s+$/g, '')
235
+ .replace(/^\n+/, '')
236
+ .trimEnd()
236
237
  );
237
238
  }
238
239
 
package/src/story-api.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { useStoryStore, trackRuntimeUnsub, recordHookWrite } from './store';
2
+ import { notifyVariableWrite } from './utils/variable-writes';
2
3
  import type { StoryState, VariableNamespaces } from './store';
3
4
  import { freeze } from 'immer';
4
5
  import {
@@ -350,7 +351,11 @@ function createStoryAPI(): StoryAPI {
350
351
  for (const [name] of entries) {
351
352
  const { isTransient, key } = parseName(name);
352
353
  warnIfUndeclared(isTransient, key);
353
- if (!isTransient) recordHookWrite(key.split('.'));
354
+ if (!isTransient) {
355
+ const path = key.split('.');
356
+ recordHookWrite(path);
357
+ notifyVariableWrite(path);
358
+ }
354
359
  }
355
360
  // One store update for all keys, in program order with any running
356
361
  // mutation. Nested writes use the mutation merge to preserve aliases.
@@ -0,0 +1,24 @@
1
+ /** Explicit writes, including assignments that leave a binding unchanged. */
2
+ const listeners = new Set<(path: readonly string[]) => void>();
3
+
4
+ export function notifyVariableWrite(path: readonly string[]): void {
5
+ for (const listener of listeners) listener(path);
6
+ }
7
+
8
+ /** Observe writes to a binding, its parent, or one of its children. */
9
+ export function subscribeVariableWrites(
10
+ binding: readonly string[],
11
+ onWrite: () => void,
12
+ ): () => void {
13
+ const listener = (path: readonly string[]) => {
14
+ const length = Math.min(path.length, binding.length);
15
+ for (let i = 0; i < length; i++) {
16
+ if (path[i] !== binding[i]) return;
17
+ }
18
+ onWrite();
19
+ };
20
+ listeners.add(listener);
21
+ return () => {
22
+ listeners.delete(listener);
23
+ };
24
+ }
@@ -866,13 +866,14 @@ export interface PieceBase {
866
866
  * For the code or text of a quoted string with escapes (a `\"`), whose
867
867
  * characters are not where `offset` plus their index says: the offset of
868
868
  * each character in the source, and of the end of the text, so that
869
- * `sourceOffsets[i]` is where character `i` of `code` or `text` is. Absent
869
+ * `sourceOffsets[i]` is where character `i` of `code`, `text` or a passage
870
+ * `name` is. The final entry maps the end boundary. Absent
870
871
  * when `offset + i` is (see {@link pieceOffset}).
871
872
  */
872
873
  sourceOffsets?: readonly number[];
873
874
  }
874
875
 
875
- /** Where character `index` of the code or text of `piece` is in the source. */
876
+ /** Where boundary `index` of a piece's code, text or name is in the source. */
876
877
  export declare function pieceOffset(piece: PieceBase, index: number): number;
877
878
 
878
879
  /**
@@ -901,6 +902,12 @@ export interface CodePiece extends PieceBase {
901
902
  export interface PassagePiece extends PieceBase {
902
903
  kind: 'passage';
903
904
  name: string;
905
+ /**
906
+ * Decoded name boundaries in the original source, including the end before
907
+ * closing quotes. Quotes/escapes do not change the whole-argument offset
908
+ * and length. Both UTF-16 units of a code-point escape map to its start.
909
+ */
910
+ sourceOffsets?: readonly number[];
904
911
  /** A known navigation target, rather than included/dialog content. */
905
912
  navigation?: true;
906
913
  /** The markup it is in, for an error: `[[Go->Hall]]`. */