parent
ecdbb81117
commit
ce523717ee
|
@ -560,6 +560,7 @@ dependencies = [
|
||||||
"log",
|
"log",
|
||||||
"regex",
|
"regex",
|
||||||
"serde",
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"uuid",
|
"uuid",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
"wasm-bindgen-test",
|
"wasm-bindgen-test",
|
||||||
|
|
|
@ -19,6 +19,7 @@ regex = "1"
|
||||||
lazy_static = "1.4"
|
lazy_static = "1.4"
|
||||||
uuid = { version = "1.3", features = ["v4", "fast-rng", "macro-diagnostics", "js", "serde"] }
|
uuid = { version = "1.3", features = ["v4", "fast-rng", "macro-diagnostics", "js", "serde"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
csv = "1.2"
|
csv = "1.2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
@ -76,25 +76,31 @@ pub fn MainWindow() -> Html {
|
||||||
web_sys::window().unwrap().alert_with_message("No transfers to export.").unwrap();
|
web_sys::window().unwrap().alert_with_message("No transfers to export.").unwrap();
|
||||||
return ()
|
return ()
|
||||||
}
|
}
|
||||||
|
web_sys::window().unwrap().alert_with_message("CSV export is currently not importable. Export as JSON if you'd like to back up your work!").unwrap();
|
||||||
if let Ok(csv) = state_to_csv(&main_state) {
|
if let Ok(csv) = state_to_csv(&main_state) {
|
||||||
let csv: &str = &csv;
|
save_str(&csv, "transfers.csv");
|
||||||
let blob = Blob::new_with_str_sequence(
|
|
||||||
&Array::from_iter(std::iter::once(JsValue::from_str(csv))));
|
|
||||||
if let Ok(blob) = blob {
|
|
||||||
let url = Url::create_object_url_with_blob(&blob).expect("We have a blob, why not URL?");
|
|
||||||
// Beneath is the cool hack to download files
|
|
||||||
let window = web_sys::window().unwrap();
|
|
||||||
let document = window.document().unwrap();
|
|
||||||
let anchor = document.create_element("a").unwrap()
|
|
||||||
.dyn_into::<HtmlAnchorElement>().unwrap();
|
|
||||||
anchor.set_download("transfers.csv");
|
|
||||||
anchor.set_href(&url);
|
|
||||||
anchor.click();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let export_json_button_callback = {
|
||||||
|
let main_state = main_state.clone();
|
||||||
|
Callback::from(move |_| {
|
||||||
|
if let Ok(json) = serde_json::to_string(&main_state) {
|
||||||
|
save_str(&json, "plate-tool-state.json");
|
||||||
|
} else {
|
||||||
|
web_sys::window().unwrap().alert_with_message("Failed to export.").unwrap();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
let import_json_button_callback = {
|
||||||
|
let main_dispatch = main_dispatch.clone();
|
||||||
|
Callback::from(move |_| {
|
||||||
|
!unimplemented!()
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
html! {
|
html! {
|
||||||
<>
|
<>
|
||||||
<div class="upper_menu">
|
<div class="upper_menu">
|
||||||
|
@ -105,9 +111,10 @@ pub fn MainWindow() -> Html {
|
||||||
<button>{"Export"}</button>
|
<button>{"Export"}</button>
|
||||||
<div>
|
<div>
|
||||||
<button onclick={export_csv_button_callback}>{"Export as CSV"}</button>
|
<button onclick={export_csv_button_callback}>{"Export as CSV"}</button>
|
||||||
<button>{"Export as ???"}</button>
|
<button onclick={export_json_button_callback}>{"Export as JSON"}</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<button onclick={import_json_button_callback}>{"Import"}</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="main_container">
|
<div class="main_container">
|
||||||
|
@ -122,3 +129,19 @@ pub fn MainWindow() -> Html {
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn save_str(data: &str, name: &str) {
|
||||||
|
let blob = Blob::new_with_str_sequence(
|
||||||
|
&Array::from_iter(std::iter::once(JsValue::from_str(data))));
|
||||||
|
if let Ok(blob) = blob {
|
||||||
|
let url = Url::create_object_url_with_blob(&blob).expect("We have a blob, why not URL?");
|
||||||
|
// Beneath is the cool hack to download files
|
||||||
|
let window = web_sys::window().unwrap();
|
||||||
|
let document = window.document().unwrap();
|
||||||
|
let anchor = document.create_element("a").unwrap()
|
||||||
|
.dyn_into::<HtmlAnchorElement>().unwrap();
|
||||||
|
anchor.set_download(name);
|
||||||
|
anchor.set_href(&url);
|
||||||
|
anchor.click();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue