-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.ts
121 lines (106 loc) · 2.96 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import MapboxDraw from '@mapbox/mapbox-gl-draw';
import { Control } from 'mapbox-gl';
import * as React from 'react';
import { MapContext } from 'react-mapbox-gl';
function noop(): void {
/* do nothing */
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type DrawHandler = (event: any) => void;
/**
* User-facing props passed to <DrawControl />
*/
export interface DrawControlProps {
boxSelect?: boolean;
clickBuffer?: number;
controls?: Partial<{
point: boolean;
line_string: boolean;
polygon: boolean;
trash: boolean;
combine_features: boolean;
uncombine_features: boolean;
}>;
defaultMode?: string;
displayControlsDefault?: boolean;
keybindings?: boolean;
modes?: object; // eslint-disable-line @typescript-eslint/ban-types
onDrawActionable?: DrawHandler;
onDrawCombine?: DrawHandler;
onDrawCreate?: DrawHandler;
onDrawDelete?: DrawHandler;
onDrawModeChange?: DrawHandler;
onDrawRender?: DrawHandler;
onDrawSelectionChange?: DrawHandler;
onDrawUncombine?: DrawHandler;
onDrawUpdate?: DrawHandler;
position?: 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right';
touchBuffer?: number;
touchEnabled?: boolean;
userProperties?: boolean;
styles?: object[]; // eslint-disable-line @typescript-eslint/ban-types
}
export default class DrawControl extends React.Component<DrawControlProps> {
static contextType = MapContext;
static defaultProps = {
position: 'top-left',
};
context!: React.ContextType<typeof MapContext>; // http://bit.ly/typescript-and-react-context
draw?: Control;
componentDidMount(): void {
const map = this.context;
// The map needs to be passed in the React Context, or welse we can't do
// anything.
if (!map || !map.getStyle()) {
throw new Error('Map is undefined in React context.');
}
const {
modes,
onDrawActionable,
onDrawCombine,
onDrawCreate,
onDrawDelete,
onDrawModeChange,
onDrawRender,
onDrawSelectionChange,
onDrawUncombine,
onDrawUpdate,
position,
} = this.props;
// Define a new Draw Control
// eslint-disable-next-line
this.draw = new MapboxDraw({
...this.props,
// eslint-disable-next-line
modes: {
...MapboxDraw.modes, // eslint-disable-line
...modes,
},
});
// Add it to our map
map.addControl(this.draw as Control, position);
// Hook draw events
map.on('draw.actionable', onDrawActionable || noop);
map.on('draw.combine', onDrawCombine || noop);
map.on('draw.create', onDrawCreate || noop);
map.on('draw.delete', onDrawDelete || noop);
map.on('draw.modechange', onDrawModeChange || noop);
map.on('draw.render', onDrawRender || noop);
map.on('draw.selectionchange', onDrawSelectionChange || noop);
map.on('draw.uncombine', onDrawUncombine || noop);
map.on('draw.update', onDrawUpdate || noop);
}
componentWillUnmount(): void {
const map = this.context;
if (!map || !map.getStyle()) {
return;
}
if (!this.draw) {
return;
}
map.removeControl(this.draw);
}
render(): null {
return null;
}
}