wip
This commit is contained in:
		
							parent
							
								
									d8ff878491
								
							
						
					
					
						commit
						e855b45f95
					
				
					 4 changed files with 50 additions and 22 deletions
				
			
		
							
								
								
									
										12
									
								
								src/app.rs
									
										
									
									
									
								
							
							
						
						
									
										12
									
								
								src/app.rs
									
										
									
									
									
								
							|  | @ -29,15 +29,13 @@ impl Component for App { | |||
| 
 | ||||
|                 <hr/> | ||||
| 
 | ||||
|                 <p> | ||||
|                     {"Please note, this has only been tested on GPX files produced by "} | ||||
|                     <a href="https://www.garmin.com">{"Garmin"}</a>{" and "} | ||||
|                     <a href="https://www.strava.com">{"Strava"}</a>{" - your mileage may vary."} | ||||
|                 </p> | ||||
|                 <p> | ||||
|                     <small>{"source (public access): git://pingo.thermokar.st/gpx-web-utils"}</small> | ||||
|                     <small> | ||||
|                         {"Please note, this has only been tested on GPX files produced by "} | ||||
|                         <a href="https://www.garmin.com">{"Garmin"}</a> | ||||
|                         {" and "} | ||||
|                         <a href="https://www.strava.com">{"Strava"}</a> | ||||
|                         {" - your mileage may vary."} | ||||
|                     </small> | ||||
|                 </p> | ||||
|             </> | ||||
|         } | ||||
|  |  | |||
							
								
								
									
										25
									
								
								src/gpx.rs
									
										
									
									
									
								
							
							
						
						
									
										25
									
								
								src/gpx.rs
									
										
									
									
									
								
							|  | @ -1,6 +1,6 @@ | |||
| use std::error::Error; | ||||
| use wasm_bindgen::prelude::*; | ||||
| use web_sys::Blob; | ||||
| use web_sys::{Blob, MouseEvent, Url}; | ||||
| 
 | ||||
| fn join_gpx_files(files: &[String]) -> Result<gpx::Gpx, Box<dyn Error>> { | ||||
|     let mut merged_gpx: gpx::Gpx = Default::default(); | ||||
|  | @ -59,6 +59,29 @@ pub fn merge(files: &[String]) -> Result<Blob, Box<dyn Error>> { | |||
|     Ok(result) | ||||
| } | ||||
| 
 | ||||
| pub fn download(merged: Blob) -> Result<(), Box<dyn Error>> { | ||||
|     let window = web_sys::window().ok_or("no global `window` exists")?; | ||||
|     let document = window | ||||
|         .document() | ||||
|         .ok_or("should have a document on window")?; | ||||
| 
 | ||||
|     let err_handler = |e: JsValue| e.as_string().unwrap(); | ||||
|     let anchor_element = document.create_element("a").map_err(err_handler)?; | ||||
|     let url = Url::create_object_url_with_blob(&merged).map_err(err_handler)?; | ||||
| 
 | ||||
|     anchor_element | ||||
|         .set_attribute("href", &url) | ||||
|         .map_err(err_handler)?; | ||||
|     anchor_element | ||||
|         .set_attribute("download", "merged.gpx") | ||||
|         .map_err(err_handler)?; | ||||
| 
 | ||||
|     let event = MouseEvent::new("click").map_err(err_handler)?; | ||||
|     anchor_element.dispatch_event(&event).map_err(err_handler)?; | ||||
| 
 | ||||
|     Ok(()) | ||||
| } | ||||
| 
 | ||||
| #[wasm_bindgen] | ||||
| extern "C" { | ||||
|     pub fn alert(s: &str); | ||||
|  |  | |||
|  | @ -2,7 +2,7 @@ use std::collections::HashMap; | |||
| 
 | ||||
| use gloo_file::callbacks::FileReader; | ||||
| use gloo_file::File; | ||||
| use web_sys::{Event, HtmlInputElement, Url, MouseEvent}; | ||||
| use web_sys::{Blob, Event, HtmlInputElement}; | ||||
| use yew::{html, html::TargetCast, Component, Context, Html}; | ||||
| 
 | ||||
| use super::gpx; | ||||
|  | @ -11,6 +11,7 @@ pub enum Msg { | |||
|     FileLoaded(String, String), | ||||
|     StartLoad(Vec<File>), | ||||
|     FilesLoaded, | ||||
|     Download(Blob), | ||||
|     Reset, | ||||
| } | ||||
| 
 | ||||
|  | @ -78,28 +79,34 @@ impl Component for Loader { | |||
|             } | ||||
| 
 | ||||
|             Msg::FilesLoaded => { | ||||
|                 let link = ctx.link(); | ||||
| 
 | ||||
|                 let merged = match gpx::merge(&self.files) { | ||||
|                     Ok(result) => result, | ||||
|                     Err(err) => { | ||||
|                         gpx::alert(&err.to_string()); | ||||
|                         ctx.link().send_message(Msg::Reset); | ||||
|                         return true | ||||
|                         link.send_message(Msg::Reset); | ||||
|                         return true; | ||||
|                     } | ||||
|                 }; | ||||
| 
 | ||||
|                 let window = web_sys::window().expect("no global `window` exists"); | ||||
|                 let document = window.document().expect("should have a document on window"); | ||||
|                 let anchor_element = document.create_element("a").unwrap(); | ||||
|                 link.send_message(Msg::Download(merged)); | ||||
| 
 | ||||
|                 let url = Url::create_object_url_with_blob(&merged).unwrap(); | ||||
|                 true | ||||
|             } | ||||
| 
 | ||||
|                 anchor_element.set_attribute("href", &url).unwrap(); | ||||
|                 anchor_element.set_attribute("download", "merged.gpx").unwrap(); | ||||
|             Msg::Download(merged) => { | ||||
|                 let link = ctx.link(); | ||||
| 
 | ||||
|                 self.is_loading = false; | ||||
| 
 | ||||
|                 let event = MouseEvent::new("click").unwrap(); | ||||
|                 anchor_element.dispatch_event(&event).unwrap(); | ||||
|                 match gpx::download(merged) { | ||||
|                     Ok(_) => (), | ||||
|                     Err(err) => { | ||||
|                         gpx::alert(&err.to_string()); | ||||
|                         link.send_message(Msg::Reset); | ||||
|                         return true; | ||||
|                     } | ||||
|                 } | ||||
|                 link.send_message(Msg::Reset); | ||||
| 
 | ||||
|                 true | ||||
|             } | ||||
|  |  | |||
|  | @ -1,8 +1,8 @@ | |||
| extern crate wee_alloc; | ||||
| 
 | ||||
| mod app; | ||||
| mod loader; | ||||
| mod gpx; | ||||
| mod loader; | ||||
| 
 | ||||
| // Use `wee_alloc` as the global allocator.
 | ||||
| #[global_allocator] | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue