Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Feb 3, 2025
1 parent c442f14 commit c932544
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 32 deletions.
82 changes: 58 additions & 24 deletions Sources/StorybookKit/Primitives/Book.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public struct Book: BookView, Identifiable {

public let title: String
public let contents: [Node]

@State private var isExpandedAll: Bool = false

public init(
title: String,
Expand Down Expand Up @@ -78,23 +80,34 @@ public struct Book: BookView, Identifiable {
}

public var body: some View {
ForEach(contents) { node in

NodeOutlineGroup(node, children: \.children) { content in
switch content {
case .folder(let folder):

HStack {
Image.init(systemName: "folder.fill")
Text(folder.title)
Section {
ForEach(contents) { node in
NodeOutlineGroup(expandsAll: isExpandedAll, node, children: \.children) { content in
switch content {
case .folder(let folder):

HStack {
Image.init(systemName: "folder.fill")
Text(folder.title)
}

case .page(let page):
page
}

case .page(let page):
page
}

}
} header: {
HStack {
Text(title)
Spacer()
Button("Expand All") {
isExpandedAll.toggle()
}
.font(.caption)
}

}

}

func allPages() -> [BookPage] {
Expand Down Expand Up @@ -261,31 +274,52 @@ private struct NodeOutlineGroup<Node, Content>: View where Node: Identifiable, C
let node: Node
let childKeyPath: KeyPath<Node, [Node]?>

@State var isExpanded: Bool = true
let expandsAll: Bool

@State var isExpanded: Bool

let content: (Node) -> Content

init(
expandsAll: Bool,
_ node: Node, children childKeyPath: KeyPath<Node, [Node]?>,
@ViewBuilder content: @escaping (Node) -> Content
) {
self._isExpanded = .init(wrappedValue: expandsAll)
self.expandsAll = expandsAll
self.node = node
self.childKeyPath = childKeyPath
self.content = content
}

var body: some View {
if node[keyPath: childKeyPath] != nil {
DisclosureGroup(
isExpanded: $isExpanded,
content: {
ForEach(node[keyPath: childKeyPath]!) { childNode in
NodeOutlineGroup(childNode, children: childKeyPath, content: content)
Group {
if node[keyPath: childKeyPath] != nil {
DisclosureGroup(
isExpanded: $isExpanded,
content: {
ForEach(node[keyPath: childKeyPath]!) { childNode in
NodeOutlineGroup(
expandsAll: expandsAll,
childNode,
children: childKeyPath,
content: content
)
}
},
label: {
content(node)
}
},
label: { content(node) })
} else {
content(node)
)

} else {
content(node)
}
}
.onChange(of: expandsAll) { value in
withAnimation(.default) {
isExpanded = value
}
}
}
}
7 changes: 6 additions & 1 deletion Sources/StorybookKit/Primitives/BookPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ public struct BookPage: BookView, Identifiable {
})
} label: {
LinkLabel(title: title, fileID: fileID, line: line)
.contextMenu(menuItems: {
Text(title)
}) {
destination
}
}

}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/StorybookKit/Storybook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public struct Storybook: View {
public var body: some View {
StorybookDisplayRootView(
bookStore: .init(
book: .init(title: "Storybook") {
book: Book.init(title: "Contents") {

if let nodes = Book.allBookPreviews() {
Book(title: "#Preview macro") {
Book(title: "#Preview") {
nodes
}
}
Expand Down
6 changes: 1 addition & 5 deletions Sources/StorybookKit/StorybookDisplayRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ struct BookContainer: View {
Text("History")
}

Section {
store.book
} header: {
Text("Contents")
}
store.book

}
.navigationTitle(store.title)
Expand Down

0 comments on commit c932544

Please sign in to comment.