diff --git a/frontend/src/app/main/data/workspace/transforms.cljs b/frontend/src/app/main/data/workspace/transforms.cljs index 3e5f8ced3a..bb3f100d87 100644 --- a/frontend/src/app/main/data/workspace/transforms.cljs +++ b/frontend/src/app/main/data/workspace/transforms.cljs @@ -1561,8 +1561,14 @@ ;; Shapes that already belong to the frame leave nothing to move (when (and (some? frame-id) (seq ids)) - (rx/of (dch/commit-changes changes) - (dwc/expand-collapse frame-id))) + (let [layout-ids (into #{} + (filter (partial cfh/has-layout? objects)) + (cons frame-id (map #(dm/get-in objects [% :parent-id]) ids)))] + (rx/concat + (rx/of (dch/commit-changes changes) + (dwc/expand-collapse frame-id)) + (when (seq layout-ids) + (rx/of (ptk/data-event :layout/update {:ids layout-ids})))))) (when add-component-to-variant? (rx/of (ev/event {::ev/name "add-component-to-variant"}))) (when add-new-variant? diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index 5802f9f6fb..e119d133c7 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -1741,12 +1741,15 @@ impl Shape { for st in structure { match st.entry_type { StructureEntryType::AddChild => { - if result.is_empty() { - result.insert(st.id); - } else { - let index = usize::min(result.len() - 1, st.index as usize); - result.shift_insert(index, st.id); - } + // Same as `d/insert-at-index`: the index counts the child + // at its old position. + let index = st.index as usize; + let index = match result.get_index_of(&st.id) { + Some(current) if current < index => usize::min(index - 1, result.len() - 1), + Some(_) => index, + None => usize::min(index, result.len()), + }; + result.shift_insert(index, st.id); } StructureEntryType::RemoveChild => { to_remove.insert(&st.id); @@ -2225,6 +2228,48 @@ mod tests { Shape::new(Uuid::nil()) } + fn add_child(shape: &mut Shape, id: Uuid, index: u32) { + shape.apply_structure(&vec![StructureEntry::new( + StructureEntryType::AddChild, + index, + Uuid::nil(), + id, + 0.0, + )]); + } + + #[test] + fn add_child_index_counts_the_child_at_its_old_position() { + let ids: Vec = (1..=4).map(|i| Uuid::from_u64_pair(0, i)).collect(); + let [a, b, c, d] = [ids[0], ids[1], ids[2], ids[3]]; + + let cases = [ + (b, 4, vec![a, c, d, b]), + (b, 3, vec![a, c, b, d]), + (b, 2, vec![a, b, c, d]), + (b, 1, vec![a, b, c, d]), + (d, 0, vec![d, a, b, c]), + ]; + + for (id, index, expected) in cases { + let mut shape = any_shape(); + shape.children = ids.clone(); + add_child(&mut shape, id, index); + assert_eq!(shape.children, expected, "move {id} to {index}"); + } + } + + #[test] + fn add_child_inserts_a_new_child_at_the_end() { + let ids: Vec = (1..=3).map(|i| Uuid::from_u64_pair(0, i)).collect(); + let new_id = Uuid::from_u64_pair(0, 9); + + let mut shape = any_shape(); + shape.children = ids.clone(); + add_child(&mut shape, new_id, 3); + assert_eq!(shape.children, [ids, vec![new_id]].concat()); + } + #[test] fn add_fill_pushes_a_new_fill() { let mut shape = any_shape();