Merge pull request #323 from tokens-studio/named-set-groups

♻ Remove TokenSetGroup [*]
This commit is contained in:
Florian Schrödl 2024-11-13 10:27:27 +01:00 committed by GitHub
commit 83e34f5fff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 112 additions and 156 deletions

View file

@ -1049,13 +1049,10 @@
(defmethod process-change :mod-token-set (defmethod process-change :mod-token-set
[data {:keys [name token-set]}] [data {:keys [name token-set]}]
(update data :tokens-lib (fn [lib] (update data :tokens-lib (fn [lib]
(let [path-changed? (not= name (:name token-set)) (-> lib
lib' (-> lib (ctob/ensure-tokens-lib)
(ctob/ensure-tokens-lib) (ctob/update-set name (fn [prev-set]
(ctob/update-set name (fn [prev-set] (merge prev-set (dissoc token-set :tokens))))))))
(merge prev-set (dissoc token-set :tokens)))))]
(cond-> lib'
path-changed? (ctob/update-set-name name (:name token-set)))))))
(defmethod process-change :move-token-set-before (defmethod process-change :move-token-set-before
[data {:keys [set-name before-set-name]}] [data {:keys [set-name before-set-name]}]

View file

@ -59,7 +59,7 @@
(join-path separator)))) (join-path separator))))
(defn get-path (defn get-path
"Get the groups part of the name as a vector. E.g. group.subgroup.name -> ['group' 'subrgoup']" "Get the groups part of the name as a vector. E.g. group.subgroup.name -> ['group' 'subgroup']"
[item separator] [item separator]
(dm/assert! (dm/assert!
"expected groupable item" "expected groupable item"
@ -67,7 +67,7 @@
(split-path (:name item) separator)) (split-path (:name item) separator))
(defn get-groups-str (defn get-groups-str
"Get the groups part of the name. E.g. group.subgroup.name -> group.subrgoup" "Get the groups part of the name. E.g. group.subgroup.name -> group.subgroup"
[item separator] [item separator]
(-> (get-path item separator) (-> (get-path item separator)
(butlast) (butlast)
@ -177,16 +177,35 @@
;; === Token Set ;; === Token Set
(def set-prefix "S-")
(def set-group-prefix "G-")
(def set-separator "/") (def set-separator "/")
(defn get-token-set-path [path] (defn add-set-prefix [set-name]
(get-path path set-separator)) (str set-prefix set-name))
(defn get-token-set-group-str [path] (defn add-set-group-prefix [group-path]
(get-groups-str path set-separator)) (str set-group-prefix group-path))
(defn split-token-set-path [path] (defn add-token-set-paths-prefix
(split-path path set-separator)) "Returns token-set paths with prefixes to differentiate between sets and set-groups.
Sets will be prefixed with `set-prefix` (S-).
Set groups will be prefixed with `set-group-prefix` (G-)."
[paths]
(let [set-path (mapv add-set-group-prefix (butlast paths))
set-name (add-set-prefix (last paths))]
(conj set-path set-name)))
(defn split-token-set-name [token-set-name]
(-> (split-path token-set-name set-separator)
(add-token-set-paths-prefix)))
(defn get-token-set-path [token-set]
(let [path (get-path token-set set-separator)]
(add-token-set-paths-prefix path)))
(defn tokens-tree (defn tokens-tree
"Convert tokens into a nested tree with their `:name` as the path. "Convert tokens into a nested tree with their `:name` as the path.
@ -299,20 +318,11 @@
token-set)) token-set))
;; === TokenSetGroup
(defrecord TokenSetGroup [attr1 attr2])
;; TODO schema, validators, etc.
(defn make-token-set-group
[]
(TokenSetGroup. "one" "two"))
;; === TokenSets (collection) ;; === TokenSets (collection)
(defprotocol ITokenSets (defprotocol ITokenSets
(add-set [_ token-set] "add a set to the library, at the end") (add-set [_ token-set] "add a set to the library, at the end")
(add-sets [_ token-set] "add a collection of sets to the library, at the end")
(update-set [_ set-name f] "modify a set in the ilbrary") (update-set [_ set-name f] "modify a set in the ilbrary")
(delete-set [_ set-name] "delete a set in the library") (delete-set [_ set-name] "delete a set in the library")
(move-set-before [_ set-name before-set-name] "move a set with `set-name` before a set with `before-set-name` in the library. (move-set-before [_ set-name before-set-name] "move a set with `set-name` before a set with `before-set-name` in the library.
@ -322,8 +332,7 @@ When `before-set-name` is nil, move set to bottom")
(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-ordered-set-names [_] "get an ordered sequence of all sets names in the library") (get-ordered-set-names [_] "get an ordered sequence of all sets names in the library")
(get-set [_ set-name] "get one set looking for name") (get-set [_ set-name] "get one set looking for name")
(get-neighbor-set-name [_ set-name index-offset] "get neighboring set name offset by `index-offset`") (get-neighbor-set-name [_ set-name index-offset] "get neighboring set name offset by `index-offset`"))
(get-set-group [_ set-group-path] "get the attributes of a set group"))
(def schema:token-set-node (def schema:token-set-node
[:schema {:registry {::node [:or ::token-set [:schema {:registry {::node [:or ::token-set
@ -518,6 +527,8 @@ When `before-set-name` is nil, move set to bottom")
;; === Tokens Lib ;; === Tokens Lib
(declare make-tokens-lib)
(defprotocol ITokensLib (defprotocol ITokensLib
"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")
@ -526,66 +537,62 @@ When `before-set-name` is nil, move set to bottom")
(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")
(get-active-themes-set-names [_] "set of set names that are active in the the active themes") (get-active-themes-set-names [_] "set of set names that are active in the the active themes")
(get-active-themes-set-tokens [_] "set of set names that are active in the the active themes") (get-active-themes-set-tokens [_] "set of set names that are active in the the active themes")
(update-set-name [_ old-set-name new-set-name] "updates set name in themes")
(encode-dtcg [_] "Encodes library to a dtcg compatible json string") (encode-dtcg [_] "Encodes library to a dtcg compatible json string")
(decode-dtcg-json [_ parsed-json] "Decodes parsed json containing tokens and converts to library") (decode-dtcg-json [_ parsed-json] "Decodes parsed json containing tokens and converts to library")
(get-all-tokens [_] "all tokens in the lib") (get-all-tokens [_] "all tokens in the lib")
(validate [_])) (validate [_]))
(deftype TokensLib [sets set-groups themes active-themes] (deftype TokensLib [sets themes active-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 :themes themes
:active-themes active-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 :themes themes
:active-themes active-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)
"themes" (clj->js themes) "themes" (clj->js themes)
"active-themes" (clj->js active-themes)))]) "active-themes" (clj->js active-themes)))])
ITokenSets ITokenSets
(add-set [_ token-set] (add-set [_ token-set]
(dm/assert! "expected valid token set" (check-token-set! token-set)) (dm/assert! "expected valid token set" (check-token-set! token-set))
(let [path (get-token-set-path token-set) (let [path (get-token-set-path token-set)]
groups-str (get-token-set-group-str token-set)]
(TokensLib. (d/oassoc-in sets path token-set) (TokensLib. (d/oassoc-in sets path token-set)
(cond-> set-groups
(not (str/empty? groups-str))
(assoc groups-str (make-token-set-group)))
themes themes
active-themes))) active-themes)))
(add-sets [this token-sets]
(reduce
(fn [lib set]
(add-set lib set))
this token-sets))
(update-set [this set-name f] (update-set [this set-name f]
(let [path (split-token-set-path set-name) (let [path (split-token-set-name set-name)
set (get-in sets path)] set (get-in sets path)]
(if set (if set
(let [set' (-> (make-token-set (f set)) (let [set' (-> (make-token-set (f set))
(assoc :modified-at (dt/now))) (assoc :modified-at (dt/now)))
path' (get-path set' "/")] path' (get-token-set-path set')]
(check-token-set! set') (check-token-set! set')
(TokensLib. (if (= (:name set) (:name set')) (TokensLib. (if (= (:name set) (:name set'))
(d/oassoc-in sets path set') (d/oassoc-in sets path set')
(-> sets (-> sets
(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
themes themes
active-themes)) active-themes))
this))) this)))
(delete-set [_ set-name] (delete-set [_ set-name]
(let [path (split-token-set-path set-name)] (let [path (split-token-set-name set-name)]
(TokensLib. (d/dissoc-in sets path) (TokensLib. (d/dissoc-in sets path)
set-groups ;; TODO remove set-group if needed
(walk/postwalk (walk/postwalk
(fn [form] (fn [form]
(if (instance? TokenTheme form) (if (instance? TokenTheme form)
@ -596,19 +603,17 @@ When `before-set-name` is nil, move set to bottom")
;; TODO Handle groups and nesting ;; TODO Handle groups and nesting
(move-set-before [this set-name before-set-name] (move-set-before [this set-name before-set-name]
(let [source-path (split-token-set-path set-name) (let [source-path (split-token-set-name set-name)
token-set (-> (get-set this set-name) token-set (-> (get-set this set-name)
(assoc :modified-at (dt/now))) (assoc :modified-at (dt/now)))
target-path (split-token-set-path before-set-name)] target-path (split-token-set-name before-set-name)]
(if before-set-name (if before-set-name
(TokensLib. (d/oassoc-in-before sets target-path source-path token-set) (TokensLib. (d/oassoc-in-before sets target-path source-path token-set)
set-groups ;; TODO remove set-group if needed
themes themes
active-themes) active-themes)
(TokensLib. (-> sets (TokensLib. (-> sets
(d/dissoc-in source-path) (d/dissoc-in source-path)
(d/oassoc-in source-path token-set)) (d/oassoc-in source-path token-set))
set-groups ;; TODO remove set-group if needed
themes themes
active-themes)))) active-themes))))
@ -626,7 +631,7 @@ When `before-set-name` is nil, move set to bottom")
(count (get-sets this))) (count (get-sets this)))
(get-set [_ set-name] (get-set [_ set-name]
(let [path (split-path set-name "/")] (let [path (split-token-set-name set-name)]
(get-in sets path))) (get-in sets path)))
(get-neighbor-set-name [this set-name index-offset] (get-neighbor-set-name [this set-name index-offset]
@ -636,14 +641,10 @@ When `before-set-name` is nil, move set to bottom")
(nth sets (+ index-offset index) nil))] (nth sets (+ index-offset index) nil))]
neighbor-set-name)) neighbor-set-name))
(get-set-group [_ set-group-path]
(get set-groups set-group-path))
ITokenThemes ITokenThemes
(add-theme [_ token-theme] (add-theme [_ token-theme]
(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
(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)) active-themes))
@ -659,7 +660,6 @@ When `before-set-name` is nil, move set to bottom")
same-path? (and same-group? same-name?)] same-path? (and same-group? same-name?)]
(check-token-theme! theme') (check-token-theme! theme')
(TokensLib. sets (TokensLib. sets
set-groups
(if same-path? (if same-path?
(update themes group' assoc name' theme') (update themes group' assoc name' theme')
(-> themes (-> themes
@ -672,7 +672,6 @@ When `before-set-name` is nil, move set to bottom")
(delete-theme [_ group name] (delete-theme [_ group name]
(TokensLib. sets (TokensLib. sets
set-groups
(d/dissoc-in themes [group name]) (d/dissoc-in themes [group name])
(disj active-themes (token-theme-path group name)))) (disj active-themes (token-theme-path group name))))
@ -697,7 +696,6 @@ When `before-set-name` is nil, move set to bottom")
(set-active-themes [_ active-themes] (set-active-themes [_ active-themes]
(TokensLib. sets (TokensLib. sets
set-groups
themes themes
active-themes)) active-themes))
@ -709,14 +707,12 @@ When `before-set-name` is nil, move set to bottom")
active-themes' (-> (set/difference active-themes group-themes) active-themes' (-> (set/difference active-themes group-themes)
(conj (theme-path theme)))] (conj (theme-path theme)))]
(TokensLib. sets (TokensLib. sets
set-groups
themes themes
active-themes')) active-themes'))
this)) this))
(deactivate-theme [_ group name] (deactivate-theme [_ group name]
(TokensLib. sets (TokensLib. sets
set-groups
themes themes
(disj active-themes (token-theme-path group name)))) (disj active-themes (token-theme-path group name))))
@ -742,35 +738,17 @@ When `before-set-name` is nil, move set to bottom")
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) (update-set this set-name #(add-token % token)))
(TokensLib. (update sets set-name add-token token)
set-groups
themes
active-themes)
this))
(update-token-in-set [this set-name token-name f] (update-token-in-set [this set-name token-name f]
(if (contains? sets set-name) (update-set this set-name #(update-token % token-name f)))
(TokensLib. (update sets set-name
#(update-token % token-name f))
set-groups
themes
active-themes)
this))
(delete-token-from-set [this set-name token-name] (delete-token-from-set [this set-name token-name]
(if (contains? sets set-name) (update-set this set-name #(delete-token % token-name)))
(TokensLib. (update sets set-name
#(delete-token % token-name))
set-groups
themes
active-themes)
this))
(toggle-set-in-theme [this theme-group theme-name set-name] (toggle-set-in-theme [this theme-group theme-name set-name]
(if-let [_theme (get-in themes theme-group theme-name)] (if-let [_theme (get-in themes theme-group theme-name)]
(TokensLib. sets (TokensLib. sets
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) active-themes)
@ -794,38 +772,24 @@ When `before-set-name` is nil, move set to bottom")
tokens (order-theme-set theme))) tokens (order-theme-set theme)))
(d/ordered-map) active-themes))) (d/ordered-map) active-themes)))
;; TODO Move to `update-set`
(update-set-name [_ old-set-name new-set-name]
(TokensLib. sets
set-groups
(walk/postwalk
(fn [form]
(if (instance? TokenTheme form)
(-> form
(update :sets disj old-set-name)
(update :sets conj new-set-name))
form))
themes)
active-themes))
(encode-dtcg [_] (encode-dtcg [_]
(into {} (map (fn [[k v]] (into {} (comp
[k (get-dtcg-tokens-tree v)]) (filter (partial instance? TokenSet))
sets))) (map (fn [token-set]
[(:name token-set) (get-dtcg-tokens-tree token-set)])))
(tree-seq d/ordered-map? vals sets)))
(decode-dtcg-json [_ parsed-json] (decode-dtcg-json [_ parsed-json]
(let [token-sets (into (d/ordered-map) (let [;; tokens-studio/plugin will add these meta properties, remove them for now
(map (fn [[set-name tokens]] sets-data (dissoc parsed-json "$themes" "$metadata")
[set-name (make-token-set lib (make-tokens-lib)
:name set-name lib' (reduce
:tokens (flatten-nested-tokens-json tokens ""))])) (fn [lib [set-name tokens]]
(-> parsed-json (add-set lib (make-token-set
;; tokens-studio/plugin will add these meta properties, remove them for now :name set-name
(dissoc "$themes" "$metadata")))] :tokens (flatten-nested-tokens-json tokens ""))))
(TokensLib. token-sets lib sets-data)]
set-groups lib'))
themes
active-themes)))
(get-all-tokens [this] (get-all-tokens [this]
(reduce (reduce
@ -834,7 +798,7 @@ When `before-set-name` is nil, move set to bottom")
{} (get-sets this))) {} (get-sets this)))
(validate [_] (validate [_]
(and (valid-token-sets? sets) ;; TODO: validate set-groups (and (valid-token-sets? sets)
(valid-token-themes? themes) (valid-token-themes? themes)
(valid-active-token-themes? active-themes)))) (valid-active-token-themes? active-themes))))
@ -858,12 +822,11 @@ When `before-set-name` is nil, move set to bottom")
;; structure the data and the order separately as we already do ;; structure the data and the order separately as we already do
;; 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 {}
:themes (d/ordered-map) :themes (d/ordered-map)
:active-themes #{})) :active-themes #{}))
([& {:keys [sets set-groups themes active-themes]}] ([& {:keys [sets themes active-themes]}]
(let [tokens-lib (TokensLib. sets set-groups themes (or active-themes #{}))] (let [tokens-lib (TokensLib. sets themes (or active-themes #{}))]
(dm/assert! (dm/assert!
"expected valid tokens lib" "expected valid tokens lib"
@ -934,16 +897,29 @@ When `before-set-name` is nil, move set to bottom")
(map->TokenTheme obj)))} (map->TokenTheme obj)))}
{:name "penpot/tokens-lib/v1" {:name "penpot/tokens-lib/v1"
:rfn (fn [r]
(let [;; Migrate sets tree without prefix to new format
prev-sets (->> (fres/read-object! r)
(tree-seq d/ordered-map? vals)
(filter (partial instance? TokenSet)))
sets (-> (make-tokens-lib)
(add-sets prev-sets)
(deref)
:sets)
_set-groups (fres/read-object! r)
themes (fres/read-object! r)
active-themes (fres/read-object! r)]
(->TokensLib sets themes active-themes)))}
{:name "penpot/tokens-lib/v1.1"
:class TokensLib :class TokensLib
:wfn (fn [n w o] :wfn (fn [n w o]
(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 (.-themes o)) (fres/write-object! w (.-themes o))
(fres/write-object! w (.-active-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)
themes (fres/read-object! r) themes (fres/read-object! r)
active-themes (fres/read-object! r)] active-themes (fres/read-object! r)]
(->TokensLib sets set-groups themes active-themes)))})) (->TokensLib sets themes active-themes)))}))

