Co-authored-by: jackfiled <xcrenchangjun@outlook.com> Reviewed-on: PostGuard/Canon#48 Co-authored-by: Ichirinko <1621543655@qq.com> Co-committed-by: Ichirinko <1621543655@qq.com>
106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
import {AppBar, Button, Grid, Toolbar, Typography} from "@mui/material";
|
|
import {InputField} from "./InputField.tsx";
|
|
import {CSSProperties, useState} from "react";
|
|
import {OutputField} from "./OutputField.tsx";
|
|
import createClient from "openapi-fetch";
|
|
import * as openapi from '../openapi';
|
|
import {enqueueSnackbar} from "notistack";
|
|
|
|
const client = createClient<openapi.paths>();
|
|
|
|
interface outputData {
|
|
compiledCode: string,
|
|
id: string,
|
|
imageAddress: string,
|
|
sourceCode: string
|
|
}
|
|
|
|
export function Index() {
|
|
|
|
const [inputValue, setInputValue] = useState('');
|
|
const [outputValue, setOutputValue] = useState<outputData>({
|
|
compiledCode: "",
|
|
sourceCode: "",
|
|
id: "",
|
|
imageAddress: "pic/uncompiled.png"
|
|
});
|
|
//const {enqueueSnackbar} = useSnackbar();
|
|
const handleValueChange = (value: string) => {
|
|
setInputValue(value);
|
|
};
|
|
|
|
|
|
async function compilerButtonClick() {
|
|
console.log(inputValue)
|
|
const {data} = await client.POST("/api/Compiler", {
|
|
body: {
|
|
code: inputValue
|
|
}
|
|
})
|
|
console.log(data)
|
|
if (data != undefined) {
|
|
setOutputValue({
|
|
compiledCode: data.compiledCode,
|
|
sourceCode: data.sourceCode,
|
|
id: data.id,
|
|
imageAddress: data.imageAddress
|
|
})
|
|
enqueueSnackbar("编译成功", {variant: "success", anchorOrigin: {vertical: 'top', horizontal: 'right'}});
|
|
} else {
|
|
// error
|
|
enqueueSnackbar("编译失败", {variant: "error", anchorOrigin: {vertical: 'top', horizontal: 'right'}});
|
|
}
|
|
}
|
|
|
|
return <>
|
|
<div className={"title"}
|
|
style={titleClassCss}>
|
|
<AppBar style={{zIndex: 0}}>
|
|
<Toolbar style={{width: '100%'}}>
|
|
<Typography variant="h6">
|
|
任昌骏组编译器
|
|
</Typography>
|
|
<Button style={{
|
|
position: "absolute",
|
|
left: "50%",
|
|
transform: "translateX(-50%)",
|
|
fontSize: "medium",
|
|
}}
|
|
variant="outlined"
|
|
color="inherit"
|
|
onClick={compilerButtonClick}
|
|
>
|
|
编译
|
|
</Button>
|
|
</Toolbar>
|
|
</AppBar>
|
|
</div>
|
|
|
|
<div className={"content"}
|
|
style={contentClassCss}>
|
|
<Grid container spacing={2} style={{width: "100%", height: "100%"}}>
|
|
<Grid item xs={12} sm={6} style={{width: "100%", height: "100%"}}>
|
|
<InputField onValueChange={handleValueChange}>
|
|
</InputField>
|
|
</Grid>
|
|
<Grid item xs={12} sm={6} style={{width: "100%", height: "100%"}}>
|
|
<OutputField imgPath={outputValue.imageAddress}>
|
|
</OutputField>
|
|
</Grid>
|
|
</Grid>
|
|
</div>
|
|
</>
|
|
}
|
|
|
|
const titleClassCss: CSSProperties = {
|
|
position: "absolute",
|
|
height: "max-content",
|
|
width: "100%",
|
|
}
|
|
const contentClassCss: CSSProperties = {
|
|
position: "relative",
|
|
height: "88%",
|
|
top: "12%",
|
|
width: "100%",
|
|
}
|