Gemini/GeminiRenderer/DocumentView.swift

61 lines
1.6 KiB
Swift
Raw Permalink Normal View History

2020-07-13 22:12:04 +00:00
//
// 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]
2020-07-15 03:15:56 +00:00
private let changeURL: ((URL) -> Void)?
2020-09-28 19:20:06 +00:00
private let scrollingEnabled: Bool
2020-07-13 22:12:04 +00:00
2020-09-28 19:20:06 +00:00
public init(document: Document, scrollingEnabled: Bool = true, changeURL: ((URL) -> Void)? = nil) {
2020-07-13 22:12:04 +00:00
self.document = document
self.blocks = document.renderingBlocks
2020-07-15 03:15:56 +00:00
self.changeURL = changeURL
2020-09-28 19:20:06 +00:00
self.scrollingEnabled = scrollingEnabled
2020-07-13 22:12:04 +00:00
}
2020-09-28 19:20:06 +00:00
@ViewBuilder
2020-07-13 22:12:04 +00:00
public var body: some View {
2020-09-28 19:20:06 +00:00
if scrollingEnabled {
ScrollView(.vertical) {
scrollBody
}
} else {
scrollBody
2020-07-13 22:12:04 +00:00
}
}
2020-09-28 19:20:06 +00:00
private var scrollBody: some View {
MaybeLazyVStack(alignment: .leading) {
ForEach(blocks.indices) { (index) in
2020-09-29 19:30:10 +00:00
RenderingBlockView(document: document, block: blocks[index], changeURL: changeURL)
2020-09-28 19:20:06 +00:00
}
}.padding([.leading, .trailing, .bottom])
}
2020-07-13 22:12:04 +00:00
}
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)
}
}