From 64eaa09c45136f2fe1443a8a791acdea43ac591c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Mond=C3=A9jar=20Rubio?= Date: Tue, 12 Mar 2024 20:52:34 +0100 Subject: [PATCH] Fix SSR support --- Cargo.lock | 1 - leptos_hotkeys/Cargo.toml | 1 - leptos_hotkeys/src/hotkeys_provider.rs | 14 ++--- leptos_hotkeys/src/use_hotkeys.rs | 81 +++++++++++++++----------- 4 files changed, 55 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8fbbdba..18436f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1180,7 +1180,6 @@ dependencies = [ name = "leptos_hotkeys" version = "0.1.6" dependencies = [ - "cfg-if", "leptos", "log", "wasm-bindgen", diff --git a/leptos_hotkeys/Cargo.toml b/leptos_hotkeys/Cargo.toml index 053b07b..e000d6b 100644 --- a/leptos_hotkeys/Cargo.toml +++ b/leptos_hotkeys/Cargo.toml @@ -13,7 +13,6 @@ keywords = ["leptos", "hotkeys", "wasm"] leptos = { version = "0.6.5", features = ["nightly"] } wasm-bindgen = { version = "0.2.92" } log = "0.4.20" -cfg-if = { version = "1.0.0", features = [] } web-sys = { version = "0.3.66" } [lib] diff --git a/leptos_hotkeys/src/hotkeys_provider.rs b/leptos_hotkeys/src/hotkeys_provider.rs index 60e188f..0ac56e2 100644 --- a/leptos_hotkeys/src/hotkeys_provider.rs +++ b/leptos_hotkeys/src/hotkeys_provider.rs @@ -1,5 +1,6 @@ use leptos::html::ElementDescriptor; use leptos::*; +#[cfg_attr(feature = "ssr", allow(unused_imports))] use std::collections::HashMap; use std::collections::HashSet; @@ -11,12 +12,11 @@ use wasm_bindgen::JsCast; pub struct HotkeysContext { #[cfg(not(feature = "ssr"))] pub(crate) pressed_keys: RwSignal>, - #[cfg(not(feature = "ssr"))] pub active_ref_target: RwSignal>, - #[cfg(not(feature = "ssr"))] pub set_ref_target: Callback>, + pub active_scopes: RwSignal>, pub enable_scope: Callback, pub disable_scope: Callback, @@ -24,8 +24,8 @@ pub struct HotkeysContext { } pub fn provide_hotkeys_context( - node_ref: NodeRef, - allow_blur_event: bool, + #[cfg_attr(feature = "ssr", allow(unused_variables))] node_ref: NodeRef, + #[cfg_attr(feature = "ssr", allow(unused_variables))] allow_blur_event: bool, initially_active_scopes: HashSet, ) -> HotkeysContext where @@ -81,13 +81,13 @@ where }) }); - #[cfg(feature = "debug")] + #[cfg(all(feature = "debug", not(feature = "ssr")))] create_effect(move |_| { - let pressed_keys_list = - move || pressed_keys.get().keys().cloned().collect::>(); + let pressed_keys_list = move || pressed_keys.get().keys().cloned().collect::>(); logging::log!("keys pressed: {:?}", pressed_keys_list()); }); + #[cfg(not(feature = "ssr"))] node_ref.on_load(move |_| { let blur_listener = wasm_bindgen::closure::Closure::wrap(Box::new(move || { if cfg!(feature = "debug") { diff --git a/leptos_hotkeys/src/use_hotkeys.rs b/leptos_hotkeys/src/use_hotkeys.rs index 3303c6c..8de784c 100644 --- a/leptos_hotkeys/src/use_hotkeys.rs +++ b/leptos_hotkeys/src/use_hotkeys.rs @@ -1,11 +1,14 @@ -use crate::hotkeys_provider::use_hotkeys_context; use crate::types::Hotkey; -use leptos::{ev::DOMEventResponder, html::ElementDescriptor, *}; +use leptos::{html::ElementDescriptor, *}; -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; -fn is_hotkey_match(hotkey: &Hotkey, pressed_keyset: &mut HashMap) -> bool { +#[cfg_attr(feature = "ssr", allow(dead_code))] +fn is_hotkey_match( + hotkey: &Hotkey, + pressed_keyset: &mut HashMap, +) -> bool { let mut modifiers_match = true; if hotkey.modifiers.ctrl { @@ -41,47 +44,58 @@ fn is_hotkey_match(hotkey: &Hotkey, pressed_keyset: &mut HashMap, - scopes: Vec, + #[cfg_attr(feature = "ssr", allow(unused_variables))] key_combination: String, + #[cfg_attr(feature = "ssr", allow(unused_variables))] on_triggered: Callback<()>, + #[cfg_attr(feature = "ssr", allow(unused_variables))] scopes: Vec, ) { - let parsed_keys: HashSet = key_combination.split(',').map(Hotkey::new).collect(); + #[cfg(not(feature = "ssr"))] + { + use crate::hotkeys_provider::use_hotkeys_context; + use std::collections::HashSet; - let hotkeys_context = use_hotkeys_context(); - let pressed_keys = hotkeys_context.pressed_keys; + let parsed_keys: HashSet = key_combination.split(',').map(Hotkey::new).collect(); - create_effect(move |_| { - let active_scopes = hotkeys_context.active_scopes.get(); - let within_scope = scopes.iter().any(|scope| active_scopes.contains(scope)); - - if within_scope { - let mut pressed_keyset = pressed_keys.get(); - if let Some(matching_hotkey) = parsed_keys - .iter() - .find(|hotkey| is_hotkey_match(hotkey, &mut pressed_keyset)) - { - if cfg!(feature = "debug") { - let message = format!("%cfiring hotkey: {}", &matching_hotkey); - web_sys::console::log_2( - &wasm_bindgen::JsValue::from_str(&message), - &wasm_bindgen::JsValue::from_str("color: #39FF14;"), - ); + let hotkeys_context = use_hotkeys_context(); + let pressed_keys = hotkeys_context.pressed_keys; + + create_effect(move |_| { + let active_scopes = hotkeys_context.active_scopes.get(); + let within_scope = scopes.iter().any(|scope| active_scopes.contains(scope)); + + if within_scope { + let mut pressed_keyset = pressed_keys.get(); + if let Some(matching_hotkey) = parsed_keys + .iter() + .find(|hotkey| is_hotkey_match(hotkey, &mut pressed_keyset)) + { + if cfg!(feature = "debug") { + let message = format!("%cfiring hotkey: {}", &matching_hotkey); + web_sys::console::log_2( + &wasm_bindgen::JsValue::from_str(&message), + &wasm_bindgen::JsValue::from_str("color: #39FF14;"), + ); + } + Callable::call(&on_triggered, ()); } - Callable::call(&on_triggered, ()); } - } - }); + }); + } } pub fn use_hotkeys_ref_scoped( - node_ref: NodeRef, - key_combination: String, - on_triggered: Callback<()>, - scopes: Vec, + #[cfg_attr(feature = "ssr", allow(unused_variables))] node_ref: NodeRef, + #[cfg_attr(feature = "ssr", allow(unused_variables))] key_combination: String, + #[cfg_attr(feature = "ssr", allow(unused_variables))] on_triggered: Callback<()>, + #[cfg_attr(feature = "ssr", allow(unused_variables))] scopes: Vec, ) where T: ElementDescriptor + 'static + Clone, { + #[cfg(not(feature = "ssr"))] create_effect(move |_| { + use crate::hotkeys_provider::use_hotkeys_context; + use leptos::ev::DOMEventResponder; + use std::collections::HashSet; + let parsed_keys: HashSet = key_combination.split(',').map(Hotkey::new).collect(); let scopes = scopes.clone(); if let Some(element) = node_ref.get() { @@ -108,6 +122,7 @@ pub fn use_hotkeys_ref_scoped( } }; + // element.add needs `leptos::ev::DOMEventResponder` let _ = element.add(ev::keypress, keydown_closure); } });