mirror of
https://github.com/penpot/penpot.git
synced 2025-07-30 01:57:21 +02:00
♻️ Refactor state locality
The main purpose of this refactor is reduce a custom state from different pages and unify them under common access patterns
This commit is contained in:
parent
f62aecb383
commit
85746e7cb2
110 changed files with 2254 additions and 2125 deletions
|
@ -18,6 +18,7 @@
|
|||
[app.main.data.changes :as dwc]
|
||||
[app.main.data.common :as dcm]
|
||||
[app.main.data.dashboard.shortcuts]
|
||||
[app.main.data.helpers :as dsh]
|
||||
[app.main.data.preview :as dp]
|
||||
[app.main.data.viewer.shortcuts]
|
||||
[app.main.data.workspace :as dw]
|
||||
|
@ -149,8 +150,10 @@
|
|||
nil)
|
||||
|
||||
(defn ^:export dump-data []
|
||||
(logjs "workspace-data" (get @st/state :workspace-data))
|
||||
nil)
|
||||
(let [fdata (-> (dsh/lookup-file @st/state)
|
||||
(get :data))]
|
||||
(logjs "file-data" fdata)
|
||||
nil))
|
||||
|
||||
(defn ^:export dump-buffer []
|
||||
(logjs "last-events" @st/last-events)
|
||||
|
@ -165,8 +168,7 @@
|
|||
|
||||
(defn dump-objects'
|
||||
[state]
|
||||
(let [page-id (get state :current-page-id)
|
||||
objects (get-in state [:workspace-data :pages-index page-id :objects])]
|
||||
(let [objects (dsh/lookup-page-objects state)]
|
||||
(logjs "objects" objects)
|
||||
nil))
|
||||
|
||||
|
@ -174,33 +176,25 @@
|
|||
[]
|
||||
(dump-objects' @st/state))
|
||||
|
||||
(defn dump-object'
|
||||
(defn get-object
|
||||
[state name]
|
||||
(let [page-id (get state :current-page-id)
|
||||
objects (get-in state [:workspace-data :pages-index page-id :objects])
|
||||
(let [objects (dsh/lookup-page-objects state)
|
||||
result (or (d/seek (fn [shape] (= name (:name shape))) (vals objects))
|
||||
(get objects (uuid/uuid name)))]
|
||||
#_(logjs name result)
|
||||
result
|
||||
|
||||
#_nil))
|
||||
result))
|
||||
|
||||
(defn ^:export dump-object
|
||||
[name]
|
||||
(dump-object' @st/state name))
|
||||
(get-object @st/state name))
|
||||
|
||||
(defn dump-selected'
|
||||
(defn get-selected
|
||||
[state]
|
||||
(let [page-id (get state :current-page-id)
|
||||
objects (get-in state [:workspace-data :pages-index page-id :objects])
|
||||
selected (get-in state [:workspace-local :selected])
|
||||
result (->> selected (map (d/getf objects)))]
|
||||
(logjs "selected" result)
|
||||
nil))
|
||||
(dsh/lookup-selected state))
|
||||
|
||||
(defn ^:export dump-selected
|
||||
[]
|
||||
(dump-selected' @st/state))
|
||||
(logjs "selected" (get-selected @st/state))
|
||||
nil)
|
||||
|
||||
(defn ^:export preview-selected
|
||||
[]
|
||||
|
@ -208,26 +202,20 @@
|
|||
|
||||
(defn ^:export parent
|
||||
[]
|
||||
(let [state @st/state
|
||||
page-id (get state :current-page-id)
|
||||
objects (get-in state [:workspace-data :pages-index page-id :objects])
|
||||
selected (first (get-in state [:workspace-local :selected]))
|
||||
parent-id (get-in objects [selected :parent-id])
|
||||
parent (get objects parent-id)]
|
||||
(when parent
|
||||
(prn (str (:name parent) " - " (:id parent))))
|
||||
(let [objects (dsh/lookup-page-objects @st/state)
|
||||
selected-id (first (dsh/get-selected-ids @st/state))
|
||||
parent-id (dm/get-in objects [selected-id :parent-id])]
|
||||
(when-let [parent (get objects parent-id)]
|
||||
(js/console.log (str (:name parent) " - " (:id parent))))
|
||||
nil))
|
||||
|
||||
(defn ^:export frame
|
||||
[]
|
||||
(let [state @st/state
|
||||
page-id (get state :current-page-id)
|
||||
objects (get-in state [:workspace-data :pages-index page-id :objects])
|
||||
selected (first (get-in state [:workspace-local :selected]))
|
||||
frame-id (get-in objects [selected :frame-id])
|
||||
frame (get objects frame-id)]
|
||||
(when frame
|
||||
(prn (str (:name frame) " - " (:id frame))))
|
||||
(let [objects (dsh/lookup-page-objects @st/state)
|
||||
selected-id (first (dsh/get-selected-ids @st/state))
|
||||
frame-id (dm/get-in objects [selected-id :frame-id])]
|
||||
(when-let [frame (get objects frame-id)]
|
||||
(js/console.log (str (:name frame) " - " (:id frame))))
|
||||
nil))
|
||||
|
||||
(defn ^:export select-by-object-id
|
||||
|
@ -246,9 +234,8 @@
|
|||
([state show-ids show-touched] (dump-tree' state show-ids show-touched false))
|
||||
([state show-ids show-touched show-modified]
|
||||
(let [page-id (get state :current-page-id)
|
||||
file (assoc (get state :workspace-file)
|
||||
:data (get state :workspace-data))
|
||||
libraries (get state :libraries)]
|
||||
file (dsh/lookup-file state)
|
||||
libraries (get state :files)]
|
||||
(ctf/dump-tree file page-id libraries {:show-ids show-ids
|
||||
:show-touched show-touched
|
||||
:show-modified show-modified}))))
|
||||
|
@ -264,14 +251,11 @@
|
|||
([state shape-id show-ids show-touched] (dump-subtree' state shape-id show-ids show-touched false))
|
||||
([state shape-id show-ids show-touched show-modified]
|
||||
(let [page-id (get state :current-page-id)
|
||||
file (assoc (get state :workspace-file)
|
||||
:data (get state :workspace-data))
|
||||
libraries (get state :libraries)
|
||||
file (dsh/lookup-file state)
|
||||
libraries (get state :files)
|
||||
shape-id (if (some? shape-id)
|
||||
(uuid/uuid shape-id)
|
||||
(let [objects (get-in state [:workspace-data :pages-index page-id :objects])
|
||||
selected (get-in state [:workspace-local :selected])]
|
||||
(->> selected (map (d/getf objects)) first :id)))]
|
||||
(first (dsh/lookup-selected state)))]
|
||||
(if (some? shape-id)
|
||||
(ctf/dump-subtree file page-id shape-id libraries {:show-ids show-ids
|
||||
:show-touched show-touched
|
||||
|
@ -363,10 +347,10 @@
|
|||
|
||||
(defn ^:export dump-modifiers
|
||||
[]
|
||||
(let [page-id (get @st/state :current-page-id)
|
||||
objects (get-in @st/state [:workspace-data :pages-index page-id :objects])]
|
||||
(.log js/console (modif->js (:workspace-modifiers @st/state) objects)))
|
||||
nil)
|
||||
(let [objects (dsh/lookup-page-objects @st/state)
|
||||
modifiers (:workspace-modifiers @st/state)]
|
||||
(js/console.log (modif->js modifiers objects))
|
||||
nil))
|
||||
|
||||
(defn ^:export set-workspace-read-only
|
||||
[read-only?]
|
||||
|
@ -381,10 +365,8 @@
|
|||
(defn ^:export validate
|
||||
([] (validate nil))
|
||||
([shape-id]
|
||||
(let [file (assoc (get @st/state :workspace-file)
|
||||
:data (get @st/state :workspace-data))
|
||||
libraries (get @st/state :libraries)]
|
||||
|
||||
(let [file (dsh/lookup-file @st/state)
|
||||
libraries (get @st/state :files)]
|
||||
(try
|
||||
(->> (if-let [shape-id (some-> shape-id parse-uuid)]
|
||||
(let [page (dm/get-in file [:data :pages-index (get @st/state :current-page-id)])]
|
||||
|
@ -398,9 +380,8 @@
|
|||
(defn ^:export validate-schema
|
||||
[]
|
||||
(try
|
||||
(-> (get @st/state :workspace-file)
|
||||
(assoc :data (get @st/state :workspace-data))
|
||||
(cfv/validate-file-schema!))
|
||||
(let [file (dsh/lookup-file @st/state)]
|
||||
(cfv/validate-file-schema! file))
|
||||
(catch :default cause
|
||||
(errors/print-error! cause))))
|
||||
|
||||
|
@ -413,11 +394,8 @@
|
|||
(let [features (features/get-team-enabled-features state)
|
||||
sid (:session-id state)
|
||||
|
||||
file (get state :workspace-file)
|
||||
fdata (get state :workspace-data)
|
||||
|
||||
file (assoc file :data fdata)
|
||||
libs (get state :libraries)
|
||||
file (dsh/lookup-file state)
|
||||
libs (get state :files)
|
||||
|
||||
errors (cfv/validate-file file libs)
|
||||
_ (l/dbg :hint "repair current file" :errors (count errors))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue