@rohal12/spindle 0.64.29 → 0.64.30
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 +131 -87
- package/dist/pkg/story-variables.js +44 -21
- package/package.json +1 -1
- package/src/define-macro.ts +6 -5
- package/src/hooks/use-modal-focus.ts +4 -0
- package/src/js-bindings.ts +68 -20
- package/src/store.ts +5 -1
- package/src/utils/control-edits.ts +113 -36
package/dist/pkg/headless.js
CHANGED
|
@@ -3,7 +3,7 @@ import { useCallback, useContext, useEffect, useErrorBoundary, useId, useLayoutE
|
|
|
3
3
|
import { memo, useSyncExternalStore } from "preact/compat";
|
|
4
4
|
import { createStore } from "zustand/vanilla";
|
|
5
5
|
import { immer } from "zustand/middleware/immer";
|
|
6
|
-
import { Immer, current, enableMapSet, enablePatches, freeze, isDraft, original } from "immer";
|
|
6
|
+
import { Immer, current, enableMapSet, enablePatches, freeze, isDraft, original, produce } from "immer";
|
|
7
7
|
import { shallow } from "zustand/vanilla/shallow";
|
|
8
8
|
import { Fragment as Fragment$1, jsx, jsxs } from "preact/jsx-runtime";
|
|
9
9
|
import { micromark } from "micromark";
|
|
@@ -19690,6 +19690,128 @@ function initializeStory(savedSession) {
|
|
|
19690
19690
|
recordStoryInitState();
|
|
19691
19691
|
}
|
|
19692
19692
|
}
|
|
19693
|
+
// src/utils/control-edits.ts
|
|
19694
|
+
/**
|
|
19695
|
+
* Records the edits made to the items of a list from inside a {for}
|
|
19696
|
+
* iteration, which {for} tells apart from a list that is replaced:
|
|
19697
|
+
*
|
|
19698
|
+
* - a reader editing a variable through a control (`{textbox}`,
|
|
19699
|
+
* `{checkbox}`, ...): typing into a control inside an iteration changes
|
|
19700
|
+
* its item, but must not remount the iteration and drop the control's
|
|
19701
|
+
* focus (#369);
|
|
19702
|
+
* - mutation code an iteration runs as it renders (`{set $party[@i].hp +=
|
|
19703
|
+
* 1}`): remounting would run the iteration's mount-only macros, and so the
|
|
19704
|
+
* write, again for every write it makes (#400).
|
|
19705
|
+
*
|
|
19706
|
+
* An edit is attributed to the variable path it wrote, so a replacement made
|
|
19707
|
+
* elsewhere (a watcher reacting to the edit, a button outside the loop)
|
|
19708
|
+
* still remounts.
|
|
19709
|
+
*/
|
|
19710
|
+
var version = 0;
|
|
19711
|
+
var MAX_LOG = 4096;
|
|
19712
|
+
/** Object identities at their own commit; old story graphs stay collectible. */
|
|
19713
|
+
var through = /* @__PURE__ */ new WeakMap();
|
|
19714
|
+
/** Paths, not story graphs: revisit only the prefixes an edit actually used. */
|
|
19715
|
+
var tracked = /* @__PURE__ */ new Map();
|
|
19716
|
+
/** Primitive slot edits also retain their committed value. */
|
|
19717
|
+
var log = [];
|
|
19718
|
+
function note(path, state) {
|
|
19719
|
+
const at = ++version;
|
|
19720
|
+
const key = JSON.stringify(path);
|
|
19721
|
+
tracked.delete(key);
|
|
19722
|
+
tracked.set(key, path);
|
|
19723
|
+
if (tracked.size > MAX_LOG) tracked.delete(tracked.keys().next().value);
|
|
19724
|
+
let node = state;
|
|
19725
|
+
for (const segment of path) {
|
|
19726
|
+
node = node !== null && typeof node === "object" ? getByPath(node, [segment]) : void 0;
|
|
19727
|
+
if (node !== null && typeof node === "object") through.set(node, at);
|
|
19728
|
+
}
|
|
19729
|
+
if (node !== null && typeof node === "object") return;
|
|
19730
|
+
log.push({
|
|
19731
|
+
version: at,
|
|
19732
|
+
path,
|
|
19733
|
+
value: node
|
|
19734
|
+
});
|
|
19735
|
+
if (log.length > MAX_LOG) log.shift();
|
|
19736
|
+
}
|
|
19737
|
+
var activeEdits;
|
|
19738
|
+
function withEdits(edits, run) {
|
|
19739
|
+
const previous = activeEdits;
|
|
19740
|
+
activeEdits = edits;
|
|
19741
|
+
try {
|
|
19742
|
+
run();
|
|
19743
|
+
} finally {
|
|
19744
|
+
activeEdits = previous;
|
|
19745
|
+
}
|
|
19746
|
+
}
|
|
19747
|
+
/** Attribute only the control's own writes, before subscribers can replace them. */
|
|
19748
|
+
function runAsControlEdit(segments, run) {
|
|
19749
|
+
withEdits([["variables", ...segments]], run);
|
|
19750
|
+
}
|
|
19751
|
+
/** Whether mutation code runs inside a {for} iteration. */
|
|
19752
|
+
var ItemEditContext = createContext(false);
|
|
19753
|
+
var NAMESPACES = [
|
|
19754
|
+
"variables",
|
|
19755
|
+
"temporary",
|
|
19756
|
+
"transient"
|
|
19757
|
+
];
|
|
19758
|
+
/** Run `run`, noting each variable path its own updates change as an edit. */
|
|
19759
|
+
function runAsItemEdit(run) {
|
|
19760
|
+
withEdits("changed", run);
|
|
19761
|
+
}
|
|
19762
|
+
/** Called with finalized state before the store notifies its subscribers. */
|
|
19763
|
+
function notifyItemEdits(before, after, notify) {
|
|
19764
|
+
const compared = /* @__PURE__ */ new WeakMap();
|
|
19765
|
+
for (const path of tracked.values()) {
|
|
19766
|
+
const ns = path[0];
|
|
19767
|
+
if (before[ns] === after[ns]) continue;
|
|
19768
|
+
let previous = before[ns];
|
|
19769
|
+
let next = after[ns];
|
|
19770
|
+
for (const segment of path.slice(1)) {
|
|
19771
|
+
previous = previous !== null && typeof previous === "object" ? getByPath(previous, [segment]) : void 0;
|
|
19772
|
+
next = next !== null && typeof next === "object" ? getByPath(next, [segment]) : void 0;
|
|
19773
|
+
if (previous === null || typeof previous !== "object" || next === null || typeof next !== "object" || previous === next) continue;
|
|
19774
|
+
const edited = through.get(previous);
|
|
19775
|
+
if (edited === void 0) continue;
|
|
19776
|
+
const copies = compared.get(previous) ?? /* @__PURE__ */ new WeakSet();
|
|
19777
|
+
if (copies.has(next)) continue;
|
|
19778
|
+
copies.add(next);
|
|
19779
|
+
compared.set(previous, copies);
|
|
19780
|
+
if (deepEqualStrict(previous, next)) through.set(next, Math.max(through.get(next) ?? 0, edited));
|
|
19781
|
+
}
|
|
19782
|
+
}
|
|
19783
|
+
if (activeEdits === "changed") for (const ns of NAMESPACES) {
|
|
19784
|
+
if (before[ns] === after[ns]) continue;
|
|
19785
|
+
for (const { path } of diffPaths(before[ns], after[ns], true)) note([ns, ...path], after);
|
|
19786
|
+
}
|
|
19787
|
+
else for (const path of activeEdits ?? []) note(path, after);
|
|
19788
|
+
withEdits(void 0, notify);
|
|
19789
|
+
}
|
|
19790
|
+
function controlEditVersion() {
|
|
19791
|
+
return version;
|
|
19792
|
+
}
|
|
19793
|
+
/**
|
|
19794
|
+
* A test of whether an edit made since `since` wrote inside `item`, element
|
|
19795
|
+
* `index` of `list`: the path leads through the item (compared by reference
|
|
19796
|
+
* in `state`), or, for a primitive item, ends at its slot in the list.
|
|
19797
|
+
*/
|
|
19798
|
+
function editedItems(since, state) {
|
|
19799
|
+
const ends = /* @__PURE__ */ new Map();
|
|
19800
|
+
for (let i = log.length - 1; i >= 0 && log[i].version > since; i--) {
|
|
19801
|
+
const { path, value } = log[i];
|
|
19802
|
+
const parent = getByPath(state, path.slice(0, -1));
|
|
19803
|
+
const key = path[path.length - 1];
|
|
19804
|
+
if (parent !== null && typeof parent === "object") {
|
|
19805
|
+
const current = getByPath(parent, [key]);
|
|
19806
|
+
if ((current === null || typeof current !== "object") && Object.is(current, value)) {
|
|
19807
|
+
const keys = ends.get(parent) ?? /* @__PURE__ */ new Set();
|
|
19808
|
+
keys.add(key);
|
|
19809
|
+
ends.set(parent, keys);
|
|
19810
|
+
}
|
|
19811
|
+
}
|
|
19812
|
+
}
|
|
19813
|
+
return (list, item, index) => typeof item === "object" && item !== null ? (through.get(item) ?? 0) > since : ends.get(list)?.has(String(index)) === true;
|
|
19814
|
+
}
|
|
19693
19815
|
// src/passage-roles.ts
|
|
19694
19816
|
/** Runtime-owned passage roles, shared with stateless authoring tools. */
|
|
19695
19817
|
var SPECIAL_PASSAGES = /* @__PURE__ */ new Set([
|
|
@@ -20455,10 +20577,12 @@ function storyStateGuard(creator) {
|
|
|
20455
20577
|
Object.assign(draft, updater);
|
|
20456
20578
|
};
|
|
20457
20579
|
const routed = routeStoreUpdate(recipe);
|
|
20458
|
-
|
|
20580
|
+
const before = get();
|
|
20581
|
+
const next = produce(before, (draft) => {
|
|
20459
20582
|
(routed ?? recipe)(draft);
|
|
20460
20583
|
keepNamespacesBare(draft);
|
|
20461
20584
|
});
|
|
20585
|
+
notifyItemEdits(before, next, () => set(next));
|
|
20462
20586
|
});
|
|
20463
20587
|
api.setState = guarded;
|
|
20464
20588
|
const state = creator(guarded, get, api);
|
|
@@ -21032,6 +21156,7 @@ var FOCUSABLE = [
|
|
|
21032
21156
|
*/
|
|
21033
21157
|
function isAvailable(el) {
|
|
21034
21158
|
if (!isRendered(el)) return false;
|
|
21159
|
+
if ((el.ownerDocument.defaultView ?? window).getComputedStyle(el).display === "contents") return false;
|
|
21035
21160
|
for (let at = el; at; at = composedParent(at)) if (at.hasAttribute("inert")) return false;
|
|
21036
21161
|
return !el.matches(":disabled");
|
|
21037
21162
|
}
|
|
@@ -21913,88 +22038,6 @@ var settings = {
|
|
|
21913
22038
|
return definitions.size > 0;
|
|
21914
22039
|
}
|
|
21915
22040
|
};
|
|
21916
|
-
// src/utils/control-edits.ts
|
|
21917
|
-
/**
|
|
21918
|
-
* Records the edits made to the items of a list from inside a {for}
|
|
21919
|
-
* iteration, which {for} tells apart from a list that is replaced:
|
|
21920
|
-
*
|
|
21921
|
-
* - a reader editing a variable through a control (`{textbox}`,
|
|
21922
|
-
* `{checkbox}`, ...): typing into a control inside an iteration changes
|
|
21923
|
-
* its item, but must not remount the iteration and drop the control's
|
|
21924
|
-
* focus (#369);
|
|
21925
|
-
* - mutation code an iteration runs as it renders (`{set $party[@i].hp +=
|
|
21926
|
-
* 1}`): remounting would run the iteration's mount-only macros, and so the
|
|
21927
|
-
* write, again for every write it makes (#400).
|
|
21928
|
-
*
|
|
21929
|
-
* An edit is attributed to the variable path it wrote, so a replacement made
|
|
21930
|
-
* elsewhere (a watcher reacting to the edit, a button outside the loop)
|
|
21931
|
-
* still remounts.
|
|
21932
|
-
*/
|
|
21933
|
-
var version = 0;
|
|
21934
|
-
var MAX_LOG = 4096;
|
|
21935
|
-
/** Edits in the order made: paths below the namespaces (`['variables', 'a']`). */
|
|
21936
|
-
var log = [];
|
|
21937
|
-
function note(path) {
|
|
21938
|
-
log.push({
|
|
21939
|
-
version: ++version,
|
|
21940
|
-
path
|
|
21941
|
-
});
|
|
21942
|
-
if (log.length > MAX_LOG) log.shift();
|
|
21943
|
-
}
|
|
21944
|
-
/** Note an edit a control made to the story variable at `segments`. */
|
|
21945
|
-
function noteControlEdit(segments) {
|
|
21946
|
-
note(["variables", ...segments]);
|
|
21947
|
-
}
|
|
21948
|
-
/**
|
|
21949
|
-
* Whether mutation code runs inside a {for} iteration: {for} provides true
|
|
21950
|
-
* around its iterations, and the writes of such code are noted as edits of
|
|
21951
|
-
* the items they reach (see runAsItemEdit).
|
|
21952
|
-
*/
|
|
21953
|
-
var ItemEditContext = createContext(false);
|
|
21954
|
-
var NAMESPACES = [
|
|
21955
|
-
"variables",
|
|
21956
|
-
"temporary",
|
|
21957
|
-
"transient"
|
|
21958
|
-
];
|
|
21959
|
-
/** Run `run`, noting each variable path it changes as an edit. */
|
|
21960
|
-
function runAsItemEdit(run) {
|
|
21961
|
-
const before = readState();
|
|
21962
|
-
try {
|
|
21963
|
-
run();
|
|
21964
|
-
} finally {
|
|
21965
|
-
const after = readState();
|
|
21966
|
-
for (const ns of NAMESPACES) {
|
|
21967
|
-
if (before[ns] === after[ns]) continue;
|
|
21968
|
-
for (const { path } of diffPaths(before[ns], after[ns], true)) note([ns, ...path]);
|
|
21969
|
-
}
|
|
21970
|
-
}
|
|
21971
|
-
}
|
|
21972
|
-
function controlEditVersion() {
|
|
21973
|
-
return version;
|
|
21974
|
-
}
|
|
21975
|
-
/**
|
|
21976
|
-
* A test of whether an edit made since `since` wrote inside `item`, element
|
|
21977
|
-
* `index` of `list`: the path leads through the item (compared by reference
|
|
21978
|
-
* in `state`), or, for a primitive item, ends at its slot in the list.
|
|
21979
|
-
*/
|
|
21980
|
-
function editedItems(since, state) {
|
|
21981
|
-
const through = /* @__PURE__ */ new Set();
|
|
21982
|
-
const ends = /* @__PURE__ */ new Map();
|
|
21983
|
-
for (let i = log.length - 1; i >= 0 && log[i].version > since; i--) {
|
|
21984
|
-
const { path } = log[i];
|
|
21985
|
-
let node = state;
|
|
21986
|
-
path.forEach((seg, n) => {
|
|
21987
|
-
if (n === path.length - 1 && node !== null && typeof node === "object") {
|
|
21988
|
-
const keys = ends.get(node) ?? /* @__PURE__ */ new Set();
|
|
21989
|
-
keys.add(seg);
|
|
21990
|
-
ends.set(node, keys);
|
|
21991
|
-
}
|
|
21992
|
-
node = node !== null && typeof node === "object" ? getByPath(node, [seg]) : void 0;
|
|
21993
|
-
if (node !== null && typeof node === "object") through.add(node);
|
|
21994
|
-
});
|
|
21995
|
-
}
|
|
21996
|
-
return (list, item, index) => typeof item === "object" && item !== null ? through.has(item) : ends.get(list)?.has(String(index)) === true;
|
|
21997
|
-
}
|
|
21998
22041
|
// src/utils/extract-text.ts
|
|
21999
22042
|
/**
|
|
22000
22043
|
* Collect plain text from AST nodes, ignoring non-text nodes.
|
|
@@ -22096,9 +22139,10 @@ function defineMacro(config, source = "builtin") {
|
|
|
22096
22139
|
ctx.value = getByPath(useStoryFields("variables").variables, segments);
|
|
22097
22140
|
ctx.getValue = () => getByPath(readState().variables, segments);
|
|
22098
22141
|
ctx.setValue = displayOnly ? refuse : (value) => {
|
|
22099
|
-
|
|
22100
|
-
|
|
22101
|
-
|
|
22142
|
+
runAsControlEdit(segments, () => {
|
|
22143
|
+
mutateState((work, adopt) => {
|
|
22144
|
+
setByPath(work.variables, segments, adopt(value), { createMissing: true });
|
|
22145
|
+
});
|
|
22102
22146
|
});
|
|
22103
22147
|
};
|
|
22104
22148
|
}
|
|
@@ -13655,17 +13655,28 @@ function checkVariableReferences(passages, declared, options) {
|
|
|
13655
13655
|
return diagnostics;
|
|
13656
13656
|
}
|
|
13657
13657
|
// src/js-bindings.ts
|
|
13658
|
-
/**
|
|
13659
|
-
|
|
13660
|
-
|
|
13661
|
-
|
|
13662
|
-
|
|
13663
|
-
|
|
13664
|
-
|
|
13665
|
-
|
|
13666
|
-
|
|
13667
|
-
|
|
13668
|
-
|
|
13658
|
+
/** Search arbitrary (also recovered, overlapping) bracket intervals. */
|
|
13659
|
+
var GroupIndex = class {
|
|
13660
|
+
constructor(groups) {
|
|
13661
|
+
this.groups = groups;
|
|
13662
|
+
let size = 1;
|
|
13663
|
+
while (size < groups.length) size *= 2;
|
|
13664
|
+
this.size = size;
|
|
13665
|
+
this.ends = new Array(size * 2).fill(-1);
|
|
13666
|
+
for (let i = 0; i < groups.length; i++) this.ends[size + i] = groups[i].close;
|
|
13667
|
+
for (let i = size - 1; i > 0; i--) this.ends[i] = Math.max(this.ends[i * 2], this.ends[i * 2 + 1]);
|
|
13668
|
+
}
|
|
13669
|
+
/** Latest opener before `at` whose closer follows it, in logarithmic time. */
|
|
13670
|
+
containing(at) {
|
|
13671
|
+
const find = (node, start, end) => {
|
|
13672
|
+
if (start >= this.groups.length || this.groups[start].open >= at || this.ends[node] <= at) return void 0;
|
|
13673
|
+
if (end - start === 1) return this.groups[start];
|
|
13674
|
+
const mid = start + end >>> 1;
|
|
13675
|
+
return find(node * 2 + 1, mid, end) ?? find(node * 2, start, mid);
|
|
13676
|
+
};
|
|
13677
|
+
return find(1, 0, this.size);
|
|
13678
|
+
}
|
|
13679
|
+
};
|
|
13669
13680
|
var CLOSERS = {
|
|
13670
13681
|
"}": "{",
|
|
13671
13682
|
")": "(",
|
|
@@ -13718,7 +13729,9 @@ var isWordChar = (c) => !!c && /[\w$]/.test(c);
|
|
|
13718
13729
|
var Bindings = class {
|
|
13719
13730
|
constructor(code) {
|
|
13720
13731
|
this.code = code;
|
|
13732
|
+
this.identifiers = /* @__PURE__ */ new Map();
|
|
13721
13733
|
this.groups = [];
|
|
13734
|
+
this.groupIndexes = /* @__PURE__ */ new Map();
|
|
13722
13735
|
this.byOpen = /* @__PURE__ */ new Map();
|
|
13723
13736
|
this.byClose = /* @__PURE__ */ new Map();
|
|
13724
13737
|
this.functions = /* @__PURE__ */ new Map();
|
|
@@ -13729,6 +13742,17 @@ var Bindings = class {
|
|
|
13729
13742
|
end: Infinity
|
|
13730
13743
|
};
|
|
13731
13744
|
this.readGroups();
|
|
13745
|
+
for (const kind of [
|
|
13746
|
+
void 0,
|
|
13747
|
+
"{",
|
|
13748
|
+
"(",
|
|
13749
|
+
"["
|
|
13750
|
+
]) this.groupIndexes.set(kind, new GroupIndex(kind === void 0 ? this.groups : this.groups.filter((g) => g.kind === kind)));
|
|
13751
|
+
for (const match of code.matchAll(/[\w$]+/g)) {
|
|
13752
|
+
const positions = this.identifiers.get(match[0]) ?? [];
|
|
13753
|
+
positions.push(match.index);
|
|
13754
|
+
this.identifiers.set(match[0], positions);
|
|
13755
|
+
}
|
|
13732
13756
|
}
|
|
13733
13757
|
/**
|
|
13734
13758
|
* The `{` of the object literal that `name`, used at `at`, is declared as
|
|
@@ -13751,11 +13775,9 @@ var Bindings = class {
|
|
|
13751
13775
|
}
|
|
13752
13776
|
/** Declaration and write facts are immutable for this source and property set. */
|
|
13753
13777
|
readBindings(name, properties) {
|
|
13754
|
-
const { code } = this;
|
|
13755
|
-
const escaped = escapeRegExp(name);
|
|
13756
13778
|
const bindings = [];
|
|
13757
|
-
for (const
|
|
13758
|
-
const binding = this.bindingAt(
|
|
13779
|
+
for (const at of this.identifiers.get(name) ?? []) {
|
|
13780
|
+
const binding = this.bindingAt(at, name.length, properties);
|
|
13759
13781
|
if (binding) bindings.push(binding);
|
|
13760
13782
|
}
|
|
13761
13783
|
const declarations = bindings.filter((b) => !b.write);
|
|
@@ -13807,11 +13829,7 @@ var Bindings = class {
|
|
|
13807
13829
|
}
|
|
13808
13830
|
/** The innermost group that has `index` inside it, of this kind if given. */
|
|
13809
13831
|
innermost(index, kind) {
|
|
13810
|
-
|
|
13811
|
-
for (let k = groups.length - 1; k >= 0; k--) {
|
|
13812
|
-
const g = groups[k];
|
|
13813
|
-
if (g.open < index && index < g.close && (!kind || g.kind === kind)) return g;
|
|
13814
|
-
}
|
|
13832
|
+
return this.groupIndexes.get(kind).containing(index);
|
|
13815
13833
|
}
|
|
13816
13834
|
/**
|
|
13817
13835
|
* The word before a `(` group (`if`, `for`, `catch`, a function's name…);
|
|
@@ -14141,7 +14159,12 @@ var Bindings = class {
|
|
|
14141
14159
|
/** `Object.assign(x, …)` and the like: the name is the first argument. */
|
|
14142
14160
|
isMutatedArgument(before, end) {
|
|
14143
14161
|
const group = this.byOpen.get(before);
|
|
14144
|
-
|
|
14162
|
+
if (group?.kind !== "(" || !",)".includes(this.code.charAt(end))) return false;
|
|
14163
|
+
const method = this.wordBefore(this.backFrom(group.open) + 1);
|
|
14164
|
+
const dot = this.backFrom(method.start);
|
|
14165
|
+
if (this.code.charAt(dot) !== ".") return false;
|
|
14166
|
+
const owner = this.wordBefore(this.backFrom(dot) + 1);
|
|
14167
|
+
return MUTATING_CALL_RE.test(this.code.slice(owner.start, group.open));
|
|
14145
14168
|
}
|
|
14146
14169
|
};
|
|
14147
14170
|
// src/macro-discovery.ts
|
package/package.json
CHANGED
package/src/define-macro.ts
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
import { useInterpolate } from './hooks/use-interpolate';
|
|
15
15
|
import {
|
|
16
16
|
ItemEditContext,
|
|
17
|
-
|
|
17
|
+
runAsControlEdit,
|
|
18
18
|
runAsItemEdit,
|
|
19
19
|
} from './utils/control-edits';
|
|
20
20
|
import { useMergedLocals } from './hooks/use-merged-locals';
|
|
@@ -254,13 +254,14 @@ export function defineMacro<const P extends readonly ParameterDef[] = []>(
|
|
|
254
254
|
ctx.setValue = displayOnly
|
|
255
255
|
? refuse
|
|
256
256
|
: (value: unknown) => {
|
|
257
|
-
noteControlEdit(segments);
|
|
258
257
|
// The commit mutation code uses, which keeps the references among
|
|
259
258
|
// variables: a write to a draft copies only the path written, and
|
|
260
259
|
// other variables sharing the object would keep the old one.
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
260
|
+
runAsControlEdit(segments, () => {
|
|
261
|
+
mutateState((work, adopt) => {
|
|
262
|
+
setByPath(work.variables, segments, adopt(value), {
|
|
263
|
+
createMissing: true,
|
|
264
|
+
});
|
|
264
265
|
});
|
|
265
266
|
});
|
|
266
267
|
};
|
|
@@ -28,6 +28,10 @@ const FOCUSABLE = [
|
|
|
28
28
|
*/
|
|
29
29
|
function isAvailable(el: HTMLElement): boolean {
|
|
30
30
|
if (!isRendered(el)) return false;
|
|
31
|
+
// Boxless containers can render focusable children, but cannot take focus
|
|
32
|
+
// themselves. Keep traversing their descendants in focusables().
|
|
33
|
+
const view = el.ownerDocument.defaultView ?? window;
|
|
34
|
+
if (view.getComputedStyle(el).display === 'contents') return false;
|
|
31
35
|
// Rendered visibility includes CSS overrides of hidden, until-found
|
|
32
36
|
// contents, closed details and shadow trees. Inertness is independent.
|
|
33
37
|
for (let at: Element | null = el; at; at = composedParent(at)) {
|
package/src/js-bindings.ts
CHANGED
|
@@ -9,7 +9,6 @@
|
|
|
9
9
|
* cannot place, it counts as a declaration of unknown value: a parameter, a
|
|
10
10
|
* pattern, an assignment. Better no answer than another declaration's.
|
|
11
11
|
*/
|
|
12
|
-
import { escapeRegExp } from './components/macros/arg-utils';
|
|
13
12
|
|
|
14
13
|
type Kind = '{' | '(' | '[';
|
|
15
14
|
|
|
@@ -21,6 +20,44 @@ interface Group {
|
|
|
21
20
|
parent: Group | undefined;
|
|
22
21
|
}
|
|
23
22
|
|
|
23
|
+
/** Search arbitrary (also recovered, overlapping) bracket intervals. */
|
|
24
|
+
class GroupIndex {
|
|
25
|
+
private readonly size: number;
|
|
26
|
+
private readonly ends: number[];
|
|
27
|
+
|
|
28
|
+
constructor(private readonly groups: readonly Group[]) {
|
|
29
|
+
let size = 1;
|
|
30
|
+
while (size < groups.length) size *= 2;
|
|
31
|
+
this.size = size;
|
|
32
|
+
this.ends = new Array<number>(size * 2).fill(-1);
|
|
33
|
+
for (let i = 0; i < groups.length; i++)
|
|
34
|
+
this.ends[size + i] = groups[i]!.close;
|
|
35
|
+
for (let i = size - 1; i > 0; i--) {
|
|
36
|
+
this.ends[i] = Math.max(this.ends[i * 2]!, this.ends[i * 2 + 1]!);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Latest opener before `at` whose closer follows it, in logarithmic time. */
|
|
41
|
+
containing(at: number): Group | undefined {
|
|
42
|
+
const find = (
|
|
43
|
+
node: number,
|
|
44
|
+
start: number,
|
|
45
|
+
end: number,
|
|
46
|
+
): Group | undefined => {
|
|
47
|
+
if (
|
|
48
|
+
start >= this.groups.length ||
|
|
49
|
+
this.groups[start]!.open >= at ||
|
|
50
|
+
this.ends[node]! <= at
|
|
51
|
+
)
|
|
52
|
+
return undefined;
|
|
53
|
+
if (end - start === 1) return this.groups[start];
|
|
54
|
+
const mid = (start + end) >>> 1;
|
|
55
|
+
return find(node * 2 + 1, mid, end) ?? find(node * 2, start, mid);
|
|
56
|
+
};
|
|
57
|
+
return find(1, 0, this.size);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
24
61
|
/** The text a name is visible in: `[start, end)`. */
|
|
25
62
|
interface Scope {
|
|
26
63
|
start: number;
|
|
@@ -75,7 +112,9 @@ const isWordChar = (c: string | undefined) => !!c && /[\w$]/.test(c);
|
|
|
75
112
|
|
|
76
113
|
/** Resolve the declaration of variables in `code` (see blankedCode in macro-discovery). */
|
|
77
114
|
export class Bindings {
|
|
115
|
+
private readonly identifiers = new Map<string, number[]>();
|
|
78
116
|
private readonly groups: Group[] = [];
|
|
117
|
+
private readonly groupIndexes = new Map<Kind | undefined, GroupIndex>();
|
|
79
118
|
private readonly byOpen = new Map<number, Group>();
|
|
80
119
|
private readonly byClose = new Map<number, Group>();
|
|
81
120
|
private readonly functions = new Map<Group, boolean>();
|
|
@@ -88,6 +127,23 @@ export class Bindings {
|
|
|
88
127
|
|
|
89
128
|
constructor(private readonly code: string) {
|
|
90
129
|
this.readGroups();
|
|
130
|
+
for (const kind of [undefined, '{', '(', '['] as const) {
|
|
131
|
+
this.groupIndexes.set(
|
|
132
|
+
kind,
|
|
133
|
+
new GroupIndex(
|
|
134
|
+
kind === undefined
|
|
135
|
+
? this.groups
|
|
136
|
+
: this.groups.filter((g) => g.kind === kind),
|
|
137
|
+
),
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
// One source scan serves every config name, including distinct names.
|
|
141
|
+
// Keep the same word boundaries as the binding reader (also for `$`).
|
|
142
|
+
for (const match of code.matchAll(/[\w$]+/g)) {
|
|
143
|
+
const positions = this.identifiers.get(match[0]) ?? [];
|
|
144
|
+
positions.push(match.index);
|
|
145
|
+
this.identifiers.set(match[0], positions);
|
|
146
|
+
}
|
|
91
147
|
}
|
|
92
148
|
|
|
93
149
|
/**
|
|
@@ -119,13 +175,9 @@ export class Bindings {
|
|
|
119
175
|
name: string,
|
|
120
176
|
properties: ReadonlySet<string>,
|
|
121
177
|
): Binding[] {
|
|
122
|
-
const { code } = this;
|
|
123
|
-
const escaped = escapeRegExp(name);
|
|
124
178
|
const bindings: Binding[] = [];
|
|
125
|
-
for (const
|
|
126
|
-
|
|
127
|
-
)) {
|
|
128
|
-
const binding = this.bindingAt(m.index, name.length, properties);
|
|
179
|
+
for (const at of this.identifiers.get(name) ?? []) {
|
|
180
|
+
const binding = this.bindingAt(at, name.length, properties);
|
|
129
181
|
if (binding) bindings.push(binding);
|
|
130
182
|
}
|
|
131
183
|
// A write is to the declaration it sees: in the scope of that one
|
|
@@ -194,14 +246,7 @@ export class Bindings {
|
|
|
194
246
|
|
|
195
247
|
/** The innermost group that has `index` inside it, of this kind if given. */
|
|
196
248
|
private innermost(index: number, kind?: Kind): Group | undefined {
|
|
197
|
-
|
|
198
|
-
for (let k = groups.length - 1; k >= 0; k--) {
|
|
199
|
-
const g = groups[k]!;
|
|
200
|
-
if (g.open < index && index < g.close && (!kind || g.kind === kind)) {
|
|
201
|
-
return g;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
return undefined;
|
|
249
|
+
return this.groupIndexes.get(kind)!.containing(index);
|
|
205
250
|
}
|
|
206
251
|
|
|
207
252
|
/**
|
|
@@ -618,10 +663,13 @@ export class Bindings {
|
|
|
618
663
|
/** `Object.assign(x, …)` and the like: the name is the first argument. */
|
|
619
664
|
private isMutatedArgument(before: number, end: number): boolean {
|
|
620
665
|
const group = this.byOpen.get(before);
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
);
|
|
666
|
+
if (group?.kind !== '(' || !',)'.includes(this.code.charAt(end)))
|
|
667
|
+
return false;
|
|
668
|
+
// The regexp only needs the callee, not every preceding statement.
|
|
669
|
+
const method = this.wordBefore(this.backFrom(group.open) + 1);
|
|
670
|
+
const dot = this.backFrom(method.start);
|
|
671
|
+
if (this.code.charAt(dot) !== '.') return false;
|
|
672
|
+
const owner = this.wordBefore(this.backFrom(dot) + 1);
|
|
673
|
+
return MUTATING_CALL_RE.test(this.code.slice(owner.start, group.open));
|
|
626
674
|
}
|
|
627
675
|
}
|
package/src/store.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { immer } from 'zustand/middleware/immer';
|
|
|
4
4
|
import type { StateCreator } from 'zustand/vanilla';
|
|
5
5
|
import {
|
|
6
6
|
current,
|
|
7
|
+
produce,
|
|
7
8
|
enableMapSet,
|
|
8
9
|
enablePatches,
|
|
9
10
|
isDraft,
|
|
@@ -66,6 +67,7 @@ import {
|
|
|
66
67
|
type PathChange,
|
|
67
68
|
} from './structural';
|
|
68
69
|
import { getByPath, setByPath } from './utils/object-path';
|
|
70
|
+
import { notifyItemEdits } from './utils/control-edits';
|
|
69
71
|
import { isIndexKey } from './utils/value-kinds';
|
|
70
72
|
import {
|
|
71
73
|
noPassageError,
|
|
@@ -1297,10 +1299,12 @@ function storyStateGuard(
|
|
|
1297
1299
|
Object.assign(draft, updater);
|
|
1298
1300
|
};
|
|
1299
1301
|
const routed = routeStoreUpdate(recipe);
|
|
1300
|
-
|
|
1302
|
+
const before = get();
|
|
1303
|
+
const next = produce(before, (draft) => {
|
|
1301
1304
|
(routed ?? recipe)(draft);
|
|
1302
1305
|
keepNamespacesBare(draft);
|
|
1303
1306
|
});
|
|
1307
|
+
notifyItemEdits(before, next, () => set(next));
|
|
1304
1308
|
}) as typeof set;
|
|
1305
1309
|
api.setState = guarded;
|
|
1306
1310
|
const state = creator(guarded, get, api);
|