2017-03-24 19:47:08 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
import RxSwift
|
|
|
|
import PureLayout
|
|
|
|
import WebKit
|
|
|
|
import Swifter
|
2017-04-02 23:06:22 +03:00
|
|
|
import EonilFileSystemEvents
|
|
|
|
|
|
|
|
fileprivate let fileSystemEventsLatency = 2.0
|
|
|
|
fileprivate let monitorDispatchQueue = DispatchQueue.global(qos: .userInitiated)
|
2017-03-24 19:47:08 +03:00
|
|
|
|
|
|
|
class HtmlPreviewTool: NSView, UiComponent {
|
|
|
|
|
|
|
|
enum Action {
|
|
|
|
|
|
|
|
case selectHtmlFile(URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
typealias StateType = MainWindow.State
|
|
|
|
|
|
|
|
let innerCustomToolbar = InnerCustomToolbar()
|
|
|
|
|
|
|
|
required init(source: Observable<StateType>, emitter: ActionEmitter, state: StateType) {
|
|
|
|
self.emitter = emitter
|
|
|
|
self.uuid = state.uuid
|
|
|
|
|
|
|
|
let configuration = WKWebViewConfiguration()
|
|
|
|
self.webview = WKWebView(frame: CGRect.zero, configuration: configuration)
|
|
|
|
|
|
|
|
super.init(frame: .zero)
|
|
|
|
self.configureForAutoLayout()
|
|
|
|
|
|
|
|
self.innerCustomToolbar.htmlPreviewTool = self
|
|
|
|
|
|
|
|
self.addViews()
|
|
|
|
|
|
|
|
self.webview.load(URLRequest(url: URL(string: "http://apple.com")!))
|
|
|
|
|
|
|
|
source
|
|
|
|
.observeOn(MainScheduler.instance)
|
|
|
|
.subscribe(onNext: { [unowned self] state in
|
2017-04-02 23:06:22 +03:00
|
|
|
guard let serverUrl = state.htmlPreview.server, let htmlFileUrl = state.htmlPreview.htmlFile else {
|
|
|
|
self.monitor = nil
|
2017-04-02 22:55:24 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if serverUrl.mark == self.mark {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-02 23:06:22 +03:00
|
|
|
self.monitor = FileSystemEventMonitor(pathsToWatch: [htmlFileUrl.path],
|
2017-04-02 23:12:14 +03:00
|
|
|
latency: fileSystemEventsLatency,
|
|
|
|
watchRoot: false,
|
|
|
|
queue: monitorDispatchQueue)
|
2017-04-02 23:06:22 +03:00
|
|
|
{ [unowned self] events in
|
|
|
|
self.webview.load(URLRequest(url: serverUrl.payload))
|
|
|
|
}
|
|
|
|
|
2017-04-02 22:55:24 +03:00
|
|
|
self.mark = serverUrl.mark
|
|
|
|
self.webview.load(URLRequest(url: serverUrl.payload))
|
2017-03-24 19:47:08 +03:00
|
|
|
})
|
|
|
|
.addDisposableTo(self.disposeBag)
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate func addViews() {
|
|
|
|
self.webview.configureForAutoLayout()
|
|
|
|
|
|
|
|
self.addSubview(self.webview)
|
|
|
|
self.webview.autoPinEdgesToSuperviewEdges()
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate let emitter: ActionEmitter
|
|
|
|
fileprivate let uuid: String
|
|
|
|
|
2017-04-02 22:55:24 +03:00
|
|
|
fileprivate var mark = Token()
|
|
|
|
|
2017-03-24 19:47:08 +03:00
|
|
|
fileprivate let webview: WKWebView
|
2017-04-02 23:06:22 +03:00
|
|
|
fileprivate var monitor: FileSystemEventMonitor?
|
2017-03-24 19:47:08 +03:00
|
|
|
|
|
|
|
fileprivate let disposeBag = DisposeBag()
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectHtmlFile(sender: Any?) {
|
|
|
|
let panel = NSOpenPanel()
|
|
|
|
panel.canChooseDirectories = false
|
|
|
|
panel.allowsMultipleSelection = false
|
|
|
|
panel.beginSheetModal(for: self.window!) { result in
|
|
|
|
guard result == NSFileHandlingPanelOKButton else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let urls = panel.urls
|
|
|
|
guard urls.count == 1 else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.selectHtmlFile(urls[0])))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension HtmlPreviewTool {
|
|
|
|
|
|
|
|
class InnerCustomToolbar: NSView {
|
|
|
|
|
|
|
|
fileprivate weak var htmlPreviewTool: HtmlPreviewTool? {
|
|
|
|
didSet {
|
|
|
|
self.selectHtmlFile.target = self.htmlPreviewTool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 23:12:14 +03:00
|
|
|
let selectHtmlFile = NSButton(forAutoLayout: ())
|
2017-03-24 19:47:08 +03:00
|
|
|
|
|
|
|
init() {
|
|
|
|
super.init(frame: .zero)
|
|
|
|
self.configureForAutoLayout()
|
|
|
|
|
|
|
|
self.addViews()
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate func addViews() {
|
|
|
|
let selectHtmlFile = self.selectHtmlFile
|
|
|
|
InnerToolBar.configureToStandardIconButton(button: selectHtmlFile, iconName: .fileCodeO)
|
|
|
|
selectHtmlFile.toolTip = "Select the HTML file"
|
|
|
|
selectHtmlFile.action = #selector(HtmlPreviewTool.selectHtmlFile)
|
|
|
|
|
|
|
|
self.addSubview(selectHtmlFile)
|
|
|
|
|
|
|
|
selectHtmlFile.autoPinEdge(toSuperviewEdge: .top)
|
|
|
|
selectHtmlFile.autoPinEdge(toSuperviewEdge: .right)
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|