Skip to content

Commit 5766bca

Browse files
authored
Remove Markdown view @State property (gonzalezreal#197)
* Remove state property from Markdown view * Adopt task(id:priority:_:) in the default image view
1 parent d4d8993 commit 5766bca

File tree

3 files changed

+16
-42
lines changed

3 files changed

+16
-42
lines changed

Sources/MarkdownUI/Extensions/DefaultImageProvider.swift

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public struct DefaultImageProvider: ImageProvider {
1212

1313
public func makeImage(url: URL?) -> some View {
1414
DefaultImageView(url: url, urlSession: self.urlSession)
15-
.id(url)
1615
}
1716
}
1817

Sources/MarkdownUI/Extensions/DefaultImageView/DefaultImageView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct DefaultImageView: View {
1111
case .notRequested, .loading, .failure:
1212
Color.clear
1313
.frame(width: 0, height: 0)
14-
.task {
14+
.task(id: self.url) {
1515
await self.viewModel.task(url: self.url, urlSession: self.urlSession)
1616
}
1717
case .success(let image, let size):

Sources/MarkdownUI/Views/Markdown.swift

+15-40
Original file line numberDiff line numberDiff line change
@@ -189,31 +189,22 @@ import SwiftUI
189189
/// )
190190
/// ```
191191
public struct Markdown: View {
192-
private enum Storage: Equatable {
193-
case text(String)
194-
case markdownContent(MarkdownContent)
195-
196-
var markdownContent: MarkdownContent {
197-
switch self {
198-
case .text(let markdown):
199-
return MarkdownContent(markdown)
200-
case .markdownContent(let markdownContent):
201-
return markdownContent
202-
}
203-
}
204-
}
205-
206192
@Environment(\.colorScheme) private var colorScheme
207193
@Environment(\.theme.text) private var text
208194

209-
@State private var blocks: [Block] = []
210-
211-
private let storage: Storage
195+
private let content: MarkdownContent
212196
private let baseURL: URL?
213197
private let imageBaseURL: URL?
214198

215-
private init(storage: Storage, baseURL: URL?, imageBaseURL: URL?) {
216-
self.storage = storage
199+
/// Creates a Markdown view from a Markdown content value.
200+
/// - Parameters:
201+
/// - content: The Markdown content value.
202+
/// - baseURL: The base URL to use when resolving Markdown URLs. If this value is `nil`, the initializer will consider all
203+
/// URLs absolute. The default is `nil`.
204+
/// - imageBaseURL: The base URL to use when resolving Markdown image URLs. If this value is `nil`, the initializer will
205+
/// determine image URLs using the `baseURL` parameter. The default is `nil`.
206+
public init(_ content: MarkdownContent, baseURL: URL? = nil, imageBaseURL: URL? = nil) {
207+
self.content = content
217208
self.baseURL = baseURL
218209
self.imageBaseURL = imageBaseURL ?? baseURL
219210
}
@@ -226,18 +217,13 @@ public struct Markdown: View {
226217
.modifier(ScaledFontSizeModifier(attributes.fontProperties?.size))
227218
}
228219
.textStyle(self.text)
229-
.onAppear {
230-
// Delay Markdown parsing until the view appears for the first time
231-
if self.blocks.isEmpty {
232-
self.blocks = self.storage.markdownContent.colorScheme(self.colorScheme).blocks
233-
}
234-
}
235-
.onChange(of: self.storage) { storage in
236-
self.blocks = storage.markdownContent.blocks
237-
}
238220
.environment(\.baseURL, self.baseURL)
239221
.environment(\.imageBaseURL, self.imageBaseURL)
240222
}
223+
224+
private var blocks: [Block] {
225+
self.content.colorScheme(self.colorScheme).blocks
226+
}
241227
}
242228

243229
extension Markdown {
@@ -249,18 +235,7 @@ extension Markdown {
249235
/// - imageBaseURL: The base URL to use when resolving Markdown image URLs. If this value is `nil`, the initializer will
250236
/// determine image URLs using the `baseURL` parameter. The default is `nil`.
251237
public init(_ markdown: String, baseURL: URL? = nil, imageBaseURL: URL? = nil) {
252-
self.init(storage: .text(markdown), baseURL: baseURL, imageBaseURL: imageBaseURL)
253-
}
254-
255-
/// Creates a Markdown view from a Markdown content value.
256-
/// - Parameters:
257-
/// - content: The Markdown content value.
258-
/// - baseURL: The base URL to use when resolving Markdown URLs. If this value is `nil`, the initializer will consider all
259-
/// URLs absolute. The default is `nil`.
260-
/// - imageBaseURL: The base URL to use when resolving Markdown image URLs. If this value is `nil`, the initializer will
261-
/// determine image URLs using the `baseURL` parameter. The default is `nil`.
262-
public init(_ content: MarkdownContent, baseURL: URL? = nil, imageBaseURL: URL? = nil) {
263-
self.init(storage: .markdownContent(content), baseURL: baseURL, imageBaseURL: imageBaseURL)
238+
self.init(MarkdownContent(markdown), baseURL: baseURL, imageBaseURL: imageBaseURL)
264239
}
265240

266241
/// Creates a Markdown view composed of any number of blocks.

0 commit comments

Comments
 (0)