maplibre-gl-layer 0.1.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/LICENSE +21 -0
- package/README.md +38 -0
- package/dist/components/LayerDialog.d.ts +12 -0
- package/dist/layers.svg +1 -0
- package/dist/main.d.ts +0 -0
- package/dist/maplibre-gl-layer.css +1 -0
- package/dist/maplibre-gl-layer.d.ts +24 -0
- package/dist/maplibre-gl-layer.js +116 -0
- package/dist/maplibre-gl-layer.umd.cjs +1 -0
- package/package.json +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Kazuki Sato
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# maplibre-gl-layer
|
|
2
|
+
|
|
3
|
+
A layer control for [MapLibre GL JS](https://maplibre.org/maplibre-gl-js/docs/).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install maplibre-gl-layer
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { Map } from "maplibre-gl";
|
|
15
|
+
import "maplibre-gl/dist/maplibre-gl.css";
|
|
16
|
+
import { LayerControl } from "maplibre-gl-layer";
|
|
17
|
+
|
|
18
|
+
const map = new Map({
|
|
19
|
+
/* YOUR_MAP_OPTIONS */
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
map.addControl(new LayerControl({
|
|
23
|
+
layers: [
|
|
24
|
+
{
|
|
25
|
+
name: 'light',
|
|
26
|
+
style: 'https://api.protomaps.com/styles/v4/white/ja.json',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: 'dark',
|
|
30
|
+
style: 'https://api.protomaps.com/styles/v4/black/ja.json',
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
}))
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
This project is licensed under the terms of the [MIT license](./LICENSE).
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ControlPosition } from 'maplibre-gl';
|
|
2
|
+
import { LayerControlOptions, Layer } from '../maplibre-gl-layer';
|
|
3
|
+
export declare class LayerDialog {
|
|
4
|
+
private dialog;
|
|
5
|
+
private options;
|
|
6
|
+
private layerChangeCallback;
|
|
7
|
+
constructor(options: LayerControlOptions, defaultLayer: Layer);
|
|
8
|
+
open(button: HTMLButtonElement, position?: ControlPosition | null): void;
|
|
9
|
+
close(): void;
|
|
10
|
+
on(type: string, callback: (layer: Layer) => void): void;
|
|
11
|
+
private setPosition;
|
|
12
|
+
}
|
package/dist/layers.svg
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#000"><path d="M480-118 120-398l66-50 294 228 294-228 66 50-360 280Zm0-202L120-600l360-280 360 280-360 280Zm0-280Zm0 178 230-178-230-178-230 178 230 178Z"/></svg>
|
package/dist/main.d.ts
ADDED
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.maplibregl-ctrl button.maplibregl-ctrl-layers .maplibregl-ctrl-icon{background-image:url(./layers.svg)}.maplibregl-ctrl-layers-dialog{position:absolute;margin:0;padding:0;background-color:transparent;border:none;box-shadow:0 0 0 2px #0000001a;border-radius:5px}.maplibregl-ctrl-layers-dialog .maplibregl-ctrl-layers-dialog-content{padding:6px 14px;background-color:#fff}.maplibregl-ctrl-layers-dialog::backdrop{background:#fff3}.maplibregl-ctrl-layers-dialog-item{margin:6px 0}.maplibregl-ctrl-layers-dialog-item label{display:flex;align-items:center;font-size:14px;color:#333}.maplibregl-ctrl-layers-dialog-item input{margin:0 8px 0 0}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Map, IControl } from 'maplibre-gl';
|
|
2
|
+
export type Layer = {
|
|
3
|
+
name: string;
|
|
4
|
+
style: string;
|
|
5
|
+
};
|
|
6
|
+
export type LayerControlOptions = {
|
|
7
|
+
layers: Layer[];
|
|
8
|
+
};
|
|
9
|
+
export declare class LayerControl implements IControl {
|
|
10
|
+
private map;
|
|
11
|
+
private options;
|
|
12
|
+
private container;
|
|
13
|
+
private currentLayer;
|
|
14
|
+
private currentInitStyle;
|
|
15
|
+
private addedSources;
|
|
16
|
+
private addedLayers;
|
|
17
|
+
private layerDialog;
|
|
18
|
+
constructor(options: LayerControlOptions);
|
|
19
|
+
onAdd(map: Map): HTMLElement;
|
|
20
|
+
onRemove(): void;
|
|
21
|
+
private getPosition;
|
|
22
|
+
private createButton;
|
|
23
|
+
private changeLayer;
|
|
24
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
var u = Object.defineProperty;
|
|
2
|
+
var g = (c, e, t) => e in c ? u(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
|
|
3
|
+
var a = (c, e, t) => g(c, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
class m {
|
|
5
|
+
constructor(e, t) {
|
|
6
|
+
a(this, "dialog");
|
|
7
|
+
a(this, "options");
|
|
8
|
+
a(this, "layerChangeCallback", () => {
|
|
9
|
+
});
|
|
10
|
+
this.options = e, this.dialog = document.createElement("dialog"), this.dialog.classList.add("maplibregl-ctrl-layers-dialog"), document.body.appendChild(this.dialog), this.dialog.addEventListener("click", () => {
|
|
11
|
+
this.dialog.close();
|
|
12
|
+
});
|
|
13
|
+
const i = document.createElement("div");
|
|
14
|
+
i.classList.add("maplibregl-ctrl-layers-dialog-content"), i.addEventListener("click", (s) => {
|
|
15
|
+
s.stopPropagation();
|
|
16
|
+
}), this.dialog.appendChild(i), this.options.layers.forEach((s) => {
|
|
17
|
+
const l = document.createElement("div");
|
|
18
|
+
l.classList.add("maplibregl-ctrl-layers-dialog-item");
|
|
19
|
+
const d = document.createElement("label");
|
|
20
|
+
l.appendChild(d);
|
|
21
|
+
const n = document.createElement("input");
|
|
22
|
+
n.setAttribute("type", "radio"), n.setAttribute("name", "layer"), t.name === s.name && n.setAttribute("checked", "checked"), n.addEventListener("change", () => {
|
|
23
|
+
var p;
|
|
24
|
+
(p = this.layerChangeCallback) == null || p.call(this, s);
|
|
25
|
+
}), d.appendChild(n);
|
|
26
|
+
const h = document.createElement("span");
|
|
27
|
+
h.textContent = s.name, d.appendChild(h), i.appendChild(l);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
open(e, t = null) {
|
|
31
|
+
this.dialog.showModal(), this.setPosition(e, t);
|
|
32
|
+
}
|
|
33
|
+
close() {
|
|
34
|
+
this.dialog.close();
|
|
35
|
+
}
|
|
36
|
+
on(e, t) {
|
|
37
|
+
e === "layerchange" && (this.layerChangeCallback = t);
|
|
38
|
+
}
|
|
39
|
+
setPosition(e, t) {
|
|
40
|
+
const i = e.getBoundingClientRect();
|
|
41
|
+
let s = "", l = "";
|
|
42
|
+
t === "top-right" ? (s = `${i.y}px`, l = `${i.x - this.dialog.clientWidth - 12}px`) : t === "top-left" ? (s = `${i.y}px`, l = `${i.x + i.width + 12}px`) : t === "bottom-right" ? (s = `${i.y - this.dialog.clientHeight + i.height}px`, l = `${i.x - this.dialog.clientWidth - 12}px`) : t === "bottom-left" && (s = `${i.y - this.dialog.clientHeight + i.height}px`, l = `${i.x + i.width + 12}px`), this.dialog.style.setProperty("top", s), this.dialog.style.setProperty("left", l);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
class f {
|
|
46
|
+
constructor(e) {
|
|
47
|
+
a(this, "map", null);
|
|
48
|
+
a(this, "options");
|
|
49
|
+
a(this, "container");
|
|
50
|
+
a(this, "currentLayer");
|
|
51
|
+
a(this, "currentInitStyle", null);
|
|
52
|
+
a(this, "addedSources", {});
|
|
53
|
+
a(this, "addedLayers", []);
|
|
54
|
+
a(this, "layerDialog");
|
|
55
|
+
this.options = e, this.container = document.createElement("div"), this.container.classList.add("maplibregl-ctrl", "maplibregl-ctrl-group"), this.currentLayer = e.layers[0], this.layerDialog = new m(this.options, this.currentLayer), this.layerDialog.on("layerchange", (t) => {
|
|
56
|
+
this.changeLayer(t), this.layerDialog.close();
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
onAdd(e) {
|
|
60
|
+
this.map = e, this.map.setStyle(this.currentLayer.style), this.map.once("style.load", () => {
|
|
61
|
+
this.map && (this.currentInitStyle = this.map.getStyle());
|
|
62
|
+
});
|
|
63
|
+
const t = this.createButton();
|
|
64
|
+
return t.addEventListener("click", () => {
|
|
65
|
+
this.layerDialog.open(t, this.getPosition());
|
|
66
|
+
}), this.container.appendChild(t), this.container;
|
|
67
|
+
}
|
|
68
|
+
onRemove() {
|
|
69
|
+
var e;
|
|
70
|
+
(e = this.container.parentNode) == null || e.removeChild(this.container);
|
|
71
|
+
}
|
|
72
|
+
getPosition() {
|
|
73
|
+
if (!this.map) return null;
|
|
74
|
+
const e = this.map._controlPositions;
|
|
75
|
+
return e["top-left"].querySelector(".maplibregl-ctrl-layers") ? "top-left" : e["top-right"].querySelector(".maplibregl-ctrl-layers") ? "top-right" : e["bottom-left"].querySelector(".maplibregl-ctrl-layers") ? "bottom-left" : e["bottom-right"].querySelector(".maplibregl-ctrl-layers") ? "bottom-right" : null;
|
|
76
|
+
}
|
|
77
|
+
createButton() {
|
|
78
|
+
const e = document.createElement("button");
|
|
79
|
+
e.classList.add("maplibregl-ctrl-layers"), e.setAttribute("title", "Change map style"), e.setAttribute("aria-label", "Change map style");
|
|
80
|
+
const t = document.createElement("i");
|
|
81
|
+
return t.classList.add("maplibregl-ctrl-icon"), e.appendChild(t), e;
|
|
82
|
+
}
|
|
83
|
+
changeLayer(e) {
|
|
84
|
+
if (!this.map) return;
|
|
85
|
+
const t = this.currentInitStyle.layers, i = this.map.getStyle().layers, s = i.filter(
|
|
86
|
+
(r) => !t.some((o) => o.id === r.id)
|
|
87
|
+
);
|
|
88
|
+
this.addedLayers = [...this.addedLayers, ...s];
|
|
89
|
+
const l = t.filter(
|
|
90
|
+
(r) => !i.some((o) => o.id === r.id)
|
|
91
|
+
);
|
|
92
|
+
this.addedLayers = this.addedLayers.filter(
|
|
93
|
+
(r) => !l.some((o) => o.id === r.id)
|
|
94
|
+
);
|
|
95
|
+
const d = this.currentInitStyle.sources, n = this.map.getStyle().sources, h = {};
|
|
96
|
+
Object.entries(n).forEach(([r, o]) => {
|
|
97
|
+
Object.keys(d).includes(r) || (h[r] = o);
|
|
98
|
+
}), this.addedSources = { ...this.addedSources, ...h }, Object.keys(d).filter(
|
|
99
|
+
(r) => !Object.keys(n).includes(r)
|
|
100
|
+
).forEach((r) => {
|
|
101
|
+
delete this.addedSources[r];
|
|
102
|
+
}), this.map.setStyle(e.style, {
|
|
103
|
+
transformStyle: (r, o) => {
|
|
104
|
+
const y = {
|
|
105
|
+
...o,
|
|
106
|
+
sources: { ...o.sources, ...this.addedSources },
|
|
107
|
+
layers: o.layers.concat(this.addedLayers)
|
|
108
|
+
};
|
|
109
|
+
return this.currentInitStyle = y, y;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export {
|
|
115
|
+
f as LayerControl
|
|
116
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(a,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(a=typeof globalThis<"u"?globalThis:a||self,s(a["maplibre-gl-layer"]={}))})(this,function(a){"use strict";var f=Object.defineProperty;var b=(a,s,h)=>s in a?f(a,s,{enumerable:!0,configurable:!0,writable:!0,value:h}):a[s]=h;var o=(a,s,h)=>b(a,typeof s!="symbol"?s+"":s,h);class s{constructor(e,t){o(this,"dialog");o(this,"options");o(this,"layerChangeCallback",()=>{});this.options=e,this.dialog=document.createElement("dialog"),this.dialog.classList.add("maplibregl-ctrl-layers-dialog"),document.body.appendChild(this.dialog),this.dialog.addEventListener("click",()=>{this.dialog.close()});const i=document.createElement("div");i.classList.add("maplibregl-ctrl-layers-dialog-content"),i.addEventListener("click",r=>{r.stopPropagation()}),this.dialog.appendChild(i),this.options.layers.forEach(r=>{const n=document.createElement("div");n.classList.add("maplibregl-ctrl-layers-dialog-item");const p=document.createElement("label");n.appendChild(p);const c=document.createElement("input");c.setAttribute("type","radio"),c.setAttribute("name","layer"),t.name===r.name&&c.setAttribute("checked","checked"),c.addEventListener("change",()=>{var y;(y=this.layerChangeCallback)==null||y.call(this,r)}),p.appendChild(c);const u=document.createElement("span");u.textContent=r.name,p.appendChild(u),i.appendChild(n)})}open(e,t=null){this.dialog.showModal(),this.setPosition(e,t)}close(){this.dialog.close()}on(e,t){e==="layerchange"&&(this.layerChangeCallback=t)}setPosition(e,t){const i=e.getBoundingClientRect();let r="",n="";t==="top-right"?(r=`${i.y}px`,n=`${i.x-this.dialog.clientWidth-12}px`):t==="top-left"?(r=`${i.y}px`,n=`${i.x+i.width+12}px`):t==="bottom-right"?(r=`${i.y-this.dialog.clientHeight+i.height}px`,n=`${i.x-this.dialog.clientWidth-12}px`):t==="bottom-left"&&(r=`${i.y-this.dialog.clientHeight+i.height}px`,n=`${i.x+i.width+12}px`),this.dialog.style.setProperty("top",r),this.dialog.style.setProperty("left",n)}}class h{constructor(e){o(this,"map",null);o(this,"options");o(this,"container");o(this,"currentLayer");o(this,"currentInitStyle",null);o(this,"addedSources",{});o(this,"addedLayers",[]);o(this,"layerDialog");this.options=e,this.container=document.createElement("div"),this.container.classList.add("maplibregl-ctrl","maplibregl-ctrl-group"),this.currentLayer=e.layers[0],this.layerDialog=new s(this.options,this.currentLayer),this.layerDialog.on("layerchange",t=>{this.changeLayer(t),this.layerDialog.close()})}onAdd(e){this.map=e,this.map.setStyle(this.currentLayer.style),this.map.once("style.load",()=>{this.map&&(this.currentInitStyle=this.map.getStyle())});const t=this.createButton();return t.addEventListener("click",()=>{this.layerDialog.open(t,this.getPosition())}),this.container.appendChild(t),this.container}onRemove(){var e;(e=this.container.parentNode)==null||e.removeChild(this.container)}getPosition(){if(!this.map)return null;const e=this.map._controlPositions;return e["top-left"].querySelector(".maplibregl-ctrl-layers")?"top-left":e["top-right"].querySelector(".maplibregl-ctrl-layers")?"top-right":e["bottom-left"].querySelector(".maplibregl-ctrl-layers")?"bottom-left":e["bottom-right"].querySelector(".maplibregl-ctrl-layers")?"bottom-right":null}createButton(){const e=document.createElement("button");e.classList.add("maplibregl-ctrl-layers"),e.setAttribute("title","Change map style"),e.setAttribute("aria-label","Change map style");const t=document.createElement("i");return t.classList.add("maplibregl-ctrl-icon"),e.appendChild(t),e}changeLayer(e){if(!this.map)return;const t=this.currentInitStyle.layers,i=this.map.getStyle().layers,r=i.filter(l=>!t.some(d=>d.id===l.id));this.addedLayers=[...this.addedLayers,...r];const n=t.filter(l=>!i.some(d=>d.id===l.id));this.addedLayers=this.addedLayers.filter(l=>!n.some(d=>d.id===l.id));const p=this.currentInitStyle.sources,c=this.map.getStyle().sources,u={};Object.entries(c).forEach(([l,d])=>{Object.keys(p).includes(l)||(u[l]=d)}),this.addedSources={...this.addedSources,...u},Object.keys(p).filter(l=>!Object.keys(c).includes(l)).forEach(l=>{delete this.addedSources[l]}),this.map.setStyle(e.style,{transformStyle:(l,d)=>{const g={...d,sources:{...d.sources,...this.addedSources},layers:d.layers.concat(this.addedLayers)};return this.currentInitStyle=g,g}})}}a.LayerControl=h,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "maplibre-gl-layer",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A layer control for MapLibre GL JS",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"main": "./dist/maplibre-gl-layer.umd.cjs",
|
|
10
|
+
"module": "./dist/maplibre-gl-layer.js",
|
|
11
|
+
"types": "./dist/maplibre-gl-layer.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/maplibre-gl-layer.js",
|
|
15
|
+
"require": "./dist/maplibre-gl-layer.umd.cjs"
|
|
16
|
+
},
|
|
17
|
+
"./style.css": "./dist/maplibre-gl-layer.css"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"dev": "vite",
|
|
21
|
+
"build": "tsc && vite build --config vite.config.js",
|
|
22
|
+
"preview": "vite preview",
|
|
23
|
+
"lint": "eslint src/*.ts src/**/*.ts",
|
|
24
|
+
"lint:fix": "npm run lint -- --fix"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@eslint/js": "^9.16.0",
|
|
28
|
+
"eslint": "^9.16.0",
|
|
29
|
+
"eslint-config-prettier": "^9.1.0",
|
|
30
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
31
|
+
"globals": "^15.12.0",
|
|
32
|
+
"maplibre-gl": "^5.3.0",
|
|
33
|
+
"pmtiles": "^4.3.0",
|
|
34
|
+
"prettier": "^3.4.1",
|
|
35
|
+
"typescript": "~5.6.2",
|
|
36
|
+
"typescript-eslint": "^8.16.0",
|
|
37
|
+
"vite": "^6.0.1",
|
|
38
|
+
"vite-plugin-dts": "^4.3.0"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/qazsato/maplibre-gl-layer.git"
|
|
43
|
+
},
|
|
44
|
+
"keywords": [
|
|
45
|
+
"MapLibre GL JS",
|
|
46
|
+
"Layer"
|
|
47
|
+
],
|
|
48
|
+
"author": "qazsato <qazsato@gmail.com>",
|
|
49
|
+
"license": "MIT"
|
|
50
|
+
}
|