Skip to content

Commit e3894a7

Browse files
authored
Merge pull request #7 from TurtIeSocks/v1
v1 release
2 parents 662f61c + 243d670 commit e3894a7

File tree

5 files changed

+60
-37
lines changed

5 files changed

+60
-37
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ Add React Leaflet Geoman
3030

3131
Since this package modifies the DOM directly, it can be imported either as hook or a component. At a minimum, it must be initiated inside of a `MapContainer` component. You can either draw shapes directly to the map container or wrap it in a `FeatureGroup` component. See the [Example](/example) code for a more detailed usage example.
3232

33+
## V2 Breaking Changes
34+
35+
This component wrapper no longer imports the Leaflet Geoman CSS file. You must import it yourself, this is for compatibility reasons for different frameworks.
36+
3337
```tsx
3438
// as a React component
39+
import '@geoman-io/leaflet-geoman-free/dist/leaflet-geoman.css'
3540
import { GeomanControls } from 'react-leaflet-geoman-v2'
3641

3742
export default function Drawing() {
@@ -57,6 +62,7 @@ export default function Drawing() {
5762
}
5863

5964
// as a hook
65+
import '@geoman-io/leaflet-geoman-free/dist/leaflet-geoman.css'
6066
import { useGeomanControls } from 'react-leaflet-geoman-v2'
6167

6268
export default function Drawing() {

example/App.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ export default function App() {
1919
<GeomanWrapper geojson={geojson} setGeojson={setGeojson} />
2020
</MapContainer>
2121
</div>
22-
<div style={{ width: '33%', textAlign: 'center', overflow: 'auto' }}>
22+
<div style={{ width: '33%', overflow: 'auto', padding: '0 20px' }}>
2323
<h3>{geojson.features.length} Features</h3>
24-
<ul>
25-
{geojson.features.map((feature, i) => (
26-
<li key={i}>{JSON.stringify(feature, null, 2)}</li>
27-
))}
28-
</ul>
24+
<pre>{JSON.stringify(geojson, null, 2)}</pre>
2925
</div>
3026
</div>
3127
)

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-leaflet-geoman-v2",
3-
"version": "0.2.2",
3+
"version": "1.0.0",
44
"description": "React wrapper for the leaflet-geoman plugin",
55
"repository": "https://github.com/TurtIeSocks/react-leaflet-geoman",
66
"author": "TurtIeSocks <58572875+TurtIeSocks@users.noreply.github.com>",
@@ -20,7 +20,7 @@
2020
"use-deep-compare-effect": "^1.8.1"
2121
},
2222
"devDependencies": {
23-
"@geoman-io/leaflet-geoman-free": "^2.13.1",
23+
"@geoman-io/leaflet-geoman-free": "^2.14.2",
2424
"@rollup/plugin-typescript": "^8.4.0",
2525
"@types/leaflet": "^1.7.11",
2626
"@types/node": "^18.7.13",
@@ -37,7 +37,7 @@
3737
"vite-plugin-checker": "^0.4.9"
3838
},
3939
"peerDependencies": {
40-
"@geoman-io/leaflet-geoman-free": "^2.13.1",
40+
"@geoman-io/leaflet-geoman-free": "^2.14.2",
4141
"react": "^17.0.0 || ^18.0.0",
4242
"react-dom": "^17.0.0 || ^18.0.0",
4343
"react-leaflet": "^4.0.2"

src/GeomanControls.ts

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
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'
43
import { useLeafletContext } from '@react-leaflet/core'
54
import type { LayerGroup } from 'leaflet'
65
import useDeepCompareEffect from 'use-deep-compare-effect'
@@ -30,42 +29,64 @@ export default function GeomanControls({
3029
return null
3130
}
3231

33-
useDeepCompareEffect(() => {
32+
useLayoutEffect(() => {
33+
// add controls
3434
if (!map.pm.controlsVisible()) {
3535
map.pm.addControls(options)
36-
map.pm.setPathOptions(pathOptions)
37-
map.pm.setGlobalOptions({
38-
layerGroup: container,
39-
...globalOptions,
40-
})
41-
map.pm.setLang(lang)
4236
if (onMount) onMount()
4337
setMounted(true)
4438
}
4539
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()
4748
if (onUnmount) onUnmount()
49+
map.pm.removeControls()
4850
setMounted(false)
4951
}
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])
5164

5265
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
5972
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+
6081
globalEvents(map, withDebug, 'on')
6182
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+
}
6990
}
7091
}, [
7192
mounted,

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@
252252
resolved "https://registry.yarnpkg.com/@esbuild/linux-loong64/-/linux-loong64-0.14.54.tgz#de2a4be678bd4d0d1ffbb86e6de779cde5999028"
253253
integrity sha512-bZBrLAIX1kpWelV0XemxBZllyRmM6vgFQQG2GdNb+r3Fkp0FOh1NJSvekXDs7jq70k4euu1cryLMfU+mTXlEpw==
254254

255-
"@geoman-io/leaflet-geoman-free@^2.13.1":
256-
version "2.13.1"
257-
resolved "https://registry.yarnpkg.com/@geoman-io/leaflet-geoman-free/-/leaflet-geoman-free-2.13.1.tgz#a968810bc1b7c4fae9fe6f7a99e0015c9aeaaeef"
258-
integrity sha512-csTmg0JJegXRfwGpNXQ9wy1Y1wha8AcrxhvaVFGm3xrhpPG8l3wouMZvO91PN8ZthG3xpZtfDnUQF+7Pm4mwXQ==
255+
"@geoman-io/leaflet-geoman-free@^2.14.2":
256+
version "2.14.2"
257+
resolved "https://registry.yarnpkg.com/@geoman-io/leaflet-geoman-free/-/leaflet-geoman-free-2.14.2.tgz#c84c2115c263f34d11dc0b43859551639fe3d56b"
258+
integrity sha512-6lIyG8RvSVdFjVjiQgBPyNASjymSyqzsiUeBW0pA+q41lB5fAg4SDC6SfJvWdEyDHa81Jb5FWjUkCc9O+u0gbg==
259259
dependencies:
260260
"@turf/boolean-contains" "^6.5.0"
261261
"@turf/kinks" "^6.5.0"

0 commit comments

Comments
 (0)