Centred canvas

This commit is contained in:
Dmitriy Shishkov 2021-09-05 11:46:40 +03:00
parent 5228bbae8f
commit 3e01632d1e
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
6 changed files with 38 additions and 33 deletions

View File

@ -10,15 +10,22 @@ import { Header } from "./components/Header";
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
root: { root: {
padding: theme.spacing(10, 2, 2, 2), padding: theme.spacing(10, 2, 2, 2),
boxSizing: "border-box",
height: "100vh", height: "100vh",
overflow: "hidden", width: "100vw",
margin: 0,
"& > div": { "& > div": {
height: "100%", height: "100%",
}, },
"& > div > *": { "& > div > div": {
height: "100%", height: "100%",
}, },
}, },
canvasContainer: {
display: "flex",
alignItems: "center",
justifyContent: "center",
},
})); }));
export const App = () => { export const App = () => {
@ -40,8 +47,13 @@ export const App = () => {
<AppTheme> <AppTheme>
<RoomContextProvider> <RoomContextProvider>
<Header /> <Header />
<Grid className={classes.root} container> <Grid spacing={2} className={classes.root} container>
<Grid ref={planContainer} item xs={9}> <Grid
className={classes.canvasContainer}
ref={planContainer}
item
xs={9}
>
<BuildingPlan width={canvasSize.w} height={canvasSize.h} /> <BuildingPlan width={canvasSize.w} height={canvasSize.h} />
</Grid> </Grid>
<Grid item xs={3}> <Grid item xs={3}>

View File

@ -5,7 +5,7 @@ import { useRoomContext } from "../context";
import { RoomDisplay } from "../types/room"; import { RoomDisplay } from "../types/room";
import { Canvas } from "./Canvas"; import { Canvas } from "./Canvas";
export type BuildingPlanProps = { width?: number; height?: number }; export type BuildingPlanProps = { width: number; height: number };
const getRoomByCoord = (x: number, y: number, map: RoomDisplay[]) => { const getRoomByCoord = (x: number, y: number, map: RoomDisplay[]) => {
for (let i = 0; i < map.length; i++) { for (let i = 0; i < map.length; i++) {
@ -53,7 +53,7 @@ export const BuildingPlan = ({ width, height }: BuildingPlanProps) => {
); );
}); });
}, },
[state.char, state.map] [state.char, state.map, state.boardSize]
); );
const clickCb = (x: number, y: number) => { const clickCb = (x: number, y: number) => {
@ -63,7 +63,12 @@ export const BuildingPlan = ({ width, height }: BuildingPlanProps) => {
return ( return (
<> <>
<Canvas clickCb={clickCb} height={height} width={width} draw={draw} /> <Canvas
clickCb={clickCb}
height={Math.min(height, state.boardSize.h)}
width={Math.min(width, state.boardSize.w)}
draw={draw}
/>
</> </>
); );
}; };

View File

@ -1,27 +1,8 @@
import { ContextData } from "./types/context"; import { ContextData } from "./types/context";
import { PeriodFormat } from "./types/time";
export const lessonPeriods: readonly PeriodFormat[] = [
"8:30-9:10",
"9:20-10:00",
"10:15-10:55",
"11:10-11:50",
"12:00-12:40",
"12:50-13:30",
] as const;
export const WeekDays = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
] as const;
export const defaultState: ContextData = { export const defaultState: ContextData = {
map: [], map: [],
char: [], char: [],
ids: {}, ids: {},
boardSize: { w: 0, h: 0 },
}; };

View File

@ -40,6 +40,9 @@ export const RoomContextProvider: FC = ({ children }) => {
char: RoomState[] = [], char: RoomState[] = [],
ids: Record<number, number> = {}; ids: Record<number, number> = {};
let maxWidth = 0,
maxHeight = 0;
for (const { for (const {
x, x,
y, y,
@ -56,9 +59,17 @@ export const RoomContextProvider: FC = ({ children }) => {
}); });
char.push({ free }); char.push({ free });
ids[id] = map.length - 1; ids[id] = map.length - 1;
maxHeight = Math.max(maxHeight, y + h);
maxWidth = Math.max(maxWidth, x + w);
} }
setState({ map, char, ids }); setState({
map,
char,
ids,
boardSize: { w: maxWidth, h: maxHeight },
});
} else if (isUpdateMessage(message)) { } else if (isUpdateMessage(message)) {
setState( setState(
produce((draft) => { produce((draft) => {

View File

@ -4,6 +4,7 @@ export interface ContextData {
map: RoomDisplay[]; map: RoomDisplay[];
char: RoomState[]; char: RoomState[];
ids: Record<number, number>; ids: Record<number, number>;
boardSize: { w: number; h: number };
} }
export type ContextValue = { export type ContextValue = {

View File

@ -1,5 +0,0 @@
import { WeekDays } from "../constants";
export type PeriodFormat = `${number}:${number}-${number}:${number}`;
export type WeekDay = typeof WeekDays[number];