mirror of
https://github.com/penpot/penpot.git
synced 2025-06-12 20:21:38 +02:00
🔧 Add token themes in tokens-lib custom type
This commit is contained in:
parent
1d7536687a
commit
316d333c96
7 changed files with 565 additions and 239 deletions
|
@ -263,7 +263,8 @@
|
||||||
[:delete-temporary-token-theme
|
[:delete-temporary-token-theme
|
||||||
[:map {:title "DeleteTemporaryTokenThemeChange"}
|
[:map {:title "DeleteTemporaryTokenThemeChange"}
|
||||||
[:type [:= :delete-temporary-token-theme]]
|
[:type [:= :delete-temporary-token-theme]]
|
||||||
[:id ::sm/uuid]]]
|
[:id ::sm/uuid]
|
||||||
|
[:name :string]]]
|
||||||
|
|
||||||
[:add-token-theme
|
[:add-token-theme
|
||||||
[:map {:title "AddTokenThemeChange"}
|
[:map {:title "AddTokenThemeChange"}
|
||||||
|
@ -274,12 +275,14 @@
|
||||||
[:map {:title "ModTokenThemeChange"}
|
[:map {:title "ModTokenThemeChange"}
|
||||||
[:type [:= :mod-token-theme]]
|
[:type [:= :mod-token-theme]]
|
||||||
[:id ::sm/uuid]
|
[:id ::sm/uuid]
|
||||||
|
[:name :string]
|
||||||
[:token-theme ::ctot/token-theme]]]
|
[:token-theme ::ctot/token-theme]]]
|
||||||
|
|
||||||
[:del-token-theme
|
[:del-token-theme
|
||||||
[:map {:title "DelTokenThemeChange"}
|
[:map {:title "DelTokenThemeChange"}
|
||||||
[:type [:= :del-token-theme]]
|
[:type [:= :del-token-theme]]
|
||||||
[:id ::sm/uuid]]]
|
[:id ::sm/uuid]
|
||||||
|
[:name :string]]]
|
||||||
|
|
||||||
[:add-token-set
|
[:add-token-set
|
||||||
[:map {:title "AddTokenSetChange"}
|
[:map {:title "AddTokenSetChange"}
|
||||||
|
@ -822,29 +825,69 @@
|
||||||
set-name
|
set-name
|
||||||
name)))))
|
name)))))
|
||||||
|
|
||||||
|
(defn- set-ids->names
|
||||||
|
[data sets]
|
||||||
|
(let [lib-sets (:token-sets-index data)
|
||||||
|
set-id->name
|
||||||
|
(fn [set-id]
|
||||||
|
(dm/get-in lib-sets [set-id :name]))]
|
||||||
|
(map set-id->name sets)))
|
||||||
|
|
||||||
(defmethod process-change :add-temporary-token-theme
|
(defmethod process-change :add-temporary-token-theme
|
||||||
[data {:keys [token-theme]}]
|
[data {:keys [token-theme]}]
|
||||||
(ctotl/add-temporary-token-theme data token-theme))
|
(-> data
|
||||||
|
(ctotl/add-temporary-token-theme token-theme)
|
||||||
|
(update :tokens-lib
|
||||||
|
#(-> %
|
||||||
|
(ctob/ensure-tokens-lib)
|
||||||
|
(ctob/add-theme (-> token-theme
|
||||||
|
(update :sets (partial set-ids->names data))
|
||||||
|
(ctob/make-token-theme)))))))
|
||||||
|
|
||||||
(defmethod process-change :update-active-token-themes
|
(defmethod process-change :update-active-token-themes
|
||||||
[data {:keys [theme-ids]}]
|
[data {:keys [theme-ids]}]
|
||||||
(ctotl/assoc-active-token-themes data theme-ids))
|
(ctotl/assoc-active-token-themes data theme-ids))
|
||||||
|
|
||||||
(defmethod process-change :delete-temporary-token-theme
|
(defmethod process-change :delete-temporary-token-theme
|
||||||
[data {:keys [id]}]
|
[data {:keys [id name]}]
|
||||||
(ctotl/delete-temporary-token-theme data id))
|
(-> data
|
||||||
|
(ctotl/delete-temporary-token-theme id)
|
||||||
|
(update :tokens-lib
|
||||||
|
#(-> %
|
||||||
|
(ctob/ensure-tokens-lib)
|
||||||
|
(ctob/delete-theme name)))))
|
||||||
|
|
||||||
(defmethod process-change :add-token-theme
|
(defmethod process-change :add-token-theme
|
||||||
[data {:keys [token-theme]}]
|
[data {:keys [token-theme]}]
|
||||||
(ctotl/add-token-theme data token-theme))
|
(-> data
|
||||||
|
(ctotl/add-token-theme token-theme)
|
||||||
|
(update :tokens-lib
|
||||||
|
#(-> %
|
||||||
|
(ctob/ensure-tokens-lib)
|
||||||
|
(ctob/add-theme (-> token-theme
|
||||||
|
(update :sets (partial set-ids->names data))
|
||||||
|
(ctob/make-token-theme)))))))
|
||||||
|
|
||||||
(defmethod process-change :mod-token-theme
|
(defmethod process-change :mod-token-theme
|
||||||
[data {:keys [id token-theme]}]
|
[data {:keys [id name token-theme]}]
|
||||||
(ctotl/update-token-theme data id merge token-theme))
|
(-> data
|
||||||
|
(ctotl/update-token-theme id merge token-theme)
|
||||||
|
(update :tokens-lib
|
||||||
|
#(-> %
|
||||||
|
(ctob/ensure-tokens-lib)
|
||||||
|
(ctob/update-theme name (fn [prev-theme]
|
||||||
|
(merge prev-theme
|
||||||
|
(-> token-theme
|
||||||
|
(update :sets (partial set-ids->names data))))))))))
|
||||||
|
|
||||||
(defmethod process-change :del-token-theme
|
(defmethod process-change :del-token-theme
|
||||||
[data {:keys [id]}]
|
[data {:keys [id name]}]
|
||||||
(ctotl/delete-token-theme data id))
|
(-> data
|
||||||
|
(ctotl/delete-token-theme id)
|
||||||
|
(update :tokens-lib
|
||||||
|
#(-> %
|
||||||
|
(ctob/ensure-tokens-lib)
|
||||||
|
(ctob/delete-theme name)))))
|
||||||
|
|
||||||
(defmethod process-change :add-token-set
|
(defmethod process-change :add-token-set
|
||||||
[data {:keys [token-set]}]
|
[data {:keys [token-set]}]
|
||||||
|
|
|
@ -699,7 +699,7 @@
|
||||||
[changes token-theme]
|
[changes token-theme]
|
||||||
(-> changes
|
(-> changes
|
||||||
(update :redo-changes conj {:type :add-temporary-token-theme :token-theme token-theme})
|
(update :redo-changes conj {:type :add-temporary-token-theme :token-theme token-theme})
|
||||||
(update :undo-changes conj {:type :delete-temporary-token-theme :id (:id token-theme)})
|
(update :undo-changes conj {:type :delete-temporary-token-theme :id (:id token-theme) :name (:name token-theme)})
|
||||||
(apply-changes-local)))
|
(apply-changes-local)))
|
||||||
|
|
||||||
(defn update-active-token-themes
|
(defn update-active-token-themes
|
||||||
|
@ -713,14 +713,14 @@
|
||||||
[changes token-theme]
|
[changes token-theme]
|
||||||
(-> changes
|
(-> changes
|
||||||
(update :redo-changes conj {:type :add-token-theme :token-theme token-theme})
|
(update :redo-changes conj {:type :add-token-theme :token-theme token-theme})
|
||||||
(update :undo-changes conj {:type :del-token-theme :id (:id token-theme)})
|
(update :undo-changes conj {:type :del-token-theme :id (:id token-theme) :name (:name token-theme)})
|
||||||
(apply-changes-local)))
|
(apply-changes-local)))
|
||||||
|
|
||||||
(defn update-token-theme
|
(defn update-token-theme
|
||||||
[changes token-theme prev-token-theme]
|
[changes token-theme prev-token-theme]
|
||||||
(-> changes
|
(-> changes
|
||||||
(update :redo-changes conj {:type :mod-token-theme :id (:id token-theme) :token-theme token-theme})
|
(update :redo-changes conj {:type :mod-token-theme :id (:id token-theme) :name (:name prev-token-theme) :token-theme token-theme})
|
||||||
(update :undo-changes conj {:type :mod-token-theme :id (:id token-theme) :token-theme (or prev-token-theme token-theme)})
|
(update :undo-changes conj {:type :mod-token-theme :id (:id token-theme) :name (:name token-theme) :token-theme (or prev-token-theme token-theme)})
|
||||||
(apply-changes-local)))
|
(apply-changes-local)))
|
||||||
|
|
||||||
(defn delete-token-theme
|
(defn delete-token-theme
|
||||||
|
@ -729,7 +729,7 @@
|
||||||
(let [library-data (::library-data (meta changes))
|
(let [library-data (::library-data (meta changes))
|
||||||
prev-token-theme (get-in library-data [:token-themes-index token-theme-id])]
|
prev-token-theme (get-in library-data [:token-themes-index token-theme-id])]
|
||||||
(-> changes
|
(-> changes
|
||||||
(update :redo-changes conj {:type :del-token-theme :id token-theme-id})
|
(update :redo-changes conj {:type :del-token-theme :id token-theme-id :name (:name prev-token-theme)})
|
||||||
(update :undo-changes conj {:type :add-token-theme :token-theme prev-token-theme})
|
(update :undo-changes conj {:type :add-token-theme :token-theme prev-token-theme})
|
||||||
(apply-changes-local))))
|
(apply-changes-local))))
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
java.time.OffsetDateTime
|
java.time.OffsetDateTime
|
||||||
java.util.List
|
java.util.List
|
||||||
linked.map.LinkedMap
|
linked.map.LinkedMap
|
||||||
|
linked.set.LinkedSet
|
||||||
org.fressian.Reader
|
org.fressian.Reader
|
||||||
org.fressian.StreamingWriter
|
org.fressian.StreamingWriter
|
||||||
org.fressian.Writer
|
org.fressian.Writer
|
||||||
|
@ -275,7 +276,12 @@
|
||||||
{:name "clj/seq"
|
{:name "clj/seq"
|
||||||
:class clojure.lang.ISeq
|
:class clojure.lang.ISeq
|
||||||
:wfn write-list-like
|
:wfn write-list-like
|
||||||
:rfn (comp sequence read-object!)})
|
:rfn (comp sequence read-object!)}
|
||||||
|
|
||||||
|
{:name "linked/set"
|
||||||
|
:class LinkedSet
|
||||||
|
:wfn write-list-like
|
||||||
|
:rfn (comp #(into (d/ordered-set) %) read-object!)})
|
||||||
|
|
||||||
;; --- PUBLIC API
|
;; --- PUBLIC API
|
||||||
|
|
||||||
|
|
|
@ -94,7 +94,8 @@
|
||||||
[:name :string]
|
[:name :string]
|
||||||
[:description [:maybe :string]]
|
[:description [:maybe :string]]
|
||||||
[:modified-at ::sm/inst]
|
[:modified-at ::sm/inst]
|
||||||
[:tokens [:map-of {:gen/max 5} :string ::token]]]
|
[:tokens [:and [:map-of {:gen/max 5} :string ::token]
|
||||||
|
[:fn d/ordered-map?]]]]
|
||||||
[:fn (partial instance? TokenSet)]])
|
[:fn (partial instance? TokenSet)]])
|
||||||
|
|
||||||
(sm/register! ::token-set schema:token-set)
|
(sm/register! ::token-set schema:token-set)
|
||||||
|
@ -127,8 +128,7 @@
|
||||||
(delete-set [_ set-name] "delete a set in the library")
|
(delete-set [_ set-name] "delete a set in the library")
|
||||||
(set-count [_] "get the total number if sets in the library")
|
(set-count [_] "get the total number if sets in the library")
|
||||||
(get-sets [_] "get an ordered sequence of all sets in the library")
|
(get-sets [_] "get an ordered sequence of all sets in the library")
|
||||||
(get-set [_ set-name] "get one set looking for name")
|
(get-set [_ set-name] "get one set looking for name"))
|
||||||
(validate [_]))
|
|
||||||
|
|
||||||
(def schema:token-sets
|
(def schema:token-sets
|
||||||
[:and
|
[:and
|
||||||
|
@ -144,10 +144,79 @@
|
||||||
(def check-token-sets!
|
(def check-token-sets!
|
||||||
(sm/check-fn ::token-sets))
|
(sm/check-fn ::token-sets))
|
||||||
|
|
||||||
|
;; === TokenTheme
|
||||||
|
|
||||||
|
(defprotocol ITokenTheme
|
||||||
|
(toggle-set [_ set-name] "togle a set used / not used in the theme"))
|
||||||
|
|
||||||
|
(defrecord TokenTheme [name description is-source modified-at sets]
|
||||||
|
ITokenTheme
|
||||||
|
(toggle-set [_ set-name]
|
||||||
|
(TokenTheme. name
|
||||||
|
description
|
||||||
|
is-source
|
||||||
|
(dt/now)
|
||||||
|
(if (sets set-name)
|
||||||
|
(disj sets set-name)
|
||||||
|
(conj sets set-name)))))
|
||||||
|
|
||||||
|
(def schema:token-theme
|
||||||
|
[:and [:map {:title "TokenTheme"}
|
||||||
|
[:name :string]
|
||||||
|
[:description [:maybe :string]]
|
||||||
|
[:is-source :boolean]
|
||||||
|
[:modified-at ::sm/inst]
|
||||||
|
[:sets [:and [:set {:gen/max 5} :string]
|
||||||
|
[:fn d/ordered-set?]]]]
|
||||||
|
[:fn (partial instance? TokenTheme)]])
|
||||||
|
|
||||||
|
(sm/register! ::token-theme schema:token-theme)
|
||||||
|
|
||||||
|
(def valid-token-theme?
|
||||||
|
(sm/validator schema:token-theme))
|
||||||
|
|
||||||
|
(def check-token-theme!
|
||||||
|
(sm/check-fn ::token-theme))
|
||||||
|
|
||||||
|
(defn make-token-theme
|
||||||
|
[& {:keys [] :as params}]
|
||||||
|
(let [params (-> params
|
||||||
|
(dissoc :id)
|
||||||
|
(dissoc :group)
|
||||||
|
(update :is-source #(or % false))
|
||||||
|
(update :modified-at #(or % (dt/now)))
|
||||||
|
(update :sets #(into (d/ordered-set) %)))
|
||||||
|
token-theme (map->TokenTheme params)]
|
||||||
|
|
||||||
|
(dm/assert!
|
||||||
|
"expected valid token theme"
|
||||||
|
(check-token-theme! token-theme))
|
||||||
|
|
||||||
|
token-theme))
|
||||||
|
|
||||||
;; === TokenThemes (collection)
|
;; === TokenThemes (collection)
|
||||||
|
|
||||||
|
(defprotocol ITokenThemes
|
||||||
|
(add-theme [_ token-theme] "add a theme to the library, at the end")
|
||||||
|
(update-theme [_ theme-name f] "modify a theme in the ilbrary")
|
||||||
|
(delete-theme [_ theme-name] "delete a theme in the library")
|
||||||
|
(theme-count [_] "get the total number if themes in the library")
|
||||||
|
(get-themes [_] "get an ordered sequence of all themes in the library")
|
||||||
|
(get-theme [_ theme-name] "get one theme looking for name"))
|
||||||
|
|
||||||
|
(def schema:token-themes
|
||||||
|
[:and
|
||||||
|
[:map-of {:title "TokenThemes"}
|
||||||
|
:string ::token-theme]
|
||||||
|
[:fn d/ordered-map?]])
|
||||||
|
|
||||||
|
(sm/register! ::token-themes schema:token-themes)
|
||||||
|
|
||||||
(def valid-token-themes?
|
(def valid-token-themes?
|
||||||
(constantly true))
|
(sm/validator schema:token-themes))
|
||||||
|
|
||||||
|
(def check-token-themes!
|
||||||
|
(sm/check-fn ::token-themes))
|
||||||
|
|
||||||
;; === Tokens Lib
|
;; === Tokens Lib
|
||||||
|
|
||||||
|
@ -155,7 +224,9 @@
|
||||||
"A library of tokens, sets and themes."
|
"A library of tokens, sets and themes."
|
||||||
(add-token-in-set [_ set-name token] "add token to a set")
|
(add-token-in-set [_ set-name token] "add token to a set")
|
||||||
(update-token-in-set [_ set-name token-name f] "update a token in a set")
|
(update-token-in-set [_ set-name token-name f] "update a token in a set")
|
||||||
(delete-token-from-set [_ set-name token-name] "delete a token from a set"))
|
(delete-token-from-set [_ set-name token-name] "delete a token from a set")
|
||||||
|
(toggle-set-in-theme [_ theme-name set-name] "toggle a set used / not used in a theme")
|
||||||
|
(validate [_]))
|
||||||
|
|
||||||
(deftype TokensLib [sets themes]
|
(deftype TokensLib [sets themes]
|
||||||
;; NOTE: This is only for debug purposes, pending to properly
|
;; NOTE: This is only for debug purposes, pending to properly
|
||||||
|
@ -193,10 +264,6 @@
|
||||||
(TokensLib. (dissoc sets set-name)
|
(TokensLib. (dissoc sets set-name)
|
||||||
themes))
|
themes))
|
||||||
|
|
||||||
(validate [_]
|
|
||||||
(and (valid-token-sets? sets)
|
|
||||||
(valid-token-themes? themes)))
|
|
||||||
|
|
||||||
(set-count [_]
|
(set-count [_]
|
||||||
(count sets))
|
(count sets))
|
||||||
|
|
||||||
|
@ -206,6 +273,39 @@
|
||||||
(get-set [_ set-name]
|
(get-set [_ set-name]
|
||||||
(get sets set-name))
|
(get sets set-name))
|
||||||
|
|
||||||
|
ITokenThemes
|
||||||
|
(add-theme [_ token-theme]
|
||||||
|
(dm/assert! "expected valid token theme" (check-token-theme! token-theme))
|
||||||
|
(TokensLib. sets
|
||||||
|
(assoc themes (:name token-theme) token-theme)))
|
||||||
|
|
||||||
|
(update-theme [this theme-name f]
|
||||||
|
(if-let [theme (get themes theme-name)]
|
||||||
|
(let [theme' (-> (make-token-theme (f theme))
|
||||||
|
(assoc :modified-at (dt/now)))]
|
||||||
|
(check-token-theme! theme')
|
||||||
|
(TokensLib. sets
|
||||||
|
(if (= (:name theme) (:name theme'))
|
||||||
|
(assoc themes (:name theme') theme')
|
||||||
|
(let [index (d/index-of (keys themes) (:name theme))]
|
||||||
|
(-> themes
|
||||||
|
(dissoc (:name theme))
|
||||||
|
(d/addm-at-index index (:name theme') theme'))))))
|
||||||
|
this))
|
||||||
|
|
||||||
|
(delete-theme [_ theme-name]
|
||||||
|
(TokensLib. sets
|
||||||
|
(dissoc themes theme-name)))
|
||||||
|
|
||||||
|
(theme-count [_]
|
||||||
|
(count themes))
|
||||||
|
|
||||||
|
(get-themes [_]
|
||||||
|
(vals themes))
|
||||||
|
|
||||||
|
(get-theme [_ theme-name]
|
||||||
|
(get themes theme-name))
|
||||||
|
|
||||||
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))
|
||||||
|
@ -226,7 +326,18 @@
|
||||||
(TokensLib. (update sets set-name
|
(TokensLib. (update sets set-name
|
||||||
#(delete-token % token-name))
|
#(delete-token % token-name))
|
||||||
themes)
|
themes)
|
||||||
this)))
|
this))
|
||||||
|
|
||||||
|
(toggle-set-in-theme [this theme-name set-name]
|
||||||
|
(if (contains? themes theme-name)
|
||||||
|
(TokensLib. sets
|
||||||
|
(update themes theme-name
|
||||||
|
#(toggle-set % set-name)))
|
||||||
|
this))
|
||||||
|
|
||||||
|
(validate [_]
|
||||||
|
(and (valid-token-sets? sets)
|
||||||
|
(valid-token-themes? themes))))
|
||||||
|
|
||||||
(defn valid-tokens-lib?
|
(defn valid-tokens-lib?
|
||||||
[o]
|
[o]
|
||||||
|
@ -269,7 +380,7 @@
|
||||||
|
|
||||||
(sm/register! ::tokens-lib type:tokens-lib)
|
(sm/register! ::tokens-lib type:tokens-lib)
|
||||||
|
|
||||||
;; === Serialization handlers for RPC API and database
|
;; === Serialization handlers for RPC API (transit) and database (fressian)
|
||||||
|
|
||||||
(t/add-handlers!
|
(t/add-handlers!
|
||||||
{:id "penpot/tokens-lib"
|
{:id "penpot/tokens-lib"
|
||||||
|
@ -282,6 +393,11 @@
|
||||||
:wfn #(into {} %)
|
:wfn #(into {} %)
|
||||||
:rfn #(make-token-set %)}
|
:rfn #(make-token-set %)}
|
||||||
|
|
||||||
|
{:id "penpot/token-theme"
|
||||||
|
:class TokenTheme
|
||||||
|
:wfn #(into {} %)
|
||||||
|
:rfn #(make-token-theme %)}
|
||||||
|
|
||||||
{:id "penpot/token"
|
{:id "penpot/token"
|
||||||
:class Token
|
:class Token
|
||||||
:wfn #(into {} %)
|
:wfn #(into {} %)
|
||||||
|
@ -307,6 +423,15 @@
|
||||||
(let [obj (fres/read-object! r)]
|
(let [obj (fres/read-object! r)]
|
||||||
(map->TokenSet obj)))}
|
(map->TokenSet obj)))}
|
||||||
|
|
||||||
|
{:name "penpot/token-theme/v1"
|
||||||
|
:class TokenTheme
|
||||||
|
:wfn (fn [n w o]
|
||||||
|
(fres/write-tag! w n 1)
|
||||||
|
(fres/write-object! w (into {} o)))
|
||||||
|
:rfn (fn [r]
|
||||||
|
(let [obj (fres/read-object! r)]
|
||||||
|
(map->TokenTheme obj)))}
|
||||||
|
|
||||||
{:name "penpot/tokens-lib/v1"
|
{:name "penpot/tokens-lib/v1"
|
||||||
:class TokensLib
|
:class TokensLib
|
||||||
:wfn (fn [n w o]
|
:wfn (fn [n w o]
|
||||||
|
|
|
@ -19,16 +19,17 @@
|
||||||
(assoc file-data :token-active-themes theme-ids))
|
(assoc file-data :token-active-themes theme-ids))
|
||||||
|
|
||||||
(defn add-temporary-token-theme
|
(defn add-temporary-token-theme
|
||||||
[file-data {:keys [id] :as token-theme}]
|
[file-data {:keys [id name] :as token-theme}]
|
||||||
(-> file-data
|
(-> file-data
|
||||||
(d/dissoc-in [:token-themes-index (:token-theme-temporary-id file-data)])
|
(d/dissoc-in [:token-themes-index (:token-theme-temporary-id file-data)])
|
||||||
(assoc :token-theme-temporary-id id)
|
(assoc :token-theme-temporary-id id)
|
||||||
|
(assoc :token-theme-temporary-name name)
|
||||||
(update :token-themes-index assoc id token-theme)))
|
(update :token-themes-index assoc id token-theme)))
|
||||||
|
|
||||||
(defn delete-temporary-token-theme
|
(defn delete-temporary-token-theme
|
||||||
[file-data token-theme-id]
|
[file-data token-theme-id]
|
||||||
(cond-> file-data
|
(cond-> file-data
|
||||||
(= (:token-theme-temporary-id file-data) token-theme-id) (dissoc :token-theme-temporary-id)
|
(= (:token-theme-temporary-id file-data) token-theme-id) (dissoc :token-theme-temporary-id :token-theme-temporary-name)
|
||||||
:always (d/dissoc-in [:token-themes-index (:token-theme-temporary-id file-data)])))
|
:always (d/dissoc-in [:token-themes-index (:token-theme-temporary-id file-data)])))
|
||||||
|
|
||||||
(defn add-token-theme
|
(defn add-token-theme
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
[app.common.types.tokens-lib :as ctob]
|
[app.common.types.tokens-lib :as ctob]
|
||||||
[clojure.test :as t]))
|
[clojure.test :as t]))
|
||||||
|
|
||||||
|
(t/testing "token"
|
||||||
(t/deftest make-token
|
(t/deftest make-token
|
||||||
(let [now (dt/now)
|
(let [now (dt/now)
|
||||||
token1 (ctob/make-token :name "test-token-1"
|
token1 (ctob/make-token :name "test-token-1"
|
||||||
|
@ -43,8 +44,10 @@
|
||||||
:type :invalid}]
|
:type :invalid}]
|
||||||
(t/is (thrown-with-msg? Exception #"expected valid token"
|
(t/is (thrown-with-msg? Exception #"expected valid token"
|
||||||
(apply ctob/make-token args)))
|
(apply ctob/make-token args)))
|
||||||
(t/is (false? (ctob/valid-token? {})))))
|
(t/is (false? (ctob/valid-token? {}))))))
|
||||||
|
|
||||||
|
|
||||||
|
(t/testing "token-set"
|
||||||
(t/deftest make-token-set
|
(t/deftest make-token-set
|
||||||
(let [now (dt/now)
|
(let [now (dt/now)
|
||||||
token-set1 (ctob/make-token-set :name "test-token-set-1")
|
token-set1 (ctob/make-token-set :name "test-token-set-1")
|
||||||
|
@ -67,12 +70,52 @@
|
||||||
(let [args {:name 777
|
(let [args {:name 777
|
||||||
:description 999}]
|
:description 999}]
|
||||||
(t/is (thrown-with-msg? Exception #"expected valid token set"
|
(t/is (thrown-with-msg? Exception #"expected valid token set"
|
||||||
(apply ctob/make-token-set args)))))
|
(apply ctob/make-token-set args))))))
|
||||||
|
|
||||||
|
|
||||||
|
(t/testing "token-theme"
|
||||||
|
(t/deftest make-token-theme
|
||||||
|
(let [now (dt/now)
|
||||||
|
token-theme1 (ctob/make-token-theme :name "test-token-theme-1")
|
||||||
|
token-theme2 (ctob/make-token-theme :name "test-token-theme-2"
|
||||||
|
:description "test description"
|
||||||
|
:is-source true
|
||||||
|
:modified-at now
|
||||||
|
:sets #{})]
|
||||||
|
|
||||||
|
(t/is (= (:name token-theme1) "test-token-theme-1"))
|
||||||
|
(t/is (nil? (:description token-theme1)))
|
||||||
|
(t/is (false? (:is-source token-theme1)))
|
||||||
|
(t/is (some? (:modified-at token-theme1)))
|
||||||
|
(t/is (empty? (:sets token-theme1)))
|
||||||
|
|
||||||
|
(t/is (= (:name token-theme2) "test-token-theme-2"))
|
||||||
|
(t/is (= (:description token-theme2) "test description"))
|
||||||
|
(t/is (true? (:is-source token-theme2)))
|
||||||
|
(t/is (= (:modified-at token-theme2) now))
|
||||||
|
(t/is (empty? (:sets token-theme2)))))
|
||||||
|
|
||||||
|
(t/deftest invalid-token-theme
|
||||||
|
(let [args {:name 777
|
||||||
|
:description 999
|
||||||
|
:is-source 42}]
|
||||||
|
(t/is (thrown-with-msg? Exception #"expected valid token theme"
|
||||||
|
(apply ctob/make-token-theme args))))))
|
||||||
|
|
||||||
|
|
||||||
|
(t/testing "tokens-lib"
|
||||||
(t/deftest make-tokens-lib
|
(t/deftest make-tokens-lib
|
||||||
(let [tokens-lib (ctob/make-tokens-lib)]
|
(let [tokens-lib (ctob/make-tokens-lib)]
|
||||||
(t/is (= (ctob/set-count tokens-lib) 0))))
|
(t/is (= (ctob/set-count tokens-lib) 0))))
|
||||||
|
|
||||||
|
(t/deftest invalid-tokens-lib
|
||||||
|
(let [args {:sets nil
|
||||||
|
:themes nil}]
|
||||||
|
(t/is (thrown-with-msg? Exception #"expected valid tokens lib"
|
||||||
|
(apply ctob/make-tokens-lib args))))))
|
||||||
|
|
||||||
|
|
||||||
|
(t/testing "token-set in a lib"
|
||||||
(t/deftest add-token-set
|
(t/deftest add-token-set
|
||||||
(let [tokens-lib (ctob/make-tokens-lib)
|
(let [tokens-lib (ctob/make-tokens-lib)
|
||||||
token-set (ctob/make-token-set :name "test-token-set")
|
token-set (ctob/make-token-set :name "test-token-set")
|
||||||
|
@ -93,19 +136,35 @@
|
||||||
(ctob/update-set "test-token-set"
|
(ctob/update-set "test-token-set"
|
||||||
(fn [token-set]
|
(fn [token-set]
|
||||||
(assoc token-set
|
(assoc token-set
|
||||||
:name "updated-name"
|
|
||||||
:description "some description")))
|
:description "some description")))
|
||||||
(ctob/update-set "not-existing-set"
|
(ctob/update-set "not-existing-set"
|
||||||
(fn [token-set]
|
(fn [token-set]
|
||||||
(assoc token-set
|
(assoc token-set
|
||||||
:name "no-effect"))))
|
:description "no-effect"))))
|
||||||
|
|
||||||
|
token-set (ctob/get-set tokens-lib "test-token-set")
|
||||||
|
token-set' (ctob/get-set tokens-lib' "test-token-set")]
|
||||||
|
|
||||||
|
(t/is (= (ctob/set-count tokens-lib') 1))
|
||||||
|
(t/is (= (:name token-set') "test-token-set"))
|
||||||
|
(t/is (= (:description token-set') "some description"))
|
||||||
|
(t/is (dt/is-after? (:modified-at token-set') (:modified-at token-set)))))
|
||||||
|
|
||||||
|
(t/deftest rename-token-set
|
||||||
|
(let [tokens-lib (-> (ctob/make-tokens-lib)
|
||||||
|
(ctob/add-set (ctob/make-token-set :name "test-token-set")))
|
||||||
|
|
||||||
|
tokens-lib' (-> tokens-lib
|
||||||
|
(ctob/update-set "test-token-set"
|
||||||
|
(fn [token-set]
|
||||||
|
(assoc token-set
|
||||||
|
:name "updated-name"))))
|
||||||
|
|
||||||
token-set (ctob/get-set tokens-lib "test-token-set")
|
token-set (ctob/get-set tokens-lib "test-token-set")
|
||||||
token-set' (ctob/get-set tokens-lib' "updated-name")]
|
token-set' (ctob/get-set tokens-lib' "updated-name")]
|
||||||
|
|
||||||
(t/is (= (ctob/set-count tokens-lib') 1))
|
(t/is (= (ctob/set-count tokens-lib') 1))
|
||||||
(t/is (= (:name token-set') "updated-name"))
|
(t/is (= (:name token-set') "updated-name"))
|
||||||
(t/is (= (:description token-set') "some description"))
|
|
||||||
(t/is (dt/is-after? (:modified-at token-set') (:modified-at token-set)))))
|
(t/is (dt/is-after? (:modified-at token-set') (:modified-at token-set)))))
|
||||||
|
|
||||||
(t/deftest delete-token-set
|
(t/deftest delete-token-set
|
||||||
|
@ -119,8 +178,10 @@
|
||||||
token-set' (ctob/get-set tokens-lib' "updated-name")]
|
token-set' (ctob/get-set tokens-lib' "updated-name")]
|
||||||
|
|
||||||
(t/is (= (ctob/set-count tokens-lib') 0))
|
(t/is (= (ctob/set-count tokens-lib') 0))
|
||||||
(t/is (nil? token-set'))))
|
(t/is (nil? token-set')))))
|
||||||
|
|
||||||
|
|
||||||
|
(t/testing "token in a lib"
|
||||||
(t/deftest add-token
|
(t/deftest add-token
|
||||||
(let [tokens-lib (-> (ctob/make-tokens-lib)
|
(let [tokens-lib (-> (ctob/make-tokens-lib)
|
||||||
(ctob/add-set (ctob/make-token-set :name "test-token-set")))
|
(ctob/add-set (ctob/make-token-set :name "test-token-set")))
|
||||||
|
@ -232,28 +293,119 @@
|
||||||
(t/is (= (ctob/set-count tokens-lib') 1))
|
(t/is (= (ctob/set-count tokens-lib') 1))
|
||||||
(t/is (= (count (:tokens token-set')) 0))
|
(t/is (= (count (:tokens token-set')) 0))
|
||||||
(t/is (nil? token'))
|
(t/is (nil? token'))
|
||||||
(t/is (dt/is-after? (:modified-at token-set') (:modified-at token-set)))))
|
(t/is (dt/is-after? (:modified-at token-set') (:modified-at token-set))))))
|
||||||
|
|
||||||
|
|
||||||
|
(t/testing "token-theme in a lib"
|
||||||
|
(t/deftest add-token-theme
|
||||||
|
(let [tokens-lib (ctob/make-tokens-lib)
|
||||||
|
token-theme (ctob/make-token-theme :name "test-token-theme")
|
||||||
|
tokens-lib' (ctob/add-theme tokens-lib token-theme)
|
||||||
|
|
||||||
|
token-themes' (ctob/get-themes tokens-lib')
|
||||||
|
token-theme' (ctob/get-theme tokens-lib' "test-token-theme")]
|
||||||
|
|
||||||
|
(prn "lib" tokens-lib')
|
||||||
|
(t/is (= (ctob/theme-count tokens-lib') 1))
|
||||||
|
(t/is (= (first token-themes') token-theme))
|
||||||
|
(t/is (= token-theme' token-theme))))
|
||||||
|
|
||||||
|
(t/deftest update-token-theme
|
||||||
|
(let [tokens-lib (-> (ctob/make-tokens-lib)
|
||||||
|
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme")))
|
||||||
|
|
||||||
|
tokens-lib' (-> tokens-lib
|
||||||
|
(ctob/update-theme "test-token-theme"
|
||||||
|
(fn [token-theme]
|
||||||
|
(assoc token-theme
|
||||||
|
:description "some description")))
|
||||||
|
(ctob/update-theme "not-existing-theme"
|
||||||
|
(fn [token-theme]
|
||||||
|
(assoc token-theme
|
||||||
|
:description "no-effect"))))
|
||||||
|
|
||||||
|
token-theme (ctob/get-theme tokens-lib "test-token-theme")
|
||||||
|
token-theme' (ctob/get-theme tokens-lib' "test-token-theme")]
|
||||||
|
|
||||||
|
(t/is (= (ctob/theme-count tokens-lib') 1))
|
||||||
|
(t/is (= (:name token-theme') "test-token-theme"))
|
||||||
|
(t/is (= (:description token-theme') "some description"))
|
||||||
|
(t/is (dt/is-after? (:modified-at token-theme') (:modified-at token-theme)))))
|
||||||
|
|
||||||
|
(t/deftest rename-token-theme
|
||||||
|
(let [tokens-lib (-> (ctob/make-tokens-lib)
|
||||||
|
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme")))
|
||||||
|
|
||||||
|
tokens-lib' (-> tokens-lib
|
||||||
|
(ctob/update-theme "test-token-theme"
|
||||||
|
(fn [token-theme]
|
||||||
|
(assoc token-theme
|
||||||
|
:name "updated-name"))))
|
||||||
|
|
||||||
|
token-theme (ctob/get-theme tokens-lib "test-token-theme")
|
||||||
|
token-theme' (ctob/get-theme tokens-lib' "updated-name")]
|
||||||
|
|
||||||
|
(t/is (= (ctob/theme-count tokens-lib') 1))
|
||||||
|
(t/is (= (:name token-theme') "updated-name"))
|
||||||
|
(t/is (dt/is-after? (:modified-at token-theme') (:modified-at token-theme)))))
|
||||||
|
|
||||||
|
(t/deftest delete-token-theme
|
||||||
|
(let [tokens-lib (-> (ctob/make-tokens-lib)
|
||||||
|
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme")))
|
||||||
|
|
||||||
|
tokens-lib' (-> tokens-lib
|
||||||
|
(ctob/delete-theme "test-token-theme")
|
||||||
|
(ctob/delete-theme "not-existing-theme"))
|
||||||
|
|
||||||
|
token-theme' (ctob/get-theme tokens-lib' "updated-name")]
|
||||||
|
|
||||||
|
(t/is (= (ctob/theme-count tokens-lib') 0))
|
||||||
|
(t/is (nil? token-theme'))))
|
||||||
|
|
||||||
|
(t/deftest toggle-set-in-theme
|
||||||
|
(let [tokens-lib (-> (ctob/make-tokens-lib)
|
||||||
|
(ctob/add-set (ctob/make-token-set :name "token-set-1"))
|
||||||
|
(ctob/add-set (ctob/make-token-set :name "token-set-2"))
|
||||||
|
(ctob/add-set (ctob/make-token-set :name "token-set-3"))
|
||||||
|
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme")))
|
||||||
|
tokens-lib' (-> tokens-lib
|
||||||
|
(ctob/toggle-set-in-theme "test-token-theme" "token-set-1")
|
||||||
|
(ctob/toggle-set-in-theme "test-token-theme" "token-set-2")
|
||||||
|
(ctob/toggle-set-in-theme "test-token-theme" "token-set-2"))
|
||||||
|
|
||||||
|
token-theme (ctob/get-theme tokens-lib "test-token-theme")
|
||||||
|
token-theme' (ctob/get-theme tokens-lib' "test-token-theme")]
|
||||||
|
|
||||||
|
(t/is (dt/is-after? (:modified-at token-theme') (:modified-at token-theme))))))
|
||||||
|
|
||||||
|
|
||||||
|
(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)
|
||||||
(ctob/add-set (ctob/make-token-set :name "test-token-set"))
|
(ctob/add-set (ctob/make-token-set :name "test-token-set"))
|
||||||
(ctob/add-token-in-set "test-token-set" (ctob/make-token :name "test-token"
|
(ctob/add-token-in-set "test-token-set" (ctob/make-token :name "test-token"
|
||||||
:type :boolean
|
:type :boolean
|
||||||
:value true)))
|
:value true))
|
||||||
|
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme"))
|
||||||
|
(ctob/toggle-set-in-theme "test-token-theme" "test-token-set"))
|
||||||
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/deftest fressian-serialization
|
(t/deftest fressian-serialization
|
||||||
(let [tokens-lib (-> (ctob/make-tokens-lib)
|
(let [tokens-lib (-> (ctob/make-tokens-lib)
|
||||||
(ctob/add-set (ctob/make-token-set :name "test-token-set"))
|
(ctob/add-set (ctob/make-token-set :name "test-token-set"))
|
||||||
(ctob/add-token-in-set "test-token-set" (ctob/make-token :name "test-token"
|
(ctob/add-token-in-set "test-token-set" (ctob/make-token :name "test-token"
|
||||||
:type :boolean
|
:type :boolean
|
||||||
:value true)))
|
:value true))
|
||||||
|
(ctob/add-theme (ctob/make-token-theme :name "test-token-theme"))
|
||||||
|
(ctob/toggle-set-in-theme "test-token-theme" "test-token-set"))
|
||||||
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)))))
|
||||||
|
|
|
@ -115,10 +115,9 @@
|
||||||
(ptk/reify ::update-token-theme
|
(ptk/reify ::update-token-theme
|
||||||
ptk/WatchEvent
|
ptk/WatchEvent
|
||||||
(watch [it state _]
|
(watch [it state _]
|
||||||
(let [prev-token-theme (wtts/get-workspace-token-theme state (:id token-theme))
|
(let [prev-token-theme (wtts/get-workspace-token-theme (:id token-theme) state)
|
||||||
changes (-> (pcb/empty-changes it)
|
changes (-> (pcb/empty-changes it)
|
||||||
(pcb/update-token-theme token-theme prev-token-theme))]
|
(pcb/update-token-theme token-theme prev-token-theme))]
|
||||||
(js/console.log "changes" changes)
|
|
||||||
(rx/of
|
(rx/of
|
||||||
(dch/commit-changes changes))))))
|
(dch/commit-changes changes))))))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue