From e855b45f9510d271730dc7f8c6beb221b54e62f3 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 8 Dec 2021 21:49:49 -0700 Subject: [PATCH] wip --- src/app.rs | 12 +++++------- src/gpx.rs | 25 ++++++++++++++++++++++++- src/loader.rs | 33 ++++++++++++++++++++------------- src/main.rs | 2 +- 4 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/app.rs b/src/app.rs index 3890cb0..062837a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -29,15 +29,13 @@ impl Component for App {
+

+ {"Please note, this has only been tested on GPX files produced by "} + {"Garmin"}{" and "} + {"Strava"}{" - your mileage may vary."} +

{"source (public access): git://pingo.thermokar.st/gpx-web-utils"} - - {"Please note, this has only been tested on GPX files produced by "} - {"Garmin"} - {" and "} - {"Strava"} - {" - your mileage may vary."} -

} diff --git a/src/gpx.rs b/src/gpx.rs index 0afb3ee..7eb470a 100644 --- a/src/gpx.rs +++ b/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> { let mut merged_gpx: gpx::Gpx = Default::default(); @@ -59,6 +59,29 @@ pub fn merge(files: &[String]) -> Result> { Ok(result) } +pub fn download(merged: Blob) -> Result<(), Box> { + 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); diff --git a/src/loader.rs b/src/loader.rs index 1762c32..0e0510b 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -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), 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 } diff --git a/src/main.rs b/src/main.rs index 508cb79..96095ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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]