Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Feb 1, 2025
1 parent a4f6672 commit 4728ca3
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "f5a894bbdc3287a91c8c33f864cfb447314305f7fc66cabf1c369e8c3a67521d",
"originHash" : "3a6dfeefaa06e81f068454f6d3b99782d6b5b64dffd02869d95d3ca2528f5116",
"pins" : [
{
"identity" : "descriptors",
Expand All @@ -10,15 +10,6 @@
"version" : "0.2.3"
}
},
{
"identity" : "mondrianlayout",
"kind" : "remoteSourceControl",
"location" : "https://github.com/muukii/MondrianLayout.git",
"state" : {
"revision" : "5f00b13984fe08316fc5b5be06e2f41c14a3befa",
"version" : "0.10.0"
}
},
{
"identity" : "resultbuilderkit",
"kind" : "remoteSourceControl",
Expand Down
12 changes: 12 additions & 0 deletions Development/SwiftUIDemoApp/Component.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ import SwiftUI
.fill(.purple)
.frame(width: 100, height: 100)
}

#Preview("Circle2") {
Circle()
.fill(.purple)
.frame(width: 100, height: 100)
}

#Preview {
Circle()
.fill(.purple)
.frame(width: 100, height: 100)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ struct PreviewRegistryWrapper: Comparable {
var fileID: String { previewType.fileID }
var line: Int { previewType.line }
var column: Int { previewType.column }

@MainActor
var displayName: String? {
guard let rawPreview = try? previewType.makePreview() else {
return nil
}
let preview: FieldReader = .init(rawPreview)
return preview["displayName"]
}

@MainActor
var makeView: (@MainActor () -> any View) {
Expand Down
63 changes: 41 additions & 22 deletions Sources/StorybookKit/Primitives/Book.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,36 @@ public struct Book: BookView, Identifiable {
title: module,
contents: { [fileIDs = fileIDsByModule[module]!.sorted()] in
fileIDs.map { fileID in
return Node.page(
.init(
fileID,
0,
title: .init(fileID[fileID.index(after: module.endIndex)...]),
destination: { [registries = registriesByFileID[fileID]!] in
LazyVStack(
alignment: .center,
spacing: 16,
pinnedViews: .sectionHeaders
) {
ForEach.inefficient(items: registries) { registry in

let name = String(fileID[fileID.index(after: module.endIndex)...])
let registries = registriesByFileID[fileID]!

return Node.folder(.init(title: name, contents: {
registries.map { registry in

let pageName: String

if let displayName = registry.displayName {
pageName = "\(displayName)"
} else {
pageName = "line: \(registry.line)"
}

return Node.page(
.init(
fileID,
registry.line,
title: pageName,
usesScrollView: false,
destination: {
AnyView(registry.makeView())
}
}
}
)
)
)
)

}
}))

}
}
)
Expand All @@ -89,7 +101,8 @@ public struct Book: BookView, Identifiable {

let _contents = contents()

let folders = _contents
let folders =
_contents
.filter {
switch $0 {
case .folder:
Expand All @@ -102,7 +115,8 @@ public struct Book: BookView, Identifiable {
a.sortingKey < b.sortingKey
}

let pages = _contents
let pages =
_contents
.filter {
switch $0 {
case .folder:
Expand Down Expand Up @@ -160,7 +174,9 @@ public struct FolderBuilder {
public typealias Element = Book.Node

@MainActor
public static func buildExpression<Provider: BookProvider>(_ expression: Provider.Type) -> [FolderBuilder.Element] {
public static func buildExpression<Provider: BookProvider>(_ expression: Provider.Type)
-> [FolderBuilder.Element]
{
return [.page(expression.bookBody)]
}

Expand All @@ -184,7 +200,8 @@ public struct FolderBuilder {
[]
}

public static func buildBlock<C: Collection>(_ contents: C...) -> [Element] where C.Element == Element {
public static func buildBlock<C: Collection>(_ contents: C...) -> [Element]
where C.Element == Element {
return contents.flatMap { $0 }
}

Expand Down Expand Up @@ -212,11 +229,13 @@ public struct FolderBuilder {
return [element]
}

public static func buildExpression<C: Collection>(_ elements: C) -> [Element] where C.Element == Element {
public static func buildExpression<C: Collection>(_ elements: C) -> [Element]
where C.Element == Element {
Array(elements)
}

public static func buildExpression<C: Collection>(_ elements: C) -> [Element] where C.Element == Optional<Element> {
public static func buildExpression<C: Collection>(_ elements: C) -> [Element]
where C.Element == Element? {
elements.compactMap { $0 }
}

Expand Down
44 changes: 32 additions & 12 deletions Sources/StorybookKit/Primitives/BookPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,39 @@ public struct BookPage: BookView, Identifiable {
declarationIdentifier
}

public let usesScrollView: Bool
public let title: String
public let destination: AnyView
public nonisolated let declarationIdentifier: DeclarationIdentifier
private let fileID: any StringProtocol
private let line: any FixedWidthInteger
private let line: any FixedWidthInteger

public init<Destination: View>(
_ fileID: any StringProtocol = #fileID,
_ line: any FixedWidthInteger = #line,
title: String,
usesScrollView: Bool = true,
@ViewBuilder destination: @MainActor () -> Destination
) {
self.fileID = fileID
self.line = line
self.title = title
self.usesScrollView = usesScrollView
self.destination = AnyView(destination())
self.declarationIdentifier = .init()
}

public var body: some View {

NavigationLink {
ScrollView {
destination
Group {
if usesScrollView {
ScrollView {
destination
}
} else {
destination
}
}
.listStyle(.plain)
.navigationTitle(title)
Expand All @@ -88,16 +97,27 @@ public struct BookPage: BookView, Identifiable {
context?.onOpen(pageID: id)
})
} label: {
HStack {
Image.init(systemName: "doc")
VStack(alignment: .leading) {
Text(title)
Text("\(fileID):\(line)")
.font(.caption.monospacedDigit())
.opacity(0.8)
}
}
LinkLabel(title: title, fileID: fileID, line: line)
}

}
}

private struct LinkLabel: View {

let title: any StringProtocol
let fileID: any StringProtocol
let line: any FixedWidthInteger

var body: some View {
HStack {
Image.init(systemName: "doc")
VStack(alignment: .leading) {
Text(title)
Text("\(fileID):\(line)")
.font(.caption.monospacedDigit())
.opacity(0.8)
}
}
}
}

0 comments on commit 4728ca3

Please sign in to comment.