@lookit/record 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/trial.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import autoBind from "auto-bind";
2
2
  import { JsPsych, JsPsychExtension, JsPsychExtensionInfo } from "jspsych";
3
3
  import Recorder from "./recorder";
4
+ import { jsPsychPluginWithInfo } from "./types";
4
5
 
5
6
  /** This extension will allow reasearchers to record trials. */
6
7
  export default class TrialRecordExtension implements JsPsychExtension {
@@ -9,6 +10,7 @@ export default class TrialRecordExtension implements JsPsychExtension {
9
10
  };
10
11
 
11
12
  private recorder?: Recorder;
13
+ private pluginName: string | undefined;
12
14
 
13
15
  /**
14
16
  * Video recording extension.
@@ -32,7 +34,8 @@ export default class TrialRecordExtension implements JsPsychExtension {
32
34
 
33
35
  /** Ran when the trial has loaded. */
34
36
  public on_load() {
35
- this.recorder?.start("trial_video");
37
+ this.pluginName = this.getCurrentPluginName();
38
+ this.recorder?.start(false, `${this.pluginName}`);
36
39
  }
37
40
 
38
41
  /**
@@ -44,4 +47,15 @@ export default class TrialRecordExtension implements JsPsychExtension {
44
47
  this.recorder?.stop();
45
48
  return {};
46
49
  }
50
+
51
+ /**
52
+ * Gets the plugin name for the trial that is being extended. This is same as
53
+ * the "trial_type" value that is stored in the data for this trial.
54
+ *
55
+ * @returns Plugin name string from the plugin class's info.
56
+ */
57
+ private getCurrentPluginName() {
58
+ const current_plugin_class = this.jsPsych.getCurrentTrial().type;
59
+ return (current_plugin_class as jsPsychPluginWithInfo).info.name;
60
+ }
47
61
  }
