@rohal12/spindle 0.64.30 → 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.
@@ -27,6 +27,7 @@ export {
27
27
  parseBoundVariable,
28
28
  effectiveWidgetDefinitions,
29
29
  findCodeEnd,
30
+ indexStrictCodeEnds,
30
31
  lexJs,
31
32
  lexTemplate,
32
33
  scanStringLiteral,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.64.30",
3
+ "version": "0.64.32",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
package/src/code-check.ts CHANGED
@@ -55,7 +55,11 @@ import {
55
55
  type PassageTarget,
56
56
  } from './components/macros/macro-args';
57
57
  import { subMacroParameters } from './components/macros/option-utils';
58
- import { readPassageBody, splitArgSpans } from './components/macros/arg-utils';
58
+ import {
59
+ readPassageBody,
60
+ readWholeQuoted,
61
+ splitArgSpans,
62
+ } from './components/macros/arg-utils';
59
63
  import { effectiveNamedDefinitions, NameMap } from './utils/macro-names';
60
64
  import { hasOwn } from './utils/namespace';
61
65
  import { isBuiltinBlockMacro, isRawMacro } from './markup/ast';
@@ -111,13 +115,14 @@ export interface PieceBase {
111
115
  * For the code or text of a quoted string with escapes (a `\"`), whose
112
116
  * characters are not where `offset` plus their index says: the offset of
113
117
  * each character in the source, and of the end of the text, so that
114
- * `sourceOffsets[i]` is where character `i` of `code` or `text` is. Absent
118
+ * `sourceOffsets[i]` is where character `i` of `code`, `text` or a passage
119
+ * `name` is. The final entry maps the end boundary. Absent
115
120
  * when `offset + i` is (see {@link pieceOffset}).
116
121
  */
117
122
  sourceOffsets?: readonly number[];
118
123
  }
119
124
 
120
- /** Where character `index` of the code or text of `piece` is in the source. */
125
+ /** Where boundary `index` of a piece's code, text or name is in the source. */
121
126
  export function pieceOffset(piece: PieceBase, index: number): number {
122
127
  return piece.sourceOffsets?.[index] ?? piece.offset + index;
123
128
  }
@@ -128,7 +133,9 @@ function extentOf(piece: Piece): number {
128
133
  ? piece.code.length
129
134
  : piece.kind === 'text'
130
135
  ? piece.text.length
131
- : piece.length;
136
+ : piece.kind === 'passage'
137
+ ? piece.name.length
138
+ : piece.length;
132
139
  }
133
140
 
134
141
  /** A piece found in a markup that is written out, for an error to point at. */
@@ -167,6 +174,12 @@ interface MacroPiece extends LabeledPiece {
167
174
  export interface PassagePiece extends MacroPiece {
168
175
  kind: 'passage';
169
176
  name: string;
177
+ /**
178
+ * Decoded name boundaries in the original source, including the end before
179
+ * closing quotes. Quotes/escapes do not change the whole-argument offset
180
+ * and length. Both UTF-16 units of a code-point escape map to its start.
181
+ */
182
+ sourceOffsets?: readonly number[];
170
183
  /** A known navigation target, rather than included/dialog content. */
171
184
  navigation?: true;
172
185
  }
@@ -530,13 +543,26 @@ export function* codeAndText(
530
543
  if (close > t + 1) {
531
544
  const text = src.slice(token.end, tokens[close]!.start);
532
545
  const written = text.trim();
546
+ const at = token.end + text.indexOf(written);
547
+ const nameValue = readPassageBody(written);
548
+ const sourceOffsets =
549
+ readWholeQuoted(written) !== null
550
+ ? escapeOffsets(
551
+ written.slice(1, -1),
552
+ at + 1,
553
+ nameValue,
554
+ 'quotes',
555
+ true,
556
+ )
557
+ : undefined;
533
558
  yield {
534
559
  kind: 'passage',
535
- name: readPassageBody(written),
536
- offset: token.end + text.indexOf(written),
560
+ name: nameValue,
561
+ offset: at,
537
562
  length: written.length,
538
563
  macro: name,
539
564
  label: src.slice(token.start, token.end),
565
+ ...(sourceOffsets && { sourceOffsets }),
540
566
  };
541
567
  }
542
568
  }
@@ -702,7 +728,7 @@ function nestedPiece(
702
728
  ): Piece {
703
729
  const offset = toRoot(piece.offset);
704
730
  const nested = { offset, nested: true as const, where };
705
- if (piece.kind === 'passage' || piece.kind === 'argument-error') {
731
+ if (piece.kind === 'argument-error') {
706
732
  const length = toRoot(piece.offset + piece.length) - offset;
707
733
  return { ...piece, ...nested, length };
708
734
  }
@@ -712,9 +738,15 @@ function nestedPiece(
712
738
  : toRoot(piece.offset + extent) - offset !== extent
713
739
  ? Array.from({ length: extent + 1 }, (_, i) => toRoot(piece.offset + i))
714
740
  : undefined;
715
- return sourceOffsets
741
+ const mapped = sourceOffsets
716
742
  ? { ...piece, ...nested, sourceOffsets }
717
743
  : { ...piece, ...nested };
744
+ return mapped.kind === 'passage'
745
+ ? {
746
+ ...mapped,
747
+ length: toRoot(piece.offset + mapped.length) - offset,
748
+ }
749
+ : mapped;
718
750
  }
719
751
 
720
752
  /** How the string of an argument is unescaped, which its escapes depend on. */
@@ -754,13 +786,15 @@ function escapeAt(
754
786
  * written as `raw` from `at` on (its escapes are those of `escapes`), is in
755
787
  * the source, and where the text ends. A character that an escape stands for
756
788
  * is where the escape begins. None when the text is where its length says, or
757
- * is not that string.
789
+ * is not that string. With `includeLinear`, return the map even without
790
+ * escapes (a passage piece's offset includes the opening quote).
758
791
  */
759
792
  export function escapeOffsets(
760
793
  raw: string,
761
794
  at: number,
762
795
  value: string,
763
796
  escapes: Escapes,
797
+ includeLinear = false,
764
798
  ): number[] | undefined {
765
799
  const offsets: number[] = [];
766
800
  let escaped = false;
@@ -778,8 +812,16 @@ export function escapeOffsets(
778
812
  escaped = true;
779
813
  i += escape.length;
780
814
  }
815
+ // A trailing line continuation emits no character but still moves the
816
+ // decoded string's end boundary to the closing quote.
817
+ while (raw[i] === '\\') {
818
+ const escape = escapeAt(raw, i, escapes);
819
+ if (!escape || escape.text.length !== 0) break;
820
+ escaped = true;
821
+ i += escape.length;
822
+ }
781
823
  offsets.push(at + i);
782
- return escaped ? offsets : undefined;
824
+ return escaped || includeLinear ? offsets : undefined;
783
825
  }
784
826
 
785
827
  /** The code and text in the arguments `args` (at `offset`) of `macro`. */
@@ -894,13 +936,24 @@ function* valuePieces(
894
936
  const [start, end] = span;
895
937
  const written = args.slice(start, end);
896
938
  /** The name as written (quotes included), at the argument. */
897
- const passage = (passageName: string): PassagePiece => ({
939
+ const passage = (
940
+ passageName: string,
941
+ at: number,
942
+ mode: Escapes = escapes,
943
+ ): PassagePiece => ({
898
944
  kind: 'passage',
899
945
  name: passageName,
900
946
  offset: offset + start,
901
947
  length: end - start,
902
948
  macro: name,
903
949
  label,
950
+ sourceOffsets: escapeOffsets(
951
+ args.slice(at - offset),
952
+ at,
953
+ passageName,
954
+ mode,
955
+ true,
956
+ ),
904
957
  ...(navigation.includes(param.name) && { navigation: true as const }),
905
958
  });
906
959
  const goal = codeGoal(param);
@@ -908,7 +961,7 @@ function* valuePieces(
908
961
  // A name written out, else an expression naming one
909
962
  const target = passageTarget(value);
910
963
  if (target.kind === 'name') {
911
- yield passage(target.name);
964
+ yield passage(target.name, offset + start + 1, 'javascript');
912
965
  continue;
913
966
  }
914
967
  }
@@ -949,7 +1002,7 @@ function* valuePieces(
949
1002
  if (sourceOffsets) piece.sourceOffsets = sourceOffsets;
950
1003
  yield piece;
951
1004
  } else if (holds === 'passage') {
952
- yield passage(value);
1005
+ yield passage(value, at);
953
1006
  } else if (holds === 'markup') {
954
1007
  const piece = textPiece(
955
1008
  value,
@@ -13,12 +13,16 @@ defineMacro({
13
13
  render(_props, ctx) {
14
14
  const label = ctx.args.label ?? '';
15
15
 
16
- const action = useVariableAction(ctx, {
17
- type: 'checkbox',
18
- label: label || ctx.varName || '',
19
- value: !!ctx.value,
20
- perform: (v) => ctx.setValue!(v !== undefined ? !!v : !ctx.getValue!()),
21
- });
16
+ const action = useVariableAction(
17
+ ctx,
18
+ {
19
+ type: 'checkbox',
20
+ label: label || ctx.varName || '',
21
+ value: !!ctx.value,
22
+ perform: (v) => ctx.setValue!(v !== undefined ? !!v : !ctx.getValue!()),
23
+ },
24
+ (control) => (control as HTMLInputElement).checked,
25
+ );
22
26
 
23
27
  return (
24
28
  <label
@@ -13,14 +13,18 @@ defineMacro({
13
13
  render({ children = [] }, ctx) {
14
14
  const options = ctx.extractOptions(children);
15
15
 
16
- const action = useVariableAction(ctx, {
17
- type: 'listbox',
18
- label: ctx.varName!,
19
- options,
20
- perform: (v) => {
21
- if (v !== undefined) ctx.setValue!(String(v));
16
+ const action = useVariableAction(
17
+ ctx,
18
+ {
19
+ type: 'listbox',
20
+ label: ctx.varName!,
21
+ options,
22
+ perform: (v) => {
23
+ if (v !== undefined) ctx.setValue!(String(v));
24
+ },
22
25
  },
23
- });
26
+ (control) => control.value,
27
+ );
24
28
 
25
29
  return (
26
30
  <select
@@ -1,8 +1,9 @@
1
1
  import { actionAttributes, useControlAction } from '../../hooks/use-action';
2
2
  import { useState } from 'preact/hooks';
3
+ import { useSyncExternalStore } from 'preact/compat';
3
4
  import { useStoryStore } from '../../store';
4
5
  import { useStoryFields } from '../../hooks/use-story-fields';
5
- import { settings } from '../../settings';
6
+ import { settings, subscribeSettings } from '../../settings';
6
7
  import { quickLoad, quickSave } from '../../quick-actions';
7
8
  import { defineMacro } from '../../define-macro';
8
9
  import { PassageDialog } from '../PassageDialog';
@@ -15,7 +16,7 @@ interface MenubarActionConfig {
15
16
  actionType: ActionType;
16
17
  title?: string;
17
18
  confirm?: string;
18
- hidden?: () => boolean;
19
+ useHidden?: () => boolean;
19
20
  setup?: () => {
20
21
  perform: () => void | Promise<void>;
21
22
  disabled?: boolean;
@@ -34,62 +35,69 @@ function hotkeyTitle(label: string, key: string | null): string {
34
35
  }
35
36
 
36
37
  export function defineMenubarAction(config: MenubarActionConfig) {
38
+ // Visibility can change after mount. Keep the button's hooks and action
39
+ // registration in a child whose lifetime follows that visibility.
40
+ function Button({ className, id }: { className?: string; id?: string }) {
41
+ const [dialogOpen, setDialogOpen] = useState(false);
42
+
43
+ const setup = config.setup?.() ?? { perform: () => {} };
44
+ const { disabled, title = config.title } = setup;
45
+
46
+ const cls = className ? `menubar-button ${className}` : 'menubar-button';
47
+
48
+ const perform = config.dialog
49
+ ? () => setDialogOpen(true)
50
+ : config.confirm
51
+ ? () => {
52
+ if (confirm(config.confirm!)) setup.perform();
53
+ }
54
+ : setup.perform;
55
+
56
+ const action = useControlAction({
57
+ type: config.actionType,
58
+ key: config.name,
59
+ authorId: id,
60
+ label: typeof config.label === 'string' ? config.label : config.name,
61
+ disabled,
62
+ perform: config.dialog ? () => setDialogOpen(true) : setup.perform,
63
+ });
64
+
65
+ return (
66
+ <>
67
+ <button
68
+ type="button"
69
+ {...actionAttributes(action, { id, cls })}
70
+ onClick={perform}
71
+ disabled={disabled}
72
+ title={title}
73
+ >
74
+ {config.label}
75
+ </button>
76
+ {config.dialog && dialogOpen && (
77
+ <PassageDialog
78
+ passageName={config.dialog.passageName}
79
+ fallbackMarkup={config.dialog.fallbackMarkup}
80
+ panelClass={config.dialog.panelClass}
81
+ label={
82
+ typeof config.label === 'string' ? config.label : config.name
83
+ }
84
+ onClose={() => setDialogOpen(false)}
85
+ showCloseButton={config.dialog.showCloseButton}
86
+ />
87
+ )}
88
+ </>
89
+ );
90
+ }
91
+
37
92
  defineMacro({
38
93
  name: config.name,
39
94
  interpolate: true,
40
95
  render(_, ctx) {
41
- if (config.hidden?.()) return null;
42
-
43
- const [dialogOpen, setDialogOpen] = useState(false);
44
-
45
- const setup = config.setup?.() ?? { perform: () => {} };
46
- const { disabled, title = config.title } = setup;
47
-
48
- const cls = ctx.className
49
- ? `menubar-button ${ctx.className}`
50
- : 'menubar-button';
51
-
52
- const perform = config.dialog
53
- ? () => setDialogOpen(true)
54
- : config.confirm
55
- ? () => {
56
- if (confirm(config.confirm!)) setup.perform();
57
- }
58
- : setup.perform;
59
-
60
- const action = useControlAction({
61
- type: config.actionType,
62
- key: config.name,
63
- authorId: ctx.id,
64
- label: typeof config.label === 'string' ? config.label : config.name,
65
- disabled,
66
- perform: config.dialog ? () => setDialogOpen(true) : setup.perform,
67
- });
68
-
69
- return (
70
- <>
71
- <button
72
- type="button"
73
- {...actionAttributes(action, { id: ctx.id, cls })}
74
- onClick={perform}
75
- disabled={disabled}
76
- title={title}
77
- >
78
- {config.label}
79
- </button>
80
- {config.dialog && dialogOpen && (
81
- <PassageDialog
82
- passageName={config.dialog.passageName}
83
- fallbackMarkup={config.dialog.fallbackMarkup}
84
- panelClass={config.dialog.panelClass}
85
- label={
86
- typeof config.label === 'string' ? config.label : config.name
87
- }
88
- onClose={() => setDialogOpen(false)}
89
- showCloseButton={config.dialog.showCloseButton}
90
- />
91
- )}
92
- </>
96
+ return config.useHidden?.() ? null : (
97
+ <Button
98
+ className={ctx.className}
99
+ id={ctx.id}
100
+ />
93
101
  );
94
102
  },
95
103
  });
@@ -173,7 +181,7 @@ defineMenubarAction({
173
181
  name: 'settings',
174
182
  label: 'Settings',
175
183
  actionType: 'dialog',
176
- hidden: () => !settings.hasAny(),
184
+ useHidden: () => !useSyncExternalStore(subscribeSettings, settings.hasAny),
177
185
  dialog: {
178
186
  passageName: 'Settings',
179
187
  fallbackMarkup: '{settings-controls}',
@@ -15,6 +15,7 @@ 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';
18
+ import { InterfaceContext } from '../../markup/render';
18
19
 
19
20
  /** Input types whose `value` is not what the user sees or edits. */
20
21
  const VALUELESS_INPUTS = new Set([
@@ -1107,7 +1108,9 @@ defineMacro({
1107
1108
  key={`ready-${navigationId}`}
1108
1109
  hidden
1109
1110
  >
1110
- {renderPassageContent(readyPassage)}
1111
+ <InterfaceContext.Provider value={false}>
1112
+ {renderPassageContent(readyPassage)}
1113
+ </InterfaceContext.Provider>
1111
1114
  </div>
1112
1115
  )}
1113
1116
  <div
@@ -17,13 +17,32 @@ defineMacro({
17
17
  const label = ctx.args.label ?? '';
18
18
  // One group per variable in each view (see ViewScopeContext)
19
19
  const view = ctx.hooks.useContext(ViewScopeContext);
20
+ const initialValue = ctx.hooks.useRef(ctx.value);
20
21
 
21
- const action = useVariableAction(ctx, {
22
- type: 'radiobutton',
23
- key: `$${ctx.varName}:${radioValue}`,
24
- label: label || radioValue,
25
- perform: () => ctx.setValue!(radioValue),
26
- });
22
+ const action = useVariableAction(
23
+ ctx,
24
+ {
25
+ type: 'radiobutton',
26
+ key: `$${ctx.varName}:${radioValue}`,
27
+ label: label || radioValue,
28
+ perform: () => ctx.setValue!(radioValue),
29
+ },
30
+ (control) => {
31
+ const radio = control as HTMLInputElement;
32
+ const group = radio.form?.elements.namedItem(radio.name) as
33
+ RadioNodeList | HTMLInputElement | null;
34
+ const selected =
35
+ group &&
36
+ ('length' in group
37
+ ? (Array.from(group).find(
38
+ (node) => (node as HTMLInputElement).checked,
39
+ ) as HTMLInputElement | undefined)
40
+ : group.checked
41
+ ? group
42
+ : undefined);
43
+ return selected ? selected.value : initialValue.current;
44
+ },
45
+ );
27
46
 
28
47
  return (
29
48
  <label
@@ -1,5 +1,11 @@
1
1
  import { useEffect, useId, useState } from 'preact/hooks';
2
- import { settings, subscribeSettings, type SettingDef } from '../../settings';
2
+ import { useSyncExternalStore } from 'preact/compat';
3
+ import {
4
+ settings,
5
+ subscribeSettings,
6
+ getSettingsRevision,
7
+ type SettingDef,
8
+ } from '../../settings';
3
9
  import { defineMacro } from '../../define-macro';
4
10
 
5
11
  function SettingRow({
@@ -117,6 +123,7 @@ function SettingsControlsContent({
117
123
  className?: string;
118
124
  id?: string;
119
125
  }) {
126
+ useSyncExternalStore(subscribeSettings, getSettingsRevision);
120
127
  const defs = settings.getDefinitions();
121
128
 
122
129
  return (