summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <j.wernick@eyeo.com>2021-06-23 18:59:41 +0200
committerJustin Wernick <j.wernick@eyeo.com>2021-06-23 18:59:41 +0200
commit33acfba9785e7e5ddfe4625c00931584815785e0 (patch)
tree2086d607b940eab57c66368ade6a530abcc91f6b
parentd22c254216f3c33025322b07f0b1aa188bd8a332 (diff)
Select code block when clicking on it
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml1
-rw-r--r--src/lib.rs17
3 files changed, 18 insertions, 1 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 31b0a67..66249cd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -439,6 +439,7 @@ dependencies = [
"structform",
"syntect",
"wasm-bindgen-test",
+ "web-sys",
]
[[package]]
diff --git a/Cargo.toml b/Cargo.toml
index 995712a..9aa195e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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"
diff --git a/src/lib.rs b/src/lib.rs
index c1d2bd3..684c581 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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"],