@kivacore/ui-pro-shared 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +187 -21
- package/src/.kiva/config.json +27 -0
- package/src/components/avatar-group.tsx +222 -0
- package/src/components/kiva/bulk-update-dialog/bulk-update-dialog.tsx +240 -0
- package/src/components/kiva/bulk-update-dialog/index.tsx +6 -0
- package/src/components/kiva/bulk-update-dialog/types.ts +33 -0
- package/src/components/kiva/calendar/animations.ts +40 -0
- package/src/components/kiva/calendar/calendar-body.tsx +106 -0
- package/src/components/kiva/calendar/calendar-sidebar.tsx +103 -0
- package/src/components/kiva/calendar/calendar.tsx +243 -0
- package/src/components/kiva/calendar/constants.ts +12 -0
- package/src/components/kiva/calendar/contexts/calendar-context.tsx +696 -0
- package/src/components/kiva/calendar/contexts/dnd-context.tsx +173 -0
- package/src/components/kiva/calendar/dialogs/add-edit-event-dialog.tsx +287 -0
- package/src/components/kiva/calendar/dialogs/delete-event-dialog.tsx +73 -0
- package/src/components/kiva/calendar/dialogs/event-details-dialog.tsx +163 -0
- package/src/components/kiva/calendar/dialogs/events-list-dialog.tsx +122 -0
- package/src/components/kiva/calendar/dnd/draggable-event.tsx +66 -0
- package/src/components/kiva/calendar/dnd/droppable-area.tsx +53 -0
- package/src/components/kiva/calendar/dnd/resizable-event.tsx +190 -0
- package/src/components/kiva/calendar/header/calendar-header.tsx +84 -0
- package/src/components/kiva/calendar/header/date-navigator.tsx +188 -0
- package/src/components/kiva/calendar/header/filter.tsx +80 -0
- package/src/components/kiva/calendar/header/today-button.tsx +49 -0
- package/src/components/kiva/calendar/header/user-select.tsx +111 -0
- package/src/components/kiva/calendar/header/view-tabs.tsx +144 -0
- package/src/components/kiva/calendar/helpers.ts +617 -0
- package/src/components/kiva/calendar/hooks/use-disclosure.ts +19 -0
- package/src/components/kiva/calendar/hooks/use-local-storage.ts +40 -0
- package/src/components/kiva/calendar/hooks/use-media-query.ts +25 -0
- package/src/components/kiva/calendar/hooks.ts +5 -0
- package/src/components/kiva/calendar/index.tsx +27 -0
- package/src/components/kiva/calendar/interfaces.ts +35 -0
- package/src/components/kiva/calendar/schemas.ts +20 -0
- package/src/components/kiva/calendar/settings/settings.tsx +116 -0
- package/src/components/kiva/calendar/skeletons/calendar-header-skeleton.tsx +27 -0
- package/src/components/kiva/calendar/skeletons/calendar-skeleton.tsx +19 -0
- package/src/components/kiva/calendar/skeletons/day-view-skeleton.tsx +40 -0
- package/src/components/kiva/calendar/skeletons/month-view-skeleton.tsx +36 -0
- package/src/components/kiva/calendar/skeletons/week-view-skeleton.tsx +48 -0
- package/src/components/kiva/calendar/skeletons/year-view-skeleton.tsx +42 -0
- package/src/components/kiva/calendar/types.ts +25 -0
- package/src/components/kiva/calendar/views/agenda-view/agenda-events.tsx +150 -0
- package/src/components/kiva/calendar/views/month-view/calendar-month-view.tsx +82 -0
- package/src/components/kiva/calendar/views/month-view/day-cell.tsx +218 -0
- package/src/components/kiva/calendar/views/month-view/event-bullet.tsx +46 -0
- package/src/components/kiva/calendar/views/month-view/month-event-badge.tsx +154 -0
- package/src/components/kiva/calendar/views/timeline/calendar-timeline-view.tsx +100 -0
- package/src/components/kiva/calendar/views/timeline/lib/timeline-layout.ts +130 -0
- package/src/components/kiva/calendar/views/timeline/lib/timeline-model.ts +504 -0
- package/src/components/kiva/calendar/views/timeline/timeline-event-bar.tsx +68 -0
- package/src/components/kiva/calendar/views/timeline/timeline-grid-header.tsx +125 -0
- package/src/components/kiva/calendar/views/timeline/timeline-row.tsx +159 -0
- package/src/components/kiva/calendar/views/timeline/timeline-section.tsx +61 -0
- package/src/components/kiva/calendar/views/timeline/use-timeline-sections.ts +278 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-day-view.tsx +277 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-time-line.tsx +47 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-week-view.tsx +229 -0
- package/src/components/kiva/calendar/views/week-and-day-view/day-view-multi-day-events-row.tsx +79 -0
- package/src/components/kiva/calendar/views/week-and-day-view/event-block.tsx +116 -0
- package/src/components/kiva/calendar/views/week-and-day-view/render-grouped-events.tsx +54 -0
- package/src/components/kiva/calendar/views/week-and-day-view/week-view-multi-day-events-row.tsx +159 -0
- package/src/components/kiva/calendar/views/year-view/calendar-year-view.tsx +159 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-active-chips.tsx +137 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-clear.tsx +34 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-search.tsx +67 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-section.tsx +77 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-async-options.tsx +354 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-boolean.tsx +66 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-checkbox-list.tsx +137 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-date-range.tsx +204 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-numeric-range.tsx +162 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-options-dropdown.tsx +191 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-text.tsx +53 -0
- package/src/components/kiva/data-table-side-filters/core/query-translator.ts +367 -0
- package/src/components/kiva/data-table-side-filters/core/types.ts +81 -0
- package/src/components/kiva/data-table-side-filters/data-table-side-filters.tsx +483 -0
- package/src/components/kiva/data-table-side-filters/hooks/use-data-table-side-filters.tsx +160 -0
- package/src/components/kiva/data-table-side-filters/hooks/use-show-more.tsx +62 -0
- package/src/components/kiva/data-table-side-filters/index.tsx +44 -0
- package/src/components/kiva/data-table-side-filters/lib/render-mode.ts +62 -0
- package/src/components/kiva/date-time-picker.tsx +163 -0
- package/src/components/kiva/editable-record-detail.tsx +809 -0
- package/src/components/kiva/enum-option-editor/enum-option-editor.tsx +891 -0
- package/src/components/kiva/enum-option-editor/index.tsx +26 -0
- package/src/components/kiva/enum-option-editor/kiva-enum-option-editor.tsx +320 -0
- package/src/components/kiva/enum-option-editor/types.ts +90 -0
- package/src/components/kiva/kanban.tsx +1376 -0
- package/src/components/kiva/kiva-data-export-menu.tsx +543 -0
- package/src/components/kiva/map.tsx +1631 -0
- package/src/components/kiva/pivot-calendar/components/group-sidebar.tsx +95 -0
- package/src/components/kiva/pivot-calendar/components/pivot-calendar-toolbar.tsx +113 -0
- package/src/components/kiva/pivot-calendar/components/stats-cell.tsx +126 -0
- package/src/components/kiva/pivot-calendar/helpers.ts +374 -0
- package/src/components/kiva/pivot-calendar/hooks/use-pivot-calendar.ts +222 -0
- package/src/components/kiva/pivot-calendar/index.tsx +32 -0
- package/src/components/kiva/pivot-calendar/pivot-calendar.tsx +107 -0
- package/src/components/kiva/pivot-calendar/types.ts +154 -0
- package/src/components/kiva/pivot-calendar/views/days-of-month-view.tsx +37 -0
- package/src/components/kiva/pivot-calendar/views/days-of-week-view.tsx +37 -0
- package/src/components/kiva/pivot-calendar/views/month-calendar-view.tsx +154 -0
- package/src/components/kiva/pivot-calendar/views/months-of-year-view.tsx +40 -0
- package/src/components/kiva/pivot-calendar/views/pivot-grid-view.tsx +251 -0
- package/src/components/kiva/pivot-filter/index.tsx +9 -0
- package/src/components/kiva/pivot-filter/pivot-filter-item.tsx +159 -0
- package/src/components/kiva/pivot-filter/pivot-filter-settings.tsx +161 -0
- package/src/components/kiva/pivot-filter/pivot-filter.tsx +187 -0
- package/src/components/kiva/pivot-filter/types.ts +82 -0
- package/src/components/kiva/pivot-grid/hooks/use-pivot-grid.ts +683 -0
- package/src/components/kiva/pivot-grid/index.tsx +23 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-color-rules.ts +98 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-export.ts +265 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-model.ts +1110 -0
- package/src/components/kiva/pivot-grid/pivot-grid-demo-data.ts +200 -0
- package/src/components/kiva/pivot-grid/pivot-grid-export-menu.tsx +66 -0
- package/src/components/kiva/pivot-grid/pivot-grid-toolbar.tsx +85 -0
- package/src/components/kiva/pivot-grid/pivot-grid.tsx +601 -0
- package/src/components/kiva/pivot-grid/types.ts +195 -0
- package/src/components/kiva/place-autocomplete.tsx +484 -0
- package/src/components/kiva/record-delete-confirm-dialog/index.tsx +10 -0
- package/src/components/kiva/record-delete-confirm-dialog/record-delete-confirm-dialog.tsx +266 -0
- package/src/components/kiva/record-delete-confirm-dialog/types.ts +25 -0
- package/src/hooks/kiva/_internal/autonumber-setting.ts +112 -0
- package/src/hooks/kiva/_internal/form-layout-to-view.ts +665 -0
- package/src/hooks/kiva/_internal/kiva-hook-utils.ts +143 -0
- package/src/hooks/kiva/_internal/use-computed-field-evaluator.tsx +310 -0
- package/src/hooks/kiva/_internal/use-field-actions-evaluator.tsx +398 -0
- package/src/hooks/kiva/_internal/use-form-actions-evaluator.tsx +363 -0
- package/src/hooks/kiva/_internal/use-form-view-engine.tsx +2200 -0
- package/src/hooks/kiva/_internal/use-kiva-form-view-fields.tsx +190 -0
- package/src/hooks/kiva/_internal/use-kiva-kanban-cards.tsx +311 -0
- package/src/hooks/kiva/use-data-export.tsx +265 -0
- package/src/hooks/kiva/use-kiva-calendar.tsx +992 -0
- package/src/hooks/kiva/use-kiva-data-export.tsx +173 -0
- package/src/hooks/kiva/use-kiva-data-grid.tsx +4750 -0
- package/src/hooks/kiva/use-kiva-data-view-select.tsx +1273 -0
- package/src/hooks/kiva/use-kiva-form-view.tsx +1581 -0
- package/src/hooks/kiva/use-kiva-inventory.tsx +184 -0
- package/src/hooks/kiva/use-kiva-kanban.tsx +1750 -0
- package/src/hooks/kiva/use-kiva-map-view.tsx +2255 -0
- package/src/hooks/kiva/use-kiva-pivot-calendar.tsx +759 -0
- package/src/hooks/kiva/use-kiva-pivot-filter.tsx +1523 -0
- package/src/hooks/kiva/use-kiva-pivot-grid.tsx +543 -0
- package/src/hooks/kiva/use-kiva-tenant.tsx +313 -0
- package/src/hooks/kiva/use-parameter-persistence.ts +191 -0
|
@@ -0,0 +1,1631 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// @ts-nocheck
|
|
4
|
+
/* eslint-disable */
|
|
5
|
+
import {
|
|
6
|
+
Suspense, createContext, lazy, useCallback, use, useEffect, useRef, useState, type ComponentProps, type ComponentType, type ReactNode, type Ref, type RefObject
|
|
7
|
+
} from 'react';
|
|
8
|
+
|
|
9
|
+
import type * as LeafletNS from 'leaflet';
|
|
10
|
+
import type * as LeafletDrawNS from 'leaflet-draw';
|
|
11
|
+
import type { } from 'leaflet.fullscreen';
|
|
12
|
+
import type { } from 'leaflet.markercluster';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
type Circle,
|
|
16
|
+
type CircleMarker,
|
|
17
|
+
type DivIconOptions,
|
|
18
|
+
type Draw,
|
|
19
|
+
type DrawEvents,
|
|
20
|
+
type DrawMap,
|
|
21
|
+
type DrawOptions,
|
|
22
|
+
type EditToolbar,
|
|
23
|
+
type ErrorEvent,
|
|
24
|
+
type FeatureGroup,
|
|
25
|
+
type LatLngExpression,
|
|
26
|
+
type LayerGroup,
|
|
27
|
+
type Map as LeafletMap,
|
|
28
|
+
type LocateOptions,
|
|
29
|
+
type LocationEvent,
|
|
30
|
+
type Marker,
|
|
31
|
+
type MarkerCluster,
|
|
32
|
+
type PointExpression,
|
|
33
|
+
type Polygon,
|
|
34
|
+
type Polyline,
|
|
35
|
+
type Popup,
|
|
36
|
+
type Rectangle,
|
|
37
|
+
type TileLayer,
|
|
38
|
+
type Tooltip
|
|
39
|
+
} from 'leaflet';
|
|
40
|
+
import 'leaflet-draw/dist/leaflet.draw.css';
|
|
41
|
+
import 'leaflet.fullscreen/dist/Control.FullScreen.css';
|
|
42
|
+
import 'leaflet.markercluster/dist/MarkerCluster.css';
|
|
43
|
+
import 'leaflet.markercluster/dist/MarkerCluster.Default.css';
|
|
44
|
+
import 'leaflet/dist/leaflet.css';
|
|
45
|
+
import {
|
|
46
|
+
CircleIcon,
|
|
47
|
+
LayersIcon,
|
|
48
|
+
LoaderCircleIcon,
|
|
49
|
+
MapPinIcon,
|
|
50
|
+
MaximizeIcon,
|
|
51
|
+
MinimizeIcon,
|
|
52
|
+
MinusIcon,
|
|
53
|
+
NavigationIcon,
|
|
54
|
+
PenLineIcon,
|
|
55
|
+
PentagonIcon,
|
|
56
|
+
PlusIcon,
|
|
57
|
+
SquareIcon,
|
|
58
|
+
Trash2Icon,
|
|
59
|
+
Undo2Icon,
|
|
60
|
+
WaypointsIcon
|
|
61
|
+
} from 'lucide-react';
|
|
62
|
+
import { useKivaTheme } from '@kivacore/ui-pro-shared/lib/kiva/theme';
|
|
63
|
+
import { renderToString } from 'react-dom/server';
|
|
64
|
+
import {
|
|
65
|
+
useMap,
|
|
66
|
+
useMapEvents,
|
|
67
|
+
type CircleMarkerProps,
|
|
68
|
+
type CircleProps,
|
|
69
|
+
type FeatureGroupProps,
|
|
70
|
+
type LayerGroupProps,
|
|
71
|
+
type MapContainerProps,
|
|
72
|
+
type MarkerProps,
|
|
73
|
+
type PolygonProps,
|
|
74
|
+
type PolylineProps,
|
|
75
|
+
type PopupProps,
|
|
76
|
+
type RectangleProps,
|
|
77
|
+
type TileLayerProps,
|
|
78
|
+
type TooltipProps
|
|
79
|
+
} from 'react-leaflet';
|
|
80
|
+
import { type MarkerClusterGroupProps } from 'react-leaflet-markercluster';
|
|
81
|
+
|
|
82
|
+
import {
|
|
83
|
+
PlaceAutocomplete,
|
|
84
|
+
type PlaceAutocompleteProps
|
|
85
|
+
} from '@kivacore/ui-pro-shared/components/kiva/place-autocomplete';
|
|
86
|
+
import { cn } from '@kivacore/ui-pro-shared/lib/utils';
|
|
87
|
+
import { Button } from '@kivacore/ui-pro-shared/components/button';
|
|
88
|
+
import { ButtonGroup } from '@kivacore/ui-pro-shared/components/button-group';
|
|
89
|
+
|
|
90
|
+
import {
|
|
91
|
+
DropdownMenu,
|
|
92
|
+
DropdownMenuCheckboxItem,
|
|
93
|
+
DropdownMenuContent,
|
|
94
|
+
DropdownMenuLabel,
|
|
95
|
+
DropdownMenuRadioGroup,
|
|
96
|
+
DropdownMenuRadioItem,
|
|
97
|
+
DropdownMenuSeparator,
|
|
98
|
+
DropdownMenuTrigger
|
|
99
|
+
} from '@kivacore/ui-pro-shared/components/dropdown-menu';
|
|
100
|
+
import { useUiTranslation } from '@kivacore/ui-pro-shared/hooks/kiva/use-ui-translation';
|
|
101
|
+
|
|
102
|
+
/* -------------------------------------------------------------------------- */
|
|
103
|
+
/* useLeaflet — lazy-load Leaflet + plugins */
|
|
104
|
+
/* -------------------------------------------------------------------------- */
|
|
105
|
+
|
|
106
|
+
export function useLeaflet() {
|
|
107
|
+
const [L, setL] = useState<typeof LeafletNS | null>(null);
|
|
108
|
+
const [LeafletDraw, setLeafletDraw] = useState<
|
|
109
|
+
typeof LeafletDrawNS | null
|
|
110
|
+
>(null);
|
|
111
|
+
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
async function loadLeaflet() {
|
|
114
|
+
const [leaflet, leafletFullscreen, leafletDraw] = await Promise.all([import('leaflet'), import('leaflet.fullscreen'), import('leaflet-draw')]);
|
|
115
|
+
|
|
116
|
+
const L_object = leaflet.default;
|
|
117
|
+
|
|
118
|
+
if (L_object.Control && !L_object.Control.FullScreen) {
|
|
119
|
+
L_object.Control.FullScreen
|
|
120
|
+
= leafletFullscreen.default || leafletFullscreen;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
setLeafletDraw(leafletDraw);
|
|
124
|
+
setL(L_object);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (L && LeafletDraw) return;
|
|
128
|
+
if (typeof window === 'undefined') return;
|
|
129
|
+
|
|
130
|
+
loadLeaflet();
|
|
131
|
+
}, [L, LeafletDraw]);
|
|
132
|
+
|
|
133
|
+
return { L, LeafletDraw };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/* -------------------------------------------------------------------------- */
|
|
137
|
+
/* Lazy Leaflet components (SSR-safe) */
|
|
138
|
+
/* -------------------------------------------------------------------------- */
|
|
139
|
+
|
|
140
|
+
/*
|
|
141
|
+
* React 19's `lazy` plus react-leaflet 5's `ForwardRefExoticComponent` exports
|
|
142
|
+
* make JSX collapse the component's prop type down to `RefAttributes<T>` at
|
|
143
|
+
* call sites. To preserve the real prop type we let the caller name it via
|
|
144
|
+
* the `P` generic — JSX then sees a plain `(props: P) => ReactNode` and
|
|
145
|
+
* accepts the full prop shape. The SSR-safe mount gate is layered on top.
|
|
146
|
+
*/
|
|
147
|
+
function createLazyComponent<P>(
|
|
148
|
+
factory: () => Promise<{ default: ComponentType<P> }>
|
|
149
|
+
): (props: P) => ReactNode {
|
|
150
|
+
const LazyComponent = lazy(factory);
|
|
151
|
+
|
|
152
|
+
return function LazyWithMountGate(props: P) {
|
|
153
|
+
const [isMounted, setIsMounted] = useState(false);
|
|
154
|
+
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
queueMicrotask(() => setIsMounted(true));
|
|
157
|
+
}, []);
|
|
158
|
+
|
|
159
|
+
if (!isMounted) {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<Suspense>
|
|
165
|
+
<LazyComponent {...(props as P & Record<string, unknown>)} />
|
|
166
|
+
</Suspense>
|
|
167
|
+
);
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/*
|
|
172
|
+
* Each component is loaded lazily but the prop type is named explicitly via
|
|
173
|
+
* the generic — react-leaflet 5 exports `ForwardRefExoticComponent`, which
|
|
174
|
+
* `lazy<T extends ComponentType<any>>` can't widen cleanly under React 19's
|
|
175
|
+
* ref-as-prop convention. Forcing the prop type via `ComponentType<P & RefAttributes<…>>`
|
|
176
|
+
* keeps JSX call sites from collapsing to `RefAttributes<…>` while still
|
|
177
|
+
* letting consumers attach a `ref`.
|
|
178
|
+
*/
|
|
179
|
+
type WithRef<P, T> = P & { ref?: Ref<T> };
|
|
180
|
+
|
|
181
|
+
const LeafletMapContainer = createLazyComponent<WithRef<MapContainerProps, LeafletMap>>(() => import('react-leaflet').then(mod => ({
|
|
182
|
+
default: mod.MapContainer as unknown as ComponentType<WithRef<MapContainerProps, LeafletMap>>
|
|
183
|
+
})));
|
|
184
|
+
const LeafletTileLayer = createLazyComponent<WithRef<TileLayerProps, TileLayer>>(() => import('react-leaflet').then(mod => ({
|
|
185
|
+
default: mod.TileLayer as unknown as ComponentType<WithRef<TileLayerProps, TileLayer>>
|
|
186
|
+
})));
|
|
187
|
+
const LeafletMarker = createLazyComponent<WithRef<MarkerProps, Marker>>(() => import('react-leaflet').then(mod => ({
|
|
188
|
+
default: mod.Marker as unknown as ComponentType<WithRef<MarkerProps, Marker>>
|
|
189
|
+
})));
|
|
190
|
+
const LeafletPopup = createLazyComponent<WithRef<PopupProps, Popup>>(() => import('react-leaflet').then(mod => ({
|
|
191
|
+
default: mod.Popup as unknown as ComponentType<WithRef<PopupProps, Popup>>
|
|
192
|
+
})));
|
|
193
|
+
const LeafletTooltip = createLazyComponent<WithRef<TooltipProps, Tooltip>>(() => import('react-leaflet').then(mod => ({
|
|
194
|
+
default: mod.Tooltip as unknown as ComponentType<WithRef<TooltipProps, Tooltip>>
|
|
195
|
+
})));
|
|
196
|
+
const LeafletCircle = createLazyComponent<WithRef<CircleProps, Circle>>(() => import('react-leaflet').then(mod => ({
|
|
197
|
+
default: mod.Circle as unknown as ComponentType<WithRef<CircleProps, Circle>>
|
|
198
|
+
})));
|
|
199
|
+
const LeafletCircleMarker = createLazyComponent<WithRef<CircleMarkerProps, CircleMarker>>(() => import('react-leaflet').then(mod => ({
|
|
200
|
+
default: mod.CircleMarker as unknown as ComponentType<WithRef<CircleMarkerProps, CircleMarker>>
|
|
201
|
+
})));
|
|
202
|
+
const LeafletPolyline = createLazyComponent<WithRef<PolylineProps, Polyline>>(() => import('react-leaflet').then(mod => ({
|
|
203
|
+
default: mod.Polyline as unknown as ComponentType<WithRef<PolylineProps, Polyline>>
|
|
204
|
+
})));
|
|
205
|
+
const LeafletPolygon = createLazyComponent<WithRef<PolygonProps, Polygon>>(() => import('react-leaflet').then(mod => ({
|
|
206
|
+
default: mod.Polygon as unknown as ComponentType<WithRef<PolygonProps, Polygon>>
|
|
207
|
+
})));
|
|
208
|
+
const LeafletRectangle = createLazyComponent<WithRef<RectangleProps, Rectangle>>(() => import('react-leaflet').then(mod => ({
|
|
209
|
+
default: mod.Rectangle as unknown as ComponentType<WithRef<RectangleProps, Rectangle>>
|
|
210
|
+
})));
|
|
211
|
+
const LeafletLayerGroup = createLazyComponent<WithRef<LayerGroupProps, LayerGroup>>(() => import('react-leaflet').then(mod => ({
|
|
212
|
+
default: mod.LayerGroup as unknown as ComponentType<WithRef<LayerGroupProps, LayerGroup>>
|
|
213
|
+
})));
|
|
214
|
+
const LeafletFeatureGroup = createLazyComponent<WithRef<FeatureGroupProps, FeatureGroup>>(() => import('react-leaflet').then(mod => ({
|
|
215
|
+
default: mod.FeatureGroup as unknown as ComponentType<WithRef<FeatureGroupProps, FeatureGroup>>
|
|
216
|
+
})));
|
|
217
|
+
const LeafletMarkerClusterGroup = createLazyComponent<WithRef<MarkerClusterGroupProps, MarkerCluster>>(async () => import('react-leaflet-markercluster').then(mod => ({
|
|
218
|
+
default: mod.default as unknown as ComponentType<WithRef<MarkerClusterGroupProps, MarkerCluster>>
|
|
219
|
+
})));
|
|
220
|
+
|
|
221
|
+
/* -------------------------------------------------------------------------- */
|
|
222
|
+
/* useDebounceLoadingState */
|
|
223
|
+
/* -------------------------------------------------------------------------- */
|
|
224
|
+
|
|
225
|
+
function useDebounceLoadingState(delay = 200) {
|
|
226
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
227
|
+
const [showLoading, setShowLoading] = useState(false);
|
|
228
|
+
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
229
|
+
|
|
230
|
+
useEffect(() => {
|
|
231
|
+
if (isLoading) {
|
|
232
|
+
timeoutRef.current = setTimeout(() => {
|
|
233
|
+
setShowLoading(true);
|
|
234
|
+
}, delay);
|
|
235
|
+
} else {
|
|
236
|
+
if (timeoutRef.current) {
|
|
237
|
+
clearTimeout(timeoutRef.current);
|
|
238
|
+
timeoutRef.current = null;
|
|
239
|
+
}
|
|
240
|
+
queueMicrotask(() => setShowLoading(false));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return () => {
|
|
244
|
+
if (timeoutRef.current) {
|
|
245
|
+
clearTimeout(timeoutRef.current);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}, [isLoading, delay]);
|
|
249
|
+
|
|
250
|
+
return [showLoading, setIsLoading] as const;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/* -------------------------------------------------------------------------- */
|
|
254
|
+
/* useMapDrawHandleIcon */
|
|
255
|
+
/* -------------------------------------------------------------------------- */
|
|
256
|
+
|
|
257
|
+
function useMapDrawHandleIcon() {
|
|
258
|
+
const { L } = useLeaflet();
|
|
259
|
+
|
|
260
|
+
if (!L) return null;
|
|
261
|
+
|
|
262
|
+
return L.divIcon({
|
|
263
|
+
iconAnchor: [8, 8],
|
|
264
|
+
html: renderToString(
|
|
265
|
+
<CircleIcon className="fill-primary stroke-primary size-4 transition-transform hover:scale-110" />
|
|
266
|
+
)
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/* -------------------------------------------------------------------------- */
|
|
271
|
+
/* Layers Context */
|
|
272
|
+
/* -------------------------------------------------------------------------- */
|
|
273
|
+
|
|
274
|
+
interface MapTileLayerOption {
|
|
275
|
+
name: string;
|
|
276
|
+
url: string;
|
|
277
|
+
attribution?: string;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
interface MapLayerGroupOption {
|
|
281
|
+
name: string;
|
|
282
|
+
disabled?: boolean;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
interface MapLayersContextType {
|
|
286
|
+
registerTileLayer: (layer: MapTileLayerOption) => void;
|
|
287
|
+
tileLayers: MapTileLayerOption[];
|
|
288
|
+
selectedTileLayer: string;
|
|
289
|
+
setSelectedTileLayer: (name: string) => void;
|
|
290
|
+
registerLayerGroup: (layer: MapLayerGroupOption) => void;
|
|
291
|
+
layerGroups: MapLayerGroupOption[];
|
|
292
|
+
activeLayerGroups: string[];
|
|
293
|
+
setActiveLayerGroups: (names: string[]) => void;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const MapLayersContext = createContext<MapLayersContextType | null>(null);
|
|
297
|
+
|
|
298
|
+
function useMapLayersContext() {
|
|
299
|
+
return use(MapLayersContext);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/* -------------------------------------------------------------------------- */
|
|
303
|
+
/* Draw Context */
|
|
304
|
+
/* -------------------------------------------------------------------------- */
|
|
305
|
+
|
|
306
|
+
type MapDrawShape = 'marker' | 'polyline' | 'circle' | 'rectangle' | 'polygon';
|
|
307
|
+
type MapDrawAction = 'edit' | 'delete';
|
|
308
|
+
type MapDrawMode = MapDrawShape | MapDrawAction | null;
|
|
309
|
+
|
|
310
|
+
interface MapDrawContextType {
|
|
311
|
+
readonly featureGroup: L.FeatureGroup | null;
|
|
312
|
+
activeMode: MapDrawMode;
|
|
313
|
+
setActiveMode: (mode: MapDrawMode) => void;
|
|
314
|
+
readonly editControlRef: RefObject<EditToolbar.Edit | null>;
|
|
315
|
+
readonly deleteControlRef: RefObject<EditToolbar.Delete | null>;
|
|
316
|
+
readonly layersCount: number;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const MapDrawContext = createContext<MapDrawContextType | null>(null);
|
|
320
|
+
|
|
321
|
+
function useMapDrawContext() {
|
|
322
|
+
return use(MapDrawContext);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/* -------------------------------------------------------------------------- */
|
|
326
|
+
/* Map */
|
|
327
|
+
/* -------------------------------------------------------------------------- */
|
|
328
|
+
|
|
329
|
+
function Map({
|
|
330
|
+
zoom = 15,
|
|
331
|
+
maxZoom = 18,
|
|
332
|
+
className,
|
|
333
|
+
...props
|
|
334
|
+
}: Omit<MapContainerProps, 'zoomControl'> & {
|
|
335
|
+
center: LatLngExpression;
|
|
336
|
+
ref?: Ref<LeafletMap>;
|
|
337
|
+
}) {
|
|
338
|
+
return (
|
|
339
|
+
<LeafletMapContainer
|
|
340
|
+
zoom={zoom}
|
|
341
|
+
maxZoom={maxZoom}
|
|
342
|
+
attributionControl={false}
|
|
343
|
+
zoomControl={false}
|
|
344
|
+
className={cn(
|
|
345
|
+
'z-50 size-full min-h-96 flex-1 rounded-md',
|
|
346
|
+
className
|
|
347
|
+
)}
|
|
348
|
+
{...props} />
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/* -------------------------------------------------------------------------- */
|
|
353
|
+
/* MapTileLayer */
|
|
354
|
+
/* -------------------------------------------------------------------------- */
|
|
355
|
+
|
|
356
|
+
function MapTileLayer({
|
|
357
|
+
name = 'Default',
|
|
358
|
+
url,
|
|
359
|
+
attribution,
|
|
360
|
+
darkUrl,
|
|
361
|
+
darkAttribution,
|
|
362
|
+
...props
|
|
363
|
+
}: Partial<TileLayerProps> & {
|
|
364
|
+
name?: string;
|
|
365
|
+
darkUrl?: string;
|
|
366
|
+
darkAttribution?: string;
|
|
367
|
+
ref?: Ref<TileLayer>;
|
|
368
|
+
}) {
|
|
369
|
+
const map = useMap();
|
|
370
|
+
|
|
371
|
+
if (map.attributionControl) {
|
|
372
|
+
map.attributionControl.setPrefix('');
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const context = use(MapLayersContext);
|
|
376
|
+
const DEFAULT_URL
|
|
377
|
+
= 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png';
|
|
378
|
+
const DEFAULT_DARK_URL
|
|
379
|
+
= 'https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png';
|
|
380
|
+
|
|
381
|
+
const { isDark } = useKivaTheme();
|
|
382
|
+
const resolvedUrl
|
|
383
|
+
= isDark ? (darkUrl ?? url ?? DEFAULT_DARK_URL) : (url ?? DEFAULT_URL);
|
|
384
|
+
const resolvedAttribution
|
|
385
|
+
= isDark && darkAttribution ? darkAttribution : (attribution
|
|
386
|
+
?? '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, © <a href="https://carto.com/attributions">CARTO</a>');
|
|
387
|
+
|
|
388
|
+
useEffect(() => {
|
|
389
|
+
if (context) {
|
|
390
|
+
context.registerTileLayer({
|
|
391
|
+
name,
|
|
392
|
+
url: resolvedUrl,
|
|
393
|
+
attribution: resolvedAttribution
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
}, [
|
|
397
|
+
context,
|
|
398
|
+
name,
|
|
399
|
+
resolvedUrl,
|
|
400
|
+
resolvedAttribution
|
|
401
|
+
]);
|
|
402
|
+
|
|
403
|
+
if (context && context.selectedTileLayer !== name) {
|
|
404
|
+
return null;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return (
|
|
408
|
+
<LeafletTileLayer
|
|
409
|
+
url={resolvedUrl}
|
|
410
|
+
attribution={resolvedAttribution}
|
|
411
|
+
{...props} />
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/* -------------------------------------------------------------------------- */
|
|
416
|
+
/* MapLayerGroup / MapFeatureGroup */
|
|
417
|
+
/* -------------------------------------------------------------------------- */
|
|
418
|
+
|
|
419
|
+
function MapLayerGroup({
|
|
420
|
+
name,
|
|
421
|
+
disabled,
|
|
422
|
+
...props
|
|
423
|
+
}: LayerGroupProps & MapLayerGroupOption & { ref?: Ref<LayerGroup> }) {
|
|
424
|
+
const context = useMapLayersContext();
|
|
425
|
+
|
|
426
|
+
useEffect(() => {
|
|
427
|
+
if (context) {
|
|
428
|
+
context.registerLayerGroup({
|
|
429
|
+
name,
|
|
430
|
+
disabled
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
}, [context, name, disabled]);
|
|
434
|
+
|
|
435
|
+
if (context && !context.activeLayerGroups.includes(name)) {
|
|
436
|
+
return null;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return <LeafletLayerGroup {...props} />;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function MapFeatureGroup({
|
|
443
|
+
name,
|
|
444
|
+
disabled,
|
|
445
|
+
...props
|
|
446
|
+
}: LayerGroupProps & MapLayerGroupOption & { ref?: Ref<FeatureGroup> }) {
|
|
447
|
+
const context = useMapLayersContext();
|
|
448
|
+
|
|
449
|
+
useEffect(() => {
|
|
450
|
+
if (context) {
|
|
451
|
+
context.registerLayerGroup({
|
|
452
|
+
name,
|
|
453
|
+
disabled
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
}, [context, name, disabled]);
|
|
457
|
+
|
|
458
|
+
if (context && !context.activeLayerGroups.includes(name)) {
|
|
459
|
+
return null;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
return <LeafletFeatureGroup {...props} />;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/* -------------------------------------------------------------------------- */
|
|
466
|
+
/* MapLayers */
|
|
467
|
+
/* -------------------------------------------------------------------------- */
|
|
468
|
+
|
|
469
|
+
const EMPTY_LAYER_GROUPS: never[] = [];
|
|
470
|
+
|
|
471
|
+
function MapLayers({
|
|
472
|
+
defaultTileLayer,
|
|
473
|
+
defaultLayerGroups = EMPTY_LAYER_GROUPS,
|
|
474
|
+
...props
|
|
475
|
+
}: Omit<ComponentProps<typeof MapLayersContext.Provider>, 'value'> & {
|
|
476
|
+
defaultTileLayer?: string;
|
|
477
|
+
defaultLayerGroups?: string[];
|
|
478
|
+
}) {
|
|
479
|
+
const [tileLayers, setTileLayers] = useState<MapTileLayerOption[]>([]);
|
|
480
|
+
const [selectedTileLayer, setSelectedTileLayer] = useState<string>(
|
|
481
|
+
defaultTileLayer || ''
|
|
482
|
+
);
|
|
483
|
+
const [layerGroups, setLayerGroups] = useState<MapLayerGroupOption[]>([]);
|
|
484
|
+
const [activeLayerGroups, setActiveLayerGroups]
|
|
485
|
+
= useState<string[]>(defaultLayerGroups);
|
|
486
|
+
|
|
487
|
+
function registerTileLayer(tileLayer: MapTileLayerOption) {
|
|
488
|
+
setTileLayers((prevTileLayers) => {
|
|
489
|
+
if (prevTileLayers.some(layer => layer.name === tileLayer.name)) {
|
|
490
|
+
return prevTileLayers;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
return [...prevTileLayers, tileLayer];
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function registerLayerGroup(layerGroup: MapLayerGroupOption) {
|
|
498
|
+
setLayerGroups((prevLayerGroups) => {
|
|
499
|
+
if (
|
|
500
|
+
prevLayerGroups.some(group => group.name === layerGroup.name)
|
|
501
|
+
) {
|
|
502
|
+
return prevLayerGroups;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
return [...prevLayerGroups, layerGroup];
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
useEffect(() => {
|
|
510
|
+
if (
|
|
511
|
+
defaultTileLayer
|
|
512
|
+
&& tileLayers.length > 0
|
|
513
|
+
&& !tileLayers.some(tileLayer => tileLayer.name === defaultTileLayer)
|
|
514
|
+
) {
|
|
515
|
+
throw new Error(
|
|
516
|
+
`Invalid defaultTileLayer "${defaultTileLayer}" provided to MapLayers. It must match a MapTileLayer's name prop.`
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if (tileLayers.length > 0 && !selectedTileLayer) {
|
|
521
|
+
const validDefaultValue
|
|
522
|
+
= defaultTileLayer
|
|
523
|
+
&& tileLayers.some(layer => layer.name === defaultTileLayer) ? defaultTileLayer : tileLayers[0]?.name ?? '';
|
|
524
|
+
|
|
525
|
+
queueMicrotask(() => setSelectedTileLayer(validDefaultValue));
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if (
|
|
529
|
+
defaultLayerGroups.length > 0
|
|
530
|
+
&& layerGroups.length > 0
|
|
531
|
+
&& defaultLayerGroups.some(
|
|
532
|
+
name => !layerGroups.some(group => group.name === name)
|
|
533
|
+
)
|
|
534
|
+
) {
|
|
535
|
+
throw new Error(
|
|
536
|
+
`Invalid defaultLayerGroups value provided to MapLayers. All names must match a MapLayerGroup's name prop.`
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
}, [
|
|
540
|
+
tileLayers,
|
|
541
|
+
defaultTileLayer,
|
|
542
|
+
selectedTileLayer,
|
|
543
|
+
layerGroups,
|
|
544
|
+
defaultLayerGroups
|
|
545
|
+
]);
|
|
546
|
+
|
|
547
|
+
return (
|
|
548
|
+
<MapLayersContext
|
|
549
|
+
value={{
|
|
550
|
+
registerTileLayer,
|
|
551
|
+
tileLayers,
|
|
552
|
+
selectedTileLayer,
|
|
553
|
+
setSelectedTileLayer,
|
|
554
|
+
registerLayerGroup,
|
|
555
|
+
layerGroups,
|
|
556
|
+
activeLayerGroups,
|
|
557
|
+
setActiveLayerGroups
|
|
558
|
+
}}
|
|
559
|
+
{...props} />
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/* -------------------------------------------------------------------------- */
|
|
564
|
+
/* MapLayersControl */
|
|
565
|
+
/* -------------------------------------------------------------------------- */
|
|
566
|
+
|
|
567
|
+
function MapLayersControl({
|
|
568
|
+
tileLayersLabel = 'Map Type',
|
|
569
|
+
layerGroupsLabel = 'Layers',
|
|
570
|
+
position = 'top-1 right-1',
|
|
571
|
+
className,
|
|
572
|
+
...props
|
|
573
|
+
}: ComponentProps<'button'> & {
|
|
574
|
+
tileLayersLabel?: string;
|
|
575
|
+
layerGroupsLabel?: string;
|
|
576
|
+
position?: string;
|
|
577
|
+
}) {
|
|
578
|
+
const { t } = useUiTranslation();
|
|
579
|
+
const layersContext = useMapLayersContext();
|
|
580
|
+
|
|
581
|
+
if (!layersContext) {
|
|
582
|
+
throw new Error('MapLayersControl must be used within MapLayers');
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const {
|
|
586
|
+
tileLayers,
|
|
587
|
+
selectedTileLayer,
|
|
588
|
+
setSelectedTileLayer,
|
|
589
|
+
layerGroups,
|
|
590
|
+
activeLayerGroups,
|
|
591
|
+
setActiveLayerGroups
|
|
592
|
+
} = layersContext;
|
|
593
|
+
|
|
594
|
+
if (tileLayers.length === 0 && layerGroups.length === 0) {
|
|
595
|
+
return null;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function handleLayerGroupToggle(name: string, checked: boolean) {
|
|
599
|
+
setActiveLayerGroups(
|
|
600
|
+
checked ? [...activeLayerGroups, name] : activeLayerGroups.filter(groupName => groupName !== name)
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
const showTileLayersDropdown = tileLayers.length > 1;
|
|
605
|
+
const showLayerGroupsDropdown = layerGroups.length > 0;
|
|
606
|
+
|
|
607
|
+
if (!showTileLayersDropdown && !showLayerGroupsDropdown) {
|
|
608
|
+
return null;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
return (
|
|
612
|
+
<DropdownMenu>
|
|
613
|
+
<DropdownMenuTrigger asChild>
|
|
614
|
+
<Button
|
|
615
|
+
type="button"
|
|
616
|
+
variant="secondary"
|
|
617
|
+
size="icon-sm"
|
|
618
|
+
aria-label={t('ui.map.selectLayers', 'Select layers')}
|
|
619
|
+
title={t('ui.map.selectLayers', 'Select layers')}
|
|
620
|
+
className={cn(
|
|
621
|
+
'absolute z-1000 border',
|
|
622
|
+
position,
|
|
623
|
+
className
|
|
624
|
+
)}
|
|
625
|
+
{...props}>
|
|
626
|
+
<LayersIcon />
|
|
627
|
+
</Button>
|
|
628
|
+
</DropdownMenuTrigger>
|
|
629
|
+
<DropdownMenuContent align="end" className="z-1000">
|
|
630
|
+
{showTileLayersDropdown && (
|
|
631
|
+
<>
|
|
632
|
+
<DropdownMenuLabel>{tileLayersLabel}</DropdownMenuLabel>
|
|
633
|
+
<DropdownMenuRadioGroup
|
|
634
|
+
value={selectedTileLayer}
|
|
635
|
+
onValueChange={setSelectedTileLayer}>
|
|
636
|
+
{tileLayers.map(tileLayer => (
|
|
637
|
+
<DropdownMenuRadioItem
|
|
638
|
+
key={tileLayer.name}
|
|
639
|
+
value={tileLayer.name}>
|
|
640
|
+
{tileLayer.name}
|
|
641
|
+
</DropdownMenuRadioItem>
|
|
642
|
+
))}
|
|
643
|
+
</DropdownMenuRadioGroup>
|
|
644
|
+
</>
|
|
645
|
+
)}
|
|
646
|
+
{showTileLayersDropdown && showLayerGroupsDropdown && (
|
|
647
|
+
<DropdownMenuSeparator />
|
|
648
|
+
)}
|
|
649
|
+
{showLayerGroupsDropdown && (
|
|
650
|
+
<>
|
|
651
|
+
<DropdownMenuLabel>
|
|
652
|
+
{layerGroupsLabel}
|
|
653
|
+
</DropdownMenuLabel>
|
|
654
|
+
{layerGroups.map(layerGroup => (
|
|
655
|
+
<DropdownMenuCheckboxItem
|
|
656
|
+
key={layerGroup.name}
|
|
657
|
+
checked={activeLayerGroups.includes(
|
|
658
|
+
layerGroup.name
|
|
659
|
+
)}
|
|
660
|
+
disabled={layerGroup.disabled}
|
|
661
|
+
onCheckedChange={checked => handleLayerGroupToggle(
|
|
662
|
+
layerGroup.name,
|
|
663
|
+
checked
|
|
664
|
+
)}>
|
|
665
|
+
{layerGroup.name}
|
|
666
|
+
</DropdownMenuCheckboxItem>
|
|
667
|
+
))}
|
|
668
|
+
</>
|
|
669
|
+
)}
|
|
670
|
+
</DropdownMenuContent>
|
|
671
|
+
</DropdownMenu>
|
|
672
|
+
);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/* -------------------------------------------------------------------------- */
|
|
676
|
+
/* MapMarker */
|
|
677
|
+
/* -------------------------------------------------------------------------- */
|
|
678
|
+
|
|
679
|
+
function MapMarker({
|
|
680
|
+
icon = <MapPinIcon className="size-6" />,
|
|
681
|
+
iconAnchor = [12, 12],
|
|
682
|
+
bgPos,
|
|
683
|
+
popupAnchor,
|
|
684
|
+
tooltipAnchor,
|
|
685
|
+
...props
|
|
686
|
+
}: Omit<MarkerProps, 'icon'>
|
|
687
|
+
& Pick<
|
|
688
|
+
DivIconOptions,
|
|
689
|
+
'iconAnchor' | 'bgPos' | 'popupAnchor' | 'tooltipAnchor'
|
|
690
|
+
> & {
|
|
691
|
+
icon?: ReactNode;
|
|
692
|
+
ref?: Ref<Marker>;
|
|
693
|
+
}) {
|
|
694
|
+
const { L } = useLeaflet();
|
|
695
|
+
|
|
696
|
+
if (!L) return null;
|
|
697
|
+
|
|
698
|
+
return (
|
|
699
|
+
<LeafletMarker
|
|
700
|
+
icon={L.divIcon({
|
|
701
|
+
className: '',
|
|
702
|
+
html: renderToString(icon),
|
|
703
|
+
iconAnchor,
|
|
704
|
+
...(bgPos ? { bgPos } : {}),
|
|
705
|
+
...(popupAnchor ? { popupAnchor } : {}),
|
|
706
|
+
...(tooltipAnchor ? { tooltipAnchor } : {})
|
|
707
|
+
})}
|
|
708
|
+
riseOnHover
|
|
709
|
+
{...props} />
|
|
710
|
+
);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
/* -------------------------------------------------------------------------- */
|
|
714
|
+
/* MapMarkerClusterGroup */
|
|
715
|
+
/* -------------------------------------------------------------------------- */
|
|
716
|
+
|
|
717
|
+
function MapMarkerClusterGroup({
|
|
718
|
+
polygonOptions = {
|
|
719
|
+
className: 'fill-foreground stroke-foreground stroke-2'
|
|
720
|
+
},
|
|
721
|
+
spiderLegPolylineOptions = {
|
|
722
|
+
className: 'fill-foreground stroke-foreground stroke-2'
|
|
723
|
+
},
|
|
724
|
+
icon,
|
|
725
|
+
...props
|
|
726
|
+
}: Omit<MarkerClusterGroupProps, 'iconCreateFunction'> & {
|
|
727
|
+
children: ReactNode;
|
|
728
|
+
icon?: (markerCount: number) => ReactNode;
|
|
729
|
+
}) {
|
|
730
|
+
const { L } = useLeaflet();
|
|
731
|
+
|
|
732
|
+
if (!L) return null;
|
|
733
|
+
|
|
734
|
+
const iconCreateFunction = icon ? (cluster: MarkerCluster) => {
|
|
735
|
+
const markerCount = cluster.getChildCount();
|
|
736
|
+
const iconNode = icon(markerCount);
|
|
737
|
+
|
|
738
|
+
return L.divIcon({
|
|
739
|
+
html: renderToString(iconNode)
|
|
740
|
+
});
|
|
741
|
+
} : undefined;
|
|
742
|
+
|
|
743
|
+
return (
|
|
744
|
+
<LeafletMarkerClusterGroup
|
|
745
|
+
polygonOptions={polygonOptions}
|
|
746
|
+
spiderLegPolylineOptions={spiderLegPolylineOptions}
|
|
747
|
+
iconCreateFunction={iconCreateFunction}
|
|
748
|
+
{...props} />
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
/* -------------------------------------------------------------------------- */
|
|
753
|
+
/* Map shapes */
|
|
754
|
+
/* -------------------------------------------------------------------------- */
|
|
755
|
+
|
|
756
|
+
function MapCircle({
|
|
757
|
+
className,
|
|
758
|
+
...props
|
|
759
|
+
}: CircleProps & { ref?: Ref<Circle> }) {
|
|
760
|
+
return (
|
|
761
|
+
<LeafletCircle
|
|
762
|
+
className={cn(
|
|
763
|
+
'fill-foreground stroke-foreground stroke-2',
|
|
764
|
+
className
|
|
765
|
+
)}
|
|
766
|
+
{...props} />
|
|
767
|
+
);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
function MapCircleMarker({
|
|
771
|
+
className,
|
|
772
|
+
...props
|
|
773
|
+
}: CircleMarkerProps & { ref?: Ref<CircleMarker> }) {
|
|
774
|
+
return (
|
|
775
|
+
<LeafletCircleMarker
|
|
776
|
+
className={cn(
|
|
777
|
+
'fill-foreground stroke-foreground stroke-2',
|
|
778
|
+
className
|
|
779
|
+
)}
|
|
780
|
+
{...props} />
|
|
781
|
+
);
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
function MapPolyline({
|
|
785
|
+
className,
|
|
786
|
+
...props
|
|
787
|
+
}: PolylineProps & { ref?: Ref<Polyline> }) {
|
|
788
|
+
return (
|
|
789
|
+
<LeafletPolyline
|
|
790
|
+
className={cn(
|
|
791
|
+
'fill-foreground stroke-foreground stroke-2',
|
|
792
|
+
className
|
|
793
|
+
)}
|
|
794
|
+
{...props} />
|
|
795
|
+
);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
function MapPolygon({
|
|
799
|
+
className,
|
|
800
|
+
...props
|
|
801
|
+
}: PolygonProps & { ref?: Ref<Polygon> }) {
|
|
802
|
+
return (
|
|
803
|
+
<LeafletPolygon
|
|
804
|
+
className={cn(
|
|
805
|
+
'fill-foreground stroke-foreground stroke-2',
|
|
806
|
+
className
|
|
807
|
+
)}
|
|
808
|
+
{...props} />
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
function MapRectangle({
|
|
813
|
+
className,
|
|
814
|
+
...props
|
|
815
|
+
}: RectangleProps & { ref?: Ref<Rectangle> }) {
|
|
816
|
+
return (
|
|
817
|
+
<LeafletRectangle
|
|
818
|
+
className={cn(
|
|
819
|
+
'fill-foreground stroke-foreground stroke-2',
|
|
820
|
+
className
|
|
821
|
+
)}
|
|
822
|
+
{...props} />
|
|
823
|
+
);
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/* -------------------------------------------------------------------------- */
|
|
827
|
+
/* MapPopup / MapTooltip */
|
|
828
|
+
/* -------------------------------------------------------------------------- */
|
|
829
|
+
|
|
830
|
+
function MapPopup({
|
|
831
|
+
className,
|
|
832
|
+
...props
|
|
833
|
+
}: Omit<PopupProps, 'content'> & { ref?: Ref<Popup> }) {
|
|
834
|
+
return (
|
|
835
|
+
<LeafletPopup
|
|
836
|
+
className={cn(
|
|
837
|
+
'bg-popover text-popover-foreground animate-in fade-out-0 fade-in-0 zoom-out-95 zoom-in-95 slide-in-from-bottom-2 z-50 w-72 rounded-md border p-4 font-sans shadow-md outline-hidden',
|
|
838
|
+
className
|
|
839
|
+
)}
|
|
840
|
+
{...props} />
|
|
841
|
+
);
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
function MapTooltip({
|
|
845
|
+
className,
|
|
846
|
+
children,
|
|
847
|
+
side = 'top',
|
|
848
|
+
sideOffset = 15,
|
|
849
|
+
...props
|
|
850
|
+
}: Omit<TooltipProps, 'offset'> & {
|
|
851
|
+
side?: 'top' | 'right' | 'bottom' | 'left';
|
|
852
|
+
sideOffset?: number;
|
|
853
|
+
ref?: Ref<Tooltip>;
|
|
854
|
+
}) {
|
|
855
|
+
const ARROW_POSITION_CLASSES = {
|
|
856
|
+
top: 'bottom-0.5 left-1/2 -translate-x-1/2 translate-y-1/2',
|
|
857
|
+
bottom: 'top-0.5 left-1/2 -translate-x-1/2 -translate-y-1/2',
|
|
858
|
+
left: 'right-0.5 top-1/2 translate-x-1/2 -translate-y-1/2',
|
|
859
|
+
right: 'left-0.5 top-1/2 -translate-x-1/2 -translate-y-1/2'
|
|
860
|
+
};
|
|
861
|
+
const DEFAULT_OFFSET = {
|
|
862
|
+
top: [0, -sideOffset] satisfies PointExpression,
|
|
863
|
+
bottom: [0, sideOffset] satisfies PointExpression,
|
|
864
|
+
left: [-sideOffset, 0] satisfies PointExpression,
|
|
865
|
+
right: [sideOffset, 0] satisfies PointExpression
|
|
866
|
+
};
|
|
867
|
+
|
|
868
|
+
return (
|
|
869
|
+
<LeafletTooltip
|
|
870
|
+
className={cn(
|
|
871
|
+
'animate-in fade-in-0 zoom-in-95 fade-out-0 zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 relative z-50 w-fit text-xs text-balance transition-opacity',
|
|
872
|
+
className
|
|
873
|
+
)}
|
|
874
|
+
data-side={side}
|
|
875
|
+
direction={side}
|
|
876
|
+
offset={DEFAULT_OFFSET[side]}
|
|
877
|
+
opacity={1}
|
|
878
|
+
{...props}>
|
|
879
|
+
{children}
|
|
880
|
+
<div
|
|
881
|
+
className={cn(
|
|
882
|
+
'bg-foreground fill-foreground absolute z-50 size-2.5 rotate-45 rounded-[2px]',
|
|
883
|
+
ARROW_POSITION_CLASSES[side]
|
|
884
|
+
)} />
|
|
885
|
+
</LeafletTooltip>
|
|
886
|
+
);
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
/* -------------------------------------------------------------------------- */
|
|
890
|
+
/* MapControlContainer */
|
|
891
|
+
/* -------------------------------------------------------------------------- */
|
|
892
|
+
|
|
893
|
+
function MapControlContainer({
|
|
894
|
+
className,
|
|
895
|
+
...props
|
|
896
|
+
}: ComponentProps<'div'>) {
|
|
897
|
+
const { L } = useLeaflet();
|
|
898
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
899
|
+
|
|
900
|
+
useEffect(() => {
|
|
901
|
+
if (!L) return;
|
|
902
|
+
const element = containerRef.current;
|
|
903
|
+
|
|
904
|
+
if (!element) return;
|
|
905
|
+
L.DomEvent.disableClickPropagation(element);
|
|
906
|
+
L.DomEvent.disableScrollPropagation(element);
|
|
907
|
+
}, [L]);
|
|
908
|
+
|
|
909
|
+
return (
|
|
910
|
+
<div
|
|
911
|
+
ref={containerRef}
|
|
912
|
+
className={cn('absolute z-1000 size-fit cursor-default', className)}
|
|
913
|
+
{...props} />
|
|
914
|
+
);
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/* -------------------------------------------------------------------------- */
|
|
918
|
+
/* MapZoomControl */
|
|
919
|
+
/* -------------------------------------------------------------------------- */
|
|
920
|
+
|
|
921
|
+
function MapZoomControl({
|
|
922
|
+
position = 'top-1 left-1',
|
|
923
|
+
className,
|
|
924
|
+
...props
|
|
925
|
+
}: ComponentProps<'div'> & { position?: string }) {
|
|
926
|
+
const { t } = useUiTranslation();
|
|
927
|
+
const map = useMap();
|
|
928
|
+
const [zoomLevel, setZoomLevel] = useState(() => map.getZoom());
|
|
929
|
+
|
|
930
|
+
useMapEvents({
|
|
931
|
+
zoomend: () => {
|
|
932
|
+
setZoomLevel(map.getZoom());
|
|
933
|
+
}
|
|
934
|
+
});
|
|
935
|
+
|
|
936
|
+
return (
|
|
937
|
+
<MapControlContainer className={cn(position, className)}>
|
|
938
|
+
<ButtonGroup
|
|
939
|
+
orientation="vertical"
|
|
940
|
+
aria-label={t('ui.map.zoomControls', 'Zoom controls')}
|
|
941
|
+
{...props}>
|
|
942
|
+
<Button
|
|
943
|
+
type="button"
|
|
944
|
+
size="icon-sm"
|
|
945
|
+
variant="secondary"
|
|
946
|
+
aria-label={t('ui.map.zoomIn', 'Zoom in')}
|
|
947
|
+
title={t('ui.map.zoomIn', 'Zoom in')}
|
|
948
|
+
className="border"
|
|
949
|
+
disabled={zoomLevel >= map.getMaxZoom()}
|
|
950
|
+
onClick={() => map.zoomIn()}>
|
|
951
|
+
<PlusIcon />
|
|
952
|
+
</Button>
|
|
953
|
+
<Button
|
|
954
|
+
type="button"
|
|
955
|
+
size="icon-sm"
|
|
956
|
+
variant="secondary"
|
|
957
|
+
aria-label={t('ui.map.zoomOut', 'Zoom out')}
|
|
958
|
+
title={t('ui.map.zoomOut', 'Zoom out')}
|
|
959
|
+
className="border"
|
|
960
|
+
disabled={zoomLevel <= map.getMinZoom()}
|
|
961
|
+
onClick={() => map.zoomOut()}>
|
|
962
|
+
<MinusIcon />
|
|
963
|
+
</Button>
|
|
964
|
+
</ButtonGroup>
|
|
965
|
+
</MapControlContainer>
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
/* -------------------------------------------------------------------------- */
|
|
970
|
+
/* MapFullscreenControl */
|
|
971
|
+
/* -------------------------------------------------------------------------- */
|
|
972
|
+
|
|
973
|
+
function MapFullscreenControl({
|
|
974
|
+
position = 'top-1 right-1',
|
|
975
|
+
className,
|
|
976
|
+
...props
|
|
977
|
+
}: ComponentProps<'button'> & { position?: string }) {
|
|
978
|
+
const map = useMap();
|
|
979
|
+
const [isFullscreen, setIsFullscreen] = useState(false);
|
|
980
|
+
|
|
981
|
+
const { L } = useLeaflet();
|
|
982
|
+
|
|
983
|
+
useEffect(() => {
|
|
984
|
+
if (!L) return;
|
|
985
|
+
|
|
986
|
+
const fullscreenControl = new L.Control.FullScreen();
|
|
987
|
+
|
|
988
|
+
fullscreenControl.addTo(map);
|
|
989
|
+
|
|
990
|
+
const container = fullscreenControl.getContainer();
|
|
991
|
+
|
|
992
|
+
if (container) {
|
|
993
|
+
container.style.display = 'none';
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
const handleEnter = () => setIsFullscreen(true);
|
|
997
|
+
const handleExit = () => setIsFullscreen(false);
|
|
998
|
+
|
|
999
|
+
map.on('enterFullscreen', handleEnter);
|
|
1000
|
+
map.on('exitFullscreen', handleExit);
|
|
1001
|
+
|
|
1002
|
+
return () => {
|
|
1003
|
+
fullscreenControl.remove();
|
|
1004
|
+
map.off('enterFullscreen', handleEnter);
|
|
1005
|
+
map.off('exitFullscreen', handleExit);
|
|
1006
|
+
};
|
|
1007
|
+
}, [L, map]);
|
|
1008
|
+
|
|
1009
|
+
return (
|
|
1010
|
+
<MapControlContainer className={cn(position, className)}>
|
|
1011
|
+
<Button
|
|
1012
|
+
type="button"
|
|
1013
|
+
size="icon-sm"
|
|
1014
|
+
variant="secondary"
|
|
1015
|
+
onClick={() => map.toggleFullscreen()}
|
|
1016
|
+
aria-label={isFullscreen ? 'Exit fullscreen' : 'Enter fullscreen'}
|
|
1017
|
+
title={isFullscreen ? 'Exit fullscreen' : 'Enter fullscreen'}
|
|
1018
|
+
className="border"
|
|
1019
|
+
{...props}>
|
|
1020
|
+
{isFullscreen ? <MinimizeIcon /> : <MaximizeIcon />}
|
|
1021
|
+
</Button>
|
|
1022
|
+
</MapControlContainer>
|
|
1023
|
+
);
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
/* -------------------------------------------------------------------------- */
|
|
1027
|
+
/* MapLocateControl */
|
|
1028
|
+
/* -------------------------------------------------------------------------- */
|
|
1029
|
+
|
|
1030
|
+
function MapLocatePulseIcon() {
|
|
1031
|
+
return (
|
|
1032
|
+
<div className="absolute -top-1 -right-1 flex size-3 rounded-full">
|
|
1033
|
+
<div className="bg-primary absolute inline-flex size-full animate-ping rounded-full opacity-75" />
|
|
1034
|
+
<div className="bg-primary relative inline-flex size-3 rounded-full" />
|
|
1035
|
+
</div>
|
|
1036
|
+
);
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
function MapLocateControl({
|
|
1040
|
+
watch = false,
|
|
1041
|
+
onLocationFound,
|
|
1042
|
+
onLocationError,
|
|
1043
|
+
position = 'right-1 bottom-1',
|
|
1044
|
+
className,
|
|
1045
|
+
...props
|
|
1046
|
+
}: ComponentProps<'button'>
|
|
1047
|
+
& Pick<LocateOptions, 'watch'> & {
|
|
1048
|
+
onLocationFound?: (location: LocationEvent) => void;
|
|
1049
|
+
onLocationError?: (error: ErrorEvent) => void;
|
|
1050
|
+
} & { position?: string }) {
|
|
1051
|
+
const map = useMap();
|
|
1052
|
+
const [isLocating, setIsLocating] = useDebounceLoadingState(200);
|
|
1053
|
+
const [location, setLocation] = useState<LatLngExpression | null>(null);
|
|
1054
|
+
|
|
1055
|
+
function startLocating() {
|
|
1056
|
+
setIsLocating(true);
|
|
1057
|
+
map.locate({ setView: true, maxZoom: map.getMaxZoom(), watch });
|
|
1058
|
+
map.on('locationfound', (loc: LocationEvent) => {
|
|
1059
|
+
setLocation(loc.latlng);
|
|
1060
|
+
setIsLocating(false);
|
|
1061
|
+
onLocationFound?.(loc);
|
|
1062
|
+
});
|
|
1063
|
+
map.on('locationerror', (error: ErrorEvent) => {
|
|
1064
|
+
setLocation(null);
|
|
1065
|
+
setIsLocating(false);
|
|
1066
|
+
onLocationError?.(error);
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
const stopLocating = useCallback(() => {
|
|
1071
|
+
map.stopLocate();
|
|
1072
|
+
map.off('locationfound');
|
|
1073
|
+
map.off('locationerror');
|
|
1074
|
+
setLocation(null);
|
|
1075
|
+
setIsLocating(false);
|
|
1076
|
+
}, [map, setIsLocating]);
|
|
1077
|
+
|
|
1078
|
+
useEffect(() => () => stopLocating(), [stopLocating]);
|
|
1079
|
+
|
|
1080
|
+
return (
|
|
1081
|
+
<MapControlContainer className={cn(position, className)}>
|
|
1082
|
+
<Button
|
|
1083
|
+
type="button"
|
|
1084
|
+
size="icon-sm"
|
|
1085
|
+
variant={location ? 'default' : 'secondary'}
|
|
1086
|
+
onClick={location ? stopLocating : startLocating}
|
|
1087
|
+
disabled={isLocating}
|
|
1088
|
+
title={isLocating ? 'Locating...' : location ? 'Stop tracking' : 'Track location'}
|
|
1089
|
+
aria-label={isLocating ? 'Locating...' : location ? 'Stop location tracking' : 'Start location tracking'}
|
|
1090
|
+
className="border"
|
|
1091
|
+
{...props}>
|
|
1092
|
+
{isLocating ? (
|
|
1093
|
+
<LoaderCircleIcon className="animate-spin" />
|
|
1094
|
+
) : (
|
|
1095
|
+
<NavigationIcon />
|
|
1096
|
+
)}
|
|
1097
|
+
</Button>
|
|
1098
|
+
{location && (
|
|
1099
|
+
<MapMarker position={location} icon={<MapLocatePulseIcon />} />
|
|
1100
|
+
)}
|
|
1101
|
+
</MapControlContainer>
|
|
1102
|
+
);
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
/* -------------------------------------------------------------------------- */
|
|
1106
|
+
/* MapSearchControl */
|
|
1107
|
+
/* -------------------------------------------------------------------------- */
|
|
1108
|
+
|
|
1109
|
+
function MapSearchControl({
|
|
1110
|
+
position = 'top-1 left-1',
|
|
1111
|
+
className,
|
|
1112
|
+
...props
|
|
1113
|
+
}: PlaceAutocompleteProps & { position?: string }) {
|
|
1114
|
+
return (
|
|
1115
|
+
<MapControlContainer className={cn('z-1001 w-60', position, className)}>
|
|
1116
|
+
<PlaceAutocomplete {...props} />
|
|
1117
|
+
</MapControlContainer>
|
|
1118
|
+
);
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
/* -------------------------------------------------------------------------- */
|
|
1122
|
+
/* MapDrawControl */
|
|
1123
|
+
/* -------------------------------------------------------------------------- */
|
|
1124
|
+
|
|
1125
|
+
function MapDrawControl({
|
|
1126
|
+
onLayersChange,
|
|
1127
|
+
position = 'bottom-1 left-1',
|
|
1128
|
+
className,
|
|
1129
|
+
...props
|
|
1130
|
+
}: ComponentProps<'div'> & {
|
|
1131
|
+
onLayersChange?: (layers: L.FeatureGroup) => void;
|
|
1132
|
+
position?: string;
|
|
1133
|
+
}) {
|
|
1134
|
+
const { L, LeafletDraw } = useLeaflet();
|
|
1135
|
+
const map = useMap();
|
|
1136
|
+
const featureGroupRef = useRef<L.FeatureGroup | null>(null);
|
|
1137
|
+
const editControlRef = useRef<EditToolbar.Edit | null>(null);
|
|
1138
|
+
const deleteControlRef = useRef<EditToolbar.Delete | null>(null);
|
|
1139
|
+
const [activeMode, setActiveMode] = useState<MapDrawMode>(null);
|
|
1140
|
+
const [layersCount, setLayersCount] = useState(0);
|
|
1141
|
+
|
|
1142
|
+
const updateLayersCount = useCallback(() => {
|
|
1143
|
+
if (featureGroupRef.current) {
|
|
1144
|
+
setLayersCount(featureGroupRef.current.getLayers().length);
|
|
1145
|
+
}
|
|
1146
|
+
}, []);
|
|
1147
|
+
|
|
1148
|
+
const handleDrawCreated = useCallback((event: DrawEvents.Created) => {
|
|
1149
|
+
if (!featureGroupRef.current) return;
|
|
1150
|
+
const { layer } = event;
|
|
1151
|
+
|
|
1152
|
+
featureGroupRef.current.addLayer(layer);
|
|
1153
|
+
onLayersChange?.(featureGroupRef.current);
|
|
1154
|
+
updateLayersCount();
|
|
1155
|
+
setActiveMode(null);
|
|
1156
|
+
}, [onLayersChange, updateLayersCount]);
|
|
1157
|
+
|
|
1158
|
+
const handleDrawEditedOrDeleted = useCallback(() => {
|
|
1159
|
+
if (!featureGroupRef.current) return;
|
|
1160
|
+
onLayersChange?.(featureGroupRef.current);
|
|
1161
|
+
updateLayersCount();
|
|
1162
|
+
setActiveMode(null);
|
|
1163
|
+
}, [onLayersChange, updateLayersCount]);
|
|
1164
|
+
|
|
1165
|
+
useEffect(() => {
|
|
1166
|
+
if (!L || !LeafletDraw || !map) return;
|
|
1167
|
+
|
|
1168
|
+
map.on(
|
|
1169
|
+
L.Draw.Event.CREATED,
|
|
1170
|
+
handleDrawCreated as L.LeafletEventHandlerFn
|
|
1171
|
+
);
|
|
1172
|
+
map.on(L.Draw.Event.EDITED, handleDrawEditedOrDeleted);
|
|
1173
|
+
map.on(L.Draw.Event.DELETED, handleDrawEditedOrDeleted);
|
|
1174
|
+
|
|
1175
|
+
return () => {
|
|
1176
|
+
map.off(
|
|
1177
|
+
L.Draw.Event.CREATED,
|
|
1178
|
+
handleDrawCreated as L.LeafletEventHandlerFn
|
|
1179
|
+
);
|
|
1180
|
+
map.off(L.Draw.Event.EDITED, handleDrawEditedOrDeleted);
|
|
1181
|
+
map.off(L.Draw.Event.DELETED, handleDrawEditedOrDeleted);
|
|
1182
|
+
};
|
|
1183
|
+
}, [
|
|
1184
|
+
L,
|
|
1185
|
+
LeafletDraw,
|
|
1186
|
+
map,
|
|
1187
|
+
handleDrawCreated,
|
|
1188
|
+
handleDrawEditedOrDeleted
|
|
1189
|
+
]);
|
|
1190
|
+
|
|
1191
|
+
return (
|
|
1192
|
+
<MapDrawContext
|
|
1193
|
+
value={{
|
|
1194
|
+
featureGroup: featureGroupRef.current,
|
|
1195
|
+
activeMode,
|
|
1196
|
+
setActiveMode,
|
|
1197
|
+
editControlRef,
|
|
1198
|
+
deleteControlRef,
|
|
1199
|
+
layersCount
|
|
1200
|
+
}}>
|
|
1201
|
+
<LeafletFeatureGroup ref={featureGroupRef} />
|
|
1202
|
+
<MapControlContainer className={cn(position, className)}>
|
|
1203
|
+
<ButtonGroup orientation="vertical" {...props} />
|
|
1204
|
+
</MapControlContainer>
|
|
1205
|
+
</MapDrawContext>
|
|
1206
|
+
);
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
/* -------------------------------------------------------------------------- */
|
|
1210
|
+
/* MapDrawShapeButton (internal) */
|
|
1211
|
+
/* -------------------------------------------------------------------------- */
|
|
1212
|
+
|
|
1213
|
+
function MapDrawShapeButton<T extends Draw.Feature>({
|
|
1214
|
+
drawMode,
|
|
1215
|
+
createDrawTool,
|
|
1216
|
+
className,
|
|
1217
|
+
...props
|
|
1218
|
+
}: ComponentProps<'button'> & {
|
|
1219
|
+
drawMode: MapDrawShape;
|
|
1220
|
+
createDrawTool: (L: typeof LeafletNS, map: DrawMap) => T;
|
|
1221
|
+
}) {
|
|
1222
|
+
const drawContext = useMapDrawContext();
|
|
1223
|
+
|
|
1224
|
+
if (!drawContext) {
|
|
1225
|
+
throw new Error('MapDrawShapeButton must be used within MapDrawControl');
|
|
1226
|
+
}
|
|
1227
|
+
const { L } = useLeaflet();
|
|
1228
|
+
const map = useMap();
|
|
1229
|
+
const controlRef = useRef<T | null>(null);
|
|
1230
|
+
const { activeMode, setActiveMode } = drawContext;
|
|
1231
|
+
const isActive = activeMode === drawMode;
|
|
1232
|
+
|
|
1233
|
+
useEffect(() => {
|
|
1234
|
+
if (!L || !isActive) {
|
|
1235
|
+
controlRef.current?.disable();
|
|
1236
|
+
controlRef.current = null;
|
|
1237
|
+
|
|
1238
|
+
return;
|
|
1239
|
+
}
|
|
1240
|
+
const control = createDrawTool(L, map as DrawMap);
|
|
1241
|
+
|
|
1242
|
+
control.enable();
|
|
1243
|
+
controlRef.current = control;
|
|
1244
|
+
|
|
1245
|
+
return () => {
|
|
1246
|
+
control.disable();
|
|
1247
|
+
controlRef.current = null;
|
|
1248
|
+
};
|
|
1249
|
+
}, [
|
|
1250
|
+
L,
|
|
1251
|
+
map,
|
|
1252
|
+
isActive,
|
|
1253
|
+
createDrawTool
|
|
1254
|
+
]);
|
|
1255
|
+
|
|
1256
|
+
function handleClick() {
|
|
1257
|
+
setActiveMode(isActive ? null : drawMode);
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
return (
|
|
1261
|
+
<Button
|
|
1262
|
+
type="button"
|
|
1263
|
+
size="icon-sm"
|
|
1264
|
+
aria-label={`Draw ${drawMode}`}
|
|
1265
|
+
title={`Draw ${drawMode}`}
|
|
1266
|
+
className={cn('border', className)}
|
|
1267
|
+
variant={isActive ? 'default' : 'secondary'}
|
|
1268
|
+
disabled={activeMode === 'edit' || activeMode === 'delete'}
|
|
1269
|
+
onClick={handleClick}
|
|
1270
|
+
{...props} />
|
|
1271
|
+
);
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
/* -------------------------------------------------------------------------- */
|
|
1275
|
+
/* MapDraw shape tools */
|
|
1276
|
+
/* -------------------------------------------------------------------------- */
|
|
1277
|
+
|
|
1278
|
+
function MapDrawMarker({ ...props }: DrawOptions.MarkerOptions) {
|
|
1279
|
+
return (
|
|
1280
|
+
<MapDrawShapeButton
|
|
1281
|
+
drawMode="marker"
|
|
1282
|
+
createDrawTool={(L, map) => new L.Draw.Marker(map, {
|
|
1283
|
+
icon: L.divIcon({
|
|
1284
|
+
className: '',
|
|
1285
|
+
iconAnchor: [12, 12],
|
|
1286
|
+
html: renderToString(<MapPinIcon className="size-6" />)
|
|
1287
|
+
}),
|
|
1288
|
+
...props
|
|
1289
|
+
})}>
|
|
1290
|
+
<MapPinIcon />
|
|
1291
|
+
</MapDrawShapeButton>
|
|
1292
|
+
);
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
function MapDrawPolyline({
|
|
1296
|
+
showLength = false,
|
|
1297
|
+
drawError = {
|
|
1298
|
+
color: 'var(--color-destructive)'
|
|
1299
|
+
},
|
|
1300
|
+
shapeOptions = {
|
|
1301
|
+
color: 'var(--color-primary)',
|
|
1302
|
+
opacity: 1,
|
|
1303
|
+
weight: 2
|
|
1304
|
+
},
|
|
1305
|
+
...props
|
|
1306
|
+
}: DrawOptions.PolylineOptions) {
|
|
1307
|
+
const mapDrawHandleIcon = useMapDrawHandleIcon();
|
|
1308
|
+
|
|
1309
|
+
return (
|
|
1310
|
+
<MapDrawShapeButton
|
|
1311
|
+
drawMode="polyline"
|
|
1312
|
+
createDrawTool={(L, map) => new L.Draw.Polyline(map, {
|
|
1313
|
+
...(mapDrawHandleIcon ? {
|
|
1314
|
+
icon: mapDrawHandleIcon,
|
|
1315
|
+
touchIcon: mapDrawHandleIcon
|
|
1316
|
+
} : {}),
|
|
1317
|
+
showLength,
|
|
1318
|
+
drawError,
|
|
1319
|
+
shapeOptions,
|
|
1320
|
+
...props
|
|
1321
|
+
})}>
|
|
1322
|
+
<WaypointsIcon />
|
|
1323
|
+
</MapDrawShapeButton>
|
|
1324
|
+
);
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
function MapDrawCircle({
|
|
1328
|
+
showRadius = false,
|
|
1329
|
+
shapeOptions = {
|
|
1330
|
+
color: 'var(--color-primary)',
|
|
1331
|
+
opacity: 1,
|
|
1332
|
+
weight: 2
|
|
1333
|
+
},
|
|
1334
|
+
...props
|
|
1335
|
+
}: DrawOptions.CircleOptions) {
|
|
1336
|
+
return (
|
|
1337
|
+
<MapDrawShapeButton
|
|
1338
|
+
drawMode="circle"
|
|
1339
|
+
createDrawTool={(L, map) => new L.Draw.Circle(map, {
|
|
1340
|
+
showRadius,
|
|
1341
|
+
shapeOptions,
|
|
1342
|
+
...props
|
|
1343
|
+
})}>
|
|
1344
|
+
<CircleIcon />
|
|
1345
|
+
</MapDrawShapeButton>
|
|
1346
|
+
);
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
function MapDrawRectangle({
|
|
1350
|
+
showArea = false,
|
|
1351
|
+
shapeOptions = {
|
|
1352
|
+
color: 'var(--color-primary)',
|
|
1353
|
+
opacity: 1,
|
|
1354
|
+
weight: 2
|
|
1355
|
+
},
|
|
1356
|
+
...props
|
|
1357
|
+
}: DrawOptions.RectangleOptions) {
|
|
1358
|
+
return (
|
|
1359
|
+
<MapDrawShapeButton
|
|
1360
|
+
drawMode="rectangle"
|
|
1361
|
+
createDrawTool={(L, map) => new L.Draw.Rectangle(map, {
|
|
1362
|
+
showArea,
|
|
1363
|
+
shapeOptions,
|
|
1364
|
+
...props
|
|
1365
|
+
})}>
|
|
1366
|
+
<SquareIcon />
|
|
1367
|
+
</MapDrawShapeButton>
|
|
1368
|
+
);
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
function MapDrawPolygon({
|
|
1372
|
+
drawError = {
|
|
1373
|
+
color: 'var(--color-destructive)'
|
|
1374
|
+
},
|
|
1375
|
+
shapeOptions = {
|
|
1376
|
+
color: 'var(--color-primary)',
|
|
1377
|
+
opacity: 1,
|
|
1378
|
+
weight: 2
|
|
1379
|
+
},
|
|
1380
|
+
...props
|
|
1381
|
+
}: DrawOptions.PolygonOptions) {
|
|
1382
|
+
const mapDrawHandleIcon = useMapDrawHandleIcon();
|
|
1383
|
+
|
|
1384
|
+
return (
|
|
1385
|
+
<MapDrawShapeButton
|
|
1386
|
+
drawMode="polygon"
|
|
1387
|
+
createDrawTool={(L, map) => new L.Draw.Polygon(map, {
|
|
1388
|
+
...(mapDrawHandleIcon ? {
|
|
1389
|
+
icon: mapDrawHandleIcon,
|
|
1390
|
+
touchIcon: mapDrawHandleIcon
|
|
1391
|
+
} : {}),
|
|
1392
|
+
drawError,
|
|
1393
|
+
shapeOptions,
|
|
1394
|
+
...props
|
|
1395
|
+
})}>
|
|
1396
|
+
<PentagonIcon />
|
|
1397
|
+
</MapDrawShapeButton>
|
|
1398
|
+
);
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
/* -------------------------------------------------------------------------- */
|
|
1402
|
+
/* MapDrawActionButton (internal) */
|
|
1403
|
+
/* -------------------------------------------------------------------------- */
|
|
1404
|
+
|
|
1405
|
+
function MapDrawActionButton<T extends EditToolbar.Edit | EditToolbar.Delete>({
|
|
1406
|
+
drawAction,
|
|
1407
|
+
createDrawTool,
|
|
1408
|
+
controlRef,
|
|
1409
|
+
className,
|
|
1410
|
+
...props
|
|
1411
|
+
}: ComponentProps<'button'> & {
|
|
1412
|
+
drawAction: MapDrawAction;
|
|
1413
|
+
createDrawTool: (
|
|
1414
|
+
L: typeof LeafletNS,
|
|
1415
|
+
map: DrawMap,
|
|
1416
|
+
featureGroup: L.FeatureGroup
|
|
1417
|
+
) => T;
|
|
1418
|
+
controlRef: RefObject<T | null>;
|
|
1419
|
+
}) {
|
|
1420
|
+
const drawContext = useMapDrawContext();
|
|
1421
|
+
|
|
1422
|
+
if (!drawContext)
|
|
1423
|
+
throw new Error(
|
|
1424
|
+
'MapDrawActionButton must be used within MapDrawControl'
|
|
1425
|
+
);
|
|
1426
|
+
|
|
1427
|
+
const { L } = useLeaflet();
|
|
1428
|
+
const map = useMap();
|
|
1429
|
+
const {
|
|
1430
|
+
featureGroup, activeMode, setActiveMode, layersCount
|
|
1431
|
+
} = drawContext;
|
|
1432
|
+
const isActive = activeMode === drawAction;
|
|
1433
|
+
const hasFeatures = layersCount > 0;
|
|
1434
|
+
|
|
1435
|
+
useEffect(() => {
|
|
1436
|
+
if (!L || !featureGroup || !isActive) {
|
|
1437
|
+
controlRef.current?.disable?.();
|
|
1438
|
+
controlRef.current = null;
|
|
1439
|
+
|
|
1440
|
+
return;
|
|
1441
|
+
}
|
|
1442
|
+
const control = createDrawTool(L, map as DrawMap, featureGroup);
|
|
1443
|
+
|
|
1444
|
+
control.enable?.();
|
|
1445
|
+
controlRef.current = control;
|
|
1446
|
+
|
|
1447
|
+
return () => {
|
|
1448
|
+
control.disable?.();
|
|
1449
|
+
controlRef.current = null;
|
|
1450
|
+
};
|
|
1451
|
+
}, [
|
|
1452
|
+
L,
|
|
1453
|
+
map,
|
|
1454
|
+
isActive,
|
|
1455
|
+
featureGroup,
|
|
1456
|
+
createDrawTool,
|
|
1457
|
+
controlRef
|
|
1458
|
+
]);
|
|
1459
|
+
|
|
1460
|
+
function handleClick() {
|
|
1461
|
+
controlRef.current?.save();
|
|
1462
|
+
setActiveMode(isActive ? null : drawAction);
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
return (
|
|
1466
|
+
<Button
|
|
1467
|
+
type="button"
|
|
1468
|
+
size="icon-sm"
|
|
1469
|
+
aria-label={`${drawAction === 'edit' ? 'Edit' : 'Remove'} shapes`}
|
|
1470
|
+
title={`${drawAction === 'edit' ? 'Edit' : 'Remove'} shapes`}
|
|
1471
|
+
variant={isActive ? 'default' : 'secondary'}
|
|
1472
|
+
disabled={!hasFeatures}
|
|
1473
|
+
onClick={handleClick}
|
|
1474
|
+
className={cn('border', className)}
|
|
1475
|
+
{...props} />
|
|
1476
|
+
);
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
/* -------------------------------------------------------------------------- */
|
|
1480
|
+
/* MapDrawEdit / MapDrawDelete / MapDrawUndo */
|
|
1481
|
+
/* -------------------------------------------------------------------------- */
|
|
1482
|
+
|
|
1483
|
+
function MapDrawEdit({
|
|
1484
|
+
selectedPathOptions = {
|
|
1485
|
+
color: 'var(--color-primary)',
|
|
1486
|
+
fillColor: 'var(--color-primary)',
|
|
1487
|
+
weight: 2
|
|
1488
|
+
},
|
|
1489
|
+
...props
|
|
1490
|
+
}: Omit<EditToolbar.EditHandlerOptions, 'featureGroup'>) {
|
|
1491
|
+
const { L } = useLeaflet();
|
|
1492
|
+
const mapDrawHandleIcon = useMapDrawHandleIcon();
|
|
1493
|
+
const drawContext = useMapDrawContext();
|
|
1494
|
+
|
|
1495
|
+
if (!drawContext) {
|
|
1496
|
+
throw new Error('MapDrawEdit must be used within MapDrawControl');
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
useEffect(() => {
|
|
1500
|
+
if (!L || !mapDrawHandleIcon) return;
|
|
1501
|
+
|
|
1502
|
+
L.Edit.PolyVerticesEdit.mergeOptions({
|
|
1503
|
+
icon: mapDrawHandleIcon,
|
|
1504
|
+
touchIcon: mapDrawHandleIcon,
|
|
1505
|
+
drawError: {
|
|
1506
|
+
color: 'var(--color-destructive)'
|
|
1507
|
+
}
|
|
1508
|
+
});
|
|
1509
|
+
L.Edit.SimpleShape.mergeOptions({
|
|
1510
|
+
moveIcon: mapDrawHandleIcon,
|
|
1511
|
+
resizeIcon: mapDrawHandleIcon,
|
|
1512
|
+
touchMoveIcon: mapDrawHandleIcon,
|
|
1513
|
+
touchResizeIcon: mapDrawHandleIcon
|
|
1514
|
+
});
|
|
1515
|
+
L.drawLocal.edit.handlers.edit.tooltip = {
|
|
1516
|
+
text: 'Drag handles or markers to edit.',
|
|
1517
|
+
subtext: ''
|
|
1518
|
+
};
|
|
1519
|
+
L.drawLocal.edit.handlers.remove.tooltip = {
|
|
1520
|
+
text: 'Click on a shape to remove.'
|
|
1521
|
+
};
|
|
1522
|
+
}, [L, mapDrawHandleIcon]);
|
|
1523
|
+
|
|
1524
|
+
return (
|
|
1525
|
+
<MapDrawActionButton
|
|
1526
|
+
drawAction="edit"
|
|
1527
|
+
controlRef={drawContext.editControlRef}
|
|
1528
|
+
createDrawTool={(L, map, featureGroup) => new L.EditToolbar.Edit(map, {
|
|
1529
|
+
featureGroup,
|
|
1530
|
+
selectedPathOptions,
|
|
1531
|
+
...props
|
|
1532
|
+
})}>
|
|
1533
|
+
<PenLineIcon />
|
|
1534
|
+
</MapDrawActionButton>
|
|
1535
|
+
);
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1538
|
+
function MapDrawDelete() {
|
|
1539
|
+
const drawContext = useMapDrawContext();
|
|
1540
|
+
|
|
1541
|
+
if (!drawContext) {
|
|
1542
|
+
throw new Error('MapDrawDelete must be used within MapDrawControl');
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
return (
|
|
1546
|
+
<MapDrawActionButton
|
|
1547
|
+
drawAction="delete"
|
|
1548
|
+
controlRef={drawContext.deleteControlRef}
|
|
1549
|
+
createDrawTool={(L, map, featureGroup) => new L.EditToolbar.Delete(map, { featureGroup })}>
|
|
1550
|
+
<Trash2Icon />
|
|
1551
|
+
</MapDrawActionButton>
|
|
1552
|
+
);
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
function MapDrawUndo({ className, ...props }: ComponentProps<'button'>) {
|
|
1556
|
+
const drawContext = useMapDrawContext();
|
|
1557
|
+
|
|
1558
|
+
if (!drawContext)
|
|
1559
|
+
throw new Error('MapDrawUndo must be used within MapDrawControl');
|
|
1560
|
+
|
|
1561
|
+
const {
|
|
1562
|
+
activeMode,
|
|
1563
|
+
setActiveMode,
|
|
1564
|
+
editControlRef,
|
|
1565
|
+
deleteControlRef,
|
|
1566
|
+
layersCount
|
|
1567
|
+
} = drawContext;
|
|
1568
|
+
const isInEditMode = activeMode === 'edit';
|
|
1569
|
+
const isInDeleteMode = activeMode === 'delete';
|
|
1570
|
+
const isActive = (isInEditMode || isInDeleteMode) && layersCount > 0;
|
|
1571
|
+
|
|
1572
|
+
function handleUndo() {
|
|
1573
|
+
if (isInEditMode) {
|
|
1574
|
+
editControlRef.current?.revertLayers();
|
|
1575
|
+
} else if (isInDeleteMode) {
|
|
1576
|
+
deleteControlRef.current?.revertLayers();
|
|
1577
|
+
}
|
|
1578
|
+
setActiveMode(null);
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
return (
|
|
1582
|
+
<Button
|
|
1583
|
+
type="button"
|
|
1584
|
+
size="icon-sm"
|
|
1585
|
+
variant="secondary"
|
|
1586
|
+
aria-label={`Undo ${activeMode}`}
|
|
1587
|
+
title={`Undo ${activeMode}`}
|
|
1588
|
+
onClick={handleUndo}
|
|
1589
|
+
disabled={!isActive}
|
|
1590
|
+
className={cn('border', className)}
|
|
1591
|
+
{...props}>
|
|
1592
|
+
<Undo2Icon />
|
|
1593
|
+
</Button>
|
|
1594
|
+
);
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
/* -------------------------------------------------------------------------- */
|
|
1598
|
+
/* Exports */
|
|
1599
|
+
/* -------------------------------------------------------------------------- */
|
|
1600
|
+
|
|
1601
|
+
export {
|
|
1602
|
+
Map,
|
|
1603
|
+
MapCircle,
|
|
1604
|
+
MapCircleMarker,
|
|
1605
|
+
MapControlContainer,
|
|
1606
|
+
MapDrawCircle,
|
|
1607
|
+
MapDrawControl,
|
|
1608
|
+
MapDrawDelete,
|
|
1609
|
+
MapDrawEdit,
|
|
1610
|
+
MapDrawMarker,
|
|
1611
|
+
MapDrawPolygon,
|
|
1612
|
+
MapDrawPolyline,
|
|
1613
|
+
MapDrawRectangle,
|
|
1614
|
+
MapDrawUndo,
|
|
1615
|
+
MapFeatureGroup,
|
|
1616
|
+
MapFullscreenControl,
|
|
1617
|
+
MapLayerGroup,
|
|
1618
|
+
MapLayers,
|
|
1619
|
+
MapLayersControl,
|
|
1620
|
+
MapLocateControl,
|
|
1621
|
+
MapMarker,
|
|
1622
|
+
MapMarkerClusterGroup,
|
|
1623
|
+
MapPolygon,
|
|
1624
|
+
MapPolyline,
|
|
1625
|
+
MapPopup,
|
|
1626
|
+
MapRectangle,
|
|
1627
|
+
MapSearchControl,
|
|
1628
|
+
MapTileLayer,
|
|
1629
|
+
MapTooltip,
|
|
1630
|
+
MapZoomControl
|
|
1631
|
+
};
|