View file

@ -184,16 +184,6 @@
(t/is (= (first token-sets') token-set)) (t/is (= (first token-sets') token-set))
(t/is (= token-set' token-set)))) (t/is (= token-set' token-set))))
(t/deftest add-token-set-with-group
(let [tokens-lib (ctob/make-tokens-lib)
token-set (ctob/make-token-set :name "test-group/test-token-set")
tokens-lib' (ctob/add-set tokens-lib token-set)
set-group (ctob/get-set-group tokens-lib' "test-group")]
(t/is (= (:attr1 set-group) "one"))
(t/is (= (:attr2 set-group) "two"))))
(t/deftest update-token-set (t/deftest update-token-set
(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")))
@ -758,31 +748,31 @@
(t/is (= (:name (nth sets-list 3)) "group1/subgroup11/token-set-4")) (t/is (= (:name (nth sets-list 3)) "group1/subgroup11/token-set-4"))
(t/is (= (:name (nth sets-list 4)) "group2/token-set-5")) (t/is (= (:name (nth sets-list 4)) "group2/token-set-5"))
(t/is (= (first node-set1) "token-set-1")) (t/is (= (first node-set1) "S-token-set-1"))
(t/is (= (ctob/group? (second node-set1)) false)) (t/is (= (ctob/group? (second node-set1)) false))
(t/is (= (:name (second node-set1)) "token-set-1")) (t/is (= (:name (second node-set1)) "token-set-1"))
(t/is (= (first node-group1) "group1")) (t/is (= (first node-group1) "G-group1"))
(t/is (= (ctob/group? (second node-group1)) true)) (t/is (= (ctob/group? (second node-group1)) true))
(t/is (= (count (second node-group1)) 3)) (t/is (= (count (second node-group1)) 3))
(t/is (= (first node-set2) "token-set-2")) (t/is (= (first node-set2) "S-token-set-2"))
(t/is (= (ctob/group? (second node-set2)) false)) (t/is (= (ctob/group? (second node-set2)) false))
(t/is (= (:name (second node-set2)) "group1/token-set-2")) (t/is (= (:name (second node-set2)) "group1/token-set-2"))
(t/is (= (first node-set3) "token-set-3")) (t/is (= (first node-set3) "S-token-set-3"))
(t/is (= (ctob/group? (second node-set3)) false)) (t/is (= (ctob/group? (second node-set3)) false))
(t/is (= (:name (second node-set3)) "group1/token-set-3")) (t/is (= (:name (second node-set3)) "group1/token-set-3"))
(t/is (= (first node-subgroup11) "subgroup11")) (t/is (= (first node-subgroup11) "G-subgroup11"))
(t/is (= (ctob/group? (second node-subgroup11)) true)) (t/is (= (ctob/group? (second node-subgroup11)) true))
(t/is (= (count (second node-subgroup11)) 1)) (t/is (= (count (second node-subgroup11)) 1))
(t/is (= (first node-set4) "token-set-4")) (t/is (= (first node-set4) "S-token-set-4"))
(t/is (= (ctob/group? (second node-set4)) false)) (t/is (= (ctob/group? (second node-set4)) false))
(t/is (= (:name (second node-set4)) "group1/subgroup11/token-set-4")) (t/is (= (:name (second node-set4)) "group1/subgroup11/token-set-4"))
(t/is (= (first node-set5) "token-set-5")) (t/is (= (first node-set5) "S-token-set-5"))
(t/is (= (ctob/group? (second node-set5)) false)) (t/is (= (ctob/group? (second node-set5)) false))
(t/is (= (:name (second node-set5)) "group2/token-set-5")))) (t/is (= (:name (second node-set5)) "group2/token-set-5"))))
@ -801,13 +791,13 @@
sets-tree (ctob/get-set-tree tokens-lib) sets-tree (ctob/get-set-tree tokens-lib)
sets-tree' (ctob/get-set-tree tokens-lib') sets-tree' (ctob/get-set-tree tokens-lib')
group1' (get sets-tree' "group1") group1' (get sets-tree' "G-group1")
token-set (get-in sets-tree ["group1" "token-set-2"]) token-set (get-in sets-tree ["G-group1" "S-token-set-2"])
token-set' (get-in sets-tree' ["group1" "token-set-2"])] token-set' (get-in sets-tree' ["G-group1" "S-token-set-2"])]
(t/is (= (ctob/set-count tokens-lib') 5)) (t/is (= (ctob/set-count tokens-lib') 5))
(t/is (= (count group1') 3)) (t/is (= (count group1') 3))
(t/is (= (d/index-of (keys group1') "token-set-2") 0)) (t/is (= (d/index-of (keys group1') "S-token-set-2") 0))
(t/is (= (:name token-set') "group1/token-set-2")) (t/is (= (:name token-set') "group1/token-set-2"))
(t/is (= (:description token-set') "some description")) (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)))))
@ -828,16 +818,18 @@
sets-tree (ctob/get-set-tree tokens-lib) sets-tree (ctob/get-set-tree tokens-lib)
sets-tree' (ctob/get-set-tree tokens-lib') sets-tree' (ctob/get-set-tree tokens-lib')
group1' (get sets-tree' "group1") group1' (get sets-tree' "G-group1")
token-set (get-in sets-tree ["group1" "token-set-2"]) token-set (get-in sets-tree ["G-group1" "S-token-set-2"])
token-set' (get-in sets-tree' ["group1" "updated-name"])] token-set' (get-in sets-tree' ["G-group1" "S-updated-name"])]
(t/is (= (ctob/set-count tokens-lib') 5)) (t/is (= (ctob/set-count tokens-lib') 5))
(t/is (= (count group1') 3)) (t/is (= (count group1') 3))
(t/is (= (d/index-of (keys group1') "updated-name") 0)) (t/is (= (d/index-of (keys group1') "S-updated-name") 0))
(t/is (= (:name token-set') "group1/updated-name")) (t/is (= (:name token-set') "group1/updated-name"))
(t/is (= (:description token-set') nil)) (t/is (= (:description token-set') nil))
(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)))
sets-tree'))
(t/deftest move-set-of-group (t/deftest move-set-of-group
(let [tokens-lib (-> (ctob/make-tokens-lib) (let [tokens-lib (-> (ctob/make-tokens-lib)
@ -855,15 +847,15 @@
sets-tree (ctob/get-set-tree tokens-lib) sets-tree (ctob/get-set-tree tokens-lib)
sets-tree' (ctob/get-set-tree tokens-lib') sets-tree' (ctob/get-set-tree tokens-lib')
group1' (get sets-tree' "group1") group1' (get sets-tree' "G-group1")
group2' (get sets-tree' "group2") group2' (get sets-tree' "G-group2")
token-set (get-in sets-tree ["group1" "token-set-2"]) token-set (get-in sets-tree ["G-group1" "S-token-set-2"])
token-set' (get-in sets-tree' ["group2" "updated-name"])] token-set' (get-in sets-tree' ["G-group2" "S-updated-name"])]
(t/is (= (ctob/set-count tokens-lib') 4)) (t/is (= (ctob/set-count tokens-lib') 4))
(t/is (= (count group1') 2)) (t/is (= (count group1') 2))
(t/is (= (count group2') 1)) (t/is (= (count group2') 1))
(t/is (= (d/index-of (keys group2') "updated-name") 0)) (t/is (nil? (get group1' "S-updated-name")))
(t/is (= (:name token-set') "group2/updated-name")) (t/is (= (:name token-set') "group2/updated-name"))
(t/is (= (:description token-set') nil)) (t/is (= (:description token-set') nil))
(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)))))
@ -1139,4 +1131,3 @@
(t/is (= @with-prev-tokens-lib @tokens-lib))) (t/is (= @with-prev-tokens-lib @tokens-lib)))
(t/testing "fresh tokens library is also equal" (t/testing "fresh tokens library is also equal"
(= @with-empty-tokens-lib @tokens-lib))))))) (= @with-empty-tokens-lib @tokens-lib)))))))

