diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index 76f653351d..daa72841d4 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -568,7 +568,7 @@ When `before-set-name` is nil, move set to bottom") [:name :string] [:group :string] [:description [:maybe :string]] - [:is-source :boolean] + [:is-source [:maybe :boolean]] [:modified-at ::sm/inst] [:sets [:set {:gen/max 5} :string]]] [:fn (partial instance? TokenTheme)]]) @@ -993,37 +993,58 @@ Will return a value that matches this schema: (filter #(and (instance? TokenTheme %) (not (hidden-temporary-theme? %)))) (map (fn [token-theme] - (->> token-theme - (into {}) - walk/stringify-keys)))) + (let [theme-map (->> token-theme + (into {}) + walk/stringify-keys)] + (-> theme-map + (set/rename-keys {"sets" "selectedTokenSets"}) + (update "selectedTokenSets" (fn [sets] + (->> (for [s sets] + [s "enabled"]) + (into {}))))))))) (tree-seq d/ordered-map? vals themes)) - sets (into {} (comp - (filter (partial instance? TokenSet)) - (map (fn [token-set] - [(:name token-set) (get-dtcg-tokens-tree token-set)]))) - (tree-seq d/ordered-map? vals sets))] - (assoc sets "$themes" themes))) + name-set-tuples (->> sets + (tree-seq d/ordered-map? vals) + (filter (partial instance? TokenSet)) + (map (fn [token-set] + [(:name token-set) (get-dtcg-tokens-tree token-set)]))) + ordered-set-names (map first name-set-tuples) + sets (into {} name-set-tuples)] + (-> sets + (assoc "$themes" themes) + (assoc-in ["$metadata" "tokenSetOrder"] ordered-set-names)))) (decode-dtcg-json [_ parsed-json] - (let [;; tokens-studio/plugin will add these meta properties, remove them for now + (let [metadata (get parsed-json "$metadata") sets-data (dissoc parsed-json "$themes" "$metadata") - themes-data (get parsed-json "$themes") + themes-data (->> (get parsed-json "$themes") + (map (fn [theme] + (-> theme + (set/rename-keys {"selectedTokenSets" "sets"}) + (update "sets" keys))))) + set-order (get metadata "tokenSetOrder") + name->pos (into {} (map-indexed (fn [idx itm] [itm idx]) set-order)) + sets-data' (sort-by (comp name->pos first) sets-data) lib (make-tokens-lib) lib' (reduce (fn [lib [set-name tokens]] (add-set lib (make-token-set :name set-name :tokens (flatten-nested-tokens-json tokens "")))) - lib sets-data)] - (reduce - (fn [lib {:strs [name group description is-source modified-at sets]}] - (add-theme lib (TokenTheme. name - group - description - is-source - (dt/parse-instant modified-at) - (set sets)))) - lib' themes-data))) + lib sets-data')] + (if-let [themes-data (seq themes-data)] + (reduce + (fn [lib {:strs [name group description is-source modified-at sets]}] + (add-theme lib (TokenTheme. name + (or group "") + description + (some? is-source) + (or (some-> modified-at + (dt/parse-instant)) + (dt/now)) + (set sets)))) + lib' themes-data) + lib'))) (get-all-tokens [this] (reduce diff --git a/common/test/common_tests/types/data/tokens-multi-set-example.json b/common/test/common_tests/types/data/tokens-multi-set-example.json index 7b44af9bc0..547f8f9010 100644 --- a/common/test/common_tests/types/data/tokens-multi-set-example.json +++ b/common/test/common_tests/types/data/tokens-multi-set-example.json @@ -802,7 +802,7 @@ "description": null, "is-source": false, "modified-at": "2024-01-01T00:00:00.000+00:00", - "sets": [ "light" ] + "selectedTokenSets": {"light": "enabled"} } ], "$metadata": { "tokenSetOrder": ["core", "light", "dark", "theme"] diff --git a/common/test/common_tests/types/tokens_lib_test.cljc b/common/test/common_tests/types/tokens_lib_test.cljc index 67fd9c105f..f392bb79d4 100644 --- a/common/test/common_tests/types/tokens_lib_test.cljc +++ b/common/test/common_tests/types/tokens_lib_test.cljc @@ -1163,7 +1163,8 @@ "is-source" false "modified-at" now "name" "theme-1" - "sets" #{"core"}}] + "selectedTokenSets" {"core" "enabled"}}] + "$metadata" {"tokenSetOrder" ["core"]} "core" {"colors" {"red" {"600" {"$value" "#e53e3e" "$type" "color"}}} diff --git a/frontend/test/frontend_tests/tokens/style_dictionary_test.cljs b/frontend/test/frontend_tests/tokens/style_dictionary_test.cljs index 9e2f6d1e9a..517f0d1027 100644 --- a/frontend/test/frontend_tests/tokens/style_dictionary_test.cljs +++ b/frontend/test/frontend_tests/tokens/style_dictionary_test.cljs @@ -32,9 +32,10 @@ (t/deftest process-json-stream-test (t/async done - (t/testing "processes empty json string" + (t/testing "process simple color token value" (let [json (-> {"core" {"color" {"$value" "red" - "$type" "color"}}} + "$type" "color"}} + "$metadata" {"tokenSetOrder" ["core"]}} (tr/encode-str {:type :json-verbose}))] (->> (rx/of json) (sd/process-json-stream) @@ -103,7 +104,8 @@ color.value tries to reference missing, which is not defined."))) done (t/testing "fails on missing references in tokens" (let [json (-> {"core" {"color" {"$value" "{missing}" - "$type" "color"}}} + "$type" "color"}} + "$metadata" {"tokenSetOrder" ["core"]}} (tr/encode-str {:type :json-verbose}))] (->> (rx/of json) (sd/process-json-stream)