Arrows layer

This commit is contained in:
Fedor Katurov 2020-01-16 12:03:02 +07:00
parent 69d1d749cf
commit de2b747a20
11 changed files with 57 additions and 58 deletions

View file

@ -16,7 +16,7 @@ import { LogoPreview } from '~/components/logo/LogoPreview';
import { IStickerPack } from '~/constants/stickers'; import { IStickerPack } from '~/constants/stickers';
import { IDialogs } from '~/constants/dialogs'; import { IDialogs } from '~/constants/dialogs';
import { Map } from '~/containers/map/Map'; import { Map } from '~/map/Map';
import { IEditorState } from '~/redux/editor'; import { IEditorState } from '~/redux/editor';
import { IState } from '~/redux/store'; import { IState } from '~/redux/store';

35
src/map/Arrows/index.tsx Normal file
View file

@ -0,0 +1,35 @@
import { FC, memo, useState, useEffect } from 'react';
import { MainMap } from '~/constants/map';
import { connect } from 'react-redux';
import { IState } from '~/redux/store';
import { selectMapRoute } from '~/redux/map/selectors';
import { ArrowsLayer } from '~/utils/map/ArrowsLayer';
const mapStateToProps = (state: IState) => ({
route: selectMapRoute(state),
});
const mapDispatchToProps = {};
type Props = ReturnType<typeof mapStateToProps> & typeof mapDispatchToProps & {};
const ArrowsUnconnected: FC<Props> = memo(({ route }) => {
const [layer, setLayer] = useState(null);
useEffect(() => {
const item = new ArrowsLayer({}).addTo(MainMap);
setLayer(item);
return () => MainMap.removeLayer(item);
}, [MainMap]);
useEffect(() => {
if (!layer) return
layer.setLatLngs(route);
}, [layer, route])
return null;
});
const Arrows = connect(mapStateToProps, mapDispatchToProps)(ArrowsUnconnected);
export { Arrows };

View file

