This commit is contained in:
Andrés Moya 2026-06-12 09:58:23 +02:00
parent fe144963e2
commit fbaf8ac63e
5 changed files with 67 additions and 80 deletions

View File

@ -379,6 +379,22 @@
;; Tokens status
(defn make-token-status-from-lib
"Make a TokenStatus from a TokensLib, activating the themes and sets
marked as active in the library (to migrate from legacy files)."
[tokens-lib]
(assert (ctob/tokens-lib? tokens-lib) "expected valid tokens-lib")
(let [active-themes (into #{}
(comp (map :id)
(filter #(not= % ctob/hidden-theme-id)))
(ctob/get-active-themes tokens-lib))
active-sets (into #{}
(comp (map #(ctob/get-set-by-name tokens-lib %))
(map ctob/get-id))
(ctob/get-active-themes-set-names tokens-lib))]
(ctos/make-token-status :active-themes active-themes
:active-sets active-sets)))
(defn set-theme-status
[token-status tokens-lib theme-id status]
(assert (ctos/token-status? token-status) "expected valid token-status")
@ -386,5 +402,5 @@
(assert (uuid? theme-id) "expected valid theme-id")
(assert (boolean? status) "expected boolean status")
(if (ctob/get-theme tokens-lib theme-id)
(ctos/set-theme-status token-status tokens-lib theme-id status)
(ctos/set-theme-status token-status theme-id status)
token-status))

View File

@ -11,6 +11,7 @@
[app.common.features :as cfeat]
[app.common.files.defaults :refer [version]]
[app.common.files.helpers :as cfh]
[app.common.files.tokens :as cfo]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[app.common.geom.shapes.tree-seq :as gsts]
@ -203,7 +204,7 @@
[file-data]
(if (and (some? (:tokens-lib file-data)) (nil? (:token-status file-data)))
;; TODO: remove this when we deprecate old-style files without token-status
(assoc file-data :token-status (ctos/make-token-status-from-lib (:tokens-lib file-data)))
(assoc file-data :token-status (cfo/make-token-status-from-lib (:tokens-lib file-data)))
(-> file-data
(update :tokens-lib #(or % (ctob/make-tokens-lib)))
(update :token-status #(or % (ctos/make-token-status))))))
@ -326,7 +327,7 @@
[file-data]
(if (and (some? (:tokens-lib file-data)) (nil? (:token-status file-data)))
;; TODO: remove this when we deprecate old-style files without token-status
(ctos/make-token-status-from-lib (:tokens-lib file-data))
(cfo/make-token-status-from-lib (:tokens-lib file-data))
(:token-status file-data)))
(defn update-tokens-lib
@ -337,9 +338,9 @@
(defn update-token-status
"Update the token-status inside file-data through a callback function.
The function will receive the tokens status, the tokens lib and the rest of args."
The function will receive the tokens status and the rest of args."
[file-data f & args]
(d/update-when file-data :token-status #(apply f % (get-tokens-lib file-data) args)))
(d/update-when file-data :token-status #(apply f % args)))
;; Asset helpers
(defn find-component-file

View File

@ -11,7 +11,6 @@
[app.common.schema :as sm]
[app.common.schema.generators :as sg]
[app.common.transit :as t]
[app.common.types.tokens-lib :as ctob]
[clojure.core.protocols :as cp]
[clojure.datafy :refer [datafy]]
[clojure.pprint :as pp]))
@ -20,9 +19,9 @@
;; in a tokens library.
(defprotocol ITokenStatus
(activate-theme [_ tokens-lib theme-id] "Add a theme uuid to active themes")
(deactivate-theme [_ tokens-lib theme-id] "Remove a theme uuid from active themes")
(set-theme-status [_ tokens-lib theme-id status] "Add or remove a theme uuid to active themes")
(activate-theme [_ theme-id] "Add a theme uuid to active themes")
(deactivate-theme [_ theme-id] "Remove a theme uuid from active themes")
(set-theme-status [_ theme-id status] "Add or remove a theme uuid to active themes")
(theme-active? [_ theme-id] "Check if a theme uuid is active")
(active-themes-count [_] "Return the number of active themes")
(activate-set [_ set-id] "Add a set uuid to active sets")
@ -43,27 +42,20 @@
(c.json/-write (datafy this) writter options))])
ITokenStatus
(activate-theme [this tokens-lib theme-id]
(assert (ctob/tokens-lib? tokens-lib) "expected valid tokens-lib")
(activate-theme [_ theme-id]
(assert (uuid? theme-id) "expected valid theme-id")
(if (ctob/get-theme tokens-lib theme-id)
(TokenStatus. (conj active-themes theme-id) active-sets)
this))
(TokenStatus. (conj active-themes theme-id) active-sets))
(deactivate-theme [this tokens-lib theme-id]
(assert (ctob/tokens-lib? tokens-lib) "expected valid tokens-lib")
(deactivate-theme [_ theme-id]
(assert (uuid? theme-id) "expected valid theme-id")
(if (ctob/get-theme tokens-lib theme-id)
(TokenStatus. (disj active-themes theme-id) active-sets)
this))
(TokenStatus. (disj active-themes theme-id) active-sets))
(set-theme-status [this tokens-lib theme-id status]
(assert (ctob/tokens-lib? tokens-lib) "expected valid tokens-lib")
(set-theme-status [this theme-id status]
(assert (uuid? theme-id) "expected valid theme-id")
(assert (boolean? status) "expected boolean status")
(if status
(activate-theme this tokens-lib theme-id)
(deactivate-theme this tokens-lib theme-id)))
(activate-theme this theme-id)
(deactivate-theme this theme-id)))
(theme-active? [_ theme-id]
(assert (uuid? theme-id) "expected valid theme-id")
@ -133,22 +125,6 @@
(check-token-status-attrs)
(map->TokenStatus)))
(defn make-token-status-from-lib
"Make a TokenStatus from a TokensLib, activating the themes and sets
marked as active in the library (to migrate from legacy files)."
[tokens-lib]
(assert (ctob/tokens-lib? tokens-lib) "expected valid tokens-lib")
(let [active-themes (into #{}
(comp (map :id)
(filter #(not= % ctob/hidden-theme-id)))
(ctob/get-active-themes tokens-lib))
active-sets (into #{}
(comp (map #(ctob/get-set-by-name tokens-lib %))
(map ctob/get-id))
(ctob/get-active-themes-set-names tokens-lib))]
(make-token-status :active-themes active-themes
:active-sets active-sets)))
;; === Pretty-print for debugging ===
(defmethod pp/simple-dispatch TokenStatus [^TokenStatus obj]

View File

@ -86,6 +86,37 @@
{:applied-tokens {:x "a"}}]
#{:y})))))
;; Make TokenStatus from a TokensLib (to migrate from legacy files)
(t/deftest make-token-status-from-tokens-lib
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-set (ctob/make-token-set :id (thi/new-id! :set-a)
:name "set-a"))
(ctob/add-set (ctob/make-token-set :id (thi/new-id! :set-b)
:name "set-b"))
(ctob/add-set (ctob/make-token-set :id (thi/new-id! :set-c)
:name "set-c"))
(ctob/add-set (ctob/make-token-set :id (thi/new-id! :set-d)
:name "set-d"))
(ctob/add-theme (ctob/make-token-theme :id (thi/new-id! :theme-1)
:name "theme-1"
:sets #{"set-a" "set-b"}))
(ctob/add-theme (ctob/make-token-theme :id (thi/new-id! :theme-2)
:name "theme-2"
:sets #{"set-b"}))
(ctob/add-theme (ctob/make-token-theme :id (thi/new-id! :theme-3)
:name "theme-3"
:sets #{"set-c" "set-d"}))
(ctob/set-active-themes #{"/theme-1" "/theme-2"}))
token-status (cfo/make-token-status-from-lib tokens-lib)]
(t/is (ctos/token-status? token-status))
(t/is (ctos/check-token-status token-status))
(t/is (= (ctos/active-themes-count token-status) 2))
(t/is (ctos/theme-active? token-status (thi/id :theme-1)))
(t/is (ctos/theme-active? token-status (thi/id :theme-2)))
(t/is (= (ctos/active-set-count token-status) 2))
(t/is (ctos/set-active? token-status (thi/id :set-a)))
(t/is (ctos/set-active? token-status (thi/id :set-b)))))
(t/deftest set-theme-status
(t/testing "setting the status of a theme gets it activated or deactivated"
(let [tokens-lib (-> (ctob/make-tokens-lib)

View File

@ -47,31 +47,25 @@
(t/deftest activate-theme
(let [theme-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :id theme-id :name "theme")))
status (ctos/make-token-status)
status' (ctos/activate-theme status tokens-lib theme-id)]
status' (ctos/activate-theme status theme-id)]
(t/is (not (ctos/theme-active? status theme-id)))
(t/is (ctos/theme-active? status' theme-id))
(t/is (= (ctos/active-themes-count status') 1))))
(t/deftest deactivate-theme
(let [theme-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :id theme-id :name "theme")))
status (ctos/make-token-status :active-themes #{theme-id})
status' (ctos/deactivate-theme status tokens-lib theme-id)]
status' (ctos/deactivate-theme status theme-id)]
(t/is (ctos/theme-active? status theme-id))
(t/is (not (ctos/theme-active? status' theme-id)))
(t/is (= (ctos/active-themes-count status') 0))))
(t/deftest set-theme-status
(let [theme-id (uuid/next)
tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :id theme-id :name "theme")))
status (ctos/make-token-status)
status' (ctos/set-theme-status status tokens-lib theme-id true)
status'' (ctos/set-theme-status status' tokens-lib theme-id false)]
status' (ctos/set-theme-status status theme-id true)
status'' (ctos/set-theme-status status' theme-id false)]
(t/is (ctos/theme-active? status' theme-id))
(t/is (not (ctos/theme-active? status'' theme-id)))
(t/is (= (ctos/active-themes-count status') 1))
@ -146,34 +140,3 @@
(t/is (map? parsed))
(t/is (= [(str theme-id)] (:active-themes parsed)))
(t/is (= [(str set-id)] (:active-sets parsed))))))
;; Make TokenStatus from a TokensLib (to migrate from legacy files)
(t/deftest make-token-status-from-tokens-lib
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-set (ctob/make-token-set :id (thi/new-id! :set-a)
:name "set-a"))
(ctob/add-set (ctob/make-token-set :id (thi/new-id! :set-b)
:name "set-b"))
(ctob/add-set (ctob/make-token-set :id (thi/new-id! :set-c)
:name "set-c"))
(ctob/add-set (ctob/make-token-set :id (thi/new-id! :set-d)
:name "set-d"))
(ctob/add-theme (ctob/make-token-theme :id (thi/new-id! :theme-1)
:name "theme-1"
:sets #{"set-a" "set-b"}))
(ctob/add-theme (ctob/make-token-theme :id (thi/new-id! :theme-2)
:name "theme-2"
:sets #{"set-b"}))
(ctob/add-theme (ctob/make-token-theme :id (thi/new-id! :theme-3)
:name "theme-3"
:sets #{"set-c" "set-d"}))
(ctob/set-active-themes #{"/theme-1" "/theme-2"}))
token-status (ctos/make-token-status-from-lib tokens-lib)]
(t/is (ctos/token-status? token-status))
(t/is (ctos/check-token-status token-status))
(t/is (= (ctos/active-themes-count token-status) 2))
(t/is (ctos/theme-active? token-status (thi/id :theme-1)))
(t/is (ctos/theme-active? token-status (thi/id :theme-2)))
(t/is (= (ctos/active-set-count token-status) 2))
(t/is (ctos/set-active? token-status (thi/id :set-a)))
(t/is (ctos/set-active? token-status (thi/id :set-b)))))