mod.rs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. use scroll::ScrollPosition;
  2. use wasm_bindgen::JsCast;
  3. use wasm_bindgen::{prelude::Closure, JsValue};
  4. use web_sys::{window, Window};
  5. use web_sys::{Event, History, ScrollRestoration};
  6. mod scroll;
  7. fn base_path() -> Option<String> {
  8. let base_path = dioxus_cli_config::web_base_path();
  9. tracing::trace!("Using base_path from the CLI: {:?}", base_path);
  10. base_path
  11. }
  12. #[allow(clippy::extra_unused_type_parameters)]
  13. fn update_scroll(window: &Window, history: &History) {
  14. let scroll = ScrollPosition::of_window(window);
  15. if let Err(err) = replace_state_with_url(history, &[scroll.x, scroll.y], None) {
  16. web_sys::console::error_1(&err);
  17. }
  18. }
  19. /// A [`HistoryProvider`] that integrates with a browser via the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API).
  20. ///
  21. /// # Prefix
  22. /// This [`HistoryProvider`] supports a prefix, which can be used for web apps that aren't located
  23. /// at the root of their domain.
  24. ///
  25. /// Application developers are responsible for ensuring that right after the prefix comes a `/`. If
  26. /// that is not the case, this [`HistoryProvider`] will replace the first character after the prefix
  27. /// with one.
  28. ///
  29. /// Application developers are responsible for not rendering the router if the prefix is not present
  30. /// in the URL. Otherwise, if a router navigation is triggered, the prefix will be added.
  31. pub struct WebHistory {
  32. do_scroll_restoration: bool,
  33. history: History,
  34. prefix: Option<String>,
  35. window: Window,
  36. }
  37. impl Default for WebHistory {
  38. fn default() -> Self {
  39. Self::new(None, true)
  40. }
  41. }
  42. impl WebHistory {
  43. /// Create a new [`WebHistory`].
  44. ///
  45. /// If `do_scroll_restoration` is [`true`], [`WebHistory`] will take control of the history
  46. /// state. It'll also set the browsers scroll restoration to `manual`.
  47. pub fn new(prefix: Option<String>, do_scroll_restoration: bool) -> Self {
  48. let myself = Self::new_inner(prefix, do_scroll_restoration);
  49. let current_route = dioxus_history::History::current_route(&myself);
  50. let current_route_str = current_route.to_string();
  51. let prefix_str = myself.prefix.as_deref().unwrap_or("");
  52. let current_url = format!("{prefix_str}{current_route_str}");
  53. let state = myself.create_state();
  54. let _ = replace_state_with_url(&myself.history, &state, Some(&current_url));
  55. myself
  56. }
  57. fn new_inner(prefix: Option<String>, do_scroll_restoration: bool) -> Self {
  58. let window = window().expect("access to `window`");
  59. let history = window.history().expect("`window` has access to `history`");
  60. if do_scroll_restoration {
  61. history
  62. .set_scroll_restoration(ScrollRestoration::Manual)
  63. .expect("`history` can set scroll restoration");
  64. }
  65. let prefix = prefix
  66. // If there isn't a base path, try to grab one from the CLI
  67. .or_else(base_path)
  68. // Normalize the prefix to start and end with no slashes
  69. .as_ref()
  70. .map(|prefix| prefix.trim_matches('/'))
  71. // If the prefix is empty, don't add it
  72. .filter(|prefix| !prefix.is_empty())
  73. // Otherwise, start with a slash
  74. .map(|prefix| format!("/{prefix}"));
  75. Self {
  76. do_scroll_restoration,
  77. history,
  78. prefix,
  79. window,
  80. }
  81. }
  82. fn scroll_pos(&self) -> ScrollPosition {
  83. self.do_scroll_restoration
  84. .then(|| ScrollPosition::of_window(&self.window))
  85. .unwrap_or_default()
  86. }
  87. fn create_state(&self) -> [f64; 2] {
  88. let scroll = self.scroll_pos();
  89. [scroll.x, scroll.y]
  90. }
  91. }
  92. impl WebHistory {
  93. fn route_from_location(&self) -> String {
  94. let location = self.window.location();
  95. let path = location.pathname().unwrap_or_else(|_| "/".into())
  96. + &location.search().unwrap_or("".into())
  97. + &location.hash().unwrap_or("".into());
  98. let mut path = match self.prefix {
  99. None => &path,
  100. Some(ref prefix) => path.strip_prefix(prefix).unwrap_or(prefix),
  101. };
  102. // If the path is empty, parse the root route instead
  103. if path.is_empty() {
  104. path = "/"
  105. }
  106. path.to_string()
  107. }
  108. fn full_path(&self, state: &String) -> String {
  109. match &self.prefix {
  110. None => state.to_string(),
  111. Some(prefix) => format!("{prefix}{state}"),
  112. }
  113. }
  114. fn handle_nav(&self, result: Result<(), JsValue>) {
  115. match result {
  116. Ok(_) => {
  117. if self.do_scroll_restoration {
  118. self.window.scroll_to_with_x_and_y(0.0, 0.0)
  119. }
  120. }
  121. Err(e) => {
  122. web_sys::console::error_2(&JsValue::from_str("failed to change state: "), &e);
  123. }
  124. }
  125. }
  126. fn navigate_external(&self, url: String) -> bool {
  127. match self.window.location().set_href(&url) {
  128. Ok(_) => true,
  129. Err(e) => {
  130. web_sys::console::error_4(
  131. &JsValue::from_str("failed to navigate to external url ("),
  132. &JsValue::from_str(&url),
  133. &JsValue::from_str("): "),
  134. &e,
  135. );
  136. false
  137. }
  138. }
  139. }
  140. }
  141. impl dioxus_history::History for WebHistory {
  142. fn current_route(&self) -> String {
  143. self.route_from_location()
  144. }
  145. fn current_prefix(&self) -> Option<String> {
  146. self.prefix.clone()
  147. }
  148. fn go_back(&self) {
  149. if let Err(e) = self.history.back() {
  150. web_sys::console::error_2(&JsValue::from_str("failed to go back: "), &e);
  151. }
  152. }
  153. fn go_forward(&self) {
  154. if let Err(e) = self.history.forward() {
  155. web_sys::console::error_2(&JsValue::from_str("failed to go forward: "), &e);
  156. }
  157. }
  158. fn push(&self, state: String) {
  159. if state == self.current_route() {
  160. // don't push the same state twice
  161. return;
  162. }
  163. let w = window().expect("access to `window`");
  164. let h = w.history().expect("`window` has access to `history`");
  165. // update the scroll position before pushing the new state
  166. update_scroll(&w, &h);
  167. let path = self.full_path(&state);
  168. let state: [f64; 2] = self.create_state();
  169. self.handle_nav(push_state_and_url(&self.history, &state, path));
  170. }
  171. fn replace(&self, state: String) {
  172. let path = self.full_path(&state);
  173. let state = self.create_state();
  174. self.handle_nav(replace_state_with_url(&self.history, &state, Some(&path)));
  175. }
  176. fn external(&self, url: String) -> bool {
  177. self.navigate_external(url)
  178. }
  179. fn updater(&self, callback: std::sync::Arc<dyn Fn() + Send + Sync>) {
  180. let w = self.window.clone();
  181. let h = self.history.clone();
  182. let d = self.do_scroll_restoration;
  183. let function = Closure::wrap(Box::new(move |_| {
  184. (*callback)();
  185. if d {
  186. if let Some([x, y]) = get_current(&h) {
  187. ScrollPosition { x, y }.scroll_to(w.clone())
  188. }
  189. }
  190. }) as Box<dyn FnMut(Event)>);
  191. self.window
  192. .add_event_listener_with_callback(
  193. "popstate",
  194. &function.into_js_value().unchecked_into(),
  195. )
  196. .unwrap();
  197. }
  198. }
  199. pub(crate) fn replace_state_with_url(
  200. history: &History,
  201. value: &[f64; 2],
  202. url: Option<&str>,
  203. ) -> Result<(), JsValue> {
  204. let position = js_sys::Array::new();
  205. position.push(&JsValue::from(value[0]));
  206. position.push(&JsValue::from(value[1]));
  207. history.replace_state_with_url(&position, "", url)
  208. }
  209. pub(crate) fn push_state_and_url(
  210. history: &History,
  211. value: &[f64; 2],
  212. url: String,
  213. ) -> Result<(), JsValue> {
  214. let position = js_sys::Array::new();
  215. position.push(&JsValue::from(value[0]));
  216. position.push(&JsValue::from(value[1]));
  217. history.push_state_with_url(&position, "", Some(&url))
  218. }
  219. pub(crate) fn get_current(history: &History) -> Option<[f64; 2]> {
  220. use wasm_bindgen::JsCast;
  221. let state = history.state();
  222. if let Err(err) = &state {
  223. web_sys::console::error_1(err);
  224. }
  225. state.ok().and_then(|state| {
  226. let state = state.dyn_into::<js_sys::Array>().ok()?;
  227. let x = state.get(0).as_f64()?;
  228. let y = state.get(1).as_f64()?;
  229. Some([x, y])
  230. })
  231. }