|
1 | 1 | import '@geoman-io/leaflet-geoman-free' |
2 | | -import '@geoman-io/leaflet-geoman-free/dist/leaflet-geoman.css' |
3 | | -import { useEffect, useState } from 'react' |
| 2 | +import { useLayoutEffect, useEffect, useState } from 'react' |
4 | 3 | import { useLeafletContext } from '@react-leaflet/core' |
5 | 4 | import type { LayerGroup } from 'leaflet' |
6 | 5 | import useDeepCompareEffect from 'use-deep-compare-effect' |
@@ -30,42 +29,64 @@ export default function GeomanControls({ |
30 | 29 | return null |
31 | 30 | } |
32 | 31 |
|
33 | | - useDeepCompareEffect(() => { |
| 32 | + useLayoutEffect(() => { |
| 33 | + // add controls |
34 | 34 | if (!map.pm.controlsVisible()) { |
35 | 35 | map.pm.addControls(options) |
36 | | - map.pm.setPathOptions(pathOptions) |
37 | | - map.pm.setGlobalOptions({ |
38 | | - layerGroup: container, |
39 | | - ...globalOptions, |
40 | | - }) |
41 | | - map.pm.setLang(lang) |
42 | 36 | if (onMount) onMount() |
43 | 37 | setMounted(true) |
44 | 38 | } |
45 | 39 | return () => { |
46 | | - map.pm.removeControls() |
| 40 | + map.pm.disableDraw() |
| 41 | + map.pm.disableGlobalEditMode() |
| 42 | + map.pm.disableGlobalRemovalMode() |
| 43 | + map.pm.disableGlobalDragMode() |
| 44 | + map.pm.disableGlobalCutMode() |
| 45 | + map.pm.disableGlobalRotateMode() |
| 46 | + map.pm.disableGlobalDragMode() |
| 47 | + map.pm.disableGlobalCutMode() |
47 | 48 | if (onUnmount) onUnmount() |
| 49 | + map.pm.removeControls() |
48 | 50 | setMounted(false) |
49 | 51 | } |
50 | | - }, [options, globalOptions, pathOptions, lang, !map]) |
| 52 | + }, []) |
| 53 | + |
| 54 | + useEffect(() => { |
| 55 | + // set path options |
| 56 | + if (mounted) map.pm.setPathOptions(pathOptions) |
| 57 | + }, [pathOptions, mounted]) |
| 58 | + |
| 59 | + useDeepCompareEffect(() => { |
| 60 | + // set global options |
| 61 | + if (mounted) |
| 62 | + map.pm.setGlobalOptions({ layerGroup: container, ...globalOptions }) |
| 63 | + }, [globalOptions, mounted]) |
51 | 64 |
|
52 | 65 | useEffect(() => { |
53 | | - const withDebug = Object.fromEntries( |
54 | | - reference.map((handler) => [handler, handlers[handler] ?? eventDebugFn]) |
55 | | - ) |
56 | | - const layers = layerContainer |
57 | | - ? container.getLayers() |
58 | | - : map.pm.getGeomanLayers() |
| 66 | + // set language |
| 67 | + if (mounted) map.pm.setLang(lang) |
| 68 | + }, [lang, mounted]) |
| 69 | + |
| 70 | + useEffect(() => { |
| 71 | + // attach and remove event handlers |
59 | 72 | if (mounted) { |
| 73 | + const withDebug = Object.fromEntries( |
| 74 | + reference.map((handler) => [handler, handlers[handler] ?? eventDebugFn]) |
| 75 | + ) |
| 76 | + const layers = layerContainer |
| 77 | + ? container.getLayers() |
| 78 | + : map.pm.getGeomanLayers() |
| 79 | + layers.forEach((layer) => layerEvents(layer, withDebug, 'on')) |
| 80 | + |
60 | 81 | globalEvents(map, withDebug, 'on') |
61 | 82 | mapEvents(map, withDebug, 'on') |
62 | | - layers.forEach((layer) => layerEvents(layer, withDebug, 'on')) |
63 | | - } |
64 | | - return () => { |
65 | | - globalEvents(map, withDebug, 'off') |
66 | | - mapEvents(map, withDebug, 'off') |
67 | | - layers.forEach((layer) => layerEvents(layer, withDebug, 'off')) |
68 | | - if (process.env.NODE_ENV === 'development') setHandlersRef(handlers) |
| 83 | + |
| 84 | + return () => { |
| 85 | + globalEvents(map, withDebug, 'off') |
| 86 | + mapEvents(map, withDebug, 'off') |
| 87 | + layers.forEach((layer) => layerEvents(layer, withDebug, 'off')) |
| 88 | + setHandlersRef(handlers) |
| 89 | + } |
69 | 90 | } |
70 | 91 | }, [ |
71 | 92 | mounted, |
|
0 commit comments