Skip to content

Commit

Permalink
Merge branch 'master' into fecach
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz authored Jan 12, 2025
2 parents 52e2849 + fac127a commit 16345d9
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const EditContent = () => {
direction={isSmallScreen ? 'column' : 'row'}
gap={2}
overflow="hidden"
flex={1}
>
{items.length > 1 && (
<Tabs
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React, { useState } from 'react';
import {
Accordion,
AccordionDetails,
AccordionSummary,
CircularProgress,
Typography,
} from '@mui/material';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';

import styled from '@emotion/styled';
import { t } from '../../../../../../services/intl';
import { useFeatureEditData } from '../SingleFeatureEditContext';
import { useInitEditFeatureMap } from './useInitEditFeatureMap';

const Container = styled.div`
width: 100%;
height: 500px;
position: relative;
`;

const LoadingContainer = styled.div`
height: 100%;
width: 100%;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
`;

const Map = styled.div<{ $isVisible: boolean }>`
visibility: ${({ $isVisible }) => ($isVisible ? 'visible' : 'hidden')};
height: 100%;
width: 100%;
`;

export default function EditFeatureMap() {
const { containerRef, isMapLoaded } = useInitEditFeatureMap();
const [expanded, setExpanded] = useState(false);

const { shortId } = useFeatureEditData();
const isNode = shortId[0] === 'n';
if (!isNode) return null;

return (
<Accordion
disableGutters
elevation={0}
square
expanded={expanded}
onChange={() => setExpanded(!expanded)}
>
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
<Typography variant="button">{t('editdialog.location')}</Typography>
</AccordionSummary>
<AccordionDetails>
<Container>
{!isMapLoaded && (
<LoadingContainer>
<CircularProgress color="primary" />
</LoadingContainer>
)}
<Map $isVisible={isMapLoaded} ref={containerRef} />
</Container>
</AccordionDetails>
</Accordion>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { useRef } from 'react';
import maplibregl, { LngLat } from 'maplibre-gl';
import { createMapEffectHook } from '../../../../../helpers';
import { LonLat } from '../../../../../../services/types';

const useUpdateDraggableFeatureMarker = createMapEffectHook<
[
{
onMarkerChange: (lngLat: LngLat) => void;
nodeLonLat: LonLat;
markerRef: React.MutableRefObject<maplibregl.Marker>;
},
]
>((map, props) => {
const { markerRef, nodeLonLat, onMarkerChange } = props;

const onDragEnd = () => {
const lngLat = markerRef.current?.getLngLat();
if (lngLat) {
onMarkerChange(lngLat);
}
};

markerRef.current?.remove();
markerRef.current = undefined;

if (nodeLonLat) {
const [lng, lat] = nodeLonLat;
markerRef.current = new maplibregl.Marker({
color: 'salmon',
draggable: true,
})
.setLngLat({
lng: parseFloat(lng.toFixed(6)),
lat: parseFloat(lat.toFixed(6)),
})
.addTo(map);

markerRef.current?.on('dragend', onDragEnd);
}
});

export function useDraggableFeatureMarker(
mapRef: React.MutableRefObject<maplibregl.Map>,
currentItem: any,
) {
const markerRef = useRef<maplibregl.Marker>();

const onMarkerChange = ({ lng, lat }: LngLat) => {
const newLonLat = [lng, lat];
currentItem.setNodeLonLat(newLonLat);
};

useUpdateDraggableFeatureMarker(mapRef.current, {
onMarkerChange,
nodeLonLat: currentItem?.nodeLonLat,
markerRef,
});
}
Loading

0 comments on commit 16345d9

Please sign in to comment.