mirror of
https://github.com/penpot/penpot.git
synced 2025-01-21 06:02:32 -05:00
Move tokens-name-tree to core, add test
This commit is contained in:
parent
ef5f019200
commit
504369ec13
3 changed files with 31 additions and 18 deletions
|
@ -61,6 +61,23 @@
|
|||
(->> (map (fn [{:keys [name] :as token}] [name token]) tokens)
|
||||
(into {})))
|
||||
|
||||
(defn name->path
|
||||
"Splits token-name into a path vector split by `.` characters.
|
||||
|
||||
Will concatenate multiple `.` characters into one."
|
||||
[token-name]
|
||||
(str/split token-name #"\.+"))
|
||||
|
||||
(defn tokens-name-tree
|
||||
"Convert tokens into a nested tree with their `:name` as the path."
|
||||
[tokens]
|
||||
(reduce
|
||||
(fn [acc [_ {:keys [name] :as token}]]
|
||||
(when (string? name)
|
||||
(let [path (name->path name)]
|
||||
(assoc-in acc path token))))
|
||||
{} tokens))
|
||||
|
||||
(defn tokens-name-map-for-type
|
||||
"Convert tokens with `token-type` into a map with their `:name` as the key.
|
||||
|
||||
|
@ -76,12 +93,6 @@
|
|||
(cond-> (assoc item :label name)
|
||||
(token-applied? item shape (or selected-attributes attributes)) (assoc :selected? true))))))
|
||||
|
||||
(defn name->path
|
||||
"Splits token-name into a path vector split by `.` characters.
|
||||
Will concatenate multiple `.` characters into one."
|
||||
[token-name]
|
||||
(str/split token-name #"\.+"))
|
||||
|
||||
;; Update functions ------------------------------------------------------------
|
||||
|
||||
(defn on-apply-token [{:keys [token token-type-props selected-shapes] :as _props}]
|
||||
|
|
|
@ -82,20 +82,9 @@
|
|||
(and (set? errors)
|
||||
(get errors :style-dictionary/missing-reference)))
|
||||
|
||||
(defn tokens-name-tree [tokens]
|
||||
(reduce
|
||||
(fn [acc [_ {:keys [name] :as token}]]
|
||||
(if (string? name)
|
||||
(let [name-path (->> (str/split name #"\.")
|
||||
(remove str/blank?))]
|
||||
(assoc-in acc name-path token))))
|
||||
{} tokens))
|
||||
|
||||
(tokens-name-tree @refs/workspace-tokens)
|
||||
|
||||
(defn resolve-tokens+
|
||||
[tokens & {:keys [debug?] :as config}]
|
||||
(p/let [sd-tokens (-> (tokens-name-tree tokens)
|
||||
(p/let [sd-tokens (-> (wtc/tokens-name-tree tokens)
|
||||
(doto js/console.log)
|
||||
(resolve-sd-tokens+ config))]
|
||||
(let [resolved-tokens (reduce
|
||||
|
|
|
@ -13,3 +13,16 @@
|
|||
(t/is (= ["foo" "bar" "baz"] (wtc/name->path "foo.bar.baz")))
|
||||
(t/is (= ["foo" "bar" "baz"] (wtc/name->path "foo..bar.baz")))
|
||||
(t/is (= ["foo" "bar" "baz"] (wtc/name->path "foo..bar.baz...."))))
|
||||
|
||||
(t/deftest tokens-name-tree
|
||||
(t/is (= {"foo"
|
||||
{"bar"
|
||||
{"baz" {:name "foo.bar.baz", :value "a"},
|
||||
"bam" {:name "foo.bar.bam", :value "b"}}},
|
||||
"baz" {"bar" {"foo" {:name "baz.bar.foo", :value "{foo.bar.baz}"}}}}
|
||||
(wtc/tokens-name-tree {:a {:name "foo.bar.baz"
|
||||
:value "a"}
|
||||
:b {:name "foo.bar.bam"
|
||||
:value "b"}
|
||||
:c {:name "baz.bar.foo"
|
||||
:value "{foo.bar.baz}"}}))))
|
||||
|
|
Loading…
Add table
Reference in a new issue