-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Enable code coverage * Implement image loading * Render single-image paragraphs * Update workflows * Render paragraphs with multiple images and no text * Run swift format * Test image links in multi-image paragraphs
- Loading branch information
Guille Gonzalez
authored
Oct 24, 2022
1 parent
8073737
commit 6396fc7
Showing
41 changed files
with
1,146 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import SwiftUI | ||
|
||
public struct ImageStyle { | ||
public struct Configuration { | ||
public struct Content: View { | ||
init<C: View>(_ content: C) { | ||
self.body = AnyView(content) | ||
} | ||
|
||
public let body: AnyView | ||
} | ||
|
||
public let content: Content | ||
} | ||
|
||
let makeBody: (Configuration) -> AnyView | ||
|
||
public init<Body>(@ViewBuilder makeBody: @escaping (Configuration) -> Body) where Body: View { | ||
self.makeBody = { configuration in | ||
AnyView(makeBody(configuration)) | ||
} | ||
} | ||
} | ||
|
||
extension ImageStyle { | ||
public static var `default`: Self { | ||
.init { $0.content } | ||
} | ||
|
||
public static func alignment(_ alignment: HorizontalAlignment) -> Self { | ||
.init { configuration in | ||
ZStack { | ||
configuration.content | ||
} | ||
.frame(maxWidth: .infinity, alignment: .init(horizontal: alignment, vertical: .center)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import SwiftUI | ||
|
||
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) | ||
struct ImageParagraphView: View { | ||
private enum Item: Hashable { | ||
case image(source: String?, alt: String, destination: String? = nil) | ||
case lineBreak | ||
} | ||
|
||
@Environment(\.theme.paragraphSpacing) private var paragraphSpacing | ||
@Environment(\.theme.horizontalImageSpacing) private var horizontalSpacing | ||
@Environment(\.theme.verticalImageSpacing) private var verticalSpacing | ||
|
||
private let items: [Identified<Int, Item>] | ||
|
||
var body: some View { | ||
Flow(horizontalSpacing: self.horizontalSpacing, verticalSpacing: self.verticalSpacing) { | ||
ForEach(self.items) { item in | ||
switch item.value { | ||
case let .image(source, alt, destination): | ||
ImageView(source: source, alt: alt, destination: destination) | ||
case .lineBreak: | ||
Spacer() | ||
} | ||
} | ||
} | ||
.preference(key: BlockSpacingPreference.self, value: paragraphSpacing) | ||
} | ||
} | ||
|
||
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) | ||
extension ImageParagraphView { | ||
init?(_ inlines: [AnyInline]) { | ||
var items: [Item] = [] | ||
|
||
for inline in inlines { | ||
switch inline { | ||
case let .text(text) where text.isEmpty: | ||
continue | ||
case .softBreak: | ||
continue | ||
case .lineBreak: | ||
items.append(.lineBreak) | ||
case let .image(source, _, children): | ||
items.append(.image(source: source, alt: children.text)) | ||
case let .link(destination, children) where children.count == 1: | ||
guard let (source, alt) = children.first?.image else { | ||
return nil | ||
} | ||
items.append(.image(source: source, alt: alt, destination: destination)) | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
self.items = items.identified() | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
Sources/MarkdownUI/Views/Blocks/SingleImageParagraphView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import SwiftUI | ||
|
||
struct SingleImageParagraphView: View { | ||
@Environment(\.theme.paragraphSpacing) private var paragraphSpacing | ||
|
||
private let content: ImageView | ||
|
||
private init(content: ImageView) { | ||
self.content = content | ||
} | ||
|
||
var body: some View { | ||
self.content | ||
.preference(key: BlockSpacingPreference.self, value: paragraphSpacing) | ||
} | ||
} | ||
|
||
extension SingleImageParagraphView { | ||
init?(_ inlines: [AnyInline]) { | ||
guard let content = ImageView(inlines) else { | ||
return nil | ||
} | ||
self.init(content: content) | ||
} | ||
} |
Oops, something went wrong.