@khanacademy/perseus 17.8.0 → 18.0.1

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.
Files changed (56) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/dist/es/index.css +0 -4
  3. package/dist/es/index.js +1422 -1021
  4. package/dist/es/index.js.map +1 -1
  5. package/dist/index.css +0 -4
  6. package/dist/index.d.ts +4 -2
  7. package/dist/index.js +1321 -954
  8. package/dist/index.js.map +1 -1
  9. package/dist/perseus-types.d.ts +4 -3
  10. package/dist/renderer.d.ts +6 -6
  11. package/dist/server-item-renderer.d.ts +2 -4
  12. package/dist/strings.js +1 -1
  13. package/dist/types.d.ts +19 -2
  14. package/dist/util/drawing-transform.d.ts +21 -0
  15. package/dist/util/extract-perseus-data.d.ts +26 -0
  16. package/dist/util/graph-bounds.d.ts +9 -0
  17. package/dist/util/graphie.d.ts +2 -0
  18. package/dist/util/interval.d.ts +1 -0
  19. package/dist/util/parse-perseus-json.d.ts +10 -0
  20. package/dist/util/tex.d.ts +1 -3
  21. package/dist/widgets/deprecated-standin/index.d.ts +4 -4
  22. package/dist/widgets/passage.d.ts +3 -3
  23. package/dist/widgets.d.ts +16 -6
  24. package/package.json +9 -9
  25. package/src/__tests__/extract-perseus-data.test.ts +1027 -0
  26. package/src/index.ts +6 -1
  27. package/src/init.ts +1 -1
  28. package/src/perseus-types.ts +4 -3
  29. package/src/renderer.tsx +12 -9
  30. package/src/server-item-renderer.tsx +2 -4
  31. package/src/styles/widgets/image.less +0 -3
  32. package/src/types.ts +19 -16
  33. package/src/util/drawing-transform.test.ts +67 -0
  34. package/src/util/drawing-transform.ts +102 -0
  35. package/src/util/extract-perseus-data.ts +539 -0
  36. package/src/util/graph-bounds.ts +23 -0
  37. package/src/util/graphie.ts +1167 -1258
  38. package/src/util/interactive.ts +2 -2
  39. package/src/util/interval.ts +4 -0
  40. package/src/util/parse-perseus-json.test.ts +10 -0
  41. package/src/util/parse-perseus-json.ts +13 -0
  42. package/src/util/tex.ts +11 -84
  43. package/src/widgets/__stories__/image.stories.tsx +12 -26
  44. package/src/widgets/__tests__/__snapshots__/grapher.test.ts.snap +72 -288
  45. package/src/widgets/__tests__/__snapshots__/interaction.test.ts.snap +5 -20
  46. package/src/widgets/__tests__/__snapshots__/interactive-graph.test.ts.snap +32 -128
  47. package/src/widgets/__tests__/__snapshots__/number-line.test.ts.snap +22 -88
  48. package/src/widgets/__tests__/__snapshots__/plotter.test.tsx.snap +12 -48
  49. package/src/widgets/deprecated-standin/__stories__/deprecated-standin.stories.tsx +8 -12
  50. package/src/widgets/deprecated-standin/__tests__/__snapshots__/deprecated-standin.test.ts.snap +6 -7
  51. package/src/widgets/deprecated-standin/index.tsx +8 -7
  52. package/src/widgets/interactive-graph.tsx +3 -2
  53. package/src/widgets/label-image/answer-pill.tsx +6 -1
  54. package/src/widgets/passage.tsx +5 -7
  55. package/src/widgets.ts +34 -10
  56. package/tsconfig-build.tsbuildinfo +1 -1
