Skip to content

Commit

Permalink
Add support for macCatalyst
Browse files Browse the repository at this point in the history
gonzalezreal committed Jan 16, 2021

Verified

This commit was signed with the committer’s verified signature.
i4ki i4k
1 parent 0de1bbe commit ba3140b
Showing 5 changed files with 12 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/gonzalezreal/AttributedText",
"state": {
"branch": null,
"revision": "8281bdb3d3aec5aac58c2a4918724ca60bc5020c",
"version": "0.2.1"
"revision": "98b392dad0f1f965b641580f414a17ed1449b5c0",
"version": "0.2.2"
}
},
{
@@ -24,8 +24,8 @@
"repositoryURL": "https://github.com/gonzalezreal/NetworkImage",
"state": {
"branch": null,
"revision": "fdcb6cf25315eded3d5928044ec225fc2bfe222a",
"version": "2.0.0"
"revision": "15582b821cb097012b41b83d6219717926ec4ed6",
"version": "2.1.0"
}
},
{
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/gonzalezreal/AttributedText",
"state": {
"branch": null,
"revision": "8281bdb3d3aec5aac58c2a4918724ca60bc5020c",
"version": "0.2.1"
"revision": "98b392dad0f1f965b641580f414a17ed1449b5c0",
"version": "0.2.2"
}
},
{
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ let package = Package(
.package(
name: "AttributedText",
url: "https://github.com/gonzalezreal/AttributedText",
from: "0.2.1"
from: "0.2.2"
),
.package(
name: "NetworkImage",
11 changes: 4 additions & 7 deletions Sources/MarkdownUI/Shared/Markdown.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if canImport(SwiftUI) && !os(watchOS) && !targetEnvironment(macCatalyst)
#if canImport(SwiftUI) && !os(watchOS)

import AttributedText
import CommonMark
@@ -63,8 +63,7 @@
layoutDirection: layoutDirection,
multilineTextAlignment: multilineTextAlignment
),
style: markdownStyle,
isSynchronous: false
style: markdownStyle
)
}
}
@@ -78,16 +77,14 @@
baseURL: URL?,
writingDirection: NSWritingDirection,
alignment: NSTextAlignment,
style: MarkdownStyle,
isSynchronous: Bool
style: MarkdownStyle
) {
renderer = MarkdownRenderer(
document: document,
baseURL: baseURL,
writingDirection: writingDirection,
alignment: alignment,
style: style,
environment: isSynchronous ? .synchronous : .default
style: style
)
}

46 changes: 1 addition & 45 deletions Sources/MarkdownUI/Shared/MarkdownRenderer.swift
Original file line number Diff line number Diff line change
@@ -20,11 +20,6 @@
scheduler: DispatchQueue.main.eraseToAnyScheduler()
)

static let synchronous = Environment(
textAttachments: syncTextAttachments(for:baseURL:),
scheduler: DispatchQueue.immediateScheduler.eraseToAnyScheduler()
)

let textAttachments: (Set<String>, URL?) -> AnyPublisher<[String: NSTextAttachment], Never>
let scheduler: AnySchedulerOf<DispatchQueue>
}
@@ -37,7 +32,7 @@
writingDirection: NSWritingDirection,
alignment: NSTextAlignment,
style: MarkdownStyle,
environment: Environment
environment: Environment = .default
) {
attributedString = NSAttributedString(
document: document,
@@ -94,43 +89,4 @@
.eraseToAnyPublisher()
}

@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)
private func syncTextAttachments(
for urls: Set<String>,
baseURL: URL?
) -> AnyPublisher<[String: NSTextAttachment], Never> {
let attachmentURLs = urls.compactMap {
URL(string: $0, relativeTo: baseURL)
}

guard !attachmentURLs.isEmpty else {
return Just([:]).eraseToAnyPublisher()
}

var result: [String: NSTextAttachment] = [:]

for url in attachmentURLs {
guard let data = try? Data(contentsOf: url),
let image = image(from: data) else { continue }

let attachment = ImageAttachment()
attachment.image = image

result[url.relativeString] = attachment
}

return Just(result).eraseToAnyPublisher()
}

#if os(macOS)
private func image(from data: Data) -> NSImage? {
NSImage(data: data)
}

#elseif canImport(UIKit)
private func image(from data: Data) -> UIImage? {
UIImage(data: data, scale: UIScreen.main.scale)
}
#endif

#endif

0 comments on commit ba3140b

Please sign in to comment.