feat: Working on node builder

This commit is contained in:
2024-12-01 15:25:42 -05:00
parent ff49b12198
commit 96a1fac07b
11 changed files with 357 additions and 21 deletions

View File

@@ -0,0 +1,58 @@
public enum LabelContentStyle: Sendable {
case inline
case `default`
case custom(any NodeRepresentable)
}
public extension NodeRepresentable {
@inlinable
func labeledContentStyle(_ style: LabelContentStyle) -> any NodeRepresentable {
modifier(LabeledContentStyleModifier(style: style))
}
}
@usableFromInline
struct LabeledContentStyleModifier: NodeModifier {
@usableFromInline
let style: LabelContentStyle
@usableFromInline
init(style: LabelContentStyle) {
self.style = style
}
@usableFromInline
func render(_ node: any NodeRepresentable) -> any NodeRepresentable {
if let many = node as? _ManyNode {
var nodes = many.nodes
print("nodes: \(nodes)")
for (idx, node) in nodes.enumerated() {
if let labeled = node as? LabeledContent {
let newNode = labeled.applyingContentStyle(style)
nodes[idx] = newNode
}
}
return _ManyNode(nodes, separator: many.separator)
} else if let labeled = node as? LabeledContent {
return labeled.applyingContentStyle(style)
}
print("no many or labeled nodes found in: \(node)")
return node
}
}
private extension LabeledContent {
func applyingContentStyle(_ style: LabelContentStyle) -> Self {
switch style {
case .inline:
return .init(separator: " ") { label } content: { content }
case .default:
return .init(separator: "\n") { label } content: { content }
case let .custom(custom):
return .init(separator: custom) { label } content: { content }
}
}
}