1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-25 23:02:35 +03:00

GH-339 React to scroll events on both sides

This commit is contained in:
Tae Won Ha 2017-01-06 14:41:19 +01:00
parent 7a37e418a2
commit d384bb519d
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
3 changed files with 46 additions and 14 deletions

View File

@ -102,6 +102,7 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
}
fileprivate let flow: EmbeddableComponent
fileprivate let scrollFlow: EmbeddableComponent
fileprivate let scheduler = ConcurrentDispatchQueueScheduler(qos: .userInitiated)
fileprivate let baseUrl = Bundle.main.resourceURL!.appendingPathComponent("markdown")
@ -131,6 +132,10 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
return self.flow.sink
}
var scrollSink: Observable<Any> {
return self.scrollFlow.sink
}
let toolbar: NSView? = NSView(forAutoLayout: ())
let menuItems: [NSMenuItem]?
@ -151,6 +156,7 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
self.template = template
self.flow = EmbeddableComponent(source: source)
self.scrollFlow = EmbeddableComponent(source: scrollSource)
let configuration = WKWebViewConfiguration()
configuration.userContentController = self.userContentController
@ -185,8 +191,6 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
super.init()
self.flow.set(subscription: self.subscription)
self.initCustomUiElements()
refreshMenuItem.target = self
@ -202,13 +206,8 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
refreshOnWriteMenuItem.target = self
refreshOnWriteMenuItem.action = #selector(MarkdownRenderer.refreshOnWriteAction)
scrollSource
.throttle(1, latest: true, scheduler: self.scheduler)
.filter { $0 is MainWindowComponent.ScrollAction }
.subscribe(onNext: { action in
NSLog("neovim scrolled to \(self.neoVimInfoProvider?.currentLine()) x \(self.neoVimInfoProvider?.currentColumn())")
})
.addDisposableTo(self.flow.disposeBag)
self.flow.set(subscription: self.subscription)
self.scrollFlow.set(subscription: self.scrollSubscription)
self.addReactions()
self.userContentController.add(webviewMessageHandler, name: "com_vimr_preview_markdown")
@ -218,6 +217,15 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
return extensions.contains(fileExtension)
}
fileprivate func scrollSubscription(source: Observable<Any>) -> Disposable {
return source
.throttle(1, latest: true, scheduler: self.scheduler)
.filter { $0 is MainWindowComponent.ScrollAction }
.subscribe(onNext: { [unowned self] action in
NSLog("neovim scrolled to \(self.neoVimInfoProvider?.currentLine()) x \(self.neoVimInfoProvider?.currentColumn())")
})
}
fileprivate func subscription(source: Observable<Any>) -> Disposable {
return source
.observeOn(self.scheduler)
@ -281,7 +289,9 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
switch action {
case let .scroll(lineBegin, columnBegin, _, _):
self?.currentPreviewPosition = Position(row: lineBegin, column: columnBegin)
self?.flow.publish(event: PreviewRendererAction.scroll(to: Position(row: lineBegin, column: columnBegin)))
self?.scrollFlow.publish(
event: PreviewRendererAction.scroll(to: Position(row: lineBegin, column: columnBegin))
)
}
})
.addDisposableTo(self.flow.disposeBag)

View File

@ -73,6 +73,7 @@ class PreviewComponent: NSView, ViewComponent, ToolDataHolder {
}
}
fileprivate let scheduler = ConcurrentDispatchQueueScheduler(qos: .userInitiated)
fileprivate let flow: EmbeddableComponent
fileprivate var currentUrl: URL?
@ -136,7 +137,7 @@ class PreviewComponent: NSView, ViewComponent, ToolDataHolder {
var view: NSView {
return self
}
init(source: Observable<Any>, scrollSource: Observable<Any>, neoVimInfoProvider: NeoVimInfoProvider, initialData: PrefData) {
self.neoVimInfoProvider = neoVimInfoProvider
self.flow = EmbeddableComponent(source: source)
@ -224,6 +225,25 @@ class PreviewComponent: NSView, ViewComponent, ToolDataHolder {
}
fileprivate func addReactions() {
self.markdownRenderer.scrollSink
.throttle(1, latest: true, scheduler: self.scheduler)
.filter { $0 is PreviewRendererAction }
.map { $0 as! PreviewRendererAction }
.subscribe(onNext: { action in
guard self.isOpen else {
return
}
switch action {
case let .scroll(to:position):
self.flow.publish(event: PreviewComponent.Action.scroll(to: position))
default:
return
}
})
.addDisposableTo(self.flow.disposeBag)
self.markdownRenderer.sink
.filter { $0 is PreviewRendererAction }
.map { $0 as! PreviewRendererAction }
@ -241,12 +261,12 @@ class PreviewComponent: NSView, ViewComponent, ToolDataHolder {
case let .view(_, view):
self.currentView = view
case let .scroll(to: position):
self.flow.publish(event: PreviewComponent.Action.scroll(to: position))
case .error:
self.webview.loadHTMLString(self.previewService.errorHtml(), baseURL: self.baseUrl)
default:
return
}
})
.addDisposableTo(self.flow.disposeBag)

View File

@ -4,6 +4,7 @@
*/
import Cocoa
import RxSwift
protocol PreviewRenderer: class {
@ -12,6 +13,7 @@ protocol PreviewRenderer: class {
var identifier: String { get }
var prefData: StandardPrefData? { get }
var scrollSink: Observable<Any> { get }
var toolbar: NSView? { get }
var menuItems: [NSMenuItem]? { get }