package/src/types.ts CHANGED
@@ -1,3 +1,11 @@
1
+ import { JsPsychPlugin, PluginInfo } from "jspsych";
2
+ import { Class } from "type-fest";
3
+
4
+ export interface jsPsychPluginWithInfo
5
+ extends Class<JsPsychPlugin<PluginInfo>> {
6
+ info: PluginInfo;
7
+ }
8
+
1
9
  /**
2
10
  * A valid CSS height/width value, which can be a number, a string containing a
3
11
  * number with units, or 'auto'.
@@ -1,7 +1,7 @@
1
1
  import { clickTarget } from "@jspsych/test-utils";
2
2
  import Handlebars from "handlebars";
3
3
  import { initJsPsych, JsPsych } from "jspsych";
4
- import videoConfig from "../templates/video-config.hbs";
4
+ import videoConfig from "../hbs/video-config.hbs";
5
5
  import { NoStreamError } from "./errors";
6
6
  import chromeInitialPrompt from "./img/chrome_initialprompt.png";
7
7
  import chromeAlwaysAllow from "./img/chrome_step1_alwaysallow.png";
@@ -1,5 +1,6 @@
1
1
  import Handlebars from "handlebars";
2
2
  import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from "jspsych";
3
+ import video_config from "../hbs/video-config.hbs";
3
4
  import chromeInitialPrompt from "../img/chrome_initialprompt.png";
4
5
  import chromeAlwaysAllow from "../img/chrome_step1_alwaysallow.png";
5
6
  import chromePermissions from "../img/chrome_step1_permissions.png";
@@ -7,7 +8,6 @@ import firefoxInitialPrompt from "../img/firefox_initialprompt.png";
7
8
  import firefoxChooseDevice from "../img/firefox_prompt_choose_device.png";
8
9
  import firefoxDevicesBlocked from "../img/firefox_prompt_devices_blocked.png";
9
10
  import { version } from "../package.json";
10
- import video_config from "../templates/video-config.hbs";
11
11
  import { MicCheckError, NoStreamError } from "./errors";
12
12
  import Recorder from "./recorder";
13
13
  // import MicCheckProcessor from './mic_check'; // TO DO: fix or remove this. See: https://github.com/lookit/lookit-jspsych/issues/44
package/dist/utils.d.ts DELETED
@@ -1,21 +0,0 @@
1
- import { PluginInfo, TrialType } from "jspsych";
2
- /**
3
- * Pulled from EFP. Function to convert researcher's text to HTML.
4
- *
5
- * @param text - Text
6
- * @returns Formatted string
7
- */
8
- export declare const expFormat: (text?: string | string[]) => string;
9
- /**
10
- * Get a translation file based on selected language.
11
- *
12
- * @param lcl - Locale object with locale
13
- * @returns Translations from i18next
14
- */
15
- export declare const getTranslation: (lcl: Intl.Locale) => Record<string, string>;
16
- /**
17
- * Initialize both i18next and Handlebars.
18
- *
19
- * @param trial - Yup
20
- */
21
- export declare const initI18nAndTemplates: (trial: TrialType<PluginInfo>) => void;
package/src/utils.spec.ts DELETED
@@ -1,55 +0,0 @@
1
- import Yaml from "js-yaml";
2
- import en_us from "../i18n/en-us.yaml";
3
- import eu from "../i18n/eu.yaml";
4
- import fr from "../i18n/fr.yaml";
5
- import hu from "../i18n/hu.yaml";
6
- import it from "../i18n/it.yaml";
7
- import ja from "../i18n/ja.yaml";
8
- import nl from "../i18n/nl.yaml";
9
- import pt_br from "../i18n/pt-br.yaml";
10
- import pt from "../i18n/pt.yaml";
11
- import { TranslationNotFoundError } from "./errors";
12
- import { expFormat, getTranslation } from "./utils";
13
-
14
- test("expFormat convert written text to format well in HTML", () => {
15
- expect(expFormat("abcdefg")).toStrictEqual("abcdefg");
16
- expect(expFormat("AAABBBCCC")).toStrictEqual("AAABBBCCC");
17
- expect(expFormat("A normal sentence with multiple words.")).toStrictEqual(
18
- "A normal sentence with multiple words.",
19
- );
20
- expect(expFormat(["Array", "of", "strings"])).toStrictEqual(
21
- "Array<br><br>of<br><br>strings",
22
- );
23
- expect(expFormat("carriage return an newline\r\n")).toStrictEqual(
24
- "carriage return an newline<br>",
25
- );
26
- expect(expFormat("new line\n")).toStrictEqual("new line<br>");
27
- expect(expFormat("carriage return\r")).toStrictEqual("carriage return<br>");
28
- expect(expFormat("\tTabbed text")).toStrictEqual(
29
- "&nbsp;&nbsp;&nbsp;&nbsp;Tabbed text",
30
- );
31
- });
32
-
33
- test("Get translation file for specified locale", () => {
34
- const translations = {
35
- ja,
36
- pt,
37
- eu,
38
- fr,
39
- hu,
40
- it,
41
- nl,
42
- "en-us": en_us,
43
- "pt-br": pt_br,
44
- };
45
-
46
- for (const [k, v] of Object.entries<string>(translations)) {
47
- expect(getTranslation(new Intl.Locale(k))).toStrictEqual(Yaml.load(v));
48
- }
49
-
50
- expect(pt_br).not.toStrictEqual(pt);
51
-
52
- expect(() => getTranslation(new Intl.Locale("not-a2code"))).toThrow(
53
- TranslationNotFoundError,
54
- );
55
- });
package/src/utils.ts DELETED
@@ -1,119 +0,0 @@
1
- import Handlebars from "handlebars";
2
- import i18next from "i18next";
3
- import ICU from "i18next-icu";
4
- import Yaml from "js-yaml";
5
- import { PluginInfo, TrialType } from "jspsych";
6
- import en_us from "../i18n/en-us.yaml";
7
- import eu from "../i18n/eu.yaml";
8
- import fr from "../i18n/fr.yaml";
9
- import hu from "../i18n/hu.yaml";
10
- import it from "../i18n/it.yaml";
11
- import ja from "../i18n/ja.yaml";
12
- import nl from "../i18n/nl.yaml";
13
- import pt_br from "../i18n/pt-br.yaml";
14
- import pt from "../i18n/pt.yaml";
15
- import { TranslationNotFoundError } from "./errors";
16
-
17
- /**
18
- * Pulled from EFP. Function to convert researcher's text to HTML.
19
- *
20
- * @param text - Text
21
- * @returns Formatted string
22
- */
23
- export const expFormat = (text?: string | string[]) => {
24
- if (!text) {
25
- return "";
26
- }
27
-
28
- if (Array.isArray(text)) {
29
- text = text.join("\n\n");
30
- }
31
-
32
- return text
33
- .replace(/(\r\n|\n|\r)/gm, "<br>")
34
- .replace(/\t/gm, "&nbsp;&nbsp;&nbsp;&nbsp;");
35
- };
36
-
37
- /**
38
- * Get a translation file based on selected language.
39
- *
40
- * @param lcl - Locale object with locale
41
- * @returns Translations from i18next
42
- */
43
- export const getTranslation = (lcl: Intl.Locale) => {
44
- /**
45
- * Switch case to find language from a string. Will throw error is language
46
- * not found.
47
- *
48
- * @param baseName - Base name from locale (en-us)
49
- * @returns Language yaml file
50
- */
51
- const getYaml = (baseName: string) => {
52
- switch (baseName) {
53
- case "en-US":
54
- return en_us;
55
- case "eu":
56
- return eu;
57
- case "fr":
58
- return fr;
59
- case "hu":
60
- return hu;
61
- case "it":
62
- return it;
63
- case "ja":
64
- return ja;
65
- case "nl":
66
- return nl;
67
- case "pt-BR":
68
- return pt_br;
69
- case "pt":
70
- return pt;
71
- default:
72
- throw new TranslationNotFoundError(baseName);
73
- }
74
- };
75
-
76
- return Yaml.load(getYaml(lcl.baseName)) as Record<string, string>;
77
- };
78
-
79
- /**
80
- * Initialize i18next with parameters from trial.
81
- *
82
- * @param trial - Trial data including user supplied parameters.
83
- */
84
- const initI18next = (trial: TrialType<PluginInfo>) => {
85
- const debug = process.env.DEBUG === "true";
86
- const lcl = new Intl.Locale(trial.locale);
87
- const translation = getTranslation(lcl);
88
-
89
- i18next.use(ICU).init({
90
- lng: lcl.baseName,
91
- debug,
92
- resources: {
93
- [lcl.language]: {
94
- translation,
95
- },
96
- },
97
- });
98
- };
99
-
100
- /**
101
- * Initialize handlebars helpers. This could be done globally, but it does go
102
- * hand in hand with initializing i18n.
103
- */
104
- const initHandlebars = () => {
105
- Handlebars.registerHelper("t", (context, { hash }) =>
106
- i18next.t(context, hash),
107
- );
108
- Handlebars.registerHelper("exp-format", (context) => expFormat(context));
109
- };
110
-
111
- /**
112
- * Initialize both i18next and Handlebars.
113
- *
114
- * @param trial - Yup
115
- */
116
- export const initI18nAndTemplates = (trial: TrialType<PluginInfo>) => {
117
- initI18next(trial);
118
- initHandlebars();
119
- };