mirror of
https://github.com/storytold/figcraft.git
synced 2026-12-04 19:41:51 -05:00
M4: Paste properties keeps a layer's styles and variables
Copy properties took the values and left behind where they come from: a frame filled from a colour variable or a fill style pasted a plain, unlinked colour, and the pasted layer's own bindings stayed, so a bound radius kept its variable's value over the pasted one. It now also carries the fill, stroke, effect, grid and text style links, the variables bound to what it copies (not sizes, auto layout spacing, visibility or text content) and a frame's layout grids. The pasted layer's bindings on those give way. A style or variable the target file lacks (a copy from another tab) is left out and its values pasted, so the paste never fails on a missing link. Layout grids coming along with a frame is not measured in Figma.
This commit is contained in:
@@ -417,7 +417,19 @@ pub(super) fn copy_props_of(doc: &Document, id: NodeId) -> Value {
|
||||
v["cornerRadius"] = json!(n.corners.radii);
|
||||
v["cornerSmoothing"] = json!(n.corners.smoothing);
|
||||
}
|
||||
// A frame's layout grids go with its look, as its fills do.
|
||||
if n.kind.is_frame_like() {
|
||||
v["grids"] = json!(n.grids);
|
||||
v["gridStyle"] = json!(n.styles.grid);
|
||||
}
|
||||
// The links behind the values: the styles and the variables they come from, so the pasted
|
||||
// layer follows them as the copied one does.
|
||||
v["fillStyle"] = json!(n.styles.fill);
|
||||
v["strokeStyle"] = json!(n.styles.stroke);
|
||||
v["effectStyle"] = json!(n.styles.effect);
|
||||
v["boundVariables"] = Value::Object(n.bound.iter().filter(|(f, _)| copied_binding(f)).map(|(f, v)| (f.clone(), json!(v))).collect());
|
||||
if let NodeKind::Text(t) = &n.kind {
|
||||
v["textStyle"] = json!(n.styles.text);
|
||||
v["fontFamily"] = json!(t.style.font_family);
|
||||
v["fontStyle"] = json!(t.style.font_style);
|
||||
v["fontSize"] = json!(t.style.font_size);
|
||||
@@ -439,17 +451,60 @@ pub(super) fn copy_props(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
/// properties only to text, the corners only to a layer with corners.
|
||||
pub(crate) fn apply_copied_props(cmd: &str, doc: &mut Document, id: NodeId, props: &Value) -> Result<()> {
|
||||
let mut m = props.as_object().cloned().unwrap_or_default();
|
||||
let is_text = matches!(doc.get(id)?.kind, NodeKind::Text(_));
|
||||
if !is_text {
|
||||
m.retain(|k, _| !k.starts_with("font") && k != "lineHeight" && k != "letterSpacing");
|
||||
let n = doc.get(id)?;
|
||||
let (is_text, corners, frame) = (matches!(n.kind, NodeKind::Text(_)), n.kind.has_corner_radius(), n.kind.is_frame_like());
|
||||
let takes = |k: &str| {
|
||||
let text = k.starts_with("font") || k.starts_with("paragraph") || matches!(k, "lineHeight" | "letterSpacing" | "textStyle");
|
||||
let corner = k == "cornerSmoothing" || k.ends_with("Radius");
|
||||
let grid = k == "gridStyle" || k == "grids" || k.starts_with("grids/");
|
||||
(is_text || !text) && (corners || !corner) && (frame || !grid)
|
||||
};
|
||||
m.retain(|k, _| takes(k));
|
||||
// A style or variable of another file (the copy came from another tab) isn't here to link to:
|
||||
// the values it gave are pasted without the link.
|
||||
let here = |v: &Value| v.as_str().is_none_or(|sid| doc.style(sid).is_some());
|
||||
m.retain(|k, v| !k.ends_with("Style") || k.starts_with("font") || here(v));
|
||||
if let Some(Value::Object(copied)) = m.get_mut("boundVariables") {
|
||||
copied.retain(|_, v| v.as_str().is_some_and(|vid| doc.variables.get_by_id(vid).is_some()));
|
||||
}
|
||||
if !doc.get(id)?.kind.has_corner_radius() {
|
||||
m.remove("cornerRadius");
|
||||
m.remove("cornerSmoothing");
|
||||
// The layer's own bindings on what is pasted give way to the copied layer's: kept, a binding
|
||||
// would hold the old value over the pasted one.
|
||||
if let Some(Value::Object(copied)) = m.get("boundVariables") {
|
||||
let mut bound: serde_json::Map<String, Value> =
|
||||
n.bound.keys().filter(|f| copied_binding(f) && takes(f)).map(|f| (f.clone(), Value::Null)).collect();
|
||||
bound.extend(copied.iter().filter(|(f, _)| takes(f)).map(|(f, v)| (f.clone(), v.clone())));
|
||||
m.insert("boundVariables".into(), Value::Object(bound));
|
||||
}
|
||||
crate::cmd::props::apply(cmd, doc, id, &m)
|
||||
}
|
||||
|
||||
/// Whether a variable bound to `field` goes with the properties [`copy_props_of`] copies: not a
|
||||
/// size, auto layout spacing, visibility or text content.
|
||||
fn copied_binding(field: &str) -> bool {
|
||||
!matches!(
|
||||
field,
|
||||
"width"
|
||||
| "height"
|
||||
| "minWidth"
|
||||
| "maxWidth"
|
||||
| "minHeight"
|
||||
| "maxHeight"
|
||||
| "gap"
|
||||
| "columnGap"
|
||||
| "rowGap"
|
||||
| "crossGap"
|
||||
| "padding"
|
||||
| "paddingHorizontal"
|
||||
| "paddingVertical"
|
||||
| "paddingTop"
|
||||
| "paddingRight"
|
||||
| "paddingBottom"
|
||||
| "paddingLeft"
|
||||
| "visible"
|
||||
| "characters"
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn paste_props(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let props = s.clipboard.props.clone().ok_or_else(|| EngineError::Disabled("edit.pasteProperties".into(), "no copied properties".into()))?;
|
||||
let ids = req_targets("edit.pasteProperties", s, p)?;
|
||||
|
||||
@@ -688,6 +688,8 @@ mod widgets_tests;
|
||||
#[cfg(test)]
|
||||
mod page_view_tests;
|
||||
#[cfg(test)]
|
||||
mod paste_properties_tests;
|
||||
#[cfg(test)]
|
||||
mod selection_reorder_tests;
|
||||
#[cfg(test)]
|
||||
mod selection_spacing_tests;
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
//! Copy properties and Paste properties (Alt+Cmd/Ctrl+C and V) carry what a layer's look comes
|
||||
//! from as well as its values: its colour, effect and grid styles, the variables bound to what is
|
||||
//! copied and a frame's layout grids. A link the file pasted into doesn't have is left out.
|
||||
|
||||
use figcraft_doc::{LayoutGrid, NodeId};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::Session;
|
||||
|
||||
fn id(v: Value) -> NodeId {
|
||||
NodeId(v["id"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
fn frame(s: &mut Session, x: f64) -> NodeId {
|
||||
id(s.execute("frame.create", &json!({"x": x, "y": 0, "width": 100, "height": 100})).unwrap())
|
||||
}
|
||||
|
||||
fn copy_onto(s: &mut Session, from: NodeId, to: NodeId) {
|
||||
s.execute("edit.copyProperties", &json!({"id": from})).unwrap();
|
||||
s.execute("edit.pasteProperties", &json!({"ids": [to]})).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_frame_pastes_its_styles_variables_and_layout_grids() {
|
||||
let mut s = Session::new();
|
||||
let (a, b, c) = (frame(&mut s, 0.0), frame(&mut s, 200.0), frame(&mut s, 400.0));
|
||||
s.execute("variable.createCollection", &json!({"name": "Tokens"})).unwrap();
|
||||
for (name, value) in [("surface", "#FF0000"), ("other", "#0000FF")] {
|
||||
s.execute("variable.create", &json!({"collection": "Tokens", "name": name, "type": "color", "value": value})).unwrap();
|
||||
}
|
||||
s.execute("variable.create", &json!({"collection": "Tokens", "name": "radius", "type": "number", "value": 16})).unwrap();
|
||||
s.execute("variable.bind", &json!({"ids": [a], "field": "fills/0/color", "variable": "surface"})).unwrap();
|
||||
s.execute("variable.bind", &json!({"ids": [a], "field": "cornerRadius", "variable": "radius"})).unwrap();
|
||||
s.execute("node.set", &json!({"ids": [a], "props": {"grids": [serde_json::to_value(LayoutGrid::default()).unwrap()]}})).unwrap();
|
||||
// B is bound to another colour, which the paste replaces.
|
||||
s.execute("variable.bind", &json!({"ids": [b], "field": "fills/0/color", "variable": "other"})).unwrap();
|
||||
let shadow = s.execute("style.create", &json!({"type": "effect", "name": "Shadow", "value": [{"type": "dropShadow"}]})).unwrap()["id"].clone();
|
||||
let green = s.execute("style.create", &json!({"type": "paint", "name": "Green", "value": "#00FF00"})).unwrap()["id"].clone();
|
||||
s.execute("style.apply", &json!({"ids": [c], "style": green})).unwrap();
|
||||
s.execute("style.apply", &json!({"ids": [c], "style": shadow})).unwrap();
|
||||
|
||||
copy_onto(&mut s, a, b);
|
||||
let (na, nb) = (s.doc().unwrap().node(a).unwrap().clone(), s.doc().unwrap().node(b).unwrap().clone());
|
||||
assert_eq!(nb.bound.get("fills/0/color"), na.bound.get("fills/0/color"), "the fill follows the same variable");
|
||||
assert_eq!(nb.bound.get("cornerRadius"), na.bound.get("cornerRadius"), "and so do the corners");
|
||||
assert_eq!(nb.corners.radii, [16.0; 4]);
|
||||
assert_eq!(nb.grids, na.grids, "a frame's layout grids come along");
|
||||
|
||||
copy_onto(&mut s, c, a);
|
||||
let (na, nc) = (s.doc().unwrap().node(a).unwrap().clone(), s.doc().unwrap().node(c).unwrap().clone());
|
||||
assert_eq!(na.styles.fill, nc.styles.fill, "the fill style is linked");
|
||||
assert!(nc.styles.effect.is_some() && na.styles.effect == nc.styles.effect, "the effect style too");
|
||||
assert!(!na.bound.contains_key("fills/0/color"), "the style replaces the fill's variable");
|
||||
assert!(!na.bound.contains_key("cornerRadius"), "C's corners aren't bound, so A's aren't any more");
|
||||
assert!(na.grids.is_empty(), "nor has it layout grids");
|
||||
assert_eq!(na.fills, nc.fills);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn links_another_file_lacks_paste_as_values() {
|
||||
let mut s = Session::new();
|
||||
let a = frame(&mut s, 0.0);
|
||||
s.execute("variable.createCollection", &json!({"name": "Tokens"})).unwrap();
|
||||
s.execute("variable.create", &json!({"collection": "Tokens", "name": "surface", "type": "color", "value": "#FF0000"})).unwrap();
|
||||
s.execute("variable.bind", &json!({"ids": [a], "field": "fills/0/color", "variable": "surface"})).unwrap();
|
||||
let st = s.execute("style.create", &json!({"type": "paint", "name": "Line", "value": "#00FF00"})).unwrap()["id"].clone();
|
||||
s.execute("node.set", &json!({"ids": [a], "props": {"strokes": [{"type": "solid", "color": "#000000"}]}})).unwrap();
|
||||
s.execute("style.apply", &json!({"ids": [a], "style": st, "target": "stroke"})).unwrap();
|
||||
s.execute("edit.copyProperties", &json!({"id": a})).unwrap();
|
||||
let copied = s.doc().unwrap().node(a).unwrap().clone();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let b = frame(&mut s, 0.0);
|
||||
s.execute("edit.pasteProperties", &json!({"ids": [b]})).unwrap();
|
||||
let nb = s.doc().unwrap().node(b).unwrap();
|
||||
assert_eq!((nb.fills.clone(), nb.strokes.clone()), (copied.fills, copied.strokes), "the values arrive");
|
||||
assert!(nb.bound.is_empty() && nb.styles.fill.is_none() && nb.styles.stroke.is_none(), "{:?} {:?}", nb.bound, nb.styles);
|
||||
}
|
||||
+2
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user