Refactoring (in progress) to put most of the logic in Redux reducers.

This commit is contained in:
vdv committed 2022-09-13 18:17:08 +02:00
1 parent fd79fba51f
commit db0761efd1
14 files changed
+193 -183

No files matched your search

+25 -62
View File
@@ -1,72 +1,35 @@
import react, { useMemo, useEffect } from 'react';
import react, { useMemo, useEffect, Fragment } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { mapActions } from '../../store/map';
import _ from 'lodash';
import { MapState, mapActions } from '../../store/map';
import { slippyActions } from '../../store/slippy';
import { lat2tile, lon2tile } from '../../lib/geo';
import Tile from './tile';
import '../../theme/map.css';
export const tileSize = 256;
import Layer from '../slippy/layer';
import Slippy from '../slippy/slippy';
import TiledMap from './tiled-map';
const Map: react.FC<{}> = (props: {}) => {
const dispatch = useDispatch();
const mapState = useSelector((state: { map: MapState }) => state.map);
console.log(`mapState: ${JSON.stringify(mapState)}`);
const resizeHandler = () => {
dispatch(mapActions.resize());
};
const debouncedResizeHandler = useMemo(
() => _.debounce(resizeHandler, 500),
[]
);
useEffect(() => {
window.addEventListener('resize', debouncedResizeHandler);
}, []);
const nbTilesY = _.ceil(mapState.viewport.height / tileSize) + 3;
const nbTilesX = _.ceil(mapState.viewport.width / tileSize) + 3;
const [tileCenterY, reminderY] = lat2tile(
mapState.scope.center.lat,
mapState.scope.zoom
);
const [tileCenterX, reminderX] = lon2tile(
mapState.scope.center.lon,
mapState.scope.zoom
);
const firstTileY = tileCenterY - _.round(nbTilesY / 2);
const firstTileX = tileCenterX - _.round(nbTilesX / 2);
const locationY = (tileCenterY + reminderY - firstTileY) * tileSize;
const locationX = (tileCenterX + reminderX - firstTileX) * tileSize;
const targetLocationY = mapState.viewport.height / 2;
const targetLocationX = mapState.viewport.width / 2;
const deltaY = targetLocationY - locationY;
const deltaX = targetLocationX - locationX;
dispatch(slippyActions.set({ scale: 1, translation: { x: deltaX, y: deltaY } }));
const dispatch = useDispatch();
const resizeHandler = () => {
dispatch(mapActions.resize());
};
const debouncedResizeHandler = useMemo(
() => _.debounce(resizeHandler, 500),
[]
);
useEffect(() => {
window.addEventListener('resize', debouncedResizeHandler);
}, []);
return (
<div className='tiles'>
{_.range(nbTilesY).map((iy) => (
<div key={'y' + iy} className='tilesRow'>
{_.range(nbTilesX).map((ix) => (
<Tile
key={'x' + ix + 'y' + iy}
iy={iy}
ix={ix}
x={firstTileX + ix}
y={firstTileY + iy}
zoom={mapState.scope.zoom}
/>
))}
</div>
))}
</div>
<Fragment>
<Slippy />
<Layer>
<TiledMap />
</Layer>
</Fragment>
);
};
-1
View File
@@ -1,5 +1,4 @@
import React from 'react';
import { tileSize } from './map';
const tileProvider = (zoom: number, x: number, y: number) =>
'https://tile.openstreetmap.org/' + zoom + '/' + x + '/' + y + '.png';
+37
View File
@@ -0,0 +1,37 @@
import react from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { MapState } from '../../store/map';
import Tile from './tile';
import '../../theme/map.css';
import _ from 'lodash';
export const tileSize = 256;
const TiledMap: react.FC<{}> = (props: {}) => {
const dispatch = useDispatch();
const tilesState = useSelector((state: { map: MapState }) => state.map.tiles);
console.log(`tilesState: ${JSON.stringify(tilesState)}`);
return (
<div className='tiles'>
{_.range(tilesState.nb.y).map((iy) => (
<div key={'y' + iy} className='tilesRow'>
{_.range(tilesState.nb.y).map((ix) => (
<Tile
key={'x' + ix + 'y' + iy}
iy={iy}
ix={ix}
x={tilesState.first.x + ix}
y={tilesState.first.y + iy}
zoom={tilesState.zoom}
/>
))}
</div>
))}
</div>
);
};
export default TiledMap;
@@ -1,9 +1,7 @@
import react, { useCallback, useState } from 'react';
import react, { useState } from 'react';
import { useDispatch } from 'react-redux';
import _ from 'lodash';
import { slippyActions } from '../../store/slippy';
import { mapActions } from '../../store/map';
interface DoubleTouchHandlerProps {
children: any;
@@ -100,13 +98,13 @@ const DoubleTouchHandler: react.FC<DoubleTouchHandlerProps> = (
y: (event.touches[0].pageY + event.touches[1].pageY) / 2,
};
dispatch(
slippyActions.scale({
mapActions.scale({
factor: factor,
center: currentCenter,
})
);
dispatch(
slippyActions.translate({
mapActions.shift({
x: currentCenter.x - previousCenter.x,
y: currentCenter.y - previousCenter.y,
})
+2 -3
View File
@@ -1,7 +1,6 @@
import react from 'react';
import { useSelector } from 'react-redux';
import { SlippyState } from '../../store/slippy';
import { MapState } from '../../store/map';
import '../../theme/layer.css';
@@ -9,7 +8,7 @@ const Layer: react.FC<{
children?: JSX.Element;
}> = (props: { children?: JSX.Element }) => {
const slippyState = useSelector(
(state: { slippy: SlippyState }) => state.slippy
(state: { map: MapState }) => state.map.slippy
);
console.log(
`--- Rendering layer, slippyState: ${JSON.stringify(slippyState)} ---`
+3 -10
View File
@@ -1,9 +1,8 @@
import react, { useState } from 'react';
import { useDispatch } from 'react-redux';
import _ from 'lodash';
import { mapActions } from '../../store/map';
import { slippyActions } from '../../store/slippy';
interface MouseHandlerProps {
children: any;
@@ -14,12 +13,6 @@ const MouseHandler: react.FC<MouseHandlerProps> = (
) => {
const dispatch = useDispatch();
interface MouseState {
down: boolean;
starting: { x: number; y: number };
timestamp: number;
}
const initialMouseState = {
down: false,
starting: { x: -1, y: -1 },
@@ -76,7 +69,7 @@ const MouseHandler: react.FC<MouseHandlerProps> = (
})}`
);
dispatch(
slippyActions.translate({
mapActions.shift({
x: event.pageX - mouseState.starting.x,
y: event.pageY - mouseState.starting.y,
})
@@ -101,7 +94,7 @@ const MouseHandler: react.FC<MouseHandlerProps> = (
const doubleClickHandler = (event: any) => {
genericHandler(event);
dispatch(
slippyActions.scale({
mapActions.scale({
factor: 2,
center: { x: event.pageX, y: event.pageY },
})
+7 -13
View File
@@ -1,9 +1,7 @@
import react, { useCallback, useState } from 'react';
import react, { useState } from 'react';
import { useDispatch } from 'react-redux';
import _ from 'lodash';
import { slippyActions } from '../../store/slippy';
import { mapActions } from '../../store/map';
interface SingleTouchHandlerProps {
children: any;
@@ -39,7 +37,6 @@ const SingleTouchHandler: react.FC<SingleTouchHandlerProps> = (
const touchCancelHandler = (event: any) => {
genericHandler(event);
throtteledTouchMoveHandler.cancel();
setTouchState(initialTouchState);
};
@@ -59,16 +56,18 @@ const SingleTouchHandler: react.FC<SingleTouchHandlerProps> = (
genericHandler(event);
// event.preventDefault();
setTouchState(initialTouchState);
throtteledTouchMoveHandler.cancel();
};
const touchMoveHandler = (event: any) => {
// event.preventDefault();
if (touchState.state === 'pointer' && (Date.now() - touchState.timestamp) > 50) {
if (
touchState.state === 'pointer' &&
Date.now() - touchState.timestamp > 50
) {
if (event.touches.length === 1) {
genericHandler(event);
dispatch(
slippyActions.translate({
mapActions.shift({
x: event.touches[0].pageX - touchState.touch.x,
y: event.touches[0].pageY - touchState.touch.y,
})
@@ -85,11 +84,6 @@ const SingleTouchHandler: react.FC<SingleTouchHandlerProps> = (
}
};
const throtteledTouchMoveHandler = useCallback(
_.throttle(touchMoveHandler, 100),
[touchState.state]
);
return (
<div
className='viewport single-touch-handler'
+8 -6
View File
@@ -1,22 +1,24 @@
import react from 'react';
import MouseHandler from './mouse-handler';
import '../../theme/slippy.css';
import SingleTouchHandler from './single-touch-handler';
import DoubleTouchHandler from './double-touch-handler';
import WheelHandler from './wheel-handler';
import { useSelector } from 'react-redux';
import { MapState } from '../../store/map';
import '../../theme/slippy.css';
const Slippy: react.FC<{}> = () => {
//console.log(`--- Rendering viewport, props: ${JSON.stringify(props)} ---`);
const viewport = useSelector(
(state: { map: MapState }) => state.map.viewport
const slippyState = useSelector(
(state: { map: MapState }) => state.map.slippy
);
console.log(`slippyState: ${JSON.stringify(slippyState)}`);
return (
<div className='slippy'>
<MouseHandler>
@@ -26,8 +28,8 @@ const Slippy: react.FC<{}> = () => {
<div
style={{
position: 'fixed',
width: viewport.width + 'px',
height: viewport.height + 'px',
width: window.innerWidth + 'px',
height: window.innerHeight + 'px',
}}
/>
</WheelHandler>
+2 -2
View File
@@ -2,8 +2,8 @@ import react, { useState } from 'react';
import { useDispatch } from 'react-redux';
import _ from 'lodash';
import { mapActions } from '../../store/map';
import { slippyActions } from '../../store/slippy';
interface WheelHandlerProps {
children: any;
@@ -46,7 +46,7 @@ const WheelHandler: react.FC<WheelHandlerProps> = (
Date.now() - wheelState.timestamp > 100
) {
dispatch(
slippyActions.scale({
mapActions.scale({
factor: event.deltaY > 0 ? 2 : 0.5,
center: { x: event.pageX, y: event.pageY },
})