mirror of
https://github.com/penpot/penpot.git
synced 2025-02-14 11:09:04 -05:00
Add better shapes selection algorithm.
This commit is contained in:
parent
59e327ae0e
commit
00679d9c68
2 changed files with 30 additions and 47 deletions
|
@ -311,36 +311,12 @@
|
|||
|
||||
;; --- Select Shapes
|
||||
|
||||
(defn- not-blocked-group?
|
||||
"Check if the shape is a blocked group."
|
||||
[shape]
|
||||
(and (not (:blocked shape))
|
||||
(= :group (:type shape))))
|
||||
|
||||
(defn- has-blocked-parent?
|
||||
"Check if shape has blocked parent."
|
||||
[shape]
|
||||
(geom/parent-satisfies? shape :blocked))
|
||||
|
||||
(defn- has-locked-parent?
|
||||
[shape]
|
||||
(geom/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 geom/outer-rect)
|
||||
(filter #(geom/contained-in? % selrect))
|
||||
(map :id))]
|
||||
(->> (into #{} xform (vals (:shapes-by-id state)))
|
||||
(assoc-in state [:workspace :selected])))))
|
||||
(let [page (get-in state [:workspace :page])
|
||||
shapes (stsh/match-by-selrect state page selrect)]
|
||||
(assoc-in state [:workspace :selected] shapes))))
|
||||
|
||||
(defn select-shapes
|
||||
"Select shapes that matches the select rect."
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
(ns uxbox.state.shapes
|
||||
"A collection of functions for manage shapes insinde the state."
|
||||
(:require [uxbox.util.data :refer (index-of)]))
|
||||
(:require [uxbox.util.data :refer (index-of)]
|
||||
[uxbox.util.geom :as geom]))
|
||||
|
||||
;; --- Shape Creation
|
||||
|
||||
|
@ -235,24 +236,30 @@
|
|||
:bottom (drop-relative state :last shape)
|
||||
(throw (ex-info "Invalid data" {}))))
|
||||
|
||||
;; --- Shape Packing
|
||||
;; --- Shape Selection
|
||||
|
||||
;; (defn- deep-scan-shape-ids
|
||||
;; [state acc id]
|
||||
;; (let [shape (get-in state [:shapes-by-id id])]
|
||||
;; (if (= (:type shape) :group)
|
||||
;; (reduce (partial deep-scan-shape-ids state)
|
||||
;; (conj acc id)
|
||||
;; (:items shape))
|
||||
;; (conj acc id))))
|
||||
(defn- try-match-shape
|
||||
[xf selrect acc {:keys [type id items] :as shape}]
|
||||
(cond
|
||||
(geom/contained-in? shape selrect)
|
||||
(conj acc id)
|
||||
|
||||
;; (defn pack-shape
|
||||
;; [state id]
|
||||
;; (let [ids (deep-scan-shape-ids state #{} id)
|
||||
;; index (reduce (fn [acc id]
|
||||
;; (let [shape (get-in state [:shapes-by-id id])]
|
||||
;; (assoc acc id shape)))
|
||||
;; {} ids)]
|
||||
;; {:type :packed-shape
|
||||
;; :index index
|
||||
;; :id id}))
|
||||
(:locked shape)
|
||||
acc
|
||||
|
||||
(= type :group)
|
||||
(reduce (partial try-match-shape xf selrect)
|
||||
acc (sequence xf items))
|
||||
|
||||
:else
|
||||
acc))
|
||||
|
||||
(defn match-by-selrect
|
||||
[state page selrect]
|
||||
(let [xf (comp (map #(get-in state [:shapes-by-id %]))
|
||||
(remove :hidden)
|
||||
(remove :blocked)
|
||||
(map geom/outer-rect))
|
||||
match (partial try-match-shape xf selrect)
|
||||
shapes (get-in state [:pages-by-id page :shapes])]
|
||||
(reduce match #{} (sequence xf shapes))))
|
||||
|
|
Loading…
Add table
Reference in a new issue