🔥 Remove unused code.

This commit is contained in:
Andrey Antukh 2020-01-07 17:25:11 +01:00
parent 152a5e8b94
commit 142bd1d049
8 changed files with 24 additions and 270 deletions

View file

@ -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 <niwi@niwi.nz>
(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)))))

View file

@ -16,7 +16,6 @@
[uxbox.main.ui.shapes.common :as common] [uxbox.main.ui.shapes.common :as common]
[uxbox.main.store :as st] [uxbox.main.store :as st]
[uxbox.main.ui.shapes.rect :refer [rect-shape]] [uxbox.main.ui.shapes.rect :refer [rect-shape]]
;; [uxbox.main.ui.workspace.streams :as uws]
[uxbox.util.data :refer [parse-int]] [uxbox.util.data :refer [parse-int]]
[uxbox.util.dom :as dom] [uxbox.util.dom :as dom]
[uxbox.util.geom.point :as gpt])) [uxbox.util.geom.point :as gpt]))

View file

@ -14,9 +14,9 @@
[uxbox.main.data.history :as udh] [uxbox.main.data.history :as udh]
[uxbox.main.data.undo :as udu] [uxbox.main.data.undo :as udu]
[uxbox.main.data.workspace :as dw] [uxbox.main.data.workspace :as dw]
[uxbox.main.data.workspace-websocket :as dws]
[uxbox.main.refs :as refs] [uxbox.main.refs :as refs]
[uxbox.main.store :as st] [uxbox.main.store :as st]
[uxbox.main.streams :as ms]
[uxbox.main.ui.confirm] [uxbox.main.ui.confirm]
[uxbox.main.ui.keyboard :as kbd] [uxbox.main.ui.keyboard :as kbd]
[uxbox.main.ui.messages :refer [messages-widget]] [uxbox.main.ui.messages :refer [messages-widget]]
@ -30,7 +30,6 @@
[uxbox.main.ui.workspace.shortcuts :as shortcuts] [uxbox.main.ui.workspace.shortcuts :as shortcuts]
[uxbox.main.ui.workspace.sidebar :refer [left-sidebar right-sidebar]] [uxbox.main.ui.workspace.sidebar :refer [left-sidebar right-sidebar]]
[uxbox.main.ui.workspace.sidebar.history :refer [history-dialog]] [uxbox.main.ui.workspace.sidebar.history :refer [history-dialog]]
[uxbox.main.ui.workspace.streams :as uws]
[uxbox.util.data :refer [classnames]] [uxbox.util.data :refer [classnames]]
[uxbox.util.dom :as dom] [uxbox.util.dom :as dom]
[uxbox.util.geom.point :as gpt] [uxbox.util.geom.point :as gpt]
@ -43,7 +42,7 @@
(let [target (.-target event) (let [target (.-target event)
top (.-scrollTop target) top (.-scrollTop target)
left (.-scrollLeft target)] left (.-scrollLeft target)]
(st/emit! (uws/scroll-event (gpt/point left top))))) (st/emit! (ms/->ScrollEvent (gpt/point left top)))))
(defn- on-wheel (defn- on-wheel
[event canvas] [event canvas]
@ -51,7 +50,7 @@
(let [prev-zoom @refs/selected-zoom (let [prev-zoom @refs/selected-zoom
dom (mf/ref-node canvas) dom (mf/ref-node canvas)
scroll-position (scroll/get-current-position-absolute dom) scroll-position (scroll/get-current-position-absolute dom)
mouse-point @uws/mouse-position] mouse-point @ms/mouse-position]
(dom/prevent-default event) (dom/prevent-default event)
(dom/stop-propagation event) (dom/stop-propagation event)
(if (pos? (.-deltaY event)) (if (pos? (.-deltaY event))

View file

@ -15,8 +15,8 @@
[uxbox.main.geom :as geom] [uxbox.main.geom :as geom]
[uxbox.main.refs :as refs] [uxbox.main.refs :as refs]
[uxbox.main.store :as st] [uxbox.main.store :as st]
[uxbox.main.streams :as ms]
[uxbox.main.ui.shapes :as shapes] [uxbox.main.ui.shapes :as shapes]
[uxbox.main.ui.workspace.streams :as uws]
[uxbox.main.workers :as uwrk] [uxbox.main.workers :as uwrk]
[uxbox.util.math :as mth] [uxbox.util.math :as mth]
[uxbox.util.dom :as dom] [uxbox.util.dom :as dom]
@ -130,10 +130,10 @@
(let [{:keys [zoom flags]} (:workspace-local state) (let [{:keys [zoom flags]} (:workspace-local state)
align? (refs/alignment-activated? flags) align? (refs/alignment-activated? flags)
stoper? #(or (uws/mouse-up? %) (= % :interrupt)) stoper? #(or (ms/mouse-up? %) (= % :interrupt))
stoper (rx/filter stoper? stream) stoper (rx/filter stoper? stream)
mouse (->> uws/mouse-position mouse (->> ms/mouse-position
(rx/mapcat #(conditional-align % align?)) (rx/mapcat #(conditional-align % align?))
(rx/map #(gpt/divide % zoom)))] (rx/map #(gpt/divide % zoom)))]
(rx/concat (rx/concat
@ -141,7 +141,7 @@
(rx/take 1) (rx/take 1)
(rx/map (fn [pt] #(initialize-drawing % pt)))) (rx/map (fn [pt] #(initialize-drawing % pt))))
(->> mouse (->> 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/map (fn [[pt ctrl?]] #(update-drawing % pt ctrl?)))
(rx/take-until stoper)) (rx/take-until stoper))
(rx/of handle-finish-drawing))))))) (rx/of handle-finish-drawing)))))))
@ -149,10 +149,10 @@
(def handle-drawing-path (def handle-drawing-path
(letfn [(stoper-event? [{:keys [type shift] :as event}] (letfn [(stoper-event? [{:keys [type shift] :as event}]
(or (= event ::end-path-drawing) (or (= event ::end-path-drawing)
(and (uws/mouse-event? event) (and (ms/mouse-event? event)
(or (and (= type :double-click) shift) (or (and (= type :double-click) shift)
(= type :context-menu))) (= type :context-menu)))
(and (uws/keyboard-event? event) (and (ms/keyboard-event? event)
(= type :down) (= type :down)
(= 13 (:key event))))) (= 13 (:key event)))))
@ -179,24 +179,24 @@
(let [{:keys [zoom flags]} (:workspace-local state) (let [{:keys [zoom flags]} (:workspace-local state)
align? (refs/alignment-activated? flags) 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) stoper (->> (rx/filter stoper-event? stream)
(rx/share)) (rx/share))
mouse (->> (rx/sample 10 uws/mouse-position) mouse (->> (rx/sample 10 ms/mouse-position)
(rx/mapcat #(conditional-align % align?)) (rx/mapcat #(conditional-align % align?))
(rx/map #(gpt/divide % zoom))) (rx/map #(gpt/divide % zoom)))
points (->> stream points (->> stream
(rx/filter uws/mouse-click?) (rx/filter ms/mouse-click?)
(rx/filter #(false? (:shift %))) (rx/filter #(false? (:shift %)))
(rx/with-latest vector mouse) (rx/with-latest vector mouse)
(rx/map second)) (rx/map second))
counter (rx/merge (rx/scan #(inc %) 1 points) (rx/of 1)) counter (rx/merge (rx/scan #(inc %) 1 points) (rx/of 1))
stream' (->> mouse stream' (->> mouse
(rx/with-latest vector uws/mouse-position-ctrl) (rx/with-latest vector ms/mouse-position-ctrl)
(rx/with-latest vector counter) (rx/with-latest vector counter)
(rx/map flatten)) (rx/map flatten))
@ -236,7 +236,7 @@
(def handle-drawing-curve (def handle-drawing-curve
(letfn [(stoper-event? [{:keys [type shift] :as event}] (letfn [(stoper-event? [{:keys [type shift] :as event}]
(uws/mouse-event? event) (= type :up)) (ms/mouse-event? event) (= type :up))
(initialize-drawing [state] (initialize-drawing [state]
(assoc-in state [:workspace-local :drawing ::initialized?] true)) (assoc-in state [:workspace-local :drawing ::initialized?] true))
@ -253,7 +253,7 @@
(let [{:keys [zoom flags]} (:workspace-local state) (let [{:keys [zoom flags]} (:workspace-local state)
align? (refs/alignment-activated? flags) align? (refs/alignment-activated? flags)
stoper (rx/filter stoper-event? stream) 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/mapcat #(conditional-align % align?))
(rx/map #(gpt/divide % zoom)))] (rx/map #(gpt/divide % zoom)))]
(rx/concat (rx/concat

View file

@ -11,7 +11,6 @@
[uxbox.main.constants :as c] [uxbox.main.constants :as c]
[uxbox.main.data.workspace :as udw] [uxbox.main.data.workspace :as udw]
[uxbox.main.store :as st] [uxbox.main.store :as st]
[uxbox.main.ui.workspace.streams :as ws]
[uxbox.util.dom :as dom] [uxbox.util.dom :as dom]
[uxbox.util.geom.point :as gpt] [uxbox.util.geom.point :as gpt]
[uxbox.util.math :as mth])) [uxbox.util.math :as mth]))

View file

@ -13,7 +13,7 @@
[uxbox.main.constants :as c] [uxbox.main.constants :as c]
[uxbox.main.refs :as refs] [uxbox.main.refs :as refs]
[uxbox.main.store :as s] [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.components :refer [use-rxsub]]
[uxbox.util.dom :as dom])) [uxbox.util.dom :as dom]))
@ -123,7 +123,7 @@
(mf/defc horizontal-rule (mf/defc horizontal-rule
{:wrap [mf/wrap-memo]} {:wrap [mf/wrap-memo]}
[props] [props]
(let [scroll (use-rxsub uws/viewport-scroll) (let [scroll (use-rxsub ms/viewport-scroll)
zoom (mf/deref refs/selected-zoom) zoom (mf/deref refs/selected-zoom)
translate-x (- (- scroll-padding) (:x scroll))] translate-x (- (- scroll-padding) (:x scroll))]
[:svg.horizontal-rule [:svg.horizontal-rule
@ -139,7 +139,7 @@
(mf/defc vertical-rule (mf/defc vertical-rule
{:wrap [mf/wrap-memo]} {:wrap [mf/wrap-memo]}
[props] [props]
(let [scroll (use-rxsub uws/viewport-scroll) (let [scroll (use-rxsub ms/viewport-scroll)
zoom (or (mf/deref refs/selected-zoom) 1) zoom (or (mf/deref refs/selected-zoom) 1)
scroll-y (:y scroll) scroll-y (:y scroll)
translate-y (+ (- scroll-padding) translate-y (+ (- scroll-padding)

View file

@ -16,7 +16,7 @@
[uxbox.main.geom :as geom] [uxbox.main.geom :as geom]
[uxbox.main.refs :as refs] [uxbox.main.refs :as refs]
[uxbox.main.store :as st] [uxbox.main.store :as st]
[uxbox.main.ui.workspace.streams :as ws] [uxbox.main.streams :as ms]
[uxbox.main.workers :as uwrk] [uxbox.main.workers :as uwrk]
[uxbox.util.dom :as dom] [uxbox.util.dom :as dom]
[uxbox.util.geom.point :as gpt])) [uxbox.util.geom.point :as gpt]))
@ -63,12 +63,12 @@
(watch [_ state stream] (watch [_ state stream]
(let [shape (->> (geom/shape->rect-shape shape) (let [shape (->> (geom/shape->rect-shape shape)
(geom/size)) (geom/size))
stoper (rx/filter ws/mouse-up? stream)] stoper (rx/filter ms/mouse-up? stream)]
(rx/concat (rx/concat
(->> ws/mouse-position (->> ms/mouse-position
(rx/map apply-zoom) (rx/map apply-zoom)
(rx/mapcat apply-grid-alignment) (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/map normalize-proportion-lock)
(rx/mapcat (partial resize shape)) (rx/mapcat (partial resize shape))
(rx/take-until stoper)) (rx/take-until stoper))
@ -157,14 +157,14 @@
(dom/stop-propagation event) (dom/stop-propagation event)
;; TODO: this need code ux refactor ;; TODO: this need code ux refactor
(let [stoper (get-edition-stream-stoper) (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))] (rx/take-until stoper))]
(when @refs/selected-alignment (when @refs/selected-alignment
(st/emit! (dw/initial-path-point-align (:id shape) index))) (st/emit! (dw/initial-path-point-align (:id shape) index)))
(rx/subscribe stream #(on-handler-move % index)))) (rx/subscribe stream #(on-handler-move % index))))
(get-edition-stream-stoper [] (get-edition-stream-stoper []
(let [stoper? #(and (ws/mouse-event? %) (= (:type %) :up))] (let [stoper? #(and (ms/mouse-event? %) (= (:type %) :up))]
(rx/merge (rx/merge
(rx/filter stoper? st/stream) (rx/filter stoper? st/stream)
(->> st/stream (->> st/stream

View file

@ -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 <niwi@niwi.nz>
(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))