0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-14 08:41:48 -05:00

State keys naming refactor.

This commit is contained in:
Andrey Antukh 2016-11-06 18:59:49 +01:00
parent 7abfaf639e
commit 7b7fd7680f
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
30 changed files with 207 additions and 207 deletions

View file

@ -115,7 +115,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [item (get-in state [:workspace :history :by-version version]) (let [item (get-in state [:workspace :history :by-version version])
page (get-in state [:pages-by-id (:page item)]) page (get-in state [:pages (:page item)])
page (assoc page page (assoc page
:history true :history true
:data (:data item))] :data (:data item))]
@ -133,7 +133,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(-> state (-> state
(update-in [:pages-by-id id] dissoc :history) (update-in [:pages id] dissoc :history)
(assoc-in [:workspace :history :selected] nil))) (assoc-in [:workspace :history :selected] nil)))
rs/WatchEvent rs/WatchEvent

View file

@ -63,7 +63,7 @@
(reduce (fn [state item] (reduce (fn [state item]
(let [id (:id item) (let [id (:id item)
item (assoc item :type :own)] item (assoc item :type :own)]
(assoc-in state [:icon-colls-by-id id] item))) (assoc-in state [:icon-collections id] item)))
state state
items))) items)))
@ -90,7 +90,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [{:keys [id] :as item} (assoc item :type :own)] (let [{:keys [id] :as item} (assoc item :type :own)]
(update state :icon-colls-by-id assoc id item))) (update state :icon-collections assoc id item)))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state stream] (-apply-watch [_ state stream]
@ -124,7 +124,7 @@
(defrecord CollectionUpdated [item] (defrecord CollectionUpdated [item]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:icon-colls-by-id (:id item)] merge item))) (update-in state [:icon-collections (:id item)] merge item)))
(defn collection-updated (defn collection-updated
[item] [item]
@ -135,7 +135,7 @@
(defrecord UpdateCollection [id] (defrecord UpdateCollection [id]
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [item (get-in state [:icon-colls-by-id id])] (let [item (get-in state [:icon-collections id])]
(->> (rp/req :update/icon-collection item) (->> (rp/req :update/icon-collection item)
(rx/map :payload) (rx/map :payload)
(rx/map collection-updated))))) (rx/map collection-updated)))))
@ -149,7 +149,7 @@
(defrecord RenameCollection [id name] (defrecord RenameCollection [id name]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:icon-colls-by-id id :name] name)) (assoc-in state [:icon-collections id :name] name))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
@ -164,7 +164,7 @@
(defrecord DeleteCollection [id] (defrecord DeleteCollection [id]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update state :icon-colls-by-id dissoc id)) (update state :icon-collections dissoc id))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
@ -182,7 +182,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [{:keys [id] :as item} (assoc item :type :icon)] (let [{:keys [id] :as item} (assoc item :type :icon)]
(update state :icons-by-id assoc id item)))) (update state :icons assoc id item))))
(defn icon-created (defn icon-created
[item] [item]
@ -250,7 +250,7 @@
(defrecord IconUpdated [id data] (defrecord IconUpdated [id data]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:icons-by-id id] data))) (assoc-in state [:icons id] data)))
(defn icon-updated (defn icon-updated
[{:keys [id] :as data}] [{:keys [id] :as data}]
@ -262,7 +262,7 @@
(defrecord UpdateIcon [id] (defrecord UpdateIcon [id]
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state stream] (-apply-watch [_ state stream]
(let [icon (get-in state [:icons-by-id id])] (let [icon (get-in state [:icons id])]
(->> (rp/req :update/icon icon) (->> (rp/req :update/icon icon)
(rx/map :payload) (rx/map :payload)
(rx/map icon-updated))))) (rx/map icon-updated)))))
@ -279,7 +279,7 @@
(-apply-update [_ state] (-apply-update [_ state]
(reduce (fn [state {:keys [id] :as icon}] (reduce (fn [state {:keys [id] :as icon}]
(let [icon (assoc icon :type :icon)] (let [icon (assoc icon :type :icon)]
(assoc-in state [:icons-by-id id] icon))) (assoc-in state [:icons id] icon)))
state state
items))) items)))
@ -308,7 +308,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(-> state (-> state
(update :icons-by-id dissoc id) (update :icons dissoc id)
(update-in [:dashboard :icons :selected] disj id))) (update-in [:dashboard :icons :selected] disj id)))
rs/WatchEvent rs/WatchEvent
@ -357,7 +357,7 @@
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state stream] (-apply-watch [_ state stream]
(let [selected (get-in state [:dashboard :icons :selected]) (let [selected (get-in state [:dashboard :icons :selected])
selected (map #(get-in state [:icons-by-id %]) selected)] selected (map #(get-in state [:icons %]) selected)]
(->> (rx/from-coll selected) (->> (rx/from-coll selected)
(rx/map #(dissoc % :id)) (rx/map #(dissoc % :id))
(rx/map #(assoc % :collection id)) (rx/map #(assoc % :collection id))
@ -377,7 +377,7 @@
(-apply-update [_ state] (-apply-update [_ state]
(let [selected (get-in state [:dashboard :icons :selected])] (let [selected (get-in state [:dashboard :icons :selected])]
(reduce (fn [state icon] (reduce (fn [state icon]
(assoc-in state [:icons-by-id icon :collection] id)) (assoc-in state [:icons icon :collection] id))
state state
selected))) selected)))

View file

@ -63,7 +63,7 @@
(reduce (fn [state item] (reduce (fn [state item]
(let [id (:id item) (let [id (:id item)
item (assoc item :type :own)] item (assoc item :type :own)]
(assoc-in state [:image-colls-by-id id] item))) (assoc-in state [:image-collections id] item)))
state state
items))) items)))
@ -90,7 +90,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [{:keys [id] :as item} (assoc item :type :own)] (let [{:keys [id] :as item} (assoc item :type :own)]
(update state :image-colls-by-id assoc id item))) (update state :image-collections assoc id item)))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state stream] (-apply-watch [_ state stream]
@ -124,7 +124,7 @@
(defrecord CollectionUpdated [item] (defrecord CollectionUpdated [item]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:image-colls-by-id (:id item)] merge item))) (update-in state [:image-collections (:id item)] merge item)))
(defn collection-updated (defn collection-updated
[item] [item]
@ -135,7 +135,7 @@
(defrecord UpdateCollection [id] (defrecord UpdateCollection [id]
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [item (get-in state [:image-colls-by-id id])] (let [item (get-in state [:image-collections id])]
(->> (rp/req :update/image-collection item) (->> (rp/req :update/image-collection item)
(rx/map :payload) (rx/map :payload)
(rx/map collection-updated))))) (rx/map collection-updated)))))
@ -149,7 +149,7 @@
(defrecord RenameCollection [id name] (defrecord RenameCollection [id name]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:image-colls-by-id id :name] name)) (assoc-in state [:image-collections id :name] name))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
@ -164,7 +164,7 @@
(defrecord DeleteCollection [id] (defrecord DeleteCollection [id]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update state :image-colls-by-id dissoc id)) (update state :image-collections dissoc id))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
@ -181,7 +181,7 @@
(defrecord ImageCreated [item] (defrecord ImageCreated [item]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update state :images-by-id assoc (:id item) item))) (update state :images assoc (:id item) item)))
(defn image-created (defn image-created
[item] [item]
@ -237,7 +237,7 @@
(defrecord ImageUpdated [id data] (defrecord ImageUpdated [id data]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:images-by-id id] data))) (assoc-in state [:images id] data)))
(defn image-updated (defn image-updated
[{:keys [id] :as data}] [{:keys [id] :as data}]
@ -249,7 +249,7 @@
(defrecord UpdateImage [id] (defrecord UpdateImage [id]
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state stream] (-apply-watch [_ state stream]
(let [image (get-in state [:images-by-id id])] (let [image (get-in state [:images id])]
(->> (rp/req :update/image image) (->> (rp/req :update/image image)
(rx/map :payload) (rx/map :payload)
(rx/map image-updated))))) (rx/map image-updated)))))
@ -265,7 +265,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(reduce (fn [state {:keys [id] :as image}] (reduce (fn [state {:keys [id] :as image}]
(assoc-in state [:images-by-id id] image)) (assoc-in state [:images id] image))
state state
items))) items)))
@ -296,7 +296,7 @@
(defrecord FetchImage [id] (defrecord FetchImage [id]
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state stream] (-apply-watch [_ state stream]
(let [existing (get-in state [:images-by-id id])] (let [existing (get-in state [:images id])]
(if existing (if existing
(rx/empty) (rx/empty)
(->> (rp/req :fetch/image {:id id}) (->> (rp/req :fetch/image {:id id})
@ -316,7 +316,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [id (:id image)] (let [id (:id image)]
(update state :images-by-id assoc id image)))) (update state :images assoc id image))))
(defn image-fetched (defn image-fetched
[image] [image]
@ -329,7 +329,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(-> state (-> state
(update :images-by-id dissoc id) (update :images dissoc id)
(update-in [:dashboard :images :selected] disj id))) (update-in [:dashboard :images :selected] disj id)))
rs/WatchEvent rs/WatchEvent
@ -378,7 +378,7 @@
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state stream] (-apply-watch [_ state stream]
(let [selected (get-in state [:dashboard :images :selected]) (let [selected (get-in state [:dashboard :images :selected])
selected (map #(get-in state [:images-by-id %]) selected)] selected (map #(get-in state [:images %]) selected)]
(->> (rx/from-coll selected) (->> (rx/from-coll selected)
(rx/map #(dissoc % :id)) (rx/map #(dissoc % :id))
(rx/map #(assoc % :collection id)) (rx/map #(assoc % :collection id))
@ -398,7 +398,7 @@
(-apply-update [_ state] (-apply-update [_ state]
(let [selected (get-in state [:dashboard :images :selected])] (let [selected (get-in state [:dashboard :images :selected])]
(reduce (fn [state image] (reduce (fn [state image]
(assoc-in state [:images-by-id image :collection] id)) (assoc-in state [:images image :collection] id))
state state
selected))) selected)))

View file

@ -27,8 +27,8 @@
(defn dissoc-page-shapes (defn dissoc-page-shapes
[state id] [state id]
(let [shapes (get-in state [:shapes-by-id])] (let [shapes (get-in state [:shapes])]
(assoc state :shapes-by-id (reduce-kv (fn [acc k v] (assoc state :shapes (reduce-kv (fn [acc k v]
(if (= (:page v) id) (if (= (:page v) id)
(dissoc acc k) (dissoc acc k)
acc)) acc))
@ -39,12 +39,12 @@
"Return a packed version of page object ready "Return a packed version of page object ready
for send to remore storage service." for send to remore storage service."
[state id] [state id]
(let [page (get-in state [:pages-by-id id]) (let [page (get-in state [:pages id])
xf (filter #(= (:page (second %)) id)) xf (filter #(= (:page (second %)) id))
shapes (into {} xf (:shapes-by-id state))] shapes (into {} xf (:shapes state))]
(-> page (-> page
(assoc-in [:data :shapes] (into [] (:shapes page))) (assoc-in [:data :shapes] (into [] (:shapes page)))
(assoc-in [:data :shapes-by-id] shapes) (assoc-in [:data :shapes] shapes)
(update-in [:data] dissoc :items) (update-in [:data] dissoc :items)
(dissoc :shapes)))) (dissoc :shapes))))
@ -54,18 +54,18 @@
[state page] [state page]
(let [data (:data page) (let [data (:data page)
shapes (:shapes data) shapes (:shapes data)
shapes-by-id (:shapes-by-id data) shapes-by-id (:shapes data)
page (-> (dissoc page :data) page (-> (dissoc page :data)
(assoc :shapes shapes))] (assoc :shapes shapes))]
(-> state (-> state
(update :shapes-by-id merge shapes-by-id) (update :shapes merge shapes-by-id)
(update :pages-by-id assoc (:id page) page)))) (update :pages assoc (:id page) page))))
(defn purge-page (defn purge-page
"Remove page and all related stuff from the state." "Remove page and all related stuff from the state."
[state id] [state id]
(-> state (-> state
(update :pages-by-id dissoc id) (update :pages dissoc id)
(update :pagedata-by-id dissoc id) (update :pagedata-by-id dissoc id)
(dissoc-page-shapes id))) (dissoc-page-shapes id)))
@ -134,7 +134,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [this state] (-apply-update [this state]
(-> state (-> state
(assoc-in [:pages-by-id (:id page) :version] (:version page)) (assoc-in [:pages (:id page) :version] (:version page))
(assoc-page page)))) (assoc-page page))))
(defn- page-synced? (defn- page-synced?
@ -159,7 +159,7 @@
(defrecord UpdatePage [id] (defrecord UpdatePage [id]
rs/WatchEvent rs/WatchEvent
(-apply-watch [this state s] (-apply-watch [this state s]
(let [page (get-in state [:pages-by-id id])] (let [page (get-in state [:pages id])]
(if (:history page) (if (:history page)
(rx/empty) (rx/empty)
(rx/of (sync-page id)))))) (rx/of (sync-page id))))))
@ -204,12 +204,12 @@
(when width {:width width}) (when width {:width width})
(when height {:height height}) (when height {:height height})
(when name {:name name})))] (when name {:name name})))]
(update-in state [:pages-by-id id] updater))) (update-in state [:pages id] updater)))
rs/WatchEvent rs/WatchEvent
(-apply-watch [this state s] (-apply-watch [this state s]
(letfn [(on-success [{page :payload}] (letfn [(on-success [{page :payload}]
#(assoc-in % [:pages-by-id id :version] (:version page)))] #(assoc-in % [:pages id :version] (:version page)))]
(->> (rp/req :update/page-metadata (into {} this)) (->> (rp/req :update/page-metadata (into {} this))
(rx/map on-success))))) (rx/map on-success)))))
@ -233,7 +233,7 @@
(defrecord UpdatePageOptions [id options] (defrecord UpdatePageOptions [id options]
rs/WatchEvent rs/WatchEvent
(-apply-watch [this state s] (-apply-watch [this state s]
(let [page (get-in state [:pages-by-id id]) (let [page (get-in state [:pages id])
page (assoc page :options options)] page (assoc page :options options)]
(rx/of (map->UpdatePageMetadata page))))) (rx/of (map->UpdatePageMetadata page)))))

View file

@ -23,13 +23,13 @@
to the state map." to the state map."
[state proj] [state proj]
(let [id (:id proj)] (let [id (:id proj)]
(update-in state [:projects-by-id id] merge proj))) (update-in state [:projects id] merge proj)))
(defn dissoc-project (defn dissoc-project
"A reduce function for dissoc the project "A reduce function for dissoc the project
from the state map." from the state map."
[state id] [state id]
(update-in state [:projects-by-id] dissoc id)) (update-in state [:projects] dissoc id))
;; --- Initialize ;; --- Initialize

View file

@ -41,7 +41,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [shape (get-in state [:shapes-by-id id])] (let [shape (get-in state [:shapes id])]
(impl/dissoc-shape state shape))))) (impl/dissoc-shape state shape)))))
(defn update-shape (defn update-shape
@ -51,7 +51,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id id] merge shape)))) (update-in state [:shapes id] merge shape))))
;; --- Shape Transformations ;; --- Shape Transformations
@ -62,8 +62,8 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [shape (get-in state [:shapes-by-id sid])] (let [shape (get-in state [:shapes sid])]
(update-in state [:shapes-by-id sid] geom/move delta))))) (update-in state [:shapes sid] geom/move delta)))))
(declare align-point) (declare align-point)
@ -76,7 +76,7 @@
(reify (reify
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [shape (get-in state [:shapes-by-id id]) (let [shape (get-in state [:shapes id])
shape (geom/outer-rect state shape) shape (geom/outer-rect state shape)
point (gpt/point (:x shape) (:y shape)) point (gpt/point (:x shape) (:y shape))
point (gpt/add point canvas-coords)] point (gpt/add point canvas-coords)]
@ -90,10 +90,10 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [shape (get-in state [:shapes-by-id sid]) (let [shape (get-in state [:shapes sid])
props (select-keys opts [:x1 :y1 :x2 :y2]) props (select-keys opts [:x1 :y1 :x2 :y2])
props' (select-keys shape [:x1 :y1 :x2 :y2])] props' (select-keys shape [:x1 :y1 :x2 :y2])]
(update-in state [:shapes-by-id sid] geom/setup (update-in state [:shapes sid] geom/setup
(merge props' props)))))) (merge props' props))))))
(defn update-rotation (defn update-rotation
@ -105,7 +105,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id sid] (update-in state [:shapes sid]
geom/rotate rotation)))) geom/rotate rotation))))
(defn update-size (defn update-size
@ -118,7 +118,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id sid] geom/resize-dim opts)))) (update-in state [:shapes sid] geom/resize-dim opts))))
(defn update-vertex-position (defn update-vertex-position
[id {:keys [vid delta]}] [id {:keys [vid delta]}]
@ -126,14 +126,14 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id id] geom/move-vertex vid delta)))) (update-in state [:shapes id] geom/move-vertex vid delta))))
(defn initial-vertext-align (defn initial-vertext-align
[id vid] [id vid]
(reify (reify
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [shape (get-in state [:shapes-by-id id]) (let [shape (get-in state [:shapes id])
point (geom/get-vertex-point shape vid) point (geom/get-vertex-point shape vid)
point (gpt/add point canvas-coords)] point (gpt/add point canvas-coords)]
(->> (align-point point) (->> (align-point point)
@ -146,7 +146,7 @@
(reify (reify
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id sid] geom/absolute-move opts)))) (update-in state [:shapes sid] geom/absolute-move opts))))
(defn update-text (defn update-text
[sid {:keys [content]}] [sid {:keys [content]}]
@ -155,7 +155,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:shapes-by-id sid :content] content)))) (assoc-in state [:shapes sid :content] content))))
(defn update-fill-attrs (defn update-fill-attrs
[sid {:keys [color opacity] :as opts}] [sid {:keys [color opacity] :as opts}]
@ -163,7 +163,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id sid] (update-in state [:shapes sid]
merge merge
(when color {:fill color}) (when color {:fill color})
(when opacity {:fill-opacity opacity}))))) (when opacity {:fill-opacity opacity})))))
@ -175,7 +175,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id sid :font] (update-in state [:shapes sid :font]
merge merge
(when line-height {:line-height line-height}) (when line-height {:line-height line-height})
(when letter-spacing {:letter-spacing letter-spacing}) (when letter-spacing {:letter-spacing letter-spacing})
@ -191,7 +191,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id sid] (update-in state [:shapes sid]
merge merge
(when type {:stroke-type type}) (when type {:stroke-type type})
(when width {:stroke-width width}) (when width {:stroke-width width})
@ -204,7 +204,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id sid] (update-in state [:shapes sid]
merge merge
(when rx {:rx rx}) (when rx {:rx rx})
(when ry {:ry ry}))))) (when ry {:ry ry})))))
@ -221,11 +221,11 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [[width height] (-> (get-in state [:shapes-by-id sid]) (let [[width height] (-> (get-in state [:shapes sid])
(geom/size) (geom/size)
(keep [:width :height])) (keep [:width :height]))
proportion (/ width height)] proportion (/ width height)]
(update-in state [:shapes-by-id sid] assoc (update-in state [:shapes sid] assoc
:proportion proportion :proportion proportion
:proportion-lock true))))) :proportion-lock true)))))
@ -236,7 +236,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id sid] assoc (update-in state [:shapes sid] assoc
:proportion-lock false)))) :proportion-lock false))))
;; --- Group Collapsing ;; --- Group Collapsing
@ -248,7 +248,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id id] assoc :collapsed true)))) (update-in state [:shapes id] assoc :collapsed true))))
(defn uncollapse-shape (defn uncollapse-shape
[id] [id]
@ -257,7 +257,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id id] assoc :collapsed false)))) (update-in state [:shapes id] assoc :collapsed false))))
;; --- Shape Visibility ;; --- Shape Visibility
@ -267,11 +267,11 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:shapes-by-id sid :hidden] true)) (assoc-in state [:shapes sid :hidden] true))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [shape (get-in state [:shapes-by-id sid])] (let [shape (get-in state [:shapes sid])]
(if-not (= (:type shape) :group) (if-not (= (:type shape) :group)
(rx/empty) (rx/empty)
(rx/from-coll (rx/from-coll
@ -283,11 +283,11 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:shapes-by-id sid :hidden] false)) (assoc-in state [:shapes sid :hidden] false))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [shape (get-in state [:shapes-by-id sid])] (let [shape (get-in state [:shapes sid])]
(if-not (= (:type shape) :group) (if-not (= (:type shape) :group)
(rx/empty) (rx/empty)
(rx/from-coll (rx/from-coll
@ -299,11 +299,11 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:shapes-by-id sid :blocked] true)) (assoc-in state [:shapes sid :blocked] true))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [shape (get-in state [:shapes-by-id sid])] (let [shape (get-in state [:shapes sid])]
(if-not (= (:type shape) :group) (if-not (= (:type shape) :group)
(rx/empty) (rx/empty)
(rx/from-coll (rx/from-coll
@ -315,11 +315,11 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:shapes-by-id sid :blocked] false)) (assoc-in state [:shapes sid :blocked] false))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [shape (get-in state [:shapes-by-id sid])] (let [shape (get-in state [:shapes sid])]
(if-not (= (:type shape) :group) (if-not (= (:type shape) :group)
(rx/empty) (rx/empty)
(rx/from-coll (rx/from-coll
@ -331,11 +331,11 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:shapes-by-id sid :locked] true)) (assoc-in state [:shapes sid :locked] true))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [shape (get-in state [:shapes-by-id sid])] (let [shape (get-in state [:shapes sid])]
(if-not (= (:type shape) :group) (if-not (= (:type shape) :group)
(rx/empty) (rx/empty)
(rx/from-coll (rx/from-coll
@ -347,11 +347,11 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(assoc-in state [:shapes-by-id sid :locked] false)) (assoc-in state [:shapes sid :locked] false))
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [shape (get-in state [:shapes-by-id sid])] (let [shape (get-in state [:shapes sid])]
(if-not (= (:type shape) :group) (if-not (= (:type shape) :group)
(rx/empty) (rx/empty)
(rx/from-coll (rx/from-coll
@ -376,7 +376,7 @@
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [page (get-in state [:workspace :page]) (let [page (get-in state [:workspace :page])
id (first (get-in state [:pages-by-id page :shapes]))] id (first (get-in state [:pages page :shapes]))]
(assoc-in state [:workspace :selected] #{id}))))) (assoc-in state [:workspace :selected] #{id})))))
(defn select-shape (defn select-shape
@ -414,7 +414,7 @@
(let [id (or (:id interaction) (let [id (or (:id interaction)
(uuid/random)) (uuid/random))
data (assoc interaction :id id)] data (assoc interaction :id id)]
(assoc-in state [:shapes-by-id shape :interactions id] data)))) (assoc-in state [:shapes shape :interactions id] data))))
(defn update-interaction (defn update-interaction
[shape interaction] [shape interaction]
@ -426,7 +426,7 @@
udp/IPageUpdate udp/IPageUpdate
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id shape :interactions] dissoc id))) (update-in state [:shapes shape :interactions] dissoc id)))
(defn delete-interaction (defn delete-interaction
[shape id] [shape id]
@ -438,7 +438,7 @@
(defrecord UpdatePath [id index delta] (defrecord UpdatePath [id index delta]
rs/UpdateEvent rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(update-in state [:shapes-by-id id :points index] gpt/add delta))) (update-in state [:shapes id :points index] gpt/add delta)))
(defn update-path (defn update-path
"Update a concrete point in the path shape." "Update a concrete point in the path shape."
@ -449,7 +449,7 @@
(defrecord InitialPathPointAlign [id index] (defrecord InitialPathPointAlign [id index]
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [shape (get-in state [:shapes-by-id id]) (let [shape (get-in state [:shapes id])
point (get-in shape [:points index]) point (get-in shape [:points index])
point (gpt/add point canvas-coords)] point (gpt/add point canvas-coords)]
(->> (align-point point) (->> (align-point point)

View file

@ -16,8 +16,8 @@
(let [sid (uuid/random) (let [sid (uuid/random)
shape (merge shape {:id sid :page page})] shape (merge shape {:id sid :page page})]
(as-> state $ (as-> state $
(update-in $ [:pages-by-id page :shapes] #(into [] (cons sid %))) (update-in $ [:pages page :shapes] #(into [] (cons sid %)))
(assoc-in $ [:shapes-by-id sid] shape)))) (assoc-in $ [:shapes sid] shape))))
(defn duplicate-shapes' (defn duplicate-shapes'
([state shapes page] ([state shapes page]
@ -30,12 +30,12 @@
shape (assoc shape :id id :page page :items []) shape (assoc shape :id id :page page :items [])
state (if (nil? group) state (if (nil? group)
(as-> state $ (as-> state $
(update-in $ [:pages-by-id page :shapes] #(into [] (cons id %))) (update-in $ [:pages page :shapes] #(into [] (cons id %)))
(assoc-in $ [:shapes-by-id id] shape)) (assoc-in $ [:shapes id] shape))
(as-> state $ (as-> state $
(update-in $ [:shapes-by-id group :items] #(into [] (cons id %))) (update-in $ [:shapes group :items] #(into [] (cons id %)))
(assoc-in $ [:shapes-by-id id] shape)))] (assoc-in $ [:shapes id] shape)))]
(->> (map #(get-in state [:shapes-by-id %]) items) (->> (map #(get-in state [:shapes %]) items)
(reduce #(duplicate-shape %1 %2 page id) state))) (reduce #(duplicate-shape %1 %2 page id) state)))
(let [id (uuid/random) (let [id (uuid/random)
shape (-> (dissoc shape :group) shape (-> (dissoc shape :group)
@ -43,11 +43,11 @@
(merge (when group {:group group})))] (merge (when group {:group group})))]
(if (nil? group) (if (nil? group)
(as-> state $ (as-> state $
(update-in $ [:pages-by-id page :shapes] #(into [] (cons id %))) (update-in $ [:pages page :shapes] #(into [] (cons id %)))
(assoc-in $ [:shapes-by-id id] shape)) (assoc-in $ [:shapes id] shape))
(as-> state $ (as-> state $
(update-in $ [:shapes-by-id group :items] #(into [] (cons id %))) (update-in $ [:shapes group :items] #(into [] (cons id %)))
(assoc-in $ [:shapes-by-id id] shape))))))] (assoc-in $ [:shapes id] shape))))))]
(reduce #(duplicate-shape %1 %2 page group) state shapes)))) (reduce #(duplicate-shape %1 %2 page group) state shapes))))
@ -60,7 +60,7 @@
(all-same-group? [coll] (all-same-group? [coll]
(let [group (:group (first coll))] (let [group (:group (first coll))]
(every? #(= group (:group %)) coll)))] (every? #(= group (:group %)) coll)))]
(let [shapes (reverse (mapv #(get-in state [:shapes-by-id %]) shapes))] (let [shapes (reverse (mapv #(get-in state [:shapes %]) shapes))]
(cond (cond
(all-toplevel? shapes) (all-toplevel? shapes)
(let [page (or page (:page (first shapes)))] (let [page (or page (:page (first shapes)))]
@ -82,28 +82,28 @@
data structure of shapes from the state." data structure of shapes from the state."
[state {:keys [id type] :as shape}] [state {:keys [id type] :as shape}]
(if (= :group type) (if (= :group type)
(let [items (map #(get-in state [:shapes-by-id %]) (:items shape))] (let [items (map #(get-in state [:shapes %]) (:items shape))]
(as-> state $ (as-> state $
(update-in $ [:shapes-by-id] dissoc id) (update-in $ [:shapes] dissoc id)
(reduce dissoc-from-index $ items))) (reduce dissoc-from-index $ items)))
(update-in state [:shapes-by-id] dissoc id))) (update-in state [:shapes] dissoc id)))
(defn dissoc-from-page (defn dissoc-from-page
"Given a shape, try to remove its reference from the "Given a shape, try to remove its reference from the
corresponding page." corresponding page."
[state {:keys [id page] :as shape}] [state {:keys [id page] :as shape}]
(as-> (get-in state [:pages-by-id page :shapes]) $ (as-> (get-in state [:pages page :shapes]) $
(into [] (remove #(= % id) $)) (into [] (remove #(= % id) $))
(assoc-in state [:pages-by-id page :shapes] $))) (assoc-in state [:pages page :shapes] $)))
(defn dissoc-from-group (defn dissoc-from-group
"Given a shape, try to remove its reference from the "Given a shape, try to remove its reference from the
corresponding group (only if it belongs to one group)." corresponding group (only if it belongs to one group)."
[state {:keys [id group] :as shape}] [state {:keys [id group] :as shape}]
(if-let [group' (get-in state [:shapes-by-id group])] (if-let [group' (get-in state [:shapes group])]
(as-> (:items group') $ (as-> (:items group') $
(into [] (remove #(= % id) $)) (into [] (remove #(= % id) $))
(assoc-in state [:shapes-by-id group :items] $)) (assoc-in state [:shapes group :items] $))
state)) state))
(declare dissoc-shape) (declare dissoc-shape)
@ -116,7 +116,7 @@
all empty parent groups of recently removed all empty parent groups of recently removed
shape." shape."
[state {:keys [group] :as shape}] [state {:keys [group] :as shape}]
(if-let [group' (get-in state [:shapes-by-id group])] (if-let [group' (get-in state [:shapes group])]
(if (empty? (:items group')) (if (empty? (:items group'))
(dissoc-shape state group') (dissoc-shape state group')
state) state)
@ -141,13 +141,13 @@
(defn drop-relative (defn drop-relative
[state loc sid] [state loc sid]
{:pre [(not (nil? sid))]} {:pre [(not (nil? sid))]}
(let [shape (get-in state [:shapes-by-id (first sid)]) (let [shape (get-in state [:shapes (first sid)])
{:keys [page group]} shape {:keys [page group]} shape
sid (:id shape) sid (:id shape)
shapes (if group shapes (if group
(get-in state [:shapes-by-id group :items]) (get-in state [:shapes group :items])
(get-in state [:pages-by-id page :shapes])) (get-in state [:pages page :shapes]))
index (case loc index (case loc
:first 0 :first 0
@ -160,19 +160,19 @@
(dissoc-from-group shape)) (dissoc-from-group shape))
shapes (if group shapes (if group
(get-in state [:shapes-by-id group :items]) (get-in state [:shapes group :items])
(get-in state [:pages-by-id page :shapes])) (get-in state [:pages page :shapes]))
shapes (drop-at-index index shapes sid)] shapes (drop-at-index index shapes sid)]
(if group (if group
(as-> state $ (as-> state $
(assoc-in $ [:shapes-by-id group :items] shapes) (assoc-in $ [:shapes group :items] shapes)
(update-in $ [:shapes-by-id sid] assoc :group group) (update-in $ [:shapes sid] assoc :group group)
(clear-empty-groups $ shape)) (clear-empty-groups $ shape))
(as-> state $ (as-> state $
(assoc-in $ [:pages-by-id page :shapes] shapes) (assoc-in $ [:pages page :shapes] shapes)
(update-in $ [:shapes-by-id sid] dissoc :group) (update-in $ [:shapes sid] dissoc :group)
(clear-empty-groups $ shape))))) (clear-empty-groups $ shape)))))
(defn drop-aside (defn drop-aside
@ -180,16 +180,16 @@
{:pre [(not= tid sid) {:pre [(not= tid sid)
(not (nil? tid)) (not (nil? tid))
(not (nil? sid))]} (not (nil? sid))]}
(let [{:keys [page group]} (get-in state [:shapes-by-id tid]) (let [{:keys [page group]} (get-in state [:shapes tid])
source (get-in state [:shapes-by-id sid]) source (get-in state [:shapes sid])
state (-> state state (-> state
(dissoc-from-page source) (dissoc-from-page source)
(dissoc-from-group source)) (dissoc-from-group source))
shapes (if group shapes (if group
(get-in state [:shapes-by-id group :items]) (get-in state [:shapes group :items])
(get-in state [:pages-by-id page :shapes])) (get-in state [:pages page :shapes]))
index (case loc index (case loc
:after (inc (index-of shapes tid)) :after (inc (index-of shapes tid))
@ -198,12 +198,12 @@
shapes (drop-at-index index shapes sid)] shapes (drop-at-index index shapes sid)]
(if group (if group
(as-> state $ (as-> state $
(assoc-in $ [:shapes-by-id group :items] shapes) (assoc-in $ [:shapes group :items] shapes)
(update-in $ [:shapes-by-id sid] assoc :group group) (update-in $ [:shapes sid] assoc :group group)
(clear-empty-groups $ source)) (clear-empty-groups $ source))
(as-> state $ (as-> state $
(assoc-in $ [:pages-by-id page :shapes] shapes) (assoc-in $ [:pages page :shapes] shapes)
(update-in $ [:shapes-by-id sid] dissoc :group) (update-in $ [:shapes sid] dissoc :group)
(clear-empty-groups $ source))))) (clear-empty-groups $ source)))))
(def drop-after #(drop-aside %1 :after %2 %3)) (def drop-after #(drop-aside %1 :after %2 %3))
@ -212,15 +212,15 @@
(defn drop-inside (defn drop-inside
[state tid sid] [state tid sid]
{:pre [(not= tid sid)]} {:pre [(not= tid sid)]}
(let [source (get-in state [:shapes-by-id sid]) (let [source (get-in state [:shapes sid])
state (-> state state (-> state
(dissoc-from-page source) (dissoc-from-page source)
(dissoc-from-group source)) (dissoc-from-group source))
shapes (get-in state [:shapes-by-id tid :items])] shapes (get-in state [:shapes tid :items])]
(if (seq shapes) (if (seq shapes)
(as-> state $ (as-> state $
(assoc-in $ [:shapes-by-id tid :items] (conj shapes sid)) (assoc-in $ [:shapes tid :items] (conj shapes sid))
(update-in $ [:shapes-by-id sid] assoc :group tid)) (update-in $ [:shapes sid] assoc :group tid))
state))) state)))
(defn drop-shape (defn drop-shape
@ -262,12 +262,12 @@
(defn match-by-selrect (defn match-by-selrect
[state page selrect] [state page selrect]
(let [xf (comp (map #(get-in state [:shapes-by-id %])) (let [xf (comp (map #(get-in state [:shapes %]))
(remove :hidden) (remove :hidden)
(remove :blocked) (remove :blocked)
(map geom/outer-rect)) (map geom/outer-rect))
match (partial try-match-shape xf selrect) match (partial try-match-shape xf selrect)
shapes (get-in state [:pages-by-id page :shapes])] shapes (get-in state [:pages page :shapes])]
(reduce match #{} (sequence xf shapes)))) (reduce match #{} (sequence xf shapes))))
(defn group-shapes (defn group-shapes
@ -282,37 +282,37 @@
(move-shapes-to-new-group [state page shapes new-group] (move-shapes-to-new-group [state page shapes new-group]
(reduce (fn [state {:keys [id group] :as shape}] (reduce (fn [state {:keys [id group] :as shape}]
(-> state (-> state
(update-in [:shapes-by-id group :items] #(remove (set [id]) %)) (update-in [:shapes group :items] #(remove (set [id]) %))
(update-in [:pages-by-id page :shapes] #(remove (set [id]) %)) (update-in [:pages page :shapes] #(remove (set [id]) %))
(clear-empty-groups shape) (clear-empty-groups shape)
(assoc-in [:shapes-by-id id :group] new-group) (assoc-in [:shapes id :group] new-group)
)) ))
state state
shapes)) shapes))
(update-shapes-on-page [state page shapes group] (update-shapes-on-page [state page shapes group]
(as-> (get-in state [:pages-by-id page :shapes]) $ (as-> (get-in state [:pages page :shapes]) $
(replace-first-item (set shapes) $ group) (replace-first-item (set shapes) $ group)
(remove (set shapes) $) (remove (set shapes) $)
(into [] $) (into [] $)
(assoc-in state [:pages-by-id page :shapes] $))) (assoc-in state [:pages page :shapes] $)))
(update-shapes-on-group [state parent-group shapes group] (update-shapes-on-group [state parent-group shapes group]
(as-> (get-in state [:shapes-by-id parent-group :items]) $ (as-> (get-in state [:shapes parent-group :items]) $
(replace-first-item (set shapes) $ group) (replace-first-item (set shapes) $ group)
(remove (set shapes) $) (remove (set shapes) $)
(into [] $) (into [] $)
(assoc-in state [:shapes-by-id parent-group :items] $))) (assoc-in state [:shapes parent-group :items] $)))
(update-shapes-on-index [state shapes group] (update-shapes-on-index [state shapes group]
(reduce (fn [state {:keys [id] :as shape}] (reduce (fn [state {:keys [id] :as shape}]
(as-> shape $ (as-> shape $
(assoc $ :group group) (assoc $ :group group)
(assoc-in state [:shapes-by-id id] $))) (assoc-in state [:shapes id] $)))
state state
shapes))] shapes))]
(let [sid (uuid/random) (let [sid (uuid/random)
shapes' (map #(get-in state [:shapes-by-id %]) shapes) shapes' (map #(get-in state [:shapes %]) shapes)
distinct-groups (distinct (map :group shapes')) distinct-groups (distinct (map :group shapes'))
parent-group (cond parent-group (cond
(not= 1 (count distinct-groups)) :multi (not= 1 (count distinct-groups)) :multi
@ -329,48 +329,48 @@
(= :multi parent-group) (= :multi parent-group)
(-> $ (-> $
(move-shapes-to-new-group page shapes' sid) (move-shapes-to-new-group page shapes' sid)
(update-in [:pages-by-id page :shapes] #(into [] (cons sid %)))) (update-in [:pages page :shapes] #(into [] (cons sid %))))
(= :page parent-group) (= :page parent-group)
(update-shapes-on-page $ page shapes sid) (update-shapes-on-page $ page shapes sid)
:else :else
(update-shapes-on-group $ parent-group shapes sid)) (update-shapes-on-group $ parent-group shapes sid))
(update $ :shapes-by-id assoc sid group) (update $ :shapes assoc sid group)
(cond (cond
(= :multi parent-group) $ (= :multi parent-group) $
(= :page parent-group) $ (= :page parent-group) $
:else (assoc-in $ [:shapes-by-id sid :group] parent-group)) :else (assoc-in $ [:shapes sid :group] parent-group))
(update $ :workspace assoc :selected #{sid}))))) (update $ :workspace assoc :selected #{sid})))))
(defn degroup-shapes (defn degroup-shapes
[state shapes page] [state shapes page]
(letfn [(empty-group [state page-id group-id] (letfn [(empty-group [state page-id group-id]
(let [group (get-in state [:shapes-by-id group-id]) (let [group (get-in state [:shapes group-id])
parent-id (:group group) parent-id (:group group)
position (if (nil? parent-id) position (if (nil? parent-id)
(index-of (get-in state [:pages-by-id page-id :shapes]) group-id) (index-of (get-in state [:pages page-id :shapes]) group-id)
(index-of (get-in state [:shapes-by-id parent-id :items]) group-id)) (index-of (get-in state [:shapes parent-id :items]) group-id))
reduce-func (fn [state item] reduce-func (fn [state item]
(if (nil? parent-id) (if (nil? parent-id)
(-> state (-> state
(update-in [:pages-by-id page-id :shapes] #(drop-at-index position % item)) (update-in [:pages page-id :shapes] #(drop-at-index position % item))
(update-in [:shapes-by-id item] dissoc :group)) (update-in [:shapes item] dissoc :group))
(-> state (-> state
(update-in [:shapes-by-id parent-id :items] #(drop-at-index position % item)) (update-in [:shapes parent-id :items] #(drop-at-index position % item))
(assoc-in [:shapes-by-id item :group] parent-id))))] (assoc-in [:shapes item :group] parent-id))))]
(as-> state $ (as-> state $
(reduce reduce-func $ (reverse (:items group))) (reduce reduce-func $ (reverse (:items group)))
(if (nil? parent-id) (if (nil? parent-id)
(update-in $ [:pages-by-id page-id :shapes] #(into [] (remove #{group-id} %))) (update-in $ [:pages page-id :shapes] #(into [] (remove #{group-id} %)))
(update-in $ [:shapes-by-id parent-id :items] #(into [] (remove #{group-id} %)))) (update-in $ [:shapes parent-id :items] #(into [] (remove #{group-id} %))))
(update-in $ [:shapes-by-id] dissoc group-id)))) (update-in $ [:shapes] dissoc group-id))))
(empty-groups [state page-id groups-ids] (empty-groups [state page-id groups-ids]
(reduce (fn [state group-id] (reduce (fn [state group-id]
(empty-group state page-id group-id)) (empty-group state page-id group-id))
state state
groups-ids))] groups-ids))]
(let [shapes' (map #(get-in state [:shapes-by-id %]) shapes) (let [shapes' (map #(get-in state [:shapes %]) shapes)
groups (filter #(= (:type %) :group) shapes') groups (filter #(= (:type %) :group) shapes')
groups-ids (map :id groups) groups-ids (map :id groups)
groups-items (remove (set groups-ids) (mapcat :items groups))] groups-items (remove (set groups-ids) (mapcat :items groups))]

View file

@ -62,7 +62,7 @@
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state stream] (-apply-watch [_ state stream]
(let [initialized? (get-in state [:undo id]) (let [initialized? (get-in state [:undo id])
page-loaded? (get-in state [:pages-by-id id])] page-loaded? (get-in state [:pages id])]
(cond (cond
(and page-loaded? initialized?) (and page-loaded? initialized?)
(rx/empty) (rx/empty)
@ -93,7 +93,7 @@
(if (>= selected (dec (count stack))) (if (>= selected (dec (count stack)))
state state
(let [pointer (inc selected) (let [pointer (inc selected)
page (get-in state [:pages-by-id page-id]) page (get-in state [:pages page-id])
data (nth stack pointer) data (nth stack pointer)
packed (assoc page :data data)] packed (assoc page :data data)]
@ -127,7 +127,7 @@
state state
(let [pointer (dec selected) (let [pointer (dec selected)
data (nth stack pointer) data (nth stack pointer)
page (get-in state [:pages-by-id page-id]) page (get-in state [:pages page-id])
packed (assoc page :data data)] packed (assoc page :data data)]
;; (println "Redo: pointer=" pointer) ;; (println "Redo: pointer=" pointer)

View file

@ -58,7 +58,7 @@
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [page-id page (let [page-id page
page (get-in state [:pages-by-id page-id])] page (get-in state [:pages page-id])]
;; Activate loaded if page is not fetched. ;; Activate loaded if page is not fetched.
(when-not page (reset! st/loader true)) (when-not page (reset! st/loader true))
@ -81,7 +81,7 @@
rs/EffectEvent rs/EffectEvent
(-apply-effect [_ state] (-apply-effect [_ state]
;; Optimistic prefetch of projects if them are not already fetched ;; Optimistic prefetch of projects if them are not already fetched
(when-not (seq (:projects-by-id state)) (when-not (seq (:projects state))
(rs/emit! (dp/fetch-projects))))) (rs/emit! (dp/fetch-projects)))))
(defn initialize (defn initialize
@ -208,7 +208,7 @@
(defrecord InitializeAlignmentIndex [id] (defrecord InitializeAlignmentIndex [id]
rs/WatchEvent rs/WatchEvent
(-apply-watch [_ state s] (-apply-watch [_ state s]
(let [page (get-in state [:pages-by-id id]) (let [page (get-in state [:pages id])
opts (:options page) opts (:options page)
message {:cmd :grid/init message {:cmd :grid/init
:width c/viewport-width :width c/viewport-width

View file

@ -39,12 +39,12 @@
:path (path-shape s) :path (path-shape s)
:circle (circle-shape s) :circle (circle-shape s)
:image (let [image-id (:image s) :image (let [image-id (:image s)
image (get-in @*state* [:images-by-id image-id])] image (get-in @*state* [:images image-id])]
(image-shape (assoc s :image image))))) (image-shape (assoc s :image image)))))
(mx/defc shape (mx/defc shape
[sid] [sid]
(shape* (get-in @*state* [:shapes-by-id sid]))) (shape* (get-in @*state* [:shapes sid])))
(mx/defc page-svg (mx/defc page-svg
[{:keys [width height] :as page}] [{:keys [width height] :as page}]
@ -60,5 +60,5 @@
(defn render-page (defn render-page
[id] [id]
(let [page (get-in @st/state [:pages-by-id id])] (let [page (get-in @st/state [:pages id])]
(mx/render-static-html (page-svg page)))) (mx/render-static-html (page-svg page))))

View file

@ -513,7 +513,7 @@
(defn- inner-rect-group (defn- inner-rect-group
[state {:keys [id group rotation dx dy] :as shape}] [state {:keys [id group rotation dx dy] :as shape}]
(let [shapes (->> (:items shape) (let [shapes (->> (:items shape)
(map #(get-in state [:shapes-by-id %])) (map #(get-in state [:shapes %]))
(map #(inner-rect state %))) (map #(inner-rect state %)))
x (apply min (map :x shapes)) x (apply min (map :x shapes))
y (apply min (map :y shapes)) y (apply min (map :y shapes))
@ -545,13 +545,13 @@
:group (outer-rect-group state shape) :group (outer-rect-group state shape)
(outer-rect-generic state shape))] (outer-rect-generic state shape))]
(if (:group shape) (if (:group shape)
(let [group (get-in state [:shapes-by-id (:group shape)])] (let [group (get-in state [:shapes (:group shape)])]
(apply-parent-deltas state shape (:group group))) (apply-parent-deltas state shape (:group group)))
shape)))) shape))))
(defn- apply-parent-deltas (defn- apply-parent-deltas
[state {:keys [x y] :as shape} id] [state {:keys [x y] :as shape} id]
(if-let [group (get-in state [:shapes-by-id id])] (if-let [group (get-in state [:shapes id])]
(let [props {:x (+ x (:dx group 0)) (let [props {:x (+ x (:dx group 0))
:y (+ y (:dy group 0))}] :y (+ y (:dy group 0))}]
(apply-parent-deltas state (merge shape props) (:group group))) (apply-parent-deltas state (merge shape props) (:group group)))
@ -559,7 +559,7 @@
(defn- outer-rect-generic (defn- outer-rect-generic
[state {:keys [x1 y1 x2 y2 group] :as shape}] [state {:keys [x1 y1 x2 y2 group] :as shape}]
(let [group (get-in state [:shapes-by-id group]) (let [group (get-in state [:shapes group])
props {:x (+ x1 (:dx group 0)) props {:x (+ x1 (:dx group 0))
:y (+ y1 (:dy group 0)) :y (+ y1 (:dy group 0))
:width (- x2 x1) :width (- x2 x1)
@ -569,7 +569,7 @@
(defn- outer-rect-circle (defn- outer-rect-circle
[state {:keys [cx cy rx ry group] :as shape}] [state {:keys [cx cy rx ry group] :as shape}]
(let [group (get-in state [:shapes-by-id group]) (let [group (get-in state [:shapes group])
props {:x (+ (- cx rx) (:dx group 0)) props {:x (+ (- cx rx) (:dx group 0))
:y (+ (- cy ry) (:dy group 0)) :y (+ (- cy ry) (:dy group 0))
:width (* rx 2) :width (* rx 2)
@ -579,7 +579,7 @@
(defn- outer-rect-path (defn- outer-rect-path
[state {:keys [points group] :as shape}] [state {:keys [points group] :as shape}]
(let [group (get-in state [:shapes-by-id group]) (let [group (get-in state [:shapes group])
minx (apply min (map :x points)) minx (apply min (map :x points))
miny (apply min (map :y points)) miny (apply min (map :y points))
maxx (apply max (map :x points)) maxx (apply max (map :x points))
@ -596,7 +596,7 @@
(defn- outer-rect-group (defn- outer-rect-group
[state {:keys [id group rotation dx dy] :as shape}] [state {:keys [id group rotation dx dy] :as shape}]
(let [shapes (->> (:items shape) (let [shapes (->> (:items shape)
(map #(get-in state [:shapes-by-id %])) (map #(get-in state [:shapes %]))
(map #(outer-rect state %))) (map #(outer-rect state %)))
x (apply min (map :x shapes)) x (apply min (map :x shapes))
y (apply min (map :y shapes)) y (apply min (map :y shapes))
@ -754,7 +754,7 @@
(defn- transformation-matrix-group (defn- transformation-matrix-group
[state {:keys [dx dy rotation items] :or {rotation 0} :as shape}] [state {:keys [dx dy rotation items] :or {rotation 0} :as shape}]
(let [shapes-by-id (get state :shapes-by-id) (let [shapes-by-id (get state :shapes)
shapes (map #(get shapes-by-id %) items) shapes (map #(get shapes-by-id %) items)
{:keys [x y width height]} (outer-rect-coll shapes) {:keys [x y width height]} (outer-rect-coll shapes)
center-x (+ x (/ width 2)) center-x (+ x (/ width 2))
@ -784,7 +784,7 @@
(resolve-parent @st/state shape)) (resolve-parent @st/state shape))
([state {:keys [group] :as shape}] ([state {:keys [group] :as shape}]
(if group (if group
(resolve-parent state (get-in state [:shapes-by-id group])) (resolve-parent state (get-in state [:shapes group]))
shape))) shape)))
(defn contained-in? (defn contained-in?
@ -827,7 +827,7 @@
(defn parent-satisfies? (defn parent-satisfies?
"Resolve the first parent that satisfies a condition." "Resolve the first parent that satisfies a condition."
[{:keys [group] :as shape} pred] [{:keys [group] :as shape} pred]
(let [shapes-by-id (:shapes-by-id @st/state)] (let [shapes-by-id (:shapes @st/state)]
(if group (if group
(loop [parent (get shapes-by-id group)] (loop [parent (get shapes-by-id group)]
(cond (cond

View file

@ -33,14 +33,14 @@
:undo {} :undo {}
:profile nil :profile nil
:workspace nil :workspace nil
:image-colls-by-id nil :image-collections nil
:images-by-id nil
:icon-colls-by-id nil
:icons-by-id nil
:shapes-by-id nil
:color-collections colors/collections :color-collections colors/collections
:projects-by-id nil :icon-collections nil
:pages-by-id nil}) :images nil
:icons nil
:shapes nil
:projects nil
:pages nil})
(defn init (defn init
"Initialize the state materialization." "Initialize the state materialization."

View file

@ -56,11 +56,11 @@
(l/derive st/state))) (l/derive st/state)))
(def ^:private collections-ref (def ^:private collections-ref
(-> (l/key :icon-colls-by-id) (-> (l/key :icon-collections)
(l/derive st/state))) (l/derive st/state)))
(def ^:private icons-ref (def ^:private icons-ref
(-> (l/key :icons-by-id) (-> (l/key :icons)
(l/derive st/state))) (l/derive st/state)))
;; (defn- focus-collection ;; (defn- focus-collection

View file

@ -52,11 +52,11 @@
(l/derive st/state))) (l/derive st/state)))
(def ^:private collections-ref (def ^:private collections-ref
(-> (l/key :image-colls-by-id) (-> (l/key :image-collections)
(l/derive st/state))) (l/derive st/state)))
(def ^:private images-ref (def ^:private images-ref
(-> (l/key :images-by-id) (-> (l/key :images)
(l/derive st/state))) (l/derive st/state)))
(def ^:private uploading?-ref (def ^:private uploading?-ref

View file

@ -60,7 +60,7 @@
;; --- Refs ;; --- Refs
(def projects-map-ref (def projects-map-ref
(-> (l/key :projects-by-id) (-> (l/key :projects)
(l/derive st/state))) (l/derive st/state)))
(def dashboard-ref (def dashboard-ref

View file

@ -24,7 +24,7 @@
(defn- focus-shape (defn- focus-shape
[id] [id]
(-> (l/in [:shapes-by-id id]) (-> (l/in [:shapes id])
(l/derive st/state))) (l/derive st/state)))
(defn render-component (defn render-component

View file

@ -20,7 +20,7 @@
(defn image-ref (defn image-ref
[id] [id]
(-> (l/in [:images-by-id id]) (-> (l/in [:images id])
(l/derive st/state))) (l/derive st/state)))
;; --- Image Component ;; --- Image Component

View file

@ -33,7 +33,7 @@
(defn- focus-selected-shapes (defn- focus-selected-shapes
[state] [state]
(let [selected (get-in state [:workspace :selected])] (let [selected (get-in state [:workspace :selected])]
(mapv #(get-in state [:shapes-by-id %]) selected))) (mapv #(get-in state [:shapes %]) selected)))
(def ^:private selected-shapes-ref (def ^:private selected-shapes-ref
(-> (l/lens focus-selected-shapes) (-> (l/lens focus-selected-shapes)

View file

@ -28,14 +28,14 @@
(def project-ref (def project-ref
(letfn [(getter [state] (letfn [(getter [state]
(let [project (get-in state [:workspace :project])] (let [project (get-in state [:workspace :project])]
(get-in state [:projects-by-id project])))] (get-in state [:projects project])))]
(-> (l/lens getter) (-> (l/lens getter)
(l/derive st/state)))) (l/derive st/state))))
(def page-ref (def page-ref
(letfn [(getter [state] (letfn [(getter [state]
(let [page (get-in state [:workspace :page])] (let [page (get-in state [:workspace :page])]
(get-in state [:pages-by-id page])))] (get-in state [:pages page])))]
(-> (l/lens getter) (-> (l/lens getter)
(l/derive st/state)))) (l/derive st/state))))
@ -52,7 +52,7 @@
(l/derive $ workspace-ref))) (l/derive $ workspace-ref)))
(def shapes-by-id-ref (def shapes-by-id-ref
(as-> (l/key :shapes-by-id) $ (as-> (l/key :shapes) $
(l/derive $ st/state))) (l/derive $ st/state)))
(def zoom-ref (def zoom-ref

View file

@ -28,7 +28,7 @@
(defn- focus-shape (defn- focus-shape
[id] [id]
(-> (l/in [:shapes-by-id id]) (-> (l/in [:shapes id])
(l/derive st/state))) (l/derive st/state)))
(defn- colorpicker-render (defn- colorpicker-render

View file

@ -28,7 +28,7 @@
(defn- resolve-pages (defn- resolve-pages
[state] [state]
(let [project (get-in state [:workspace :project])] (let [project (get-in state [:workspace :project])]
(->> (vals (:pages-by-id state)) (->> (vals (:pages state))
(filter #(= project (:project %))) (filter #(= project (:project %)))
(sort-by :created-at)))) (sort-by :created-at))))

View file

@ -28,11 +28,11 @@
(l/derive st/state))) (l/derive st/state)))
(def ^:private collections-ref (def ^:private collections-ref
(-> (l/key :image-colls-by-id) (-> (l/key :image-collections)
(l/derive st/state))) (l/derive st/state)))
(def ^:private images-ref (def ^:private images-ref
(-> (l/key :images-by-id) (-> (l/key :images)
(l/derive st/state))) (l/derive st/state)))
(def ^:private uploading?-ref (def ^:private uploading?-ref

View file

@ -28,7 +28,7 @@
(defn- focus-page (defn- focus-page
[id] [id]
(-> (l/in [:pages-by-id id]) (-> (l/in [:pages id])
(l/derive st/state))) (l/derive st/state)))
(defn- select-shape (defn- select-shape

View file

@ -103,7 +103,7 @@
(letfn [(getter [state] (letfn [(getter [state]
(let [selected (get-in state [:workspace :selected])] (let [selected (get-in state [:workspace :selected])]
(when (= 1 (count selected)) (when (= 1 (count selected))
(get-in state [:shapes-by-id (first selected)]))))] (get-in state [:shapes (first selected)]))))]
(-> (l/lens getter) (-> (l/lens getter)
(l/derive st/state)))) (l/derive st/state))))

View file

@ -141,8 +141,8 @@
(defn- collect-shapes (defn- collect-shapes
[state page] [state page]
(let [shapes-by-id (:shapes-by-id state) (let [shapes-by-id (:shapes state)
shapes (get-in state [:pages-by-id page :shapes])] shapes (get-in state [:pages page :shapes])]
(letfn [(resolve-shape [acc id] (letfn [(resolve-shape [acc id]
(let [shape (get shapes-by-id id)] (let [shape (get shapes-by-id id)]
(if (= (:type shape) :group) (if (= (:type shape) :group)

View file

@ -29,7 +29,7 @@
(defn- resolve-pages (defn- resolve-pages
[state] [state]
(let [project (get-in state [:workspace :project])] (let [project (get-in state [:workspace :project])]
(->> (vals (:pages-by-id state)) (->> (vals (:pages state))
(filter #(= project (:project %))) (filter #(= project (:project %)))
(sort-by :created-at)))) (sort-by :created-at))))

View file

@ -16,8 +16,8 @@
:pages nil :pages nil
:page nil :page nil
:flags #{:sitemap} :flags #{:sitemap}
:shapes-by-id {} :shapes {}
:pages-by-id {}}) :pages {}})
(defn ^:export init (defn ^:export init
[] []

View file

@ -36,12 +36,12 @@
[state page] [state page]
(let [data (:data page) (let [data (:data page)
shapes (:shapes data) shapes (:shapes data)
shapes-by-id (:shapes-by-id data) shapes-by-id (:shapes data)
page (-> page page (-> page
(dissoc page :data) (dissoc page :data)
(assoc :shapes shapes))] (assoc :shapes shapes))]
(-> state (-> state
(update :shapes-by-id merge shapes-by-id) (update :shapes merge shapes-by-id)
(update :pages conj page)))) (update :pages conj page))))
(defrecord DataLoaded [data] (defrecord DataLoaded [data]

View file

@ -98,7 +98,7 @@
(defn- run-opacity-interaction (defn- run-opacity-interaction
[{:keys [element opacity easing delay [{:keys [element opacity easing delay
duration animation direction]}] duration animation direction]}]
(let [shape (get-in @st/state [:shapes-by-id element]) (let [shape (get-in @st/state [:shapes element])
dom (dom/get-element (str "shape-" element))] dom (dom/get-element (str "shape-" element))]
(if (= direction :reverse) (if (= direction :reverse)
(animate* dom {:fillOpacity (:fill-opacity shape "1") (animate* dom {:fillOpacity (:fill-opacity shape "1")
@ -115,7 +115,7 @@
;; (defn- run-size-interaction ;; (defn- run-size-interaction
;; [{:keys [element resize-width resize-height ;; [{:keys [element resize-width resize-height
;; easing delay duration] :as opts}] ;; easing delay duration] :as opts}]
;; (let [shape (get-in @st/state [:shapes-by-id element]) ;; (let [shape (get-in @st/state [:shapes element])
;; tf (geom/transformation-matrix ;; tf (geom/transformation-matrix
;; (geom/resize shape (gpt/point resize-width resize-height)))] ;; (geom/resize shape (gpt/point resize-width resize-height)))]
;; (animate :targets [(str "#shape-" element)] ;; (animate :targets [(str "#shape-" element)]
@ -176,7 +176,7 @@
(defn- run-size-interaction (defn- run-size-interaction
[{:keys [element] :as opts}] [{:keys [element] :as opts}]
(let [shape (get-in @st/state [:shapes-by-id element])] (let [shape (get-in @st/state [:shapes element])]
(case (:type shape) (case (:type shape)
:icon (run-size-interaction-icon shape opts) :icon (run-size-interaction-icon shape opts)
:rect (run-size-interaction-rect shape opts)))) :rect (run-size-interaction-rect shape opts))))
@ -191,7 +191,7 @@
(defn- run-color-interaction (defn- run-color-interaction
[{:keys [element fill-color stroke-color direction easing delay duration]}] [{:keys [element fill-color stroke-color direction easing delay duration]}]
(let [shape (get-in @st/state [:shapes-by-id element]) (let [shape (get-in @st/state [:shapes element])
dom (dom/get-element (str "shape-" element))] dom (dom/get-element (str "shape-" element))]
(if (= direction :reverse) (if (= direction :reverse)
(animate* dom {:easing (translate-ease easing) (animate* dom {:easing (translate-ease easing)
@ -207,7 +207,7 @@
(defn- run-rotate-interaction (defn- run-rotate-interaction
[{:keys [element rotation direction easing delay duration] :as opts}] [{:keys [element rotation direction easing delay duration] :as opts}]
(let [shape (get-in @st/state [:shapes-by-id element]) (let [shape (get-in @st/state [:shapes element])
dom (dom/get-element (str "shape-" element)) dom (dom/get-element (str "shape-" element))
mtx1 (geom/transformation-matrix (update shape :rotation + rotation)) mtx1 (geom/transformation-matrix (update shape :rotation + rotation))
mtx2 (geom/transformation-matrix shape)] mtx2 (geom/transformation-matrix shape)]

View file

@ -76,5 +76,5 @@
(mx/defc shape (mx/defc shape
[sid] [sid]
(let [item (get-in @st/state [:shapes-by-id sid])] (let [item (get-in @st/state [:shapes sid])]
(interactions-wrapper item shape*))) (interactions-wrapper item shape*)))