maplibre-gl-layer-control 0.2.0 → 0.3.0
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/README.md +4 -0
- package/dist/types/index.d.ts +375 -0
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# maplibre-gl-layer-control
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/maplibre-gl-layer-control)
|
|
4
|
+
[](https://www.npmjs.com/package/maplibre-gl-layer-control)
|
|
5
|
+
[](https://github.com/opengeos/maplibre-gl-layer-control/blob/main/LICENSE)
|
|
6
|
+
|
|
3
7
|
A comprehensive layer control for MapLibre GL with advanced styling capabilities. Built with TypeScript and React, providing both vanilla JavaScript and React integration options.
|
|
4
8
|
|
|
5
9
|
## Features
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1,376 @@
|
|
|
1
|
+
import { IControl } from 'maplibre-gl';
|
|
2
|
+
import { Map as Map_2 } from 'maplibre-gl';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Clamp a numeric value between min and max
|
|
6
|
+
* @param value Value to clamp
|
|
7
|
+
* @param min Minimum value
|
|
8
|
+
* @param max Maximum value
|
|
9
|
+
* @returns Clamped value
|
|
10
|
+
*/
|
|
11
|
+
export declare function clamp(value: number, min: number, max: number): number;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Format a numeric value based on the step size
|
|
15
|
+
* @param value Numeric value to format
|
|
16
|
+
* @param step Step size (determines decimal places)
|
|
17
|
+
* @returns Formatted string
|
|
18
|
+
*/
|
|
19
|
+
export declare function formatNumericValue(value: number, step: number): string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Get the current opacity value for a layer
|
|
23
|
+
* @param map MapLibre map instance
|
|
24
|
+
* @param layerId Layer ID
|
|
25
|
+
* @param layerType Layer type
|
|
26
|
+
* @returns Current opacity value (0-1)
|
|
27
|
+
*/
|
|
28
|
+
export declare function getLayerOpacity(map: Map_2, layerId: string, layerType: string): number;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Get layer type from map
|
|
32
|
+
* @param map MapLibre map instance
|
|
33
|
+
* @param layerId Layer ID
|
|
34
|
+
* @returns Layer type or null if layer not found
|
|
35
|
+
*/
|
|
36
|
+
export declare function getLayerType(map: Map_2, layerId: string): string | null;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Check if a layer type supports style editing
|
|
40
|
+
* @param layerType Layer type
|
|
41
|
+
* @returns True if the layer type supports style editing
|
|
42
|
+
*/
|
|
43
|
+
export declare function isStyleableLayerType(layerType: string): layerType is StyleableLayerType;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* LayerControl - A comprehensive layer control for MapLibre GL
|
|
47
|
+
* Provides visibility toggle, opacity control, and advanced style editing
|
|
48
|
+
*/
|
|
49
|
+
export declare class LayerControl implements IControl {
|
|
50
|
+
private map;
|
|
51
|
+
private container;
|
|
52
|
+
private button;
|
|
53
|
+
private panel;
|
|
54
|
+
private state;
|
|
55
|
+
private targetLayers;
|
|
56
|
+
private styleEditors;
|
|
57
|
+
private minPanelWidth;
|
|
58
|
+
private maxPanelWidth;
|
|
59
|
+
private widthSliderEl;
|
|
60
|
+
private widthThumbEl;
|
|
61
|
+
private widthValueEl;
|
|
62
|
+
private isWidthSliderActive;
|
|
63
|
+
private widthDragRectWidth;
|
|
64
|
+
private widthDragStartX;
|
|
65
|
+
private widthDragStartWidth;
|
|
66
|
+
private widthFrame;
|
|
67
|
+
constructor(options?: LayerControlOptions);
|
|
68
|
+
/**
|
|
69
|
+
* Called when the control is added to the map
|
|
70
|
+
*/
|
|
71
|
+
onAdd(map: Map_2): HTMLElement;
|
|
72
|
+
/**
|
|
73
|
+
* Called when the control is removed from the map
|
|
74
|
+
*/
|
|
75
|
+
onRemove(): void;
|
|
76
|
+
/**
|
|
77
|
+
* Auto-detect layers from the map and populate layerStates
|
|
78
|
+
*/
|
|
79
|
+
private autoDetectLayers;
|
|
80
|
+
/**
|
|
81
|
+
* Generate a friendly display name from a layer ID
|
|
82
|
+
*/
|
|
83
|
+
private generateFriendlyName;
|
|
84
|
+
/**
|
|
85
|
+
* Create the main container element
|
|
86
|
+
*/
|
|
87
|
+
private createContainer;
|
|
88
|
+
/**
|
|
89
|
+
* Create the toggle button
|
|
90
|
+
*/
|
|
91
|
+
private createToggleButton;
|
|
92
|
+
/**
|
|
93
|
+
* Create the panel element
|
|
94
|
+
*/
|
|
95
|
+
private createPanel;
|
|
96
|
+
/**
|
|
97
|
+
* Create the panel header with title and width control
|
|
98
|
+
*/
|
|
99
|
+
private createPanelHeader;
|
|
100
|
+
/**
|
|
101
|
+
* Create the width control slider
|
|
102
|
+
*/
|
|
103
|
+
private createWidthControl;
|
|
104
|
+
/**
|
|
105
|
+
* Setup event listeners for width slider
|
|
106
|
+
*/
|
|
107
|
+
private setupWidthSliderEvents;
|
|
108
|
+
/**
|
|
109
|
+
* Update panel width from pointer event
|
|
110
|
+
*/
|
|
111
|
+
private updateWidthFromPointer;
|
|
112
|
+
/**
|
|
113
|
+
* Apply panel width (clamped to min/max)
|
|
114
|
+
*/
|
|
115
|
+
private applyPanelWidth;
|
|
116
|
+
/**
|
|
117
|
+
* Update width display (value label and thumb position)
|
|
118
|
+
*/
|
|
119
|
+
private updateWidthDisplay;
|
|
120
|
+
/**
|
|
121
|
+
* Setup main event listeners
|
|
122
|
+
*/
|
|
123
|
+
private setupEventListeners;
|
|
124
|
+
/**
|
|
125
|
+
* Setup listeners for map layer changes
|
|
126
|
+
*/
|
|
127
|
+
private setupLayerChangeListeners;
|
|
128
|
+
/**
|
|
129
|
+
* Toggle panel expanded/collapsed state
|
|
130
|
+
*/
|
|
131
|
+
private toggle;
|
|
132
|
+
/**
|
|
133
|
+
* Expand the panel
|
|
134
|
+
*/
|
|
135
|
+
private expand;
|
|
136
|
+
/**
|
|
137
|
+
* Collapse the panel
|
|
138
|
+
*/
|
|
139
|
+
private collapse;
|
|
140
|
+
/**
|
|
141
|
+
* Build layer items (called initially and when layers change)
|
|
142
|
+
*/
|
|
143
|
+
private buildLayerItems;
|
|
144
|
+
/**
|
|
145
|
+
* Add a single layer item to the panel
|
|
146
|
+
*/
|
|
147
|
+
private addLayerItem;
|
|
148
|
+
/**
|
|
149
|
+
* Toggle layer visibility
|
|
150
|
+
*/
|
|
151
|
+
private toggleLayerVisibility;
|
|
152
|
+
/**
|
|
153
|
+
* Change layer opacity
|
|
154
|
+
*/
|
|
155
|
+
private changeLayerOpacity;
|
|
156
|
+
/**
|
|
157
|
+
* Check if a layer is a user-added layer (vs basemap layer)
|
|
158
|
+
*/
|
|
159
|
+
private isUserAddedLayer;
|
|
160
|
+
/**
|
|
161
|
+
* Toggle visibility for all background layers (basemap layers)
|
|
162
|
+
*/
|
|
163
|
+
private toggleBackgroundVisibility;
|
|
164
|
+
/**
|
|
165
|
+
* Change opacity for all background layers (basemap layers)
|
|
166
|
+
*/
|
|
167
|
+
private changeBackgroundOpacity;
|
|
168
|
+
/**
|
|
169
|
+
* Create legend button for Background layer
|
|
170
|
+
*/
|
|
171
|
+
private createBackgroundLegendButton;
|
|
172
|
+
/**
|
|
173
|
+
* Toggle background legend panel visibility
|
|
174
|
+
*/
|
|
175
|
+
private toggleBackgroundLegend;
|
|
176
|
+
/**
|
|
177
|
+
* Open background legend panel
|
|
178
|
+
*/
|
|
179
|
+
private openBackgroundLegend;
|
|
180
|
+
/**
|
|
181
|
+
* Close background legend panel
|
|
182
|
+
*/
|
|
183
|
+
private closeBackgroundLegend;
|
|
184
|
+
/**
|
|
185
|
+
* Create the background legend panel with individual layer controls
|
|
186
|
+
*/
|
|
187
|
+
private createBackgroundLegendPanel;
|
|
188
|
+
/**
|
|
189
|
+
* Check if a layer is currently rendered in the map viewport
|
|
190
|
+
*/
|
|
191
|
+
private isLayerRendered;
|
|
192
|
+
/**
|
|
193
|
+
* Populate the background layer list with individual layers
|
|
194
|
+
*/
|
|
195
|
+
private populateBackgroundLayerList;
|
|
196
|
+
/**
|
|
197
|
+
* Toggle visibility of an individual background layer
|
|
198
|
+
*/
|
|
199
|
+
private toggleIndividualBackgroundLayer;
|
|
200
|
+
/**
|
|
201
|
+
* Set visibility for all background layers
|
|
202
|
+
*/
|
|
203
|
+
private setAllBackgroundLayersVisibility;
|
|
204
|
+
/**
|
|
205
|
+
* Update the main Background checkbox based on individual layer states
|
|
206
|
+
*/
|
|
207
|
+
private updateBackgroundCheckboxState;
|
|
208
|
+
/**
|
|
209
|
+
* Create style button for a layer
|
|
210
|
+
*/
|
|
211
|
+
private createStyleButton;
|
|
212
|
+
/**
|
|
213
|
+
* Toggle style editor for a layer
|
|
214
|
+
*/
|
|
215
|
+
private toggleStyleEditor;
|
|
216
|
+
/**
|
|
217
|
+
* Open style editor for a layer
|
|
218
|
+
*/
|
|
219
|
+
private openStyleEditor;
|
|
220
|
+
/**
|
|
221
|
+
* Close style editor for a layer
|
|
222
|
+
*/
|
|
223
|
+
private closeStyleEditor;
|
|
224
|
+
/**
|
|
225
|
+
* Create style editor UI
|
|
226
|
+
*/
|
|
227
|
+
private createStyleEditor;
|
|
228
|
+
/**
|
|
229
|
+
* Add style controls based on layer type
|
|
230
|
+
*/
|
|
231
|
+
private addStyleControlsForLayerType;
|
|
232
|
+
/**
|
|
233
|
+
* Add controls for fill layers
|
|
234
|
+
*/
|
|
235
|
+
private addFillControls;
|
|
236
|
+
/**
|
|
237
|
+
* Add controls for line layers
|
|
238
|
+
*/
|
|
239
|
+
private addLineControls;
|
|
240
|
+
/**
|
|
241
|
+
* Add controls for circle layers
|
|
242
|
+
*/
|
|
243
|
+
private addCircleControls;
|
|
244
|
+
/**
|
|
245
|
+
* Add controls for raster layers
|
|
246
|
+
*/
|
|
247
|
+
private addRasterControls;
|
|
248
|
+
/**
|
|
249
|
+
* Add controls for symbol layers
|
|
250
|
+
*/
|
|
251
|
+
private addSymbolControls;
|
|
252
|
+
/**
|
|
253
|
+
* Create a color control
|
|
254
|
+
*/
|
|
255
|
+
private createColorControl;
|
|
256
|
+
/**
|
|
257
|
+
* Create a slider control
|
|
258
|
+
*/
|
|
259
|
+
private createSliderControl;
|
|
260
|
+
/**
|
|
261
|
+
* Reset layer style to original
|
|
262
|
+
*/
|
|
263
|
+
private resetLayerStyle;
|
|
264
|
+
/**
|
|
265
|
+
* Update layer states from map (sync UI with map)
|
|
266
|
+
*/
|
|
267
|
+
private updateLayerStatesFromMap;
|
|
268
|
+
/**
|
|
269
|
+
* Update UI elements for a specific layer
|
|
270
|
+
*/
|
|
271
|
+
private updateUIForLayer;
|
|
272
|
+
/**
|
|
273
|
+
* Check for new layers and add them to the control
|
|
274
|
+
*/
|
|
275
|
+
private checkForNewLayers;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Options for LayerControl constructor
|
|
280
|
+
*/
|
|
281
|
+
export declare interface LayerControlOptions {
|
|
282
|
+
/** Whether the control starts collapsed (default: true) */
|
|
283
|
+
collapsed?: boolean;
|
|
284
|
+
/** Initial layer states (keyed by layer ID) */
|
|
285
|
+
layerStates?: LayerStates;
|
|
286
|
+
/** Array of layer IDs to control (if not specified, controls all layers) */
|
|
287
|
+
layers?: string[];
|
|
288
|
+
/** Initial panel width in pixels (default: 320) */
|
|
289
|
+
panelWidth?: number;
|
|
290
|
+
/** Minimum panel width in pixels (default: 240) */
|
|
291
|
+
panelMinWidth?: number;
|
|
292
|
+
/** Maximum panel width in pixels (default: 420) */
|
|
293
|
+
panelMaxWidth?: number;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* State for a single layer
|
|
298
|
+
*/
|
|
299
|
+
export declare interface LayerState {
|
|
300
|
+
/** Whether the layer is visible */
|
|
301
|
+
visible: boolean;
|
|
302
|
+
/** Layer opacity (0-1) */
|
|
303
|
+
opacity: number;
|
|
304
|
+
/** Display name for the layer */
|
|
305
|
+
name: string;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Collection of layer states keyed by layer ID
|
|
310
|
+
*/
|
|
311
|
+
export declare interface LayerStates {
|
|
312
|
+
[layerId: string]: LayerState;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Normalize a color value to hex format
|
|
317
|
+
* Handles hex strings, RGB strings, and RGB arrays
|
|
318
|
+
* @param value Color value in various formats
|
|
319
|
+
* @returns Normalized hex color string (always 6 digits)
|
|
320
|
+
*/
|
|
321
|
+
export declare function normalizeColor(value: any): string;
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Paint properties for different layer types
|
|
325
|
+
*/
|
|
326
|
+
export declare interface PaintProperty {
|
|
327
|
+
/** Property name (e.g., 'fill-color') */
|
|
328
|
+
name: string;
|
|
329
|
+
/** Current value */
|
|
330
|
+
value: any;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Convert RGB values to hex color string
|
|
335
|
+
* @param r Red component (0-255)
|
|
336
|
+
* @param g Green component (0-255)
|
|
337
|
+
* @param b Blue component (0-255)
|
|
338
|
+
* @returns Hex color string (e.g., '#ff0000')
|
|
339
|
+
*/
|
|
340
|
+
export declare function rgbToHex(r: number, g: number, b: number): string;
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Set the opacity for a layer
|
|
344
|
+
* @param map MapLibre map instance
|
|
345
|
+
* @param layerId Layer ID
|
|
346
|
+
* @param layerType Layer type
|
|
347
|
+
* @param opacity Opacity value (0-1)
|
|
348
|
+
*/
|
|
349
|
+
export declare function setLayerOpacity(map: Map_2, layerId: string, layerType: string, opacity: number): void;
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* MapLibre layer types that support styling
|
|
353
|
+
*/
|
|
354
|
+
export declare type StyleableLayerType = 'fill' | 'line' | 'circle' | 'symbol' | 'raster';
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Control for a paint property (color picker or slider)
|
|
358
|
+
*/
|
|
359
|
+
export declare interface StyleControlConfig {
|
|
360
|
+
/** Label to display */
|
|
361
|
+
label: string;
|
|
362
|
+
/** Paint property name */
|
|
363
|
+
property: string;
|
|
364
|
+
/** Control type */
|
|
365
|
+
type: 'color' | 'slider';
|
|
366
|
+
/** For sliders: minimum value */
|
|
367
|
+
min?: number;
|
|
368
|
+
/** For sliders: maximum value */
|
|
369
|
+
max?: number;
|
|
370
|
+
/** For sliders: step increment */
|
|
371
|
+
step?: number;
|
|
372
|
+
/** Default value if property is not set */
|
|
373
|
+
defaultValue?: any;
|
|
374
|
+
}
|
|
375
|
+
|
|
1
376
|
export { }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "maplibre-gl-layer-control",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A comprehensive layer control for MapLibre GL with advanced styling capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -89,5 +89,8 @@
|
|
|
89
89
|
"vite": "^6.0.0",
|
|
90
90
|
"vite-plugin-dts": "^4.3.0",
|
|
91
91
|
"vitest": "^2.1.0"
|
|
92
|
+
},
|
|
93
|
+
"dependencies": {
|
|
94
|
+
"maplibre-gl-layer-control": "^0.2.0"
|
|
92
95
|
}
|
|
93
96
|
}
|