Skip to content

Commit

Permalink
Remove Markdown view @State property (#197)
Browse files Browse the repository at this point in the history
* Remove state property from Markdown view

* Adopt task(id:priority:_:) in the default image view
  • Loading branch information
gonzalezreal authored Mar 11, 2023
1 parent d4d8993 commit 5766bca
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 42 deletions.
1 change: 0 additions & 1 deletion Sources/MarkdownUI/Extensions/DefaultImageProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public struct DefaultImageProvider: ImageProvider {

public func makeImage(url: URL?) -> some View {
DefaultImageView(url: url, urlSession: self.urlSession)
.id(url)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct DefaultImageView: View {
case .notRequested, .loading, .failure:
Color.clear
.frame(width: 0, height: 0)
.task {
.task(id: self.url) {
await self.viewModel.task(url: self.url, urlSession: self.urlSession)
}
case .success(let image, let size):
Expand Down
55 changes: 15 additions & 40 deletions Sources/MarkdownUI/Views/Markdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,31 +189,22 @@ import SwiftUI
/// )
/// ```
public struct Markdown: View {
private enum Storage: Equatable {
case text(String)
case markdownContent(MarkdownContent)

var markdownContent: MarkdownContent {
switch self {
case .text(let markdown):
return MarkdownContent(markdown)
case .markdownContent(let markdownContent):
return markdownContent
}
}
}

@Environment(\.colorScheme) private var colorScheme
@Environment(\.theme.text) private var text

@State private var blocks: [Block] = []

private let storage: Storage
private let content: MarkdownContent
private let baseURL: URL?
private let imageBaseURL: URL?

private init(storage: Storage, baseURL: URL?, imageBaseURL: URL?) {
self.storage = storage
/// Creates a Markdown view from a Markdown content value.
/// - Parameters:
/// - content: The Markdown content value.
/// - baseURL: The base URL to use when resolving Markdown URLs. If this value is `nil`, the initializer will consider all
/// URLs absolute. The default is `nil`.
/// - imageBaseURL: The base URL to use when resolving Markdown image URLs. If this value is `nil`, the initializer will
/// determine image URLs using the `baseURL` parameter. The default is `nil`.
public init(_ content: MarkdownContent, baseURL: URL? = nil, imageBaseURL: URL? = nil) {
self.content = content
self.baseURL = baseURL
self.imageBaseURL = imageBaseURL ?? baseURL
}
Expand All @@ -226,18 +217,13 @@ public struct Markdown: View {
.modifier(ScaledFontSizeModifier(attributes.fontProperties?.size))
}
.textStyle(self.text)
.onAppear {
// Delay Markdown parsing until the view appears for the first time
if self.blocks.isEmpty {
self.blocks = self.storage.markdownContent.colorScheme(self.colorScheme).blocks
}
}
.onChange(of: self.storage) { storage in
self.blocks = storage.markdownContent.blocks
}
.environment(\.baseURL, self.baseURL)
.environment(\.imageBaseURL, self.imageBaseURL)
}

private var blocks: [Block] {
self.content.colorScheme(self.colorScheme).blocks
}
}

extension Markdown {
Expand All @@ -249,18 +235,7 @@ extension Markdown {
/// - imageBaseURL: The base URL to use when resolving Markdown image URLs. If this value is `nil`, the initializer will
/// determine image URLs using the `baseURL` parameter. The default is `nil`.
public init(_ markdown: String, baseURL: URL? = nil, imageBaseURL: URL? = nil) {
self.init(storage: .text(markdown), baseURL: baseURL, imageBaseURL: imageBaseURL)
}

/// Creates a Markdown view from a Markdown content value.
/// - Parameters:
/// - content: The Markdown content value.
/// - baseURL: The base URL to use when resolving Markdown URLs. If this value is `nil`, the initializer will consider all
/// URLs absolute. The default is `nil`.
/// - imageBaseURL: The base URL to use when resolving Markdown image URLs. If this value is `nil`, the initializer will
/// determine image URLs using the `baseURL` parameter. The default is `nil`.
public init(_ content: MarkdownContent, baseURL: URL? = nil, imageBaseURL: URL? = nil) {
self.init(storage: .markdownContent(content), baseURL: baseURL, imageBaseURL: imageBaseURL)
self.init(MarkdownContent(markdown), baseURL: baseURL, imageBaseURL: imageBaseURL)
}

/// Creates a Markdown view composed of any number of blocks.
Expand Down

0 comments on commit 5766bca

Please sign in to comment.