1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-28 08:13:17 +03:00

Implement refresh now

This commit is contained in:
Tae Won Ha 2017-02-12 11:24:15 +01:00
parent bce3e9e5f0
commit 7092c226f1
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
7 changed files with 104 additions and 74 deletions

View File

@ -29,6 +29,7 @@
1929B73E5EC0B108B83F82EB /* FileItemService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B3A98687DF171307AAC8 /* FileItemService.swift */; }; 1929B73E5EC0B108B83F82EB /* FileItemService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B3A98687DF171307AAC8 /* FileItemService.swift */; };
1929B7A2F2B423AA9740FD45 /* FileUtilsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B5D977261F1EBFA9E8F1 /* FileUtilsTest.swift */; }; 1929B7A2F2B423AA9740FD45 /* FileUtilsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B5D977261F1EBFA9E8F1 /* FileUtilsTest.swift */; };
1929B8FB248D71BF88A35761 /* PreviewTool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B6C6C7792B05164B0216 /* PreviewTool.swift */; }; 1929B8FB248D71BF88A35761 /* PreviewTool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B6C6C7792B05164B0216 /* PreviewTool.swift */; };
1929B902534935D0EADED52E /* PreviewUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BC589C366CF02CDB4997 /* PreviewUtils.swift */; };
1929B93DBAD09835E428F610 /* PrefPane.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BB251F74BEFC82CEEF84 /* PrefPane.swift */; }; 1929B93DBAD09835E428F610 /* PrefPane.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BB251F74BEFC82CEEF84 /* PrefPane.swift */; };
1929BA120290D6A2A61A4468 /* ArrayCommonsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B477E1E62433BC48E10B /* ArrayCommonsTest.swift */; }; 1929BA120290D6A2A61A4468 /* ArrayCommonsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B477E1E62433BC48E10B /* ArrayCommonsTest.swift */; };
1929BA3BB94B77E9AE051FE5 /* PreviewComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B8DA5AA33536F0082200 /* PreviewComponent.swift */; }; 1929BA3BB94B77E9AE051FE5 /* PreviewComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B8DA5AA33536F0082200 /* PreviewComponent.swift */; };
@ -325,6 +326,7 @@
1929BB8BCA48637156F92945 /* PreviewService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreviewService.swift; sourceTree = "<group>"; }; 1929BB8BCA48637156F92945 /* PreviewService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreviewService.swift; sourceTree = "<group>"; };
1929BBC84557C8351EC6183E /* FileItemIgnorePatternTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileItemIgnorePatternTest.swift; sourceTree = "<group>"; }; 1929BBC84557C8351EC6183E /* FileItemIgnorePatternTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileItemIgnorePatternTest.swift; sourceTree = "<group>"; };
1929BC19C1BC19246AFF1621 /* MatcherTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MatcherTests.swift; sourceTree = "<group>"; }; 1929BC19C1BC19246AFF1621 /* MatcherTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MatcherTests.swift; sourceTree = "<group>"; };
1929BC589C366CF02CDB4997 /* PreviewUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreviewUtils.swift; sourceTree = "<group>"; };
1929BD4149D5A25C82064DD8 /* UiRoot.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UiRoot.swift; sourceTree = "<group>"; }; 1929BD4149D5A25C82064DD8 /* UiRoot.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UiRoot.swift; sourceTree = "<group>"; };
1929BD83A13BF133741766CC /* MainWindowTransformer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainWindowTransformer.swift; sourceTree = "<group>"; }; 1929BD83A13BF133741766CC /* MainWindowTransformer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainWindowTransformer.swift; sourceTree = "<group>"; };
1929BD8CBADC191CF8C85309 /* MainWindow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainWindow.swift; sourceTree = "<group>"; }; 1929BD8CBADC191CF8C85309 /* MainWindow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainWindow.swift; sourceTree = "<group>"; };
@ -543,6 +545,14 @@
path = resources; path = resources;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
1929B452B8E7E8DF65FC5F78 /* Utils */ = {
isa = PBXGroup;
children = (
1929BC589C366CF02CDB4997 /* PreviewUtils.swift */,
);
name = Utils;
sourceTree = "<group>";
};
1929B5E773BDB3B4EE9D00C1 /* Transformers */ = { 1929B5E773BDB3B4EE9D00C1 /* Transformers */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@ -573,6 +583,7 @@
1929B32401E8914DE9BF76CA /* Components */, 1929B32401E8914DE9BF76CA /* Components */,
1929B5E773BDB3B4EE9D00C1 /* Transformers */, 1929B5E773BDB3B4EE9D00C1 /* Transformers */,
1929BFA93DC859DD76C46192 /* Services */, 1929BFA93DC859DD76C46192 /* Services */,
1929B452B8E7E8DF65FC5F78 /* Utils */,
); );
name = Redesign; name = Redesign;
sourceTree = "<group>"; sourceTree = "<group>";
@ -1386,6 +1397,7 @@
1929B5C1BABBC0D09D97C3EF /* PreviewNewService.swift in Sources */, 1929B5C1BABBC0D09D97C3EF /* PreviewNewService.swift in Sources */,
1929B0F599D1F62C7BE53D2C /* HttpServerService.swift in Sources */, 1929B0F599D1F62C7BE53D2C /* HttpServerService.swift in Sources */,
1929B3AC66EFE35D68C020E3 /* PreviewToolTransformer.swift in Sources */, 1929B3AC66EFE35D68C020E3 /* PreviewToolTransformer.swift in Sources */,
1929B902534935D0EADED52E /* PreviewUtils.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@ -12,16 +12,15 @@ class StateContext {
let actionEmitter = Emitter<Any>() let actionEmitter = Emitter<Any>()
init(_ initialState: AppState) { init(_ initialState: AppState) {
self.httpServerService = HttpServerService(port: initialState.baseServerUrl.port ?? 0)
self.appDelegateTransformer = AppDelegateTransformer(baseServerUrl: initialState.baseServerUrl) self.appDelegateTransformer = AppDelegateTransformer(baseServerUrl: initialState.baseServerUrl)
self.previewTransformer = PreviewTransformer(baseServerUrl: initialState.baseServerUrl) self.previewTransformer = PreviewTransformer(baseServerUrl: initialState.baseServerUrl)
self.httpServerService = HttpServerService(port: initialState.baseServerUrl.port ?? 0) self.previewToolTransformer = PreviewToolTransformer(baseServerUrl: initialState.baseServerUrl)
self.appState = initialState self.appState = initialState
self.stateSource = self.stateSubject.asObservable() self.stateSource = self.stateSubject.asObservable()
let actionSource = self.actionEmitter.observable/*.do(onNext: { _ in let actionSource = self.actionEmitter.observable
self.appState.mainWindows.keys.forEach { self.appState.mainWindows[$0]?.resetInstantStates() }
})*/
Observable Observable
.of( .of(
@ -45,7 +44,7 @@ class StateContext {
}) })
.addDisposableTo(self.disposeBag) .addDisposableTo(self.disposeBag)
actionSource let mainWindowSource = actionSource
.mapOmittingNil { $0 as? UuidAction<MainWindow.Action> } .mapOmittingNil { $0 as? UuidAction<MainWindow.Action> }
.mapOmittingNil { action in .mapOmittingNil { action in
guard let mainWindowState = self.appState.mainWindows[action.uuid] else { guard let mainWindowState = self.appState.mainWindows[action.uuid] else {
@ -62,13 +61,8 @@ class StateContext {
.apply(to: self.previewService) .apply(to: self.previewService)
.apply(to: self.httpServerService) .apply(to: self.httpServerService)
.map { $0.state } .map { $0.state }
.subscribe(onNext: { state in
self.appState.mainWindows[state.uuid] = state.payload
self.stateSubject.onNext(self.appState)
})
.addDisposableTo(self.disposeBag)
actionSource let previewToolSource = actionSource
.mapOmittingNil { $0 as? UuidAction<PreviewTool.Action> } .mapOmittingNil { $0 as? UuidAction<PreviewTool.Action> }
.mapOmittingNil { action in .mapOmittingNil { action in
guard let mainWindowState = self.appState.mainWindows[action.uuid] else { guard let mainWindowState = self.appState.mainWindows[action.uuid] else {
@ -82,6 +76,10 @@ class StateContext {
.transform(by: self.previewToolTransformer) .transform(by: self.previewToolTransformer)
.filter { $0.modified } .filter { $0.modified }
.map { $0.state } .map { $0.state }
Observable
.of(mainWindowSource, previewToolSource)
.merge()
.subscribe(onNext: { state in .subscribe(onNext: { state in
self.appState.mainWindows[state.uuid] = state.payload self.appState.mainWindows[state.uuid] = state.payload
self.stateSubject.onNext(self.appState) self.stateSubject.onNext(self.appState)
@ -109,7 +107,7 @@ class StateContext {
fileprivate let uiRootTransformer = UiRootTransformer() fileprivate let uiRootTransformer = UiRootTransformer()
fileprivate let mainWindowTransformer = MainWindowTransformer() fileprivate let mainWindowTransformer = MainWindowTransformer()
fileprivate let previewTransformer: PreviewTransformer fileprivate let previewTransformer: PreviewTransformer
fileprivate let previewToolTransformer = PreviewToolTransformer() fileprivate let previewToolTransformer: PreviewToolTransformer
fileprivate let previewService = PreviewNewService() fileprivate let previewService = PreviewNewService()
fileprivate let httpServerService: HttpServerService fileprivate let httpServerService: HttpServerService

View File

@ -15,7 +15,6 @@ class PreviewTool: NSView, UiComponent, WKNavigationDelegate {
case refreshNow case refreshNow
case reverseSearch(to: Marked<Position>) case reverseSearch(to: Marked<Position>)
case forwardSearch
case scroll(to: Marked<Position>) case scroll(to: Marked<Position>)
@ -168,22 +167,19 @@ extension PreviewTool {
} }
func reverseSearchAction(_: Any?) { func reverseSearchAction(_: Any?) {
self.previewPosition = Marked(self.previewPosition.payload) self.previewPosition = Marked(self.previewPosition.payload) // set a new mark
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.reverseSearch(to: self.previewPosition))) self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.reverseSearch(to: self.previewPosition)))
} }
func automaticForwardSearchAction(_ sender: NSMenuItem) { func automaticForwardSearchAction(_ sender: NSMenuItem) {
NSLog("\(sender.boolState) -> \(!sender.boolState)")
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.setAutomaticForwardSearch(to: !sender.boolState))) self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.setAutomaticForwardSearch(to: !sender.boolState)))
} }
func automaticReverseSearchAction(_ sender: NSMenuItem) { func automaticReverseSearchAction(_ sender: NSMenuItem) {
NSLog("\(sender.boolState) -> \(!sender.boolState)")
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.setAutomaticReverseSearch(to: !sender.boolState))) self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.setAutomaticReverseSearch(to: !sender.boolState)))
} }
func refreshOnWriteAction(_ sender: NSMenuItem) { func refreshOnWriteAction(_ sender: NSMenuItem) {
NSLog("\(sender.boolState) -> \(!sender.boolState)")
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.setRefreshOnWrite(to: !sender.boolState))) self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.setRefreshOnWrite(to: !sender.boolState)))
} }
} }

