diff options
author | Justin Wernick <j.wernick@eyeo.com> | 2021-06-23 18:59:41 +0200 |
---|---|---|
committer | Justin Wernick <j.wernick@eyeo.com> | 2021-06-23 18:59:41 +0200 |
commit | 33acfba9785e7e5ddfe4625c00931584815785e0 (patch) | |
tree | 2086d607b940eab57c66368ade6a530abcc91f6b | |
parent | d22c254216f3c33025322b07f0b1aa188bd8a332 (diff) |
Select code block when clicking on it
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/lib.rs | 17 |
3 files changed, 18 insertions, 1 deletions
@@ -439,6 +439,7 @@ dependencies = [ "structform", "syntect", "wasm-bindgen-test", + "web-sys", ] [[package]] @@ -13,6 +13,7 @@ wasm-bindgen-test = "0.3.18" [dependencies] seed = "0.8.0" +web-sys = { version = "0.3.47", features = ["Range"] } syntect = { version = "4.5", default-features = false, features = ["default-fancy"], optional = true} lazy_static = "1.4.0" structform = "0.1.0" @@ -16,6 +16,7 @@ fn init(_: Url, _: &mut impl Orders<Msg>) -> Model { struct Model { form: ShareLinksOptionsForm, links: Vec<Node<Msg>>, + code_area: ElRef<web_sys::Node>, } #[derive(Default, Debug)] @@ -35,6 +36,7 @@ struct ShareLinksOptionsForm { pub enum Msg { Input(ShareLinksOptionsFormField, String), + ClickCode, } fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) { @@ -55,6 +57,15 @@ fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) { } } } + Msg::ClickCode => { + // TODO: Error handling :( This is the most javascript code that ever javascripted. At least fail gracefully with a console error. + let range = browser::util::document().create_range().unwrap(); + let code_area = model.code_area.get().unwrap(); + range.select_node(&code_area).unwrap(); + let selection = browser::util::window().get_selection().unwrap().unwrap(); + selection.remove_all_ranges().unwrap(); + selection.add_range(&range).unwrap(); + } } } @@ -159,7 +170,11 @@ fn view(model: &Model) -> Node<Msg> { div![ C!["results-code"], p!["Copy this code into your site to add your sharing buttons"], - codify(&model.links) + div![ + el_ref(&model.code_area), + ev(Ev::Click, |_| Msg::ClickCode), + codify(&model.links) + ] ], div![ C!["results-demo"], |