mirror of
https://github.com/penpot/penpot.git
synced 2025-04-04 19:11:20 -05:00
Split some conditions as separated predicate functions.
This commit is contained in:
parent
70282daaf7
commit
ef0e0ab74c
2 changed files with 46 additions and 15 deletions
|
@ -13,7 +13,6 @@
|
|||
[uxbox.state :as st]
|
||||
[uxbox.state.shapes :as stsh]
|
||||
[uxbox.schema :as sc]
|
||||
[uxbox.xforms :as xf]
|
||||
[uxbox.data.pages :as udp]
|
||||
[uxbox.util.geom.point :as gpt]
|
||||
[uxbox.util.data :refer (index-of)]))
|
||||
|
@ -294,23 +293,43 @@
|
|||
(update-in state [:workspace :selected] disj id)
|
||||
(update-in state [:workspace :selected] conj id))))))
|
||||
|
||||
;; --- Select Shapes
|
||||
|
||||
(defn- not-blocked-group?
|
||||
"Check if the shape is a blocked group."
|
||||
[shape]
|
||||
(and (not (:blocked shape))
|
||||
(= :builtin/group (:type shape))))
|
||||
|
||||
(defn- has-blocked-parent?
|
||||
"Check if shape has blocked parent."
|
||||
[shape]
|
||||
(sh/parent-satisfies? shape :blocked))
|
||||
|
||||
(defn- has-locked-parent?
|
||||
[shape]
|
||||
(sh/parent-satisfies? shape :locked))
|
||||
|
||||
(defrecord SelectShapes [selrect]
|
||||
rs/UpdateEvent
|
||||
(-apply-update [_ state]
|
||||
(let [pageid (get-in state [:workspace :page])
|
||||
xform (comp (filter #(= (:page %) pageid))
|
||||
(remove :hidden)
|
||||
(remove :blocked)
|
||||
(remove not-blocked-group?)
|
||||
(remove has-locked-parent?)
|
||||
(remove has-blocked-parent?)
|
||||
(map sh/outer-rect')
|
||||
(filter #(sh/contained-in? % selrect))
|
||||
(map :id))]
|
||||
(->> (into #{} xform (vals (:shapes-by-id state)))
|
||||
(assoc-in state [:workspace :selected])))))
|
||||
|
||||
(defn select-shapes
|
||||
"Select shapes that matches the select rect."
|
||||
[selrect]
|
||||
(reify
|
||||
rs/UpdateEvent
|
||||
(-apply-update [_ state]
|
||||
(let [pageid (get-in state [:workspace :page])
|
||||
xf (comp
|
||||
(filter #(= (:page %) pageid))
|
||||
(remove :hidden)
|
||||
(remove #(and (not (:blocked %)) (= (:type %) :builtin/group)))
|
||||
(remove #(and (not= % (sh/resolve-parent %)) (:blocked (sh/resolve-parent %))))
|
||||
(map sh/outer-rect')
|
||||
(filter #(sh/contained-in? % selrect))
|
||||
(map :id))]
|
||||
(->> (into #{} xf (vals (:shapes-by-id state)))
|
||||
(assoc-in state [:workspace :selected]))))))
|
||||
(SelectShapes. selrect))
|
||||
|
||||
;; --- Events (implicit) (for selected)
|
||||
|
||||
|
|
|
@ -498,6 +498,18 @@
|
|||
(resolve-parent (get-in @st/state [:shapes-by-id group]))
|
||||
shape))
|
||||
|
||||
(defn parent-satisfies?
|
||||
"Resolve the first parent that satisfies a condition."
|
||||
[{:keys [group] :as shape} pred]
|
||||
(let [shapes-by-id (:shapes-by-id @st/state)]
|
||||
(if group
|
||||
(loop [parent (get shapes-by-id group)]
|
||||
(cond
|
||||
(pred parent) true
|
||||
(:group parent) (recur (get shapes-by-id (:group parent)))
|
||||
:else false))
|
||||
false)))
|
||||
|
||||
(defn contained-in?
|
||||
"Check if a shape is contained in the
|
||||
provided selection rect."
|
||||
|
|
Loading…
Add table
Reference in a new issue