Clean up RenderingBlockView.swift

This commit is contained in:
Shadowfacts 2020-07-15 22:46:48 -04:00
parent 2d05e318ff
commit ab34d537d8
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 60 additions and 36 deletions

View File

@ -21,12 +21,29 @@ struct RenderingBlockView: View {
var body: some View { var body: some View {
switch block { switch block {
case let .text(text): case let .text(text):
Text(verbatim: 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) .font(.documentBody)
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
case let .link(url, text: linkText): }
let text = linkText ?? url.absoluteString
Button { private func link(_ url: URL, text: String?) -> some View {
let text = text ?? url.absoluteString
return Button {
self.changeURL?(url) self.changeURL?(url)
} label: { } label: {
Text(verbatim: text) Text(verbatim: text)
@ -35,17 +52,23 @@ struct RenderingBlockView: View {
.underline() .underline()
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
}.buttonStyle(LinkButtonStyle()) }.buttonStyle(LinkButtonStyle())
case let .preformatted(text, alt: _): }
private func preformatted(_ text: String) -> some View {
ScrollView(.horizontal) { ScrollView(.horizontal) {
Text(verbatim: text) Text(verbatim: text)
.font(.documentBodyPreformatted) .font(.documentBodyPreformatted)
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
} }
case let .heading(text, level: level): }
private func heading(_ text: String, level: Document.HeadingLevel) -> some View {
Text(verbatim: text) Text(verbatim: text)
.font(level.font) .font(level.font)
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
case let .unorderedListItem(text): }
private func unorderedListItem(_ text: String) -> some View {
// todo: should this be .firstTextBaseline? // todo: should this be .firstTextBaseline?
HStack(alignment: .top, spacing: 4) { HStack(alignment: .top, spacing: 4) {
Text(verbatim: "\u{2022}") Text(verbatim: "\u{2022}")
@ -53,7 +76,9 @@ struct RenderingBlockView: View {
.font(.documentBody) .font(.documentBody)
Spacer() Spacer()
}.frame(maxWidth: .infinity, alignment: .leading) }.frame(maxWidth: .infinity, alignment: .leading)
case let .quote(text): }
private func quote(_ text: String) -> some View {
HStack(spacing: 4) { HStack(spacing: 4) {
Color.gray Color.gray
.frame(width: 4) .frame(width: 4)
@ -63,7 +88,6 @@ struct RenderingBlockView: View {
Spacer() Spacer()
}.frame(maxWidth: .infinity, alignment: .leading) }.frame(maxWidth: .infinity, alignment: .leading)
} }
}
} }
struct RenderingBlockView_Previews: PreviewProvider { struct RenderingBlockView_Previews: PreviewProvider {