View file

@ -200,13 +200,9 @@
context] context]
:as _props}] :as _props}]
(let [{:keys [editing? new? on-edit on-create on-reset] :as ctx} (or context (sets-context/use-context)) (let [{:keys [editing? new? on-edit on-create on-reset] :as ctx} (or context (sets-context/use-context))
avoid-token-set-grouping #(str/replace % "/" "-")
submit-token submit-token
#(do #(do
;; TODO: We don't support set grouping for now so we rename sets for now (on-create-token-set %)
(when (str/includes? (:name %) "/")
(warn-on-try-create-token-set-group!))
(on-create-token-set (update % :name avoid-token-set-grouping))
(on-reset))] (on-reset))]
[:ul {:class (stl/css :sets-list)} [:ul {:class (stl/css :sets-list)}
(if (and (if (and
@ -218,10 +214,7 @@
(when token-set (when token-set
(let [update-token (let [update-token
#(do #(do
;; TODO: We don't support set grouping for now so we rename sets for now (on-update-token-set (:name token-set) %)
(when (str/includes? (:name %) "/")
(warn-on-try-create-token-set-group!))
(on-update-token-set (avoid-token-set-grouping (:name token-set)) (update % :name avoid-token-set-grouping))
(on-reset))] (on-reset))]
[:& sets-tree [:& sets-tree
{:key (:name token-set) {:key (:name token-set)

View file

@ -182,8 +182,7 @@
(->> data-stream (->> data-stream
(rx/map (fn [data] (rx/map (fn [data]
(try (try
(-> (str/replace data "/" "-") ;; TODO Remove when token groups work (t/decode-str data)
(t/decode-str))
(catch js/Error e (catch js/Error e
(throw (wte/error-ex-info :error.import/json-parse-error data e)))))) (throw (wte/error-ex-info :error.import/json-parse-error data e))))))
(rx/map (fn [json-data] (rx/map (fn [json-data]