Skip to content

Commit

Permalink
feat: add Document with its methods such as selection, add more metho…
Browse files Browse the repository at this point in the history
…ds to Window
  • Loading branch information
ivanceras committed Apr 2, 2024
1 parent 0a967e3 commit 6e185eb
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
2 changes: 2 additions & 0 deletions crates/core/src/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cfg_if! {if #[cfg(feature = "with-dom")] {
use crate::dom::events::MountEvent;
pub use window::Window;
pub use dom_node::DomNode;
pub use document::Document;

mod application;
pub mod dispatch;
Expand All @@ -47,6 +48,7 @@ cfg_if! {if #[cfg(feature = "with-dom")] {
mod raf;
mod ric;
mod window;
mod document;
mod timeout;


Expand Down
33 changes: 33 additions & 0 deletions crates/core/src/dom/document.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::dom::{dom_node::intern,Cmd};
use futures::channel::mpsc;
use wasm_bindgen::{prelude::*, JsCast};
use crate::dom::document;

/// Provides function for document related functions
#[derive(Clone, Copy)]
pub struct Document;

impl Document {

///
pub fn on_selectionchange<F, MSG>(mut cb: F) -> Cmd<MSG>
where
F: FnMut(Option<web_sys::Selection>) -> MSG + Clone + 'static,
MSG: 'static,
{
let (mut tx, rx) = mpsc::unbounded();
let closure_cb: Closure<dyn FnMut(web_sys::Event)> =
Closure::new(move |_event: web_sys::Event| {
let selection = document().get_selection().ok().flatten();
let msg = cb(selection);
tx.start_send(msg).expect("send");
});
document()
.add_event_listener_with_callback(
intern("selectionchange"),
closure_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, closure_cb)
}
}
66 changes: 66 additions & 0 deletions crates/core/src/dom/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,72 @@ impl Window {
Cmd::sub(rx, mousemove_cb)
}

///
pub fn on_click<F, MSG>(mut cb: F) -> Cmd<MSG>
where
F: FnMut(web_sys::MouseEvent) -> MSG + Clone + 'static,
MSG: 'static,
{
let (mut tx, rx) = mpsc::unbounded();
let mousemove_cb: Closure<dyn FnMut(web_sys::Event)> =
Closure::new(move |event: web_sys::Event| {
let mouse_event: MouseEvent = event.dyn_into().expect("must be mouse event");
let msg = cb(mouse_event);
tx.start_send(msg).expect("send");
});
window()
.add_event_listener_with_callback(
intern("click"),
mousemove_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, mousemove_cb)
}

///
pub fn on_keyup<F, MSG>(mut cb: F) -> Cmd<MSG>
where
F: FnMut(web_sys::KeyboardEvent) -> MSG + Clone + 'static,
MSG: 'static,
{
let (mut tx, rx) = mpsc::unbounded();
let closure_cb: Closure<dyn FnMut(web_sys::Event)> =
Closure::new(move |event: web_sys::Event| {
let key_event: web_sys::KeyboardEvent = event.dyn_into().expect("must be key event");
let msg = cb(key_event);
tx.start_send(msg).expect("send");
});
window()
.add_event_listener_with_callback(
intern("keyup"),
closure_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, closure_cb)
}

///
pub fn on_keydown<F, MSG>(mut cb: F) -> Cmd<MSG>
where
F: FnMut(web_sys::KeyboardEvent) -> MSG + Clone + 'static,
MSG: 'static,
{
let (mut tx, rx) = mpsc::unbounded();
let closure_cb: Closure<dyn FnMut(web_sys::Event)> =
Closure::new(move |event: web_sys::Event| {
let key_event: web_sys::KeyboardEvent = event.dyn_into().expect("must be key event");
let msg = cb(key_event);
tx.start_send(msg).expect("send");
});
window()
.add_event_listener_with_callback(
intern("keydown"),
closure_cb.as_ref().unchecked_ref(),
)
.expect("add event callback");
Cmd::sub(rx, closure_cb)
}

/// do this task at every `ms` interval
pub fn every_interval<F, MSG>(interval_ms: i32, cb: F) -> Cmd<MSG>
where
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub mod prelude {
pub use wasm_bindgen::prelude::*;
pub use serde_wasm_bindgen;
pub use crate::html::events::*;
pub use crate::dom::{Application, SkipDiff, skip_if, events, Program, document, now, window, Window, Dispatch,
pub use crate::dom::{Application, SkipDiff, skip_if, events, Program, document, Document, now, window, Window, Dispatch,
AnimationFrameHandle, Component, StatefulComponent, Effects, Measurements, MountAction,
MountTarget, Cmd, TimeoutCallbackHandle, DomAttrValue,
stateful_component,
Expand Down

0 comments on commit 6e185eb

Please sign in to comment.