@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.
Files changed (156) hide show
  1. package/README.md +796 -229
  2. package/bin/add.js +56 -0
  3. package/bin/deploy.js +48 -0
  4. package/bin/download.js +28 -0
  5. package/bin/ghsetup.js +21 -0
  6. package/bin/native.js +81 -0
  7. package/bin/setup.js +11 -17
  8. package/bin/study-deploy.js +60 -0
  9. package/bin/undeploy.js +15 -0
  10. package/extras/transcription/compose.yaml +32 -0
  11. package/{template → extras}/transcription/transcribe.py +4 -9
  12. package/package.json +62 -64
  13. package/src/bun-env.d.ts +1 -0
  14. package/src/bun-plugin.ts +22 -0
  15. package/src/components/canvasblock.tsx +209 -537
  16. package/src/components/checkdevice.tsx +64 -84
  17. package/src/components/fullscreenblock.tsx +130 -0
  18. package/src/components/icons.tsx +50 -0
  19. package/src/components/index.ts +9 -11
  20. package/src/components/microphonecheck.tsx +76 -38
  21. package/src/components/plaininput.tsx +48 -41
  22. package/src/components/prolificending.tsx +64 -28
  23. package/src/components/quest.tsx +89 -87
  24. package/src/components/{randomdotkinetogram.tsx → randomdotkinematogram.tsx} +205 -267
  25. package/src/components/{voicerecorder.tsx → recorder.tsx} +81 -106
  26. package/src/components/requestfilepermission.tsx +63 -0
  27. package/src/components/stimulus.tsx +373 -0
  28. package/src/components/storeui.tsx +94 -148
  29. package/src/components/text.tsx +61 -59
  30. package/src/components/tutorial.tsx +45 -47
  31. package/src/components/upload.tsx +133 -309
  32. package/src/components/voicerecording.tsx +90 -94
  33. package/src/core/backend.ts +20 -0
  34. package/src/core/bytecode.ts +178 -0
  35. package/src/core/columns.ts +50 -0
  36. package/src/core/component.ts +238 -0
  37. package/src/core/development.ts +32 -0
  38. package/src/core/experiment.ts +263 -0
  39. package/src/core/files.ts +291 -0
  40. package/src/core/params.ts +166 -0
  41. package/src/core/platform.ts +29 -0
  42. package/src/core/rows.ts +106 -0
  43. package/src/core/simulation.ts +158 -0
  44. package/src/core/slides.ts +178 -0
  45. package/src/core/store.ts +22 -0
  46. package/src/core/timeline.ts +399 -0
  47. package/src/fonts/AtkinsonHyperlegible-Bold.woff2 +0 -0
  48. package/src/fonts/AtkinsonHyperlegible-BoldItalic.woff2 +0 -0
  49. package/src/fonts/AtkinsonHyperlegible-Italic.woff2 +0 -0
  50. package/src/fonts/AtkinsonHyperlegible-Regular.woff2 +0 -0
  51. package/src/index.css +5 -7
  52. package/src/mod.tsx +31 -17
  53. package/src/native.ts +127 -0
  54. package/src/quest.ts +2 -0
  55. package/src/runner/contexts.ts +35 -0
  56. package/src/runner/devpanel.tsx +68 -0
  57. package/src/runner/experimentrunner.tsx +210 -0
  58. package/src/runner/fullscreenguard.tsx +93 -0
  59. package/src/runner/paraminput.tsx +86 -0
  60. package/src/runner/progressbar.tsx +20 -0
  61. package/src/runner/settingsscreen.tsx +517 -0
  62. package/src/{utils → runner}/theme.ts +9 -8
  63. package/src/server.ts +93 -0
  64. package/src/tutorial.ts +3 -0
  65. package/src/utils/array.ts +30 -62
  66. package/src/utils/canvas.ts +66 -0
  67. package/src/utils/ddm.ts +47 -70
  68. package/src/utils/distributions.ts +352 -61
  69. package/src/utils/fullscreen.ts +38 -0
  70. package/src/utils/ids.ts +11 -0
  71. package/src/utils/keys.ts +46 -0
  72. package/src/utils/llm.ts +15 -15
  73. package/src/utils/time.ts +12 -0
  74. package/src/utils/tts.ts +24 -24
  75. package/template/.dockerignore +3 -1
  76. package/template/.env.example +5 -0
  77. package/template/.github/workflows/deploy.yml +25 -0
  78. package/template/.gitignore +14 -0
  79. package/template/Dockerfile +9 -14
  80. package/template/README.md +74 -214
  81. package/template/biome.json +34 -0
  82. package/template/build.ts +20 -0
  83. package/template/bunfig.toml +3 -0
  84. package/template/docker-compose.yaml +4 -33
  85. package/template/index.html +2 -2
  86. package/template/package.json +16 -89
  87. package/template/public/.gitkeep +0 -0
  88. package/template/server.ts +32 -0
  89. package/template/simulate.ts +4 -44
  90. package/template/src/Experiment.tsx +76 -128
  91. package/template/src/bun-env.d.ts +1 -0
  92. package/template/src/index.css +1 -9
  93. package/template/src/main.tsx +25 -10
  94. package/template/tsconfig.json +4 -6
  95. package/.claude/settings.local.json +0 -61
  96. package/.eslintrc.cjs +0 -18
  97. package/.playwright-mcp/console-2026-04-07T14-22-59-060Z.log +0 -1
  98. package/.prettierrc +0 -5
  99. package/Dockerfile +0 -20
  100. package/dist/mod-HDegGPrf.js +0 -87434
  101. package/dist/mod.d.ts +0 -946
  102. package/dist/reactive.es.js +0 -87
  103. package/dist/reactive.umd.js +0 -10255
  104. package/dist/style.css +0 -5
  105. package/dist/web-7BbR1Ad7.js +0 -435
  106. package/dist/web-CATfZO1Q.js +0 -12
  107. package/planned.md +0 -103
  108. package/postcss.config.js +0 -6
  109. package/raw-loader-hooks.mjs +0 -25
  110. package/raw-loader.mjs +0 -5
  111. package/rdk_doc.md +0 -341
  112. package/src/components/enterfullscreen.tsx +0 -230
  113. package/src/components/exitfullscreen.tsx +0 -113
  114. package/src/components/experimentprovider.tsx +0 -69
  115. package/src/components/experimentrunner.tsx +0 -497
  116. package/src/components/mobilefilepermission.tsx +0 -68
  117. package/src/components/settingsscreen.tsx +0 -770
  118. package/src/types/array.d.ts +0 -6
  119. package/src/utils/audiodevice.ts +0 -4
  120. package/src/utils/bytecode.ts +0 -261
  121. package/src/utils/common.ts +0 -291
  122. package/src/utils/ddm.test.ts +0 -233
  123. package/src/utils/distributions.test.ts +0 -209
  124. package/src/utils/nodecompat.ts +0 -11
  125. package/src/utils/overlays.ts +0 -19
  126. package/src/utils/request.ts +0 -25
  127. package/src/utils/simulation.ts +0 -299
  128. package/src/utils/upload.ts +0 -275
  129. package/src/vite-env.d.ts +0 -1
  130. package/template/.env.template +0 -2
  131. package/template/.eslintrc.cjs +0 -18
  132. package/template/.prettierrc +0 -5
  133. package/template/backend/package-lock.json +0 -2544
  134. package/template/backend/package.json +0 -31
  135. package/template/backend/src/backend.ts +0 -131
  136. package/template/backend/tsconfig.json +0 -110
  137. package/template/capacitor.config.ts +0 -15
  138. package/template/electron/main.js +0 -131
  139. package/template/electron/preload.js +0 -9
  140. package/template/electron.vite.config.js +0 -45
  141. package/template/package-lock.json +0 -12467
  142. package/template/postcss.config.js +0 -5
  143. package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-Bold.ttf +0 -0
  144. package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-BoldItalic.ttf +0 -0
  145. package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-Italic.ttf +0 -0
  146. package/template/public/Atkinson_Hyperlegible/AtkinsonHyperlegible-Regular.ttf +0 -0
  147. package/template/shared.vite.config.js +0 -39
  148. package/template/src/vite-env.d.ts +0 -2
  149. package/template/tsconfig.node.json +0 -12
  150. package/template/vite.config.js +0 -11
  151. package/tsconfig.json +0 -30
  152. package/tsconfig.node.json +0 -12
  153. package/vite.config.ts +0 -39
  154. /package/{template → extras}/transcription/Dockerfile.cpu +0 -0
  155. /package/{template → extras}/transcription/Dockerfile.gpu +0 -0
  156. /package/{template/public/Atkinson_Hyperlegible → src/fonts}/OFL.txt +0 -0
