Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved Leaflet components to modules and update Rust #13

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
}
30 changes: 30 additions & 0 deletions leaflet/src/crs.rs
Original file line number Diff line number Diff line change
@@ -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;
}
27 changes: 27 additions & 0 deletions leaflet/src/feature_group.rs
Original file line number Diff line number Diff line change
@@ -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;
}
27 changes: 27 additions & 0 deletions leaflet/src/geo_json.rs
Original file line number Diff line number Diff line change
@@ -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);
}
21 changes: 21 additions & 0 deletions leaflet/src/latlng_bounds.rs
Original file line number Diff line number Diff line change
@@ -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;
}
158 changes: 12 additions & 146 deletions leaflet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
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;
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::{
DragEndEvent, ErrorEvent, LocateOptions, LocationEvent, Map, MapOptions, MouseEvent,
PopupEvent, TooltipEvent,
};
pub use marker::{Marker, MarkerOptions};
pub use point::Point;
pub use popup::{Popup, PopupOptions};
pub use raster::{
ImageOverlay, ImageOverlayOptions, TileLayer, TileLayerOptions, TileLayerWms,
Expand All @@ -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)),
Expand All @@ -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)),
Expand Down Expand Up @@ -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<LatLngBounds> for (LatLng, LatLng) {
fn into(self) -> LatLngBounds {
Expand Down
Loading