From 4728ca3e9efb63d777321dcc7b262f39058838ff Mon Sep 17 00:00:00 2001 From: Muukii Date: Sat, 1 Feb 2025 19:49:19 +0900 Subject: [PATCH] Update --- .../xcshareddata/swiftpm/Package.resolved | 11 +--- Development/SwiftUIDemoApp/Component.swift | 12 ++++ .../Preview/PreviewRegistryWrapper.swift | 9 +++ Sources/StorybookKit/Primitives/Book.swift | 63 ++++++++++++------- .../StorybookKit/Primitives/BookPage.swift | 44 +++++++++---- 5 files changed, 95 insertions(+), 44 deletions(-) diff --git a/Development/Storybook.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Development/Storybook.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 3a65b7d..ba16761 100644 --- a/Development/Storybook.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Development/Storybook.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "f5a894bbdc3287a91c8c33f864cfb447314305f7fc66cabf1c369e8c3a67521d", + "originHash" : "3a6dfeefaa06e81f068454f6d3b99782d6b5b64dffd02869d95d3ca2528f5116", "pins" : [ { "identity" : "descriptors", @@ -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", diff --git a/Development/SwiftUIDemoApp/Component.swift b/Development/SwiftUIDemoApp/Component.swift index 48479a0..cc789ac 100644 --- a/Development/SwiftUIDemoApp/Component.swift +++ b/Development/SwiftUIDemoApp/Component.swift @@ -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) +} diff --git a/Sources/StorybookKit/Internals/Preview/PreviewRegistryWrapper.swift b/Sources/StorybookKit/Internals/Preview/PreviewRegistryWrapper.swift index c94af2a..b2fe9e3 100644 --- a/Sources/StorybookKit/Internals/Preview/PreviewRegistryWrapper.swift +++ b/Sources/StorybookKit/Internals/Preview/PreviewRegistryWrapper.swift @@ -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) { diff --git a/Sources/StorybookKit/Primitives/Book.swift b/Sources/StorybookKit/Primitives/Book.swift index 642fc48..f28e103 100644 --- a/Sources/StorybookKit/Primitives/Book.swift +++ b/Sources/StorybookKit/Primitives/Book.swift @@ -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()) } - } - } - ) - ) + ) + ) + + } + })) + } } ) @@ -89,7 +101,8 @@ public struct Book: BookView, Identifiable { let _contents = contents() - let folders = _contents + let folders = + _contents .filter { switch $0 { case .folder: @@ -102,7 +115,8 @@ public struct Book: BookView, Identifiable { a.sortingKey < b.sortingKey } - let pages = _contents + let pages = + _contents .filter { switch $0 { case .folder: @@ -160,7 +174,9 @@ public struct FolderBuilder { public typealias Element = Book.Node @MainActor - public static func buildExpression(_ expression: Provider.Type) -> [FolderBuilder.Element] { + public static func buildExpression(_ expression: Provider.Type) + -> [FolderBuilder.Element] + { return [.page(expression.bookBody)] } @@ -184,7 +200,8 @@ public struct FolderBuilder { [] } - public static func buildBlock(_ contents: C...) -> [Element] where C.Element == Element { + public static func buildBlock(_ contents: C...) -> [Element] + where C.Element == Element { return contents.flatMap { $0 } } @@ -212,11 +229,13 @@ public struct FolderBuilder { return [element] } - public static func buildExpression(_ elements: C) -> [Element] where C.Element == Element { + public static func buildExpression(_ elements: C) -> [Element] + where C.Element == Element { Array(elements) } - public static func buildExpression(_ elements: C) -> [Element] where C.Element == Optional { + public static func buildExpression(_ elements: C) -> [Element] + where C.Element == Element? { elements.compactMap { $0 } } diff --git a/Sources/StorybookKit/Primitives/BookPage.swift b/Sources/StorybookKit/Primitives/BookPage.swift index 956f702..2d4d9d3 100644 --- a/Sources/StorybookKit/Primitives/BookPage.swift +++ b/Sources/StorybookKit/Primitives/BookPage.swift @@ -56,21 +56,24 @@ 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( _ 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() } @@ -78,8 +81,14 @@ public struct BookPage: BookView, Identifiable { public var body: some View { NavigationLink { - ScrollView { - destination + Group { + if usesScrollView { + ScrollView { + destination + } + } else { + destination + } } .listStyle(.plain) .navigationTitle(title) @@ -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) + } + } + } +}