2017-01-22 16:22:05 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
2017-01-17 21:47:59 +03:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RxSwift
|
|
|
|
|
|
|
|
typealias ActionEmitter = Emitter<Any>
|
|
|
|
|
|
|
|
class Emitter<T> {
|
|
|
|
|
|
|
|
let observable: Observable<T>
|
|
|
|
|
|
|
|
init() {
|
|
|
|
self.observable = self.subject.asObservable().observeOn(scheduler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func emit(_ action: T) {
|
|
|
|
self.subject.onNext(action)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
self.subject.onCompleted()
|
|
|
|
}
|
|
|
|
|
|
|
|
fileprivate let scheduler = SerialDispatchQueueScheduler(qos: .userInitiated)
|
|
|
|
fileprivate let subject = PublishSubject<T>()
|
|
|
|
}
|
|
|
|
|
|
|
|
class StateActionPair<S, A> {
|
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
let modified: Bool
|
2017-01-17 21:47:59 +03:00
|
|
|
let state: S
|
|
|
|
let action: A
|
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
init(state: S, action: A, modified: Bool = true) {
|
|
|
|
self.modified = modified
|
2017-01-17 21:47:59 +03:00
|
|
|
self.state = state
|
|
|
|
self.action = action
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UuidAction<A>: CustomStringConvertible {
|
|
|
|
|
|
|
|
let uuid: String
|
|
|
|
let payload: A
|
|
|
|
|
|
|
|
var description: String {
|
|
|
|
return "UuidAction(uuid: \(uuid), payload: \(String(reflecting: payload)))"
|
|
|
|
}
|
|
|
|
|
|
|
|
init(uuid: String, action: A) {
|
|
|
|
self.uuid = uuid
|
|
|
|
self.payload = action
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UuidState<S>: CustomStringConvertible {
|
|
|
|
|
|
|
|
let uuid: String
|
|
|
|
let payload: S
|
|
|
|
|
|
|
|
var description: String {
|
2017-02-03 00:39:05 +03:00
|
|
|
return "UuidState(uuid: \(uuid), payload: \(String(reflecting: payload)))"
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
init(uuid: String, state: S) {
|
|
|
|
self.uuid = uuid
|
|
|
|
self.payload = state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 02:31:49 +03:00
|
|
|
class Marked<T> {
|
|
|
|
|
|
|
|
let mark = Token()
|
|
|
|
let payload: T
|
|
|
|
|
|
|
|
init(_ payload: T) {
|
|
|
|
self.payload = payload
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
protocol Morpher {
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
associatedtype In
|
|
|
|
associatedtype Out
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
func transform(_ source: Observable<In>) -> Observable<Out>
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
protocol Transformer: Morpher {
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
associatedtype Element
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
typealias In = Element
|
|
|
|
typealias Out = Element
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
func transform(_ source: Observable<Element>) -> Observable<Element>
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
|
2017-02-05 17:17:25 +03:00
|
|
|
protocol Service {
|
|
|
|
|
|
|
|
associatedtype Element
|
|
|
|
|
|
|
|
func apply(_: Element)
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
struct AppState {
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-02-06 20:57:50 +03:00
|
|
|
static let `default` = AppState(baseServerUrl: URL(string: "http://localhost:\(NetUtils.openPort())")!,
|
|
|
|
mainWindow: MainWindow.State.default)
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
var currentMainWindow: MainWindow.State
|
|
|
|
var mainWindows: [String: MainWindow.State] = [:]
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
let baseServerUrl: URL
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-02-06 20:57:50 +03:00
|
|
|
init(baseServerUrl: URL, mainWindow: MainWindow.State) {
|
|
|
|
self.baseServerUrl = baseServerUrl
|
2017-01-22 16:22:05 +03:00
|
|
|
self.currentMainWindow = mainWindow
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 00:39:55 +03:00
|
|
|
struct PreviewState {
|
2017-02-03 00:39:05 +03:00
|
|
|
|
2017-02-07 00:54:22 +03:00
|
|
|
static let `default` = PreviewState()
|
2017-02-06 20:36:57 +03:00
|
|
|
|
|
|
|
enum Status {
|
|
|
|
|
|
|
|
case none
|
|
|
|
case notSaved
|
|
|
|
case error
|
|
|
|
case markdown
|
|
|
|
}
|
|
|
|
|
|
|
|
var status = Status.none
|
2017-02-06 00:39:55 +03:00
|
|
|
|
|
|
|
var buffer: URL?
|
|
|
|
var html: URL?
|
|
|
|
var server: URL?
|
2017-02-07 00:54:22 +03:00
|
|
|
|
|
|
|
var updateDate = Date.distantPast
|
|
|
|
|
2017-02-09 02:31:49 +03:00
|
|
|
var editorPosition = Marked(Position(row: 1, column: 1))
|
|
|
|
var previewPosition = Marked(Position(row: 1, column: 1))
|
2017-02-07 00:54:22 +03:00
|
|
|
|
|
|
|
init(status: Status = .none,
|
|
|
|
buffer: URL? = nil,
|
|
|
|
html: URL? = nil,
|
|
|
|
server: URL? = nil,
|
2017-02-09 02:31:49 +03:00
|
|
|
updateDate: Date = Date.distantPast)
|
2017-02-07 00:54:22 +03:00
|
|
|
{
|
|
|
|
self.status = status
|
|
|
|
self.buffer = buffer
|
|
|
|
self.html = html
|
|
|
|
self.server = server
|
|
|
|
self.updateDate = updateDate
|
|
|
|
}
|
2017-02-03 00:39:05 +03:00
|
|
|
}
|
|
|
|
|
2017-01-17 21:47:59 +03:00
|
|
|
extension MainWindow {
|
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
struct State {
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-02-04 17:34:13 +03:00
|
|
|
static let `default` = State(isAllToolsVisible: true, isToolButtonsVisible: true)
|
2017-01-17 21:47:59 +03:00
|
|
|
|
|
|
|
var isAllToolsVisible = true
|
|
|
|
var isToolButtonsVisible = true
|
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
////// transient
|
|
|
|
|
2017-02-06 00:39:55 +03:00
|
|
|
var preview = PreviewState.default
|
2017-02-07 00:54:22 +03:00
|
|
|
var previewTool = PreviewTool.State.default
|
|
|
|
|
2017-02-04 17:34:13 +03:00
|
|
|
var isClosed = false
|
2017-02-03 00:39:05 +03:00
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
// neovim
|
2017-01-17 21:47:59 +03:00
|
|
|
var uuid = UUID().uuidString
|
2017-01-22 16:22:05 +03:00
|
|
|
var currentBuffer: NeoVimBuffer?
|
2017-01-17 21:47:59 +03:00
|
|
|
var buffers = [NeoVimBuffer]()
|
|
|
|
var cwd = FileUtils.userHomeUrl
|
|
|
|
|
|
|
|
var isDirty = false
|
|
|
|
|
|
|
|
var font = NSFont.userFixedPitchFont(ofSize: 13)!
|
|
|
|
var linespacing: CGFloat = 1
|
|
|
|
var isUseLigatures = false
|
|
|
|
var isUseInteractiveZsh = false
|
|
|
|
|
|
|
|
// transient^2
|
|
|
|
var urlsToOpen = [URL: OpenMode]()
|
|
|
|
|
2017-02-04 17:34:13 +03:00
|
|
|
init(isAllToolsVisible: Bool, isToolButtonsVisible: Bool) {
|
2017-01-17 21:47:59 +03:00
|
|
|
self.isAllToolsVisible = isAllToolsVisible
|
|
|
|
self.isToolButtonsVisible = isToolButtonsVisible
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 00:54:22 +03:00
|
|
|
extension PreviewTool {
|
|
|
|
|
|
|
|
struct State {
|
|
|
|
|
|
|
|
static let `default` = State()
|
|
|
|
|
|
|
|
var isForwardSearchAutomatically = false
|
|
|
|
var isReverseSearchAutomatically = false
|
|
|
|
var isRefreshOnWrite = true
|
|
|
|
}
|
|
|
|
}
|