mirror of
https://github.com/penpot/penpot.git
synced 2025-05-17 23:06:10 +02:00
Move state.project functions to appropriate data related namespaces.
This commit is contained in:
parent
8b32c69377
commit
ad5095ce5d
7 changed files with 122 additions and 129 deletions
|
@ -16,7 +16,6 @@
|
||||||
[uxbox.util.schema :as sc]
|
[uxbox.util.schema :as sc]
|
||||||
[uxbox.main.data.pages :as udp]
|
[uxbox.main.data.pages :as udp]
|
||||||
[uxbox.main.state :as st]
|
[uxbox.main.state :as st]
|
||||||
[uxbox.main.state.project :as stpr]
|
|
||||||
[uxbox.util.datetime :as dt]
|
[uxbox.util.datetime :as dt]
|
||||||
[uxbox.util.data :refer (without-keys
|
[uxbox.util.data :refer (without-keys
|
||||||
replace-by-id
|
replace-by-id
|
||||||
|
@ -135,7 +134,7 @@
|
||||||
:history true
|
:history true
|
||||||
:data (:data item))]
|
:data (:data item))]
|
||||||
(-> state
|
(-> state
|
||||||
(stpr/unpack-page page)
|
(udp/unpack-page page)
|
||||||
(assoc-in [:workspace :history :selected] version)))))
|
(assoc-in [:workspace :history :selected] version)))))
|
||||||
|
|
||||||
(defn select-page-history
|
(defn select-page-history
|
||||||
|
@ -165,7 +164,7 @@
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(let [packed (get-in state [:pagedata-by-id id])]
|
(let [packed (get-in state [:pagedata-by-id id])]
|
||||||
(-> (stpr/unpack-page state packed)
|
(-> (udp/unpack-page state packed)
|
||||||
(assoc-in [:workspace :history :deselecting] true)
|
(assoc-in [:workspace :history :deselecting] true)
|
||||||
(assoc-in [:workspace :history :selected] nil))))
|
(assoc-in [:workspace :history :selected] nil))))
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
[uxbox.util.i18n :refer (tr)]
|
[uxbox.util.i18n :refer (tr)]
|
||||||
[uxbox.util.schema :as sc]
|
[uxbox.util.schema :as sc]
|
||||||
[uxbox.main.state :as st]
|
[uxbox.main.state :as st]
|
||||||
[uxbox.main.state.project :as stpr]
|
|
||||||
[uxbox.util.datetime :as dt]
|
[uxbox.util.datetime :as dt]
|
||||||
[uxbox.util.data :refer (without-keys replace-by-id)]))
|
[uxbox.util.data :refer (without-keys replace-by-id)]))
|
||||||
|
|
||||||
|
@ -24,14 +23,69 @@
|
||||||
"A marker protocol for mark events that alters the
|
"A marker protocol for mark events that alters the
|
||||||
page and is subject to perform a backend synchronization.")
|
page and is subject to perform a backend synchronization.")
|
||||||
|
|
||||||
|
;; --- Page Pack/Unpack
|
||||||
|
|
||||||
|
(defn dissoc-page-shapes
|
||||||
|
[state id]
|
||||||
|
(let [shapes (get-in state [:shapes-by-id])]
|
||||||
|
(assoc state :shapes-by-id (reduce-kv (fn [acc k v]
|
||||||
|
(if (= (:page v) id)
|
||||||
|
(dissoc acc k)
|
||||||
|
acc))
|
||||||
|
shapes
|
||||||
|
shapes))))
|
||||||
|
|
||||||
|
(defn pack-page
|
||||||
|
"Return a packed version of page object ready
|
||||||
|
for send to remore storage service."
|
||||||
|
[state id]
|
||||||
|
(let [page (get-in state [:pages-by-id id])
|
||||||
|
xf (filter #(= (:page (second %)) id))
|
||||||
|
shapes (into {} xf (:shapes-by-id state))]
|
||||||
|
(-> page
|
||||||
|
(assoc-in [:data :shapes] (into [] (:shapes page)))
|
||||||
|
(assoc-in [:data :shapes-by-id] shapes)
|
||||||
|
(update-in [:data] dissoc :items)
|
||||||
|
(dissoc :shapes))))
|
||||||
|
|
||||||
|
(defn unpack-page
|
||||||
|
"Unpacks packed page object and assocs it to the
|
||||||
|
provided state."
|
||||||
|
[state page]
|
||||||
|
(let [data (:data page)
|
||||||
|
shapes (:shapes data)
|
||||||
|
shapes-by-id (:shapes-by-id data)
|
||||||
|
page (-> page
|
||||||
|
(dissoc page :data)
|
||||||
|
(assoc :shapes shapes))]
|
||||||
|
(-> state
|
||||||
|
(update :shapes-by-id merge shapes-by-id)
|
||||||
|
(update :pages-by-id assoc (:id page) page))))
|
||||||
|
|
||||||
|
(defn purge-page
|
||||||
|
"Remove page and all related stuff from the state."
|
||||||
|
[state id]
|
||||||
|
(-> state
|
||||||
|
(update :pages-by-id dissoc id)
|
||||||
|
(update :pagedata-by-id dissoc id)
|
||||||
|
(dissoc-page-shapes id)))
|
||||||
|
|
||||||
|
(defn assoc-page
|
||||||
|
[state {:keys [id] :as page}]
|
||||||
|
(assoc-in state [:pagedata-by-id id] page))
|
||||||
|
|
||||||
|
(defn dissoc-page
|
||||||
|
[state id]
|
||||||
|
(update state :pagedata-by-id dissoc id))
|
||||||
|
|
||||||
;; --- Pages Fetched
|
;; --- Pages Fetched
|
||||||
|
|
||||||
(defrecord PagesFetched [pages]
|
(defrecord PagesFetched [pages]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(as-> state $
|
(as-> state $
|
||||||
(reduce stpr/unpack-page $ pages)
|
(reduce unpack-page $ pages)
|
||||||
(reduce stpr/assoc-page $ pages))))
|
(reduce assoc-page $ pages))))
|
||||||
|
|
||||||
(defn pages-fetched?
|
(defn pages-fetched?
|
||||||
[v]
|
[v]
|
||||||
|
@ -56,8 +110,8 @@
|
||||||
(-apply-watch [this state s]
|
(-apply-watch [this state s]
|
||||||
(letfn [(on-created [{page :payload}]
|
(letfn [(on-created [{page :payload}]
|
||||||
(rx/of
|
(rx/of
|
||||||
#(stpr/unpack-page % page)
|
#(unpack-page % page)
|
||||||
#(stpr/assoc-page % page)))]
|
#(assoc-page % page)))]
|
||||||
(let [params (-> (into {} this)
|
(let [params (-> (into {} this)
|
||||||
(assoc :data {}))]
|
(assoc :data {}))]
|
||||||
(->> (rp/req :create/page params)
|
(->> (rp/req :create/page params)
|
||||||
|
@ -82,7 +136,7 @@
|
||||||
(-apply-update [this state]
|
(-apply-update [this state]
|
||||||
(-> state
|
(-> state
|
||||||
(assoc-in [:pages-by-id (:id page) :version] (:version page))
|
(assoc-in [:pages-by-id (:id page) :version] (:version page))
|
||||||
(stpr/assoc-page page))))
|
(assoc-page page))))
|
||||||
|
|
||||||
(defn- page-synced?
|
(defn- page-synced?
|
||||||
[event]
|
[event]
|
||||||
|
@ -93,7 +147,7 @@
|
||||||
(defrecord SyncPage [id]
|
(defrecord SyncPage [id]
|
||||||
rs/WatchEvent
|
rs/WatchEvent
|
||||||
(-apply-watch [this state s]
|
(-apply-watch [this state s]
|
||||||
(let [page (stpr/pack-page state id)]
|
(let [page (pack-page state id)]
|
||||||
(->> (rp/req :update/page page)
|
(->> (rp/req :update/page page)
|
||||||
(rx/map (comp ->PageSynced :payload))))))
|
(rx/map (comp ->PageSynced :payload))))))
|
||||||
|
|
||||||
|
@ -188,7 +242,7 @@
|
||||||
rs/WatchEvent
|
rs/WatchEvent
|
||||||
(-apply-watch [_ state s]
|
(-apply-watch [_ state s]
|
||||||
(letfn [(on-success [_]
|
(letfn [(on-success [_]
|
||||||
#(stpr/purge-page % id))]
|
#(purge-page % id))]
|
||||||
(->> (rp/req :delete/page id)
|
(->> (rp/req :delete/page id)
|
||||||
(rx/map on-success)
|
(rx/map on-success)
|
||||||
(rx/tap callback)
|
(rx/tap callback)
|
||||||
|
|
|
@ -14,7 +14,6 @@
|
||||||
[uxbox.main.repo :as rp]
|
[uxbox.main.repo :as rp]
|
||||||
[uxbox.util.i18n :refer (tr)]
|
[uxbox.util.i18n :refer (tr)]
|
||||||
[uxbox.util.schema :as sc]
|
[uxbox.util.schema :as sc]
|
||||||
[uxbox.main.state.project :as stpr]
|
|
||||||
[uxbox.main.data.pages :as udp]))
|
[uxbox.main.data.pages :as udp]))
|
||||||
|
|
||||||
;; --- Initialize
|
;; --- Initialize
|
||||||
|
@ -46,10 +45,12 @@
|
||||||
|
|
||||||
;; --- Projects Fetched
|
;; --- Projects Fetched
|
||||||
|
|
||||||
|
(declare assoc-project)
|
||||||
|
|
||||||
(defrecord ProjectsFetched [projects]
|
(defrecord ProjectsFetched [projects]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(reduce stpr/assoc-project state projects)))
|
(reduce assoc-project state projects)))
|
||||||
|
|
||||||
(defn projects-fetched
|
(defn projects-fetched
|
||||||
[projects]
|
[projects]
|
||||||
|
@ -77,7 +78,7 @@
|
||||||
(defrecord ProjectCreated [project]
|
(defrecord ProjectCreated [project]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(stpr/assoc-project state project)))
|
(assoc-project state project)))
|
||||||
|
|
||||||
(defn project-created
|
(defn project-created
|
||||||
[data]
|
[data]
|
||||||
|
@ -113,11 +114,13 @@
|
||||||
|
|
||||||
;; --- Delete Project (by id)
|
;; --- Delete Project (by id)
|
||||||
|
|
||||||
|
(declare dissoc-project)
|
||||||
|
|
||||||
(defrecord DeleteProject [id]
|
(defrecord DeleteProject [id]
|
||||||
rs/WatchEvent
|
rs/WatchEvent
|
||||||
(-apply-watch [_ state s]
|
(-apply-watch [_ state s]
|
||||||
(letfn [(on-success [_]
|
(letfn [(on-success [_]
|
||||||
#(stpr/dissoc-project % id))]
|
#(dissoc-project % id))]
|
||||||
(->> (rp/req :delete/project id)
|
(->> (rp/req :delete/project id)
|
||||||
(rx/map on-success)))))
|
(rx/map on-success)))))
|
||||||
|
|
||||||
|
@ -163,24 +166,7 @@
|
||||||
([projectid] (GoTo. projectid))
|
([projectid] (GoTo. projectid))
|
||||||
([projectid pageid] (GoToPage. projectid pageid)))
|
([projectid pageid] (GoToPage. projectid pageid)))
|
||||||
|
|
||||||
;; --- Helpers
|
;; --- UI related events
|
||||||
|
|
||||||
(defn sort-projects-by
|
|
||||||
[ordering projs]
|
|
||||||
(case ordering
|
|
||||||
:name (sort-by :name projs)
|
|
||||||
:created (reverse (sort-by :created-at projs))
|
|
||||||
projs))
|
|
||||||
|
|
||||||
(defn contains-term?
|
|
||||||
[phrase term]
|
|
||||||
(str/contains? (str/lower phrase) (str/trim (str/lower term))))
|
|
||||||
|
|
||||||
(defn filter-projects-by
|
|
||||||
[term projs]
|
|
||||||
(if (str/blank? term)
|
|
||||||
projs
|
|
||||||
(filter #(contains-term? (:name %) term) projs)))
|
|
||||||
|
|
||||||
(defn set-project-ordering
|
(defn set-project-ordering
|
||||||
[order]
|
[order]
|
||||||
|
@ -202,3 +188,36 @@
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(assoc-in state [:dashboard :project-filter] ""))))
|
(assoc-in state [:dashboard :project-filter] ""))))
|
||||||
|
|
||||||
|
;; --- Helpers
|
||||||
|
|
||||||
|
(defn sort-projects-by
|
||||||
|
[ordering projs]
|
||||||
|
(case ordering
|
||||||
|
:name (sort-by :name projs)
|
||||||
|
:created (reverse (sort-by :created-at projs))
|
||||||
|
projs))
|
||||||
|
|
||||||
|
(defn contains-term?
|
||||||
|
[phrase term]
|
||||||
|
(str/contains? (str/lower phrase) (str/trim (str/lower term))))
|
||||||
|
|
||||||
|
(defn filter-projects-by
|
||||||
|
[term projs]
|
||||||
|
(if (str/blank? term)
|
||||||
|
projs
|
||||||
|
(filter #(contains-term? (:name %) term) projs)))
|
||||||
|
|
||||||
|
(defn assoc-project
|
||||||
|
"A reduce function for assoc the project
|
||||||
|
to the state map."
|
||||||
|
[state proj]
|
||||||
|
(let [id (:id proj)]
|
||||||
|
(update-in state [:projects-by-id id] merge proj)))
|
||||||
|
|
||||||
|
(defn dissoc-project
|
||||||
|
"A reduce function for dissoc the project
|
||||||
|
from the state map."
|
||||||
|
[state id]
|
||||||
|
(update-in state [:projects-by-id] dissoc id))
|
||||||
|
|
||||||
|
|
|
@ -1,76 +0,0 @@
|
||||||
(ns uxbox.main.state.project
|
|
||||||
"A collection of functions for manage shapes insinde the state.")
|
|
||||||
|
|
||||||
(defn assoc-project
|
|
||||||
"A reduce function for assoc the project
|
|
||||||
to the state map."
|
|
||||||
[state proj]
|
|
||||||
(let [id (:id proj)]
|
|
||||||
(update-in state [:projects-by-id id] merge proj)))
|
|
||||||
|
|
||||||
(defn dissoc-project
|
|
||||||
"A reduce function for dissoc the project
|
|
||||||
from the state map."
|
|
||||||
[state id]
|
|
||||||
(update-in state [:projects-by-id] dissoc id))
|
|
||||||
|
|
||||||
(defn dissoc-page-shapes
|
|
||||||
[state id]
|
|
||||||
(let [shapes (get-in state [:shapes-by-id])]
|
|
||||||
(assoc state :shapes-by-id (reduce-kv (fn [acc k v]
|
|
||||||
(if (= (:page v) id)
|
|
||||||
(dissoc acc k)
|
|
||||||
acc))
|
|
||||||
shapes
|
|
||||||
shapes))))
|
|
||||||
(defn assoc-page
|
|
||||||
[state {:keys [id] :as page}]
|
|
||||||
(assoc-in state [:pagedata-by-id id] page))
|
|
||||||
|
|
||||||
(defn dissoc-page
|
|
||||||
[state id]
|
|
||||||
(update state :pagedata-by-id dissoc id))
|
|
||||||
|
|
||||||
(defn pack-page
|
|
||||||
"Return a packed version of page object ready
|
|
||||||
for send to remore storage service."
|
|
||||||
[state id]
|
|
||||||
(let [page (get-in state [:pages-by-id id])
|
|
||||||
xf (filter #(= (:page (second %)) id))
|
|
||||||
shapes (into {} xf (:shapes-by-id state))]
|
|
||||||
(-> page
|
|
||||||
(assoc-in [:data :shapes] (into [] (:shapes page)))
|
|
||||||
(assoc-in [:data :shapes-by-id] shapes)
|
|
||||||
(update-in [:data] dissoc :items)
|
|
||||||
(dissoc :shapes))))
|
|
||||||
|
|
||||||
(defn unpack-page
|
|
||||||
"Unpacks packed page object and assocs it to the
|
|
||||||
provided state."
|
|
||||||
[state page]
|
|
||||||
(let [data (:data page)
|
|
||||||
shapes (:shapes data)
|
|
||||||
shapes-by-id (:shapes-by-id data)
|
|
||||||
page (-> page
|
|
||||||
(dissoc page :data)
|
|
||||||
(assoc :shapes shapes))]
|
|
||||||
(-> state
|
|
||||||
(update :shapes-by-id merge shapes-by-id)
|
|
||||||
(update :pages-by-id assoc (:id page) page))))
|
|
||||||
|
|
||||||
(defn purge-page
|
|
||||||
"Remove page and all related stuff from the state."
|
|
||||||
[state id]
|
|
||||||
(-> state
|
|
||||||
(update :pages-by-id dissoc id)
|
|
||||||
(update :pagedata-by-id dissoc id)
|
|
||||||
(dissoc-page-shapes id)))
|
|
||||||
|
|
||||||
(defn project-pages
|
|
||||||
"Get a ordered list of pages that
|
|
||||||
belongs to a specified project."
|
|
||||||
[state projectid]
|
|
||||||
(->> (vals (:pages-by-id state))
|
|
||||||
(filter #(= projectid (:project %)))
|
|
||||||
(sort-by :created-at)))
|
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
[lentes.core :as l]
|
[lentes.core :as l]
|
||||||
[uxbox.util.rstore :as rs]
|
[uxbox.util.rstore :as rs]
|
||||||
[uxbox.main.state :as st]
|
[uxbox.main.state :as st]
|
||||||
[uxbox.main.state.project :as stpr]
|
|
||||||
[uxbox.main.data.workspace :as dw]
|
[uxbox.main.data.workspace :as dw]
|
||||||
[uxbox.main.data.shapes :as uds]
|
[uxbox.main.data.shapes :as uds]
|
||||||
[uxbox.main.geom.point :as gpt]
|
[uxbox.main.geom.point :as gpt]
|
||||||
|
|
|
@ -14,7 +14,6 @@
|
||||||
[uxbox.util.router :as r]
|
[uxbox.util.router :as r]
|
||||||
[uxbox.util.rstore :as rs]
|
[uxbox.util.rstore :as rs]
|
||||||
[uxbox.main.state :as st]
|
[uxbox.main.state :as st]
|
||||||
[uxbox.main.state.project :as stpr]
|
|
||||||
[uxbox.main.library :as library]
|
[uxbox.main.library :as library]
|
||||||
[uxbox.main.data.projects :as dp]
|
[uxbox.main.data.projects :as dp]
|
||||||
[uxbox.main.data.pages :as udp]
|
[uxbox.main.data.pages :as udp]
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
[uxbox.util.router :as rt]
|
[uxbox.util.router :as rt]
|
||||||
[uxbox.util.schema :as sc]
|
[uxbox.util.schema :as sc]
|
||||||
[uxbox.util.data :refer (parse-int)]
|
[uxbox.util.data :refer (parse-int)]
|
||||||
[uxbox.main.state.project :as stpr]
|
|
||||||
[uxbox.main.repo :as rp]
|
[uxbox.main.repo :as rp]
|
||||||
[uxbox.main.data.pages :as udpg]
|
[uxbox.main.data.pages :as udpg]
|
||||||
[uxbox.main.data.projects :as udpj]))
|
[uxbox.main.data.projects :as udpj]))
|
||||||
|
@ -29,30 +28,31 @@
|
||||||
[token]
|
[token]
|
||||||
(Initialize. token))
|
(Initialize. token))
|
||||||
|
|
||||||
;; (defrecord Initialize [id token]
|
|
||||||
;; rs/UpdateEvent
|
|
||||||
;; (-apply-update [_ state]
|
|
||||||
;; (assoc state
|
|
||||||
;; :token token
|
|
||||||
;; :index id)))
|
|
||||||
|
|
||||||
;; (defn initialize
|
|
||||||
;; "Initialize the viewer state."
|
|
||||||
;; [id token]
|
|
||||||
;; (let [id (parse-int id 0)]
|
|
||||||
;; (Initialize. id token)))
|
|
||||||
|
|
||||||
;; --- Data Loaded
|
;; --- Data Loaded
|
||||||
|
|
||||||
|
(defn- unpack-page
|
||||||
|
"Unpacks packed page object and assocs it to the
|
||||||
|
provided state."
|
||||||
|
[state page]
|
||||||
|
(let [data (:data page)
|
||||||
|
shapes (:shapes data)
|
||||||
|
shapes-by-id (:shapes-by-id data)
|
||||||
|
page (-> page
|
||||||
|
(dissoc page :data)
|
||||||
|
(assoc :shapes shapes))]
|
||||||
|
(-> state
|
||||||
|
(update :shapes-by-id merge shapes-by-id)
|
||||||
|
(update :pages conj page))))
|
||||||
|
|
||||||
(defrecord DataLoaded [data]
|
(defrecord DataLoaded [data]
|
||||||
rs/UpdateEvent
|
rs/UpdateEvent
|
||||||
(-apply-update [_ state]
|
(-apply-update [_ state]
|
||||||
(let [project (dissoc data :pages)
|
(let [project (dissoc data :pages)
|
||||||
pages (vec (:pages data))]
|
pages (sort-by :created-at (:pages data))]
|
||||||
(as-> state $
|
(as-> state $
|
||||||
(assoc $ :project project)
|
(assoc $ :project project)
|
||||||
(assoc $ :pages pages)
|
(assoc $ :pages [])
|
||||||
(reduce stpr/unpack-page $ pages)))))
|
(reduce unpack-page $ pages)))))
|
||||||
|
|
||||||
(defn data-loaded
|
(defn data-loaded
|
||||||
[data]
|
[data]
|
||||||
|
@ -97,7 +97,6 @@
|
||||||
"Toggle the enabled flag of the specified tool."
|
"Toggle the enabled flag of the specified tool."
|
||||||
[key]
|
[key]
|
||||||
{:pre [(keyword? key)]}
|
{:pre [(keyword? key)]}
|
||||||
(println "toggle-flag" key)
|
|
||||||
(ToggleFlag. key))
|
(ToggleFlag. key))
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue