2020-07-13 22:12:04 +00:00
|
|
|
//
|
|
|
|
// RenderingBlockView.swift
|
|
|
|
// GeminiRenderer
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 7/13/20.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import GeminiFormat
|
|
|
|
|
|
|
|
struct RenderingBlockView: View {
|
2020-09-29 19:30:10 +00:00
|
|
|
let document: Document
|
2020-07-13 22:12:04 +00:00
|
|
|
let block: RenderingBlock
|
2020-07-15 03:15:56 +00:00
|
|
|
let changeURL: ((URL) -> Void)?
|
2020-07-16 02:52:15 +00:00
|
|
|
@State var hovering = false
|
2020-07-15 03:15:56 +00:00
|
|
|
|
2020-09-29 19:30:10 +00:00
|
|
|
init(document: Document, block: RenderingBlock, changeURL: ((URL) -> Void)? = nil) {
|
|
|
|
self.document = document
|
2020-07-15 03:15:56 +00:00
|
|
|
self.block = block
|
|
|
|
self.changeURL = changeURL
|
|
|
|
}
|
2020-07-13 22:12:04 +00:00
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
var body: some View {
|
|
|
|
switch block {
|
|
|
|
case let .text(text):
|
2020-07-16 02:46:48 +00:00
|
|
|
self.text(text)
|
2020-07-13 22:12:04 +00:00
|
|
|
case let .link(url, text: linkText):
|
2020-07-16 02:46:48 +00:00
|
|
|
self.link(url, text: linkText)
|
2020-07-13 22:12:04 +00:00
|
|
|
case let .preformatted(text, alt: _):
|
2020-07-16 02:46:48 +00:00
|
|
|
preformatted(text)
|
2020-07-13 22:12:04 +00:00
|
|
|
case let .heading(text, level: level):
|
2020-07-16 02:46:48 +00:00
|
|
|
heading(text, level: level)
|
2020-07-13 22:12:04 +00:00
|
|
|
case let .unorderedListItem(text):
|
2020-07-16 02:46:48 +00:00
|
|
|
unorderedListItem(text)
|
2020-07-13 22:12:04 +00:00
|
|
|
case let .quote(text):
|
2020-07-16 02:46:48 +00:00
|
|
|
quote(text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func text(_ text: String) -> some View {
|
|
|
|
Text(text)
|
|
|
|
.font(.documentBody)
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func link(_ url: URL, text: String?) -> some View {
|
|
|
|
let text = text ?? url.absoluteString
|
2020-07-16 03:45:37 +00:00
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
let buttonStyle = LinkButtonStyle()
|
|
|
|
#elseif os(iOS)
|
|
|
|
let buttonStyle = PlainButtonStyle()
|
|
|
|
#endif
|
|
|
|
|
2020-09-29 19:30:10 +00:00
|
|
|
let imageName: String
|
|
|
|
if url.scheme != "gemini" {
|
|
|
|
imageName = "arrow.up.left.square"
|
|
|
|
} else if url.host == document.url.host {
|
|
|
|
imageName = "arrow.right"
|
|
|
|
} else {
|
|
|
|
imageName = "link"
|
|
|
|
}
|
|
|
|
|
2020-07-16 02:52:15 +00:00
|
|
|
let button: some View = Button {
|
2020-07-16 02:46:48 +00:00
|
|
|
self.changeURL?(url)
|
|
|
|
} label: {
|
2020-09-29 19:30:10 +00:00
|
|
|
HStack(alignment: .firstTextBaseline, spacing: 4) {
|
|
|
|
Image(systemName: imageName)
|
|
|
|
|
|
|
|
Text(verbatim: text)
|
|
|
|
.font(.documentBody)
|
|
|
|
.foregroundColor(hovering ? .blue : Color.blue.opacity(0.8))
|
|
|
|
.underline()
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
}
|
2020-07-16 02:52:15 +00:00
|
|
|
}
|
2020-07-16 03:45:37 +00:00
|
|
|
.buttonStyle(buttonStyle)
|
2020-07-16 02:52:15 +00:00
|
|
|
.onHover { hovering in
|
|
|
|
self.hovering = hovering
|
|
|
|
}
|
|
|
|
|
2020-09-29 19:30:10 +00:00
|
|
|
return button.maybeHelp(url.absoluteString)
|
2020-07-16 02:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func preformatted(_ text: String) -> some View {
|
|
|
|
ScrollView(.horizontal) {
|
|
|
|
Text(verbatim: text)
|
|
|
|
.font(.documentBodyPreformatted)
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
2020-07-13 22:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-16 02:46:48 +00:00
|
|
|
|
|
|
|
private func heading(_ text: String, level: Document.HeadingLevel) -> some View {
|
|
|
|
Text(verbatim: text)
|
|
|
|
.font(level.font)
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func unorderedListItem(_ text: String) -> some View {
|
|
|
|
// todo: should this be .firstTextBaseline?
|
|
|
|
HStack(alignment: .top, spacing: 4) {
|
|
|
|
Text(verbatim: "\u{2022}")
|
|
|
|
Text(verbatim: text)
|
|
|
|
.font(.documentBody)
|
|
|
|
Spacer()
|
|
|
|
}.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func quote(_ text: String) -> some View {
|
|
|
|
HStack(spacing: 4) {
|
|
|
|
Color.gray
|
|
|
|
.frame(width: 4)
|
|
|
|
Text(verbatim: text)
|
|
|
|
.font(Font.documentBody.italic())
|
|
|
|
.foregroundColor(.gray)
|
|
|
|
Spacer()
|
|
|
|
}.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
}
|
2020-07-13 22:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct RenderingBlockView_Previews: PreviewProvider {
|
2020-09-29 19:30:10 +00:00
|
|
|
static let doc = Document(url: URL(string: "gemini://localhost/test.gmi")!, lines: [.text("Some Text"), .quote("A Quote")])
|
|
|
|
|
2020-07-13 22:12:04 +00:00
|
|
|
static var previews: some View {
|
|
|
|
Group {
|
2020-09-29 19:30:10 +00:00
|
|
|
RenderingBlockView(document: doc, block: .text("Some Text"))
|
|
|
|
RenderingBlockView(document: doc, block: .quote("A Quote"))
|
2020-07-13 22:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|