@rohal12/spindle 0.62.2 → 0.62.4
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 +464 -272
- package/dist/pkg/play-worker.js +95 -7
- package/dist/pkg/play.js +3 -3
- package/dist/pkg/story-variables.js +590 -386
- package/dist/pkg/tooling.js +1 -0
- package/package.json +1 -1
- package/src/code-check.ts +1 -1
- package/src/completion-context.ts +226 -0
- package/src/components/Passage.tsx +28 -3
- package/src/define-macro.ts +6 -1
- package/src/hooks/use-render-options.ts +4 -0
- package/src/macro-discovery.ts +27 -15
- package/src/markup/code-attributes.ts +10 -3
- package/src/markup/parse.ts +19 -4
- package/src/markup/render.tsx +6 -4
- package/src/markup/spindle.d.peggy.ts +2 -0
- package/src/markup/spindle.peggy +22 -13
- package/src/passage-render-state.ts +7 -0
- package/src/play/session.ts +3 -3
- package/src/play/window-events.ts +63 -0
- package/src/play/worker.ts +62 -9
- package/src/saves/format.ts +4 -0
- package/src/saves/types.ts +6 -6
- package/src/static-literal.ts +24 -22
- package/src/store.ts +42 -16
- package/src/tooling.ts +4 -0
- package/src/triggers.ts +107 -0
- package/src/twine-editor/references.ts +9 -11
- package/src/widgets/ast-scanner.ts +40 -44
- package/src/widgets/widget-def.ts +74 -45
- package/types/play.d.ts +6 -5
- package/types/tooling.d.ts +17 -0
package/dist/pkg/play-worker.js
CHANGED
|
@@ -36,6 +36,56 @@ async function loadOptional(pkg, feature, importer = defaultImporter) {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
//#endregion
|
|
39
|
+
//#region src/play/window-events.ts
|
|
40
|
+
/** Expose the story's global window at the DOM event boundary. */
|
|
41
|
+
function installWindowEvents(window) {
|
|
42
|
+
const storyWindow = globalThis;
|
|
43
|
+
const wrapped = /* @__PURE__ */ new WeakMap();
|
|
44
|
+
const originals = /* @__PURE__ */ new WeakMap();
|
|
45
|
+
const wrap = (listener) => {
|
|
46
|
+
if (typeof listener !== "function") return listener;
|
|
47
|
+
let callback = wrapped.get(listener);
|
|
48
|
+
if (!callback) {
|
|
49
|
+
callback = listener.bind(storyWindow);
|
|
50
|
+
wrapped.set(listener, callback);
|
|
51
|
+
originals.set(callback, listener);
|
|
52
|
+
}
|
|
53
|
+
return callback;
|
|
54
|
+
};
|
|
55
|
+
const { addEventListener, removeEventListener } = window;
|
|
56
|
+
window.addEventListener = (type, listener, options) => addEventListener(type, wrap(listener), options);
|
|
57
|
+
window.removeEventListener = (type, listener) => removeEventListener(type, wrapped.get(listener) ?? listener);
|
|
58
|
+
for (const name of Object.getOwnPropertyNames(window)) {
|
|
59
|
+
const descriptor = Object.getOwnPropertyDescriptor(window, name);
|
|
60
|
+
if (!name.startsWith("on") || !descriptor.get || !descriptor.set) continue;
|
|
61
|
+
Object.defineProperty(window, name, {
|
|
62
|
+
...descriptor,
|
|
63
|
+
get() {
|
|
64
|
+
const listener = descriptor.get.call(window);
|
|
65
|
+
return originals.get(listener) ?? listener;
|
|
66
|
+
},
|
|
67
|
+
set(listener) {
|
|
68
|
+
descriptor.set.call(window, wrap(listener));
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const exposeWindow = (target) => target === window ? storyWindow : target;
|
|
73
|
+
const prototype = window.Event.prototype;
|
|
74
|
+
for (const name of ["target", "currentTarget"]) {
|
|
75
|
+
const descriptor = Object.getOwnPropertyDescriptor(prototype, name);
|
|
76
|
+
Object.defineProperty(prototype, name, {
|
|
77
|
+
...descriptor,
|
|
78
|
+
get() {
|
|
79
|
+
return exposeWindow(descriptor.get.call(this));
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
const { composedPath } = prototype;
|
|
84
|
+
prototype.composedPath = function() {
|
|
85
|
+
return composedPath.call(this).map((target) => target === window ? storyWindow : target);
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
39
89
|
//#region src/play/worker.ts
|
|
40
90
|
/**
|
|
41
91
|
* The worker thread of a play session: boots the story on happy-dom and
|
|
@@ -49,30 +99,68 @@ Object.defineProperty(process, "stdout", {
|
|
|
49
99
|
});
|
|
50
100
|
var port = parentPort;
|
|
51
101
|
var data = workerData;
|
|
52
|
-
/**
|
|
102
|
+
/** Install happy-dom's DOM in the global environment Spindle runs in. */
|
|
53
103
|
async function installDom() {
|
|
54
104
|
const { Window } = await loadOptional("happy-dom", "play sessions");
|
|
55
105
|
const window = new Window({ url: "http://localhost/" });
|
|
106
|
+
installWindowEvents(window);
|
|
56
107
|
const global = globalThis;
|
|
57
108
|
const source = window;
|
|
58
|
-
global.window = window;
|
|
59
109
|
global.document = window.document;
|
|
60
|
-
for (const name of Object.getOwnPropertyNames(window)) if (!(name in globalThis))
|
|
110
|
+
for (const name of Object.getOwnPropertyNames(window)) if (!(name in globalThis)) Object.defineProperty(globalThis, name, Object.getOwnPropertyDescriptor(window, name));
|
|
61
111
|
for (const name of HAPPY_DOM_GLOBALS) Object.defineProperty(globalThis, name, {
|
|
62
112
|
configurable: true,
|
|
63
113
|
writable: true,
|
|
64
114
|
value: source[name]
|
|
65
115
|
});
|
|
66
|
-
|
|
116
|
+
for (const name of [
|
|
117
|
+
"window",
|
|
118
|
+
"self",
|
|
119
|
+
"parent",
|
|
120
|
+
"top",
|
|
121
|
+
"frames"
|
|
122
|
+
]) Object.defineProperty(globalThis, name, {
|
|
67
123
|
configurable: true,
|
|
68
124
|
writable: true,
|
|
69
|
-
value:
|
|
125
|
+
value: globalThis
|
|
70
126
|
});
|
|
71
|
-
Object.defineProperty(
|
|
127
|
+
Object.defineProperty(window, "Story", {
|
|
72
128
|
configurable: true,
|
|
73
|
-
get: () =>
|
|
129
|
+
get: () => global.Story,
|
|
130
|
+
set: (value) => {
|
|
131
|
+
global.Story = value;
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
installIdleCallbacks();
|
|
135
|
+
Object.defineProperty(globalThis, "BroadcastChannel", {
|
|
136
|
+
configurable: true,
|
|
137
|
+
writable: true,
|
|
138
|
+
value: void 0
|
|
74
139
|
});
|
|
75
140
|
}
|
|
141
|
+
/** happy-dom has no idle scheduler; yield to a timer with a 50 ms budget. */
|
|
142
|
+
function installIdleCallbacks() {
|
|
143
|
+
const timers = /* @__PURE__ */ new Map();
|
|
144
|
+
let nextHandle = 0;
|
|
145
|
+
globalThis.requestIdleCallback = (callback, options) => {
|
|
146
|
+
const handle = ++nextHandle;
|
|
147
|
+
const requested = performance.now();
|
|
148
|
+
timers.set(handle, setTimeout(() => {
|
|
149
|
+
timers.delete(handle);
|
|
150
|
+
const started = performance.now();
|
|
151
|
+
const didTimeout = options?.timeout !== void 0 && started - requested >= options.timeout;
|
|
152
|
+
callback({
|
|
153
|
+
didTimeout,
|
|
154
|
+
timeRemaining: () => didTimeout ? 0 : Math.max(0, 50 - (performance.now() - started))
|
|
155
|
+
});
|
|
156
|
+
}, 1));
|
|
157
|
+
return handle;
|
|
158
|
+
};
|
|
159
|
+
globalThis.cancelIdleCallback = (handle) => {
|
|
160
|
+
clearTimeout(timers.get(handle));
|
|
161
|
+
timers.delete(handle);
|
|
162
|
+
};
|
|
163
|
+
}
|
|
76
164
|
/**
|
|
77
165
|
* Globals happy-dom's window replaces even where Node has its own:
|
|
78
166
|
* - the event classes: happy-dom's dispatchEvent() refuses events made with
|
package/dist/pkg/play.js
CHANGED
|
@@ -192,9 +192,9 @@ function workerExecArgv(execArgv) {
|
|
|
192
192
|
return kept;
|
|
193
193
|
}
|
|
194
194
|
/**
|
|
195
|
-
* Opens a session that runs the story in a worker
|
|
196
|
-
*
|
|
197
|
-
*
|
|
195
|
+
* Opens a session that runs the story in a worker with happy-dom's DOM.
|
|
196
|
+
* `window` is `globalThis`: properties authored on it are bare globals too.
|
|
197
|
+
* Idle callbacks use a timer-backed scheduler.
|
|
198
198
|
* Requires `trusted: true`: story code has unrestricted host Node access.
|
|
199
199
|
*/
|
|
200
200
|
function openPlaySession(options) {
|