@khanacademy/perseus 42.0.0 → 42.0.2

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
@@ -58,6 +58,7 @@ export { isItemRenderableByVersion } from "./renderability";
58
58
  export { violatingWidgets } from "./a11y";
59
59
  export { interactiveSizes } from "./styles/constants";
60
60
  export { displaySigFigs } from "./sigfigs";
61
+ export { conversionRequired, convertDeprecatedWidgets, } from "./util/deprecated-widgets/modernize-widgets-utils";
61
62
  export { PerseusI18nContextProvider, PerseusI18nContext, usePerseusI18n, } from "./components/i18n-context";
62
63
  /**
63
64
  * Util
package/dist/index.js CHANGED
@@ -18875,6 +18875,210 @@ class QuestionParagraph extends React__namespace.Component {
18875
18875
  }
18876
18876
  }
18877
18877
 
18878
+ // Methods to convert input-number widgets to numeric-input widgets
18879
+
18880
+ const inputNumberToNumericInput = json => {
18881
+ // First we need to create a map of the old input-number keys to the new numeric-input keys
18882
+ // so that we can ensure we update the content and widgets accordingly
18883
+ const renameMap = getInputNumberRenameMap(json);
18884
+
18885
+ // Then we can use this to update the JSON
18886
+ return convertInputNumberJson(json, renameMap);
18887
+ };
18888
+
18889
+ // We need to be able to run this code recursively in order to convert nested input-number widgets
18890
+ const convertInputNumberJson = (json, renameMap) => {
18891
+ const updatedContent = convertDeprecatedWidgetsInContent(json, renameMap);
18892
+ const updatedWidgets = convertInputNumberWidgetOptions(json, renameMap);
18893
+ const modernizedJson = {
18894
+ ...json,
18895
+ content: updatedContent,
18896
+ widgets: updatedWidgets
18897
+ };
18898
+ return modernizedJson;
18899
+ };
18900
+
18901
+ // Convert the input-number json in the widgets of a PerseusRenderer object
18902
+ const convertInputNumberWidgetOptions = (json, renameMap) => {
18903
+ const widgets = {
18904
+ ...json.widgets
18905
+ };
18906
+ // The question.widgets is a dictionary map of widgets, so we need to loop through in order to convert input-number to numeric-input
18907
+ // which can exist as both the key or as a value on widget.type.
18908
+ for (const key of Object.keys(widgets)) {
18909
+ // Loop through the keys of the widgets dictionary
18910
+ if (widgets[key].options.widgets) {
18911
+ widgets[key].options = {
18912
+ ...inputNumberToNumericInput(widgets[key].options)
18913
+ };
18914
+ }
18915
+ // Check if the widget is an input-number
18916
+ if (widgets[key].type === "input-number") {
18917
+ // If the answerType is not number or percent, we need to provide
18918
+ // the answer form for the numeric-input widget
18919
+ const provideAnswerForm = widgets[key].options.answerType !== "number" && widgets[key].options.answerType !== "percent";
18920
+ // We need to determine the mathFormat for the numeric-input widget
18921
+ const mathFormat = widgets[key].options.answerType === "rational" ? "proper" // input-number uses "rational" for proper fractions
18922
+ : widgets[key].options.answerType; // Otherwise, we can use the answerType directly
18923
+
18924
+ // We need to update the answers prop to match the numeric-input widget format
18925
+ const answers = [{
18926
+ value: widgets[key].options.value,
18927
+ simplify: widgets[key].options.simplify,
18928
+ // Input Number doesn't have a strict prop, so we default to false
18929
+ strict: false,
18930
+ // We only want to set maxError if the inexact prop is true
18931
+ maxError: widgets[key].options.inexact ? widgets[key].options.maxError : 0,
18932
+ status: "correct",
18933
+ // Input-number only allows correct answers
18934
+ message: ""
18935
+ }];
18936
+
18937
+ // Add the required answerForms if provided/applicable
18938
+ if (provideAnswerForm) {
18939
+ answers[0].answerForms = [mathFormat];
18940
+ }
18941
+
18942
+ // Update the options prop to match the numeric-input widget format
18943
+ const upgradedWidgetOptions = {
18944
+ answers,
18945
+ size: widgets[key].options.size,
18946
+ coefficient: false,
18947
+ // input-number doesn't have a coefficient prop
18948
+ labelText: "",
18949
+ // input-number doesn't have a labelText prop
18950
+ static: false,
18951
+ // static is always false for numeric-input
18952
+ rightAlign: widgets[key].options.rightAlign || false
18953
+ };
18954
+ const upgradedWidget = {
18955
+ options: upgradedWidgetOptions,
18956
+ type: "numeric-input"
18957
+ };
18958
+ const newWidgetName = renameMap[key];
18959
+ // Create the new key entry
18960
+ widgets[newWidgetName] = upgradedWidget;
18961
+
18962
+ // We need to delete the old widget key
18963
+ delete widgets[key];
18964
+ }
18965
+ }
18966
+ return widgets;
18967
+ };
18968
+
18969
+ // Convert the deprecated widget refs in the content string
18970
+ // of a PerseusRenderer object to their renamed equivalents
18971
+ const convertDeprecatedWidgetsInContent = (json, renameMap) => {
18972
+ return Object.keys(renameMap).reduce((newContent, oldKey) => {
18973
+ const newKey = renameMap[oldKey];
18974
+ return newKey ? newContent.replace(oldKey, newKey) : newContent;
18975
+ }, json.content);
18976
+ };
18977
+
18978
+ // Create a map of the old input-number keys to the new numeric-input keys
18979
+ const getInputNumberRenameMap = json => {
18980
+ const numericRegex = /(?<=\[\[\u2603 )(numeric-input \d+)(?=\]\])/g;
18981
+ const inputNumberRegex = /(?<=\[\[\u2603 )(input-number \d+)(?=\]\])/g;
18982
+
18983
+ // Get all the content strings within the json, which might be nested within widgets
18984
+ const allContentStrings = json.content;
18985
+
18986
+ // Loop through the content strings to get all the input-number widgets
18987
+ const renameMap = {};
18988
+ const inputNumberMatches = [...(allContentStrings.match(inputNumberRegex) || [])];
18989
+
18990
+ // We want to count any pre-existing numeric-input widgets
18991
+ // so that we can start the new ids at the next number
18992
+ const numericMatches = [...(allContentStrings.match(numericRegex) || [])];
18993
+ let currentNumericCount = numericMatches.reduce((count, match) => {
18994
+ const id = parseInt(match.split(" ")[1], 10);
18995
+ return id >= count ? id + 1 : count; // We want to start at the next highest number
18996
+ }, 1);
18997
+
18998
+ // Now that we have all the necessary information, we can create the renameMap
18999
+ for (const match of inputNumberMatches) {
19000
+ const oldKey = match;
19001
+ const newKey = `numeric-input ${currentNumericCount}`;
19002
+ renameMap[oldKey] = newKey;
19003
+ currentNumericCount++;
19004
+ }
19005
+ return renameMap;
19006
+ };
19007
+
19008
+ // Convert the user input data keys from input-number to numeric-input
19009
+ const convertUserInputNumberData = (userInputMap, renameMap) => {
19010
+ const updatedUserInputMap = {
19011
+ ...userInputMap
19012
+ };
19013
+ for (const key of Object.keys(userInputMap)) {
19014
+ if (key.includes("input-number")) {
19015
+ const updatedKey = renameMap[key];
19016
+ updatedUserInputMap[updatedKey] = userInputMap[key];
19017
+ delete updatedUserInputMap[key];
19018
+ }
19019
+ }
19020
+ return updatedUserInputMap;
19021
+ };
19022
+
19023
+ // Used to convert InputNumber widgets to NumericInput widgets for scoring
19024
+ const convertInputNumberForScoring = (rubric, userInputMap) => {
19025
+ // First we need to create a map of the old input-number keys to the new numeric-input keys
19026
+ // so that we can ensure we update the content, widgets, AND userInput accordingly
19027
+ const renameMap = getInputNumberRenameMap(rubric);
19028
+ const convertedRubric = convertInputNumberJson(rubric, renameMap);
19029
+ const convertedUserData = convertUserInputNumberData(userInputMap, renameMap);
19030
+ return {
19031
+ convertedRubric,
19032
+ convertedUserData
19033
+ };
19034
+ };
19035
+
19036
+ const widgetRegExes = [/input-number \d+/]; // We can add more regexes here in the future
19037
+
19038
+ // This utils in this file are used to modernize our Perseus JSON structure,
19039
+ // so that we can convert deprecated widgets to their modern equivalents when
19040
+ // content creators use the Editor Page to update content containing these widgets.
19041
+ // Currently, we're only converting input-number to numeric-input,
19042
+ // but we can add more conversions here in the future.
19043
+
19044
+ // Modernize the json content of a PerseusRenderer object
19045
+ // by converting deprecated widgets to their modern equivalents
19046
+ const convertDeprecatedWidgets = json => {
19047
+ // Currently we're only converting input-number to numeric-input,
19048
+ // But we can add more conversions here in the future
19049
+ return inputNumberToNumericInput(json);
19050
+ };
19051
+ const conversionRequired = json => {
19052
+ // If there's no content, then there's no conversion required
19053
+ if (!json.content) {
19054
+ return false;
19055
+ }
19056
+ // Check the content string for any top-level input-number widgets
19057
+ if (widgetRegExes.some(regex => regex.test(json.content))) {
19058
+ return true;
19059
+ }
19060
+
19061
+ // If there's no deprecated widget in the top-level, then check for any nested widgets
19062
+ for (const key of Object.keys(json.widgets)) {
19063
+ if (json.widgets[key].options.widgets) {
19064
+ const nestedJson = json.widgets[key].options;
19065
+ if (conversionRequired(nestedJson)) {
19066
+ return true;
19067
+ }
19068
+ }
19069
+ }
19070
+ return false;
19071
+ };
19072
+
19073
+ /**
19074
+ * Convert the deprecated widgets in the rubric and user data to their
19075
+ * modern equivalents for scoring. These need to be updated together
19076
+ * in order to ensure that the widgetKeys match between the rubric and user data.
19077
+ */
19078
+ const convertDeprecatedWidgetsForScoring = (rubric, userInputMap) => {
19079
+ return convertInputNumberForScoring(rubric, userInputMap);
19080
+ };
19081
+
18878
19082
  // These are all the current widgets that require user-input and are supported for automatic scoring
