💄 Minor cosmetic changes on workspace ns.

This commit is contained in:
Andrey Antukh 2019-10-17 19:36:34 +02:00
parent 097a6c924a
commit dcc7b09dd8
2 changed files with 59 additions and 72 deletions

View file

@ -206,6 +206,4 @@
(defn materialize-xfmt (defn materialize-xfmt
[state id xfmt] [state id xfmt]
(let [{:keys [type items] :as shape} (get-in state [:shapes id])] (let [{:keys [type items] :as shape} (get-in state [:shapes id])]
(if (= type :group) (update-in state [:shapes id] geom/transform xfmt)))
(reduce #(materialize-xfmt %1 %2 xfmt) state items)
(update-in state [:shapes id] geom/transform xfmt))))

View file

@ -47,51 +47,48 @@
(declare initialize-alignment) (declare initialize-alignment)
(defrecord Initialize [project-id page-id]
ptk/UpdateEvent
(update [_ state]
(let [default-flags #{:sitemap :drawtools :layers :element-options :rules}
initial-workspace {:project-id project-id
:page-id page-id
:zoom 1
:flags default-flags
:selected #{}
:drawing nil
:drawing-tool nil
:tooltip nil}]
(-> state
(update-in [:workspace page-id] #(if (nil? %) initial-workspace %))
(assoc-in [:workspace :current] page-id))))
ptk/WatchEvent
(watch [_ state stream]
(let [page (get-in state [:pages page-id])]
;; Activate loaded if page is not fetched.
(when-not page (reset! st/loader true))
(if page
(rx/of (initialize-alignment page-id))
(rx/merge
(rx/of (udp/fetch-pages project-id))
(->> stream
(rx/filter udp/pages-fetched?)
(rx/take 1)
(rx/do #(reset! st/loader false))
(rx/map #(initialize-alignment page-id)))))))
ptk/EffectEvent
(effect [_ state stream]
;; Optimistic prefetch of projects if them are not already fetched
(when-not (seq (:projects state))
(st/emit! (dp/fetch-projects)))))
(defn initialize (defn initialize
"Initialize the workspace state." "Initialize the workspace state."
[project page] [project-id page-id]
{:pre [(uuid? project) (s/assert ::us/uuid project-id)
(uuid? page)]} (s/assert ::us/uuid page-id)
(Initialize. project page)) (ptk/reify ::initialize
ptk/UpdateEvent
(update [_ state]
(let [default-flags #{:sitemap :drawtools :layers :element-options :rules}
initial-workspace {:project-id project-id
:page-id page-id
:zoom 1
:flags default-flags
:selected #{}
:drawing nil
:drawing-tool nil
:tooltip nil}]
(-> state
(update-in [:workspace page-id] #(if (nil? %) initial-workspace %))
(assoc-in [:workspace :current] page-id))))
ptk/WatchEvent
(watch [_ state stream]
(let [page (get-in state [:pages page-id])]
;; Activate loaded if page is not fetched.
(when-not page (reset! st/loader true))
(if page
(rx/of (initialize-alignment page-id))
(rx/merge
(rx/of (udp/fetch-pages project-id))
(->> stream
(rx/filter udp/pages-fetched?)
(rx/take 1)
(rx/do #(reset! st/loader false))
(rx/map #(initialize-alignment page-id)))))))
ptk/EffectEvent
(effect [_ state stream]
;; Optimistic prefetch of projects if them are not already fetched
(when-not (seq (:projects state))
(st/emit! (dp/fetch-projects))))))
;; --- Workspace Tooltips ;; --- Workspace Tooltips
@ -347,7 +344,7 @@
(defn add-shape (defn add-shape
[data] [data]
(reify (ptk/reify ::add-shape
udp/IPageUpdate udp/IPageUpdate
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
@ -438,17 +435,19 @@
(s/assert ::us/uuid id) (s/assert ::us/uuid id)
(s/assert ::ds/attributes attrs) (s/assert ::ds/attributes attrs)
(let [atts (s/conform ::ds/attributes attrs)] (let [atts (s/conform ::ds/attributes attrs)]
(reify (ptk/reify ::update-shape-attrs
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(update-in state [:shapes id] merge attrs))))) (update-in state [:shapes id] merge attrs)))))
;; --- Update Selected Shapes attrs ;; --- Update Selected Shapes attrs
;; TODO: improve performance of this event
(defn update-selected-shapes-attrs (defn update-selected-shapes-attrs
[attrs] [attrs]
(s/assert ::ds/attributes attrs) (s/assert ::ds/attributes attrs)
(reify (ptk/reify ::update-selected-shapes-attrs
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [pid (get-in state [:workspace :current]) (let [pid (get-in state [:workspace :current])
@ -492,7 +491,7 @@
[direction speed] [direction speed]
(s/assert ::direction direction) (s/assert ::direction direction)
(s/assert ::speed speed) (s/assert ::speed speed)
(reify (ptk/reify ::move-selected
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [page-id (get-in state [:workspace :current]) (let [page-id (get-in state [:workspace :current])
@ -520,7 +519,6 @@
(update [_ state] (update [_ state]
(let [id (get-in state [:workspace :current]) (let [id (get-in state [:workspace :current])
selected (get-in state [:workspace id :selected])] selected (get-in state [:workspace id :selected])]
(prn "order-selected-shapes" selected)
;; NOTE: multiple selection ordering not supported ;; NOTE: multiple selection ordering not supported
(if (pos? (count selected)) (if (pos? (count selected))
(ds/order-shape state (first selected) loc) (ds/order-shape state (first selected) loc)
@ -544,7 +542,7 @@
(def delete-selected (def delete-selected
"Deselect all and remove all selected shapes." "Deselect all and remove all selected shapes."
(reify (ptk/reify ::delete-selected
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [id (get-in state [:workspace :current]) (let [id (get-in state [:workspace :current])
@ -556,7 +554,7 @@
(defn rename-shape (defn rename-shape
[id name] [id name]
{:pre [(uuid? id) (string? name)]} {:pre [(uuid? id) (string? name)]}
(reify (ptk/reify ::rename-shape
udp/IPageUpdate udp/IPageUpdate
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
@ -567,7 +565,7 @@
(defn change-shape-order (defn change-shape-order
[{:keys [id index] :as params}] [{:keys [id index] :as params}]
{:pre [(uuid? id) (number? index)]} {:pre [(uuid? id) (number? index)]}
(reify (ptk/reify ::change-shape-order
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [page-id (get-in state [:shapes id :page]) (let [page-id (get-in state [:shapes id :page])
@ -583,7 +581,7 @@
[{:keys [id index] :as params}] [{:keys [id index] :as params}]
(s/assert ::us/uuid id) (s/assert ::us/uuid id)
(s/assert ::us/number index) (s/assert ::us/number index)
(reify (ptk/reify ::change-canvas-order
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [page-id (get-in state [:shapes id :page]) (let [page-id (get-in state [:shapes id :page])
@ -613,8 +611,6 @@
;; --- Temportal displacement for Shape / Selection ;; --- Temportal displacement for Shape / Selection
;; TODO: this can be done in more performant way using transients
(defn apply-temporal-displacement-in-bulk (defn apply-temporal-displacement-in-bulk
"Apply the same displacement delta to all shapes identified by the "Apply the same displacement delta to all shapes identified by the
set if ids." set if ids."
@ -685,7 +681,7 @@
(defn start-edition-mode (defn start-edition-mode
[id] [id]
{:pre [(uuid? id)]} {:pre [(uuid? id)]}
(reify (ptk/reify ::start-edition-mode
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [pid (get-in state [:workspace :current])] (let [pid (get-in state [:workspace :current])]
@ -702,23 +698,16 @@
;; --- Select for Drawing ;; --- Select for Drawing
(def clear-drawing (def clear-drawing
(reify (ptk/reify ::clear-drawing
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [pid (get-in state [:workspace :current])] (let [pid (get-in state [:workspace :current])]
(update-in state [:workspace pid] dissoc :drawing-tool :drawing))))) (update-in state [:workspace pid] dissoc :drawing-tool :drawing)))))
(defn select-for-drawing?
[e]
(= (::type (meta e)) ::select-for-drawing))
(defn select-for-drawing (defn select-for-drawing
([tool] (select-for-drawing tool nil)) ([tool] (select-for-drawing tool nil))
([tool data] ([tool data]
(reify (ptk/reify ::select-for-drawing
IMeta
(-meta [_] {::type ::select-for-drawing})
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
(let [pid (get-in state [:workspace :current])] (let [pid (get-in state [:workspace :current])]
@ -770,7 +759,7 @@
[id dimensions] [id dimensions]
(s/assert ::us/uuid id) (s/assert ::us/uuid id)
(s/assert ::update-dimensions dimensions) (s/assert ::update-dimensions dimensions)
(reify (ptk/reify ::update-dimensions
udp/IPageUpdate udp/IPageUpdate
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
@ -853,7 +842,7 @@
(map :id))] (map :id))]
(reduce impl-set-hidden $ (sequence xform shapes))) (reduce impl-set-hidden $ (sequence xform shapes)))
$))))] $))))]
(reify (ptk/reify ::set-hidden-attr
udp/IPageUpdate udp/IPageUpdate
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
@ -876,7 +865,7 @@
(map :id))] (map :id))]
(reduce impl-set-blocked $ (sequence xform shapes))) (reduce impl-set-blocked $ (sequence xform shapes)))
$))))] $))))]
(reify (ptk/reify ::set-blocked-attr
udp/IPageUpdate udp/IPageUpdate
ptk/UpdateEvent ptk/UpdateEvent
(update [_ state] (update [_ state]
@ -927,7 +916,7 @@
(defn delete-page (defn delete-page
[id] [id]
{:pre [(uuid? id)]} {:pre [(uuid? id)]}
(reify (ptk/reify ::delete-page
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(let [pid (get-in state [:pages id :project])] (let [pid (get-in state [:pages id :project])]
@ -985,7 +974,7 @@
[id metadata] [id metadata]
(s/assert ::us/uuid id) (s/assert ::us/uuid id)
(s/assert ::udp/metadata metadata) (s/assert ::udp/metadata metadata)
(reify (ptk/reify ::update-metadata
ptk/WatchEvent ptk/WatchEvent
(watch [_ state s] (watch [_ state s]
(rx/of (udp/update-metadata id metadata) (rx/of (udp/update-metadata id metadata)