Skip to content

Commit

Permalink
Revert "Ensuring UserInput and Rubric widget keys match (#1884)"
Browse files Browse the repository at this point in the history
This reverts commit b4cf444.
  • Loading branch information
SonicScrewdriver committed Nov 22, 2024
1 parent 9a00cd7 commit 21e494f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 40 deletions.
18 changes: 8 additions & 10 deletions packages/perseus/src/renderer-util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Util from "./util";
import {
conversionRequired,
convertDeprecatedWidgetsForScoring,
convertDeprecatedWidgets,
convertUserInputData,
} from "./util/deprecated-widgets/modernize-widgets-utils";
import {getWidgetIdsFromContent} from "./widget-type-utils";
import {getWidgetScorer} from "./widgets";
Expand Down Expand Up @@ -65,17 +66,14 @@ export function scorePerseusItem(
strings: PerseusStrings,
locale: string,
): PerseusScore {
let convertedRenderData = perseusRenderData;
let convertedUserInputMap = userInputMap;

// Check if the PerseusRenderer object contains any deprecated widgets that need to be converted
const mustConvertData = conversionRequired(perseusRenderData);
if (mustConvertData) {
const {convertedRubric, convertedUserData} =
convertDeprecatedWidgetsForScoring(perseusRenderData, userInputMap);
convertedRenderData = convertedRubric;
convertedUserInputMap = convertedUserData;
}
const convertedRenderData = mustConvertData
? convertDeprecatedWidgets(perseusRenderData) // Convert deprecated widgets to their modern equivalents
: perseusRenderData;
const convertedUserInputMap = mustConvertData
? convertUserInputData(userInputMap) // Convert deprecated user input data keys to their modern equivalents
: userInputMap;

// There seems to be a chance that PerseusRenderer.widgets might include
// widget data for widgets that are not in PerseusRenderer.content,
Expand Down
21 changes: 2 additions & 19 deletions packages/perseus/src/util/deprecated-widgets/input-number.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Methods to convert input-number widgets to numeric-input widgets

import type {
NumericInputWidget,
PerseusRenderer,
Expand Down Expand Up @@ -161,34 +162,16 @@ export const getInputNumberRenameMap = (
// Convert the user input data keys from input-number to numeric-input
export const convertUserInputNumberData = (
userInputMap: UserInputMap,
renameMap: WidgetRenameMap,
): UserInputMap => {
const updatedUserInputMap = {...userInputMap};

for (const key of Object.keys(userInputMap)) {
if (key.includes("input-number")) {
const updatedKey = renameMap[key];
const updatedKey = key.replace("input-number", "numeric-input");
updatedUserInputMap[updatedKey] = userInputMap[key];
delete updatedUserInputMap[key];
}
}

return updatedUserInputMap;
};

// Used to convert InputNumber widgets to NumericInput widgets for scoring
export const convertInputNumberForScoring = (
rubric: PerseusRenderer,
userInputMap: UserInputMap,
): {convertedRubric: PerseusRenderer; convertedUserData: UserInputMap} => {
// First we need to create a map of the old input-number keys to the new numeric-input keys
// so that we can ensure we update the content, widgets, AND userInput accordingly
const renameMap = getInputNumberRenameMap(rubric);
const convertedRubric = convertInputNumberJson(rubric, renameMap);
const convertedUserData = convertUserInputNumberData(
userInputMap,
renameMap,
);

return {convertedRubric, convertedUserData};
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
inputNumberToNumericInput,
convertInputNumberForScoring,
convertUserInputNumberData,
} from "./input-number";

import type {PerseusRenderer, UserInputMap} from "@khanacademy/perseus";
Expand All @@ -12,7 +12,6 @@ const widgetRegExes = [/input-number \d+/]; // We can add more regexes here in t
// content creators use the Editor Page to update content containing these widgets.
// Currently, we're only converting input-number to numeric-input,
// but we can add more conversions here in the future.

// Modernize the json content of a PerseusRenderer object
// by converting deprecated widgets to their modern equivalents
export const convertDeprecatedWidgets = (
Expand Down Expand Up @@ -46,14 +45,11 @@ export const conversionRequired = (json: PerseusRenderer): boolean => {
return false;
};

/**
* Convert the deprecated widgets in the rubric and user data to their
* modern equivalents for scoring. These need to be updated together
* in order to ensure that the widgetKeys match between the rubric and user data.
*/
export const convertDeprecatedWidgetsForScoring = (
rubric: PerseusRenderer,
// Convert the user input data keys for deprecated widgets to their modern equivalents
export const convertUserInputData = (
userInputMap: UserInputMap,
): {convertedRubric: PerseusRenderer; convertedUserData: UserInputMap} => {
return convertInputNumberForScoring(rubric, userInputMap);
): UserInputMap => {
// Currently we're only converting input-number to numeric-input,
// But we can add more conversions here in the future
return convertUserInputNumberData(userInputMap);
};

0 comments on commit 21e494f

Please sign in to comment.