mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-19 00:27:49 +00:00
Merge pull request #2439 from storytold/carry/2359
M1: Paste with a layer in a frame selected puts the copy next to it (#2355)
This commit is contained in:
4 files changed
+163
-8
No files matched your search
@@ -295,6 +295,9 @@ pub(super) fn paste(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
})
|
||||
.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<Value> {
|
||||
}
|
||||
};
|
||||
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<NodeId> {
|
||||
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<usize>) -> 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::<Vec<_>>().into_iter().enumerate()
|
||||
{
|
||||
doc.reorder(i, above + k)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn paste_over(s: &mut Session, _p: &Value) -> Result<Value> {
|
||||
let sel = s.selection();
|
||||
let page = s.page()?;
|
||||
@@ -337,14 +372,7 @@ pub(super) fn paste_over(s: &mut Session, _p: &Value) -> Result<Value> {
|
||||
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::<Vec<_>>().into_iter().enumerate()
|
||||
{
|
||||
doc.reorder(i, above + k)?;
|
||||
}
|
||||
}
|
||||
move_above(s, &ids, parent, above)?;
|
||||
s.set_selection(ids.clone());
|
||||
Ok(json!({"ids": ids}))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user