leaflet#LeafletEventHandlerFn TypeScript Examples
The following examples show how to use
leaflet#LeafletEventHandlerFn.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FeatureMarker.tsx From Teyvat.moe with GNU General Public License v3.0 | 4 votes |
_FeatureMarker: FunctionComponent<FeatureMarkerProps> = ({
marker,
featureKey,
icons,
completed,
completedAlpha,
editable = false,
allowExternalMedia = false,
markFeature,
unmarkFeature,
}) => {
// CSS classes.
const classes = useStyles();
let svg = false;
let clusterIconName = '';
if (completed) {
// == false sucks, but TypeScript needs it to guarantee
// the structure of the marker icon data.
if (icons?.done?.marker == false) {
svg = icons?.done?.svg ?? false;
clusterIconName = icons?.done?.clusterIcon ?? '';
}
} else {
if (icons?.base?.marker == false) {
svg = icons?.base?.svg ?? false;
clusterIconName = icons?.base?.clusterIcon ?? '';
}
}
// Build the icon for this marker. Relies on completion status.
const icon =
icons == null
? editorMarker
: createMapIcon({
...(completed ? icons?.done : icons?.base),
marker: (completed ? icons?.done?.marker : icons?.base?.marker) ?? true,
done: !!completed,
ext: svg ? 'svg' : 'png',
key: (completed ? icons?.done?.key : icons?.base?.key) ?? icons?.filter ?? '',
});
// Build the cluster icon for the marker. Also relies on completion status.
const clusterIcon = createClusterIcon({
marker: (completed ? icons?.done?.marker : icons?.base?.marker) ?? true,
extension: svg ? 'svg' : 'png',
key: (completed ? icons?.done?.key : icons?.base?.key) ?? icons?.filter ?? '',
clusterIcon: clusterIconName,
});
const onDoubleClick = (_event: LeafletEvent) => {
// Calls on double clicks, not single clicks.
// Mark as completed.
if (completed) {
unmarkFeature();
} else {
markFeature();
}
};
const DOUBLE_CLICK_TIMEOUT = 300;
const onCopyPermalink = () => copyPermalink(marker.id);
const onSwitchCompleted = (value: boolean): void => {
// If we switch to true but the marker is already completed,
// do nothing.
if (value === !!completed) return;
if (value) {
markFeature();
} else {
unmarkFeature();
}
};
/* eslint-disable no-param-reassign */
const eventHandlers: Record<string, LeafletEventHandlerFn> = {
add: (event) => {
// We will be triggering popups manually.
event.target.off('click', event.target._openPopup);
if (editable) {
event.target.enableEdit();
}
},
click: (event) => {
if (event.target.clicks === undefined) event.target.clicks = 0;
event.target.clicks += 1;
setTimeout(() => {
if (event.target.clicks === 1) {
onSingleClick(event);
}
event.target.clicks = 0;
}, DOUBLE_CLICK_TIMEOUT);
},
dblclick: (event) => {
event.target.clicks = 0;
onDoubleClick(event);
},
};
/* eslint-enable no-param-reassign */
const title = localizeField(marker?.popupTitle);
const content = localizeField(marker?.popupContent);
return (
<ExtendedMarker
clusterIconUrl={clusterIcon}
completed={!!completed}
eventHandlers={eventHandlers}
icon={icon}
key={marker.id}
markerKey={`${featureKey}/${marker.id}`}
opacity={completed ? completedAlpha : 1}
position={marker.coordinates}
>
{/* A modern variant of MapPopupLegacy. */}
<Popup maxWidth={540} minWidth={192} autoPan={false} keepInView={false}>
<Box className={classes.popupContainer}>
{title && title !== '' ? (
<Typography className={classes.popupTitle}>{title}</Typography>
) : (
<Typography className={classes.popupTitle}>
{f('map-ui:marker-id-format', { id: marker.id.slice(0, 7) })}
</Typography>
)}
<Box>
<FeatureMedia media={marker.popupMedia} allowExternalMedia={allowExternalMedia} />
</Box>
{content && content !== '' ? (
<SafeHTML className={classes.popupContent}>{content}</SafeHTML>
) : null}
{!editable ? (
<Box className={classes.actionContainer}>
<Tooltip title={t('completed')}>
<Box className={classes.innerActionContainer}>
<InputSwitch
size="small"
color="primary"
label={<AssignmentTurnedInIcon />}
value={Boolean(completed)}
onChange={onSwitchCompleted}
/>
</Box>
</Tooltip>
<Tooltip title={t('map-ui:copy-permalink')}>
<Box className={classes.innerActionContainer}>
<IconButton onClick={onCopyPermalink}>
<LinkIcon />
</IconButton>
</Box>
</Tooltip>
</Box>
) : null}
{completed ? (
<Typography className={classes.completedSubtitle}>
{f('map-ui:completed-time-format', {
time: formatUnixTimestamp(completed),
})}
</Typography>
) : null}
</Box>
</Popup>
</ExtendedMarker>
);
}