-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Document with its methods such as selection, add more metho…
…ds to Window
- Loading branch information
Showing
4 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters