@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
|
@@ -640,14 +640,14 @@ function wrapLines(
|
|
|
640
640
|
isBlock: (name: string) => boolean,
|
|
641
641
|
): string[] {
|
|
642
642
|
const result: string[] = [];
|
|
643
|
-
const
|
|
643
|
+
const verbatim = verbatimLines(lines);
|
|
644
644
|
const structured = markdownLines(lines.join('\n'), isBlock, NO_WRAP);
|
|
645
645
|
|
|
646
646
|
for (const [index, line] of lines.entries()) {
|
|
647
647
|
// A `{do}` body is JavaScript: a line break there can change the program
|
|
648
648
|
if (
|
|
649
649
|
line.length <= maxLen ||
|
|
650
|
-
|
|
650
|
+
verbatim.has(index) ||
|
|
651
651
|
structured.has(index) ||
|
|
652
652
|
literalLines.has(line.trim())
|
|
653
653
|
) {
|
|
@@ -692,16 +692,24 @@ function wrapLines(
|
|
|
692
692
|
return result;
|
|
693
693
|
}
|
|
694
694
|
|
|
695
|
-
/**
|
|
696
|
-
function
|
|
695
|
+
/** Lines holding JavaScript or a multiline HTML tag must not be wrapped. */
|
|
696
|
+
function verbatimLines(lines: string[]): Set<number> {
|
|
697
697
|
const found = new Set<number>();
|
|
698
698
|
const text = lines.join('\n');
|
|
699
699
|
const lineAt = sourceLines(text);
|
|
700
|
-
|
|
700
|
+
const { doBlocks, tags } = scanSpindleMarkup(text);
|
|
701
|
+
for (const block of doBlocks) {
|
|
701
702
|
const first = lineAt(block.bodyStart);
|
|
702
703
|
const last = lineAt(block.bodyEnd);
|
|
703
704
|
for (let i = first; i <= last; i++) found.add(i);
|
|
704
705
|
}
|
|
706
|
+
for (const tag of tags) {
|
|
707
|
+
const first = lineAt(tag.start);
|
|
708
|
+
const last = lineAt(tag.end - 1);
|
|
709
|
+
if (first === last) continue;
|
|
710
|
+
// Continuation lines cannot be tokenized as HTML on their own.
|
|
711
|
+
for (let i = first; i <= last; i++) found.add(i);
|
|
712
|
+
}
|
|
705
713
|
return found;
|
|
706
714
|
}
|
|
707
715
|
|
|
@@ -715,7 +723,7 @@ function wordWrap(text: string, maxLen: number, indent: string): string[] {
|
|
|
715
723
|
/** Whether the character is inside a token whose text must stay as written. */
|
|
716
724
|
const inToken = new Array<boolean>(text.length).fill(false);
|
|
717
725
|
for (const token of tokenizeMarkupTolerant(text).tokens) {
|
|
718
|
-
if (token.type === 'text'
|
|
726
|
+
if (token.type === 'text') continue;
|
|
719
727
|
inToken.fill(true, token.start, token.end);
|
|
720
728
|
}
|
|
721
729
|
const room = maxLen - indentColumn(indent);
|
|
@@ -128,15 +128,6 @@ export function multilineJsLiterals(code: string): [number, number][] {
|
|
|
128
128
|
return spans;
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
/** Whether an HTML tag (opening, closing or self-closing) overlaps the range. */
|
|
132
|
-
function lineHasTag(
|
|
133
|
-
tags: { start: number; end: number }[],
|
|
134
|
-
start: number,
|
|
135
|
-
end: number,
|
|
136
|
-
): boolean {
|
|
137
|
-
return tags.some((tag) => tag.start < end && tag.end > start);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
131
|
export interface PlaceholderResult {
|
|
141
132
|
text: string;
|
|
142
133
|
tokens: string[];
|
|
@@ -166,6 +157,8 @@ export const SPINDLE_PLACEHOLDERS: PlaceholdersOf = (i) => [
|
|
|
166
157
|
`__SP${i}__`,
|
|
167
158
|
];
|
|
168
159
|
|
|
160
|
+
const PLACEHOLDER_PATTERN = /<!--(?:SP|SVG):\d+-->|__SP\d+__/g;
|
|
161
|
+
|
|
169
162
|
/**
|
|
170
163
|
* Whether `text` (what a formatter made of the placeholdered text) holds
|
|
171
164
|
* the placeholder of every one of `tokens` exactly once. If not, the
|
|
@@ -177,13 +170,17 @@ export function placeholdersIntact(
|
|
|
177
170
|
tokens: readonly string[],
|
|
178
171
|
placeholdersOf: PlaceholdersOf,
|
|
179
172
|
): boolean {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
)
|
|
173
|
+
if (tokens.length === 0) return true;
|
|
174
|
+
const indices = new Map<string, number>();
|
|
175
|
+
const counts = new Array<number>(tokens.length).fill(0);
|
|
176
|
+
tokens.forEach((_, index) => {
|
|
177
|
+
for (const marker of placeholdersOf(index)) indices.set(marker, index);
|
|
178
|
+
});
|
|
179
|
+
for (const [marker] of text.matchAll(PLACEHOLDER_PATTERN)) {
|
|
180
|
+
const index = indices.get(marker);
|
|
181
|
+
if (index !== undefined && ++counts[index]! > 1) return false;
|
|
182
|
+
}
|
|
183
|
+
return counts.every((count) => count === 1);
|
|
187
184
|
}
|
|
188
185
|
|
|
189
186
|
/** Whether `before` and `after` hold the same Spindle tokens, in the same order. */
|
|
@@ -212,7 +209,7 @@ function restorePlaceholders(
|
|
|
212
209
|
replacements.set(marker, token);
|
|
213
210
|
});
|
|
214
211
|
}
|
|
215
|
-
return text.replace(
|
|
212
|
+
return text.replace(PLACEHOLDER_PATTERN, (marker) => {
|
|
216
213
|
const token = replacements.get(marker);
|
|
217
214
|
replacements.delete(marker);
|
|
218
215
|
// Callback form also preserves literal `$&`, `$'`, etc.
|
|
@@ -276,6 +273,7 @@ export function replaceSpindleTokens(html: string): PlaceholderResult {
|
|
|
276
273
|
const tokens: string[] = [];
|
|
277
274
|
const resultLines: string[] = [];
|
|
278
275
|
let unit = 0;
|
|
276
|
+
let tag = 0;
|
|
279
277
|
let lineStart = 0;
|
|
280
278
|
while (lineStart <= html.length) {
|
|
281
279
|
// A line ends at a line break outside every unit
|
|
@@ -296,10 +294,14 @@ export function replaceSpindleTokens(html: string): PlaceholderResult {
|
|
|
296
294
|
}
|
|
297
295
|
const line = html.slice(lineStart, lineEnd);
|
|
298
296
|
const indent = line.match(/^(\s*)/)?.[1] ?? '';
|
|
297
|
+
// Lines and tags are ordered: visit each tag only once, keeping a tag
|
|
298
|
+
// that spans several lines until its end is behind the current line.
|
|
299
|
+
while (tag < tags.length && tags[tag]!.end <= lineStart) tag++;
|
|
300
|
+
const hasTag = tag < tags.length && tags[tag]!.start < lineEnd;
|
|
299
301
|
|
|
300
302
|
if (lineUnits.length === 0) {
|
|
301
303
|
resultLines.push(line);
|
|
302
|
-
} else if (!
|
|
304
|
+
} else if (!hasTag) {
|
|
303
305
|
// Spindle tokens but no HTML: one placeholder for the whole line
|
|
304
306
|
const idx = tokens.length;
|
|
305
307
|
tokens.push(line.trim());
|
package/src/index.tsx
CHANGED
|
@@ -101,9 +101,10 @@ function storeVarMacroNames(): string[] {
|
|
|
101
101
|
function variableErrors(
|
|
102
102
|
storyData: StoryData,
|
|
103
103
|
schema: Parameters<typeof validatePassages>[1],
|
|
104
|
+
only: (passage: MarkupPassage) => boolean = () => true,
|
|
104
105
|
): string[] {
|
|
105
106
|
return validatePassages(
|
|
106
|
-
storyData.passages,
|
|
107
|
+
new Map([...storyData.passages].filter(([, passage]) => only(passage))),
|
|
107
108
|
schema,
|
|
108
109
|
storeVarMacroNames(),
|
|
109
110
|
knownParameters(),
|
|
@@ -168,7 +169,7 @@ export function boot() {
|
|
|
168
169
|
// External scripts can register custom macros (including block macros) here.
|
|
169
170
|
document.dispatchEvent(new CustomEvent(':storystartup'));
|
|
170
171
|
|
|
171
|
-
// Parse StoryVariables and validate
|
|
172
|
+
// Parse StoryVariables and validate StoryInit before it executes
|
|
172
173
|
let defaults: Record<string, unknown> = {};
|
|
173
174
|
const storyVarsPassage = storyData.passages.get('StoryVariables');
|
|
174
175
|
|
|
@@ -192,7 +193,11 @@ export function boot() {
|
|
|
192
193
|
setBlockWidget(name, block);
|
|
193
194
|
}
|
|
194
195
|
|
|
195
|
-
const errors = variableErrors(
|
|
196
|
+
const errors = variableErrors(
|
|
197
|
+
storyData,
|
|
198
|
+
schema,
|
|
199
|
+
(p) => p.name === 'StoryInit',
|
|
200
|
+
);
|
|
196
201
|
// StoryInit's markup must be valid before it runs; the other passages are
|
|
197
202
|
// validated once it has run, as it may define macros (see below).
|
|
198
203
|
errors.push(...markupErrors(storyData, (p) => p.name === 'StoryInit'));
|
package/src/tooling.ts
CHANGED
|
@@ -192,11 +192,11 @@ export function validateStoryMarkup(
|
|
|
192
192
|
const all = [...macros];
|
|
193
193
|
const known = new Set<string>();
|
|
194
194
|
// The calls of widgets that declare what their arguments hold are checked
|
|
195
|
-
const widgets =
|
|
195
|
+
const widgets = effectiveWidgetDefinitions(
|
|
196
|
+
readWidgetDefinitions(list, parameterLookup(all)),
|
|
197
|
+
);
|
|
196
198
|
const blocks = new Set(
|
|
197
|
-
|
|
198
|
-
.filter((def) => def.block)
|
|
199
|
-
.map((def) => def.name.toLowerCase()),
|
|
199
|
+
widgets.filter((def) => def.block).map((def) => def.name.toLowerCase()),
|
|
200
200
|
);
|
|
201
201
|
const parametersOf = parameterLookup([
|
|
202
202
|
...all,
|
|
@@ -315,7 +315,9 @@ function referenceOptions(
|
|
|
315
315
|
passages: readonly MarkupPassage[] = [],
|
|
316
316
|
) {
|
|
317
317
|
const list = [...macros];
|
|
318
|
-
const widgets =
|
|
318
|
+
const widgets = effectiveWidgetDefinitions(
|
|
319
|
+
readWidgetDefinitions(passages, parameterLookup(list)),
|
|
320
|
+
);
|
|
319
321
|
return {
|
|
320
322
|
all,
|
|
321
323
|
storeVarMacros: new Set(
|
package/src/utils/object-path.ts
CHANGED
|
@@ -100,7 +100,18 @@ export function setByPath(
|
|
|
100
100
|
options: SetByPathOptions = {},
|
|
101
101
|
): void {
|
|
102
102
|
const parent = walkToParent(root, segments, options.createMissing ?? false);
|
|
103
|
-
|
|
103
|
+
const key = segments[segments.length - 1]!;
|
|
104
|
+
if (
|
|
105
|
+
value === 0 &&
|
|
106
|
+
isDraft(parent) &&
|
|
107
|
+
parent[key] === 0 &&
|
|
108
|
+
!Object.is(parent[key], value) &&
|
|
109
|
+
!(Array.isArray(parent) && key === 'length')
|
|
110
|
+
) {
|
|
111
|
+
// Immer's assignment uses === after copying, losing a zero's changed sign.
|
|
112
|
+
parent[key] = undefined;
|
|
113
|
+
}
|
|
114
|
+
parent[key] = value;
|
|
104
115
|
}
|
|
105
116
|
|
|
106
117
|
/**
|