From 39a0fd69236dc6ee77b91666d4ef4c819539e676 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 30 Nov 2026 12:01:00 -0800 Subject: [PATCH] Scripts: objectOnly paint, empty closed paths and style applyTo keep their rules - An objectOnly paint during an open interaction also restores the paint waiting for the commit, so it no longer reaches Recent Colors. - setEntirePath([]) on a closed path keeps an empty closed subpath, so the path stays closed. - A character or paragraph style's applyTo throws Cannot edit, with nothing changed, on a hidden or locked text frame or one on a locked artboard, as other text edits do. --- crates/engine/src/cmd/paint.rs | 4 ++-- crates/engine/src/script/dom/paths.rs | 2 +- crates/engine/src/script/dom/text/styles.rs | 4 +++- crates/engine/src/tests/editcolors.rs | 15 +++++++++++++++ crates/engine/src/tests/script_dom_paths.rs | 2 +- crates/engine/src/tests/script_text.rs | 3 +++ docs/mcp.md | 3 ++- 7 files changed, 27 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/paint.rs b/crates/engine/src/cmd/paint.rs index 726aada285..26951ce41c 100644 --- a/crates/engine/src/cmd/paint.rs +++ b/crates/engine/src/cmd/paint.rs @@ -955,9 +955,9 @@ pub(crate) fn object_only(s: &mut Session, p: &Value, cmd: &str, run: impl FnOnc if (!names(p, "ids") && !names(p, "id")) || asks_defaults_only(p) { return Err(bad(cmd, "objectOnly needs ids or id and cannot be combined with defaultsOnly")); } - let kept = (s.paint.clone(), s.fill_active, s.last_solid, s.last_gradient.clone(), s.recent_colors.clone()); + let kept = (s.paint.clone(), s.fill_active, s.last_solid, s.last_gradient.clone(), s.recent_colors.clone(), s.pending_paint.clone()); let result = run(s); - (s.paint, s.fill_active, s.last_solid, s.last_gradient, s.recent_colors) = kept; + (s.paint, s.fill_active, s.last_solid, s.last_gradient, s.recent_colors, s.pending_paint) = kept; if result.is_ok() { s.note_defaults_only(); } diff --git a/crates/engine/src/script/dom/paths.rs b/crates/engine/src/script/dom/paths.rs index 7785dc25a8..5d2cfdd52f 100644 --- a/crates/engine/src/script/dom/paths.rs +++ b/crates/engine/src/script/dom/paths.rs @@ -857,7 +857,7 @@ fn path_set_entire(h: &super::Host, a: &[Data]) -> Answer { let c = h.dom.coords.get(); let anchors: Vec = script.iter().map(|p| in_canvas(f.to_document(c, *p), "each point").map(Anchor::corner)).collect::>()?; - let subs = if anchors.is_empty() { Vec::new() } else { vec![SubPath::new(anchors, closed)] }; + let subs = if anchors.is_empty() && !closed { Vec::new() } else { vec![SubPath::new(anchors, closed)] }; write_path(h, item, subs)?; cut_points(h, item); Ok(Data::Undefined) diff --git a/crates/engine/src/script/dom/text/styles.rs b/crates/engine/src/script/dom/text/styles.rs index e9121eee6b..45d1a428f3 100644 --- a/crates/engine/src/script/dom/text/styles.rs +++ b/crates/engine/src/script/dom/text/styles.rs @@ -342,7 +342,8 @@ fn target_span(h: &Host, d: &Data) -> std::result::Result { span_of(h, d, &TEXT) } -/// `applyTo(textItem, clearingOverrides?)`. +/// `applyTo(textItem, clearingOverrides?)`. Throws `Cannot edit`, with nothing changed, when the +/// text frame is protected as for any other text edit. fn style_apply_to(h: &Host, a: &[Data]) -> Answer { let style = this_style(h, a)?; let target = target_span(h, arg(a, 1))?; @@ -355,6 +356,7 @@ fn style_apply_to(h: &Host, a: &[Data]) -> Answer { return Err(HostError::Error("the text is in another document than the style".into())); } let (index, s, e, _) = resolve(h, target)?; + super::refuse_protected(h, target.item, "TextFrame")?; h.charge_call(16 + (e - s) as u64)?; let (start, end) = if style.para { let (_, ba, bb) = para_range(&index, s, e); diff --git a/crates/engine/src/tests/editcolors.rs b/crates/engine/src/tests/editcolors.rs index a42f2c4ca7..1539ece240 100644 --- a/crates/engine/src/tests/editcolors.rs +++ b/crates/engine/src/tests/editcolors.rs @@ -70,6 +70,21 @@ fn balance_previews_and_commits_one_step() { assert_eq!(fill_of(&s, a).to_hex(), "#808080"); } +/// An `objectOnly` paint during an open interaction keeps the remembered colors: the paint it would +/// leave waiting for the commit is not added to Recent Colors when the interaction commits. +#[test] +fn object_only_paint_in_an_interaction_keeps_recent_colors() { + let mut s = session(); + let a = rect(&mut s, json!("#808080")); + s.recent_colors.clear(); + let recent = (s.recent_colors.clone(), s.last_solid); + s.begin_interaction("Adjust Colors").unwrap(); + s.execute("paint.setFill", &json!({"ids": [a.0], "color": "#ff0000", "objectOnly": true})).unwrap(); + assert_eq!(fill_of(&s, a).to_hex(), "#ff0000"); + s.commit_interaction().unwrap(); + assert_eq!((s.recent_colors.clone(), s.last_solid), recent, "the recent colors are as they were"); +} + /// Saturate needs its intensity: a call without one, or with one that is not a number, is an error /// that changes nothing and records no undo step. A number outside −100..100 counts as its nearest end. #[test] diff --git a/crates/engine/src/tests/script_dom_paths.rs b/crates/engine/src/tests/script_dom_paths.rs index 93f2aaadf9..c0beeeb0a4 100644 --- a/crates/engine/src/tests/script_dom_paths.rs +++ b/crates/engine/src/tests/script_dom_paths.rs @@ -124,7 +124,7 @@ fn mixed_anchor_and_segment_picks_keep_independent_anchors() { #[test] fn an_empty_path_keeps_closed_before_points_are_added() { - for add in ["p.pathPoints.add(); p.pathPoints.add();", "p.setEntirePath([[0, 0], [10, 0], [10, 10]]);"] { + for add in ["p.pathPoints.add(); p.pathPoints.add();", "p.setEntirePath([[0, 0], [10, 0], [10, 10]]);", "p.setEntirePath([]);"] { let (mut s, _, _) = doc(); let start = s.journal.len(); let out = log( diff --git a/crates/engine/src/tests/script_text.rs b/crates/engine/src/tests/script_text.rs index a18184d7ce..af5bc55ea0 100644 --- a/crates/engine/src/tests/script_text.rs +++ b/crates/engine/src/tests/script_text.rs @@ -348,6 +348,9 @@ fn hidden_or_locked_type_is_not_edited() { "r.contents = 'xyz';", "r.characters.add('!');", "r.justification = Justification.CENTER;", + "var b = app.activeDocument.characterStyles.add('Big'); b.characterAttributes.size = 30; b.applyTo(r);", + "var b = app.activeDocument.characterStyles.add('Big'); b.characterAttributes.size = 30; b.applyTo(t);", + "var c = app.activeDocument.paragraphStyles.add('C'); c.paragraphAttributes.justification = Justification.CENTER; c.applyTo(r);", ] { // Each case starts from a fresh frame, so the hidden cases test a frame that is only hidden. let (mut s, id) = doc_with("abc"); diff --git a/docs/mcp.md b/docs/mcp.md index 555e456e3b..49d76671f4 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3292,7 +3292,8 @@ through `$`: style's old value. `applyTo` takes a text range, a text frame or its characters, words or paragraphs, in the style's document; a character style on an empty range inside text changes nothing, on empty text it styles the text typed or set later, and a paragraph style styles every paragraph the text touches. `clearingOverrides` left - out is false. A range lists each style once, in the order + out is false. `applyTo` on a frame that is hidden or locked, in a hidden or locked layer or group, or on a locked + artboard throws `Cannot edit` with nothing changed. 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 or reverted, it throws