mirror of
https://github.com/qvacua/vimr.git
synced 2024-12-27 15:53:31 +03:00
Use marked positions to distinguish the cases to set the position
This commit is contained in:
parent
b62a51f286
commit
379068de13
@ -12,14 +12,17 @@ class StateContext {
|
||||
let actionEmitter = Emitter<Any>()
|
||||
|
||||
init(_ initialState: AppState) {
|
||||
self.appState = initialState
|
||||
self.stateSource = self.stateSubject.asObservable()
|
||||
let actionSource = self.actionEmitter.observable
|
||||
|
||||
self.appDelegateTransformer = AppDelegateTransformer(baseServerUrl: initialState.baseServerUrl)
|
||||
self.previewTransformer = PreviewTransformer(baseServerUrl: initialState.baseServerUrl)
|
||||
self.httpServerService = HttpServerService(port: initialState.baseServerUrl.port ?? 0)
|
||||
|
||||
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() }
|
||||
})*/
|
||||
|
||||
Observable
|
||||
.of(
|
||||
actionSource
|
||||
|
@ -31,7 +31,7 @@ class MainWindowTransformer: Transformer {
|
||||
state.currentBuffer = buffer
|
||||
|
||||
case let .scroll(to: position), let .setCursor(to: position):
|
||||
state.cursorPosition = position
|
||||
state.preview.editorPosition = Marked(position)
|
||||
|
||||
case .close:
|
||||
state.isClosed = true
|
||||
|
@ -80,15 +80,18 @@ class PreviewTool: NSView, UiComponent, WKNavigationDelegate {
|
||||
source
|
||||
.observeOn(MainScheduler.instance)
|
||||
.subscribe(onNext: { [unowned self] state in
|
||||
self.currentEditorPosition = state.cursorPosition
|
||||
self.automaticForwardMenuItem.boolState = state.previewTool.isForwardSearchAutomatically
|
||||
self.automaticReverseMenuItem.boolState = state.previewTool.isReverseSearchAutomatically
|
||||
self.refreshOnWriteMenuItem.boolState = state.previewTool.isRefreshOnWrite
|
||||
|
||||
if state.previewTool.isForwardSearchAutomatically {
|
||||
self.forwardSearch(position: state.cursorPosition)
|
||||
if state.previewTool.isForwardSearchAutomatically
|
||||
&& state.preview.editorPosition.mark != self.editorPosition.mark
|
||||
{
|
||||
self.forwardSearch(position: state.preview.editorPosition.payload)
|
||||
}
|
||||
|
||||
self.editorPosition = state.preview.editorPosition
|
||||
|
||||
guard state.preview.updateDate > self.lastUpdateDate else { return }
|
||||
guard let serverUrl = state.preview.server else { return }
|
||||
|
||||
@ -100,7 +103,7 @@ class PreviewTool: NSView, UiComponent, WKNavigationDelegate {
|
||||
self.webviewMessageHandler.source
|
||||
.throttle(0.75, latest: true, scheduler: self.scheduler)
|
||||
.subscribe(onNext: { [unowned self] position in
|
||||
self.currentPreviewPosition = position
|
||||
self.previewPosition = position
|
||||
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.reverseSearch(to: position)))
|
||||
})
|
||||
.addDisposableTo(self.disposeBag)
|
||||
@ -129,8 +132,8 @@ class PreviewTool: NSView, UiComponent, WKNavigationDelegate {
|
||||
fileprivate var isOpen = false
|
||||
|
||||
fileprivate var lastUpdateDate = Date.distantPast
|
||||
fileprivate var currentEditorPosition = Position(row: 1, column: 1)
|
||||
fileprivate var currentPreviewPosition = Position(row: 1, column: 1)
|
||||
fileprivate var editorPosition = Marked(Position(row: 1, column: 1))
|
||||
fileprivate var previewPosition = Position(row: 1, column: 1)
|
||||
|
||||
fileprivate let userContentController = WKUserContentController()
|
||||
fileprivate let webviewMessageHandler = WebviewMessageHandler()
|
||||
@ -160,11 +163,11 @@ extension PreviewTool {
|
||||
}
|
||||
|
||||
func forwardSearchAction(_: Any?) {
|
||||
self.forwardSearch(position: self.currentEditorPosition)
|
||||
self.forwardSearch(position: self.editorPosition.payload)
|
||||
}
|
||||
|
||||
func reverseSearchAction(_: Any?) {
|
||||
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.reverseSearch(to: self.currentPreviewPosition)))
|
||||
self.emitter.emit(UuidAction(uuid: self.uuid, action: Action.reverseSearch(to: self.previewPosition)))
|
||||
}
|
||||
|
||||
func automaticForwardSearchAction(_ sender: NSMenuItem) {
|
||||
|
@ -13,6 +13,35 @@ class PreviewToolTransformer: Transformer {
|
||||
typealias Pair = StateActionPair<UuidState<MainWindow.State>, PreviewTool.Action>
|
||||
|
||||
func transform(_ source: Observable<Pair>) -> Observable<Pair> {
|
||||
return source
|
||||
return source.map { pair in
|
||||
var state = pair.state.payload
|
||||
|
||||
switch pair.action {
|
||||
|
||||
case .refreshNow:
|
||||
return pair
|
||||
|
||||
case let .reverseSearch(to:position):
|
||||
return pair
|
||||
|
||||
case .forwardSearch:
|
||||
return pair
|
||||
|
||||
case let .scroll(to:position):
|
||||
state.preview.previewPosition = Marked(position)
|
||||
|
||||
case let .setAutomaticReverseSearch(to:value):
|
||||
return pair
|
||||
|
||||
case let .setAutomaticForwardSearch(to:value):
|
||||
return pair
|
||||
|
||||
case let .setRefreshOnWrite(to:value):
|
||||
return pair
|
||||
|
||||
}
|
||||
|
||||
return StateActionPair(state: UuidState(uuid: state.uuid, state: state), action: pair.action)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -71,6 +71,16 @@ class UuidState<S>: CustomStringConvertible {
|
||||
}
|
||||
}
|
||||
|
||||
class Marked<T> {
|
||||
|
||||
let mark = Token()
|
||||
let payload: T
|
||||
|
||||
init(_ payload: T) {
|
||||
self.payload = payload
|
||||
}
|
||||
}
|
||||
|
||||
protocol Morpher {
|
||||
|
||||
associatedtype In
|
||||
@ -132,21 +142,20 @@ struct PreviewState {
|
||||
|
||||
var updateDate = Date.distantPast
|
||||
|
||||
var scrollPosition = Position(row: 1, column: 1)
|
||||
var editorPosition = Marked(Position(row: 1, column: 1))
|
||||
var previewPosition = Marked(Position(row: 1, column: 1))
|
||||
|
||||
init(status: Status = .none,
|
||||
buffer: URL? = nil,
|
||||
html: URL? = nil,
|
||||
server: URL? = nil,
|
||||
updateDate: Date = Date.distantPast,
|
||||
scrollPosition: Position = Position(row: 1, column: 1))
|
||||
updateDate: Date = Date.distantPast)
|
||||
{
|
||||
self.status = status
|
||||
self.buffer = buffer
|
||||
self.html = html
|
||||
self.server = server
|
||||
self.updateDate = updateDate
|
||||
self.scrollPosition = scrollPosition
|
||||
}
|
||||
}
|
||||
|
||||
@ -171,7 +180,6 @@ extension MainWindow {
|
||||
var currentBuffer: NeoVimBuffer?
|
||||
var buffers = [NeoVimBuffer]()
|
||||
var cwd = FileUtils.userHomeUrl
|
||||
var cursorPosition = Position(row: 1, column: 1)
|
||||
|
||||
var isDirty = false
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user