This commit is contained in:
Matthew Ryan Dillon 2021-12-07 21:21:22 -07:00
parent 78153e8bf4
commit 8c8b5d0b8f
2 changed files with 11 additions and 6 deletions

View file

@ -2,7 +2,7 @@ use std::collections::HashMap;
use gloo_file::callbacks::FileReader; use gloo_file::callbacks::FileReader;
use gloo_file::File; use gloo_file::File;
use web_sys::{Event, HtmlInputElement, Url, MouseEvent}; use web_sys::{Event, HtmlInputElement, MouseEvent, Url};
use yew::{html, html::TargetCast, Component, Context, Html}; use yew::{html, html::TargetCast, Component, Context, Html};
use super::utils; use super::utils;
@ -45,6 +45,7 @@ impl Component for Loader {
if self.files.len() == self.count { if self.files.len() == self.count {
ctx.link().send_message(Msg::FilesLoaded); ctx.link().send_message(Msg::FilesLoaded);
} }
true true
} }
@ -72,6 +73,7 @@ impl Component for Loader {
}; };
self.readers.insert(file_name, task); self.readers.insert(file_name, task);
} }
true true
} }
@ -81,7 +83,7 @@ impl Component for Loader {
Err(err) => { Err(err) => {
utils::alert(&err.to_string()); utils::alert(&err.to_string());
ctx.link().send_message(Msg::Reset); ctx.link().send_message(Msg::Reset);
return true return true;
} }
}; };
@ -92,7 +94,9 @@ impl Component for Loader {
let url = Url::create_object_url_with_blob(&merged).unwrap(); let url = Url::create_object_url_with_blob(&merged).unwrap();
anchor_element.set_attribute("href", &url).unwrap(); anchor_element.set_attribute("href", &url).unwrap();
anchor_element.set_attribute("download", "merged.gpx").unwrap(); anchor_element
.set_attribute("download", "merged.gpx")
.unwrap();
self.is_loading = false; self.is_loading = false;
@ -108,6 +112,7 @@ impl Component for Loader {
self.count = 0; self.count = 0;
self.is_loading = false; self.is_loading = false;
self.field_value = ""; self.field_value = "";
true true
} }
} }

View file

@ -1,6 +1,6 @@
use std::error::Error; use std::error::Error;
use web_sys::Blob;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use web_sys::Blob;
fn join_gpx_files(files: &Vec<String>) -> Result<gpx::Gpx, Box<dyn Error>> { fn join_gpx_files(files: &Vec<String>) -> Result<gpx::Gpx, Box<dyn Error>> {
let mut merged_gpx: gpx::Gpx = Default::default(); let mut merged_gpx: gpx::Gpx = Default::default();
@ -54,12 +54,12 @@ fn write_gpx_to_buffer(gpx: gpx::Gpx) -> Result<js_sys::Array, Box<dyn Error>> {
pub fn merge(files: &Vec<String>) -> Result<Blob, Box<dyn Error>> { pub fn merge(files: &Vec<String>) -> Result<Blob, Box<dyn Error>> {
let merged: gpx::Gpx = join_gpx_files(files)?; let merged: gpx::Gpx = join_gpx_files(files)?;
let out_vec = write_gpx_to_buffer(merged)?; let out_vec = write_gpx_to_buffer(merged)?;
let result = Blob::new_with_u8_array_sequence(&out_vec).map_err(|e| e.as_string().unwrap() )?; let result = Blob::new_with_u8_array_sequence(&out_vec).map_err(|e| e.as_string().unwrap())?;
Ok(result) Ok(result)
} }
#[wasm_bindgen] #[wasm_bindgen]
extern { extern "C" {
pub fn alert(s: &str); pub fn alert(s: &str);
} }