Keep Export As destination approval on its opening drawing

This commit is contained in:
dotCooCoo committed 2026-11-26 19:39:49 -08:00
1 parent 0aea8fed95
commit 969f817332
3 files changed
+80 -9

No files matched your search

+12 -7
View File
@@ -112,7 +112,7 @@ pub fn open(app: &mut VectorcraftApp, format: Option<&str>, p: &Value) {
fn open_dialog(app: &mut VectorcraftApp, format: Option<&str>, p: &Value) -> Result<(), String> {
let ctx = app.ui_ctx.clone().ok_or("no window")?;
let source = app.session.active();
let document = source.map(|source| source.uid);
let document = source.map(|source| (source.uid, source.reverts));
let n = source.map_or(0, |source| source.doc.artboards.len());
let missing = format.and_then(|f| UNAVAILABLE.iter().find(|u| u.0.eq_ignore_ascii_case(f.trim_start_matches('.'))));
let format = match missing {
@@ -138,17 +138,19 @@ fn open_dialog(app: &mut VectorcraftApp, format: Option<&str>, p: &Value) -> Res
export_host::open(app, Export::As { document }, false, dialog).map(|_| ()).ok_or_else(|| "no window".into())
}
fn require_source(app: &VectorcraftApp, document: Option<u64>) -> Result<(), String> {
let document = document.ok_or_else(|| tl!("Open Export As from the drawing you want to export").to_owned())?;
if app.session.active().is_none_or(|source| source.uid != document) {
return Err(tl!("Return to the original drawing to export it").into());
fn require_source(app: &VectorcraftApp, document: Option<(u64, u64)>) -> Result<(), String> {
let (uid, reverts) = document.ok_or_else(|| tl!("Open Export As from the drawing you want to export").to_owned())?;
let source =
app.session.active().filter(|source| source.uid == uid).ok_or_else(|| tl!("Return to the original drawing to export it").to_owned())?;
if source.reverts != reverts {
return Err(tl!("The document was reverted. Reopen this dialog to continue.").into());
}
Ok(())
}
/// Check the artboard range and that there is something to export; the save panel and the format's options or
/// the file follow once the dialog has closed.
pub(super) fn confirm(app: &mut VectorcraftApp, document: Option<u64>, fields: &DialogFields) -> Result<After, String> {
pub(super) fn confirm(app: &mut VectorcraftApp, document: Option<(u64, u64)>, fields: &DialogFields) -> Result<After, String> {
require_source(app, document)?;
if let Some((_, hint)) = unavailable(fields) {
return Err(hint.into());
@@ -175,7 +177,10 @@ pub(super) fn confirm(app: &mut VectorcraftApp, document: Option<u64>, fields: &
let pages = Pages { use_artboards, all, range, selection: false };
Ok(Some(Box::new(move |app| {
require_source(app, document)?;
io::export_path(app, None, f, move |app, path| options_or_write(app, f, plugin, path, pages))
io::export_path(app, None, f, move |app, path| {
require_source(app, document)?;
options_or_write(app, f, plugin, path, pages)
})
})))
}
+2 -2
View File
@@ -22,8 +22,8 @@ pub(crate) type After = Option<Box<dyn FnOnce(&mut VectorcraftApp) -> Result<Val
pub(crate) enum Export {
/// Export As.
As {
/// The drawing whose export the dialog configures, when one was open.
document: Option<u64>,
/// The drawing and content generation whose export the dialog configures, when one was open.
document: Option<(u64, u64)>,
},
/// The options of the raster format `format`.
Raster {
@@ -504,3 +504,69 @@ fn export_as_writes_only_the_chosen_artboard_in_every_format() {
}
}
}
fn native_export_as_source() -> (VectorcraftApp, Written, crate::tests_picks::Picker, vectorcraft_testkit::TempDir) {
let (mut app, written) = app(1);
let dir = vectorcraft_testkit::TempDir::new("export-as-after-revert");
let capture = written.clone();
app.services.write = Some(Box::new(move |path, bytes| {
if path.ends_with(".vectorcraft") {
std::fs::write(path, bytes).map_err(|error| error.to_string())
} else {
capture.borrow_mut().push((path.to_string(), bytes.to_vec()));
Ok(())
}
}));
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|error| error.to_string())));
let picker = crate::tests_picks::scripted(&mut app);
app.run("file.saveAs", json!({"path": dir.join("Export.vectorcraft")})).unwrap();
app.run("shape.ellipse", json!({"x": 45, "y": 20, "width": 10, "height": 10})).unwrap();
(app, written, picker, dir)
}
fn revert_export_source(app: &mut VectorcraftApp) {
let source = app.session.active().unwrap();
let (uid, reverts) = (source.uid, source.reverts);
app.run("file.revert", json!({"confirmed": true})).unwrap();
let source = app.session.active().unwrap();
assert_eq!((source.uid, source.reverts), (uid, reverts + 1));
}
#[test]
fn export_as_refuses_an_approval_from_before_native_revert() {
let (mut app, written, picker, _dir) = native_export_as_source();
app.run("file.exportAs", json!({"format": "png", "useArtboards": true})).unwrap();
revert_export_source(&mut app);
let error = try_confirm(&mut app).expect_err("an old Export As approval must stay in the dialog");
assert!(error.contains("Reopen this dialog"));
assert_eq!(kind(&app).as_deref(), Some("exportAs"));
assert_eq!(fields(&app)["useArtboards"], true);
assert!(picker.requests().is_empty(), "the retired draft did not open a save picker");
assert!(written.borrow().is_empty());
}
#[test]
fn export_as_cannot_use_a_destination_selected_after_native_revert() {
let (mut app, written, picker, _dir) = native_export_as_source();
app.run("file.exportAs", json!({"format": "png"})).unwrap();
try_confirm(&mut app).unwrap();
assert_eq!(picker.requests().len(), 1);
revert_export_source(&mut app);
picker.answer_saved("/output/Old.png", None);
crate::file_asks::poll(&mut app);
frame(&mut app);
assert!(kind(&app).is_none(), "the retired pick did not open PNG Options");
assert!(written.borrow().is_empty());
app.run("file.exportAs", json!({"format": "png"})).unwrap();
try_confirm(&mut app).unwrap();
assert_eq!(picker.requests().len(), 2);
picker.answer_saved("/output/Fresh.png", None);
crate::file_asks::poll(&mut app);
frame(&mut app);
assert_eq!(kind(&app).as_deref(), Some("pngOptions"));
try_confirm(&mut app).unwrap();
assert_eq!(names(&written), ["/output/Fresh.png"]);
}