0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-07 15:39:42 -05:00

Merge pull request #289 from tokens-studio/revert-288-active-themes

Revert "Adds `active-themes` to `TokensLib`"
This commit is contained in:
Florian Schrödl 2024-09-25 16:22:20 +02:00 committed by GitHub
commit 6f086326f5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 167 deletions

View file

@ -12,7 +12,6 @@
[app.common.time :as dt] [app.common.time :as dt]
[app.common.transit :as t] [app.common.transit :as t]
[app.common.types.token :as cto] [app.common.types.token :as cto]
[clojure.set :as set]
[cuerdas.core :as str] [cuerdas.core :as str]
#?(:clj [app.common.fressian :as fres]))) #?(:clj [app.common.fressian :as fres])))
@ -250,14 +249,8 @@
;; === TokenTheme ;; === TokenTheme
(def theme-separator "/")
(defn token-theme-path [group name]
(join-path [group name] theme-separator))
(defprotocol ITokenTheme (defprotocol ITokenTheme
(toggle-set [_ set-name] "togle a set used / not used in the theme") (toggle-set [_ set-name] "togle a set used / not used in the theme"))
(theme-path [_] "get `token-theme-path` from theme"))
(defrecord TokenTheme [name group description is-source modified-at sets] (defrecord TokenTheme [name group description is-source modified-at sets]
ITokenTheme ITokenTheme
@ -269,9 +262,7 @@
(dt/now) (dt/now)
(if (sets set-name) (if (sets set-name)
(disj sets set-name) (disj sets set-name)
(conj sets set-name)))) (conj sets set-name)))))
(theme-path [_]
(token-theme-path group name)))
(def schema:token-theme (def schema:token-theme
[:and [:map {:title "TokenTheme"} [:and [:map {:title "TokenTheme"}
@ -325,13 +316,7 @@
(get-theme-tree [_] "get a nested tree of all themes in the library") (get-theme-tree [_] "get a nested tree of all themes in the library")
(get-themes [_] "get an ordered sequence of all themes in the library") (get-themes [_] "get an ordered sequence of all themes in the library")
(get-theme [_ group name] "get one theme looking for name") (get-theme [_ group name] "get one theme looking for name")
(get-theme-groups [_] "get a sequence of group names by order") (get-theme-groups [_] "get a sequence of group names by order"))
(get-active-theme-paths [_] "get the active theme paths")
(get-active-themes [_] "get an ordered sequence of active themes in the library")
(theme-active? [_ group name] "predicate if token theme is active")
(activate-theme [_ group name] "adds theme from the active-themes")
(deactivate-theme [_ group name] "removes theme from the active-themes")
(toggle-theme-active? [_ group name] "toggles theme in the active-themes"))
(def schema:token-themes (def schema:token-themes
[:and [:and
@ -348,12 +333,6 @@
(def check-token-themes! (def check-token-themes!
(sm/check-fn ::token-themes)) (sm/check-fn ::token-themes))
(def schema:active-token-themes
[:set string?])
(def valid-active-token-themes?
(sm/validator schema:active-token-themes))
;; === Tokens Lib ;; === Tokens Lib
(defprotocol ITokensLib (defprotocol ITokensLib
@ -364,25 +343,18 @@
(toggle-set-in-theme [_ group-name theme-name set-name] "toggle a set used / not used in a theme") (toggle-set-in-theme [_ group-name theme-name set-name] "toggle a set used / not used in a theme")
(validate [_])) (validate [_]))
(deftype TokensLib [sets set-groups themes active-themes] (deftype TokensLib [sets set-groups themes]
;; NOTE: This is only for debug purposes, pending to properly ;; NOTE: This is only for debug purposes, pending to properly
;; implement the toString and alternative printing. ;; implement the toString and alternative printing.
#?@(:clj [clojure.lang.IDeref #?@(:clj [clojure.lang.IDeref
(deref [_] {:sets sets (deref [_] {:sets sets :set-groups set-groups :themes themes})]
:set-groups set-groups
:themes themes
:active-themes active-themes})]
:cljs [cljs.core/IDeref :cljs [cljs.core/IDeref
(-deref [_] {:sets sets (-deref [_] {:sets sets :set-groups set-groups :themes themes})])
:set-groups set-groups
:themes themes
:active-themes active-themes})])
#?@(:cljs [cljs.core/IEncodeJS #?@(:cljs [cljs.core/IEncodeJS
(-clj->js [_] (js-obj "sets" (clj->js sets) (-clj->js [_] (js-obj "sets" (clj->js sets)
"set-groups" (clj->js set-groups) "set-groups" (clj->js set-groups)
"themes" (clj->js themes) "themes" (clj->js themes)))])
"active-themes" (clj->js active-themes)))])
ITokenSets ITokenSets
(add-set [_ token-set] (add-set [_ token-set]
@ -393,8 +365,7 @@
(cond-> set-groups (cond-> set-groups
(not (str/empty? groups-str)) (not (str/empty? groups-str))
(assoc groups-str (make-token-set-group))) (assoc groups-str (make-token-set-group)))
themes themes)))
active-themes)))
(update-set [this set-name f] (update-set [this set-name f]
(let [path (split-path set-name "/") (let [path (split-path set-name "/")
@ -410,16 +381,14 @@
(d/oassoc-in-before path path' set') (d/oassoc-in-before path path' set')
(d/dissoc-in path))) (d/dissoc-in path)))
set-groups ;; TODO update set-groups as needed set-groups ;; TODO update set-groups as needed
themes themes))
active-themes))
this))) this)))
(delete-set [_ set-name] (delete-set [_ set-name]
(let [path (split-path set-name "/")] (let [path (split-path set-name "/")]
(TokensLib. (d/dissoc-in sets path) (TokensLib. (d/dissoc-in sets path)
set-groups ;; TODO remove set-group if needed set-groups ;; TODO remove set-group if needed
themes themes)))
active-themes)))
(get-set-tree [_] (get-set-tree [_]
sets) sets)
@ -443,8 +412,7 @@
(dm/assert! "expected valid token theme" (check-token-theme! token-theme)) (dm/assert! "expected valid token theme" (check-token-theme! token-theme))
(TokensLib. sets (TokensLib. sets
set-groups set-groups
(update themes (:group token-theme) d/oassoc (:name token-theme) token-theme) (update themes (:group token-theme) d/oassoc (:name token-theme) token-theme)))
active-themes))
(update-theme [this group name f] (update-theme [this group name f]
(let [theme (dm/get-in themes [group name])] (let [theme (dm/get-in themes [group name])]
@ -460,15 +428,13 @@
(update themes group' assoc name' theme') (update themes group' assoc name' theme')
(-> themes (-> themes
(d/oassoc-in-before [group name] [group' name'] theme') (d/oassoc-in-before [group name] [group' name'] theme')
(d/dissoc-in [group name]))) (d/dissoc-in [group name])))))
active-themes))
this))) this)))
(delete-theme [_ group name] (delete-theme [_ group name]
(TokensLib. sets (TokensLib. sets
set-groups set-groups
(d/dissoc-in themes [group name]) (d/dissoc-in themes [group name])))
(disj active-themes (token-theme-path group name))))
(get-theme-tree [_] (get-theme-tree [_]
themes) themes)
@ -489,52 +455,13 @@
(get-theme [_ group name] (get-theme [_ group name]
(dm/get-in themes [group name])) (dm/get-in themes [group name]))
(activate-theme [this group name]
(if-let [theme (get-theme this group name)]
(let [group-themes (->> (get themes group)
(map (comp theme-path val))
(into #{}))
active-themes' (-> (set/difference active-themes group-themes)
(conj (theme-path theme)))]
(TokensLib. sets
set-groups
themes
active-themes'))
this))
(deactivate-theme [_ group name]
(TokensLib. sets
set-groups
themes
(disj active-themes (token-theme-path group name))))
(theme-active? [_ group name]
(contains? active-themes (token-theme-path group name)))
(toggle-theme-active? [this group name]
(if (theme-active? this group name)
(deactivate-theme this group name)
(activate-theme this group name)))
(get-active-theme-paths [_]
active-themes)
(get-active-themes [this]
(into
(list)
(comp
(filter (partial instance? TokenTheme))
(filter #(theme-active? this (:group %) (:name %))))
(tree-seq d/ordered-map? vals themes)))
ITokensLib ITokensLib
(add-token-in-set [this set-name token] (add-token-in-set [this set-name token]
(dm/assert! "expected valid token instance" (check-token! token)) (dm/assert! "expected valid token instance" (check-token! token))
(if (contains? sets set-name) (if (contains? sets set-name)
(TokensLib. (update sets set-name add-token token) (TokensLib. (update sets set-name add-token token)
set-groups set-groups
themes themes)
active-themes)
this)) this))
(update-token-in-set [this set-name token-name f] (update-token-in-set [this set-name token-name f]
@ -542,8 +469,7 @@
(TokensLib. (update sets set-name (TokensLib. (update sets set-name
#(update-token % token-name f)) #(update-token % token-name f))
set-groups set-groups
themes themes)
active-themes)
this)) this))
(delete-token-from-set [this set-name token-name] (delete-token-from-set [this set-name token-name]
@ -551,8 +477,7 @@
(TokensLib. (update sets set-name (TokensLib. (update sets set-name
#(delete-token % token-name)) #(delete-token % token-name))
set-groups set-groups
themes themes)
active-themes)
this)) this))
(toggle-set-in-theme [this theme-group theme-name set-name] (toggle-set-in-theme [this theme-group theme-name set-name]
@ -560,14 +485,12 @@
(TokensLib. sets (TokensLib. sets
set-groups set-groups
(d/oupdate-in themes [theme-group theme-name] (d/oupdate-in themes [theme-group theme-name]
#(toggle-set % set-name)) #(toggle-set % set-name)))
active-themes)
this)) this))
(validate [_] (validate [_]
(and (valid-token-sets? sets) ;; TODO: validate set-groups (and (valid-token-sets? sets) ;; TODO: validate set-groups
(valid-token-themes? themes) (valid-token-themes? themes))))
(valid-active-token-themes? active-themes))))
(defn valid-tokens-lib? (defn valid-tokens-lib?
[o] [o]
@ -590,11 +513,10 @@
;; with pages and pages-index. ;; with pages and pages-index.
(make-tokens-lib :sets (d/ordered-map) (make-tokens-lib :sets (d/ordered-map)
:set-groups {} :set-groups {}
:themes (d/ordered-map) :themes (d/ordered-map)))
:active-themes #{}))
([& {:keys [sets set-groups themes active-themes]}] ([& {:keys [sets set-groups themes]}]
(let [tokens-lib (TokensLib. sets set-groups themes (or active-themes #{}))] (let [tokens-lib (TokensLib. sets set-groups themes)]
(dm/assert! (dm/assert!
"expected valid tokens lib" "expected valid tokens lib"
@ -619,16 +541,16 @@
:class TokensLib :class TokensLib
:wfn deref :wfn deref
:rfn #(make-tokens-lib %)} :rfn #(make-tokens-lib %)}
{:id "penpot/token-set" {:id "penpot/token-set"
:class TokenSet :class TokenSet
:wfn #(into {} %) :wfn #(into {} %)
:rfn #(make-token-set %)} :rfn #(make-token-set %)}
{:id "penpot/token-theme" {:id "penpot/token-theme"
:class TokenTheme :class TokenTheme
:wfn #(into {} %) :wfn #(into {} %)
:rfn #(make-token-theme %)} :rfn #(make-token-theme %)}
{:id "penpot/token" {:id "penpot/token"
:class Token :class Token
@ -670,11 +592,9 @@
(fres/write-tag! w n 3) (fres/write-tag! w n 3)
(fres/write-object! w (.-sets o)) (fres/write-object! w (.-sets o))
(fres/write-object! w (.-set-groups o)) (fres/write-object! w (.-set-groups o))
(fres/write-object! w (.-themes o)) (fres/write-object! w (.-themes o)))
(fres/write-object! w (.-active-themes o)))
:rfn (fn [r] :rfn (fn [r]
(let [sets (fres/read-object! r) (let [sets (fres/read-object! r)
set-groups (fres/read-object! r) set-groups (fres/read-object! r)
themes (fres/read-object! r) themes (fres/read-object! r)]
active-themes (fres/read-object! r)] (->TokensLib sets set-groups themes)))}))
(->TokensLib sets set-groups themes active-themes)))}))

View file

@ -392,54 +392,6 @@
(t/is (dt/is-after? (:modified-at token-theme') (:modified-at token-theme)))))) (t/is (dt/is-after? (:modified-at token-theme') (:modified-at token-theme))))))
(t/testing "theme activation in a lib"
(t/deftest get-theme-collections
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :group "" :name "other-theme"))
(ctob/add-theme (ctob/make-token-theme :group "" :name "theme-1"))
(ctob/activate-theme "" "theme-1")
(ctob/add-theme (ctob/make-token-theme :group "group-1" :name "theme-2"))
(ctob/activate-theme "group-1" "theme-2"))
expected-active-themes (->> (ctob/get-active-themes tokens-lib)
(map #(select-keys % [:name :group])))]
(t/is (= #{"/theme-1" "group-1/theme-2"}
(ctob/get-active-theme-paths tokens-lib))
"should be set of active theme paths")
(t/is (= (list {:group "group-1" :name "theme-2"} {:group "" :name "theme-1"})
expected-active-themes))))
(t/deftest toggle-theme-activity-in-group
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :group "group-1" :name "theme-1"))
(ctob/add-theme (ctob/make-token-theme :group "group-1" :name "theme-2")))
tokens-lib' (-> tokens-lib
(ctob/activate-theme "group-1" "theme-1")
(ctob/activate-theme "group-1" "theme-2"))]
(t/is (not (ctob/theme-active? tokens-lib' "group-1" "theme-1")) "theme-1 should be de-activated")
(t/is (ctob/theme-active? tokens-lib' "group-1" "theme-2") "theme-1 should be activated")))
(t/deftest toggle-theme-activity
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/add-theme (ctob/make-token-theme :group "group-1" :name "theme-1"))
(ctob/toggle-theme-active? "group-1" "theme-1"))
tokens-lib' (-> tokens-lib
(ctob/toggle-theme-active? "group-1" "theme-1"))]
(t/is (ctob/theme-active? tokens-lib "group-1" "theme-1") "theme-1 should be activated")
(t/is (not (ctob/theme-active? tokens-lib' "group-1" "theme-2")) "theme-1 got deactivated by toggling")))
(t/deftest activating-missing-theme-noop
(let [tokens-lib (-> (ctob/make-tokens-lib)
(ctob/toggle-theme-active? "group-1" "theme-1"))]
(t/is (= #{} (ctob/get-active-theme-paths tokens-lib)) "Should not non-existing theme to the active-themes"))))
(t/testing "serialization" (t/testing "serialization"
(t/deftest transit-serialization (t/deftest transit-serialization
(let [tokens-lib (-> (ctob/make-tokens-lib) (let [tokens-lib (-> (ctob/make-tokens-lib)
@ -448,15 +400,13 @@
:type :boolean :type :boolean
:value true)) :value true))
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme")) (ctob/add-theme (ctob/make-token-theme :name "test-token-theme"))
(ctob/toggle-set-in-theme "" "test-token-theme" "test-token-set") (ctob/toggle-set-in-theme "" "test-token-theme" "test-token-set"))
(ctob/activate-theme "" "test-token-theme"))
encoded-str (tr/encode-str tokens-lib) encoded-str (tr/encode-str tokens-lib)
tokens-lib' (tr/decode-str encoded-str)] tokens-lib' (tr/decode-str encoded-str)]
(t/is (ctob/valid-tokens-lib? tokens-lib')) (t/is (ctob/valid-tokens-lib? tokens-lib'))
(t/is (= (ctob/set-count tokens-lib') 1)) (t/is (= (ctob/set-count tokens-lib') 1))
(t/is (= (ctob/theme-count tokens-lib') 1)) (t/is (= (ctob/theme-count tokens-lib') 1))))
(t/is (= (ctob/get-active-theme-paths tokens-lib') #{"/test-token-theme"}))))
(t/deftest fressian-serialization (t/deftest fressian-serialization
(let [tokens-lib (-> (ctob/make-tokens-lib) (let [tokens-lib (-> (ctob/make-tokens-lib)
@ -465,16 +415,13 @@
:type :boolean :type :boolean
:value true)) :value true))
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme")) (ctob/add-theme (ctob/make-token-theme :name "test-token-theme"))
(ctob/toggle-set-in-theme "" "test-token-theme" "test-token-set") (ctob/toggle-set-in-theme "" "test-token-theme" "test-token-set"))
(ctob/activate-theme "" "test-token-theme"))
encoded-blob (fres/encode tokens-lib) encoded-blob (fres/encode tokens-lib)
tokens-lib' (fres/decode encoded-blob)] tokens-lib' (fres/decode encoded-blob)]
(t/is (ctob/valid-tokens-lib? tokens-lib')) (t/is (ctob/valid-tokens-lib? tokens-lib'))
(t/is (= (ctob/set-count tokens-lib') 1)) (t/is (= (ctob/set-count tokens-lib') 1))
(t/is (= (ctob/theme-count tokens-lib') 1)) (t/is (= (ctob/theme-count tokens-lib') 1)))))
(t/is (= (ctob/get-active-theme-paths tokens-lib') #{"/test-token-theme"})))))
(t/testing "grouping" (t/testing "grouping"
(t/deftest split-and-join (t/deftest split-and-join