diff options
author | Justin Wernick <j.wernick@eyeo.com> | 2021-06-23 16:41:06 +0200 |
---|---|---|
committer | Justin Wernick <j.wernick@eyeo.com> | 2021-06-23 16:41:06 +0200 |
commit | d22c254216f3c33025322b07f0b1aa188bd8a332 (patch) | |
tree | b3faa479f99cb40f1ca95762188d24bd25217d6c | |
parent | 3bc808f306be580c8463652056ea0c493bbf6f7b (diff) |
Add twitter support
-rw-r--r-- | src/facebook.rs | 5 | ||||
-rw-r--r-- | src/form.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 27 | ||||
-rw-r--r-- | src/twitter.rs | 40 |
4 files changed, 67 insertions, 7 deletions
diff --git a/src/facebook.rs b/src/facebook.rs index 0adf521..0f86382 100644 --- a/src/facebook.rs +++ b/src/facebook.rs @@ -28,12 +28,9 @@ pub fn link(url: &str) -> Node<Msg> { St::TextDecoration => "none", St::Background => FB_BLUE, St::Color => "white", - St::FontFamily => "Helvetica, Arial, sans-serif", - St::FontSize => "11px", - St::FontWeight => "bold", + St::Font => "bold 11px Helvetica, Arial, sans-serif", St::Padding => "4px 7px", St::BorderRadius => "3px", - St::Height => "20px", }, icon("white"), " ", diff --git a/src/form.rs b/src/form.rs index c5ffe3e..b1e023c 100644 --- a/src/form.rs +++ b/src/form.rs @@ -1,4 +1,3 @@ -use seed::log; use structform::{derive_form_input, impl_text_input_with_stringops, ParseAndFormat, ParseError}; derive_form_input! {FormTextInput} @@ -9,7 +8,6 @@ derive_form_input! {FormCheckboxInput} impl ParseAndFormat<bool> for FormCheckboxInput<bool> { fn parse(value: &str) -> Result<bool, ParseError> { - log!(value); Ok(value == "true") } fn format(value: &bool) -> String { @@ -6,6 +6,7 @@ use structform::StructForm; mod facebook; mod form; +mod twitter; fn init(_: Url, _: &mut impl Orders<Msg>) -> Model { Model::default() @@ -21,6 +22,7 @@ struct Model { struct ShareLinksOptions { url: String, facebook: bool, + twitter: bool, } #[derive(Default, StructForm)] @@ -28,6 +30,7 @@ struct ShareLinksOptions { struct ShareLinksOptionsForm { url: FormTextInput<String>, facebook: FormCheckboxInput<bool>, + twitter: FormCheckboxInput<bool>, } pub enum Msg { @@ -44,7 +47,10 @@ fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) { model.links = Vec::new(); } Ok(options) => { - let option_links = vec![options.facebook.then(|| facebook::link(&options.url))]; + let option_links = vec![ + options.facebook.then(|| facebook::link(&options.url)), + options.twitter.then(|| twitter::link(&options.url)), + ]; model.links = option_links.into_iter().flatten().collect() } } @@ -127,6 +133,25 @@ fn view(model: &Model) -> Node<Msg> { input_ev(Ev::Input, move |_| Msg::Input(ShareLinksOptionsFormField::Facebook, (!is_checked).to_string())) ] }, + ], + div![ + label![ + attrs! { + At::For => "twitter", + }, + "Twitter" + ], + { + let is_checked = model.form.twitter.input == "true"; + input![ + attrs!{ + At::Name => "twitter", + At::Type => "checkbox", + At::Checked => is_checked.as_at_value(), + }, + input_ev(Ev::Input, move |_| Msg::Input(ShareLinksOptionsFormField::Twitter, (!is_checked).to_string())) + ] + }, ] ], (!model.links.is_empty()).then(|| nodes![ diff --git a/src/twitter.rs b/src/twitter.rs new file mode 100644 index 0000000..d6c37af --- /dev/null +++ b/src/twitter.rs @@ -0,0 +1,40 @@ +use crate::Msg; +use seed::{prelude::*, *}; + +const TWIT_BLUE: &'static str = "#1da1f2"; + +pub fn icon(color: &str) -> Node<Msg> { + let mut result = Node::from_html( + r##"<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!-- Font Awesome Free 5.15.3 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) --><path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"/></svg>"##, + ).into_iter().next().unwrap(); + result.add_style("height", "11px"); + result.add_style("fill", color); + result +} + +pub fn link(url: &str) -> Node<Msg> { + let twitter_url = format!( + "https://twitter.com/intent/tweet?url={}", + Url::encode_uri_component(url) + ); + let twitter_link = a![ + attrs! { + At::Href => twitter_url, + At::Target => "_blank", + At::Rel => "nofollow noopener noreferrer" + }, + style! { + St::Display => "inline-block", + St::TextDecoration => "none", + St::Background => TWIT_BLUE, + St::Color => "white", + St::Font => "bold 11px Helvetica, Arial, sans-serif", + St::Padding => "4px 7px", + St::BorderRadius => "3px", + }, + icon("white"), + " ", + span!["Tweet"] + ]; + twitter_link +} |