diff --git a/frontend/src/app/main/ui/workspace/tokens/core.cljs b/frontend/src/app/main/ui/workspace/tokens/core.cljs index 695e364cb..8587bab36 100644 --- a/frontend/src/app/main/ui/workspace/tokens/core.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/core.cljs @@ -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}] diff --git a/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs b/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs index f0d36dcef..892ec724d 100644 --- a/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/style_dictionary.cljs @@ -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 diff --git a/frontend/test/token_tests/token_core_test.cljs b/frontend/test/token_tests/token_core_test.cljs index 96e4321fd..8241640d7 100644 --- a/frontend/test/token_tests/token_core_test.cljs +++ b/frontend/test/token_tests/token_core_test.cljs @@ -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}"}}))))