web: scroll to top on page changes

This commit is contained in:
2025-02-24 18:39:47 -08:00
parent 45e4edb1dd
commit 77943b3570
2 changed files with 23 additions and 14 deletions

View File

@@ -59,13 +59,20 @@ pub fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
fn on_url_changed(uc: subs::UrlChanged) -> Msg {
let mut url = uc.0;
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();
if did_change {
messages.push(Msg::ScrollToTop)
}
info!(
"url changed '{}', history {}",
url,
"url changed\nold '{current_url}'\nnew '{url}', history {}",
history().length().unwrap_or(0)
);
let hpp = url.remaining_hash_path_parts();
match hpp.as_slice() {
let msg = match hpp.as_slice() {
["t", tid] => Msg::ShowThreadRequest {
thread_id: tid.to_string(),
},
@@ -102,7 +109,9 @@ fn on_url_changed(uc: subs::UrlChanged) -> Msg {
last: None,
}
}
}
};
messages.push(msg);
Msg::MultiMsg(messages)
}
// `update` describes how to handle each `Msg`.
@@ -536,6 +545,10 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
.expect("failed to copy to clipboard");
});
}
Msg::ScrollToTop => {
info!("scrolling to the top");
web_sys::window().unwrap().scroll_to_with_x_and_y(0., 0.);
}
Msg::WindowScrolled => {
if let Some(el) = model.content_el.get() {
let ih = window()
@@ -608,6 +621,7 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
orders.send_msg(Msg::CatchupNext);
}
Msg::CatchupNext => {
orders.send_msg(Msg::ScrollToTop);
let Some(catchup) = &mut model.catchup else {
orders.send_msg(Msg::GoToSearchResults);
return;
@@ -777,6 +791,7 @@ pub enum Msg {
CopyToClipboard(String),
ScrollToTop,
WindowScrolled,
SetProgress(f64),
UpdateServerVersion(String),