diff --git a/frontend/src/uxbox/main/data/workspace_websocket.cljs b/frontend/src/uxbox/main/data/workspace_websocket.cljs deleted file mode 100644 index 353ddafa6..000000000 --- a/frontend/src/uxbox/main/data/workspace_websocket.cljs +++ /dev/null @@ -1,127 +0,0 @@ -;; 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) 2019 Andrey Antukh - -(ns uxbox.main.data.workspace-websocket - (:require - [beicon.core :as rx] - [cljs.spec.alpha :as s] - [potok.core :as ptk] - [uxbox.config :as cfg] - [uxbox.common.data :as d] - [uxbox.common.pages :as cp] - [uxbox.main.websockets :as ws] - [uxbox.main.data.icons :as udi] - [uxbox.main.data.projects :as dp] - [uxbox.main.repo.core :as rp] - [uxbox.main.store :as st] - [uxbox.util.transit :as t] - [vendor.randomcolor])) - -;; --- Initialize WebSocket - -(declare fetch-users) -(declare handle-who) -(declare handle-pointer-update) -(declare handle-page-snapshot) - -(s/def ::type keyword?) -(s/def ::message - (s/keys :req-un [::type])) - -(defn initialize - [file-id] - (ptk/reify ::initialize - ptk/UpdateEvent - (update [_ state] - (let [uri (str "ws://localhost:6060/sub/" file-id)] - (assoc-in state [:ws file-id] (ws/open uri)))) - - ptk/WatchEvent - (watch [_ state stream] - (let [wsession (get-in state [:ws file-id])] - (->> (rx/merge - (rx/of (fetch-users file-id)) - (->> (ws/-stream wsession) - (rx/filter #(= :message (:type %))) - (rx/map (comp t/decode :payload)) - (rx/filter #(s/valid? ::message %)) - (rx/map (fn [{:keys [type] :as msg}] - (case type - :who (handle-who msg) - :pointer-update (handle-pointer-update msg) - :page-snapshot (handle-page-snapshot msg) - ::unknown))))) - - - (rx/take-until - (rx/filter #(= ::finalize %) stream))))))) - -;; --- Finalize Websocket - -(defn finalize - [file-id] - (ptk/reify ::finalize - ptk/WatchEvent - (watch [_ state stream] - (ws/-close (get-in state [:ws file-id])) - (rx/of ::finalize)))) - -;; --- Fetch Workspace Users - -(declare users-fetched) - -(defn fetch-users - [file-id] - (ptk/reify ::fetch-users - ptk/WatchEvent - (watch [_ state stream] - (->> (rp/query :project-file-users {:file-id file-id}) - (rx/map users-fetched))))) - -(defn users-fetched - [users] - (ptk/reify ::users-fetched - ptk/UpdateEvent - (update [_ state] - (reduce (fn [state user] - (assoc-in state [:workspace-users :by-id (:id user)] user)) - state - users)))) - - -;; --- Handle: Who - -;; TODO: assign color - -(defn handle-who - [{:keys [users] :as msg}] - (s/assert set? users) - (ptk/reify ::handle-who - ptk/UpdateEvent - (update [_ state] - (assoc-in state [:workspace-users :active] users)))) - -(defn handle-pointer-update - [{:keys [user-id page-id x y] :as msg}] - (ptk/reify ::handle-pointer-update - ptk/UpdateEvent - (update [_ state] - (assoc-in state [:workspace-users :pointer user-id] - {:page-id page-id - :user-id user-id - :x x - :y y})))) - -(defn handle-page-snapshot - [{:keys [user-id page-id version operations :as msg]}] - (ptk/reify ::handle-page-snapshot - ptk/UpdateEvent - (update [_ state] - (-> state - (assoc-in [:workspace-page :version] version) - (assoc-in [:pages page-id :version] version) - (update-in [:pages-data page-id] cp/process-ops operations) - (update :workspace-data cp/process-ops operations))))) diff --git a/frontend/src/uxbox/main/ui/shapes/canvas.cljs b/frontend/src/uxbox/main/ui/shapes/canvas.cljs index 1f2a37de6..ea61005d4 100644 --- a/frontend/src/uxbox/main/ui/shapes/canvas.cljs +++ b/frontend/src/uxbox/main/ui/shapes/canvas.cljs @@ -16,7 +16,6 @@ [uxbox.main.ui.shapes.common :as common] [uxbox.main.store :as st] [uxbox.main.ui.shapes.rect :refer [rect-shape]] - ;; [uxbox.main.ui.workspace.streams :as uws] [uxbox.util.data :refer [parse-int]] [uxbox.util.dom :as dom] [uxbox.util.geom.point :as gpt])) diff --git a/frontend/src/uxbox/main/ui/workspace.cljs b/frontend/src/uxbox/main/ui/workspace.cljs index 288ba6219..70c947d52 100644 --- a/frontend/src/uxbox/main/ui/workspace.cljs +++ b/frontend/src/uxbox/main/ui/workspace.cljs @@ -14,9 +14,9 @@ [uxbox.main.data.history :as udh] [uxbox.main.data.undo :as udu] [uxbox.main.data.workspace :as dw] - [uxbox.main.data.workspace-websocket :as dws] [uxbox.main.refs :as refs] [uxbox.main.store :as st] + [uxbox.main.streams :as ms] [uxbox.main.ui.confirm] [uxbox.main.ui.keyboard :as kbd] [uxbox.main.ui.messages :refer [messages-widget]] @@ -30,7 +30,6 @@ [uxbox.main.ui.workspace.shortcuts :as shortcuts] [uxbox.main.ui.workspace.sidebar :refer [left-sidebar right-sidebar]] [uxbox.main.ui.workspace.sidebar.history :refer [history-dialog]] - [uxbox.main.ui.workspace.streams :as uws] [uxbox.util.data :refer [classnames]] [uxbox.util.dom :as dom] [uxbox.util.geom.point :as gpt] @@ -43,7 +42,7 @@ (let [target (.-target event) top (.-scrollTop target) left (.-scrollLeft target)] - (st/emit! (uws/scroll-event (gpt/point left top))))) + (st/emit! (ms/->ScrollEvent (gpt/point left top))))) (defn- on-wheel [event canvas] @@ -51,7 +50,7 @@ (let [prev-zoom @refs/selected-zoom dom (mf/ref-node canvas) scroll-position (scroll/get-current-position-absolute dom) - mouse-point @uws/mouse-position] + mouse-point @ms/mouse-position] (dom/prevent-default event) (dom/stop-propagation event) (if (pos? (.-deltaY event)) diff --git a/frontend/src/uxbox/main/ui/workspace/drawarea.cljs b/frontend/src/uxbox/main/ui/workspace/drawarea.cljs index c9828b665..49add35a0 100644 --- a/frontend/src/uxbox/main/ui/workspace/drawarea.cljs +++ b/frontend/src/uxbox/main/ui/workspace/drawarea.cljs @@ -15,8 +15,8 @@ [uxbox.main.geom :as geom] [uxbox.main.refs :as refs] [uxbox.main.store :as st] + [uxbox.main.streams :as ms] [uxbox.main.ui.shapes :as shapes] - [uxbox.main.ui.workspace.streams :as uws] [uxbox.main.workers :as uwrk] [uxbox.util.math :as mth] [uxbox.util.dom :as dom] @@ -130,10 +130,10 @@ (let [{:keys [zoom flags]} (:workspace-local state) align? (refs/alignment-activated? flags) - stoper? #(or (uws/mouse-up? %) (= % :interrupt)) + stoper? #(or (ms/mouse-up? %) (= % :interrupt)) stoper (rx/filter stoper? stream) - mouse (->> uws/mouse-position + mouse (->> ms/mouse-position (rx/mapcat #(conditional-align % align?)) (rx/map #(gpt/divide % zoom)))] (rx/concat @@ -141,7 +141,7 @@ (rx/take 1) (rx/map (fn [pt] #(initialize-drawing % pt)))) (->> mouse - (rx/with-latest vector uws/mouse-position-ctrl) + (rx/with-latest vector ms/mouse-position-ctrl) (rx/map (fn [[pt ctrl?]] #(update-drawing % pt ctrl?))) (rx/take-until stoper)) (rx/of handle-finish-drawing))))))) @@ -149,10 +149,10 @@ (def handle-drawing-path (letfn [(stoper-event? [{:keys [type shift] :as event}] (or (= event ::end-path-drawing) - (and (uws/mouse-event? event) + (and (ms/mouse-event? event) (or (and (= type :double-click) shift) (= type :context-menu))) - (and (uws/keyboard-event? event) + (and (ms/keyboard-event? event) (= type :down) (= 13 (:key event))))) @@ -179,24 +179,24 @@ (let [{:keys [zoom flags]} (:workspace-local state) align? (refs/alignment-activated? flags) - last-point (volatile! (gpt/divide @uws/mouse-position zoom)) + last-point (volatile! (gpt/divide @ms/mouse-position zoom)) stoper (->> (rx/filter stoper-event? stream) (rx/share)) - mouse (->> (rx/sample 10 uws/mouse-position) + mouse (->> (rx/sample 10 ms/mouse-position) (rx/mapcat #(conditional-align % align?)) (rx/map #(gpt/divide % zoom))) points (->> stream - (rx/filter uws/mouse-click?) + (rx/filter ms/mouse-click?) (rx/filter #(false? (:shift %))) (rx/with-latest vector mouse) (rx/map second)) counter (rx/merge (rx/scan #(inc %) 1 points) (rx/of 1)) stream' (->> mouse - (rx/with-latest vector uws/mouse-position-ctrl) + (rx/with-latest vector ms/mouse-position-ctrl) (rx/with-latest vector counter) (rx/map flatten)) @@ -236,7 +236,7 @@ (def handle-drawing-curve (letfn [(stoper-event? [{:keys [type shift] :as event}] - (uws/mouse-event? event) (= type :up)) + (ms/mouse-event? event) (= type :up)) (initialize-drawing [state] (assoc-in state [:workspace-local :drawing ::initialized?] true)) @@ -253,7 +253,7 @@ (let [{:keys [zoom flags]} (:workspace-local state) align? (refs/alignment-activated? flags) stoper (rx/filter stoper-event? stream) - mouse (->> (rx/sample 10 uws/mouse-position) + mouse (->> (rx/sample 10 ms/mouse-position) (rx/mapcat #(conditional-align % align?)) (rx/map #(gpt/divide % zoom)))] (rx/concat diff --git a/frontend/src/uxbox/main/ui/workspace/ruler.cljs b/frontend/src/uxbox/main/ui/workspace/ruler.cljs index 94518e58c..633abf90a 100644 --- a/frontend/src/uxbox/main/ui/workspace/ruler.cljs +++ b/frontend/src/uxbox/main/ui/workspace/ruler.cljs @@ -11,7 +11,6 @@ [uxbox.main.constants :as c] [uxbox.main.data.workspace :as udw] [uxbox.main.store :as st] - [uxbox.main.ui.workspace.streams :as ws] [uxbox.util.dom :as dom] [uxbox.util.geom.point :as gpt] [uxbox.util.math :as mth])) diff --git a/frontend/src/uxbox/main/ui/workspace/rules.cljs b/frontend/src/uxbox/main/ui/workspace/rules.cljs index ec798d4bf..0d61e88a2 100644 --- a/frontend/src/uxbox/main/ui/workspace/rules.cljs +++ b/frontend/src/uxbox/main/ui/workspace/rules.cljs @@ -13,7 +13,7 @@ [uxbox.main.constants :as c] [uxbox.main.refs :as refs] [uxbox.main.store :as s] - [uxbox.main.ui.workspace.streams :as uws] + [uxbox.main.streams :as ms] [uxbox.util.components :refer [use-rxsub]] [uxbox.util.dom :as dom])) @@ -123,7 +123,7 @@ (mf/defc horizontal-rule {:wrap [mf/wrap-memo]} [props] - (let [scroll (use-rxsub uws/viewport-scroll) + (let [scroll (use-rxsub ms/viewport-scroll) zoom (mf/deref refs/selected-zoom) translate-x (- (- scroll-padding) (:x scroll))] [:svg.horizontal-rule @@ -139,7 +139,7 @@ (mf/defc vertical-rule {:wrap [mf/wrap-memo]} [props] - (let [scroll (use-rxsub uws/viewport-scroll) + (let [scroll (use-rxsub ms/viewport-scroll) zoom (or (mf/deref refs/selected-zoom) 1) scroll-y (:y scroll) translate-y (+ (- scroll-padding) diff --git a/frontend/src/uxbox/main/ui/workspace/selection.cljs b/frontend/src/uxbox/main/ui/workspace/selection.cljs index fc8ac7186..203ebee0a 100644 --- a/frontend/src/uxbox/main/ui/workspace/selection.cljs +++ b/frontend/src/uxbox/main/ui/workspace/selection.cljs @@ -16,7 +16,7 @@ [uxbox.main.geom :as geom] [uxbox.main.refs :as refs] [uxbox.main.store :as st] - [uxbox.main.ui.workspace.streams :as ws] + [uxbox.main.streams :as ms] [uxbox.main.workers :as uwrk] [uxbox.util.dom :as dom] [uxbox.util.geom.point :as gpt])) @@ -63,12 +63,12 @@ (watch [_ state stream] (let [shape (->> (geom/shape->rect-shape shape) (geom/size)) - stoper (rx/filter ws/mouse-up? stream)] + stoper (rx/filter ms/mouse-up? stream)] (rx/concat - (->> ws/mouse-position + (->> ms/mouse-position (rx/map apply-zoom) (rx/mapcat apply-grid-alignment) - (rx/with-latest vector ws/mouse-position-ctrl) + (rx/with-latest vector ms/mouse-position-ctrl) (rx/map normalize-proportion-lock) (rx/mapcat (partial resize shape)) (rx/take-until stoper)) @@ -157,14 +157,14 @@ (dom/stop-propagation event) ;; TODO: this need code ux refactor (let [stoper (get-edition-stream-stoper) - stream (->> (ws/mouse-position-deltas @ws/mouse-position) + stream (->> (ms/mouse-position-deltas @ms/mouse-position) (rx/take-until stoper))] (when @refs/selected-alignment (st/emit! (dw/initial-path-point-align (:id shape) index))) (rx/subscribe stream #(on-handler-move % index)))) (get-edition-stream-stoper [] - (let [stoper? #(and (ws/mouse-event? %) (= (:type %) :up))] + (let [stoper? #(and (ms/mouse-event? %) (= (:type %) :up))] (rx/merge (rx/filter stoper? st/stream) (->> st/stream diff --git a/frontend/src/uxbox/main/ui/workspace/streams.cljs b/frontend/src/uxbox/main/ui/workspace/streams.cljs deleted file mode 100644 index 687a7484f..000000000 --- a/frontend/src/uxbox/main/ui/workspace/streams.cljs +++ /dev/null @@ -1,116 +0,0 @@ -;; 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) 2019 Andrey Antukh - -(ns uxbox.main.ui.workspace.streams - "User interaction events and streams." - (:require - [beicon.core :as rx] - [uxbox.main.store :as st] - [uxbox.main.refs :as refs] - [uxbox.main.workers :as uwrk] - [uxbox.util.geom.point :as gpt])) - -;; --- User Events - -(defrecord KeyboardEvent [type key shift ctrl]) - -(defn keyboard-event - [type key ctrl shift] - {:pre [(keyword? type) - (integer? key) - (boolean? ctrl) - (boolean? shift)]} - (KeyboardEvent. type key ctrl shift)) - -(defn keyboard-event? - [v] - (instance? KeyboardEvent v)) - -(defrecord MouseEvent [type ctrl shift]) - -(defn mouse-event - [type ctrl shift] - {:pre [(keyword? type) - (boolean? ctrl) - (boolean? shift)]} - (MouseEvent. type ctrl shift)) - -(defn mouse-event? - [v] - (instance? MouseEvent v)) - -(defn mouse-up? - [v] - (and (mouse-event? v) - (= :up (:type v)))) - -(defn mouse-click? - [v] - (and (mouse-event? v) - (= :click (:type v)))) - -(defrecord PointerEvent [source pt ctrl shift]) - -(defn pointer-event? - [v] - (instance? PointerEvent v)) - -(defrecord ScrollEvent [point]) - -(defn scroll-event - [pt] - {:pre [(gpt/point? pt)]} - (ScrollEvent. pt)) - -(defn scroll-event? - [v] - (instance? ScrollEvent v)) - -(defn interaction-event? - [event] - (or (keyboard-event? event) - (mouse-event? event))) - -;; --- Derived streams - -(defonce mouse-position - (let [sub (rx/behavior-subject nil) - ob (->> st/stream - (rx/filter pointer-event?) - (rx/filter #(= :viewport (:source %))) - (rx/map :pt))] - (rx/subscribe-with ob sub) - sub)) - -(defonce mouse-position-ctrl - (let [sub (rx/behavior-subject nil) - ob (->> st/stream - (rx/filter pointer-event?) - (rx/map :ctrl) - (rx/dedupe))] - (rx/subscribe-with ob sub) - sub)) - -(defn mouse-position-deltas - [current] - (->> (rx/concat (rx/of current) - (rx/sample 10 mouse-position)) - (rx/map #(gpt/divide % @refs/selected-zoom)) - (rx/mapcat (fn [point] - (if @refs/selected-alignment - (uwrk/align-point point) - (rx/of point)))) - (rx/buffer 2 1) - (rx/map (fn [[old new]] - (gpt/subtract new old))))) - -(defonce viewport-scroll - (let [sub (rx/behavior-subject nil) - sob (->> (rx/filter scroll-event? st/stream) - (rx/map :point))] - (rx/subscribe-with sob sub) - sub)) -