@@ -32,8 +32,10 @@ private final class IssueCommentHtmlCellWebView: UIWebView {
32
32
final class IssueCommentHtmlCell : IssueCommentBaseCell , ListBindable , UIWebViewDelegate {
33
33
34
34
private static let ImgScheme = " freetime-img "
35
+ private static let HeightScheme = " freetime-hgt "
35
36
private static let htmlHead = """
36
37
<!DOCTYPE html><html><head><style>
38
+ * {margin: 0;padding: 0;}
37
39
body{
38
40
// html whitelist: https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/sanitization_filter.rb#L45-L49
39
41
// lint compiled style with http://csslint.net/
@@ -77,6 +79,23 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
77
79
for (var i = 0; i < imgs.length; i++) {
78
80
imgs[i].addEventListener('click', tapAction);
79
81
}
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
+ });
80
99
</script>
81
100
</body>
82
101
</html>
@@ -95,6 +114,7 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
95
114
96
115
webView. backgroundColor = . clear
97
116
webView. delegate = self
117
+ webView. scrollView. bounces = false
98
118
99
119
let scrollView = webView. scrollView
100
120
scrollView. scrollsToTop = false
@@ -109,13 +129,12 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
109
129
110
130
override func prepareForReuse( ) {
111
131
super. prepareForReuse ( )
112
- webView. isHidden = true
132
+ webView. alpha = 0
113
133
}
114
134
115
135
override func layoutSubviews( ) {
116
136
super. layoutSubviews ( )
117
137
if webView. frame != contentView. bounds {
118
- print ( " previous frame: \( webView. frame) " )
119
138
webView. frame = contentView. bounds
120
139
}
121
140
}
@@ -134,13 +153,20 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
134
153
// MARK: UIWebViewDelegate
135
154
136
155
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 }
138
158
139
159
if url. scheme == IssueCommentHtmlCell . ImgScheme,
140
160
let host = url. host,
141
161
let imageURL = URL ( string: host) {
142
162
imageDelegate? . webViewDidTapImage ( cell: self , url: imageURL)
143
163
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
144
170
}
145
171
146
172
if let baseURL = webViewBaseURL, url == baseURL {
@@ -155,13 +181,7 @@ final class IssueCommentHtmlCell: IssueCommentBaseCell, ListBindable, UIWebViewD
155
181
}
156
182
157
183
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
165
185
}
166
186
167
187
}
0 commit comments