mirror of
https://github.com/penpot/penpot.git
synced 2025-02-08 08:09:14 -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
|
(ns app.main.ui.workspace.tokens.token
|
||||||
(:require
|
(:require
|
||||||
[cuerdas.core :as str]))
|
[cuerdas.core :as str]
|
||||||
|
[clojure.set :as set]))
|
||||||
|
|
||||||
(defn attributes-map
|
(defn attributes-map
|
||||||
"Creats an attributes map using collection of `attributes` for `id`."
|
"Creats an attributes map using collection of `attributes` for `id`."
|
||||||
|
@ -45,8 +46,20 @@
|
||||||
[token shapes token-attributes]
|
[token shapes token-attributes]
|
||||||
(some #(token-applied? token % token-attributes) shapes))
|
(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
|
(defn group-shapes-by-all-applied
|
||||||
""
|
|
||||||
[token shapes token-attributes]
|
[token shapes token-attributes]
|
||||||
(reduce
|
(reduce
|
||||||
(fn [acc cur-shape]
|
(fn [acc cur-shape]
|
||||||
|
|
|
@ -29,34 +29,35 @@
|
||||||
(t/is (= #{:x} (wtt/token-applied-attributes {:id :a}
|
(t/is (= #{:x} (wtt/token-applied-attributes {:id :a}
|
||||||
{:applied-tokens {:x :a :y :b}}
|
{:applied-tokens {:x :a :y :b}}
|
||||||
#{:x :missing}))))
|
#{:x :missing}))))
|
||||||
(t/deftest token-context-menu
|
(t/deftest shapes-ids-by-applied-attributes
|
||||||
(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/testing "Returns set of matched attributes that fit the applied token"
|
(t/testing "Returns set of matched attributes that fit the applied token"
|
||||||
(let [attributes #{:x :y :z}
|
(let [attributes #{:x :y :z}
|
||||||
shape-applied-x {:applied-tokens {:x 1}}
|
shape-applied-x {:id :shape-applied-x
|
||||||
shape-applied-y {:applied-tokens {:y 1}}
|
:applied-tokens {:x 1}}
|
||||||
shape-applied-x-y {:applied-tokens {:x 1 :y 1}}
|
shape-applied-y {:id :shape-applied-y
|
||||||
shape-applied-none {:applied-tokens {}}
|
:applied-tokens {:y 1}}
|
||||||
shape-applied-all {:applied-tokens {:x 1 :y 1 :z 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
|
shapes [shape-applied-x
|
||||||
shape-applied-y
|
shape-applied-y
|
||||||
shape-applied-x-y
|
shape-applied-x-y
|
||||||
shape-applied-all
|
shape-applied-all
|
||||||
shape-applied-none]
|
shape-applied-none]
|
||||||
expected (wtt/group-shapes-by-all-applied {:id 1} shapes attributes)]
|
expected (wtt/shapes-ids-by-applied-attributes {:id 1} shapes attributes)]
|
||||||
(t/is (= (:all expected) [shape-applied-all]))
|
(t/is (= (:x expected) (ids-set shape-applied-x
|
||||||
(t/is (= (:none expected) [shape-applied-none]))
|
shape-applied-x-y
|
||||||
(t/is (= (get-in expected [:some :x]) [shape-applied-x shape-applied-x-y]))
|
shape-applied-all)))
|
||||||
(t/is (= (get-in expected [:some :y]) [shape-applied-y shape-applied-x-y]))
|
(t/is (= (:y expected) (ids-set shape-applied-y
|
||||||
(t/is (nil? (get-in expected [:some :z]))))))
|
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/deftest tokens-applied-test
|
||||||
(t/testing "is true when single shape matches the token and attributes"
|
(t/testing "is true when single shape matches the token and attributes"
|
||||||
|
|
Loading…
Add table
Reference in a new issue