2020-04-15 21:15:16 +10:00
|
|
|
import React, { useRef, useState } from "react";
|
|
|
|
|
import {
|
|
|
|
|
Box,
|
|
|
|
|
Button,
|
|
|
|
|
Image as UIImage,
|
|
|
|
|
Flex,
|
|
|
|
|
Label,
|
|
|
|
|
Input,
|
|
|
|
|
Text,
|
|
|
|
|
} from "theme-ui";
|
2020-04-13 18:15:00 +10:00
|
|
|
|
|
|
|
|
import Modal from "../components/Modal";
|
|
|
|
|
|
|
|
|
|
function AddMapModal({
|
|
|
|
|
isOpen,
|
|
|
|
|
onRequestClose,
|
|
|
|
|
onDone,
|
|
|
|
|
onImageUpload,
|
2020-04-20 14:49:38 +10:00
|
|
|
gridX,
|
|
|
|
|
onGridXChange,
|
|
|
|
|
gridY,
|
|
|
|
|
onGridYChange,
|
2020-04-13 18:15:00 +10:00
|
|
|
imageLoaded,
|
|
|
|
|
mapSource,
|
|
|
|
|
}) {
|
|
|
|
|
const fileInputRef = useRef();
|
|
|
|
|
|
2020-04-20 16:34:38 +10:00
|
|
|
function handleImageUpload(file) {
|
|
|
|
|
if (file.name) {
|
|
|
|
|
// Match against a regex to find the grid size in the file name
|
|
|
|
|
// e.g. Cave 22x23 will return [["22x22", "22", "x", "23"]]
|
|
|
|
|
const gridMatches = [...file.name.matchAll(/(\d+) ?(x|X) ?(\d+)/g)];
|
|
|
|
|
if (gridMatches.length > 0) {
|
|
|
|
|
const lastMatch = gridMatches[gridMatches.length - 1];
|
|
|
|
|
const matchX = parseInt(lastMatch[1]);
|
|
|
|
|
const matchY = parseInt(lastMatch[3]);
|
|
|
|
|
if (!isNaN(matchX) && !isNaN(matchY)) {
|
|
|
|
|
onImageUpload(file, matchX, matchY);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
onImageUpload(file);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 18:15:00 +10:00
|
|
|
function openImageDialog() {
|
|
|
|
|
if (fileInputRef.current) {
|
|
|
|
|
fileInputRef.current.click();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-15 21:15:16 +10:00
|
|
|
|
|
|
|
|
const [dragging, setDragging] = useState(false);
|
|
|
|
|
function handleImageDragEnter(event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
setDragging(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleImageDragLeave(event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
setDragging(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleImageDrop(event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
const file = event.dataTransfer.files[0];
|
2020-04-15 21:57:18 +10:00
|
|
|
if (file && file.type.startsWith("image")) {
|
2020-04-20 16:34:38 +10:00
|
|
|
handleImageUpload(file);
|
2020-04-15 21:15:16 +10:00
|
|
|
}
|
|
|
|
|
setDragging(false);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 18:15:00 +10:00
|
|
|
return (
|
|
|
|
|
<Modal isOpen={isOpen} onRequestClose={onRequestClose}>
|
|
|
|
|
<Box
|
|
|
|
|
as="form"
|
|
|
|
|
onSubmit={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
onDone();
|
|
|
|
|
}}
|
2020-04-15 21:15:16 +10:00
|
|
|
onDragEnter={handleImageDragEnter}
|
2020-04-13 18:15:00 +10:00
|
|
|
>
|
|
|
|
|
<input
|
2020-04-20 16:34:38 +10:00
|
|
|
onChange={(event) => handleImageUpload(event.target.files[0])}
|
2020-04-13 18:15:00 +10:00
|
|
|
type="file"
|
|
|
|
|
accept="image/*"
|
|
|
|
|
style={{ display: "none" }}
|
|
|
|
|
ref={fileInputRef}
|
|
|
|
|
/>
|
|
|
|
|
|
2020-04-15 21:15:16 +10:00
|
|
|
<Flex
|
|
|
|
|
sx={{
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Label pt={2} pb={1}>
|
|
|
|
|
Add map
|
|
|
|
|
</Label>
|
2020-04-13 18:15:00 +10:00
|
|
|
<UIImage
|
|
|
|
|
my={2}
|
|
|
|
|
sx={{
|
|
|
|
|
width: "500px",
|
|
|
|
|
minHeight: "200px",
|
|
|
|
|
maxHeight: "300px",
|
|
|
|
|
objectFit: "contain",
|
|
|
|
|
borderRadius: "4px",
|
|
|
|
|
}}
|
|
|
|
|
src={mapSource}
|
|
|
|
|
onClick={openImageDialog}
|
|
|
|
|
bg="muted"
|
|
|
|
|
/>
|
|
|
|
|
<Flex>
|
|
|
|
|
<Box mb={2} mr={1} sx={{ flexGrow: 1 }}>
|
2020-04-20 14:49:38 +10:00
|
|
|
<Label htmlFor="gridX">Columns</Label>
|
2020-04-13 18:15:00 +10:00
|
|
|
<Input
|
|
|
|
|
type="number"
|
2020-04-20 14:49:38 +10:00
|
|
|
name="gridX"
|
|
|
|
|
value={gridX}
|
|
|
|
|
onChange={(e) => onGridXChange(e.target.value)}
|
2020-04-13 18:15:00 +10:00
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box mb={2} ml={1} sx={{ flexGrow: 1 }}>
|
2020-04-20 14:49:38 +10:00
|
|
|
<Label htmlFor="gridY">Rows</Label>
|
2020-04-13 18:15:00 +10:00
|
|
|
<Input
|
|
|
|
|
type="number"
|
2020-04-20 14:49:38 +10:00
|
|
|
name="gridY"
|
|
|
|
|
value={gridY}
|
|
|
|
|
onChange={(e) => onGridYChange(e.target.value)}
|
2020-04-13 18:15:00 +10:00
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
</Flex>
|
|
|
|
|
{mapSource ? (
|
|
|
|
|
<Button variant="primary" disabled={!imageLoaded}>
|
|
|
|
|
Done
|
|
|
|
|
</Button>
|
|
|
|
|
) : (
|
|
|
|
|
<Button
|
|
|
|
|
varient="primary"
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
openImageDialog();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Select Image
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
2020-04-15 21:15:16 +10:00
|
|
|
{dragging && (
|
|
|
|
|
<Flex
|
|
|
|
|
bg="muted"
|
|
|
|
|
sx={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
top: 0,
|
|
|
|
|
right: 0,
|
|
|
|
|
left: 0,
|
|
|
|
|
bottom: 0,
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
cursor: "copy",
|
|
|
|
|
}}
|
|
|
|
|
onDragLeave={handleImageDragLeave}
|
|
|
|
|
onDragOver={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
e.dataTransfer.dropEffect = "copy";
|
|
|
|
|
}}
|
|
|
|
|
onDrop={handleImageDrop}
|
|
|
|
|
>
|
|
|
|
|
<Text sx={{ pointerEvents: "none" }}>Drop map to upload</Text>
|
|
|
|
|
</Flex>
|
|
|
|
|
)}
|
2020-04-13 18:15:00 +10:00
|
|
|
</Flex>
|
|
|
|
|
</Box>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default AddMapModal;
|