@@ -7,7 +7,7 @@ import * as Solver from "@math-blocks/solver";
7
7
import * as Typesetter from "@math-blocks/typesetter" ;
8
8
import * as Tex from "@math-blocks/tex" ;
9
9
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" ;
11
11
import type { Font } from "@math-blocks/opentype" ;
12
12
import { macros } from "@math-blocks/tex" ;
13
13
@@ -17,10 +17,18 @@ import Substeps from "./substeps";
17
17
18
18
const operators = Object . keys ( macros ) . filter ( ( key ) => key === macros [ key ] ) ;
19
19
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
+ } ;
24
32
25
33
const safeParse = ( input : Editor . types . CharRow ) : Semantic . types . Node | null => {
26
34
try {
@@ -31,18 +39,18 @@ const safeParse = (input: Editor.types.CharRow): Semantic.types.Node | null => {
31
39
} ;
32
40
33
41
// TODO:
34
- // - provide a UI disclosing sub-steps
35
42
// - use the colorMap option to highlight related nodes between steps
36
43
// e.g. 2(x + y) -> 2x + 2y the 2s would be the same color, etc.
37
44
38
45
const SolverPage = ( ) => {
46
+ const [ action , setAction ] = React . useState < Solver . Problem [ "type" ] > (
47
+ "SolveSystemOfEquations"
48
+ ) ;
49
+ const [ initialInput , setInitialInput ] = React . useState ( initialInputs [ action ] ) ;
39
50
const [ input , setInput ] = React . useState ( initialInput ) ;
40
51
const [ answer , setAnswer ] = React . useState < Editor . types . CharRow | null > ( null ) ;
41
52
const [ step , setStep ] = React . useState < Solver . Step | null > ( null ) ;
42
53
const [ error , setError ] = React . useState < string | null > ( null ) ;
43
- const [ action , setAction ] = React . useState < Solver . Problem [ "type" ] > (
44
- "SolveSystemOfEquations"
45
- ) ;
46
54
47
55
const ast = React . useMemo ( ( ) => safeParse ( input ) , [ input ] ) ;
48
56
@@ -161,7 +169,7 @@ const SolverPage = () => {
161
169
const loadFont = async ( ) : Promise < void > => {
162
170
const res = await fetch ( stix2 ) ;
163
171
const blob = await res . blob ( ) ;
164
- const font = await parse ( blob as Blob ) ;
172
+ const font = await parseFont ( blob as Blob ) ;
165
173
console . log ( font ) ;
166
174
setFont ( font ) ;
167
175
} ;
@@ -211,9 +219,14 @@ const SolverPage = () => {
211
219
< select
212
220
style = { { marginRight : 8 } }
213
221
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
+ } }
217
230
>
218
231
< option value = "Factor" > Factor</ option >
219
232
< option value = "SimplifyExpression" > Simplify Expression</ option >
0 commit comments