web: more scroll to top improvements by reworking URL changes

This commit is contained in:
Bill Thiede 2025-02-25 15:58:24 -08:00
parent 8977f8bab5
commit 4982057500
4 changed files with 25 additions and 16 deletions

8
Cargo.lock generated
View File

@ -910,9 +910,9 @@ dependencies = [
[[package]] [[package]]
name = "console_log" name = "console_log"
version = "0.1.2" version = "0.1.4"
source = "sparse+https://git.z.xinu.tv/api/packages/wathiede/cargo/" source = "sparse+https://git.z.xinu.tv/api/packages/wathiede/cargo/"
checksum = "e628484ff9348e6c256644436f215c0a9766867820da8cf161c567db1c877e32" checksum = "d36495b7586d34322c3ffcff0e0d9d0b70f3a4ce88a9c199b3d8a01afb1debd7"
dependencies = [ dependencies = [
"log", "log",
"wasm-bindgen", "wasm-bindgen",
@ -1620,9 +1620,9 @@ dependencies = [
[[package]] [[package]]
name = "flate2" name = "flate2"
version = "1.0.35" version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07c" checksum = "11faaf5a5236997af9848be0bef4db95824b1d534ebc64d0f0c6cf3e67bd38dc"
dependencies = [ dependencies = [
"crc32fast", "crc32fast",
"miniz_oxide", "miniz_oxide",

View File

@ -18,6 +18,9 @@ fn main() {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
console_error_panic_hook::set_once(); console_error_panic_hook::set_once();
#[cfg(debug_assertions)]
let lvl = Level::Debug;
#[cfg(not(debug_assertions))]
let lvl = Level::Info; let lvl = Level::Info;
console_log::init_with_level(lvl).expect("failed to initialize console logging"); console_log::init_with_level(lvl).expect("failed to initialize console logging");
// Mount the `app` to the element with the `id` "app". // Mount the `app` to the element with the `id` "app".

View File

@ -32,13 +32,13 @@ pub fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
if url.hash().is_none() { if url.hash().is_none() {
orders.request_url(urls::search(unread_query(), 0)); orders.request_url(urls::search(unread_query(), 0));
} else { } else {
orders.notify(subs::UrlRequested::new(url)); orders.request_url(url);
}; };
orders.stream(streams::window_event(Ev::Resize, |_| Msg::OnResize)); orders.stream(streams::window_event(Ev::Resize, |_| Msg::OnResize));
// TODO(wathiede): only do this while viewing the index? Or maybe add a new message that force // TODO(wathiede): only do this while viewing the index? Or maybe add a new message that force
// 'notmuch new' on the server periodically? // 'notmuch new' on the server periodically?
orders.stream(streams::interval(30_000, || Msg::RefreshStart)); orders.stream(streams::interval(30_000, || Msg::RefreshStart));
orders.subscribe(on_url_changed); orders.subscribe(Msg::OnUrlChanged);
orders.stream(streams::window_event(Ev::Scroll, |_| Msg::WindowScrolled)); orders.stream(streams::window_event(Ev::Scroll, |_| Msg::WindowScrolled));
build_info::build_info!(fn bi); build_info::build_info!(fn bi);
@ -54,24 +54,21 @@ pub fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
server: None, server: None,
}, },
catchup: None, catchup: None,
last_url: Url::current(),
} }
} }
fn on_url_changed(uc: subs::UrlChanged) -> Msg { fn on_url_changed(old: &Url, mut new: Url) -> Msg {
let mut url = uc.0; let did_change = *old != new;
let href = document().location().unwrap().href().unwrap();
let origin = document().location().unwrap().origin().unwrap();
let current_url = &href[origin.len()..];
let did_change = current_url != url.to_string();
let mut messages = Vec::new(); let mut messages = Vec::new();
if did_change { if did_change {
messages.push(Msg::ScrollToTop) messages.push(Msg::ScrollToTop)
} }
info!( info!(
"url changed\nold '{current_url}'\nnew '{url}', history {}", "url changed\nold '{old}'\nnew '{new}', history {}",
history().length().unwrap_or(0) history().length().unwrap_or(0)
); );
let hpp = url.remaining_hash_path_parts(); let hpp = new.remaining_hash_path_parts();
let msg = match hpp.as_slice() { let msg = match hpp.as_slice() {
["t", tid] => Msg::ShowThreadRequest { ["t", tid] => Msg::ShowThreadRequest {
thread_id: tid.to_string(), thread_id: tid.to_string(),
@ -142,7 +139,7 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
orders.perform_cmd(async move { Msg::Refresh }); orders.perform_cmd(async move { Msg::Refresh });
} }
Msg::Refresh => { Msg::Refresh => {
orders.perform_cmd(async move { on_url_changed(subs::UrlChanged(Url::current())) }); orders.request_url(Url::current());
} }
Msg::Reload => { Msg::Reload => {
window() window()
@ -150,6 +147,10 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
.reload() .reload()
.expect("failed to reload window"); .expect("failed to reload window");
} }
Msg::OnUrlChanged(new_url) => {
orders.send_msg(on_url_changed(&model.last_url, new_url.0.clone()));
model.last_url = new_url.0;
}
Msg::OnResize => (), Msg::OnResize => (),
Msg::NextPage => { Msg::NextPage => {
@ -676,6 +677,7 @@ pub struct Model {
pub content_el: ElRef<HtmlElement>, pub content_el: ElRef<HtmlElement>,
pub versions: Version, pub versions: Version,
pub catchup: Option<Catchup>, pub catchup: Option<Catchup>,
pub last_url: Url,
} }
#[derive(Debug)] #[derive(Debug)]
@ -741,6 +743,8 @@ pub enum Msg {
Refresh, Refresh,
// Tell the client to reload whole page from server // Tell the client to reload whole page from server
Reload, Reload,
// TODO: add GoToUrl
OnUrlChanged(subs::UrlChanged),
// Window has changed size // Window has changed size
OnResize, OnResize,
// Tell the server to update state // Tell the server to update state

View File

@ -1048,6 +1048,7 @@ fn render_attachements(
] ]
} }
// TODO: add cathup_mode:bool and hide elements when true
#[topo::nested] #[topo::nested]
fn thread( fn thread(
thread: &ShowThreadQueryThreadOnEmailThread, thread: &ShowThreadQueryThreadOnEmailThread,
@ -1348,6 +1349,8 @@ pub fn view_tags(tags: &Option<Vec<Tag>>) -> Node<Msg> {
] ]
] ]
} }
// TODO: add cathup_mode:bool and hide elements when true
fn news_post(post: &ShowThreadQueryThreadOnNewsPost, content_el: &ElRef<HtmlElement>) -> Node<Msg> { fn news_post(post: &ShowThreadQueryThreadOnNewsPost, content_el: &ElRef<HtmlElement>) -> Node<Msg> {
let subject = &post.title; let subject = &post.title;
set_title(subject); set_title(subject);
@ -1540,7 +1543,6 @@ fn reading_progress(ratio: f64) -> Node<Msg> {
] ]
} }
pub fn view_versions(versions: &Version) -> Node<Msg> { pub fn view_versions(versions: &Version) -> Node<Msg> {
debug!("versions {versions:?}");
aside![ aside![
C!["p-2"], C!["p-2"],
p![C!["uppercase", "font-bold"], "Versions"], p![C!["uppercase", "font-bold"], "Versions"],