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

211 lines
7.4 KiB
Swift
Raw Normal View History

2017-01-22 16:22:05 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Cocoa
import RxSwift
import PureLayout
import WebKit
import Swifter
class PreviewTool: NSView, UiComponent, WKNavigationDelegate {
2017-02-07 00:54:22 +03:00
enum Action {
case refreshNow
case reverseSearch(to: Marked<Position>)
2017-02-07 00:54:22 +03:00
case scroll(to: Marked<Position>)
2017-02-07 00:54:22 +03:00
case setAutomaticReverseSearch(to: Bool)
case setAutomaticForwardSearch(to: Bool)
case setRefreshOnWrite(to: Bool)
}
2017-01-22 16:22:05 +03:00
typealias StateType = MainWindow.State
2017-02-07 00:54:22 +03:00
let menuItems: [NSMenuItem]
2017-01-25 00:39:19 +03:00
required init(source: Observable<StateType>, emitter: ActionEmitter, state: StateType) {
2017-02-07 00:54:22 +03:00
self.emitter = emitter
self.uuid = state.uuid
let configuration = WKWebViewConfiguration()
configuration.userContentController = self.userContentController
self.webview = WKWebView(frame: CGRect.zero, configuration: configuration)
let refreshMenuItem = NSMenuItem(title: "Refresh Now", action: nil, keyEquivalent: "")
let forwardSearchMenuItem = NSMenuItem(title: "Forward Search", action: nil, keyEquivalent: "")
let reverseSearchMenuItem = NSMenuItem(title: "Reverse Search", action: nil, keyEquivalent: "")
let automaticForward = self.automaticForwardMenuItem
let automaticReverse = self.automaticReverseMenuItem
let refreshOnWrite = self.refreshOnWriteMenuItem
automaticForward.boolState = state.previewTool.isForwardSearchAutomatically
automaticReverse.boolState = state.previewTool.isReverseSearchAutomatically
refreshOnWrite.boolState = state.previewTool.isRefreshOnWrite
self.menuItems = [
refreshMenuItem,
forwardSearchMenuItem,
reverseSearchMenuItem,
NSMenuItem.separator(),
automaticForward,
automaticReverse,
NSMenuItem.separator(),
refreshOnWrite,
]
2017-01-22 16:22:05 +03:00
super.init(frame: .zero)
self.configureForAutoLayout()
2017-02-07 00:54:22 +03:00
refreshMenuItem.target = self
refreshMenuItem.action = #selector(PreviewTool.refreshNowAction)
forwardSearchMenuItem.target = self
forwardSearchMenuItem.action = #selector(PreviewTool.forwardSearchAction)
reverseSearchMenuItem.target = self
reverseSearchMenuItem.action = #selector(PreviewTool.reverseSearchAction)
automaticForward.target = self
automaticForward.action = #selector(PreviewTool.automaticForwardSearchAction)
automaticReverse.target = self
automaticReverse.action = #selector(PreviewTool.automaticReverseSearchAction)
refreshOnWrite.target = self
refreshOnWrite.action = #selector(PreviewTool.refreshOnWriteAction)
2017-01-22 16:22:05 +03:00
self.addViews()
2017-02-06 00:39:55 +03:00
source
2017-02-07 00:54:22 +03:00
.observeOn(MainScheduler.instance)
.subscribe(onNext: { [unowned self] state in
self.automaticForwardMenuItem.boolState = state.previewTool.isForwardSearchAutomatically
self.automaticReverseMenuItem.boolState = state.previewTool.isReverseSearchAutomatically
self.refreshOnWriteMenuItem.boolState = state.previewTool.isRefreshOnWrite
if state.previewTool.isForwardSearchAutomatically
&& state.preview.editorPosition.mark != self.editorPosition.mark
{
NSLog("!!!!!!!!!!!!!!! forward!")
self.forwardSearch(position: state.preview.editorPosition.payload)
2017-02-07 00:54:22 +03:00
}
self.editorPosition = state.preview.editorPosition
2017-02-07 00:54:22 +03:00
guard state.preview.updateDate > self.lastUpdateDate else { return }
guard let serverUrl = state.preview.server else { return }
self.lastUpdateDate = state.preview.updateDate
self.webview.load(URLRequest(url: serverUrl))
})
.addDisposableTo(self.disposeBag)
self.webviewMessageHandler.source
.throttle(0.75, latest: true, scheduler: self.scheduler)
.subscribe(onNext: { [unowned self] position in
self.previewPosition = Marked(position)
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.scroll(to: self.previewPosition)))
2017-02-06 00:39:55 +03:00
})
.addDisposableTo(self.disposeBag)
2017-01-22 16:22:05 +03:00
}
fileprivate func addViews() {
2017-02-07 00:54:22 +03:00
self.webview.navigationDelegate = self
self.userContentController.add(webviewMessageHandler, name: "com_vimr_tools_preview_markdown")
self.webview.loadHTMLString("", baseURL: nil)
self.webview.configureForAutoLayout()
2017-01-22 16:22:05 +03:00
self.addSubview(self.webview)
self.webview.autoPinEdgesToSuperviewEdges()
}
func webView(_: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
NSLog("ERROR preview component's webview: \(error)")
}
2017-02-07 00:54:22 +03:00
fileprivate let emitter: ActionEmitter
fileprivate let uuid: String
fileprivate let webview: WKWebView
2017-02-06 00:39:55 +03:00
fileprivate let disposeBag = DisposeBag()
2017-02-07 00:54:22 +03:00
fileprivate let scheduler = ConcurrentDispatchQueueScheduler(qos: .userInitiated)
2017-01-22 16:22:05 +03:00
fileprivate var isOpen = false
2017-02-07 00:54:22 +03:00
fileprivate var lastUpdateDate = Date.distantPast
fileprivate var editorPosition = Marked(Position(row: 1, column: 1))
fileprivate var previewPosition = Marked(Position(row: 1, column: 1))
2017-02-07 00:54:22 +03:00
fileprivate let userContentController = WKUserContentController()
fileprivate let webviewMessageHandler = WebviewMessageHandler()
fileprivate let automaticForwardMenuItem = NSMenuItem(title: "Automatic Forward Search",
action: nil,
keyEquivalent: "")
fileprivate let automaticReverseMenuItem = NSMenuItem(title: "Automatic Reverse Search",
action: nil,
keyEquivalent: "")
fileprivate let refreshOnWriteMenuItem = NSMenuItem(title: "Refresh on Write", action: nil, keyEquivalent: "")
2017-01-22 16:22:05 +03:00
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
2017-02-07 00:54:22 +03:00
fileprivate func forwardSearch(position: Position) {
self.webview.evaluateJavaScript("scrollToPosition(\(position.row), \(position.column));")
}
}
// MARK: - Actions
extension PreviewTool {
func refreshNowAction(_: Any?) {
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.refreshNow))
}
func forwardSearchAction(_: Any?) {
self.forwardSearch(position: self.editorPosition.payload)
2017-02-07 00:54:22 +03:00
}
func reverseSearchAction(_: Any?) {
2017-02-12 13:24:15 +03:00
self.previewPosition = Marked(self.previewPosition.payload) // set a new mark
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.reverseSearch(to: self.previewPosition)))
2017-02-07 00:54:22 +03:00
}
func automaticForwardSearchAction(_ sender: NSMenuItem) {
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.setAutomaticForwardSearch(to: !sender.boolState)))
}
func automaticReverseSearchAction(_ sender: NSMenuItem) {
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.setAutomaticReverseSearch(to: !sender.boolState)))
}
func refreshOnWriteAction(_ sender: NSMenuItem) {
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.setRefreshOnWrite(to: !sender.boolState)))
}
}
fileprivate class WebviewMessageHandler: NSObject, WKScriptMessageHandler {
var source: Observable<Position> {
return self.subject.asObservable()
}
deinit {
self.subject.onCompleted()
}
func userContentController(_: WKUserContentController, didReceive message: WKScriptMessage) {
guard let msgBody = message.body as? [String: Int] else {
return
}
guard let lineBegin = msgBody["lineBegin"], let columnBegin = msgBody["columnBegin"] else {
return
}
self.subject.onNext(Position(row: lineBegin, column: columnBegin))
}
fileprivate let subject = PublishSubject<Position>()
2017-01-22 16:22:05 +03:00
}