2017-01-22 16:22:05 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Foundation
|
2017-02-04 17:34:13 +03:00
|
|
|
|
2017-04-11 20:26:34 +03:00
|
|
|
class MarkdownReducer {
|
2017-01-22 16:22:05 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
typealias Pair = StateActionPair<UuidState<MainWindow.State>, OpenedFileList.Action>
|
|
|
|
typealias MainWindowPair = StateActionPair<UuidState<MainWindow.State>, MainWindow.Action>
|
|
|
|
|
2017-04-11 20:26:34 +03:00
|
|
|
static let basePath = "/tools/markdown"
|
|
|
|
static let saveFirstPath = "/tools/markdown/save-first.html"
|
|
|
|
static let errorPath = "/tools/markdown/error.html"
|
|
|
|
static let nonePath = "/tools/markdown/empty.html"
|
2017-02-06 21:22:28 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
func reduceOpenedFileList(_ pair: Pair) -> Pair {
|
|
|
|
var state = pair.state.payload
|
2017-01-22 16:22:05 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
switch pair.action {
|
2017-01-22 16:22:05 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
case let .open(buffer):
|
|
|
|
state.preview = PreviewUtils.state(for: pair.state.uuid, baseUrl: self.baseServerUrl, buffer: buffer)
|
2017-02-27 01:01:06 +03:00
|
|
|
|
2017-03-30 21:03:28 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
return StateActionPair(state: UuidState(uuid: pair.state.uuid, state: state), action: pair.action)
|
|
|
|
}
|
2017-02-27 01:01:06 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
func reduceMainWindow(_ pair: MainWindowPair) -> MainWindowPair {
|
|
|
|
var state = pair.state.payload
|
2017-02-27 01:01:06 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
switch pair.action {
|
2017-02-27 01:01:06 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
case let .setCurrentBuffer(buffer):
|
|
|
|
guard state.previewTool.isRefreshOnWrite else {
|
|
|
|
return pair
|
2017-02-27 01:01:06 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
state.preview = PreviewUtils.state(for: pair.state.uuid, baseUrl: self.baseServerUrl, buffer: buffer)
|
|
|
|
state.preview.ignoreNextReverse = true
|
2017-02-27 01:01:06 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
case .close:
|
|
|
|
state.preview = PreviewUtils.state(for: .none, baseUrl: self.baseServerUrl)
|
2017-02-27 01:01:06 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
default:
|
|
|
|
return pair
|
2017-03-30 21:03:28 +03:00
|
|
|
}
|
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
return StateActionPair(state: UuidState(uuid: pair.state.uuid, state: state), action: pair.action)
|
|
|
|
}
|
2017-02-27 01:01:06 +03:00
|
|
|
|
2017-04-30 10:25:15 +03:00
|
|
|
init(baseServerUrl: URL) {
|
|
|
|
self.baseServerUrl = baseServerUrl
|
2017-02-27 01:01:06 +03:00
|
|
|
}
|
2017-04-30 10:25:15 +03:00
|
|
|
|
|
|
|
fileprivate let baseServerUrl: URL
|
2017-01-22 16:22:05 +03:00
|
|
|
}
|