mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Create untitled copies from browser template picks
This commit is contained in:
1 parent
6a661c54c4
commit
e8327cc3d8
2 files changed
+65
-4
No files matched your search
@@ -646,13 +646,25 @@ fn open_file_with(app: &mut VectorcraftApp, path: &str, background: bool, p: &Va
|
||||
}
|
||||
|
||||
/// File → New from Template…: a template (or any readable file) as a new untitled document. Without
|
||||
/// a path the open dialog starts in the Templates folder. On the web the browser's file picker
|
||||
/// opens it (`.vctemplate`, `.ait` and template files open untitled there too).
|
||||
/// a path the open dialog starts in the Templates folder. The browser supplies the picked file's
|
||||
/// name and bytes for the same template loader.
|
||||
pub fn new_from_template(app: &mut VectorcraftApp, path: Option<String>) -> Result<Value, String> {
|
||||
let Some(path) = path else {
|
||||
let filters = std::iter::once(("Templates", TEMPLATE_EXTS)).chain(fileio::builtin_open_filters());
|
||||
let request = open_request("template", fileio::templates_dialog_folder(&app.session.prefs), filters);
|
||||
return ask_open(app, request, Owner::Free, new_from_path);
|
||||
return Ok(file_asks::ask(app, request, Owner::Free, |app, picked| match picked {
|
||||
Picked::Contents(files) => {
|
||||
let (name, bytes) = files.into_iter().next().ok_or("cancelled")?;
|
||||
if let Err(error) = open_template(app, &name, &bytes, None) {
|
||||
report_open_error(app, &name, &error);
|
||||
}
|
||||
Ok(Value::Null)
|
||||
}
|
||||
picked => {
|
||||
let path = picked.first_path().ok_or("cancelled")?;
|
||||
new_from_path(app, path.to_string_lossy().into_owned())
|
||||
}
|
||||
}));
|
||||
};
|
||||
new_from_path(app, path)
|
||||
}
|
||||
@@ -660,7 +672,11 @@ pub fn new_from_template(app: &mut VectorcraftApp, path: Option<String>) -> Resu
|
||||
/// [`new_from_template`] for the file at `path`.
|
||||
fn new_from_path(app: &mut VectorcraftApp, path: String) -> Result<Value, String> {
|
||||
let bytes = read(app, &path)?;
|
||||
let r = fileio::open_template(&mut app.session, &path, &bytes, Some(&path)).map_err(|e| e.to_string())?;
|
||||
open_template(app, &path, &bytes, Some(&path))
|
||||
}
|
||||
|
||||
fn open_template(app: &mut VectorcraftApp, name: &str, bytes: &[u8], path: Option<&str>) -> Result<Value, String> {
|
||||
let r = fileio::open_template(&mut app.session, name, bytes, path).map_err(|e| e.to_string())?;
|
||||
opened(app, &r);
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
@@ -170,6 +170,51 @@ fn new_from_template_takes_bytes_from_agents() {
|
||||
assert_eq!(app.session.active().unwrap().path, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_from_template_opens_browser_bytes_and_desktop_paths_as_untitled_copies() {
|
||||
let folder = dir("new-from-template-pick");
|
||||
let name = "Source Drawing.svg";
|
||||
let bytes = br#"<svg xmlns="http://www.w3.org/2000/svg" width="80" height="60"><rect width="20" height="10"/></svg>"#;
|
||||
let path = folder.join(name);
|
||||
std::fs::write(&path, bytes).unwrap();
|
||||
for browser in [false, true] {
|
||||
let mut app = app();
|
||||
if browser {
|
||||
app.services.download = Some(Box::new(|_, _| Ok(())));
|
||||
}
|
||||
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
|
||||
rect(&mut app);
|
||||
let source = app.session.active().unwrap();
|
||||
let source_uid = source.uid;
|
||||
let source_undo = source.history.undo.len();
|
||||
let picker = scripted(&mut app);
|
||||
picks::asks(&mut app, "file.newFromTemplate");
|
||||
assert_eq!(picker.requests()[0].purpose, "template");
|
||||
if browser {
|
||||
picker.answer_contents(&[(name, bytes)]);
|
||||
} else {
|
||||
picker.answer(&[path.to_str().unwrap()]);
|
||||
}
|
||||
poll(&mut app);
|
||||
|
||||
let opened = app.session.active().unwrap();
|
||||
assert!(opened.title().starts_with("Untitled-"), "browser={browser}: the template copy was named {:?}", opened.title());
|
||||
assert_eq!((app.session.documents().len(), app.views.len()), (2, 2));
|
||||
assert_eq!((opened.path.as_deref(), opened.history.undo.len()), (None, 0));
|
||||
assert_eq!(opened.doc.layers[0].children().unwrap().len(), 1);
|
||||
let source = &app.session.documents()[0];
|
||||
assert_eq!((source.uid, source.history.undo.len()), (source_uid, source_undo));
|
||||
if browser {
|
||||
picks::asks(&mut app, "file.newFromTemplate");
|
||||
picker.answer_contents(&[(name, b"not a drawing")]);
|
||||
poll(&mut app);
|
||||
assert_eq!(app.session.documents().len(), 2, "an invalid template opens no document");
|
||||
assert!(app.ui.status.contains("Couldn't open Source Drawing.svg"), "{}", app.ui.status);
|
||||
}
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(folder);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn show_in_folder_needs_a_saved_document_and_a_file_manager() {
|
||||
let shown = Rc::new(RefCell::new(vec![]));
|
||||
|
||||
Reference in new issue
Block a user