-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathinteractive-graph-widget.ts
More file actions
391 lines (353 loc) · 13.2 KB
/
Copy pathinteractive-graph-widget.ts
File metadata and controls
391 lines (353 loc) · 13.2 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
import {lockedFigureColorNames} from "../../data-schema";
import {
array,
boolean,
constant,
enumeration,
nullable,
number,
object,
optional,
pair,
pipeParsers,
string,
trio,
union,
} from "../general-purpose-parsers";
import {convert} from "../general-purpose-parsers/convert";
import {defaulted} from "../general-purpose-parsers/defaulted";
import {discriminatedUnionOn} from "../general-purpose-parsers/discriminated-union";
import {parsePerseusImageBackground} from "./perseus-image-background";
import {parseWidget} from "./widget";
import type {PerseusGraphTypeLinear} from "../../data-schema";
// Used to represent 2-D points and ranges
const pairOfNumbers = pair(number, number);
const parsePerseusGraphTypeAngle = object({
type: constant("angle"),
showAngles: optional(boolean),
allowReflexAngles: optional(boolean),
angleOffsetDeg: optional(nullable(number)),
snapDegrees: optional(number),
match: optional(constant("congruent")),
coords: optional(trio(pairOfNumbers, pairOfNumbers, pairOfNumbers)),
startCoords: optional(trio(pairOfNumbers, pairOfNumbers, pairOfNumbers)),
pointLabels: optional(trio(string, string, string)),
});
const parsePerseusGraphTypeCircle = object({
type: constant("circle"),
center: optional(pairOfNumbers),
radius: optional(number),
startCoords: optional(
object({
center: pairOfNumbers,
radius: number,
}),
),
pointLabels: optional(array(string)),
});
const parsePerseusGraphTypeLinear = object({
type: constant("linear"),
coords: optional(nullable(pair(pairOfNumbers, pairOfNumbers))),
startCoords: optional(pair(pairOfNumbers, pairOfNumbers)),
pointLabels: optional(pair(string, string)),
});
const parsePerseusGraphTypeLinearSystem = object({
type: constant("linear-system"),
// TODO(benchristel): default coords to empty array?
coords: optional(nullable(array(pair(pairOfNumbers, pairOfNumbers)))),
startCoords: optional(array(pair(pairOfNumbers, pairOfNumbers))),
pointLabels: optional(array(string)),
});
const parsePerseusGraphTypeNone = object({
type: constant("none"),
});
const parsePerseusGraphTypePoint = object({
type: constant("point"),
numPoints: optional(union(number).or(constant("unlimited")).parser),
coords: optional(nullable(array(pairOfNumbers))),
startCoords: optional(array(pairOfNumbers)),
coord: optional(pairOfNumbers),
pointLabels: optional(array(string)),
});
const parsePerseusGraphTypePolygon = object({
type: constant("polygon"),
numSides: optional(union(number).or(constant("unlimited")).parser),
showAngles: optional(boolean),
showSides: optional(boolean),
snapTo: optional(enumeration("grid", "angles", "sides")),
match: optional(enumeration("similar", "congruent", "approx", "exact")),
startCoords: optional(array(pairOfNumbers)),
coords: optional(nullable(array(pairOfNumbers))),
pointLabels: optional(array(string)),
});
const parsePerseusGraphTypeQuadratic = object({
type: constant("quadratic"),
coords: optional(
nullable(trio(pairOfNumbers, pairOfNumbers, pairOfNumbers)),
),
startCoords: optional(trio(pairOfNumbers, pairOfNumbers, pairOfNumbers)),
pointLabels: optional(trio(string, string, string)),
});
const parsePerseusGraphTypeRay = object({
type: constant("ray"),
coords: optional(nullable(pair(pairOfNumbers, pairOfNumbers))),
startCoords: optional(pair(pairOfNumbers, pairOfNumbers)),
pointLabels: optional(pair(string, string)),
});
const parsePerseusGraphTypeSegment = object({
type: constant("segment"),
// TODO(benchristel): default numSegments?
numSegments: optional(number),
coords: optional(nullable(array(pair(pairOfNumbers, pairOfNumbers)))),
startCoords: optional(array(pair(pairOfNumbers, pairOfNumbers))),
pointLabels: optional(array(string)),
});
const parsePerseusGraphTypeSinusoid = object({
type: constant("sinusoid"),
coords: optional(nullable(array(pairOfNumbers))),
startCoords: optional(array(pairOfNumbers)),
pointLabels: optional(array(string)),
});
const parsePerseusGraphTypeExponential = object({
type: constant("exponential"),
coords: optional(nullable(array(pairOfNumbers))),
asymptote: optional(nullable(number)),
startCoords: optional(
object({
coords: pair(pairOfNumbers, pairOfNumbers),
asymptote: number,
}),
),
pointLabels: optional(array(string)),
});
const parsePerseusGraphTypeAbsoluteValue = object({
type: constant("absolute-value"),
coords: optional(nullable(pair(pairOfNumbers, pairOfNumbers))),
startCoords: optional(pair(pairOfNumbers, pairOfNumbers)),
pointLabels: optional(pair(string, string)),
});
const parsePerseusGraphTypeTangent = object({
type: constant("tangent"),
coords: optional(nullable(array(pairOfNumbers))),
startCoords: optional(array(pairOfNumbers)),
pointLabels: optional(array(string)),
});
const parsePerseusGraphTypeLogarithm = object({
type: constant("logarithm"),
coords: optional(nullable(array(pairOfNumbers))),
asymptote: optional(nullable(number)),
startCoords: optional(
object({
coords: pair(pairOfNumbers, pairOfNumbers),
asymptote: number,
}),
),
pointLabels: optional(array(string)),
});
const parsePerseusGraphTypeVector = object({
type: constant("vector"),
coords: optional(nullable(pair(pairOfNumbers, pairOfNumbers))),
startCoords: optional(pair(pairOfNumbers, pairOfNumbers)),
match: optional(enumeration("exact", "congruent")),
});
export const parsePerseusGraphType = discriminatedUnionOn("type")
.withBranch("absolute-value", parsePerseusGraphTypeAbsoluteValue)
.withBranch("angle", parsePerseusGraphTypeAngle)
.withBranch("circle", parsePerseusGraphTypeCircle)
.withBranch("exponential", parsePerseusGraphTypeExponential)
.withBranch("linear", parsePerseusGraphTypeLinear)
.withBranch("linear-system", parsePerseusGraphTypeLinearSystem)
.withBranch("none", parsePerseusGraphTypeNone)
.withBranch("point", parsePerseusGraphTypePoint)
.withBranch("polygon", parsePerseusGraphTypePolygon)
.withBranch("quadratic", parsePerseusGraphTypeQuadratic)
.withBranch("ray", parsePerseusGraphTypeRay)
.withBranch("segment", parsePerseusGraphTypeSegment)
.withBranch("sinusoid", parsePerseusGraphTypeSinusoid)
.withBranch("tangent", parsePerseusGraphTypeTangent)
.withBranch("logarithm", parsePerseusGraphTypeLogarithm)
.withBranch("vector", parsePerseusGraphTypeVector).parser;
const parseLockedFigureColor = enumeration(...lockedFigureColorNames);
const parseLockedFigureFillType = enumeration(
"none",
"white",
"translucent",
"solid",
);
const parseLockedLineStyle = enumeration("solid", "dashed");
const parseStrokeWeight = defaulted(
enumeration("medium", "thin", "thick"),
() => "medium" as const,
);
const parseLockedLabelType = object({
type: constant("label"),
coord: pairOfNumbers,
text: string,
color: parseLockedFigureColor,
size: enumeration("small", "medium", "large"),
});
const parseLockedPointType = object({
type: constant("point"),
coord: pairOfNumbers,
color: parseLockedFigureColor,
filled: boolean,
labels: defaulted(array(parseLockedLabelType), () => []),
ariaLabel: optional(string),
});
const parseLockedLineType = object({
type: constant("line"),
kind: enumeration("line", "ray", "segment"),
points: pair(parseLockedPointType, parseLockedPointType),
color: parseLockedFigureColor,
lineStyle: parseLockedLineStyle,
showPoint1: defaulted(boolean, () => false),
showPoint2: defaulted(boolean, () => false),
weight: parseStrokeWeight,
labels: defaulted(array(parseLockedLabelType), () => []),
ariaLabel: optional(string),
});
const parseLockedVectorType = object({
type: constant("vector"),
points: pair(pairOfNumbers, pairOfNumbers),
color: parseLockedFigureColor,
weight: parseStrokeWeight,
labels: defaulted(array(parseLockedLabelType), () => []),
ariaLabel: optional(string),
});
const parseLockedEllipseType = object({
type: constant("ellipse"),
center: pairOfNumbers,
radius: pairOfNumbers,
angle: number,
color: parseLockedFigureColor,
fillStyle: parseLockedFigureFillType,
strokeStyle: parseLockedLineStyle,
weight: parseStrokeWeight,
labels: defaulted(array(parseLockedLabelType), () => []),
ariaLabel: optional(string),
});
const parseLockedPolygonPointType = object({
coord: pairOfNumbers,
});
const parseLockedPolygonPointsType = union(
array(parseLockedPolygonPointType),
).or(
// Normalize legacy representation of points as Coord[] to
// LockedPolygonPointType[].
pipeParsers(array(pairOfNumbers)).then(
convert((coords) => coords.map((coord) => ({coord}))),
).parser,
).parser;
const parseLockedPolygonType = object({
type: constant("polygon"),
points: parseLockedPolygonPointsType,
color: parseLockedFigureColor,
showVertices: boolean,
fillStyle: parseLockedFigureFillType,
strokeStyle: parseLockedLineStyle,
weight: parseStrokeWeight,
labels: defaulted(array(parseLockedLabelType), () => []),
ariaLabel: optional(string),
});
const numberOrNegativeInfinity = union(number).or(constant(-Infinity)).parser;
const numberOrPositiveInfinity = union(number).or(constant(Infinity)).parser;
// Exported for testing.
export const parseLockedFunctionDomain = defaulted(
pair(
// NOTE: Infinity does not serialize (becomes null), so we cannot assume
// that this function acts as a validator for the widget. This
// will ensure that parsed data is up-to-date, but it is quite
// probable that the data is serialized after being parsed. The
// widget should therefore account for null values in the domain.
defaulted(numberOrNegativeInfinity, () => -Infinity),
defaulted(numberOrPositiveInfinity, () => Infinity),
),
(): [number, number] => [-Infinity, Infinity],
);
const parseLockedFunctionType = object({
type: constant("function"),
color: parseLockedFigureColor,
strokeStyle: parseLockedLineStyle,
weight: parseStrokeWeight,
equation: string,
directionalAxis: enumeration("x", "y"),
domain: parseLockedFunctionDomain,
labels: defaulted(array(parseLockedLabelType), () => []),
ariaLabel: optional(string),
});
const parseLockedFigure = discriminatedUnionOn("type")
.withBranch("point", parseLockedPointType)
.withBranch("line", parseLockedLineType)
.withBranch("vector", parseLockedVectorType)
.withBranch("ellipse", parseLockedEllipseType)
.withBranch("polygon", parseLockedPolygonType)
.withBranch("function", parseLockedFunctionType)
.withBranch("label", parseLockedLabelType).parser;
const parseLabelLocation = union(enumeration("onAxis", "alongEdge")).or(
// If the labelLocation is an empty string, we default to "onAxis".
pipeParsers(constant("")).then(convert(() => "onAxis" as const)).parser,
).parser;
export const parseInteractiveGraphWidget = parseWidget(
constant("interactive-graph"),
object({
step: pairOfNumbers,
// TODO(benchristel): rather than making gridStep and snapStep
// optional, we should duplicate the defaulting logic from the
// InteractiveGraph component. See parse-perseus-json/README.md for
// why.
gridStep: optional(pairOfNumbers),
snapStep: optional(pairOfNumbers),
backgroundImage: optional(parsePerseusImageBackground),
markings: enumeration("graph", "grid", "none", "axes"),
labels: optional(array(string)),
labelLocation: optional(parseLabelLocation),
showProtractor: boolean,
showRuler: optional(boolean),
showTooltips: optional(boolean),
rulerLabel: optional(string),
rulerTicks: optional(number),
range: pair(pairOfNumbers, pairOfNumbers),
showAxisArrows: defaulted(
object({
xMin: boolean,
xMax: boolean,
yMin: boolean,
yMax: boolean,
}),
() => ({
xMin: true,
xMax: true,
yMin: true,
yMax: true,
}),
),
showAxisTicks: defaulted(
object({
x: boolean,
y: boolean,
}),
() => ({
x: true,
y: true,
}),
),
// NOTE(benchristel): I copied the default graph from
// interactive-graph.tsx. See the parse-perseus-json/README.md for
// an explanation of why we want to duplicate the default here.
graph: defaulted(
parsePerseusGraphType,
(): PerseusGraphTypeLinear => ({
type: "linear" as const,
}),
),
correct: defaulted(
parsePerseusGraphType,
(): PerseusGraphTypeLinear => ({
type: "linear" as const,
}),
),
lockedFigures: defaulted(array(parseLockedFigure), () => []),
fullGraphAriaLabel: optional(string),
fullGraphAriaDescription: optional(string),
}),
);