diff --git a/common/src/app/common/types/tokens_lib.cljc b/common/src/app/common/types/tokens_lib.cljc index 2ccbe68134..8cdc4609dd 100644 --- a/common/src/app/common/types/tokens_lib.cljc +++ b/common/src/app/common/types/tokens_lib.cljc @@ -23,6 +23,8 @@ ;; === Groups handling +;; TODO: add again the removed functions and refactor the rest of the module to use them + (def schema:groupable-item [:map {:title "Groupable item"} [:name :string]]) @@ -51,21 +53,6 @@ [path separator] (str/join separator path)) -(defn group-item - "Add a group to the item name, in the form group.name." - [item group-name separator] - (assert (valid-groupable-item? item) "expected groupable item") - (update item :name #(dm/str group-name separator %))) - -(defn ungroup-item - "Remove the first group from the item name." - [item separator] - (assert (valid-groupable-item? item) "expected groupable item") - (update item :name #(-> % - (split-path separator) - (rest) - (join-path separator)))) - (defn get-path "Get the path of object by specified separator (E.g. with '.' separator, the 'group.subgroup.name' -> ['group' 'subgroup'])" @@ -74,33 +61,6 @@ (->> (split-path (:name item) separator) (not-empty))) -(defn get-groups-str - "Get the groups part of the name. E.g. group.subgroup.name -> group.subgroup" - [item separator] - (-> (get-path item separator) - (butlast) - (join-path separator))) - -(defn get-final-name - "Get the final part of the name. E.g. group.subgroup.name -> name" - [item separator] - (assert (valid-groupable-item? item) "expected groupable item") - (-> (:name item) - (split-path separator) - (last))) - -(defn group? - "Check if a node of the grouping tree is a group or a final item." - [item] - (d/ordered-map? item)) - -(defn get-children - "Get all children of a group of a grouping tree. Each child is - a tuple [name item], where item " - [group] - (assert (group? group) "expected group node") - (seq group)) - ;; === Token (def token-separator ".") @@ -175,13 +135,6 @@ tokens)] (group-by :type tokens'))) -(defn filter-by-type [token-type tokens] - (let [token-type? #(= token-type (:type %))] - (cond - (d/ordered-map? tokens) (into (d/ordered-map) (filter (comp token-type? val) tokens)) - (map? tokens) (into {} (filter (comp token-type? val) tokens)) - :else (filter token-type? tokens)))) - ;; === Token Set (def set-prefix "S-") @@ -190,10 +143,6 @@ (def set-separator "/") -(defn join-set-path-str [& args] - (->> (filter some? args) - (str/join set-separator))) - (defn join-set-path [path] (join-path path set-separator)) @@ -233,27 +182,9 @@ (-> (set-group-path->set-group-prefixed-path path) (join-set-path))) -(defn split-set-prefix [set-path] - (some->> set-path - (re-matches #"^([SG]-)(.*)") - (rest))) - -(defn add-set-prefix [set-name] - (str set-prefix set-name)) - (defn add-set-group-prefix [group-path] (str set-group-prefix group-path)) -(defn add-token-set-paths-prefix - "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 get-token-set-path [token-set] (get-path token-set set-separator)) @@ -275,12 +206,6 @@ (split-token-set-name name)) (str/join set-separator)))) -;; FIXME: revisit -(defn get-token-set-final-name - [name] - (-> (split-token-set-name name) - (peek))) - (defn set-name->prefixed-full-path [name-str] (-> (split-token-set-name name-str) (set-full-path->set-prefixed-full-path))) @@ -328,9 +253,7 @@ (assoc-in [:ids temp-id] token)))) {:tokens-tree {} :ids {}} tokens)) - (defprotocol ITokenSet - (update-name [_ set-name] "change a token set name while keeping the path") (add-token [_ token] "add a token at the end of the list") (update-token [_ token-name f] "update a token in the list") (delete-token [_ token-name] "delete a token from the list") @@ -339,16 +262,6 @@ (defrecord TokenSet [id name description modified-at tokens] ITokenSet - (update-name [_ set-name] - (TokenSet. id - (-> (split-token-set-name name) - (drop-last) - (concat [set-name]) - (join-set-path)) - description - (dt/now) - tokens)) - (add-token [_ token] (let [token (check-token token)] (TokenSet. id @@ -424,9 +337,6 @@ (sm/register! ::token-set schema:token-set) -(def valid-token-set? - (sm/validator schema:token-set)) - (def check-token-set (sm/check-fn schema:token-set :hint "expected valid token set")) @@ -469,15 +379,12 @@ (move-set-group [_ from-path to-path before-path before-group?] "Move token set group at `from-path` to `to-path` and order it before `before-path` with `before-group?`.") (set-count [_] "get the total number if sets in the library") (get-set-tree [_] "get a nested tree of all sets in the library") - (get-in-set-tree [_ path] "get `path` in nested tree of all sets in the library") (get-sets [_] "get an ordered sequence of all sets in the library") - (get-path-sets [_ path] "get an ordered sequence of sets at `path` in the library") (get-sets-at-prefix-path [_ prefixed-path-str] "get an ordered sequence of sets at `prefixed-path-str` in the library") (get-sets-at-path [_ path-str] "get an ordered sequence of sets at `path` in the library") (rename-set-group [_ from-path-str to-path-str] "renames set groups and all child set names from `from-path-str` to `to-path-str`") (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-neighbor-set-name [_ set-name index-offset] "get neighboring set name offset by `index-offset`")) + (get-set [_ set-name] "get one set looking for name")) (def schema:token-set-node [:schema {:registry {::node [:or ::token-set @@ -598,11 +505,6 @@ (sm/required-keys schema:token-theme-attrs) [:fn token-theme?]]) -(sm/register! ::token-theme schema:token-theme) - -(def valid-token-theme? - (sm/validator schema:token-theme)) - (def check-token-theme (sm/check-fn schema:token-theme :hint "expected a valid token-theme")) @@ -674,7 +576,6 @@ (def valid-active-token-themes? (sm/validator schema:active-themes)) -;; DEPRECATED (defn walk-sets-tree-seq "Walk sets tree as a flat list. @@ -861,8 +762,6 @@ Will return a value that matches this schema: active-themes))) this))) - - (delete-set [_ set-name] (let [prefixed-path (set-name->prefixed-full-path set-name)] (TokensLib. (d/dissoc-in sets prefixed-path) @@ -999,18 +898,10 @@ Will return a value that matches this schema: (get-set-tree [_] sets) - (get-in-set-tree [_ path] - (get-in sets path)) - (get-sets [_] (->> (tree-seq d/ordered-map? vals sets) (filter (partial instance? TokenSet)))) - (get-path-sets [_ name] - (some->> (get-in sets (split-token-set-name name)) - (tree-seq d/ordered-map? vals) - (filter (partial instance? TokenSet)))) - (get-sets-at-prefix-path [_ prefixed-path-str] (some->> (get-in sets (split-token-set-name prefixed-path-str)) (tree-seq d/ordered-map? vals) @@ -1043,13 +934,6 @@ Will return a value that matches this schema: (let [path (set-name->prefixed-full-path set-name)] (get-in sets path))) - (get-neighbor-set-name [this set-name index-offset] - (let [sets (get-ordered-set-names this) - index (d/index-of sets set-name) - neighbor-set-name (when index - (nth sets (+ index-offset index) nil))] - neighbor-set-name)) - ITokenThemes (add-theme [_ token-theme] (let [token-theme (check-token-theme token-theme)] @@ -1208,7 +1092,6 @@ Will return a value that matches this schema: (into tokens' (map (fn [x] [(:name x) x]) (get-tokens set)))) {} (get-sets this))) - ;; FIXME: revisit if this still necessary (validate [_] (and (valid-token-sets? sets) (valid-token-themes? themes) @@ -1242,11 +1125,6 @@ Will return a value that matches this schema: data (d/oassoc data hidden-token-theme-name (make-hidden-token-theme)))))) -;; NOTE: is possible that ordered map is not the most apropriate -;; data structure and maybe we need a specific that allows us an -;; easy way to reorder it, or just store inside Tokens data -;; structure the data and the order separately as we already do -;; with pages and pages-index. (defn make-tokens-lib "Create an empty or prepopulated tokens library." [& {:keys [sets themes active-themes]}] @@ -1558,7 +1436,7 @@ Will return a value that matches this schema: (tree-seq d/ordered-map? vals) (into [] themes-xform)) - ;; Active themes without exposing hidden penpot theme + ;; Active themes without exposing hidden penpot theme active-themes-clear (-> (get-active-theme-paths tokens-lib) (disj hidden-token-theme-path)) diff --git a/common/test/common_tests/types/tokens_lib_test.cljc b/common/test/common_tests/types/tokens_lib_test.cljc index d113cfdffd..c354d5625f 100644 --- a/common/test/common_tests/types/tokens_lib_test.cljc +++ b/common/test/common_tests/types/tokens_lib_test.cljc @@ -872,35 +872,6 @@ (t/is (= (second path) "subgroup")) (t/is (= (nth path 2) "name")))) -(t/deftest group-and-ungroup-token-set - (let [token-set1 (ctob/make-token-set :name "token-set1") - token-set2 (ctob/make-token-set :name "some group/token-set2") - - token-set1' (ctob/group-item token-set1 "big group" "/") - token-set2' (ctob/group-item token-set2 "big group" "/") - token-set1'' (ctob/ungroup-item token-set1' "/") - token-set2'' (ctob/ungroup-item token-set2' "/")] - (t/is (= (:name token-set1') "big group/token-set1")) - (t/is (= (:name token-set2') "big group/some group/token-set2")) - (t/is (= (:name token-set1'') "token-set1")) - (t/is (= (:name token-set2'') "some group/token-set2")))) - -(t/deftest get-token-set-groups-str - (let [token-set1 (ctob/make-token-set :name "token-set1") - token-set2 (ctob/make-token-set :name "some-group/token-set2") - token-set3 (ctob/make-token-set :name "some-group/some-subgroup/token-set3")] - (t/is (= (ctob/get-groups-str token-set1 "/") "")) - (t/is (= (ctob/get-groups-str token-set2 "/") "some-group")) - (t/is (= (ctob/get-groups-str token-set3 "/") "some-group/some-subgroup")))) - -(t/deftest get-token-set-final-name - (let [token-set1 (ctob/make-token-set :name "token-set1") - token-set2 (ctob/make-token-set :name "some-group/token-set2") - token-set3 (ctob/make-token-set :name "some-group/some-subgroup/token-set3")] - (t/is (= (ctob/get-final-name token-set1 "/") "token-set1")) - (t/is (= (ctob/get-final-name token-set2 "/") "token-set2")) - (t/is (= (ctob/get-final-name token-set3 "/") "token-set3")))) - (t/deftest add-tokens-in-set (let [tokens-lib (-> (ctob/make-tokens-lib) (ctob/add-set (ctob/make-token-set :name "test-token-set")) @@ -1062,65 +1033,6 @@ (t/is (nil? token')) (t/is (dt/is-after? (:modified-at token-set') (:modified-at token-set))))) -(t/deftest add-token-set-with-groups - (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 "group1/token-set-2")) - (ctob/add-set (ctob/make-token-set :name "group1/token-set-3")) - (ctob/add-set (ctob/make-token-set :name "group1/subgroup11/token-set-4")) - (ctob/add-set (ctob/make-token-set :name "group2/token-set-5"))) - - sets-list (ctob/get-sets tokens-lib) - - sets-tree (ctob/get-set-tree tokens-lib) - - [node-set1 node-group1 node-group2] - (ctob/get-children sets-tree) - - [node-set2 node-set3 node-subgroup11] - (ctob/get-children (second node-group1)) - - [node-set4] - (ctob/get-children (second node-subgroup11)) - - [node-set5] - (ctob/get-children (second node-group2))] - - (t/is (= (count sets-list) 5)) - (t/is (= (:name (nth sets-list 0)) "token-set-1")) - (t/is (= (:name (nth sets-list 1)) "group1/token-set-2")) - (t/is (= (:name (nth sets-list 2)) "group1/token-set-3")) - (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 (= (first node-set1) "S-token-set-1")) - (t/is (= (ctob/group? (second node-set1)) false)) - (t/is (= (:name (second node-set1)) "token-set-1")) - - (t/is (= (first node-group1) "G-group1")) - (t/is (= (ctob/group? (second node-group1)) true)) - (t/is (= (count (second node-group1)) 3)) - - (t/is (= (first node-set2) "S-token-set-2")) - (t/is (= (ctob/group? (second node-set2)) false)) - (t/is (= (:name (second node-set2)) "group1/token-set-2")) - - (t/is (= (first node-set3) "S-token-set-3")) - (t/is (= (ctob/group? (second node-set3)) false)) - (t/is (= (:name (second node-set3)) "group1/token-set-3")) - - (t/is (= (first node-subgroup11) "G-subgroup11")) - (t/is (= (ctob/group? (second node-subgroup11)) true)) - (t/is (= (count (second node-subgroup11)) 1)) - - (t/is (= (first node-set4) "S-token-set-4")) - (t/is (= (ctob/group? (second node-set4)) false)) - (t/is (= (:name (second node-set4)) "group1/subgroup11/token-set-4")) - - (t/is (= (first node-set5) "S-token-set-5")) - (t/is (= (ctob/group? (second node-set5)) false)) - (t/is (= (:name (second node-set5)) "group2/token-set-5")))) - (t/deftest update-token-set-in-groups (let [tokens-lib (-> (ctob/make-tokens-lib) (ctob/add-set (ctob/make-token-set :name "token-set-1")) @@ -1218,68 +1130,6 @@ (t/is (= (count sets-tree') 1)) (t/is (nil? token-set')))) -(t/deftest add-theme-with-groups - (let [tokens-lib (-> (ctob/make-tokens-lib) - (ctob/add-theme (ctob/make-token-theme :group "" :name "token-theme-1")) - (ctob/add-theme (ctob/make-token-theme :group "group1" :name "token-theme-2")) - (ctob/add-theme (ctob/make-token-theme :group "group1" :name "token-theme-3")) - (ctob/add-theme (ctob/make-token-theme :group "group2" :name "token-theme-4"))) - - themes-list (ctob/get-themes tokens-lib) - - themes-tree (ctob/get-theme-tree tokens-lib) - - [node-group0 node-group1 node-group2] - (ctob/get-children themes-tree) - - [hidden-theme node-theme1] - (ctob/get-children (second node-group0)) - - [node-theme2 node-theme3] - (ctob/get-children (second node-group1)) - - [node-theme4] - (ctob/get-children (second node-group2))] - - (t/is (= (count themes-list) 5)) - (t/is (= (:name (nth themes-list 0)) "__PENPOT__HIDDEN__TOKEN__THEME__")) - (t/is (= (:name (nth themes-list 1)) "token-theme-1")) - (t/is (= (:name (nth themes-list 2)) "token-theme-2")) - (t/is (= (:name (nth themes-list 3)) "token-theme-3")) - (t/is (= (:name (nth themes-list 4)) "token-theme-4")) - (t/is (= (:group (nth themes-list 1)) "")) - (t/is (= (:group (nth themes-list 2)) "group1")) - (t/is (= (:group (nth themes-list 3)) "group1")) - (t/is (= (:group (nth themes-list 4)) "group2")) - - (t/is (= (first node-group0) "")) - (t/is (= (ctob/group? (second node-group0)) true)) - (t/is (= (count (second node-group0)) 2)) - - (t/is (= (first hidden-theme) "__PENPOT__HIDDEN__TOKEN__THEME__")) - (t/is (= (ctob/group? (second hidden-theme)) false)) - (t/is (= (:name (second hidden-theme)) "__PENPOT__HIDDEN__TOKEN__THEME__")) - - (t/is (= (first node-theme1) "token-theme-1")) - (t/is (= (ctob/group? (second node-theme1)) false)) - (t/is (= (:name (second node-theme1)) "token-theme-1")) - - (t/is (= (first node-group1) "group1")) - (t/is (= (ctob/group? (second node-group1)) true)) - (t/is (= (count (second node-group1)) 2)) - - (t/is (= (first node-theme2) "token-theme-2")) - (t/is (= (ctob/group? (second node-theme2)) false)) - (t/is (= (:name (second node-theme2)) "token-theme-2")) - - (t/is (= (first node-theme3) "token-theme-3")) - (t/is (= (ctob/group? (second node-theme3)) false)) - (t/is (= (:name (second node-theme3)) "token-theme-3")) - - (t/is (= (first node-theme4) "token-theme-4")) - (t/is (= (ctob/group? (second node-theme4)) false)) - (t/is (= (:name (second node-theme4)) "token-theme-4")))) - (t/deftest update-token-theme-in-groups (let [tokens-lib (-> (ctob/make-tokens-lib) (ctob/add-theme (ctob/make-token-theme :group "" :name "token-theme-1"))