@@ -0,0 +1,539 @@
1
+ import {keys} from "@khanacademy/wonder-stuff-core";
2
+
3
+ import type {PerseusRenderer} from "../perseus-types";
4
+
5
+ /**
6
+ * This function extracts the answers from the widgets.
7
+ *
8
+ * For each widget type, we first check to make sure the path to the answer exists,
9
+ * then we extract the answer and add it to the list of answers.
10
+ *
11
+ * A list is returned because some widgets, like multi-select radio widgets and groups
12
+ * can have multiple answers.
13
+ *
14
+ * @param {PerseusRenderer["widgets"]} widgets
15
+ * @returns a list of strings that are the answers to the widgets
16
+ */
17
+ function getAnswersFromWidgets(
18
+ widgets: PerseusRenderer["widgets"],
19
+ ): ReadonlyArray<string> {
20
+ const answers: Array<string> = [];
21
+
22
+ keys(widgets).forEach((widgetID) => {
23
+ const widget = widgets[widgetID];
24
+
25
+ if (!widget.options) {
26
+ return;
27
+ }
28
+
29
+ switch (widget.type) {
30
+ case "radio":
31
+ const radio = widget;
32
+ const options = radio.options;
33
+ // Answer is the content of the correct choice
34
+ if (options?.choices?.length) {
35
+ for (const choice of options.choices) {
36
+ if (choice?.correct) {
37
+ answers.push(choice.content);
38
+ }
39
+ }
40
+ }
41
+ break;
42
+ case "categorizer":
43
+ // Answer is a list of items mapped to their category
44
+ // e.g. ['item 1: category 1', 'item 2: category 2']
45
+ const categorizer = widget;
46
+ if (
47
+ categorizer.options?.categories &&
48
+ categorizer.options?.items &&
49
+ categorizer.options?.values
50
+ ) {
51
+ const categories = categorizer.options?.categories;
52
+ const items = categorizer.options?.items;
53
+ const values = categorizer.options?.values;
54
+ answers.push(
55
+ ...values.map(
56
+ (value, index) =>
57
+ `${items[index]}: ${categories[value]}`,
58
+ ),
59
+ );
60
+ }
61
+ break;
62
+ case "dropdown":
63
+ // Answer is the content of correct dropdown option
64
+ const dropdown = widget;
65
+ if (dropdown.options?.choices) {
66
+ for (const choice of dropdown.options.choices) {
67
+ if (choice.correct) {
68
+ answers.push(choice.content);
69
+ }
70
+ }
71
+ }
72
+ break;
73
+ case "numeric-input":
74
+ // Answer is the numeric value cast to a string
75
+ const numericInput = widget;
76
+ if (numericInput.options?.answers) {
77
+ for (const ans of numericInput.options.answers) {
78
+ if (ans.status === "correct" && ans.value != null) {
79
+ answers.push(ans.value.toString());
80
+ }
81
+ }
82
+ }
83
+ break;
84
+ case "input-number":
85
+ // Answer is the correct value
86
+ const inputNumber = widget;
87
+ if (inputNumber.options?.value) {
88
+ answers.push(inputNumber.options.value.toString());
89
+ }
90
+ break;
91
+ case "expression":
92
+ // Answer is a list of potential correct expressions,
93
+ // since there can be multiple correct forms of the expression
94
+ const expression = widget;
95
+ if (expression.options?.answerForms) {
96
+ answers.push(
97
+ ...expression.options.answerForms.map(
98
+ (answer) => answer.value,
99
+ ),
100
+ );
101
+ }
102
+ break;
103
+ case "group":
104
+ case "graded-group":
105
+ // Groups can contain multiple widgets, so we recursively
106
+ // call this function to get the nested answers from the
107
+ // group's widgets
108
+ const gradedGroup = widget;
109
+ if (gradedGroup.options?.widgets) {
110
+ answers.push(
111
+ ...getAnswersFromWidgets(gradedGroup.options.widgets),
112
+ );
113
+ }
114
+ break;
115
+ case "plotter":
116
+ // Answer is a map of the correct value for each category
117
+ // e.g. '{Soccer: 35, Basketball: 20, Golf: 45, Volleyball: 15,
118
+ // Tennis: 30}'
119
+ const plotter = widget;
120
+ if (
121
+ plotter.options?.categories &&
122
+ plotter.options?.correct &&
123
+ plotter.options.categories.length ===
124
+ plotter.options.correct.length
125
+ ) {
126
+ const {categories, correct} = plotter.options;
127
+ answers.push(
128
+ `{${categories
129
+ .map(
130
+ (category, index) =>
131
+ `${category}: ${correct[index]}`,
132
+ )
133
+ .join(", ")}}`,
134
+ );
135
+ }
136
+ break;
137
+ case "interactive-graph":
138
+ case "grapher":
139
+ // Answer is a list of points that should be on the graph
140
+ // E.g. 'There should be point(s) on [(-1, 1), (1, 1)]'
141
+ const grapher = widget;
142
+ // @ts-expect-error - TS2339 - Property 'coords' does not exist on type '{ type: "absolute_value"; coords: [Coord, Coord]; } | { type: "exponential"; asymptote: [Coord, Coord]; coords: [Coord, Coord]; } | { type: "linear"; coords: [...]; } | ... 4 more ... | PerseusGraphType'.
143
+ if (grapher.options?.correct?.coords) {
144
+ answers.push(
145
+ // @ts-expect-error - TS2339 - Property 'coords' does not exist on type '{ type: "absolute_value"; coords: [Coord, Coord]; } | { type: "exponential"; asymptote: [Coord, Coord]; coords: [Coord, Coord]; } | { type: "linear"; coords: [...]; } | ... 4 more ... | PerseusGraphType'.
146
+ `There should be point(s) on [${grapher.options.correct?.coords.join(
147
+ "], [",
148
+ )}]`,
149
+ );
150
+ }
151
+ break;
152
+ case "orderer":
153
+ // Answer is the content of the each option
154
+ // in the correct order on newlines
155
+ // E.g. 'item 1\nitem 2\nitem 3'
156
+ const orderer = widget;
157
+ if (orderer.options?.correctOptions) {
158
+ answers.push(
159
+ orderer.options.correctOptions
160
+ .map((option) => option.content)
161
+ .join("\n"),
162
+ );
163
+ }
164
+ break;
165
+ case "sorter":
166
+ // Answer is the content of each option in the correct order
167
+ // separated by commas
168
+ // E.g. 'item 1, item 2, item 3'
169
+ const sorter = widget;
170
+ if (sorter.options?.correct) {
171
+ answers.push(sorter.options.correct.join(", "));
172
+ }
173
+ break;
174
+ case "label-image":
175
+ // Answer is a list of labels, their positions, and their answer
176
+ // E.g. '{label: "label 1", position: {x: 0.5, y: 0.5}, answer: "answer 1"}'
177
+ const labelImage = widget;
178
+ if (labelImage.options?.markers) {
179
+ answers.push(
180
+ ...labelImage.options.markers.map(
181
+ (m) =>
182
+ `{label: "${m.label}", position: {${m.x},${
183
+ m.y
184
+ }}, answer: "${m.answers.join(", ")}"}`,
185
+ ),
186
+ );
187
+ }
188
+ break;
189
+ case "number-line":
190
+ // Answer is the correct x position on the number line
191
+ // E.g. '0.5'
192
+ const numberLine = widget;
193
+ if (numberLine.options?.correctX != null) {
194
+ answers.push(numberLine.options.correctX.toString());
195
+ }
196
+ break;
197
+ case "matrix":
198
+ // Answer is a list of rows representing the matrix
199
+ // E.g. '[[1, 2], [3, 4]]'
200
+ const matrix = widget;
201
+ if (matrix.options?.answers) {
202
+ answers.push(`[${matrix.options.answers.join("], [")}]`);
203
+ }
204
+ break;
205
+ case "matcher":
206
+ // Answer is a table of the left and right items
207
+ // E.g. '| Left | Right |\n| --- | --- |\n| left item 1 | right item 1 |\n| left item 2 | right item 2 |'
208
+ const matcher = widget;
209
+ if (matcher.options?.left && matcher.options?.right) {
210
+ const {left, right} = matcher.options;
211
+ const [leftHeader, rightHeader] = matcher.options.labels;
212
+ const tableHeader = `| ${leftHeader} | ${rightHeader} |\n| --- | --- |`;
213
+ const tableRows = left.map((leftItem, index) => {
214
+ return `| ${leftItem} | ${right[index]} |`;
215
+ });
216
+ const table = [tableHeader, ...tableRows].join("\n");
217
+ answers.push(table);
218
+ }
219
+ break;
220
+ }
221
+ });
222
+
223
+ return answers;
224
+ }
225
+
226
+ /**
227
+ * Inject a string equivalent of the widgets into the content.
228
+ *
229
+ * Content may contain Perseus widgets, that looks like this: '[[☃ Radio 1]]'.
230
+ * This function replaces those widgets with its equivalent string.
231
+ *
232
+ * @param {string} content
233
+ * @param {PerseusRenderer["widgets"]} widgets
234
+ * @returns
235
+ */
236
+ function injectWidgets(
237
+ content: string,
238
+ widgets: PerseusRenderer["widgets"],
239
+ ): string {
240
+ // The types for taskProgress.itemData are not well defined,
241
+ // so there is a chance that widgets or content could be undefined.
242
+ // In that scenario, simply exit early and return the content as is.
243
+ if (!content) {
244
+ return "";
245
+ }
246
+ if (!widgets) {
247
+ return content;
248
+ }
249
+
250
+ let context = content;
251
+
252
+ keys(widgets).forEach((widgetID) => {
253
+ const widget = widgets[widgetID];
254
+
255
+ if (!widget.options) {
256
+ return;
257
+ }
258
+
259
+ switch (widget.type) {
260
+ case "radio":
261
+ // Replace radio with the radio options
262
+ // '[[☃ Radio 1]]' -> 'option 1\noption 2\noption 3'
263
+ const radio = widget;
264
+ if (radio.options?.choices?.length) {
265
+ let radioContext = radio.options.choices
266
+ .map((choice) => choice.content)
267
+ .join("\n");
268
+
269
+ if (radio.options?.randomize) {
270
+ radioContext +=
271
+ "\nThose options are displayed in a different order to the user. If the user says the letter, number, or ordinal number, always ask them clarify which option they are referring to.\n";
272
+ }
273
+
274
+ context = context.replace(
275
+ `[[☃ ${widgetID}]]`,
276
+ radioContext,
277
+ );
278
+ }
279
+ break;
280
+ case "image":
281
+ // Replace image with the image's alt text
282
+ // '[[☃ Image 1]]' -> '<img id="Image 1" alt="The text for this image">'
283
+ const image = widget;
284
+ if (image.options?.alt) {
285
+ context = context.replace(
286
+ `[[☃ ${widgetID}]]`,
287
+ `<img id="${widgetID}" alt="${image.options.alt}">`,
288
+ );
289
+ }
290
+ break;
291
+ case "label-image":
292
+ // Replace label-image with the label-image's alt text
293
+ // '[[☃ Label Image 1]]' -> '[An image with dots that user needs to label. Label choices: [choice 1, choice 2]. Image alt text: The text for this image]'
294
+ const labelImage = widget;
295
+ if (labelImage.options?.imageAlt) {
296
+ context = context.replace(
297
+ `[[☃ ${widgetID}]]`,
298
+ `[An image with dots that user needs to label. Label choices: [${labelImage.options.choices.join(
299
+ ", ",
300
+ )}]. Image alt text: ${
301
+ labelImage.options?.imageAlt ?? ""
302
+ }]`,
303
+ );
304
+ }
305
+ break;
306
+ case "explanation":
307
+ // Replace explaintion with the explaination text
308
+ // Note: explainations can have widgets inside of them too
309
+ // '[[☃ Explanation 1]]' -> 'This is the hidden explanation text'
310
+ const explanation = widget;
311
+ if (explanation.options?.explanation) {
312
+ context = context.replace(
313
+ `[[☃ ${widgetID}]]`,
314
+ injectWidgets(
315
+ explanation.options.explanation,
316
+ explanation.options.widgets,
317
+ ),
318
+ );
319
+ }
320
+ break;
321
+ case "passage":
322
+ // Replace passage with the passage text
323
+ // '[[☃ Passage 1]]' -> '# Passage Title\n\nPassage text'
324
+ const passage = widget;
325
+ if (
326
+ passage.options?.passageTitle ||
327
+ passage.options?.passageText
328
+ ) {
329
+ const {passageTitle, passageText} = passage.options;
330
+ context = context.replace(
331
+ `[[☃ ${widgetID}]]`,
332
+ `# ${passageTitle}\n\n${passageText}`,
333
+ );
334
+ }
335
+ break;
336
+ case "group":
337
+ case "graded-group":
338
+ // Replace group with the group's nested content
339
+ // '[[☃ Group 1]]' -> 'Nested question\n[[☃ Radio 1]]'
340
+ // -> 'Nested question\noption 1\noption 2\noption 3'
341
+ const group = widget;
342
+ if (group.options?.widgets && group.options.content) {
343
+ context = context.replace(
344
+ `[[☃ ${widgetID}]]`,
345
+ injectWidgets(
346
+ group.options.content,
347
+ group.options.widgets,
348
+ ),
349
+ );
350
+ }
351
+ break;
352
+ case "graded-group-set":
353
+ // Replace graded-group-set with the the nested groups
354
+ // '[[☃ Graded Group Set 1]]'
355
+ // -> 'Nested question 1\noption 1\noption 2\noption 3\nNested question 2\noption 1\noption 2\noption 3'
356
+ const gradedGroup = widget;
357
+ if (gradedGroup.options?.gradedGroups) {
358
+ const gradedGroups = gradedGroup.options.gradedGroups;
359
+
360
+ const gradedGroupsContent = gradedGroups.reduce(
361
+ (acc, group) => {
362
+ if (group.widgets && group.content) {
363
+ acc +=
364
+ injectWidgets(
365
+ group.content,
366
+ group.widgets,
367
+ ) + "\n";
368
+ }
369
+ return acc;
370
+ },
371
+ "",
372
+ );
373
+
374
+ context = context.replace(
375
+ `[[☃ ${widgetID}]]`,
376
+ gradedGroupsContent,
377
+ );
378
+ }
379
+ break;
380
+ case "categorizer":
381
+ // Replace categorizer with the options and categories
382
+ // '[[☃ Categorizer 1]]' -> '''
383
+ // For each item, select the correct category. Categories: category 1, category 2, category 3.
384
+ // Items:
385
+ // item 1
386
+ // item 2
387
+ // '''
388
+ const categorizer = widget;
389
+ if (
390
+ categorizer.options?.categories &&
391
+ categorizer.options.items
392
+ ) {
393
+ const categories = categorizer.options.categories;
394
+ const items = categorizer.options.items;
395
+ context = context.replace(
396
+ `[[☃ ${widgetID}]]`,
397
+ `For each item, select the correct category. Categories: ${categories.join(
398
+ ", ",
399
+ )}.\nItems:\n${items.join("\n")}\n`,
400
+ );
401
+ }
402
+ break;
403
+ case "dropdown":
404
+ // Replace dropdown with the dropdown options
405
+ // '[[☃ Dropdown 1]]' -> '[option 1 | option 2 | option 3]'
406
+ const dropdown = widget;
407
+ if (dropdown.options?.choices) {
408
+ const choices = dropdown.options.choices.map(
409
+ (choice) => choice.content,
410
+ );
411
+ context = context.replace(
412
+ `[[☃ ${widgetID}]]`,
413
+ `[${choices.join(" | ")}]`,
414
+ );
415
+ }
416
+ break;
417
+ case "definition":
418
+ // Replace definition with the word that is being defined
419
+ // NOTE(chase): this does not include the definition itself,
420
+ // because I believe the AI can infer that without wasting extra tokens
421
+ // '[[☃ Definition 1]]' -> 'word'
422
+ const definition = widget;
423
+ if (definition.options?.togglePrompt) {
424
+ context = context.replace(
425
+ `[[☃ ${widgetID}]]`,
426
+ definition.options.togglePrompt,
427
+ );
428
+ }
429
+ break;
430
+ case "orderer":
431
+ // Replace orderer with the orderer options
432
+ // '[[☃ Orderer 1]]' -> 'option 1\noption 2\noption 3'
433
+ const orderer = widget;
434
+ if (orderer.options?.options) {
435
+ context = context.replace(
436
+ `[[☃ ${widgetID}]]`,
437
+ orderer.options.options
438
+ .map((option) => option.content)
439
+ .join("\n"),
440
+ );
441
+ }
442
+ break;
443
+ case "sorter":
444
+ // Replace sorter with the options
445
+ // NOTE(chase): These are already in the correct order
446
+ // '[[☃ Sorter 1]]' -> '[option 1 | option 2 | option 3]'
447
+ const sorter = widget;
448
+ if (sorter.options?.correct) {
449
+ const choices = sorter.options.correct;
450
+ context = context.replace(
451
+ `[[☃ ${widgetID}]]`,
452
+ `[${choices.join(" | ")}]`,
453
+ );
454
+ }
455
+ break;
456
+ case "interactive-graph":
457
+ // Replace interactive-graph with the graph's range
458
+ // '[[☃ Interactive Graph 1]]' -> '[Graph with an x range of -10 to 10 and y range of -10 to 10]'
459
+ const interactiveGraph = widget;
460
+ if (interactiveGraph.options?.range.length === 2) {
461
+ const [x, y] = interactiveGraph.options.range;
462
+ context = context.replace(
463
+ `[[☃ ${widgetID}]]`,
464
+ `[Graph with an x range of ${x[0]} to ${x[1]} and y range of ${y[0]} to ${y[1]}]`,
465
+ );
466
+ }
467
+ break;
468
+ case "number-line":
469
+ // Replace number-line with the number line's range, step, and initial position
470
+ // '[[☃ Number Line 1]]' -> '[Number line with a range of -10 to 10, a step of 1, and an initial position of 0]'
471
+ const numberLine = widget;
472
+ if (
473
+ numberLine.options?.range.length === 2 &&
474
+ numberLine.options?.tickStep &&
475
+ numberLine.options?.initialX
476
+ ) {
477
+ const [min, max] = numberLine.options.range;
478
+ const step = numberLine.options.tickStep;
479
+ const initialPosition = numberLine.options.initialX;
480
+ context = context.replace(
481
+ `[[☃ ${widgetID}]]`,
482
+ `[Number line with a range of ${min} to ${max}, a step of ${step}, and an initial position of ${initialPosition}]`,
483
+ );
484
+ }
485
+ break;
486
+ case "matrix":
487
+ // Replace matrix with the matrix's dimensions
488
+ // '[[☃ Matrix 1]]' -> '[Matrix with 2 rows and 3 columns. The user can click on each cell to enter a value]'
489
+ const matrix = widget;
490
+ if (matrix.options?.matrixBoardSize.length === 2) {
491
+ const [rows, columns] = matrix.options.matrixBoardSize;
492
+ context = context.replace(
493
+ `[[☃ ${widgetID}]]`,
494
+ `[Matrix with ${rows} rows and ${columns} columns. The user can click on each cell to enter a value]`,
495
+ );
496
+ }
497
+ break;
498
+ case "matcher":
499
+ // Replace matcher with a table representing the left and right columns
500
+ // '[[☃ Matcher 1]]' -> '| Left Column | Right Column |\n| --- | --- |\n| option 1 | option 2 |\n| option 3 | option 4 |'
501
+ const matcher = widget;
502
+ if (matcher.options?.left && matcher.options?.right) {
503
+ const {left, right} = matcher.options;
504
+ const [leftHeader, rightHeader] = matcher.options.labels;
505
+ const tableHeader = `| ${leftHeader} | ${rightHeader} |\n| --- | --- |`;
506
+ const tableRows = left.map((leftItem, index) => {
507
+ return `| ${leftItem} | ${right[index]} |`;
508
+ });
509
+ const table = [tableHeader, ...tableRows].join("\n");
510
+ const matcherWidgetExplanation =
511
+ "The user needs to move items in the right column to match the correct option on the left. The order of items on the right side will be different from what the user sees.";
512
+ context = context.replace(
513
+ `[[☃ ${widgetID}]]`,
514
+ `${matcherWidgetExplanation}\n${table}`,
515
+ );
516
+ }
517
+ break;
518
+ case "numeric-input":
519
+ case "input-number":
520
+ case "expression":
521
+ // These widgets are just input fields, so we replace them with a ?
522
+ // 'Solve 2+2 [[☃ Numeric Input 1]]' -> 'Solve 2+2 ?'
523
+ // (we've found that using "?" gives the best results on fill-
524
+ // in-the-blank questions).
525
+ context = context.replace(`[[☃ ${widgetID}]]`, "?");
526
+ break;
527
+ default:
528
+ // Tell the bot that it doesn't understand this widget
529
+ context = context.replace(
530
+ `[[☃ ${widgetID}]]`,
531
+ `[[Unsupported ${widget.type} widget: Explain to the user that you are unable to understand the content in this widget and ask them to describe it.]]`,
532
+ );
533
+ }
534
+ });
535
+
536
+ return context;
537
+ }
538
+
539
+ export {getAnswersFromWidgets, injectWidgets};
@@ -0,0 +1,23 @@
1
+ // Defines the bounds of the visible area of a graph, in Cartesian coordinates.
2
+ // GraphBounds is immutable.
3
+ export class GraphBounds {
4
+ readonly xMin: number;
5
+ readonly xMax: number;
6
+ readonly yMin: number;
7
+ readonly yMax: number;
8
+
9
+ constructor(xRange: [number, number], yRange: [number, number]) {
10
+ this.xMin = xRange[0];
11
+ this.xMax = xRange[1];
12
+ this.yMin = yRange[0];
13
+ this.yMax = yRange[1];
14
+ }
15
+
16
+ width(): number {
17
+ return this.xMax - this.xMin;
18
+ }
19
+
20
+ height(): number {
21
+ return this.yMax - this.yMin;
22
+ }
23
+ }