@adriansteffan/reactive 0.2.6 → 0.5.0
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/README.md +796 -229
- package/bin/add.js +56 -0
- package/bin/deploy.js +48 -0
- package/bin/download.js +28 -0
- package/bin/ghsetup.js +21 -0
- package/bin/native.js +81 -0
- package/bin/setup.js +11 -17
- package/bin/study-deploy.js +60 -0
- package/bin/undeploy.js +15 -0
- package/extras/transcription/compose.yaml +32 -0
- package/{template → extras}/transcription/transcribe.py +4 -9
- package/package.json +62 -64
- package/src/bun-env.d.ts +1 -0
- package/src/bun-plugin.ts +22 -0
- package/src/components/canvasblock.tsx +209 -537
- package/src/components/checkdevice.tsx +64 -84
- package/src/components/fullscreenblock.tsx +130 -0
- package/src/components/icons.tsx +50 -0
- package/src/components/index.ts +9 -11
- package/src/components/microphonecheck.tsx +76 -38
- package/src/components/plaininput.tsx +48 -41
- package/src/components/prolificending.tsx +64 -28
- package/src/components/quest.tsx +89 -87
- package/src/components/{randomdotkinetogram.tsx → randomdotkinematogram.tsx} +205 -267
- package/src/components/{voicerecorder.tsx → recorder.tsx} +81 -106
- package/src/components/requestfilepermission.tsx +63 -0
- package/src/components/stimulus.tsx +373 -0
- package/src/components/storeui.tsx +94 -148
- package/src/components/text.tsx +61 -59
- package/src/components/tutorial.tsx +45 -47
- package/src/components/upload.tsx +133 -309
- package/src/components/voicerecording.tsx +90 -94
- package/src/core/backend.ts +20 -0
- package/src/core/bytecode.ts +178 -0
- package/src/core/columns.ts +50 -0
- package/src/core/component.ts +238 -0
- package/src/core/development.ts +32 -0
- package/src/core/experiment.ts +263 -0
- package/src/core/files.ts +291 -0
- package/src/core/params.ts +166 -0
- package/src/core/platform.ts +29 -0
- package/src/core/rows.ts +106 -0
- package/src/core/simulation.ts +158 -0
- package/src/core/slides.ts +178 -0
- package/src/core/store.ts +22 -0
- package/src/core/timeline.ts +399 -0
- package/src/fonts/AtkinsonHyperlegible-Bold.woff2 +0 -0
- package/src/fonts/AtkinsonHyperlegible-BoldItalic.woff2 +0 -0
- package/src/fonts/AtkinsonHyperlegible-Italic.woff2 +0 -0
- package/src/fonts/AtkinsonHyperlegible-Regular.woff2 +0 -0
- package/src/index.css +5 -7
- package/src/mod.tsx +31 -17
- package/src/native.ts +127 -0
- package/src/quest.ts +2 -0
- package/src/runner/contexts.ts +35 -0
- package/src/runner/devpanel.tsx +68 -0
- package/src/runner/experimentrunner.tsx +210 -0
- package/src/runner/fullscreenguard.tsx +93 -0
- package/src/runner/paraminput.tsx +86 -0
- package/src/runner/progressbar.tsx +20 -0
- package/src/runner/settingsscreen.tsx +517 -0
- package/src/{utils → runner}/theme.ts +9 -8
- package/src/server.ts +93 -0
- package/src/tutorial.ts +3 -0
- package/src/utils/array.ts +30 -62
- package/src/utils/canvas.ts +66 -0
- package/src/utils/ddm.ts +47 -70
- package/src/utils/distributions.ts +352 -61
- package/src/utils/fullscreen.ts +38 -0
- package/src/utils/ids.ts +11 -0
- package/src/utils/keys.ts +46 -0
- package/src/utils/llm.ts +15 -15
- package/src/utils/time.ts +12 -0
- package/src/utils/tts.ts +24 -24
- package/template/.dockerignore +3 -1
- package/template/.env.example +5 -0
- package/template/.github/workflows/deploy.yml +25 -0
- package/template/.gitignore +14 -0
- package/template/Dockerfile +9 -14
- package/template/README.md +74 -214
- package/template/biome.json +34 -0
- package/template/build.ts +20 -0
- package/template/bunfig.toml +3 -0
- package/template/docker-compose.yaml +4 -33
- package/template/index.html +2 -2
- package/template/package.json +16 -89
- package/template/public/.gitkeep +0 -0
- package/template/server.ts +32 -0
- package/template/simulate.ts +4 -44
- package/template/src/Experiment.tsx +76 -128
- package/template/src/bun-env.d.ts +1 -0
- package/template/src/index.css +1 -9
- package/template/src/main.tsx +25 -10
- package/template/tsconfig.json +4 -6
- package/.claude/settings.local.json +0 -61
- package/.eslintrc.cjs +0 -18
- package/.playwright-mcp/console-2026-04-07T14-22-59-060Z.log +0 -1
- package/.prettierrc +0 -5
- package/Dockerfile +0 -20
- package/dist/mod-HDegGPrf.js +0 -87434
- package/dist/mod.d.ts +0 -946
- package/dist/reactive.es.js +0 -87
- package/dist/reactive.umd.js +0 -10255
- package/dist/style.css +0 -5
- package/dist/web-7BbR1Ad7.js +0 -435
- package/dist/web-CATfZO1Q.js +0 -12
- package/planned.md +0 -103
- package/postcss.config.js +0 -6
- package/raw-loader-hooks.mjs +0 -25
- package/raw-loader.mjs +0 -5
- package/rdk_doc.md +0 -341
- package/src/components/enterfullscreen.tsx +0 -230
- package/src/components/exitfullscreen.tsx +0 -113
- package/src/components/experimentprovider.tsx +0 -69
- package/src/components/experimentrunner.tsx +0 -497
- package/src/components/mobilefilepermission.tsx +0 -68
- package/src/components/settingsscreen.tsx +0 -770
- package/src/types/array.d.ts +0 -6
- package/src/utils/audiodevice.ts +0 -4
- package/src/utils/bytecode.ts +0 -261
- package/src/utils/common.ts +0 -291
- package/src/utils/ddm.test.ts +0 -233
- package/src/utils/distributions.test.ts +0 -209
- package/src/utils/nodecompat.ts +0 -11
- package/src/utils/overlays.ts +0 -19
- package/src/utils/request.ts +0 -25
- package/src/utils/simulation.ts +0 -299
- package/src/utils/upload.ts +0 -275
- package/src/vite-env.d.ts +0 -1
- package/template/.env.template +0 -2
- package/template/.eslintrc.cjs +0 -18
- package/template/.prettierrc +0 -5
- package/template/backend/package-lock.json +0 -2544
- package/template/backend/package.json +0 -31
- package/template/backend/src/backend.ts +0 -131
- package/template/backend/tsconfig.json +0 -110
- package/template/capacitor.config.ts +0 -15
- package/template/electron/main.js +0 -131
- package/template/electron/preload.js +0 -9
- package/template/electron.vite.config.js +0 -45
- package/template/package-lock.json +0 -12467
- package/template/postcss.config.js +0 -5
- package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-Bold.ttf +0 -0
- package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-BoldItalic.ttf +0 -0
- package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-Italic.ttf +0 -0
- package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-Regular.ttf +0 -0
- package/template/shared.vite.config.js +0 -39
- package/template/src/vite-env.d.ts +0 -2
- package/template/tsconfig.node.json +0 -12
- package/template/vite.config.js +0 -11
- package/tsconfig.json +0 -30
- package/tsconfig.node.json +0 -12
- package/vite.config.ts +0 -39
- /package/{template → extras}/transcription/Dockerfile.cpu +0 -0
- /package/{template → extras}/transcription/Dockerfile.gpu +0 -0
- /package/{template/public/Atkinson_Hyperlegible → src/fonts}/OFL.txt +0 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import type { ExperimentData } from '../core/rows';
|
|
3
|
+
import type { Store } from '../core/store';
|
|
4
|
+
import type { TrialContent } from '../core/timeline';
|
|
5
|
+
|
|
6
|
+
// React elements in props refer back to React's internals, which JSON cannot hold
|
|
7
|
+
const showJson = (value: unknown) =>
|
|
8
|
+
JSON.stringify(value, (_key, v) => (React.isValidElement(v) ? '<element>' : v), 2);
|
|
9
|
+
|
|
10
|
+
// Metadata given as a function of the timeline context is only known once the trial ends
|
|
11
|
+
const showMetadata = (value: unknown) => showJson(typeof value === 'function' ? '(dynamic)' : value);
|
|
12
|
+
|
|
13
|
+
/** The ?dev=true panel: the trial on screen and its props, the store, and everything recorded so far. */
|
|
14
|
+
export function DevPanel({
|
|
15
|
+
content,
|
|
16
|
+
props,
|
|
17
|
+
store,
|
|
18
|
+
data,
|
|
19
|
+
}: {
|
|
20
|
+
content: TrialContent | undefined;
|
|
21
|
+
props: Record<string, any>;
|
|
22
|
+
store: Store;
|
|
23
|
+
data: ExperimentData;
|
|
24
|
+
}) {
|
|
25
|
+
const [open, setOpen] = useState(false);
|
|
26
|
+
const [showData, setShowData] = useState(false);
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<>
|
|
30
|
+
<button
|
|
31
|
+
type='button'
|
|
32
|
+
onClick={() => setOpen(!open)}
|
|
33
|
+
className='fixed top-2 right-2 z-50 px-3 py-1 bg-white text-black text-xs font-bold border-2 border-black shadow-[2px_2px_0px_rgba(0,0,0,1)] hover:translate-x-[1px] hover:translate-y-[1px] hover:shadow-none cursor-pointer'
|
|
34
|
+
>
|
|
35
|
+
{open ? '✕' : 'DEV'}
|
|
36
|
+
</button>
|
|
37
|
+
{open && (
|
|
38
|
+
<div className='fixed top-10 right-2 z-50 w-96 max-h-[80vh] overflow-auto bg-white text-black text-xs font-mono border-2 border-black shadow-[4px_4px_0px_rgba(0,0,0,1)] p-3'>
|
|
39
|
+
{content && (
|
|
40
|
+
<>
|
|
41
|
+
<div className='font-bold mb-2 text-sm'>
|
|
42
|
+
{content.type} {content.name ? `(${content.name})` : ''}
|
|
43
|
+
</div>
|
|
44
|
+
<div className='mb-2'>
|
|
45
|
+
<span className='font-bold'>Props:</span>
|
|
46
|
+
<pre className='mt-1 whitespace-pre-wrap break-all'>{showJson(props)}</pre>
|
|
47
|
+
</div>
|
|
48
|
+
{content.metadata && (
|
|
49
|
+
<div className='mb-2'>
|
|
50
|
+
<span className='font-bold'>Metadata:</span>
|
|
51
|
+
<pre className='mt-1 whitespace-pre-wrap break-all'>{showMetadata(content.metadata)}</pre>
|
|
52
|
+
</div>
|
|
53
|
+
)}
|
|
54
|
+
</>
|
|
55
|
+
)}
|
|
56
|
+
<div className='mb-2'>
|
|
57
|
+
<span className='font-bold'>Store:</span>
|
|
58
|
+
<pre className='mt-1 whitespace-pre-wrap break-all'>{showJson(store)}</pre>
|
|
59
|
+
</div>
|
|
60
|
+
<button type='button' className='font-bold cursor-pointer' onClick={() => setShowData(!showData)}>
|
|
61
|
+
Data ({data.trials.length} trials) {showData ? '▾' : '▸'}
|
|
62
|
+
</button>
|
|
63
|
+
{showData && <pre className='mt-1 whitespace-pre-wrap break-all'>{showJson(data)}</pre>}
|
|
64
|
+
</div>
|
|
65
|
+
)}
|
|
66
|
+
</>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { type ReactNode, useCallback, useEffect, useMemo, useReducer, useRef, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
createRunState,
|
|
4
|
+
type Experiment,
|
|
5
|
+
realign,
|
|
6
|
+
respond,
|
|
7
|
+
type RunState,
|
|
8
|
+
type KeptRun,
|
|
9
|
+
startRun,
|
|
10
|
+
trialAt,
|
|
11
|
+
} from '../core/experiment';
|
|
12
|
+
import { urlParams } from '../core/params';
|
|
13
|
+
import { createExperimentData } from '../core/rows';
|
|
14
|
+
import type { CompiledTimeline } from '../core/timeline';
|
|
15
|
+
import { uuidv4 } from '../utils/ids';
|
|
16
|
+
import { now } from '../utils/time';
|
|
17
|
+
import { AudioDeviceContext, CompiledTimelineContext, ParamsProvider } from './contexts';
|
|
18
|
+
import { DevPanel } from './devpanel';
|
|
19
|
+
import { FullscreenGuard } from './fullscreenguard';
|
|
20
|
+
import { ProgressBar } from './progressbar';
|
|
21
|
+
import { SettingsScreen } from './settingsscreen';
|
|
22
|
+
import { ThemeContext } from './theme';
|
|
23
|
+
|
|
24
|
+
// Counts the frames the screen shows and reports their rate every second, for 20 seconds
|
|
25
|
+
function collectRefreshRate(callback: (refreshRate: number) => void) {
|
|
26
|
+
const start = now();
|
|
27
|
+
let frames = 0;
|
|
28
|
+
let reported = start;
|
|
29
|
+
let rafId = 0;
|
|
30
|
+
const count = (timestamp: number) => {
|
|
31
|
+
frames++;
|
|
32
|
+
if (timestamp - reported >= 1000) {
|
|
33
|
+
callback(Math.round((frames * 1000) / (timestamp - start)));
|
|
34
|
+
reported = timestamp;
|
|
35
|
+
}
|
|
36
|
+
if (timestamp - start < 20_000) rafId = requestAnimationFrame(count);
|
|
37
|
+
};
|
|
38
|
+
rafId = requestAnimationFrame(count);
|
|
39
|
+
return () => cancelAnimationFrame(rafId);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type RunEvent =
|
|
43
|
+
| {
|
|
44
|
+
type: 'respond';
|
|
45
|
+
recorded: unknown;
|
|
46
|
+
storeUpdates: Record<string, any>;
|
|
47
|
+
startMs: number;
|
|
48
|
+
endMs: number;
|
|
49
|
+
}
|
|
50
|
+
| { type: 'rebuild'; previous: CompiledTimeline };
|
|
51
|
+
|
|
52
|
+
// Switches given as environment variables arrive as text
|
|
53
|
+
function parseBooleanProp(value: unknown): boolean {
|
|
54
|
+
if (typeof value === 'boolean') return value;
|
|
55
|
+
if (typeof value === 'string') {
|
|
56
|
+
const lower = value.trim().toLowerCase();
|
|
57
|
+
return lower !== '' && lower !== 'false' && lower !== '0';
|
|
58
|
+
}
|
|
59
|
+
return !!value;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default function ExperimentRunner({
|
|
63
|
+
experiment,
|
|
64
|
+
disableSettings,
|
|
65
|
+
disableDev,
|
|
66
|
+
}: {
|
|
67
|
+
experiment: Experiment<any>;
|
|
68
|
+
/** Turns off the settings page at /settings. */
|
|
69
|
+
disableSettings?: boolean | string;
|
|
70
|
+
/** Turns off the ?dev=true inspector panel, which shows the store and all recorded data. */
|
|
71
|
+
disableDev?: boolean | string;
|
|
72
|
+
}) {
|
|
73
|
+
const showSettings =
|
|
74
|
+
!parseBooleanProp(disableSettings) && window.location.pathname.endsWith('/settings');
|
|
75
|
+
|
|
76
|
+
return showSettings ? (
|
|
77
|
+
<SettingsScreen experiment={experiment} />
|
|
78
|
+
) : (
|
|
79
|
+
<Runner experiment={experiment} devDisabled={parseBooleanProp(disableDev)} />
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function Runner({ experiment, devDisabled }: { experiment: Experiment<any>; devDisabled: boolean }) {
|
|
84
|
+
// Space would scroll the participant's page, where it is often a response key
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
const preventSpaceScroll = (e: KeyboardEvent) => {
|
|
87
|
+
if (
|
|
88
|
+
e.key === ' ' &&
|
|
89
|
+
!(e.target instanceof HTMLInputElement) &&
|
|
90
|
+
!(e.target instanceof HTMLTextAreaElement) &&
|
|
91
|
+
!(e.target as HTMLElement).isContentEditable
|
|
92
|
+
) {
|
|
93
|
+
e.preventDefault();
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
window.addEventListener('keydown', preventSpaceScroll);
|
|
97
|
+
return () => window.removeEventListener('keydown', preventSpaceScroll);
|
|
98
|
+
}, []);
|
|
99
|
+
|
|
100
|
+
// Hot reloading renders this runner again with the edited experiment, which keeps the participant's
|
|
101
|
+
// params and build seed, so the rebuilt timeline matches the one they are in
|
|
102
|
+
const keptRef = useRef<KeptRun | undefined>(undefined);
|
|
103
|
+
const run = useMemo(() => {
|
|
104
|
+
const started = startRun(experiment, { url: urlParams() }, keptRef.current);
|
|
105
|
+
keptRef.current = started;
|
|
106
|
+
return started;
|
|
107
|
+
}, [experiment]);
|
|
108
|
+
|
|
109
|
+
const [state, dispatch] = useReducer(
|
|
110
|
+
(state: RunState, event: RunEvent): RunState => {
|
|
111
|
+
switch (event.type) {
|
|
112
|
+
case 'respond':
|
|
113
|
+
return respond(run.compiled, state, event);
|
|
114
|
+
case 'rebuild':
|
|
115
|
+
return realign(event.previous, run.compiled, state);
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
null,
|
|
119
|
+
() =>
|
|
120
|
+
createRunState(
|
|
121
|
+
run.compiled,
|
|
122
|
+
createExperimentData({ userAgent: navigator.userAgent, url: urlParams(), run }),
|
|
123
|
+
{ __uploadId: uuidv4() },
|
|
124
|
+
),
|
|
125
|
+
);
|
|
126
|
+
// The timeline the state belongs to, until a hot reload replaces it
|
|
127
|
+
const [compiled, setCompiled] = useState(run.compiled);
|
|
128
|
+
|
|
129
|
+
const trialNumber = state.data.trials.length + 1;
|
|
130
|
+
// When the trial on screen started, and which trial that is
|
|
131
|
+
const trialStartRef = useRef({ trialNumber, ms: now() });
|
|
132
|
+
// The changes the trial on screen made to the store, which take effect when it ends
|
|
133
|
+
const storeUpdatesRef = useRef<Record<string, any>>({});
|
|
134
|
+
|
|
135
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: each trial starts at the top of the page
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
window.scrollTo(0, 0);
|
|
138
|
+
}, [trialNumber]);
|
|
139
|
+
|
|
140
|
+
const next = useCallback(
|
|
141
|
+
(recorded?: unknown): void => {
|
|
142
|
+
const start = trialStartRef.current;
|
|
143
|
+
// A trial ends once; a later call, such as from a timer its component left running, is ignored
|
|
144
|
+
if (start.trialNumber !== trialNumber) return;
|
|
145
|
+
const storeUpdates = storeUpdatesRef.current;
|
|
146
|
+
storeUpdatesRef.current = {};
|
|
147
|
+
dispatch({ type: 'respond', recorded, storeUpdates, startMs: start.ms, endMs: now() });
|
|
148
|
+
// The next trial starts as this one ends, also when its component ends it as soon as it appears
|
|
149
|
+
trialStartRef.current = { trialNumber: trialNumber + 1, ms: now() };
|
|
150
|
+
},
|
|
151
|
+
[trialNumber],
|
|
152
|
+
);
|
|
153
|
+
const updateStore = useCallback((updates: Record<string, any>) => {
|
|
154
|
+
storeUpdatesRef.current = { ...storeUpdatesRef.current, ...updates };
|
|
155
|
+
}, []);
|
|
156
|
+
|
|
157
|
+
const content = trialAt(compiled, state.pointer);
|
|
158
|
+
|
|
159
|
+
const measureRefreshRate = content?.collectRefreshRate;
|
|
160
|
+
useEffect(() => {
|
|
161
|
+
if (!measureRefreshRate) return;
|
|
162
|
+
return collectRefreshRate((rate) => updateStore({ __screenRefreshRate: rate }));
|
|
163
|
+
}, [measureRefreshRate, updateStore]);
|
|
164
|
+
|
|
165
|
+
if (compiled !== run.compiled) {
|
|
166
|
+
setCompiled(run.compiled);
|
|
167
|
+
dispatch({ type: 'rebuild', previous: compiled });
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let componentToRender: ReactNode = null;
|
|
172
|
+
if (content) {
|
|
173
|
+
const Component = content.component;
|
|
174
|
+
componentToRender = (
|
|
175
|
+
<Component
|
|
176
|
+
key={trialNumber}
|
|
177
|
+
next={next}
|
|
178
|
+
updateStore={updateStore}
|
|
179
|
+
store={state.store}
|
|
180
|
+
data={state.data}
|
|
181
|
+
{...state.props}
|
|
182
|
+
name={content.name}
|
|
183
|
+
/>
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const length = compiled.bytecode.instructions.length;
|
|
188
|
+
const withinFullscreen = compiled.bytecode.fullscreen.some(([start, end]) => state.pointer >= start && state.pointer < end);
|
|
189
|
+
return (
|
|
190
|
+
<ThemeContext.Provider value={experiment.config?.theme ?? 'light'}>
|
|
191
|
+
<AudioDeviceContext.Provider value={state.store.__audioInputId}>
|
|
192
|
+
<ParamsProvider value={run.params}>
|
|
193
|
+
<CompiledTimelineContext.Provider value={compiled}>
|
|
194
|
+
<div className='w-full h-full'>
|
|
195
|
+
<FullscreenGuard within={withinFullscreen}>
|
|
196
|
+
{experiment.config?.showProgressBar && (
|
|
197
|
+
<ProgressBar progress={length > 0 ? state.pointer / length : 0} />
|
|
198
|
+
)}
|
|
199
|
+
{componentToRender}
|
|
200
|
+
</FullscreenGuard>
|
|
201
|
+
{!devDisabled && run.params.dev === true && (
|
|
202
|
+
<DevPanel content={content} props={state.props} store={state.store} data={state.data} />
|
|
203
|
+
)}
|
|
204
|
+
</div>
|
|
205
|
+
</CompiledTimelineContext.Provider>
|
|
206
|
+
</ParamsProvider>
|
|
207
|
+
</AudioDeviceContext.Provider>
|
|
208
|
+
</ThemeContext.Provider>
|
|
209
|
+
);
|
|
210
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import { createContext, useContext, useEffect, useState } from 'react';
|
|
3
|
+
import { getNative } from '../core/platform';
|
|
4
|
+
import { enterFullscreen, exitFullscreen, isFullscreen } from '../utils/fullscreen';
|
|
5
|
+
|
|
6
|
+
const SetGuardContext = createContext<(config: FullscreenGuardConfig | null) => void>(() => {});
|
|
7
|
+
|
|
8
|
+
/** Turns the fullscreen guard on with a config, or off with `null`. Unlike a store change, it takes effect at once. */
|
|
9
|
+
export const useSetFullscreenGuard = () => useContext(SetGuardContext);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Asks the participant back into fullscreen while a fullscreenBlock with keepFullscreen runs, and
|
|
13
|
+
* leaves fullscreen once the run is outside every fullscreenBlock, whether it ended or a jump left it.
|
|
14
|
+
*/
|
|
15
|
+
export function FullscreenGuard({ within, children }: { within: boolean; children: React.ReactNode }) {
|
|
16
|
+
const [cfg, setCfg] = useState<FullscreenGuardConfig | null>(null);
|
|
17
|
+
const [fs, setFs] = useState(isFullscreen());
|
|
18
|
+
const active = !!cfg;
|
|
19
|
+
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (!within) return;
|
|
22
|
+
return () => {
|
|
23
|
+
setCfg(null);
|
|
24
|
+
getNative()?.exitFullscreen();
|
|
25
|
+
if (isFullscreen()) exitFullscreen().catch((err) => console.error('Leaving fullscreen failed:', err));
|
|
26
|
+
};
|
|
27
|
+
}, [within]);
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (!active) return;
|
|
31
|
+
setFs(isFullscreen());
|
|
32
|
+
let interval: ReturnType<typeof setInterval> | null = null;
|
|
33
|
+
const timeout = setTimeout(() => {
|
|
34
|
+
interval = setInterval(() => setFs(isFullscreen()), 3000);
|
|
35
|
+
}, 5000);
|
|
36
|
+
return () => {
|
|
37
|
+
clearTimeout(timeout);
|
|
38
|
+
if (interval) clearInterval(interval);
|
|
39
|
+
};
|
|
40
|
+
}, [active]);
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<SetGuardContext.Provider value={setCfg}>
|
|
44
|
+
{children}
|
|
45
|
+
{cfg && !fs && <GuardPrompt cfg={cfg} />}
|
|
46
|
+
</SetGuardContext.Provider>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function GuardPrompt({ cfg }: { cfg: FullscreenGuardConfig }) {
|
|
51
|
+
const reenter = async () => {
|
|
52
|
+
try {
|
|
53
|
+
// Clear stale DOM fullscreen state first — on macOS Chrome after ESC,
|
|
54
|
+
// fullscreenElement may still be set even though the window is windowed,
|
|
55
|
+
// causing requestFullscreen to silently no-op.
|
|
56
|
+
if (document.fullscreenElement) {
|
|
57
|
+
await exitFullscreen().catch(() => {});
|
|
58
|
+
}
|
|
59
|
+
await enterFullscreen();
|
|
60
|
+
} catch (err) {
|
|
61
|
+
console.error('[FullscreenGuard] requestFullscreen failed:', err);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const buttonText = cfg.buttonText ?? 'Return to Fullscreen';
|
|
65
|
+
const btnClass = 'px-6 py-3 bg-white text-black text-sm font-bold border-2 border-black rounded-xl shadow-[2px_2px_0px_rgba(0,0,0,1)] hover:translate-x-[1px] hover:translate-y-[1px] hover:shadow-none cursor-pointer';
|
|
66
|
+
|
|
67
|
+
if (cfg.mode === 'overlay') {
|
|
68
|
+
return (
|
|
69
|
+
<div className='fixed inset-0 z-50 bg-white flex flex-col items-center justify-center gap-6 p-8 text-center'>
|
|
70
|
+
{cfg.content ?? (
|
|
71
|
+
<>
|
|
72
|
+
<h1 className='text-3xl font-bold'>Please return to fullscreen</h1>
|
|
73
|
+
<p>This experiment requires fullscreen mode. Click the button below to continue.</p>
|
|
74
|
+
</>
|
|
75
|
+
)}
|
|
76
|
+
<button onClick={reenter} className={btnClass}>{buttonText}</button>
|
|
77
|
+
</div>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
return (
|
|
81
|
+
<button onClick={reenter} className={`fixed top-6 left-6 z-50 ${btnClass}`}>
|
|
82
|
+
{buttonText}
|
|
83
|
+
</button>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* How a fullscreen block asks the participant back into fullscreen: with a small button in the
|
|
89
|
+
* top-left corner (the default), or with a screen over the trial that shows `content` above the button.
|
|
90
|
+
*/
|
|
91
|
+
export type FullscreenGuardConfig =
|
|
92
|
+
| { mode?: 'button'; buttonText?: string; content?: never }
|
|
93
|
+
| { mode: 'overlay'; buttonText?: string; content?: React.ReactNode };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { ParamType } from '../core/params';
|
|
2
|
+
|
|
3
|
+
const fieldClass =
|
|
4
|
+
'block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm';
|
|
5
|
+
|
|
6
|
+
/** The settings page's on/off switch. */
|
|
7
|
+
export function Switch({ on, onClick }: { on: boolean; onClick: () => void }) {
|
|
8
|
+
return (
|
|
9
|
+
<button
|
|
10
|
+
type='button'
|
|
11
|
+
onClick={onClick}
|
|
12
|
+
className={`relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 ${on ? 'bg-blue-600' : 'bg-gray-200'}`}
|
|
13
|
+
role='switch'
|
|
14
|
+
aria-checked={on}
|
|
15
|
+
>
|
|
16
|
+
<span
|
|
17
|
+
className={`pointer-events-none inline-block h-5 w-5 rounded-full bg-white shadow transform ring-0 transition ease-in-out duration-200 ${on ? 'translate-x-5' : 'translate-x-0'}`}
|
|
18
|
+
/>
|
|
19
|
+
</button>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The settings page's field for a param's value, kept as text; an empty value leaves the param at its default. */
|
|
24
|
+
export function ParamInput({
|
|
25
|
+
type,
|
|
26
|
+
value,
|
|
27
|
+
onChange,
|
|
28
|
+
values,
|
|
29
|
+
defaultValue,
|
|
30
|
+
}: {
|
|
31
|
+
type: ParamType;
|
|
32
|
+
value: string;
|
|
33
|
+
onChange: (value: string) => void;
|
|
34
|
+
/** The values the param can take, offered in a dropdown. */
|
|
35
|
+
values?: readonly (string | number)[];
|
|
36
|
+
defaultValue?: unknown;
|
|
37
|
+
}) {
|
|
38
|
+
if (values) {
|
|
39
|
+
return (
|
|
40
|
+
<select className={fieldClass} value={value} onChange={(e) => onChange(e.target.value)}>
|
|
41
|
+
<option value=''>
|
|
42
|
+
{defaultValue === undefined ? 'Default (computed for each participant)' : `Default (${defaultValue})`}
|
|
43
|
+
</option>
|
|
44
|
+
{values.map((option) => (
|
|
45
|
+
<option key={String(option)} value={String(option)}>
|
|
46
|
+
{String(option)}
|
|
47
|
+
</option>
|
|
48
|
+
))}
|
|
49
|
+
</select>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (type === 'boolean') {
|
|
54
|
+
// A switch nobody has touched shows the default
|
|
55
|
+
const on = (value || String(defaultValue)) === 'true';
|
|
56
|
+
return (
|
|
57
|
+
<>
|
|
58
|
+
<Switch on={on} onClick={() => onChange(on ? 'false' : 'true')} />
|
|
59
|
+
<span className='ml-4 text-gray-500'>{on ? 'True' : 'False'}</span>
|
|
60
|
+
</>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (type === 'array' || type === 'json') {
|
|
65
|
+
return (
|
|
66
|
+
<textarea
|
|
67
|
+
className={fieldClass}
|
|
68
|
+
value={value}
|
|
69
|
+
onChange={(e) => onChange(e.target.value)}
|
|
70
|
+
placeholder={type === 'array' ? 'A JSON array, e.g. [1, 2, 3]' : 'A JSON object, e.g. {"key": "value"}'}
|
|
71
|
+
rows={3}
|
|
72
|
+
/>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const hasDefault = defaultValue !== undefined && defaultValue !== '';
|
|
77
|
+
return (
|
|
78
|
+
<input
|
|
79
|
+
type={type === 'number' ? 'number' : 'text'}
|
|
80
|
+
className={fieldClass}
|
|
81
|
+
value={value}
|
|
82
|
+
onChange={(e) => onChange(e.target.value)}
|
|
83
|
+
placeholder={hasDefault ? `Default: ${defaultValue}` : 'Enter a value...'}
|
|
84
|
+
/>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { t, useTheme } from './theme';
|
|
2
|
+
|
|
3
|
+
/** How far the participant is through the timeline, from 0 to 1. */
|
|
4
|
+
export function ProgressBar({ progress }: { progress: number }) {
|
|
5
|
+
const th = t(useTheme());
|
|
6
|
+
return (
|
|
7
|
+
<div className='px-4 mt-4 sm:mt-12 max-w-2xl mx-auto'>
|
|
8
|
+
<div className={`h-6 ${th.progressBg} rounded-full overflow-hidden`}>
|
|
9
|
+
<div
|
|
10
|
+
className={`h-full rounded-full ${progress > 0 ? 'border-black border-2' : ''}`}
|
|
11
|
+
style={{
|
|
12
|
+
width: `${progress * 100}%`,
|
|
13
|
+
backgroundImage: `repeating-linear-gradient(-45deg, ${th.progressStripe1}, ${th.progressStripe1} 10px, ${th.progressStripe2} 10px, ${th.progressStripe2} 20px)`,
|
|
14
|
+
transition: 'width 300ms ease-in-out',
|
|
15
|
+
}}
|
|
16
|
+
/>
|
|
17
|
+
</div>
|
|
18
|
+
</div>
|
|
19
|
+
);
|
|
20
|
+
}
|