@jspsych/plugin-html-keyboard-response 1.1.3 → 2.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.
@@ -1,174 +1,184 @@
1
1
  var jsPsychHtmlKeyboardResponse = (function (jspsych) {
2
2
  'use strict';
3
3
 
4
+ var version = "2.1.0";
5
+
4
6
  const info = {
5
- name: "html-keyboard-response",
6
- parameters: {
7
- /**
8
- * The HTML string to be displayed.
9
- */
10
- stimulus: {
11
- type: jspsych.ParameterType.HTML_STRING,
12
- pretty_name: "Stimulus",
13
- default: undefined,
14
- },
15
- /**
16
- * Array containing the key(s) the subject is allowed to press to respond to the stimulus.
17
- */
18
- choices: {
19
- type: jspsych.ParameterType.KEYS,
20
- pretty_name: "Choices",
21
- default: "ALL_KEYS",
22
- },
23
- /**
24
- * Any content here will be displayed below the stimulus.
25
- */
26
- prompt: {
27
- type: jspsych.ParameterType.HTML_STRING,
28
- pretty_name: "Prompt",
29
- default: null,
30
- },
31
- /**
32
- * How long to show the stimulus.
33
- */
34
- stimulus_duration: {
35
- type: jspsych.ParameterType.INT,
36
- pretty_name: "Stimulus duration",
37
- default: null,
38
- },
39
- /**
40
- * How long to show trial before it ends.
41
- */
42
- trial_duration: {
43
- type: jspsych.ParameterType.INT,
44
- pretty_name: "Trial duration",
45
- default: null,
46
- },
47
- /**
48
- * If true, trial will end when subject makes a response.
49
- */
50
- response_ends_trial: {
51
- type: jspsych.ParameterType.BOOL,
52
- pretty_name: "Response ends trial",
53
- default: true,
54
- },
7
+ name: "html-keyboard-response",
8
+ version,
9
+ parameters: {
10
+ /**
11
+ * The string to be displayed.
12
+ */
13
+ stimulus: {
14
+ type: jspsych.ParameterType.HTML_STRING,
15
+ default: void 0
16
+ },
17
+ /**
18
+ * This array contains the key(s) that the participant is allowed to press in order to respond
19
+ * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see
20
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}
21
+ * and
22
+ * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}
23
+ * for more examples. Any key presses that are not listed in the
24
+ * array will be ignored. The default value of `"ALL_KEYS"` means that all keys will be accepted as valid responses.
25
+ * Specifying `"NO_KEYS"` will mean that no responses are allowed.
26
+ */
27
+ choices: {
28
+ type: jspsych.ParameterType.KEYS,
29
+ default: "ALL_KEYS"
30
+ },
31
+ /**
32
+ * This string can contain HTML markup. Any content here will be displayed below the stimulus.
33
+ * The intention is that it can be used to provide a reminder about the action the participant
34
+ * is supposed to take (e.g., which key to press).
35
+ */
36
+ prompt: {
37
+ type: jspsych.ParameterType.HTML_STRING,
38
+ default: null
39
+ },
40
+ /**
41
+ * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus
42
+ * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will
43
+ * remain visible until the trial ends.
44
+ */
45
+ stimulus_duration: {
46
+ type: jspsych.ParameterType.INT,
47
+ default: null
48
+ },
49
+ /**
50
+ * How long to wait for the participant to make a response before ending the trial in milliseconds.
51
+ * If the participant fails to make a response before this timer is reached, the participant's response
52
+ * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,
53
+ * then the trial will wait for a response indefinitely.
54
+ */
55
+ trial_duration: {
56
+ type: jspsych.ParameterType.INT,
57
+ default: null
55
58
  },
59
+ /**
60
+ * If true, then the trial will end whenever the participant makes a response (assuming they make their
61
+ * response before the cutoff specified by the trial_duration parameter). If false, then the trial will
62
+ * continue until the value for trial_duration is reached. You can set this parameter to false to force
63
+ * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.
64
+ */
65
+ response_ends_trial: {
66
+ type: jspsych.ParameterType.BOOL,
67
+ default: true
68
+ }
69
+ },
70
+ data: {
71
+ /** Indicates which key the participant pressed. */
72
+ response: {
73
+ type: jspsych.ParameterType.STRING
74
+ },
75
+ /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. */
76
+ rt: {
77
+ type: jspsych.ParameterType.INT
78
+ },
79
+ /** The HTML content that was displayed on the screen. */
80
+ stimulus: {
81
+ type: jspsych.ParameterType.STRING
82
+ }
83
+ },
84
+ // prettier-ignore
85
+ citations: {
86
+ "apa": "de Leeuw, J. R., Gilbert, R. A., & Luchterhandt, B. (2023). jsPsych: Enabling an Open-Source Collaborative Ecosystem of Behavioral Experiments. Journal of Open Source Software, 8(85), 5351. https://doi.org/10.21105/joss.05351 ",
87
+ "bibtex": '@article{Leeuw2023jsPsych, author = {de Leeuw, Joshua R. and Gilbert, Rebecca A. and Luchterhandt, Bj{\\" o}rn}, journal = {Journal of Open Source Software}, doi = {10.21105/joss.05351}, issn = {2475-9066}, number = {85}, year = {2023}, month = {may 11}, pages = {5351}, publisher = {Open Journals}, title = {jsPsych: Enabling an {Open}-{Source} {Collaborative} {Ecosystem} of {Behavioral} {Experiments}}, url = {https://joss.theoj.org/papers/10.21105/joss.05351}, volume = {8}, } '
88
+ }
56
89
  };
57
- /**
58
- * **html-keyboard-response**
59
- *
60
- * jsPsych plugin for displaying a stimulus and getting a keyboard response
61
- *
62
- * @author Josh de Leeuw
63
- * @see {@link https://www.jspsych.org/plugins/jspsych-html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}
64
- */
65
90
  class HtmlKeyboardResponsePlugin {
66
- constructor(jsPsych) {
67
- this.jsPsych = jsPsych;
91
+ constructor(jsPsych) {
92
+ this.jsPsych = jsPsych;
93
+ }
94
+ static {
95
+ this.info = info;
96
+ }
97
+ trial(display_element, trial) {
98
+ var new_html = '<div id="jspsych-html-keyboard-response-stimulus">' + trial.stimulus + "</div>";
99
+ if (trial.prompt !== null) {
100
+ new_html += trial.prompt;
101
+ }
102
+ display_element.innerHTML = new_html;
103
+ var response = {
104
+ rt: null,
105
+ key: null
106
+ };
107
+ const end_trial = () => {
108
+ if (typeof keyboardListener !== "undefined") {
109
+ this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
110
+ }
111
+ var trial_data = {
112
+ rt: response.rt,
113
+ stimulus: trial.stimulus,
114
+ response: response.key
115
+ };
116
+ this.jsPsych.finishTrial(trial_data);
117
+ };
118
+ var after_response = (info2) => {
119
+ display_element.querySelector("#jspsych-html-keyboard-response-stimulus").className += " responded";
120
+ if (response.key == null) {
121
+ response = info2;
122
+ }
123
+ if (trial.response_ends_trial) {
124
+ end_trial();
125
+ }
126
+ };
127
+ if (trial.choices != "NO_KEYS") {
128
+ var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({
129
+ callback_function: after_response,
130
+ valid_responses: trial.choices,
131
+ rt_method: "performance",
132
+ persist: false,
133
+ allow_held_key: false
134
+ });
68
135
  }
69
- trial(display_element, trial) {
70
- var new_html = '<div id="jspsych-html-keyboard-response-stimulus">' + trial.stimulus + "</div>";
71
- // add prompt
72
- if (trial.prompt !== null) {
73
- new_html += trial.prompt;
74
- }
75
- // draw
76
- display_element.innerHTML = new_html;
77
- // store response
78
- var response = {
79
- rt: null,
80
- key: null,
81
- };
82
- // function to end trial when it is time
83
- const end_trial = () => {
84
- // kill any remaining setTimeout handlers
85
- this.jsPsych.pluginAPI.clearAllTimeouts();
86
- // kill keyboard listeners
87
- if (typeof keyboardListener !== "undefined") {
88
- this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);
89
- }
90
- // gather the data to store for the trial
91
- var trial_data = {
92
- rt: response.rt,
93
- stimulus: trial.stimulus,
94
- response: response.key,
95
- };
96
- // clear the display
97
- display_element.innerHTML = "";
98
- // move on to the next trial
99
- this.jsPsych.finishTrial(trial_data);
100
- };
101
- // function to handle responses by the subject
102
- var after_response = (info) => {
103
- // after a valid response, the stimulus will have the CSS class 'responded'
104
- // which can be used to provide visual feedback that a response was recorded
105
- display_element.querySelector("#jspsych-html-keyboard-response-stimulus").className +=
106
- " responded";
107
- // only record the first response
108
- if (response.key == null) {
109
- response = info;
110
- }
111
- if (trial.response_ends_trial) {
112
- end_trial();
113
- }
114
- };
115
- // start the response listener
116
- if (trial.choices != "NO_KEYS") {
117
- var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({
118
- callback_function: after_response,
119
- valid_responses: trial.choices,
120
- rt_method: "performance",
121
- persist: false,
122
- allow_held_key: false,
123
- });
124
- }
125
- // hide stimulus if stimulus_duration is set
126
- if (trial.stimulus_duration !== null) {
127
- this.jsPsych.pluginAPI.setTimeout(() => {
128
- display_element.querySelector("#jspsych-html-keyboard-response-stimulus").style.visibility = "hidden";
129
- }, trial.stimulus_duration);
130
- }
131
- // end trial if trial_duration is set
132
- if (trial.trial_duration !== null) {
133
- this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);
134
- }
136
+ if (trial.stimulus_duration !== null) {
137
+ this.jsPsych.pluginAPI.setTimeout(() => {
138
+ display_element.querySelector(
139
+ "#jspsych-html-keyboard-response-stimulus"
140
+ ).style.visibility = "hidden";
141
+ }, trial.stimulus_duration);
135
142
  }
136
- simulate(trial, simulation_mode, simulation_options, load_callback) {
137
- if (simulation_mode == "data-only") {
138
- load_callback();
139
- this.simulate_data_only(trial, simulation_options);
140
- }
141
- if (simulation_mode == "visual") {
142
- this.simulate_visual(trial, simulation_options, load_callback);
143
- }
143
+ if (trial.trial_duration !== null) {
144
+ this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);
144
145
  }
145
- create_simulation_data(trial, simulation_options) {
146
- const default_data = {
147
- stimulus: trial.stimulus,
148
- rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
149
- response: this.jsPsych.pluginAPI.getValidKey(trial.choices),
150
- };
151
- const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
152
- this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
153
- return data;
146
+ }
147
+ simulate(trial, simulation_mode, simulation_options, load_callback) {
148
+ if (simulation_mode == "data-only") {
149
+ load_callback();
150
+ this.simulate_data_only(trial, simulation_options);
154
151
  }
155
- simulate_data_only(trial, simulation_options) {
156
- const data = this.create_simulation_data(trial, simulation_options);
157
- this.jsPsych.finishTrial(data);
152
+ if (simulation_mode == "visual") {
153
+ this.simulate_visual(trial, simulation_options, load_callback);
158
154
  }
159
- simulate_visual(trial, simulation_options, load_callback) {
160
- const data = this.create_simulation_data(trial, simulation_options);
161
- const display_element = this.jsPsych.getDisplayElement();
162
- this.trial(display_element, trial);
163
- load_callback();
164
- if (data.rt !== null) {
165
- this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
166
- }
155
+ }
156
+ create_simulation_data(trial, simulation_options) {
157
+ const default_data = {
158
+ stimulus: trial.stimulus,
159
+ rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),
160
+ response: this.jsPsych.pluginAPI.getValidKey(trial.choices)
161
+ };
162
+ const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);
163
+ this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);
164
+ return data;
165
+ }
166
+ simulate_data_only(trial, simulation_options) {
167
+ const data = this.create_simulation_data(trial, simulation_options);
168
+ this.jsPsych.finishTrial(data);
169
+ }
170
+ simulate_visual(trial, simulation_options, load_callback) {
171
+ const data = this.create_simulation_data(trial, simulation_options);
172
+ const display_element = this.jsPsych.getDisplayElement();
173
+ this.trial(display_element, trial);
174
+ load_callback();
175
+ if (data.rt !== null) {
176
+ this.jsPsych.pluginAPI.pressKey(data.response, data.rt);
167
177
  }
178
+ }
168
179
  }
169
- HtmlKeyboardResponsePlugin.info = info;
170
180
 
171
181
  return HtmlKeyboardResponsePlugin;
172
182
 
173
183
  })(jsPsychModule);
174
- //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-html-keyboard-response@1.1.3/dist/index.browser.js.map
184
+ //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-html-keyboard-response@2.1.0/dist/index.browser.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.browser.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n parameters: {\n /**\n * The HTML string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /**\n * Array containing the key(s) the subject is allowed to press to respond to the stimulus.\n */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /**\n * Any content here will be displayed below the stimulus.\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /**\n * How long to show the stimulus.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /**\n * How long to show trial before it ends.\n */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /**\n * If true, trial will end when subject makes a response.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **html-keyboard-response**\n *\n * jsPsych plugin for displaying a stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html = '<div id=\"jspsych-html-keyboard-response-stimulus\">' + trial.stimulus + \"</div>\";\n\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-html-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-html-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n}\n\nexport default HtmlKeyboardResponsePlugin;\n"],"names":["ParameterType"],"mappings":";;;EAEA,MAAM,IAAI,GAAU;EAClB,IAAA,IAAI,EAAE,wBAAwB;EAC9B,IAAA,UAAU,EAAE;EACV;;EAEG;EACH,QAAA,QAAQ,EAAE;cACR,IAAI,EAAEA,qBAAa,CAAC,WAAW;EAC/B,YAAA,WAAW,EAAE,UAAU;EACvB,YAAA,OAAO,EAAE,SAAS;EACnB,SAAA;EACD;;EAEG;EACH,QAAA,OAAO,EAAE;cACP,IAAI,EAAEA,qBAAa,CAAC,IAAI;EACxB,YAAA,WAAW,EAAE,SAAS;EACtB,YAAA,OAAO,EAAE,UAAU;EACpB,SAAA;EACD;;EAEG;EACH,QAAA,MAAM,EAAE;cACN,IAAI,EAAEA,qBAAa,CAAC,WAAW;EAC/B,YAAA,WAAW,EAAE,QAAQ;EACrB,YAAA,OAAO,EAAE,IAAI;EACd,SAAA;EACD;;EAEG;EACH,QAAA,iBAAiB,EAAE;cACjB,IAAI,EAAEA,qBAAa,CAAC,GAAG;EACvB,YAAA,WAAW,EAAE,mBAAmB;EAChC,YAAA,OAAO,EAAE,IAAI;EACd,SAAA;EACD;;EAEG;EACH,QAAA,cAAc,EAAE;cACd,IAAI,EAAEA,qBAAa,CAAC,GAAG;EACvB,YAAA,WAAW,EAAE,gBAAgB;EAC7B,YAAA,OAAO,EAAE,IAAI;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,GAAG,IAAI,CAAC,OAAO,CAAC,iBAAiB,EAAE,CAAC;EAEzD,QAAA,IAAI,CAAC,KAAK,CAAC,eAAe,EAAE,KAAK,CAAC,CAAC;EACnC,QAAA,aAAa,EAAE,CAAC;EAEhB,QAAA,IAAI,IAAI,CAAC,EAAE,KAAK,IAAI,EAAE;EACpB,YAAA,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;EACzD,SAAA;OACF;;EAtIM,0BAAI,CAAA,IAAA,GAAG,IAAI;;;;;;;;"}
1
+ {"version":3,"file":"index.browser.js","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-html-keyboard-response\",\n \"version\": \"2.1.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-html-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/html-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n version: version,\n parameters: {\n /**\n * The string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n default: undefined,\n },\n /**\n * This array contains the key(s) that the participant is allowed to press in order to respond\n * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}\n * and\n * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}\n * for more examples. Any key presses that are not listed in the\n * array will be ignored. The default value of `\"ALL_KEYS\"` means that all keys will be accepted as valid responses.\n * Specifying `\"NO_KEYS\"` will mean that no responses are allowed.\n */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**\n * This string can contain HTML markup. Any content here will be displayed below the stimulus.\n * The intention is that it can be used to provide a reminder about the action the participant\n * is supposed to take (e.g., which key to press).\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /**\n * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus\n * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will\n * remain visible until the trial ends.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * How long to wait for the participant to make a response before ending the trial in milliseconds.\n * If the participant fails to make a response before this timer is reached, the participant's response\n * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,\n * then the trial will wait for a response indefinitely.\n */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * If true, then the trial will end whenever the participant makes a response (assuming they make their\n * response before the cutoff specified by the trial_duration parameter). If false, then the trial will\n * continue until the value for trial_duration is reached. You can set this parameter to false to force\n * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n },\n data: {\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n /** The HTML content that was displayed on the screen. */\n stimulus: {\n type: ParameterType.STRING,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays HTML content and records responses generated with the keyboard.\n * The stimulus can be displayed until a response is given, or for a pre-determined amount of time.\n * The trial can be ended automatically if the participant has failed to respond within a fixed length of time.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html = '<div id=\"jspsych-html-keyboard-response-stimulus\">' + trial.stimulus + \"</div>\";\n\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-html-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-html-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n}\n\nexport default HtmlKeyboardResponsePlugin;\n"],"names":[],"mappings":";;;EAEE,IAAW,OAAA,GAAA,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ICiFA,SAAA,EAAA;EAAA;;KAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,2 +1,2 @@
1
- var jsPsychHtmlKeyboardResponse=function(e){"use strict";function t(e,t){for(var s=0;s<t.length;s++){var i=t[s];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,(r=i.key,a=void 0,"symbol"==typeof(a=function(e,t){if("object"!=typeof e||null===e)return e;var s=e[Symbol.toPrimitive];if(void 0!==s){var i=s.call(e,t||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(r,"string"))?a:String(a)),i)}var r,a}var s={name:"html-keyboard-response",parameters:{stimulus:{type:e.ParameterType.HTML_STRING,pretty_name:"Stimulus",default:void 0},choices:{type:e.ParameterType.KEYS,pretty_name:"Choices",default:"ALL_KEYS"},prompt:{type:e.ParameterType.HTML_STRING,pretty_name:"Prompt",default:null},stimulus_duration:{type:e.ParameterType.INT,pretty_name:"Stimulus duration",default:null},trial_duration:{type:e.ParameterType.INT,pretty_name:"Trial duration",default:null},response_ends_trial:{type:e.ParameterType.BOOL,pretty_name:"Response ends trial",default:!0}}},i=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.jsPsych=t}var s,i,r;return s=e,i=[{key:"trial",value:function(e,t){var s=this,i='<div id="jspsych-html-keyboard-response-stimulus">'+t.stimulus+"</div>";null!==t.prompt&&(i+=t.prompt),e.innerHTML=i;var r={rt:null,key:null},a=function(){s.jsPsych.pluginAPI.clearAllTimeouts(),void 0!==n&&s.jsPsych.pluginAPI.cancelKeyboardResponse(n);var i={rt:r.rt,stimulus:t.stimulus,response:r.key};e.innerHTML="",s.jsPsych.finishTrial(i)};if("NO_KEYS"!=t.choices)var n=this.jsPsych.pluginAPI.getKeyboardResponse({callback_function:function(s){e.querySelector("#jspsych-html-keyboard-response-stimulus").className+=" responded",null==r.key&&(r=s),t.response_ends_trial&&a()},valid_responses:t.choices,rt_method:"performance",persist:!1,allow_held_key:!1});null!==t.stimulus_duration&&this.jsPsych.pluginAPI.setTimeout((function(){e.querySelector("#jspsych-html-keyboard-response-stimulus").style.visibility="hidden"}),t.stimulus_duration),null!==t.trial_duration&&this.jsPsych.pluginAPI.setTimeout(a,t.trial_duration)}},{key:"simulate",value:function(e,t,s,i){"data-only"==t&&(i(),this.simulate_data_only(e,s)),"visual"==t&&this.simulate_visual(e,s,i)}},{key:"create_simulation_data",value:function(e,t){var s={stimulus:e.stimulus,rt:this.jsPsych.randomization.sampleExGaussian(500,50,1/150,!0),response:this.jsPsych.pluginAPI.getValidKey(e.choices)},i=this.jsPsych.pluginAPI.mergeSimulationData(s,t);return this.jsPsych.pluginAPI.ensureSimulationDataConsistency(e,i),i}},{key:"simulate_data_only",value:function(e,t){var s=this.create_simulation_data(e,t);this.jsPsych.finishTrial(s)}},{key:"simulate_visual",value:function(e,t,s){var i=this.create_simulation_data(e,t),r=this.jsPsych.getDisplayElement();this.trial(r,e),s(),null!==i.rt&&this.jsPsych.pluginAPI.pressKey(i.response,i.rt)}}],i&&t(s.prototype,i),r&&t(s,r),Object.defineProperty(s,"prototype",{writable:!1}),e}();return i.info=s,i}(jsPsychModule);
2
- //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-html-keyboard-response@1.1.3/dist/index.browser.min.js.map
1
+ var jsPsychHtmlKeyboardResponse=function(i){"use strict";var u="2.1.0";const p={name:"html-keyboard-response",version:u,parameters:{stimulus:{type:i.ParameterType.HTML_STRING,default:void 0},choices:{type:i.ParameterType.KEYS,default:"ALL_KEYS"},prompt:{type:i.ParameterType.HTML_STRING,default:null},stimulus_duration:{type:i.ParameterType.INT,default:null},trial_duration:{type:i.ParameterType.INT,default:null},response_ends_trial:{type:i.ParameterType.BOOL,default:!0}},data:{response:{type:i.ParameterType.STRING},rt:{type:i.ParameterType.INT},stimulus:{type:i.ParameterType.STRING}},citations:{apa:"de Leeuw, J. R., Gilbert, R. A., & Luchterhandt, B. (2023). jsPsych: Enabling an Open-Source Collaborative Ecosystem of Behavioral Experiments. Journal of Open Source Software, 8(85), 5351. https://doi.org/10.21105/joss.05351 ",bibtex:'@article{Leeuw2023jsPsych, author = {de Leeuw, Joshua R. and Gilbert, Rebecca A. and Luchterhandt, Bj{\\" o}rn}, journal = {Journal of Open Source Software}, doi = {10.21105/joss.05351}, issn = {2475-9066}, number = {85}, year = {2023}, month = {may 11}, pages = {5351}, publisher = {Open Journals}, title = {jsPsych: Enabling an {Open}-{Source} {Collaborative} {Ecosystem} of {Behavioral} {Experiments}}, url = {https://joss.theoj.org/papers/10.21105/joss.05351}, volume = {8}, } '}};class n{constructor(t){this.jsPsych=t}trial(t,e){var a='<div id="jspsych-html-keyboard-response-stimulus">'+e.stimulus+"</div>";e.prompt!==null&&(a+=e.prompt),t.innerHTML=a;var s={rt:null,key:null};const r=()=>{typeof o!="undefined"&&this.jsPsych.pluginAPI.cancelKeyboardResponse(o);var l={rt:s.rt,stimulus:e.stimulus,response:s.key};this.jsPsych.finishTrial(l)};var m=l=>{t.querySelector("#jspsych-html-keyboard-response-stimulus").className+=" responded",s.key==null&&(s=l),e.response_ends_trial&&r()};if(e.choices!="NO_KEYS")var o=this.jsPsych.pluginAPI.getKeyboardResponse({callback_function:m,valid_responses:e.choices,rt_method:"performance",persist:!1,allow_held_key:!1});e.stimulus_duration!==null&&this.jsPsych.pluginAPI.setTimeout(()=>{t.querySelector("#jspsych-html-keyboard-response-stimulus").style.visibility="hidden"},e.stimulus_duration),e.trial_duration!==null&&this.jsPsych.pluginAPI.setTimeout(r,e.trial_duration)}simulate(t,e,a,s){e=="data-only"&&(s(),this.simulate_data_only(t,a)),e=="visual"&&this.simulate_visual(t,a,s)}create_simulation_data(t,e){const a={stimulus:t.stimulus,rt:this.jsPsych.randomization.sampleExGaussian(500,50,.006666666666666667,!0),response:this.jsPsych.pluginAPI.getValidKey(t.choices)},s=this.jsPsych.pluginAPI.mergeSimulationData(a,e);return this.jsPsych.pluginAPI.ensureSimulationDataConsistency(t,s),s}simulate_data_only(t,e){const a=this.create_simulation_data(t,e);this.jsPsych.finishTrial(a)}simulate_visual(t,e,a){const s=this.create_simulation_data(t,e),r=this.jsPsych.getDisplayElement();this.trial(r,t),a(),s.rt!==null&&this.jsPsych.pluginAPI.pressKey(s.response,s.rt)}}return n.info=p,n}(jsPsychModule);
2
+ //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-html-keyboard-response@2.1.0/dist/index.browser.min.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.browser.min.js","sources":["../src/index.ts"],"sourcesContent":["import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n parameters: {\n /**\n * The HTML string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Stimulus\",\n default: undefined,\n },\n /**\n * Array containing the key(s) the subject is allowed to press to respond to the stimulus.\n */\n choices: {\n type: ParameterType.KEYS,\n pretty_name: \"Choices\",\n default: \"ALL_KEYS\",\n },\n /**\n * Any content here will be displayed below the stimulus.\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n pretty_name: \"Prompt\",\n default: null,\n },\n /**\n * How long to show the stimulus.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n pretty_name: \"Stimulus duration\",\n default: null,\n },\n /**\n * How long to show trial before it ends.\n */\n trial_duration: {\n type: ParameterType.INT,\n pretty_name: \"Trial duration\",\n default: null,\n },\n /**\n * If true, trial will end when subject makes a response.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n pretty_name: \"Response ends trial\",\n default: true,\n },\n },\n};\n\ntype Info = typeof info;\n\n/**\n * **html-keyboard-response**\n *\n * jsPsych plugin for displaying a stimulus and getting a keyboard response\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/plugins/jspsych-html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html = '<div id=\"jspsych-html-keyboard-response-stimulus\">' + trial.stimulus + \"</div>\";\n\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill any remaining setTimeout handlers\n this.jsPsych.pluginAPI.clearAllTimeouts();\n\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // clear the display\n display_element.innerHTML = \"\";\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-html-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-html-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n}\n\nexport default HtmlKeyboardResponsePlugin;\n"],"names":["info","name","parameters","stimulus","type","ParameterType","HTML_STRING","pretty_name","default","undefined","choices","KEYS","prompt","stimulus_duration","INT","trial_duration","response_ends_trial","BOOL","HtmlKeyboardResponsePlugin","jsPsych","_classCallCheck","this","key","value","display_element","trial","_this","new_html","innerHTML","response","rt","end_trial","pluginAPI","clearAllTimeouts","keyboardListener","cancelKeyboardResponse","trial_data","finishTrial","getKeyboardResponse","callback_function","querySelector","className","valid_responses","rt_method","persist","allow_held_key","setTimeout","style","visibility","simulation_mode","simulation_options","load_callback","simulate_data_only","simulate_visual","default_data","randomization","sampleExGaussian","getValidKey","data","mergeSimulationData","ensureSimulationDataConsistency","create_simulation_data","getDisplayElement","pressKey"],"mappings":"wiBAEA,IAAMA,EAAc,CAClBC,KAAM,yBACNC,WAAY,CAIVC,SAAU,CACRC,KAAMC,EAAaA,cAACC,YACpBC,YAAa,WACbC,aAASC,GAKXC,QAAS,CACPN,KAAMC,EAAaA,cAACM,KACpBJ,YAAa,UACbC,QAAS,YAKXI,OAAQ,CACNR,KAAMC,EAAaA,cAACC,YACpBC,YAAa,SACbC,QAAS,MAKXK,kBAAmB,CACjBT,KAAMC,EAAaA,cAACS,IACpBP,YAAa,oBACbC,QAAS,MAKXO,eAAgB,CACdX,KAAMC,EAAaA,cAACS,IACpBP,YAAa,iBACbC,QAAS,MAKXQ,oBAAqB,CACnBZ,KAAMC,EAAaA,cAACY,KACpBV,YAAa,sBACbC,SAAS,KAeTU,EAA0B,WAG9B,SAAAA,EAAoBC,gGAAgBC,MAAAF,GAAhBG,KAAOF,QAAPA,CAAmB,WAoItC,SApIuCD,IAAA,CAAA,CAAAI,IAAA,QAAAC,MAExC,SAAMC,EAA8BC,GAAsB,IAAAC,EAAAL,KACpDM,EAAW,qDAAuDF,EAAMtB,SAAW,SAGlE,OAAjBsB,EAAMb,SACRe,GAAYF,EAAMb,QAIpBY,EAAgBI,UAAYD,EAG5B,IAAIE,EAAW,CACbC,GAAI,KACJR,IAAK,MAIDS,EAAY,WAEhBL,EAAKP,QAAQa,UAAUC,wBAGS,IAArBC,GACTR,EAAKP,QAAQa,UAAUG,uBAAuBD,GAIhD,IAAIE,EAAa,CACfN,GAAID,EAASC,GACb3B,SAAUsB,EAAMtB,SAChB0B,SAAUA,EAASP,KAIrBE,EAAgBI,UAAY,GAG5BF,EAAKP,QAAQkB,YAAYD,IAqB3B,GAAqB,WAAjBX,EAAMf,QACR,I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,EAAkBH,KAAKF,QAAQ2C,oBAErCzC,KAAKI,MAAMD,EAAiBC,GAC5B0B,IAEgB,OAAZO,EAAK5B,IACPT,KAAKF,QAAQa,UAAU+B,SAASL,EAAK7B,SAAU6B,EAAK5B,GAExD,qFAACZ,CAAA,CAvI6B,UACvBA,EAAIlB,KAAGA"}
1
+ {"version":3,"file":"index.browser.min.js","sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@jspsych/plugin-html-keyboard-response\",\n \"version\": \"2.1.0\",\n \"description\": \"jsPsych plugin for displaying a stimulus and getting a keyboard response\",\n \"type\": \"module\",\n \"main\": \"dist/index.cjs\",\n \"exports\": {\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"typings\": \"dist/index.d.ts\",\n \"unpkg\": \"dist/index.browser.min.js\",\n \"files\": [\n \"src\",\n \"dist\"\n ],\n \"source\": \"src/index.ts\",\n \"scripts\": {\n \"test\": \"jest\",\n \"test:watch\": \"npm test -- --watch\",\n \"tsc\": \"tsc\",\n \"build\": \"rollup --config\",\n \"build:watch\": \"npm run build -- --watch\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/jspsych/jsPsych.git\",\n \"directory\": \"packages/plugin-html-keyboard-response\"\n },\n \"author\": \"Josh de Leeuw\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/jspsych/jsPsych/issues\"\n },\n \"homepage\": \"https://www.jspsych.org/latest/plugins/html-keyboard-response\",\n \"peerDependencies\": {\n \"jspsych\": \">=7.1.0\"\n },\n \"devDependencies\": {\n \"@jspsych/config\": \"^3.2.0\",\n \"@jspsych/test-utils\": \"^1.2.0\"\n }\n}\n","import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from \"jspsych\";\n\nimport { version } from \"../package.json\";\n\nconst info = <const>{\n name: \"html-keyboard-response\",\n version: version,\n parameters: {\n /**\n * The string to be displayed.\n */\n stimulus: {\n type: ParameterType.HTML_STRING,\n default: undefined,\n },\n /**\n * This array contains the key(s) that the participant is allowed to press in order to respond\n * to the stimulus. Keys should be specified as characters (e.g., `'a'`, `'q'`, `' '`, `'Enter'`, `'ArrowDown'`) - see\n * {@link https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values this page}\n * and\n * {@link https://www.freecodecamp.org/news/javascript-keycode-list-keypress-event-key-codes/ this page (event.key column)}\n * for more examples. Any key presses that are not listed in the\n * array will be ignored. The default value of `\"ALL_KEYS\"` means that all keys will be accepted as valid responses.\n * Specifying `\"NO_KEYS\"` will mean that no responses are allowed.\n */\n choices: {\n type: ParameterType.KEYS,\n default: \"ALL_KEYS\",\n },\n /**\n * This string can contain HTML markup. Any content here will be displayed below the stimulus.\n * The intention is that it can be used to provide a reminder about the action the participant\n * is supposed to take (e.g., which key to press).\n */\n prompt: {\n type: ParameterType.HTML_STRING,\n default: null,\n },\n /**\n * How long to display the stimulus in milliseconds. The visibility CSS property of the stimulus\n * will be set to `hidden` after this time has elapsed. If this is null, then the stimulus will\n * remain visible until the trial ends.\n */\n stimulus_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * How long to wait for the participant to make a response before ending the trial in milliseconds.\n * If the participant fails to make a response before this timer is reached, the participant's response\n * will be recorded as null for the trial and the trial will end. If the value of this parameter is null,\n * then the trial will wait for a response indefinitely.\n */\n trial_duration: {\n type: ParameterType.INT,\n default: null,\n },\n /**\n * If true, then the trial will end whenever the participant makes a response (assuming they make their\n * response before the cutoff specified by the trial_duration parameter). If false, then the trial will\n * continue until the value for trial_duration is reached. You can set this parameter to false to force\n * the participant to view a stimulus for a fixed amount of time, even if they respond before the time is complete.\n */\n response_ends_trial: {\n type: ParameterType.BOOL,\n default: true,\n },\n },\n data: {\n /** Indicates which key the participant pressed. */\n response: {\n type: ParameterType.STRING,\n },\n /** The response time in milliseconds for the participant to make a response. The time is measured from when the stimulus first appears on the screen until the participant's response. */\n rt: {\n type: ParameterType.INT,\n },\n /** The HTML content that was displayed on the screen. */\n stimulus: {\n type: ParameterType.STRING,\n },\n },\n // prettier-ignore\n citations: '__CITATIONS__',\n};\n\ntype Info = typeof info;\n\n/**\n * This plugin displays HTML content and records responses generated with the keyboard.\n * The stimulus can be displayed until a response is given, or for a pre-determined amount of time.\n * The trial can be ended automatically if the participant has failed to respond within a fixed length of time.\n *\n * @author Josh de Leeuw\n * @see {@link https://www.jspsych.org/latest/plugins/html-keyboard-response/ html-keyboard-response plugin documentation on jspsych.org}\n */\nclass HtmlKeyboardResponsePlugin implements JsPsychPlugin<Info> {\n static info = info;\n constructor(private jsPsych: JsPsych) {}\n\n trial(display_element: HTMLElement, trial: TrialType<Info>) {\n var new_html = '<div id=\"jspsych-html-keyboard-response-stimulus\">' + trial.stimulus + \"</div>\";\n\n // add prompt\n if (trial.prompt !== null) {\n new_html += trial.prompt;\n }\n\n // draw\n display_element.innerHTML = new_html;\n\n // store response\n var response = {\n rt: null,\n key: null,\n };\n\n // function to end trial when it is time\n const end_trial = () => {\n // kill keyboard listeners\n if (typeof keyboardListener !== \"undefined\") {\n this.jsPsych.pluginAPI.cancelKeyboardResponse(keyboardListener);\n }\n\n // gather the data to store for the trial\n var trial_data = {\n rt: response.rt,\n stimulus: trial.stimulus,\n response: response.key,\n };\n\n // move on to the next trial\n this.jsPsych.finishTrial(trial_data);\n };\n\n // function to handle responses by the subject\n var after_response = (info) => {\n // after a valid response, the stimulus will have the CSS class 'responded'\n // which can be used to provide visual feedback that a response was recorded\n display_element.querySelector(\"#jspsych-html-keyboard-response-stimulus\").className +=\n \" responded\";\n\n // only record the first response\n if (response.key == null) {\n response = info;\n }\n\n if (trial.response_ends_trial) {\n end_trial();\n }\n };\n\n // start the response listener\n if (trial.choices != \"NO_KEYS\") {\n var keyboardListener = this.jsPsych.pluginAPI.getKeyboardResponse({\n callback_function: after_response,\n valid_responses: trial.choices,\n rt_method: \"performance\",\n persist: false,\n allow_held_key: false,\n });\n }\n\n // hide stimulus if stimulus_duration is set\n if (trial.stimulus_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(() => {\n display_element.querySelector<HTMLElement>(\n \"#jspsych-html-keyboard-response-stimulus\"\n ).style.visibility = \"hidden\";\n }, trial.stimulus_duration);\n }\n\n // end trial if trial_duration is set\n if (trial.trial_duration !== null) {\n this.jsPsych.pluginAPI.setTimeout(end_trial, trial.trial_duration);\n }\n }\n\n simulate(\n trial: TrialType<Info>,\n simulation_mode,\n simulation_options: any,\n load_callback: () => void\n ) {\n if (simulation_mode == \"data-only\") {\n load_callback();\n this.simulate_data_only(trial, simulation_options);\n }\n if (simulation_mode == \"visual\") {\n this.simulate_visual(trial, simulation_options, load_callback);\n }\n }\n\n private create_simulation_data(trial: TrialType<Info>, simulation_options) {\n const default_data = {\n stimulus: trial.stimulus,\n rt: this.jsPsych.randomization.sampleExGaussian(500, 50, 1 / 150, true),\n response: this.jsPsych.pluginAPI.getValidKey(trial.choices),\n };\n\n const data = this.jsPsych.pluginAPI.mergeSimulationData(default_data, simulation_options);\n\n this.jsPsych.pluginAPI.ensureSimulationDataConsistency(trial, data);\n\n return data;\n }\n\n private simulate_data_only(trial: TrialType<Info>, simulation_options) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n this.jsPsych.finishTrial(data);\n }\n\n private simulate_visual(trial: TrialType<Info>, simulation_options, load_callback: () => void) {\n const data = this.create_simulation_data(trial, simulation_options);\n\n const display_element = this.jsPsych.getDisplayElement();\n\n this.trial(display_element, trial);\n load_callback();\n\n if (data.rt !== null) {\n this.jsPsych.pluginAPI.pressKey(data.response, data.rt);\n }\n }\n}\n\nexport default HtmlKeyboardResponsePlugin;\n"],"names":["version"],"mappings":"yDAEEA,IAAAA,EAAW,8gBCiFA,UAAA,iuBAAe"}