-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy pathdata-schema.ts
More file actions
2193 lines (2068 loc) · 73.8 KB
/
data-schema.ts
File metadata and controls
2193 lines (2068 loc) · 73.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable max-lines */
/**
* The Perseus "data schema" file.
*
* This file, and the types in it, represents the "data schema" that Perseus
* uses. The @khanacademy/perseus-editor package edits and produces objects
* that conform to the types in this file. Similarly, the top-level renderers
* in @khanacademy/perseus, consume objects that conform to these types.
*
* WARNING: This file should not import any types from elsewhere so that it is
* easy to reason about changes that alter the Perseus schema. This helps
* ensure that it is not changed accidentally when upgrading a dependant
* package or other part of Perseus code. Note that TypeScript does type
* checking via something called "structural typing". This means that as long
* as the shape of a type matches, the name it goes by doesn't matter. As a
* result, a `Coord` type that looks like this `[x: number, y: number]` is
* _identical_, in TypeScript's eyes, to this `Vector2` type `[x: number, y:
* number]`. Also, with tuples, the labels for each entry is ignored, so `[x:
* number, y: number]` is compatible with `[min: number, max: number]`. The
* labels are for humans, not TypeScript. :)
*
* If you make changes to types in this file, be very sure that:
*
* a) the changes are backwards compatible. If they are not, old data from
* previous versions of the "schema" could become unrenderable, or worse,
* introduce hard-to-diagnose bugs.
* b) the parsing code (`util/parse-perseus-json/`) is updated to handle
* the new format _as well as_ the old format.
*/
// TODO(LEMS-3080): Don't import KeypadKey here; data-schema.ts is supposed to
// be independent of everything else.
import type {KeypadKey} from "./keypad";
export type Coord = [x: number, y: number];
export type Interval = [min: number, max: number];
export type Vector2 = Coord; // Same name as Mafs
export type Range = Interval;
export type Size = [width: number, height: number];
export type CollinearTuple = [Vector2, Vector2];
export type ShowSolutions = "all" | "selected" | "none";
export type ShowAxisArrows = {
xMin: boolean;
xMax: boolean;
yMin: boolean;
yMax: boolean;
};
/**
* 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: string[] };
* dropdown: { choices: 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: string[] };
* `dropdown ${number}`: { choices: 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.
*
* This interface is the basis for "registering" all Perseus widget types.
*
* There should be one key/value pair for each supported widget. If you create
* a new widget, an entry should be added to this interface. Note that this
* only registers the widget options type, you'll also need to register the
* widget so that it's available at runtime using `registerWidget` in this
* library (as well as equivalent `registerWidget` functions in
* `@khanacademy/perseus` (for UI support) and `@khanacademy/perseus-score`
* (for scoring support)).
*
* Importantly, the key should be the name that is used in widget IDs. For most
* widgets that is the same as the widget option's `type` field. In cases where
* a widget has been deprecated and replaced with the deprecated-standin
* widget, it should be the original widget type!
*
* If you define the widget outside of this package, you can still add the new
* widget to this interface by writing the following in that package that
* contains the widget. TypeScript will merge that definition of the
* `PerseusWidgets` with the one defined below.
*
* ```typescript
* declare module "@khanacademy/perseus-core" {
* interface PerseusWidgetTypes {
* // A new widget
* "new-awesomeness": MyAwesomeNewWidget;
*
* // A deprecated widget
* "super-old-widget": DeprecatedStandinWidget;
* }
* }
*
* // The new widget's options definition
* type MyAwesomeNewWidget = WidgetOptions<'new-awesomeness', MyAwesomeNewWidgetOptions>;
*
* // The deprecated widget's options definition
* type SuperOldWidget = WidgetOptions<'super-old-widget', object>;
* ```
*
* This interface can be extended through the magic of TypeScript "Declaration
* merging". Specifically, we augment this module and extend this interface.
*
* @see {@link https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation}
*/
export interface PerseusWidgetTypes {
categorizer: CategorizerWidget;
"cs-program": CSProgramWidget;
definition: DefinitionWidget;
dropdown: DropdownWidget;
explanation: ExplanationWidget;
expression: ExpressionWidget;
"free-response": FreeResponseWidget;
grapher: GrapherWidget;
"graded-group-set": GradedGroupSetWidget;
"graded-group": GradedGroupWidget;
group: GroupWidget;
iframe: IFrameWidget;
image: ImageWidget;
"input-number": InputNumberWidget;
interaction: InteractionWidget;
"interactive-graph": InteractiveGraphWidget;
"label-image": LabelImageWidget;
matcher: MatcherWidget;
matrix: MatrixWidget;
measurer: MeasurerWidget;
"molecule-renderer": MoleculeRendererWidget;
"number-line": NumberLineWidget;
"numeric-input": NumericInputWidget;
orderer: OrdererWidget;
"phet-simulation": PhetSimulationWidget;
"python-program": PythonProgramWidget;
plotter: PlotterWidget;
radio: RadioWidget;
sorter: SorterWidget;
table: TableWidget;
video: VideoWidget;
// Deprecated widgets
"passage-ref-target": DeprecatedStandinWidget;
"passage-ref": DeprecatedStandinWidget;
passage: DeprecatedStandinWidget;
"lights-puzzle": DeprecatedStandinWidget;
sequence: DeprecatedStandinWidget;
simulator: DeprecatedStandinWidget;
transformer: DeprecatedStandinWidget;
}
/**
* A map of widget IDs to widget options. This is most often used as the type
* for a set of widgets defined in a `PerseusItem` but can also be useful to
* represent a function parameter where only `widgets` from a `PerseusItem` are
* needed. Today Widget IDs are made up of the widget type and an incrementing
* integer (eg. `interactive-graph 1` or `radio 3`). It is suggested to avoid
* reading/parsing the widget id to derive any information from it, except in
* the case of this map.
*
* @see {@link PerseusWidgetTypes} additional widgets can be added to this map type
* by augmenting the PerseusWidgetTypes with new widget types!
*/
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
* `ServerItemRenderer` and the layout is pre-set.
*/
export type PerseusItem = {
/** The details of the question being asked to the user. */
question: PerseusRenderer;
/**
* A collection of hints to be offered to the user that support answering
* the question.
*/
hints: Hint[];
/**
* Question helpers that should be made available to the user. Perseus
* itself does not ship with any of these tools, they are strictly hints to
* the host application.
*/
// TODO(benchristel): The parser for PerseusAnswerArea never returns
// undefined. We could remove `| undefined` here, but we'd
// have to update a bunch of test data in both this repo and frontend.
answerArea?: PerseusAnswerArea | undefined;
};
/**
* A "PerseusArticle" is an item that is meant to be rendered as an article.
* This item is never scored and is rendered by the `ArticleRenderer`.
*/
export type PerseusArticle = PerseusRenderer | PerseusRenderer[];
export type Version = {
/** The major part of the version */
major: number;
/** The minor part of the version */
minor: number;
};
export type PerseusRenderer = {
/**
* Translatable Markdown content to be rendered. May include references to
* widgets (as `[[☃ widget-id]]`) or [deprecated] images (as ``). This markdown can also include Math in the form of
* TeX surrounded by `$` characters (eg. `Solve the following: $1 + 1 =
* ?$.`).
*
* For each widget found in the Markdown, there _must_ be an entry in the
* {@link PerseusRenderer.widgets} object using the widget-id as the key.
*
* For each image found in the Markdown, there can be an entry in the
* {@link PerseusRenderer.images} object with the key being the image's url
* which defines additional attributes for the image.
*/
content: string;
/**
* A dictionary of {[widgetName]: Widget} to be referenced from the content
* field.
*/
widgets: PerseusWidgetsMap;
/**
* Formerly used in the PerseusGradedGroup widget. A list of "tags" that
* are keys that represent other content in the system. Not rendered to
* the user.
* @deprecated
*/
metadata?: any;
/**
* A dictionary of {[imageUrl]: {@link PerseusImageDetail}}.
*
* @deprecated Use of inline images is deprecated in top-level Perseus
* content but may be used when widgets embed a PerseusRenderer (such as
* the `radio` widget). In top-level content, please use an `image` widget
* instead.
*/
images: {
[imageUrl: string]: PerseusImageDetail;
};
};
export type Hint = PerseusRenderer & {
/**
* When `true`, causes the previous hint to be replaced with this hint when
* it is displayed.
*
* When `false`, the previous hint remains visible when this one is
* displayed. This allows for hints that build upon each other.
*/
replace?: boolean;
/**
* The UI needs to know how many hints there are before we have
* answerful PerseusItems. In a crunch, we decided to replace existing hints
* with empty hints and add a placeholder flag to signal that they're
* not real hints.
*
* TODO(LEMS-3806): there's probably a better way to do this
*/
placeholder?: boolean;
};
export type PerseusImageDetail = {
/** The width of the image */
width: number;
/** the height of the image */
height: number;
};
/**
* ItemExtras represent extra UI elements that help the learner in answering
* the question (such as a calculator for questions where solving by hand is
* not material to testing understanding of the skill).
*/
export const ItemExtras = [
/**
* The user might benefit from using a Scientific Calculator.
*
* Provided on Khan Academy when true.
*/
"calculator",
/**
* The user might benefit from using a monthly payments financial
* calculator.
*
* Provided on Khan Academy when true.
*/
"financialCalculatorMonthlyPayment",
/**
* The user might benefit from using a total amount financial calculator.
*
* Provided on Khan Academy when true.
*/
"financialCalculatorTotalAmount",
/**
* The user might benefit from using a time to pay off financial
* calculator.
*
* Provided on Khan Academy when true.
*/
"financialCalculatorTimeToPayOff",
/**
* The user might benefit from using a Periodic Table of Elements.
*
* Provided on Khan Academy when true.
*/
"periodicTable",
/**
* The user might benefit from using a Periodic Table of Elements with key.
*
* Provided on Khan Academy when true.
*/
"periodicTableWithKey",
] as const;
export type PerseusAnswerArea = Record<(typeof ItemExtras)[number], boolean>;
/**
* The type representing the common structure of all widget's options. The
* `Options` generic type represents the widget-specific option data.
*/
export type WidgetOptions<
Type extends string,
Options extends Record<string, any>,
> = {
/**
* The "type" of widget which will define what the Options field looks
* like.
*/
type: Type;
/**
* Whether this widget is displayed with the values and is immutable. For
* display only.
*/
static?: boolean;
/**
* Whether a widget is scored. Usually true except for IFrame widgets (deprecated).
* Default: true
*/
graded?: boolean;
/**
* The HTML alignment of the widget. "default" or "block".
*
* If the alignment is "default", it gets the default alignment from the
* widget logic, which can be various other alignments (e.g.
* "inline-block", "inline", etc).
*/
alignment?: string;
/**
* Options specific to the type field of the widget. See Perseus*WidgetOptions for
* more details
*/
options: Options;
/**
* Only used by interactive child widgets (line, point, etc) to identify the
* components
*/
key?: number | null;
/**
* The version of the widget data spec. Used to differentiate between newer
* and older content data. The parsers (`parseAndMigratePerseusItem`,
* `parseAndMigratePerseusArticle`, or `parseAndMigratePerseusRenderer`)
* will upgrade non-current versions of widget options to the latest.
*/
version?: Version;
};
// prettier-ignore
export type CategorizerWidget = WidgetOptions<'categorizer', PerseusCategorizerWidgetOptions>;
// prettier-ignore
export type CSProgramWidget = WidgetOptions<'cs-program', PerseusCSProgramWidgetOptions>;
// prettier-ignore
export type DefinitionWidget = WidgetOptions<'definition', PerseusDefinitionWidgetOptions>;
// prettier-ignore
export type DropdownWidget = WidgetOptions<'dropdown', PerseusDropdownWidgetOptions>;
// prettier-ignore
export type ExplanationWidget = WidgetOptions<'explanation', PerseusExplanationWidgetOptions>;
// prettier-ignore
export type ExpressionWidget = WidgetOptions<'expression', PerseusExpressionWidgetOptions>;
// prettier-ignore
export type FreeResponseWidget = WidgetOptions<'free-response', PerseusFreeResponseWidgetOptions>;
// prettier-ignore
export type GradedGroupSetWidget = WidgetOptions<'graded-group-set', PerseusGradedGroupSetWidgetOptions>;
// prettier-ignore
export type GradedGroupWidget = WidgetOptions<'graded-group', PerseusGradedGroupWidgetOptions>;
// prettier-ignore
export type GrapherWidget = WidgetOptions<'grapher', PerseusGrapherWidgetOptions>;
// prettier-ignore
export type GroupWidget = WidgetOptions<'group', PerseusGroupWidgetOptions>;
// prettier-ignore
export type IFrameWidget = WidgetOptions<'iframe', PerseusIFrameWidgetOptions>;
// prettier-ignore
export type ImageWidget = WidgetOptions<'image', PerseusImageWidgetOptions>;
// prettier-ignore
export type InteractionWidget = WidgetOptions<'interaction', PerseusInteractionWidgetOptions>;
// prettier-ignore
export type InteractiveGraphWidget = WidgetOptions<'interactive-graph', PerseusInteractiveGraphWidgetOptions>;
// prettier-ignore
export type LabelImageWidget = WidgetOptions<'label-image', PerseusLabelImageWidgetOptions>;
// prettier-ignore
export type MatcherWidget = WidgetOptions<'matcher', PerseusMatcherWidgetOptions>;
// prettier-ignore
export type MatrixWidget = WidgetOptions<'matrix', PerseusMatrixWidgetOptions>;
// prettier-ignore
export type MeasurerWidget = WidgetOptions<'measurer', PerseusMeasurerWidgetOptions>;
// prettier-ignore
export type NumberLineWidget = WidgetOptions<'number-line', PerseusNumberLineWidgetOptions>;
// prettier-ignore
export type NumericInputWidget = WidgetOptions<'numeric-input', PerseusNumericInputWidgetOptions>;
// prettier-ignore
export type OrdererWidget = WidgetOptions<'orderer', PerseusOrdererWidgetOptions>;
// prettier-ignore
export type PhetSimulationWidget = WidgetOptions<'phet-simulation', PerseusPhetSimulationWidgetOptions>;
// prettier-ignore
export type PlotterWidget = WidgetOptions<'plotter', PerseusPlotterWidgetOptions>;
// prettier-ignore
export type PythonProgramWidget = WidgetOptions<'python-program', PerseusPythonProgramWidgetOptions>;
// prettier-ignore
export type RadioWidget = WidgetOptions<'radio', PerseusRadioWidgetOptions>;
// prettier-ignore
export type SorterWidget = WidgetOptions<'sorter', PerseusSorterWidgetOptions>;
// prettier-ignore
export type TableWidget = WidgetOptions<'table', PerseusTableWidgetOptions>;
// prettier-ignore
export type InputNumberWidget = WidgetOptions<'input-number', PerseusInputNumberWidgetOptions>;
// prettier-ignore
export type MoleculeRendererWidget = WidgetOptions<'molecule-renderer', PerseusMoleculeRendererWidgetOptions>;
// prettier-ignore
export type VideoWidget = WidgetOptions<'video', PerseusVideoWidgetOptions>;
//prettier-ignore
export type DeprecatedStandinWidget = WidgetOptions<'deprecated-standin', object>;
/**
* A background image applied to various widgets.
*/
export type PerseusImageBackground = {
/** The URL of the image */
url?: string | null;
/** The width of the image */
width?: number;
/** The height of the image */
height?: number;
/** The top offset of the image */
top?: number;
/** The left offset of the image */
left?: number;
/** The scale of the image */
scale?: number;
/** The bottom offset of the image */
bottom?: number;
};
/**
* The type of markings to display on the graph.
* - axes: shows the axes without the grid lines
* - graph: shows the axes and the grid lines
* - grid: shows only the grid lines
* - none: shows no markings
*/
export type MarkingsType = "axes" | "graph" | "grid" | "none";
export type AxisLabelLocation = "onAxis" | "alongEdge";
/** Options for the categorizer widget. Presents items to sort into groups. */
export type PerseusCategorizerWidgetOptions = {
/**
* Translatable text; a list of items to categorize. e.g. ["banana",
* "yellow", "apple", "purple", "shirt"]
*/
items: string[];
/** Translatable text; a list of categories. e.g. ["fruits", "colors", "clothing"] */
categories: string[];
/** Whether the items should be randomized */
randomizeItems: boolean;
/** Whether this widget is displayed with the results and immutable */
static: boolean;
/**
* The correct answers where index relates to the items and value relates to
* the category. e.g. [0, 1, 0, 1, 2]
*/
values: number[];
/** Whether we should highlight i18n linter errors found on this widget */
// TODO(benchristel): highlightLint doesn't seem to be used. Delete it.
highlightLint?: boolean;
};
/** Options for the definition widget. Reveals a definition on click. */
export type PerseusDefinitionWidgetOptions = {
/** Translatable text; the word to define. e.g. "vertex" */
togglePrompt: string;
/** Translatable text; the definition of the word. e.g. "where 2 rays connect" */
definition: string;
/** Always false. Not used for this widget */
static: boolean;
};
/** Options for the dropdown widget. A list of choices in a dropdown. */
export type PerseusDropdownWidgetOptions = {
/** A list of choices for the dropdown */
choices: PerseusDropdownChoice[];
/** Translatable Text; placeholder text for a dropdown. e.g. "Please select a fruit" */
placeholder: string;
/** Always false. Not used for this widget */
static: boolean;
/** Translatable Text; visible label for the dropdown */
visibleLabel?: string;
/** Translatable Text; aria label that screen readers will read */
ariaLabel?: string;
};
export type PerseusDropdownChoice = {
/** Translatable text; The text for the option. e.g. "Banana" or "Orange" */
content: string;
/** Whether this is the correct option or not */
correct: boolean;
};
/** Options for the explanation widget. Reveals an explanation on click. */
export type PerseusExplanationWidgetOptions = {
/**
* Translatable Text; The clickable text to expand an explanation.
* e.g. "What is an apple?"
*/
showPrompt: string;
/**
* Translatable Text; The clickable text to hide an explanation.
* e.g. "Thanks. I got it!"
*/
hidePrompt: string;
/**
* Translatable Markdown; The explanation that is shown when showPrompt is
* clicked. e.g. "An apple is a tasty fruit."
*/
explanation: string;
/**
* explanation fields can embed widgets. When they do, the details of the
* widgets are here.
*/
widgets: PerseusWidgetsMap;
/** Always false. Not used for this widget */
static: boolean;
};
export type LegacyButtonSets = Array<
| "basic"
| "basic+div"
| "trig"
| "prealgebra"
| "logarithms"
| "basic relations"
| "advanced relations"
| "scientific"
>;
/** Options for the expression widget. Accepts a math expression answer. */
export type PerseusExpressionWidgetOptions = {
/** The expression forms the answer may come in */
answerForms: PerseusExpressionAnswerForm[];
buttonSets: LegacyButtonSets;
/** Variables that can be used as functions. Default: ["f", "g", "h"] */
functions: string[];
/** Use x for rendering multiplication instead of a center dot. */
times: boolean;
/**
* What extra keys need to be displayed on the keypad so that the question
* can be answerable without a keyboard (ie mobile)
*/
extraKeys?: KeypadKey[];
/** Visible label associated with the MathQuill field */
visibleLabel?: string;
/** Aria label for screen readers attached to MathQuill field */
ariaLabel?: string;
/**
* Controls when buttons for special characters are visible when using a
* desktop browser. Defaults to "focused".
* NOTE: This isn't listed in perseus-format.js or perseus_data.go, but
* appears in item data in the datastore.
*/
buttonsVisible?: "always" | "never" | "focused";
};
export const PerseusExpressionAnswerFormConsidered = [
"correct",
"wrong",
"ungraded",
] as const;
export type PerseusExpressionAnswerForm = {
/** The TeX form of the expression. e.g. "x\\cdot3=y" */
value: string;
/** The Answer expression must have the same form */
form: boolean;
/** The answer expression must be fully expanded and simplified */
simplify: boolean;
/** Whether the form is considered "correct", "wrong", or "ungraded" */
considered: (typeof PerseusExpressionAnswerFormConsidered)[number];
/**
* A key to identify the answer form in a list. Used only by the Perseus
* editor!
*/
key?: string;
};
/** Options for the graded-group widget. A self-contained scoreable group. */
export type PerseusGradedGroupWidgetOptions = {
/** Translatable Text; A title to be displayed for the group. */
title: string;
/** Not used in Perseus (but is set in (en, pt) production data) */
hasHint?: boolean | null | undefined;
/** A section to define hints for the group. */
hint?: PerseusRenderer | null | undefined;
/** Translatable Markdown. May include widgets and images embedded. */
content: string;
/** See {@link PerseusRenderer.widgets} */
widgets: PerseusWidgetsMap;
/** Not used in Perseus */
widgetEnabled?: boolean | null | undefined;
/** Not used in Perseus */
immutableWidgets?: boolean | null | undefined;
/** See {@link PerseusRenderer.images} */
images: {
[key: string]: PerseusImageDetail;
};
};
/** Options for the graded-group-set widget. A set of graded groups. */
export type PerseusGradedGroupSetWidgetOptions = {
/** A list of Widget Groups */
gradedGroups: PerseusGradedGroupWidgetOptions[];
};
/** A 2D coordinate range: x-axis [min, max] and y-axis [min, max]. */
export type GraphRange = [
x: [min: number, max: number],
y: [min: number, max: number],
];
/**
* The state of the grapher widget's plotted function, discriminated by
* the `type` field. Used as both the learner's
* user input and the rubric's correct answer.
*/
export type GrapherAnswerTypes =
| {
/**
* A V-shaped graph defined by its vertex.
*/
type: "absolute_value";
/**
* The vertex and a second point defining the V-shape. If null,
* the graph is not gradable and all answers score as invalid.
*/
coords: null | [vertex: Coord, secondPoint: Coord];
}
| {
/**
* A curve of the form y = a·bˣ + c approaching a horizontal.
*/
type: "exponential";
/**
* Two points along the horizontal asymptote line. Only the
* y-coordinate of the first point is used during scoring;
* x-coordinates are completely ignored.
*/
asymptote: [Coord, Coord];
/**
* Two points along the exponential curve. One end of the curve
* trends towards the asymptote. If null, the graph is not
* gradable and all answers score as invalid.
*/
coords: null | [Coord, Coord];
}
| {
/**
* A straight line of the form y = mx + b.
*/
type: "linear";
/**
* Two points along the straight line. If null, the graph is not
* gradable and all answers score as invalid.
*/
coords: null | [Coord, Coord];
}
| {
/**
* A curve of the form y = a·log_b(x - c) approaching a vertical
* asymptote.
*/
type: "logarithm";
/** Two points along the asymptote line. */
asymptote: [Coord, Coord];
/**
* Two points along the logarithmic curve. One end of the curve
* trends towards the asymptote. If null, the graph is not
* gradable and all answers score as invalid.
*/
coords: null | [Coord, Coord];
}
| {
/**
* A parabola of the form y = ax² + bx + c.
*/
type: "quadratic";
/**
* The vertex and a second point defining the parabola. If null,
* the graph is not gradable and all answers score as invalid.
*/
coords: null | [vertex: Coord, secondPoint: Coord];
}
| {
/**
* A periodic wave of the form y = a·sin(bx - c) + d.
*/
type: "sinusoid";
/**
* Two points on the same slope of the sinusoid. If null, the
* graph is not gradable and all answers score as invalid.
*/
coords: null | [Coord, Coord];
}
| {
/**
* A periodic curve of the form y = a·tan(bx - c) + d.
*/
type: "tangent";
/**
* Two points on the same slope of the tangent curve. If null,
* the graph is not gradable and all answers score as invalid.
*/
coords: null | [Coord, Coord];
};
/**
* Options for the Grapher widget. Defines the available function
* types, the correct answer, and the visual graph configuration.
*/
export type PerseusGrapherWidgetOptions = {
/** The set of function types the learner can choose from when plotting. */
availableTypes: Array<
| "absolute_value"
| "exponential"
| "linear"
| "logarithm"
| "quadratic"
| "sinusoid"
| "tangent"
>;
/** The correct answer; used to score the learner's plotted function. */
correct: GrapherAnswerTypes;
/** Visual configuration for the coordinate plane. */
graph: {
/** An optional background image displayed behind the graph. */
backgroundImage: {
/** Vertical offset from the bottom of the graph in pixels. */
bottom?: number;
/** Height of the image in pixels. */
height?: number;
/** Horizontal offset from the left edge of the graph in pixels. */
left?: number;
/** Scale factor applied to the image. */
scale?: number;
/** URL of the background image, or null/undefined if none. */
url?: string | null | undefined;
/** Width of the image in pixels. */
width?: number;
};
/** The [width, height] of the graph canvas in pixels. */
box?: [number, number];
/** Which graph settings are editable in the editor UI. */
editableSettings?: Array<"graph" | "snap" | "image" | "measure">;
/** The [x, y] spacing between grid lines. */
gridStep?: [number, number];
/** The [x-axis, y-axis] labels. */
labels: [string, string];
/** Which markings to show on the graph (axes, grid, graph, or none). */
markings: MarkingsType;
/** The visible [x-range, y-range] of the coordinate plane. */
range: GraphRange;
/** The label for the ruler overlay (currently always empty string). */
rulerLabel: "";
/** The number of tick marks on the ruler overlay. */
rulerTicks: number;
/** When true, a protractor overlay is shown on the graph. */
showProtractor?: boolean;
/** When true, a ruler overlay is shown on the graph. */
showRuler?: boolean;
/** When true, coordinate tooltips are shown on hover. */
showTooltips?: boolean;
/** The [x, y] snap increment for interactive elements. */
snapStep?: [number, number];
/** The [x, y] distance between labeled tick marks. */
step: [number, number];
/**
* Whether the graph configuration is valid. Can be false or an
* error message string.
*/
valid?: boolean | string;
};
};
/** Options for the group widget. An alias for PerseusRenderer. */
export type PerseusGroupWidgetOptions = PerseusRenderer;
/** Options for the image widget. Shows an image with a caption and alt text. */
export type PerseusImageWidgetOptions = {
/** Translatable Markdown; Text to be shown for the title of the image */
title?: string;
/** Translatable Markdown; Text to be shown in the caption section of an image */
caption?: string;
/** Translatable Text; The alt text to be shown in the img.alt attribute */
alt?: string;
/** Translatable Markdown; Text to be shown as the long description of an image */
longDescription?: string;
/**
* When true, standalone image will be rendered with alt="" and without any alt
* text, caption, title, or long description.
*/
decorative?: boolean;
/** The image details for the image to be displayed */
backgroundImage: PerseusImageBackground;
/** The size scale of the image */
scale?: number;
/** Always false. Not used for this widget */
static?: boolean;
/** @deprecated - labels were removed from the image widget in 2017 */
labels?: Array<PerseusImageLabel>;
/** @deprecated - range for labels was removed from the image widget in 2017 */
range?: [Interval, Interval];
/** @deprecated - box for labels was removed from the image widget in 2017 */
box?: Size;
};
export type PerseusImageLabel = {
/** Translatable Text; The content of the label to display */
content: string;
/** The visual alignment of the label. default: "center" */
alignment: string;
/** The point on the image to display the label */
coordinates: number[];
};
/** Options for the interactive-graph widget. An interactive geometry graph. */
export type PerseusInteractiveGraphWidgetOptions = {
/**
* Where the little black axis lines & labels (ticks) should render. Also
* known as the tick step. default [1, 1]
*/
step: [number, number];
/** Where the grid lines on the graph will render. default [1, 1] */
gridStep?: [x: number, y: number];
/**
* Where the graph points will lock to when they are moved.
* default [0.5, 0.5]
*/
snapStep?: [x: number, y: number];
/** An optional image to use in the background */
backgroundImage?: PerseusImageBackground;
/**
* The type of markings to display on the graph.
*/
markings: MarkingsType;
/** How to label the X and Y axis. default: ["x", "y"] */
labels?: string[];
/**
* Specifies the location of the labels on the graph. default: "onAxis".
* - "onAxis": Labels are positioned on the axis at the right (x) and top
* (y) of the graph.
* - "alongEdge": Labels are centered along the bottom (x) and left (y)
* edges of the graph. The y label is rotated. Typically used when the
* range min is near 0 with longer labels.
*/
labelLocation?: AxisLabelLocation;
/** Which sides of the graph are bounded (removed axis arrows). */
showAxisArrows: ShowAxisArrows;
/** Whether to show the Protractor tool overlayed on top of the graph */
showProtractor: boolean;
/**
* Whether to show the Ruler tool overlayed on top of the graph.
* @deprecated - no longer used by the InteractiveGraph widget. The
* property is kept on this type to prevent its accidental reuse in future
* features, since it may appear in production data.
*/
showRuler?: boolean;
/** Whether to show tooltips on the graph */
showTooltips?: boolean;
/**
* The unit to show on the ruler. e.g. "mm", "cm", "m", "km", "in", "ft",
* "yd", "mi".
* @deprecated - no longer used by the InteractiveGraph widget. The
* property is kept on this type to prevent its accidental reuse in future
* features, since it may appear in production data.
*/
rulerLabel?: string;
/**
* How many ticks to show on the ruler. e.g. 1, 2, 4, 8, 10, 16. Must be
* an integer.
* @deprecated - no longer used by the InteractiveGraph widget. The
* property is kept on this type to prevent its accidental reuse in future
* features, since it may appear in production data.
*/
rulerTicks?: number;
/**
* The X and Y coordinate ranges for the view of the graph.
* default: [[-10, 10], [-10, 10]]
*/
range: GraphRange;
/** The type of graph */
graph: PerseusGraphType;
/** The correct kind of graph, if being used to select function type */
// TODO(LEMS-2344): make the type of `correct` more specific
correct: PerseusGraphType;
/**
* Shapes (points, chords, etc) displayed on the graph that cannot be moved
* by the user.
*/
lockedFigures: LockedFigure[];
/** Aria label that applies to the entire graph. */
fullGraphAriaLabel?: string;
/** Aria description that applies to the entire graph. */
fullGraphAriaDescription?: string;
};
export const lockedFigureColorNames = [
"blue",
"green",
"grayH",
"purple",
"pink",
"orange",
"red",
] as const;
export type LockedFigureColor = (typeof lockedFigureColorNames)[number];
export const lockedFigureColors: Record<LockedFigureColor, string> = {
blue: "#3D7586",
green: "#447A53",
grayH: "#3B3D45",
purple: "#594094",
pink: "#B25071",
red: "#D92916",
orange: "#946700",
} as const;
export type StrokeWeight = "thin" | "medium" | "thick";
export type LockedFigure =
| LockedPointType
| LockedLineType
| LockedVectorType
| LockedEllipseType
| LockedPolygonType
| LockedFunctionType
| LockedLabelType;
export type LockedFigureType = LockedFigure["type"];
export type LockedLineStyle = "solid" | "dashed";