Skip to content

Commit

Permalink
move handleFileChange to controls component and remove styled components
Browse files Browse the repository at this point in the history
  • Loading branch information
VeraZab committed Jan 9, 2024
1 parent b663776 commit 24290d3
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 47 deletions.
31 changes: 2 additions & 29 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ThemeProvider } from "@emotion/react";
import Box from "@mui/material/Box";
import { ChangeEvent, useRef, useState } from "react";
import { useRef, useState } from "react";

import { Theme } from "@mui/material";
import Canvas from "./components/Canvas";
Expand Down Expand Up @@ -40,33 +40,6 @@ const App: React.FC = () => {
url: null,
});

const handleFileChange = (event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files ? event.target.files[0] : null;
if (file && file.type.match("image.*")) {
const imageUrl = URL.createObjectURL(file);
setImageState((prev) => ({
...prev,
url: imageUrl,
fileName: file.name.replace(".png", ""),
}));

const img = new Image();

img.onload = () => {
setTileState({
originalHeight: img.naturalHeight,
originalWidth: img.naturalWidth,
height: img.naturalHeight,
width: img.naturalWidth,
});

setImageState((prev) => ({ ...prev, image: img }));
};

img.src = imageUrl;
}
};

return (
<ThemeProvider theme={theme}>
<Layout>
Expand All @@ -86,7 +59,7 @@ const App: React.FC = () => {
tileState={tileState}
setTileState={setTileState}
imageState={imageState}
handleFileChange={handleFileChange}
setImageState={setImageState}
/>
</Box>
</Layout>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const Canvas: React.FC<CanvasProps> = ({ canvasRef, tileState, imageState, canva
canvasRef.current.width = canvasState.width;
canvasRef.current.height = canvasState.height;
}
}, [canvasState, canvasRef]);
}, [canvasState.height, canvasState.width, canvasRef]);

useEffect(() => {
if (imageState.image && canvasRef.current) {
Expand All @@ -39,7 +39,7 @@ const Canvas: React.FC<CanvasProps> = ({ canvasRef, tileState, imageState, canva
ctx.drawImage(imageState.image, 0, 0, tileState.width, tileState.height);
}
}
}, [imageState.image, canvasRef, tileState, canvasState]);
}, [imageState.image, canvasRef, tileState, canvasState.width, canvasState.height]);

return (
<Box
Expand Down
41 changes: 39 additions & 2 deletions src/components/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import CloudUploadIcon from "@mui/icons-material/CloudUpload";
import ReplayIcon from "@mui/icons-material/Replay";
import { Box, Button, IconButton, InputAdornment, TextField } from "@mui/material";
import { Theme } from "@mui/material/styles";
import { ChangeEvent } from "react";

import { CanvasDimensions } from "../types/appState";
import { ControlsProps } from "../types/controls";
Expand Down Expand Up @@ -58,8 +59,35 @@ const Controls: React.FC<ControlsProps> = ({
tileState,
setTileState,
imageState,
handleFileChange,
setImageState
}) => {
const handleFileChange = (event: ChangeEvent<HTMLInputElement>) => {
const file = event.target.files ? event.target.files[0] : null;
if (file && file.type.match("image.*")) {
const imageUrl = URL.createObjectURL(file);
setImageState((prev) => ({
...prev,
url: imageUrl,
fileName: file.name.replace(".png", ""),
}));

const img = new Image();

img.onload = () => {
setTileState({
originalHeight: img.naturalHeight,
originalWidth: img.naturalWidth,
height: img.naturalHeight,
width: img.naturalWidth,
});

setImageState((prev) => ({ ...prev, image: img }));
};

img.src = imageUrl;
}
};

const tile = () => {
if (imageState.url && canvasRef.current) {
const img = new Image();
Expand Down Expand Up @@ -139,7 +167,7 @@ const Controls: React.FC<ControlsProps> = ({
>
<Button component="label" variant="contained" startIcon={<CloudUploadIcon />}>
Upload tile
<ImageInput type="file" onChange={handleFileChange} />
<ImageInput onChange={handleFileChange} />
</Button>
</Box>

Expand All @@ -165,7 +193,9 @@ const Controls: React.FC<ControlsProps> = ({
}
}}
/>

<Box sx={controlsTextStyles}>X</Box>

<TextField
value={canvasState.height}
variant="standard"
Expand All @@ -186,8 +216,10 @@ const Controls: React.FC<ControlsProps> = ({
}
}}
/>

<Box sx={controlsTextStyles}>Canvas Size</Box>
</Box>

<Box sx={controlStyles}>
<TextField
value={tileState.width}
Expand All @@ -208,7 +240,9 @@ const Controls: React.FC<ControlsProps> = ({
}
}}
/>

<Box sx={controlsTextStyles}>X</Box>

<TextField
value={tileState.height}
variant="standard"
Expand All @@ -228,7 +262,9 @@ const Controls: React.FC<ControlsProps> = ({
}
}}
/>

<Box sx={controlsTextStyles}>Tile Size</Box>

<IconButton
onClick={() => {
setTileState((prev) => ({
Expand All @@ -252,6 +288,7 @@ const Controls: React.FC<ControlsProps> = ({
Tile
</Button>
</Box>

<Box>
<Button variant="contained" onClick={download} disableElevation>
Download
Expand Down
2 changes: 2 additions & 0 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const Header: React.FC = () => {
src="pattern-tiler-logo.png"
style={logoStyles}
/>

<Box
sx={appTitleStyles}
>
Expand All @@ -53,6 +54,7 @@ const Header: React.FC = () => {
>
About this tool
</Box>

<HelpIcon />
</Box>
</Tooltip>
Expand Down
31 changes: 19 additions & 12 deletions src/components/ImageInput.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { styled } from "@mui/material/styles";
import { ImageInputProps } from "../types/imageInput";

const ImageInput = styled("input")({
clip: "rect(0 0 0 0)",
clipPath: "inset(50%)",
height: 1,
overflow: "hidden",
position: "absolute",
bottom: 0,
left: 0,
whiteSpace: "nowrap",
width: 1,
});
const ImageInput: React.FC<ImageInputProps> = ({ onChange }) => (
<input
type="file"
style={
{
clip: "rect(0 0 0 0)",
clipPath: "inset(50%)",
height: 1,
overflow: "hidden",
position: "absolute",
bottom: 0,
left: 0,
whiteSpace: "nowrap",
width: 1,
}
}
onChange={onChange} />
)

export default ImageInput;
2 changes: 2 additions & 0 deletions src/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const Layout: React.FC<LayoutProps> = ({ children }) => {
sx={layoutContainerStyles}
>
<Header />

{children}

<Footer />
</Box>
);
Expand Down
4 changes: 2 additions & 2 deletions src/types/controls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeEvent } from "react";
import React, { ChangeEvent } from "react";

import { CanvasDimensions, ImageAttributes, TileDimensions } from "../types/appState";

Expand All @@ -9,5 +9,5 @@ export interface ControlsProps {
tileState: TileDimensions;
setTileState: React.Dispatch<React.SetStateAction<TileDimensions>>;
imageState: ImageAttributes;
handleFileChange: (event: ChangeEvent<HTMLInputElement>) => void;
setImageState: React.Dispatch<React.SetStateAction<ImageAttributes>>;
}
5 changes: 5 additions & 0 deletions src/types/imageInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ChangeEvent } from "react";

export interface ImageInputProps {
onChange: (e: ChangeEvent<HTMLInputElement>) => void;
};

0 comments on commit 24290d3

Please sign in to comment.