@@ -1,77 +1,62 @@
1
- import { BaseComponentProps } from '../mod';
1
+ import { BaseComponentProps } from '../core/component';
2
2
  import { useState } from 'react';
3
- import { registerSimulation } from '../utils/simulation';
4
- import { registerFlattener } from '../utils/upload';
3
+ import { defineComponent } from '../core/component';
5
4
  import { uniform } from '../utils/distributions';
6
- import { useTheme, t } from '../utils/theme';
7
-
8
- registerFlattener('PlainInput', 'session');
9
-
10
- registerSimulation('PlainInput', (trialProps, _experimentState, simulators, participant) => {
11
- const result = simulators.respond(trialProps, participant);
12
- const typingDuration = String(result.value).length * uniform(50, 150);
13
- return {
14
- responseData: { value: result.value },
15
- participantState: result.participantState,
16
- storeUpdates: trialProps.storeupdate ? trialProps.storeupdate(result.value) : undefined,
17
- duration: typingDuration,
18
- };
19
- }, {
20
- respond: (_input: any, participant: any) => ({
21
- value: 'simulated_input',
22
- participantState: participant,
23
- }),
24
- });
5
+ import { useTheme, t } from '../runner/theme';
25
6
 
26
7
  function PlainInput({
27
8
  content,
28
- buttonText = 'Click me',
9
+ buttonText = 'Continue',
29
10
  className = '',
11
+ containerClass,
12
+ centered = true,
30
13
  next,
31
- updateStore,
32
14
  animate = false,
33
- storeupdate,
34
15
  placeholder = 'Enter your response here',
35
- }: BaseComponentProps & {
36
- content: React.ReactNode;
16
+ required = true,
17
+ }: BaseComponentProps<{ value: string }> & {
18
+ content?: React.ReactNode;
19
+ /** The button that submits the answer; an empty text shows none, and Enter submits. */
37
20
  buttonText?: string;
38
- onButtonClick?: () => void;
39
21
  className?: string;
22
+ containerClass?: string;
23
+ centered?: boolean;
40
24
  animate?: boolean;
41
- storeupdate?: (entry: string) => { [key: string]: any };
42
25
  placeholder?: string;
26
+ /** Keeps the button disabled until something is entered. */
27
+ required?: boolean;
43
28
  }) {
44
29
  const th = t(useTheme());
45
30
  const [inputValue, setInputValue] = useState('');
31
+ const canSubmit = !required || inputValue.trim() !== '';
46
32
 
47
33
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
48
34
  setInputValue(e.target.value);
49
35
  };
