@@ -32,8 +32,10 @@ private final class IssueCommentHtmlCellWebView: UIWebView {
3232final class IssueCommentHtmlCell : IssueCommentBaseCell , ListBindable , UIWebViewDelegate {
3333
3434 private static let ImgScheme = " freetime-img "
35+ private static let HeightScheme = " freetime-hgt "
3536 private static let htmlHead = """
3637 <!DOCTYPE html><html><head><style>
38+ * {margin: 0;padding: 0;}
3739 body{
3840 // html whitelist: https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/sanitization_filter.rb#L45-L49
3941 // lint compiled style with http://csslint.net/
@@ -77,6 +79,23 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
7779 for (var i = 0; i < imgs.length; i++) {
7880 imgs[i].addEventListener('click', tapAction);
7981 }
82+ function onElementHeightChange(elm, callback) {
83+ var lastHeight = elm.offsetHeight, newHeight;
84+ (function run() {
85+ newHeight = elm.offsetHeight;
86+ if(lastHeight != newHeight) {
87+ callback(newHeight);
88+ }
89+ lastHeight = newHeight;
90+ if(elm.onElementHeightChangeTimer) {
91+ clearTimeout(elm.onElementHeightChangeTimer);
92+ }
93+ elm.onElementHeightChangeTimer = setTimeout(run, 300);
94+ })();
95+ }
96+ onElementHeightChange(document.body, function(height) {
97+ document.location = " \( HeightScheme) :// " + height;
98+ });
8099 </script>
81100 </body>
82101 </html>
@@ -95,6 +114,7 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
95114
96115 webView. backgroundColor = . clear
97116 webView. delegate = self
117+ webView. scrollView. bounces = false
98118
99119 let scrollView = webView. scrollView
100120 scrollView. scrollsToTop = false
@@ -109,13 +129,12 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
109129
110130 override func prepareForReuse( ) {
111131 super. prepareForReuse ( )
112- webView. isHidden = true
132+ webView. alpha = 0
113133 }
114134
115135 override func layoutSubviews( ) {
116136 super. layoutSubviews ( )
117137 if webView. frame != contentView. bounds {
118- print ( " previous frame: \( webView. frame) " )
119138 webView. frame = contentView. bounds
120139 }
121140 }
@@ -134,13 +153,20 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
134153 // MARK: UIWebViewDelegate
135154
136155 func webView( _ webView: UIWebView , shouldStartLoadWith request: URLRequest , navigationType: UIWebViewNavigationType ) -> Bool {
137- guard let url = request. url else { return true }
156+ // if the cell is hidden, its been put back in the reuse pool
157+ guard isHidden == false , let url = request. url else { return true }
138158
139159 if url. scheme == IssueCommentHtmlCell . ImgScheme,
140160 let host = url. host,
141161 let imageURL = URL ( string: host) {
142162 imageDelegate? . webViewDidTapImage ( cell: self , url: imageURL)
143163 return false
164+ } else if url. scheme == IssueCommentHtmlCell . HeightScheme,
165+ let heightString = url. host as NSString ? {
166+ webView. alpha = 1
167+ let size = CGSize ( width: contentView. bounds. width, height: CGFloat ( heightString. floatValue) )
168+ delegate? . webViewDidResize ( cell: self , html: body, cellWidth: size. width, size: size)
169+ return false
144170 }
145171
146172 if let baseURL = webViewBaseURL, url == baseURL {
@@ -155,13 +181,7 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
155181 }
156182
157183 func webViewDidFinishLoad( _ webView: UIWebView ) {
158- // if the cell is hidden, its been put back in the reuse pool
159- guard self . isHidden == false ,
160- let contentHeight = webView. stringByEvaluatingJavaScript ( from: " document.body.offsetHeight " ) as NSString ?
161- else { return }
162-
163- let size = CGSize ( width: contentView. bounds. width, height: CGFloat ( contentHeight. floatValue) )
164- delegate? . webViewDidResize ( cell: self , html: body, cellWidth: size. width, size: size)
184+ webView. alpha = 1
165185 }
166186
167187}
0 commit comments