Keep Export As attached to the original drawing

This commit is contained in:
dotCooCoo committed 2026-11-26 15:18:52 -08:00
1 parent c646504cec
commit fc8575ecfb
3 files changed
+41 -7

No files matched your search

+18 -5
View File
@@ -19,7 +19,7 @@ use craft::ui::style::TypeRole;
use serde_json::{Value, json};
use vectorcraft_engine::cmd::fileio::{self, ArtboardPick, Format};
use super::export_host::{self, After, Export, Open};
use super::export_host::{self, After, Export};
use super::{dxf_options, eps_options, form, hpgl_options, plugin, png_options, save_pdf, svg_options, text_export};
use crate::{VectorcraftApp, io};
@@ -111,7 +111,9 @@ 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 n = app.session.active().map_or(0, |s| s.doc.artboards.len());
let source = app.session.active().ok_or("no document")?;
let document = source.uid;
let n = 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 {
Some((id, _)) => id,
@@ -133,12 +135,20 @@ fn open_dialog(app: &mut VectorcraftApp, format: Option<&str>, p: &Value) -> Res
})
.ok_label(Label::from("Export…"))
});
export_host::open(app, Export::As, false, dialog).map(|_| ()).ok_or_else(|| "no window".into())
export_host::open(app, Export::As { document }, false, dialog).map(|_| ()).ok_or_else(|| "no window".into())
}
fn require_source(app: &VectorcraftApp, document: u64) -> Result<(), String> {
if app.session.active().is_none_or(|source| source.uid != document) {
return Err(tl!("Return to the original drawing to export it").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, _open: &Open, fields: &DialogFields) -> Result<After, String> {
pub(super) fn confirm(app: &mut VectorcraftApp, document: u64, fields: &DialogFields) -> Result<After, String> {
require_source(app, document)?;
if let Some((_, hint)) = unavailable(fields) {
return Err(hint.into());
}
@@ -162,7 +172,10 @@ pub(super) fn confirm(app: &mut VectorcraftApp, _open: &Open, fields: &DialogFie
}
let range = if all { "all".to_string() } else { range };
let pages = Pages { use_artboards, all, range, selection: false };
Ok(Some(Box::new(move |app| io::export_path(app, None, f, move |app, path| options_or_write(app, f, plugin, path, pages)))))
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))
})))
}
/// `params` of an options dialog for [`io::export`]: with [`io::EXPORT_SELECTION`] when
+5 -2
View File
@@ -21,7 +21,10 @@ pub(crate) type After = Option<Box<dyn FnOnce(&mut VectorcraftApp) -> Result<Val
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum Export {
/// Export As.
As,
As {
/// The drawing whose export the dialog configures.
document: u64,
},
/// The options of the raster format `format`.
Raster {
/// The format's id.
@@ -126,7 +129,7 @@ fn answer(app: &mut VectorcraftApp, result: &ParamResult) -> Result<(), String>
fn confirm(app: &mut VectorcraftApp, open: &Open, fields: &DialogFields) -> Result<After, String> {
match &open.kind {
Export::As => export_as::confirm(app, open, fields),
Export::As { document } => export_as::confirm(app, *document, fields),
Export::Raster { .. } => png_options::confirm(app, open, fields),
Export::Svg { .. } => svg_options::confirm(app, open, fields),
Export::Eps => eps_options::confirm(app, open, fields),
@@ -344,6 +344,24 @@ fn a_bad_range_keeps_export_as_open() {
assert!(written.borrow().is_empty());
}
#[test]
fn export_as_waits_for_the_drawing_that_opened_it() {
let (mut app, written) = app(2);
app.run("file.exportAs", json!({"format": "png", "useArtboards": true, "range": "1"})).unwrap();
app.run("file.new", json!({ "width": 200, "height": 100 })).unwrap();
let error = try_confirm(&mut app).expect_err("Export As must refuse the other drawing");
assert!(error.contains("original drawing"), "{error}");
assert_eq!(kind(&app).as_deref(), Some("exportAs"));
assert_eq!(fields(&app)["range"], "1");
PANEL.with_borrow(|panel| assert!(panel.as_ref().unwrap().picker.requests().is_empty()));
app.run("document.activate", json!({ "index": 0 })).unwrap();
confirm(&mut app);
assert_eq!(kind(&app).as_deref(), Some("pngOptions"));
confirm(&mut app);
assert_eq!(written.borrow().len(), 1);
assert_eq!(&written.borrow()[0].1[16..20], 60u32.to_be_bytes());
}
/// Each format's Export As continues in its options dialog, writes nothing before that dialog's OK, and then
/// writes its files: (case, artboards, Export As params, the options' key, the files, the pages of a PDF, the
/// page size of a PDF of the art's bounds).