From d6bfe68936f176213737b780143f5cea52569925 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Fri, 13 Nov 2026 16:51:10 -0500 Subject: [PATCH] M1: Paste with a layer in a frame selected puts the copy next to it (#2355) Cmd/Ctrl+V with one layer inside a frame selected pasted onto the page on top of the frame. It now pastes into the layer's parent just above it, in an auto layout frame the item after it. Page-level layers, several layers, instance children, board atoms, component sets and copied components keep today's behaviour. Co-Authored-By: Claude Sonnet 5.5 Model-Effort: Opus 5.5 drove; Opus 5.5 briefed; Sonnet 5.5 wrote at medium --- crates/engine/src/cmd/edit/clipboard.rs | 44 ++++++-- crates/engine/src/lib.rs | 2 + crates/engine/src/paste_sibling_tests.rs | 122 +++++++++++++++++++++++ docs/ui-parity.md | 5 +- 4 files changed, 164 insertions(+), 9 deletions(-) create mode 100644 crates/engine/src/paste_sibling_tests.rs diff --git a/crates/engine/src/cmd/edit/clipboard.rs b/crates/engine/src/cmd/edit/clipboard.rs index dbf4ec180..09c79190b 100644 --- a/crates/engine/src/cmd/edit/clipboard.rs +++ b/crates/engine/src/cmd/edit/clipboard.rs @@ -295,6 +295,9 @@ pub(super) fn paste(s: &mut Session, p: &Value) -> Result { }) .filter(|i| !copy_local || !s.clipboard.copied_components.iter().any(|root| doc.is_ancestor(*root, *i))) }); + // One layer selected that isn't a frame target: the copy joins its parent, just above it (#2355). + let sibling_of = if target.is_none() && id(p, "parent").is_none() { sibling_anchor(s, &sel, copy_local) } else { None }; + let target = target.or_else(|| sibling_of.and_then(|i| doc.node(i).and_then(|n| n.parent))); let parent = target.unwrap_or(page); let at = match (num(p, "x"), num(p, "y")) { (Some(x), Some(y)) => Some((x, y)), @@ -321,10 +324,42 @@ pub(super) fn paste(s: &mut Session, p: &Value) -> Result { } }; let ids = paste_into(s, parent, at)?; + if let Some(sel) = sibling_of { + let above = s.doc()?.index_in_parent(sel).map(|ix| ix + 1); + move_above(s, &ids, parent, above)?; + } s.set_selection(ids.clone()); Ok(json!({"ids": ids})) } +/// The one selected layer a plain paste goes next to: not a page-level layer, not inside an instance +/// (its children are fixed), not a board atom, a component set (a variant's parent) or inside a copied +/// component. Else the page, as before. +fn sibling_anchor(s: &Session, sel: &[NodeId], copy_local: bool) -> Option { + let [only] = sel else { return None }; + let doc = s.doc().ok()?; + let parent = doc.node(*only)?.parent?; + let pn = doc.node(parent)?; + let ok = !pn.is_page() + && pn.kind.is_frame_like() + && !pn.is_board_atom() + && !crate::components::inside_instance(doc, *only) + && !matches!(pn.kind, NodeKind::Instance { .. } | NodeKind::ComponentSet { .. }) + && (!copy_local || !s.clipboard.copied_components.iter().any(|root| doc.is_ancestor(*root, parent))); + ok.then_some(*only) +} + +/// Move the pasted copies that landed in `parent` to `above` and after, in the copied order. +fn move_above(s: &mut Session, ids: &[NodeId], parent: NodeId, above: Option) -> Result<()> { + let Some(above) = above else { return Ok(()) }; + let doc = s.doc_mut()?; + for (k, i) in ids.iter().filter(|i| doc.node(**i).is_some_and(|n| n.parent == Some(parent))).copied().collect::>().into_iter().enumerate() + { + doc.reorder(i, above + k)?; + } + Ok(()) +} + pub(super) fn paste_over(s: &mut Session, _p: &Value) -> Result { let sel = s.selection(); let page = s.page()?; @@ -337,14 +372,7 @@ pub(super) fn paste_over(s: &mut Session, _p: &Value) -> Result { None => (page, None, None), }; let ids = paste_into(s, parent, at)?; - if let Some(above) = above { - let doc = s.doc_mut()?; - for (k, i) in - ids.iter().filter(|i| doc.node(**i).is_some_and(|n| n.parent == Some(parent))).copied().collect::>().into_iter().enumerate() - { - doc.reorder(i, above + k)?; - } - } + move_above(s, &ids, parent, above)?; s.set_selection(ids.clone()); Ok(json!({"ids": ids})) } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 3c81b9147..91de56d61 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -474,6 +474,8 @@ mod panels_behaviour_tests; #[cfg(test)] mod paste_missing_image_tests; #[cfg(test)] +mod paste_sibling_tests; +#[cfg(test)] mod path_read_caps_tests; #[cfg(test)] mod pdf_print_tests; diff --git a/crates/engine/src/paste_sibling_tests.rs b/crates/engine/src/paste_sibling_tests.rs new file mode 100644 index 000000000..70174fff6 --- /dev/null +++ b/crates/engine/src/paste_sibling_tests.rs @@ -0,0 +1,122 @@ +//! Cmd/Ctrl+V with one layer inside a frame selected pastes next to it, in its parent (#2355). + +use serde_json::{Value, json}; + +use crate::Session; +use crate::params::id_of; +use figcraft_doc::NodeId; + +fn id(v: &Value) -> NodeId { + id_of(&v["id"]).unwrap() +} + +fn sess() -> Session { + Session::new() +} + +fn rect(s: &mut Session, parent: Option, x: f64, y: f64) -> NodeId { + let mut p = json!({"x": x, "y": y, "width": 40, "height": 20}); + if let Some(parent) = parent { + p["parent"] = json!(parent); + } + id(&s.execute("shape.rectangle", &p).unwrap()) +} + +fn kids(s: &Session, parent: NodeId) -> Vec { + s.doc().unwrap().node(parent).unwrap().children.clone() +} + +fn parent_of(s: &Session, n: NodeId) -> Option { + s.doc().unwrap().node(n).unwrap().parent +} + +fn paste(s: &mut Session) -> Vec { + s.execute("edit.paste", &json!({})).unwrap()["ids"].as_array().unwrap().iter().filter_map(id_of).collect() +} + +#[test] +fn paste_with_a_layer_in_an_auto_layout_frame_selected_goes_right_after_it() { + let mut s = sess(); + let a = rect(&mut s, None, 0.0, 0.0); + let b = rect(&mut s, None, 0.0, 30.0); + let r = s.execute("layout.add", &json!({"ids": [a, b], "direction": "vertical"})).unwrap(); + let frame = id_of(&r["ids"][0]).unwrap(); + s.execute("node.set", &json!({"id": frame, "props": {"gap": 10}})).unwrap(); + let h0 = s.doc().unwrap().node(frame).unwrap().height; + assert_eq!(h0, 50.0); + assert_eq!(kids(&s, frame), vec![a, b]); + s.set_selection(vec![b]); + s.execute("edit.copy", &json!({})).unwrap(); + let pasted = paste(&mut s); + assert_eq!(pasted.len(), 1); + assert_eq!(parent_of(&s, pasted[0]), Some(frame), "the copy joins the selected layer's frame"); + assert_eq!(kids(&s, frame), vec![a, b, pasted[0]], "right after the selected layer in the flow"); + assert_eq!(s.doc().unwrap().node(frame).unwrap().height, h0 + 20.0 + 10.0, "the frame grew by the copy's height plus the gap"); + assert_eq!(s.selection(), pasted); +} + +#[test] +fn paste_with_a_layer_in_a_plain_frame_selected_stays_in_the_frame_in_place() { + let mut s = sess(); + let frame = id(&s.execute("frame.create", &json!({"x": 0, "y": 0, "width": 200, "height": 200})).unwrap()); + let a = rect(&mut s, Some(frame), 10.0, 10.0); + let b = rect(&mut s, Some(frame), 100.0, 100.0); + s.set_selection(vec![a]); + s.execute("edit.copy", &json!({})).unwrap(); + let pasted = paste(&mut s); + assert_eq!(pasted.len(), 1); + assert_eq!(parent_of(&s, pasted[0]), Some(frame)); + assert_eq!(kids(&s, frame), vec![a, pasted[0], b], "just above the selected layer"); + let wb = s.doc().unwrap().world_bounds(pasted[0]).unwrap(); + assert_eq!((wb.x0, wb.y0), (10.0, 10.0), "at the selected layer's position"); + assert_eq!(s.selection(), pasted); +} + +#[test] +fn paste_with_a_top_level_layer_selected_is_unchanged() { + let mut s = sess(); + let a = rect(&mut s, None, 10.0, 10.0); + let page = parent_of(&s, a).unwrap(); + let before = kids(&s, page); + s.set_selection(vec![a]); + s.execute("edit.copy", &json!({})).unwrap(); + let pasted = paste(&mut s); + assert_eq!(parent_of(&s, pasted[0]), Some(page)); + let mut want = before; + want.push(pasted[0]); + assert_eq!(kids(&s, page), want, "on the page, on top, as before"); +} + +#[test] +fn paste_with_two_layers_selected_is_unchanged() { + let mut s = sess(); + let frame = id(&s.execute("frame.create", &json!({"x": 0, "y": 0, "width": 200, "height": 200})).unwrap()); + let a = rect(&mut s, Some(frame), 10.0, 10.0); + let b = rect(&mut s, Some(frame), 100.0, 100.0); + let page = parent_of(&s, frame).unwrap(); + s.set_selection(vec![a, b]); + s.execute("edit.copy", &json!({})).unwrap(); + let pasted = paste(&mut s); + assert_eq!(pasted.len(), 2); + assert!(pasted.iter().all(|p| parent_of(&s, *p) == Some(page)), "on the page, as before"); + assert_eq!(kids(&s, frame), vec![a, b]); +} + +#[test] +fn paste_with_a_layer_inside_an_instance_selected_is_unchanged() { + let mut s = sess(); + let f = id(&s.execute("frame.create", &json!({"x": 0, "y": 0, "width": 100, "height": 50, "name": "Button"})).unwrap()); + s.execute("text.create", &json!({"text": "OK", "x": 10, "y": 10, "parent": f})).unwrap(); + let c = id(&s.execute("component.create", &json!({"ids": [f]})).unwrap()); + let inst = id(&s.execute("instance.create", &json!({"component": c, "x": 200, "y": 0})).unwrap()); + let page = parent_of(&s, inst).unwrap(); + let inner = kids(&s, inst)[0]; + let inner_kids = kids(&s, inst); + let other = rect(&mut s, None, 500.0, 500.0); + s.set_selection(vec![other]); + s.execute("edit.copy", &json!({})).unwrap(); + s.set_selection(vec![inner]); + let pasted = paste(&mut s); + assert_eq!(parent_of(&s, pasted[0]), Some(page), "an instance's children are fixed: the copy goes to the page"); + assert_eq!(kids(&s, inst), inner_kids); +} diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 2ec810638..fcd4212c6 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -159,7 +159,9 @@ in the table. `crates/ui-egui/src/shortcut_keys_tests.rs` drives the keys a look presenting. The paint bucket's tooltip shows Shift+B (B still works). - **Fixed (2026-10-17):** Shift+Cmd/Ctrl+V (Paste over selection) and Alt+Cmd/Ctrl+V (Paste properties) work on the desktop (they were bound but didn't act there, #716); Cmd/Ctrl+V pastes an - image copied in another app (#663). Counts above unchanged (the suite checks bindings). + image copied in another app (#663). With one layer inside a frame selected, Cmd/Ctrl+V pastes + into its parent just above it (in an auto layout frame, the item after it), #2355. Counts above + unchanged (the suite checks bindings). - **Where the documentation disagrees with itself** (noted per row): pixel grid (Shift+' or Cmd+'), letter spacing (Alt or Cmd with < >; Cmd would be the font size keys, Alt bound), Windows remove fill (Alt+/ or Alt+,; Alt+, is letter spacing), the Layers panel on Windows (Alt+1 @@ -208,6 +210,7 @@ This audit records candidates for subsequent fixes; their behaviour is unchanged | Date | Change | Summary | |---|---|---| +| 2026-11-18 | patch | M1, #2355: Cmd/Ctrl+V with one layer inside a frame selected pasted the copy onto the page on top of the frame; it now goes into the layer's parent just above it (in an auto layout frame, the item after it, so the layout grows), keeping today's behaviour for a page-level layer, several layers, an instance's children and a board atom. Tests: `paste_sibling_tests` (5) | | 2026-11-08 | minor | M8, #2109: in vector edit mode the Align commands (`align.left` to `align.bottom`, Alt+A/H/D/W/V/S) and Distribute horizontal and vertical spacing act on the selected points when neither `ids` nor `id` is given (either one targets layers), as in Figma (measured in Figma on 2026-11-08): each point moves along one axis only, to its box's left, horizontal centre, right, top, vertical centre or bottom, and distribute spaces the points evenly between the outermost two in order along the axis; handles go with their vertex, a single point stays, with no point selected nothing moves (the layer is not aligned to its frame, as measured in Figma), and one undo restores them. The Design panel draws the alignment row first, with a Distribute menu from three points. Numbers unchanged. | | 2026-11-08 | patch | Number fields: scrubbing a label moves half a step a point, not a step, as measured in Figma (X from 100: +6 for a 12.2 px drag, +31 for 61.2 px; rotation 6° and opacity 5% for 12.2 px); Shift (x10), Alt (x0.1) and the speed by height are kept on top of it and were not measured, and no field's scrub direction changed | | 2026-11-17 | patch | M8: each page keeps its view in a session: going back to a page (the Pages list, `page.select`, Previous/Next page) shows it where it was left instead of fitting it again; a new page is still fitted, and going to the page shown moves nothing (`page_view_tests`) |