@khanacademy/perseus-core 18.0.0 → 18.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.
package/dist/es/index.js CHANGED
@@ -180,9 +180,51 @@ const parsePerseusAnswerArea=pipeParsers(defaulted(object({}),()=>({}))).then(co
180
180
 
181
181
  const parsePerseusItem$1=object({question:parsePerseusRenderer,hints:defaulted(array(parseHint),()=>[]),answerArea:parsePerseusAnswerArea});
182
182
 
183
- function parsePerseusItem(json){if(isRealJSONParse(JSON.parse)){return JSON.parse(json)}throw new Error("Something went wrong.")}function parseAndMigratePerseusItem(data){throwErrorIfCheatingDetected();const object=typeof data==="string"?JSON.parse(data):data;const result=parse(object,parsePerseusItem$1);if(isFailure(result)){return failure({message:result.detail,invalidObject:object})}return result}function parseAndMigratePerseusArticle(data){throwErrorIfCheatingDetected();const object=typeof data==="string"?JSON.parse(data):data;const result=parse(object,parsePerseusArticle);if(isFailure(result)){return failure({message:result.detail,invalidObject:object})}return result}function throwErrorIfCheatingDetected(){if(!isRealJSONParse(JSON.parse)){throw new Error("Something went wrong.")}}
183
+ const parseCategorizerUserInput=object({values:array(number)});
184
184
 
185
- const libName="@khanacademy/perseus-core";const libVersion="18.0.0";addLibraryVersionToPerseusDebug(libName,libVersion);
185
+ const parseCSProgramUserInput=object({status:enumeration("correct","incorrect","incomplete"),message:nullable(string)});
186
+
187
+ const parseDropdownUserInput=object({value:number});
188
+
189
+ const parseExpressionUserInput=string;
190
+
191
+ const parseFreeResponseUserInput=object({currentValue:string});
192
+
193
+ const coord=pair(number,number);const coordPair=pair(coord,coord);const parseAbsoluteValueBranch=object({type:constant("absolute_value"),coords:nullable(coordPair)});const parseExponentialBranch=object({type:constant("exponential"),asymptote:coordPair,coords:nullable(coordPair)});const parseLinearBranch=object({type:constant("linear"),coords:nullable(coordPair)});const parseLogarithmBranch=object({type:constant("logarithm"),asymptote:coordPair,coords:nullable(coordPair)});const parseQuadraticBranch=object({type:constant("quadratic"),coords:nullable(coordPair)});const parseSinusoidBranch=object({type:constant("sinusoid"),coords:nullable(coordPair)});const parseTangentBranch=object({type:constant("tangent"),coords:nullable(coordPair)});const parseGrapherUserInput=discriminatedUnionOn("type").withBranch("absolute_value",parseAbsoluteValueBranch).withBranch("exponential",parseExponentialBranch).withBranch("linear",parseLinearBranch).withBranch("logarithm",parseLogarithmBranch).withBranch("quadratic",parseQuadraticBranch).withBranch("sinusoid",parseSinusoidBranch).withBranch("tangent",parseTangentBranch).parser;
194
+
195
+ const parseGroupUserInput=(rawVal,ctx)=>parseUserInputMap(rawVal,ctx);
196
+
197
+ const parseIFrameUserInput=object({status:enumeration("correct","incorrect","incomplete"),message:nullable(string)});
198
+
199
+ const parseInputNumberUserInput=object({currentValue:string});
200
+
201
+ const parseInteractiveGraphUserInput=parsePerseusGraphType;
202
+
203
+ const parseLabelImageUserInput=object({markers:array(object({selected:optional(array(string)),label:string}))});
204
+
205
+ const parseMatcherUserInput=object({left:array(string),right:array(string)});
206
+
207
+ const parseMatrixUserInput=object({answers:array(array(number))});
208
+
209
+ const parseNumberLineUserInput=object({numLinePosition:number,rel:enumeration("eq","lt","gt","le","ge"),numDivisions:number});
210
+
211
+ const parseNumericInputUserInput=object({currentValue:string});
212
+
213
+ const parseOrdererUserInput=object({current:array(string)});
214
+
215
+ const parsePlotterUserInput=array(number);
216
+
217
+ const parseRadioUserInput=object({choicesSelected:array(boolean)});
218
+
219
+ const parseSorterUserInput=object({options:array(string),changed:boolean});
220
+
221
+ const parseTableUserInput=array(array(string));
222
+
223
+ const parseUserInputMap=(rawValue,ctx)=>{if(!isPlainObject(rawValue)){return ctx.failure("UserInputMap",rawValue)}const userInputMap={};for(const key of Object.keys(rawValue)){const entryResult=parseUserInputMapEntry([key,rawValue[key]],userInputMap,ctx.forSubtree(key));if(isFailure(entryResult)){return entryResult}}return ctx.success(userInputMap)};const parseUserInputMapEntry=([id,userInput],userInputMap,ctx)=>{const idComponentsResult=parseWidgetIdComponents(id.split(" "),ctx.forSubtree("(widget ID)"));if(isFailure(idComponentsResult)){return idComponentsResult}const[type,n]=idComponentsResult.value;function parseAndAssign(key,parse){const userInputResult=parse(userInput,ctx);if(isFailure(userInputResult)){return userInputResult}userInputMap[key]=userInputResult.value;return ctx.success(undefined)}switch(type){case "categorizer":return parseAndAssign(`categorizer ${n}`,parseCategorizerUserInput);case "cs-program":return parseAndAssign(`cs-program ${n}`,parseCSProgramUserInput);case "dropdown":return parseAndAssign(`dropdown ${n}`,parseDropdownUserInput);case "expression":return parseAndAssign(`expression ${n}`,parseExpressionUserInput);case "free-response":return parseAndAssign(`free-response ${n}`,parseFreeResponseUserInput);case "grapher":return parseAndAssign(`grapher ${n}`,parseGrapherUserInput);case "group":return parseAndAssign(`group ${n}`,parseGroupUserInput);case "iframe":return parseAndAssign(`iframe ${n}`,parseIFrameUserInput);case "input-number":return parseAndAssign(`input-number ${n}`,parseInputNumberUserInput);case "interactive-graph":return parseAndAssign(`interactive-graph ${n}`,parseInteractiveGraphUserInput);case "label-image":return parseAndAssign(`label-image ${n}`,parseLabelImageUserInput);case "matcher":return parseAndAssign(`matcher ${n}`,parseMatcherUserInput);case "matrix":return parseAndAssign(`matrix ${n}`,parseMatrixUserInput);case "number-line":return parseAndAssign(`number-line ${n}`,parseNumberLineUserInput);case "numeric-input":return parseAndAssign(`numeric-input ${n}`,parseNumericInputUserInput);case "orderer":return parseAndAssign(`orderer ${n}`,parseOrdererUserInput);case "plotter":return parseAndAssign(`plotter ${n}`,parsePlotterUserInput);case "radio":return parseAndAssign(`radio ${n}`,parseRadioUserInput);case "sorter":return parseAndAssign(`sorter ${n}`,parseSorterUserInput);case "table":return parseAndAssign(`table ${n}`,parseTableUserInput);default:return parseAndAssign(`${type} ${n}`,any)}};
224
+
225
+ function parsePerseusItem(json){if(isRealJSONParse(JSON.parse)){return JSON.parse(json)}throw new Error("Something went wrong.")}function parseAndMigratePerseusItem(data){throwErrorIfCheatingDetected();const object=typeof data==="string"?JSON.parse(data):data;const result=parse(object,parsePerseusItem$1);if(isFailure(result)){return failure({message:result.detail,invalidObject:object})}return result}function parseAndMigratePerseusArticle(data){throwErrorIfCheatingDetected();const object=typeof data==="string"?JSON.parse(data):data;const result=parse(object,parsePerseusArticle);if(isFailure(result)){return failure({message:result.detail,invalidObject:object})}return result}function parseAndMigrateUserInputMap(data){const object=typeof data==="string"?JSON.parse(data):data;const result=parse(object,parseUserInputMap);if(isFailure(result)){return failure({message:result.detail,invalidObject:object})}return result}function throwErrorIfCheatingDetected(){if(!isRealJSONParse(JSON.parse)){throw new Error("Something went wrong.")}}
226
+
227
+ const libName="@khanacademy/perseus-core";const libVersion="18.1.0";addLibraryVersionToPerseusDebug(libName,libVersion);
186
228
 
187
229
  const Errors=Object.freeze({Unknown:"Unknown",Internal:"Internal",InvalidInput:"InvalidInput",NotAllowed:"NotAllowed",TransientService:"TransientService",Service:"Service"});
188
230
 
@@ -224,6 +266,8 @@ const defaultWidgetOptions$n={graph:{labels:["x","y"],range:[[-10,10],[-10,10]],
224
266
 
225
267
  class Registry{throwIfUnregistered(){if(!this.anythingRegistered){throw new Error(`${this.name} accessed before initialization!`)}}has(key){this.throwIfUnregistered();return Object.prototype.hasOwnProperty.call(this.contents,key)}get(key){this.throwIfUnregistered();return this.contents[key]}keys(){this.throwIfUnregistered();return Object.keys(this.contents)}entries(){this.throwIfUnregistered();return Object.entries(this.contents)}set(key,value){this.anythingRegistered=true;this.contents[key]=value;}constructor(name="Registry"){this.contents={};this.anythingRegistered=false;this.name=name;}}
226
268
 
269
+ const deprecatedStandinWidgetLogic={name:"deprecated-standin",accessible:true};
270
+
227
271
  function getIFramePublicWidgetOptions(options){return options}
228
272
 
229
273
  const defaultWidgetOptions$m={url:"",settings:[{name:"",value:""}],width:"400",height:"400",allowFullScreen:false,allowTopNavigation:false};const iframeWidgetLogic={name:"iframe",defaultWidgetOptions: defaultWidgetOptions$m,getPublicWidgetOptions:getIFramePublicWidgetOptions,accessible:false};
@@ -300,7 +344,7 @@ const defaultRows=4;const defaultColumns=1;const answers=new Array(defaultRows).
300
344
 
301
345
  const defaultWidgetOptions$1={location:""};const videoWidgetLogic={name:"video",defaultWidgetOptions: defaultWidgetOptions$1,supportedAlignments:["block","float-left","float-right","full-width"],defaultAlignment:"block",accessible:true};
302
346
 
303
- const widgets=new Registry("Core widget registry");function registerWidget(type,logic){widgets.set(type,logic);}function isWidgetRegistered(type){const widgetLogic=widgets.get(type);return Boolean(widgetLogic)}function getCurrentVersion(type){const widgetLogic=widgets.get(type);return widgetLogic?.version||{major:0,minor:0}}const getPublicWidgetOptionsFunction=type=>{return widgets.get(type)?.getPublicWidgetOptions??(i=>i)};function getDefaultWidgetOptions(type){const widgetLogic=widgets.get(type);return widgetLogic?.defaultWidgetOptions||{}}function isAccessible(type,widgetOptions){const accessible=widgets.get(type)?.accessible;return typeof accessible==="function"?accessible(widgetOptions):!!accessible}const traverseChildWidgets$1=(widgetInfo,traverseRenderer)=>{if(!traverseRenderer){throw new PerseusError("traverseRenderer must be provided, but was not",Errors.Internal)}if(!widgetInfo||!widgetInfo.type||!widgets.get(widgetInfo.type)){return widgetInfo}const widgetExports=widgets.get(widgetInfo.type);const props=widgetInfo.options;if(widgetExports?.traverseChildWidgets!=null&&props!=null){const newProps=widgetExports.traverseChildWidgets(props,traverseRenderer);return {...widgetInfo,options:newProps}}return widgetInfo};const getSupportedAlignments=type=>{const widgetLogic=widgets.get(type);if(!widgetLogic?.supportedAlignments?.[0]){return ["default"]}return widgetLogic?.supportedAlignments};const getDefaultAlignment=type=>{const widgetLogic=widgets.get(type);if(!widgetLogic?.defaultAlignment){return "block"}return widgetLogic.defaultAlignment};function registerCoreWidgets(){const widgets=[categorizerWidgetLogic,csProgramWidgetLogic,definitionWidgetLogic,dropdownWidgetLogic,explanationWidgetLogic,expressionWidgetLogic,gradedGroupWidgetLogic,gradedGroupSetWidgetLogic,grapherWidgetLogic,groupWidgetLogic,iframeWidgetLogic,imageWidgetLogic,inputNumberWidgetLogic,interactionWidgetLogic,interactiveGraphWidgetLogic,labelImageWidgetLogic,matcherWidgetLogic,matrixWidgetLogic,measurerWidgetLogic,numberLineWidgetLogic,numericInputWidgetLogic,ordererWidgetLogic,passageWidgetLogic,passageRefWidgetLogic,passageRefTargetWidgetLogic,phetSimulationWidgetLogic,plotterWidgetLogic,pythonProgramWidgetLogic,radioWidgetLogic,sorterWidgetLogic,tableWidgetLogic,videoWidgetLogic];widgets.forEach(w=>{registerWidget(w.name,w);});}
347
+ const widgets=new Registry("Core widget registry");function registerWidget(type,logic){widgets.set(type,logic);}function isWidgetRegistered(type){const widgetLogic=widgets.get(type);return Boolean(widgetLogic)}function getCurrentVersion(type){const widgetLogic=widgets.get(type);return widgetLogic?.version||{major:0,minor:0}}const getPublicWidgetOptionsFunction=type=>{return widgets.get(type)?.getPublicWidgetOptions??(i=>i)};function getDefaultWidgetOptions(type){const widgetLogic=widgets.get(type);return widgetLogic?.defaultWidgetOptions||{}}function isAccessible(type,widgetOptions){const accessible=widgets.get(type)?.accessible;return typeof accessible==="function"?accessible(widgetOptions):!!accessible}const traverseChildWidgets$1=(widgetInfo,traverseRenderer)=>{if(!traverseRenderer){throw new PerseusError("traverseRenderer must be provided, but was not",Errors.Internal)}if(!widgetInfo||!widgetInfo.type||!widgets.get(widgetInfo.type)){return widgetInfo}const widgetExports=widgets.get(widgetInfo.type);const props=widgetInfo.options;if(widgetExports?.traverseChildWidgets!=null&&props!=null){const newProps=widgetExports.traverseChildWidgets(props,traverseRenderer);return {...widgetInfo,options:newProps}}return widgetInfo};const getSupportedAlignments=type=>{const widgetLogic=widgets.get(type);if(!widgetLogic?.supportedAlignments?.[0]){return ["default"]}return widgetLogic?.supportedAlignments};const getDefaultAlignment=type=>{const widgetLogic=widgets.get(type);if(!widgetLogic?.defaultAlignment){return "block"}return widgetLogic.defaultAlignment};function registerCoreWidgets(){const widgets=[categorizerWidgetLogic,csProgramWidgetLogic,definitionWidgetLogic,deprecatedStandinWidgetLogic,dropdownWidgetLogic,explanationWidgetLogic,expressionWidgetLogic,gradedGroupWidgetLogic,gradedGroupSetWidgetLogic,grapherWidgetLogic,groupWidgetLogic,iframeWidgetLogic,imageWidgetLogic,inputNumberWidgetLogic,interactionWidgetLogic,interactiveGraphWidgetLogic,labelImageWidgetLogic,matcherWidgetLogic,matrixWidgetLogic,measurerWidgetLogic,numberLineWidgetLogic,numericInputWidgetLogic,ordererWidgetLogic,passageWidgetLogic,passageRefWidgetLogic,passageRefTargetWidgetLogic,phetSimulationWidgetLogic,plotterWidgetLogic,pythonProgramWidgetLogic,radioWidgetLogic,sorterWidgetLogic,tableWidgetLogic,videoWidgetLogic];widgets.forEach(w=>{registerWidget(w.name,w);});}
304
348
 
305
349
  var coreWidgetRegistry = /*#__PURE__*/Object.freeze({
306
350
  __proto__: null,
@@ -336,5 +380,5 @@ function violatingWidgets(itemData){const widgetTypes=[];traverse(itemData.quest
336
380
 
337
381
  registerCoreWidgets();
338
382
 
339
- export { coreWidgetRegistry as CoreWidgetRegistry, Errors, grapherUtil as GrapherUtil, ItemExtras, PerseusError, PerseusExpressionAnswerFormConsidered, PerseusFeatureFlags, Registry, addWidget, applyDefaultsToWidget, applyDefaultsToWidgets, approximateDeepEqual, approximateEqual, categorizerWidgetLogic as categorizerLogic, csProgramWidgetLogic as csProgramLogic, deepClone, definitionWidgetLogic as definitionLogic, deriveExtraKeys, deriveNumCorrect, dropdownWidgetLogic as dropdownLogic, explanationWidgetLogic as explanationLogic, expressionWidgetLogic as expressionLogic, freeResponseWidgetLogic as freeResponseLogic, generateTestPerseusItem, generateTestPerseusRenderer, getBaseUrl, getCSProgramPublicWidgetOptions, getCategorizerPublicWidgetOptions, getDataUrl, getDecimalSeparator, getDefaultAnswerArea, getDropdownPublicWidgetOptions, getExpressionPublicWidgetOptions, getFreeResponsePublicWidgetOptions, getGrapherPublicWidgetOptions, getGroupPublicWidgetOptions, getIFramePublicWidgetOptions, getImageSizeModern, getInteractiveGraphPublicWidgetOptions, getLabelImagePublicWidgetOptions, getMatcherPublicWidgetOptions, getMatrixPublicWidgetOptions, getMatrixSize, getNumberLinePublicWidgetOptions, getNumericInputPublicWidgetOptions, getOrdererPublicWidgetOptions, getPlotterPublicWidgetOptions, getRadioPublicWidgetOptions, getRealImageUrl, getSorterPublicWidgetOptions, getSvgUrl, getTablePublicWidgetOptions, getWidgetIdsFromContent, getWidgetIdsFromContentByType, gradedGroupWidgetLogic as gradedGroupLogic, gradedGroupSetWidgetLogic as gradedGroupSetLogic, grapherWidgetLogic as grapherLogic, groupWidgetLogic as groupLogic, iframeWidgetLogic as iframeLogic, imageWidgetLogic as imageLogic, inputNumberWidgetLogic as inputNumberLogic, interactionWidgetLogic as interactionLogic, interactiveGraphWidgetLogic as interactiveGraphLogic, isFailure, isItemAccessible, isLabeledSVG, isSuccess, itemHasHints, itemHasRationales, labelImageWidgetLogic as labelImageLogic, libVersion, lockedFigureColorNames, lockedFigureColors, lockedFigureFillStyles, mapObject, matcherWidgetLogic as matcherLogic, matrixWidgetLogic as matrixLogic, measurerWidgetLogic as measurerLogic, numberLineWidgetLogic as numberLineLogic, numericInputWidgetLogic as numericInputLogic, ordererWidgetLogic as ordererLogic, parseAndMigratePerseusArticle, parseAndMigratePerseusItem, parsePerseusItem, passageWidgetLogic as passageLogic, passageRefWidgetLogic as passageRefLogic, passageRefTargetWidgetLogic as passageRefTargetLogic, phetSimulationWidgetLogic as phetSimulationLogic, plotterWidgetLogic as plotterLogic, plotterPlotTypes, pluck, pythonProgramWidgetLogic as pythonProgramLogic, radioWidgetLogic as radioLogic, random, seededRNG, shuffle, shuffleMatcher, shuffleSorter, sorterWidgetLogic as sorterLogic, splitPerseusItem, splitPerseusItemJSON, tableWidgetLogic as tableLogic, traverse, usesNumCorrect, videoWidgetLogic as videoLogic, violatingWidgets };
383
+ export { coreWidgetRegistry as CoreWidgetRegistry, Errors, grapherUtil as GrapherUtil, ItemExtras, PerseusError, PerseusExpressionAnswerFormConsidered, PerseusFeatureFlags, Registry, addWidget, applyDefaultsToWidget, applyDefaultsToWidgets, approximateDeepEqual, approximateEqual, categorizerWidgetLogic as categorizerLogic, csProgramWidgetLogic as csProgramLogic, deepClone, definitionWidgetLogic as definitionLogic, deriveExtraKeys, deriveNumCorrect, dropdownWidgetLogic as dropdownLogic, explanationWidgetLogic as explanationLogic, expressionWidgetLogic as expressionLogic, freeResponseWidgetLogic as freeResponseLogic, generateTestPerseusItem, generateTestPerseusRenderer, getBaseUrl, getCSProgramPublicWidgetOptions, getCategorizerPublicWidgetOptions, getDataUrl, getDecimalSeparator, getDefaultAnswerArea, getDropdownPublicWidgetOptions, getExpressionPublicWidgetOptions, getFreeResponsePublicWidgetOptions, getGrapherPublicWidgetOptions, getGroupPublicWidgetOptions, getIFramePublicWidgetOptions, getImageSizeModern, getInteractiveGraphPublicWidgetOptions, getLabelImagePublicWidgetOptions, getMatcherPublicWidgetOptions, getMatrixPublicWidgetOptions, getMatrixSize, getNumberLinePublicWidgetOptions, getNumericInputPublicWidgetOptions, getOrdererPublicWidgetOptions, getPlotterPublicWidgetOptions, getRadioPublicWidgetOptions, getRealImageUrl, getSorterPublicWidgetOptions, getSvgUrl, getTablePublicWidgetOptions, getWidgetIdsFromContent, getWidgetIdsFromContentByType, gradedGroupWidgetLogic as gradedGroupLogic, gradedGroupSetWidgetLogic as gradedGroupSetLogic, grapherWidgetLogic as grapherLogic, groupWidgetLogic as groupLogic, iframeWidgetLogic as iframeLogic, imageWidgetLogic as imageLogic, inputNumberWidgetLogic as inputNumberLogic, interactionWidgetLogic as interactionLogic, interactiveGraphWidgetLogic as interactiveGraphLogic, isFailure, isItemAccessible, isLabeledSVG, isSuccess, itemHasHints, itemHasRationales, labelImageWidgetLogic as labelImageLogic, libVersion, lockedFigureColorNames, lockedFigureColors, lockedFigureFillStyles, mapObject, matcherWidgetLogic as matcherLogic, matrixWidgetLogic as matrixLogic, measurerWidgetLogic as measurerLogic, numberLineWidgetLogic as numberLineLogic, numericInputWidgetLogic as numericInputLogic, ordererWidgetLogic as ordererLogic, parseAndMigratePerseusArticle, parseAndMigratePerseusItem, parseAndMigrateUserInputMap, parsePerseusItem, passageWidgetLogic as passageLogic, passageRefWidgetLogic as passageRefLogic, passageRefTargetWidgetLogic as passageRefTargetLogic, phetSimulationWidgetLogic as phetSimulationLogic, plotterWidgetLogic as plotterLogic, plotterPlotTypes, pluck, pythonProgramWidgetLogic as pythonProgramLogic, radioWidgetLogic as radioLogic, random, seededRNG, shuffle, shuffleMatcher, shuffleSorter, sorterWidgetLogic as sorterLogic, splitPerseusItem, splitPerseusItemJSON, tableWidgetLogic as tableLogic, traverse, usesNumCorrect, videoWidgetLogic as videoLogic, violatingWidgets };
340
384
  //# sourceMappingURL=index.js.map