mirror of
https://github.com/penpot/penpot.git
synced 2026-11-18 16:12:24 +00:00
🐛 Fix active themes in token export and the empty file import error (#13123)
* 🐛 Fix token export writing the wrong active themes The export passed only the tokens library, so the active themes and sets in `$metadata` were the ones of the imported file, kept in the library, instead of the ones activated in the file. The multi-file export now needs the file's status, so no caller falls back to them. Signed-off-by: Florian Schroedl <flo.schroedl@gmail.com> * 🐛 Say no tokens were found when importing a file without tokens Importing `{}`, or a JSON file without any token, said the token data was invalid. Signed-off-by: Florian Schroedl <flo.schroedl@gmail.com> --------- Signed-off-by: Florian Schroedl <flo.schroedl@gmail.com>
This commit is contained in:
parent
d403a66122
commit
cdfbff7c53
@ -1678,7 +1678,7 @@
|
||||
(every? empty? (vals sets))
|
||||
(and (empty? sets) (empty? themes)))
|
||||
(throw (ex-info "the file doesn't contain any tokens"
|
||||
{:error/code :error.import/invalid-json-data})))
|
||||
{:error/code :error.import/no-token-files-found})))
|
||||
|
||||
(as-> (make-tokens-lib) library
|
||||
(reduce-kv (fn [library name tokens]
|
||||
@ -1817,8 +1817,6 @@
|
||||
A `:dtcg` file is not meant to import back unchanged: tokens come back
|
||||
with their DTCG type and value, a spacing token as a dimension, `4` as
|
||||
`4px` and `rgb(255, 0, 0)` as `#ff0000`."
|
||||
([tokens-lib]
|
||||
(tsw/write-files (tokens-lib->document tokens-lib (legacy-activation tokens-lib))))
|
||||
([tokens-lib tokens-status]
|
||||
(export-dtcg-multi-file tokens-lib tokens-status nil))
|
||||
([tokens-lib tokens-status opts]
|
||||
|
||||
@ -47,14 +47,27 @@
|
||||
:on-click on-export}
|
||||
(tr "workspace.tokens.export")]]])
|
||||
|
||||
(defn- single-file-json
|
||||
"The tokens of the file as one JSON file. Its active themes and sets are
|
||||
the ones activated in this file, not the ones stored in the library."
|
||||
[tokens-lib tokens-status]
|
||||
(when (and tokens-lib tokens-status)
|
||||
(some-> (ctob/export-dtcg-json tokens-lib tokens-status)
|
||||
(json/encode :key-fn identity :indent 2))))
|
||||
|
||||
(defn- multi-file-entries
|
||||
"The tokens of the file as one JSON file per set, plus the themes and
|
||||
the metadata, with the active themes and sets of `single-file-json`."
|
||||
[tokens-lib tokens-status]
|
||||
(when (and tokens-lib tokens-status)
|
||||
(ctob/export-dtcg-multi-file tokens-lib tokens-status)))
|
||||
|
||||
(mf/defc single-file-tab*
|
||||
{::mf/private true}
|
||||
[]
|
||||
(let [tokens-data (some-> (deref refs/tokens-lib)
|
||||
(ctob/export-dtcg-json))
|
||||
tokens-json (some-> tokens-data
|
||||
(json/encode :key-fn identity :indent 2))
|
||||
is-disabled (empty? tokens-data)
|
||||
(let [tokens-json (single-file-json (deref refs/tokens-lib)
|
||||
(deref refs/tokens-status))
|
||||
is-disabled (nil? tokens-json)
|
||||
on-export
|
||||
(mf/use-fn
|
||||
(mf/deps tokens-json)
|
||||
@ -79,8 +92,8 @@
|
||||
(mf/defc multi-file-tab*
|
||||
{::mf/private true}
|
||||
[]
|
||||
(let [files (some->> (deref refs/tokens-lib)
|
||||
(ctob/export-dtcg-multi-file))
|
||||
(let [files (multi-file-entries (deref refs/tokens-lib)
|
||||
(deref refs/tokens-status))
|
||||
is-disabled (or (empty? files)
|
||||
(every? (fn [[_ v]] (empty? v)) files))
|
||||
on-export
|
||||
|
||||
@ -133,6 +133,7 @@
|
||||
[frontend-tests.text-editor-paste-guard-test]
|
||||
[frontend-tests.tokens.context-menu-test]
|
||||
[frontend-tests.tokens.copy-paste-props-test]
|
||||
[frontend-tests.tokens.export-modal-test]
|
||||
[frontend-tests.tokens.import-export-test]
|
||||
[frontend-tests.tokens.logic.token-actions-test]
|
||||
[frontend-tests.tokens.logic.token-data-test]
|
||||
@ -357,6 +358,7 @@
|
||||
'frontend-tests.svg-filters-test
|
||||
'frontend-tests.tokens.copy-paste-props-test
|
||||
'frontend-tests.tokens.context-menu-test
|
||||
'frontend-tests.tokens.export-modal-test
|
||||
'frontend-tests.tokens.import-export-test
|
||||
'frontend-tests.tokens.logic.token-actions-test
|
||||
'frontend-tests.tokens.logic.token-data-test
|
||||
|
||||
65
frontend/test/frontend_tests/tokens/export_modal_test.cljs
Normal file
65
frontend/test/frontend_tests/tokens/export_modal_test.cljs
Normal file
@ -0,0 +1,65 @@
|
||||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) KALEIDOS SUBSIDIARY SL
|
||||
|
||||
(ns frontend-tests.tokens.export-modal-test
|
||||
"The active themes and sets the export writes into the files."
|
||||
(:require
|
||||
[app.common.files.tokens :as cfo]
|
||||
[app.common.json :as json]
|
||||
[app.common.types.tokens-lib :as ctob]
|
||||
[app.main.ui.workspace.tokens.export.modal :as export-modal]
|
||||
[cljs.test :as t :include-macros true]))
|
||||
|
||||
(def ^:private single-file-json @#'export-modal/single-file-json)
|
||||
(def ^:private multi-file-entries @#'export-modal/multi-file-entries)
|
||||
|
||||
(defn- imported-lib
|
||||
"A library imported from a file with the themes `mode/light` and
|
||||
`mode/dark`, where the file marked `mode/dark` as active."
|
||||
[]
|
||||
(ctob/parse-multi-set-dtcg-json
|
||||
{"mode/light" {"surface" {"$value" "#ffffff" "$type" "color"}}
|
||||
"mode/dark" {"surface" {"$value" "#111111" "$type" "color"}}
|
||||
"$themes" [{"name" "light" "group" "mode" "selectedTokenSets" {"mode/light" "enabled"}}
|
||||
{"name" "dark" "group" "mode" "selectedTokenSets" {"mode/dark" "enabled"}}]
|
||||
"$metadata" {"tokenSetOrder" ["mode/light" "mode/dark"]
|
||||
"activeThemes" ["mode/dark"]
|
||||
"activeSets" ["mode/dark"]}}))
|
||||
|
||||
(defn- status-with-light-active
|
||||
"The file's status after the user picked `mode/light` in the theme
|
||||
selector."
|
||||
[lib]
|
||||
(let [light (ctob/get-theme-by-name lib "mode" "light")]
|
||||
(cfo/activate-theme (cfo/make-tokens-status-from-lib lib) lib (ctob/get-id light))))
|
||||
|
||||
(defn- active-in
|
||||
[metadata]
|
||||
{:themes (set (get metadata "activeThemes"))
|
||||
:sets (set (get metadata "activeSets"))})
|
||||
|
||||
(t/deftest export-writes-the-themes-active-in-the-file
|
||||
(let [lib (imported-lib)
|
||||
status (status-with-light-active lib)]
|
||||
(t/testing "the library still holds the themes active in the imported file"
|
||||
(t/is (contains? (ctob/get-legacy-active-themes lib) "mode/dark")))
|
||||
|
||||
(t/testing "single file"
|
||||
(let [exported (json/decode (single-file-json lib status) :key-fn identity)]
|
||||
(t/is (= {:themes #{"mode/light"} :sets #{"mode/light"}}
|
||||
(active-in (get exported "$metadata"))))))
|
||||
|
||||
(t/testing "multiple files"
|
||||
(t/is (= {:themes #{"mode/light"} :sets #{"mode/light"}}
|
||||
(active-in (get (multi-file-entries lib status) "$metadata.json")))))))
|
||||
|
||||
(t/deftest export-without-tokens
|
||||
(t/testing "an empty library has nothing to export"
|
||||
(let [lib (ctob/make-tokens-lib)]
|
||||
(t/is (nil? (single-file-json lib (cfo/make-tokens-status-from-lib lib))))))
|
||||
(t/testing "without a status there is nothing to export"
|
||||
(t/is (nil? (single-file-json (imported-lib) nil)))
|
||||
(t/is (nil? (multi-file-entries (imported-lib) nil)))))
|
||||
@ -112,7 +112,7 @@ color.value tries to reference missing, which is not defined.")))
|
||||
(fn [_]
|
||||
(throw (js/Error. "Should be an error")))
|
||||
(fn [err]
|
||||
(t/is (= :error.import/invalid-json-data (:error/code (ex-data err))))
|
||||
(t/is (= :error.import/no-token-files-found (:error/code (ex-data err))))
|
||||
(done)))))))
|
||||
|
||||
(t/deftest import-invalid-json-stream-test
|
||||
@ -138,7 +138,7 @@ color.value tries to reference missing, which is not defined.")))
|
||||
(fn []
|
||||
(throw (js/Error. "Should be an error")))
|
||||
(fn [err]
|
||||
(t/is (= :error.import/invalid-json-data (:error/code (ex-data err))))
|
||||
(t/is (= :error.import/no-token-files-found (:error/code (ex-data err))))
|
||||
(done)))))))
|
||||
|
||||
(t/deftest import-missing-references-json-test
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user