diff --git a/.vscode/settings.json b/.vscode/settings.json index d6328f3..5621a9d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,9 +1,9 @@ { "rust-analyzer.linkedProjects": [ ".\\examples\\simple-map\\Cargo.toml", + ".\\examples\\with-server\\Cargo.toml", ".\\Cargo.toml" ], - "rust-analyzer.server.path": "rust-analyzer", // "rust-analyzer.server.extraEnv": {"RA_LOG":"project_model=debug"}, // "rust-analyzer.trace.server": "messages", "rust-analyzer.check.allTargets": false, @@ -14,7 +14,7 @@ "rust-analyzer.workspace.symbol.search.kind": "all_symbols", "rust-analyzer.workspace.symbol.search.limit": 512, "rust-analyzer.cargo.noDefaultFeatures": true, - "rust-analyzer.cargo.features": ["csr"], + "rust-analyzer.cargo.features": [], "rust-analyzer.procMacro.enable": true, "rust-analyzer.procMacro.attributes.enable": true } \ No newline at end of file diff --git a/leaflet/src/crs.rs b/leaflet/src/crs.rs new file mode 100644 index 0000000..b1542a7 --- /dev/null +++ b/leaflet/src/crs.rs @@ -0,0 +1,30 @@ +use wasm_bindgen::prelude::*; + +use crate::{LatLng, Point}; + +#[wasm_bindgen] +extern "C" { + #[derive(Debug)] + pub type Crs; + + #[wasm_bindgen(constructor, js_namespace = ["L", "CRS"], js_name = "Simple")] + pub fn new_simple() -> Crs; + + #[wasm_bindgen(constructor, js_namespace = ["L", "CRS"], js_name = "Earth")] + pub fn new_earth() -> Crs; + + #[wasm_bindgen(constructor, js_namespace = ["L", "CRS"], js_name = "EPSG3395")] + pub fn new_epsg_3395() -> Crs; + + #[wasm_bindgen(constructor, js_namespace = ["L", "CRS"], js_name = "EPSG3857")] + pub fn new_epsg_3857() -> Crs; + + #[wasm_bindgen(constructor, js_namespace = ["L", "CRS"], js_name = "EPSG4326")] + pub fn new_epsg_4326() -> Crs; + + #[wasm_bindgen(constructor, js_namespace = ["L", "CRS"], js_name = "Base")] + pub fn new_base() -> Crs; + + #[wasm_bindgen(method, js_name = "latLngToPoint")] + pub fn latLngToPoint(this: &Crs, latlng: LatLng, zoom: f32) -> Point; +} diff --git a/leaflet/src/feature_group.rs b/leaflet/src/feature_group.rs new file mode 100644 index 0000000..946f170 --- /dev/null +++ b/leaflet/src/feature_group.rs @@ -0,0 +1,27 @@ +use wasm_bindgen::prelude::*; + +use crate::{LatLngBounds, LayerGroup}; + +#[wasm_bindgen] +extern "C" { + /// [`FeatureGroup`](https://leafletjs.com/reference-1.7.1.html#featuregroup) + #[derive(Clone, Debug)] + #[wasm_bindgen(extends = LayerGroup)] + pub type FeatureGroup; + + /// [`setStyle`](https://leafletjs.com/reference-1.7.1.html#featuregroup-setstyle) + #[wasm_bindgen(method)] + pub fn setStyle(this: &FeatureGroup, style: &JsValue); + + /// [`bringToFront`](https://leafletjs.com/reference-1.7.1.html#featuregroup-bringtofront) + #[wasm_bindgen(method)] + pub fn bringToFront(this: &FeatureGroup); + + /// [`bringToBack`](https://leafletjs.com/reference-1.7.1.html#featuregroup-bringtoback) + #[wasm_bindgen(method)] + pub fn bringToBack(this: &FeatureGroup); + + /// [`getBounds`](https://leafletjs.com/reference-1.7.1.html#featuregroup-getbounds) + #[wasm_bindgen(method)] + pub fn getBounds(this: &FeatureGroup) -> LatLngBounds; +} diff --git a/leaflet/src/geo_json.rs b/leaflet/src/geo_json.rs new file mode 100644 index 0000000..d5a2d9d --- /dev/null +++ b/leaflet/src/geo_json.rs @@ -0,0 +1,27 @@ +use wasm_bindgen::prelude::*; + +use crate::Layer; + +#[wasm_bindgen] +extern "C" { + /// [`GeoJSON`](https://leafletjs.com/reference-1.7.1.html#geojson) + #[derive(Clone, Debug)] + #[wasm_bindgen(extends = Layer)] + pub type GeoJSON; + + /// [`L.geoJSON`](https://leafletjs.com/reference-1.7.1.html#geojson-l-geojson) + #[wasm_bindgen(js_namespace = L)] + pub fn geoJSON(geojson: &JsValue, options: &JsValue) -> GeoJSON; + + /// [`addData`](https://leafletjs.com/reference-1.7.1.html#geojson-adddata) + #[wasm_bindgen(method)] + pub fn addData(this: &GeoJSON, data: &JsValue); + + /// [`resetStyle`](https://leafletjs.com/reference-1.7.1.html#geojson-resetstyle) + #[wasm_bindgen(method)] + pub fn resetStyle(this: &GeoJSON, layer: Option<&Layer>); + + /// [`setStyle`](https://leafletjs.com/reference-1.7.1.html#geojson-setstyle) + #[wasm_bindgen(method)] + pub fn setStyle(this: &GeoJSON, style: &JsValue); +} diff --git a/leaflet/src/latlng_bounds.rs b/leaflet/src/latlng_bounds.rs new file mode 100644 index 0000000..7ea975f --- /dev/null +++ b/leaflet/src/latlng_bounds.rs @@ -0,0 +1,21 @@ +use wasm_bindgen::prelude::*; + +use crate::LatLng; + +#[wasm_bindgen] +extern "C" { + #[derive(Debug)] + pub type LatLngBounds; + + #[wasm_bindgen(constructor, js_namespace = L)] + pub fn new(corner1: &LatLng, corner2: &LatLng) -> LatLngBounds; + + #[wasm_bindgen(method)] + pub fn getNorthEast(this: &LatLngBounds) -> LatLng; + + #[wasm_bindgen(method)] + pub fn getSouthWest(this: &LatLngBounds) -> LatLng; + + #[wasm_bindgen(method)] + pub fn contains(this: &LatLngBounds, latlng: &LatLng) -> bool; +} diff --git a/leaflet/src/lib.rs b/leaflet/src/lib.rs index aa15258..5e8c795 100644 --- a/leaflet/src/lib.rs +++ b/leaflet/src/lib.rs @@ -1,26 +1,31 @@ mod control; +mod crs; mod div_icon; mod div_overlay; mod event; mod evented; +mod feature_group; +mod geo_json; mod grid_layer; mod handler; mod icon; mod lat_lng; +mod latlng_bounds; mod layer; mod layer_control; mod layer_group; mod map; mod marker; +mod point; mod popup; mod raster; mod shapes; mod tooltip; use js_sys::Array; -use wasm_bindgen::prelude::*; pub use control::Control; +pub use crs::Crs; pub use div_icon::{DivIcon, DivIconOptions}; pub use div_overlay::DivOverlay; pub use event::Event; @@ -28,10 +33,13 @@ pub use evented::{ DragEvents, Evented, EventedHandle, LayerEvents, MouseEvents, MoveEvents, PopupEvents, TooltipEvents, }; +pub use feature_group::FeatureGroup; +pub use geo_json::GeoJSON; pub use grid_layer::{GridLayer, GridLayerOptions}; pub use handler::Handler; pub use icon::{Icon, IconOptions}; pub use lat_lng::LatLng; +pub use latlng_bounds::LatLngBounds; pub use layer::Layer; pub use layer_group::LayerGroup; pub use map::{ @@ -39,6 +47,7 @@ pub use map::{ PopupEvent, TooltipEvent, }; pub use marker::{Marker, MarkerOptions}; +pub use point::Point; pub use popup::{Popup, PopupOptions}; pub use raster::{ ImageOverlay, ImageOverlayOptions, TileLayer, TileLayerOptions, TileLayerWms, @@ -53,7 +62,7 @@ pub use tooltip::{Tooltip, TooltipOptions}; #[macro_export] macro_rules! object_property_set { ($a:ident, $b:ty) => { - pub fn $a(&mut self, val: $b) -> &mut Self { + pub unsafe fn $a(&mut self, val: $b) -> &mut Self { let r = js_sys::Reflect::set( self.as_ref(), &wasm_bindgen::JsValue::from(stringify!($a)), @@ -64,7 +73,7 @@ macro_rules! object_property_set { } }; ($a:ident, $b:ident, $c:ty) => { - pub fn $a(&mut self, val: $c) -> &mut Self { + pub unsafe fn $a(&mut self, val: $c) -> &mut Self { let r = js_sys::Reflect::set( self.as_ref(), &wasm_bindgen::JsValue::from(stringify!($b)), @@ -103,149 +112,6 @@ macro_rules! object_constructor { }; } -#[wasm_bindgen] -extern "C" { - // CRS - #[derive(Debug)] - pub type Crs; - - #[wasm_bindgen(constructor, js_namespace = ["L", "CRS"], js_name = "Simple")] - pub fn new_simple() -> Crs; - - #[wasm_bindgen(constructor, js_namespace = ["L", "CRS"], js_name = "Earth")] - pub fn new_earth() -> Crs; - - #[wasm_bindgen(constructor, js_namespace = ["L", "CRS"], js_name = "EPSG3395")] - pub fn new_epsg_3395() -> Crs; - - #[wasm_bindgen(constructor, js_namespace = ["L", "CRS"], js_name = "EPSG3857")] - pub fn new_epsg_3857() -> Crs; - - #[wasm_bindgen(constructor, js_namespace = ["L", "CRS"], js_name = "EPSG4326")] - pub fn new_epsg_4326() -> Crs; - - #[wasm_bindgen(constructor, js_namespace = ["L", "CRS"], js_name = "Base")] - pub fn new_base() -> Crs; - - #[wasm_bindgen(method, js_name = "latLngToPoint")] - pub fn latLngToPoint(this: &Crs, latlng: LatLng, zoom: f32) -> Point; - - // Point - #[derive(Debug)] - pub type Point; - - #[wasm_bindgen(constructor, js_namespace = L)] - pub fn new(x: f64, y: f64) -> Point; - - #[wasm_bindgen(method, getter)] - pub fn x(this: &Point) -> f64; - - #[wasm_bindgen(method, getter)] - pub fn y(this: &Point) -> f64; - - #[wasm_bindgen(method)] - pub fn add(this: &Point, other: &Point) -> Point; - - #[wasm_bindgen(method)] - pub fn subtract(this: &Point, other: &Point) -> Point; - - #[wasm_bindgen(method)] - pub fn multiplyBy(this: &Point, scalar: f64) -> Point; - - #[wasm_bindgen(method)] - pub fn divideBy(this: &Point, scalar: f64) -> Point; - - #[wasm_bindgen(method)] - pub fn scaleBy(this: &Point, other: &Point) -> Point; - - #[wasm_bindgen(method)] - pub fn unscaleByTo(this: &Point, other: &Point) -> Point; - - #[wasm_bindgen(method)] - pub fn round(this: &Point) -> Point; - - #[wasm_bindgen(method)] - pub fn floor(this: &Point) -> Point; - - #[wasm_bindgen(method)] - pub fn ceil(this: &Point) -> Point; - - #[wasm_bindgen(method)] - pub fn trunc(this: &Point) -> bool; - - #[wasm_bindgen(method)] - pub fn equals(this: &Point, other: &Point) -> bool; - - #[wasm_bindgen(method)] - pub fn contains(this: &Point, other: &Point) -> f64; - - #[wasm_bindgen(method)] - pub fn distanceTo(this: &Point, other: &Point) -> f64; - - // LatLngBounds - - #[derive(Debug)] - pub type LatLngBounds; - - #[wasm_bindgen(constructor, js_namespace = L)] - pub fn new(corner1: &LatLng, corner2: &LatLng) -> LatLngBounds; - - #[wasm_bindgen(method)] - pub fn getNorthEast(this: &LatLngBounds) -> LatLng; - - #[wasm_bindgen(method)] - pub fn getSouthWest(this: &LatLngBounds) -> LatLng; - - #[wasm_bindgen(method)] - pub fn contains(this: &LatLngBounds, latlng: &LatLng) -> bool; - - // FeatureGroup - - /// [`FeatureGroup`](https://leafletjs.com/reference-1.7.1.html#featuregroup) - #[derive(Clone, Debug)] - #[wasm_bindgen(extends = LayerGroup)] - pub type FeatureGroup; - - /// [`setStyle`](https://leafletjs.com/reference-1.7.1.html#featuregroup-setstyle) - #[wasm_bindgen(method)] - pub fn setStyle(this: &FeatureGroup, style: &JsValue); - - /// [`bringToFront`](https://leafletjs.com/reference-1.7.1.html#featuregroup-bringtofront) - #[wasm_bindgen(method)] - pub fn bringToFront(this: &FeatureGroup); - - /// [`bringToBack`](https://leafletjs.com/reference-1.7.1.html#featuregroup-bringtoback) - #[wasm_bindgen(method)] - pub fn bringToBack(this: &FeatureGroup); - - /// [`getBounds`](https://leafletjs.com/reference-1.7.1.html#featuregroup-getbounds) - #[wasm_bindgen(method)] - pub fn getBounds(this: &FeatureGroup) -> LatLngBounds; - - // GeoJSON - - /// [`GeoJSON`](https://leafletjs.com/reference-1.7.1.html#geojson) - #[derive(Clone, Debug)] - #[wasm_bindgen(extends = Layer)] - pub type GeoJSON; - - /// [`L.geoJSON`](https://leafletjs.com/reference-1.7.1.html#geojson-l-geojson) - #[wasm_bindgen(js_namespace = L)] - pub fn geoJSON(geojson: &JsValue, options: &JsValue) -> GeoJSON; - - /// [`addData`](https://leafletjs.com/reference-1.7.1.html#geojson-adddata) - #[wasm_bindgen(method)] - pub fn addData(this: &GeoJSON, data: &JsValue); - - /// [`resetStyle`](https://leafletjs.com/reference-1.7.1.html#geojson-resetstyle) - #[wasm_bindgen(method)] - pub fn resetStyle(this: &GeoJSON, layer: Option<&Layer>); - - /// [`setStyle`](https://leafletjs.com/reference-1.7.1.html#geojson-setstyle) - #[wasm_bindgen(method)] - pub fn setStyle(this: &GeoJSON, style: &JsValue); -} - #[allow(clippy::from_over_into)] impl Into for (LatLng, LatLng) { fn into(self) -> LatLngBounds { diff --git a/leaflet/src/point.rs b/leaflet/src/point.rs new file mode 100644 index 0000000..7d5cb6a --- /dev/null +++ b/leaflet/src/point.rs @@ -0,0 +1,55 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + #[derive(Debug)] + pub type Point; + + #[wasm_bindgen(constructor, js_namespace = L)] + pub fn new(x: f64, y: f64) -> Point; + + #[wasm_bindgen(method, getter)] + pub fn x(this: &Point) -> f64; + + #[wasm_bindgen(method, getter)] + pub fn y(this: &Point) -> f64; + + #[wasm_bindgen(method)] + pub fn add(this: &Point, other: &Point) -> Point; + + #[wasm_bindgen(method)] + pub fn subtract(this: &Point, other: &Point) -> Point; + + #[wasm_bindgen(method)] + pub fn multiplyBy(this: &Point, scalar: f64) -> Point; + + #[wasm_bindgen(method)] + pub fn divideBy(this: &Point, scalar: f64) -> Point; + + #[wasm_bindgen(method)] + pub fn scaleBy(this: &Point, other: &Point) -> Point; + + #[wasm_bindgen(method)] + pub fn unscaleByTo(this: &Point, other: &Point) -> Point; + + #[wasm_bindgen(method)] + pub fn round(this: &Point) -> Point; + + #[wasm_bindgen(method)] + pub fn floor(this: &Point) -> Point; + + #[wasm_bindgen(method)] + pub fn ceil(this: &Point) -> Point; + + #[wasm_bindgen(method)] + pub fn trunc(this: &Point) -> bool; + + #[wasm_bindgen(method)] + pub fn equals(this: &Point, other: &Point) -> bool; + + #[wasm_bindgen(method)] + pub fn contains(this: &Point, other: &Point) -> f64; + + #[wasm_bindgen(method)] + pub fn distanceTo(this: &Point, other: &Point) -> f64; +} \ No newline at end of file diff --git a/leptos-leaflet/src/components/map_container.rs b/leptos-leaflet/src/components/map_container.rs index f3b61ff..cafc542 100644 --- a/leptos-leaflet/src/components/map_container.rs +++ b/leptos-leaflet/src/components/map_container.rs @@ -44,17 +44,16 @@ pub fn MapContainer( let map_load = map_ref; map_load.on_load(move |map_div| { - let center = center; let html_node = map_div.unchecked_ref::(); // Randomize the id of the map if html_node.id().is_empty() { let id = format!("map-{}", rand::random::()); - map_div.clone().id(id); + _ = map_div.clone().id(id); } let events = events.clone(); let popup_events = popup_events.clone(); let tooltip_events = tooltip_events.clone(); - map_div.on_mount(move |map_div| { + _ = map_div.on_mount(move |map_div| { let map_div = map_div.unchecked_ref::(); let mut options = leaflet::MapOptions::new(); options.zoom(zoom);