dvStudioExpandComponent function
DVPageNode?
dvStudioExpandComponent(
- DVPageNode instance, {
- DVPageDocument? lookup(
- String route
- int depth = 0,
The use instance drawn out: a copy of its component's tree with every
prop filled in -- the use's value, else the component's default -- or
null when the component is not there.
lookup finds a document by its address; the store's cache by default.
Components used inside the component are drawn out too, to a depth, so
one that uses itself stops rather than recursing forever.
Implementation
DVPageNode? dvStudioExpandComponent(
DVPageNode instance, {
DVPageDocument? Function(String route)? lookup,
int depth = 0,
}) {
final String? name = dvStudioComponentOf(instance);
if (name == null || depth >= _maxDepth) return null;
final DVPageDocument? Function(String route) find =
lookup ?? DVPageStore.cached;
final DVPageDocument? component = find(dvStudioComponentRoute(name));
if (component == null) return null;
final Map<String, Object?> given = dvStudioInstancePropsOf(instance);
final Map<String, Object?> values = <String, Object?>{
for (final DVStudioComponentProp prop in dvStudioComponentPropsOf(component))
prop.name: given.containsKey(prop.name) ? given[prop.name] : prop.value,
};
Object? fill(Object? value) {
if (value is! String || !value.contains('{{')) return value;
// A value that is one prop and nothing else takes the prop's value as it
// is; one with words around it has the words kept.
final RegExpMatch? whole = RegExp(r'^\{\{\s*(\w+)\s*\}\}$').firstMatch(value);
if (whole != null) return values[whole.group(1)];
return value.replaceAllMapped(
RegExp(r'\{\{\s*(\w+)\s*\}\}'),
(Match m) => '${values[m.group(1)] ?? ''}',
);
}
DVPageNode copy(DVPageNode node) {
if (node.type == dvStudioComponentType) {
// A component inside the component: its props may themselves come
// from this one's.
final DVPageNode inner = DVPageNode(
id: node.id,
type: node.type,
properties: <String, Object?>{
...node.properties,
'props': <String, Object?>{
for (final MapEntry<String, Object?> e
in dvStudioInstancePropsOf(node).entries)
e.key: fill(e.value),
},
},
);
return dvStudioExpandComponent(inner, lookup: find, depth: depth + 1) ??
DVPageNode(id: node.id, type: 'box');
}
final Map<String, Object?>? action = node.action;
final Map<String, Object?>? drawnAction =
action != null && action['type'] == 'prop'
? (values[action['name']] as Map?)?.cast<String, Object?>()
: action;
return DVPageNode(
id: node.id,
type: node.type,
layout: node.layout,
properties: <String, Object?>{
for (final MapEntry<String, Object?> e in node.properties.entries)
if (e.key != _propsKey) e.key: fill(e.value),
},
action: drawnAction,
breakpoints: <String, Map<String, Object?>>{
for (final MapEntry<String, Map<String, Object?>> b
in node.breakpoints.entries)
b.key: <String, Object?>{
for (final MapEntry<String, Object?> e in b.value.entries)
e.key: fill(e.value),
},
},
children: <DVPageNode>[for (final DVPageNode c in node.children) copy(c)],
);
}
return copy(component.root);
}