From b9ddeaadc19647aefbe822e12b8ed0d6b3820a3c Mon Sep 17 00:00:00 2001 From: Heiss Date: Thu, 2 Mar 2023 21:06:08 +0100 Subject: [PATCH 1/9] fix xtask for windows --- xtask/src/main.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 8a40070..851022e 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -35,11 +35,20 @@ fn main() -> anyhow::Result<()> { fn build() -> anyhow::Result<()> { cmd!("wasm-pack build --no-typescript --target web").run()?; fs::copy("pkg/caniuse_rs_bg.wasm", "public/caniuse_rs.wasm")?; + #[cfg(target_os = "linux")] cmd!("rollup src/main.js --format iife --file public/caniuse_rs.js").run()?; + #[cfg(target_os = "windows")] + cmd!("rollup.cmd src/main.js --format iife --file public/caniuse_rs.js").run()?; + let static_files: Vec<_> = fs::read_dir("static")?.map(|entry| Ok(entry?.path())).collect::>()?; + #[cfg(target_os = "linux")] cmd!("cp -r {static_files...} public/").run()?; + #[cfg(target_os = "windows")] + for file in static_files.iter() { + cmd!("xcopy {file} public /i /s /y /q").run()?; + } Ok(()) } From e1ab899459be4b11d6bf6e6ffca2d36dbd51a5ce Mon Sep 17 00:00:00 2001 From: Heiss Date: Thu, 2 Mar 2023 21:06:21 +0100 Subject: [PATCH 2/9] implement search querystring --- src/components/app.rs | 19 +++++++++++++++++-- src/components/header.rs | 15 ++++++++++----- src/lib.rs | 2 ++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/components/app.rs b/src/components/app.rs index a061d6b..08cc03c 100644 --- a/src/components/app.rs +++ b/src/components/app.rs @@ -40,7 +40,19 @@ impl Component for App { } }); - Self { input_ref: NodeRef::default(), search_query: Rc::new(String::new()), _key_listener } + let search_query = Rc::new( + web_sys::window() + .expect("cannot access window object") + .location() + .pathname() + .expect("unable to get location") + .split("search/") + .skip(1) + .next() + .unwrap_or(String::new().as_str()) + .to_string(), + ); + Self { input_ref: NodeRef::default(), search_query, _key_listener } } fn update(&mut self, _: &Context, msg: Msg) -> bool { @@ -65,6 +77,9 @@ impl Component for App { let search_query = self.search_query.clone(); let render_route = Switch::render(move |route| match &route { + AppRoute::SearchIndex { query: slug } => { + html! { } + } AppRoute::Index | AppRoute::RecentlyStabilized | AppRoute::Unstable => { let show = if search_query.is_empty() { IndexContents::Explore(match &route { @@ -98,7 +113,7 @@ impl Component for App { html! { -
+
diff --git a/src/components/header.rs b/src/components/header.rs index 950bd06..54a56bb 100644 --- a/src/components/header.rs +++ b/src/components/header.rs @@ -3,8 +3,8 @@ use std::mem; use gloo_events::EventListener; use gloo_utils::{body, document_element, window}; use wasm_bindgen::JsCast; -use web_sys::{HtmlElement, InputEvent, MouseEvent}; -use yew::{html, Callback, Component, Context, Html, NodeRef, Properties}; +use web_sys::{HtmlElement, HtmlInputElement, InputEvent, MouseEvent}; +use yew::{html, Callback, Component, Context, Html, NodeRef, Properties, TargetCast}; use yew_router::{ history::{History, Location}, scope_ext::RouterScopeExt, @@ -31,6 +31,7 @@ pub struct Props { #[prop_or_default] pub input_ref: NodeRef, pub oninput: Callback, + pub inputval: String, } impl Component for Header { @@ -122,9 +123,12 @@ impl Component for Header { let history = ctx.link().history().unwrap(); let cb = ctx.props().oninput.clone(); - let oninput = move |ev| { - if history.location().route::() != Some(AppRoute::Index) { - history.push(AppRoute::Index); + let oninput = move |ev: InputEvent| { + let search_query = ev.target_unchecked_into::().value(); + if !search_query.is_empty() { + history.replace(AppRoute::SearchIndex { query: search_query }); + } else { + history.push(AppRoute::Index) } cb.emit(ev); }; @@ -135,6 +139,7 @@ impl Component for Header {
{"?"}
diff --git a/src/lib.rs b/src/lib.rs index 9c242bc..e00f803 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,8 @@ enum AppRoute { Unstable, #[at("/")] Index, + #[at("/search/:query")] + SearchIndex { query: String }, } type RouterLink = yew_router::components::Link; From 0861fe248c9dfb34db70d8aab01e3fbda7e266c3 Mon Sep 17 00:00:00 2001 From: Heiss Date: Thu, 2 Mar 2023 21:20:03 +0100 Subject: [PATCH 3/9] add urlencoding for searchquery --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/components/app.rs | 26 ++++++++++++++------------ src/components/header.rs | 8 +++----- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7e8ec68..1195898 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,6 +92,7 @@ dependencies = [ "serde_json", "tera", "toml", + "urlencoding", "wasm-bindgen", "web-sys", "yew", @@ -1137,6 +1138,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" +[[package]] +name = "urlencoding" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8db7427f936968176eaa7cdf81b7f98b980b18495ec28f1b5791ac3bfe3eea9" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 484498c..5fcb990 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ once_cell = "1.4.0" wasm-bindgen = "0.2.64" yew = "0.19.3" yew-router = "0.16.0" +urlencoding = "2.1.2" [dependencies.web-sys] version = "0.3.36" diff --git a/src/components/app.rs b/src/components/app.rs index 08cc03c..b273e8e 100644 --- a/src/components/app.rs +++ b/src/components/app.rs @@ -2,6 +2,7 @@ use std::rc::Rc; use gloo_events::EventListener; use gloo_utils::document; +use urlencoding::decode; use wasm_bindgen::JsCast; use web_sys::{HtmlInputElement, KeyboardEvent}; use yew::{html, Component, Context, Html, NodeRef}; @@ -40,18 +41,19 @@ impl Component for App { } }); - let search_query = Rc::new( - web_sys::window() - .expect("cannot access window object") - .location() - .pathname() - .expect("unable to get location") - .split("search/") - .skip(1) - .next() - .unwrap_or(String::new().as_str()) - .to_string(), - ); + let search_query = web_sys::window() + .expect("cannot access window object") + .location() + .pathname() + .expect("unable to get location") + .split("/search/") + .skip(1) + .next() + .unwrap_or(String::new().as_str()) + .to_string(); + let search_query = + Rc::new(decode(&search_query).expect("Cannot decode search_query").to_string()); + Self { input_ref: NodeRef::default(), search_query, _key_listener } } diff --git a/src/components/header.rs b/src/components/header.rs index 54a56bb..1c7cef8 100644 --- a/src/components/header.rs +++ b/src/components/header.rs @@ -2,13 +2,11 @@ use std::mem; use gloo_events::EventListener; use gloo_utils::{body, document_element, window}; +use urlencoding::encode; use wasm_bindgen::JsCast; use web_sys::{HtmlElement, HtmlInputElement, InputEvent, MouseEvent}; use yew::{html, Callback, Component, Context, Html, NodeRef, Properties, TargetCast}; -use yew_router::{ - history::{History, Location}, - scope_ext::RouterScopeExt, -}; +use yew_router::{history::History, scope_ext::RouterScopeExt}; use crate::{ icons::{fa_bars, fa_moon, fa_question_circle, fa_sun}, @@ -126,7 +124,7 @@ impl Component for Header { let oninput = move |ev: InputEvent| { let search_query = ev.target_unchecked_into::().value(); if !search_query.is_empty() { - history.replace(AppRoute::SearchIndex { query: search_query }); + history.replace(AppRoute::SearchIndex { query: encode(&search_query).to_string() }); } else { history.push(AppRoute::Index) } From 268fb2203569b1b8d5d3e28d227d2c63dedfcad2 Mon Sep 17 00:00:00 2001 From: Peter Heiss Date: Fri, 3 Mar 2023 14:19:49 +0100 Subject: [PATCH 4/9] use percent-encoding instead of urlencode --- Cargo.lock | 8 +------- Cargo.toml | 2 +- src/components/app.rs | 10 +++++++--- src/components/header.rs | 6 ++++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1195898..7ad6495 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,13 +86,13 @@ dependencies = [ "gloo-timers", "gloo-utils", "once_cell", + "percent-encoding", "proc-macro2", "quote", "serde", "serde_json", "tera", "toml", - "urlencoding", "wasm-bindgen", "web-sys", "yew", @@ -1138,12 +1138,6 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" -[[package]] -name = "urlencoding" -version = "2.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8db7427f936968176eaa7cdf81b7f98b980b18495ec28f1b5791ac3bfe3eea9" - [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 5fcb990..2f91117 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,10 +15,10 @@ gloo-events = "0.1.2" gloo-timers = "0.2.4" gloo-utils = "0.1.2" once_cell = "1.4.0" +percent-encoding = "2.2" wasm-bindgen = "0.2.64" yew = "0.19.3" yew-router = "0.16.0" -urlencoding = "2.1.2" [dependencies.web-sys] version = "0.3.36" diff --git a/src/components/app.rs b/src/components/app.rs index b273e8e..3acd376 100644 --- a/src/components/app.rs +++ b/src/components/app.rs @@ -2,7 +2,7 @@ use std::rc::Rc; use gloo_events::EventListener; use gloo_utils::document; -use urlencoding::decode; +use percent_encoding::percent_decode; use wasm_bindgen::JsCast; use web_sys::{HtmlInputElement, KeyboardEvent}; use yew::{html, Component, Context, Html, NodeRef}; @@ -51,8 +51,12 @@ impl Component for App { .next() .unwrap_or(String::new().as_str()) .to_string(); - let search_query = - Rc::new(decode(&search_query).expect("Cannot decode search_query").to_string()); + let search_query = Rc::new( + percent_decode(search_query.as_bytes()) + .decode_utf8() + .expect("Cannot decode search_query") + .to_string(), + ); Self { input_ref: NodeRef::default(), search_query, _key_listener } } diff --git a/src/components/header.rs b/src/components/header.rs index 1c7cef8..fa1128c 100644 --- a/src/components/header.rs +++ b/src/components/header.rs @@ -2,7 +2,7 @@ use std::mem; use gloo_events::EventListener; use gloo_utils::{body, document_element, window}; -use urlencoding::encode; +use percent_encoding::{percent_encode, NON_ALPHANUMERIC}; use wasm_bindgen::JsCast; use web_sys::{HtmlElement, HtmlInputElement, InputEvent, MouseEvent}; use yew::{html, Callback, Component, Context, Html, NodeRef, Properties, TargetCast}; @@ -124,7 +124,9 @@ impl Component for Header { let oninput = move |ev: InputEvent| { let search_query = ev.target_unchecked_into::().value(); if !search_query.is_empty() { - history.replace(AppRoute::SearchIndex { query: encode(&search_query).to_string() }); + history.replace(AppRoute::SearchIndex { + query: percent_encode(search_query.as_bytes(), NON_ALPHANUMERIC).to_string(), + }); } else { history.push(AppRoute::Index) } From c978267692a64a39104abcf26a284a42cec82665 Mon Sep 17 00:00:00 2001 From: Peter Heiss Date: Fri, 3 Mar 2023 14:38:08 +0100 Subject: [PATCH 5/9] implement querystring with hash --- src/components/app.rs | 10 +++++----- src/lib.rs | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/app.rs b/src/components/app.rs index 3acd376..63a72a8 100644 --- a/src/components/app.rs +++ b/src/components/app.rs @@ -44,13 +44,13 @@ impl Component for App { let search_query = web_sys::window() .expect("cannot access window object") .location() - .pathname() + .hash() .expect("unable to get location") - .split("/search/") - .skip(1) - .next() - .unwrap_or(String::new().as_str()) + .split("#q=") + .nth(1) + .unwrap_or("") .to_string(); + let search_query = Rc::new( percent_decode(search_query.as_bytes()) .decode_utf8() diff --git a/src/lib.rs b/src/lib.rs index e00f803..bb525ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ enum AppRoute { Unstable, #[at("/")] Index, - #[at("/search/:query")] + #[at("/#q=:query")] SearchIndex { query: String }, } From cf1bb7c4bf7fc97a00a692878624fce3618abb78 Mon Sep 17 00:00:00 2001 From: Peter Heiss Date: Fri, 3 Mar 2023 14:57:09 +0100 Subject: [PATCH 6/9] remove query route --- src/components/app.rs | 3 --- src/components/header.rs | 16 +++++----------- src/lib.rs | 2 -- 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/src/components/app.rs b/src/components/app.rs index 63a72a8..014f1ea 100644 --- a/src/components/app.rs +++ b/src/components/app.rs @@ -83,9 +83,6 @@ impl Component for App { let search_query = self.search_query.clone(); let render_route = Switch::render(move |route| match &route { - AppRoute::SearchIndex { query: slug } => { - html! { } - } AppRoute::Index | AppRoute::RecentlyStabilized | AppRoute::Unstable => { let show = if search_query.is_empty() { IndexContents::Explore(match &route { diff --git a/src/components/header.rs b/src/components/header.rs index fa1128c..18f1454 100644 --- a/src/components/header.rs +++ b/src/components/header.rs @@ -2,11 +2,10 @@ use std::mem; use gloo_events::EventListener; use gloo_utils::{body, document_element, window}; -use percent_encoding::{percent_encode, NON_ALPHANUMERIC}; use wasm_bindgen::JsCast; -use web_sys::{HtmlElement, HtmlInputElement, InputEvent, MouseEvent}; -use yew::{html, Callback, Component, Context, Html, NodeRef, Properties, TargetCast}; -use yew_router::{history::History, scope_ext::RouterScopeExt}; +use web_sys::{HtmlElement, InputEvent, MouseEvent}; +use yew::{html, Callback, Component, Context, Html, NodeRef, Properties}; +use yew_router::{history::History, scope_ext::RouterScopeExt, prelude::Location}; use crate::{ icons::{fa_bars, fa_moon, fa_question_circle, fa_sun}, @@ -122,13 +121,8 @@ impl Component for Header { let history = ctx.link().history().unwrap(); let cb = ctx.props().oninput.clone(); let oninput = move |ev: InputEvent| { - let search_query = ev.target_unchecked_into::().value(); - if !search_query.is_empty() { - history.replace(AppRoute::SearchIndex { - query: percent_encode(search_query.as_bytes(), NON_ALPHANUMERIC).to_string(), - }); - } else { - history.push(AppRoute::Index) + if history.location().route::() != Some(AppRoute::Index) { + history.push(AppRoute::Index); } cb.emit(ev); }; diff --git a/src/lib.rs b/src/lib.rs index bb525ac..9c242bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,8 +39,6 @@ enum AppRoute { Unstable, #[at("/")] Index, - #[at("/#q=:query")] - SearchIndex { query: String }, } type RouterLink = yew_router::components::Link; From ddb8a16fd78228749f3153bc7dc5ba5d4149f430 Mon Sep 17 00:00:00 2001 From: Peter Heiss Date: Fri, 3 Mar 2023 16:21:06 +0100 Subject: [PATCH 7/9] attempt to implement better querystring --- src/components/app.rs | 21 +++------------------ src/components/header.rs | 6 +++--- src/util.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/components/app.rs b/src/components/app.rs index 014f1ea..dff5c26 100644 --- a/src/components/app.rs +++ b/src/components/app.rs @@ -2,7 +2,6 @@ use std::rc::Rc; use gloo_events::EventListener; use gloo_utils::document; -use percent_encoding::percent_decode; use wasm_bindgen::JsCast; use web_sys::{HtmlInputElement, KeyboardEvent}; use yew::{html, Component, Context, Html, NodeRef}; @@ -13,6 +12,7 @@ use crate::{ index::{Explore, IndexContents}, About, FeaturePage, Header, Index, VersionPage, }, + util::{get_searchquery, set_searchquery}, AppRoute, FEATURES, VERSIONS, }; @@ -41,22 +41,7 @@ impl Component for App { } }); - let search_query = web_sys::window() - .expect("cannot access window object") - .location() - .hash() - .expect("unable to get location") - .split("#q=") - .nth(1) - .unwrap_or("") - .to_string(); - - let search_query = Rc::new( - percent_decode(search_query.as_bytes()) - .decode_utf8() - .expect("Cannot decode search_query") - .to_string(), - ); + let search_query = Rc::new(get_searchquery()); Self { input_ref: NodeRef::default(), search_query, _key_listener } } @@ -116,7 +101,7 @@ impl Component for App { html! { -
+
diff --git a/src/components/header.rs b/src/components/header.rs index 18f1454..66cce1b 100644 --- a/src/components/header.rs +++ b/src/components/header.rs @@ -1,4 +1,4 @@ -use std::mem; +use std::{mem, rc::Rc}; use gloo_events::EventListener; use gloo_utils::{body, document_element, window}; @@ -28,7 +28,7 @@ pub struct Props { #[prop_or_default] pub input_ref: NodeRef, pub oninput: Callback, - pub inputval: String, + pub input_val: String } impl Component for Header { @@ -133,7 +133,7 @@ impl Component for Header {
{"?"}
diff --git a/src/util.rs b/src/util.rs index f34caf8..a779c6d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,6 @@ use std::fmt::Display; +use percent_encoding::{percent_decode, NON_ALPHANUMERIC, percent_encode, utf8_percent_encode}; use yew::{ html, virtual_dom::{VList, VNode, VTag, VText}, @@ -65,3 +66,28 @@ pub fn maybe_link(text: &str, link_base: &str, opt_rest: Option) None => html! {}, } } + +pub fn get_searchquery() -> String { + let search_query = web_sys::window() + .expect("cannot access window object") + .location() + .hash() + .expect("unable to get location") + .split("#q=") + .nth(1) + .unwrap_or("") + .to_string(); + + percent_decode(search_query.as_bytes()) + .decode_utf8() + .expect("Cannot decode search_query") + .to_string() +} + +pub fn set_searchquery(search_query: &str) -> bool { + web_sys::window() + .expect("cannot access window object") + .location() + .replace(&format!("/#q={}", utf8_percent_encode(search_query, NON_ALPHANUMERIC).to_string())) + .is_ok() +} \ No newline at end of file From dd5e16bd00c5bd8a1ed3b39e3859e265b80e4434 Mon Sep 17 00:00:00 2001 From: Heiss Date: Sat, 4 Mar 2023 13:51:45 +0100 Subject: [PATCH 8/9] remove hash, if search_query found --- src/components/app.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/components/app.rs b/src/components/app.rs index dff5c26..ef2c27d 100644 --- a/src/components/app.rs +++ b/src/components/app.rs @@ -12,7 +12,7 @@ use crate::{ index::{Explore, IndexContents}, About, FeaturePage, Header, Index, VersionPage, }, - util::{get_searchquery, set_searchquery}, + util::get_searchquery, AppRoute, FEATURES, VERSIONS, }; @@ -43,6 +43,14 @@ impl Component for App { let search_query = Rc::new(get_searchquery()); + if !search_query.is_empty() { + web_sys::window() + .expect("cannot access window object") + .location() + .set_hash("") + .expect("cannot set location"); + } + Self { input_ref: NodeRef::default(), search_query, _key_listener } } From 2d7ab7f45de87edce0f0dd44fdeddc0e7eb6274f Mon Sep 17 00:00:00 2001 From: Heiss Date: Sat, 4 Mar 2023 14:42:35 +0100 Subject: [PATCH 9/9] add text to aboutpage for query --- src/components/about.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/components/about.rs b/src/components/about.rs index d64ad6a..4137df2 100644 --- a/src/components/about.rs +++ b/src/components/about.rs @@ -34,6 +34,11 @@ impl Component for About { {"You can find the code for this site on "} {"GitHub"}{"."}

+

{"Features"}

+

+ {"You can add the page to your browser searchbar. The syntax is: "} + {"https://caniuse.rs/#q=%s"} +

{"About the creator"}

{"I'm Jonas and I work on free software in my spare time, usually on "}