🐛 Fix token file import of Figma generated file (#5591)

* 🐛 Fix token sets selection representation in exported theme

* 🐛 Fix the loss of token set order

* 🐛 Change data shape according to internal representaion

* 🐛 Persist sets order on import according to metadata

* 🐛 Add fallback for nil values

* 🐛 Fix test assertions accoding to the exported json format

* 🐛 Make `:is-source` optional

* ♻️ Fix test description

* ♻️ Remove outdated comment
This commit is contained in:
Andrei Fëdorov 2025-01-16 12:38:03 +01:00 committed by GitHub
parent cf82e42125
commit 5793c526c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 51 additions and 27 deletions

View file

@ -568,7 +568,7 @@ When `before-set-name` is nil, move set to bottom")
[:name :string] [:name :string]
[:group :string] [:group :string]
[:description [:maybe :string]] [:description [:maybe :string]]
[:is-source :boolean] [:is-source [:maybe :boolean]]
[:modified-at ::sm/inst] [:modified-at ::sm/inst]
[:sets [:set {:gen/max 5} :string]]] [:sets [:set {:gen/max 5} :string]]]
[:fn (partial instance? TokenTheme)]]) [:fn (partial instance? TokenTheme)]])
@ -993,37 +993,58 @@ Will return a value that matches this schema:
(filter #(and (instance? TokenTheme %) (filter #(and (instance? TokenTheme %)
(not (hidden-temporary-theme? %)))) (not (hidden-temporary-theme? %))))
(map (fn [token-theme] (map (fn [token-theme]
(->> token-theme (let [theme-map (->> token-theme
(into {}) (into {})
walk/stringify-keys)))) 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)) (tree-seq d/ordered-map? vals themes))
sets (into {} (comp name-set-tuples (->> sets
(filter (partial instance? TokenSet)) (tree-seq d/ordered-map? vals)
(map (fn [token-set] (filter (partial instance? TokenSet))
[(:name token-set) (get-dtcg-tokens-tree token-set)]))) (map (fn [token-set]
(tree-seq d/ordered-map? vals sets))] [(:name token-set) (get-dtcg-tokens-tree token-set)])))
(assoc sets "$themes" themes))) 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] (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") 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 (make-tokens-lib)
lib' (reduce lib' (reduce
(fn [lib [set-name tokens]] (fn [lib [set-name tokens]]
(add-set lib (make-token-set (add-set lib (make-token-set
:name set-name :name set-name
:tokens (flatten-nested-tokens-json tokens "")))) :tokens (flatten-nested-tokens-json tokens ""))))
lib sets-data)] lib sets-data')]
(reduce (if-let [themes-data (seq themes-data)]
(fn [lib {:strs [name group description is-source modified-at sets]}] (reduce
(add-theme lib (TokenTheme. name (fn [lib {:strs [name group description is-source modified-at sets]}]
group (add-theme lib (TokenTheme. name
description (or group "")
is-source description
(dt/parse-instant modified-at) (some? is-source)
(set sets)))) (or (some-> modified-at
lib' themes-data))) (dt/parse-instant))
(dt/now))
(set sets))))
lib' themes-data)
lib')))
(get-all-tokens [this] (get-all-tokens [this]
(reduce (reduce

View file

@ -802,7 +802,7 @@
"description": null, "description": null,
"is-source": false, "is-source": false,
"modified-at": "2024-01-01T00:00:00.000+00:00", "modified-at": "2024-01-01T00:00:00.000+00:00",
"sets": [ "light" ] "selectedTokenSets": {"light": "enabled"}
} ], } ],
"$metadata": { "$metadata": {
"tokenSetOrder": ["core", "light", "dark", "theme"] "tokenSetOrder": ["core", "light", "dark", "theme"]

View file

@ -1163,7 +1163,8 @@
"is-source" false "is-source" false
"modified-at" now "modified-at" now
"name" "theme-1" "name" "theme-1"
"sets" #{"core"}}] "selectedTokenSets" {"core" "enabled"}}]
"$metadata" {"tokenSetOrder" ["core"]}
"core" "core"
{"colors" {"red" {"600" {"$value" "#e53e3e" {"colors" {"red" {"600" {"$value" "#e53e3e"
"$type" "color"}}} "$type" "color"}}}

View file

@ -32,9 +32,10 @@
(t/deftest process-json-stream-test (t/deftest process-json-stream-test
(t/async (t/async
done done
(t/testing "processes empty json string" (t/testing "process simple color token value"
(let [json (-> {"core" {"color" {"$value" "red" (let [json (-> {"core" {"color" {"$value" "red"
"$type" "color"}}} "$type" "color"}}
"$metadata" {"tokenSetOrder" ["core"]}}
(tr/encode-str {:type :json-verbose}))] (tr/encode-str {:type :json-verbose}))]
(->> (rx/of json) (->> (rx/of json)
(sd/process-json-stream) (sd/process-json-stream)
@ -103,7 +104,8 @@ color.value tries to reference missing, which is not defined.")))
done done
(t/testing "fails on missing references in tokens" (t/testing "fails on missing references in tokens"
(let [json (-> {"core" {"color" {"$value" "{missing}" (let [json (-> {"core" {"color" {"$value" "{missing}"
"$type" "color"}}} "$type" "color"}}
"$metadata" {"tokenSetOrder" ["core"]}}
(tr/encode-str {:type :json-verbose}))] (tr/encode-str {:type :json-verbose}))]
(->> (rx/of json) (->> (rx/of json)
(sd/process-json-stream) (sd/process-json-stream)