mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-30 02:07:10 +01:00
Scripts: style objects made before File > Revert go stale
A style object kept working after file.revert when the file read back held a style with the same name. Each style object now keeps the document's revert count, as text ranges do, and one made before a revert throws Stale style. The range style lists follow the per-paragraph entries of the text model, and the host name table lists the style classes.
This commit is contained in:
1 parent
a2063cea75
commit
de11e1e755
4 files changed
+69
-18
No files matched your search
@@ -12,8 +12,8 @@
|
||||
//! panel lists them.
|
||||
//! - A style object names its style for the rest of the run, also after the script renames it
|
||||
//! through `name`. Once the style is removed, or renamed or removed other than through the object
|
||||
//! model, or its document is closed, every member throws `Stale style`; a style added later under
|
||||
//! the same name is another style.
|
||||
//! model, or its document is closed or reverted with File › Revert, every member throws
|
||||
//! `Stale style`; a style added later under the same name is another style.
|
||||
//! - A style name is trimmed, and must then be 1 to 256 characters long with no control
|
||||
//! characters.
|
||||
//! - A style's `characterAttributes` and `paragraphAttributes` read `undefined` for an attribute the
|
||||
@@ -54,9 +54,11 @@ pub const MAX_STYLE_NAME: usize = 256;
|
||||
/// The most styles one run's style objects name.
|
||||
pub const MAX_HELD: usize = 4096;
|
||||
|
||||
/// A style that a run's style objects name: its document, its kind and its name now.
|
||||
/// A style that a run's style objects name: its document and the document's revert count then, its
|
||||
/// kind and its name now.
|
||||
struct Slot {
|
||||
uid: u64,
|
||||
reverts: u64,
|
||||
para: bool,
|
||||
name: String,
|
||||
live: bool,
|
||||
@@ -91,7 +93,7 @@ pub(in crate::script) fn after_command(h: &Host) {
|
||||
seen.retain(|(uid, ..)| *uid != st.uid);
|
||||
seen.push((st.uid, st.doc.char_styles.clone(), st.doc.para_styles.clone()));
|
||||
for slot in slots.iter_mut().filter(|x| x.live && x.uid == st.uid) {
|
||||
slot.live = has_style(st, slot.para, &slot.name);
|
||||
slot.live = slot.reverts == st.reverts && has_style(st, slot.para, &slot.name);
|
||||
}
|
||||
}
|
||||
for slot in slots.iter_mut().filter(|x| x.live) {
|
||||
@@ -131,7 +133,7 @@ pub(in crate::script) fn put_back(h: &Host, mark: Mark) {
|
||||
let (older, newer) = slots.split_at_mut(i);
|
||||
let Some(slot) = newer.first_mut() else { break };
|
||||
let named = older.iter().any(|o| o.live && o.uid == slot.uid && o.para == slot.para && o.name == slot.name);
|
||||
slot.live = !named && s.documents().iter().any(|st| st.uid == slot.uid && has_style(st, slot.para, &slot.name));
|
||||
slot.live = !named && s.documents().iter().any(|st| st.uid == slot.uid && st.reverts == slot.reverts && has_style(st, slot.para, &slot.name));
|
||||
}
|
||||
*styles.seen.borrow_mut() = mark.seen;
|
||||
}
|
||||
@@ -174,14 +176,15 @@ fn stale() -> HostError {
|
||||
/// The style object for the style `name` of the document `uid`: the same slot as every other
|
||||
/// object naming it in this run.
|
||||
fn style_ref(h: &Host, uid: u64, para: bool, name: &str) -> Answer {
|
||||
let reverts = h.session.borrow().documents().iter().find(|d| d.uid == uid).map_or(0, |d| d.reverts);
|
||||
let mut slots = h.dom.text.styles.slots.borrow_mut();
|
||||
let i = match slots.iter().position(|s| s.live && s.uid == uid && s.para == para && s.name == name) {
|
||||
let i = match slots.iter().position(|s| s.live && s.uid == uid && s.reverts == reverts && s.para == para && s.name == name) {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
if slots.len() >= MAX_HELD {
|
||||
return Err(HostError::Range(format!("a script can name at most {MAX_HELD} styles in one run")));
|
||||
}
|
||||
slots.push(Slot { uid, para, name: name.to_string(), live: true });
|
||||
slots.push(Slot { uid, reverts, para, name: name.to_string(), live: true });
|
||||
slots.len() - 1
|
||||
}
|
||||
};
|
||||
@@ -207,7 +210,7 @@ fn style_of(h: &Host, d: &Data, classes: &[(&str, bool)]) -> std::result::Result
|
||||
let slot = usize_of(*slot).ok_or_else(stale)?;
|
||||
let mut slots = h.dom.text.styles.slots.borrow_mut();
|
||||
let s = slots.get_mut(slot).filter(|s| s.live && s.uid == doc.uid && s.para == para).ok_or_else(stale)?;
|
||||
if !with_doc(h, doc, |_, st| has_style(st, para, &s.name)) {
|
||||
if !with_doc(h, doc, |_, st| st.reverts == s.reverts && has_style(st, para, &s.name)) {
|
||||
s.live = false;
|
||||
return Err(stale());
|
||||
}
|
||||
@@ -552,7 +555,7 @@ fn listed(h: &Host, of: &Of, para: bool) -> std::result::Result<(Doc, Vec<String
|
||||
let (paras, _, _) = para_range(&index, s, e);
|
||||
paras.map(|i| t.para_at(i).style_name.clone()).collect()
|
||||
} else {
|
||||
styles_in(t, index.byte(s, false), index.byte(e, true))?.into_iter().map(|(c, _)| c.style_name).collect()
|
||||
styles_in(t, index.byte(s, false), index.byte(e, true), false)?.into_iter().map(|(c, _)| c.style_name).collect()
|
||||
};
|
||||
h.charge_call(used.len() as u64)?;
|
||||
let mut out: Vec<String> = Vec::new();
|
||||
|
||||
@@ -326,6 +326,28 @@ fn apply_to_refuses_bad_arguments_with_nothing_changed() {
|
||||
}
|
||||
|
||||
/// A run names at most 4,096 styles; reading past that throws instead of growing without bound.
|
||||
#[test]
|
||||
fn style_objects_made_before_file_revert_go_stale_even_when_the_style_is_back() {
|
||||
let (mut s, _) = doc_with("Hello");
|
||||
s.execute("charStyle.new", &json!({"name": "B", "attrs": {"size": 30}})).unwrap();
|
||||
let path = std::env::temp_dir().join(format!("vc-styles-revert-{}.vectorcraft", std::process::id()));
|
||||
s.execute("document.save", &json!({"path": path.to_string_lossy()})).unwrap();
|
||||
let out = log(
|
||||
&mut s,
|
||||
&format!(
|
||||
"{DOC} var a = cs.getByName('B');\
|
||||
vc.run('shape.rectangle', {{x: 1, y: 1, width: 5, height: 5}}); vc.run('file.revert', {{}});\
|
||||
var cs2 = app.activeDocument.characterStyles;\
|
||||
try {{ vc.log(a.name); }} catch (e) {{ vc.log(e.message); }}\
|
||||
vc.log(cs2.getByName('B').name); vc.log(cs2.getByName('B') === a);"
|
||||
),
|
||||
);
|
||||
let _ = std::fs::remove_file(&path);
|
||||
assert!(out[0].as_str().is_some_and(|m| m.contains("Stale style")), "{out:?}");
|
||||
assert_eq!(out[1], json!("B"), "a style object made after the revert names the reverted style");
|
||||
assert_eq!(out[2], json!(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_run_names_at_most_4096_styles() {
|
||||
let (mut s, _) = doc_with("Hello");
|
||||
|
||||
@@ -96,10 +96,10 @@ const INSIDE: &[(&str, &str)] = &[
|
||||
("Document.", "prototype"),
|
||||
(
|
||||
"Document.prototype.",
|
||||
"activeLayer activeLayer= artboards artboards= close close= constructor convertCoordinate documentColorSpace \
|
||||
groupItems groupItems= height layers layers= name pageItems parent pathItems pathItems= rulerOrigin rulerOrigin= \
|
||||
rulerUnits saveAs saveAs= selection selection= swatches swatches= symbols symbols= textFrames typename \
|
||||
views views= width",
|
||||
"activeLayer activeLayer= artboards artboards= characterStyles close close= constructor convertCoordinate \
|
||||
documentColorSpace groupItems groupItems= height layers layers= name pageItems paragraphStyles parent pathItems \
|
||||
pathItems= rulerOrigin rulerOrigin= rulerUnits saveAs saveAs= selection selection= swatches swatches= symbols \
|
||||
symbols= textFrames typename views views= width",
|
||||
),
|
||||
("Layer.", "prototype"),
|
||||
("Layer.prototype.", "constructor name typename"),
|
||||
@@ -131,8 +131,9 @@ const INSIDE: &[(&str, &str)] = &[
|
||||
("ZOrderMethod.", "BRINGFORWARD BRINGTOFRONT SENDBACKWARD SENDTOBACK"),
|
||||
(
|
||||
"",
|
||||
"CharacterAttributes Characters Justification ParagraphAttributes Paragraphs TextFont TextFonts TextFrame TextFrames \
|
||||
TextOrientation TextRange TextType Words",
|
||||
"CharacterAttributes CharacterStyle CharacterStyleAttributes CharacterStyles Characters Justification \
|
||||
ParagraphAttributes ParagraphStyle ParagraphStyleAttributes ParagraphStyles Paragraphs TextFont TextFonts TextFrame \
|
||||
TextFrames TextOrientation TextRange TextType Words",
|
||||
),
|
||||
("TextFrames.", "prototype"),
|
||||
("TextFrames.prototype.", "add areaText constructor getByName index length parent pathText pointText removeAll typename"),
|
||||
@@ -150,11 +151,11 @@ const INSIDE: &[(&str, &str)] = &[
|
||||
(
|
||||
"TextRange.prototype.",
|
||||
"autoLeading autoLeading= baselineShift baselineShift= changeCaseTo changeCaseTo= characterAttributes characterOffset \
|
||||
characterOffset= characterStyles characterStyles= characters constructor contents contents= deSelect deSelect= \
|
||||
characterOffset= characterStyles characters constructor contents contents= deSelect deSelect= \
|
||||
duplicate duplicate= end end= getLocalCharOverridesJSON getLocalCharOverridesJSON= getLocalParaOverridesJSON \
|
||||
getLocalParaOverridesJSON= getParagraphLength getTextRunLength horizontalScale horizontalScale= insertionPoints \
|
||||
insertionPoints= kerning kerning= leading leading= length length= lines lines= move move= paragraphAttributes \
|
||||
paragraphStyles paragraphStyles= paragraphs parent remove rotation rotation= select select= size size= start start= \
|
||||
paragraphStyles paragraphs parent remove rotation rotation= select select= size size= start start= \
|
||||
story story= strikeThrough strikeThrough= textFont textFont= textRanges textRanges= textSelection textSelection= \
|
||||
tracking tracking= typename underline underline= verticalScale verticalScale= words",
|
||||
),
|
||||
@@ -177,6 +178,30 @@ const INSIDE: &[(&str, &str)] = &[
|
||||
"constructor firstLineIndent firstLineIndent= justification justification= leftIndent leftIndent= rightIndent \
|
||||
rightIndent= spaceAfter spaceAfter= spaceBefore spaceBefore= typename",
|
||||
),
|
||||
("CharacterStyles.", "prototype"),
|
||||
("CharacterStyles.prototype.", "add constructor getByName index length parent removeAll typename"),
|
||||
("CharacterStyle.", "prototype"),
|
||||
("CharacterStyle.prototype.", "applyTo characterAttributes constructor name name= parent remove typename"),
|
||||
("CharacterStyleAttributes.", "prototype"),
|
||||
(
|
||||
"CharacterStyleAttributes.prototype.",
|
||||
"autoLeading autoLeading= baselineShift baselineShift= constructor horizontalScale horizontalScale= leading leading= \
|
||||
rotation rotation= size size= strikeThrough strikeThrough= textFont textFont= tracking tracking= typename underline \
|
||||
underline= verticalScale verticalScale=",
|
||||
),
|
||||
("ParagraphStyles.", "prototype"),
|
||||
("ParagraphStyles.prototype.", "add constructor getByName index length parent removeAll typename"),
|
||||
("ParagraphStyle.", "prototype"),
|
||||
(
|
||||
"ParagraphStyle.prototype.",
|
||||
"applyTo characterAttributes characterAttributes= constructor name name= paragraphAttributes parent remove typename",
|
||||
),
|
||||
("ParagraphStyleAttributes.", "prototype"),
|
||||
(
|
||||
"ParagraphStyleAttributes.prototype.",
|
||||
"constructor firstLineIndent firstLineIndent= justification justification= leftIndent leftIndent= rightIndent \
|
||||
rightIndent= spaceAfter spaceAfter= spaceBefore spaceBefore= typename",
|
||||
),
|
||||
("TextFonts.", "prototype"),
|
||||
("TextFonts.prototype.", "constructor getByName index length parent typename"),
|
||||
("TextFont.", "prototype"),
|
||||
|
||||
+2
-1
@@ -2760,7 +2760,8 @@ app through the global object `vc`, and writes to the Script Console and runs ot
|
||||
paragraph the text touches. `clearingOverrides` left out is false. A range lists each style once, in the order
|
||||
its text first uses it, the Normal style standing for text in no other style, and its lists cannot add or remove
|
||||
styles. The Normal styles cannot be renamed or removed. A style object keeps naming its style after a rename made
|
||||
through `name`; after the style is removed, renamed another way or its document closed, it throws `Stale style`.
|
||||
through `name`; after the style is removed, renamed another way or its document closed or reverted, it throws
|
||||
`Stale style`.
|
||||
One run names at most 4,096 styles. `ParagraphStyle.characterAttributes` throws an error that says it is not
|
||||
available yet.
|
||||
- `$.write(...values)` writes the values to the Script Console with no separator, each as `String(value)` gives it, and
|
||||
|
||||
Reference in new issue
Block a user