Skip to content

Commit

Permalink
Demo sandbox milestone 1 (#18)
Browse files Browse the repository at this point in the history
* set up webpack and package.json
* set up material ui (mui) components
* refactor, prepare type definitions
* disable dev overlay, remove validation TODOs, add error boundary, refactor
* address comments
  • Loading branch information
TheTallJerry authored Feb 12, 2024
1 parent fe4fd3b commit 07ec7a7
Show file tree
Hide file tree
Showing 7 changed files with 466 additions and 56 deletions.
7 changes: 5 additions & 2 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Demo website for memory-models-rough",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build-dev": "webpack"
"build-dev": "webpack",
"start": "webpack serve"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -33,8 +34,10 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"@mui/material": "^5.15.7",
"@picocss/pico": "^1.5.10",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-error-boundary": "^4.0.12"
}
}
47 changes: 25 additions & 22 deletions demo/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
import React from "react";
import React, { useState } from "react";
import SvgDisplay from "./SvgDisplay";

const DEMO_OBJECTS = [
{
isClass: true,
name: "__main__",
id: null,
value: { lst1: 82, lst2: 84, p: 99, d: 10, t: 11 },
stack_frame: true,
},
{
isClass: false,
name: "str",
id: 19,
value: "David is cool!",
style: ["highlight"],
},
{ isClass: false, name: "int", id: 13, value: 7 },
];
import MemoryModelsUserInput from "./MemoryModels";
import { ErrorBoundary } from "react-error-boundary";

export default function App() {
const [formData, setFormData] = useState("");
const [jsonResult, setJsonResult] = useState(null);

const onSubmit = async (event) => {
event.preventDefault();
try {
setJsonResult(JSON.parse(formData));
} catch (error) {
console.error("Error parsing inputted JSON: ", error);
setJsonResult(null);
}
};

return (
<>
<MemoryModelsUserInput
formData={formData}
setFormData={setFormData}
onSubmit={onSubmit}
/>
<section>
<h2>Input</h2>
<pre>{JSON.stringify(DEMO_OBJECTS)}</pre>
<h2>Output</h2>
<ErrorBoundary fallback={<div>Something went wrong</div>}>
<SvgDisplay jsonResult={jsonResult} />
</ErrorBoundary>
</section>
<SvgDisplay />
</>
);
}
46 changes: 46 additions & 0 deletions demo/src/MemoryModels.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from "react";
import { Button, Card, CardContent, TextField, Grid } from "@mui/material";

export default function MemoryModelsUserInput(props) {
const handleTextFieldChange = (event) => {
props.setFormData(event.target.value);
};

return (
<form onSubmit={props.onSubmit}>
<Card>
<CardContent>
<Grid container spacing={2}>
<Grid item xs={12}>
<TextField
id="multiline-textfield"
label="Enter memory model JSON here"
multiline
fullWidth
rows={10}
maxRows={20}
variant="outlined"
value={props.formData}
onChange={handleTextFieldChange}
style={{
width: "100%",
fontFamily: "Monospace",
}}
/>
</Grid>
<Grid item xs={12}>
<Button
type="submit"
variant="contained"
color="primary"
style={{ textTransform: "none" }}
>
Draw Diagram
</Button>
</Grid>
</Grid>
</CardContent>
</Card>
</form>
);
}
34 changes: 9 additions & 25 deletions demo/src/SvgDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
import React, { useRef, useEffect } from "react";
import mem from "../../src/index"; // TODO: replace with import of the package after it's been published

const DEMO_OBJECTS = [
{
isClass: true,
name: "__main__",
id: null,
value: { lst1: 82, lst2: 84, p: 99, d: 10, t: 11 },
stack_frame: true,
},
{
isClass: false,
name: "str",
id: 19,
value: "David is cool!",
style: ["highlight"],
},
{ isClass: false, name: "int", id: 13, value: 7 },
];

export default function SvgDisplay() {
export default function SvgDisplay(props) {
const canvasRef = useRef(null);

useEffect(() => {
const m = mem.draw(DEMO_OBJECTS, true, { width: 1300 });
m.render(canvasRef.current);
}, []);
if (props.jsonResult !== null) {
const m = mem.draw(props.jsonResult, true, { width: 1300 });
m.clear(canvasRef.current);
m.render(canvasRef.current);
}
}, [props.jsonResult]);

return (
<section>
<h2>Output</h2>
<>
<canvas ref={canvasRef} width={1000} height={1000} />;
</section>
</>
);
}
10 changes: 10 additions & 0 deletions demo/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ module.exports = [
path: path.resolve(__dirname, "dist"),
filename: "index.js",
},
devServer: {
static: {
directory: path.join(__dirname, "dist"),
},
compress: true,
port: 9000,
client: {
overlay: false,
},
},
module: {
rules: [
{
Expand Down
Loading

0 comments on commit 07ec7a7

Please sign in to comment.