Merge pull request #9555 from dotCooCoo/fix/variables-browser-download

Download Variables CSV and XML exports in the browser
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-28 08:03:38 -08:00
commit bb954425a0
2 files changed
+111 -15

No files matched your search

+13 -4
View File
@@ -39,6 +39,9 @@ pub(crate) enum Owner {
/// A pick asked for and not yet answered.
struct Waiting {
ticket: PickId,
owner: Owner,
/// The file service is writing bytes that the caller already prepared.
saving: bool,
/// The active drawing's UID and Revert counter when asked, if the request is document-owned.
doc: Option<(u64, u64)>,
/// The automation roots in force when asked: the continuation runs under them, not under the
@@ -122,7 +125,7 @@ pub(crate) fn ask(
owner: Owner,
then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result<Value, String> + 'static,
) -> Value {
start(app, owner, |files| files.pick(request), None, then)
start(app, owner, |files| files.pick(request), false, None, then)
}
/// Like [`ask`], but drops a reply if its requester is gone, before choosing a document or reporting a picker error.
@@ -133,7 +136,7 @@ pub(crate) fn ask_while(
current: impl FnOnce(&VectorcraftApp) -> bool + 'static,
then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result<Value, String> + 'static,
) -> Value {
start(app, owner, |files| files.pick(request), Some(Box::new(current)), then)
start(app, owner, |files| files.pick(request), false, Some(Box::new(current)), then)
}
/// Save `bytes` as the file service saves them (`Files::save`: the desktop asks where with `request` and
@@ -146,7 +149,7 @@ pub(crate) fn save(
owner: Owner,
then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result<Value, String> + 'static,
) -> Value {
start(app, owner, |files| files.save(request, bytes), None, then)
start(app, owner, |files| files.save(request, bytes), true, None, then)
}
/// Asks the file service with `ask` and files `then` for its answer.
@@ -154,6 +157,7 @@ fn start(
app: &mut VectorcraftApp,
owner: Owner,
ask: impl FnOnce(&dyn Files) -> PickId,
saving: bool,
current: Option<PickGuard>,
then: impl FnOnce(&mut VectorcraftApp, Picked) -> Result<Value, String> + 'static,
) -> Value {
@@ -164,7 +168,7 @@ fn start(
Owner::Free => None,
};
let (scope, automated) = (ScopeHandle::capture(), app.automation_asked());
app.file_asks.waiting.push(Waiting { ticket, doc, scope, automated, current, then: Box::new(then) });
app.file_asks.waiting.push(Waiting { ticket, owner, saving, doc, scope, automated, current, then: Box::new(then) });
json!({ "pending": "fileDialog" })
}
@@ -318,6 +322,11 @@ pub(crate) fn poll(app: &mut VectorcraftApp) {
}
match &picked {
Picked::Cancelled => continue,
Picked::Failed(error) if w.saving => {
// The file service already had the bytes. It can report a failure without a drawing.
report(app, Err(error.clone()));
continue;
}
Picked::Unavailable(why) => {
// The desktop says what gives it a file dialog; the browser has none for this kind of pick.
#[cfg(not(target_arch = "wasm32"))]
+98 -11
View File
@@ -706,14 +706,29 @@ fn ask_open(app: &mut VectorcraftApp, filters: &[(&str, &[&str])], command: &'st
})
}
/// Asks for a file `name` to write with `filters` and runs `command` with its path; a cancelled
/// dialog does nothing → what [`file_asks::ask`] returns.
fn ask_save(app: &mut VectorcraftApp, name: &str, filters: &[(&str, &[&str])], command: &'static str) -> Value {
/// Desktop saves run the path command after the picker; browser saves serialize through the
/// command and give the bytes to the file service for a save or download.
fn ask_save(app: &mut VectorcraftApp, name: &str, filters: &[(&str, &[&str])], command: &'static str, field: &str) -> Value {
let request = filters.iter().fold(PickRequest::save("variables").file_name(name), |r, (name, exts)| r.filter(*name, exts));
file_asks::ask(app, request, Owner::Document, move |app, picked| match picked.first_path() {
Some(path) => app.run(command, json!({"path": path.to_string_lossy()})),
None => Ok(Value::Null),
})
if !app.host.files().browser_saves() {
return file_asks::ask(app, request, Owner::Document, move |app, picked| match picked.first_path() {
Some(path) => app.run(command, json!({"path": path.to_string_lossy()})),
None => Ok(Value::Null),
});
}
let output = match app.run(command, json!({})) {
Ok(output) => output,
Err(error) => {
app.status(error);
return Value::Null;
}
};
let Some(text) = output.get(field).and_then(Value::as_str) else {
app.status("The export returned no file contents");
return Value::Null;
};
let bytes = text.as_bytes().to_vec();
file_asks::save(app, request, bytes, Owner::Document, |_, _| Ok(Value::Null))
}
/// Import…: one file dialog for a CSV, TSV, TXT or XML file, and `variable.import` on the chosen
@@ -727,9 +742,9 @@ fn import_datasets(app: &mut VectorcraftApp) -> Value {
ask_open(app, &[CSV], "dataset.import")
}
/// Export Data Sets…: a path, and `dataset.export` to it.
/// Export Data Sets…: save or download the CSV from `dataset.export`.
fn export_datasets(app: &mut VectorcraftApp) -> Value {
ask_save(app, "datasets.csv", &[("CSV", &["csv"])], "dataset.export")
ask_save(app, "datasets.csv", &[("CSV", &["csv"])], "dataset.export", "csv")
}
/// Load Variable Library…: an XML file, and `variable.loadLibrary` on it.
@@ -737,9 +752,9 @@ fn load_library(app: &mut VectorcraftApp) -> Value {
ask_open(app, &[XML], "variable.loadLibrary")
}
/// Save Variable Library…: a path for an XML file, and `variable.saveLibrary` to it.
/// Save Variable Library…: save or download the XML from `variable.saveLibrary`.
fn save_library(app: &mut VectorcraftApp) -> Value {
ask_save(app, "variables.xml", &[XML], "variable.saveLibrary")
ask_save(app, "variables.xml", &[XML], "variable.saveLibrary", "xml")
}
#[cfg(test)]
@@ -789,6 +804,78 @@ mod tests {
}
use crate::i18n::LangExt;
#[test]
fn exports_give_the_file_service_the_csv_and_xml_contents() {
for (command, path, marker) in [(EXPORT_DATA_SETS, "/datasets.csv", "Alice"), (SAVE_LIBRARY, "/variables.xml", "Alice")] {
let mut app = app_with_data();
let revision = app.session.active().unwrap().revision;
let files = Arc::new(craft::host::ScriptedFiles::held().in_browser());
crate::tests::picks::install_files(&mut app, files.clone());
run(&mut app, command, &json!({})).unwrap().unwrap();
files.answer(craft::host::Picked::Saved { path: path.into(), file_type: None });
crate::file_asks::poll(&mut app);
let bytes = files.file(std::path::Path::new(path)).expect("the file service saved the serialized export");
let text = String::from_utf8(bytes).unwrap();
assert!(text.contains(marker), "the export keeps the captured text value");
assert_eq!(app.session.active().unwrap().revision, revision, "export does not change the drawing");
let import = if command == EXPORT_DATA_SETS { "dataset.import" } else { "variable.loadLibrary" };
let params = if command == EXPORT_DATA_SETS { json!({"csv": text}) } else { json!({"xml": text}) };
app.run(import, params).unwrap();
let datasets = app.run("dataset.list", json!({})).unwrap();
assert_eq!(datasets["datasets"].as_array().unwrap().len(), 1);
}
}
#[test]
fn browser_export_completion_keeps_the_drawing_and_reports_write_failures() {
use craft::host::{Picked, ScriptedFiles};
for command in [EXPORT_DATA_SETS, SAVE_LIBRARY] {
for answer in [Picked::Downloaded("export".into()), Picked::Cancelled, Picked::Failed("save was refused".into())] {
let mut app = app_with_data();
let revision = app.session.active().unwrap().revision;
let datasets = app.run("dataset.list", json!({})).unwrap();
let files = Arc::new(ScriptedFiles::held().in_browser());
crate::tests::picks::install_files(&mut app, files.clone());
run(&mut app, command, &json!({})).unwrap().unwrap();
let failed = matches!(answer, Picked::Failed(_));
files.answer(answer);
crate::file_asks::poll(&mut app);
assert_eq!(app.session.active().unwrap().revision, revision);
assert_eq!(app.run("dataset.list", json!({})).unwrap(), datasets);
if failed {
assert_eq!(app.ui.status, "save was refused");
}
}
}
}
#[test]
fn a_failed_variables_download_reports_its_error_after_the_source_closes() {
use craft::host::{Picked, ScriptedFiles};
for command in [EXPORT_DATA_SETS, SAVE_LIBRARY] {
for answer in [Picked::Failed("downloads are blocked".into()), Picked::Cancelled, Picked::Downloaded("export".into())] {
let mut app = app_with_data();
let files = Arc::new(ScriptedFiles::held().in_browser());
crate::tests::picks::install_files(&mut app, files.clone());
run(&mut app, command, &json!({})).unwrap().unwrap();
app.session.execute("file.close", &json!({"index": 0})).unwrap();
app.run("file.new", json!({"width": 80, "height": 80})).unwrap();
app.status("another drawing");
let before = app.session.active().unwrap().doc.clone();
let expected = match &answer {
Picked::Failed(error) => error.as_str(),
Picked::Cancelled => "another drawing",
_ => file_asks::closed(),
};
files.answer(answer.clone());
file_asks::poll(&mut app);
assert_eq!(app.ui.status, expected);
assert_eq!(app.session.active().unwrap().doc, before);
}
}
}
fn app_with_data() -> VectorcraftApp {
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
app.session.execute("file.new", &json!({"width": 300, "height": 200})).unwrap();