Skip to content

Commit

Permalink
Merge pull request #48 from ohmantics/ohmantics
Browse files Browse the repository at this point in the history
Allow setting the WebKit baseURL for requests to something other than the Bundle
  • Loading branch information
NuPlay authored Apr 1, 2024
2 parents 16b87cf + d22aefb commit d7eaa4f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
8 changes: 7 additions & 1 deletion Sources/RichText/Extensions/RichText+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ extension RichText {
result.configuration.linkOpenType = linkOpenType
return result
}


public func baseURL(_ baseURL: URL) -> RichText {
var result = self
result.configuration.baseURL = baseURL
return result
}

public func colorPreference(forceColor: ColorPreference) -> RichText {
var result = self
result.configuration.isColorsImportant = forceColor
Expand Down
3 changes: 3 additions & 0 deletions Sources/RichText/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public struct Configuration {

public var linkOpenType: LinkOpenType
public var linkColor: ColorSet
public var baseURL: URL?

public var isColorsImportant: ColorPreference

Expand All @@ -35,6 +36,7 @@ public struct Configuration {
imageRadius: CGFloat = 0,
linkOpenType: LinkOpenType = .Safari,
linkColor: ColorSet = .init(light: "007AFF", dark: "0A84FF", isImportant: true),
baseURL: URL? = Bundle.main.bundleURL,
isColorsImportant: ColorPreference = .onlyLinks,
transition: Animation? = .none
) {
Expand All @@ -46,6 +48,7 @@ public struct Configuration {
self.imageRadius = imageRadius
self.linkOpenType = linkOpenType
self.linkColor = linkColor
self.baseURL = baseURL
self.isColorsImportant = isColorsImportant
self.transition = transition
}
Expand Down
13 changes: 4 additions & 9 deletions Sources/RichText/Views/Webview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ extension WebView: UIViewRepresentable {
webview.scrollView.isScrollEnabled = false

DispatchQueue.main.async {
let bundleURL = Bundle.main.bundleURL
webview.loadHTMLString(generateHTML(), baseURL: bundleURL)
webview.loadHTMLString(generateHTML(), baseURL: conf.baseURL)
}

webview.isOpaque = false
Expand All @@ -50,10 +49,8 @@ extension WebView: UIViewRepresentable {

func updateUIView(_ uiView: WKWebView, context: Context) {
DispatchQueue.main.async {
uiView.loadHTMLString(generateHTML(), baseURL: conf.baseURL)
uiView.frame.size = .init(width: width, height: dynamicHeight)

let bundleURL = Bundle.main.bundleURL
uiView.loadHTMLString(generateHTML(), baseURL: bundleURL)
}
}

Expand All @@ -74,8 +71,7 @@ extension WebView: NSViewRepresentable {
let webview = ScrollAdjustedWKWebView()
webview.navigationDelegate = context.coordinator
DispatchQueue.main.async {
let bundleURL = Bundle.main.bundleURL
webview.loadHTMLString(generateHTML(), baseURL: bundleURL)
webview.loadHTMLString(generateHTML(), baseURL: conf.baseURL)
}
webview.setValue(false, forKey: "drawsBackground")

Expand All @@ -84,8 +80,7 @@ extension WebView: NSViewRepresentable {

func updateNSView(_ nsView: WKWebView, context _: Context) {
DispatchQueue.main.async {
let bundleURL = Bundle.main.bundleURL
nsView.loadHTMLString(generateHTML(), baseURL: bundleURL)
nsView.loadHTMLString(generateHTML(), baseURL: conf.baseURL)
}
}

Expand Down

0 comments on commit d7eaa4f

Please sign in to comment.