mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
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.
This commit is contained in:
1 parent
70e6f4e797
commit
60eb7e002b
4 files changed
+68
-8
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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<Option<HashSet<String>>> = 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<T>(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<Note> = 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() {
|
||||
|
||||
@@ -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`.
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user