mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 02:35:37 +01:00
Scripts: closing with SAVECHANGES keeps the document's format, and an empty artboardRange is refused
Document.close(SaveOptions.SAVECHANGES) now saves in the format the document was last saved in, so a PDF saved to a file whose extension names another format stays a PDF. An artboardRange with no parts, such as ',' or ' , ', now throws, as a range used for the save does.
This commit is contained in:
1 parent
e8a0aa6ba4
commit
3ef4b97983
3 files changed
+42
-16
No files matched your search
@@ -260,8 +260,8 @@ fn artboard_files(format: &str, q: &mut Map<String, Value>) -> std::result::Resu
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Whether `r` is written as an artboard range, whatever artboards a document has: parts separated
|
||||
/// by commas, each a number from 1, or two of them joined by `-` or `–` with either one left out
|
||||
/// Whether `r` is written as an artboard range, whatever artboards a document has: one or more
|
||||
/// parts separated by commas, each a number from 1, or two of them joined by `-` or `–` with either one left out
|
||||
/// and the first no greater than the second.
|
||||
fn written_as_range(r: &str) -> bool {
|
||||
// A number of digits alone, from 1; past `u64` it counts as the largest.
|
||||
@@ -273,14 +273,16 @@ fn written_as_range(r: &str) -> bool {
|
||||
let n = t.bytes().all(|b| b.is_ascii_digit()).then(|| t.parse::<u64>().unwrap_or(u64::MAX))?;
|
||||
(n >= 1).then_some(Some(n))
|
||||
};
|
||||
r.split(',').map(str::trim).filter(|p| !p.is_empty()).all(|part| match part.replace('\u{2013}', "-").split_once('-') {
|
||||
Some((a, b)) => match (num(a), num(b)) {
|
||||
(Some(Some(a)), Some(Some(b))) => a <= b,
|
||||
(Some(a), Some(b)) => a.is_some() || b.is_some(),
|
||||
_ => false,
|
||||
},
|
||||
None => matches!(num(part), Some(Some(_))),
|
||||
})
|
||||
let mut parts = r.split(',').map(str::trim).filter(|p| !p.is_empty()).peekable();
|
||||
parts.peek().is_some()
|
||||
&& parts.all(|part| match part.replace('\u{2013}', "-").split_once('-') {
|
||||
Some((a, b)) => match (num(a), num(b)) {
|
||||
(Some(Some(a)), Some(Some(b))) => a <= b,
|
||||
(Some(a), Some(b)) => a.is_some() || b.is_some(),
|
||||
_ => false,
|
||||
},
|
||||
None => matches!(num(part), Some(Some(_))),
|
||||
})
|
||||
}
|
||||
|
||||
const AI_KEPT: &[(&str, Default)] = &[
|
||||
@@ -896,7 +898,7 @@ const NO_FILE_TO_SAVE: &str =
|
||||
/// `document.close(saveOptions?)`: `file.close` of the document. A document without unsaved
|
||||
/// changes closes with any `SaveOptions`. With unsaved changes, `SaveOptions.DONOTSAVECHANGES`
|
||||
/// closes it without them; `SaveOptions.SAVECHANGES` saves them to the document's file with
|
||||
/// `file.saveAs` in the format its extension names, after the write-access check `saveAs` makes of
|
||||
/// `file.saveAs` in the format the document was last saved in, after the write-access check `saveAs` makes of
|
||||
/// the path, and then closes it, and throws [`NO_FILE_TO_SAVE`]
|
||||
/// for a document that has no file, a converted or recovered one included; and
|
||||
/// `SaveOptions.PROMPTTOSAVECHANGES`, the default, throws [`UNSAVED_CHANGES`], since a script
|
||||
@@ -907,7 +909,8 @@ fn doc_close(h: &Host, a: &[Data]) -> Answer {
|
||||
Some(d) => member(d, "SaveOptions", &["DONOTSAVECHANGES", "PROMPTTOSAVECHANGES", "SAVECHANGES"], "saveOptions")?,
|
||||
None => "PROMPTTOSAVECHANGES",
|
||||
};
|
||||
let (dirty, file) = super::with_doc(h, doc, |_, st| (st.is_dirty(), st.path.clone().filter(|_| !st.converted && !st.recovered)));
|
||||
let (dirty, file, format) =
|
||||
super::with_doc(h, doc, |_, st| (st.is_dirty(), st.path.clone().filter(|_| !st.converted && !st.recovered), st.format));
|
||||
if dirty {
|
||||
match how {
|
||||
"DONOTSAVECHANGES" => {}
|
||||
@@ -916,7 +919,7 @@ fn doc_close(h: &Host, a: &[Data]) -> Answer {
|
||||
// A refusal names the document, as `fullName` does when the read roots hide its file.
|
||||
let name = super::with_doc(h, doc, |_, st| st.title());
|
||||
let real = h.disk.borrow().save_target(&path).map_err(|m| refused(m, &name))?;
|
||||
let p = json!({"path": plain(real)});
|
||||
let p = json!({"path": plain(real), "format": format});
|
||||
exactly(h, || on_doc(h, doc.uid, false, |h| command(h, "file.saveAs", &p)))?;
|
||||
}
|
||||
_ => return Err(HostError::Error(UNSAVED_CHANGES.into())),
|
||||
|
||||
@@ -696,6 +696,29 @@ fn a_document_without_unsaved_changes_closes_with_any_save_options() {
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// `close(SaveOptions.SAVECHANGES)` saves in the format the document was last saved in, not the
|
||||
/// one its file's extension names.
|
||||
#[test]
|
||||
fn closing_with_save_changes_keeps_the_documents_format() {
|
||||
let base = folder("closeformat");
|
||||
let mut s = session(&base);
|
||||
let dir = data_folder(&mut s, &base);
|
||||
let d = quoted(&dir);
|
||||
logged(
|
||||
&mut s,
|
||||
&base,
|
||||
&format!(
|
||||
"Folder.userData; var d = app.documents.add(DocumentColorSpace.RGB, 200, 100);\n\
|
||||
vc.run('shape.rectangle', {{x: 10, y: 10, width: 50, height: 20}}); d.saveAs(new File({d} + '/p.svg'), new PDFSaveOptions());\n\
|
||||
vc.run('shape.rectangle', {{x: 20, y: 20, width: 5, height: 5}}); d.close(SaveOptions.SAVECHANGES); vc.log(app.documents.length);"
|
||||
),
|
||||
);
|
||||
let bytes = std::fs::read(dir.join("p.svg")).unwrap_or_default();
|
||||
assert!(bytes.starts_with(b"%PDF"), "the save on close keeps the PDF format");
|
||||
assert!(s.documents().is_empty());
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// Setting `Document.saved` records the document's saved state without saving it: `true` lets
|
||||
/// `close()` close a document with edits, and `false` makes an untouched one ask.
|
||||
#[test]
|
||||
@@ -867,7 +890,7 @@ fn save_multiple_artboards_writes_a_file_per_artboard() {
|
||||
),
|
||||
);
|
||||
assert!(dir.join("aioff/c.ai").is_file(), "a range past the document's artboards is not used and does not stop the save");
|
||||
for bad in ["'abc'", "'0-'", "'3-1'", "'-'", "-1", "2.5", "0"] {
|
||||
for bad in ["'abc'", "'0-'", "'3-1'", "'-'", "','", "' , '", "-1", "2.5", "0"] {
|
||||
let e = failure(
|
||||
&mut s,
|
||||
&base,
|
||||
|
||||
+2
-2
@@ -2751,8 +2751,8 @@ app through the global object `vc`, and writes to the Script Console and runs ot
|
||||
`Script file access denied`. Setting `saved` runs `document.setSaved`: `true` records the document as saved
|
||||
without saving it, so `close()` then closes it, and `false` records it as having unsaved changes; neither adds an
|
||||
undo step. With unsaved changes, `close` closes without them with `SaveOptions.DONOTSAVECHANGES`;
|
||||
`SaveOptions.SAVECHANGES` first saves them to the document's file with `file.saveAs`, in the format its extension
|
||||
names and after the same write-access check of the path as `saveAs`, and throws an error
|
||||
`SaveOptions.SAVECHANGES` first saves them to the document's file with `file.saveAs`, in the format the document
|
||||
was last saved in and after the same write-access check of the path as `saveAs`, and throws an error
|
||||
and leaves the document open when the document has no file (a converted or recovered document included); and
|
||||
`SaveOptions.PROMPTTOSAVECHANGES`, the default, throws an error and leaves the document open, since a script cannot
|
||||
show the prompt. A bad value throws an error before anything is written.
|
||||
|
||||
Reference in new issue
Block a user