@khanacademy/perseus 20.7.0 → 20.8.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/dist/index.d.ts CHANGED
@@ -65,7 +65,7 @@ export { default as KhanColors } from "./util/colors";
65
65
  export { default as preprocessTex } from "./util/katex-preprocess";
66
66
  export { registerAllWidgetsForTesting } from "./util/register-all-widgets-for-testing";
67
67
  export * as SizingUtils from "./util/sizing-utils";
68
- export { getAnswersFromWidgets, injectWidgets, } from "./util/extract-perseus-data";
68
+ export { getAnswersFromWidgets, injectWidgets, getWidgetTypeFromWidgetKey, isWrongAnswerSupported, shouldHaveIndividualAnswer, isWidgetKeyInContent, getValidWidgetKeys, getCorrectAnswerForWidgetKey, getAnswerFromUserInput, } from "./util/extract-perseus-data";
69
69
  export { parsePerseusItem } from "./util/parse-perseus-json";
70
70
  export { generateTestPerseusItem, genericPerseusItemData, } from "./util/test-utils";
71
71
  export { convertWidgetNameToEnum, getWidgetTypeFromWidgetId, } from "./util/widget-enum-utils";
package/dist/index.js CHANGED
@@ -41894,7 +41894,7 @@ const init = function (options) {
41894
41894
 
41895
41895
  // This file is processed by a Rollup plugin (replace) to inject the production
41896
41896
  const libName = "@khanacademy/perseus";
41897
- const libVersion = "20.7.0";
41897
+ const libVersion = "20.8.0";
41898
41898
  perseusCore.addLibraryVersionToPerseusDebug(libName, libVersion);
41899
41899
 
41900
41900
  const apiVersion = {
@@ -44542,6 +44542,66 @@ function injectWidgets(content, widgets) {
44542
44542
  return context;
44543
44543
  }
44544
44544
 
44545
+ /* Widgets that have individual answers */
44546
+ const INDIVIDUAL_ANSWER_WIDGETS = ["interactive-graph", "categorizer", "grapher"];
44547
+
44548
+ /* Widgets that are supported for automatic scoring */
44549
+ const SUPPORTED_WIDGETS = ["radio", "numeric-input", "input-number", "expression", ...INDIVIDUAL_ANSWER_WIDGETS];
44550
+ const getWidgetTypeFromWidgetKey = widgetKey => {
44551
+ return widgetKey.split(" ")[0];
44552
+ };
44553
+
44554
+ /* Verify if the perseus item has supported widgets for automatic scoring */
44555
+ const isWrongAnswerSupported = widgetKeys => {
44556
+ return widgetKeys.length !== 0 && widgetKeys.every(widgetKey => SUPPORTED_WIDGETS.includes(getWidgetTypeFromWidgetKey(widgetKey)));
44557
+ };
44558
+
44559
+ /* Verify if the widget key has an individual answer for the coach report view */
44560
+ const shouldHaveIndividualAnswer = widgetKey => {
44561
+ return INDIVIDUAL_ANSWER_WIDGETS.includes(getWidgetTypeFromWidgetKey(widgetKey));
44562
+ };
44563
+
44564
+ /* Returns the answer userInput submission for currently supported widgets */
44565
+ // TODO (LEMS-1834): Fix userInput any types to be specific
44566
+ const getAnswerFromUserInput = (widgetType, userInput) => {
44567
+ switch (widgetType) {
44568
+ case "categorizer":
44569
+ return userInput.values;
44570
+ case "input-number":
44571
+ return userInput.currentValue;
44572
+ case "numeric-input":
44573
+ return userInput.currentValue;
44574
+ case "radio":
44575
+ return userInput.choicesSelected;
44576
+ }
44577
+ return userInput;
44578
+ };
44579
+
44580
+ /* Returns the correct answer for a given widget key and Perseus Item */
44581
+ // TODO (LEMS-1835): We should fix the resonse type from getWidget to be specific.
44582
+ // TODO (LEMS-1836): We should also consider adding the getOneCorrectAnswerFromRubric method to all widgets.
44583
+ const getCorrectAnswerForWidgetKey = (widgetKey, itemData) => {
44584
+ const rubric = itemData.question.widgets[widgetKey].options;
44585
+ const widgetType = getWidgetTypeFromWidgetKey(widgetKey);
44586
+ const widget = getWidget(widgetType);
44587
+
44588
+ // @ts-expect-error - TS2339 - Property 'getOneCorrectAnswerFromRubric' does not exist on type 'ComponentType<any>'.
44589
+ return widget?.getOneCorrectAnswerFromRubric?.(rubric);
44590
+ };
44591
+
44592
+ /* Verify if the widget key exists in the content string of the Perseus Item */
44593
+ const isWidgetKeyInContent = (perseusItem, widgetKey) => {
44594
+ return perseusItem.question.content.indexOf(widgetKey) !== -1;
44595
+ };
44596
+
44597
+ /* Return an array of all the widget keys that exist in the content string of a Perseus Item */
44598
+ const getValidWidgetKeys = perseusItem => {
44599
+ const {
44600
+ widgets
44601
+ } = perseusItem.question;
44602
+ return wonderStuffCore.keys(widgets).filter(key => isWidgetKeyInContent(perseusItem, key));
44603
+ };
44604
+
44545
44605
  /**
44546
44606
  * Helper to parse PerseusItem JSON
44547
44607
  * Why not just use JSON.parse? We want:
@@ -44873,9 +44933,13 @@ exports.generateTestPerseusItem = generateTestPerseusItem;
44873
44933
  exports.genericPerseusItemData = genericPerseusItemData;
44874
44934
  exports.getAllWidgetIds = getAllWidgetIds;
44875
44935
  exports.getAllWidgetTypes = getAllWidgetTypes;
44936
+ exports.getAnswerFromUserInput = getAnswerFromUserInput;
44876
44937
  exports.getAnswersFromWidgets = getAnswersFromWidgets;
44938
+ exports.getCorrectAnswerForWidgetKey = getCorrectAnswerForWidgetKey;
44877
44939
  exports.getQuestionWidgetIds = getQuestionWidgetIds;
44940
+ exports.getValidWidgetKeys = getValidWidgetKeys;
44878
44941
  exports.getWidgetTypeFromWidgetId = getWidgetTypeFromWidgetId;
44942
+ exports.getWidgetTypeFromWidgetKey = getWidgetTypeFromWidgetKey;
44879
44943
  exports.globalConstants = globalConstants;
44880
44944
  exports.globalStyles = globalStyles;
44881
44945
  exports.icons = iconPaths;
@@ -44883,6 +44947,8 @@ exports.init = init;
44883
44947
  exports.injectWidgets = injectWidgets;
44884
44948
  exports.interactiveSizes = interactiveSizes$1;
44885
44949
  exports.isItemRenderableByVersion = isItemRenderableByVersion;
44950
+ exports.isWidgetKeyInContent = isWidgetKeyInContent;
44951
+ exports.isWrongAnswerSupported = isWrongAnswerSupported;
44886
44952
  exports.itemDataVersion = itemDataVersion;
44887
44953
  exports.itemToTree = itemToTree;
44888
44954
  exports.itemVersion = ItemVersion;
@@ -44893,6 +44959,7 @@ exports.plotterPlotTypes = plotterPlotTypes;
44893
44959
  exports.preprocessTex = preprocessTex;
44894
44960
  exports.registerAllWidgetsForTesting = registerAllWidgetsForTesting;
44895
44961
  exports.shapes = shapes;
44962
+ exports.shouldHaveIndividualAnswer = shouldHaveIndividualAnswer;
44896
44963
  exports.traverse = traverse;
44897
44964
  exports.violatingWidgets = violatingWidgets;
44898
44965
  exports.widgets = allWidgets;