1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-28 11:35:35 +03:00

Load preview html

This commit is contained in:
Tae Won Ha 2017-02-05 22:39:55 +01:00
parent 2fc6e4bf5a
commit 0f00d53b22
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
6 changed files with 60 additions and 30 deletions

View File

@ -43,7 +43,8 @@ class HttpServerService: Service {
return
}
guard case let .markdown(file:buffer, html:html, server:server) = pair.state.payload.preview else {
let preview = pair.state.payload.preview
guard let buffer = preview.buffer, let html = preview.html, let server = preview.server else {
return
}

View File

@ -76,6 +76,7 @@ class MainWindow: NSObject,
self.neoVimView.configureForAutoLayout()
self.workspace = Workspace(mainView: self.neoVimView)
self.preview = PreviewTool(source: source, emitter: emitter, state: state)
self.windowController = NSWindowController(windowNibName: "MainWindow")
@ -149,10 +150,20 @@ class MainWindow: NSObject,
self.neoVimView.closeAllWindowsWithoutSaving()
}
fileprivate func setupTools() {
let previewConfig = WorkspaceTool.Config(title: "Preview", view: self.preview)
let previewContainer = WorkspaceTool(previewConfig)
previewContainer.dimension = 300
self.workspace.append(tool: previewContainer, location: .right)
previewContainer.toggle()
}
fileprivate func addViews() {
let contentView = self.window.contentView!
contentView.addSubview(self.workspace)
self.setupTools()
self.workspace.autoPinEdgesToSuperviewEdges()
}
@ -168,6 +179,8 @@ class MainWindow: NSObject,
fileprivate let workspace: Workspace
fileprivate let neoVimView: NeoVimView
fileprivate let preview: PreviewTool
fileprivate let scrollDebouncer = Debouncer<Action>(interval: 0.75)
fileprivate let cursorDebouncer = Debouncer<Action>(interval: 0.75)

View File

@ -33,20 +33,8 @@ class PreviewNewService: Service {
let uuid = pair.state.uuid
switch pair.state.payload.preview {
case let .markdown(file:file, html:html, server:_):
NSLog("\(file) -> \(html)")
do {
try self.render(file, to: html)
self.previewFiles[uuid] = html
} catch let error as NSError {
// FIXME: error handling!
NSLog("ERROR rendering \(file) to \(html): \(error)")
return
}
default:
let preview = pair.state.payload.preview
guard let buffer = preview.buffer, let html = preview.html else {
guard let previewUrl = self.previewFiles[uuid] else {
return
}
@ -54,6 +42,17 @@ class PreviewNewService: Service {
try? FileManager.default.removeItem(at: previewUrl)
self.previewFiles.removeValue(forKey: uuid)
return
}
NSLog("\(buffer) -> \(html)")
do {
try self.render(buffer, to: html)
self.previewFiles[uuid] = html
} catch let error as NSError {
// FIXME: error handling!
NSLog("ERROR rendering \(buffer) to \(html): \(error)")
return
}
}

View File

@ -20,8 +20,6 @@ class PreviewTool: NSView, UiComponent, WKNavigationDelegate {
typealias StateType = MainWindow.State
static let basePath = "tools/previews"
required init(source: Observable<StateType>, emitter: ActionEmitter, state: StateType) {
self.webview.configureForAutoLayout()
@ -32,6 +30,15 @@ class PreviewTool: NSView, UiComponent, WKNavigationDelegate {
self.webview.loadHTMLString("", baseURL: nil)
self.addViews()
source
.map { $0.preview }
.mapOmittingNil { $0.server }
.subscribe(onNext: { [unowned self] serverUrl in
let urlReq = URLRequest(url: serverUrl)
self.webview.load(urlReq)
})
.addDisposableTo(self.disposeBag)
}
fileprivate func addViews() {
@ -44,6 +51,7 @@ class PreviewTool: NSView, UiComponent, WKNavigationDelegate {
}
fileprivate let webview = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
fileprivate let disposeBag = DisposeBag()
fileprivate var isOpen = false
required init?(coder: NSCoder) {

View File

@ -29,26 +29,34 @@ class PreviewTransformer: Transformer {
case let .setCurrentBuffer(buffer):
guard let url = buffer.url else {
state.preview = .notSaved(server: self.serverUrl(for: uuid, lastComponent: "not-saved.html"))
state.preview = PreviewState(buffer: nil,
html: nil,
server: self.serverUrl(for: uuid, lastComponent: "save-first.html"))
break
}
guard FileUtils.fileExists(at: url) else {
state.preview = .error(server: self.serverUrl(for: uuid, lastComponent: "error.html"))
state.preview = PreviewState(buffer: nil,
html: nil,
server: self.serverUrl(for: uuid, lastComponent: "error.html"))
break
}
guard self.extensions.contains(url.pathExtension) else {
state.preview = .none(server: self.serverUrl(for: uuid, lastComponent: "none.html"))
state.preview = PreviewState(buffer: nil,
html: nil,
server: self.serverUrl(for: uuid, lastComponent: "none.html"))
break
}
state.preview = .markdown(file: url,
html: self.htmlUrl(with: uuid),
server: self.serverUrl(for: uuid, lastComponent: "index.html"))
state.preview = PreviewState(buffer: url,
html: self.htmlUrl(with: uuid),
server: self.serverUrl(for: uuid, lastComponent: "index.html"))
case .close:
state.preview = .none(server: self.serverUrl(for: uuid, lastComponent: "none.html"))
state.preview = PreviewState(buffer: nil,
html: nil,
server: self.serverUrl(for: uuid, lastComponent: "none.html"))
default:
return pair

View File

@ -111,12 +111,13 @@ struct AppState {
}
}
enum PreviewState {
struct PreviewState {
case none(server: URL)
case error(server: URL)
case notSaved(server: URL)
case markdown(file: URL, html: URL, server: URL)
static let `default` = PreviewState(buffer: nil, html: nil, server: nil)
var buffer: URL?
var html: URL?
var server: URL?
}
extension MainWindow {
@ -130,7 +131,7 @@ extension MainWindow {
////// transient
var preview = PreviewState.none(server: URL(string: "http://localhost/dummy")!)
var preview = PreviewState.default
var isClosed = false
// neovim