// // DocumentView.swift // GeminiRenderer // // Created by Shadowfacts on 7/12/20. // import SwiftUI import GeminiFormat public struct DocumentView: View { private let document: Document private let blocks: [RenderingBlock] private let changeURL: ((URL) -> Void)? private let scrollingEnabled: Bool public init(document: Document, scrollingEnabled: Bool = true, changeURL: ((URL) -> Void)? = nil) { self.document = document self.blocks = document.renderingBlocks self.changeURL = changeURL self.scrollingEnabled = scrollingEnabled } @ViewBuilder public var body: some View { if scrollingEnabled { ScrollView(.vertical) { scrollBody } } else { scrollBody } } private var scrollBody: some View { MaybeLazyVStack(alignment: .leading) { ForEach(blocks.indices) { (index) in RenderingBlockView(document: document, block: blocks[index], changeURL: changeURL) } }.padding([.leading, .trailing, .bottom]) } } struct DocumentView_Previews: PreviewProvider { static var doc: Document { Document(url: URL(string: "gemini://example.com")!, lines: [ .heading("Hello World", level: .h1), .text("Some text"), .preformattedToggle(alt: "blah"), .preformattedText("test"), .preformattedToggle(alt: nil), .quote("whatever"), .unorderedListItem("something") ]) } static var previews: some View { DocumentView(document: doc) } }