Clean up RenderingBlockView.swift
This commit is contained in:
parent
2d05e318ff
commit
ab34d537d8
|
@ -21,49 +21,73 @@ struct RenderingBlockView: View {
|
|||
var body: some View {
|
||||
switch block {
|
||||
case let .text(text):
|
||||
self.text(text)
|
||||
case let .link(url, text: linkText):
|
||||
self.link(url, text: linkText)
|
||||
case let .preformatted(text, alt: _):
|
||||
preformatted(text)
|
||||
case let .heading(text, level: level):
|
||||
heading(text, level: level)
|
||||
case let .unorderedListItem(text):
|
||||
unorderedListItem(text)
|
||||
case let .quote(text):
|
||||
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
|
||||
return Button {
|
||||
self.changeURL?(url)
|
||||
} label: {
|
||||
Text(verbatim: text)
|
||||
.font(.documentBody)
|
||||
.foregroundColor(.blue)
|
||||
.underline()
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
case let .link(url, text: linkText):
|
||||
let text = linkText ?? url.absoluteString
|
||||
Button {
|
||||
self.changeURL?(url)
|
||||
} label: {
|
||||
Text(verbatim: text)
|
||||
.font(.documentBody)
|
||||
.foregroundColor(.blue)
|
||||
.underline()
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}.buttonStyle(LinkButtonStyle())
|
||||
case let .preformatted(text, alt: _):
|
||||
ScrollView(.horizontal) {
|
||||
Text(verbatim: text)
|
||||
.font(.documentBodyPreformatted)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
case let .heading(text, level: level):
|
||||
}.buttonStyle(LinkButtonStyle())
|
||||
}
|
||||
|
||||
private func preformatted(_ text: String) -> some View {
|
||||
ScrollView(.horizontal) {
|
||||
Text(verbatim: text)
|
||||
.font(level.font)
|
||||
.font(.documentBodyPreformatted)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
case let .unorderedListItem(text):
|
||||
// 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)
|
||||
case let .quote(text):
|
||||
HStack(spacing: 4) {
|
||||
Color.gray
|
||||
.frame(width: 4)
|
||||
Text(verbatim: text)
|
||||
.font(Font.documentBody.italic())
|
||||
.foregroundColor(.gray)
|
||||
Spacer()
|
||||
}.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
struct RenderingBlockView_Previews: PreviewProvider {
|
||||
|
|
Loading…
Reference in New Issue