Skip to content

Commit cc63dd7

Browse files
committed
fix: logic fixes, create toolbar in map ready to avoid runtime issues
1 parent 9c64c77 commit cc63dd7

File tree

6 files changed

+36
-21
lines changed

6 files changed

+36
-21
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
control: {},
3+
drawing: {}
4+
}

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ export default {
5555
babelHelpers: "runtime",
5656
include: ["./src/**/*"],
5757
}),
58-
// terser(),
58+
terser(),
5959
],
6060
};

src/contexts/AzureMapDrawingManagerContext.tsx

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { createContext, useContext, useEffect, useState } from 'react'
22
import 'azure-maps-drawing-tools/dist/atlas-drawing.min.css'
33

44
import atlas from 'azure-maps-control'
5-
import { drawing } from 'azure-maps-drawing-tools'
5+
import { drawing, control } from 'azure-maps-drawing-tools'
66
import {
77
DrawingManagerType,
88
IAzureDrawingManagerStatefulProviderProps,
@@ -40,13 +40,22 @@ const AzureMapDrawingManagerStatefulProvider = ({
4040
const { mapRef } = useContext<IAzureMapsContextProps>(AzureMapsContext)
4141
const [drawingManagerRef, setDrawingManagerRef] = useState<drawing.DrawingManager | null>(null)
4242
const [dataSourceRef, setDataSourceRef] = useState<atlas.source.DataSource | null>(null)
43+
const [toolbarOnceReadyRef, setToolbarOnceReadyRef] = useState<control.DrawingToolbar | undefined>(undefined)
4344

44-
useCheckRef<MapType, DrawingManagerType>(mapRef, drawingManagerRef, (mref, dmref) => {
45+
useCheckRef<MapType, MapType>(mapRef, mapRef, mref => {
4546
mref.events.add('ready', () => {
46-
const drawingManager = new drawing.DrawingManager(mref, options)
47+
// NOTE: if DrawingToolbar gets instantiated before map is 'ready', weird runtime errors follow.
48+
// create toolbar here instead
49+
const drawingManager = new drawing.DrawingManager(mref)
50+
const toolbar = options.toolbar ? new control.DrawingToolbar(options.toolbar) : undefined
51+
drawingManager.setOptions({
52+
...options,
53+
toolbar
54+
})
55+
4756
setDrawingManagerRef(drawingManager)
4857
setDataSourceRef(drawingManager.getSource())
49-
drawingManager.getLayers()
58+
setToolbarOnceReadyRef(toolbar)
5059

5160
// register drawing events
5261
for (const eventType in events) {
@@ -57,31 +66,33 @@ const AzureMapDrawingManagerStatefulProvider = ({
5766

5867
// NOTE: duplication to have the explicit types instead of any
5968
if(eventType == 'drawingmodechanged'){
60-
(mref.events as _EventManager).add(eventType, dmref, handler as (a: drawing.DrawingMode) => void)
69+
(mref.events as _EventManager).add(eventType, drawingManager, handler as (a: drawing.DrawingMode) => void)
6170
} else {
62-
(mref.events as _EventManager).add(eventType as IAzureDrawingManagerDrawingEventType, dmref, handler as (e: Shape) => void)
71+
(mref.events as _EventManager).add(eventType as IAzureDrawingManagerDrawingEventType, drawingManager, handler as (e: Shape) => void)
6372
}
6473
}
6574

6675
return () => {
6776
setDrawingManagerRef(null)
6877
setDataSourceRef(null)
78+
setToolbarOnceReadyRef(undefined)
6979

7080
for (const eventType in events) {
7181
const handler = events[eventType as IAzureDrawingManagerEventType];
7282
if(handler){
73-
(mref.events as _EventManager).remove(eventType, dmref, handler)
83+
(mref.events as _EventManager).remove(eventType, drawingManager, handler)
7484
}
7585
}
7686
}
7787
})
7888
})
79-
89+
8090
useEffect(() => {
81-
if(drawingManagerRef && options){
82-
drawingManagerRef.setOptions(options)
91+
if(drawingManagerRef && options){
92+
drawingManagerRef.setOptions({ ...options, toolbar: toolbarOnceReadyRef })
93+
toolbarOnceReadyRef?.setOptions(options.toolbar)
8394
}
84-
}, [drawingManagerRef, options])
95+
}, [drawingManagerRef, options, toolbarOnceReadyRef])
8596

8697
return (
8798
<DrawingManagerProvider

src/hooks/useAzureMapDrawingLayer.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import React, { createContext, useContext, useEffect, useState } from 'react'
1+
import { useContext, useEffect, useState } from 'react'
22
import { useCheckRef } from '../hooks/useCheckRef'
3-
import { DrawingManagerType, IAzureLayerStatefulProviderProps, IAzureDrawingLayerStatefulProviderProps, IAzureMapDrawingManagerProps, IAzureMapLayerProps, IAzureMapsContextProps, LayerType, MapType } from '../types'
3+
import { DrawingManagerType, IAzureDrawingLayerStatefulProviderProps, IAzureMapDrawingManagerProps, IAzureMapsContextProps, LayerType, MapType } from '../types'
44
import { AzureMapsContext } from '../contexts/AzureMapContext'
55
import { AzureMapDrawingManagerContext } from '../contexts/AzureMapDrawingManagerContext'
66

7-
const AzureMapLayerContext = createContext<IAzureMapLayerProps>({
8-
layerRef: null
9-
})
10-
const { Provider, Consumer: AzureMapLayerConsumer } = AzureMapLayerContext
11-
127
export const useAzureMapDrawingLayer = ({
138
options,
149
type,

src/react-azure-maps.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export {
2222
AzureMapDrawingManagerConsumer,
2323
AzureMapDrawingManagerProvider
2424
} from './contexts/AzureMapDrawingManagerContext'
25+
export {
26+
AzureMapDrawingLayerContext,
27+
AzureMapDrawingLayerConsumer,
28+
AzureMapDrawingLayerProvider
29+
} from './contexts/AzureMapDrawingLayerContext'
2530
export { default as AzureMapPopup } from './components/AzureMapPopup/AzureMapPopup'
2631
export { default as useCreatePopup } from './components/AzureMapPopup/useCreateAzureMapPopup'
2732

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import atlas, {
3434
BubbleLayerOptions,
3535
LayerOptions
3636
} from 'azure-maps-control'
37-
import { drawing, DrawingManagerOptions } from 'azure-maps-drawing-tools'
37+
import { drawing, DrawingManagerOptions, DrawingToolbarOptions } from 'azure-maps-drawing-tools'
3838

3939
export type IAzureMapOptions = ServiceOptions &
4040
StyleOptions &
@@ -339,7 +339,7 @@ export interface IAzureMapDrawingManagerProps {
339339
}
340340

341341
export interface IAzureDrawingManagerStatefulProviderProps {
342-
options: DrawingManagerOptions
342+
options: Omit<DrawingManagerOptions, 'toolbar'> & { 'toolbar': DrawingToolbarOptions }
343343
events?: IAzureDrawingManagerEvent
344344
children?:
345345
| Array<IAzureDataSourceChildren | null>

0 commit comments

Comments
 (0)