2020-03-20 17:56:34 +11:00
|
|
|
import React, { useRef, useState, useEffect } from "react";
|
|
|
|
|
import {
|
|
|
|
|
IconButton,
|
|
|
|
|
Box,
|
|
|
|
|
Button,
|
|
|
|
|
Image as UIImage,
|
|
|
|
|
Flex,
|
|
|
|
|
Label,
|
|
|
|
|
Input
|
|
|
|
|
} from "theme-ui";
|
2020-03-20 14:48:46 +11:00
|
|
|
|
|
|
|
|
import Modal from "./Modal";
|
|
|
|
|
|
|
|
|
|
const defaultMapSize = 22;
|
2020-03-19 17:33:57 +11:00
|
|
|
|
2020-03-20 18:06:24 +11:00
|
|
|
function AddMapButton({ onMapChanged }) {
|
2020-03-19 17:33:57 +11:00
|
|
|
const fileInputRef = useRef();
|
|
|
|
|
|
2020-03-20 14:49:40 +11:00
|
|
|
function openImageDialog() {
|
2020-03-19 17:33:57 +11:00
|
|
|
if (fileInputRef.current) {
|
|
|
|
|
fileInputRef.current.click();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 14:48:46 +11:00
|
|
|
const [isAddModalOpen, setIsAddModalOpen] = useState(false);
|
|
|
|
|
function openModal() {
|
|
|
|
|
setIsAddModalOpen(true);
|
|
|
|
|
}
|
|
|
|
|
function closeModal() {
|
|
|
|
|
setIsAddModalOpen(false);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 17:56:34 +11:00
|
|
|
const [imageLoaded, setImageLoaded] = useState(false);
|
|
|
|
|
|
2020-03-20 14:48:46 +11:00
|
|
|
const mapDataRef = useRef(null);
|
2020-03-20 17:56:34 +11:00
|
|
|
const [mapSource, setMapSource] = useState(null);
|
2020-03-20 14:48:46 +11:00
|
|
|
function handleImageUpload(event) {
|
2020-03-20 17:56:34 +11:00
|
|
|
const file = event.target.files[0];
|
|
|
|
|
const url = URL.createObjectURL(file);
|
|
|
|
|
let image = new Image();
|
|
|
|
|
image.onload = function() {
|
|
|
|
|
mapDataRef.current = {
|
|
|
|
|
file,
|
|
|
|
|
rows,
|
|
|
|
|
columns,
|
|
|
|
|
width: image.width,
|
|
|
|
|
height: image.height
|
|
|
|
|
};
|
|
|
|
|
setImageLoaded(true);
|
|
|
|
|
};
|
|
|
|
|
image.src = url;
|
|
|
|
|
setMapSource(url);
|
2020-03-20 14:48:46 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleDone() {
|
|
|
|
|
if (mapDataRef.current && mapSource) {
|
2020-03-20 18:06:24 +11:00
|
|
|
onMapChanged(mapDataRef.current, mapSource);
|
2020-03-20 14:48:46 +11:00
|
|
|
}
|
|
|
|
|
closeModal();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [rows, setRows] = useState(defaultMapSize);
|
|
|
|
|
const [columns, setColumns] = useState(defaultMapSize);
|
2020-03-20 17:56:34 +11:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (mapDataRef.current) {
|
|
|
|
|
mapDataRef.current.rows = rows;
|
|
|
|
|
mapDataRef.current.columns = columns;
|
|
|
|
|
}
|
|
|
|
|
}, [rows, columns]);
|
2020-03-20 14:48:46 +11:00
|
|
|
|
2020-03-19 17:33:57 +11:00
|
|
|
return (
|
|
|
|
|
<>
|
2020-03-20 14:48:46 +11:00
|
|
|
<IconButton aria-label="Add Map" onClick={openModal}>
|
2020-03-19 17:33:57 +11:00
|
|
|
<svg
|
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
|
height="24"
|
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
|
width="24"
|
2020-03-19 18:57:33 +11:00
|
|
|
fill="currentcolor"
|
2020-03-19 17:33:57 +11:00
|
|
|
>
|
|
|
|
|
<path d="M0 0h24v24H0V0z" fill="none" />
|
|
|
|
|
<path d="M21.02 5H19V2.98c0-.54-.44-.98-.98-.98h-.03c-.55 0-.99.44-.99.98V5h-2.01c-.54 0-.98.44-.99.98v.03c0 .55.44.99.99.99H17v2.01c0 .54.44.99.99.98h.03c.54 0 .98-.44.98-.98V7h2.02c.54 0 .98-.44.98-.98v-.04c0-.54-.44-.98-.98-.98zM16 9.01V8h-1.01c-.53 0-1.03-.21-1.41-.58-.37-.38-.58-.88-.58-1.44 0-.36.1-.69.27-.98H5c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2v-8.28c-.3.17-.64.28-1.02.28-1.09-.01-1.98-.9-1.98-1.99zM15.96 19H6c-.41 0-.65-.47-.4-.8l1.98-2.63c.21-.28.62-.26.82.02L10 18l2.61-3.48c.2-.26.59-.27.79-.01l2.95 3.68c.26.33.03.81-.39.81z" />
|
|
|
|
|
</svg>
|
|
|
|
|
</IconButton>
|
2020-03-20 14:48:46 +11:00
|
|
|
<Modal isOpen={isAddModalOpen} onRequestClose={closeModal}>
|
|
|
|
|
<Box
|
|
|
|
|
as="form"
|
|
|
|
|
onSubmit={e => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
handleDone();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<input
|
|
|
|
|
onChange={handleImageUpload}
|
|
|
|
|
type="file"
|
|
|
|
|
accept="image/*"
|
|
|
|
|
style={{ display: "none" }}
|
|
|
|
|
ref={fileInputRef}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Flex sx={{ flexDirection: "column" }}>
|
2020-03-20 17:56:34 +11:00
|
|
|
<UIImage
|
2020-03-20 14:48:46 +11:00
|
|
|
my={2}
|
|
|
|
|
sx={{
|
|
|
|
|
width: "500px",
|
|
|
|
|
minHeight: "200px",
|
|
|
|
|
maxHeight: "500px",
|
|
|
|
|
objectFit: "contain",
|
|
|
|
|
borderRadius: "4px"
|
|
|
|
|
}}
|
|
|
|
|
src={mapSource}
|
|
|
|
|
onClick={openImageDialog}
|
|
|
|
|
bg="muted"
|
|
|
|
|
/>
|
|
|
|
|
<Flex>
|
|
|
|
|
<Box mb={2} mr={1} sx={{ flexGrow: 1 }}>
|
|
|
|
|
<Label htmlFor="rows">Rows</Label>
|
|
|
|
|
<Input
|
|
|
|
|
type="number"
|
|
|
|
|
name="rows"
|
|
|
|
|
value={rows}
|
|
|
|
|
onChange={e => setRows(e.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
<Box mb={2} ml={1} sx={{ flexGrow: 1 }}>
|
|
|
|
|
<Label htmlFor="columns">Columns</Label>
|
|
|
|
|
<Input
|
|
|
|
|
type="number"
|
|
|
|
|
name="columns"
|
|
|
|
|
value={columns}
|
|
|
|
|
onChange={e => setColumns(e.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
</Flex>
|
|
|
|
|
{mapSource ? (
|
2020-03-20 17:56:34 +11:00
|
|
|
<Button variant="primary" disabled={!imageLoaded}>
|
|
|
|
|
Done
|
|
|
|
|
</Button>
|
2020-03-20 14:48:46 +11:00
|
|
|
) : (
|
|
|
|
|
<Button
|
|
|
|
|
varient="primary"
|
|
|
|
|
onClick={e => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
openImageDialog();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Select Image
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</Flex>
|
|
|
|
|
</Box>
|
|
|
|
|
</Modal>
|
2020-03-19 17:33:57 +11:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default AddMapButton;
|