1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-01 10:02:36 +03:00

GH-383 Retain the scroll position

This commit is contained in:
Tae Won Ha 2017-04-02 22:33:25 +02:00
parent af7c8bbf2c
commit d0c6b3c4ae
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44

View File

@ -13,7 +13,7 @@ import EonilFileSystemEvents
fileprivate let fileSystemEventsLatency = 2.0
fileprivate let monitorDispatchQueue = DispatchQueue.global(qos: .userInitiated)
class HtmlPreviewTool: NSView, UiComponent {
class HtmlPreviewTool: NSView, UiComponent, WKNavigationDelegate {
enum Action {
@ -34,6 +34,7 @@ class HtmlPreviewTool: NSView, UiComponent {
super.init(frame: .zero)
self.configureForAutoLayout()
self.webview.navigationDelegate = self
self.innerCustomToolbar.htmlPreviewTool = self
self.addViews()
@ -57,15 +58,24 @@ class HtmlPreviewTool: NSView, UiComponent {
watchRoot: false,
queue: monitorDispatchQueue)
{ [unowned self] events in
self.webview.load(URLRequest(url: serverUrl.payload))
self.reloadWebview(with: serverUrl.payload)
}
self.mark = serverUrl.mark
self.webview.load(URLRequest(url: serverUrl.payload))
self.reloadWebview(with: serverUrl.payload)
})
.addDisposableTo(self.disposeBag)
}
fileprivate func reloadWebview(with url: URL) {
self.webview.evaluateJavaScript("document.body.scrollTop") { (result, error) in
self.scrollTop = result as? Int ?? 0
self.webview.load(URLRequest(url: url))
NSLog("\(self.scrollTop)")
}
}
fileprivate func addViews() {
self.webview.configureForAutoLayout()
@ -77,6 +87,7 @@ class HtmlPreviewTool: NSView, UiComponent {
fileprivate let uuid: String
fileprivate var mark = Token()
fileprivate var scrollTop = 0
fileprivate let webview: WKWebView
fileprivate var monitor: FileSystemEventMonitor?
@ -104,6 +115,10 @@ class HtmlPreviewTool: NSView, UiComponent {
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.selectHtmlFile(urls[0])))
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.webview.evaluateJavaScript("document.body.scrollTop = \(self.scrollTop)")
}
}
extension HtmlPreviewTool {