From a0fec4d9c7af59bbed6dd75d764bfdcec0007956 Mon Sep 17 00:00:00 2001 From: Haydn Ewers <27211+haydn@users.noreply.github.com> Date: Wed, 26 Jun 2019 16:03:07 +1000 Subject: [PATCH] Improve playground UI --- ulm-playground/src/App.tsx | 81 ++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/ulm-playground/src/App.tsx b/ulm-playground/src/App.tsx index a4a9092..46b74db 100644 --- a/ulm-playground/src/App.tsx +++ b/ulm-playground/src/App.tsx @@ -2,22 +2,35 @@ import React, { useState } from "react"; import katex from "katex"; import "./App.css"; -import { compile, toLatex } from "ulm-js"; +import { AST, compile, toLatex } from "ulm-js"; const useCompiler = ( initialCode: string -): [string, object, string, (newCode: string) => void] => { - const render = (newCode: string): [object, string] => { +): [ + string, + { ast?: AST; error?: string }, + { html?: string; error?: string }, + (newCode: string) => void +] => { + const render = ( + newCode: string + ): [{ ast?: AST; error?: string }, { html?: string; error?: string }] => { let newAst, newHtml; try { - newAst = compile(newCode); - newHtml = katex.renderToString(toLatex(newAst), { - throwOnError: false - }); + newAst = { ast: compile(newCode) }; + try { + newHtml = { + html: katex.renderToString(toLatex(newAst.ast), { + throwOnError: false + }) + }; + } catch (error) { + newHtml = { error: error.toString() }; + } } catch (error) { - newAst = { error }; - newHtml = ""; + newAst = { error: error.toString() }; + newHtml = { html: "" }; } return [newAst, newHtml]; @@ -43,7 +56,12 @@ const useCompiler = ( }; const App: React.FC = () => { - const [code, ast, html, update] = useCompiler( + const [ + code, + { ast, error: astError }, + { html, error: htmlError }, + update + ] = useCompiler( "NumericEqualRelation(\n NumericAdditionOperation(\n NumericAdditionOperation(0.1, 0.3),\n 0.2\n ),\n 3/5\n)" ); return ( @@ -52,7 +70,7 @@ const App: React.FC = () => {

Code