mirror of
https://github.com/penpot/penpot.git
synced 2025-01-21 06:02:32 -05:00
Improved logic to run once for all shapes
This commit is contained in:
parent
90618ec89a
commit
da0389e304
2 changed files with 37 additions and 23 deletions
|
@ -1,6 +1,7 @@
|
|||
(ns app.main.ui.workspace.tokens.token
|
||||
(:require
|
||||
[cuerdas.core :as str]))
|
||||
[cuerdas.core :as str]
|
||||
[clojure.set :as set]))
|
||||
|
||||
(defn attributes-map
|
||||
"Creats an attributes map using collection of `attributes` for `id`."
|
||||
|
@ -45,8 +46,20 @@
|
|||
[token shapes token-attributes]
|
||||
(some #(token-applied? token % token-attributes) shapes))
|
||||
|
||||
(defn shapes-ids-by-applied-attributes [token shapes token-attributes]
|
||||
(reduce (fn [acc shape]
|
||||
(let [applied-ids-by-attribute (->> (map #(when (token-attribute-applied? token shape %)
|
||||
[% #{(:id shape)}])
|
||||
token-attributes)
|
||||
(filter some?)
|
||||
(into {}))]
|
||||
(merge-with into acc applied-ids-by-attribute)))
|
||||
{} shapes))
|
||||
|
||||
(defn shapes-applied-all? [ids-by-attributes shape-ids attributes]
|
||||
(every? #(set/superset? (get ids-by-attributes %) shape-ids) attributes))
|
||||
|
||||
(defn group-shapes-by-all-applied
|
||||
""
|
||||
[token shapes token-attributes]
|
||||
(reduce
|
||||
(fn [acc cur-shape]
|
||||
|
|
|
@ -29,34 +29,35 @@
|
|||
(t/is (= #{:x} (wtt/token-applied-attributes {:id :a}
|
||||
{:applied-tokens {:x :a :y :b}}
|
||||
#{:x :missing}))))
|
||||
(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 (true? (wtt/group-shapes-by-all-applied-all? expected)))
|
||||
(t/is (= (:all expected) shapes))
|
||||
(t/is (empty? (:other expected)))
|
||||
(t/is (empty? (:some expected)))))
|
||||
|
||||
(t/deftest shapes-ids-by-applied-attributes
|
||||
(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}}
|
||||
shape-applied-x {:id :shape-applied-x
|
||||
:applied-tokens {:x 1}}
|
||||
shape-applied-y {:id :shape-applied-y
|
||||
:applied-tokens {:y 1}}
|
||||
shape-applied-x-y {:id :shape-applied-x-y
|
||||
:applied-tokens {:x 1 :y 1}}
|
||||
shape-applied-none {:id :shape-applied-none
|
||||
:applied-tokens {}}
|
||||
shape-applied-all {:id :shape-applied-all
|
||||
:applied-tokens {:x 1 :y 1 :z 1}}
|
||||
ids-set (fn [& xs] (into #{} (map :id xs)))
|
||||
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]))))))
|
||||
expected (wtt/shapes-ids-by-applied-attributes {:id 1} shapes attributes)]
|
||||
(t/is (= (:x expected) (ids-set shape-applied-x
|
||||
shape-applied-x-y
|
||||
shape-applied-all)))
|
||||
(t/is (= (:y expected) (ids-set shape-applied-y
|
||||
shape-applied-x-y
|
||||
shape-applied-all)))
|
||||
(t/is (= (:z expected) (ids-set shape-applied-all)))
|
||||
(t/is (true? (wtt/shapes-applied-all? expected (ids-set shape-applied-all) attributes)))
|
||||
(t/is (false? (wtt/shapes-applied-all? expected (apply ids-set shapes) attributes))))))
|
||||
|
||||
(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