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

Update_to_0.6.1 #16

Merged
merged 2 commits into from
Nov 16, 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
34 changes: 9 additions & 25 deletions examples/simple-map/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 16 additions & 32 deletions examples/with-server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion leptos-leaflet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
[package]
name = "leptos-leaflet"
version = "0.6.1-beta"
authors = ["Daniel Santana <[email protected]>", "Artur Leao <[email protected]>", "Lewin Probst <[email protected]>"]
version = "0.6.1"
edition = "2021"
license = "MIT"
repository = "https://github.com/headless-studio/leptos-leaflet"
description = "Leaflet components to use in Leptos."
readme = "../README.md"

[dependencies]
js-sys.workspace = true
Expand Down
32 changes: 31 additions & 1 deletion leptos-leaflet/src/components/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,43 @@ impl LeafletMapContext {
}
}

/// Sets the map for the context.
///
/// # Arguments
///
/// * `map` - A reference to a `leaflet::Map` object.
pub fn set_map(&self, map: &leaflet::Map) {
self.map.set(Some(map.clone()));
}

/// Returns an optional `leaflet::Map` instance.
pub fn map(&self) -> Option<leaflet::Map> {
self.map.get()
}

/// Returns a signal that emits the current map instance.
pub fn map_signal(&self) -> ReadSignal<Option<leaflet::Map>> {
self.map.read_only()
}

/// Adds a layer to the context.
///
/// # Arguments
///
/// * `layer` - A reference to the layer to be added.
///
pub fn add_layer<L: Into<leaflet::Layer> + Clone>(&self, layer: &L) {
let map = self.map.get_untracked().expect("Map to be available");
let layer: leaflet::Layer = layer.to_owned().into();
layer.add_to(&map);
}

/// Removes a layer from the context.
///
/// # Arguments
///
/// * `layer` - A reference to the layer to be removed.
///
pub fn remove_layer<L: Into<leaflet::Layer> + Clone>(&self, layer: &L) {
let map = self.map.get_untracked().expect("Map to be available");
let layer: leaflet::Layer = layer.to_owned().into();
Expand All @@ -44,38 +63,49 @@ impl Default for LeafletMapContext {
}
}

/// Provides the Leaflet map context.
pub fn provide_leaflet_context() -> LeafletMapContext {
let context = LeafletMapContext::new();
provide_context(context);
context
}

/// Returns an optional `LeafletMapContext` object that can be used to access the current state of the Leaflet map.
pub fn use_leaflet_context() -> Option<LeafletMapContext> {
use_context::<LeafletMapContext>()
}

/// Extends the context with an overlay container.
pub fn extend_context_with_overlay() -> LeafletOverlayContainerContext {
let overlay_context = LeafletOverlayContainerContext::new();
provide_context(overlay_context);
overlay_context
}

/// Returns an optional `LeafletOverlayContainerContext` for the current overlay.
pub fn use_overlay_context() -> Option<LeafletOverlayContainerContext> {
use_context::<LeafletOverlayContainerContext>()
}

/// Returns an optional `leafet::Layer` for the overlay context layer.
pub fn use_overlay_context_layer<T>() -> Option<T>
where
T: Into<leaflet::Layer> + Clone + JsCast,
{
expect_context::<LeafletOverlayContainerContext>().container::<T>()
}

/// Updates the overlay context with the given layer.
///
/// # Arguments
///
/// * `layer` - A cloneable object that can be converted into a `leaflet::Layer`.
pub fn update_overlay_context<C: Into<leaflet::Layer> + Clone>(layer: &C) {
let overlay_context = use_context::<LeafletOverlayContainerContext>().expect("overlay context");
overlay_context.set_container(layer);
}

/// A context struct for Leaflet overlay container.
#[derive(Debug, Clone, Copy)]
pub struct LeafletOverlayContainerContext {
container: RwSignal<Option<leaflet::Layer>>,
Expand Down Expand Up @@ -110,4 +140,4 @@ impl Default for LeafletOverlayContainerContext {
fn default() -> Self {
Self::new()
}
}
}