@slopit/adapter-psychojs 0.1.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/LICENSE +21 -0
- package/dist/index.d.ts +206 -0
- package/dist/index.js +211 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aaron Steven White
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { RecorderConfig, StimulusInfo, BaseRecorder, CompletedTrial } from '@slopit/adapter-shared';
|
|
2
|
+
import { PlatformInfo, ResponseInfo } from '@slopit/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Type definitions for PsychoJS adapter.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Configuration for PsychoJSRecorder.
|
|
12
|
+
*
|
|
13
|
+
* Extends the base RecorderConfig with PsychoJS-specific options.
|
|
14
|
+
*/
|
|
15
|
+
interface PsychoJSRecorderConfig extends RecorderConfig {
|
|
16
|
+
/**
|
|
17
|
+
* PsychoJS instance for version detection.
|
|
18
|
+
*
|
|
19
|
+
* If provided, the recorder will attempt to detect the PsychoJS version
|
|
20
|
+
* from this instance.
|
|
21
|
+
*/
|
|
22
|
+
psychoJS?: unknown;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Configuration for starting a trial in PsychoJS.
|
|
26
|
+
*
|
|
27
|
+
* This interface maps to common PsychoJS trial parameters.
|
|
28
|
+
*/
|
|
29
|
+
interface PsychoJSTrialConfig {
|
|
30
|
+
/**
|
|
31
|
+
* Unique identifier for the trial.
|
|
32
|
+
*
|
|
33
|
+
* If not provided, one will be generated automatically.
|
|
34
|
+
*/
|
|
35
|
+
trialId?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Zero-indexed position of the trial in the experiment.
|
|
38
|
+
*
|
|
39
|
+
* If not provided, will be auto-incremented.
|
|
40
|
+
*/
|
|
41
|
+
trialIndex?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Type identifier for the trial (e.g., "text_entry", "survey").
|
|
44
|
+
*/
|
|
45
|
+
trialType?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Information about the stimulus presented in this trial.
|
|
48
|
+
*/
|
|
49
|
+
stimulus?: StimulusInfo;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* PsychoJS recorder implementation.
|
|
54
|
+
*
|
|
55
|
+
* @module
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* SlopitRecorder for PsychoJS experiments.
|
|
60
|
+
*
|
|
61
|
+
* Provides behavioral capture integration for PsychoJS/Pavlovia experiments.
|
|
62
|
+
* This recorder extends BaseRecorder with PsychoJS-specific functionality
|
|
63
|
+
* for handling PIXI.js elements and PsychoJS experiment handlers.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* import { SlopitRecorder } from "@slopit/adapter-psychojs";
|
|
68
|
+
*
|
|
69
|
+
* const recorder = new SlopitRecorder({ psychoJS });
|
|
70
|
+
*
|
|
71
|
+
* // In routineBegin:
|
|
72
|
+
* recorder.startTrial("trial_1", textBox._pixi);
|
|
73
|
+
*
|
|
74
|
+
* // In routineEnd:
|
|
75
|
+
* const data = recorder.endTrial(textBox.text);
|
|
76
|
+
*
|
|
77
|
+
* // At experiment end:
|
|
78
|
+
* const session = recorder.exportSession();
|
|
79
|
+
* recorder.storeInExperiment(psychoJS);
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare class SlopitRecorder extends BaseRecorder {
|
|
83
|
+
/**
|
|
84
|
+
* Reference to the PsychoJS instance for version detection.
|
|
85
|
+
*/
|
|
86
|
+
private psychoJS;
|
|
87
|
+
/**
|
|
88
|
+
* Creates a new SlopitRecorder for PsychoJS.
|
|
89
|
+
*
|
|
90
|
+
* @param config - Recorder configuration
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```typescript
|
|
94
|
+
* const recorder = new SlopitRecorder({
|
|
95
|
+
* psychoJS,
|
|
96
|
+
* participantId: "P001",
|
|
97
|
+
* studyId: "study-123",
|
|
98
|
+
* });
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
constructor(config?: PsychoJSRecorderConfig);
|
|
102
|
+
/**
|
|
103
|
+
* Gets platform-specific information for PsychoJS.
|
|
104
|
+
*
|
|
105
|
+
* @returns Platform information including detected PsychoJS version
|
|
106
|
+
*/
|
|
107
|
+
protected getPlatformInfo(): PlatformInfo;
|
|
108
|
+
/**
|
|
109
|
+
* Detects the PsychoJS version from available sources.
|
|
110
|
+
*
|
|
111
|
+
* Attempts detection in the following order:
|
|
112
|
+
* 1. From the psychoJS instance's version property
|
|
113
|
+
* 2. From the global PsychoJS.VERSION constant
|
|
114
|
+
* 3. Falls back to "unknown" if detection fails
|
|
115
|
+
*
|
|
116
|
+
* @returns Detected PsychoJS version or "unknown"
|
|
117
|
+
*/
|
|
118
|
+
private detectPsychoJSVersion;
|
|
119
|
+
/**
|
|
120
|
+
* Starts capturing behavioral data for a trial.
|
|
121
|
+
*
|
|
122
|
+
* Call this in routineBegin of your PsychoJS experiment.
|
|
123
|
+
*
|
|
124
|
+
* @param trialIdOrConfig - Trial ID string or full trial configuration
|
|
125
|
+
* @param element - DOM element or PsychoJS/PIXI component to capture
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* // Simple usage with trial ID and element
|
|
130
|
+
* recorder.startTrial("trial_1", textBox._pixi);
|
|
131
|
+
*
|
|
132
|
+
* // Full configuration
|
|
133
|
+
* recorder.startTrial({
|
|
134
|
+
* trialId: "trial_1",
|
|
135
|
+
* trialIndex: 0,
|
|
136
|
+
* trialType: "free_response",
|
|
137
|
+
* stimulus: {
|
|
138
|
+
* type: "text",
|
|
139
|
+
* content: "Please describe your experience.",
|
|
140
|
+
* },
|
|
141
|
+
* }, textBox._pixi);
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
startTrial(trialIdOrConfig: string | PsychoJSTrialConfig, element?: HTMLElement | unknown): void;
|
|
145
|
+
/**
|
|
146
|
+
* Builds a TrialConfig from PsychoJSTrialConfig, handling optional properties.
|
|
147
|
+
*
|
|
148
|
+
* @param psychoConfig - PsychoJS trial configuration
|
|
149
|
+
* @returns TrialConfig with only defined properties
|
|
150
|
+
*/
|
|
151
|
+
private buildTrialConfig;
|
|
152
|
+
/**
|
|
153
|
+
* Extracts the actual DOM element from a PsychoJS/PIXI object.
|
|
154
|
+
*
|
|
155
|
+
* PsychoJS TextBox components wrap HTML input elements in PIXI structures.
|
|
156
|
+
* This method navigates that structure to find the underlying DOM element.
|
|
157
|
+
*
|
|
158
|
+
* @param element - HTMLElement or PsychoJS/PIXI component
|
|
159
|
+
* @returns The extracted HTMLElement or undefined if not found
|
|
160
|
+
*/
|
|
161
|
+
private extractDOMElement;
|
|
162
|
+
/**
|
|
163
|
+
* Ends the current trial and returns captured data.
|
|
164
|
+
*
|
|
165
|
+
* Call this in routineEnd of your PsychoJS experiment.
|
|
166
|
+
*
|
|
167
|
+
* @param response - The participant's response (text string or ResponseInfo object)
|
|
168
|
+
* @returns The completed trial data including behavioral capture
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* // Simple usage with text response
|
|
173
|
+
* const data = recorder.endTrial(textBox.text);
|
|
174
|
+
*
|
|
175
|
+
* // With full response info
|
|
176
|
+
* const data = recorder.endTrial({
|
|
177
|
+
* type: "text",
|
|
178
|
+
* value: textBox.text,
|
|
179
|
+
* characterCount: textBox.text.length,
|
|
180
|
+
* wordCount: textBox.text.trim().split(/\s+/).length,
|
|
181
|
+
* });
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
endTrial(response?: string | ResponseInfo): CompletedTrial;
|
|
185
|
+
/**
|
|
186
|
+
* Stores session data in PsychoJS experiment handler.
|
|
187
|
+
*
|
|
188
|
+
* Call this at experiment end before uploading to Pavlovia.
|
|
189
|
+
* The session data will be included in the experiment data file.
|
|
190
|
+
*
|
|
191
|
+
* @param psychoJS - PsychoJS instance with experimentHandler
|
|
192
|
+
* @param key - Key to store the session under (default: "slopit_session")
|
|
193
|
+
*
|
|
194
|
+
* @example
|
|
195
|
+
* ```typescript
|
|
196
|
+
* // At experiment end:
|
|
197
|
+
* recorder.storeInExperiment(psychoJS);
|
|
198
|
+
*
|
|
199
|
+
* // With custom key:
|
|
200
|
+
* recorder.storeInExperiment(psychoJS, "behavioral_data");
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
storeInExperiment(psychoJS: unknown, key?: string): void;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export { SlopitRecorder as PsychoJSRecorder, type PsychoJSRecorderConfig, type PsychoJSTrialConfig, SlopitRecorder };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { BaseRecorder } from '@slopit/adapter-shared';
|
|
2
|
+
|
|
3
|
+
// src/recorder.ts
|
|
4
|
+
var ADAPTER_VERSION = "0.1.0";
|
|
5
|
+
var SlopitRecorder = class extends BaseRecorder {
|
|
6
|
+
/**
|
|
7
|
+
* Reference to the PsychoJS instance for version detection.
|
|
8
|
+
*/
|
|
9
|
+
psychoJS;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new SlopitRecorder for PsychoJS.
|
|
12
|
+
*
|
|
13
|
+
* @param config - Recorder configuration
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const recorder = new SlopitRecorder({
|
|
18
|
+
* psychoJS,
|
|
19
|
+
* participantId: "P001",
|
|
20
|
+
* studyId: "study-123",
|
|
21
|
+
* });
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
constructor(config) {
|
|
25
|
+
super(config);
|
|
26
|
+
this.psychoJS = config?.psychoJS;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Gets platform-specific information for PsychoJS.
|
|
30
|
+
*
|
|
31
|
+
* @returns Platform information including detected PsychoJS version
|
|
32
|
+
*/
|
|
33
|
+
getPlatformInfo() {
|
|
34
|
+
return {
|
|
35
|
+
name: "PsychoJS",
|
|
36
|
+
version: this.detectPsychoJSVersion(),
|
|
37
|
+
adapterVersion: ADAPTER_VERSION
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Detects the PsychoJS version from available sources.
|
|
42
|
+
*
|
|
43
|
+
* Attempts detection in the following order:
|
|
44
|
+
* 1. From the psychoJS instance's version property
|
|
45
|
+
* 2. From the global PsychoJS.VERSION constant
|
|
46
|
+
* 3. Falls back to "unknown" if detection fails
|
|
47
|
+
*
|
|
48
|
+
* @returns Detected PsychoJS version or "unknown"
|
|
49
|
+
*/
|
|
50
|
+
detectPsychoJSVersion() {
|
|
51
|
+
if (this.psychoJS !== null && this.psychoJS !== void 0) {
|
|
52
|
+
const instance = this.psychoJS;
|
|
53
|
+
if (typeof instance["version"] === "string") {
|
|
54
|
+
return instance["version"];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (typeof window !== "undefined") {
|
|
58
|
+
const win = window;
|
|
59
|
+
const psychoJSGlobal = win["PsychoJS"];
|
|
60
|
+
if (psychoJSGlobal !== void 0 && typeof psychoJSGlobal["VERSION"] === "string") {
|
|
61
|
+
return psychoJSGlobal["VERSION"];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return "unknown";
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Starts capturing behavioral data for a trial.
|
|
68
|
+
*
|
|
69
|
+
* Call this in routineBegin of your PsychoJS experiment.
|
|
70
|
+
*
|
|
71
|
+
* @param trialIdOrConfig - Trial ID string or full trial configuration
|
|
72
|
+
* @param element - DOM element or PsychoJS/PIXI component to capture
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* // Simple usage with trial ID and element
|
|
77
|
+
* recorder.startTrial("trial_1", textBox._pixi);
|
|
78
|
+
*
|
|
79
|
+
* // Full configuration
|
|
80
|
+
* recorder.startTrial({
|
|
81
|
+
* trialId: "trial_1",
|
|
82
|
+
* trialIndex: 0,
|
|
83
|
+
* trialType: "free_response",
|
|
84
|
+
* stimulus: {
|
|
85
|
+
* type: "text",
|
|
86
|
+
* content: "Please describe your experience.",
|
|
87
|
+
* },
|
|
88
|
+
* }, textBox._pixi);
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
startTrial(trialIdOrConfig, element) {
|
|
92
|
+
const config = typeof trialIdOrConfig === "string" ? { trialId: trialIdOrConfig } : this.buildTrialConfig(trialIdOrConfig);
|
|
93
|
+
const extractedElement = element !== void 0 ? this.extractDOMElement(element) : void 0;
|
|
94
|
+
if (extractedElement !== void 0) {
|
|
95
|
+
config.targetElement = extractedElement;
|
|
96
|
+
}
|
|
97
|
+
super.startTrial(config);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Builds a TrialConfig from PsychoJSTrialConfig, handling optional properties.
|
|
101
|
+
*
|
|
102
|
+
* @param psychoConfig - PsychoJS trial configuration
|
|
103
|
+
* @returns TrialConfig with only defined properties
|
|
104
|
+
*/
|
|
105
|
+
buildTrialConfig(psychoConfig) {
|
|
106
|
+
const config = {};
|
|
107
|
+
if (psychoConfig.trialId !== void 0) {
|
|
108
|
+
config.trialId = psychoConfig.trialId;
|
|
109
|
+
}
|
|
110
|
+
if (psychoConfig.trialIndex !== void 0) {
|
|
111
|
+
config.trialIndex = psychoConfig.trialIndex;
|
|
112
|
+
}
|
|
113
|
+
if (psychoConfig.trialType !== void 0) {
|
|
114
|
+
config.trialType = psychoConfig.trialType;
|
|
115
|
+
}
|
|
116
|
+
if (psychoConfig.stimulus !== void 0) {
|
|
117
|
+
config.stimulus = psychoConfig.stimulus;
|
|
118
|
+
}
|
|
119
|
+
return config;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Extracts the actual DOM element from a PsychoJS/PIXI object.
|
|
123
|
+
*
|
|
124
|
+
* PsychoJS TextBox components wrap HTML input elements in PIXI structures.
|
|
125
|
+
* This method navigates that structure to find the underlying DOM element.
|
|
126
|
+
*
|
|
127
|
+
* @param element - HTMLElement or PsychoJS/PIXI component
|
|
128
|
+
* @returns The extracted HTMLElement or undefined if not found
|
|
129
|
+
*/
|
|
130
|
+
extractDOMElement(element) {
|
|
131
|
+
if (element === null || element === void 0) {
|
|
132
|
+
return void 0;
|
|
133
|
+
}
|
|
134
|
+
if (element instanceof HTMLElement) {
|
|
135
|
+
return element;
|
|
136
|
+
}
|
|
137
|
+
const pixiElement = element;
|
|
138
|
+
if (pixiElement["_input_div"] instanceof HTMLElement) {
|
|
139
|
+
return pixiElement["_input_div"];
|
|
140
|
+
}
|
|
141
|
+
if (pixiElement["htmlInput"] instanceof HTMLElement) {
|
|
142
|
+
return pixiElement["htmlInput"];
|
|
143
|
+
}
|
|
144
|
+
const pixi = pixiElement["_pixi"];
|
|
145
|
+
if (pixi !== void 0 && pixi["_input_div"] instanceof HTMLElement) {
|
|
146
|
+
return pixi["_input_div"];
|
|
147
|
+
}
|
|
148
|
+
return void 0;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Ends the current trial and returns captured data.
|
|
152
|
+
*
|
|
153
|
+
* Call this in routineEnd of your PsychoJS experiment.
|
|
154
|
+
*
|
|
155
|
+
* @param response - The participant's response (text string or ResponseInfo object)
|
|
156
|
+
* @returns The completed trial data including behavioral capture
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* // Simple usage with text response
|
|
161
|
+
* const data = recorder.endTrial(textBox.text);
|
|
162
|
+
*
|
|
163
|
+
* // With full response info
|
|
164
|
+
* const data = recorder.endTrial({
|
|
165
|
+
* type: "text",
|
|
166
|
+
* value: textBox.text,
|
|
167
|
+
* characterCount: textBox.text.length,
|
|
168
|
+
* wordCount: textBox.text.trim().split(/\s+/).length,
|
|
169
|
+
* });
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
endTrial(response) {
|
|
173
|
+
const responseInfo = typeof response === "string" ? {
|
|
174
|
+
type: "text",
|
|
175
|
+
value: response,
|
|
176
|
+
characterCount: response.length,
|
|
177
|
+
wordCount: response.trim().split(/\s+/).filter(Boolean).length
|
|
178
|
+
} : response;
|
|
179
|
+
return super.endTrial(responseInfo);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Stores session data in PsychoJS experiment handler.
|
|
183
|
+
*
|
|
184
|
+
* Call this at experiment end before uploading to Pavlovia.
|
|
185
|
+
* The session data will be included in the experiment data file.
|
|
186
|
+
*
|
|
187
|
+
* @param psychoJS - PsychoJS instance with experimentHandler
|
|
188
|
+
* @param key - Key to store the session under (default: "slopit_session")
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```typescript
|
|
192
|
+
* // At experiment end:
|
|
193
|
+
* recorder.storeInExperiment(psychoJS);
|
|
194
|
+
*
|
|
195
|
+
* // With custom key:
|
|
196
|
+
* recorder.storeInExperiment(psychoJS, "behavioral_data");
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
storeInExperiment(psychoJS, key = "slopit_session") {
|
|
200
|
+
const session = this.exportSession();
|
|
201
|
+
const psychoJSInstance = psychoJS;
|
|
202
|
+
const handler = psychoJSInstance["experiment"];
|
|
203
|
+
if (handler !== void 0 && typeof handler["addData"] === "function") {
|
|
204
|
+
handler["addData"](key, JSON.stringify(session));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
export { SlopitRecorder as PsychoJSRecorder, SlopitRecorder };
|
|
210
|
+
//# sourceMappingURL=index.js.map
|
|
211
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/recorder.ts"],"names":[],"mappings":";;;AAcA,IAAM,eAAA,GAAkB,OAAA;AA0BjB,IAAM,cAAA,GAAN,cAA6B,YAAA,CAAa;AAAA;AAAA;AAAA;AAAA,EAIvC,QAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAgBR,YAAY,MAAA,EAAiC;AAC3C,IAAA,KAAA,CAAM,MAAM,CAAA;AACZ,IAAA,IAAA,CAAK,WAAW,MAAA,EAAQ,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CAAU,OAAO,CAAC,CAAA;AAAA,IAC3F;AAAA,EACF;AACF","file":"index.js","sourcesContent":["/**\n * PsychoJS recorder implementation.\n *\n * @module\n */\n\nimport { BaseRecorder, type CompletedTrial, type TrialConfig } from \"@slopit/adapter-shared\";\nimport type { PlatformInfo, ResponseInfo } from \"@slopit/core\";\n\nimport type { PsychoJSRecorderConfig, PsychoJSTrialConfig } from \"./types.js\";\n\n/**\n * Adapter version for this package.\n */\nconst ADAPTER_VERSION = \"0.1.0\";\n\n/**\n * SlopitRecorder for PsychoJS experiments.\n *\n * Provides behavioral capture integration for PsychoJS/Pavlovia experiments.\n * This recorder extends BaseRecorder with PsychoJS-specific functionality\n * for handling PIXI.js elements and PsychoJS experiment handlers.\n *\n * @example\n * ```typescript\n * import { SlopitRecorder } from \"@slopit/adapter-psychojs\";\n *\n * const recorder = new SlopitRecorder({ psychoJS });\n *\n * // In routineBegin:\n * recorder.startTrial(\"trial_1\", textBox._pixi);\n *\n * // In routineEnd:\n * const data = recorder.endTrial(textBox.text);\n *\n * // At experiment end:\n * const session = recorder.exportSession();\n * recorder.storeInExperiment(psychoJS);\n * ```\n */\nexport class SlopitRecorder extends BaseRecorder {\n /**\n * Reference to the PsychoJS instance for version detection.\n */\n private psychoJS: unknown;\n\n /**\n * Creates a new SlopitRecorder for PsychoJS.\n *\n * @param config - Recorder configuration\n *\n * @example\n * ```typescript\n * const recorder = new SlopitRecorder({\n * psychoJS,\n * participantId: \"P001\",\n * studyId: \"study-123\",\n * });\n * ```\n */\n constructor(config?: PsychoJSRecorderConfig) {\n super(config);\n this.psychoJS = config?.psychoJS;\n }\n\n /**\n * Gets platform-specific information for PsychoJS.\n *\n * @returns Platform information including detected PsychoJS version\n */\n protected override getPlatformInfo(): PlatformInfo {\n return {\n name: \"PsychoJS\",\n version: this.detectPsychoJSVersion(),\n adapterVersion: ADAPTER_VERSION,\n };\n }\n\n /**\n * Detects the PsychoJS version from available sources.\n *\n * Attempts detection in the following order:\n * 1. From the psychoJS instance's version property\n * 2. From the global PsychoJS.VERSION constant\n * 3. Falls back to \"unknown\" if detection fails\n *\n * @returns Detected PsychoJS version or \"unknown\"\n */\n private detectPsychoJSVersion(): string {\n // Try to detect from the provided psychoJS instance\n if (this.psychoJS !== null && this.psychoJS !== undefined) {\n const instance = this.psychoJS as Record<string, unknown>;\n if (typeof instance[\"version\"] === \"string\") {\n return instance[\"version\"];\n }\n }\n\n // Try to detect from global PsychoJS object\n if (typeof window !== \"undefined\") {\n const win = window as unknown as Record<string, unknown>;\n const psychoJSGlobal = win[\"PsychoJS\"] as Record<string, unknown> | undefined;\n if (psychoJSGlobal !== undefined && typeof psychoJSGlobal[\"VERSION\"] === \"string\") {\n return psychoJSGlobal[\"VERSION\"];\n }\n }\n\n return \"unknown\";\n }\n\n /**\n * Starts capturing behavioral data for a trial.\n *\n * Call this in routineBegin of your PsychoJS experiment.\n *\n * @param trialIdOrConfig - Trial ID string or full trial configuration\n * @param element - DOM element or PsychoJS/PIXI component to capture\n *\n * @example\n * ```typescript\n * // Simple usage with trial ID and element\n * recorder.startTrial(\"trial_1\", textBox._pixi);\n *\n * // Full configuration\n * recorder.startTrial({\n * trialId: \"trial_1\",\n * trialIndex: 0,\n * trialType: \"free_response\",\n * stimulus: {\n * type: \"text\",\n * content: \"Please describe your experience.\",\n * },\n * }, textBox._pixi);\n * ```\n */\n override startTrial(\n trialIdOrConfig: string | PsychoJSTrialConfig,\n element?: HTMLElement | unknown,\n ): void {\n const config: TrialConfig =\n typeof trialIdOrConfig === \"string\"\n ? { trialId: trialIdOrConfig }\n : this.buildTrialConfig(trialIdOrConfig);\n\n const extractedElement = element !== undefined ? this.extractDOMElement(element) : undefined;\n if (extractedElement !== undefined) {\n config.targetElement = extractedElement;\n }\n\n super.startTrial(config);\n }\n\n /**\n * Builds a TrialConfig from PsychoJSTrialConfig, handling optional properties.\n *\n * @param psychoConfig - PsychoJS trial configuration\n * @returns TrialConfig with only defined properties\n */\n private buildTrialConfig(psychoConfig: PsychoJSTrialConfig): TrialConfig {\n const config: TrialConfig = {};\n\n if (psychoConfig.trialId !== undefined) {\n config.trialId = psychoConfig.trialId;\n }\n if (psychoConfig.trialIndex !== undefined) {\n config.trialIndex = psychoConfig.trialIndex;\n }\n if (psychoConfig.trialType !== undefined) {\n config.trialType = psychoConfig.trialType;\n }\n if (psychoConfig.stimulus !== undefined) {\n config.stimulus = psychoConfig.stimulus;\n }\n\n return config;\n }\n\n /**\n * Extracts the actual DOM element from a PsychoJS/PIXI object.\n *\n * PsychoJS TextBox components wrap HTML input elements in PIXI structures.\n * This method navigates that structure to find the underlying DOM element.\n *\n * @param element - HTMLElement or PsychoJS/PIXI component\n * @returns The extracted HTMLElement or undefined if not found\n */\n private extractDOMElement(element: HTMLElement | unknown): HTMLElement | undefined {\n // Handle null/undefined\n if (element === null || element === undefined) {\n return undefined;\n }\n\n // If it's already an HTML element, return it\n if (element instanceof HTMLElement) {\n return element;\n }\n\n // Element is some other object (likely PIXI or PsychoJS component)\n const pixiElement = element as Record<string, unknown>;\n\n // Try to extract from PIXI TextInput (_input_div property)\n if (pixiElement[\"_input_div\"] instanceof HTMLElement) {\n return pixiElement[\"_input_div\"];\n }\n\n // Try htmlInput property (some versions use this)\n if (pixiElement[\"htmlInput\"] instanceof HTMLElement) {\n return pixiElement[\"htmlInput\"];\n }\n\n // For TextBox, the _pixi property may contain the input\n const pixi = pixiElement[\"_pixi\"] as Record<string, unknown> | undefined;\n if (pixi !== undefined && pixi[\"_input_div\"] instanceof HTMLElement) {\n return pixi[\"_input_div\"];\n }\n\n return undefined;\n }\n\n /**\n * Ends the current trial and returns captured data.\n *\n * Call this in routineEnd of your PsychoJS experiment.\n *\n * @param response - The participant's response (text string or ResponseInfo object)\n * @returns The completed trial data including behavioral capture\n *\n * @example\n * ```typescript\n * // Simple usage with text response\n * const data = recorder.endTrial(textBox.text);\n *\n * // With full response info\n * const data = recorder.endTrial({\n * type: \"text\",\n * value: textBox.text,\n * characterCount: textBox.text.length,\n * wordCount: textBox.text.trim().split(/\\s+/).length,\n * });\n * ```\n */\n override endTrial(response?: string | ResponseInfo): CompletedTrial {\n const responseInfo: ResponseInfo | undefined =\n typeof response === \"string\"\n ? {\n type: \"text\",\n value: response,\n characterCount: response.length,\n wordCount: response.trim().split(/\\s+/).filter(Boolean).length,\n }\n : response;\n\n return super.endTrial(responseInfo);\n }\n\n /**\n * Stores session data in PsychoJS experiment handler.\n *\n * Call this at experiment end before uploading to Pavlovia.\n * The session data will be included in the experiment data file.\n *\n * @param psychoJS - PsychoJS instance with experimentHandler\n * @param key - Key to store the session under (default: \"slopit_session\")\n *\n * @example\n * ```typescript\n * // At experiment end:\n * recorder.storeInExperiment(psychoJS);\n *\n * // With custom key:\n * recorder.storeInExperiment(psychoJS, \"behavioral_data\");\n * ```\n */\n storeInExperiment(psychoJS: unknown, key = \"slopit_session\"): void {\n const session = this.exportSession();\n const psychoJSInstance = psychoJS as Record<string, unknown>;\n const handler = psychoJSInstance[\"experiment\"] as Record<string, unknown> | undefined;\n\n if (handler !== undefined && typeof handler[\"addData\"] === \"function\") {\n (handler[\"addData\"] as (key: string, value: string) => void)(key, JSON.stringify(session));\n }\n }\n}\n\n/**\n * Alias for SlopitRecorder.\n *\n * Provides a more explicit name for the PsychoJS adapter.\n */\nexport { SlopitRecorder as PsychoJSRecorder };\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@slopit/adapter-psychojs",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "PsychoJS adapter for slopit behavioral capture",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Aaron Steven White",
|
|
9
|
+
"email": "aaron.white@rochester.edu",
|
|
10
|
+
"url": "https://aaronstevenwhite.io"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://slopit.github.io",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/slopit/slopit.git",
|
|
16
|
+
"directory": "packages/adapter-psychojs"
|
|
17
|
+
},
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/slopit/slopit/issues"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"psychojs",
|
|
26
|
+
"pavlovia",
|
|
27
|
+
"behavioral-research",
|
|
28
|
+
"experiment",
|
|
29
|
+
"psychology",
|
|
30
|
+
"ai-detection",
|
|
31
|
+
"crowdsourcing"
|
|
32
|
+
],
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"import": "./dist/index.js",
|
|
36
|
+
"types": "./dist/index.d.ts"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist"
|
|
41
|
+
],
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@slopit/behavioral": "0.1.0",
|
|
44
|
+
"@slopit/core": "0.1.0",
|
|
45
|
+
"@slopit/adapter-shared": "0.1.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"jsdom": "^25.0.1",
|
|
49
|
+
"tsup": "^8.3.5",
|
|
50
|
+
"typescript": "^5.7.2",
|
|
51
|
+
"vitest": "^2.1.8"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsup",
|
|
55
|
+
"typecheck": "tsc --noEmit",
|
|
56
|
+
"test": "vitest run",
|
|
57
|
+
"test:watch": "vitest",
|
|
58
|
+
"clean": "rm -rf dist"
|
|
59
|
+
}
|
|
60
|
+
}
|