From 7092c226f14ce77adad0ad914160b4721eec7cdc Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Sun, 12 Feb 2017 11:24:15 +0100 Subject: [PATCH] Implement refresh now --- VimR.xcodeproj/project.pbxproj | 12 ++++++ VimR/Context.swift | 22 +++++------ VimR/PreviewTool.swift | 6 +-- VimR/PreviewToolTransformer.swift | 18 +++++---- VimR/PreviewTransformer.swift | 50 +---------------------- VimR/PreviewUtils.swift | 66 +++++++++++++++++++++++++++++++ VimR/States.swift | 4 +- 7 files changed, 104 insertions(+), 74 deletions(-) create mode 100644 VimR/PreviewUtils.swift diff --git a/VimR.xcodeproj/project.pbxproj b/VimR.xcodeproj/project.pbxproj index 39f6a7ea..76a5c4ca 100644 --- a/VimR.xcodeproj/project.pbxproj +++ b/VimR.xcodeproj/project.pbxproj @@ -29,6 +29,7 @@ 1929B73E5EC0B108B83F82EB /* FileItemService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B3A98687DF171307AAC8 /* FileItemService.swift */; }; 1929B7A2F2B423AA9740FD45 /* FileUtilsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B5D977261F1EBFA9E8F1 /* FileUtilsTest.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 */; }; 1929BA120290D6A2A61A4468 /* ArrayCommonsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B477E1E62433BC48E10B /* ArrayCommonsTest.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 = ""; }; 1929BBC84557C8351EC6183E /* FileItemIgnorePatternTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileItemIgnorePatternTest.swift; sourceTree = ""; }; 1929BC19C1BC19246AFF1621 /* MatcherTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MatcherTests.swift; sourceTree = ""; }; + 1929BC589C366CF02CDB4997 /* PreviewUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PreviewUtils.swift; sourceTree = ""; }; 1929BD4149D5A25C82064DD8 /* UiRoot.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UiRoot.swift; sourceTree = ""; }; 1929BD83A13BF133741766CC /* MainWindowTransformer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainWindowTransformer.swift; sourceTree = ""; }; 1929BD8CBADC191CF8C85309 /* MainWindow.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainWindow.swift; sourceTree = ""; }; @@ -543,6 +545,14 @@ path = resources; sourceTree = ""; }; + 1929B452B8E7E8DF65FC5F78 /* Utils */ = { + isa = PBXGroup; + children = ( + 1929BC589C366CF02CDB4997 /* PreviewUtils.swift */, + ); + name = Utils; + sourceTree = ""; + }; 1929B5E773BDB3B4EE9D00C1 /* Transformers */ = { isa = PBXGroup; children = ( @@ -573,6 +583,7 @@ 1929B32401E8914DE9BF76CA /* Components */, 1929B5E773BDB3B4EE9D00C1 /* Transformers */, 1929BFA93DC859DD76C46192 /* Services */, + 1929B452B8E7E8DF65FC5F78 /* Utils */, ); name = Redesign; sourceTree = ""; @@ -1386,6 +1397,7 @@ 1929B5C1BABBC0D09D97C3EF /* PreviewNewService.swift in Sources */, 1929B0F599D1F62C7BE53D2C /* HttpServerService.swift in Sources */, 1929B3AC66EFE35D68C020E3 /* PreviewToolTransformer.swift in Sources */, + 1929B902534935D0EADED52E /* PreviewUtils.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/VimR/Context.swift b/VimR/Context.swift index 9e8085b9..5d3d3af1 100644 --- a/VimR/Context.swift +++ b/VimR/Context.swift @@ -12,16 +12,15 @@ class StateContext { let actionEmitter = Emitter() init(_ initialState: AppState) { + self.httpServerService = HttpServerService(port: initialState.baseServerUrl.port ?? 0) self.appDelegateTransformer = AppDelegateTransformer(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.stateSource = self.stateSubject.asObservable() - let actionSource = self.actionEmitter.observable/*.do(onNext: { _ in - self.appState.mainWindows.keys.forEach { self.appState.mainWindows[$0]?.resetInstantStates() } - })*/ + let actionSource = self.actionEmitter.observable Observable .of( @@ -45,7 +44,7 @@ class StateContext { }) .addDisposableTo(self.disposeBag) - actionSource + let mainWindowSource = actionSource .mapOmittingNil { $0 as? UuidAction } .mapOmittingNil { action in guard let mainWindowState = self.appState.mainWindows[action.uuid] else { @@ -62,13 +61,8 @@ class StateContext { .apply(to: self.previewService) .apply(to: self.httpServerService) .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 } .mapOmittingNil { action in guard let mainWindowState = self.appState.mainWindows[action.uuid] else { @@ -82,6 +76,10 @@ class StateContext { .transform(by: self.previewToolTransformer) .filter { $0.modified } .map { $0.state } + + Observable + .of(mainWindowSource, previewToolSource) + .merge() .subscribe(onNext: { state in self.appState.mainWindows[state.uuid] = state.payload self.stateSubject.onNext(self.appState) @@ -109,7 +107,7 @@ class StateContext { fileprivate let uiRootTransformer = UiRootTransformer() fileprivate let mainWindowTransformer = MainWindowTransformer() fileprivate let previewTransformer: PreviewTransformer - fileprivate let previewToolTransformer = PreviewToolTransformer() + fileprivate let previewToolTransformer: PreviewToolTransformer fileprivate let previewService = PreviewNewService() fileprivate let httpServerService: HttpServerService diff --git a/VimR/PreviewTool.swift b/VimR/PreviewTool.swift index 914eb829..324d5289 100644 --- a/VimR/PreviewTool.swift +++ b/VimR/PreviewTool.swift @@ -15,7 +15,6 @@ class PreviewTool: NSView, UiComponent, WKNavigationDelegate { case refreshNow case reverseSearch(to: Marked) - case forwardSearch case scroll(to: Marked) @@ -168,22 +167,19 @@ extension PreviewTool { } 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))) } func automaticForwardSearchAction(_ sender: NSMenuItem) { - NSLog("\(sender.boolState) -> \(!sender.boolState)") self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.setAutomaticForwardSearch(to: !sender.boolState))) } func automaticReverseSearchAction(_ sender: NSMenuItem) { - NSLog("\(sender.boolState) -> \(!sender.boolState)") self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.setAutomaticReverseSearch(to: !sender.boolState))) } func refreshOnWriteAction(_ sender: NSMenuItem) { - NSLog("\(sender.boolState) -> \(!sender.boolState)") self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.setRefreshOnWrite(to: !sender.boolState))) } } diff --git a/VimR/PreviewToolTransformer.swift b/VimR/PreviewToolTransformer.swift index 00ec7bd0..626ace9c 100644 --- a/VimR/PreviewToolTransformer.swift +++ b/VimR/PreviewToolTransformer.swift @@ -4,14 +4,16 @@ */ import Foundation -import CocoaMarkdown import RxSwift -import CocoaMarkdown class PreviewToolTransformer: Transformer { typealias Pair = StateActionPair, PreviewTool.Action> + init(baseServerUrl: URL) { + self.baseServerUrl = baseServerUrl + } + func transform(_ source: Observable) -> Observable { return source.map { pair in var state = pair.state.payload @@ -19,13 +21,13 @@ class PreviewToolTransformer: Transformer { switch pair.action { case .refreshNow: - return pair + state.preview = PreviewUtils.state(for: pair.state.uuid, + baseUrl: self.baseServerUrl, + buffer: state.currentBuffer) case let .reverseSearch(to:position): - return pair - - case .forwardSearch: - return pair + state.preview.previewPosition = position + state.preview.ignoreNextForward = true case let .scroll(to:position): state.preview.previewPosition = position @@ -45,4 +47,6 @@ class PreviewToolTransformer: Transformer { return StateActionPair(state: UuidState(uuid: state.uuid, state: state), action: pair.action) } } + + fileprivate let baseServerUrl: URL } diff --git a/VimR/PreviewTransformer.swift b/VimR/PreviewTransformer.swift index 82057145..607683f9 100644 --- a/VimR/PreviewTransformer.swift +++ b/VimR/PreviewTransformer.swift @@ -4,11 +4,7 @@ */ import Foundation -import CocoaMarkdown import RxSwift -import CocoaMarkdown - -fileprivate let markdownPath = "tools/preview/markdown" // Currently supports only markdown class PreviewTransformer: Transformer { @@ -26,7 +22,6 @@ class PreviewTransformer: Transformer { func transform(_ source: Observable) -> Observable { return source.map { pair in - let uuid = pair.state.uuid var state = pair.state.payload switch pair.action { @@ -36,37 +31,10 @@ class PreviewTransformer: Transformer { return pair } - guard let url = buffer.url else { - 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()) + state.preview = PreviewUtils.state(for: pair.state.uuid, baseUrl: self.baseServerUrl, buffer: buffer) case .close: - state.preview = PreviewState(status: .none, - server: self.simpleServerUrl(with: PreviewTransformer.nonePath), - updateDate: Date()) + state.preview = PreviewUtils.state(for: .none, baseUrl: self.baseServerUrl) default: 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 } diff --git a/VimR/PreviewUtils.swift b/VimR/PreviewUtils.swift new file mode 100644 index 00000000..128b5093 --- /dev/null +++ b/VimR/PreviewUtils.swift @@ -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) +} diff --git a/VimR/States.swift b/VimR/States.swift index 5cdd6ae7..329a0a75 100644 --- a/VimR/States.swift +++ b/VimR/States.swift @@ -146,7 +146,7 @@ struct PreviewState { var html: URL? var server: URL? - var updateDate = Date.distantPast + var updateDate: Date var editorPosition = Marked(Position(row: 1, column: 1)) var previewPosition = Marked(Position(row: 1, column: 1)) @@ -156,7 +156,7 @@ struct PreviewState { buffer: URL? = nil, html: URL? = nil, server: URL? = nil, - updateDate: Date = Date.distantPast) + updateDate: Date = Date()) { self.status = status self.buffer = buffer