@khanacademy/perseus-core
Advanced tools
Comparing version 0.0.0-PR2147-20250123194001 to 0.0.0-PR2147-20250123210940
@@ -37,2 +37,45 @@ /** | ||
/** | ||
* A utility type that constructs a widget map from a "registry interface". | ||
* The keys of the registry should be the widget type (aka, "categorizer" or | ||
* "radio", etc) and the value should be the option type stored in the value | ||
* of the map. | ||
* | ||
* You can think of this as a type that generates another type. We use | ||
* "registry interfaces" as a way to keep a set of widget types to their data | ||
* type in several places in Perseus. This type then allows us to generate a | ||
* map type that maps a widget id to its data type and keep strong typing by | ||
* widget id. | ||
* | ||
* For example, given a fictitious registry such as this: | ||
* | ||
* ``` | ||
* interface DummyRegistry { | ||
* categorizer: { categories: ReadonlyArray<string> }; | ||
* dropdown: { choices: ReadonlyArray<string> }: | ||
* } | ||
* ``` | ||
* | ||
* If we create a DummyMap using this helper: | ||
* | ||
* ``` | ||
* type DummyMap = MakeWidgetMap<DummyRegistry>; | ||
* ``` | ||
* | ||
* We'll get a map that looks like this: | ||
* | ||
* ``` | ||
* type DummyMap = { | ||
* `categorizer ${number}`: { categories: ReadonlyArray<string> }; | ||
* `dropdown ${number}`: { choices: ReadonlyArray<string> }; | ||
* } | ||
* ``` | ||
* | ||
* We use interfaces for the registries so that they can be extended in cases | ||
* where the consuming app brings along their own widgets. Interfaces in | ||
* TypeScript are always open (ie. you can extend them) whereas types aren't. | ||
*/ | ||
export type MakeWidgetMap<TRegistry> = { | ||
[Property in keyof TRegistry as `${Property & string} ${number}`]: TRegistry[Property]; | ||
}; | ||
/** | ||
* Our core set of Perseus widgets. | ||
@@ -58,3 +101,3 @@ * | ||
* ```typescript | ||
* declare module "@khanacademy/perseus" { | ||
* declare module "@khanacademy/perseus-core" { | ||
* interface PerseusWidgetTypes { | ||
@@ -101,3 +144,2 @@ * // A new widget | ||
measurer: MeasurerWidget; | ||
"mock-widget": MockWidget; | ||
"molecule-renderer": MoleculeRendererWidget; | ||
@@ -134,6 +176,15 @@ "number-line": NumberLineWidget; | ||
*/ | ||
export type PerseusWidgetsMap = { | ||
[Property in keyof PerseusWidgetTypes as `${Property} ${number}`]: PerseusWidgetTypes[Property]; | ||
}; | ||
export type PerseusWidgetsMap = MakeWidgetMap<PerseusWidgetTypes>; | ||
/** | ||
* PerseusWidget is a union of all the different types of widget options that | ||
* Perseus knows about. | ||
* | ||
* Thanks to it being based on PerseusWidgetTypes interface, this union is | ||
* automatically extended to include widgets used in tests without those widget | ||
* option types seeping into our production types. | ||
* | ||
* @see MockWidget for an example | ||
*/ | ||
export type PerseusWidget = PerseusWidgetTypes[keyof PerseusWidgetTypes]; | ||
/** | ||
* A "PerseusItem" is a classic Perseus item. It is rendered by the | ||
@@ -235,3 +286,2 @@ * `ServerItemRenderer` and the layout is pre-set. | ||
export type MeasurerWidget = WidgetOptions<'measurer', PerseusMeasurerWidgetOptions>; | ||
export type MockWidget = WidgetOptions<'mock-widget', MockWidgetOptions>; | ||
export type NumberLineWidget = WidgetOptions<'number-line', PerseusNumberLineWidgetOptions>; | ||
@@ -253,3 +303,2 @@ export type NumericInputWidget = WidgetOptions<'numeric-input', PerseusNumericInputWidgetOptions>; | ||
export type DeprecatedStandinWidget = WidgetOptions<'deprecated-standin', object>; | ||
export type PerseusWidget = CategorizerWidget | CSProgramWidget | DefinitionWidget | DropdownWidget | ExplanationWidget | ExpressionWidget | GradedGroupSetWidget | GradedGroupWidget | GrapherWidget | GroupWidget | IFrameWidget | ImageWidget | InputNumberWidget | InteractionWidget | InteractiveGraphWidget | LabelImageWidget | MatcherWidget | MatrixWidget | MeasurerWidget | MockWidget | MoleculeRendererWidget | NumberLineWidget | NumericInputWidget | OrdererWidget | PassageRefWidget | PassageWidget | PhetSimulationWidget | PlotterWidget | PythonProgramWidget | RadioWidget | RefTargetWidget | SorterWidget | TableWidget | VideoWidget | DeprecatedStandinWidget; | ||
/** | ||
@@ -964,6 +1013,2 @@ * A background image applied to various widgets. | ||
}; | ||
export type MockWidgetOptions = { | ||
static?: boolean; | ||
value: string; | ||
}; | ||
export type PerseusInputNumberWidgetOptions = { | ||
@@ -970,0 +1015,0 @@ answerType?: "number" | "decimal" | "integer" | "rational" | "improper" | "mixed" | "percent" | "pi"; |
@@ -23,1 +23,2 @@ export type { PerseusAnalyticsEvent, AnalyticsEventHandlerFn } from "./analytics"; | ||
export type * from "./widgets/logic-export.types"; | ||
export { default as getOrdererPublicWidgetOptions } from "./utils/orderer-util"; |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "0.0.0-PR2147-20250123194001", | ||
"version": "0.0.0-PR2147-20250123210940", | ||
"publishConfig": { | ||
@@ -9,0 +9,0 @@ "access": "public" |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
829234
27
7002