From 3bd8fce6ab1d2492d40059fc1d10936e38627923 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Tue, 7 Apr 2020 11:47:06 +1000 Subject: [PATCH] Added custom sizing support --- src/components/Map.js | 55 +++++++++++++++++++----------------- src/components/ProxyToken.js | 18 ++++++------ src/components/SizeInput.js | 52 ++++++++++++++++++++++++++++++++++ src/components/Token.js | 15 ++++++---- src/components/Tokens.js | 47 +++++++++++++++++++----------- 5 files changed, 130 insertions(+), 57 deletions(-) create mode 100644 src/components/SizeInput.js diff --git a/src/components/Map.js b/src/components/Map.js index 0d3ad8a..68ffa85 100644 --- a/src/components/Map.js +++ b/src/components/Map.js @@ -17,7 +17,7 @@ function Map({ tokens, onMapTokenMove, onMapTokenRemove, - onMapChanged + onMapChanged, }) { function handleProxyDragEnd(isOnMap, token) { if (isOnMap && onMapTokenMove) { @@ -36,29 +36,29 @@ function Map({ interact(".map") .gesturable({ listeners: { - move: event => { - setMapScale(previousMapScale => + move: (event) => { + setMapScale((previousMapScale) => Math.max(Math.min(previousMapScale + event.ds, maxZoom), minZoom) ); - setMapTranslate(previousMapTranslate => ({ + setMapTranslate((previousMapTranslate) => ({ x: previousMapTranslate.x + event.dx, - y: previousMapTranslate.y + event.dy + y: previousMapTranslate.y + event.dy, })); - } - } + }, + }, }) .draggable({ inertia: true, listeners: { - move: event => { - setMapTranslate(previousMapTranslate => ({ + move: (event) => { + setMapTranslate((previousMapTranslate) => ({ x: previousMapTranslate.x + event.dx, - y: previousMapTranslate.y + event.dy + y: previousMapTranslate.y + event.dy, })); - } - } + }, + }, }); - interact(".map").on("doubletap", event => { + interact(".map").on("doubletap", (event) => { event.preventDefault(); setMapTranslate({ x: 0, y: 0 }); setMapScale(1); @@ -67,7 +67,7 @@ function Map({ function handleZoom(event) { const deltaY = event.deltaY * zoomSpeed; - setMapScale(previousMapScale => + setMapScale((previousMapScale) => Math.max(Math.min(previousMapScale + deltaY, maxZoom), minZoom) ); } @@ -93,7 +93,7 @@ function Map({ overflow: "hidden", backgroundColor: "rgba(0, 0, 0, 0.1)", userSelect: "none", - touchAction: "none" + touchAction: "none", }} bg="background" onWheel={handleZoom} @@ -103,19 +103,19 @@ function Map({ position: "relative", top: "50%", left: "50%", - transform: "translate(-50%, -50%)" + transform: "translate(-50%, -50%)", }} > @@ -144,24 +144,27 @@ function Map({ top: 0, left: 0, right: 0, - bottom: 0 + bottom: 0, }} > - {Object.values(tokens).map(token => ( + {Object.values(tokens).map((token) => ( diff --git a/src/components/ProxyToken.js b/src/components/ProxyToken.js index 2469df6..0b37d44 100644 --- a/src/components/ProxyToken.js +++ b/src/components/ProxyToken.js @@ -16,7 +16,7 @@ function ProxyToken({ tokenClassName, onProxyDragEnd }) { useEffect(() => { interact(`.${tokenClassName}`).draggable({ listeners: { - start: event => { + start: (event) => { let target = event.target; // Hide the token and copy it's image to the proxy target.style.opacity = "0.25"; @@ -39,7 +39,7 @@ function ProxyToken({ tokenClassName, onProxyDragEnd }) { } }, - move: event => { + move: (event) => { let proxy = imageRef.current; // Move the proxy based off of the movment of the token if (proxy) { @@ -66,7 +66,7 @@ function ProxyToken({ tokenClassName, onProxyDragEnd }) { } }, - end: event => { + end: (event) => { let target = event.target; let proxy = imageRef.current; if (proxy) { @@ -85,12 +85,12 @@ function ProxyToken({ tokenClassName, onProxyDragEnd }) { x = x / (mapRect.right - mapRect.left); y = y / (mapRect.bottom - mapRect.top); - const id = target.getAttribute("data-token-id"); onProxyDragEnd(proxyOnMap.current, { image: imageSource, x, y, - id + // Pass in props stored as data- in the dom node + ...target.dataset, }); } @@ -103,8 +103,8 @@ function ProxyToken({ tokenClassName, onProxyDragEnd }) { // Show the token target.style.opacity = "1"; setImageSource(""); - } - } + }, + }, }); }, [imageSource, onProxyDragEnd, tokenClassName, proxyContainer]); @@ -121,7 +121,7 @@ function ProxyToken({ tokenClassName, onProxyDragEnd }) { top: 0, left: 0, bottom: 0, - right: 0 + right: 0, }} > diff --git a/src/components/SizeInput.js b/src/components/SizeInput.js new file mode 100644 index 0000000..2c5f5d7 --- /dev/null +++ b/src/components/SizeInput.js @@ -0,0 +1,52 @@ +import React from "react"; +import { Box, Flex, IconButton, Text, Label } from "theme-ui"; + +function SizeInput({ value, onChange }) { + return ( + + + Size + + + value > 1 && onChange(value - 1)} + > + + + + + + {value} + onChange(value + 1)} + > + + + + + + + + ); +} + +SizeInput.defaultProps = { + value: 1, + onChange: () => {}, +}; + +export default SizeInput; diff --git a/src/components/Token.js b/src/components/Token.js index 7acf7b6..9dc3dcf 100644 --- a/src/components/Token.js +++ b/src/components/Token.js @@ -1,21 +1,26 @@ import React from "react"; import { Image } from "theme-ui"; -function Token({ image, className, tokenId, sx }) { - // Store the token id in the html element for the drag code to pick it up - const idProp = tokenId ? { "data-token-id": tokenId } : {}; +// The data prop is used to pass data into the dom element +// this can be used to pass state to the ProxyToken +function Token({ image, className, data, sx }) { + // Map the keys in data to have the `data-` prefix + const dataProps = Object.fromEntries( + Object.entries(data).map(([key, value]) => [`data-${key}`, value]) + ); return ( ); } Token.defaultProps = { - sx: {} + data: {}, + sx: {}, }; export default Token; diff --git a/src/components/Tokens.js b/src/components/Tokens.js index fe936e2..befd695 100644 --- a/src/components/Tokens.js +++ b/src/components/Tokens.js @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useState } from "react"; import { Flex, Box } from "theme-ui"; import shortid from "shortid"; @@ -6,34 +6,47 @@ import * as tokens from "../tokens"; import Token from "./Token"; import ProxyToken from "./ProxyToken"; +import SizeInput from "./SizeInput"; const listTokenClassName = "list-token"; function Tokens({ onCreateMapToken }) { + const [tokenSize, setTokenSize] = useState(1); + function handleProxyDragEnd(isOnMap, token) { if (isOnMap && onCreateMapToken) { // Give the token an id - onCreateMapToken({ ...token, id: shortid.generate() }); + onCreateMapToken({ ...token, id: shortid.generate(), size: tokenSize }); } } return ( <> - - {Object.entries(tokens).map(([id, image]) => ( - - - - ))} + + + {Object.entries(tokens).map(([id, image]) => ( + + + + ))} + + + +