Skip to content

Commit

Permalink
Merge pull request #30 from mirko-milovanovic-vidiemme/main
Browse files Browse the repository at this point in the history
Fix (#29): dynamic height evaluation
  • Loading branch information
NuPlay authored Mar 8, 2023
2 parents 6bd367c + fba1e9b commit b104bcf
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions Sources/RichText/Views/Webview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ extension WebView {
}

public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.documentElement.scrollHeight", completionHandler: { (height, _) in
webView.evaluateJavaScript("document.getElementById(\"NuPlay_RichText\").offsetHeight", completionHandler: { (height, _) in
DispatchQueue.main.async {
withAnimation(self.parent.conf.transition) {
self.parent.dynamicHeight = height as! CGFloat
Expand Down Expand Up @@ -120,7 +120,7 @@ extension WebView {
<meta name='viewport' content='width=device-width, shrink-to-fit=YES, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'>
</head>
\(generateCSS())
<div>\(html)</div>
<div id="NuPlay_RichText">\(html)</div>
</BODY>
</HTML>
"""
Expand All @@ -129,9 +129,29 @@ extension WebView {
func generateCSS() -> String {
switch conf.colorScheme {
case .light:
return "<style type='text/css'>\(conf.css(isLight: true, alignment: alignment))\(conf.customCSS)</style><BODY>"
return """
<style type='text/css'>
\(conf.css(isLight: true, alignment: alignment))
\(conf.customCSS)
body {
margin: 0;
padding: 0;
}
</style>
<BODY>
"""
case .dark:
return "<style type='text/css'>\(conf.css(isLight: false, alignment: alignment))\(conf.customCSS)</style><BODY>"
return """
<style type='text/css'>
\(conf.css(isLight: false, alignment: alignment))
\(conf.customCSS)
body {
margin: 0;
padding: 0;
}
</style>
<BODY>
"""
case .auto:
return """
<style type='text/css'>
Expand All @@ -142,6 +162,10 @@ extension WebView {
\(conf.css(isLight: false, alignment: alignment))
}
\(conf.customCSS)
body {
margin: 0;
padding: 0;
}
</style>
<BODY>
"""
Expand Down

0 comments on commit b104bcf

Please sign in to comment.