View File

@ -4,14 +4,16 @@
*/ */
import Foundation import Foundation
import CocoaMarkdown
import RxSwift import RxSwift
import CocoaMarkdown
class PreviewToolTransformer: Transformer { class PreviewToolTransformer: Transformer {
typealias Pair = StateActionPair<UuidState<MainWindow.State>, PreviewTool.Action> typealias Pair = StateActionPair<UuidState<MainWindow.State>, PreviewTool.Action>
init(baseServerUrl: URL) {
self.baseServerUrl = baseServerUrl
}
func transform(_ source: Observable<Pair>) -> Observable<Pair> { func transform(_ source: Observable<Pair>) -> Observable<Pair> {
return source.map { pair in return source.map { pair in
var state = pair.state.payload var state = pair.state.payload
@ -19,13 +21,13 @@ class PreviewToolTransformer: Transformer {
switch pair.action { switch pair.action {
case .refreshNow: case .refreshNow:
return pair state.preview = PreviewUtils.state(for: pair.state.uuid,
baseUrl: self.baseServerUrl,
buffer: state.currentBuffer)
case let .reverseSearch(to:position): case let .reverseSearch(to:position):
return pair state.preview.previewPosition = position
state.preview.ignoreNextForward = true
case .forwardSearch:
return pair
case let .scroll(to:position): case let .scroll(to:position):
state.preview.previewPosition = position state.preview.previewPosition = position
@ -45,4 +47,6 @@ class PreviewToolTransformer: Transformer {
return StateActionPair(state: UuidState(uuid: state.uuid, state: state), action: pair.action) return StateActionPair(state: UuidState(uuid: state.uuid, state: state), action: pair.action)
} }
} }
fileprivate let baseServerUrl: URL
} }

View File

@ -4,11 +4,7 @@
*/ */
import Foundation import Foundation
import CocoaMarkdown
import RxSwift import RxSwift
import CocoaMarkdown
fileprivate let markdownPath = "tools/preview/markdown"
// Currently supports only markdown // Currently supports only markdown
class PreviewTransformer: Transformer { class PreviewTransformer: Transformer {
@ -26,7 +22,6 @@ class PreviewTransformer: Transformer {
func transform(_ source: Observable<Pair>) -> Observable<Pair> { func transform(_ source: Observable<Pair>) -> Observable<Pair> {
return source.map { pair in return source.map { pair in
let uuid = pair.state.uuid
var state = pair.state.payload var state = pair.state.payload
switch pair.action { switch pair.action {
@ -36,37 +31,10 @@ class PreviewTransformer: Transformer {
return pair return pair
} }
guard let url = buffer.url else { state.preview = PreviewUtils.state(for: pair.state.uuid, baseUrl: self.baseServerUrl, buffer: buffer)
state.preview = PreviewState(status: .notSaved,
server: self.simpleServerUrl(with: PreviewTransformer.saveFirstPath),
updateDate: Date())
break
}
guard FileUtils.fileExists(at: url) else {
state.preview = PreviewState(status: .error,
server: self.simpleServerUrl(with: PreviewTransformer.errorPath),
updateDate: Date())
break
}
guard self.extensions.contains(url.pathExtension) else {
state.preview = PreviewState(status: .none,
server: self.simpleServerUrl(with: PreviewTransformer.nonePath),
updateDate: Date())
break
}
state.preview = PreviewState(status: .markdown,
buffer: url,
html: self.htmlUrl(with: uuid),
server: self.serverUrl(for: uuid, lastComponent: "index.html"),
updateDate: Date())
case .close: case .close:
state.preview = PreviewState(status: .none, state.preview = PreviewUtils.state(for: .none, baseUrl: self.baseServerUrl)
server: self.simpleServerUrl(with: PreviewTransformer.nonePath),
updateDate: Date())
default: default:
return pair return pair
@ -76,19 +44,5 @@ class PreviewTransformer: Transformer {
} }
} }
fileprivate func serverUrl(for uuid: String, lastComponent: String) -> URL {
return self.baseServerUrl.appendingPathComponent("\(uuid)/\(markdownPath)/\(lastComponent)")
}
fileprivate func htmlUrl(with uuid: String) -> URL {
return self.tempDir.appendingPathComponent("\(uuid)-markdown-index.html")
}
fileprivate func simpleServerUrl(with path: String) -> URL {
return self.baseServerUrl.appendingPathComponent(path)
}
fileprivate let extensions = Set(["md", "markdown"])
fileprivate let tempDir = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
fileprivate let baseServerUrl: URL fileprivate let baseServerUrl: URL
} }

66
VimR/PreviewUtils.swift Normal file
View File

@ -0,0 +1,66 @@
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Foundation
fileprivate let markdownPath = "tools/preview/markdown"
class PreviewUtils {
static func state(for status: PreviewState.Status, baseUrl: URL) -> PreviewState {
switch status {
case .none:
return PreviewState(status: .none,
server: self.simpleServerUrl(with: PreviewTransformer.nonePath, baseUrl: baseUrl))
case .error:
return PreviewState(status: .error,
server: self.simpleServerUrl(with: PreviewTransformer.errorPath, baseUrl: baseUrl))
case .notSaved:
return PreviewState(status: .notSaved,
server: self.simpleServerUrl(with: PreviewTransformer.saveFirstPath, baseUrl: baseUrl))
case .markdown:
preconditionFailure("ERROR Use the other previewState()!")
}
}
static func state(for uuid: String, baseUrl: URL, buffer: NeoVimBuffer?) -> PreviewState {
guard let url = buffer?.url else {
return self.state(for: .notSaved, baseUrl: baseUrl)
}
guard FileUtils.fileExists(at: url) else {
return self.state(for: .error, baseUrl: baseUrl)
}
guard self.extensions.contains(url.pathExtension) else {
return self.state(for: .none, baseUrl: baseUrl)
}
return PreviewState(status: .markdown,
buffer: url,
html: self.htmlUrl(with: uuid),
server: self.serverUrl(for: uuid, baseUrl: baseUrl, lastComponent: "index.html"))
}
fileprivate static func serverUrl(for uuid: String, baseUrl: URL, lastComponent: String) -> URL {
return baseUrl.appendingPathComponent("\(uuid)/\(markdownPath)/\(lastComponent)")
}
fileprivate static func htmlUrl(with uuid: String) -> URL {
return self.tempDir.appendingPathComponent("\(uuid)-markdown-index.html")
}
fileprivate static func simpleServerUrl(with path: String, baseUrl: URL) -> URL {
return baseUrl.appendingPathComponent(path)
}
fileprivate static let extensions = Set(["md", "markdown"])
fileprivate static let tempDir = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
}

View File

@ -146,7 +146,7 @@ struct PreviewState {
var html: URL? var html: URL?
var server: URL? var server: URL?
var updateDate = Date.distantPast var updateDate: Date
var editorPosition = Marked(Position(row: 1, column: 1)) var editorPosition = Marked(Position(row: 1, column: 1))
var previewPosition = Marked(Position(row: 1, column: 1)) var previewPosition = Marked(Position(row: 1, column: 1))
@ -156,7 +156,7 @@ struct PreviewState {
buffer: URL? = nil, buffer: URL? = nil,
html: URL? = nil, html: URL? = nil,
server: URL? = nil, server: URL? = nil,
updateDate: Date = Date.distantPast) updateDate: Date = Date())
{ {
self.status = status self.status = status
self.buffer = buffer self.buffer = buffer