diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 586deb604c..652f3e39b5 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -885,7 +885,7 @@ pub use colorcmds::{Backdrop, contrast_measure, contrast_pick_backdrop}; /// write a resolution and a number of bytes. pub use docinfo::{CATEGORIES, OBJECT_LABELS, byte_size, ppi_number}; pub use docmenu::guide_indexes; -pub(crate) use edit::{Unrecorded, artboard_tool_picks, settle_interaction, typing_in_progress}; +pub(crate) use edit::{Unrecorded, artboard_tool_picks, emptied_compounds, settle_interaction, typing_in_progress}; pub(crate) use object::bounds_rewrite; pub(crate) use object::{keep_aligned_in, refresh_pixel_aligned}; /// The rules `object.scale`, `object.reflect` and `object.shear` check their params with. diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 55726c31c9..ea5f688b44 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -1363,17 +1363,12 @@ fn clear(s: &mut Session, p: &Value) -> Result { } s.edit("Clear", |d, sel| { let clipped = clipped_ancestors(d, ids.iter().copied()); - let parents: Vec<_> = if keep_empty { - Vec::new() - } else { - ids.iter().filter_map(|id| d.parent_of(*id).filter(|p| matches!(d.node(*p).map(|n| &n.kind), Some(NodeKind::Compound { .. })))).collect() - }; + let emptied = if keep_empty { Vec::new() } else { emptied_compounds(d, &ids) }; + // A compound path inside another emptied one leaves with it. + let outer: Vec = + emptied.iter().copied().filter(|p| !d.ancestry(*p).unwrap_or_default().iter().rev().skip(1).any(|a| emptied.binary_search(a).is_ok())).collect(); d.remove_many(&ids)?; - for p in parents { - if d.node(p).is_some_and(|n| n.children().is_some_and(|c| c.is_empty())) { - d.remove(p)?; - } - } + d.remove_many(&outer)?; if with_guides { let gone = sel.guides.clone(); d.retain_guides(sel, |g| !g.id.is_some_and(|id| gone.contains(&id))); @@ -1390,6 +1385,27 @@ fn clear(s: &mut Session, p: &Value) -> Result { ok() } +/// The compound paths that deleting the objects `ids` together leaves with no members, sorted by +/// id: a compound path counts when each of its members is deleted or is such a compound path, so +/// a compound path holding only an emptied inner one is emptied too. +pub(crate) fn emptied_compounds(d: &Document, ids: &[NodeId]) -> Vec { + let mut gone: BTreeSet = ids.iter().copied().collect(); + let mut emptied = BTreeSet::new(); + loop { + let parents: BTreeSet = gone.iter().filter_map(|i| d.parent_of(*i)).filter(|p| !gone.contains(p)).collect(); + let found: BTreeSet = parents + .into_iter() + .filter(|p| d.node(*p).is_some_and(|n| matches!(n.kind, NodeKind::Compound { .. }))) + .filter(|p| d.children(Some(*p)).is_some_and(|k| k.iter().all(|n| gone.contains(&n.id)))) + .collect(); + if found.is_empty() { + return emptied.into_iter().collect(); + } + gone.extend(found.iter().copied()); + emptied.extend(found); + } +} + /// Does Clear remove direct-selected anchors without opening their paths (`keepJoined` param, else /// General › Delete Removes Anchor Points and Keeps Paths Joined)? Noted in the journal, so a /// replay removes them the same way whatever the preference is at the time. A saved step without `keepJoined` diff --git a/crates/engine/src/cmd/edit/tests.rs b/crates/engine/src/cmd/edit/tests.rs index 7e0471f0f5..8764d313b8 100644 --- a/crates/engine/src/cmd/edit/tests.rs +++ b/crates/engine/src/cmd/edit/tests.rs @@ -400,3 +400,59 @@ fn selected_roots_are_found_once_for_each_document_and_selection() { selected_roots(&s).unwrap(); assert_eq!(roots_found() - before, 2, "an edit finds the roots once again"); } + +/// Clear removes a compound path whose members are cleared paths and a compound path the clear +/// empties, whichever order the ids come in, and the tracing guard's set counts both as deleted. +#[test] +fn clear_removes_a_compound_path_emptied_through_an_emptied_inner_one() { + for reversed in [false, true] { + let mut s = new_doc(); + let outer_paths = [(); 2].map(|()| rect(&mut s)); + let (outer, outer_members) = compound(&mut s, &outer_paths); + let inner_paths = [(); 2].map(|()| rect(&mut s)); + let (inner, inner_members) = compound(&mut s, &inner_paths); + s.edit("Nest", |d, _| { + let taken = d.remove_many(&[inner])?; + d.insert(Some(outer), 1, (*taken[0]).clone())?; + Ok(()) + }) + .unwrap(); + let mut ids: Vec = outer_members.iter().chain(&inner_members).copied().collect(); + if reversed { + ids.reverse(); + } + let d = &s.doc().unwrap().doc; + assert_eq!(d.parent_of(inner), Some(outer), "the inner compound path is a member of the outer one"); + let want: Vec = BTreeSet::from([outer, inner]).into_iter().collect(); + assert_eq!(emptied_compounds(d, &ids), want, "reversed: {reversed}"); + run(&mut s, "edit.clear", json!({"ids": ids.iter().map(|id| id.0).collect::>()})); + let d = &s.doc().unwrap().doc; + assert!(d.node(inner).is_none(), "the emptied inner compound path goes (reversed: {reversed})"); + assert!(d.node(outer).is_none(), "the outer compound path it leaves empty goes too (reversed: {reversed})"); + } +} + +/// With a direct-selected anchor of another path in the selection, Clear still removes the outer +/// compound path that an emptied inner one leaves empty. +#[test] +fn clear_with_direct_anchors_removes_a_compound_path_emptied_through_an_emptied_inner_one() { + let mut s = new_doc(); + let outer_paths = [(); 2].map(|()| rect(&mut s)); + let (outer, outer_members) = compound(&mut s, &outer_paths); + let inner_paths = [(); 2].map(|()| rect(&mut s)); + let (inner, inner_members) = compound(&mut s, &inner_paths); + let other = rect(&mut s); + s.edit("Nest", |d, _| { + let taken = d.remove_many(&[inner])?; + d.insert(Some(outer), 1, (*taken[0]).clone())?; + Ok(()) + }) + .unwrap(); + let whole: Vec = outer_members.iter().chain(&inner_members).copied().collect(); + select_direct(&mut s, &whole, &[(other, two_anchors())]); + run(&mut s, "edit.clear", json!({})); + let d = &s.doc().unwrap().doc; + assert!(d.node(inner).is_none(), "the emptied inner compound path goes"); + assert!(d.node(outer).is_none(), "the outer compound path it leaves empty goes too"); + assert!(d.node(other).is_some(), "the path with anchors picked keeps its other sides"); +} diff --git a/crates/engine/src/cmd/path.rs b/crates/engine/src/cmd/path.rs index 190c8b5810..a3234c2264 100644 --- a/crates/engine/src/cmd/path.rs +++ b/crates/engine/src/cmd/path.rs @@ -1998,9 +1998,13 @@ pub(crate) fn delete_anchors_joined(s: &mut Session, p: &Value, keep_joined: boo } } emptied.finish(d)?; - for p in compounds { + // A compound path left empty by an inner one that went is checked again after it. + let mut compounds = compounds; + while let Some(p) = compounds.pop_first() { if d.node(p).is_some_and(|n| n.children().is_some_and(|c| c.is_empty())) { + let up = d.parent_of(p).filter(|u| matches!(d.node(*u).map(|n| &n.kind), Some(NodeKind::Compound { .. }))); d.remove(p)?; + compounds.extend(up); } } super::edit::release_empty_clips(d, clipped); diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index 6494a79ee7..511a8b95c2 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -3055,16 +3055,10 @@ fn makes_tracing(d: &Document, parent: NodeId, id: NodeId, at: Option) -> /// Whether deleting the objects `ids` together would leave an image first in an ordinary group /// named as a tracing, which would make that group one. `edit.clear` also deletes a compound path -/// it leaves empty, so such a compound path counts as deleted. +/// it leaves empty, nested ones included, so such a compound path counts as deleted. pub(super) fn clearing_makes_tracing(d: &Document, ids: &[NodeId]) -> bool { let mut gone: HashSet = ids.iter().copied().collect(); - let compounds: HashSet = - ids.iter().filter_map(|i| d.parent_of(*i)).filter(|p| matches!(d.node(*p).map(|n| &n.kind), Some(NodeKind::Compound { .. }))).collect(); - for c in compounds { - if d.children(Some(c)).is_some_and(|k| k.iter().all(|n| gone.contains(&n.id))) { - gone.insert(c); - } - } + gone.extend(crate::cmd::emptied_compounds(d, ids)); let parents: HashSet = gone.iter().filter_map(|i| d.parent_of(*i)).filter(|p| !gone.contains(p)).collect(); parents.into_iter().filter(|p| named_as_tracing(d, *p)).any(|p| { d.children(Some(p)).and_then(|c| c.iter().find(|n| !gone.contains(&n.id)).map(|n| matches!(n.kind, NodeKind::Image(_)))).unwrap_or(false)