<commit_before>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2015-2017 <NAME> <<EMAIL>>
;; Copyright (c) 2015-2017 <NAME> <<EMAIL>>

(ns uxbox.main.ui.workspace
  (:require [beicon.core :as rx]
            [potok.core :as ptk]
            [lentes.core :as l]
            [uxbox.main.store :as st]
            [uxbox.main.constants :as c]
            [uxbox.main.refs :as refs]
            [uxbox.main.streams :as streams]
            [uxbox.main.data.workspace :as dw]
            [uxbox.main.data.pages :as udp]
            [uxbox.main.data.history :as udh]
            [uxbox.main.data.undo :as udu]
            [uxbox.main.user-events :as uev]
            [uxbox.main.ui.messages :refer [messages-widget]]
            [uxbox.main.ui.confirm]
            [uxbox.main.ui.workspace.images]
            [uxbox.main.ui.keyboard :as kbd]
            [uxbox.main.ui.workspace.scroll :as scroll]
            [uxbox.main.ui.workspace.download]
            [uxbox.main.ui.workspace.shortcuts :refer [shortcuts-mixin]]
            [uxbox.main.ui.workspace.header :refer [header]]
            [uxbox.main.ui.workspace.rules :refer [horizontal-rule vertical-rule]]
            [uxbox.main.ui.workspace.sidebar.history :refer [history-dialog]]
            [uxbox.main.ui.workspace.sidebar :refer [left-sidebar right-sidebar]]
            [uxbox.main.ui.workspace.colorpalette :refer [colorpalette]]
            [uxbox.main.ui.workspace.canvas :refer [viewport]]
            [uxbox.util.dom :as dom]
            [uxbox.util.geom.point :as gpt]
            [uxbox.util.data :refer [classnames]]
            [uxbox.util.mixins :as mx :include-macros true]))

;; --- Workspace

(defn- workspace-will-mount
  [own]
  (let [[projectid pageid] (:rum/args own)]
    (st/emit! (dw/initialize projectid pageid))
    own))

(defn- workspace-did-mount
  [own]
  (let [[projectid pageid] (:rum/args own)
        dom (mx/ref-node own "workspace-canvas")
        scroll-to-page-center #(scroll/scroll-to-page-center dom @refs/selected-page)
        sub (rx/subscribe streams/page-id-ref-s scroll-to-page-center)]

    (scroll-to-page-center)

    (st/emit! (udp/watch-page-changes pageid)
              (udu/watch-page-changes pageid)
              (udh/watch-page-changes pageid))

    (assoc own ::sub sub)))

(defn- workspace-will-unmount
  [own]
  (st/emit! ::udp/stop-page-watcher)
  (rx/cancel! (::sub own))
  (dissoc own ::sub))

(defn- workspace-did-remount
  [old-state state]
  (let [[projectid pageid] (:rum/args state)
        [oldprojectid oldpageid] (:rum/args old-state)]
    (when (not= pageid oldpageid)
      (st/emit! (dw/initialize projectid pageid)
                ::udp/stop-page-watcher
                (udp/watch-page-changes pageid)
                (udu/watch-page-changes pageid)
                (udh/watch-page-changes pageid)))
    state))

(defn- on-scroll
  [event]
  (let [target (.-target event)
        top (.-scrollTop target)
        left (.-scrollLeft target)]
    (st/emit! (uev/scroll-event (gpt/point left top)))))

(defn- on-wheel
  [own event]
  (when (kbd/ctrl? event)
    (let [prev-zoom @refs/selected-zoom
          dom (mx/ref-node own "workspace-canvas")
          scroll-position (scroll/get-current-position-absolute dom)
          mouse-point @refs/viewport-mouse-position]
      (dom/prevent-default event)
      (dom/stop-propagation event)
      (if (pos? (.-deltaY event))
        (st/emit! (dw/increase-zoom))
        (st/emit! (dw/decrease-zoom)))
      (scroll/scroll-to-point dom mouse-point scroll-position))))

(def ^:private workspace-page-ref
  (-> (l/key :page)
      (l/derive refs/workspace)))