18879
19083
  const QUESTION_WIDGETS = ["numeric-input", "input-number", "radio", "interactive-graph", "interactive-number-line", "categorizer", "plotter", "orderer", "protractor", "matcher", "sorter"];
18880
19084
 
@@ -19474,11 +19678,25 @@ function scorePerseusItem(perseusRenderData, userInputMap,
19474
19678
  // TODO(LEMS-2461,LEMS-2391): these probably
19475
19679
  // need to be removed before we move this to the server
19476
19680
  strings, locale) {
19681
+ let convertedRenderData = perseusRenderData;
19682
+ let convertedUserInputMap = userInputMap;
19683
+
19684
+ // Check if the PerseusRenderer object contains any deprecated widgets that need to be converted
19685
+ const mustConvertData = conversionRequired(perseusRenderData);
19686
+ if (mustConvertData) {
19687
+ const {
19688
+ convertedRubric,
19689
+ convertedUserData
19690
+ } = convertDeprecatedWidgetsForScoring(perseusRenderData, userInputMap);
19691
+ convertedRenderData = convertedRubric;
19692
+ convertedUserInputMap = convertedUserData;
19693
+ }
19694
+
19477
19695
  // There seems to be a chance that PerseusRenderer.widgets might include
19478
19696
  // widget data for widgets that are not in PerseusRenderer.content,
19479
19697
  // so this checks that the widgets are being used before scoring them
19480
- const usedWidgetIds = getWidgetIdsFromContent(perseusRenderData.content);
19481
- const scores = scoreWidgetsFunctional(perseusRenderData.widgets, usedWidgetIds, userInputMap, strings, locale);
19698
+ const usedWidgetIds = getWidgetIdsFromContent(convertedRenderData.content);
19699
+ const scores = scoreWidgetsFunctional(convertedRenderData.widgets, usedWidgetIds, convertedUserInputMap, strings, locale);
19482
19700
  return Util.flattenScores(scores);
19483
19701
  }
19484
19702
  function scoreWidgetsFunctional(widgets,
@@ -45203,7 +45421,7 @@ const init = function () {
45203
45421
 
45204
45422
  // This file is processed by a Rollup plugin (replace) to inject the production
45205
45423
  const libName = "@khanacademy/perseus";
45206
- const libVersion = "42.0.0";
45424
+ const libVersion = "42.0.2";
45207
45425
  perseusCore.addLibraryVersionToPerseusDebug(libName, libVersion);
45208
45426
 
45209
45427
  const apiVersion = {
@@ -49843,6 +50061,8 @@ exports.buildMapper = buildMapper;
49843
50061
  exports.components = components;
49844
50062
  exports.containerSizeClass = containerSizeClass;
49845
50063
  exports.contentHasWidgetType = contentHasWidgetType;
50064
+ exports.conversionRequired = conversionRequired;
50065
+ exports.convertDeprecatedWidgets = convertDeprecatedWidgets;
49846
50066
  exports.convertWidgetNameToEnum = convertWidgetNameToEnum;
49847
50067
  exports.displaySigFigs = displaySigFigs;
49848
50068
  exports.generateTestCategorizerWidget = generateTestCategorizerWidget;