0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-06 14:50:20 -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.transit :as t]
[app.common.types.token :as cto]
[clojure.set :as set]
[cuerdas.core :as str]
#?(:clj [app.common.fressian :as fres])))
@ -250,14 +249,8 @@
;; === TokenTheme
(def theme-separator "/")
(defn token-theme-path [group name]
(join-path [group name] theme-separator))
(defprotocol ITokenTheme
(toggle-set [_ set-name] "togle a set used / not used in the theme")
(theme-path [_] "get `token-theme-path` from theme"))
(toggle-set [_ set-name] "togle a set used / not used in the theme"))
(defrecord TokenTheme [name group description is-source modified-at sets]
ITokenTheme
@ -269,9 +262,7 @@
(dt/now)
(if (sets set-name)
(disj sets set-name)
(conj sets set-name))))
(theme-path [_]
(token-theme-path group name)))
(conj sets set-name)))))
(def schema:token-theme
[:and [:map {:title "TokenTheme"}
@ -325,13 +316,7 @@
(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-theme [_ group name] "get one theme looking for name")
(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"))
(get-theme-groups [_] "get a sequence of group names by order"))
(def schema:token-themes
[:and
@ -348,12 +333,6 @@
(def check-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
(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")
(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
;; implement the toString and alternative printing.
#?@(:clj [clojure.lang.IDeref
(deref [_] {:sets sets
:set-groups set-groups
:themes themes
:active-themes active-themes})]
(deref [_] {:sets sets :set-groups set-groups :themes themes})]
:cljs [cljs.core/IDeref
(-deref [_] {:sets sets
:set-groups set-groups
:themes themes
:active-themes active-themes})])
(-deref [_] {:sets sets :set-groups set-groups :themes themes})])
#?@(:cljs [cljs.core/IEncodeJS
(-clj->js [_] (js-obj "sets" (clj->js sets)
"set-groups" (clj->js set-groups)
"themes" (clj->js themes)
"active-themes" (clj->js active-themes)))])
"themes" (clj->js themes)))])
ITokenSets
(add-set [_ token-set]
@ -393,8 +365,7 @@
(cond-> set-groups
(not (str/empty? groups-str))
(assoc groups-str (make-token-set-group)))
themes
active-themes)))
themes)))
(update-set [this set-name f]
(let [path (split-path set-name "/")
@ -410,16 +381,14 @@
(d/oassoc-in-before path path' set')
(d/dissoc-in path)))
set-groups ;; TODO update set-groups as needed
themes
active-themes))
themes))
this)))
(delete-set [_ set-name]
(let [path (split-path set-name "/")]
(TokensLib. (d/dissoc-in sets path)
set-groups ;; TODO remove set-group if needed
themes
active-themes)))
themes)))
(get-set-tree [_]
sets)
@ -443,8 +412,7 @@
(dm/assert! "expected valid token theme" (check-token-theme! token-theme))
(TokensLib. sets
set-groups
(update themes (:group token-theme) d/oassoc (:name token-theme) token-theme)
active-themes))
(update themes (:group token-theme) d/oassoc (:name token-theme) token-theme)))
(update-theme [this group name f]
(let [theme (dm/get-in themes [group name])]
@ -460,15 +428,13 @@
(update themes group' assoc name' theme')
(-> themes
(d/oassoc-in-before [group name] [group' name'] theme')
(d/dissoc-in [group name])))
active-themes))
(d/dissoc-in [group name])))))
this)))
(delete-theme [_ group name]
(TokensLib. sets
set-groups
(d/dissoc-in themes [group name])
(disj active-themes (token-theme-path group name))))
(d/dissoc-in themes [group name])))
(get-theme-tree [_]
themes)
@ -489,52 +455,13 @@
(get-theme [_ 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
(add-token-in-set [this set-name token]
(dm/assert! "expected valid token instance" (check-token! token))
(if (contains? sets set-name)
(TokensLib. (update sets set-name add-token token)
set-groups
themes
active-themes)
themes)
this))
(update-token-in-set [this set-name token-name f]
@ -542,8 +469,7 @@
(TokensLib. (update sets set-name
#(update-token % token-name f))
set-groups
themes
active-themes)
themes)
this))
(delete-token-from-set [this set-name token-name]
@ -551,8 +477,7 @@
(TokensLib. (update sets set-name
#(delete-token % token-name))
set-groups
themes
active-themes)
themes)
this))
(toggle-set-in-theme [this theme-group theme-name set-name]
@ -560,14 +485,12 @@
(TokensLib. sets
set-groups
(d/oupdate-in themes [theme-group theme-name]
#(toggle-set % set-name))
active-themes)
#(toggle-set % set-name)))
this))
(validate [_]
(and (valid-token-sets? sets) ;; TODO: validate set-groups
(valid-token-themes? themes)
(valid-active-token-themes? active-themes))))
(valid-token-themes? themes))))
(defn valid-tokens-lib?
[o]
@ -590,11 +513,10 @@
;; with pages and pages-index.
(make-tokens-lib :sets (d/ordered-map)
:set-groups {}
:themes (d/ordered-map)
:active-themes #{}))
:themes (d/ordered-map)))
([& {:keys [sets set-groups themes active-themes]}]
(let [tokens-lib (TokensLib. sets set-groups themes (or active-themes #{}))]
([& {:keys [sets set-groups themes]}]
(let [tokens-lib (TokensLib. sets set-groups themes)]
(dm/assert!
"expected valid tokens lib"
@ -619,16 +541,16 @@
:class TokensLib
:wfn deref
:rfn #(make-tokens-lib %)}
{:id "penpot/token-set"
:class TokenSet
:wfn #(into {} %)
:rfn #(make-token-set %)}
:rfn #(make-token-set %)}
{:id "penpot/token-theme"
:class TokenTheme
:wfn #(into {} %)
:rfn #(make-token-theme %)}
:rfn #(make-token-theme %)}
{:id "penpot/token"
:class Token
@ -670,11 +592,9 @@
(fres/write-tag! w n 3)
(fres/write-object! w (.-sets o))
(fres/write-object! w (.-set-groups o))
(fres/write-object! w (.-themes o))
(fres/write-object! w (.-active-themes o)))
(fres/write-object! w (.-themes o)))
:rfn (fn [r]
(let [sets (fres/read-object! r)
set-groups (fres/read-object! r)
themes (fres/read-object! r)
active-themes (fres/read-object! r)]
(->TokensLib sets set-groups themes active-themes)))}))
(let [sets (fres/read-object! r)
set-groups (fres/read-object! r)
themes (fres/read-object! r)]
(->TokensLib sets set-groups themes)))}))

View file

@ -392,54 +392,6 @@
(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/deftest transit-serialization
(let [tokens-lib (-> (ctob/make-tokens-lib)
@ -448,15 +400,13 @@
:type :boolean
:value true))
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme"))
(ctob/toggle-set-in-theme "" "test-token-theme" "test-token-set")
(ctob/activate-theme "" "test-token-theme"))
(ctob/toggle-set-in-theme "" "test-token-theme" "test-token-set"))
encoded-str (tr/encode-str tokens-lib)
tokens-lib' (tr/decode-str encoded-str)]
(t/is (ctob/valid-tokens-lib? tokens-lib'))
(t/is (= (ctob/set-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/is (= (ctob/theme-count tokens-lib') 1))))
(t/deftest fressian-serialization
(let [tokens-lib (-> (ctob/make-tokens-lib)
@ -465,16 +415,13 @@
:type :boolean
:value true))
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme"))
(ctob/toggle-set-in-theme "" "test-token-theme" "test-token-set")
(ctob/activate-theme "" "test-token-theme"))
(ctob/toggle-set-in-theme "" "test-token-theme" "test-token-set"))
encoded-blob (fres/encode tokens-lib)
tokens-lib' (fres/decode encoded-blob)]
(t/is (ctob/valid-tokens-lib? tokens-lib'))
(t/is (= (ctob/set-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/is (= (ctob/theme-count tokens-lib') 1)))))
(t/testing "grouping"
(t/deftest split-and-join