From 60eb7e002be84c3a00599810f7f71f02bfc7b878 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 15:06:16 -0800 Subject: [PATCH] Scripts: exportForScreens numbers a file that an earlier item of the same call named Document.exportForScreens exports the artboards, the assets and the document that itemToExport names one after another into the folder. A later export now names its files around the names the earlier ones took, so an asset named as an artboard writes Logo-2.svg next to Logo.svg instead of writing over it. --- crates/engine/src/cmd/fileio.rs | 4 +-- crates/engine/src/cmd/fileio/screens.rs | 34 +++++++++++++++++++ .../src/script/dom/save_open/screens.rs | 16 +++++---- crates/engine/src/tests/script_screens.rs | 22 ++++++++++++ 4 files changed, 68 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/fileio.rs b/crates/engine/src/cmd/fileio.rs index 7aca68fd02..4830432047 100644 --- a/crates/engine/src/cmd/fileio.rs +++ b/crates/engine/src/cmd/fileio.rs @@ -112,8 +112,8 @@ pub use save::{ }; pub(crate) use screens::{ PARAM_KEYS as SCREEN_PARAM_KEYS, ROW_KEYS as SCREEN_ROW_KEYS, UNWRITTEN_FORMATS as SCREEN_UNWRITTEN_FORMATS, - check_settings as check_screen_settings, export as export_screens, safe_file_name, store_settings as store_screen_settings, - with_export_preset as with_screen_export_preset, + check_settings as check_screen_settings, export as export_screens, safe_file_name, sharing_names as sharing_screen_names, + store_settings as store_screen_settings, with_export_preset as with_screen_export_preset, }; pub use screens::{ PRESET_FILE_EXTS as SCREEN_EXPORT_PRESET_EXTS, PRESET_FILE_FORMAT as SCREEN_EXPORT_PRESET_FORMAT, PRESET_KEYS as SCREEN_EXPORT_PRESET_KEYS, diff --git a/crates/engine/src/cmd/fileio/screens.rs b/crates/engine/src/cmd/fileio/screens.rs index 442f38e3cc..4ab1c492a1 100644 --- a/crates/engine/src/cmd/fileio/screens.rs +++ b/crates/engine/src/cmd/fileio/screens.rs @@ -638,6 +638,25 @@ fn fold(name: &str) -> String { .collect() } +thread_local! { + /// The [`fold`]ed names the exports run by [`sharing_names`] on this thread have taken so far. + static SHARED_NAMES: std::cell::RefCell>> = const { std::cell::RefCell::new(None) }; +} + +/// `f`, with every `document.exportForScreens` it runs on this thread naming its files around +/// the names the ones before it took, so a later export does not write over an earlier one's file +/// (it numbers its own: `-2`, `-3`…). +pub(crate) fn sharing_names(f: impl FnOnce() -> T) -> T { + let outer = SHARED_NAMES.with(|n| n.borrow_mut().replace(HashSet::new())); + let r = f(); + SHARED_NAMES.with(|n| *n.borrow_mut() = outer); + r +} + +/// The key the names taken by an earlier export are held under in [`Output::taken`]: no file of +/// this export has it. +const EARLIER_EXPORT: &str = "\0earlier export"; + /// The file names (and bytes) an export wrote, or the files it returns. #[derive(Default)] struct Output { @@ -1342,6 +1361,14 @@ fn encode_and_write(export: ScreenExport, writer: &mut dyn FnMut(&str, &[u8]) -> create_dir(dir)?; } let mut out = Output::default(); + // The names earlier exports of a [`sharing_names`] run took into the same files. + if !o.zip { + SHARED_NAMES.with(|n| { + for name in n.borrow().iter().flatten() { + out.taken.insert(name.clone(), EARLIER_EXPORT.to_string()); + } + }); + } // The notes on the options, then the encoders' notes for every file, each once. let mut notes: Vec = Vec::new(); for text in unused { @@ -1417,6 +1444,13 @@ fn encode_and_write(export: ScreenExport, writer: &mut dyn FnMut(&str, &[u8]) -> files += 1; } } + if !o.zip { + SHARED_NAMES.with(|n| { + if let Some(shared) = n.borrow_mut().as_mut() { + shared.extend(out.taken.keys().cloned()); + } + }); + } // A cancel that came while the last file was encoded returns no files. A folder export // without a ZIP has written every file by now, so it returns them. if o.zip || folder.is_none() { diff --git a/crates/engine/src/script/dom/save_open/screens.rs b/crates/engine/src/script/dom/save_open/screens.rs index f4008be1b6..c4cf253ecd 100644 --- a/crates/engine/src/script/dom/save_open/screens.rs +++ b/crates/engine/src/script/dom/save_open/screens.rs @@ -461,12 +461,16 @@ fn doc_export_for_screens(h: &Host, a: &[Data]) -> Answer { if let Some(prefix) = prefix { base.insert("prefix".into(), json!(prefix)); } - for items in exports { - let mut p = base.clone(); - p.extend(items); - exactly(h, || on_doc(h, doc.uid, false, |h| command(h, "document.exportForScreens", &Value::Object(p))))?; - } - Ok(Data::Undefined) + // The artboards, the assets and the document are exported one after another into the folder: + // a later export numbers a file whose name an earlier one took instead of writing over it. + crate::cmd::fileio::sharing_screen_names(|| { + for items in exports { + let mut p = base.clone(); + p.extend(items); + exactly(h, || on_doc(h, doc.uid, false, |h| command(h, "document.exportForScreens", &Value::Object(p))))?; + } + Ok(Data::Undefined) + }) } /// The members these add to `Document`. diff --git a/crates/engine/src/tests/script_screens.rs b/crates/engine/src/tests/script_screens.rs index c9bd000e1d..343feff945 100644 --- a/crates/engine/src/tests/script_screens.rs +++ b/crates/engine/src/tests/script_screens.rs @@ -195,6 +195,28 @@ fn item_to_export_picks_artboards_assets_and_the_document() { let _ = std::fs::remove_dir_all(base); } +/// An asset named as an artboard of the item's range gets a file of its own: the artboard's file +/// keeps its name and the asset's is numbered, in place of writing over it. +#[test] +fn an_asset_named_as_an_artboard_does_not_overwrite_its_file() { + let base = folder("clash"); + let mut s = session(&base); + let dir = data_folder(&mut s, &base); + logged( + &mut s, + &base, + &format!( + "{}vc.run('artboard.setProps', {{artboards: [{{index: 0, name: 'Logo'}}]}});\n\ + var ids = vc.run('assets.add', {{name: 'Logo', multiple: false}}).assets;\n\ + var both = new ExportForScreensItemToExport(); both.artboards = '1'; both.assets = ids;\n\ + d.exportForScreens(new Folder(dir + '/both'), ExportForScreensType.SE_SVG, undefined, both);", + two_artboards(&dir) + ), + ); + assert_eq!(names(&dir.join("both")), ["Logo-2.svg", "Logo.svg"]); + let _ = std::fs::remove_dir_all(base); +} + /// The prefix comes before each file's name, and a path character in it does not make a folder or /// reach out of the export folder. #[test]