(mx/defcs workspace<commit_msg>Invert scroll/zoom behavior.<commit_after>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2015-2017 <NAME> <<EMAIL>>
;; Copyright (c) 2015-2017 <NAME> <<EMAIL>>

(ns uxbox.main.ui.workspace
  (:require [beicon.core :as rx]
            [potok.core :as ptk]
            [lentes.core :as l]
            [uxbox.main.store :as st]
            [uxbox.main.constants :as c]
            [uxbox.main.refs :as refs]
            [uxbox.main.streams :as streams]
            [uxbox.main.data.workspace :as dw]
            [uxbox.main.data.pages :as udp]
            [uxbox.main.data.history :as udh]
            [uxbox.main.data.undo :as udu]
            [uxbox.main.user-events :as uev]
            [uxbox.main.ui.messages :refer [messages-widget]]
            [uxbox.main.ui.confirm]
            [uxbox.main.ui.workspace.images]
            [uxbox.main.ui.keyboard :as kbd]
            [uxbox.main.ui.workspace.scroll :as scroll]
            [uxbox.main.ui.workspace.download]
            [uxbox.main.ui.workspace.shortcuts :refer [shortcuts-mixin]]
            [uxbox.main.ui.workspace.header :refer [header]]
            [uxbox.main.ui.workspace.rules :refer [horizontal-rule vertical-rule]]
            [uxbox.main.ui.workspace.sidebar.history :refer [history-dialog]]
            [uxbox.main.ui.workspace.sidebar :refer [left-sidebar right-sidebar]]
            [uxbox.main.ui.workspace.colorpalette :refer [colorpalette]]
            [uxbox.main.ui.workspace.canvas :refer [viewport]]
            [uxbox.util.dom :as dom]
            [uxbox.util.geom.point :as gpt]
            [uxbox.util.data :refer [classnames]]
            [uxbox.util.mixins :as mx :include-macros true]))

;; --- Workspace

(defn- workspace-will-mount
  [own]
  (let [[projectid pageid] (:rum/args own)]
    (st/emit! (dw/initialize projectid pageid))
    own))

(defn- workspace-did-mount
  [own]
  (let [[projectid pageid] (:rum/args own)
        dom (mx/ref-node own "workspace-canvas")
        scroll-to-page-center #(scroll/scroll-to-page-center dom @refs/selected-page)
        sub (rx/subscribe streams/page-id-ref-s scroll-to-page-center)]

    (scroll-to-page-center)

    (st/emit! (udp/watch-page-changes pageid)
              (udu/watch-page-changes pageid)
              (udh/watch-page-changes pageid))

    (assoc own ::sub sub)))

(defn- workspace-will-unmount
  [own]
  (st/emit! ::udp/stop-page-watcher)
  (rx/cancel! (::sub own))
  (dissoc own ::sub))

(defn- workspace-did-remount
  [old-state state]
  (let [[projectid pageid] (:rum/args state)
        [oldprojectid oldpageid] (:rum/args old-state)]
    (when (not= pageid oldpageid)
      (st/emit! (dw/initialize projectid pageid)
                ::udp/stop-page-watcher
                (udp/watch-page-changes pageid)
                (udu/watch-page-changes pageid)
                (udh/watch-page-changes pageid)))
    state))

(defn- on-scroll
  [event]
  (let [target (.-target event)
        top (.-scrollTop target)
        left (.-scrollLeft target)]
    (st/emit! (uev/scroll-event (gpt/point left top)))))

(defn- on-wheel
  [own event]
  (when (kbd/ctrl? event)
    (let [prev-zoom @refs/selected-zoom
          dom (mx/ref-node own "workspace-canvas")
          scroll-position (scroll/get-current-position-absolute dom)
          mouse-point @refs/viewport-mouse-position]
      (dom/prevent-default event)
      (dom/stop-propagation event)
      (if (pos? (.-deltaY event))
        (st/emit! (dw/decrease-zoom))
        (st/emit! (dw/increase-zoom)))
      (scroll/scroll-to-point dom mouse-point scroll-position))))

(def ^:private workspace-page-ref
  (-> (l/key :page)
      (l/derive refs/workspace)))

(mx/defcs workspace
{'fraction_non_alphanumeric': 0.14888917760166298, 'fraction_numerical': 0.005196829933740418, 'mean_word_length': 2.2967880085653105, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5616631', 'n_tokens_mistral': 2690, 'n_tokens_neox': 2637, 'n_words': 563}