1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-28 11:35: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 flow: EmbeddableComponent
fileprivate let scrollFlow: EmbeddableComponent
fileprivate let scheduler = ConcurrentDispatchQueueScheduler(qos: .userInitiated) fileprivate let scheduler = ConcurrentDispatchQueueScheduler(qos: .userInitiated)
fileprivate let baseUrl = Bundle.main.resourceURL!.appendingPathComponent("markdown") fileprivate let baseUrl = Bundle.main.resourceURL!.appendingPathComponent("markdown")
@ -131,6 +132,10 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
return self.flow.sink return self.flow.sink
} }
var scrollSink: Observable<Any> {
return self.scrollFlow.sink
}
let toolbar: NSView? = NSView(forAutoLayout: ()) let toolbar: NSView? = NSView(forAutoLayout: ())
let menuItems: [NSMenuItem]? let menuItems: [NSMenuItem]?
@ -151,6 +156,7 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
self.template = template self.template = template
self.flow = EmbeddableComponent(source: source) self.flow = EmbeddableComponent(source: source)
self.scrollFlow = EmbeddableComponent(source: scrollSource)
let configuration = WKWebViewConfiguration() let configuration = WKWebViewConfiguration()
configuration.userContentController = self.userContentController configuration.userContentController = self.userContentController
@ -185,8 +191,6 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
super.init() super.init()
self.flow.set(subscription: self.subscription)
self.initCustomUiElements() self.initCustomUiElements()
refreshMenuItem.target = self refreshMenuItem.target = self
@ -202,13 +206,8 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
refreshOnWriteMenuItem.target = self refreshOnWriteMenuItem.target = self
refreshOnWriteMenuItem.action = #selector(MarkdownRenderer.refreshOnWriteAction) refreshOnWriteMenuItem.action = #selector(MarkdownRenderer.refreshOnWriteAction)
scrollSource self.flow.set(subscription: self.subscription)
.throttle(1, latest: true, scheduler: self.scheduler) self.scrollFlow.set(subscription: self.scrollSubscription)
.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.addReactions() self.addReactions()
self.userContentController.add(webviewMessageHandler, name: "com_vimr_preview_markdown") self.userContentController.add(webviewMessageHandler, name: "com_vimr_preview_markdown")
@ -218,6 +217,15 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
return extensions.contains(fileExtension) 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 { fileprivate func subscription(source: Observable<Any>) -> Disposable {
return source return source
.observeOn(self.scheduler) .observeOn(self.scheduler)
@ -281,7 +289,9 @@ class MarkdownRenderer: NSObject, Flow, PreviewRenderer {
switch action { switch action {
case let .scroll(lineBegin, columnBegin, _, _): case let .scroll(lineBegin, columnBegin, _, _):
self?.currentPreviewPosition = Position(row: lineBegin, column: 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) .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 let flow: EmbeddableComponent
fileprivate var currentUrl: URL? fileprivate var currentUrl: URL?
@ -136,7 +137,7 @@ class PreviewComponent: NSView, ViewComponent, ToolDataHolder {
var view: NSView { var view: NSView {
return self return self
} }
init(source: Observable<Any>, scrollSource: Observable<Any>, neoVimInfoProvider: NeoVimInfoProvider, initialData: PrefData) { init(source: Observable<Any>, scrollSource: Observable<Any>, neoVimInfoProvider: NeoVimInfoProvider, initialData: PrefData) {
self.neoVimInfoProvider = neoVimInfoProvider self.neoVimInfoProvider = neoVimInfoProvider
self.flow = EmbeddableComponent(source: source) self.flow = EmbeddableComponent(source: source)
@ -224,6 +225,25 @@ class PreviewComponent: NSView, ViewComponent, ToolDataHolder {
} }
fileprivate func addReactions() { 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 self.markdownRenderer.sink
.filter { $0 is PreviewRendererAction } .filter { $0 is PreviewRendererAction }
.map { $0 as! PreviewRendererAction } .map { $0 as! PreviewRendererAction }
@ -241,12 +261,12 @@ class PreviewComponent: NSView, ViewComponent, ToolDataHolder {
case let .view(_, view): case let .view(_, view):
self.currentView = view self.currentView = view
case let .scroll(to: position):
self.flow.publish(event: PreviewComponent.Action.scroll(to: position))
case .error: case .error:
self.webview.loadHTMLString(self.previewService.errorHtml(), baseURL: self.baseUrl) self.webview.loadHTMLString(self.previewService.errorHtml(), baseURL: self.baseUrl)
default:
return
} }
}) })
.addDisposableTo(self.flow.disposeBag) .addDisposableTo(self.flow.disposeBag)

View File

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