@ -1,20 +1,19 @@
import React, { FC, useEffect, useState } from 'react'; import React, { FC, useEffect, useState, memo } from 'react';
import { KmMarksLayer } from '~/utils/marks'; import { KmMarksLayer } from '~/utils/marks';
import { MainMap } from '~/constants/map'; import { MainMap } from '~/constants/map';
import { selectMap } from '~/redux/map/selectors'; import { selectMap } from '~/redux/map/selectors';
import pick from 'ramda/es/pick'; import pick from 'ramda/es/pick';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { IState } from '~/redux/store';
const mapStateToProps = state => ({ const mapStateToProps = (state: IState) => ({
map: pick(['route'], selectMap(state)), map: pick(['route'], selectMap(state)),
}); });
const mapDispatchToProps = {}; const mapDispatchToProps = {};
type Props = ReturnType<typeof mapStateToProps> & typeof mapDispatchToProps & {}; type Props = ReturnType<typeof mapStateToProps> & typeof mapDispatchToProps & {};
const KmMarksUnconnected: FC<Props> = ({ const KmMarksUnconnected: FC<Props> = memo(({ map: { route } }) => {
map: { route },
}) => {
const [layer, setLayer] = useState(null); const [layer, setLayer] = useState(null);
useEffect(() => { useEffect(() => {
@ -30,7 +29,7 @@ const KmMarksUnconnected: FC<Props> = ({
layer.setLatLngs(route); layer.setLatLngs(route);
}, [layer, route]); }, [layer, route]);
return null; return null;
}; });
const KmMarks = connect(mapStateToProps, mapDispatchToProps)(KmMarksUnconnected); const KmMarks = connect(mapStateToProps, mapDispatchToProps)(KmMarksUnconnected);

View file

@ -6,11 +6,12 @@ import { selectMapProvider, selectMapRoute, selectMapStickers } from '~/redux/ma
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import * as MAP_ACTIONS from '~/redux/map/actions'; import * as MAP_ACTIONS from '~/redux/map/actions';
import { Route } from '~/containers/map/Route'; import { Route } from '~/map/Route';
import { Router } from '~/containers/map/Router'; import { Router } from '~/map/Router';
import { TileLayer } from '~/containers/map/TileLayer'; import { TileLayer } from '~/map/TileLayer';
import { Stickers } from '~/containers/map/Stickers'; import { Stickers } from '~/map/Stickers';
import { KmMarks } from '~/containers/map/KmMarks'; import { KmMarks } from '~/map/KmMarks';
import { Arrows } from '~/map/Arrows';
import 'leaflet/dist/leaflet.css'; import 'leaflet/dist/leaflet.css';
import { selectEditorEditing, selectEditorMode } from '~/redux/editor/selectors'; import { selectEditorEditing, selectEditorMode } from '~/redux/editor/selectors';
@ -74,9 +75,10 @@ const MapUnconnected: React.FC<IProps> = ({
/> />
<Route /> <Route />
<Router /> <Router />
<KmMarks /> <KmMarks />
<Arrows />
</div>, </div>,
document.getElementById('canvas') document.getElementById('canvas')
); );

View file

@ -1,8 +1,7 @@
import React, { FC, useEffect, memo, useState, useCallback } from 'react'; import React, { FC, useEffect, memo, useState, useCallback } from 'react';
import { IMapRoute } from '../../../redux/map/types';
import { InteractivePoly } from '~/utils/map/InteractivePoly'; import { InteractivePoly } from '~/utils/map/InteractivePoly';
import { isMobile } from '~/utils/window'; import { isMobile } from '~/utils/window';
import { LatLng, Map, LeafletEvent } from 'leaflet'; import { LatLng } from 'leaflet';
import { selectEditor } from '~/redux/editor/selectors'; import { selectEditor } from '~/redux/editor/selectors';
import pick from 'ramda/es/pick'; import pick from 'ramda/es/pick';
import * as MAP_ACTIONS from '~/redux/map/actions'; import * as MAP_ACTIONS from '~/redux/map/actions';

View file

@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import { IStickerDump } from '~/redux/map/types'; import { IStickerDump } from '~/redux/map/types';
import { FeatureGroup, Map } from 'leaflet'; import { FeatureGroup, Map } from 'leaflet';
import { Sticker } from '~/containers/map/Sticker'; import { Sticker } from '~/map/Sticker';
import { mapSetSticker, mapDropSticker } from '~/redux/map/actions'; import { mapSetSticker, mapDropSticker } from '~/redux/map/actions';
import { MapContainer, MainMap } from '~/constants/map'; import { MapContainer, MainMap } from '~/constants/map';

View file

@ -1,5 +1,5 @@
import React from "react"; import React from "react";
import { TileContext } from "../../../utils/context"; import { TileContext } from "~/utils/context";
import { TileLayer as TileLayerInterface, tileLayer, Map } from "leaflet"; import { TileLayer as TileLayerInterface, tileLayer, Map } from "leaflet";
import { DEFAULT_PROVIDER, PROVIDERS } from "~/constants/providers"; import { DEFAULT_PROVIDER, PROVIDERS } from "~/constants/providers";
import { IMapReducer } from "~/redux/map"; import { IMapReducer } from "~/redux/map";

View file

@ -1,36 +0,0 @@
import {
Map as MapInterface,
map,
tileLayer,
TileLayer,
} from 'leaflet';
import 'leaflet/dist/leaflet.css';
import { PROVIDER } from '$config/frontend'; //
import { DEFAULT_PROVIDER, PROVIDERS } from '$constants/providers';
interface Props {
container: string
}
export class Map {
constructor({ container }: Props) {
this.map = map(container).setView([55.0153275, 82.9071235], 13);
// todo: change coords?
this.tileLayer.addTo(this.map);
}
map: MapInterface;
tileLayer: TileLayer = tileLayer(PROVIDER.url, {
attribution: 'Независимое Велосообщество',
maxNativeZoom: 18,
maxZoom: 18,
});
setProvider = (provider: string): void => {
const { url } = (provider && PROVIDERS[provider] && PROVIDERS[provider]) || PROVIDERS[DEFAULT_PROVIDER];
this.tileLayer.setUrl(url);
}
}

View file

@ -3,7 +3,7 @@ import { arrowClusterIcon, createArrow } from "~/utils/arrow";
import { MarkerClusterGroup } from 'leaflet.markercluster/dist/leaflet.markercluster-src.js'; import { MarkerClusterGroup } from 'leaflet.markercluster/dist/leaflet.markercluster-src.js';
import { angleBetweenPoints, dist2, middleCoord } from "~/utils/geom"; import { angleBetweenPoints, dist2, middleCoord } from "~/utils/geom";
class Component extends LayerGroup { class ArrowsLayer extends LayerGroup {
constructor(props){ constructor(props){
super(props); super(props);
} }
@ -47,19 +47,19 @@ class Component extends LayerGroup {
} }
Component.addInitHook(function () { ArrowsLayer.addInitHook(function () {
this.once('add', (event) => { this.once('add', (event) => {
if (event.target instanceof Arrows) { if (event.target instanceof ArrowsLayer) {
this.map = event.target._map; this.map = event.target._map;
this.arrowLayer.addTo(this.map); this.arrowLayer.addTo(this.map);
} }
}); });
this.once('remove', (event) => { this.once('remove', (event) => {
if (event.target instanceof Arrows) { if (event.target instanceof ArrowsLayer) {
this.arrowLayer.removeFrom(this.map); this.arrowLayer.removeFrom(this.map);
} }
}); });
}); });
export const Arrows = Component; export { ArrowsLayer };