@rohal12/spindle 0.64.14 → 0.64.16
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 +79 -64
- package/dist/pkg/story-variables.js +62 -57
- package/package.json +2 -2
- package/src/components/macros/detached-body.tsx +3 -1
- package/src/markup/code-end.ts +69 -41
- package/src/markup/render.tsx +17 -3
- package/src/store.ts +7 -2
- package/src/twine-editor/mode.ts +10 -5
package/dist/pkg/headless.js
CHANGED
|
@@ -13617,41 +13617,6 @@ function lastEnd(body) {
|
|
|
13617
13617
|
*/
|
|
13618
13618
|
var NON_STRING_QUOTE_PREFIX = /[\p{L}\p{N}_\\]/u;
|
|
13619
13619
|
/**
|
|
13620
|
-
* Skip the literal opening at `i`: a '…' or "…" string, which must close on
|
|
13621
|
-
* its line, or a `…` template, whose `${…}` parts are read as braces.
|
|
13622
|
-
* Returns the index just past its closing quote or backtick, or -1.
|
|
13623
|
-
*/
|
|
13624
|
-
function skipLiteral(input, i) {
|
|
13625
|
-
const quote = input[i];
|
|
13626
|
-
const template = quote === "`";
|
|
13627
|
-
let ranOut = false;
|
|
13628
|
-
for (let j = i + 1; j < input.length;) {
|
|
13629
|
-
const c = input[j];
|
|
13630
|
-
if (c === "\\") j += 2;
|
|
13631
|
-
else if (c === quote) return {
|
|
13632
|
-
end: j + 1,
|
|
13633
|
-
ranOut
|
|
13634
|
-
};
|
|
13635
|
-
else if (!template && c === "\n") return {
|
|
13636
|
-
end: -1,
|
|
13637
|
-
ranOut
|
|
13638
|
-
};
|
|
13639
|
-
else if (template && c === "$" && input[j + 1] === "{") {
|
|
13640
|
-
const close = lenientRead(input, j + 2);
|
|
13641
|
-
ranOut ||= close.ranOut;
|
|
13642
|
-
if (close.end === -1) return {
|
|
13643
|
-
end: -1,
|
|
13644
|
-
ranOut
|
|
13645
|
-
};
|
|
13646
|
-
j = close.end + 1;
|
|
13647
|
-
} else j++;
|
|
13648
|
-
}
|
|
13649
|
-
return {
|
|
13650
|
-
end: -1,
|
|
13651
|
-
ranOut: ranOut || template
|
|
13652
|
-
};
|
|
13653
|
-
}
|
|
13654
|
-
/**
|
|
13655
13620
|
* The closing `}` of braces read leniently from `i` (just past a `{`): braces
|
|
13656
13621
|
* count except inside string and template literals, and a quote that can't
|
|
13657
13622
|
* start a string (an apostrophe, or one not closed on its line) is text, as
|
|
@@ -13677,31 +13642,71 @@ function lenientRead(input, i) {
|
|
|
13677
13642
|
}
|
|
13678
13643
|
var memoInput = "";
|
|
13679
13644
|
var memo = /* @__PURE__ */ new Map();
|
|
13680
|
-
function scanLenient(input,
|
|
13681
|
-
|
|
13682
|
-
|
|
13683
|
-
|
|
13684
|
-
|
|
13685
|
-
|
|
13686
|
-
|
|
13687
|
-
|
|
13688
|
-
|
|
13689
|
-
|
|
13690
|
-
end: i,
|
|
13691
|
-
ranOut
|
|
13692
|
-
};
|
|
13693
|
-
depth--;
|
|
13694
|
-
i++;
|
|
13695
|
-
} else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) {
|
|
13696
|
-
const close = skipLiteral(input, i);
|
|
13697
|
-
ranOut ||= close.ranOut;
|
|
13698
|
-
i = close.end === -1 ? i + 1 : close.end;
|
|
13699
|
-
} else i++;
|
|
13700
|
-
}
|
|
13701
|
-
return {
|
|
13645
|
+
function scanLenient(input, start) {
|
|
13646
|
+
const codeFrame = (start) => ({
|
|
13647
|
+
kind: "code",
|
|
13648
|
+
start,
|
|
13649
|
+
i: start,
|
|
13650
|
+
depth: 0,
|
|
13651
|
+
ranOut: false
|
|
13652
|
+
});
|
|
13653
|
+
const frames = [codeFrame(start)];
|
|
13654
|
+
let result = {
|
|
13702
13655
|
end: -1,
|
|
13703
13656
|
ranOut: true
|
|
13704
13657
|
};
|
|
13658
|
+
const finish = (end, ranOut) => {
|
|
13659
|
+
const frame = frames.pop();
|
|
13660
|
+
result = {
|
|
13661
|
+
end,
|
|
13662
|
+
ranOut
|
|
13663
|
+
};
|
|
13664
|
+
if (frame.kind === "code") memo.set(frame.start, result);
|
|
13665
|
+
const parent = frames[frames.length - 1];
|
|
13666
|
+
if (!parent) return;
|
|
13667
|
+
parent.ranOut ||= ranOut;
|
|
13668
|
+
if (parent.kind === "code") parent.i = end === -1 ? parent.i + 1 : end;
|
|
13669
|
+
else if (end === -1) parent.i = input.length;
|
|
13670
|
+
else parent.i = end + 1;
|
|
13671
|
+
};
|
|
13672
|
+
while (frames.length) {
|
|
13673
|
+
const frame = frames[frames.length - 1];
|
|
13674
|
+
const { i, ranOut } = frame;
|
|
13675
|
+
if (i >= input.length) {
|
|
13676
|
+
finish(-1, ranOut || frame.kind === "code" || frame.quote === "`");
|
|
13677
|
+
continue;
|
|
13678
|
+
}
|
|
13679
|
+
const c = input[i];
|
|
13680
|
+
if (frame.kind === "literal") {
|
|
13681
|
+
if (c === "\\") frame.i += 2;
|
|
13682
|
+
else if (c === frame.quote) finish(i + 1, ranOut);
|
|
13683
|
+
else if (frame.quote !== "`" && c === "\n") finish(-1, ranOut);
|
|
13684
|
+
else if (frame.quote === "`" && c === "$" && input[i + 1] === "{") {
|
|
13685
|
+
const cached = memo.get(i + 2);
|
|
13686
|
+
if (cached) {
|
|
13687
|
+
frame.ranOut ||= cached.ranOut;
|
|
13688
|
+
frame.i = cached.end === -1 ? input.length : cached.end + 1;
|
|
13689
|
+
} else frames.push(codeFrame(i + 2));
|
|
13690
|
+
} else frame.i++;
|
|
13691
|
+
} else if (c === "{") {
|
|
13692
|
+
frame.depth++;
|
|
13693
|
+
frame.i++;
|
|
13694
|
+
} else if (c === "}") {
|
|
13695
|
+
if (frame.depth === 0) finish(i, ranOut);
|
|
13696
|
+
else {
|
|
13697
|
+
frame.depth--;
|
|
13698
|
+
frame.i++;
|
|
13699
|
+
}
|
|
13700
|
+
} else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) frames.push({
|
|
13701
|
+
kind: "literal",
|
|
13702
|
+
start: i,
|
|
13703
|
+
i: i + 1,
|
|
13704
|
+
quote: c,
|
|
13705
|
+
ranOut: false
|
|
13706
|
+
});
|
|
13707
|
+
else frame.i++;
|
|
13708
|
+
}
|
|
13709
|
+
return result;
|
|
13705
13710
|
}
|
|
13706
13711
|
/**
|
|
13707
13712
|
* The JavaScript reader Spindle uses: js-lexer, then a lenient scan. With
|
|
@@ -16908,7 +16913,7 @@ var SETTABLE_NAME = /^[A-Za-z_:][\w:.-]*$/;
|
|
|
16908
16913
|
*/
|
|
16909
16914
|
function setRawAttribute(el, name, value) {
|
|
16910
16915
|
if (SETTABLE_NAME.test(name)) {
|
|
16911
|
-
el.setAttribute(name, value);
|
|
16916
|
+
if (el.getAttribute(name) !== value) el.setAttribute(name, value);
|
|
16912
16917
|
return;
|
|
16913
16918
|
}
|
|
16914
16919
|
const parsed = parseHtmlInert(`<i ${name}=""></i>`).firstChild.attributes[0];
|
|
@@ -16936,7 +16941,10 @@ function isNativeDefault(tag, name) {
|
|
|
16936
16941
|
* loads, also drops an empty `class` and `translate="no"`.
|
|
16937
16942
|
*
|
|
16938
16943
|
* On HTML elements an upper-case prop name avoids all of these, and
|
|
16939
|
-
* setAttribute lower-cases it back.
|
|
16944
|
+
* setAttribute lower-cases it back. Native tags also pass lowercase `is`
|
|
16945
|
+
* so customized built-ins initialize when Preact creates them (#733),
|
|
16946
|
+
* and set its attribute directly in case the component owns an `is` property.
|
|
16947
|
+
* Names compat matches in any case
|
|
16940
16948
|
* (`on…`, `translate`), names setAttribute rejects, and on SVG elements,
|
|
16941
16949
|
* whose names are case-sensitive, Preact's reserved names and the `class`
|
|
16942
16950
|
* and `className` compat rewrites (dropping an empty `class`, turning
|
|
@@ -16949,7 +16957,10 @@ function splitAttributes(attributes, foreign, tag) {
|
|
|
16949
16957
|
for (const [name, value, written, bound] of attributes) {
|
|
16950
16958
|
const lower = name.toLowerCase();
|
|
16951
16959
|
if (BOOLEAN_ATTRIBUTES.has(lower) && written !== "" && value === "") continue;
|
|
16952
|
-
else if (!foreign &&
|
|
16960
|
+
else if (!foreign && lower === "is" && !tag.includes("-")) {
|
|
16961
|
+
props.is = value;
|
|
16962
|
+
direct.push([name, value]);
|
|
16963
|
+
} else if (!foreign && LIVE_PROPERTIES.has(lower)) {
|
|
16953
16964
|
if (!bound && isNativeDefault(tag, lower)) defaults.push([name.toUpperCase(), lower === "value" ? value : ""]);
|
|
16954
16965
|
else props[lower] = lower !== "value" && written === "" ? true : value;
|
|
16955
16966
|
} else if (!SETTABLE_NAME.test(name) || lower.startsWith("on") || lower === "translate" || foreign && (FOREIGN_DIRECT.has(name) || name === "__proto__")) direct.push([name, value]);
|
|
@@ -16968,13 +16979,15 @@ function splitAttributes(attributes, foreign, tag) {
|
|
|
16968
16979
|
*/
|
|
16969
16980
|
function useDirectAttributes(direct) {
|
|
16970
16981
|
const elementRef = useRef(null);
|
|
16982
|
+
const currentDirect = useRef(direct);
|
|
16983
|
+
currentDirect.current = direct;
|
|
16971
16984
|
const directKey = JSON.stringify(direct);
|
|
16972
16985
|
useLayoutEffect(() => {
|
|
16973
16986
|
const el = elementRef.current;
|
|
16974
16987
|
if (!el || direct.length === 0) return;
|
|
16975
16988
|
for (const [name, value] of direct) setRawAttribute(el, name, value);
|
|
16976
16989
|
return () => {
|
|
16977
|
-
for (const [name] of direct) el.removeAttribute(name);
|
|
16990
|
+
for (const [name] of direct) if (!currentDirect.current.some(([next]) => next === name)) el.removeAttribute(name);
|
|
16978
16991
|
};
|
|
16979
16992
|
}, [directKey]);
|
|
16980
16993
|
return direct.length > 0 ? elementRef : void 0;
|
|
@@ -19502,7 +19515,7 @@ function replacesState(fn) {
|
|
|
19502
19515
|
*/
|
|
19503
19516
|
function moveInHistory(get, set, step) {
|
|
19504
19517
|
const { historyIndex, history } = get();
|
|
19505
|
-
|
|
19518
|
+
let target = historyIndex + step;
|
|
19506
19519
|
if (target < 0 || target >= history.length) return;
|
|
19507
19520
|
finishEnteredMoment(get, set);
|
|
19508
19521
|
const previousPassage = get().currentPassage;
|
|
@@ -19510,9 +19523,11 @@ function moveInHistory(get, set, step) {
|
|
|
19510
19523
|
const navigation = get().navigationId;
|
|
19511
19524
|
emit("beforenavigate", targetPassage);
|
|
19512
19525
|
if (get().navigationId !== navigation) return;
|
|
19526
|
+
target = get().historyIndex + step;
|
|
19527
|
+
if (target < 0 || target >= get().history.length) return;
|
|
19513
19528
|
const restoredVars = deepClone(reconstructVarsAt(target));
|
|
19514
19529
|
set((state) => {
|
|
19515
|
-
state.historyIndex
|
|
19530
|
+
state.historyIndex = target;
|
|
19516
19531
|
state.currentPassage = state.history[state.historyIndex].passage;
|
|
19517
19532
|
state.navigationId++;
|
|
19518
19533
|
state.variables = restoredVars;
|
|
@@ -24755,6 +24770,7 @@ function useDetachedBody() {
|
|
|
24755
24770
|
mounted.current.splice(at, 1);
|
|
24756
24771
|
render(null, container);
|
|
24757
24772
|
} : null;
|
|
24773
|
+
mounted.current.push(container);
|
|
24758
24774
|
render(/* @__PURE__ */ jsx(FrozenStateContext.Provider, {
|
|
24759
24775
|
value: frozenAlways,
|
|
24760
24776
|
children: /* @__PURE__ */ jsx(LocalsUpdateContext.Provider, {
|
|
@@ -24800,7 +24816,6 @@ function useDetachedBody() {
|
|
|
24800
24816
|
})
|
|
24801
24817
|
})
|
|
24802
24818
|
}), container);
|
|
24803
|
-
mounted.current.push(container);
|
|
24804
24819
|
};
|
|
24805
24820
|
return (children) => runWithCommittedMutations(() => run(children));
|
|
24806
24821
|
}
|
|
@@ -10183,41 +10183,6 @@ function completionIdentifier(source, cursor, goal = "expression") {
|
|
|
10183
10183
|
*/
|
|
10184
10184
|
var NON_STRING_QUOTE_PREFIX = /[\p{L}\p{N}_\\]/u;
|
|
10185
10185
|
/**
|
|
10186
|
-
* Skip the literal opening at `i`: a '…' or "…" string, which must close on
|
|
10187
|
-
* its line, or a `…` template, whose `${…}` parts are read as braces.
|
|
10188
|
-
* Returns the index just past its closing quote or backtick, or -1.
|
|
10189
|
-
*/
|
|
10190
|
-
function skipLiteral(input, i) {
|
|
10191
|
-
const quote = input[i];
|
|
10192
|
-
const template = quote === "`";
|
|
10193
|
-
let ranOut = false;
|
|
10194
|
-
for (let j = i + 1; j < input.length;) {
|
|
10195
|
-
const c = input[j];
|
|
10196
|
-
if (c === "\\") j += 2;
|
|
10197
|
-
else if (c === quote) return {
|
|
10198
|
-
end: j + 1,
|
|
10199
|
-
ranOut
|
|
10200
|
-
};
|
|
10201
|
-
else if (!template && c === "\n") return {
|
|
10202
|
-
end: -1,
|
|
10203
|
-
ranOut
|
|
10204
|
-
};
|
|
10205
|
-
else if (template && c === "$" && input[j + 1] === "{") {
|
|
10206
|
-
const close = lenientRead(input, j + 2);
|
|
10207
|
-
ranOut || (ranOut = close.ranOut);
|
|
10208
|
-
if (close.end === -1) return {
|
|
10209
|
-
end: -1,
|
|
10210
|
-
ranOut
|
|
10211
|
-
};
|
|
10212
|
-
j = close.end + 1;
|
|
10213
|
-
} else j++;
|
|
10214
|
-
}
|
|
10215
|
-
return {
|
|
10216
|
-
end: -1,
|
|
10217
|
-
ranOut: ranOut || template
|
|
10218
|
-
};
|
|
10219
|
-
}
|
|
10220
|
-
/**
|
|
10221
10186
|
* The closing `}` of braces read leniently from `i` (just past a `{`): braces
|
|
10222
10187
|
* count except inside string and template literals, and a quote that can't
|
|
10223
10188
|
* start a string (an apostrophe, or one not closed on its line) is text, as
|
|
@@ -10243,31 +10208,71 @@ function lenientRead(input, i) {
|
|
|
10243
10208
|
}
|
|
10244
10209
|
var memoInput = "";
|
|
10245
10210
|
var memo = /* @__PURE__ */ new Map();
|
|
10246
|
-
function scanLenient(input,
|
|
10247
|
-
|
|
10248
|
-
|
|
10249
|
-
|
|
10250
|
-
|
|
10251
|
-
|
|
10252
|
-
|
|
10253
|
-
|
|
10254
|
-
|
|
10255
|
-
|
|
10256
|
-
end: i,
|
|
10257
|
-
ranOut
|
|
10258
|
-
};
|
|
10259
|
-
depth--;
|
|
10260
|
-
i++;
|
|
10261
|
-
} else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) {
|
|
10262
|
-
const close = skipLiteral(input, i);
|
|
10263
|
-
ranOut || (ranOut = close.ranOut);
|
|
10264
|
-
i = close.end === -1 ? i + 1 : close.end;
|
|
10265
|
-
} else i++;
|
|
10266
|
-
}
|
|
10267
|
-
return {
|
|
10211
|
+
function scanLenient(input, start) {
|
|
10212
|
+
const codeFrame = (start) => ({
|
|
10213
|
+
kind: "code",
|
|
10214
|
+
start,
|
|
10215
|
+
i: start,
|
|
10216
|
+
depth: 0,
|
|
10217
|
+
ranOut: false
|
|
10218
|
+
});
|
|
10219
|
+
const frames = [codeFrame(start)];
|
|
10220
|
+
let result = {
|
|
10268
10221
|
end: -1,
|
|
10269
10222
|
ranOut: true
|
|
10270
10223
|
};
|
|
10224
|
+
const finish = (end, ranOut) => {
|
|
10225
|
+
const frame = frames.pop();
|
|
10226
|
+
result = {
|
|
10227
|
+
end,
|
|
10228
|
+
ranOut
|
|
10229
|
+
};
|
|
10230
|
+
if (frame.kind === "code") memo.set(frame.start, result);
|
|
10231
|
+
const parent = frames[frames.length - 1];
|
|
10232
|
+
if (!parent) return;
|
|
10233
|
+
parent.ranOut || (parent.ranOut = ranOut);
|
|
10234
|
+
if (parent.kind === "code") parent.i = end === -1 ? parent.i + 1 : end;
|
|
10235
|
+
else if (end === -1) parent.i = input.length;
|
|
10236
|
+
else parent.i = end + 1;
|
|
10237
|
+
};
|
|
10238
|
+
while (frames.length) {
|
|
10239
|
+
const frame = frames[frames.length - 1];
|
|
10240
|
+
const { i, ranOut } = frame;
|
|
10241
|
+
if (i >= input.length) {
|
|
10242
|
+
finish(-1, ranOut || frame.kind === "code" || frame.quote === "`");
|
|
10243
|
+
continue;
|
|
10244
|
+
}
|
|
10245
|
+
const c = input[i];
|
|
10246
|
+
if (frame.kind === "literal") {
|
|
10247
|
+
if (c === "\\") frame.i += 2;
|
|
10248
|
+
else if (c === frame.quote) finish(i + 1, ranOut);
|
|
10249
|
+
else if (frame.quote !== "`" && c === "\n") finish(-1, ranOut);
|
|
10250
|
+
else if (frame.quote === "`" && c === "$" && input[i + 1] === "{") {
|
|
10251
|
+
const cached = memo.get(i + 2);
|
|
10252
|
+
if (cached) {
|
|
10253
|
+
frame.ranOut || (frame.ranOut = cached.ranOut);
|
|
10254
|
+
frame.i = cached.end === -1 ? input.length : cached.end + 1;
|
|
10255
|
+
} else frames.push(codeFrame(i + 2));
|
|
10256
|
+
} else frame.i++;
|
|
10257
|
+
} else if (c === "{") {
|
|
10258
|
+
frame.depth++;
|
|
10259
|
+
frame.i++;
|
|
10260
|
+
} else if (c === "}") {
|
|
10261
|
+
if (frame.depth === 0) finish(i, ranOut);
|
|
10262
|
+
else {
|
|
10263
|
+
frame.depth--;
|
|
10264
|
+
frame.i++;
|
|
10265
|
+
}
|
|
10266
|
+
} else if (c === "`" || (c === "\"" || c === "'") && !(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]))) frames.push({
|
|
10267
|
+
kind: "literal",
|
|
10268
|
+
start: i,
|
|
10269
|
+
i: i + 1,
|
|
10270
|
+
quote: c,
|
|
10271
|
+
ranOut: false
|
|
10272
|
+
});
|
|
10273
|
+
else frame.i++;
|
|
10274
|
+
}
|
|
10275
|
+
return result;
|
|
10271
10276
|
}
|
|
10272
10277
|
/**
|
|
10273
10278
|
* The JavaScript reader Spindle uses: js-lexer, then a lenient scan. With
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rohal12/spindle",
|
|
3
|
-
"version": "0.64.
|
|
3
|
+
"version": "0.64.16",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A Preact-based story format for Twine 2.",
|
|
6
6
|
"license": "Unlicense",
|
|
@@ -108,7 +108,7 @@
|
|
|
108
108
|
"@vitest/coverage-v8": "^5.0.3",
|
|
109
109
|
"codemirror": "^5.65.21",
|
|
110
110
|
"fake-indexeddb": "^6.2.5",
|
|
111
|
-
"fallow": "3.
|
|
111
|
+
"fallow": "3.38.1",
|
|
112
112
|
"fast-check": "^4.10.2",
|
|
113
113
|
"happy-dom": "^21.0.4",
|
|
114
114
|
"husky": "^9.1.7",
|
|
@@ -149,6 +149,9 @@ export function useDetachedBody(): (children: ASTNode[]) => void {
|
|
|
149
149
|
render(null, container);
|
|
150
150
|
}
|
|
151
151
|
: null;
|
|
152
|
+
// Replacement can happen synchronously inside render (for example {do}).
|
|
153
|
+
// Register first so that cancellation also sees the body doing it.
|
|
154
|
+
mounted.current.push(container);
|
|
152
155
|
render(
|
|
153
156
|
<FrozenStateContext.Provider value={frozenAlways}>
|
|
154
157
|
<LocalsUpdateContext.Provider value={updater}>
|
|
@@ -185,7 +188,6 @@ export function useDetachedBody(): (children: ASTNode[]) => void {
|
|
|
185
188
|
</FrozenStateContext.Provider>,
|
|
186
189
|
container,
|
|
187
190
|
);
|
|
188
|
-
mounted.current.push(container);
|
|
189
191
|
};
|
|
190
192
|
return (children) => runWithCommittedMutations(() => run(children));
|
|
191
193
|
}
|
package/src/markup/code-end.ts
CHANGED
|
@@ -35,30 +35,6 @@ interface Lenient {
|
|
|
35
35
|
ranOut: boolean;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
/**
|
|
39
|
-
* Skip the literal opening at `i`: a '…' or "…" string, which must close on
|
|
40
|
-
* its line, or a `…` template, whose `${…}` parts are read as braces.
|
|
41
|
-
* Returns the index just past its closing quote or backtick, or -1.
|
|
42
|
-
*/
|
|
43
|
-
function skipLiteral(input: string, i: number): Lenient {
|
|
44
|
-
const quote = input[i];
|
|
45
|
-
const template = quote === '`';
|
|
46
|
-
let ranOut = false;
|
|
47
|
-
for (let j = i + 1; j < input.length;) {
|
|
48
|
-
const c = input[j];
|
|
49
|
-
if (c === '\\') j += 2;
|
|
50
|
-
else if (c === quote) return { end: j + 1, ranOut };
|
|
51
|
-
else if (!template && c === '\n') return { end: -1, ranOut };
|
|
52
|
-
else if (template && c === '$' && input[j + 1] === '{') {
|
|
53
|
-
const close = lenientRead(input, j + 2);
|
|
54
|
-
ranOut ||= close.ranOut;
|
|
55
|
-
if (close.end === -1) return { end: -1, ranOut };
|
|
56
|
-
j = close.end + 1;
|
|
57
|
-
} else j++;
|
|
58
|
-
}
|
|
59
|
-
return { end: -1, ranOut: ranOut || template };
|
|
60
|
-
}
|
|
61
|
-
|
|
62
38
|
/**
|
|
63
39
|
* The closing `}` of braces read leniently from `i` (just past a `{`): braces
|
|
64
40
|
* count except inside string and template literals, and a quote that can't
|
|
@@ -94,31 +70,83 @@ function lenientRead(input: string, i: number): Lenient {
|
|
|
94
70
|
let memoInput = '';
|
|
95
71
|
const memo = new Map<number, Lenient>();
|
|
96
72
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
73
|
+
/** A suspended code or literal scan; nested templates use heap frames. */
|
|
74
|
+
type ScanFrame = {
|
|
75
|
+
start: number;
|
|
76
|
+
i: number;
|
|
77
|
+
ranOut: boolean;
|
|
78
|
+
} & ({ kind: 'code'; depth: number } | { kind: 'literal'; quote: string });
|
|
79
|
+
|
|
80
|
+
function scanLenient(input: string, start: number): Lenient {
|
|
81
|
+
const codeFrame = (start: number): ScanFrame => ({
|
|
82
|
+
kind: 'code',
|
|
83
|
+
start,
|
|
84
|
+
i: start,
|
|
85
|
+
depth: 0,
|
|
86
|
+
ranOut: false,
|
|
87
|
+
});
|
|
88
|
+
const frames: ScanFrame[] = [codeFrame(start)];
|
|
89
|
+
let result: Lenient = { end: -1, ranOut: true };
|
|
90
|
+
const finish = (end: number, ranOut: boolean) => {
|
|
91
|
+
const frame = frames.pop()!;
|
|
92
|
+
result = { end, ranOut };
|
|
93
|
+
if (frame.kind === 'code') memo.set(frame.start, result);
|
|
94
|
+
const parent = frames[frames.length - 1];
|
|
95
|
+
if (!parent) return;
|
|
96
|
+
parent.ranOut ||= ranOut;
|
|
97
|
+
if (parent.kind === 'code') {
|
|
98
|
+
// An unclosed quote/backtick is plain text in the lenient reader.
|
|
99
|
+
parent.i = end === -1 ? parent.i + 1 : end;
|
|
100
|
+
} else if (end === -1) {
|
|
101
|
+
// The interpolation did not close, so neither can its template.
|
|
102
|
+
parent.i = input.length;
|
|
103
|
+
} else {
|
|
104
|
+
parent.i = end + 1;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
while (frames.length) {
|
|
108
|
+
const frame = frames[frames.length - 1]!;
|
|
109
|
+
const { i, ranOut } = frame;
|
|
110
|
+
if (i >= input.length) {
|
|
111
|
+
finish(-1, ranOut || frame.kind === 'code' || frame.quote === '`');
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
101
114
|
const c = input[i]!;
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
i
|
|
115
|
+
if (frame.kind === 'literal') {
|
|
116
|
+
if (c === '\\') frame.i += 2;
|
|
117
|
+
else if (c === frame.quote) finish(i + 1, ranOut);
|
|
118
|
+
else if (frame.quote !== '`' && c === '\n') finish(-1, ranOut);
|
|
119
|
+
else if (frame.quote === '`' && c === '$' && input[i + 1] === '{') {
|
|
120
|
+
const cached = memo.get(i + 2);
|
|
121
|
+
if (cached) {
|
|
122
|
+
frame.ranOut ||= cached.ranOut;
|
|
123
|
+
frame.i = cached.end === -1 ? input.length : cached.end + 1;
|
|
124
|
+
} else frames.push(codeFrame(i + 2));
|
|
125
|
+
} else frame.i++;
|
|
126
|
+
} else if (c === '{') {
|
|
127
|
+
frame.depth++;
|
|
128
|
+
frame.i++;
|
|
105
129
|
} else if (c === '}') {
|
|
106
|
-
if (depth === 0)
|
|
107
|
-
|
|
108
|
-
|
|
130
|
+
if (frame.depth === 0) finish(i, ranOut);
|
|
131
|
+
else {
|
|
132
|
+
frame.depth--;
|
|
133
|
+
frame.i++;
|
|
134
|
+
}
|
|
109
135
|
} else if (
|
|
110
136
|
c === '`' ||
|
|
111
137
|
((c === '"' || c === "'") &&
|
|
112
138
|
!(i > 0 && NON_STRING_QUOTE_PREFIX.test(input[i - 1]!)))
|
|
113
139
|
) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
140
|
+
frames.push({
|
|
141
|
+
kind: 'literal',
|
|
142
|
+
start: i,
|
|
143
|
+
i: i + 1,
|
|
144
|
+
quote: c,
|
|
145
|
+
ranOut: false,
|
|
146
|
+
});
|
|
147
|
+
} else frame.i++;
|
|
120
148
|
}
|
|
121
|
-
return
|
|
149
|
+
return result;
|
|
122
150
|
}
|
|
123
151
|
|
|
124
152
|
/**
|
package/src/markup/render.tsx
CHANGED
|
@@ -540,7 +540,8 @@ const SETTABLE_NAME = /^[A-Za-z_:][\w:.-]*$/;
|
|
|
540
540
|
*/
|
|
541
541
|
function setRawAttribute(el: Element, name: string, value: string) {
|
|
542
542
|
if (SETTABLE_NAME.test(name)) {
|
|
543
|
-
|
|
543
|
+
// Preact may already have set it; avoid firing custom-element callbacks twice.
|
|
544
|
+
if (el.getAttribute(name) !== value) el.setAttribute(name, value);
|
|
544
545
|
return;
|
|
545
546
|
}
|
|
546
547
|
const parsed = (parseHtmlInert(`<i ${name}=""></i>`).firstChild as Element)
|
|
@@ -571,7 +572,10 @@ function isNativeDefault(tag: string, name: string): boolean {
|
|
|
571
572
|
* loads, also drops an empty `class` and `translate="no"`.
|
|
572
573
|
*
|
|
573
574
|
* On HTML elements an upper-case prop name avoids all of these, and
|
|
574
|
-
* setAttribute lower-cases it back.
|
|
575
|
+
* setAttribute lower-cases it back. Native tags also pass lowercase `is`
|
|
576
|
+
* so customized built-ins initialize when Preact creates them (#733),
|
|
577
|
+
* and set its attribute directly in case the component owns an `is` property.
|
|
578
|
+
* Names compat matches in any case
|
|
575
579
|
* (`on…`, `translate`), names setAttribute rejects, and on SVG elements,
|
|
576
580
|
* whose names are case-sensitive, Preact's reserved names and the `class`
|
|
577
581
|
* and `className` compat rewrites (dropping an empty `class`, turning
|
|
@@ -591,6 +595,9 @@ function splitAttributes(
|
|
|
591
595
|
const lower = name.toLowerCase();
|
|
592
596
|
if (BOOLEAN_ATTRIBUTES.has(lower) && written !== '' && value === '') {
|
|
593
597
|
continue;
|
|
598
|
+
} else if (!foreign && lower === 'is' && !tag.includes('-')) {
|
|
599
|
+
props.is = value;
|
|
600
|
+
direct.push([name, value]);
|
|
594
601
|
} else if (!foreign && LIVE_PROPERTIES.has(lower)) {
|
|
595
602
|
if (!bound && isNativeDefault(tag, lower)) {
|
|
596
603
|
defaults.push([name.toUpperCase(), lower === 'value' ? value : '']);
|
|
@@ -621,13 +628,20 @@ function useDirectAttributes(
|
|
|
621
628
|
direct: [string, string][],
|
|
622
629
|
): RefObject<Element | null> | undefined {
|
|
623
630
|
const elementRef = useRef<Element>(null);
|
|
631
|
+
const currentDirect = useRef(direct);
|
|
632
|
+
currentDirect.current = direct;
|
|
624
633
|
const directKey = JSON.stringify(direct);
|
|
625
634
|
useLayoutEffect(() => {
|
|
626
635
|
const el = elementRef.current;
|
|
627
636
|
if (!el || direct.length === 0) return;
|
|
628
637
|
for (const [name, value] of direct) setRawAttribute(el, name, value);
|
|
629
638
|
return () => {
|
|
630
|
-
for (const [name] of direct)
|
|
639
|
+
for (const [name] of direct) {
|
|
640
|
+
// Keep retained attributes through updates without remove/add callbacks.
|
|
641
|
+
if (!currentDirect.current.some(([next]) => next === name)) {
|
|
642
|
+
el.removeAttribute(name);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
631
645
|
};
|
|
632
646
|
}, [directKey]);
|
|
633
647
|
return direct.length > 0 ? elementRef : undefined;
|
package/src/store.ts
CHANGED
|
@@ -509,7 +509,7 @@ function replacesState(fn: () => void): boolean {
|
|
|
509
509
|
*/
|
|
510
510
|
function moveInHistory(get: StoreGet, set: StoreSet, step: -1 | 1): void {
|
|
511
511
|
const { historyIndex, history } = get();
|
|
512
|
-
|
|
512
|
+
let target = historyIndex + step;
|
|
513
513
|
if (target < 0 || target >= history.length) return;
|
|
514
514
|
finishEnteredMoment(get, set);
|
|
515
515
|
|
|
@@ -523,10 +523,15 @@ function moveInHistory(get: StoreGet, set: StoreSet, step: -1 | 1): void {
|
|
|
523
523
|
emit('beforenavigate', targetPassage);
|
|
524
524
|
if (get().navigationId !== navigation) return;
|
|
525
525
|
|
|
526
|
+
// Lowering maxHistory in the callback can trim either end without a new
|
|
527
|
+
// navigation. The retained moments stay adjacent to the current one;
|
|
528
|
+
// resolve the target again, or cancel if it was removed (#732).
|
|
529
|
+
target = get().historyIndex + step;
|
|
530
|
+
if (target < 0 || target >= get().history.length) return;
|
|
526
531
|
const restoredVars = deepClone(reconstructVarsAt(target));
|
|
527
532
|
|
|
528
533
|
set((state) => {
|
|
529
|
-
state.historyIndex
|
|
534
|
+
state.historyIndex = target;
|
|
530
535
|
state.currentPassage = state.history[state.historyIndex]!.passage;
|
|
531
536
|
state.navigationId++;
|
|
532
537
|
state.variables = restoredVars;
|
package/src/twine-editor/mode.ts
CHANGED
|
@@ -17,8 +17,10 @@ export interface ModeState {
|
|
|
17
17
|
* from its start, ending in a line break; `''` when nothing is open.
|
|
18
18
|
*/
|
|
19
19
|
tail: string;
|
|
20
|
-
/** The
|
|
20
|
+
/** The current line’s spans, in window offsets; immutable during emission. */
|
|
21
21
|
spans: StyledSpan[];
|
|
22
|
+
/** Index of the next span to emit. */
|
|
23
|
+
spanIndex: number;
|
|
22
24
|
/** Where the current line starts in the window its spans came from. */
|
|
23
25
|
lineStart: number;
|
|
24
26
|
/**
|
|
@@ -89,6 +91,7 @@ function startLine(stream: StringStream, state: ModeState): void {
|
|
|
89
91
|
end: Math.min(span.end, head.length),
|
|
90
92
|
style: span.style,
|
|
91
93
|
}));
|
|
94
|
+
state.spanIndex = 0;
|
|
92
95
|
state.lineStart = lineStart;
|
|
93
96
|
state.capped = capped;
|
|
94
97
|
state.context = line.context;
|
|
@@ -111,17 +114,18 @@ export function mode(): Mode<ModeState> {
|
|
|
111
114
|
context: '',
|
|
112
115
|
tail: '',
|
|
113
116
|
spans: [],
|
|
117
|
+
spanIndex: 0,
|
|
114
118
|
lineStart: 0,
|
|
115
119
|
capped: false,
|
|
116
120
|
}),
|
|
117
|
-
copyState: (state) => ({ ...state
|
|
121
|
+
copyState: (state) => ({ ...state }),
|
|
118
122
|
blankLine: (state) => {
|
|
119
123
|
if (state.tail) state.tail += '\n';
|
|
120
124
|
},
|
|
121
125
|
token: (stream, state) => {
|
|
122
126
|
try {
|
|
123
|
-
if (stream.sol()
|
|
124
|
-
const span = state.spans[
|
|
127
|
+
if (stream.sol()) startLine(stream, state);
|
|
128
|
+
const span = state.spans[state.spanIndex];
|
|
125
129
|
if (!span) {
|
|
126
130
|
stream.skipToEnd();
|
|
127
131
|
return null;
|
|
@@ -131,12 +135,13 @@ export function mode(): Mode<ModeState> {
|
|
|
131
135
|
stream.pos = span.start - state.lineStart;
|
|
132
136
|
return null;
|
|
133
137
|
}
|
|
134
|
-
state.
|
|
138
|
+
state.spanIndex++;
|
|
135
139
|
stream.pos = span.end - state.lineStart;
|
|
136
140
|
return span.style;
|
|
137
141
|
} catch {
|
|
138
142
|
stream.skipToEnd();
|
|
139
143
|
state.spans = [];
|
|
144
|
+
state.spanIndex = 0;
|
|
140
145
|
state.tail = '';
|
|
141
146
|
state.context = '';
|
|
142
147
|
state.capped = false;
|