50
36
 
51
- const handleClick = () => {
52
- if (storeupdate) {
53
- updateStore(storeupdate(inputValue));
54
- }
55
- next({ value: inputValue });
37
+ const handleSubmit = (e: React.FormEvent) => {
38
+ e.preventDefault();
39
+ if (canSubmit) next({ value: inputValue });
56
40
  };
57
41
 
58
42
  return (
59
- <div className={`min-h-screen ${th.containerBg}`}>
60
- <div className={`max-w-prose mx-auto ${className} mt-20 mb-20 px-4 `}>
43
+ <div className={`min-h-screen ${centered ? 'flex items-center justify-center' : ''} ${containerClass ?? th.containerBg}`}>
44
+ <div className={`w-full max-w-prose mx-auto ${className} pt-20 pb-20 px-4 `}>
61
45
  <article
62
- className={`prose prose-2xl ${th.prose} text-xl ${th.proseLink} prose-a:underline prose-h1:text-4xl prose-h1:mb-10 prose-h1:font-bold prose-p:mb-4 prose-strong:font-bold ${th.text} leading-relaxed
46
+ className={`${th.article}
63
47
  ${animate ? 'animate-slide-down opacity-0' : ''}`}
64
48
  >
65
49
  {content}
66
50
  </article>
67
51
 
52
+ <form onSubmit={handleSubmit}>
68
53
  <div className={`mt-8 ${animate ? 'animate-slide-down opacity-0' : ''}`}>
69
54
  <input
70
55
  type='text'
71
56
  value={inputValue}
72
57
  onChange={handleChange}
73
58
  placeholder={placeholder}
74
- className={`w-full px-4 py-3 border-2 ${th.inputBorder} ${th.inputBg} ${th.inputText} rounded-xl text-lg focus:outline-none focus:ring-2 ${th.focusRing}`}
59
+ className={`w-full px-4 py-3 border-2 ${th.inputBorder} ${th.inputBg} ${th.inputText} rounded-xl text-lg focus:outline-none`}
75
60
  />
76
61
  </div>
77
62
 
@@ -81,16 +66,38 @@ function PlainInput({
81
66
  style={animate ? { animationDelay: '1s' } : {}}
82
67
  >
83
68
  <button
84
- onClick={handleClick}
85
- className={`${th.buttonBg} cursor-pointer px-8 py-3 border-2 ${th.buttonBorder} font-bold ${th.buttonText} text-lg rounded-xl ${th.buttonShadow} hover:translate-x-[2px] hover:translate-y-[2px] hover:shadow-none`}
69
+ type='submit'
70
+ disabled={!canSubmit}
71
+ className={
72
+ canSubmit
73
+ ? th.button
74
+ : `px-8 py-3 border-2 font-bold text-lg rounded-xl ${th.buttonDisabledBg} ${th.buttonDisabledBorder} ${th.buttonDisabledText} ${th.buttonDisabledShadow} cursor-default`
75
+ }
86
76
  >
87
77
  {buttonText}
88
78
  </button>
89
79
  </div>
90
80
  )}
81
+ </form>
91
82
  </div>
92
83
  </div>
93
84
  );
94
85
  }
95
86
 
96
- export default PlainInput;
87
+ export default defineComponent({
88
+ name: 'PlainInput',
89
+ component: PlainInput,
90
+ session: true,
91
+ simulate: ({ props, simulators, participant }) => {
92
+ const result = simulators.respond({ props, participant });
93
+ const typingDuration = String(result.value).length * uniform(50, 150);
94
+ return {
95
+ recorded: { value: result.value },
96
+ participant: result.participant,
97
+ durationMs: typingDuration,
98
+ };
99
+ },
100
+ defaultSimulators: {
101
+ respond: () => ({ value: 'simulated_input' }),
102
+ },
103
+ });
@@ -1,45 +1,81 @@
1
- import { BaseComponentProps, getParam, registerComponentParams } from '../utils/common';
2
- import Text from '../components/text';
3
- import { registerSimulation, noopSimulate } from '../utils/simulation';
4
- import { registerFlattener } from '../utils/upload';
1
+ import type { ReactNode } from 'react';
2
+ import { useParams } from '../runner/contexts';
3
+ import Text from './text';
4
+ import { noopSimulate } from '../core/simulation';
5
+ import { defineComponent } from '../core/component';
5
6
 
6
- registerFlattener('ProlificEnding', null);
7
- registerSimulation('ProlificEnding', noopSimulate, {});
7
+ const params = {
8
+ cc: { default: '', description: 'Prolific completion code, used when the prolificCode prop is not set' },
9
+ };
8
10
 
9
- registerComponentParams('ProlificEnding', [
10
- { name: 'cc', defaultValue: '', type: 'string', description: 'Completion code of the Profilic experiment (more commonly supplied via the code)' },
11
- ])
11
+ const defaultContent = (code: string) => (
12
+ <p>
13
+ Thank you! Your data has been successfully submitted. <br /> You can go back to Prolific and
14
+ enter the code {code} to finish the study. Alternatively, you can click on this link:{' '}
15
+ <a
16
+ target='_blank'
17
+ rel='noopener noreferrer'
18
+ className='text-blue-400'
19
+ href={`https://app.prolific.com/submissions/complete?cc=${code}`}
20
+ >
21
+ Go to Prolific
22
+ </a>
23
+ </p>
24
+ );
12
25
 
13
- export default function ProlificEnding({
26
+ function ProlificEnding({
14
27
  prolificCode,
28
+ content = defaultContent,
15
29
  className,
16
30
  containerClass,
17
31
  centered,
18
32
  animate,
19
- data,
20
- updateStore
21
- }: { prolificCode?: string; className?: string; containerClass?: string; centered?: boolean; animate?: boolean } & BaseComponentProps) {
22
- let prolificCodeUsed = prolificCode ?? getParam('cc', '', 'string') ?? null;
33
+ }: {
34
+ prolificCode?: string;
35
+ /** The message, given the completion code. Prolific's completion link is https://app.prolific.com/submissions/complete?cc=<code>. */
36
+ content?: (code: string) => ReactNode;
37
+ className?: string;
38
+ containerClass?: string;
39
+ centered?: boolean;
40
+ animate?: boolean;
41
+ }) {
42
+ const { cc } = useParams(params);
23
43
 
24
- const content = (
44
+ const message = (
25
45
  <div className='flex flex-col items-center'>
26
46
  <svg className='w-12 h-12' fill='none' stroke='currentColor' viewBox='0 0 24 24'>
27
47
  <path strokeLinecap='round' strokeLinejoin='round' strokeWidth={2} d='M5 13l4 4L19 7' />
28
48
  </svg>
29
- <p className=''>
30
- Thank you! Your data has been successfully submitted. <br /> You can go back to Prolific and
31
- enter the code {prolificCodeUsed} to finish the study. Alternatively, you can click on this
32
- link:{' '}
33
- <a
34
- target='_blank'
35
- className='text-blue-400'
36
- href={`https://app.prolific.com/submissions/complete?cc=${prolificCodeUsed}`}
37
- >
38
- Go to Prolific
39
- </a>
40
- </p>
49
+ {content(prolificCode ?? cc)}
41
50
  </div>
42
51
  );
43
52
 
44
- return <Text data={data} updateStore={updateStore} className={className} containerClass={containerClass} centered={centered} animate={animate} content={content} buttonText='' next={() => {}} />;
53
+ return (
54
+ <Text
55
+ className={className}
56
+ containerClass={containerClass}
57
+ centered={centered}
58
+ animate={animate}
59
+ content={message}
60
+ buttonText=''
61
+ next={() => {}}
62
+ />
63
+ );
45
64
  }
65
+
66
+ export default defineComponent({
67
+ name: 'ProlificEnding',
68
+ component: ProlificEnding,
69
+ csv: null,
70
+ params,
71
+ simulate: noopSimulate,
72
+ check: ({ props, params, trials, index, production }) => {
73
+ if (!trials.slice(0, index).some((trial) => trial.type === 'Upload')) {
74
+ return 'ProlificEnding ends the experiment, so an Upload has to come before it, or the data is never saved.';
75
+ }
76
+ // Development and simulation run without the real link, which may be what sets the code
77
+ if (production && props !== undefined && !props.prolificCode && !params.cc) {
78
+ return 'ProlificEnding has no completion code to show: set its prolificCode, or the cc param in the link.';
79
+ }
80
+ },
81
+ });
@@ -1,70 +1,12 @@
1
- import { useCallback, createElement, ComponentType } from 'react';
1
+ import { createElement, type ComponentType, useState } from 'react';
2
2
  import { defaultCss, Model, Question, Serializer } from 'survey-core';
3
3
  import { ReactQuestionFactory, Survey, SurveyQuestionElementBase } from 'survey-react-ui';
4
4
  import { ContrastLight, ContrastDark } from 'survey-core/themes';
5
5
  import 'survey-core/survey-core.min.css';
6
- import { registerSimulation } from '../utils/simulation';
7
- import { registerFlattener } from '../utils/upload';
6
+ import { defineComponent } from '../core/component';
8
7
  import { uniform } from '../utils/distributions';
9
- import { useTheme, t } from '../utils/theme';
10
-
11
- registerFlattener('Quest', 'session');
12
-
13
- registerSimulation('Quest', (trialProps, _experimentState, simulators, participant) => {
14
- const responseData: Record<string, any> = {};
15
- let totalDuration = 0;
16
- const pages = trialProps.surveyJson?.pages || [{ elements: trialProps.surveyJson?.elements || [] }];
17
- for (const page of pages) {
18
- for (const el of page.elements || []) {
19
- if (!el.name) continue;
20
- const result = simulators.answerQuestion(el, participant);
21
- participant = result.participantState;
22
- responseData[el.name] = result.value;
23
- totalDuration += result.duration ?? 0;
24
- }
25
- }
26
- return { responseData, participantState: participant, duration: totalDuration };
27
- }, {
28
- answerQuestion: (question: any, participant: any) => {
29
- let value;
30
- switch (question.type) {
31
- case 'rating': {
32
- const min = question.rateMin ?? 1, max = question.rateMax ?? 5;
33
- value = min + Math.floor(uniform(0, max - min + 1));
34
- break;
35
- }
36
- case 'boolean': value = uniform(0, 1) > 0.5; break;
37
- case 'text': case 'comment': value = 'simulated_response'; break;
38
- case 'radiogroup': case 'dropdown': {
39
- const c = question.choices?.[Math.floor(uniform(0, question.choices?.length || 0))];
40
- value = c !== undefined ? (typeof c === 'object' ? c.value : c) : null;
41
- break;
42
- }
43
- case 'checkbox': {
44
- if (question.choices?.length) {
45
- const n = 1 + Math.floor(uniform(0, question.choices.length));
46
- value = [...question.choices]
47
- .sort(() => uniform(0, 1) - 0.5).slice(0, n)
48
- .map((c: any) => typeof c === 'object' ? c.value : c);
49
- }
50
- break;
51
- }
52
- case 'matrix': {
53
- if (question.rows?.length && question.columns?.length) {
54
- value = Object.fromEntries(
55
- question.rows.map((r: any) => {
56
- const col = question.columns[Math.floor(uniform(0, question.columns.length))];
57
- return [typeof r === 'object' ? r.value : r, typeof col === 'object' ? col.value : col];
58
- }),
59
- );
60
- }
61
- break;
62
- }
63
- default: value = null;
64
- }
65
- return { value, participantState: participant, duration: uniform(1000, 5000) };
66
- },
67
- });
8
+ import { useTheme, t } from '../runner/theme';
9
+ import { Recorder } from './recorder';
68
10
 
69
11
  type ComponentsMap = {
70
12
  [key: string]: ComponentType<any>;
@@ -113,52 +55,112 @@ const registerCustomQuestion = (name: string, component: ComponentType<any>) =>
113
55
  });
114
56
  };
115
57
 
116
- // The main Quest component
58
+ // The voicerecorder question type, which records an answer with the shared recorder
59
+ function VoiceRecorderQuestion({ setValue }: { setValue: (data: object | null) => void }) {
60
+ return <Recorder handleSaveVoiceData={(data) => setValue(data)} handleDiscardVoiceData={() => setValue(null)} />;
61
+ }
62
+
117
63
  function Quest({
118
64
  next,
119
65
  surveyJson,
120
- customQuestions = {},
121
- theme,
66
+ customQuestions,
67
+ surveyTheme,
68
+ className = '',
122
69
  containerClass,
123
70
  }: {
124
- next: (data: object) => void;
125
- surveyJson: object;
71
+ next: (data?: Record<string, any>) => void;
72
+ surveyJson: Record<string, any>;
73
+ /** Custom question types, by the name the survey's elements use as their type. */
126
74
  customQuestions?: ComponentsMap;
127
- theme?: any;
75
+ /** A SurveyJS theme; by default one that matches the experiment's theme. */
76
+ surveyTheme?: any;
77
+ className?: string;
128
78
  containerClass?: string;
129
79
  }) {
130
80
  const contextTheme = useTheme();
131
81
  const th = t(contextTheme);
132
82
 
133
- Object.keys(customQuestions).forEach((name) => {
134
- registerCustomQuestion(name, customQuestions[name]);
135
- });
136
-
137
- const survey = new Model({
138
- ...surveyJson,
139
- css: { ...defaultCss, root: 'sd-root-modern custom-root' },
83
+ // One survey for the trial, so that a re-render keeps the participant's answers
84
+ const [survey] = useState(() => {
85
+ for (const [name, component] of Object.entries({ voicerecorder: VoiceRecorderQuestion, ...customQuestions })) {
86
+ registerCustomQuestion(name, component);
87
+ }
88
+ const model = new Model({ ...surveyJson, css: { ...defaultCss, root: 'sd-root-modern custom-root' } });
89
+ model.applyTheme(surveyTheme ?? (contextTheme.startsWith('dark') ? ContrastDark : ContrastLight));
90
+ model.onComplete.add((sender: Model) => next({ ...sender.data }));
91
+ return model;
140
92
  });
141
- survey.applyTheme(theme ?? (contextTheme.startsWith('dark') ? ContrastDark : ContrastLight));
142
-
143
- const saveResults = useCallback(
144
- (sender: any) => {
145
- next({ ...sender.data });
146
- },
147
- [next],
148
- );
149
-
150
- survey.onComplete.add(saveResults);
151
93
 
152
94
  const useCustomBg = !!containerClass || contextTheme.startsWith('dark');
153
95
 
154
96
  return (
155
97
  <div className={`min-h-screen ${containerClass ?? th.containerBg}`}>
156
98
  {!useCustomBg && <div className='absolute inset-0 -z-10 h-full w-full bg-white bg-[radial-gradient(#e5e7eb_1px,transparent_1px)] [background-size:16px_16px]' />}
157
- <div className={`max-w-4xl mx-auto px-4${useCustomBg ? ' quest-custom-bg' : ''}`}>
99
+ <div className={`max-w-4xl mx-auto px-4 ${className}${useCustomBg ? ' quest-custom-bg' : ''}`}>
158
100
  <Survey model={survey} />
159
101
  </div>
160
102
  </div>
161
103
  );
162
104
  }
163
105
 
164
- export default Quest;
106
+ export default defineComponent({
107
+ name: 'Quest',
108
+ component: Quest,
109
+ session: true,
110
+ simulate: ({ props, simulators, participant }) => {
111
+ const recorded: Record<string, any> = {};
112
+ let totalDuration = 0;
113
+ const pages = props.surveyJson?.pages || [{ elements: props.surveyJson?.elements || [] }];
114
+ for (const page of pages) {
115
+ for (const el of page.elements || []) {
116
+ if (!el.name) continue;
117
+ const result = simulators.answerQuestion({ props, participant, question: el });
118
+ participant = result.participant ?? participant;
119
+ recorded[el.name] = result.value.answer;
120
+ totalDuration += result.value.durationMs;
121
+ }
122
+ }
123
+ return { recorded, participant, durationMs: totalDuration };
124
+ },
125
+ defaultSimulators: {
126
+ answerQuestion: ({ question }) => {
127
+ let value: any;
128
+ switch (question.type) {
129
+ case 'rating': {
130
+ const min = question.rateMin ?? 1, max = question.rateMax ?? 5;
131
+ value = min + Math.floor(uniform(0, max - min + 1));
132
+ break;
133
+ }
134
+ case 'boolean': value = uniform(0, 1) > 0.5; break;
135
+ case 'text': case 'comment': value = 'simulated_response'; break;
136
+ case 'radiogroup': case 'dropdown': {
137
+ const c = question.choices?.[Math.floor(uniform(0, question.choices?.length || 0))];
138
+ value = c !== undefined ? (typeof c === 'object' ? c.value : c) : null;
139
+ break;
140
+ }
141
+ case 'checkbox': {
142
+ if (question.choices?.length) {
143
+ const n = 1 + Math.floor(uniform(0, question.choices.length));
144
+ value = [...question.choices]
145
+ .sort(() => uniform(0, 1) - 0.5).slice(0, n)
146
+ .map((c: any) => typeof c === 'object' ? c.value : c);
147
+ }
148
+ break;
149
+ }
150
+ case 'matrix': {
151
+ if (question.rows?.length && question.columns?.length) {
152
+ value = Object.fromEntries(
153
+ question.rows.map((r: any) => {
154
+ const col = question.columns[Math.floor(uniform(0, question.columns.length))];
155
+ return [typeof r === 'object' ? r.value : r, typeof col === 'object' ? col.value : col];
156
+ }),
157
+ );
158
+ }
159
+ break;
160
+ }
161
+ default: value = null;
162
+ }
163
+ return { value: { answer: value, durationMs: uniform(1000, 5000) } };
164
+ },
165
+ },
166
+ });