From cdfbff7c530833addb33ca1f0935f4dc1edb7287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Schr=C3=B6dl?= Date: Wed, 18 Nov 2026 09:19:55 +0100 Subject: [PATCH] :bug: Fix active themes in token export and the empty file import error (#13123) * :bug: 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 * :bug: 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 --------- Signed-off-by: Florian Schroedl --- common/src/app/common/types/tokens_lib.cljc | 4 +- .../ui/workspace/tokens/export/modal.cljs | 27 ++++++-- frontend/test/frontend_tests/runner.cljs | 2 + .../tokens/export_modal_test.cljs | 65 +++++++++++++++++++ .../tokens/import_export_test.cljs | 4 +- 5 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 frontend/test/frontend_tests/tokens/export_modal_test.cljs diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index d045e93e55..950cde98d9 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -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] diff --git a/frontend/src/app/main/ui/workspace/tokens/export/modal.cljs b/frontend/src/app/main/ui/workspace/tokens/export/modal.cljs index b1c9ee0f09..1d5db32d98 100644 --- a/frontend/src/app/main/ui/workspace/tokens/export/modal.cljs +++ b/frontend/src/app/main/ui/workspace/tokens/export/modal.cljs @@ -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 diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 8541fff980..c5d4f9518e 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -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 diff --git a/frontend/test/frontend_tests/tokens/export_modal_test.cljs b/frontend/test/frontend_tests/tokens/export_modal_test.cljs new file mode 100644 index 0000000000..834ea88736 --- /dev/null +++ b/frontend/test/frontend_tests/tokens/export_modal_test.cljs @@ -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))))) diff --git a/frontend/test/frontend_tests/tokens/import_export_test.cljs b/frontend/test/frontend_tests/tokens/import_export_test.cljs index 315cd1c851..a38110d19f 100644 --- a/frontend/test/frontend_tests/tokens/import_export_test.cljs +++ b/frontend/test/frontend_tests/tokens/import_export_test.cljs @@ -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