import React, { useEffect, useState } from "react"; import { Flex, Box, Label, Input, Checkbox, IconButton } from "theme-ui"; import ExpandMoreIcon from "../../icons/ExpandMoreIcon"; import { isEmpty } from "../../helpers/shared"; import { getGridUpdatedInset } from "../../helpers/grid"; import { useDataURL } from "../../contexts/AssetsContext"; import { mapSources as defaultMapSources } from "../../maps"; import Divider from "../Divider"; import Select from "../Select"; const qualitySettings = [ { value: "low", label: "Low" }, { value: "medium", label: "Medium" }, { value: "high", label: "High" }, { value: "ultra", label: "Ultra High" }, { value: "original", label: "Original" }, ]; const gridTypeSettings = [ { value: "square", label: "Square" }, { value: "hexVertical", label: "Hex Vertical" }, { value: "hexHorizontal", label: "Hex Horizontal" }, ]; const gridSquareMeasurementTypeSettings = [ { value: "chebyshev", label: "Chessboard (D&D 5e)" }, { value: "alternating", label: "Alternating Diagonal (D&D 3.5e)" }, { value: "euclidean", label: "Euclidean" }, { value: "manhattan", label: "Manhattan" }, ]; const gridHexMeasurementTypeSettings = [ { value: "manhattan", label: "Manhattan" }, { value: "euclidean", label: "Euclidean" }, ]; function MapSettings({ map, mapState, onSettingsChange, onStateSettingsChange, showMore, onShowMoreChange, }) { function handleFlagChange(event, flag) { if (event.target.checked) { onStateSettingsChange("editFlags", [...mapState.editFlags, flag]); } else { onStateSettingsChange( "editFlags", mapState.editFlags.filter((f) => f !== flag) ); } } function handleGridSizeXChange(event) { const value = parseInt(event.target.value) || 0; let grid = { ...map.grid, size: { ...map.grid.size, x: value, }, }; grid.inset = getGridUpdatedInset(grid, map.width, map.height); onSettingsChange("grid", grid); } function handleGridSizeYChange(event) { const value = parseInt(event.target.value) || 0; let grid = { ...map.grid, size: { ...map.grid.size, y: value, }, }; grid.inset = getGridUpdatedInset(grid, map.width, map.height); onSettingsChange("grid", grid); } function handleGridTypeChange(option) { const type = option.value; let grid = { ...map.grid, type, measurement: { ...map.grid.measurement, type: type === "square" ? "chebyshev" : "manhattan", }, }; grid.inset = getGridUpdatedInset(grid, map.width, map.height); onSettingsChange("grid", grid); } function handleGridMeasurementTypeChange(option) { const grid = { ...map.grid, measurement: { ...map.grid.measurement, type: option.value, }, }; onSettingsChange("grid", grid); } function handleGridMeasurementScaleChange(event) { const grid = { ...map.grid, measurement: { ...map.grid.measurement, scale: event.target.value, }, }; onSettingsChange("grid", grid); } const mapURL = useDataURL(map, defaultMapSources); const [mapSize, setMapSize] = useState(0); useEffect(() => { async function updateMapSize() { if (mapURL) { const response = await fetch(mapURL); const blob = await response.blob(); let size = blob.size; size /= 1000000; // Bytes to Megabytes setMapSize(size.toFixed(2)); } else { setMapSize(0); } } updateMapSize(); }, [mapURL]); const mapEmpty = !map || isEmpty(map); const mapStateEmpty = !mapState || isEmpty(mapState); return ( onSettingsChange("name", e.target.value)} disabled={mapEmpty || map.type === "default"} my={1} /> {showMore && ( <> s.value === map.grid.measurement.type ) } onChange={handleGridMeasurementTypeChange} isSearchable={false} /> {!mapEmpty && map.type !== "default" && (