mirror of
https://github.com/penpot/penpot.git
synced 2025-01-21 06:02:32 -05:00
Add grouping function by type
This commit is contained in:
parent
3c67872d3c
commit
62ecee2cf8
2 changed files with 47 additions and 18 deletions
|
@ -29,6 +29,12 @@
|
|||
[token shape token-attributes]
|
||||
(some #(token-attribute-applied? token shape %) token-attributes))
|
||||
|
||||
(defn token-applied-attributes
|
||||
"Return a set of which `token-attributes` are applied with `token`."
|
||||
[token shape token-attributes]
|
||||
(-> (filter #(token-attribute-applied? token shape %) token-attributes)
|
||||
(set)))
|
||||
|
||||
(defn shapes-token-applied?
|
||||
"Test if `token` is applied to to any of `shapes` with at least one of the one of the given `token-attributes`."
|
||||
[token shapes token-attributes]
|
||||
|
@ -39,6 +45,20 @@
|
|||
[token shapes token-attributes]
|
||||
(some #(token-applied? token % token-attributes) shapes))
|
||||
|
||||
(defn group-shapes-by-all-applied
|
||||
""
|
||||
[token shapes token-attributes]
|
||||
(reduce
|
||||
(fn [acc cur-shape]
|
||||
(let [applied-attrs (token-applied-attributes token cur-shape token-attributes)]
|
||||
(cond
|
||||
(empty? applied-attrs) (update acc :none (fnil conj []) cur-shape)
|
||||
(= applied-attrs token-attributes) (update acc :all (fnil conj []) cur-shape)
|
||||
:else (reduce (fn [acc' cur']
|
||||
(update-in acc' [:some cur'] (fnil conj []) cur-shape))
|
||||
acc applied-attrs))))
|
||||
{} shapes))
|
||||
|
||||
(defn token-name->path
|
||||
"Splits token-name into a path vector split by `.` characters.
|
||||
|
||||
|
|
|
@ -29,24 +29,33 @@
|
|||
(t/is (= #{:x} (wtt/token-applied-attributes {:id :a}
|
||||
{:applied-tokens {:x :a :y :b}}
|
||||
#{:x :missing}))))
|
||||
(comment
|
||||
(let [test-fn (fn [& args])]
|
||||
(t/deftest token-context-menu
|
||||
(let [shapes [{:applied-tokens {:x :a
|
||||
:y :a}}
|
||||
{:applied-tokens {:x :a
|
||||
:y :a}}]]
|
||||
(t/is (= :all (test-fn {:id :a} shapes #{:x :y}))))
|
||||
(let [shapes [{:applied-tokens {:x :a
|
||||
:y :_}}
|
||||
{:applied-tokens {:x :_
|
||||
:y :a}}]]
|
||||
(t/is (= #{:x :y} (test-fn {:id :a} shapes #{:x :y}))))
|
||||
(let [shapes [{:applied-tokens {:x :a
|
||||
:y :_}}
|
||||
{:applied-tokens {:x :a
|
||||
:y :_}}]]
|
||||
(t/is (= #{:x} (test-fn {:id :a} shapes #{:x})))))))
|
||||
(t/deftest token-context-menu
|
||||
(t/testing "Returns :all when token is applied to every shape"
|
||||
(let [shapes [{:applied-tokens {:x 1 :y 1}}
|
||||
{:applied-tokens {:x 1 :y 1}}]
|
||||
expected (wtt/group-shapes-by-all-applied {:id 1} shapes #{:x :y})]
|
||||
(t/is (= (:all expected) shapes))
|
||||
(t/is (empty? (:other expected)))
|
||||
(t/is (empty? (:some expected)))))
|
||||
|
||||
(t/testing "Returns set of matched attributes that fit the applied token"
|
||||
(let [attributes #{:x :y :z}
|
||||
shape-applied-x {:applied-tokens {:x 1}}
|
||||
shape-applied-y {:applied-tokens {:y 1}}
|
||||
shape-applied-x-y {:applied-tokens {:x 1 :y 1}}
|
||||
shape-applied-none {:applied-tokens {}}
|
||||
shape-applied-all {:applied-tokens {:x 1 :y 1 :z 1}}
|
||||
shapes [shape-applied-x
|
||||
shape-applied-y
|
||||
shape-applied-x-y
|
||||
shape-applied-all
|
||||
shape-applied-none]
|
||||
expected (wtt/group-shapes-by-all-applied {:id 1} shapes attributes)]
|
||||
(t/is (= (:all expected) [shape-applied-all]))
|
||||
(t/is (= (:none expected) [shape-applied-none]))
|
||||
(t/is (= (get-in expected [:some :x]) [shape-applied-x shape-applied-x-y]))
|
||||
(t/is (= (get-in expected [:some :y]) [shape-applied-y shape-applied-x-y]))
|
||||
(t/is (nil? (get-in expected [:some :z]))))))
|
||||
|
||||
(t/deftest tokens-applied-test
|
||||
(t/testing "is true when single shape matches the token and attributes"
|
||||
|
|
Loading…
Add table
Reference in a new issue