@@ -7,7 +7,7 @@ import * as Solver from "@math-blocks/solver";
77import * as Typesetter from "@math-blocks/typesetter" ;
88import * as Tex from "@math-blocks/tex" ;
99import { 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" ;
1111import type { Font } from "@math-blocks/opentype" ;
1212import { macros } from "@math-blocks/tex" ;
1313
@@ -17,10 +17,18 @@ import Substeps from "./substeps";
1717
1818const 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
2533const 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
3845const 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