0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-23 05:01:23 -05:00

🐛 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]
[: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

View file

@ -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"]

View file

@ -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"}}}

View file

@ -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)