Merge pull request #2440 from B-uchi/pr/inspect-stored-keys

M8: get_node reports text and stroke settings it stores
This commit is contained in:
BFlat authored and GitHub committed 2026-11-18 19:46:38 +07:00
commit 18b4acfda9
3 files changed
+98 -1

No files matched your search

+39 -1
View File
@@ -1,6 +1,6 @@
//! Document inspection as JSON, so agents can verify their work without screenshots.
use figcraft_doc::{Document, Node, NodeId, NodeKind, StrokeJoin, TextData, TextDecoration};
use figcraft_doc::{Document, Node, NodeId, NodeKind, StrokeCap, StrokeJoin, TextAlignV, TextCase, TextData, TextDecoration};
use serde_json::{Value, json};
use crate::Session;
@@ -34,7 +34,10 @@ pub const NODE_KEYS: &[&str] = &[
"devResources",
"devStatus",
"effects",
"endCap",
"exports",
"exposedInstance",
"features",
"fills",
"flows",
"fontFamily",
@@ -76,13 +79,17 @@ pub const NODE_KEYS: &[&str] = &[
"opacity",
"operandOperation",
"overrides",
"paragraphIndent",
"paragraphSpacing",
"pluginData",
"pointCount",
"propertyReferences",
"rotation",
"sectionContentsHidden",
"segments",
"sizing",
"slot",
"startCap",
"strokeAlign",
"strokeJoin",
"strokeSides",
@@ -91,7 +98,9 @@ pub const NODE_KEYS: &[&str] = &[
"styledRuns",
"styles",
"textAlign",
"textAlignVertical",
"textAutoResize",
"textCase",
"textDecoration",
"textDecorationColor",
"textDecorationOffset",
@@ -102,6 +111,7 @@ pub const NODE_KEYS: &[&str] = &[
"textPath",
"textStyleRanges",
"type",
"variableModes",
"vertices",
"visible",
"width",
@@ -263,6 +273,11 @@ fn paint_props(n: &Node, o: &mut serde_json::Map<String, Value>, options: &Inspe
if !n.stroke.dashes.is_empty() {
insert_prop(o, options, "dashes", || json!(n.stroke.dashes));
}
for (k, cap) in [("startCap", n.stroke.cap_start), ("endCap", n.stroke.cap_end)] {
if cap != StrokeCap::None {
insert_prop(o, options, k, || json!(cap));
}
}
if let Some(cap) = n.stroke.dash_cap {
insert_prop(o, options, "dashCap", || json!(cap));
}
@@ -383,6 +398,15 @@ fn dev_props(doc: &Document, n: &Node, o: &mut serde_json::Map<String, Value>, o
if !n.bound.is_empty() {
insert_prop(o, options, "boundVariables", || json!(n.bound));
}
if !n.var_modes.is_empty() {
insert_prop(o, options, "variableModes", || json!(n.var_modes));
}
if !n.prop_refs.is_empty() {
insert_prop(o, options, "propertyReferences", || json!(n.prop_refs));
}
if n.exposed {
insert_prop(o, options, "exposedInstance", || json!(true));
}
if !n.overrides.is_empty() {
insert_prop(o, options, "overrides", || json!(n.overrides));
}
@@ -417,7 +441,21 @@ fn text_props(t: &TextData, o: &mut serde_json::Map<String, Value>, options: &In
insert_prop(o, options, "lineHeight", || json!(t.style.line_height.label()));
insert_prop(o, options, "letterSpacing", || json!(t.style.letter_spacing.label()));
insert_prop(o, options, "textAlign", || json!(t.align_h));
if t.align_v != TextAlignV::default() {
insert_prop(o, options, "textAlignVertical", || json!(t.align_v));
}
insert_prop(o, options, "textAutoResize", || json!(t.auto_resize));
if t.style.case != TextCase::default() {
insert_prop(o, options, "textCase", || json!(t.style.case));
}
for (k, v) in [("paragraphSpacing", t.paragraph_spacing), ("paragraphIndent", t.paragraph_indent)] {
if v != 0.0 {
insert_prop(o, options, k, || json!(v));
}
}
if !t.features.is_empty() {
insert_prop(o, options, "features", || json!(t.features));
}
if t.max_lines > 0 {
insert_prop(o, options, "maxLines", || json!(t.max_lines));
}
+56
View File
@@ -505,3 +505,59 @@ fn node_get_uses_options_but_keeps_its_existing_depth_and_selection() {
assert_eq!(s.selection(), selection);
assert_eq!(s.undo_depth(), history);
}
/// Text and stroke settings a layer stores are reported, under the names `node.set` takes, so a
/// value read can be written back unchanged.
#[test]
fn stored_text_and_stroke_settings_are_read_back_as_set() {
let mut s = Session::new();
s.execute("file.new", &json!({})).unwrap();
let text = s.execute("node.create", &json!({"type": "text", "text": "Hi", "width": 120})).unwrap()["id"].clone();
let line =
s.execute("node.create", &json!({"type": "line", "width": 80, "props": {"strokes": ["#000000"], "strokeWeight": 2}})).unwrap()["id"].clone();
let set = json!({
"textAlignVertical": "middle", "textCase": "upper", "paragraphSpacing": 8.0, "paragraphIndent": 12.0, "features": {"liga": false},
});
s.execute("node.set", &json!({"ids": [text.clone()], "props": set})).unwrap();
s.execute("node.set", &json!({"ids": [line.clone()], "props": {"startCap": "round", "endCap": "triangleArrow"}})).unwrap();
let got = s.execute("node.get", &json!({"id": text})).unwrap();
for (key, value) in set.as_object().unwrap() {
assert_eq!(&got[key], value, "{key}: {got}");
// Written back as read, it changes nothing.
s.execute("node.set", &json!({"ids": [text.clone()], "props": {key.as_str(): got[key].clone()}})).unwrap();
assert_eq!(s.execute("node.get", &json!({"id": text})).unwrap()[key], *value, "{key}");
}
let got = s.execute("node.get", &json!({"id": line})).unwrap();
assert_eq!((got["startCap"].as_str(), got["endCap"].as_str()), (Some("round"), Some("triangleArrow")), "{got}");
// Defaults stay out, as they do for other keys.
let plain = s.execute("node.create", &json!({"type": "text", "text": "x"})).unwrap()["id"].clone();
let got = s.execute("node.get", &json!({"id": plain})).unwrap();
for key in ["textAlignVertical", "textCase", "paragraphSpacing", "paragraphIndent", "features", "startCap", "endCap", "variableModes"] {
assert!(got.get(key).is_none(), "{key} at its default: {got}");
}
}
/// A frame's variable modes, a layer's component property bindings and an exposed nested instance
/// are reported.
#[test]
fn modes_property_bindings_and_exposed_instances_are_read() {
let mut s = Session::new();
s.execute("file.new", &json!({})).unwrap();
let c = s.execute("variable.createCollection", &json!({"name": "Theme", "modes": ["Light", "Dark"]})).unwrap();
let frame = s.execute("node.create", &json!({"type": "frame", "width": 100, "height": 100})).unwrap()["id"].clone();
s.execute("variable.setMode", &json!({"ids": [frame.clone()], "collection": c["id"], "mode": "Dark"})).unwrap();
let modes = s.execute("node.get", &json!({"id": frame})).unwrap()["variableModes"].clone();
assert_eq!(modes.as_object().map(|m| m.len()), Some(1), "{modes}");
let card = s.execute("node.create", &json!({"type": "frame", "name": "Card", "width": 100, "height": 50})).unwrap()["id"].clone();
let title = s.execute("node.create", &json!({"type": "text", "parent": card.clone(), "text": "Title"})).unwrap()["id"].clone();
let main = s.execute("component.create", &json!({"ids": [card]})).unwrap()["id"].clone();
s.execute("component.addProperty", &json!({"id": main.clone(), "name": "Label", "type": "text", "layer": title.clone()})).unwrap();
let refs = s.execute("node.get", &json!({"id": title})).unwrap()["propertyReferences"].clone();
assert!(refs.as_object().is_some_and(|r| r.values().any(|v| v.as_str().is_some_and(|p| p.starts_with("Label")))), "{refs}");
let tag = s.execute("node.create", &json!({"type": "frame", "name": "Tag", "x": 300, "width": 40, "height": 20})).unwrap()["id"].clone();
let tag_main = s.execute("component.create", &json!({"ids": [tag]})).unwrap()["id"].clone();
let nested = s.execute("instance.create", &json!({"component": tag_main})).unwrap()["id"].clone();
s.execute("node.reparent", &json!({"ids": [nested.clone()], "parent": main})).unwrap();
s.execute("component.exposeInstance", &json!({"id": nested.clone()})).unwrap();
assert_eq!(s.execute("node.get", &json!({"id": nested})).unwrap()["exposedInstance"], true);
}
+3
View File
@@ -195,6 +195,9 @@ layout (including grid `columnTracks` / `rowTracks`), sizing, `minWidth` / `maxW
children), `gridCell` (a grid auto layout child's column, row and spans), clip, text properties,
component/instance links, overrides, `slot` (a slot layer's property name), boundVariables, interactions, exports,
`devStatus` (handoff status), `devChanged` (changed since it was marked ready for dev),
`textAlignVertical`, `textCase`, `paragraphSpacing`, `paragraphIndent`, `features` (OpenType
toggles), `startCap` and `endCap` (when not none), `variableModes` (a frame's collection → mode),
`propertyReferences` (which component property controls a field), `exposedInstance`,
`annotations` (Developer Mode notes: `[{label, properties, category?}]`), `codeLinks` (a component's
links to code: `[{language, importPath, name, template}]`), `devResources` (links for developers,
its own and inherited: `[{name, url, index?, from?}]`), `measurements` (Developer Mode measurements