diff --git a/src/components/map/MapEditor.js b/src/components/map/MapEditor.js index 57ff6b8..f8dbb8a 100644 --- a/src/components/map/MapEditor.js +++ b/src/components/map/MapEditor.js @@ -1,4 +1,4 @@ -import React, { useState, useRef, useContext } from "react"; +import React, { useState, useRef } from "react"; import { Box, IconButton } from "theme-ui"; import { Stage, Layer, Image } from "react-konva"; import ReactResizeDetector from "react-resize-detector"; @@ -10,9 +10,9 @@ import useImageCenter from "../../hooks/useImageCenter"; import useResponsiveLayout from "../../hooks/useResponsiveLayout"; import { getGridDefaultInset, getGridMaxZoom } from "../../helpers/grid"; +import KonvaBridge from "../../helpers/KonvaBridge"; import { MapInteractionProvider } from "../../contexts/MapInteractionContext"; -import KeyboardContext from "../../contexts/KeyboardContext"; import { GridProvider } from "../../contexts/GridContext"; import ResetMapIcon from "../../icons/ResetMapIcon"; @@ -90,11 +90,9 @@ function MapEditor({ map, onSettingsChange }) { setPreventMapInteraction, mapWidth, mapHeight, + interactionEmitter: null, }; - // Get keyboard context to pass to Konva - const keyboardValue = useContext(KeyboardContext); - const canEditGrid = map.type !== "default"; const gridChanged = @@ -106,77 +104,90 @@ function MapEditor({ map, onSettingsChange }) { const layout = useResponsiveLayout(); return ( - - - + + - - - - + + ( + + {children} + + )} + > + + {showGridControls && canEditGrid && ( - + <> - + )} - - - - - - {gridChanged && ( - - - - )} - {canEditGrid && ( - setShowGridControls(!showGridControls)} - bg="overlay" - sx={{ - borderRadius: "50%", - position: "absolute", - bottom: 0, - right: 0, - }} - m={2} - p="6px" - > - {showGridControls ? : } - - )} - + + + + {gridChanged && ( + + + + )} + {canEditGrid && ( + setShowGridControls(!showGridControls)} + bg="overlay" + sx={{ + borderRadius: "50%", + position: "absolute", + bottom: 0, + right: 0, + }} + m={2} + p="6px" + > + {showGridControls ? : } + + )} + + + ); } diff --git a/src/components/map/MapInteraction.js b/src/components/map/MapInteraction.js index e5f5ab9..1bed065 100644 --- a/src/components/map/MapInteraction.js +++ b/src/components/map/MapInteraction.js @@ -10,21 +10,12 @@ import useStageInteraction from "../../hooks/useStageInteraction"; import useImageCenter from "../../hooks/useImageCenter"; import { getGridMaxZoom } from "../../helpers/grid"; +import KonvaBridge from "../../helpers/KonvaBridge"; import { MapInteractionProvider } from "../../contexts/MapInteractionContext"; -import { MapStageProvider, useMapStage } from "../../contexts/MapStageContext"; -import AuthContext, { useAuth } from "../../contexts/AuthContext"; -import SettingsContext, { useSettings } from "../../contexts/SettingsContext"; -import KeyboardContext from "../../contexts/KeyboardContext"; -import TokenDataContext, { - useTokenData, -} from "../../contexts/TokenDataContext"; +import { useMapStage } from "../../contexts/MapStageContext"; import { GridProvider } from "../../contexts/GridContext"; import { useKeyboard } from "../../contexts/KeyboardContext"; -import { - ImageSourcesStateContext, - ImageSourcesUpdaterContext, -} from "../../contexts/ImageSourceContext"; function MapInteraction({ map, @@ -162,8 +153,6 @@ function MapInteraction({ } useKeyboard(handleKeyDown, handleKeyUp); - // Get keyboard context to pass to Konva - const keyboardValue = useContext(KeyboardContext); function getCursorForTool(tool) { switch (tool) { @@ -171,9 +160,7 @@ function MapInteraction({ return "move"; case "fog": case "drawing": - return settings.settings[tool].type === "move" - ? "pointer" - : "crosshair"; + return "crosshair"; case "measure": case "pointer": case "note": @@ -183,12 +170,6 @@ function MapInteraction({ } } - const auth = useAuth(); - const settings = useSettings(); - const tokenData = useTokenData(); - const imageSources = useContext(ImageSourcesStateContext); - const setImageSources = useContext(ImageSourcesUpdaterContext); - const mapInteraction = { stageScale, stageWidth, @@ -200,69 +181,49 @@ function MapInteraction({ }; return ( - - - + + - - - {/* Forward auth context to konva elements */} - - - - - - - - - - {mapLoaded && children} - - - - - - - - - - - - - - + + ( + + {children} + + )} + > + + + + {mapLoaded && children} + + + {controls} - - - + + + ); } diff --git a/src/contexts/GridContext.js b/src/contexts/GridContext.js index b0ec78e..a4b173f 100644 --- a/src/contexts/GridContext.js +++ b/src/contexts/GridContext.js @@ -37,19 +37,21 @@ const defaultValue = { gridCellPixelOffset: new Vector2(0, 0), }; -const GridContext = React.createContext(defaultValue.grid); -const GridPixelSizeContext = React.createContext(defaultValue.gridPixelSize); -const GridCellPixelSizeContext = React.createContext( +export const GridContext = React.createContext(defaultValue.grid); +export const GridPixelSizeContext = React.createContext( + defaultValue.gridPixelSize +); +export const GridCellPixelSizeContext = React.createContext( defaultValue.gridCellPixelSize ); -const GridCellNormalizedSizeContext = React.createContext( +export const GridCellNormalizedSizeContext = React.createContext( defaultValue.gridCellNormalizedSize ); -const GridOffsetContext = React.createContext(defaultValue.gridOffset); -const GridStrokeWidthContext = React.createContext( +export const GridOffsetContext = React.createContext(defaultValue.gridOffset); +export const GridStrokeWidthContext = React.createContext( defaultValue.gridStrokeWidth ); -const GridCellPixelOffsetContext = React.createContext( +export const GridCellPixelOffsetContext = React.createContext( defaultValue.gridCellPixelOffset ); diff --git a/src/contexts/MapInteractionContext.js b/src/contexts/MapInteractionContext.js index 1a8a434..1132999 100644 --- a/src/contexts/MapInteractionContext.js +++ b/src/contexts/MapInteractionContext.js @@ -1,14 +1,14 @@ import React, { useContext } from "react"; import useDebounce from "../hooks/useDebounce"; -const StageScaleContext = React.createContext(); -const DebouncedStageScaleContext = React.createContext(); -const StageWidthContext = React.createContext(); -const StageHeightContext = React.createContext(); -const SetPreventMapInteractionContext = React.createContext(); -const MapWidthContext = React.createContext(); -const MapHeightContext = React.createContext(); -const InteractionEmitterContext = React.createContext(); +export const StageScaleContext = React.createContext(); +export const DebouncedStageScaleContext = React.createContext(); +export const StageWidthContext = React.createContext(); +export const StageHeightContext = React.createContext(); +export const SetPreventMapInteractionContext = React.createContext(); +export const MapWidthContext = React.createContext(); +export const MapHeightContext = React.createContext(); +export const InteractionEmitterContext = React.createContext(); export function MapInteractionProvider({ value, children }) { const { @@ -32,7 +32,7 @@ export function MapInteractionProvider({ value, children }) { {children} diff --git a/src/helpers/KonvaBridge.js b/src/helpers/KonvaBridge.js new file mode 100644 index 0000000..b8fb3db --- /dev/null +++ b/src/helpers/KonvaBridge.js @@ -0,0 +1,143 @@ +import React, { useContext } from "react"; + +import { + InteractionEmitterContext, + SetPreventMapInteractionContext, + StageWidthContext, + StageHeightContext, + MapWidthContext, + MapHeightContext, + StageScaleContext, + DebouncedStageScaleContext, + useInteractionEmitter, + useSetPreventMapInteraction, + useStageWidth, + useStageHeight, + useMapWidth, + useMapHeight, + useStageScale, + useDebouncedStageScale, +} from "../contexts/MapInteractionContext"; +import { MapStageProvider, useMapStage } from "../contexts/MapStageContext"; +import AuthContext, { useAuth } from "../contexts/AuthContext"; +import SettingsContext, { useSettings } from "../contexts/SettingsContext"; +import KeyboardContext from "../contexts/KeyboardContext"; +import TokenDataContext, { useTokenData } from "../contexts/TokenDataContext"; +import { + ImageSourcesStateContext, + ImageSourcesUpdaterContext, +} from "../contexts/ImageSourceContext"; +import { + useGrid, + useGridCellPixelSize, + useGridCellNormalizedSize, + useGridStrokeWidth, + useGridCellPixelOffset, + useGridOffset, + useGridPixelSize, + GridContext, + GridPixelSizeContext, + GridCellPixelSizeContext, + GridCellNormalizedSizeContext, + GridOffsetContext, + GridStrokeWidthContext, + GridCellPixelOffsetContext, +} from "../contexts/GridContext"; + +/** + * Provide a bridge for konva that forwards our contexts + */ +function KonvaBridge({ stageRender, children }) { + const mapStageRef = useMapStage(); + const auth = useAuth(); + const settings = useSettings(); + const tokenData = useTokenData(); + const imageSources = useContext(ImageSourcesStateContext); + const setImageSources = useContext(ImageSourcesUpdaterContext); + const keyboardValue = useContext(KeyboardContext); + + const stageScale = useStageScale(); + const stageWidth = useStageWidth(); + const stageHeight = useStageHeight(); + const setPreventMapInteraction = useSetPreventMapInteraction(); + const mapWidth = useMapWidth(); + const mapHeight = useMapHeight(); + const interactionEmitter = useInteractionEmitter(); + const debouncedStageScale = useDebouncedStageScale(); + + const grid = useGrid(); + const gridPixelSize = useGridPixelSize(); + const gridCellNormalizedSize = useGridCellNormalizedSize(); + const gridCellPixelSize = useGridCellPixelSize(); + const gridStrokeWidth = useGridStrokeWidth(); + const gridCellPixelOffset = useGridCellPixelOffset(); + const gridOffset = useGridOffset(); + + return stageRender( + + + + + + + + + + + + + + + + + + + + + + + {children} + + + + + + + + + + + + + + + + + + + + + + + ); +} + +export default KonvaBridge;