|
| 1 | +window.hyperdiv.registerPlugin("leaflet", ctx => { |
| 2 | + const L = window.L; |
| 3 | + |
| 4 | + const props = {...ctx.initialProps}; |
| 5 | + |
| 6 | + const mapContainer = document.createElement("div"); |
| 7 | + mapContainer.style.width = "100%"; |
| 8 | + mapContainer.style.height = "100%"; |
| 9 | + ctx.domElement.appendChild(mapContainer); |
| 10 | + |
| 11 | + const map = L.map(mapContainer); |
| 12 | + map.setView([props.lat, props.lng], props.zoom); |
| 13 | + |
| 14 | + L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", { |
| 15 | + maxZoom: 19, |
| 16 | + attribution: |
| 17 | + '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>', |
| 18 | + }).addTo(map); |
| 19 | + |
| 20 | + map.on("zoomend", () => { |
| 21 | + props.zoom = map.getZoom(); |
| 22 | + ctx.updateProp("zoom", props.zoom); |
| 23 | + }); |
| 24 | + |
| 25 | + map.on("moveend", () => { |
| 26 | + const center = map.getCenter(); |
| 27 | + props.lat = center.lat; |
| 28 | + props.lng = center.lng; |
| 29 | + ctx.updateProp("lat", props.lat); |
| 30 | + ctx.updateProp("lng", props.lng); |
| 31 | + }); |
| 32 | + |
| 33 | + ctx.onPropUpdate((propName, propValue) => { |
| 34 | + props[propName] = propValue; |
| 35 | + map.setView([props.lat, props.lng], props.zoom); |
| 36 | + }); |
| 37 | + |
| 38 | + const resizeObserver = new ResizeObserver(() => map.invalidateSize()); |
| 39 | + resizeObserver.observe(mapContainer); |
| 40 | +}); |
0 commit comments