🐛 Keep the drop index when moving a child to the end of a flex layout (#13098)

Closes #12929

AI-assisted-by: claude-opus-5-5
This commit is contained in:
Elena Torró 2026-11-17 08:48:20 +01:00 committed by GitHub
parent 23468f9e4f
commit 1068421d0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 8 deletions

View File

@ -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?

View File

@ -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<Uuid> = (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<Uuid> = (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();