mirror of
https://github.com/storytold/figcraft.git
synced 2026-11-27 17:01:38 +01:00
M8: design context gives Code Connect links as the connect file does
`inspect.designContext` wrote a component's links through `camel`, which renames keys at every depth, so a mapped design value such as `extra_large` came out as `extraLarge`. Its components and the code it gives each connected layer now use Code Connect's own link JSON: value names stay as written, and the code carries the link's `source` and `notes`. Export also sorts its links once, after merging the file's other links.
This commit is contained in:
1 parent
d23c8b6456
commit
dbccabce41
4 files changed
+46
-9
No files matched your search
@@ -407,3 +407,29 @@ fn a_join_stopped_at_its_budget_says_so() {
|
||||
let (some, complete) = crate::design_context::referenced(s.doc().unwrap(), &start, &none, 3);
|
||||
assert_eq!((some.len(), complete), (3, false), "{some:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn code_links_keep_their_value_names_and_carry_source_and_notes() {
|
||||
let mut s = session();
|
||||
let variants: Vec<_> = ["small", "extra_large"]
|
||||
.iter()
|
||||
.map(|v| {
|
||||
let f = node(&mut s, json!({"type": "frame", "name": format!("Size={v}"), "width": 80, "height": 32}));
|
||||
id(&s.execute("component.create", &json!({"ids": [f]})).unwrap())
|
||||
})
|
||||
.collect();
|
||||
let set = id(&s.execute("component.combineVariants", &json!({"ids": variants})).unwrap());
|
||||
s.execute(
|
||||
"codeConnect.set",
|
||||
&json!({"id": set, "name": "Button", "template": "<Button size={Size} />", "source": "src/button.tsx",
|
||||
"notes": "Use for main actions", "values": {"Size": {"extra_large": "xl"}}}),
|
||||
)
|
||||
.unwrap();
|
||||
let instance = id(&s.execute("instance.create", &json!({"component": variants[1]})).unwrap());
|
||||
let v = context(&mut s, json!({"ids": [instance]}));
|
||||
let link = &v["components"][set.to_string()]["codeLinks"][0];
|
||||
assert_eq!(link["values"]["Size"]["extra_large"], "xl", "a design value keeps its name: {link}");
|
||||
let code = &only(&v)["code"][0];
|
||||
assert_eq!((code["source"].as_str(), code["notes"].as_str()), (Some("src/button.tsx"), Some("Use for main actions")), "{code}");
|
||||
assert!(code["code"].as_str().unwrap().contains("xl"), "{code}");
|
||||
}
|
||||
@@ -601,7 +601,15 @@ pub fn variants(doc: &Document, owner: NodeId) -> Vec<(String, Vec<String>)> {
|
||||
|
||||
/// A link as the connect file holds it.
|
||||
fn link_json(component: &str, l: &CodeLink) -> Value {
|
||||
let mut v = json!({"component": component, "language": l.language, "importPath": l.import_path, "name": l.name, "template": l.template});
|
||||
let mut v = link_fields(l);
|
||||
v["component"] = json!(component);
|
||||
v
|
||||
}
|
||||
|
||||
/// A link's own fields, as the connect file and `inspect.designContext` give them: its keys are
|
||||
/// the file's, and a mapping's property names and values stay as written.
|
||||
pub(crate) fn link_fields(l: &CodeLink) -> Value {
|
||||
let mut v = json!({"language": l.language, "importPath": l.import_path, "name": l.name, "template": l.template});
|
||||
if !l.source.is_empty() {
|
||||
v["source"] = json!(l.source);
|
||||
}
|
||||
@@ -633,10 +641,7 @@ fn entry(doc: &Document, id: NodeId, root: Option<&str>) -> Option<Value> {
|
||||
.iter()
|
||||
.map(|l| {
|
||||
let lang = CodeLang::from_id(&l.language);
|
||||
let mut v = link_json(&n.name, l);
|
||||
if let Some(o) = v.as_object_mut() {
|
||||
o.remove("component");
|
||||
}
|
||||
let mut v = link_fields(l);
|
||||
v["import"] = json!(lang.map(|k| import_line(l, k)));
|
||||
v["snippet"] = json!(lang.and_then(|k| connected_code(doc, id, k, l)));
|
||||
if let Some(root) = root.filter(|_| !l.source.is_empty()) {
|
||||
@@ -746,7 +751,6 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
doc.nodes().filter(|n| owner(doc, n.id) == Some(n.id)).flat_map(|n| n.code_links.iter().map(|l| link_json(&n.name, l))).collect();
|
||||
let key =
|
||||
|v: &Value| (v["component"].as_str().unwrap_or_default().to_string(), v["language"].as_str().unwrap_or_default().to_string());
|
||||
links.sort_by_key(key);
|
||||
let count = links.len();
|
||||
// The file also holds the project's settings and the links of other designs: only
|
||||
// the links of this file's components are this command's.
|
||||
|
||||
@@ -178,7 +178,14 @@ fn code(doc: &Document, id: NodeId) -> Vec<Value> {
|
||||
.filter_map(|l| {
|
||||
let lang = CodeLang::from_id(&l.language)?;
|
||||
let snippet = code_connect::connected_code(doc, id, lang, l)?;
|
||||
Some(json!({"language": l.language, "importPath": l.import_path, "name": l.name, "code": snippet}))
|
||||
// The snippet already holds the template and the values it maps.
|
||||
let mut v = code_connect::link_fields(l);
|
||||
if let Some(o) = v.as_object_mut() {
|
||||
o.remove("template");
|
||||
o.remove("values");
|
||||
}
|
||||
v["code"] = json!(snippet);
|
||||
Some(v)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -355,7 +362,7 @@ fn components_json(doc: &Document, ids: &BTreeSet<NodeId>) -> Value {
|
||||
v["description"] = json!(description);
|
||||
}
|
||||
if !n.code_links.is_empty() {
|
||||
v["codeLinks"] = camel(serde_json::to_value(&n.code_links).unwrap_or(Value::Null));
|
||||
v["codeLinks"] = json!(n.code_links.iter().map(code_connect::link_fields).collect::<Vec<_>>());
|
||||
}
|
||||
out.insert(id.to_string(), v);
|
||||
}
|
||||
|
||||
+1
-1
@@ -450,7 +450,7 @@ Each layer in `nodes` has `id`, `name`, `type` and:
|
||||
with the `name` of each. `explicit` holds the value they resolved to.
|
||||
- `unsupported`: `{property, why}` for what has no direct CSS equivalent. The value stays in `explicit`.
|
||||
- `code`: for a component or instance connected to code, per language, `{language, importPath, name,
|
||||
code}`, with the instance's own values in `code`. A connected instance stops there: `childCount`
|
||||
source?, notes?, code}`, with the instance's own values in `code`. A connected instance stops there: `childCount`
|
||||
says what it holds, and `get_node` reads it.
|
||||
- `children`, or `childCount` where the tree stops.
|
||||
|
||||
|
||||
Reference in new issue
Block a user