<commit_before>/**
 * <NAME> - http://taewon.de - @hataewon
 * See LICENSE
 */

import Cocoa
import RxSwift
import PureLayout
import WebKit
import EonilFileSystemEvents

private let fileSystemEventsLatency = 1.0
private let monitorDispatchQueue = DispatchQueue.global(qos: .userInitiated)

class HtmlPreviewTool: NSView, UiComponent, WKNavigationDelegate {

  enum Action {

    case selectHtmlFile(URL)
  }

  typealias StateType = MainWindow.State

  let innerCustomToolbar = InnerCustomToolbar()

  required init(source: Observable<StateType>, emitter: ActionEmitter, state: StateType) {
    self.emit = emitter.typedEmit()
    self.uuid = state.uuid

    let configuration = WKWebViewConfiguration()
    self.webview = WKWebView(frame: CGRect.zero, configuration: configuration)

    super.init(frame: .zero)
    self.configureForAutoLayout()

    self.webview.navigationDelegate = self
    self.innerCustomToolbar.htmlPreviewTool = self

    self.addViews()

    if let serverUrl = state.htmlPreview.server?.payload {
      self.webview.load(URLRequest(url: serverUrl))
    }

    source
      .observeOn(MainScheduler.instance)
      .subscribe(onNext: { [unowned self] state in
        guard let serverUrl = state.htmlPreview.server, let htmlFileUrl = state.htmlPreview.htmlFile else {
          self.monitor = nil
          return
        }

        if serverUrl.mark == self.mark {
          return
        }

        self.monitor = FileSystemEventMonitor(pathsToWatch: [htmlFileUrl.path],
                                              latency: fileSystemEventsLatency,
                                              watchRoot: false,
                                              queue: monitorDispatchQueue)
        { [unowned self] events in
          self.reloadWebview(with: serverUrl.payload)
        }

        self.innerCustomToolbar.selectHtmlFile.toolTip = (htmlFileUrl.path as NSString).abbreviatingWithTildeInPath
        self.mark = serverUrl.mark
        self.reloadWebview(with: serverUrl.payload)
      })
      .disposed(by: self.disposeBag)
  }

  private func reloadWebview(with url: URL) {
    self.webview.evaluateJavaScript("document.body.scrollTop") { (result, error) in
      self.scrollTop = result as? Int ?? 0

      self.webview.load(URLRequest(url: url))
    }
  }

  private func addViews() {
    self.webview.configureForAutoLayout()<commit_msg>GH-609 Fix HTML preview<commit_after>/**
 * <NAME> - http://taewon.de - @hataewon
 * See LICENSE
 */

import Cocoa
import RxSwift
import PureLayout
import WebKit
import EonilFileSystemEvents

private let fileSystemEventsLatency = 1.0
private let monitorDispatchQueue = DispatchQueue.global(qos: .userInitiated)

class HtmlPreviewTool: NSView, UiComponent, WKNavigationDelegate {

  enum Action {

    case selectHtmlFile(URL)
  }

  typealias StateType = MainWindow.State

  let innerCustomToolbar = InnerCustomToolbar()

  required init(source: Observable<StateType>, emitter: ActionEmitter, state: StateType) {
    self.emit = emitter.typedEmit()
    self.uuid = state.uuid

    let configuration = WKWebViewConfiguration()
    self.webview = WKWebView(frame: CGRect.zero, configuration: configuration)

    super.init(frame: .zero)
    self.configureForAutoLayout()

    self.webview.navigationDelegate = self
    self.innerCustomToolbar.htmlPreviewTool = self

    self.addViews()

    if let serverUrl = state.htmlPreview.server?.payload {
      self.webview.load(URLRequest(url: serverUrl))
    }

    source
      .observeOn(MainScheduler.instance)
      .subscribe(onNext: { [unowned self] state in
        guard let serverUrl = state.htmlPreview.server, let htmlFileUrl = state.htmlPreview.htmlFile else {
          self.monitor = nil
          return
        }

        if serverUrl.mark == self.mark {
          return
        }

        self.monitor = FileSystemEventMonitor(pathsToWatch: [htmlFileUrl.path],
                                              latency: fileSystemEventsLatency,
                                              watchRoot: false,
                                              queue: monitorDispatchQueue)
        { [unowned self] events in
          self.reloadWebview(with: serverUrl.payload)
        }

        self.innerCustomToolbar.selectHtmlFile.toolTip = (htmlFileUrl.path as NSString).abbreviatingWithTildeInPath
        self.mark = serverUrl.mark
        self.reloadWebview(with: serverUrl.payload)
      })
      .disposed(by: self.disposeBag)
  }

  private func reloadWebview(with url: URL) {
    DispatchQueue.main.async {
      self.webview.evaluateJavaScript("document.body.scrollTop") { (result, error) in
        self.scrollTop = result as? Int ?? 0
      }
    }

    self.webview.load(URLRequest(url: url))
  }

  private func addViews() {
    self.webview.configureForAutoLayout()
{'fraction_non_alphanumeric': 0.08860759493670886, 'fraction_numerical': 0.001867607387424777, 'mean_word_length': 2.6295180722891565, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7635627', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1329, 'n_words': 283}