2017-01-22 16:22:05 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RxSwift
|
2017-02-04 17:34:13 +03:00
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
// Currently supports only markdown
|
2017-02-27 01:01:06 +03:00
|
|
|
class PreviewTransformer {
|
2017-01-22 16:22:05 +03:00
|
|
|
|
2017-02-06 21:58:10 +03:00
|
|
|
static let basePath = "/tools/preview"
|
|
|
|
static let saveFirstPath = "/tools/preview/save-first.html"
|
|
|
|
static let errorPath = "/tools/preview/error.html"
|
|
|
|
static let nonePath = "/tools/preview/empty.html"
|
2017-02-06 21:22:28 +03:00
|
|
|
|
2017-02-27 01:01:06 +03:00
|
|
|
let forMainWindow = MainWindowPreviewTransformer()
|
|
|
|
let forOpenedFileList = OpenedFileListTransformer()
|
|
|
|
|
2017-02-04 17:34:13 +03:00
|
|
|
init(baseServerUrl: URL) {
|
|
|
|
self.baseServerUrl = baseServerUrl
|
2017-02-27 01:01:06 +03:00
|
|
|
|
|
|
|
self.forMainWindow.parentTransformer = self
|
|
|
|
self.forOpenedFileList.parentTransformer = self
|
2017-02-04 17:34:13 +03:00
|
|
|
}
|
|
|
|
|
2017-02-27 01:01:06 +03:00
|
|
|
fileprivate let baseServerUrl: URL
|
|
|
|
}
|
|
|
|
|
|
|
|
extension PreviewTransformer {
|
2017-01-22 16:22:05 +03:00
|
|
|
|
2017-02-27 01:01:06 +03:00
|
|
|
class MainWindowPreviewTransformer: Transformer {
|
2017-01-22 16:22:05 +03:00
|
|
|
|
2017-02-27 01:01:06 +03:00
|
|
|
typealias Pair = StateActionPair<UuidState<MainWindow.State>, MainWindow.Action>
|
|
|
|
|
|
|
|
func transform(_ source: Observable<Pair>) -> Observable<Pair> {
|
|
|
|
return source.map { pair in
|
|
|
|
guard let baseServerUrl = self.parentTransformer?.baseServerUrl else {
|
2017-02-07 00:54:22 +03:00
|
|
|
return pair
|
|
|
|
}
|
|
|
|
|
2017-02-27 01:01:06 +03:00
|
|
|
var state = pair.state.payload
|
2017-02-04 17:34:13 +03:00
|
|
|
|
2017-02-27 01:01:06 +03:00
|
|
|
switch pair.action {
|
2017-01-22 16:22:05 +03:00
|
|
|
|
2017-02-27 01:01:06 +03:00
|
|
|
case let .setCurrentBuffer(buffer):
|
|
|
|
guard state.previewTool.isRefreshOnWrite else {
|
|
|
|
return pair
|
|
|
|
}
|
2017-01-22 16:22:05 +03:00
|
|
|
|
2017-02-27 01:01:06 +03:00
|
|
|
state.preview = PreviewUtils.state(for: pair.state.uuid, baseUrl: baseServerUrl, buffer: buffer)
|
|
|
|
|
|
|
|
case .close:
|
|
|
|
state.preview = PreviewUtils.state(for: .none, baseUrl: baseServerUrl)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return pair
|
|
|
|
}
|
|
|
|
|
|
|
|
return StateActionPair(state: UuidState(uuid: pair.state.uuid, state: state), action: pair.action)
|
|
|
|
}
|
2017-01-22 16:22:05 +03:00
|
|
|
}
|
2017-02-27 01:01:06 +03:00
|
|
|
|
|
|
|
fileprivate var parentTransformer: PreviewTransformer?
|
2017-01-22 16:22:05 +03:00
|
|
|
}
|
|
|
|
|
2017-02-27 01:01:06 +03:00
|
|
|
class OpenedFileListTransformer: Transformer {
|
|
|
|
|
|
|
|
typealias Pair = StateActionPair<UuidState<MainWindow.State>, OpenedFileList.Action>
|
|
|
|
|
|
|
|
func transform(_ source: Observable<Pair>) -> Observable<Pair> {
|
|
|
|
return source.map { pair in
|
|
|
|
guard let baseServerUrl = self.parentTransformer?.baseServerUrl else {
|
|
|
|
return pair
|
|
|
|
}
|
|
|
|
|
|
|
|
var state = pair.state.payload
|
|
|
|
|
|
|
|
switch pair.action {
|
|
|
|
|
|
|
|
case let .open(buffer):
|
|
|
|
state.preview = PreviewUtils.state(for: pair.state.uuid, baseUrl: baseServerUrl, buffer: buffer)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return StateActionPair(state: UuidState(uuid: pair.state.uuid, state: state), action: pair.action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate var parentTransformer: PreviewTransformer?
|
|
|
|
}
|
2017-01-22 16:22:05 +03:00
|
|
|
}
|