Skip to content

Commit 248e188

Browse files
Update dropdown on solver page to switch between example problems (#4)
1 parent 4bc887b commit 248e188

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/solver/solver-page.tsx

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as Solver from "@math-blocks/solver";
77
import * as Typesetter from "@math-blocks/typesetter";
88
import * as Tex from "@math-blocks/tex";
99
import { MathEditor, MathRenderer, FontDataContext } from "@math-blocks/react";
10-
import { getFontData, parse } from "@math-blocks/opentype";
10+
import { getFontData, parse as parseFont } from "@math-blocks/opentype";
1111
import type { Font } from "@math-blocks/opentype";
1212
import { macros } from "@math-blocks/tex";
1313

@@ -17,10 +17,18 @@ import Substeps from "./substeps";
1717

1818
const operators = Object.keys(macros).filter((key) => key === macros[key]);
1919

20-
// const parser = new Tex.Parser('x^2 + 5x + 6 = 0');
21-
// const parser = new Tex.Parser('2x + 3y \u2212 7 = x \u2212 y + 1');
22-
const parser = new Tex.Parser("3x \u2212 y = 6, x + 2y = \u22121");
23-
const initialInput: Editor.types.CharRow = parser.parse();
20+
const parse = (input: string): Editor.types.CharRow => {
21+
const parser = new Tex.Parser(input);
22+
return parser.parse();
23+
};
24+
25+
const initialInputs: Record<Solver.Problem["type"], Editor.types.CharRow> = {
26+
Factor: parse("3x^2 + 11x + 6"),
27+
SimplifyExpression: parse("2x + 3x"),
28+
SolveLinearRelation: parse("2x + 3y \u2212 7 = x \u2212 y + 1"),
29+
SolveQuadraticEquation: parse("x^2 + 5x + 6 = 0"),
30+
SolveSystemOfEquations: parse("3x \u2212 y = 6, x + 2y = \u22121"),
31+
};
2432

2533
const safeParse = (input: Editor.types.CharRow): Semantic.types.Node | null => {
2634
try {
@@ -31,18 +39,18 @@ const safeParse = (input: Editor.types.CharRow): Semantic.types.Node | null => {
3139
};
3240

3341
// TODO:
34-
// - provide a UI disclosing sub-steps
3542
// - use the colorMap option to highlight related nodes between steps
3643
// e.g. 2(x + y) -> 2x + 2y the 2s would be the same color, etc.
3744

3845
const SolverPage = () => {
46+
const [action, setAction] = React.useState<Solver.Problem["type"]>(
47+
"SolveSystemOfEquations"
48+
);
49+
const [initialInput, setInitialInput] = React.useState(initialInputs[action]);
3950
const [input, setInput] = React.useState(initialInput);
4051
const [answer, setAnswer] = React.useState<Editor.types.CharRow | null>(null);
4152
const [step, setStep] = React.useState<Solver.Step | null>(null);
4253
const [error, setError] = React.useState<string | null>(null);
43-
const [action, setAction] = React.useState<Solver.Problem["type"]>(
44-
"SolveSystemOfEquations"
45-
);
4654

4755
const ast = React.useMemo(() => safeParse(input), [input]);
4856

@@ -161,7 +169,7 @@ const SolverPage = () => {
161169
const loadFont = async (): Promise<void> => {
162170
const res = await fetch(stix2);
163171
const blob = await res.blob();
164-
const font = await parse(blob as Blob);
172+
const font = await parseFont(blob as Blob);
165173
console.log(font);
166174
setFont(font);
167175
};
@@ -211,9 +219,14 @@ const SolverPage = () => {
211219
<select
212220
style={{ marginRight: 8 }}
213221
value={action}
214-
onChange={(event) =>
215-
setAction(event.target.value as Solver.Problem["type"])
216-
}
222+
onChange={(event) => {
223+
const problemType = event.target
224+
.value as Solver.Problem["type"];
225+
setAction(problemType);
226+
setInitialInput(initialInputs[problemType]);
227+
setInput(initialInputs[problemType]);
228+
setAnswer(null);
229+
}}
217230
>
218231
<option value="Factor">Factor</option>
219232
<option value="SimplifyExpression">Simplify Expression</option>

0 commit comments

Comments
 (0)